acer

32747 Reputation

29 Badges

20 years, 112 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

If only one feasible point is wanted, then the following can be quick,

> eqs:={20<=a,a<=60, 20<=b,b<=60, 20<=c, c<=60,
>       20<=d,d<=60, a+b+c+d=160, 4*a+6*b+8*c+12*d=1200}:

> Optimization:-Minimize(1,eqs,assume=integer);
                     [1, [a = 40, b = 40, c = 40, d = 40]]
 
> eval(eqs,%[2]);
                 {160 = 160, 1200 = 1200, 20 <= 40, 40 <= 60}

If you want to characterize all the solution space, and can wait, then the following might do,

SolveTools:-Inequality:-LinearMultivariateSystem(eqs,[a,b,c,d]);

acer

Note, when you say "simplify units in 2D math" it appears that you actually mean simplify units using the context-menus.

1) After executing the assignment to m1, and right-clicking, I did get the Units submenu in the pop-up context-menus. (Unfortunately in Maple 13 applying the Units->Simplify menu-item produces new 2D Math output 2*Unit(kg) instead of the properly typeset 2*[[kg]] as it did in Maple 12. A bug.)

2) For the equation F=m1*a the context-menu system is not providing Units->Simplify. I suspect that this is because it is only coded to detect an algebraic object (with units). It would indeed be an improvement, if it also detected an equation where either rhs or lhs had units attached.

3) If you have assigned to symbol m then you can indeed unassign by issuing either of these commands (as you tried, and where in fact that unassignment succeeded).

> m := 'm':

> unassign('m');

The 1D Math input command Unit('m') should work even when symbol m has been assigned a value. One combination that does not seem to work is using the [[m]] palette entry in a Document following assignment to the global name m.

But one can still get the nicely typeset [['m']] as 2D Math with the quoted name 'm' in the input by using command-completion. That will display as [[m]] in the output, without quotes. Suppose that I type in 45*Unit<Ctl-shift-spacebar> to get command completion suggestions in a pop-up. I choose "unit" which is the top suggestion. Then for the highlighted x I type in 'm'.

Note. The exact keystrokes to get command-completion suggestions may vary with platform. On Linux, I used Ctl-shift-spacebar.

You tried to use Units:-Natural at one point. I strongly advise against this. That subpackage's environment makes all these namespace issues much worse. Also, another reason it didn't work for you because your Document restarts after loading that package. The restart clears whatever packages are loaded.

4a) I don't understand what you mean about "a value included in the numerator", sorry.

4b) To separate an expression and its units, one can use the command convert,unit_free. It may be appropriate to first apply combine,units. For example,

> restart:
> F := 45*Unit(kg)*Unit(m)/Unit(s^2);
                                    45 [m]
                               F := ------ [kg]
                                     [ 2]
                                     [s ]
 
> F_value := convert(combine(F,units),unit_free,F_units);
                                 F_value := 45
 
> F_units;
                                      [N]

Note. You might consider loading the Units:-Standard package, which would bring about some automatic combining & simplification of units for arithmetic done at the top-level. Even if you choose not to load that package, there are commands to manipulate several aspects of the units in expression, which might serve even when the smaller subset of functionality in the right-click context-menus isn't working out for you.

acer

You'll need to compile and link as .dll (or .so on Linux, etc).

See define_external and related help.

acer

with(Statistics):
oldpb:=kernelopts(printbytes=false):
to 4 do
  r:=evalf(Sample(RandomVariable(Normal(0,1)),1)[1],1);
  print(r); # or DocumentTools:-Do
  st:=time[real]();
  while time[real]()-st < 2 do end do;
  end do:
kernelopts(printbytes=oldpb):

acer

Do you mean this sort of forced rearrangement, of a set of equations? 

> problematic_set:={qk2=5,qk1=17,qk3=13};
               problematic_set := {qk1 = 17, qk2 = 5, qk3 = 13}

