acer

32747 Reputation

29 Badges

20 years, 112 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

Congratulations on the acute observation, as well as your circumspection.

You are quite right in that `op` can be used in this way, to get the dimensions of a Matrix.

And it's sensible to ask whether it can be relied upon to continue to work. As a general rule, I would usually advise using the command (such as LinearAlgebra:-Dimension), over picking apart the structure.

But let's observe two things. First, the `op` command has worked in this way for every release from Maple 6 through to Maple 13. And second, `op` is used in this way in many places within the Maple Library, including LinearAlgebra routines.

I would thus suggest that your choice might depend on the performance cost versus legibility. Will your code call this operation many times, so that the extra Library level function calls would be a measurable hit?

acer

Using names for the numeric values forestalls (numeric) evaluation. But it is (all) the arithmetic operations which the OP wanted to delay. So, how about rebinding those operators with inert versions. And then, just to get it to display nicely, create print extensions for those inert operators which themselves utilize names-for-numerics.

> restart:

> p:=module() export `+`, `^`, abs;
> option package;
>   `+`:=proc() &+(args); end proc:
>   `^`:=proc() &^(args); end proc:
>   abs:=proc() &abs(args); end proc:
> end module:

> `print/&+`:=proc(a,b) :-`+`(`if`(type(a,numeric),convert(a,name),a),
                             
> `if`(type(b,numeric),convert(b,name),b)); end proc:

> `print/&^`:=proc(a,b) :-`^`(`if`(type(a,numeric),convert(a,name),a),
                            
> `if`(type(b,numeric) and b<-1,convert(b,name),b)); end proc:

> `print/&abs`:=proc(a) :-`abs`(`if`(type(a,numeric),convert(a,name),a)); end proc:

> f:=proc(X)
>    subsindets(X,specfunc(numeric,
>       {`&^`,`&+`,`&abs`}),
>       t->`if`(op(0,t)=`&^`,:-`^`(op(t)),
>          `if`(op(0,t)=`&+`,:-`+`(op(t)),:-abs(op(t)))));
> end proc:

> with(p):

> expr:=12/(1-abs(2^2-3^2))+abs(-7)/abs(6-2^2)^2;
                                  12             | -7 |
                    expr := --------------- + ------------
                                   2    2             2  2
                            1 - | 2  - 3  |   | -6 + 2  |
 
> f(expr);
                               12              7
                         -------------- + -----------
                         1 - | 4 + -9 |             2
                                          | 6 + -4 |
 
> f(%);
                                  12         7
                              ---------- + ------
                              1 - | -5 |        2
                                           | 2 |
 
> f(%);
                                   12      7
                                 ------ + ----
                                 1 + -5     2
                                           2
 
> f(%);
                                   12
                                  ---- + 7/4
                                   -4
 
> f(%);
                                   -3 + 7/4
 
> f(%);
                                     -5/4

acer

If you know some keyword(s) that you remember appearing in the thread then you could try google itself (using `mapleprimes` & keywords). But I don't  advise spending time trying mapleprimes own search facility -- it is heavily broken and does not seem to have been recording new posts for many months now. (See here.)

Or you could manually search all the forums.

acer

Maybe you could use the Statistics package, with high working precision.

> restart:with(Statistics):

> X:=RandomVariable('Normal'(0,1)):

> evalf[50](2*CDF(X,1)-1);
             0.68268949213708589717046509126407584495582593345319
 
> evalf[50](Quantile(X,(%+1)/2));
             0.99999999999999999999999999999999999999999999999997

> evalf[50](Quantile(X,CDF(X,1)));
             0.99999999999999999999999999999999999999999999999996

Did I do that right?

acer

Use Array instead of array, with the first letter capitalized.

A:=Array(1..3):
for i from 1 to 3 do
  A[i]:=f(i);
end do:
A;
                           [f(1) f(2) f(3)]

B:=Array(1..3,i->f(i));
                         B:=[f(1) f(2) f(3)]

See the help-page ?Array

acer

Have you tried using `map` to apply `diff` to each entry of the Vector?

acer

> restart:
> H:=Array(1..2^16,datatype=float[8],storage=rectangular,order=C_order);
                              [ 1..65536 1-D Array   ]
                         H := [ Data Type: float[8]  ]
                              [ Storage: rectangular ]
                              [ Order: C_order       ]
 
> rtable_dims(H);
                                  1 .. 65536
 
> rtable_options(H);
 datatype = float[8], subtype = Array, storage = rectangular, order = C_order
 
> rtable_options(H,order,storage);
                             C_order, rectangular

This is one of a few such helpful routines which can be used together to copy an Array/Matrix/Vector.

