acer

32385 Reputation

29 Badges

19 years, 339 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

Yes, the parameter names generated by LinearSolve are indexed, and can be found by their type.

You can also programatically generate the base name, as a "new" (previously unassigned) name.

But I don't like your methodology of finding them, as it relies on each such name being present as an entry value (alone) of sol. That might be true as consequence of the LinearSolve algorithm, but it's an unnecessary weakness. And there are also unnecessary mappings and conversions.

You can choose the base name, and pass that to LinearSolve using its free option. And you can even programmatically generate such base names -- unassigned. That's pretty decent, and safe, control.

This alleviates the difficulty you've mentioned with not knowing what name like _t, _t2, etc, that will be indexed in the result. If you don't generate the base name using `tools/genglobal` then LinearSolve will. That's the same mechanism as LinearSolve uses.

So, generate the base name yourself, and pass it with the free option, and then you'll be sure to know what it is.

restart;

randomize():

for iter from 1 to 5 do
  A := LinearAlgebra:-RandomMatrix(10,density=0.15):
  v := Vector(10):

  # This generates an unassigned global name.
  frname := `tools/genglobal`('K');

  sol := LinearAlgebra:-LinearSolve(A,v,free=frname);
  found := indets(sol,'specindex'(frname));
  print(found);
end do:

{K[4], K[5], K[8]}

{K0[4], K0[7]}

{K1[3], K1[5], K1[8], K1[9]}

{K2[3], K2[5]}

{K3[10]}

 

Download specindexLS.mw

You don't need to generate discrete data (and interpolate it), in order to obtain the smooth plot of the lambda operator.

The smooth plot of lambda is assigned to P2, below. You can use the so-called parametric calling sequence of the plot command to get the orientation for lambda(t) versus t.

   plot( [ lambda(t), t, t=0..1 ] );

You don't even need the discrete data here. I included it simply to overlay the point-plot with the smooth curve, to show you that they agree.

restart

lambda := proc (x) options operator, arrow; (-1.565845910+2.393779704*x^2+1.564996800*x^4+1.800900000*x^6)/(x^2+2)^4 end proc

proc (x) options operator, arrow; (-1.565845910+2.393779704*x^2+1.564996800*x^4+1.800900000*x^6)/(x^2+2)^4 end proc

data := [seq([lambda(x), x], x = 0 .. 1, .1)]

[[-0.9786536938e-1, 0], [-0.9445602702e-1, .1], [-0.8473253124e-1, .2], [-0.7004169612e-1, .3], [-0.5215959052e-1, .4], [-0.3283205351e-1, .5], [-0.1345044703e-1, .6], [0.5065808511e-2, .7], [0.2221891338e-1, .8], [0.3780341321e-1, .9], [0.5177568635e-1, 1.0]]

P1 := plot(data, style = point, symbol = solidcircle, symbolsize = 15, color = blue)

P2 := plot([lambda(t), t, t = 0 .. 1])

plots:-display(P1, P2)

 

Download help2_acc.mw

Yes, as you mention you could freeze the function calls. The type system lets you target them quite well, and so subsindets is useful.

I don't like using frontend for this, since the set of things not to freeze varies by example. It's ad hoc. Consider what you'd need to do to handle,
   {x(0) + y(0) = 5, x(0) - sin(y(0)) = 3}
And now realize that it's much more straightforward to freeze specifically the function calls (of type constant) in question than it is to programmatically determine example by example what kinds of symbolic calls and subexpressions (depending on those function calls) which must be excluded from being frozen.

sys:={x(0) + y(0) = 5, x(0) - y(0) = 3}:

thaw(fsolve(subsindets(sys,And(anyfunc(constant),
                               Not(complex(realcons))),
                       freeze)));

              {x(0) = 4., y(0) = 1.}

sys:={x(0) + y(0) = 5, x(0) - sin(y(0)) = 3}:

thaw(fsolve(subsindets(sys,And(anyfunc(constant),
                               Not(complex(realcons))),
                       freeze)));

            {x(0) = 3.893939842, y(0) = 1.106060158}

