use Math::Cephes::Matrix qw(mat); \$a = mat [[1, 2], [3, 4]]; \$b = [1, 1]; print "@{\$a->mul(\$b)}"; # print "3 7" ##```## use Math::MatrixReal; \$a = Math::MatrixReal->new_from_rows( [[1, 2], [3, 4]] ); \$b = Math::MatrixReal->new_from_cols( [ [1, 1] ] ); print \$a*\$b; # print "[ 3.000000000000E+000 ] # [ 7.000000000000E+000 ]" ##``````## use PDL; \$a = pdl [[1, 2],[3, 4]]; \$b = pdl [[1], [1]]; print \$a x \$b; # print "[ # [3] # [7] # ]" ##``````## >> a = [1, 2; 3, 4]; >> b = [1; 1]; >> a*b ans = 3 7 ##``````## > a = matrix(c(1,2,3,4), ncol=2, byrow=T) > b = matrix(c(1,1), ncol=1) > a%*%b [,1] [1,] 3 [2,] 7 ##``````## use Math::Cephes::Matrix qw(mat); \$mat = mat [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; \$mat = \$mat->coef; for my \$i (1..2) { # 0 first index print "@{\$mat->[\$i]}[1..2]\n"; } # print "5 6 # 8 9" ##``````## use Math::MatrixReal; \$mat = Math::MatrixReal->new_from_rows( [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ); for my \$i (2..3) { # 1 first index for my \$j (2..3) { print \$mat->element(\$i, \$j), " "; } print "\n"; } # print "5 6 # 8 9" ##``````## use PDL; \$mat = pdl [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; print \$mat->slice("1:2,1:2"); # 0 first index # print "[ # [5 6] # [8 9] # ]" ##``````## mat = [1,2,3; 4,5,6; 7,8,9]; mat(2:3, 2:3) ans = 5 6 8 9 ##``````## > mat = matrix(1:9, ncol=3, byrow=T) > mat[2:3, 2:3] [,1] [,2] [1,] 5 6 [2,] 8 9 ##``````## > vec = 1:10 > vec [1] 1 2 3 4 5 6 7 8 9 10 > vec %% 2 [1] 1 0 1 0 1 0 1 0 1 0 > vec[vec %% 2 == 1] [1] 1 3 5 7 9 > vec[vec %% 2 == 1] + 1 [1] 2 4 6 8 10 ##``````## #! /usr/local/bin/perl -w use strict; # ----------------------------------------------------------- # RSA algorithm -- assymetrical\public-key cryptography # ----------------------------------------------------------- use Math::Pari qw(gcd PARI) ; # ----------------------------------------------------------- # m -- message my \$m = 'Perl' ; print "original: \$m\n" ; my \$tmpl = 'C*' ; my @m = unpack(\$tmpl, \$m) ; # string -> unsigned char values print "coded: @m\n" ; # n = pq -- in RSA, p & q = prime, each 1024 bits/308 digits long my \$p = PARI("prime(".int(rand 50).")") ; my \$q = PARI("prime(".int(rand 50).")") ; my \$n = \$p*\$q ; # \$n = Pari's obj # choose a random number r, s.t. # 1 < r < (p-1)(q-1) = b # gcd(r, b) = 1 -- relative prime my \$b = (\$p-1)*(\$q-1) ; my \$r ; do {\$r = int rand \$b ; } until (gcd(\$r,\$b) == 1) ; \$r = PARI \$r ; # rk = 1 mod (p-1)(q-1) -- k = private key; (n, r) public my \$k = (1/\$r)%\$b ; # Pari's math operators, since vars = Pari # encrypt -- c = (m ^ r) mod n my @c ; map { \$c[\$_] = (\$m[\$_]**\$r)%\$n } 0..\$#m ; # Perl: ** for power print "ciphered: @c\n" ; # decrypt -- m = (c ^ k) mod n my @d ; map { \$d[\$_] = PARI("(\$c[\$_]^\$k)%\$n") } 0..\$#c ; # Pari: ^ for power print "deciphered: @d\n" ; print "decoded: " . pack(\$tmpl, @d) . "\n" ; __END__ original: Perl coded: 80 101 114 108 ciphered: 18431 6512 5843 7236 deciphered: 80 101 114 108 decoded: Perl ##``````## #! /usr/local/bin/perl -w use strict ; R("getwd()"); sub R { my \$Rpath = "C:\\R\\rw\\bin\\" ; my \$Rcmd = \$Rpath . "rterm --vanilla --quiet --slave" ; my \$Rscript = shift ; \$Rscript =~ s/(\r|;\r)/ ;/gm ; \$Rscript =~ s/<-/=/gm ; # \r or <- will break "echo" return `echo \$Rscript | \$Rcmd` ; } ##``````## my \$dir = 'D:\\tmp\\prog\\math\\lp32'; my \$lp_solve = "d:\\mydir\\lp_solve.exe"; my \$lpfile = "d:\\mydir\\model.lp"; (my \$lp = << " EOF") =~ s/^\s+//gm; min: 8 x1 + 15 x2 ; c1: 10 x1 + 21 x2 > 156 ; c2: 2 x1 + x2 > 22 ; EOF open OUTFILE, "+>\$lpfile"; print OUTFILE \$lp; close OUTFILE; \$output = qx(\$lp_solve < \$lpfile); \$output =~ s/\r//gm; print \$lp; print \$output; system("del \$lpfile"); ##``````## use strict; use warnings; my (\$a1,\$a2,\$a3,\$ref); for my \$x (0..20) { for my \$y (0..20) { \$ref->[\$x][\$y] = rand 100; } } use Math::Cephes::Matrix qw(mat); \$a1 = mat \$ref; use Math::MatrixReal; \$a2 = Math::MatrixReal->new_from_rows( \$ref ); use PDL; use PDL::Slatec; \$a3 = pdl \$ref; use Benchmark qw(cmpthese); cmpthese(100, { cephes=>sub{\$a1->inv()}, matrixreal=>sub{\$a2->inverse()}, pdl=>sub{matinv(\$a3)} } ); __END__ Rate matrixreal cephes pdl matrixreal 5.28/s -- -94% -99% cephes 83.3/s 1479% -- -87% pdl 625/s 11744% 650% -- ```