Expand an expression in Maple contening modulus - complex-numbers

I want to develop an expression such as |a+b+c|^2 . I tried the command " expand " but it does not work. ( it works without the modulus )

The issue is that expand is unable to recognize whether a, b and c are positive or negative. So, you make that clear then expand will work the way it should.
restart:
A:=abs(a+b+c)^2;
assume(a>0,b>0,c>0):expand(A);
a^2+2*a*b+2*a*c+b^2+2*b*c+c^2
if
assume(a>0,b>0,c<0):
then use
expand(simplify(A))
Now, lets take three complex numbers and then take its modulus square,
B:=abs((a1+b1*I)+(a2+b2*I)+(a3+b3*I))^2;
Once again the same issue as above.
assume(a1>0,b1<0,a2>0,b2<0,a3<0,b3>0):expand(simplify(B));
a1^2+2*a1*a2+2*a1*a3+a2^2+2*a2*a3+a3^2+b1^2+2*b1*b2+2*b1*b3+b2^2+2*b2*b3+b3^2
Finally, assign some random values for a's and b's and find modulus,
subs(a1=1,a2=1,a3=1,b1=1,b2=2,b3=3,B);

Related

When merging runs A,B in Timsort (in function merge_lo) it says "Must also have that ssa.keys[na-1] belongs at the end of the merge". Why?

A and B are adjacent runs on a stack, with A being the bottom and smaller run(If B were smaller merge_hi would be performing the merging but the same question applies there as well).I have been trying to figure why the last element of A MUST be bigger than the last element of B because I don't see how the run decomposition (or the rest of the algorithm) would ensure that condition. Also, in the same function the code seems to suggest that the first element of B is always smaller than the first element of A which I also don't understand why, but I'm guessing the answer to this question is tied to the answer of the first question.
In short, galloping is the reason. That's why we haven't seen it at first, because I thought gallop_{left,right} is called only from merge_{lo,hi}. But it isn't true. gallop_... are also called from merge_at, before merge_{lo,hi} are called, in order to find "Where does b start in a?" and "Where does a end in b?". Those calls (and subsequent code) change ssb (and its length nb), and also ssa and its length na, such that the invariant in the title is satisfied.
That is, the point is that A and B are not "adjacent runs on the runstack" as found in the original list to be sorted. Before calling merge_{lo,hi}, they are trimmed so that the condition in the title holds. That is, prior to A being merged with B, the elements of B greater than the last element of A are explicitly left out of consideration. In other words, "Must also have that ssa.keys[na-1] belongs at the end of the merge" is true not because of some special property of ssa.keys[na-1], but because we have defined "the end of the merge" in such a way. :-)
That also means that when you implement Timsort yourself, and you leave out the galloping, you must also leave out that optimization being talked about here, otherwise the code won't work right.

#NLConstraint with vectorized constraint JuMP/Julia

I am trying to solve a problem involving the equating of sums of exponentials.
This is how I would do it hardcoded:
#NLconstraint(m, exp(x[25])==exp(x[14])+exp(x[18]))
This works fine with the rest of the code. However, when I try to do it for an arbitrary set of equations like the above I get an error. Here's my code:
#NLconstraint(m,[k=1:length(LHSSum)],sum(exp.(LHSSum[k][i]) for i=1:length(LHSSum[k]))==sum(exp.(RHSSum[k][i]) for i=1:length(RHSSum[k])))
where LHSSum and RHSSum are arrays containing arrays of the elements that need to be exponentiated and then summed over. That is LHSSum[1]=[x[1],x[2],x[3],...,x[n]]. Where x[i] are variables of type JuMP.Variable. Note that length(LHSSum)=length(RHSSum).
The error returned is:
LoadError: exp is not defined for type Variable. Are you trying to build a nonlinear problem? Make sure you use #NLconstraint/#NLobjective.
So a simple solution would be to simply do all the exponentiating and summing outside of the #NLconstraint function, so the input would be a scalar. However, this too presents a problem since exp(x) is not defined since x is of type JuMP.variable, whereas exp expects something of type real. This is strange since I am able to calculate exponentials just fine when the function is called within an #NLconstraint(). I.e. when I code this line#NLconstraint(m,exp(x)==exp(z)+exp(y)) instead of the earlier line, no errors are thrown.
Another thing I thought to do would be a Taylor Series expansion, but this too presents a problem since it goes into #NLconstraint land for powers greater than 2, and then I get stuck with the same vectorization problem.
So I feel stuck, I feel like if JuMP would allow for the vectorized evaluation of #NLconstraint like it does for #constraint, this would not even be an issue. Another fix would be if JuMP implements it's own exp function to allow for the exponentiation of JuMP.Variable type. However, as it is I don't see a way to solve this problem in general using the JuMP framework. Do any of you have any solutions to this problem? Any clever workarounds that I am missing?
I'm confused why i isn't used in the expressions you wrote. Do you mean:
#NLconstraint(m, [k = 1:length(LHSSum)],
sum(exp(LHSSum[k][i]) for i in 1:length(LHSSum[k]))
==
sum(exp(RHSSum[k][i]) for i in 1:length(RHSSum[k])))

Mathematica: Commands return no output, but itself. Bug?