> my_forced_order:=eval([qk3,qk2,qk1],problematic_set);
                        my_forced_order := [13, 5, 17]

> my_forced_order:=[seq(eval(qk||(4-i),problematic_set),i=1..3)];
                        my_forced_order := [13, 5, 17]

> my_forced_order:=[seq(qk||(4-i)=eval(qk||(4-i),problematic_set),i=1..3)];
               my_forced_order := [qk3 = 13, qk2 = 5, qk1 = 17]

acer

Maybe something like this?

> f := (x,y) -> cos(x+y): # or more complicated

> g:=(a,b)->a^2+b^2: # or more complicated

> F := y->(fsolve(f(x,y),x=0..1/2),y): # returns x,y pair

> F(1.4);
                               0.1707963268, 1.4

> f(%); # should show that last was a zero of f
                                              -9
                              -0.2051033808 10

> plot(g@F, 1.1..1.6);

You might also check out transforming the implicit plot of f.

> k:=plottools:-transform((a,b)->[b,g(a,b)]):
> P1:=plots:-implicitplot(f,0..1/2,1.1..1.6):
> plots:-display(k(P1));

> # ...or, as function of first value (as you asked, I think)

> k:=plottools:-transform((a,b)->[a,g(a,b)]):
> plots:-display(k(P1));

> FF := x->(x,fsolve(f(x,y),y=0..3)): # returns x,y pair
> FF(0.2);
                               0.2, 1.370796327

> f(%);
                                              -9
                              -0.2051033808 10

> plot(g@FF, 0..0.5);

acer

Change the lowercase int() calls to Int(). That should be enough to allow plot to use numerical quadrature instead of churning away on symbolic integration at each x-y pair.

You said that you had a function f(x,y), but it looks like an expression. Hopefully, any such discrepency will be sorted by you.

acer

It's difficult to answer this well, without knowing what your code does. You may need to paste or upload an example of it going wrong for you.

