acer

32363 Reputation

29 Badges

19 years, 332 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

Your attachment had an invalid Equation blob of XML, just after the "Kronecker Delta" text.

Removing that I get the following, which opens ok in Maple 2022.2.

Lecture_2_notes_ac.mw

ps. Maple 2024 seems to have much less of this kind of problem (and might even be able to itself recover your attachment acceptably...). If your school offers it then I suggest you upgrade.

ps. The Mapleprimes posting editor's toolbar has a green up-arrow that allows on to upload and attach worksheet documents to posts. In future you could use that instead of a 3rd-party download site.

Your code snippet shows the name shift being used both as an optional parameter of F as well as for an equation in each of the lists passed for the Q, vec, and point parameter of F. It seems that you want to be able to pass those independently, when calling F.

You might also want one of those to override the other, ie. either the instances inside the lists, or the separate parameter. I've made a guess as to which that might be, since you haven't really explained your goal. (You should easily be able to switch which might be an override, or instead utilize them wholly separately, and/or assign the values to three different locals, etc.)

Using variants on the code below you should be able to access the shift equations (and their rhs values) specified by each of your Q, vec, or point lists. Accessing each of those simply involves using the eval command.

Eg., inside F, you can do any of,
   eval(':-shift',Q)
   eval(':-shift',vec)
   eval(':-shift',point)

One of your queries here is about how to access the rhs of shift=something when that appears in a list. That was also in a Reply of another recent Question of yours on this site. And I used the same approach above as previously recommended -- 2-argument eval -- the only difference is that now you have different local/param vs global references of the name hanging about.

(Again, you didn't state whether you wanted the separate shift optional parameter of F be subordinate, or an override, or unrelated to those three. I've made a guess, and demonstrated a way in which they could inter-relate. Remove that aspect, if not desired.)

restart;
F := proc(point::list:=[':-symbolsize'=12,':-symbol'=':-solidcircle',
                        ':-colour'=':-blue',':-shift'=0.4],
          shift::float:=0.7)
  local lshift;
  if eval(':-shift',point)::float then
    lshift := eval(':-shift',point);
  else
    lshift := shift;
  end if;
  lshift;
end proc:

 

F();

.4

F([symbolsize=12,symbol=solidcircle,colour=blue]);

.7

F([symbolsize=12,symbol=solidcircle,colour=blue,':-shift'=foo]);

.7

F([symbolsize=12,symbol=solidcircle,colour=blue,':-shift'=':-shift']);

.7

F([symbolsize=12,symbol=solidcircle,colour=blue,':-shift'=0.3]);

.3


Download param_pr_2.mw

I think that you could make it more clear and explicit what behavior you want an expect here. Showing a representative variety of function calls and corresponding expected results (even if the proc code is not given) is a basic thing. You should be explicit about the functionality you want.

ps. I suggest that you run maplemint on your procedures. I expect that you might find that it reports quite a few cases where global names are being used without protection against having prior values. (Eg. Try to run your example with :-shift assigned a float value at the top-level, and see what breaks, etc, etc.) I've tried to make the above "mint-clean".

pps. I understand that your actual procedures might be too long to include here. But you really ought to at least try to write a shorter proc and example that is self-contained and illustrates your query and runs stand-alone. In this case it wouldn't take long.

You wrote that you wanted to solve the pde numerically, so I'll address that methodology.

In that case you could try setting up that BC with a call to a black-box procedure, by which I mean a procedure that returns the numeric integration result when passed a numeric input, but for which a symbolic function call returns unevaluated.

This technique can also be used for some other flavors of example; ie. the black-box procedure doesn't necessarily involve an integral.

restart;

pde := diff(v(x, t), t) = diff(v(x, t), x, x);

diff(v(x, t), t) = diff(diff(v(x, t), x), x)

f := x -> exp(-x^2);

proc (x) options operator, arrow; exp(-x^2) end proc

a := -1.68858; b := 1.68858; delx := 1e-2;

-1.68858

1.68858

0.1e-1

K := proc(x) #option trace;
 local res, s;
 if not x::numeric then return 'procname'(args); end if;
 res := evalf[15](1 - Int(f(f(s)), s = 0 .. x, method=_d01ajc, digits=15, epsilon=1e-10));
 evalf[10](res);
end proc:

K(x); # unevaluated. like a black box.

K(x)

plot(K(x), x=-2..2);

sol := pdsolve(pde, {v(a, t) = 0, v(b, t) = 0,
                     v(x, 0) = K(x)},
               numeric, range = a .. b, time = t, spacestep = delx):

F[1] := sol:-value(t=1,output=listprocedure):

plot(eval(v(x,t),F[1]), a..b);

sol:-plot3d(t=0..5,x=a..b,axes=boxed);

sol:-animate(t=1,frames=100);

 

 

Download pds_bb.mw

That region can be shaded using the plots:-shadebetween command. (There are several other ways, using say plots:-inequal or plots:-implicitplot)

Just for fun, I played with some other options for the overall look&feel. Adust to taste.

shb.mw

See here the response about disabling scrollable rtables.

Disabling that GUI preference seems to have fixed a similar instance of the problem.

Is this the kind of effect that you're after?

(There might be a terser way; I just edited what you had before...)

restart;

F := proc(ee, LL)
  local Hx,Hy;
  Hx := nprintf(`#mfenced(mrow(%a,mo("⁢"),mi("x")));`,
                Typesetting:-Typeset(Typesetting:-EV(LL[1])));
  Hy := nprintf(`#mfenced(mrow(%a,mo("⁢"),mi("y")));`,
                Typesetting:-Typeset(Typesetting:-EV(LL[2])));
 Typesetting:-mrow(InertForm:-Typeset(InertForm:-Display(eval(eval(InertForm:-MakeInert(factor(ee)), [`%*` = `*`]) = InertForm:-MakeInert(sort(algsubs(b*y = Hy, algsubs(a*x = Hx, ee)), order = plex(Hx,Hy))), `=`~([a, b], LL)), inert = false)), Typesetting:-mo("="), InertForm:-Typeset(eval(ee, `=`~([a, b], LL)))); end proc:

p := a^3*x^3 + 3*a^2*b*x^2*y + 3*a*b^2*x*y^2 + b^3*y^3;

a^3*x^3+3*a^2*b*x^2*y+3*a*b^2*x*y^2+b^3*y^3

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

