dharr

Dr. David Harrington

8482 Reputation

22 Badges

21 years, 33 days
University of Victoria
Professor or university staff
Victoria, British Columbia, Canada

Social Networks and Content at Maplesoft.com

Maple Application Center
I am a retired professor of chemistry at the University of Victoria, BC, Canada. My research areas are electrochemistry and surface science. I have been a user of Maple since about 1990.

MaplePrimes Activity


These are answers submitted by dharr

Here's one way. The conversion from the Array to integer is a significant part of the time. (Edit: fixed problem with counts and start values above 9.)

restart;

nextone:=proc(L)
  local i,j,k,
    out:=Array([]),
    val:=L[1],
    count:=1;
  for i in L[2..-1] do
    if i=val then
      count++;
    else
      if count>9 then
         k:=convert(count,base,10);
         count:=seq(k[j],j=nops(k)..1,-1);
      end if;
      out,=count,val;
      val:=i;
      count:=1;
    end if
  end do;
  if count>9 then
     k:=convert(count,base,10);
     count:=seq(k[j],j=nops(k)..1,-1);
  end if;
  out,=count,val;
end proc:

LookAndSay:=proc(n::posint,start::nonnegint:=1)
  local i,k,L,LandS;
  k:=convert(start,base,10);
  L:=Array([seq(k[i],i=nops(k)..1,-1)]);
  LandS:=Vector(n,{1=start});
  for i from 2 to n do
    L:=nextone(L);
    LandS[i]:=parse(cat("", entries(L,'nolist')))
  end do;
  LandS
end proc:

CodeTools:-Usage(LookAndSay(50));

memory used=315.04MiB, alloc change=38.93MiB, cpu time=4.41s, real time=4.42s, gc time=406.25ms

_rtable[36893490681181765868]

 

Download LookAndSay.mw

Code is slightly cleaner if the scanning is done from the end to the beginning:

LookAndSayBackwards.mw

Using a 1-D Array with datatype=integer[1] as a character array is faster:

LookAndSayString.mw

 

For Q1, you can put things in a Table (Insert Table), and change its column sizes, and other properties, including making the lines invisible. See the help page.

I don't understand the second question about the stretchable output. If you want to see the text form for some output you can use lprint on the command. Or you can change the settings for the type of output to get - See Tools->options->display->output display. A more specific example of what you mean might help.

Very strange - seems that it fails the type test on entry, even though others on the list, such as SparseDirect work.

