Carl Love

Carl Love

28015 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

A multivariate truncated Taylor series can be obtained using mtaylor. Example (using your function):

f:= (x,y)-> x*y/(y-x*sqrt(y)-x^2):
mtaylor(f(x,y), [x,y]=~ [1,1], 6)

where [1,1] could be replaced by any point where f is analytic. Using instead

mtaylor(f(x,y), [x,y], 6)

implies that the desired expansion point is [0,0], which won't work for this particular function. 


 

It can be done by temporarily integerizing the exponents on epsilon. One such way is to substitute epsilon= epsilon^2 followed by combine(..., power). Immediately after defining A, do

A2:= collect(combine(subs(epsilon= epsilon^2, A), power), epsilon);
add(coeff(A2, epsilon, k)*epsilon^(k/2), k= 0..2);

 

I wouldn't use either way: The overload way leads to duplication of code, which causes big problems when code needs to be modified. The "traditional" way is bad because it's best to isolate the input processing from the main algorithm. In other words, within the main algorithm, you don't want to be taking branches due to trivial differences in the input type, because that distracts the reader from the finer mathematical points of the algorithm.

Maple provides a solution for this situation called "data type coercion" (help page ?coercion). If a procedure's argument is not the desired type, then another (trivial) procedure (that you write) will be called to convert it to the desired type, if possible. To use this, both the type name and conversion procedure name should begin with ~. Example:

restart
:
#Type-coercion procedure (name must begin ~): 
~set:= proc(A, T::type)
    if A::{list, thistype}(T) then 
        `if`(A::list, {A[]}, {A})
    else
        error "%1 not coercible to type set(%2)", A, T
    fi
end proc
:
foo:= proc(A::~set(`=`))
    A
end proc
:
foo({x=3, y=4}), foo(x=3), foo([x=3]);
                {x = 3, y = 4}, {x = 3}, {x = 3}
foo(3);
Error, (in foo) 3 not coercible to type set(=)

 

I think that @nm has the right idea for a "purely Maple" Answer. But I think that you should also learn the easy formula for it. Suppose that you have a decimal number of the form

x = n.d1d2...djr1r2...rk...

where n is nonnegative integer and d1d2, ..., djr1r2, ..., rk are digits (0--9) with the r1r2...rk being the repeating part. Then

x = n + (d1d2...dj + r1r2...rk/(10^k-1))/10^j

For example:

x:= 7.421232323;

r:= 7 + (421 + 23/99)/1000;
             
r := 367351/49500

evalf(r);
             
7.421232323

Here's a procedure for it:

F:= proc(A::Matrix, B::Matrix)
local A0;
    if upperbound(B)[2] < upperbound(A)[2] then return thisproc(B,A) fi;
    A0:= <A | Matrix(upperbound(A)[1], upperbound(B)[2] - upperbound(A)[2])>[.., ..-2];
    evalb({convert(A0, listlist)[]} = {convert(B[.., ..-2], listlist)[]})
end proc
:

Assuming that g is a binary variable (i.e.,  it can only be 0 or 1), the easiest way to deal with it is to solve separately for those cases, then take the maximum of the two solutions. It can be done like this:

params:= [
    Pp= 1600, delta= 8760, Cm= 104, x= 175200, Ep= 1.5, Rc= 8, v= 80, Ec= 0.5, Cs= 0.5,
    Csc= 0.6, Sc= 1500, Ch= 20, Er= 2, Rp= 100, Ec= 0.5, a= 100, b= 2.5, Do= 1500, Ig= 80,
    Ch1= 22, Ce= 6, Ces= 4, E(Pr)= 0.05, E(1/(1 - Pr))= 1.0536, E(Pr/(1 - Pr))= 0.0536
]:
TOTP := unapply(eval(ETPU(y,Sr), params), [Sr,y]):
TotP_g:= g-> Optimization:-Maximize(
    eval([TOTP(Sr,y), eval({300 <= D(g,Sr), D(g,Sr) <= 2000}, params)], :-g= g)[], 
    y= 200..400, Sr= 200..400
):
TotP_g(0);
    [94829.4555304706155, [Sr = 400., y = 317.129445700422]]

TotP_g(1);
  [114611.718817373156, [Sr = 400., y = 364.268415001416]]

 

Shouldn't the command point(C, a, -c) be point(C, a, -b)?

This is I think the most direct translation:

[ListTools:-Deal](
    [seq](
        `if`(x<0, x+2^16, x), 
         x= FileTools:-Binary:-ReadFile("Namefile", datatype= integer[2])
    ), 
    8
);

ListTools:-Deal combines Mathematica's Transpose@Partition into a single command. Deal was added in Maple 2021, so if you have older Maple, this'll need to be adjusted.