[[2, 3], [1, 2], [1/3, -2^(1/2)]]

ans := F~(p, L);

[Typesetting:-mrow(Typesetting:-mrow(Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mrow(Typesetting:-mn("2"), Typesetting:-mo("⁢"), Typesetting:-mi("x")), Typesetting:-mo("+"), Typesetting:-mrow(Typesetting:-mn("3"), Typesetting:-mo("⁢"), Typesetting:-mi("y"))), Typesetting:-_Hold([`%+`(2*x, 3*y)])))), Typesetting:-mn("3"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`%+`(2*x, 3*y), 3)])), Typesetting:-mo("="), Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("2"), Typesetting:-mo("⁢"), Typesetting:-mi("x")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("3"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("x")));`, 3)])), Typesetting:-mo("+"), Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mn("3"), Typesetting:-mo("⋅"), Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("2"), Typesetting:-mo("⁢"), Typesetting:-mi("x")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("2"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("x")));`, 2)])), Typesetting:-mo("⋅"), Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("3"), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-msemantics = "atomic")), Typesetting:-_Hold([`%*`(3, `%^`(`#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting:-mn("3"),mo("⁢"),mi("y")));`)])), Typesetting:-mo("+"), Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mn("3"), Typesetting:-mo("⋅"), Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("2"), Typesetting:-mo("⁢"), Typesetting:-mi("x")), Typesetting:-msemantics = "atomic"), Typesetting:-mo("⋅"), Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("3"), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("2"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mn("3"),mo("⁢"),mi("y")));`, 2)]))), Typesetting:-_Hold([`%*`(3, `#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting:-mn("3"),mo("⁢"),mi("y")));`, 2))])), Typesetting:-mo("+"), Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("3"), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("3"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mn("3"),mo("⁢"),mi("y")));`, 3)]))), Typesetting:-_Hold([`%+`(`%^`(`#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("x")));`, 3), `%*`(3, `%^`(`#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting:-mn("3"),mo("⁢"),mi("y")));`), `%*`(3, `#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting:-mn("3"),mo("⁢"),mi("y")));`, 2)), `%^`(`#mfenced(mrow(Typesetting:-mn("3"),mo("⁢"),mi("y")));`, 3))]))), Typesetting:-mo("="), Typesetting:-mrow(Typesetting:-mrow(Typesetting:-mn("8"), Typesetting:-mo("⁢"), Typesetting:-msup(Typesetting:-mi("x"), Typesetting:-mn("3"))), Typesetting:-mo("+"), Typesetting:-mrow(Typesetting:-mn("36"), Typesetting:-mo("⁢"), Typesetting:-msup(Typesetting:-mi("x"), Typesetting:-mn("2")), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-mo("+"), Typesetting:-mrow(Typesetting:-mn("54"), Typesetting:-mo("⁢"), Typesetting:-mi("x"), Typesetting:-mo("⁢"), Typesetting:-msup(Typesetting:-mi("y"), Typesetting:-mn("2"))), Typesetting:-mo("+"), Typesetting:-mrow(Typesetting:-mn("27"), Typesetting:-mo("⁢"), Typesetting:-msup(Typesetting:-mi("y"), Typesetting:-mn("3"))))), Typesetting:-mrow(Typesetting:-mrow(Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mi("x"), Typesetting:-mo("+"), Typesetting:-mrow(Typesetting:-mn("2"), Typesetting:-mo("⁢"), Typesetting:-mi("y"))), Typesetting:-_Hold([`%+`(x, 2*y)])))), Typesetting:-mn("3"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`%+`(x, 2*y), 3)])), Typesetting:-mo("="), Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("1"), Typesetting:-mo("⁢"), Typesetting:-mi("x")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("3"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mn("1"),mo("⁢"),mi("x")));`, 3)])), Typesetting:-mo("+"), Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mn("3"), Typesetting:-mo("⋅"), Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("1"), Typesetting:-mo("⁢"), Typesetting:-mi("x")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("2"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mn("1"),mo("⁢"),mi("x")));`, 2)])), Typesetting:-mo("⋅"), Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("2"), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-msemantics = "atomic")), Typesetting:-_Hold([`%*`(3, `%^`(`#mfenced(mrow(Typesetting:-mn("1"),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("y")));`)])), Typesetting:-mo("+"), Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mn("3"), Typesetting:-mo("⋅"), Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("1"), Typesetting:-mo("⁢"), Typesetting:-mi("x")), Typesetting:-msemantics = "atomic"), Typesetting:-mo("⋅"), Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("2"), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("2"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("y")));`, 2)]))), Typesetting:-_Hold([`%*`(3, `#mfenced(mrow(Typesetting:-mn("1"),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("y")));`, 2))])), Typesetting:-mo("+"), Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("2"), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("3"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("y")));`, 3)]))), Typesetting:-_Hold([`%+`(`%^`(`#mfenced(mrow(Typesetting:-mn("1"),mo("⁢"),mi("x")));`, 3), `%*`(3, `%^`(`#mfenced(mrow(Typesetting:-mn("1"),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("y")));`), `%*`(3, `#mfenced(mrow(Typesetting:-mn("1"),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("y")));`, 2)), `%^`(`#mfenced(mrow(Typesetting:-mn("2"),mo("⁢"),mi("y")));`, 3))]))), Typesetting:-mo("="), Typesetting:-mrow(Typesetting:-msup(Typesetting:-mi("x"), Typesetting:-mn("3")), Typesetting:-mo("+"), Typesetting:-mrow(Typesetting:-mn("6"), Typesetting:-mo("⁢"), Typesetting:-msup(Typesetting:-mi("x"), Typesetting:-mn("2")), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-mo("+"), Typesetting:-mrow(Typesetting:-mn("12"), Typesetting:-mo("⁢"), Typesetting:-mi("x"), Typesetting:-mo("⁢"), Typesetting:-msup(Typesetting:-mi("y"), Typesetting:-mn("2"))), Typesetting:-mo("+"), Typesetting:-mrow(Typesetting:-mn("8"), Typesetting:-mo("⁢"), Typesetting:-msup(Typesetting:-mi("y"), Typesetting:-mn("3"))))), Typesetting:-mrow(Typesetting:-mrow(Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mfrac(Typesetting:-mi("x"), Typesetting:-mn("3")), Typesetting:-mo("−"), Typesetting:-mrow(Typesetting:-msqrt(Typesetting:-mn("2")), Typesetting:-mo("⁢"), Typesetting:-mi("y"))), Typesetting:-_Hold([`%+`((1/3)*x, -2^(1/2)*y)])))), Typesetting:-mn("3"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`%+`((1/3)*x, -2^(1/2)*y), 3)])), Typesetting:-mo("="), Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mfrac(Typesetting:-mn("1"), Typesetting:-mn("3")), Typesetting:-mo("⁢"), Typesetting:-mi("x")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("3"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mfrac(Typesetting:-mn("1"),Typesetting:-mn("3")),mo("⁢"),mi("x")));`, 3)])), Typesetting:-mo("+"), Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mn("3"), Typesetting:-mo("⋅"), Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mfrac(Typesetting:-mn("1"), Typesetting:-mn("3")), Typesetting:-mo("⁢"), Typesetting:-mi("x")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("2"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mfrac(Typesetting:-mn("1"),Typesetting:-mn("3")),mo("⁢"),mi("x")));`, 2)])), Typesetting:-mo("⋅"), Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mo("&uminus0;"), Typesetting:-msqrt(Typesetting:-mn("2")), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-msemantics = "atomic")), Typesetting:-_Hold([`%*`(3, `%^`(`#mfenced(mrow(Typesetting:-mfrac(Typesetting:-mn("1"),Typesetting:-mn("3")),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting:-mrow(Typesetting:-mo("&uminus0;"),Typesetting:-msqrt(Typesetting:-mn("2"))),mo("⁢"),mi("y")));`)])), Typesetting:-mo("+"), Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mn("3"), Typesetting:-mo("⋅"), Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mfrac(Typesetting:-mn("1"), Typesetting:-mn("3")), Typesetting:-mo("⁢"), Typesetting:-mi("x")), Typesetting:-msemantics = "atomic"), Typesetting:-mo("⋅"), Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mo("&uminus0;"), Typesetting:-msqrt(Typesetting:-mn("2")), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("2"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mrow(Typesetting:-mo("&uminus0;"),Typesetting:-msqrt(Typesetting:-mn("2"))),mo("⁢"),mi("y")));`, 2)]))), Typesetting:-_Hold([`%*`(3, `#mfenced(mrow(Typesetting:-mfrac(Typesetting:-mn("1"),Typesetting:-mn("3")),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting:-mrow(Typesetting:-mo("&uminus0;"),Typesetting:-msqrt(Typesetting:-mn("2"))),mo("⁢"),mi("y")));`, 2))])), Typesetting:-mo("+"), Typesetting:-mcomplete(Typesetting:-msup(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mo("&uminus0;"), Typesetting:-msqrt(Typesetting:-mn("2")), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-msemantics = "atomic")), Typesetting:-mn("3"), Typesetting:-msemantics = "^"), Typesetting:-_Hold([`%^`(`#mfenced(mrow(Typesetting:-mrow(Typesetting:-mo("&uminus0;"),Typesetting:-msqrt(Typesetting:-mn("2"))),mo("⁢"),mi("y")));`, 3)]))), Typesetting:-_Hold([`%+`(`%^`(`#mfenced(mrow(Typesetting:-mfrac(Typesetting:-mn("1"),Typesetting:-mn("3")),mo("⁢"),mi("x")));`, 3), `%*`(3, `%^`(`#mfenced(mrow(Typesetting:-mfrac(Typesetting:-mn("1"),Typesetting:-mn("3")),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting:-mrow(Typesetting:-mo("&uminus0;"),Typesetting:-msqrt(Typesetting:-mn("2"))),mo("⁢"),mi("y")));`), `%*`(3, `#mfenced(mrow(Typesetting:-mfrac(Typesetting:-mn("1"),Typesetting:-mn("3")),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting:-mrow(Typesetting:-mo("&uminus0;"),Typesetting:-msqrt(Typesetting:-mn("2"))),mo("⁢"),mi("y")));`, 2)), `%^`(`#mfenced(mrow(Typesetting:-mrow(Typesetting:-mo("&uminus0;"),Typesetting:-msqrt(Typesetting:-mn("2"))),mo("⁢"),mi("y")));`, 3))]))), Typesetting:-mo("="), Typesetting:-mrow(Typesetting:-mfrac(Typesetting:-msup(Typesetting:-mi("x"), Typesetting:-mn("3")), Typesetting:-mn("27")), Typesetting:-mo("−"), Typesetting:-mfrac(Typesetting:-mrow(Typesetting:-msqrt(Typesetting:-mn("2")), Typesetting:-mo("⁢"), Typesetting:-msup(Typesetting:-mi("x"), Typesetting:-mn("2")), Typesetting:-mo("⁢"), Typesetting:-mi("y")), Typesetting:-mn("3")), Typesetting:-mo("+"), Typesetting:-mrow(Typesetting:-mn("2"), Typesetting:-mo("⁢"), Typesetting:-mi("x"), Typesetting:-mo("⁢"), Typesetting:-msup(Typesetting:-mi("y"), Typesetting:-mn("2"))), Typesetting:-mo("−"), Typesetting:-mrow(Typesetting:-mn("2"), Typesetting:-mo("⁢"), Typesetting:-msqrt(Typesetting:-mn("2")), Typesetting:-mo("⁢"), Typesetting:-msup(Typesetting:-mi("y"), Typesetting:-mn("3")))))]

