## 29 Badges

14 years, 315 days

## assuming...

Not all of Maple takes advantage of all assumptions.

If there isn't an example on a routine's help-page of it working with assumptions, then that's a hint that the routine doesn't respect assumptions.

In the case of `solve`, the help-pages describe `solve`'s ability to deal with assumptions on parameters but not on the main variables (unknowns for which to solve).

There was a time when almost nothing of interest in Maple took advantage of assumptions. This has been changing, slowly. Making some routines utilize assumptions would be very difficult, and could conceivably require a near-full rewrite for the routine in question.

ps. were you expecting a null return for your example, under the assumptions?

acer

## more info...

Please tell us what `Ranges` and `Img` are.

I am imagining that an efficient in-place solution using the container=V option may be possible. But it'd help to see the structure of Ranges and Img.

acer

## sample...

fff := proc(dist, s_one, d, n)
option hfloat;
#
# parameters:
#   sample procedure
#   Starting price stock
#   Trailing distance#
#   Number of periods
#
# returns:
#   Profit/loss with stop loss,
#   Profit/loss without stop loss
#
local rand_, i, A, C, w, w_w_, w_w, C_C,
Exit_period, Exit_price, Last_close;

rand_:=dist(n):

# Stock price
A[1]:=s_one: # Starting price stock
for i from 2 to n do
A[i]:=A[i-1]+rand_[i];
end do:

# Trailing Stop-Loss
C[1]:=(A[1]-d):
for i from 2 to n do
if A[i]>A[i-1] and A[i]>(A[i]-d)
and (A[i]-d)>C[i-1] then
C[i]:=(A[i]-d);
else
C[i]:=C[i-1];
end if;
end do:

w[1]:=0:
for i from 2 to n do
if A[i]>C[i] then
w[i]:=0;
else
w[i]:=1;
end if;
end do;

w_w_[1]:=0:
for i from 2 to n do
w_w_[i]:=(w[i]+w_w_[i-1]);
end do;

w_w[1]:=0:
for i from 2 to n do
w_w[i]:=(w_w_[i]+w_w_[i-1]);
end do;

# Final Corrections
C_C[1]:=(A[1]-d):
for i from 2 to n do
if w_w[i]=0 then
C_C[i]:=C[i];
else
C_C[i]:=C_C[i-1];
end if;
end do:

############## Results #########
# Exit period due to stop loss
Exit_period:=add(`if`(w_w[i]=1,i,0),i=1..n);

# Exit price stop-loss
Exit_price:=`if`(Exit_period=0,0,C_C[n]);

# Last close stock price
Last_close:=`if`(A[n]<0,0,A[n]);

# Profit/loss with stop loss,
# Profit/loss without stop loss
`if`(Exit_price=0,Last_close-A[1],Exit_price-A[1]),
Last_close-A[1];

end proc:

randomize():
X := Statistics:-Sample(Statistics:-RandomVariable(Normal(0,1))):

fff( X, 100, 4, 100 );

seq( fff( X, 100, 4, 100 ), i=1..10 );

time( seq( fff( X, 100, 4, 100 ), i=1..1000 ) );

If you need this to be really fast, then the local tables (A, C_C, etc) could be created as float[8] Vectors outside of fff and then get passed in and re-used.

acer

## procedure...

