Carl Love

Carl Love

28025 Reputation

25 Badges

12 years, 308 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

The listlist k is a specification of 12 values of z; the (x,y)-values are determined implicitly, using a scheme similar to what you proposed. The following code will expand any GRID to the (x,y,z)-values of all its points:

ExpandGrid:= proc(
    G:= GRID(range(numeric), range(numeric), listlist(realcons))
)
local a, b, c, d, k:= op(3, G), n:= nops(k)-1, m:= nops(k[1])-1, i, j; 
    (a,b):= (lhs,rhs)(op(1,G)); (c,d):= (lhs,rhs)(op(2,G));
    [seq](
        [seq]([a+j*(b-a)/n, c+i*(d-c)/m, k[j+1][i+1]], j= 0..n), 
        i= 0..m
    )
end proc
:
k:=[[1,2,10],[1,1,8],[0,1,5],[0,0,6]]:
ExpandGrid(GRID(1..2,1..3,k));
        [[           [4      ]  [5      ]           ]  
        [[[1, 1, 1], [-, 1, 1], [-, 1, 0], [2, 1, 0]], 
        [[           [3      ]  [3      ]           ]  

          [           [4      ]  [5      ]           ]  
          [[1, 2, 2], [-, 2, 1], [-, 2, 1], [2, 2, 0]], 
          [           [3      ]  [3      ]           ]  

          [            [4      ]  [5      ]           ]]
          [[1, 3, 10], [-, 3, 8], [-, 3, 5], [2, 3, 6]]]
          [            [3      ]  [3      ]           ]]

This is an Answer to the other question that you asked, the one in the final sentence of your Question, regarding working through a step-by-step solution of a separable ODE from an old worksheet by Harald Pleym.

Maple's int will not automatically perform a definite integral when the integrand contains an unknown function even in cases where the antiderivative seems obvious. Compare:

int(diff(P(t),t), t); #Indefinite integral works.
int(diff(P(t), t), t= 0..T); #Definite integral returns unevaluated.

The reason that it won't do it is that the answer P(T) - P(0) is not necessarily correct[*1] if has a discontinuity between and T. Since P is unknown, whether there's a discontinuity is also unknown. You get around this by using option continuous, which tells int to assume that it's continuous (at least between the limits of integration):

int(diff(P(t), t), t= 0..T, continuous);
                          -P(0) + P(T)

The need to use this option may have been introduced after Harald Pleym wrote that worksheet.

If you pass this or any other option to the inert Int, the option will be passed on to int when you use value. You only need to make one change to make the whole worksheet work: In these lines of code:

g=convert(g,parfrac,P(t));
map(x->x*diff(P(t),t),(%));
map(Int,%,t=0..T);

simply change the last line to 

map(Int, %, t= 0..T, continuous);


Footnote: [*1] It's not necessarily correct because different constants of integration may need to be used on the piece containing the lower limit of integration and the piece containing the upper.

Those are called piecewise functions both in standard mathematical English and in Maple. Your phrase "multithreaded function" means something completely different[*1], and you risk causing great confusion by using that phrase. I recommend that you edit the title of this Question to change "multithreaded" to "piecewise".

Your example function can be specified:

f:= x-> piecewise(x < 0, 1/x, x >= 0, sqrt(x));

See help page ?piecewise.

Footnote: [*1] multithreaded function is one intended to execute simutaneously in multiple processors sharing the same memory on a single computer. Thus they either do not make assignments to nonlocal memory, or they make those assignments in a carefully controlled way.

The sin(Pi/5) needs to be converted to radical (i.e., algebraic number) form or RootOf form. This does the whole thing, and gives an answer much simpler than Mathematica's:

evala(abs(convert(v, radical)));
                            1  (4/5)
                            - 2     
                            2       

This works also, and may be needed when a "radical" form does not exist:

allvalues(evala(abs(convert(v, RootOf))));

