pagan

5147 Reputation

23 Badges

17 years, 127 days

 

 

"A map that tried to pin down a sheep trail was just credible,

 but it was an optimistic map that tried to fix a the path made by the wind,

 or a path made across the grass by the shadow of flying birds."

                                                                 - _A Walk through H_, Peter Greenaway

 

MaplePrimes Activity


These are answers submitted by pagan

The DynamicsSystems package was new in Maple 12. See ?updates,Maple12,newpackages

 

Why not just this simpler way, also using D?

dv:=D(v);

So for example

> v:=x->sin(x)+x^3;
                                       3
                        x -> sin(x) + x 

> dv:=D(v);
                                        2
                       x -> cos(x) + 3 x 

> dv(1);
                           cos(1) + 3

In the original attempt using `diff`, the formal parameter x is being abused in a faulty attempt to also make it the dummy variable of differentiation. You might alternatively try to correct your original use of `diff`, like this

dv:=proc(x) local t; eval(diff(v(t),t),t=x); end proc;

For your given example

> a:=[["he","45",123,76,"1.0",4],["know","4",9,34,"3.2",5]]:

> subsindets(a,string,parse);
            [[he, 45, 123, 76, 1.0, 4], [know, 4, 9, 34, 3.2, 5]]

But you could also map it in.

> map~(t->`if`(type(t,string),parse(t),t),a);
     [[he, 45, 123, 76, 1.0, 4], [know, 4, 9, 34, 3.2, 5]]

Converting every entry of each sublist to a string (or name) just so that parse can be applied looks like unnecessary cost. You can do it if you want, of course.

> map~(t->parse(convert(t,'string')),a); # or 'name' instead of 'string'
     [[he, 45, 123, 76, 1.0, 4], [know, 4, 9, 34, 3.2, 5]]

Eventually, you may drift away from code in which you actually assign numeric values to names.

> restart:
> data:=[a=1,b=2,c=-1]:
> L:=[a,c,a,b,c,b,a]:

> LL:=eval(L,data): X:=max(LL):
> seq(`if`(LL[i]=X,[i,L[i]=X],NULL),i=1..nops(L)); # with position too
                     [4, b = 2], [6, b = 2]

> op(2,%[1]);
                             b = 2

> select(t->rhs(t)=X,data); # without position
                            [b = 2]

> b;
                               b

> eval(b,data);
                               2

There are lots of variants possible upon the above. Notice that you can refer to name b afterwards, as a name if you wish, since it is unassigned. Or you can easily get its value in `data`.

If you really need to assign to a,b, and c then you'd make life easier by only doing so after creating L. Note that in the assignment version below any subsequent handling of name b, as a name, is going to be a pain since you've assigned to it.

> restart:
> L:=[a,c,a,b,c,b,a]:
> a:=1: b:=2: c:=-1:

> X:=max(L):
> seq(`if`(L[i]=X,[i,eval(L,1)[i]=X],NULL),i=1..nops(L));
                     [4, b = 2], [6, b = 2]

> op(2,%[1]);
                            2 = 2

> b; 2

I'm not sure how to supply a weighting function as a direct argument to Statistics:-ExpectedValue in the case that the main argument is a RandomVariable (and not a concrete, finite sample). You could construct a new distribution which has a weighted density based upon the original random variable.

> restart:
> with(Statistics):

> assume(mu::real,sigma>0);

> X:=RandomVariable(Normal(mu,sigma)):

> Y:=RandomVariable(Distribution(PDF=subs(f=unapply(PDF(X,T),T),t->u(t)*f(t)))):
> # alternatively > # Y:=RandomVariable(Distribution(PDF=subs(f=PDF(X,T),t->u(t)*eval(f,T=t)))):
> EY:=ExpectedValue(Y); # returns an unevaluated int

> eval(EY,[u=1]);
mu

> eval(Variance(Y),u=1);
2
sigma

And you could make a procedure to do all that automatically.

> restart:

> assume(mu::real,sigma>0);

> X:=Statistics:-RandomVariable(Normal(mu,sigma)):

> MyExpectedValue:=proc(XX,ff)
>   local f, u, T;
>   uses Statistics;
>   eval(ExpectedValue(RandomVariable(Distribution(
>          PDF=subs(f=unapply(PDF(XX,T),T),t->u(t)*f(t))))),
>        u=ff);
> end proc:

> MyExpectedValue(X,1);
                               mu
> MyExpectedValue(X,U); # general form

[Next example added, to follow up comments below and clarify.]

