Carl Love

Carl Love

27641 Reputation

25 Badges

12 years, 108 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

If is an unassigned symbol, then A[B] is equivalent to A:-B. On the other hand, if is an export of A, and is also an assigned symbol outside of A, then A:-B must be used to disambiguate the Bs.

Like this:

foldl(gcd, lst1[]);

You could also use foldr. Since gcd is associative, it doesn't make any difference.

If is any set, and f is any binary operation on S, then the fold commands can be used to extend f to an arbitrary number of arguments.

After the loop, do

save f, "filename";

I don't know any way to append to a file with the save command.

Here is an implementation in Maple of an infix operator akin to the that you showed from bash. I added an additional feature that the version number is automatically incremented.

restart
:
(* An infix operator for filename formatting with automatically incrementing
   version number

Parameters:
   f::anything   the "stem" of the filename
   V::anything   the version
      If V is a variable assigned an integer, then it's incremented and used;
      if V is an unassigned variable, then it's assigned 1 and used;
      if V is anything else, it's appended to the filename as is.
   w::posint     the version field width
      The field width to use for nonnegative integer version numbers. They get
      padded with leading zeros to width w.
   ext::string   the file name "extension" 
*)

`&$`:= proc(f, V::uneval, w::posint:= 2, ext::string:= "txt")
local v:= eval(V), N:= V::name, n:= v::name, i:= v::integer, p:= `if`(N,1,0);
    String(
        f,
        0 $ w - 
            if i and (N implies v > -2) then length(v+p) + `if`(v = -p, 1, 0)
            else `if`(n,1,0)
            fi,
        `if`(N, `if`(n, (V:= 1), `if`(i, ++V, v)), v), 
        `if`(ext="", "", "."||ext)
    )
end proc
:
#Usage examples:
V:= 'V': #Erase prior value, if any
filename &$ V;
                        "filename01.txt"
filename &$ V;
                        "filename02.txt"
V;
                               2

#To change the width or extension from their defaults, use the operator in prefix form:
&$ (myfile, 7, 3, "map");
                        "myfile007.map"

 

What you want is a basic string operation, not a file operation. That might be why you had trouble finding it.

MakeName:= proc(f::string, ver::posint, wid::posint:= 2, ext::string:= "txt")
    sprintf("%s%0*d.%s", f, wid, ver, ext)
end proc:

MakeName("MyFile", 1);
                         "MyFile01.txt"

 

This works:

thaw(eval(evalindets(expr, specfunc(invlaplace), freeze), t= 0));

To understand why your other ideas can't work, look at

In1:= indets(expr, anything);
In2:= indets(expr, Not(specfunc(invlaplace)));

For any type TNot(T) is also a type; it doesn't help you do want you want in this case.

Just add this to your initialization file:

plots:-setoptions(size= [300, 300]):

For some of the negative values of in your given range (-0.9 .. 0.2*Pi), the solve command in Proc returns (correctly) results with nonzero imaginary parts. This causes problems for some of the subsequent plotting commands. If you change the range to -0.4 .. 0.2*Pi, for example, then the command completes fine. I'm not saying that this is the widest range that'll work, simply that it's one that works.

Both your mysterious lack of randomization and the absence of infolevel printouts are due to the integral's value being "remembered" by evalf. If you simply put forget(evalf) into your loops, those anomalies will disappear.  Indeed, all that you need is this forget; you can remove the randomize.

restart:
infolevel[`evalf/int`]:= 4:
to 3 do
    ## seed:= randomize(rand());
    forget(evalf);
    J:= (evalf@Int)(
        4*x1*x3^2*exp(2*x1*x3)/(1+x2+x4)^2, 
        [x||(1..4)]=~ 0..1, method= _MonteCarlo, epsilon= 1e-2
    )
    ## print('seed' = seed,  'J' = J);
od;