print~(ans):

`%^`(`%+`(2*x, 3*y), 3) = `%+`(`%^`(`#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("x")));`, 3), `%*`(3, `%^`(`#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting`:-`mn("3"),mo("⁢"),mi("y")));`), `%*`(3, `#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting`:-`mn("3"),mo("⁢"),mi("y")));`, 2)), `%^`(`#mfenced(mrow(Typesetting`:-`mn("3"),mo("⁢"),mi("y")));`, 3)) and `%+`(`%^`(`#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("x")));`, 3), `%*`(3, `%^`(`#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting`:-`mn("3"),mo("⁢"),mi("y")));`), `%*`(3, `#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting`:-`mn("3"),mo("⁢"),mi("y")));`, 2)), `%^`(`#mfenced(mrow(Typesetting`:-`mn("3"),mo("⁢"),mi("y")));`, 3)) = 8*x^3+36*x^2*y+54*x*y^2+27*y^3

`%^`(`%+`(x, 2*y), 3) = `%+`(`%^`(`#mfenced(mrow(Typesetting`:-`mn("1"),mo("⁢"),mi("x")));`, 3), `%*`(3, `%^`(`#mfenced(mrow(Typesetting`:-`mn("1"),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("y")));`), `%*`(3, `#mfenced(mrow(Typesetting`:-`mn("1"),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("y")));`, 2)), `%^`(`#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("y")));`, 3)) and `%+`(`%^`(`#mfenced(mrow(Typesetting`:-`mn("1"),mo("⁢"),mi("x")));`, 3), `%*`(3, `%^`(`#mfenced(mrow(Typesetting`:-`mn("1"),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("y")));`), `%*`(3, `#mfenced(mrow(Typesetting`:-`mn("1"),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("y")));`, 2)), `%^`(`#mfenced(mrow(Typesetting`:-`mn("2"),mo("⁢"),mi("y")));`, 3)) = x^3+6*x^2*y+12*x*y^2+8*y^3