I am working with Wolfram Mathematica 8 and have the following problem. I have an optimization problem under certain constraints and want to have an analytical (symbolical solution). I am maximizing function piA. My input is:
piA[a_, WA1_, WA0_] =
a/(1 + a)*(X - (y*WA1 + 1)^(1/y)) - 1/(1 + a) ((y*WA0 + 1)^(1/y));
Maximize[{piA[a, WA1, WA0], WA0 >= -1/y, WA1 >= -1/y}, WA0]
What I get most of the times is:
Maximize[{-((1 + WA0 y)^((1/y))/(1 + a)) + (
a (X - (1 + WA1 y)^(1/y)))/(1 + a), WA0 >= -(1/y), WA1 >= -(1/y)},a]
Basically, the command does nothing, but outputs itself. Only once I have managed to get the proper output (too long to paste here). I have tested it with simpler functions and it works. Unfortunately, I cannot understand what causes the problem. It is not a syntax problem, since it has worked like that several times. Any help would be very much appreciated.
P.S. Just checked again and my input ALWAYS generates the wrong output. The time it generated the solution was when I accidentally set parameters X and y to certain numbers.
The most likely reason is that given the function and constraints, Mathematica doesn't know how to maximize your function with respect to WA0. Note you also have a free variables X and a in there, and it might not have enough information about the domain of X and a to be able to properly form a solution to your equation.
I've had instances where I tried feeding in some equations and constraints and Mathematica simply couldn't do anything with them because they were too general. This may be the case here as well. Is there a specific problem you're trying to solve, and is there any way you could give Mathematica more context?
I don't think this is a bug at all, but it's unfortunate that sometimes Mathematica will just spit back your input when it doesn't have any rules for solving what you gave it.
The usual reason these things happens seems to be when the expressions given are too general for Mathematica to handle, or when it it's faced with a set of expressions that are ill formed.
Just as an example, I tried passing in fractions into a function I wrote that specifically looked for rational expressions, thinking it would work. It turned out that it needed to handle both Rational[a, b] and Times[a, Power[b, -1]]. It could be the case that Mathematica is not expecting a constraint to be of the form GreaterEqual[a, b].
Mathematica returns an answer if you assign the variable a some value. Maybe you could build your strategy on that? In fact it does provide an answer if you assign a value to any of the variables.
( I would need more background of the problem to go from there... )

Question about using Maple's arrow (->) notation

Hi can someone show me how to do this:
Your function should take a Maple list of complex numbers as its input and return the largest modulus from that list.
Hint: Use the map command to apply the abs command to some such list. Then apply the max command to that. Now repeat, by composing that second operation around the first. Finally, create an operator which takes L to that composed operation applied to L.
Look to the help-pages ?max, ?map, ?abs, and ?operators,functional, if necessary.

can a variable have multiple values

In algebra if I make the statement x + y = 3, the variables I used will hold the values either 2 and 1 or 1 and 2. I know that assignment in programming is not the same thing, but I got to wondering. If I wanted to represent the value of, say, a quantumly weird particle, I would want my variable to have two values at the same time and to have it resolve into one or the other later. Or maybe I'm just dreaming?
Is it possible to say something like i = 3 or 2;?
This is one of the features planned for Perl 6 (junctions), with syntax that should look like my $a = 1|2|3;
If ever implemented, it would work intuitively, like $a==1 being true at the same time as $a==2. Also, for example, $a+1 would give you a value of 2|3|4.
This feature is actually available in Perl5 as well through Perl6::Junction and Quantum::Superpositions modules, but without the syntax sugar (through 'functions' all and any).
At least for comparison (b < any(1,2,3)) it was also available in Microsoft Cω experimental language, however it was not documented anywhere (I just tried it when I was looking at Cω and it just worked).
You can't do this with native types, but there's nothing stopping you from creating a variable object (presuming you are using an OO language) which has a range of values or even a probability density function rather than an actual value.
You will also need to define all the mathematical operators between your variables and your variables and native scalars. Same goes for the equality and assignment operators.
numpy arrays do something similar for vectors and matrices.
That's also the kind of thing you can do in Prolog. You define rules that constraint your variables and then let Prolog resolve them ...
It takes some time to get used to it, but it is wonderful for certain problems once you know how to use it ...
Damien Conways Quantum::Superpositions might do what you want,
https://metacpan.org/pod/Quantum::Superpositions
You might need your crack-pipe however.
What you're asking seems to be how to implement a Fuzzy Logic system. These have been around for some time and you can undoubtedly pick up a library for the common programming languages quite easily.
You could use a struct and handle the operations manualy. Otherwise, no a variable only has 1 value at a time.
A variable is nothing more than an address into memory. That means a variable describes exactly one place in memory (length depending on the type). So as long as we have no "quantum memory" (and we dont have it, and it doesnt look like we will have it in near future), the answer is a NO.
If you want to program and to modell this behaviour, your way would be to use a an array (with length equal to the number of max. multiple values). With this comes the increased runtime, hence the computations must be done on each of the values (e.g. x+y, must compute with 2 different values x1+y1, x2+y2, x1+y2 and x2+y1).
In Perl , you can .
If you use Scalar::Util , you can have a var take 2 values . One if it's used in string context , and another if it's used in a numerical context .