evala is Maple's longstanding "package" of highly sophisticated algebraic-number simplifiers (and other commands). It's not a "package" in the sense of the with command; it's just a collection of procedures. See ?evala.

`print/...` procedure can be used for any function. This mechanism has been used in Maple for more than 20 years, so it'll certainly work in your version. This doesn't require any typesetting setting, anything from the Typesetting package, any rule assistants, or anything menu-based.

`print/BesselJ`:= (nu,z)-> local J; J[nu](z):

In older versions of Maple, you'll need to use the proc syntax (this is a purely syntactic variation; the functionality is identical):

`print/BesselJ`:= proc(nu,z) local J; J[nu](z) end proc:

This `print/BesselJ` procedure will be called automatically whenever an attempt is made to do 2D output of BesselJ(...).

This would need to be done for every function whose display you want to alter, but it could be done through an initialization file.

Edit: I think that you may be able to fix your issue entirely by using EnableTypesetRule. See, below, my Reply to the Answer by @dharr.

There is no command Remove that would do anything useful with the arguments -10..10, -1. The closest actual command to that is 

remove(`=`, [$-10..10], -1)

which would give you the integers from -10 to 10 with -1 removed. Just using integers won't give you a good plot. But you can do a parametric plot like this:

plot(
    `[]`~(f, g, t=~ [-10..-1.3, -0.7..10]), 
    style= point, symbol= cross, symbolsize= 12, colour= coral
);

Now I'm thinking of the uniform-distribution-of-points problem from your followup.

Use plots:-textplot inside plots:-display to add annotations to a plot. Like this:

InterPlot:= proc(f1,f2)
local 
    F:= [f1,f2], x:= indets(F, And(name, Not(constant)))[], 
    y, v:= [x,y], Int:= solve(y=~ F, v)
;
    plots:-display(
        plot(F, (v=~ (min-1..max+1)~(map(eval~, v, Int)))[]),
        plots:-textplot(
            map2(eval, [v[], v], Int), align= [above,left], color= magenta
        ),
        title= typeset(F)
    )
end proc
:
(print@InterPlot@op)~(
    [x^~[1,3], x^~[2,4], x^~[-1,-2], x^~[2,1/2], [x^3, abs(x^(1/3))]]
):

The next 4 plots are similar and are omitted.

If you like brevity and avoiding the need for loop indices, then you should like this:

lunev:= %exp~([2*k, 2*k+1, 1/2, 3/2, 1/3]*~(Pi*I)):
print~(lunev =~ value(lunev)) assuming k::integer:

Putting immediately after an operator makes it elementwise, i.e., makes it act on the elements of some container(s), such as lists. See help page ?elementwise.

Putting immediately before an operator makes it inert. This is a way of preventing evaluation that's usually easier to use and more robust than single quotes. The value command converts inert operators to their non-inert forms. See ?value.

for loop or a seq command can iterate directly over the elements of a container, i.e., without needing to index the container or know how many elements it has. For example, the 2nd line above could be replaced with

for x in lunev do print(x = value(x)) assuming k::integer od: 
or
seq(print(x=value(x)), x= lunev) assuming k::integer;

There is a library command for this. Suppose that A is the Array, then

B:= ArrayTools:-Permute(A, [2,1,3]);

This works for any number of dimensions, any number of which can be exchanged with others.

I will refer to the unnamed user who sent you this Question as the OP. Unfortunately, I can't ask the OP for more specific details about what they want, but you can. It seems quite unlikely to me that your solution would provide anything close to what they want because either option completely eliminates 2D output for the expression being displayed.

To write the program below, I've made the following reasonable assumptions about what the OP wants. Any part of these assumptions can be easily changed; just let me know.

  1. The overall output of an expression ex should be fundamentally 2D.
  2. All subexpressions of ex with exponents (including instances of the exp function) are to be flattened and displayed with the explicit binary infix operator ^.

Then the following code does it:

