Alec Mihailovs

Dr. Aleksandrs Mihailovs

4470 Reputation

21 Badges

20 years, 13 days
Mihailovs, Inc.
Owner, President, and CEO
Tyngsboro, Massachusetts, United States

Social Networks and Content at Maplesoft.com

I received my Ph.D. from the University of Pennsylvania in 1998 and I have been teaching since then at SUNY Oneonta for 1 year, at Shepherd University for 5 years, at Tennessee Tech for 2 years, at Lane College for 1 year, and this year I taught at the University of Massachusetts Lowell. My research interests include Representation Theory and Combinatorics.

MaplePrimes Activity


These are answers submitted by Alec Mihailovs

seq

See quicksort procedure, for example, which can be easily modified for your purposes as

qsort := proc(l::list)
    local lt, ge;
    if l=[] then []
        else
        lt,ge := selectremove(evalf@`<`, l[2..], l[1]);
        [qsort(ge)[], l[1], qsort(lt)[]]
    fi
end:

L:=[-1,10,2/3,sqrt(2)]:
qsort(L);

                               1/2
                         [10, 2   , 2/3, -1]

Or, you could do something like

mysort:=l->ListTools:-Reverse(
    [{map(x->[evalhf(x),x],l)[]}[]][..,2]):

mysort(L);

                               1/2
                         [10, 2   , 2/3, -1]

Alec

You need with(LinearAlgebra); for that, or call it as LinearAlgebra:-SingularValues.

Also, evalf is not necessary - with it or without, LinearAlgebra will do evalhf itself.

And instead of S-1 it should be something different,

U,S,Vt:=LinearAlgebra:-SingularValues(A,output=['U','S','Vt']):
Vt^%T.Matrix(3,6,LinearAlgebra:-DiagonalMatrix(S[1..3])^(-1)).U^%T.b;

                        [2.63293929474354194]
                        [                   ]
                        [3.66652382290048262]
                        [                   ]
                        [5.20251471911851393]

Alec

One can use define

define( '`&*`', 'flat', 'multilinear', 
    `&*`(X::name^n::algebraic,X::name^m::algebraic)=X^(n+m),
    'identity' = 1 );
(1-A)&*(1+A+A^2);
                                     3
                                1 - A

Alec

It is faster without SVD, using (ATA)-1AT for the pseudoinverse,

(A^%T.A)^(-1).A^%T.b;

                              [918543 ]
                              [------ ]
                              [348866 ]
                              [       ]
                              [2558251]
                              [-------]
                              [697732 ]
                              [       ]
                              [3629961]
                              [-------]
                              [697732 ]

evalhf(%);

                        [2.63293929474354061]
                        [                   ]
                        [3.66652382290048306]
                        [                   ]
                        [5.20251471911851571]

Alec

For example,

sort(select(a->a[1]=6,a))[-1];

                               [6, 12]

Or

[6,max(select(a->a[1]=6,a)[..,2])];

                               [6, 12]

Alec

The puzzle worked OK for me.

Also, map can be used instead of seq,

assign(map(`=`,L,Array(1..16)));

And ~ can be used,

assign(L=~Array~([(1..16)$3]));

Alec

It is in the kernel,

rtable_zip(`/`,Array([3],datatype=integer[4]),Array([9],datatype=integer[4]));
                           
                        [0.333333333333333315]

Alec

By the way, that reminded me of an old trick switching values of 2 variables without introducing another one, i.e. not doing c:=a; a:=b; b:=c. 

It is either a:=a+b; b:=a-b; a:=a-b, or the same with bitwise xor instead of + and - (which avoids the overflowing problem.)

Using that for switching 2 rows may be not faster, but saves some memory.

Alec

The expected values in the 3rd and 4th columns can be calculated as

L:=[0$3,1$7,2$2]:
[seq(convert(convert~(select(x->convert(x[1..5],`+`)=i,
    combinat:-permute(L))[..,6..10],`+`),`+`),i=2..7)]*
    792000/combinat:-numbperm(L);

          [135000, 680000, 1160000, 1080000, 475000, 100000]

evalf(%/~(map2(numboccur,convert~(combinat:-permute(L)[..,1..5],`+`), [$2..7])*
    792000/combinat:-numbperm(L)));

  [6.428571429, 5.714285714, 5., 4.285714286, 3.571428571,

        2.857142857]

Also pretty close to the simulated results.

Alec

Also, Linear Algebra:-Modular provides in-place shuffling, 

with(LinearAlgebra:-Modular):
A:=Mod(37,[[11,12,13,14,15],[21,22,23,24,25], 
    [31,32,33,34,35]],integer[]);

                       [11    12    13    14    15]
                       [                          ]
                  A := [21    22    23    24    25]
                       [                          ]
                       [31    32    33    34    35]

S:=Vector~([[],[2],[3],[1,3],[2,3],[3,3]],
    datatype=integer[4]):
r:=rand(1..6):
Permute(37,S[r()],A,true,false);
A;

                     [21    22    23    24    25]
                     [                          ]
                     [31    32    33    34    35]
                     [                          ]
                     [11    12    13    14    15]

t:=time():
to 1000000 do Permute(37,S[r()],A,true,false) od; 
time()-t;

                                3.884

Million random row permutations in less than 4 seconds.

Alec

Well, one can do something like

globals:="g1,g2,gn":
myproc:=(a,l,s)->parse(cat("proc(",a,") local ", l, 
    ";global ",globals, ";", s, " end")):
a:=myproc("b,c","e,f","g1:=1; g2:=2; gn:=NULL");

  a := proc(b, c)
      local e, f;
      global g1, g2, gn;
      g1 := 1; g2 := 2; gn := NULL
  end proc

a();
g1,g2,gn;

                                 1, 2

Alec

NAG libraries have routines for in-place random shuffling of rows or columns of Arrays with hardware datatypes. If they are accessible in Maple 14 (which I can't check because I don't have it), as it is said in the Maple 14 announcement, it should be possible just to call them using external calling.

Alec

save cat(Mickey_,i), cat("./Mickey_",i,".txt")

Alec

The convergence in the first example can be visualized as follows,

x[1] := 2.30: 
for n from 2 to 10 do x[n] := 9*sqrt(x[n-1]+6) od: 
S := [[x[1], 0], seq(`if`(i::odd, [x[i], x[i+1]], 
    [x[i+1], x[i]]), i = 1 .. 9)]: 
plots:-display( plot(9*sqrt(x+6), x = 0 .. 90), 
    plot([9*sqrt(x+6), x, x = 0 .. 90]), 
    plot(S, color = green));

135_visual.png

Alec

First 7 8 9 10 11 12 13 Last Page 9 of 76