Some advice can still be given:

  • Vector() is different from vector().
  • evalm() is not appropriate for uppercase Vector or Matrix.
  • A+B should work directly for Vectors A and B. (It's a shortcut to LinearAlgebra:-VectorAdd.)
  • row Vectors are different from column Vectors.

The error message "invalid arguments" from rtable/Sum arises when one attempts to add mismatched objects. For example,

> V1:=Vector(2):
> V2:=Vector(5):
> V3:=Vector[row](2):
> M:=Matrix(1..2,1):

> V1+V2: # size mismatch
Error, (in rtable/Sum) invalid arguments
> V1+V2[1..2]:  # ok

> V1+V3: # orientation mismatch
Error, (in rtable/Sum) invalid arguments
> V1+V3^%T:  # ok

> V1+M: # object mismatch
Error, (in rtable/Sum) invalid arguments
> V1+convert(M,Vector): # ok

Did I see you using Statistics in another post? It produces row Vectors, while the default for the Vector() constructor is column vectors. Perhaps that is your issue.

> with(Statistics):
> V := Sample(Normal(0,1),2);
              V := [-0.479341933636560025, 0.784173699733298979]
 
> op(0,V);
                                  Vector[row]
 
> type(V,'Vector[column]');
                                     false
 
> type(V,'Vector[row]');
                                     true
 
> type(V,'Vector');
                                     true
 
> V + Vector(2,[a,b]);  # orientation mismatch
Error, (in rtable/Sum) invalid arguments

> V + Vector(2)^%T;  # transpose either, to match
                 [-0.479341933636560025, 0.784173699733298979]
 
> with(LinearAlgebra):
> V + Transpose(Vector(2));  # transpose either, to match
                 [-0.479341933636560025, 0.784173699733298979]

acer

> E := t* (1-exp(-(1-t)^2/(2*o^2))):

> f := oo -> Optimization:-Maximize(eval(E,o=oo),t=0..1)[1]:

> # test it first
> f(0.5);
                             0.205693857126463153
 
> eval(E,o=1/2);
                                                2
                           t (1 - exp(-2 (1 - t) ))
 
> Optimization:-Maximize(%,t=0..1);
              [0.205693857126463153, [t = 0.417189146242829434]]

> # now plot it
> plot(f,0..1);

> # alternate way
> plot('Optimization:-Maximize'(E,t=0..1)[1], o=0..1);

acer

On 64bit Maple, 2^(2^65536) will produce a numeric exception (overflow). And B(4,1,4) is already on the order of 2^(2^(2^65536)). So what were you planning on doing with B(5,2,5)?

acer

This might be a reference to some of the Tutors in the Student[Calculus1] package. See the SingleStepOverview help-page.

For less structured but more verbose ways to see what Maple is doing, look at the help-pages for infolevel, trace, and printlevel.

acer

Are you just asking for a way to store a large Vector whose entries are precomputed points on the n-dimensional hypersphere? Would a point generator suffice, if it were fast enough? Do you want a completely even set of points, or a (statistical) uniform distribution? Can you go from here?

acer

The parsing of strings will produce global names such as :-Sample. So your example acts like this,

> use Statistics in :-Sample(:-RandomVariable(Normal(0, 1)),100)  end use;
                   Sample(RandomVariable(Normal(0, 1)), 100)

The names in that output are the untouched global (:- prefixed) names.

All I can think of right now, to make this work for you, is substitution.

> whole:=[seq(x=Statistics[x],
>             x in map(convert,[exports(Statistics)],`global`))]:

> use Statistics in
>   eval(parse("Sample(RandomVariable(Normal(0, 1)),100)",statement),whole);
> end use;
                          [ 100 Element Row Vector ]
                          [ Data Type: float[8]    ]
                          [ Storage: rectangular   ]
                          [ Order: Fortran_order   ]

> use Statistics in
>   eval(:-Sample(:-RandomVariable(Normal(0, 1)),100),whole);
> end use;
                          [ 100 Element Row Vector ]
                          [ Data Type: float[8]    ]
                          [ Storage: rectangular   ]
                          [ Order: Fortran_order   ]

This is one of the very few times that I've wanted the [] rather than the :- notation for referencing module exports, so as to be able to generate that substitution sequence programatically.

acer

I'm not sure that I quite follow. If you have assigned something to name expr then entering simply expr; at the top-level, or print(expr); inside a procedure, should display its current value.

This brings up a related notion. People often talk about assigning to lots of variables. (It's a FAQ here, how to make assignments from the sequence of results returned by solve. The best answer is probably "Why do you want to do that?") A great deal of the time it is easier to instead use 2-argument eval, eg, eval(expr,[solve_answer]). Assigning to a whole bunch of variables is often just unnecessary. It brings the difficulty that, once x say has a value, it can't be used to form a new general expression unless its first unassigned.

And this is all aside from the general under-use of procedures in people's code, which deserves a blog in its own right.

acer

One should supply either a list or a Vector according to which particular form is used.

> NonlinearFit((x,y,a,b,c)->exp(a+b*x+c*y), XY, Z,
>              initialvalues=initxy);

                            [-0.209656805295074966]
                            [                     ]
                            [0.413711756831582622 ]
                            [                     ]
                            [0.0552760189352312359]

> NonlinearFit(exp(a+b*x+c*y), XY, Z,
>              [x,y],initialvalues=[a=1,b=2,c=3]);
 exp(-0.209656805295074966 + 0.413711756831582622 x + 0.0552760189352312359 y)

In your attempt, you use an expression with names a,b,c and a 3-Vector. But you could have used tt,rr,ss as the names. How then would NonlinearFit know which name to associate with which entry of the 3-Vector? There's no really great way to infer the (intended) order of the names, so as to be able to associate them with the Vector. But in the case of an operator (procedure) rather than an expression, the unnamed entries of the Vector can be naturally matched with the procedural parameters of the operator.

 acer

First 301 302 303 304 305 306 307 Last Page 303 of 341