The output of FileTools:-Binary:-ReadFile is an Array. The [seq] converts that to a list. I only did that in the spirit of "direct" translation; an Array or Matrix would likely be better suited to your ultimate purpose. That would certainly be true if memory is an issue.

Maple's integer[2] format is signed 2-byte integers. The `if`(x < 0, x+2^16, x) converts them to unsigned.

Yes, Maple has the updating assignment operators += and /=, as well as ++, -=, --, .=*=mod=, ^=,=||=and=or=xor=, implies=, union=, intersect=, and minus=. To answer your first-sentence question: Yes, I do use these, every chance I get! Like all syntax enhancements made in the last 4--5 years, these only work in 1-D input. These are documented on the help page
?assignment.

You wrote: 

  • the divide / keeps applying as the single divide and a long line under the variable before I can type =.   This happens in both 1-D and 2-D.  

That's impossible in 1-D. If your display is changing the positions, size, or orientation (such as slant to horizontal) of the characters that you type, then you're definitely using 2-D Input.

As Christian said, you can't use error as a variable; it's a reserved word. You could use `error` however.

Change from 0.5 to 1/2. Then simplify f(x) before plotting:

plot(simplify(f(x)), x= 0..10);

I wouldn't call it a "bug". It's just the usual floating point abnormalities that come from evaluating a complicated expression. Note that the unsimplified f(x) has 10 positive and 10 negative terms with denominators ranging from 10 to 19,353,600 and powers as high as x^10.

Let's suppose that the population is infinite, or at least so large that the 100 people are an insignificant fraction of it. Then the "sampling distribution" is Binomial(100, 0.3). Suppose that we want to draw 9 samples of size 100 from that population, and count the number in each sample who'll get sick:

[seq](trunc(x), x= Statistics:-Sample(Binomial(100, .3), 9));
             
[28, 20, 26, 36, 35, 38, 33, 40, 23]

Now let's suppose that the population is some known relatively small number, say 300. Then the number of those who'll get sick is 300*0.3 = 90. Still, we want 9 samples of size 100. The sampling distribution is Hypergeometric(300, 90, 100)

[seq](trunc(x), x= Statistics:-Sample(Hypergeometric(300, 90, 100), 9));
              [32, 30, 28, 38, 33, 25, 28, 33, 29]

The desired form can be obtained by

expand(simplify(eval(eq_5_22, solve(eq_5_23, {L__ad}))));

In other words, eq_5_23 is used to eliminate L__ad from eq_5_22. It makes no difference whether you use eq_5_23 or eq_5_23x. There's a way to combine the operations simplifyeval, and solve in the above called "simplifying with side relations" (see help page ?simplify,siderels):

expand(simplify(eq_5_22, {eq_5_23}, [L__ad]));

I'm having trouble conceiving of this operation as a generalized form of the standard algebraic operation "collecting", but I haven't given it much thought. 

