Carl Love

Carl Love

28055 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

You need to put the if statement inside the procedure, like this:

MARK:= (a,b,c,d)->
    if a.d-b.c = 0 then
         [0,0,0,0]
    else
         [d/(a.d-b.c), -b/(a.d-b.c), -c/(a.d-b.c), a/(a.d-b.c)]
    fi;

The print statement is not needed. Indeed, print is never an appropriate way for a procedure to return its output.

restart:
`mod/ReduceExponents`:= (p::polynom, n::posint)->
     evalindets(p, '`^`'(name, posint), T-> op(1,T)^(op(2,T) mod n)):
p:= x-> 1+x^3+x^17+x^19:
ReduceExponents(p(x)) mod 17;

ArrayInterpolation will not accept exact integer values; they must be converted to floating-point values with evalf. Also, do not use with inside a procedure; use uses instead. And I made your variables local. A will be the return value of the procedure.

InterProc := proc ()
uses CurveFitting;
local i, A, variable1, variable2;
     A := Matrix(10, 4);
     variable1 := evalf([0, 100]);
     variable2 := evalf([12, 20]);

     for i from 1 to 10 do
          A(i, 1) := evalf(3+i);
          A(i, 2) := 2*i+A(i, 1);
          A(i, 3) := A(i, 2)-1;
          A(i, 4) := ArrayInterpolation(variable1, variable2, A(i, 1));
     end do;
end proc;

Three errors I see:

  1. "if" must be with a lowercase "i".
  2. Need a semicolon at the end of the first line.
  3. The denominators need to be in parentheses: (a*d - b*c).

You should make your code into a procedure, like below. There is no need for the print command.

 

restart:

Inverse:= proc(M::Matrix(2,2))
local a,b,c,d,D;
    (a,b,c,d):= convert(M,list)[];
    D:= a*d-b*c;
    if D=0 then
         error "Matrix is not invertible."
    else
         < < d, -b > | < -c, a > > / D
    end if
end proc:

M:= < < a, b > | < c , d > >;

M := Matrix(2, 2, {(1, 1) = a, (1, 2) = c, (2, 1) = b, (2, 2) = d})

Inverse(M);

Matrix(2, 2, {(1, 1) = d/(a*d-b*c), (1, 2) = -c/(a*d-b*c), (2, 1) = -b/(a*d-b*c), (2, 2) = a/(a*d-b*c)})

M.Inverse(M);

Matrix(2, 2, {(1, 1) = a*d/(a*d-b*c)-b*c/(a*d-b*c), (1, 2) = 0, (2, 1) = 0, (2, 2) = a*d/(a*d-b*c)-b*c/(a*d-b*c)})

simplify(%);

Matrix(2, 2, {(1, 1) = 1, (1, 2) = 0, (2, 1) = 0, (2, 2) = 1})

M:= Matrix(2,2);

M := Matrix(2, 2, {(1, 1) = 0, (1, 2) = 0, (2, 1) = 0, (2, 2) = 0})

Inverse(M);

Error, (in Inverse) Matrix is not invertible.

M:= LinearAlgebra:-RandomMatrix(2,2);

M := Matrix(2, 2, {(1, 1) = 44, (1, 2) = -31, (2, 1) = 92, (2, 2) = 67})

Inverse(M);

Matrix(2, 2, {(1, 1) = 67/5800, (1, 2) = 31/5800, (2, 1) = -23/1450, (2, 2) = 11/1450})

M.Inverse(M);

Matrix(2, 2, {(1, 1) = 1, (1, 2) = 0, (2, 1) = 0, (2, 2) = 1})

 

 

Download Inverse.mw

 

Use the function form on input, delta(x) and Delta(x), and define the following procedures, perhaps in your initialization file:

`diff/delta`:= x-> delta(x):
`diff/Delta`:= x-> Delta(x):
`print/delta`:= x-> delta*x:
`print/Delta`:= x-> Delta*x:

Now the function form will display as the multiplication form, it won't be affected by taking the derivative, and simplify will only see the function form and won't change it.