`%^`(`%+`((1/3)*x, -sqrt(2)*y), 3) = `%+`(`%^`(`#mfenced(mrow(Typesetting`:-`mfrac(Typesetting`:-`mn("1"),Typesetting`:-`mn("3")),mo("⁢"),mi("x")));`, 3), `%*`(3, `%^`(`#mfenced(mrow(Typesetting`:-`mfrac(Typesetting`:-`mn("1"),Typesetting`:-`mn("3")),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting`:-`mrow(Typesetting`:-`mo("&uminus0;"),Typesetting`:-`msqrt(Typesetting`:-`mn("2"))),mo("⁢"),mi("y")));`), `%*`(3, `#mfenced(mrow(Typesetting`:-`mfrac(Typesetting`:-`mn("1"),Typesetting`:-`mn("3")),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting`:-`mrow(Typesetting`:-`mo("&uminus0;"),Typesetting`:-`msqrt(Typesetting`:-`mn("2"))),mo("⁢"),mi("y")));`, 2)), `%^`(`#mfenced(mrow(Typesetting`:-`mrow(Typesetting`:-`mo("&uminus0;"),Typesetting`:-`msqrt(Typesetting`:-`mn("2"))),mo("⁢"),mi("y")));`, 3)) and `%+`(`%^`(`#mfenced(mrow(Typesetting`:-`mfrac(Typesetting`:-`mn("1"),Typesetting`:-`mn("3")),mo("⁢"),mi("x")));`, 3), `%*`(3, `%^`(`#mfenced(mrow(Typesetting`:-`mfrac(Typesetting`:-`mn("1"),Typesetting`:-`mn("3")),mo("⁢"),mi("x")));`, 2), `#mfenced(mrow(Typesetting`:-`mrow(Typesetting`:-`mo("&uminus0;"),Typesetting`:-`msqrt(Typesetting`:-`mn("2"))),mo("⁢"),mi("y")));`), `%*`(3, `#mfenced(mrow(Typesetting`:-`mfrac(Typesetting`:-`mn("1"),Typesetting`:-`mn("3")),mo("⁢"),mi("x")));`, `%^`(`#mfenced(mrow(Typesetting`:-`mrow(Typesetting`:-`mo("&uminus0;"),Typesetting`:-`msqrt(Typesetting`:-`mn("2"))),mo("⁢"),mi("y")));`, 2)), `%^`(`#mfenced(mrow(Typesetting`:-`mrow(Typesetting`:-`mo("&uminus0;"),Typesetting`:-`msqrt(Typesetting`:-`mn("2"))),mo("⁢"),mi("y")));`, 3)) = (1/27)*x^3-(1/3)*sqrt(2)*x^2*y+2*x*y^2-2*sqrt(2)*y^3; "_noterminate"


Download minhthien_ts.mw

I have used Maple 2024.1 below.

You can use fprintf instead of printf, if you want to write to a file.

(please check for typos.)

restart:

F := proc(ee,LL)
  uses InertForm, Typesetting;
  mrow(Typeset(Display(eval(eval(MakeInert(factor(ee)),[`%*`=`*`])
                                 =MakeInert(subs(b=MakeInert(b*y)/y,
                        a=MakeInert(a*x)/x,p)),[a,b]=~LL),
                       inert=false)),
       mo("="),Typeset(eval(ee,[a,b]=~LL)))
end proc:
p := (a*x)^2 - 2*a*x*b*y + (b*y)^2:
L := [[sqrt(2),3],[2,5],[3,12],[1/3,5/7]]:
ans := F~(p, L):

 

print~(ans):

`%^`(`%+`(sqrt(2)*x, -3*y), 2) = `%+`(`%^`(`%*`(sqrt(2), x), 2), `%*`(-2, `%*`(sqrt(2), x), `%*`(3, y)), `%^`(`%*`(3, y), 2)) and `%+`(`%^`(`%*`(sqrt(2), x), 2), `%*`(-2, `%*`(sqrt(2), x), `%*`(3, y)), `%^`(`%*`(3, y), 2)) = 2*x^2-6*sqrt(2)*x*y+9*y^2

`%^`(`%+`(2*x, -5*y), 2) = `%+`(`%^`(`%*`(2, x), 2), `%*`(-2, `%*`(2, x), `%*`(5, y)), `%^`(`%*`(5, y), 2)) and `%+`(`%^`(`%*`(2, x), 2), `%*`(-2, `%*`(2, x), `%*`(5, y)), `%^`(`%*`(5, y), 2)) = 4*x^2-20*x*y+25*y^2

`%^`(`%+`(3*x, -12*y), 2) = `%+`(`%^`(`%*`(3, x), 2), `%*`(-2, `%*`(3, x), `%*`(12, y)), `%^`(`%*`(12, y), 2)) and `%+`(`%^`(`%*`(3, x), 2), `%*`(-2, `%*`(3, x), `%*`(12, y)), `%^`(`%*`(12, y), 2)) = 9*x^2-72*x*y+144*y^2

`%^`(`%+`((1/3)*x, -(5/7)*y), 2) = `%+`(`%^`(`%*`(1/3, x), 2), `%*`(-2, `%*`(1/3, x), `%*`(5/7, y)), `%^`(`%*`(5/7, y), 2)) and `%+`(`%^`(`%*`(1/3, x), 2), `%*`(-2, `%*`(1/3, x), `%*`(5/7, y)), `%^`(`%*`(5/7, y), 2)) = (1/9)*x^2-(10/21)*x*y+(25/49)*y^2