> MyExpectedValue(X,t->(1-exp(-phi*t))/t);

        /                 /1    2      2\\             
        |exp(mu phi) - exp|- phi  sigma || exp(-mu phi)
        \                 \2            //             

> expand(%);
                            /1    2      2\
                         exp|- phi  sigma |
                            \2            /
                     1 - ------------------
                            exp(mu phi)    

Provided that all that subs action in PEffSQ is correct for your unstated definitions of SX, PCellMax and PInc, then for your objective

PEffSQ := proc(Egx, Egy) if Egy <= Egx then 100*evalf(subs(subs(Eg1 = Egx, Eg2 = Egy, EV),
SX(Egx, Egy), PCellMax/PInc)) end if end proc;

change

maximize(PEffSQ(eg1, eg2), initialpoint = {eg1 = 2.3, eg2 = 1.4}, variables = [eg1, eg2]);

to one of these

Optimization:-Maximize('PEffSQ'(eg1, eg2), initialpoint = {eg1 = 2.3, eg2 = 1.4},
 variables = [eg1, eg2]);

Optimization:-Maximize(PEffSQ, initialpoint = [2.3, 1.4]);

The problem is that the initial argument to your original maximization call would evaluate before eq1 and eq2 got values from `Maximize`. Under Maple's usual rules of evaluation, the arguments to a procedure call are evaluated first, before the computation is done. That is, it was evaluating PEffSQ(eq1,eq2) for unassigned names eq1 and eq2.

> PEffSQ(eg1, eg2);
Error, (in PEffSQ) cannot determine if this expression is true or false: eg2 <= eg1

So you can either delay the evaluation with uneval quotes or you can use the operator form of a Maximize call. Also, the relevant Optimization command is Maximize, not maximize.

A third alternative is to change the PEffSQ procedure so that it returns unevaluated for name arguments. If that were done then you could call it in your original manner.

PEffSQ := proc(Egx, Egy)
if not( type(Egx,numeric) and type(Egy,numeric) ) then
return 'procname'(args);
end if;
if Egy <= Egx then
100*evalf(subs(subs(Eg1 = Egx, Eg2 = Egy, EV), SX(Egx, Egy), PCellMax/PInc));
end if;
end proc:
> PEffSQ(eg1, eg2); # now it's ok PEffSQ(eg1, eg2)
Optimization:-Maximize(PEffSQ(eg1, eg2), initialpoint = {eg1 = 2.3, eg2 = 1.4},
 variables = [eg1, eg2]);

Let's examine an earlier post of yours, where you ran into roundoff problems, as an example.

The Document running Exploration assistant gets a new Maple kernel, which doesn't pick up Digits settings in the original worksheet. And you can't tell the GUI to share the old kernel, or else Maple hangs.

Here's a way to get a varying working precision using Explore (and indicating that it might be easier to do this using embedded components entirely in your original worksheet -- see here or here for notes on that.)

restart:

sCARA4 := -ln(-(mu/sigma^2)^(mu^2/(mu-sigma^2))*(sigma^2/mu)^(mu^2/(mu-sigma^2))
+(sigma^2/mu)^(mu^2/(mu-sigma^2))*((exp(phi)*sigma^2+mu-sigma^2)
*exp(-phi)/sigma^2)^(mu^2/(mu-sigma^2))+1)/phi:

dsCARA4:=diff(sCARA4,mu):

eval(subs(Q=subs(FOO=dsCARA4,proc(a,b,c,d) evalf[d](eval(FOO,[mu=a,phi=b,sigma=c])); end proc),
'Explore'(Q(mu,phi,sigma,digits))));

Using the above, you can set the ranges as follows, to see interesting bits. Don't toggle the checkbox that says "floating-point computation" in the initial Explore pop-up.

mu: 28.0 to 30.0
phi: 0.5 to 1.5
sigma: 0.5 to 1.5
digits: 10 to 20

Near phi=sigma=1.0 and mu=29.414 the result appears negative at low digits but more accurate and positive at higher digits of working precision.

The workaround is uglier than it ideally would be, because Explore has special evaluation rules, doesn't allow for lexical scoping in the expression, doesn't pick up the d in evalf[d] as an exploration variable, and because evalf(...,d) has remember issues. It would help if the Exploration assistant could share the kernel of the original worksheet. And the initial Explore pop-up's "floating-point computation" checkbox currently causes the explored expression to get wrapped in evalf, so this could all be much easier if that pop-up also had an entry box to set the digits with that instead being an evalf[d].

The result of Mean(X1) contains a q with assumptions on it. You `solve` call uses the global q. Maple sees them as being different names.

This is one of the differences between assume and assuming. The latter doesn't dig deep when replacing the assumed names in output.