Im not a big fan of the idea that these function calls (of type constant) should be considered as "variables". It's unclear what problems might arise, were fsolve to support this directly. Next might come unknowns x(a) and y(b), but then fsolve would need be careful to reject examples with unknown x(a) alongside bare unknown a.

Could you draw it twice, with two different padding sizes and the layout the same (so they are overlaid)?

The colors might be chosen with different chroma but similar intensity, so that the text labels are legible.

restart;

with(GraphTheory):

G := CycleGraph([v__1,v__2,v__3,v__4]):
H:=Graph({{u__1,u__2}}):

DrawGraph(G,size=[250,250],stylesheet=[vertexborder=false,vertexpadding=10,
vertexcolor="Orange",edgethickness=3]);

DrawGraph(H,size=[250,250],
          stylesheet=[vertexborder=false,vertexpadding=10,
                      vertexcolor="Cyan",edgethickness=3]);

GH:=CartesianProduct(G,H):

 

plots:-display(
  DrawGraph(GH,style=spring,
            stylesheet=[vertexborder=false,vertexpadding=4,
                        vertexcolor="Orange",edgethickness=3]),
  DrawGraph(GH,style=spring,
            stylesheet=[vertexborder=false,vertexpadding=14,
                        vertexcolor="Cyan",edgethickness=3])
);

 

Download graph_twice.mw

The evalf (kernel builtin) extension mechanism invokes the extension procedure `evalf/XXX` even when the function call to XXX is a call to the local of that name.

restart;

local gamma:

trace(`evalf/gamma`):

evalf(gamma(1));