[Edit - I see on LUDecomposition, it is the global ":-SparseDirectMKL" in the type test, though using that the output is strange. Perhaps this is just not fully implemented yet; it isn't documented for LinearSolve]

NULL

restart:

Digits:=15;

15

A:=Matrix(4,4,[[-1,2,0,0],[2,-1,2,0],[0,2,-1,2],[0,0,1,-1]],datatype=float[8],storage=sparse);

Matrix(4, 4, {(1, 1) = -1.0, (1, 2) = 2.0, (1, 3) = 0., (1, 4) = 0., (2, 1) = 2.0, (2, 2) = -1.0, (2, 3) = 2.0, (2, 4) = 0., (3, 1) = 0., (3, 2) = 2.0, (3, 3) = -1.0, (3, 4) = 2.0, (4, 1) = 0., (4, 2) = 0., (4, 3) = 1.0, (4, 4) = -1.0})

b:=Vector(4,[1,0.,0,0],datatype=float[8]):

 

sol:=LinearAlgebra:-LinearSolve(A,b,'method'=':-SparseDirectXML');

Error, invalid input: LinearAlgebra:-LinearSolve expects value for keyword parameter method to be of type identical(none,SparseLU,SparseDirect,SparseDirectMKL,SparseIterative,LU,QR,solve,hybrid,Cholesky,subs,modular), but received SparseDirectXML

showstat(LinearAlgebra:-LinearSolve,1);


LinearAlgebra:-LinearSolve := proc(A, B::Non(`=`) := ':-NoUserValue', {conjugate::truefalse := true, free::symbol := `tools/genglobal`('_t'), inplace::truefalse := false, method::identical(none,SparseLU,SparseDirect,SparseDirectMKL,SparseIterative,
  LU,QR,solve,hybrid,Cholesky,subs,modular) := ':-none', methodoptions::list({BooleanOpt(modify), BooleanOpt(pivot), BooleanOpt(precon)
  , identical(droptol) = nonnegative, identical(initsol) = Vector, identical(
  matnorm) = positive, identical(maxiter) = posint, identical(norm) = {1, 2,
  identical(infinity,Euclidean)}, identical(toler) = nonnegative, identical(
  weight) = Vector, identical(diagscal) = nonnegative, identical(itermethod) =
  identical(CG,SYMMLQ,CGS,TFQMR,RGMRES,BICGSTAB), identical(levelfill) =
  nonnegint, identical(precstor) = posint}) := [], outputoptions::list := []})
local _A, _B, OO, _AA, _BB, attribs, b, chol_forced, DatatypeA, DatatypeB,
  DatatypeMix, DT, eqns, IndexA, i, ipiv, is_matrix, j, k, localB, m, meth, mL
  , mP, mQ, mR, mU, NAGstyle, n, OO_less_RO, OO_less_RO_DT, OrderA, p,
  prefactored, RO, StorageA, StorageB, sol, sols, splu_forced, spitr_forced,
  temp, tt, typeA, typeB, vars, X, y, z;
   1   if type(A,{('list')({'Matrix', 'Vector'}), 'Matrix'}) then
           ...
       else
           ...
       end if;
       ...
end proc
 

sol:=LinearAlgebra:-LUDecomposition(A,'method'=':-SparseDirectMKL');

before P call
after P call

"External/000002BA4F5512A0"

showstat(LinearAlgebra:-LUDecomposition,1)


LinearAlgebra:-LUDecomposition := proc(A::~Matrix, {conjugate::truefalse := true, definite::identical(indefinite,positive,LinearAlgebra:-NoUserValue) :=
  LinearAlgebra:-NoUserValue, domain::identical(real,complex,LinearAlgebra:-NoUserValue) := LinearAlgebra:-
  NoUserValue, inplace::truefalse := false, method::{identical(none,:-GaussianElimination,LinearAlgebra:-
  GaussianElimination,RREF,FractionFree,Cholesky,SparseDirect,:-
  SparseDirectMKL)} := 'none', output::{list, thistype}(identical(P,L,U,U1,R,NAG,determinant,rank,handle)) :=
[], `outputoptions[L]`::list := [], `outputoptions[NAG]`::list := [], `outputoptions[P]`::list := [], `outputoptions[R]`::list := [], `outputoptions[U1]`::list := [], `outputoptions[U]`::list := [], symmetry::identical(none,structural,symmetric,hermitian,LinearAlgebra:-
  NoUserValue) := LinearAlgebra:-NoUserValue})
local _output, RO, OO_less_RO, StorageA, localA, DatatypeA, DatatypeMix, meth,
  IndFnA, typeA, mP, mL, mU, mU1, mR, split_U, currentpivot, ipiv, get_rank,
  get_det, nagerr, NAGform, posdeffail, DT, ST, OrderA, OO_less_RO_DT,
  foundpivot, prevval, m, n, argidx, out_optlist, i, j, k, pivot, pivlist, rat
  , dt, mrank, out, lead_col, lead_list, nm, nulldim, outopt;
   1   _output := `if`(type(output,'list'),output,[output]);
       ...
end proc
 

NULL

Download buglinearsolve.mw

If I change NLPSolve to NNLPSolve, I can see what is being passed to it; see below. Some things here don't make sense, like 1[1] and 2[1]. Do you also expect s[1] and similar variables to be there?

Your C1, C2, C3, C4 are simple relationships that could be used to simplify the objective. For example you have ph[1]+ph[2]=0, so you could just substitute ph[2]=-ph[1] to eliminate ph[2] and make it simpler.

It is hepful if you upload your worksheet with the green up-arrow in the Mapleprimes editor when you post.

The error message means rhs was passed a number and not an equation, suggesting sigma[qq] is not an equation. You have not given all the code, so you will need to upload your worksheet using the green up-arrow in the Mapleprimes editor for some more specific help.

It works in 1-D without the extra parentheses, which are not in your code region versions. 

CP2 := (X, Y) ->local  x,y; {seq(seq([x, y], y = Y), x = X)};

Edit: The code edit region codes and 1-D codes should work identically. 2-D has not kept up with recent additions to the Maple language, so my workaround is just to use 1-D (or in this case leave out the local declaration and ignore the warnings)

If you remove the 0.00005..0.00015 from the plot, you see the curves are all close to 1-eta, and there was nothing to see in the vertical range you specified. Of course I can't comment on why the parameters don't give what you were expecting.

There was earlier comment on related issues here and here. The conclusion seems to be high Digits (pehaps 40) are frequently required. If you have a nice matrix (normal or perhaps even just diagonalizable), you can just do the function on the eigenvalues and bypass Maple's routine. But in your case you have a non-diagonalizable matrix (JordonForm not diagonal). Then the general algorithm is much more complicated and involves an interpolating polynomial though the eigenvalues, and I'd guess this leads to numerical issues. In Horn and Johnson's discussion of this (Topics in Matrix Analysis, ch 6), they use Lagrange-Hermite interpolation for their discussion of the analytical case, but that would not be a good way to do it numerically - not sure of the method Maple uses. Both the nature of the function and the nature of the matrix mean this is a difficult problem numerically.

You didn't upload your worksheet (using green up-arrow in the Mapleprimes editor) so it's hard to tell. For me in Maple 2023 it is working. I changed some "." to "*" ( as shown in red), though thay didn't seem to be a problem. The plot looks correct, the Gibbs oscillations always need lots of terms to be less prominent.

Download fourier.mw

Edit - I realised your output is the general formula for any N. Do you really want that? Usually the sum you get when asking for a specific numberof terms, say fourier_f(5), is what is expected. If you don't need the general formula, add rather than sum is better.

restart;

assume(n,posint);

target_f := x -> piecewise(-Pi < x and x < 0, 0, 0 < x and x < Pi, x, Pi < x and x < 2*Pi, 0, 2*Pi < x and x < 3*Pi, x - 2*Pi);
a0 := simplify(int(target_f(x), x = -Pi .. Pi)/(2*Pi));
a_n:=int(target_f(x)*cos(n*x), x = -Pi .. Pi)/Pi;
b_n:=int(target_f(x)*sin(n*x), x = -Pi .. Pi)/Pi;
fourier_f := unapply(a0+'add'(a_n*cos(n*x) + b_n*sin(n*x), n = 1 .. N),N);


 

target_f := proc (x) options operator, arrow; piecewise(-Pi < x and x < 0, 0, 0 < x and x < Pi, x, Pi < x and x < 2*Pi, 0, 2*Pi < x and x < 3*Pi, x-2*Pi) end proc

(1/4)*Pi

(-1+(-1)^n)/(n^2*Pi)

-(-1)^n/n

proc (N) options operator, arrow; (1/4)*Pi+add((-1+(-1)^n)*cos(n*x)/(n^2*Pi)-(-1)^n*sin(n*x)/n, n = 1 .. N) end proc

plot(fourier_f(20),x=-Pi..Pi);

 

Download fourier_with_add.mw


 

restart

with(Grading)

Quiz("What is the sum of $B and $C", proc () options operator, arrow; evalb(Quiz:-Get(`$RESPONSE`) = Quiz:-Get(`$B`)+Quiz:-Get(`$C`)) end proc, proc () Quiz:-Set(`$B` = (rand(0 .. 100))(), `$C` = (rand(0 .. 100))()) end proc)

NULL

Download Quiz1.mw

The BellmanFordAlgorithm can do a whole row of the matrix at a time if you supply a list as the last argument, leading to a much faster result.

[Edit - newer version using topological order added]

restart

randomize(14161147548192)

14161147548192

G := GraphTheory:-RandomGraphs:-RandomNetwork(200, .2, 'acyclic', 'weights' = 0 .. .2)

G__0 := applyop(`-`, -1, G)NULL

GRAPHLN(directed, weighted, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200], Array(1..200, {(1) = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, (2) = {3, 6, 8, 10, 13, 16, 17, 18, 20}, (3) = {4, 6, 7, 8, 10, 13, 17, 18, 20}, (4) = {5, 6, 7, 8, 9, 13, 14, 18, 20}, (5) = {6, 7, 10, 14, 15, 18, 19, 20}, (6) = {7, 9, 10, 14, 15, 16, 17}, (7) = {11, 12, 19, 20}, (8) = {9, 10, 11, 12, 15, 17, 18, 20}, (9) = {11, 14, 15, 16, 18}, (10) = {15, 16, 17, 19}, (11) = {15, 16, 17, 18, 19, 20}, (12) = {13, 15, 18, 19, 21, 24}, (13) = {16, 18, 20, 21, 23, 25, 26, 27, 28}, (14) = {15, 16, 17, 18, 20, 22, 27}, (15) = {16, 19, 20, 22, 24, 26, 27, 28}, (16) = {17, 19, 22, 23, 24, 27}, (17) = {18, 20, 26, 27}, (18) = {21, 23, 24, 25, 26, 28}, (19) = {23, 25}, (20) = {21, 23, 24, 25, 26, 28}, (21) = {22, 24, 25, 26, 29, 31, 33, 34, 35}, (22) = {23, 25, 26, 28, 29, 31}, (23) = {24, 26, 28, 30, 32, 34, 35}, (24) = {25, 28, 29, 34, 35}, (25) = {26, 27, 29, 32, 34}, (26) = {27, 30, 32, 34}, (27) = {28, 30, 31, 32, 35}, (28) = {29, 31}, (29) = {30, 32, 34, 35, 37, 40, 42, 45}, (30) = {31, 34, 35, 36, 43, 45}, (31) = {33, 34, 35, 36, 37, 38, 40, 41, 45}, (32) = {33, 34, 35, 36, 37, 41, 42, 44, 45}, (33) = {34, 35, 36, 37, 38, 39, 40, 41, 42, 43}, (34) = {35, 36, 39, 41, 42, 44}, (35) = {37, 40, 42, 43, 44}, (36) = {37, 38, 39, 40, 43, 44, 45, 46, 47, 48, 49, 51, 52}, (37) = {43, 44, 45, 47, 51}, (38) = {40, 41, 42, 43, 44, 46, 48, 49, 52}, (39) = {41, 42, 44, 46, 51}, (40) = {42, 43, 44, 45, 46, 48, 50, 51, 52}, (41) = {44, 49, 51}, (42) = {43, 45, 46, 47, 48, 49, 50, 51}, (43) = {47, 49, 50, 52}, (44) = {45, 46, 49, 52}, (45) = {46, 48, 49, 52}, (46) = {48, 49, 50, 51}, (47) = {48, 49, 51, 52, 53}, (48) = {51}, (49) = {52}, (50) = {52}, (51) = {52}, (52) = {53}, (53) = {54, 55, 56, 57, 58, 59, 60, 61}, (54) = {55, 56, 57, 58, 60, 61, 64}, (55) = {57, 58, 62, 64}, (56) = {57, 60, 61, 62}, (57) = {58, 61, 63, 64}, (58) = {59, 61, 62}, (59) = {60, 61, 62, 63}, (60) = {61, 63, 64}, (61) = {63, 64}, (62) = {65, 66}, (63) = {65, 66}, (64) = {65, 66, 67}, (65) = {67, 69, 70, 75, 76}, (66) = {68, 71, 72, 74}, (67) = {68, 69, 70, 71, 73, 74, 76}, (68) = {69, 71, 73, 74, 75, 77, 82, 84, 85, 86}, (69) = {72, 73, 75, 78, 79, 80, 82, 83, 87}, (70) = {73, 75, 77, 79, 80, 81, 82, 83, 84, 85, 86, 87}, (71) = {76, 78, 80, 83, 84, 86}, (72) = {74, 76, 77, 78, 79, 80, 81, 87}, (73) = {76, 77, 79, 80, 81, 82, 85, 87}, (74) = {75, 78, 81, 83, 84, 86}, (75) = {76, 77, 79, 82, 83, 84, 86, 87}, (76) = {77, 78, 79, 80, 85, 87}, (77) = {79, 80, 83, 85, 87, 89, 93}, (78) = {80, 83, 84, 85, 90, 92, 93}, (79) = {80, 81, 82, 83, 85, 88}, (80) = {82, 85, 86, 87, 88, 90, 91}, (81) = {83, 85, 87, 88, 93}, (82) = {83, 86, 87, 88, 89, 91, 92, 93}, (83) = {84, 85, 86, 87, 88, 90, 91, 92, 93}, (84) = {85, 86, 87, 88, 90, 91}, (85) = {87, 88, 90, 91, 92, 93}, (86) = {89, 90, 92, 93}, (87) = {88, 89}, (88) = {89, 90, 93, 94}, (89) = {90, 92, 94}, (90) = {94}, (91) = {92, 94}, (92) = {93, 94}, (93) = {94}, (94) = {95}, (95) = {96, 97, 98, 99, 100, 101, 102, 103}, (96) = {97, 101, 104, 105}, (97) = {98, 99, 100, 102, 103, 106}, (98) = {105}, (99) = {105}, (100) = {101, 102, 104, 105, 106}, (101) = {102, 104, 105}, (102) = {106}, (103) = {104}, (104) = {105, 107, 109, 110, 111, 112, 113}, (105) = {107, 108, 109, 110}, (106) = {107, 109, 111, 113}, (107) = {111, 114}, (108) = {109, 110, 114}, (109) = {110, 111, 112}, (110) = {111, 113}, (111) = {114}, (112) = {113}, (113) = {114}, (114) = {115}, (115) = {116, 117}, (116) = {118}, (117) = {118}, (118) = {119, 120, 121, 122, 123}, (119) = {125, 129, 130}, (120) = {122, 123, 125, 126, 128, 129, 130}, (121) = {126, 128, 129}, (122) = {123, 124, 125, 126, 127, 130}, (123) = {125, 126, 127, 128, 130}, (124) = {125, 128, 131, 133}, (125) = {126, 127, 128, 131, 132, 135, 137, 138}, (126) = {127, 129, 130, 132, 138}, (127) = {131, 134, 136}, (128) = {130, 131, 132, 137}, (129) = {130, 131, 132, 133, 134, 136, 137}, (130) = {131, 135, 136, 137}, (131) = {133, 134, 136, 137, 139}, (132) = {133, 134, 135, 136, 139, 140, 142, 143}, (133) = {136, 137, 138, 140, 141, 142, 143}, (134) = {135, 136, 139, 140, 141, 143}, (135) = {136, 137, 138, 139, 140, 141, 142, 143}, (136) = {139, 142}, (137) = {140, 141, 142, 143}, (138) = {139, 140, 141, 142}, (139) = {140, 143, 144}, (140) = {141, 142, 144}, (141) = {144}, (142) = {144}, (143) = {144}, (144) = {145, 146, 147, 148, 149, 150, 151}, (145) = {149, 150, 151, 152}, (146) = {149}, (147) = {150, 151, 152}, (148) = {149, 150, 151}, (149) = {152}, (150) = {152}, (151) = {152}, (152) = {153, 154}, (153) = {154, 155, 156}, (154) = {156, 157}, (155) = {156, 157, 159, 160, 161, 162, 163, 164}, (156) = {158, 160, 161, 162, 163, 164}, (157) = {158, 160, 161, 163}, (158) = {161, 162, 164, 166, 167, 170, 172}, (159) = {160, 161, 162, 163, 164, 167, 170, 171, 172}, (160) = {162, 163, 165, 166, 168, 169, 170, 171}, (161) = {163, 164, 168, 169, 170, 171, 173}, (162) = {163, 166, 167, 169, 170, 171, 172, 173}, (163) = {164, 165, 167, 168}, (164) = {165, 167, 169, 170}, (165) = {167, 168, 169, 173, 174, 175, 176, 181}, (166) = {167, 170, 175, 177, 178, 179, 181, 182, 183, 184}, (167) = {169, 170, 176, 177, 180, 182, 183, 184}, (168) = {169, 170, 171, 173, 174, 176, 178, 179, 180, 181}, (169) = {171, 174, 175, 176, 178, 179, 181, 183}, (170) = {174, 175, 177, 179, 180, 182}, (171) = {172, 175, 176, 180, 181, 182}, (172) = {173, 174, 175, 176, 184}, (173) = {175, 178, 180, 181, 183}, (174) = {175, 176, 178, 181, 182, 183, 184, 185, 186, 187, 188, 191}, (175) = {176, 177, 181, 182, 183, 184, 185, 189}, (176) = {177, 179, 180, 181, 182, 183, 185, 188, 189, 190, 191}, (177) = {179, 180, 182, 183, 185, 187, 188, 189, 191}, (178) = {180, 182, 183, 184, 185, 186, 188, 189, 190}, (179) = {180, 181, 182, 185, 187, 188, 189}, (180) = {184, 185, 187, 188, 189, 190}, (181) = {183, 184, 186, 188, 189, 191}, (182) = {186, 188, 189, 190, 191}, (183) = {186, 187, 190}, (184) = {185, 188, 189, 191}, (185) = {186, 190, 191, 192, 193}, (186) = {188, 189, 190, 191, 192}, (187) = {189, 192, 193}, (188) = {190, 192, 193}, (189) = {191, 192, 193}, (190) = {191, 193}, (191) = {192}, (192) = {193}, (193) = {194}, (194) = {195, 196}, (195) = {196, 197, 198}, (196) = {197, 198}, (197) = {198, 199}, (198) = {199}, (199) = {200}, (200) = {}}), `GRAPHLN/table/1`, )

