acer

32333 Reputation

29 Badges

19 years, 319 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

The syntax which you have used for `eq` has several mistakes.

Did you mean,

eq := x*(0.0178)*tan(sqrt(x)*2e-5)^2 = 2.32e12-x;

instead?

acer

Is this what you're after?

plot(exp((1/3)*x)*sin(2*x), x=-2..2);

plot(exp((1/3)*x)*sin(2*x), x=-2..2)

acer

Yes.

You could replace calls like RowDimension(A) with LinearAlgebra:-RowDimension(A) throughout the procedure.

Or, you could replace calls like RowDimension(A) with op([1,1],A) and calls like LinearAlgebra:-ColumnDimension(B) with op([1,2],B).

By the way, you could also add new local variables rowdimA and coldimB to the procedure. Then assign them their values at the start of the procedure, calling RowDimension and ColumnDimension just once each. The way you have it right now those both get called more than once, which is unnecessary use of resources.

Also, you don't need to explicitly set all of the the C[i,j] to zero inside the the double loop. When you previously create C with the Matrix() constructor then all the entries get preassigned and stored as zero by default.

acer

In the way you tried to create the X[k] in a loop, the operators were not formed using each current value of `k`.

Try it this way, instead,

restart:

for k from 1 to 10 do
  X[k]:=Statistics:-RandomVariable(ChiSquare(k));
  f[k]:=unapply(Statistics:-PDF(X[k],x),x);
end do:

f[1](x);

                      /           (1/2)    /  1  \\
                      |          2      exp|- - x||
                      |                    \  2  /|
             piecewise|x < 0, 0, -----------------|
                      |              (1/2)  (1/2) |
                      \          2 Pi      x      /

f[2](x);

                        /          1    /  1  \\
               piecewise|x < 0, 0, - exp|- - x||
                        \          2    \  2  //

A simpler example of the issue,

restart:
k:=2:
f:=x->k*x;
                           x -> k x
k:=11:
f(x);
                              11 x

restart:
k:=2:
f:=unapply(k*x,x);
                           x -> 2 x
k:=11:
f(x);
                              2 x

restart:
k:=2:
f:=subs(U=k, x->U*x);
                           x -> 2 x
k:=11:
f(x);
                              2 x

acer

If you are wanting this for 2D Math input, then you can used the Layout palette.

Look for the middle entry in the second row, which looks like a magenta b over a green A. If you hover your mouse over it then its balloon help is "over".

Here's how the palette entry works, you click on the entry, while in 2D Math mode. That should insert the magenta-green thing, with the A highlighted. Type the letter x, say, which should overwrote the A. Then hit the Tab key, which should move the highlighting cursor up to the b. Type just the . (period) key, which should overwrite the b. Then hit return, or advance the cursor using the right-arrow key.

If your Maple typesetting level is set to "extended" then the derivative as 2D Math output will also display as that x-dot. If your typesetting level is set to "standard" then the output will display just like diff(x(t),t) .

You can change the typesetting level (extended or standard) using either the interface command or the GUI's top menubar as Tools->Options->Display->Typesetting level.

By default, the dot notation applies to the derivative with respect to `t` (meaning: time). You can also change that aspect, using the Typesetting:-RuleAssistant command.

acer

The output below for `P` is how it prettyprints as 2D Math.

> T:=convert(sin(x),Sum,dummy=k):

> Q:=subsindets(T,specfunc(anything,factorial),z->factorial(``(op(z)))):

> P:=value(eval(Q,infinity=5)):
> P;

                                x/factorial(``(1))-x^3/factorial(``(3))+x^5/factorial(``(5))-x^7/factorial(``(7))+x^9/factorial(``(9))-x^11/factorial(``(11))

expand(P);

         1  3    1   5    1    7     1     9      1      11
     x - - x  + --- x  - ---- x  + ------ x  - -------- x  
         6      120      5040      362880      39916800    

acer

Here is something you might experiment with, simply using indexing,

M := Matrix([[1,2],[-2,3],[3,18],[-5,6]]);

                                     [ 1   2]
                                     [      ]
                                     [-2   3]
                                M := [      ]
                                     [ 3  18]
                                     [      ]
                                     [-5   6]

m,n := op(1,M);

                                m, n := 4, 2

rowlist:=[seq(`if`(not M[i,1]<0, i, NULL), i=1..m)];

                              rowlist := [1, 3]

M[rowlist,1..n];

                                   [1   2]
                                   [     ]
                                   [3  18]

acer

This below is a simple minded approach. It's likely slower than necessary for very some long lists that in principle could be failed early, since it builds all the conditions before checking any of them.

palin:=proc(T::list)
  evalb(`and`(seq(T[i]=T[nops(T)-i+1],i=1..nops(T)/2)));
end proc:

palin( [a,b,c,d,c,b,a] ); # odd number of entries

                              true

palin( [a,b,c,c,b,a] ); # even number of entries

                              true

palin( [a,b,c,d,b,a] );

                             false

palin( [17,[a,b],0.3,0.3,[a,b],17] );

                              true

palin( [P] );

                              true

palin( [] ); # add special case for this, if undesirable

                              true

acer

Here's something that you could try out. The basic idea is that the assignment is not distributed through the translated object. The hope is that the Maple `piecewise` could be used in usual ways (here, assignment).

Sorry if there are any syntax errors to fix, I am not in front of a C compiler this minute.

I reversed the conditions in your example, just to make it perhaps a little more interesting.

restart:

