in reply to RFC: 100 PDL Exercises (ported from numpy)
I like the PDL::NiceSlice for indexing. It makes sense to me. I could have also created the matrix with my $C = outer($x, $y); or gotten the size of the arrays with $x->getdim(0) and if I grokked threading rather than just skimming PDL::Threading, this might look way cooler.use PDL; use PDL::NiceSlice; my $x = sequence(8); my $y = $x + 0.5; my ($nx, $ny) = (nelem($x), nelem($y)); my $C = zeroes($nx, $ny); for (my $i = 0; $i < $nx; $i++) { for (my $j = 0; $j < $ny; $j++) { $C($i,$j) .= 1/($x($i) - $y($j)); } } print $C;
NB the ".=" in the assignment breaks the link between the matrix and the 2 arrays. It's important.
That was just the Cauchy matrix. Some people want the Cauchy determinant (as long as the 2 arrays are the same size). Easy! Just import PDL::MatrixOps
use PDL::MatrixOps; print det $C;
Sometimes I can think of 6 impossible LDAP attributes before breakfast.
YAPC::Europe::2018 — Hmmm, need to talk to work about sending me.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: 47. Construct the Cauchy matrix
by Ea (Chaplain) on Feb 20, 2020 at 18:03 UTC | |
by etj (Priest) on Mar 16, 2022 at 21:05 UTC |