{--> enter \`evalf/gamma\`, args = 1

<-- exit \`evalf/gamma\` (now at top level) = -0.7281584548e-1}

-0.7281584548e-1

restart;

proc(x)
  local sin;
  evalf(sin(Pi/4));
end proc();

.7071067813

restart;

m := module()
  option package;
  export sin;
end module:
with(m);

[sin]

K:=sin(Pi/4);
op(0,%);
addressof(%), addressof(:-sin);

sin((1/4)*Pi)

sin

18446884039962474558, 18446884039962016894

evalf(K);

.7071067813

 

Download evalf_extension.mw

I have a very vague recollection of reporting/complaining about this -- but that would be almost two decades ago. IIRC I was concerned with some cases of what happened (unexpectly, for me) under evalhf.

You can enter your expression using the inert operator `%*` , as Kitonum shows.

You can also enter your expression as a string, and use InertForm:-Parse (though it's trickier to target just the powers in question, if part of a larger expression).

InertForm:-Parse("x*x*x*x");

`%*`(x, x, x, x)


And you can cobble together mechanisms to decompose. (Adjust or robustify as needed...)

And you can convert back to the active `*` form, in a few ways.

restart;

expr := x^2 + 1/y^5 + (z+1)^2 + Sum(i,i=1..N);

x^2+1/y^5+(z+1)^2+Sum(i, i = 1 .. N)

T := subsindets(expr, anything^integer,
           proc(u) if op(2,u)>0 then
                     `%*`(op(1,u)$op(2,u));
                   else
                     1/`%*`(op(1,u)$(-op(2,u)));
                   end if;
           end proc);

`%*`(x, x)+1/`%*`(y, y, y, y, y)+`%*`(z+1, z+1)+Sum(i, i = 1 .. N)

# To display without the gray operator.

InertForm:-Display(T, 'inert'=false);

`%*`(x, x)+1/`%*`(y, y, y, y, y)+`%*`(z+1, z+1)+Sum(i, i = 1 .. N)

InertForm:-Value(T);

x^2+1/y^5+(z+1)^2+(1/2)*(N+1)^2-(1/2)*N-1/2

# And, without affecting the `Sum`

subsindets(T, specfunc(anything,`%*`), u->`*`(op(u)));

x^2+1/y^5+(z+1)^2+Sum(i, i = 1 .. N)

 

Download inert_pow_prod.mw

You should be able to use the results from the ifactors command, programmatically. (You can trivially massage its output into similar formats of your own invention.)

ifactors(725);

        [1, [[5, 2], [29, 1]]]

ifactors(1125);

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

ifactors(2048);

             [1, [[2, 11]]]

If I understand your sentences rightly then yes, the inner procedure that is defined in the body of the outer procedure will be recreated each time the outer is invoked/called, and this is unnecessary overhead (computational effort and memory management) that could be avoided.

If instead the procedure A were simply defined at a higher level than procedure B, then you should be able to call A from within B, under the lexical scoping rules.

You could also implement both A and B within the same module, if you would prefer some compartmentalization.

You could also make the outer procedure be the local ModuleApply of a module B, where A is another local procedure within that module. That would make B into a so-called appliable module, where the name B could be called with arguments.

Would you like examples of all this? Have you read the Programming Guide (esp. the section on modules?)

If you call the ZeroMatrix command without specifying its option

    compact=false

then (by default) the Matrix will be constructed with a special indexing function.

The effect of that is that the Matrix can only contain zeroes. (This is great for efficiency in some programming situations, since very large zero-matrices can take up little/constant memory.)

Much simpler is just to use the Matrix command instead of the ZeroMatrix command. The default fill-values of a call to Matrix are zero, and those don't have to be specified explicitly.

This is a guess, since you did not provide you code. It's almost always better to provide full code to reproduce. (The green up arrow in the Mapleprimes editor allows you to upload and attach documents. )

restart;

MZ1 := LinearAlgebra:-ZeroMatrix(3, 4);

Matrix(3, 4, {(1, 1) = 0, (1, 2) = 0, (1, 3) = 0, (1, 4) = 0, (2, 1) = 0, (2, 2) = 0, (2, 3) = 0, (2, 4) = 0, (3, 1) = 0, (3, 2) = 0, (3, 3) = 0, (3, 4) = 0})

MZ1[2,3] := 17;

Error, attempt to assign non-zero to zero Matrix entry

MZ2 := LinearAlgebra:-ZeroMatrix(3, 4, compact=false);

Matrix(3, 4, {(1, 1) = 0, (1, 2) = 0, (1, 3) = 0, (1, 4) = 0, (2, 1) = 0, (2, 2) = 0, (2, 3) = 0, (2, 4) = 0, (3, 1) = 0, (3, 2) = 0, (3, 3) = 0, (3, 4) = 0})

MZ2[2,3] := 17;

17

MZ2;

Matrix(3, 4, {(1, 1) = 0, (1, 2) = 0, (1, 3) = 0, (1, 4) = 0, (2, 1) = 0, (2, 2) = 0, (2, 3) = 17, (2, 4) = 0, (3, 1) = 0, (3, 2) = 0, (3, 3) = 0, (3, 4) = 0})

MZ3 := Matrix(3, 4);

Matrix(3, 4, {(1, 1) = 0, (1, 2) = 0, (1, 3) = 0, (1, 4) = 0, (2, 1) = 0, (2, 2) = 0, (2, 3) = 0, (2, 4) = 0, (3, 1) = 0, (3, 2) = 0, (3, 3) = 0, (3, 4) = 0})

MZ3[2,3] := 17;

17

MZ3;

Matrix(3, 4, {(1, 1) = 0, (1, 2) = 0, (1, 3) = 0, (1, 4) = 0, (2, 1) = 0, (2, 2) = 0, (2, 3) = 17, (2, 4) = 0, (3, 1) = 0, (3, 2) = 0, (3, 3) = 0, (3, 4) = 0})

 

If I set the "rtable" (ie. Matrix) associated with the above DataTable to be MZ1 then I'll get a popup error "Unable to change value" whenever I attempt to edit an entry and hit the Return/Enter key,

 

But if I associate MZ2 or MZ3 with the DataTable instead then I can edit the entries.

 

Creating MZ3 as above is much simpler than creating MZ2 as above.

 

Download zeromatrix_edit.mw

For the 2D case here are two short versions. (They could be shorter still, but I wanted to show a few of the intermediate assignments.)

First, one using operators.

with(plots):
F:=(x,y)->4*x^2+9*y^2;
G:=[D[1],D[2]](F)(2,1);
display(arrow([2,1],G),
        implicitplot(F(x,y)=F(2,1),x=-4..4,y=-2..2),
        scaling=constrained);

And one using expression form.

with(plots):
f:=4*x^2+9*y^2;
fval:=eval(f,[x=2,y=1]);
gval:=eval(zip(diff,f,[x,y]),[x=2,y=1]);
display(arrow([2,1],gval),
        implicitplot(f=fval,x=-4..4,y=-2..2),
        scaling=constrained);

Both of those can be adjusted using various options for the arrow dimensions, etc.

And 3D versions can also be made. The 2D implicit plot can be raised to a level height on the 3D surface, or a single contour line can be specified at the specific height. See attached.

gradient_arrow_operator.mw

gradient_arrow.mw

I think that your example is interesting, since the "size" of the desired target expression is smaller than that of the original expression, using any of these as norm:
   length`simplify/size/size`MmaTranslator:-Mma:-LeafCount
So it seems reasonable to ask why the "smaller" form is not found directly by some flavour of call to simplify.

My compressor oracle found these two ways:

ex1:=(2*sqrt(N__s) + N__s + 1)/(2*N__s + 2);

(2*N__s^(1/2)+N__s+1)/(2*N__s+2)

eval(collect(collect(ex1,[N__s],__P),[__P]),
     __P=(u->u));

1/2+N__s^(1/2)/(N__s+1)

thaw(collect(subsindets(ex1,radical,freeze),
     [freeze~(indets(ex1,radical))[]],simplify));

1/2+N__s^(1/2)/(N__s+1)

Download simp_radical_ex.mw

You probably already realize this, but you should not use the symbolic option of the simplify command if you care about results branch cuts and always being correct. However, your current example and desired target are equivalent. I just wanted to mention it, since you made it sound as if simplify(...,symbolic) might be generally applicable/valid.

I took reciprocals (twice, naturally) in order to get op(2,X) and v into the exact same form. But the rest was not so bad.

It's always interesting when simplify (or the is command) requires extra help. [I will submit a Software Change Request, citing the examples.)

restart

 

In Paul J. Nahin's book "The story of the square of " sqrt(-1) ", "Scipione del Ferro discovered the roots of the depressed cubic f (below, with p and q positive) by replacing x with u + v and by assuming that g (below) = 0.

del Ferro then found the values of u and v below

 

f := x^3+p*x = q

g := 3*u*v+p

u := ((1/2)*q+((1/4)*q^2+(1/27)*p^3)^(1/2))^(1/3); v := -(-(1/2)*q+((1/4)*q^2+(1/27)*p^3)^(1/2))^(1/3)


Although Maple's roots of f (Sol below) do not resemble del Ferro's, its two ops satisfy g and evaluate to x = 2 when p and q are 6 and 20 respectively, as in the example Nahin gives in his book.

 

How is this so when Maple's roots seem so different from del Ferro's?

 

Sol := solve([f, p > 0, q > 0], x)

piecewise(And(0 < p, 0 < q), [{x = (1/6)*(108*q+12*(12*p^3+81*q^2)^(1/2))^(1/3)-2*p/(108*q+12*(12*p^3+81*q^2)^(1/2))^(1/3)}], [])

X := `assuming`([eval(x, Sol[1])], [p > 0, q > 0])

(1/6)*(108*q+12*(12*p^3+81*q^2)^(1/2))^(1/3)-2*p/(108*q+12*(12*p^3+81*q^2)^(1/2))^(1/3)

`assuming`([simplify(combine(g))], [p > 0, q > 0])

0

simplify(eval(X, [p = 6, q = 20]))

2

`assuming`([simplify(combine(rationalize(X-u-v)))], [p > 0, q > 0])

0

simplify(eval(X-u-v, [p = 6, q = 20]))

0

`assuming`([simplify(combine(rationalize(op(1, X)-u)))], [p > 0, q > 0])

0

`assuming`([simplify(combine(rationalize(op(2, X)-v)))], [p > 0, q > 0])

0

simplify(rationalize(u), radical); op(1, X); evalb(% = `%%`)

(1/6)*(108*q+12*(12*p^3+81*q^2)^(1/2))^(1/3)

(1/6)*(108*q+12*(12*p^3+81*q^2)^(1/2))^(1/3)

true

`assuming`([simplify(rationalize(v))], [p > 0, q > 0]); `assuming`([1/simplify(1/v, radical)], [p > 0, q > 0]); evalb(% = `%%`)

-(1/6)*(-108*q+12*(12*p^3+81*q^2)^(1/2))^(1/3)

-(1/6)*(-108*q+12*(12*p^3+81*q^2)^(1/2))^(1/3)

true

`assuming`([is(combine(rationalize(op(1, X)-u)) = 0)], [p > 0, q > 0])

true

`assuming`([is(combine(rationalize(op(2, X)-v)) = 0)], [p > 0, q > 0])

true

NULL

Download CubicRootsacc.mw

The line,
    combine(factor(expand(ee)))
will handle your expression, assigned below to ee.

If you have similar examples you might wish to restrict the combine by its exp option. If you have other examples where you don't want a blanket expand then you could target the original exp calls by using subsindets.

restart;

ee := exp(4*y) + 2*exp(2*y) + 1;

exp(4*y)+2*exp(2*y)+1

factor(expand(ee))

((exp(y))^2+1)^2

combine(factor(expand(ee)));

(exp(2*y)+1)^2

combine(factor(expand(ee)),exp);

(exp(2*y)+1)^2

 

Download exp_factor.mw

Is it because you misspelled "typeset"?

 

It's important to understand the basic kinds of structures and operations for differentiating.

Once you understand that you could move on to typeset "2D Math" representations. But IMO it's very important to understand what might be going on underneath.

note. You don't have to define a procedure such as f below, in order to obtain an expression such as
   y*sin(z)+2*cos(y)
You could also just type that expression out, assign that to some name, differentiate using the diff command, etc.

After you understand the following, you might look at these two older posts, here and here. (Note that I have not checked how those behave in modern versions.) There may also be support syntax for your input form from the Physics package. But you didn't attach your actual attempts.

restart;

# A procedure that takes two arguments (ie. has two
# parameters.

f := (y,z) -> y*sin(z)+2*cos(y);

proc (y, z) options operator, arrow; y*sin(z)+2*cos(y) end proc

# An expression

f(y,z);

y*sin(z)+2*cos(y)

# Differentiating the procedure with respect to its first
# parameter, by applying the differential operator `D`.

D[1](f);

proc (y, z) options operator, arrow; sin(z)-2*sin(y) end proc

# Using D as previously, and then evaluating at the
# arguments (Pi/2,Pi).

D[1](f)(Pi/2,Pi);

-2

# Differentiating the expression, with respect to y

diff(f(y,z),y);

sin(z)-2*sin(y)

# Differentiating the expression, with respect to y,
# and then evaluating at the point y=Pi/2,z=Pi.

eval(diff(f(y,z),y), [y=Pi/2,z=Pi]);

-2

 

And we can repeat all the above, but this time differentiating with
respect to the second parameter of procedure f, or with respect to
the name `z` for the expression f(y,z).

D[2](f);

proc (y, z) options operator, arrow; y*cos(z) end proc

D[2](f)(Pi/2,Pi);

-(1/2)*Pi

diff(f(y,z),z);

y*cos(z)

eval(diff(f(y,z),z), [y=Pi/2,z=Pi]);

-(1/2)*Pi

 

In 2D Input mode, the following syntax will also create a procedure.

note(!) This syntax will not accomplish the same thing in 1D plaintext
input. In that mode you should use the syntax at the start of this worksheet.

 

restart

"f(y,z):=y*sin(z)+2*cos(y)"

proc (y, z) options operator, arrow, function_assign; y*sin(z)+2*cos(y) end proc

f := (y,z) -> y*sin(z)+2*cos(y);

proc (y, z) options operator, arrow; y*sin(z)+2*cos(y) end proc

 

Download procs_expressions_differentiation.mw

First 109 110 111 112 113 114 115 Last Page 111 of 336