tomleslie

13876 Reputation

20 Badges

15 years, 175 days

MaplePrimes Activity


These are answers submitted by tomleslie

there are several ways to do this.

If you change the filepath/name in the attached to something appropriate for your installation, then the following will pretty much always work

restart;
mat:= NULL:
while true do
   line := readline("C:/Users/TomLeslie/Desktop/test.txt"):
   if   line= 0
   then break
   else mat:= mat, parse~(StringTools:-Split(line, " ")):
   fi;
end do:
M:=Matrix( [mat]);

Matrix(2, 2, {(1, 1) = a, (1, 2) = b, (2, 1) = b, (2, 2) = a})

(1)

 


 

Download readToMat.mw

shown in the attached? (There are other possibilities)

  restart;
#
# Load the integral transforms package
#
  with(inttrans):
#
# Define some a time-domain function
# for 'test' purposes
#
  f:= t->t*exp(-3*t)*Heaviside(t):
#
# compute the Fourier sine transform
# and plot it
#
  plot( fouriersin(f(t), t,s),
        s=0..3
      );
 

 

#######################################
# Or restart and do everything in one
# step
#
  restart;
  plot( inttrans:-fouriersin
                  ( t*exp(-3*t)*Heaviside(t),
                    t,
                    s
                  ),
        s=0..3
      );

 

#########################################
# Or restart, define a couple of test
# functions, and a function which accepts
# a function name as argument, and returns
# its Fourier transform
#
  restart;

  f:= x-> x*exp(-3*x)*Heaviside(x):
  g:= x-> 3/(x + 2):

  FT:=h-> inttrans:-fouriersin( h(t), t, s):

  plot(FT(f), s=0..3);
  plot(FT(g), s=0..3);

 

 

 

Download FT.mw

Hmm- according to you original post

From versions to versions the Maple definition of this distribution switches these parameters.

So in which version(s) does the Maple definition actually 'switch' - cos I've just checked the following

from the Maple 18 help
Calling Sequence
Gamma(b, c)
GammaDistribution(b, c)
Parameters
b-scale parameter
c-shape parameter

from the Maple 2015 help
Calling Sequence
Gamma(b, c)
GammaDistribution(b, c)
Parameters
b-scale parameter
c-shape parameter

from the Maple 2016 help
Calling Sequence
Gamma(b, c)
GammaDistribution(b, c)
Parameters
b-scale parameter
c-shape parameter

from the Maple 2017 help
Calling Sequence
Gamma(b, c)
GammaDistribution(b, c)
Parameters
b-scale parameter
c-shape parameter

from the Maple 2018 help
Calling Sequence
Gamma(b, c)
GammaDistribution(b, c)
Parameters
b-scale parameter
c-shape parameter

So Maple has been entirely consistent across the last five releases. I cannot (easily) check any earlier versions, but if you specify at which version the Maple definition of this function actually switched, I'm sure I can verify from backups.

If the (entirely consistent) version really bothers you that much, then why don't you use a simple function definition to 'flip' the inputs, as in

myGammaDistribution:= (x,y) -> GammaDistribution(y,x);
with(Statistics):
X := RandomVariable(GammaDistribution(c,b)):
PDF(X, u);
X := RandomVariable(myGammaDistribution(b,c)):
PDF(X, u);

You could even incude this definition in your Maple.ini file, so it is always available (without definition) in a soecific worksheet. (Exchanging worksheets with others may become problematic, for obvious reasons)

restart;
myV:=proc(x1,x2)
                    Vector( [x1+2*x2, x2^2+x1]);
          end proc;
CodeGeneration:-Matlab( myV,
                                            output="C:/Users/TomLeslie/Desktop/test.m"
                                         );

You will have to change the filepath/name in the 'output=' option to something appropriate for your machine

 

The easiest way to do this:

  1. store all the data in a matrix
  2. The "normal" way would be to use ExcelTools:-Export(). This "works" but Excel complains about 'NULL' entries, which have to be "repaired". These NULL entries occur because your two loops start at different values and have different step sizes
  3. As an alternative save the data matrix as a "csv" file, which can then be imported into Excel without complaint. On my (Windoiws) system I have files of type "csv" associated with Excel, so just clicking on the "csv" file will open it with Excel "automagically"
  4. You will have to change the filepaths/names in the attached to something appropriate for your machine