with(CodeGeneration):
with(LanguageDefinition):
    
    LanguageDefinition:-Define("NewC", extend="C",
        AddFunction("piecewise", anything::numeric,
            proc()
                local i;
                Printer:-Print("( (",_passed[1],") ? ",_passed[2]);
                for i from 3 to _npassed-2 by 2 do
                    Printer:-Print(" : (",_passed[i],") ? ",_passed[i+1]);
                end do;
                Printer:-Print(" : ",_passed[_npassed]," ) ");
            end proc,
        numeric=double)
    );

myp:=proc(x::numeric) local result::numeric;
        result := piecewise(x>3,3*x,x>2,2*x,x>1,1*x,0);
    end proc:

Translate(myp, language="NewC");

double myp (double x)
{
  double result;
  result = ( (0.3e1 < x) ? 0.3e1 * x : (0.2e1 < x) ? 0.2e1 * x : (0.1e1 < x) ? x : 0 ) ;
  return(result);
}

acer

Try the Maple 15 User Manual which is available from here.

acer

You could change settings so that by default your get the 1D instead of the 2D Math input mode while working in the Standard GUI. See here. That would make "whole equation stays inline," like in the Classic GUI.

You can also set another option so that by default you get Worksheets instead of Documents in the Standard GUI. (ie. from the top menubar, Tools->Options->Interface tabm then "Default format for new worksheets". That makes it behave even more like Classic, for entering input.

acer

You can create a procedure that generates the EOM for any value of elenum.

restart:
makeEOM:=proc(elenum)
local i, node, nodes, L, E0, E0M;
   nodes:=elenum*2+1:
   L:=evalf((Pi*2)/(elenum*2)):
   E0:=Matrix([[L/3,2*L/3,L/3],[2*L/3,11*L/15,2*L/3],[L/11,2*L/3,L/110]]):
   E0M:=Matrix(nodes);
   for i from 1 to elenum do
      node:=(2*(i-1))+1;
      E0M[node..node+2,node..node+2]:=E0M[node..node+2,node..node+2]+E0;
   end do:
   E0M:
end proc:

Now you can create then at will,

interface(rtablesize=infinity):
makeEOM(6);

And if you want to you can create them for all the values in that Array, and store them.

V := Array([2,4,6,8,16]):
for en in V do
  eom[en]:=makeEOM(en); # store them for later use
end do:

eom[4];

And you can also access them by position of the value in V,

V[1];

eom[V[1]];

Procedures can improve the flow of your code.

acer

If this is a floating-point problem, and you want a double-precision computation, then you are in luck. Putting shape=band[1,1] and datatype=float[8] on your Matrix and your RHS (Vector, or Matrix) will greatly improve the performance by virtue of using a dedicated solver from CLAPACK for band shape & storage. (There also exist dedicated solvers for tridiagonal Matrices -- which usually means specifically the main, 1st super-, and 1st sub-diagonal in the numerical linear algebra discipline -- but the band solver can do relatively well here too, as illustrated below.)

First, let's look at the case where the datatype=float[8] Matrix A has full rectangular storage.

> restart:
> N:=5000:
> with(LinearAlgebra):                                     

> Arect:=RandomMatrix(N,outputoptions=[datatype=float[8]]):

> kernelopts(bytesalloc); # total memory allocated so far  
                                   201813920

> B:=RandomVector(N,outputoptions=[datatype=float[8]]):

> X := CodeTools:-Usage( LinearSolve(Arect,B) ):
memory used=191.68MiB, alloc change=191.34MiB, cpu time=12.62s, real time=12.67s

> kernelopts(bytesalloc); # total memory allocated so far  
                                   402448408

> Norm(Arect.X-B);    
                                                  -8
                           0.971647295955335721 10

Nor let's compare, but this time Matrix A will also have shape=band[1,1]. Notice that the total memory allocation to store A and to get the computation done is much less, the computation is much faster, and the result has better forward error upon resubstitution.

> restart:                                                 
> N:=5000:                                                 
> with(LinearAlgebra):                                     

> A:=RandomMatrix(N,outputoptions=[shape=band[1,1],datatype=float[8]]):
> kernelopts(bytesalloc); # total memory allocated so far              
                                    2096768

> B:=RandomVector(N,outputoptions=[datatype=float[8]]):                

> X := CodeTools:-Usage( LinearSolve(A,B) ):                           
memory used=1.08MiB, alloc change=0.87MiB, cpu time=11.00ms, real time=32.00ms

> kernelopts(bytesalloc); # total memory allocated so far
                                    3145152

> Norm(A.X-B);                                                         
                                                 -10
                          0.436557456851005554 10

In essence, the computational complexity of the problem has been reduced by a power of N, affecting both memory storage and computation time. That means that the relative benefit of using the specialized shape & storage will increase (ie. get even better, in a relative sense) as the size of the problem increases.

Now, you will be constructing your Matrix A using some process other than calling RandomMatrix, of course. If your code creates A up front, and then populates it in some manner, then make sure that the creating has those options. Eg,

A := Matrix(m,m,datatype=float[8],shape=band[1,1]);

Let us know, if you have problems implementing it is this way.

acer

See the help-page for `is` and related property checking,

is( 5/sqrt(x+4) > 0 ) assuming x>1;

                                       true

acer

restart:

for k to 5 do
   f[k] := unapply(piecewise(x > 0, k, 0),x);
end do:

f[3](1);
f[3](-1);
f[2](1);
f[2](-1);

restart:

f:=proc(x)
      if type(procname,indexed) then
         piecewise(x>0,op(1,procname),0);
      end if:
end proc:

f[3](1);
f[3](-1);
f[2](1);
f[2](-1);

restart:

f:=proc(x,k)
      piecewise(x>0,k,0);
end proc:

f(1, 3);
f(-1, 3);
f(1, 2);
f(-1, 2);

acer

First 266 267 268 269 270 271 272 Last Page 268 of 336