t, s := combinat:-randcomb(GraphTheory:-Vertices(G__0), 5^2), combinat:-randcomb(GraphTheory:-Vertices(G__0), integermul2exp(5, 2))

[4, 21, 25, 26, 30, 35, 47, 48, 60, 74, 84, 93, 100, 104, 110, 126, 140, 144, 158, 165, 167, 171, 179, 182, 196], [5, 40, 42, 43, 54, 74, 80, 81, 86, 89, 93, 96, 103, 104, 123, 125, 126, 133, 144, 160]

Original version

"DataFrame((`M__1`:=CodeTools:-Usage(Matrix(numelems(s),numelems(t),(i,j)->numelems((GraphTheory:-BellmanFordAlgorithm(`G__0`,s[i],t[j]))[1]),datatype=integer[2]))),'columns'=t,'rows'=s):"

memory used=8.45GiB, alloc change=-2.00MiB, cpu time=4.58m, real time=4.85m, gc time=28.11s

New version BellmanFordAlgorithm doing one row at a time.

"DataFrame((`M__2`:=CodeTools:-Usage(Matrix(numelems(s), numelems(t),[seq(map(x->nops(x[1]), GraphTheory:-BellmanFordAlgorithm(`G__0`,s[i],t) ),i=1..numelems(s))]))),'columns'=t,'rows'=s):"