See the attached

restart; with(LinearAlgebra); Digits := 7

c, mu := 1, 1; fe := 20; r := .1; 1/r; Lambda := .9; N := 500

0.1e2

(1)

opMat:=Matrix(fe*20+2, 8, fill=NULL):
opMat[1,..]:=Vector[row](["fg", "qe", "c*(1-r)/(mu-Lambda*r)", "c*(1-r)/(mu-Lambda)", "Sqe", "S0", "ne", "Sne"]);   ;

Vector[row](8, {(1) = "fg", (2) = "qe", (3) = "c*(1-r)/(mu-Lambda*r)", (4) = "c*(1-r)/(mu-Lambda)", (5) = "Sqe", (6) = "S0", (7) = "ne", (8) = "Sne"})

(2)

count := 2; for fg from 0 by 0.5e-1 to fe do f := fg-(1-r)*fe; if f <= 0 then qe := 0 else if Lambda <= mu-c*(1-r)/f then qe := 1 elif mu-c*(1-r)/f < Lambda and Lambda <= mu/r-c*(1-r)/(r*f) then qe := (-Lambda*r+mu)/(Lambda*(1-r))-c/(Lambda*f) elif mu/r-c*(1-r)/(r*f) < Lambda then qe := 0 end if end if; Sqe := c*(Lambda*qe*(1-r)+Lambda*r)/(mu-Lambda*r-Lambda*qe*(1-r)); S0 := c*Lambda*r/(-Lambda*r+mu); opMat[count, 1 .. 6] := Vector([fg, qe, c*(1-r)/(-Lambda*r+mu), c*(1-r)/(mu-Lambda), Sqe, S0]); count := count+1 end do

count := 22; for fg from 1.0 by .1 to fe do rho := Lambda/mu; if fg <= (1-r)*fe then ne := 0 else ne := floor(mu*(fg-(1-r)*fe)/(c*(1-r))) end if; if rho <> 1 then p[0] := (1-rho)*(-r*rho+1)/(1-r*rho-(1-r)*rho^(ne+1)) else p[0] := 1/(ne+1) end if; for n to N do if n < ne then p[n] := rho^n*p[0] else p[n] := rho^n*r^(n-ne)*p[0] end if end do; unassign('n'); Sne := sum(Lambda*c*(n+1)*p[n]/mu, n = 0 .. ne-1)+sum(Lambda*r*c*(n+1)*p[n]/mu, n = ne .. N); opMat[count, 7 .. 8] := Vector([ne, Sne]); count := count+2 end do

ExcelTools:-Export(opMat, "C:/Users/TomLeslie/Desktop/XLdat.xlsx", "Sheet1", "A1");
ExportMatrix("C:/Users/TomLeslie/Desktop/XLdat.csv", opMat);

23051

(3)

 

 

Download toExcel.mw

but you can 'spoof' the effect by using a couple of textplots(), see the attached. NB, as usual, the plot 'renders' much better within Maple than it does on this site. (eg no 'gridlines')

In order to get the coordinates for the textplot, I just "eyeballed" them. If you are doing this a lot, then it would probably be better to use the plots:-getdata() command, which will return sufficient information to calculate the location of the text automatically

restart;
with(plots):
p1:=plot(x, x=0..1, axes=normal):
p2:=textplot( [ [ 0.5, 1.1,"Identity function", 'font'=["times","bold",  20],  color=red],
                [ 0.5, 1.0, "illustration",     'font'=["times","italic",20], color=blue ]
              ],
              axes=none
            ):
display([p1,p2]);

 

 

 


 

Download tplot.mw

Your difficulty is that you are completely incapable of stating clearly what it is you are trying to achieve.

This means that every time I answer one of your questions, the answer is wrong because, your question was unclear/ambiguoius, and this is starting to get very tedious

