use strict;
use warnings;
use Math::Complex;
my @list = ( cplx(1, 8), 2 + 5*i );
print "List: @list\n";
foreach (@list) {
print "$_\t-> re=", Re( $_ ), ", im=", Im( $_ ), "\n";
}
Output:
List: 1+8i 2+5i
1+8i -> re=1, im=8
2+5i -> re=2, im=5
| [reply] [d/l] [select] |
Thanks... I guess I was just doing something stupid. My data was in a single column, so I tried this...Thanks... I guess I was just doing something stupid. My data was in a single column, so I tried this... use Math::Complex;
my @array = qw(
1+i -1+i -1-i 1-i 3 3*i -3 -3*i 1+2*i 2+i -1+2*i
.
.
.
And, it doesn't work with the qw or without the qw. Without the qw I got zillions of errors, with the qw the Re portion was the whole thing and the there were no imaginary portions. I guess the complex constructor would not be called if the array element is considered to be a string...
So, I just change the delimiter for my data from a new line to a comma-space, like you have, and it now works...
use strict; use warnings;
use Math::Complex;
my @list = (1+i, -1+i, -1-i, 1-i, 3, 3*i, -3, -3*i, 1+2*i, 2+i, -1+2*i, -2+i, -2-i, -1-2*i, 1-2*i, 2-i, 7, 7*i, -7, -7*i, 11, 11*i, -11, -11*i, 2+3*i, 3+2*i, -2+3*i, -3+2*i, -2-3*i, -3-2*i, 2-3*i, 3-2*i, 1+4*i, 4+i, -1+4*i, -4+i, -1-4*i, -4-i, 1-4*i, 4-i, 19, 19*i, -19, -19*i, 23, 23*i, -23, -23*i, 2+5*i, 5+2*i, -2+5*i, -5+2*i, -2-5*i, -5-2*i, 2-5*i, 5-2*i, 31, 31*i, -31, -31*i, 1+6*i, 6+i, -1+6*i, -6+i, -1-6*i, -6-i, 1-6*i, 6-i, 4+5*i, 5+4*i, -4+5*i, -5+4*i, -4-5*i, -5-4*i, 4-5*i, 5-4*i, 43, 43*i, -43, -43*i, 47, 47*i, -47, -47*i);
print "List: @list\n";
foreach (@list) { print "$_\t-> re=", Re( $_ ), ", im=", Im( $_ ), "\n"; }
Thanks all of you for the help. I noticed that some of you are using Perl 6... I think 'say' is Perl 6? I'm using Perl 5 with Padre on DWIM Perl on Windows. My experience with Perl on Windows is difficult... Active Perl is a pain because so many of the Perl modules that I want can't be had, or cost a lot of money for a subscription. And Strawberry Perl's a pain when someone who's NOT a Perl Monk has to figure out how to use a module that you've downloaded from CPAN. So, DWIM Perl is easier for some of these things, and it comes with Padre. However, it's Perl 5... is there a relatively easy way a NON-Perl Monk can upgrade the Perl that DWIM Perl and Padre are using to Perl 6? Thanks again, Paul
| [reply] |
I think 'say' is Perl 6?
It's also available in perl 5 if you request it:
C:\>perl -e "say 'ok';"
String found where operator expected at -e line 1, near "say 'ok'"
(Do you need to predeclare say?)
syntax error at -e line 1, near "say 'ok'"
Execution of -e aborted due to compilation errors.
C:\>perl -Mfeature="say" -e "say 'ok';"
ok
C:\>perl -E "say 'ok';"
ok
C:\
And Strawberry Perl's a pain when someone who's NOT a Perl Monk has to figure out how to use a module that you've downloaded from CPAN
That's something that most of us had to go through - and it gets easier with practice and experience.
is there a relatively easy way a NON-Perl Monk can upgrade the Perl that DWIM Perl and Padre are using to Perl 6?
No, perl 6 is quite different to perl 5. You can run perl 5 code/modules on perl 6 (using Inline::Perl5) - if that's what you are seeking.
Last time I tried Inline::Perl5, it was quite good on Linux, but pretty crap on Windows.
Cheers, Rob | [reply] [d/l] |
"I noticed that some of you are using Perl 6... I think 'say is Perl 6?"
It can be Perl 6, but it is also Perl 5, for any version of Perl from December 2007, onward (Perl 5.10 or newer). The key is to enable that keyword using the feature pragma:
use feature 'say';
Or to enable groups of features for a given Perl version by using feature bundles or specifying a Perl version number (again, see feature for an explanation).
At any rate, none of the examples I've seen so far are in Perl 6. Unless someone submits a Perl 6 solution after I post this, all of the solutions here should all work with DWIM Perl, or any other modern version of Perl.
| [reply] [d/l] |
You didn't really show us what you've tried and how it's failing, so about the best we can do is demonstrate some techniques for creating a list of complex numbers given some input list. Here are a few:
use strict;
use warnings;
use feature 'say';
use Math::Complex;
my @array = map {cplx($_->[0], $_->[1])} (
[1, 8],
[2, 5],
[6, 4],
);
say for @array;
The output will be:
1+8i
2+5i
6+4i
You could also use List::Util's pairmap:
use strict;
use warnings;
use feature 'say';
use Math::Complex;
use List::Util 'pairmap';
my @array = pairmap {cplx($a, $b)} (1, 8, 2, 5, 6, 4);
say for @array;
This produces the same output.
Per the documentation for Math::Complex, cplx() is approximately synonymous with Math::Complex->make(), so this would also work:
use strict;
use warnings;
use feature 'say';
use Math::Complex;
my @array;
push @array, Math::Complex->make($_->[0], $_->[1])
for ([1,8], [2,5], [6,4]);
say for @array;
And the output will be the same.
| [reply] [d/l] [select] |