memory used=348.13MiB, alloc change=0 bytes, cpu time=9.42s, real time=9.42s, gc time=406.25ms

Original version sped up with topological sort

"DataFrame((`M__3`:=CodeTools:-Usage(Matrix(numelems(s),numelems(t),proc(i::posint,j::posint,` $`)::nonnegint;  uses ListTools,GraphTheory; local ts::list(posint):=TopologicSort(`G__0`,'output'='permutation'),q::posint:=Search(t[j],ts),p::posint:=Search(s[i],ts); if  p>q then 0 elif q=p then 1 else numelems(BellmanFordAlgorithm(`G__0`,s[i],t[j])[1]) fi end,datatype=integer))),':-columns'=t,':-rows'=s):"

memory used=4.75GiB, alloc change=0 bytes, cpu time=2.29m, real time=2.21m, gc time=10.86s

New version sped up with topological sort

ShortestPathsDAG:=proc(G::Graph,s::list,t::list)
  local ts,tge,lthsge,ns,nt,lths,i,j,M;
  ns:=nops(s);
  nt:=nops(t);
  M:=Matrix(ns,nt);
  ts:=GraphTheory:-TopologicSort(G,'output'='permutation');
  for i to ns do  # row i in Matrix
    # only do Bellman Ford on values in t that are later in topo seq than vertex s[i] for this row
    j:=ListTools:-Search(s[i],ts);
    tge:=convert({t[]} intersect {ts[j..-1][]},list);
    lthsge:=map(x->nops(x[1]),GraphTheory:-BellmanFordAlgorithm(G,s[i],tge));
    lths:=table('sparse',tge=~lthsge);
    M[i]:=Vector[row](nt,j->lths[t[j]]);
  end do;
  M;
