Joe Riel

9530 Reputation

23 Badges

20 years, 23 days

MaplePrimes Activity


These are answers submitted by Joe Riel

Note that your typing of v is not valid, try executing the procedure without compilation and kernelopts(assertlevel=2). Also, ?Compiler,Compile states:
Array Support
- Arrays can only appear as parameters of a procedure that is to be compiled.
While not ideal, you could make v a parameter:
kernelopts(assertlevel=2);
                                       0

v := Vector([1$4],datatype=float[8]):
foo := proc(x::float, v::Vector(4,datatype=float[8]))
    return x*v[2];
end proc:

foo(1.0,v);
                                      1.0


fooc := Compiler:-Compile(foo);
fooc := proc()
option call_external, define_external(_ma78ea510b1b4af6a145704a5af10ab6b,
MAPLE, LIB = "/var/tmp/joe-30090/_ma78ea510b1b4af6a145704a5af10ab6b.so");
    call_external(0, 1073846032, true, args)
end proc


fooc(1.0,v);
                                      1.
You can also use a regular conditional: SKIPLINES := true:
if SKIPLINES <> true then
...
end if;
to skip lines of code.
One way is to symbolically evaluate the function to generate an expression, then use unapply to create a new function:
g := unapply(f(A,'b'),'b');
                              g := b -> 3.5 + b
Note the forward quotes around b to delay its evaluation. This method won't always work, since it isn't always possible to symbolically evaluate a function. A more general technique is the following:
g := subs(_A=A, b -> f(_A,b));
                                   g := b -> f(3.5, b)
Another way to do that is the following
g := (a -> b -> f(a,b))(A);
                                   g := b -> f(3.5, b)
It is briefly mentioned in the the help page for rtable. Its first argument is a range (0..1), its second argument specifies the percentage of nonzero values (1.0, you want all to be nonzero). The elements of the rtable are randomly and uniformly initialized between 0 and 1. Using frandom is much faster than other methods (see generating an array of random floats). Here is an example of creating a Vector with 10 random elements:
rnd := rtable(1..10, frandom(0..1,1.0), subtype=Vector[column], datatype=float[8]):
                                                  [ 0.530484209985166988 ]
                                                  [ 0.952299352583890979 ]
                                                  [0.0354787515261802966 ]
                                                  [ 0.240688559954775894 ]
                                                  [ 0.767101828106371397 ]
                                         rnd :=   [ 0.724353985897650987 ]
                                                  [ 0.388089818893285866 ]
                                                  [ 0.219477841874477498 ]
                                                  [0.00149287817728582796]
                                                  [ 0.436224435569102509 ]