This works

with(Statistics):

X1 := RandomVariable(Geometric(q)) assuming 0<q, q<1:

M:=Mean(X1);

Q:=op(indets(M,`local`));

s1:=solve({mu=M},{Q});

An alternative, to convert and replace all assumed names in Mean(xx) with their global equivalents, doesn't seem to work the way it ought to.

Is this the kind of thing you want to do?

> p:=proc()
> local alpha;
> return `tools/genglobal`(convert(alpha,`global`));
> end proc:

> q:=proc()
> local alpha;
> `tools/genglobal`(convert(alpha,`global`));
> end proc:

> seq( [p(),q()], i=1..10);
 [alpha, alpha0], [alpha1, alpha2], [alpha3, alpha4], 

   [alpha5, alpha6], [alpha7, alpha8], [alpha9, alpha10], 

   [alpha11, alpha12], [alpha13, alpha14], [alpha15, alpha16], 

   [alpha17, alpha18]

Or, perhaps this.

> r:=proc(x)
> return `tools/genglobal`(x);
> end proc:

> seq( r(beta), i=1..10);
 beta, beta0, beta1, beta2, beta3, beta4, beta5, beta6, beta7, beta8

Mostly it is written in... the Maple programming language.

See here.

Should the parent be converted to a Question?

Yes, it is not the fully correct answer.

Over the complex domain, the answer ln(x) is wrong on the branch cut of the negative real axis. Maple's indefinite integration does not return a piecewise result (which otherwise might be made to be correct everywhere). It's correct everywhere except on a set of measure zero, if that helps.

Also, the correct answer is not obtained under assumptions of "realness" for the domain of x, in Maple 14.

> restart: int(1/x,x) assuming x<0;
ln(x)

> restart: int(1/x,x) assuming x::real;
ln(x)
> restart: with(RealDomain): int(1/x,x); ln(x)

For definite integration, it is different.

> int(1/x,x=a..b) assuming a<0;
-ln(-a) + ln(-b)

This problem has been noted before, I think.

You can use op or PiecewiseTools:-ToList on that piecewise object, to get at its contents. 

For a point of equality you could just use eval(pw,q[S]=z). For your inequality case, how about just

simplify(pw) assuming q[S]>0.13;

where pw is your piecewise?

Could you try using Maple's solve command, with inequality areguments? See ?solve,ineq

You could try something like,

  solve( { tderiv>0, x>a, x<b, y>c, y<d } ); 

I'm not sure whether you also have to call that under assuming or after assume, to get the best effect. Maybe you could post your example?

An alternative would be to try numerical optimization.

Joe's already shown you how to solve it.

But you can also look at what happens at x=0, after solving.

> f := ((diff(y(x), x))*(diff(y(x), x, x))/sqrt(y(x)^2+diff(y(x), x))+diff(y(x), x, x)) = x^6:

> integ := dsolve({f, y(0) = 2, D(y)(0) = 1.5}, numeric, range = 0 .. 10, output=listprocedure):

> Y:=eval(y(x),integ):
> DY:=eval(diff(y(x),x),integ):
> Z:=isolate(f,diff(y(x),x,x)):
> DDY:=eval(rhs(Z),integ):

> #plot([Y,DY,DDY],-1.5..1.5,legend=['Y','DY','DDY']);
> Y(0),DY(0),DDY(0);
2., 1.500000000000, 0.0

You might want to note the discrepency with your initial post which had (D(D(y)))(0) = 1

Judging by your second response, you might not have to deal with deeper structures. But if you do, then this might help with lists of list of (mixes of) lists and scalars, etc.

> g := LL -> [subsindets(LL,list,t->op(t))]:

> > L := [[1,2],[3],[4,5,6,[[[7,8,[9]]],10]]];
         [[1, 2], [3], [4, 5, 6, [[[7, 8, [9]]], 10]]]

> map(op,L);
            [1, 2, 3, 4, 5, 6, [[[7, 8, [9]]], 10]]

> g(L);
                [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

> L := [[1,2],3,[[7,8]]];
                     [[1, 2], 3, [[7, 8]]]

> map(op,L);
                       [1, 2, 3, [7, 8]]

> g(L);
                        [1, 2, 3, 7, 8]

Is there an example which would need something that digs even deeper, like this??

> f:=proc(L::list)
> local LL;
>   LL:=L:
>   while nops(indets(LL,list)) > 1 do
>     LL:=[subsindets(LL,list,t->op(t))];
>   end do;
>   LL;
> end proc:
First 30 31 32 33 34 35 36 Last Page 32 of 48