end proc:

"DataFrame((`M__4`:=CodeTools:-Usage(ShortestPathsDAG(`G__0`,s,t))),'columns'=t,'rows'=s):"

memory used=348.33MiB, alloc change=0 bytes, cpu time=9.38s, real time=9.06s, gc time=718.75ms

andmap(LinearAlgebra:-Equal, [M__2, M__3, M__4], M__1)

true

NULL

Download longest_paths_in_a_DAG.mw

 

If you upload your worksheet (green up arrow in the Mapleprimes editor) then we can run the code without retyping it in.

In your first procedure, the wiggly line on line 2 tells you there is a syntax error there, which is that it is seeing you want m as a local even though you used it already in the first line. Your first line should be U::Matrix and not U::Matrix(m).

In the second procedure A:=Matrix is not correct and should be removed. In both cases, the Dimension command is in the LinearAlgebra package and so should be LinearAlgebra:-Dimension (and since it returns two things the second version uses it correctly)

You should assign the procedure to a name, for example fillme:=proc(A)

 

Here's a small step in the right direction.

restart;

eq:=erf(x)=erf(Pi);

erf(x) = erf(Pi)

Define inverse function

inverf:=erf@@(-1);

erf@@(-1)

Apply it to both sides of the equation

map(inverf,eq);

x = Pi

solve(inverf(erf(x))=inverf(erf(Pi)),x);

Pi

But inverf isn't a linear function, so we don't expect anything more complicated to work.

inverf(erf(x)-erf(Pi))=inverf(0);

(erf@@(-1))(erf(x)-erf(Pi)) = (erf@@(-1))(0)

NULL

Download inverf.mw

The following parameters make it positive semidefinite:

My procedure was pretty ad-hoc, using Satisfy and just adding conditions until it worked.

positivedefinite.mw

@mmcdara - I think you needed C[1]<=0, but in 2015 it didn't lead to a solution even when I made that change, 2023 just gave one solution when solving the conditions. Since the matrix is symmetric, the discriminants of the quadratics have to be positive anyway, and the signs of the coefficents can be used to test for two positive roots. But I don't really see why your method gave a different result.


@sursumCorda I'd be suspicious of the deprecated linalg routine. All principal minors (of all sizes) need to non-neg, so perhaps systematically working through these would be a better method. 

Use datasetlabels = contents for this, but it isn't pretty.

First 29 30 31 32 33 34 35 Last Page 31 of 83