pagan

5147 Reputation

23 Badges

17 years, 126 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

For Int and Diff, you can use command-completion to get the inert form as 2D Math input. Starting in 2D Math entry mode, simply type out the name of such a command, and then hit the command-completion keystrokes.

This is the nicest way I know to get 2D Math inert Int and friends, since the palette entries are mostly hooked up to non-inert operators (or to the inert ones, but not properly!).

Is that adequate, or do you need to render inert more complicated expressions which they display as 2D Math?

You can store a sequence of 2D Matrices in the entries of a 1D Vector or Array, as layers.

Or you can store data in a 3D Array. See the help page for Array.

Or you can store a collection of 1D Vectors or Arrays in the entries of a 2D Matrix

Each of those structures will have different (but similar) access syntax using [] to get at the entries for access or assignment. The middle choice would require just one pair of brackets, while the others would need two each.

Only the 3D Array choice, with a hardware datatype, would have thedata stored in a single contiguous section of memory (for most efficient access/update or Compiler use).

A 3 dimensional Array is called... an Array. It's just a question of terminology. A 2D Array (only) is sometimes called a Matrix (a mathematical rather than data structure term, to represent a mapping from one vector space to another).

> unassign('i'):unassign('p'):

> N:=xi->N40(xi)*((kn[i+p+1]-xi)/(kn[i+p+1]-kn[i]+1));
      N40(xi) (kn[i + p + 1] - xi)
xi -> ----------------------------
       kn[i + p + 1] - kn[i] + 1  

> N31:=subs([i=3,p=1],eval(N));
      N40(xi) (kn[5] - xi)
xi -> --------------------
       kn[5] - kn[3] + 1  

> N41:=subs([i=4,p=1],eval(N));
      N40(xi) (kn[6] - xi)
xi -> --------------------
       kn[6] - kn[4] + 1  

> N31(3.2);
                           N40(3.2) (kn[5] - 3.2)
                           ----------------------
                             kn[5] - kn[3] + 1   

> i,p := 177,-20:

> N31(3.2);
                           N40(3.2) (kn[5] - 3.2)
                           ----------------------
                             kn[5] - kn[3] + 1   
> restart:

> f:=X0*exp(-r*t); # general form
                               f := X0 exp(-r t)
 
> eval(f,t=0); # notice that X0 is initial (t=0) sample size
                                      X0
 
> solve(eval(f,t=600)=0.62*X0,r); # find r, using given data
                                0.0007967263349
 
> f:=eval(f,r=%); # redefine f, using found r
                        f := X0 exp(-0.0007967263349 t)
 
> solve(f=0.5*X0,t); # find t at which f equals 1/2 of X0
                                  869.9940622
 
> solve(f=(48.0/100.0)*X0,t); # find t at which f equals 48/100 of X0
                                  921.2312220

Notice that it's not very educational, to be able to do the above without having to figure out how to do the following by hand.

> restart:

> f:=X0*exp(-r*t); # general form
                               f := X0 exp(-r t)
 
> solve(f=H,r);
                                        H
                                    ln(----)
                                        X0
                                  - --------
                                       t
 
> solve(f=H,t);
                                        H
                                    ln(----)
                                        X0
                                  - --------
                                       r

This is the closest I could get. All the MySI setup stuff is so that N*m does not automatically turn into joules, etc, as it's supposed to in the SI system. The ``() is to prevent S from multiplying out.

with(Units:-Standard):
Units:-AddSystem(MySI,Units:-GetSystem(SI),N*m,kN*m,N/m):
Units:-UseSystem(MySI):

q:=5*Unit(kN/m);

L:=10*Unit(m);

S:=``(q)*``(L)^2/8;

evalf(combine(expand(S),units));

You could use testfloat (or verify with float option), or apply fnormal first.

I prefer verify,float usually, for floating-point scalar number comparison.

After cranking infolevel[GlobalOptimization] up to 6, the printed userinfo messages appear to show that, for your example, the very best result is found during some inital "local search phase". But then it goes on to do a global search phase and returns whatever that finds -- even if it is not as good as the local search phase results.

Try passing method=reducedgradient to GlobalSolve, which is supposed to bypass the subsequent global search. If it works for you, then you could have your main procedure issue two calls to GlobalSolve, the first (quick one) with reducedgradient and the second with multistart. Then code it to take the best of those two results. 

oldecho:=interface(echo):
interface(echo=0):

appendto("./out.txt"):
printf("\n The total elapsed time has been of %d hour(s)  %d minute(s) and %a seconds \n",hou, mini,seci):

writeto(terminal):
interface(echo=oldecho):

