sursumCorda

1064 Reputation

13 Badges

1 years, 286 days

MaplePrimes Activity


These are answers submitted by sursumCorda

I don't why the try statement (in showstat(`convert/RootOf`, 80)) cannot catch some exception at first time.
However, if you add the following inelegant code: 

# restart;
`convert/RootOf` := FromInert(subsop([5, 8, 1, 1, 1, 1] = ToInert('membertype('identical(index) = real[integer]', map2(op, -1, indets(f1, 'specfunc(RootOf)'))) or f1::('RootOf'(anything)^anything)'), ToInert(eval(`convert/RootOf`)))):

your code will work. 

  #now use coulditbe, it should use hard value?

Well, the following instance may be what you want:

restart;showstat(`is/internal`, 161);

`is/internal` := proc(obj, prop)
local eqns, i, inds, oldinds, nobj, r, oprop, p, z, newind, lnames, lnenames;
       ...
 161   if r = FAIL and (nops(inds) <= 3 or _EnvTry = ('hard')) then
         ...
       end if;
       ...
end proc

foo1 := proc(f)::boolean_constant:
    undefine('_EnvTry');
    `assuming`([coulditbe(f)], real)
end:
foo2 := proc(f)::boolean_constant:
    _EnvTry := hard;
    `assuming`([coulditbe(f)], real)
end:

Now we can find that

foo1(And(x^2 + y^2 <= 11, x^3 - y^2 = 2, x*y > 3));
 = 
                              FAIL

foo2(And(x^2 + y^2 <= 11, x^3 - y^2 = 2, x*y > 3));
 = 
                              true

However, I don't know whether proc uses lexical scoping or dynamic scoping. (Maybe the former mechanism?)

Suppose that there exists some MapleSim model in the current dictionary.
For example, 

FileTools:-Compressed:-ReadFile("MapleSimVersion.manifest" in "CreateYourFirstModel.msim", string); # from Create Your First Model In MapleSim (maplesoft.com)
 = 
              "MapleSimMajorVersion = 2016


                MapleSimMinorVersion = 1


                ModelicaVersion = Modelica 3.2.1


                MapleSimVariant = MapleSim V4.1


                "


  Therefore, this model seems to be created in MapleSim 2016.1.

It appears that this seq version is more or less fater than yours (on my computer). 
 

restart; with(LinearAlgebra)

with(combinat); with(GroupTheory)

DetDef := proc (A) local i, n, sigma; description "Jeremy Johnson. Downloaded from https://www.cs.drexel.edu/~jjohnson/2016-17/winter/cs300/lectures/determinant.mw"; n := RowDimension(A); add(PermParity(Perm(sigma))*mul(A[i, sigma[i]], i = 1 .. n), `in`(sigma, permute([`$`(1 .. n)]))) end proc

InnerMatrix := proc (M::Matrix) SubMatrix(M, 2 .. RowDimension(M)-1, 2 .. ColumnDimension(M)-1) end proc

MatrixDet := proc (M::Matrix) local C, n, i, j; n := RowDimension(M)-1; Matrix(n, n, proc (i, j) options operator, arrow; Determinant(M([i, i+1], [j, j+1])) end proc) end proc

Dodgson := proc(M::Matrix)
 MatrixDet(M);
InnerMatrix(M) ^~ (-1) *~ MatrixDet(MatrixDet(M));
do if 1 < RowDimension(%) then InnerMatrix(`%%`) ^~ (-1) *~ MatrixDet(%);
end if;
until RowDimension(%) = 1;
Trace(%):
end proc:

Dodgsonseq := proc (E::Matrix) local w, dim, Z; dim := RowDimension(E); Z[dim] := E; Z[dim-1] := MatrixDet(E); Z[dim-2] := `~`[`*`](`~`[`^`](InnerMatrix(E), -1), MatrixDet(MatrixDet(E))); seq(assign('Z[w-1]', `~`[`*`](`~`[`^`](InnerMatrix(Z[w+1]), -1), MatrixDet(Z[w]))), w = dim-1 .. 1, -1); Trace(Z[1]) end proc

LaPlace := proc (M::Matrix) local c; add((-1)^(c+1)*M[1, c]*Minor(M, 1, c), c = 1 .. ColumnDimension(M)) end proc

dim := 7; A := Matrix(dim, dim, shape = symmetric, symbol = a)

                                  dim := 7

 