There are three distinct ways to interpret your Question. By "distinct", I mean that your Question is not vague; the different interpretations are based on different definitions of function and input, which are words that you used in your Question. @tomleslie  and @dharr  have each given correct solutions (although Tom's is far more elaborate than need be) to exactly one of those interpretations (each picking a different interpretation). But I am convinced that it's that third as-yet-unmentioned interpretation that you're really interested in, although you may not realize that yet.

First, we need precise definitions of functionprocedureparameter, and argument in the specific context of Maple code and this Question.

Function and procedure (which are fairly common words of technical English) are often used somewhat interchangeably in computer science, in the Maple community (such as this forum), and even in Maple documentation. However, when they are used as type names in Maple code, they are completely distinct, mutually exclusive, without overlap [I realize that that's redundant; I'm just emphasizing]. Both function and procedure are keywords that can be used as type names in Maple code.

FunctionSuppose that f is an unassigned variable (aka, a symbol or name). Then expressions such as f()f(a,b,c), and f(1,2,3) are of type function. The a, b, c,  1, 2, 3, etc. are called arguments. Functions (in this sense) are simply static data structures; they don't "do" any computations; rather, they are objects upon which computations are done. (In the complete definition of type function, the f can be more general than just a symbol or name; however, those cases alone are adequate to complete this discussion.) Note that f by itself is not a function. A function must have an argument sequence, even if it's empty as with f()

Procedure: An expression such as (x, y, z)-> (x+y)/z is a procedure. (There are other ways to make procedures, but the arrow -> is sufficient for our purposes.) The items on the left side of the -> are called parameters; unlike arguments, they are always names (and, more specifically, symbols).

Usually (but certainly not always) a procedure will be assigned to a name, as in p:= (x, y, z)-> (x + y + z). Then the procedure can be "called" (aka "invoked", "run", "used", "executed") via p(a, b, c)p(1, 2, 3), etc. The a, b, c,  1, 2, 3, etc. are called arguments, just like with a function. Procedures can be called in other ways, but the values substituted for (or passed to) the parameters are still called arguments. Note that the procedure is the assigned value of itself and alone; in contrast to a function, it's not the presence of arguments that makes it a procedure; rather it's the presence of a parameter sequence (which is possibly the empty sequence) in the assigned value that does that.

So, the word "input" that you used in your Question could mean either parameters or arguments, and the word "function" that you used could mean either function or procedure. You haven't used the words incorrectly; rather, the issue is that their common definitions in technical English aren't precise enough for this Question. The three possible interpretations of your Question are that you want to count

  1. the arguments of a function,
  2. the arguments passed to a procedure, or
  3. the parameters of a procedure.

So, you may be thinking that there's a fourth possibily---the parameters of a function---but there's no such thing under the precise definition of function being used here.

Maple syntax allows a procedure to be passed a different number of arguments than it has parameters. The count of arguments or parameters is called arity, which is a noun derived from the adjectival suffix -ary used in words such as binaryunarynullaryternary, etc. In other words, arity is the formal word for what you've called "input number" and "domain number".

  • Tom's Answer shows an excessively elaborate method to count the arguments of a function. It's excessive because it can be done simply with the command nops, e.g., nops(f(a, b, c)) returns 3. The command name nops stands for number of operands
     
  • The Answer by dharr shows a method to count the number of arguments passed to a procedure. The keywords _npassed or nargs can be used for this. They mean exactly the same thing. These keywords are only meaningful when used within the code of the procedure.
     
  • But I think that you want to count (and also, to list) the parameters of a procedure. That's the only way to perform the operation that you pseudo-coded in your Reply to dharr's Answer.

A procedure created with the arrow -> is also of type operator. The code I give in the rest of this Answer is only meant to be used with that type of procedure. Other procedures allow for far-more-complicated kinds of parameter specifications for which this code will not always work.

The parameters of an operator-procedure (such as p given above) can be counted by 

nops([op](1, eval(p)))

The symbols of its parameters can be listed by

convert~(op~(1, [op](1, eval(p))), `local`)

Using this, your pseudo-coded example can be written in Maple like this:

F:= (x,y,z)-> (r[x]+r[y])*r[z]:

G:= proc(f::And(procedure, operator), n::nonnegint)
local params:= convert~(op~(1, [op](1, eval(f))), `local`)[];
    (`+`(params) + f(params)/2)*n
end proc
:

G(F, 3);

This is a generalization of the procedure dharr gave for the same puspose. It'll work for some cases for which his will not, (although I think it's likely that his will work for all the cases that you're actually interested in). The 3 problematic cases are

  1. procedures that aren't operators: my code specifically rejects them;
  2. parameter symbols which have been assigned values as global variables: these will work superficially with dharr's code, but the output value will change when it's "evaluated", which is very likely to happen automatically;
  3. parameters with a type specification, such as n::nonnegint.

Here are the steps that I used. I only used Maple to do a simplified version of the integral (and, later, to do a verification in exact arithmetic). I'm sure that more steps could be done with Maple, but I didn't try because doing them in my head was easy enough, and likely faster.

  1. Use eq1 to eliminate c[2] in eq2.
  2. Factor (m/2/(m+2))^(1/m) out of the integral and move it to the other side of the equation.
  3. The remaining integral is int((Z^2 - x^2)^(1/m), x= 0..Z). Maple 2022.1 will do this integral, giving me
    (Z^2)^(1/m)*GAMMA(1/m+1)*Z*sqrt(Pi)/2/GAMMA(1/m+3/2).
  4. Combine the powers of to get Z^((m+2)/m) and move the other factors to the other side of the equation.
  5. Raise both sides to power m/(m+2) to get 
    Z = (alpha*2*GAMMA(1/m+3/2)/GAMMA(1+1/m)/sqrt(Pi)*(2*(m+2)/m)^(1/m))^(m/(m+2)).
  6. Substitute this for in eq1 to get c[2].
  7. I verified my solution for and c[2] in exact arithmetic at alpha=6, m=2 (same as acer). I didn't do a decimal-arithmetic and/or numeric-integration verification. Amazingly, I didn't make a single mistake rearranging all those fractions in my head.

I've implicitly assumed Z >= 0; thus both the integrand and the integral are nonnegative.

The system can be seen with the naked eye (due to the repetition of coefficients) to be inconsistent. There is no solution. Indeed, in a sense, it's maximally inconsistent: Every pair of equations is inconsistent.

First 41 42 43 44 45 46 47 Last Page 43 of 394