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

Ok, so you can stuff all that data into a float[8] rtable (eg. Array), and plot that directly. See quickplot from R.Israel's Maple Advisor Database for a nice user-friendly routine for doing that. Here's an example of the kind of thing it does under the hood to make it more efficient for large numbers of points,

PLOT(POINTS(Array(1..3,1..2,
                  [[-0.5,1],[0.25,0],[0.5,-0.5]],
                  datatype=float[8])));

But a plot of a million points will likely still render too slowly and use a lot of memory. So instead of making a plot perhaps you should be thinking about making an image file directly, using the ImageTools package. In particular, ImageTools:-Create might help.

ps. There may also be an issue with your code. If you post it or upload it here, someone might be able to offer useful suggestions.

acer

Just use fclose on the file.

acer

> M:=ImportMatrix("foo.txt",source=Matlab):

> for i from 1 to 3 do
>   p[i],n[i] := M[i][1..3]^%T, M[i][4..-1]^%T;
> od:

> p[1],n[1],p[2],n[2],p[3],n[3];
                        [1]  [4]  [2]  [ 8]  [3]  [12]
                        [ ]  [ ]  [ ]  [  ]  [ ]  [  ]
                        [2], [5], [4], [10], [6], [15]
                        [ ]  [ ]  [ ]  [  ]  [ ]  [  ]
                        [3]  [6]  [6]  [12]  [9]  [18]

acer

> L1/2+L2;
                                                              -1
              [1, -1/2, 1/3, -1/4, 1/5, -1/6, 1/7, -1/8, 1/9, --]
                                                              10

> L1/2+L2/2;
                              -3                    -3        -3
             [3/4, -3/8, 1/4, --, 3/20, -1/8, 3/28, --, 1/12, --]
                              16                    32        40

...and so on.

If that's not what you want, then I cannot see what your goal is. What do you want to add together, to get the first element of the final result? Is it L1[1]/2 |+ L2[1]/2, or some mix of indices used for each list, or...?

acer

> subs((x-y)^(-1/2)=1/g,f2);
                                    a/g + b

acer

> xy:=Matrix([[10,10],[-14,-2],[-10,-10],[4,-24],[10,10]]):

> x:=xy[1..-1,1]:
> y:=xy[1..-1,2]:

> n := LinearAlgebra[RowDimension](xy):

> A := (1/2)*(add(x[i]*y[i+1]-y[i]*x[i+1], i = 1 .. n-1));
                                   A := 400
 
> 1/2*( x[1..-2].y[2..-1] - y[1..-2].x[2..-1] );
                                      400

Sometimes the inadvertant use of `sum` instead of `add` can work successfully for doing summation (adding) of finitely many terms. But not when there are unspecified Vector, Matrix or Array entries in the summed expression such as x[i]. The x[i] term is cannot be evaluated and is not allowed, prior to i being specified by an actual integer. And `sum` attempts that evaluation, while `add` delays it.