Since your latet requirement is equally unclear, the attached might fulfil it - but probably won't!

  restart;
  with(plots):
  with(plottools):

  fileToImport := "C:/Users/TomLeslie/Desktop/test.txt":
  M1:=ImportMatrix( fileToImport):
#
# Get the row numbers where 1728 appears
#
  L:=[ seq( `if`
            ( M1[j,1]=1728,
              j,
              NULL
            ),
            j=1..op([1,1], M1)
          )
      ]:
#
# Split the input matrix into a list of matrices, bounded
# by where 1728 appears
#
  LM:= [ M1[1..L[1]-1, 1],
         seq( M1[L[j]+1..L[j+1]-1,1],
              j=1..numelems(L)-1
            ),
         M1[L[-1]+1..-1,1]
       ]:
#
# Strip the "generated by VMD" stuff
#
  F:= vec-> < seq( `if`
                   ( vec[j]="generated by VMD",
                     NULL,
                     <vec[j]>
                   ),
                   j=1..op(1, vec)
                 )
            >:
  M2:=F~(LM[2..-1]):
#
# Parse each matrix in the list
#
  G:= mat-> Matrix
            ( sscanf~( convert(mat, list),
                       "%s%f%f%f"
                     )
            ):
  M3:=G~(M2):
#
# Retain only entries matching iden
#
  iden:="C1":
  H1:= mat-> < seq( `if`
                    ( mat[j,1]=iden,
                      mat[j,..],
                      NULL
                    ),
                    j=1..op([1,1],mat)
                  )
             >:
  H2:= mat-> < seq( `if`
                    ( mat[j,1]=iden,
                      mat[j,2..],
                      NULL
                    ),
                    j=1..op([1,1],mat)
                  )
             >:
  M4:=H1~(M3):
  M5:=H2~(M3):
#
# Put the data back into single matrices
#
  M6:= < select( i->op([1,1],i)>0,
                 M4
               )[]
       >;
  M7:= < select( i->op([1,1],i)>0,
                 M5
               )[]
       >;
#
# Data is now n*3, so treat each row in the
# matrix as a simple datapoint and plot these
# points using a simple pointplot(). Put a
# solidsphere at each data point
#
  pointplot3d( M6[1..-1, 2..-1],
               color=red,
               symbol=solidsphere,
               symbolsize=30,
               axes=boxed,
               labels=[x,y,z],
               labelfont=[times, bold, 20],
               scaling=constrained
             );
#
# For reasons whihc are a complete mystery to me
# OP wants to use the sphere() plot command. This
# requires *four* values, three to determine the
# centre of the sphere and one to determine the
# radius. Since only three values exist, the
# following uses the sphere() command with the
# matrix data defining the sphere centre, and the
# sphere radius (arbitrarily) set to 1
#
  display( [ seq( sphere
                  ( convert(M6[j,2..-1], list ),
                    1,
                    color=red,
                    style=surface
                  ),
                  j=1..op([1,1], M6)
                )
            ],
            axes=boxed,
            labels=[x,y,z],
            labelfont=[times, bold, 20],
            scaling=constrained
         );
#
# Note that other than a slight variation in the
# size of the symbols plotted at each data point
# these two plots are indentical. The second method
# is just an incredibly inefficient way to do it
#

Matrix(4, 4, {(1, 1) = "C1", (1, 2) = -6.932300, (1, 3) = 12.540000, (1, 4) = -20.260000, (2, 1) = "C1", (2, 2) = -7.005000, (2, 3) = 13.914000, (2, 4) = -18.431999, (3, 1) = "C1", (3, 2) = 10.458000, (3, 3) = -20.974001, (3, 4) = -1.023700, (4, 1) = "C1", (4, 2) = 9.977000, (4, 3) = -19.162001, (4, 4) = .231220})

 

Matrix(%id = 18446744074331885806)

 

 

 

 

 

 

 

Download impData3.mw

consider c:=nops(x)/2. When the list 'x' has an odd number of elements, 'c' will not be an integer: if 'x' has 5 elements, then c will be 2.5. Later in your code you access a[c]: with 'c' not being an integer, how do you expect to evaluate a[c]?

but as you have observed, you can set x-axis tickmarks, but not y-axis.

