Mercurial > octave
comparison scripts/special-matrix/hilb.m @ 28520:627da618dcc4 stable
hilb.m: Speed-up function using broadcasting.
* scripts/special-matrix/hilb.m: Speed-up function using broadcasting. For more
information read https://octave.discourse.group/t/boosting-hilb/48 .
author | Kai T. Ohlhus <k.ohlhus@gmail.com> |
---|---|
date | Wed, 01 Jul 2020 16:03:41 +0900 |
parents | bd51beb6205e |
children | d8318c12d903 0a5b15007766 |
comparison
equal
deleted
inserted
replaced
28515:49f4d7814760 | 28520:627da618dcc4 |
---|---|
64 print_usage (); | 64 print_usage (); |
65 elseif (! isscalar (n)) | 65 elseif (! isscalar (n)) |
66 error ("hilb: N must be a scalar integer"); | 66 error ("hilb: N must be a scalar integer"); |
67 endif | 67 endif |
68 | 68 |
69 retval = zeros (n); | 69 ## Very elegant solution by N. Higham |
70 tmp = 1:n; | 70 ## https://nhigham.com/2020/06/30/what-is-the-hilbert-matrix/ |
71 for i = 1:n | 71 j = 1:n; |
72 retval(i, :) = 1.0 ./ tmp; | 72 retval = 1 ./ (j' + j - 1); |
73 tmp += 1; | |
74 endfor | |
75 | 73 |
76 endfunction | 74 endfunction |
77 | 75 |
78 | 76 |
79 %!assert (hilb (2), [1, 1/2; 1/2, 1/3]) | 77 %!assert (hilb (2), [1, 1/2; 1/2, 1/3]) |