Joe Riel

8049 Reputation

22 Badges

15 years, 10 days

MaplePrimes Activity


These are replies submitted by Joe Riel

I had already done that, but out of necessity because I already had an icoeff4.
On review, the flat option fails (as does the "inline" version). This because
indets(4*a,Not(integer));
         {a, 4*a}
so 4*a gets replaced by 1.
On review, the flat option fails (as does the "inline" version). This because
indets(4*a,Not(integer));
         {a, 4*a}
so 4*a gets replaced by 1.
It nicely handles complex values and is certainly easier to understand, if less compact.
It nicely handles complex values and is certainly easier to understand, if less compact.
Thanks for the reminder. Someday maybe that will make it into the help page 8-). Of course, for a bit more speed I could just inline subsindets[flat]:
icoeff5 := t-> `if`(t::{integer,`*`},subs(map(x->x=1, indets(t,Not(integer))),t),1);
That is about twice as fast as using the call to subsindets[flat], which is twice as fast as not using the flat option, which is a couple orders of magnitude faster than using patmatch. If the arguments were mainly integers it would be slightly faster to handle them separately, but at this point speed is not an issue.
Thanks for the reminder. Someday maybe that will make it into the help page 8-). Of course, for a bit more speed I could just inline subsindets[flat]:
icoeff5 := t-> `if`(t::{integer,`*`},subs(map(x->x=1, indets(t,Not(integer))),t),1);
That is about twice as fast as using the call to subsindets[flat], which is twice as fast as not using the flat option, which is a couple orders of magnitude faster than using patmatch. If the arguments were mainly integers it would be slightly faster to handle them separately, but at this point speed is not an issue.
How about,
icoeff3 := t-> `if`(t::{integer,`*`},subsindets(t,Not(integer),1),1);
That it fails (safely) with 3*I (as do the other approaches) isn't a concern in the particular application. By safely, I mean that returning 1 is always acceptable (though not ideal). I'm looking for a common integer factor among various terms; failing to find one is not critical, finding one that doesn't exist is.
How about,
icoeff3 := t-> `if`(t::{integer,`*`},subsindets(t,Not(integer),1),1);
That it fails (safely) with 3*I (as do the other approaches) isn't a concern in the particular application. By safely, I mean that returning 1 is always acceptable (though not ideal). I'm looking for a common integer factor among various terms; failing to find one is not critical, finding one that doesn't exist is.
Using subsindets is clever, thanks. To make it robust, I can do the following:
icoeff2 := t-> `if`(t::`*`,subsindets(t,Not(integer),1),1);
That eliminates the errors with sums. It's not immediately clear how subsindets does the job. To see it, do
subsindets(x^2,Not(integer),Q);
                       Q(Q(x)^2)
Because integers, when used as functions, return themselves, using 1 instead of Q returns 1.
Using subsindets is clever, thanks. To make it robust, I can do the following:
icoeff2 := t-> `if`(t::`*`,subsindets(t,Not(integer),1),1);
That eliminates the errors with sums. It's not immediately clear how subsindets does the job. To see it, do
subsindets(x^2,Not(integer),Q);
                       Q(Q(x)^2)
Because integers, when used as functions, return themselves, using 1 instead of Q returns 1.
Let me exand on what Alec mentioned. If you post Maple code (a good idea if you have a particular question concerning it), the most useful format is that which allows us to easily copy and paste it. Please use the 1D Maple format. When posting code I use the
 tag so that indentation is preserved.  A caveat with the 
 tag is that it does not handle the "<" character.  To insert it a "<" character, use < (the semicolon is required).
So far as I know, there isn't staff at Maplesoft assigned to answer these questions. Many, probably most, of the responses are from members of the Maple community. If the question is easy (say a usage or programming issue) it will probably be quickly interested. If the question is unclear (e.g. the one titled "scientific notation") it may not be answered at all. If the question involves some difficult mathematics, well then it depends if someone finds it interesting and has the time. The best way to get a question answered is to clearly formulate it, as simply as possible. Then give it an appropriate subject.
Another possibility, which works with pure reals and imaginaries, is (Re^2+Im^2)(z), or a variant. Here is a comparison of several methods for generating the magnitude (or square of magnitude).
restart;
use RandomTools in
    zs := [seq](Generate(complex(float(range=0..1,method=uniform))),i=1..10^3);
end use:

n := 2;
time(proc() to 10^n do map(z -> z*conjugate(z),zs) od end());
time(proc() to 10^n do map(Re^2+Im^2, zs) od end());
time(proc() to 10^n do map(z -> Re(z)^2+Im(z)^2, zs) od end());
time(proc() to 10^n do map(abs,zs) od end());
time(proc() to 10^n do map(z->op(1,z)^2+op(2,z)^2,zs) od end());

time(proc() to 10^n do [seq(Re(z)^2+Im(z)^2, z=zs)] od end());
time(proc() to 10^n do [seq(op(1,z)^2+op(2,z)^2,z=zs)] od end());

                                     7.570
                                     6.259
                                     6.879
                                    12.050
                                     7.079
                                     5.410
                                     5.610
Using seq is somewhat faster than map. The fastest here, Re(z)^2+Im(z)^2, is also robust, it works for pure reals and imaginaries.
Thanks for the suggestion, that looks like a useful function. One way to guarantee the order when mapping over Vectors and Matrices is to use LinearAlgebra:-Map. While its help page doesn't state that it maps from lowest to highest, it uses LinearAlgebra:-`Map/Internal` which uses a do loop with the indices.
counter := module()
export count,reset;
local cnt;
    reset := proc() cnt := 0 end;
    count := proc() cnt := cnt+1 end;
    reset()
end module:

M := Matrix([[0$5]$5]):
counter:-reset():
LinearAlgebra:-Map(counter:-count,M):
print(M);
                         [ 1     2     3     4     5]
                         [                          ]
                         [ 6     7     8     9    10]
                         [                          ]
                         [11    12    13    14    15]
                         [                          ]
                         [16    17    18    19    20]
                         [                          ]
                         [21    22    23    24    25]
First 174 175 176 177 178 179 180 Page 176 of 182