Carl Love

Carl Love

26488 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

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.

The way that coeffs is used in Christian Wolinski's Answer is so common to me (yet awkward also) that in almost all cases I replace coeffs with this: 

Coeffs:= (e::algebraic, v::{list, set, thistype}({name, function}))->
    local T, C:= coeffs(e,v,T); table(sparse, [T]=~[C])
:

This makes the usage more like coeff and much less awkward:

F:=2*y*sin(beta*x)+6*z*cos(beta*x)+24*sin(beta*x)*cos(beta*x):
C:= Coeffs(F, [sin,cos](beta*x)):

C[sin(beta*x)];

                              2 y

C[cos(beta*x)];
                              6 z

C[(sin*cos)(beta*x)];
                               24

My procedure Coeffs above is essentially the same as in the Answer that you linked to but that you said didn't work for you. The crucial difference that makes this one work for your case is that I included function in the allowed variable types.

Do you need to use specifically numer and denom for this purpose? If not, then you could just extract the 1st and 2nd operands of the inert quotient. For example,

x:= 3%/6:
op(1,x); op(2,x);

It's also possible to overload numer and denom so that they detect the presence of inert quotients and respond accordingly. This is fairly easy, but it's overkill if what I suggested in the first paragraph fulfills your need. On the other hand, an overload solution might be preferable if you want to apply numer and denom to general non-inert expressions that contain inert quotients as subexpressions.

Reading Matlab's help page for deal, I see that its purpose is to work around a purely syntactic issue with multiple assignment. An equivalent in Maple could use the keyword _nresults, which gives a count of the number of operands on the left side of the assignment operator. The keyword only works inside a procedure; so since we need a procedure anyway, we might as well name it deal:

deal:= X-> `if`(_nresults=undefined, X, seq(copy(X), _nresults));

#Example:
(A,B):= deal(Matrix(2,2));

Note that copy behaves correctly even for objects that can't be copied (which are called immutable in Maple).

Maple has a package named MTM (not to be confused with the package named Matlab) that has Maple analogues of a large number of Matlab commands with exactly the same names that they have in Matlab. (But it doesn't have deal.) Thus, this package helps you translate Matlab code to Maple. The MTM package works strictly within Maple; on other the hand, the Matlab package is for sending commands to an actively running copy of Matlab.

I don't understand the purpose of your solve commands, and I don't think Maple understands either. What is m? The highly symbolic output of these solve commands is obviously useless for plotting the 4 vertices of a rectangle.

Are you trying to circumscribe an ellipse with the minimal rectangle whose sides are parallel to the coordinate axes? If so, then your aa and bb should be the maximal x and y values on the ellipse. These can computed via implicit differentiation (command implicitdiff(ell, y, x)) for arbitrary ab, and theta. I get

aa:= sqrt((a^2-b^2)*cos(theta)^2 + b^2);
bb:= sqrt((b^2-a^2)*cos(theta)^2 + a^2);

 

If p is the PLOT structure, then do

rhs~(indets(p, "originalview"=anything))[];
             [-0.986250000000000071 .. 9.98625000000000007, -1. .. 1.]

Using "magic" position numbers and op seems risky: The numbers could easily change. Using getdata is slightly more robust, but still involves magic numbers. My method is based on the keyword "originalview"; it uses no magic numbers.

Edit: The identical(...that I had before is superfluous: A string constant can be used itself as a type.

First 29 30 31 32 33 34 35 Last Page 31 of 382