Joe Riel

9660 Reputation

23 Badges

20 years, 4 days

MaplePrimes Activity


These are answers submitted by Joe Riel

Maple, alas, doesn't not currently have a sort-in-place procedure that operates on rtable (Matrices).  You can accomplish this by converting to a listlist and sorting that. For example

A := <<5,3,6,2>|<0.9,0.99,0.8,0.85>>:
L := convert(A,listlist);
sort(L, (a,b) -> a[2]>b[2]);
Matrix(%);
                                  [3    0.99]
                                  [         ]
                                  [5    0.9 ]
                                  [         ]
                                  [2    0.85]
                                  [         ]
                                  [6    0.8 ]

For large Matrices you might use a modification of sorting-with-attributes:

n := op([1,1],A);
L2 := [seq(setattribute(SFloat(A[i,2]),i), i=1..n)]:
L3 := sort(L2,`>`);
Matrix([seq([A[attributes(L3[i])]], i=1..n)]);
                                  [3    0.99]
                                  [         ]
                                  [5    0.9 ]
                                  [         ]
                                  [2    0.85]
                                  [         ]
                                  [6    0.8 ]

 

The real challenge is making this reasonably fast.  Here is my first attempt, not particularly fast.  This isn't the nicest approach, from a pedagogical viewpoint.

Harmonic := module()
export ModuleApply;
local nmax;
    nmax := 1;
    ModuleApply := proc(n::posint)
    option remember;
    local k;
        procname(nmax) + add(1/k,k=nmax+1..n);
    end proc;
    ModuleApply(1) := 1;
end module:

A := proc(n::posint)
local k;
    for k while Harmonic(k) < n do end do;
    return k;
end proc:

harmonic1 := proc(n)
    Vector['row'](n, A);
end proc:

harmonic1(3);
                  [1, 4, 11]
 

 

The procedure you want is dsolve, not solve.  Also, you need to use y(x), not y, as the dependent variable:

ode := diff(y(x), x) = y(x)^2*exp(x);
dsolve(ode,y(x));                    
                                                  1
                                    y(x) = - ------------
                                             exp(x) - _C1

You can include an initial condition so that dsolve will solve for the integration constant:

 dsolve({ode,y(0)=y0}, y(x));         
                                                    y0
                                     y(x) = - ------------------
                                              exp(x) y0 - 1 - y0

You could use fsolve, which permits specifying a range:

fsolve((p+1)^2-p^2, p=0..infinity);
                                                           NULL
fsolve((p+1)^2-p^2, p=-infinity..0);          
                                                      -0.5000000000

Your loop is assigning a procedure, not a value. Try
b := Array(0..500, 0..500):
for i from 0 to 500 by 2 do
   b[i,0] := i!/...;
end do:

Solving this by hand is trivial.  Just split the sum into two sums and adjust the second summand so that it cancels all all but the first term of the first.  Of course, you need to demonstrate that the sum actually converges, but you should know how to do that (Maple can be helpful here).

 

The easiest way to do this is probably

C := c[0]*sqrt(x):
C*expand(y1/C);

Note that you should be using add rather than sum. Regardless, I don't understand why your computation has numerical coefficients; maybe you cut and pasted something else? Oh, I see, your c must have been defined in terms of c[0].  I'm surprised that worked, since c[0] is referenced...

You cannot readily do so.  Well, you could if you reassigned print, but I only recommend that when absolutely necessary. What you really want to do [from your previous posts] is to save data generated in a loop so that you can use it outside the loop.  One way to do that is to insert the data into a table.  For example

T := table():
for i to 10 do
   data := <generate data>:
   T[i] := data;
end do:
A := convert(T, 'list');

 

I'm not sure what you want, but the following code might do just that:

A:=[[a,b],[b,c],[b,c,c],[d,c,c]]:
f := proc(a,b) evalb(a[2..]=b[2..]) end proc:
ListTools:-MakeUnique(A,f);
                                  [[a, b], [b, c], [b, c, c]]

Note that `equal' is not a Maple procedure. also RETURN is deprecated (use return).

Why not just enclose the section in a do loop, terminated with a colon (so the lines do not automatically generate printed output)  and add a print statement to the line that generates the output?  For example:

to 10 do
  < your code here >
  print(%); # print the last value
end do:

One approach is

T := combinat:-cartprod([[a,b],[c,d]]):
while not T[finished] do combinat:-permute(T[nextvalue]()); end do;

To instantiate a cartesian produce at once, you might look at http://www.mapleprimes.com/book/cartesian-products-of-lists.

Here's a brute force solution.  To reduce the solution space, I solved for a pan-magic square; in addition to the usual constraints, the sums of all the broken diagonals must also equal the magic number.

PanMagic := proc(L,n::posint)
local i,j,M,rng;
    rng := 0..n-1;
    M := (n^3+n)/2;
    [seq(s=M, s in [NULL
                    , seq(add(L[i,j], j=rng), i=rng)
                    , seq(add(L[i,j], i=rng), j=rng)
                    , seq(add(L[i,modp(i+j,n)],i=rng), j=rng)
                    , seq(add(L[i,modp(j-i-1,n)],i=rng), j=rng)
                   ])];
end proc:

to 1 do
    n := 4:
    eqs := PanMagic(L,n);
    sol := solve(eqs);
    vals := map(rhs,sol);
    vars := convert(indets(vals),'list');
end do:

all := {seq(1..n^2)}:

P := combinat:-permute(convert(all,'list'),nops(vars)):

found := false:
for p in P do
    eqs := Equate(vars,p);
    if subs(eqs, vals) = all then
        found := true;
        break;
    end if;
end do:

if found=true then
    M := subs(sol,eqs, Matrix(n,n,(i,j) -> L[i-1,j-1]));
end if;

                               [ 1     8    10    15]
                               [                    ]
                               [12    13     3     6]
                          M := [                    ]
                               [ 7     2    16     9]
                               [                    ]
                               [14    11     5     4]

You should add some context to that question. 

Do you want to tally the occurrences of each distinct n-tuple?  Here is a simple way to do so.  First, I'll create the data (I'm using pairs, the extension to any n-tuple is clear).

L := [[a,b],[a,b],[b,a],[a,a]]:

We cannot do

Statistics:-Tally(L);
                                [a = 5, b = 3]

However, if we wrap each sublist in an unevaluated function call, Tally then tallys the functions and not the elements of the sublists. A neat way to do that is with the empty function, ``, followed by a call to expand, which expands ``(x) into x:

expand(Statistics:-Tally(map(``,L))); 
                      [[a, a] = 1, [b, a] = 1, [a, b] = 2]

Is that what you want?
 


 

 

It is not clear what you want.

First 90 91 92 93 94 95 96 Last Page 92 of 114