Carl Love

Carl Love

28130 Reputation

25 Badges

13 years, 306 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

You wrote:

  • That is, P(n) = 0 when n > 365, so our formula for P(n) above is limited to values of n that are 365 or less.

The formula that you gave for P(n) --- product((365-k)/365, k= 0..n-1) --- is exactly for integers n > 365. So, the formula is not limited in the manner that you say. However, if the formula were evaluated in floating-point arithmetic, perhaps it wouldn't be exactly 0. I call that a limitation of the implementation, not of the formula.

@C_R In your worksheet local_assignement_attempts.mw, you show several attempts at creating local variables inside a procedure based on names passed into that procedure. In your final attempt, you have something that's close to something that'll work. It's close because it uses convert(..., `local`). Here is your last procedure:

foo:= proc(params) assign(params); convert(a,`local`); print(a,b) end proc

There are 3 problems with that, and after describing them, I'll show a modification that actually works. The problems are

  1. The conversion to local must be done before the assign. Otherwise, you're assigning to globals and then creating locals that never get assigned to.
  2. The result of the convert command must be assigned to something in order to be able to use those new local variables in any meaningful way. You need a "handle" or "pointer" to those new variables.
  3. You can't explicitly refer to the names that you passed in from within the procedure and expect those to be references to the new locals. All references to the new locals need to be made indirectly through the handle from point 2 above.

Here's my modification:

restart
:
foo:= proc(params::list({name,string}= anything))
local new_locals:= convert~(lhs~(params), `local`);
    assign(new_locals=~ rhs~(params));
    lprint(new_locals, eval(new_locals))
end proc
:
foo([a= 1, b= 2]);
[a, b], [1, 2]

#Verify that globals a and b are unchanged:
lprint(a,b);
a, b

To refer to the new local or b individually from within the procedure, use new_locals[1] or new_locals[2].

The eval is needed due to the restricted 1-level evaluatlon that locals ordinarily get. It is not specifically due to the use of convert(..., `local`) or assign. The 1st level is new_locals => [a, b]; the 2nd is [a, b] => [1, 2]. That 1st level is equivalent to doing eval(new_locals, 1). Here is a help excerpt that explains that difference between local and global evaluation and also explains eval(..., 1), which I used (unnecessarily) in the procedure my_assign. (The AI-generated help that you got for eval(..., 1) is abyssmally wrong.) This excerpt is from the 7th paragraph in the Description section of help page ?eval:

  • The default evaluation rules in Maple are full evaluation for global variables, and one-level evaluation for local variables and parameters. Sometimes the user requires full evaluation or one-level evaluation explicitly. For example, if [a user executes] x := y and y := 1 in a Maple session, what is the value of x? In an interactive session where x and y are global variables, x would evaluate to 1 and we would say that x is "fully evaluated". For one-level evaluation, we would use the command eval(x, 1) which would in this case yield y. However, inside a Maple procedure, if x is a local variable or a parameter, then x evaluates to y and we would say x evaluated "one level". For parameters, this is only relevant when the variable being passed as a parameter is not a global variable, since it would then be fully evaluated before being passed to the procedure. For local variables and parameters, full evaluation is obtained with eval(x), yielding 1 in this example.

@C_R In your updated Question, you say that you want something akin to passing a plot option, e.g., numpoints= n. Names such as the numpoints in that example are called keyword parameters, and you can easily create them for your own procedures. You just need to put them in { } in the procedure header and give them a default value:

restart:
foo:= proc({b:= (), c:= (), d:= ()})
    print(b,c,d)
end proc
:
foo(b= 2, c= 3, d= 4);
b,c,d;
                            2, 3, 4
                            b, c, d

See the section "Keyword Parameters" of the help page ?parameter_classes.

That makes b, c, and d procedure parameters, which are technically neither locals nor globals, but they may have the properties of locals that you want.

To help prevent bugs, it's best to pass keyword parameters in unevaluation quotes, especially if they have short simple names:

foo('b'= 3, 'c'= 4, 'd'= 5)