Furthermore, it seems as if you were to use randomize, it would have no effect. I suspect (but I'm not sure) that this is because the random numbers are being generated by externally compiled code rather than by the Maple call-backs mentioned by @acer. This is unfortunate, because it means that the values cannot be stabilized by using randomize(key) where key is a loop invariant. The lack of stable answers makes debugging difficult.

I find this little utility procedure useful, because it lets you "forget" without needing a separate line of code for it (thus promoting functional programming):

forgotten:= C-> (forget(C), C):

Then in the loop, remove forget(evalf), and replace evalf@Int with forgotten(evalf)@Int.

For polynomials in C[x], the distinguishing of roots is done by the index option of RootOf. See help pages ?RootOf and ?RootOf,indexed. An ordering of the complex numbers based on argument and magnitude is used for this purpose. The ordering can be changed (this is described on ?RootOf,indexed). Since it looks like you're a new Maple user, I wouldn't recommend changing the ordering on your own just yet.

Here is an example:

The RootOf command always has a single bound variable, which it changes to _Z regardless of your initial specification. In the case of an insoluble irrecducible polynomial in C[x] with exact coefficients, the allvalues command separates to roots by index.

Rts:= [allvalues](RootOf(x^5+x^2+1, x));

[RootOf(_Z^5+_Z^2+1, index = 1), RootOf(_Z^5+_Z^2+1, index = 2), RootOf(_Z^5+_Z^2+1, index = 3), RootOf(_Z^5+_Z^2+1, index = 4), RootOf(_Z^5+_Z^2+1, index = 5)]

NRts:= <evalf(Rts)>:
DataFrame(<NRts | argument~(NRts) | abs~(NRts)>, columns= [Root, Arg, Abs]);

DataFrame(Matrix(5, 3, {(1, 1) = .7515192324+.7846159210*I, (1, 2) = .8069402580, (1, 3) = 1.086463667, (2, 1) = -.1545896767+.8280741332*I, (2, 2) = 1.755357607, (2, 3) = .8423803999, (3, 1) = -1.193859111, (3, 2) = 3.141592654, (3, 3) = 1.193859111, (4, 1) = -.1545896767-.8280741332*I, (4, 2) = -1.755357607, (4, 3) = .8423803999, (5, 1) = .7515192324-.7846159210*I, (5, 2) = -.8069402580, (5, 3) = 1.086463667}), rows = [1, 2, 3, 4, 5], columns = [Root, Arg, Abs])

 

Download IndexedRootOf.mw

This works:

thaw(PDEtools:-Solve(subs(Ys=~ (Fz:= freeze~(Ys)), sys), Fz));

The variable names that have session-dependent assumptions are the indices of the table `property/OrigName`. Thus the following supplies the information that you're looking for, although not exactly in the format that you showed:

about(indices(`property/OrigName`, 'nolist'));

You can learn where the information is stored by reading the code of about, which is fairly short and straightforward:

showstat(about);

@C_R The fact that map(t-> t*~2, eqs) "works" is just a red herring, and has nothing to do with the elementwise operator, since map(t-> t*2, eqs) also works, producing identical results. Indeed, applying any elementwise operator to an equation (or indeed to any structure other than a setlisttable, rtable, or expression sequence) will give identical results to the non-elementwise form of the operator.

The anomaly with `*` must be due to the lack of a final eval internally, the same thing that you noticed. I can't yet explain the anomaly with `^`, but it might be related to either or both of these:

  • Of the 5 operators you tested, only `^` is non-associative.
  • Of those 5, only `^` requires a specific number of arguments, namely 2.

I don't think that the behavior of the standard arithmetic operators on non-algebraic structures is documented, so I can't call what you discovered a bug; it's just an anomaly. You can change the behavior like this:

AdjustArithOps:= module()
export
    `*`:= eval@:-`*`,

    `^`:= proc(a,b,$)
        try :-`^`(a,b) 
        catch "non-algebraic base": 
            if a::atomic then error else map(thisproc, a, b) fi
        catch "non-algebraic exponent": 
            if b::atomic then error else map2(thisproc, a, b) fi
        end try
    end proc
;
end module
:
eqs:= [a=b, c=d]:
use AdjustArithOps in 
    #Now you can do what you tried in your Question, or you can 
    #simply use the elementwise forms:
    map(`^`, eqs, 2); eqs^~2; map(`*`, eqs, 2); eqs*~2
end use; 
                       [ 2    2   2    2]
                       [a  = b , c  = d ]

                       [ 2    2   2    2]
                       [a  = b , c  = d ]

                     [2 a = 2 b, 2 c = 2 d]

                     [2 a = 2 b, 2 c = 2 d]

 

Yes, your computations can be greatly simplified by using elementwise operations (see help page ?elementwise).

restart

C := x^2/A^2+y^2/B^2-1

x^2/A^2+y^2/B^2-1

L := y = k*x+b

y = k*x+b

#option "explicit" not necessary, but might help for more complicated equations:
x||(1..2):= solve(subs(L,C), x, explicit);

-(b*k*A-(A^2*B^2*k^2+B^4-B^2*b^2)^(1/2))*A/(A^2*k^2+B^2)

-(b*k*A+(A^2*B^2*k^2+B^4-B^2*b^2)^(1/2))*A/(A^2*k^2+B^2)

L||(1..2):= subs~([y=~ y||(1..2)], [x=~ x||(1..2)], L)[];

y1 = -k*(b*k*A-(A^2*B^2*k^2+B^4-B^2*b^2)^(1/2))*A/(A^2*k^2+B^2)+b

y2 = -k*(b*k*A+(A^2*B^2*k^2+B^4-B^2*b^2)^(1/2))*A/(A^2*k^2+B^2)+b

y1_plus_y2, y1_by_y2 := simplify(`~`[rhs]([L1+L2, L1*L2]))[]

2*B^2*b/(A^2*k^2+B^2), B^2*(-A^2*k^2+b^2)/(A^2*k^2+B^2)

NULL

Download Elementwise.mw

The possibility of a programmatic or automatic solution to this has been mentioned, but no code actually given. This is easier to code than one may expect.

restart
:

IntofSum2SumofInt:= IofS->
    evalindets(
        IofS,
        And(specfunc({Int,int}), specfunc({sum,Sum}) &under curry(op,1))
            &under combine,
        e-> local Op:= rcurry(op, combine(e))@`[]`;
            Op(1,0)(Op(0)(Op(1,1), Op(2..)), Op(1, 2..))
    )
:

inte_eq:= int(
    -(sum(B[i]*beta[i]*exp(-beta[i]*(t-tau))*tau, i= 1..n)),
    tau= 0..t
);

int(-(sum(B[i]*beta[i]*exp(-beta[i]*(t-tau))*tau, i = 1 .. n)), tau = 0 .. t)

IntofSum2SumofInt(inte_eq);

sum(-B[i]*(beta[i]*t+exp(-beta[i]*t)-1)/beta[i], i = 1 .. n)

 

Download IntofSum.mw

I don't know why the prettyprintred integrals and sums didn't appear above, but you can see them in the worksheet.

You could replace the extra factor of tau with 1/tau in the above, but the result is trivial because the integrals diverge at tau=0 (unless the Bs and betas make all the integrands zero).

1 2 3 4 5 6 7 Last Page 1 of 392