The evaluation can also be delayed for this example using so-called uneval-quotes. (That doesn't make it easier. I just mention it for completeness.)

> A := (1/2)*(sum('x[i]'*'y[i+1]'-'y[i]'*'x[i+1]', i = 1 .. n-1));
                                   A := 400

Who knows... maybe that behaviour of accessing unknown rtable entries like y[i] will change some day.

This topic may also be a common source of mistakes by new users, if the palette entries (Greek Sigma) supply `sum`.

acer

with(CurveFitting):
with(plots):
data := [[2005,2.85],[2006,5.70],[2007,10.0],[2008,14.8],[2011,25.0]]:
curve:=LeastSquares(data,v);
P1:=pointplot(data):
P2:=plot(curve,v=2005..2011):
display(P1,P2);

acer

Try it with a full colon after end do, but wrap the pointplot3d call in print.

 acer

Try here or here.

You also simply enter the word distance into Maple's help browser.

acer

Have a look here.

This brings up an interesting point. The Online Help is for the current release (which is Maple 13 at the time I write this). It would be useful if each help-page included a note as to the release in which the page was introduced or became relevant. Even if that could not be done comprehensively for all existing pages, it might still be useful were it done for all pages added since, say, Maple 12.

acer

Replace printf below with a call to fprintf (with a file name as the new first argument). That will make it print to a file rather than to the Maple interface.

> listA:=[40,50,60]:
> listB:=[80,100]:
> values:=[[1,2],[4,5],[6,7]]:

> for i from 1 to nops(listA) do
>   for j from 1 to nops(listB) do
>      printf("name_%d_%d = %d\n", listA[i],listB[j],values[i,j]);
>   end do:
> end do:

name_40_80 = 1
name_40_100 = 2
name_50_80 = 4
name_50_100 = 5
name_60_80 = 6
name_60_100 = 7

If values[i,j] are floating-point then you might use %g or %e or %f rather than %d. See the fprintf help-page for descriptions of the various formats.

acer

Presumably you are supposed to implement the elementwise parts of that task, otherwise you could write the absurdly trivial procedure,

inv2x2 := (A::Matrix(2,2)) -> A^(-1):

One way to get the formula for this is to find the inverse of a "general" 2x2 Matrix. By "general" I mean that every element has its own unique and mathematically unrelated name.

> M := Matrix(2,2,symbol=m);
                                [m[1, 1]    m[1, 2]]
                           M := [                  ]
                                [m[2, 1]    m[2, 2]]
 

So just issue the command M^(-1) and implement inside your precdure the formulae you see for each entry of the result .

Look for a common expression as the denominator of each entry, and have your procedure compute that first. Ask youself, what should your procedure do if that denominator is zero?

This may not always be the best way to go about such problems, but in this case it works easily.

acer

> Eq := cT*a*b*sT+cT*c+sT*d+cT*sT;
                     Eq := cT a b sT + cT c + sT d + cT sT
 
> algsubs(cT*sT=s2T, Eq);
                          cT c + sT d + a b s2T + s2T

The help-page for subs shows a few algsubs examples, and has a link to the algsubs help-page in its See Also section.

acer

I hope that there is an easier way than this (presuming that I haven't done it all wrong). It'd be easy if one could simply subtract from the original sample values. But you mentioned that you wanted those states as the immediate output (from Sample).

> MyProbTable := proc(plist::list)
>   module()
>   export Conditions, ParentName, Parameters, CDF, Mean,
>          ProbabilityFunction, RandomSample, RandomVariate;
>   options Distribution, Discrete;
>   Conditions := [plist::('list(realcons)')];
>   ParentName := ':-ProbabilityTable';
>   Parameters := [plist];
>   CDF := t -> sum(plist['k'+1],('k') = 0 .. min(floor(t),nops(plist)-1));
>   ProbabilityFunction := t -> piecewise(t < 0,0,t+1 <= nops(plist),plist[floor(t)+1],0);
>   Mean := sum((i-1)*plist[i],i = 1 .. nops(plist));
>   RandomSample := proc(n::posint) local pr, oldopmod;
>     oldopmod := kernelopts(opaquemodules);
>     kernelopts(opaquemodules=false);
>     try
>       Statistics:-ExternalSupport:-Initialize();
>       pr := Statistics:-ExternalSupport:-DefineExternal("MapleAliasUrnSample");
>     catch:
>       error;
>     finally
>       kernelopts(opaquemodules=oldopmod);
>     end try;
>     pr(n,Array([op(plist)],('datatype') = float[8]));
>   end proc;
>   RandomVariate := proc() local pr, oldopmod;
>     oldopmod := kernelopts(opaquemodules);
>     kernelopts(opaquemodules=false);
>     try
>       Statistics:-ExternalSupport:-Initialize();
>       pr := Statistics:-ExternalSupport:-DefineExternal("MapleAliasUrnSample");
>     catch:
>       error;
>     finally
>       kernelopts(opaquemodules=oldopmod);
>     end try;
>     pr(1,Array([op(plist)],('datatype') = float[8]));
>   end proc;
> end module:
> end proc:

> with(Statistics):

> P := [1/2,1/8,3/8]:

> X := RandomVariable(MyProbTable(P)):

> Sample(X,20);
          [2, 2, 2, 0, 2, 2, 0, 2, 0, 0, 2, 1, 0, 0, 1, 0, 2, 0, 2, 0]
 
> Mean(X), evalf(Mean(X)), evalf(Mean(Sample(X,1000000)));
                        7/8, 0.8750000000, 0.8748580000

> CDF(X,-1), CDF(X,0), CDF(X,1), CDF(X,2), CDF(X,3);
                               0, 1/2, 5/8, 1, 1

This trick doesn't extend to the situation where your values instead are, say, -3, -2, and -1. I'd be tempted to suspect that there must be an easier way, except that Statistics is not so strong for customized discrete distributions.

acer

You may have typo'd in the fourth list, which you gave as [4,5,5,7]. If so, then it doesn't affect the method below. If you didn't, then it's not clear whether you wanted to transpose the data (have a look at Matrix(L)).

> L := [[1,2,3,4],[2,3,5,5],[3,4,4,6],[4,5,5,7]]:
 
> sort(L,(a,b)->a[3]<b[3]);
           [[1, 2, 3, 4], [3, 4, 4, 6], [4, 5, 5, 7], [2, 3, 5, 5]]
> sort(L,(a,b)->a[4]<b[4]);
           [[1, 2, 3, 4], [2, 3, 5, 5], [3, 4, 4, 6], [4, 5, 5, 7]]
 
> S:=(l,n)->sort(l,(a,b)->a[n]<b[n]):

> S(L,3);
           [[1, 2, 3, 4], [3, 4, 4, 6], [4, 5, 5, 7], [2, 3, 5, 5]]
 
> S(L,4);
           [[1, 2, 3, 4], [2, 3, 5, 5], [3, 4, 4, 6], [4, 5, 5, 7]]

> L := [[1,2,3,4],[2,3,5,5],[3,4,4,6],[4,5,6,7]]:
> S(L,3);
           [[1, 2, 3, 4], [3, 4, 4, 6], [2, 3, 5, 5], [4, 5, 6, 7]]
 
> S(L,4);
           [[1, 2, 3, 4], [2, 3, 5, 5], [3, 4, 4, 6], [4, 5, 6, 7]]

acer

First 300 301 302 303 304 305 306 Last Page 302 of 337