How about putting the whole thing in a procedure called f (which might even take no arguments at all. Have it return that final result. Then call f() repeatedly, or call seq(f(),i=1..10).

acer

## hmm...

I thought that _S01 (and other names similarly prefixed by _S) are what `solve`  uses to manage substitutions internally. That is to say, that they are temporary placeholders that `solve` uses while it does its work. If that's true, then it could well be a bug if they appear in the return value.

Could you post or upload an example of input, and say what Maple version you're using?

acer

## help-page...

Have you read the Maple help-page for limit and limit,return ?

acer

## attempt...

I've tried to lay out a method, and prototype code, for doing this here.

acer

## colon-equals...

It's not clear what you're asking. It appears to be a question about how to write an assignment. Here's an example,

x := 2*sin(y);

The colon-equals is what does the assignment of the expression 2*sin(y) to name (variable) x. The semicolon at the end is a statement terminator, and completes the command. If I had used a colon at the end then it would still do the assignment but the printing of the output would be suppressed.

Are you also wondering about the rules (eg. arithmetic operators, etc) that can be used to form expressions?

acer

## least squares...

The following should find the radius which minimizes the sum of the distances from the points to the circle.

p1:=[-10,2]: p2:=[-8,5]: p3:=[-1,-12]: p4:=[4,6]: p5:=[10,-1]:
L := [p1,p2,p3,p4,p5]:
CX:=-1: CY:=-4: # the given center
ceq:=(x-CX)^2+(y-CY)^2=r^2:
Optimization:-Minimize(
add(abs((L[i,1]-CX)^2+(L[i,2]-CY)^2-r^2),i=1..nops(L)),
r=0..15);
R:=rhs(op(%[2]));
PP:=plots:-pointplot([p1,p2,p3,p4,p5]):
PC:=plots:-implicitplot( subs(r=R,ceq), x=-20..20, y=-20..20):
plots:-display([PP,PC]);

acer

## parametric form...

Your idea about how to go about showing it is good.

Write the equation of the line in parametric form, using say the parameter name t.

Now, substitute the formulae(in terms of t) for x, y, and z of that parametric form in for the x, y, and z that appear in the equation for the sphere. That results in a quadratic equation in the parameter t. Now solve that quadratic equation, to get two solutions for t. Plug each of those two values of t into the parametric form of the equation of the line, and those results are the two intersecting points.

Here's how to do those steps in Maple. First, create the points, line, and sphere.

with(geom3d):
point(a,-3,-10,2),point(b,5,14,8):
line(L,[a,b]):
_EnvXName := x: _EnvYName := y: _EnvZName := z:
sphere(s,x^2+y^2+z^2 = 36,[x,y,z]):

Maple can now give the answer right away,

intersection(P,s,L):
coordinates(P[1]);coordinates(P[2]);

Or you can do each step in Maple,

eL:=Equation(L,'t'); # equation of line
es:=Equation(s);

# substitute components of line form into sphere's equation
teq:=expand(eval(es,[x=eL[1],y=eL[2],z=eL[3]]));

# solve the quadratic equation
tsols:=solve(teq,t);

# plug both t values into the line (parametric form).
seq(eval(eL,t=tval),tval in [tsols]);

acer

## file...

What is the output from the shell command,

file try_it.so

What is your machine, a G5? And the OSX, is it 10.5? (I'm wondering whether there's a 32/64bit mismatch between the shared library and the Maple binary. I'm wondering whether the shared object needs to be compiled/linked with a -arch flag to force its architecture to match Maple's. Just an idea...)

acer

## _EnvTry...

There are two factors at play here, in the multivariate Newton's method solver. One is the number of iterations that get done, for a given starting point. Another is the number of different starting points from which iteration gets done.

Try setting,

> _EnvTry:='hard':

This line in `fsolve/sysnewton` hints that the number of allowed iterations gets affected by this.

> showstat(`fsolve/sysnewton`,41);

41     for it to `if`(_EnvTry = ('hard'),max(Digits-7,50)*n+60,(Digits-7)*n+16) do
end proc

As you can see, the value of Digits also affects that maximal number of allowed iterations. You had said that increasing Digits (and hence increasing the number of iterations allowed) didn't help much. So this may not suffice.

The number of different starting points from which it begins iterating isn't an option that can be set, however.

The very brave might try to surgically alter the routine, to change the maximum number of starting points,

> showstat(`fsolve/sysnewton`,20);

20   maxguess := 20;

In this post Jacques did live surgery on another routine.

I would advise not tightening the bounds too much. I realize that it seems intuitive that doing so would assist the solver. But there may be bounds check going on, which (inappropriately) short-circuits a guess/iteration-loop if an intermediate value steps out of bounds. So with very tight bounds such early cut-out can actually prevent a root from being located.

acer

## evaluation rules...

When you pass S(n) as an argument to plot, it is evaluated right away. Ie, it evaluates with the unassigned n, just as if you'd entered S(n) on its own.

S(n);
Error, (in F) final value in for loop must be numeric or character

Try plot('S'(n),n=1..10) or plot(S,1..10) instead.

ps. There are ways to speed up the sampling, if you're interested...

acer

## affine mapping...

I won't go into efficiency. But, given what you have so far,

> with(Statistics):
> S:=Sample(Bernoulli(0.5),2):
> S := 2*(S-<0.5,0.5>);
S := [1., -1.]
Also, what's k? Is it supposed to be n?

acer

## pos is declared local, but unassigned...

You have declared pos as a local variable of procedure treach. And treach doesn't assign to the pos variable. So when treach runs it cannot figure out the conditionals involving the pos[n] term.

acer

 First 195 196 197 198 199 200 201 Last Page 197 of 222
﻿