RR := cat(
"\n\\documentclass[12pt,a4paper]{article}
\\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\\usepackage{amsmath}
\\usepackage{amsthm}
\\usepackage{enumitem}
\\theoremstyle{definition}
\\newtheorem{ex}{Exercise}
\\begin{document}\n\n",
seq(sprintf("\\begin{ex}\n\\[ %s \\]\n\\end{ex}\n",
            latex(ee, output=string)),ee=ans),
"\n\\end{document}"):

 

printf(RR);


\documentclass[12pt,a4paper]{article}
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{enumitem}
\theoremstyle{definition}
\newtheorem{ex}{Exercise}
\begin{document}

\begin{ex}
\[ \left(\sqrt{2}\, x -3 y \right)^{2}=\left(\sqrt{2}\cdot x \right)^{2}-2\cdot \left(\sqrt{2}\cdot x \right)\cdot \left(3\cdot y \right)+\left(3\cdot y \right)^{2}=2 x^{2}-6 \sqrt{2}\, x y +9 y^{2} \]
\end{ex}
\begin{ex}
\[ \left(2 x -5 y \right)^{2}=\left(2\cdot x \right)^{2}-2\cdot \left(2\cdot x \right)\cdot \left(5\cdot y \right)+\left(5\cdot y \right)^{2}=4 x^{2}-20 x y +25 y^{2} \]
\end{ex}
\begin{ex}
\[ \left(3 x -12 y \right)^{2}=\left(3\cdot x \right)^{2}-2\cdot \left(3\cdot x \right)\cdot \left(12\cdot y \right)+\left(12\cdot y \right)^{2}=9 x^{2}-72 x y +144 y^{2} \]
\end{ex}
\begin{ex}
\[ \left(\frac{x}{3}-\frac{5 y}{7}\right)^{2}=\left(\frac{1}{3}\cdot x \right)^{2}-2\cdot \left(\frac{1}{3}\cdot x \right)\cdot \left(\frac{5}{7}\cdot y \right)+\left(\frac{5}{7}\cdot y \right)^{2}=\frac{1}{9} x^{2}-\frac{10}{21} x y +\frac{25}{49} y^{2} \]
\end{ex}

\end{document}

 

 

Download minhthien_latex.mw

Does this behavior get affected/improved if you change the interface(rtablesize) setting?

Does this behaviour get affected/improved if you manage to disable the (new in M2024) so-called scrollable rtable/Matrix/Vector display?

ps. I have changed your Post into a Question.

See the Comparing sum and add section on the ?sum,details Help page.

And also note that the add command has special evaluation rules on its first parameter.

Also, your myfac procedure can be done using mul (or add of the powers), instead of a loop. A symbolic formula for it can also be constructed using the product (or sum) commands.