start_time := time(); st := time[real](); Det1 := abs(A); CPUtime_used_Build_in_Determinant := time()-start_time; REALtime_used_Build_in_Determinant := time[real]()-st; start_time := time(); st := time[real](); Det2 := DetDef(A); CPUtime_used_Jeremy_Johnson_Determinant := time()-start_time; REALtime_used_Jeremy_Johnson_Determinant := time[real]()-st; start_time := time(); st := time[real](); Det3 := Dodgsonseq(A); CPUtime_usedDodgsonseq := time()-start_time; REALCPUtime_usedDodgsonseq := time[real]()-st; start_time := time(); st := time[real](); Det4 := Dodgson(A); CPUtime_usedDodgson := time()-start_time; REALtime_usedDodgson := time[real]()-st; start_time := time(); st := time[real](); Det5 := LaPlace(A); CPUtime_usedLaPlace := time()-start_time; REALtime_usedLaPlace := time[real]()-st; simplify(Det1-Det2); simplify(Det1-Det3); simplify(Det1-Det4); simplify(Det1-Det5)
NULL

.109

 

.117

 

.297

 

.319

 

124.907

 

97.223

 

108.046

 

78.326

 

.125

 

.146

 

0

 

0

 

0

 

0

(1)

Dodgsonseq_new := proc (E::(`~Matrix`('square'))) local w, dim, Z; dim := LinearAlgebra['ColumnDimension'](E); if dim = 1 then RETURN(E(1)) else w, Z := MatrixDet(E), E; `$`('assign(('w', 'Z') = (`~`[`/`](MatrixDet(w), InnerMatrix(Z)), w))', dim-2); Trace(w) end if end proc

start_time := time(); st := time[real](); Det6 := Dodgsonseq_new(A); CPUtime_usedDodgsonseq_new := time()-start_time; REALtime_usedDodgsonseq_new := time[real]()-st; simplify(Det1-Det6)

101.172

 

79.417

 

0

(2)

 


 

Download test_Determinants_symbolic_Modified.mw

But I'm not sure if these hinge upon PC performance.

Use op(⑴) or ⑴[]

isolate([6600.0*theta(q) - 17000.0*theta(q - 1) + 14400.0*theta(q - 2) - 4000.0*theta(q - 3) = v(q) - 0.20*theta(q) + 0.20*theta(q - 1)][], theta(q));
 = 
 theta(q) = 2.575709827 theta(q - 1) - 2.181752068 theta(q - 2)

    + 0.6060422412 theta(q - 3) + 0.0001515105603 v(q)


 

One may use the forgotten applyrule
 

