PM is a good choice for this project. It's got the discussion of the best solution(s) to the question. Hopefully someone jumps in an offers a better solution than mine. It feels a little ugly explicitly stating the indices.
Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj))
First, what's a
Cauchy matrix? Ahh, this question is just how to construct a matrix from 2 arrays, where no elements from one array are in the other. Just create a sequence of number for the first array and then make the second array 0.5 more than the first array to get the inputs. Here's a brute force method.
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;
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.
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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.