> P:=Array(1..3,[11,13,17],datatype=float[8],storage=rectangular,order=C_order);
                             P := [11., 13., 17.]
 
> rtable(rtable_dims(P),rtable_elems(P),rtable_options(P));
                                [11., 13., 17.]
And the `copy` routine does it even more carefully,
> interface(verboseproc=3):
> showstat(copy,2);
 
copy := proc(A)
local X, r, e;
       ...
   2     rtable(rtable_indfns(A),rtable_dims(A),A,rtable_options(A),readonly = false)
       ...
end proc

acer

Note that below I use an Array (capitalized).

> X:=Array(3): # originally with 3 elements only

> for i from 1 to 5 do
>  X(i):=i^2;
> end do:

> X; # using Maple 13, X can be "grown" to be of length 5
                        [1 4 9 16 25]

In much older versions of Maple, I would not have been able to grow X in size, and would have been limited to its original 3 entry spots. And I would have had to use the syntax X[i]:=... instead of X(i):=...

I could also have used a table structure, which is yet another type of mutable structure with an open-ended number of entries.

Note also that X was not created using X:=[...] as that would have created a list rather than an Array. See here for a bit more about comparison between Maple's data structures.

acer

I may be in the minority here, but I prefer using the Matrix() constructor with its listlist argument for the data. For example, I find that I only very rarely get the syntax wrong for something like this,

Matrix([[1,3],[-3,1]]);

You might also consider guessing what the common syntax mistakes are with using the angle brackets, and perhaps even convert/accept typical mistakes. For example, you could convert, <<1,3>,<3,-1>> and give partial or full marks.

acer

The Tolerances package uses evalr.

> R1:=INTERVAL(76.0 .. 84.0):
> R2:=INTERVAL(114.0 .. 126.0):
> Req:=R1*R2/(R1+R2):

> evalr(Req);
                     INTERVAL(41.25714285 .. 55.70526317)
 
> evalr(1/(expand(1/Req)));
                     INTERVAL(45.59999998 .. 50.40000003)

Now, that second result was just lucky. Terms simplified and repeated instances of R1 and R2 vanished before evalr got its hands on them.

Are you trying to bound the behaviour of the system, given bounds on the capabilities of the components? If so, then I can imagine that a Tolerances package that functions well could be useful.

If not, then are you trying to characterize behaviour given physical measurements of the resistances (with of course have an associated measurement error)? In that case, is the ScientificErrorAnalysis package of any use? Note that for that package, the error does not represent an interval, but rather a distribution.

> restart:

> R1:=ScientificErrorAnalysis:-Quantity(80.1,4.0)*Unit(ohm):
> R2:=ScientificErrorAnalysis:-Quantity(120.1,6.0)*Unit(ohm):

> Req:=combine(R1*R2/(R1+R2),errors);
           Req := Quantity(48.05199800 [Omega], 1.730531812 [Omega])

acer

map(trunc,a);

map(round,a);
Or, in Maple 13,
trunc~(a);

round~(a);
Notice that the ~ elementwise operation preserved the float[8] datatype, which is why those two results came back as floats.

acer

With a small but nonzero chance (probability) you might have to wait for 1000 rolls before that happens. And an even smaller chance that you'd have to wait a million rolls.

So your question could do with an associated probabaility in order to make good sense.

More often, the reverse is asked. For example, "What is the probability that I would roll all those many instances within my first 500 rolls?"

I'm assuming that you're not just asking the simplistic question: "What's the minimal number of rolls needed for it to be possible?" (ie. 1+2+3+4+5+6)

acer

See the ?evalf help-page.

acer

You may be able to write a Compilable proc that does what falls inside your i-loop, and possibly also to handle the sorting that follows it.

See here for an example of explicit code to do Statistics:-StandardDeviation, which may be used with Compiler:-Compile.

Such a Compilable proc might also accept a datatype=float[8] Vector parameter SH and act on that inplace, by populating it with the looped results. Of course, the datatype=float[8] Matrix/Array StockReturn would be one of its input parameters.

You could either loop and call such a proc, passing in each permutation, of even better you could pass in the full `per` permutation (as acceptable data for the Compiler) and loop inside the very proc itself.

acer

I've now got a routine that induces barrel, pincushion, and moustache distortion, which seems to work well as a prototype. Right now, it takes about .2-.25 sec on an 800x600 jpg (as a three-layer float[8] Array, in Maple).

I hope to hammer on it a bit more, to attain even less image degradation by using ArrayInterpolation. And then get the inverted operations. And then post the module code.

acer

First 294 295 296 297 298 299 300 Last Page 296 of 341