transformed impulse response (

(10*exp(3/10)-7*exp(1/5)-3*exp(2/5))*exp(1/10)*U(z)/z+(exp(1/10)*(-3+10*exp(1/10)-7*exp(1/5))-(10*exp(3/10)-7*exp(1/5)-3*exp(2/5))*exp(1/10))*U(z)/z^2-(-3+10*exp(1/10)-7*exp(1/5))*exp(1/10)*U(z)/z^3 = 8*Y(z)*exp(1/10)*exp(3/10)*exp(1/5)+(8*((-exp(1/5)-exp(3/10))*exp(1/10)-exp(3/10)*exp(1/5)))*Y(z)/z+(8*(exp(1/10)+exp(1/5)+exp(3/10)))*Y(z)/z^2-8*Y(z)/z^3

(10*exp(3/10)-7*exp(1/5)-3*exp(2/5))*exp(1/10)*U(z)/z+(exp(1/10)*(-3+10*exp(1/10)-7*exp(1/5))-(10*exp(3/10)-7*exp(1/5)-3*exp(2/5))*exp(1/10))*U(z)/z^2-(-3+10*exp(1/10)-7*exp(1/5))*exp(1/10)*U(z)/z^3 = 8*Y(z)*exp(1/10)*exp(3/10)*exp(1/5)+8*((-exp(1/5)-exp(3/10))*exp(1/10)-exp(3/10)*exp(1/5))*Y(z)/z+8*(exp(1/10)+exp(1/5)+exp(3/10))*Y(z)/z^2-8*Y(z)/z^3

(1)

deg_mx := 3

3

(2)

Replacements to get finite difference equation

[seq(U(z)/z^i = U[q+1-i], i = deg_mx .. 0, -1), seq(Y(z)/z^i = Y[q+1-i], i = deg_mx .. 0, -1)]

[U(z)/z^3 = U[q-2], U(z)/z^2 = U[q-1], U(z)/z = U[q], U(z) = U[q+1], Y(z)/z^3 = Y[q-2], Y(z)/z^2 = Y[q-1], Y(z)/z = Y[q], Y(z) = Y[q+1]]

(3)

Attempt to replace

"map2(applyrule,,)" =

U[q]*(10*exp(3/10)-7*exp(1/5)-3*exp(2/5))*exp(1/10)+U[q-1]*(exp(1/10)*(-3+10*exp(1/10)-7*exp(1/5))-(10*exp(3/10)-7*exp(1/5)-3*exp(2/5))*exp(1/10))-U[q-2]*exp(1/10)*(-3+10*exp(1/10)-7*exp(1/5)) = 8*Y[q+1]*exp(1/10)*exp(3/10)*exp(1/5)+Y[q]*(8*(-exp(1/5)-exp(3/10))*exp(1/10)-8*exp(3/10)*exp(1/5))+Y[q-1]*(8*exp(1/10)+8*exp(1/5)+8*exp(3/10))-8*Y[q-2]

(4)

%?


 

Download Another_way.mws

By the way, the new command Physics:-Substitute is too "smart" here.

Maple's rational conversion function is `convert/rational`.

Two examples: 
 

 

 

restart

M__1 := `<|>`(`<,>`(1, 5, 9, 13), `<,>`(2, 6, 10, 14), `<,>`(3, 7, 11, 15), `<,>`(4, 8, 12, 16))

Matrix(%id = 36893490308353225180)

(1.1)

M__2 := (Vector[row](4, {(1) = 17, (2) = 18, (3) = 19, (4) = 20}))^%T

Vector[column](%id = 36893490308353213252)

(1.2)

zip[inplace](`/`@@2, M__1, `<|>`(`$`(M__2, 4)))

Matrix(%id = 36893490308353225180)

(1.3)

M__1

Matrix(%id = 36893490308353225180)

(1.4)

 

 

restart

M__1 := `<|>`(`<,>`(1, 5, 9, 13), `<,>`(2, 6, 10, 14), `<,>`(3, 7, 11, 15), `<,>`(4, 8, 12, 16))

Matrix(%id = 36893490308353225180)

(2.1)

M__2 := (Vector[row](4, {(1) = 17, (2) = 18, (3) = 19, (4) = 20}))^%T

Vector[column](%id = 36893490308353213252)

(2.2)

M__1 := reduce(`<|>`, ArrayTools:-GeneralInnerProduct(M__2, `<,>`, `/`, M__1))

Matrix(%id = 36893490308353205060)

(2.3)

NULL


 

Download elementwise_division.mw

Edit. Note that if you don't need the inplace option, 

M1 := zip(`/`, `<|>`(M2, n), M1):

or @acer's 

M1 := `<|>`(M2$n)/~M1:

is much faster than 

zip[inplace](`/`@@2, M1, `<|>`(M2, n)):

Besides, the advantage of the second example above is that we don't need know the dimensions. (In fact, @dharr's 

M1 := LinearAlgebra:-DiagonalMatrix(M2)*M1 ^~ (-1):

 is faster, but it doesn't work in generalized cases.)

map(f, a + b, t);
 = 
                       f(a, t) + f(b, t)

I think that this is due to Maple's internal machinery of the simplification.
Compare: (Which one do you think is simpler for the subscribers? Maple's (-c - d)*b + a or Mathematica's a - b*(c + d)?)

> simplify([a - b*c - b*d, a - b*2 - c*2]); (*Maple*)
                                            [(-c - d) b + a, a - 2 b - 2 c]


In[1]:= Simplify[{a - b*c - b*d, a - b*2 - c*2}] (*another CAS*)

Out[1]= {a - b (c + d), a - 2 (b + c)}

However, I find that if we work in degrees, Degrees[Simplify] will give a more readable result: 

use Degrees in Simplify(x = -sind(alpha)*(L + (e + r)/tand(-90 + alpha/2))/2 + sind(alpha)*L) end;
 = 
    /                     /1      \                  /1      \\ 
x = |(e + r) Degrees:-sind|- alpha| + L Degrees:-cosd|- alpha|| 
    \                     \2      /                  \2      // 

               /1      \
  Degrees:-sind|- alpha|
               \2      /



The reason for the failure is: 

ToInert(op([1, 2, -1], sol)):

is not identical to 

ToInert(_Z1):

However, this works inelegantly

parse(StringTools:-Subs({"_B2" = "n", "_Z1" = "n", "_Z2" = "n"}, String(sol)));
 = 
           /                     1            2  
          { alpha = Pi n, beta = - Pi + 2 Pi n , 
           \                     2               

                           /2    /      2\\         \ 
            lambda = arctan|- sin\2 Pi n /| + Pi _Z4 }
                           \3             /         / 


 

If we work with closed intervals, another approach can be adopted. (Firstly, one must convert RealRange into list.) 

mergeThem := proc(bds::nonemptylist(And([realcons, realcons], satisfies(x -> is(x[1] <= x[2])))), $) 
    option encrypted; 
    uses ListTools; 
    local aux := map([min, max], [Categorize]((x, y) -> is(x[2] >= y[1]) and is(y[2] >= x[1]), bds)); 
    `if`(Sorted(aux[sort(evalf(aux), output = permutation)], (x, y) -> is(x[2] < y[1])), RETURN(aux), thisproc(aux)); 
    end:

Appendix. In the style of Mathematica

mergeThem@bds:{{(-Infinity|_?NumericQ|Infinity)~Repeated~{2}}?(Apply@FullSimplify@*LessEqual)..}:=NestWhile[MinMax/@OperatorApplied[Gather][VectorGreaterEqual[{{Indexed[#1,{2}],Indexed[#2,{2}]},{Indexed[#2,{1}],Indexed[#1,{1}]}}]&/*FullSimplify][#]&,bds,!OrderedQ[NumericalSort@#,#1[[2]]<#2[[1]]&//FullSimplify]&]

See also https://www.mapleprimes.com/questions/39068-Unions-Of-Ranges and https://www.mapleprimes.com/questions/230430-How-To-Perform-Set-Operations-With-Intervals.

A subtle point: You have to use [algebraic, algebraic, algebraic] rather than [algebraic $ 3]. Alternatively, 

restart;

test := proc(l1::[{satisfies(s -> type(s, [algebraic $ 3])), 'Vector[row]'(3, algebraic)}, 'Vector[column]'(3, algebraic)], l2::[{satisfies(s -> type(s, [algebraic $ 3])), 'Vector[row]'(3, algebraic)}, 'Vector[column]'(3, algebraic)]) print("inputs recognised") end;

proc (l1::[{satisfies(proc (s) options operator, arrow; type(s, [`$`(algebraic, 3)]) end proc), ('Vector[row]')(3, algebraic)}, ('Vector[column]')(3, algebraic)], l2::[{satisfies(proc (s) options operator, arrow; type(s, [`$`(algebraic, 3)]) end proc), ('Vector[row]')(3, algebraic)}, ('Vector[column]')(3, algebraic)]) print("inputs recognised") end proc

(1)

l1 := [`<|>`(1, 5, 7), `<,>`(3, 7, 9)] = [Vector[row](3, {(1) = 1, (2) = 5, (3) = 7}), Vector(3, {(1) = 3, (2) = 7, (3) = 9})]NULL

l2 := [`<|>`(-4, 2, 1), `<,>`(3, 8, -9)] = [Vector[row](3, {(1) = -4, (2) = 2, (3) = 1}), Vector(3, {(1) = 3, (2) = 8, (3) = -9})]NULL

test(l1, l2)

"inputs recognised"

(2)

l3 := [[1, 5, 7], `<,>`(3, 7, 9)] = [[1, 5, 7], Vector(3, {(1) = 3, (2) = 7, (3) = 9})]NULL

l4 := [[-4, 2, 1], `<,>`(3, 2, -9)] = [[-4, 2, 1], Vector(3, {(1) = 3, (2) = 2, (3) = -9})]NULL

test(l3, l4)

"inputs recognised"

(3)

NULL


 

Download Procedure_testing_types_in_list_inputs.mw

X:=(eval@@2)(foldl(''SubstituteAll'',s,''op''([L1[k],L2[k]])$k=1..numelems(L1)));
 = 
  X := "{ {0, 1}, {1, 2}, {1, 10}, {2, 3}, {3, 4}, {4, 5},

    {4, 9}, {5, 6}, {6, 7}, {7, 8},{8, 9}, {10, 11}, {11, 12},

    {11, 16}, {12, 13}, {13, 14}, {14, 15}, {15, 16}}"


Besides, if we don't know StringTools[CharacterMap]

Subs(convert(L1, list) =~ convert(L2, list), s); # also works
 = 
  "{ {0, 1}, {1, 2}, {1, 10}, {2, 3}, {3, 4}, {4, 5},

    {4, 9}, {5, 6}, {6, 7}, {7, 8},{8, 9}, {10, 11}, {11, 12},

    {11, 16}, {12, 13}, {13, 14}, {14, 15}, {15, 16}}"


 


 

restart;

bigList := RandomTools:-Generate(listlist(integer(range = -10^3 .. 10^3), 10^4, 10^2))

s1 := CodeTools:-Usage(subsindets(bigList, list, convert, set))

memory used=27.62MiB, alloc change=8.00MiB, cpu time=703.00ms, real time=560.00ms, gc time=281.25ms

 

s2 := CodeTools:-Usage(convert(map(convert, bigList, set, nested), set, nested))

memory used=20.30MiB, alloc change=0 bytes, cpu time=375.00ms, real time=382.00ms, gc time=0ns

 

s3 := CodeTools:-Usage(convert(convert(bigList, Array, fill = NULL), set, nested))

memory used=32.15MiB, alloc change=35.63MiB, cpu time=859.00ms, real time=788.00ms, gc time=203.12ms

 

is(s1 = s2 and s2 = s3) = trueNULL


 

Download nested_convert_to_set.mw

1 2 3 4 5 Page 4 of 5