acer

32485 Reputation

29 Badges

20 years, 7 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by 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

One problem is that, in Maple 11.02, there is no member Size in the ArrayTools package. Something like this might serve as an alternate (for Array input) to the simple way that you are calling it,

> Size := proc(M::Array,n::posint)
> local rng;
>   rng := ArrayDims(M)[n];
>   abs(op(2,rng)-op(1,rng))+1;
> end proc:
>
> A := Array(0..1,-4..3,1..6,-17..-15):
> Size(A,1),Size(A,2),Size(A,3),Size(A,4);
                                  2, 8, 6, 3

Another issue is that you omitted the uses statement in your procedure. Alec's proc had this line, which allowed Determinant and the Typeset calls to resolve to the exports of their respective packages,

uses ArrayTools, LinearAlgebra, Typesetting;

acer

CrossProduct is not a "top-level" command. There are a few routines with that name, inside packages. See the using packages help-page.

My guess is that you have omitted the step that loads the appropriate package, and so the apparent call to a CrossProduct routine returns just the unevaluated function call. Try issuing this first,

with(VectorCalculus):

or,

with(LinearAlgebra):

depending on which was in use in that part of the textbook.

acer

Why is the result of residuals(N1, N2, lambda1, lambda2) being wrapped in a list? Try not enclosing it in [] square brackets before passing to NLPSolve, or pass op() of it as the objective.

acer

First 298 299 300 301 302 303 304 Last Page 300 of 337