There are various ways, some less efficient than others.

Sometimes, figuring out why one is better than another can help learn Maple.

> G:=rand(1..6):
> found:=0:
> All:=Vector[row](6):
> while found<6 do
>   new:=G();
>   if All[new]=new then
>     printf("discarding another %d\n",new);
>   else
>     All[new]:=new;
>     found:=found+1;
>     printf("rolled a %d\n",new);
>   end if;
> end do:
rolled a 5
rolled a 2
discarding another 5
rolled a 6
discarding another 2
rolled a 3
rolled a 4
discarding another 4
discarding another 6
discarding another 5
discarding another 3
rolled a 1
> All;
                              [1, 2, 3, 4, 5, 6]

Another way might be this.

> restart:
> G:=rand(1..6):
> T:={}:
> while T<>{1,2,3,4,5,6} do
>   new:=G();
>   if member(new,T) then
>     printf("discarding another %d\n",new);
>   else
>     T := T union {new};
>     printf("%a\n",T);
>   end if;
> end do:
{5}
{2, 5}
discarding another 5
{2, 5, 6}
discarding another 2
{2, 3, 5, 6}
{2, 3, 4, 5, 6}
discarding another 4
discarding another 6
discarding another 5
discarding another 3
{1, 2, 3, 4, 5, 6}

So, here are some questions. How many times does the first method create a new Vector? How many times does the second method create a new set? Is the second method still worse if you replace its expensive while-condition above with nops(T)<>6 ?

You could time these.

restart:
st:=time():
G:=rand(1..6666):
T:={}:
while nops(T)<>6666 do
  new:=G();
  if member(new,T) then
  else
    T := T union {new};
  end if;
end do:
time()-st;

restart:
st:=time():
G:=rand(1..6666):
found:=0:
All:=Vector[row](6666):
while found<6666 do
  new:=G();
  if All[new]=new then
  else
    All[new]:=new;
    found:=found+1;
  end if:
end do:
time()-st;

The first of those needs 95MB to run, and the second only needs 5MB.

> G:=RandomTools:-Generate(distribution(Normal(0,1)),makeproc=true):

> Matrix(2,G+G*I,shape=antihermitian);
             [0 , -0.500486574831822351 - 0.491050898933502511 I]
             [                                                  ]
             [0.500486574831822351 - 0.491050898933502511 I ,  0]

It is the semicolon after the od (or end do) that is controlling it.

See the ?for help page.

In particular, see the subsection "Note about Nested Loops", near the bottom of the Description section.

Did you forget to assign each result to a, before adding a to Total?

...or perhaps just put the whole thing inside yet another `add` call.

Also, for such addition of finitely many terms, you might use `add` instead of `sum`.

> z1:=-I:

> f := z -> (-1./z)*ln(1.-z):
> f1:= (f(z1)-f(z))/(f(z)*(z-z1)):

> limit(f1, z = z1);
                               Float(undefined)

> limit(diff(numer(f1),z)/diff(denom(f1),z),z=z1);
                        -0.2977247557 + 0.2320028670 I

> f := z -> (-1/z)*ln(1-z): # no floats!
> f1:= (f(z1)-f(z))/(f(z)*(z-z1)):

> limit(f1, z = z1);
                           -Pi + 2 I ln(2) + 2 - 2 I
                           -------------------------
                                2 ln(2) + Pi I

> evalf(%);
                        -0.2977247558 + 0.2320028674 I

It makes some sense and is useful, if qq2 is also an equation.

> int1 := f(x) = (2*x^2+4)*sin(x):

> map(int, int1, x)+(0 = c);
                    /
                   |                       2
                   |  f(x) dx = -2 cos(x) x  + 4 x sin(x) + c
                   |
                  /

It works because Maple can "add" equations.

> (b=a) + (d=c);
                                 b + d = a + c

> (b=a) + (0=c);
                                   b = a + c

Consider question 2). As Robert suggested, first create the empty Array with the correct dimensions, outside of any for..do..end loops.

Look at the first row. Notice that the entries are powers of 3. Now look down any column, and notice how the entries are all multiples of the first entry in that column.

Find a simple formula in terms of i and j which would give all  the values of that 4x4 data, if you plugged in i anything from 1..4 and j anything from 1..4.

Use that formula when assigning to the Array entry, inside the inner loop.

Now for question 1) you could do the same sort of thing, but with a formula involving only i (and not j)  if the data was actually supposed to be [0,5,10,15,20].  Just curious, are you sure that you gave that Q 1) correctly?

First 36 37 38 39 40 41 42 Last Page 38 of 48