You can use the frandom initializer to rtable to quickly create a Vector of floats. In the following I'll generate a Matrix (rather than a Vector) so that I can use the PlotHistogram procedure from ImageTools to generate the histogram.
rnd := rtable(1..10\000, 1..1, frandom(0..1,1.0), subtype=Matrix, datatype=float[8]):
plt := ImageTools:-PlotHistogram(rnd,10):
display[plots](plt, style=point);
Just do ans mod d*e. Actually, because the inline mod operator can be reassigned to be either modp or mods (see the help page for mod), it is frequently better to use the modp or mods function directly. Thus, change the last line in the procedure to
modp({...}, d*e)
, where the ellipsis indicates the original statement (depending on which version you used.
Good point. Note that the problem exists even if you stay in document mode (or 2D input). That is, cutting and pasting the output to an input region causes a conversion from the literal subscript format to the indexed format. Rather than doing evalb(%=t[0]), you can evaluate lprint(%), that prints the 1D representation of the symbol. In this case,
lprint(t0);
`#msub(mi("t"),mn("0"))`
After pasting the output into an lprint we get
lprint(t0);
t[0]
I rarely cut and paste from the output into an input region. Better to learn to use Maple operators to pick off the element of interest.
UCS := proc(d,e)
local ab;
    ab := {[0, 0], [1, 1], [0, 2], [1, 3], [0, 4], [1, 5], [0, 6], [1, 0], [0, 1], [1, 2], [0, 3], [1, 4], [0, 5], [1, 6], [0, 0]};
    map(chrem, ab, [d,e]);
end proc:

UCS(2,11);
              {0, 1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 15, 16, 17}
Note that I used a set for ab. That ensures that the output contains unique elements. Another approach is to use a list and then remove duplicates. With that approach you may want to explicitly sort the results; that may not be necessary with a set since, for small integers, the results are automatically sorted:
UCS := proc(d,e)
local ab;
uses ListTools;
    ab := [[0, 0], [1, 1], [0, 2], [1, 3], [0, 4], [1, 5], [0, 6], [1, 0], [0, 1], [1, 2], [0, 3], [1, 4], [0, 5], [1, 6], [0, 0]];
    sort(MakeUnique(map(chrem, ab, [d,e])));
end proc:
It would be a lot easier if you just pasted the code into your post.
Practically, I do what Axel does, I use t0. However, because one of the goals of the document interface is to be able to generate presentation quality output, there should be a relatively convenient way to use the subscripted variable in the display. I don't consider palette selection for something as common as subscripts to be convenient; a keyboard shortcut is called for. Note that there is a shortcut for generating a prefixed subscript: 0t. That can be done from 2D input by typing t Ctrl-Shift-_ 0. Considering that prefixed subscripts are rare beasts, it would have been better if that generated a literal subscript (suffixed subscript). Maybe there is a short cut, but I don't know what it is (I found that one by trial and error). Similarly, t Ctrl-Shift-^ 0 produces 0t. A literal suffixed superscript (t0) would be more useful.
Another way to do this, even less practical than my previous suggestion, is to convert the base symbol (t) in the indexed name (t0) to a local variable. Use a macro to make this less slightly less impractical:
macro(t0 = convert(t,`local`)[0]):
int(t0, t);
              t0 t
The reason that the first doesn't display properly is that Maple's standard gui doesn't automatically display arrays (or Arrays) whose indices don't start at one. The simple way to get around that is to make it a Matrix (whose indices always start at one). That is do
EBO := Matrix(11,1):
...
EBO[s+1,n] := ... ;  # shift index by one
Alas, that doesn't quite work. If you do that you get something that looks like the following:
                           [ 11 x 2 Matrix        ]
                           [ Data Type: anything  ]
                           [ Storage: rectangular ]
                           [ Order: Fortran_order ]
The 'rtablesize' option of the interface controls the maximum size of rtables that are displayed. The default is 10. Increasing this to 11 allows this Matrix to display:
interface(rtablesize=11):
A simpler possibility, which avoids these issues is to use the printf command:
EBO := Array(0..10,1..2): # note that this is an Array, not array
...
EBO[s,n] := ...
...
printf("%0.3f\n", EBO);
1.000 4.000
0.368 3.018
0.104 2.110
0.023 1.348
0.004 0.781
0.001 0.410
0.000 0.195
0.000 0.085
0.000 0.034
0.000 0.012
0.000 0.004
I don't understand your question. What does a loop have to do with generating random data? One can use the RandomTools package to generate random values in whatever form you desire. For example,
RandomTools:-Generate(list(float(range=2..3,method=uniform), 20));
[2.277986140, 2.581869302, 2.669121262, 2.365109113, 2.545404204, 2.940029919,
    2.701693957, 2.949333985, 2.568478650, 2.249825579, 2.418932835,
    2.202810917, 2.122398916, 2.809094426, 2.201354591, 2.891235047,
    2.922939225, 2.934992634, 2.676943009, 2.969970961]
There has been some discussion on this site about efficient ways of generating random values, but at this point it isn't clear whether efficiency is even a concern.
From symmetry its clear that the solution is phi=1-2/Pi*theta, for theta from 0 to Pi. This is easily verified with the VectorCalculus package:
with(VectorCalculus):
SetCoordinates('polar[r,theta]'):
phi := 1-2/Pi*theta:
Nabla(phi);
                                    2   _
                                 - ---- e
                                   r Pi  theta
Laplacian(phi);
                                     0
Maple 11 adds the ability to readily define multiple names for a keyword parameter when assigning a procedure. That is, one can do
myproc := proc( {[capitalize,capitalise] ::= truefalse := false}) ... end proc:
to assign a procedure with a truefalse keyword parameter "capitalize" that can also be spelled "capitalise".
First 104 105 106 107 108 109 110 Last Page 106 of 114