In fact HeatMap has its 'own' option to set what the help page refers to as 'rowlabels' and'columnlabels', so the attached will achieve what you want : for some reason when I insert the worksheet here, the heatmaps do no 'render' - the code does work in Maple, honest

restart:
with(Statistics):
RM := LinearAlgebra:-RandomMatrix(10):
HeatMap(RM);
NewTickMarks := [seq(cat(`A`, k), k=1..10)]:
HeatMap( RM, rowlabels=NewTickMarks, columnlabels=NewTickMarks);

 

 

 

Download heatTicks.mw

but I would go with something like the attached, which presents the "data" corresponding to "C1" in a couple of different ways, because it is not completel clear to me whihc output format you want.

You will have to change the filepath/name to something appropriate for your machine

restart;
fileToImport := "C:/Users/TomLeslie/Desktop/test.txt":
M1:=Matrix(sscanf~(convert(ImportMatrix( fileToImport), list), "%s%f%f%f"));
iden:="C1":
M2:=<seq(`if`(M1[j,1]=iden, M1[j,..], NULL), j=1..op([1,1],M1))>;
M3:=<seq(`if`(M1[j,1]=iden, M1[j,2..], NULL), j=1..op([1,1],M1))>;

Matrix(10, 4, {(1, 1) = "C1", (1, 2) = -6.932300, (1, 3) = 12.540000, (1, 4) = -20.260000, (2, 1) = "C1", (2, 2) = -7.005000, (2, 3) = 13.914000, (2, 4) = -18.431999, (3, 1) = "C2", (3, 2) = -8.143600, (3, 3) = 11.942000, (3, 4) = -19.424000, (4, 1) = "C2", (4, 2) = -8.236300, (4, 3) = 12.972000, (4, 4) = -18.327000, (5, 1) = "C3", (5, 2) = -5.110600, (5, 3) = 12.270000, (5, 4) = -18.719000, (6, 1) = "C4", (6, 2) = -5.390700, (6, 3) = 14.318000, (6, 4) = -20.143000, (7, 1) = "C5", (7, 2) = -4.419600, (7, 3) = 15.242000, (7, 4) = -19.368999, (8, 1) = "C6", (8, 2) = -2.534000, (8, 3) = 15.407000, (8, 4) = -20.941000, (9, 1) = "C1", (9, 2) = 10.458000, (9, 3) = -20.974001, (9, 4) = -1.023700, (10, 1) = "C1", (10, 2) = 9.977000, (10, 3) = -19.162001, (10, 4) = .231220})

 

Matrix(4, 4, {(1, 1) = "C1", (1, 2) = -6.932300, (1, 3) = 12.540000, (1, 4) = -20.260000, (2, 1) = "C1", (2, 2) = -7.005000, (2, 3) = 13.914000, (2, 4) = -18.431999, (3, 1) = "C1", (3, 2) = 10.458000, (3, 3) = -20.974001, (3, 4) = -1.023700, (4, 1) = "C1", (4, 2) = 9.977000, (4, 3) = -19.162001, (4, 4) = .231220})

 

Matrix(%id = 18446744074370378318)

(1)

 

 

 

Download impData.mw

According to MAple's help pages, the arrow() command does not accept a 'color=' option. Don't ask me why, becuase I can think of no reason!. Kitonum has shown that (with certain restrictions) the arrow() command will correctly interpret a 'color=' option. Since this is contrary to the help documentation, I would prefer to avoid this choice. One can get around it be supplying the list of arrow() plots as the first argument to a display() command, and the 'color' options as the second argument to the display() command. Since the display() command definitely does accept 'color=' as an option - this is pretty much gurantedd to always work.

See the attached

  restart;
  with(plots):
  Vectors:=[[1,0,0],[0,1,0],[0,0,1]]:
  Colors:=[red,blue,green]:
#
# Slightly surprised that this works since
# according to the help, the arrow() command
# *does not* accept a 'color=' option
#
  display( [ seq( arrow( Vectors[i],
                         shape = cylindrical_arrow,
                         length = 1,
                         color=Colors[i]
                       ),
                  i=1..3
                )
           ]
         );