Your first line is x:= x0. That should be x0:= x.

Then you'll have a problem because your last two break statements are not in a loop. I suggest that you change your procedure to something like this:

proc_r:= proc(x :: realcons)
local x0;
     x0:= evalf(x);

     #reducing the value to between -Pi and Pi
     while x0 > evalf(Pi) do  x0:= x0 - 2*evalf(Pi)  end do;
     while x0 < evalf(-Pi) do  x0:= x0 + 2*evalf(Pi)  end do;

     #using the symmetry of sin to reduce to between -Pi/2 and Pi/2
     if x0 > evalf(Pi/2) then  x0:= evalf(Pi) - x0
     elif x0 < evalf(-Pi/2) then  x0:= evalf(-Pi) - x0
     end if;

     return x0
end proc:

proc_r(7);  
                                   0.71681469282042

If M is your Matrix, then do

ArrayTools:-AddAlongDimension(M,2);

@Andriy Here is your file with my modifications. All I did was change q and Np from globals into parameters. Please check that this generates the correct matrix, and compare the time to sequential code on a reasonably sized example. The Grid option will no doubt take longer on an example that is too small.

MatrElems_Carl1.mw

plot([cosh(x), x^2], x= -2..2);
f:= x-> x^2:
r:= fsolve(cosh(x)=x^2, x);
[r, f(r)], [-r, f(-r)];

I don't know what the ultimate goal is, so I am not sure that this is correct. But surely ormap is a better way to add a Vector to an Array of Vectors iff it is not already in the Array. This code increases the size of the Array by 1 each time a new entry is added.

proc_cerny1:= proc(A::Matrix, B::Matrix, C::Vector[row], N::nonnegint)
local x:= 2^N, S:= Array(0..0, fill= C), i, j:= 1, T, R;
     for i from 0 to x-1 while i <> j do
          T:= S[i].A;
          if not ormap(LinearAlgebra:-Equal, S, T) then
               j:= j+1;
               S(j):= T #not S[j]
          end if;

          R:= S[i].B;
          if not ormap(LinearAlgebra:-Equal, S, R) then
               j:= j+1;
               S(j):= R
          end if
     end do;
     S
end proc:

My solution allows you to create, display, and assign the sequence all in one step. It also works for lists and sets. You only need to append a ! to the object. I chose ! because it is reminiscent of a column. This does not affect the usage of ! for factorials.

This version only works in Maple 17. If you need it for earlier Maple, I can make some slight modifications.


restart:

local `!`:= overload([
     proc(a::anything, b::anything)
     option overload;
     local x:= b; #Force a check for a second argument
          seq(print(x, ``), x= [args[1..-2]]);
          print(args[-1]);
          args
     end proc,

     proc(L::{list, set})
     option overload;
     local x;
          seq(print(x, ``), x= L[1..-2]);
          print(L[-1]);
          L
     end proc,

     factorial
]):
          

S:= (A,B,C)!:

A, ``

B, ``

C

S;

A, B, C

S:= [a,b,c]!:

a, ``

b, ``

c

S;

[a, b, c]

S:= {a,b,c}!:

a, ``

b, ``

c

S;

{a, b, c}

Factorials still work.

n!, 4!;

factorial(n), 24

 


Download overload_!.mw

You have a first-order ODE, so dsolve will only allow one condition, even though the presence of the a should allow for another condition. We can fool dsolve into accepting a second condition by making a a constant function, i.e. a function whose derivative is 0, and then treating the system as a BVP.

restart:
fx:= diff(n(x),x)-a(x), diff(a(x),x) :
eval(n(x), dsolve({fx, n(0) = 1, n(1) = 2}, {a(x), n(x)}));

constants:= ({constants} minus {gamma})[];

A procedure is a mutable data structure, just like a table, Array, or module.

Use applyrule instead of algsubs:

applyrule(P^q + R_(n+1)^q = B, %);

First 321 322 323 324 325 326 327 Last Page 323 of 395