Here is an explanation of how it was going wrong for you (note the error message's meaning), as well as alternatives mentioned.

NULL

restart

myfac := proc (n::nonnegint) local out, i, a, q; a := [3]; q := 2; out := a[1]; for i from 0 to n do out := out*q^i end do; out end proc

myfac(4)

3072

values := [seq(myfac(i), i = 0 .. 3)]

[3, 6, 24, 192]

add(values)

225

sum(myfac(i), i = 0 .. 3)

Error, invalid input: myfac expects its 1st argument, n, to be of type nonnegint, but received i

myfac(i)

Error, invalid input: myfac expects its 1st argument, n, to be of type nonnegint, but received i

add(myfac(i), i = 0 .. 3)

225

sum(('myfac')(i), i = 0 .. 3)

225

factor(sum(i, i = 1 .. n))

(1/2)*n*(n+1)

Alternate way to write myfac.
nb. Another is to use 3*2^add(i,i=0..n)
myfac2 := proc (n) local i; 3*mul(2^i, i = 0 .. n) end proc

seq(myfac2(i), i = 0 .. 3)

3, 6, 24, 192

Symbolic formula for myfac,
using product instead of mul
formula := 3*(product(2^i, i = 0 .. n)); formula := factor(formula); myfac3 := unapply(formula, n)

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

3*2^((1/2)*n*(n+1))

proc (n) options operator, arrow; 3*2^((1/2)*n*(n+1)) end proc

seq(myfac3(i), i = 0 .. 3)

3, 6, 24, 192

add(myfac3(i), i = 0 .. 3)

225

Note also,

factor(sum(i, i = 0 .. n)); 3*2^%

(1/2)*n*(n+1)

3*2^((1/2)*n*(n+1))

NULL

Download sum_of_geometric_sequence_ac.mw

Is this more what you're after? (Using Maple 2021.2)

I notice that your 2D Input is actually entered using the sqrt command. So one alternative (to fixing it up after the fact) might be to instead enter those with (...)^(1/2) instead of sqrt(...).

restart

K := {w = a[3]*(9*lambda*a[4]*a[5]-2*a[3]^2)/(54*a[4]^2), a[1] = 0, a[2] = -(3*lambda*a[4]*a[5]-2*a[3]^2)/(6*a[4]), alpha[0] = -a[3]/(3*a[4]), alpha[1] = `&-+`(sqrt(-a[5]/(2*a[4]))), beta[0] = `&-+`(sqrt(-(lambda^2*B[1]^2*a[5]-lambda^2*B[2]^2*a[5]-mu^2*a[5])/(2*lambda*a[4])))}

{w = (1/54)*a[3]*(9*lambda*a[4]*a[5]-2*a[3]^2)/a[4]^2, a[1] = 0, a[2] = -(1/6)*(3*lambda*a[4]*a[5]-2*a[3]^2)/a[4], alpha[0] = -(1/3)*a[3]/a[4], alpha[1] = `&-+`((1/2)*(-2*a[5]/a[4])^(1/2)), beta[0] = `&-+`((1/2)*(-2*(lambda^2*B[1]^2*a[5]-lambda^2*B[2]^2*a[5]-mu^2*a[5])/(lambda*a[4]))^(1/2))}

subsindets(K, `&*`(rational, anything^(1/2)), proc (u) options operator, arrow; (u^2)^(1/2) end proc)

{w = (1/54)*a[3]*(9*lambda*a[4]*a[5]-2*a[3]^2)/a[4]^2, a[1] = 0, a[2] = -(1/6)*(3*lambda*a[4]*a[5]-2*a[3]^2)/a[4], alpha[0] = -(1/3)*a[3]/a[4], alpha[1] = `&-+`((-(1/2)*a[5]/a[4])^(1/2)), beta[0] = `&-+`((-(1/2)*(lambda^2*B[1]^2*a[5]-lambda^2*B[2]^2*a[5]-mu^2*a[5])/(lambda*a[4]))^(1/2))}

latex(%)

\left\{w =
\frac{a_{3} \left(9 \lambda  a_{4} a_{5}-2 a_{3}^{2}\right)}{54 a_{4}^{2}}
, a_{1} = 0, a_{2} =
-\frac{3 \lambda  a_{4} a_{5}-2 a_{3}^{2}}{6 a_{4}}, \alpha_{0} =
-\frac{a_{3}}{3 a_{4}}, \alpha_{1} = \mp \sqrt{-\frac{a_{5}}{2 a_{4}}}
, \beta_{0} =
\mp \sqrt{-\frac{\lambda^{2} B_{1}^{2} a_{5}-\lambda^{2} B_{2}^{2} a_{5}-\mu^{2} a_{5}}{2 \lambda  a_{4}}}
\right\}

KK := {w = a[3]*(9*lambda*a[4]*a[5]-2*a[3]^2)/(54*a[4]^2), a[1] = 0, a[2] = -(3*lambda*a[4]*a[5]-2*a[3]^2)/(6*a[4]), alpha[0] = -a[3]/(3*a[4]), alpha[1] = `&-+`((-a[5]/(2*a[4]))^(1/2)), beta[0] = `&-+`((-(lambda^2*B[1]^2*a[5]-lambda^2*B[2]^2*a[5]-mu^2*a[5])/(2*lambda*a[4]))^(1/2))}

{w = (1/54)*a[3]*(9*lambda*a[4]*a[5]-2*a[3]^2)/a[4]^2, a[1] = 0, a[2] = -(1/6)*(3*lambda*a[4]*a[5]-2*a[3]^2)/a[4], alpha[0] = -(1/3)*a[3]/a[4], alpha[1] = `&-+`((-(1/2)*a[5]/a[4])^(1/2)), beta[0] = `&-+`((-(1/2)*(lambda^2*B[1]^2*a[5]-lambda^2*B[2]^2*a[5]-mu^2*a[5])/(lambda*a[4]))^(1/2))}

latex(KK)

\left\{w =
\frac{a_{3} \left(9 \lambda  a_{4} a_{5}-2 a_{3}^{2}\right)}{54 a_{4}^{2}}
, a_{1} = 0, a_{2} =
-\frac{3 \lambda  a_{4} a_{5}-2 a_{3}^{2}}{6 a_{4}}, \alpha_{0} =
-\frac{a_{3}}{3 a_{4}}, \alpha_{1} = \mp \sqrt{-\frac{a_{5}}{2 a_{4}}}
, \beta_{0} =
\mp \sqrt{-\frac{\lambda^{2} B_{1}^{2} a_{5}-\lambda^{2} B_{2}^{2} a_{5}-\mu^{2} a_{5}}{2 \lambda  a_{4}}}
\right\}

NULL

Download latex2_ac.mw

I don't see num_steps defined or assigned. Perhaps you intend that as, say, ceil((tmax - 0)/dt) or similar.

You don't ever evalf the contents of U0, so everything blows up as enormous symbolic expressions. That's the basic reason why it appeared to take forever. I expect that you intended to utilize floating-point values.

You can use Tabulate to overwrite (in-place) an output of the running plotU. If you want all the intermediate plots shown then you could use print(plotU) instead of your original display(plotU). But note that your original display(plotU) wasn't being assigned to anything. If instead you were to assign all the individual plotU plots as entries in a table TT then after the loop you could display(entries(TT),insequence) and get an animation.

Numerical_Simulation_of_discrete_Klein-Gordon_Equation_ac.mw

ps. I have not revised or examined the underlying algorithm or its implementation. Commenting the code better might be useful.

It is more generally straightforward to programmatically access the results from the ifactors command, rather than from the ifactor command.

x := ifactors(6);

[1, [[2, 1], [3, 1]]]

x[2];

[[2, 1], [3, 1]]

x[2,1,1]

2

x[2,2,1]

3


Download ifactors_ex.mw

One aspect that makes it more sensible is that the methodology can be consistent whether the factors have multiplicity 1 or greater than 1. Eg, whether the original is say 6, or 72. But with ifactor the deconstruction would need a separate case for whether the power was 1 (in which case not actually present) or greater than 1.

a := "x^2";

"x^2"

StringTools:-RegSplit("\\^",a);

"x", "2"


Download regsplit_ex.mw

The codegen:-cost command does this computation (with addition and subtraction having the same "cost").

The individual numbers can be extracted in a usual manner, using coeff.

A := (g*m*s*y-g*m*t*x-g*n*r*y+g*n*t*w+g*o*r*x-g*o*s*w-h*l*s*y+h*l*t*x+h*n*q*y-h*n*t*v-h*o*q*x+h*o*s*v+h2*l*r*y-h2*l*t*w-h2*m*q*y+h2*m*t*v+h2*o*q*w-h2*o*r*v-j*l*r*x+j*l*s*w+j*m*q*x-j*m*s*v-j*n*q*w+j*n*r*v)/(c*h2*o*p*v+d*f*m*q*y-b*h*o*s*u-b*h2*k*r*y+b*h2*k*t*w+d*f*l*t*w-d*f*l*r*y+d*h*k*t*v+d2*f*l*r*x-d*j*m*q*u+d2*g*k*s*w-d2*g*k*r*x+b*h*o*p*x+b*h*n*t*u-b*h*n*p*y+c*f*o*q*x-d*h*k*q*y-d*g*o*r*u+d*g*o*p*w+d*g*m*t*u+c*j*n*q*u-c*j*n*p*v-c*j*l*s*u+c*j*l*p*x-c*g*k*s*y-c*f*o*s*v-d2*h2*k*q*w+d2*h2*k*r*v-c*f*n*q*y-c*f*l*t*x+c*f*l*s*y-b*j*n*r*u-b*f*o*r*x+c*g*n*p*y+c*g*k*t*x+a*h2*l*r*y-a*h2*l*t*w+c*h2*k*q*y+c*g*o*s*u-c*g*o*p*x-c*g*n*t*u-d*j*l*p*w-a*h*o*q*x+a*h*o*s*v+a*h*l*t*x+a*h*n*q*y-a*h*n*t*v+a*g*o*r*x-a*g*o*s*w-a*h*l*s*y+a*g*m*s*y-a*g*m*t*x-a*g*n*r*y+a*g*n*t*w+d*j*m*p*v+b*j*n*p*w+b*j*m*s*u-b*j*m*p*x+d*j*l*r*u+d*h*o*q*u+d*j*k*q*w-d*j*k*r*v-d2*h*l*p*x-d2*f*l*s*w-d2*f*m*q*x+d2*f*m*s*v+d2*f*n*q*w+d2*h2*l*p*w-d2*h2*l*r*u-d2*h2*m*p*v-a*j*l*r*x+a*j*l*s*w+a*j*m*q*x-a*j*m*s*v-a*j*n*q*w+a*j*n*r*v-b*f*m*s*y+b*f*m*t*x+b*f*n*r*y-b*f*n*t*w+b*f*o*s*w-d2*h*k*s*v+c*f*n*t*v-a*h2*m*q*y+a*h2*m*t*v+a*h2*o*q*w-a*h2*o*r*v-d2*f*n*r*v-d2*h*n*q*u+d2*h*k*q*x+d2*g*n*r*u-d2*g*n*p*w+b*h*k*s*y-d2*g*m*s*u+d2*h*n*p*v+c*h2*l*t*u-c*h2*l*p*y-b*h*k*t*x-b*j*k*s*w+b*j*k*r*x-c*h2*o*q*u-c*j*k*q*x+c*j*k*s*v+b*h2*o*r*u-b*h2*o*p*w+d2*g*m*p*x-c*h2*k*t*v-b*h2*m*t*u+b*h2*m*p*y-d*g*m*p*y-d*g*k*t*w+d*g*k*r*y+d*f*o*r*v-d*h*o*p*v+d2*h2*m*q*u-d*h*l*t*u+d2*h*l*s*u+d*h*l*p*y-d*f*o*q*w-d*f*m*t*v)

raw := codegen:-cost(A)

142*additions+552*multiplications+divisions

lprint(raw)

142*additions+552*multiplications+divisions

`~`[coeff](raw, [additions, multiplications, divisions])

[142, 552, 1]


Download Count_the_number_of_operations_in_A_ac.mw

note: If you do it by way of conversion to string then be wary of any special names that might contain the characters of the relevant operators. (That doesn't affect your example, of course.)

You might also be interested in some means of reducting of these numbers.

restart

A := (g*m*s*y-g*m*t*x-g*n*r*y+g*n*t*w+g*o*r*x-g*o*s*w-h*l*s*y+h*l*t*x+h*n*q*y-h*n*t*v-h*o*q*x+h*o*s*v+h2*l*r*y-h2*l*t*w-h2*m*q*y+h2*m*t*v+h2*o*q*w-h2*o*r*v-j*l*r*x+j*l*s*w+j*m*q*x-j*m*s*v-j*n*q*w+j*n*r*v)/(c*h2*l*t*u-c*h2*k*t*v+c*h2*k*q*y+d2*g*k*s*w-b*j*k*s*w-d*j*k*r*v-d*h*k*q*y-d2*h2*k*q*w-d*g*k*t*w+d*g*k*r*y+d*f*o*r*v-c*h2*l*p*y-d2*f*m*q*x+d2*f*m*s*v+d2*f*n*q*w-d2*f*n*r*v-d2*g*k*r*x+d*f*l*t*w+d*f*m*q*y-d*f*m*t*v-d2*g*n*p*w+b*h2*m*p*y-d*g*o*r*u+d2*h*l*s*u+c*g*k*t*x-c*g*k*s*y-c*f*o*s*v+c*f*o*q*x+c*f*n*t*v-c*f*n*q*y-c*f*l*t*x+c*f*l*s*y-b*j*n*r*u-d*f*o*q*w+a*j*n*r*v-b*f*m*s*y-c*g*n*t*u+c*g*n*p*y-d*j*m*q*u-b*h2*m*t*u-b*h2*o*p*w+d*j*m*p*v+d*j*l*r*u-b*j*m*p*x+b*j*m*s*u+b*j*n*p*w+c*h2*o*p*v-d*h*o*p*v-d2*g*m*s*u+d2*g*m*p*x+a*j*m*q*x+a*j*l*s*w-a*h2*o*r*v-a*j*l*r*x+a*h2*o*q*w-a*h2*m*q*y+a*h2*m*t*v+a*h2*l*r*y-a*h2*l*t*w-a*h*o*q*x+a*h*o*s*v+a*h*n*q*y-a*h*n*t*v-a*g*o*s*w-a*h*l*s*y+a*h*l*t*x-a*g*m*t*x-a*g*n*r*y+a*g*n*t*w+a*g*o*r*x-d*f*l*r*y+b*f*m*t*x+b*f*n*r*y-b*f*n*t*w-b*f*o*r*x+b*f*o*s*w+b*h*k*s*y+c*j*n*q*u-c*j*n*p*v+a*g*m*s*y-d2*h*n*q*u+d2*h*n*p*v-b*h*k*t*x+d*h*o*q*u-d2*h*l*p*x-d2*h*k*s*v+d2*h*k*q*x-c*h2*o*q*u+b*h2*k*t*w-b*h*n*p*y+b*h*n*t*u+d*h*k*t*v+d*h*l*p*y-d*h*l*t*u+b*h*o*p*x-b*h*o*s*u+c*j*k*s*v+c*j*l*p*x-c*j*l*s*u+d*j*k*q*w-b*h2*k*r*y-c*j*k*q*x+d2*h2*m*q*u-a*j*m*s*v-d2*h2*m*p*v+d*g*o*p*w+d*g*m*t*u-d*g*m*p*y-a*j*n*q*w-d2*h2*l*r*u+d2*h2*l*p*w+b*h2*o*r*u+b*j*k*r*x-d*j*l*p*w+d2*g*n*r*u-d2*f*l*s*w+d2*f*l*r*x-c*g*o*p*x+c*g*o*s*u+d2*h2*k*r*v)

raw := codegen:-cost(A)

142*additions+552*multiplications+divisions

new1 := simplify(A, size)

(((-r*y+t*w)*l+(q*y-t*v)*m-o*(q*w-r*v))*h2+((-s*y+t*x)*m+(r*y-t*w)*n-o*(r*x-s*w))*g+((s*y-t*x)*l+(-q*y+t*v)*n+o*(q*x-s*v))*h+((r*x-s*w)*l+(-q*x+s*v)*m+n*(q*w-r*v))*j)/((((-a*y+d2*u)*r+(a*w-c*u)*t-p*(-c*y+d2*w))*l+((a*y-d2*u)*q+(-a*v+b*u)*t-p*(b*y-d2*v))*m+((-a*w+c*u)*q+(a*v-b*u)*r+p*(b*w-c*v))*o+k*((-c*y+d2*w)*q+(b*y-d2*v)*r-t*(b*w-c*v)))*h2+(((-a*y+d2*u)*s+(a*x-d*u)*t-p*(-d*y+d2*x))*m+((a*y-d2*u)*r+(-a*w+c*u)*t+p*(-c*y+d2*w))*n+((-a*x+d*u)*r+(a*w-c*u)*s-p*(-c*x+d*w))*o+k*((-d*y+d2*x)*r+(c*y-d2*w)*s+t*(-c*x+d*w)))*g+(((a*y-d2*u)*s+(-a*x+d*u)*t+p*(-d*y+d2*x))*l+((-a*y+d2*u)*q+(a*v-b*u)*t+p*(b*y-d2*v))*n+((a*x-d*u)*q+(-a*v+b*u)*s-p*(b*x-d*v))*o-k*((-d*y+d2*x)*q+(b*y-d2*v)*s-t*(b*x-d*v)))*h+(((a*x-d*u)*r+(-a*w+c*u)*s+p*(-c*x+d*w))*l+((-a*x+d*u)*q+(a*v-b*u)*s+p*(b*x-d*v))*m+((a*w-c*u)*q+(-a*v+b*u)*r-p*(b*w-c*v))*n-k*((-c*x+d*w)*q+(b*x-d*v)*r-s*(b*w-c*v)))*j-f*(((-d*y+d2*x)*r+(c*y-d2*w)*s+t*(-c*x+d*w))*l+((d*y-d2*x)*q+(-b*y+d2*v)*s+t*(b*x-d*v))*m+((-c*y+d2*w)*q+(b*y-d2*v)*r-t*(b*w-c*v))*n-((-c*x+d*w)*q+(b*x-d*v)*r-s*(b*w-c*v))*o))

codegen:-cost(new1)

142*additions+245*multiplications+divisions

vars := indets(A, And(name, Not(constant))); new2 := (codegen:-optimize(unapply(A, vars[]), tryhard))(vars[])

(g*((s*y-t*x)*m-(r*y-t*w)*n+o*(r*x-s*w))-h*((s*y-t*x)*l-(q*y-t*v)*n+o*(q*x-s*v))+h2*((r*y-t*w)*l-(q*y-t*v)*m+o*(q*w-r*v))-j*((r*x-s*w)*l-(q*x-s*v)*m+n*(q*w-r*v)))/((f*((r*x-s*w)*l-(q*x-s*v)*m+n*(q*w-r*v))-g*((r*x-s*w)*k-(p*x-s*u)*m+(p*w-r*u)*n)+h*((q*x-s*v)*k-(p*x-s*u)*l+(p*v-q*u)*n)-h2*((q*w-r*v)*k-(p*w-r*u)*l+(p*v-q*u)*m))*d2+(-f*((r*y-t*w)*l-(q*y-t*v)*m+o*(q*w-r*v))+g*((r*y-t*w)*k-(p*y-t*u)*m+(p*w-r*u)*o)-h*((q*y-t*v)*k-(p*y-t*u)*l+(p*v-q*u)*o)+j*((q*w-r*v)*k-(p*w-r*u)*l+(p*v-q*u)*m))*d+(f*((s*y-t*x)*l-(q*y-t*v)*n+o*(q*x-s*v))-g*((s*y-t*x)*k-(p*y-t*u)*n+(p*x-s*u)*o)+h2*((q*y-t*v)*k-(p*y-t*u)*l+(p*v-q*u)*o)-j*((q*x-s*v)*k-(p*x-s*u)*l+(p*v-q*u)*n))*c+(-f*((s*y-t*x)*m-(r*y-t*w)*n+o*(r*x-s*w))+h*((s*y-t*x)*k-(p*y-t*u)*n+(p*x-s*u)*o)-h2*((r*y-t*w)*k-(p*y-t*u)*m+(p*w-r*u)*o)+j*((r*x-s*w)*k-(p*x-s*u)*m+(p*w-r*u)*n))*b+(g*((s*y-t*x)*m-(r*y-t*w)*n+o*(r*x-s*w))-h*((s*y-t*x)*l-(q*y-t*v)*n+o*(q*x-s*v))+h2*((r*y-t*w)*l-(q*y-t*v)*m+o*(q*w-r*v))-j*((r*x-s*w)*l-(q*x-s*v)*m+n*(q*w-r*v)))*a)

codegen:-cost(new2)

142*additions+245*multiplications+divisions

LC := MmaTranslator:-Mma:-LeafCount

`~`[LC]([A, new1, new2])

[917, 829, 848]

Download Count_the_number_of_operations_in_A_acc.mw

Sometimes one can beat simplify(...,size) for this kind of thing through special use of collect.

The applyrule command is not receiving what you think, due to usual evaluation rules for procedure calls.

You could do this, instead,

e1 := sin(2*sqrt(a*b)*(t+mu));

sin(2*(a*b)^(1/2)*(t+mu))

applyrule( sin(x::anything) = cos('expand'(x)), e1 );

cos(2*(a*b)^(1/2)*t+2*(a*b)^(1/2)*mu)


Download applyrule_defer.mw

You can use trace to see what applyrule was actually receiving, for your example.

e1 := sin(2*sqrt(a*b)*(t+mu)):
trace(applyrule):

applyrule( sin(x::anything) = cos(expand(x)), e1 );

{--> enter applyrule, args = sin(x::anything) = cos(x), sin(2*(a*b)^(1/2)*(t+mu))
...

The above trace details show that applyrule was actually receiving the first of these next two results as its first argument. But you want it to receive something like the second.

sin(x::anything) = cos(expand(x))
                   sin(x::anything) = cos(x)

sin(x::anything) = cos('expand'(x))
               sin(x::anything) = cos(expand(x))
First 16 17 18 19 20 21 22 Last Page 18 of 336