FlattenPowers:= proc(ex)
uses T= Typesetting;
local e:= T:-mo("&ExponentialE;");
    evalindets(
        ex, {`^`, specfunc(exp)}, 
        p-> 
            if p::`^` then
                if op(2,p)::And(rational, negative) then
                    1/op(1,p) &^ (-op(2,p))
                else
                    `&^`(op(p))
                fi
            else 
                exp(1) &^ op(p)
            fi
    );
    subsindets(
        subs(T:-mfenced(e)= e, T:-Typeset(%)),
        anyfunc(
            anything,
            specfunc(T:-mspace),
            identical(T:-mo("&amp;&circ;")),
            specfunc(T:-mspace), 
            anything
        ),
        f-> subsop(2= (), 3= T:-mo("^"), 4= (), f)
    )
end proc
:
#Example usage:
FlattenPowers([(a+b^2)^3, exp(x), a^x, exp(-(x-mu)^2/2/sigma^2)]);

 

Your answer for part a) is completely correct.

I agree with Kitonum and VV in this sense: If I were coding these plots for my own purposes, I'd use the evalc method that they show. However, Kitonum and VV seem to think that your trouble is due a lack of Maple knowledge, so they suggest Maple-based solutions. But I can see (with absolute certainty) that you're having trouble understanding the fundamentals of complex arithmetic. My reasons for emphasizing the (x,y) method are pedagogical, to aid your learning.

  1. You had already started using it anyway.
  2. Your book uses it.
  3. It's absolutely essential that you learn how to do it "by hand" for these extremely simple cases.
  4. If you were coding a Maple `evalc/...subprocedure, you'd probably need to use it. You'd at least need to have a fundamental understanding of it.

The reason that problem 3b seems weird is that its author had an additional pedagogical purpose: showing the multiplicative properties of abs and arg:

  1. For any complex a and babs(a*b) = abs(a)*abs(b)
  2. For complex a and b other than 0, arg(a*b) = arg(a) + arg(b) "mod 2*Pi". In other words, arg has a property very similar to logarithms. This is not a coincidence. (By "mod 2*Pi", I mean that you add or subtract an integer multiple of 2*Pi to keep the result in the correct interval ((-Pi, Pi] in Maple).)

So here's how to apply the (x,y) method to 3b:

arg(2iz) = arg(2i(x+yi)) = arg(2ix + 2i2y) = arg(-2y + 2xi) = arctan(2x, -2y).

abs(4iz) = abs(4i(x+yi)) = abs(-4y + 4xi) = sqrt(16y^2 + 16x^2) = 4abs(z).

I agree with Acer: You should define a scalar operation without using elementwise operators, and then use a single elementwise operation to apply that operation to vectors (or lists).

However, the source of your error is not at all your fault; rather, it seems to be a severe[*1] bug in the precedence and associativity of the elementwise division operator in 2D Input. A fraction with a long horizontal bar in its 2D representation is meant to have both its numerator and denominator grouped with parentheses in any 1D representation. In other words, this fraction: 

 
                             a + b
                             -----
                             c + d

is a 2D representation of (a+b)/(c+d); this is taught with basic arithmetic in elementary school. But the 2D Input parser is interpreting it as a + b/(c + d).

[*1] The erroneous effect of this bug is severe; fixing it should be trivial.

Yes, go to your Questions page: https://www.mapleprimes.com/users/janhardo/questions
There you will get a list of all your Questions. If you click on the title of any of these Questions, then you can browse it in exactly the same way that you'd browse any other Question on MaplePrimes.

Maple has 5 binary-infix operators, [=, <, <=, .., ::], with the following properties (note that and >= are always reversed in their internal representation and thus can't be treated as separate, independent operators):

  1. They are non-associative.
  2. They always have exactly two operands, one on the left and one on the right.
  3. These operands can be extracted with lhs and rhs.
  4. These operands can be any valid Maple expression, including NULL, a multi-term sequence, or another instance of one of these very same binary-operator expressions. 
  5. When the operand is as described in "including ..." in #4, it must be enclosed in parentheses.
  6. Expressions formed with these operators are inert.

Thus, equations, inequalities, and combinations thereof with more than two operands can be represented as valid Maple expressions by using parentheses, e.g.,

(1 < x) <= 3
(A = B) = C

The only problem with these is that we don't want those extra parentheses to display in the output. We can achieve this by first using Typesetting:-Typeset on these and then removing the unwanted parentheses, which is why I titled this "Working in the other direction". Here's a procedure that does this:

MultiEquation:= proc(eq)
uses T= Typesetting;
    subsindets(
        T:-Typeset(eq), 
        specfunc(
            And(
                specfunc(T:-mrow), 
                satisfies(
                    f-> membertype(
                        specfunc(
                            identical("&equals;", "&lt;", "&le;"), 
                            T:-mo
                        ),
                        f
                    )
                )
            ), 
            T:-mfenced #TypeMK for parentheses
        ),
        op
    )
end proc
:

Examples:
MultiEquation((1 < x) <= 3);
MultiEquation(1 < (x <= 3));
MultiEquation((A=B)=C);
MultiEquation(A=(B=C));
MultiEquation(((A=B)=C)=D);
#Desired parentheses are not removed:
MultiEquation((1 < (x+1)^2) < 2);

As you already realize, each problem can be reduced to a set of equations and/or inequalities in the two real variables x and y. For inequalities, the best command is plots:-inequal.

Here are some mistakes that you made:

a) 1. Note that z + 2 - I = z - (2+I). So, the distance formula is sqrt((x-2)^2 + (y-1)^2).
2. Your sqrt(2) should just be 2.

b) 1. Same mistake as above: distance formula on left side should be sqrt((x-1)^2 + (y-2)^2).
2. Distance formula on right side should be sqrt((x-3)^2 + (y-0)^2).

c) The bar symbol over a complex number or variable is called conjugate in English and Maple. It simply means to negate th​​​​​​e imaginary part. So, the left side is 2*x and the right side is x^2 + y^2.

d) For real x and y, arg(x+I*y) = arctan(y,x), using Maple's two-argument form of arctan.

e) 1. This is an equation not an inequality. Thus, its graph must be a curve (essentially 1-dimensional), not a region (essentially 2-dimnesional).
2. Re(z) = xIm(z) = y.

f) Same as d.

Opts:= labels= [Re,Im](z), scaling= constrained: #common plot options

(*3a:
For inequalities, use plots:-inequal.
 
You could use the distance formula for abs (absolute value), but it's a
bit easier to use its square.
*)
plots:-inequal(
    [1 <= (x-2)^2 + (y-1)^2, (x-2)^2 + (y-1)^2 <= 4],
    x= 0..4, y= -1..3, Opts
);
(*3b:
This is asking for the points equidistant between two points. Obviously
that's a line. If you solve the abs equation, all the square terms will
cancel leaving just y = x-1. Thus the regular plot command is a good
choice for this one.
*) 
plot(
    solve((x-1)^2 + (y-2)^2 = (x-3)^2 + y^2, y), x= 0..4, y= 0..4, 
    Opts
);
(*3c:
When put in (x,y) form, the equation has both x^2 and y^2. Thus
plots:-implicitplot is a good choice for this one.
*)
plots:-implicitplot(2*x = x^2+y^2, x= 0..2, y= -1..1, Opts);

(*3d: 
For real x and y, argument(x+I*y) = arctan(y,x).
optionsopen is used to highlight the effect of the strict
inequalities.
*)
plots:-inequal(
    [
        1 <= x^2+y^2, x^2+y^2 < 9,
        -Pi/4 <= arctan(y,x), arctan(y,x) < 3*Pi/4
    ],
    x= -3..3, y= -3..3,
    optionsopen= [linestyle= dash, color= red, thickness= 2], Opts
);

 

First 55 56 57 58 59 60 61 Last Page 57 of 395