This is the kind of problem for which
PDL was created. I have only just started learning PDL, so my contrib below is going to be naive, but here is how I would do it
# a [3 x 100K] piddle
$a = sequence 3, 100_000
[
[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
..
]
# create an extra [1 x 100K] piddle to hold the norms
$b = zeros 1, 100_1000
[
[0]
[0]
[0]
..
]
# append $b to $a
$c = $a->append($b)
[
[ 0 1 2 0]
[ 3 4 5 0]
[ 6 7 8 0]
..
]
# get the slices to the different cols
$col1 = $c->slice('0,:')
$col2 = $c->slice('1,:')
$col4 = $c->slice('3,:')
# calc the norms
$col4 .= (($col1 ** 2) + ($col2 ** 2)) ** 0.5
[
[ 0 1 2 1]
[ 3 4 5 5]
[ 6 7 8 9.2195445]
..
]
# or, do it all in one line
$b = $a->append(zeros 1, 100_000)
$norm = $b->slice('3,:')
$norm .= (($b->slice('0,:') ** 2) + ($b->slice('1,:') ** 2)) ** 0.5
I am sure PDL vets would improve the above many different ways, however, PDL is ideally suited for the kind of problem you are posing.
--
when small people start casting long shadows, it is time to go to bed
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.