#
# The following is guaranteed to work, because
# no 'color' is assigned in the arrow() command.
# The latter just generates three plot structures.
# The 'color' option is applied as part of the
# display() command, where it is a perfectly valid,
# documented, option
#
  display( [ seq( arrow( Vectors[i],
                         shape = cylindrical_arrow,
                         length = 1
                       ),
                  i=1..3
                )
           ],
           color=Colors
         )

 

 

 

Download vecCol.mw

 

the attached?

restart:
times3:=[[], [.140], [1.344, .891], [1.578, 1.312, 1.375, 1.437, 1.922, 2.625, 6.406], [2.188, 2.312, 1.687, 2.110, 2.047, 1.578, 8.953, 1.891, 1.875, 9.344, 2.203, 55.969, 2.266, 2.531, 81.078, 2.172, 50.641, 2.500, 3.141, 61.656, 3.406, 3.375]]:
`~`[`~`[log10]](times3):
times1:=[[.718], [.766, 4.703], [.750, .797, 7.594, 3.938], [6.594, 7.718, 11.969, 8.485, 11.391, 130.583, 548.284, 974.435], [7.281, 8.515, 65.569, 7.016, 8.312, 9.500, 8.562, 9.766, 10.641, 12.609, 13.281, 17.453, 18.640, 1763.860, 2659.990, 7812.89, 8189.139]]:

Statistics:-BoxPlot( [ seq( [ log10~(times3[j]),
                              log10~(times1[j])
                            ][],
                            j=1..numelems(times3)
                          )
                     ],
                     color=[ seq( [ red, blue ][],
                                  j=1..numelems(times3)
                                )
                           ],
                     tickmarks=[ [ seq( [ i+0.375="good",
                                          i+1.375="bad"
                                        ][],
                                        i=1..2*numelems(times3),2)
                                 ],
                                 default
                               ]
                   );

 

 

 

Download box.mw

depending on your interpretation of a "vector".

Does it have geometrical significance? If so use the VectorCalculus() package

Is it just a 1-dimensional "container for numbers? If so use the LinearAlgebra package

I don't want to be bothered by either of the above - so do it the "quick and dirty" way.

All three are shown in the attached (and there are probably others

  restart;
#
# Load the Vector Calculus package
#
  with(VectorCalculus):
#
# Define a (Cartesian) vector
#
  A:=<4.00, 7.00>;
#
# Get the magnitude of the vector
#
  Norm(A);

Vector(2, {(1) = 4.00, (2) = 7.00})

 

8.062257748

(1)

  restart;
#
# Load the LinearAlgebra package
#
  with(LinearAlgebra):
#
# Define a (Cartesian) vector
#
  A:=<4.00, 7.00>;
#
# Get the magnitude of the vector
#
  VectorNorm(A,2);

Vector(2, {(1) = 4.00, (2) = 7.00})

 

8.06225774829854913

(2)

#
# Quick and dirty - define very little
#
  restart;
  A:=<4.00, 7.00>;
  sqrt(A.A);

Vector(2, {(1) = 4.00, (2) = 7.00})

 

8.062257748

(3)

 

Download vnorm.mw

If you are not interested in staitistical samples obeying sone PDF, but just want to be able to obtain values of a simple function - then define the function using operator notation, as shown in the toy example below. The value of the function for any given argument can then be simply obtained.


 

F:=x->x^2+sin(x);
F(1.0);
F(2.0);
F(3.0);

proc (x) options operator, arrow; x^2+sin(x) end proc

 

1.841470985

 

4.909297427

 

9.141120008

(1)

 


 

Download aFunc.mw

your worksheet contains the command

with(plots, implicitplot)

so you can access the command implicitplot() from the plots package. However you also use the display() command from the plots package - but this command has not been loaded

It would be simpler just to replace the above using with(plots), which will load all the commands from the plots package. So in your case, both implicitplot() and display() will be available for use.

Programming styles vary, and no one is ever definitively "correct". For what it's worth, if I have a worksheet which uses commands from specific packages, then I tend to have all of the relevant with() commands in an execution group immediately following the restart command

First 124 125 126 127 128 129 130 Last Page 126 of 207