The { } are used for the header declaration only, not the call.

@C_R My procedure my_assign from above works regardless of whether the lhs's of the equations are local, global, or any mixture of those:

Foo:= proc()
local b, c; #keeping d implicitly global
    my_assign([b= 2, c= 3, d= 4]);
    print(b, c, d);
    my_assign([b= 3, c= 4, d= 5]);
    print(b, c, d);
end proc
:
Foo();
b,c,d;
                            2, 3, 4
                            3, 4, 5
                            b, c, 5

 

@C_R See help page ?evalapply. And note that in the cases that you're interested in here, no explicit use of evalapply is needed. I only mention that help page because its examples show many of those cases that are handled automatically by the kernel. You should also read ?exprseq.

@Anthrazit The extra space is not just after the first letters of words. Look after the w in "Norwegian" and the second c in "calculation".

@Alfred_F In the 1D input that Christian used, the spaces surrounding the catenation operator || do not make any difference. But these spaces cause confusion in your 2D-Input. So, change c || i to c||i in both instances.

@C_R You wrote: "I do not see a difference."

The difference that I see immediately is the spacing between letters. Note especially the excessive space after T and M in "This Maple...." There is also excessive space after lowercase w.

@Alfred_F You wrote:

  • I am currently interested in how polynomials map given paths in the complex plane to this plane.

This is quite easy to plot for any given polynomial p and complex path P. Here I show the image of the unit circle under the polynomial from the original question.

p:= z-> z^4 - 12*z - 12:
P:= exp(I*t):  R:= t= -Pi..Pi:
plot(
    [(Re,Im)(p(P)), R], 
    scaling= constrained, labels= [Re,Im], labelfont= [times,bold,14]
);

@C_R A list(list) is a list all of whose members are lists; it's literally a list of only lists. Your list has members `^` and 2, which are not lists.

@C_R In addition to its own help page ?`?[]` (as mentioned by @acer), there is decent documentation of `?[]` and similar overloadable builtin operators like `?()` at ?use. This is a bit counterintuitive since these operators can be used (and usually are used) without a use statement. See the last bullet point in the Semantics section of the help page. The library command index provides functionality similar to the builtin `?[]` but it is far more user friendly. However, if you want to overload the semantics of indexing (including indexed expressions on the left side of the assignment operator :=) then you need to use `?[]`. Likewise, the library command apply is far more user friendly than the builtin `?()`.

Something not clearly stated at ?use (it's only vaguely mentioned) is that the prefix form of a prefix elementwise operator usage such as f~(A,B) is `~`[f](A,B), and that of an infix usage such as A +~ B is `~`[`+`](A,B). Like almost all operators in Maple, these prefix forms of `~` are overloadable.

Also, you've used the type name listlist incorrectly in the title of this Question. A listlist is a type more specific than a list of lists (this latter type is formally list(list)). To be a listlist, the member lists must all have the same number of elements. Formally, type listlist is semantically equivalent to And(list(list), 1 &under (nops@nops~@{op})).

Please post your example. You can attach a worksheet by using the green uparrow on the toolbar of the MaplePrimes editor.

In defense of my contribution to this thread, I'd like to point out that I didn't say a single word about mixtures of random variables, nor did I claim to.

If you define via 

assume(w >= 0, w <= 1);
Z:= sqrt(w)*X + sqrt(1-w)*Y;

then you'll have Var(Z) = w*a^2 + (1-w)*b^2.

There were 3 other minor errors in your original code that I corrected without mention in my Answer above. Now I have time to explain them. All 3 were in your use of the parameters option to Explore:

  1. The option's keyword is spelled parameters, not parameter, even if there is only a single parameter.
  2. The option's value is a list (enclosed in [...]); you had a set (enclosed in {...}).
  3. If you want the parameter to range over floating-point values rather than strictly integers, then you should include decimal points in the range endpoints; hence, 0.0..1.0 rather than your 0..1. (The trailing 0s are only for reading clarity, but at least one of the two decimal points is required.)
1 2 3 4 5 6 7 Last Page 1 of 710