Joe Riel

9660 Reputation

23 Badges

20 years, 3 days

MaplePrimes Activity


These are replies submitted by Joe Riel

That depends on what you mean by the sine of a Matrix. Do you mean the sine of each element in the matrix? Or, for a square matrix M, do you mean
 sin(M) = M - 1/3!*M^3 + 1/5!*M^5 + ... ?
For the former, use the map function. For the latter use MatrixFunction in the LinearAlgebra package.
It's easy enough to write one.
MovingAvg := proc(vals :: list, n :: posint)
local i, num, avg, sum;
    num := nops(vals);
    avg := Vector(num);
    sum := 0;
    for i to min(n,num) do
        sum := sum + vals[i];
        avg[i] := sum/i;
    end do;
    for i from n+1 to num do
        sum := sum + vals[i] - vals[i-n];
        avg[i] := sum/n
    end do;
    convert(avg,list);
end proc:
Nice write up. A few minor points. The @ command is not builtin, which slows this down a bit. I think you'll find the following slightly faster:
transpose2 := proc(A)
    Matrix(op([1,2],A), op([1,1],A)
           , map(eq -> (op([1,2],eq),op([1,1],eq)) = op(2,eq)
                 , op(2,A))
           , 'storage'='sparse')
end proc:
If you want to inline that (not sure why) you can rewrite reverse so the @ is not required.
reverse2 := proc(eq) option inline; (op([1,2],eq),op([1,1],eq)) = op(2,eq) end proc:
transpose3 := proc(A) option inline;
    Matrix(op([1,2],A), op([1,1],A)
           , map(reverse2, op(2,A))
           , 'storage'='sparse')
end proc:
It may be marginally faster than transpose2. There is a subtle bug in the original assignment of transpose. It doesn't work if the Matrix contains sequences (unlikely, but possible). Consider
M := Matrix(1,2, {(1,1)=(a,b), (1,2)=(x,y)});
transpose(M);
                                      [a]
                                      [ ]
                                      [x]
transpose2(M);
                                   [a    b]
                                   [      ]
                                   [x    y]
I'm sorry to hear the conjecture was disproved. Just mentioning this site (mapleprimes) is fine by me. Roman Pearce made the initial suggestion, so you might want to see what he prefers for an attribution.
I'm sorry to hear the conjecture was disproved. Just mentioning this site (mapleprimes) is fine by me. Roman Pearce made the initial suggestion, so you might want to see what he prefers for an attribution.
There is a typo in my computation of fy. Should be
fy = diff(f,y):
There is a typo in my computation of fy. Should be
fy = diff(f,y):
The critical point are those points at which all partial derivatives equal zero. To solve for them, you can do
fx := diff(f,x):
fy := diff(y,y):
solve({fx,fy}, {x,y} ):
where f is the expression of interest. Somewhat shorter is
solve(map2(diff,f,[x,y]),[x,y]);
The critical point are those points at which all partial derivatives equal zero. To solve for them, you can do
fx := diff(f,x):
fy := diff(y,y):
solve({fx,fy}, {x,y} ):
where f is the expression of interest. Somewhat shorter is
solve(map2(diff,f,[x,y]),[x,y]);
conjecture := proc(p)
    module()
    export alpha,epsilon;
        alpha := proc(i)
        option remember;
            if alpha(i-1) + epsilon(i-1) + epsilon(i-2) <= 1 then
                0
            else 1
            end if;
        end proc;

        alpha(1) := 1;
        alpha(2) := 0;

        epsilon := proc(i)
        option remember;
            if i < p+1 then
                modp(i,2);
            else
                modp(epsilon(i-p) - epsilon(i-1) - alpha(i), 2);
            end if;
        end proc;
        epsilon(p+1) := epsilon(p);
    end module;
end proc:
Something I forgot to mention. The first technique, which uses the global variable p, gives the wrong result if the global variable p is changed. That's because the remember table does not depend on the global variable. The second technique (the only one shown in this post) doesn't have that problem.
conjecture := proc(p)
    module()
    export alpha,epsilon;
        alpha := proc(i)
        option remember;
            if alpha(i-1) + epsilon(i-1) + epsilon(i-2) <= 1 then
                0
            else 1
            end if;
        end proc;

        alpha(1) := 1;
        alpha(2) := 0;

        epsilon := proc(i)
        option remember;
            if i < p+1 then
                modp(i,2);
            else
                modp(epsilon(i-p) - epsilon(i-1) - alpha(i), 2);
            end if;
        end proc;
        epsilon(p+1) := epsilon(p);
    end module;
end proc:
Something I forgot to mention. The first technique, which uses the global variable p, gives the wrong result if the global variable p is changed. That's because the remember table does not depend on the global variable. The second technique (the only one shown in this post) doesn't have that problem.
Your description is imprecise. There are a few typos (eps instead of epsi). Also it isn't clear whether in the recursive definition of epsi you meant the (mod 2) to be applied to the entire expression or just the last term (alpha(i)). I assumed the former, so epsilon (my name) returns 0 or 1. Following are two implementations. Simple Approach
alpha := proc(i)
option remember;
    if alpha(i-1) + epsilon(i-1) + epsilon(i-2) <= 1 then
        0
    else 1
    end if;
end proc;

alpha(1) := 1;
alpha(2) := 0;

epsilon := proc(i)
global p;
option remember;
    if i < p+2 then
        modp(i,2);
    else
        epsilon(i) := modp(epsilon(i-p) - epsilon(i-1) - alpha(i), 2);
    end if;
end proc;

p := 4:
seq(epsilon(i),i=1..20);
          1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0
Modular Approach This is nearly the same, but uses a constructor that takes the argument p and returns a module with two exports, alpha and epsilon.
conjecture := proc(p)
    module()
    export alpha,epsilon;

        alpha := proc(i)
        option remember;
            if alpha(i-1) + epsilon(i-1) + epsilon(i-2) <= 1 then
                0
            else 1
            end if;
        end proc;

        alpha(1) := 1;
        alpha(2) := 0;

        epsilon := proc(i)
        option remember;
            if i < p+2 then
                modp(i,2);
            else
                epsilon(i) := modp(epsilon(i-p) - epsilon(i-1) - alpha(i), 2);
            end if;
        end proc;

    end module;

end proc:

conj := conjecture(4);
seq(conj:-epsilon(i), i = 1..200);
Your description is imprecise. There are a few typos (eps instead of epsi). Also it isn't clear whether in the recursive definition of epsi you meant the (mod 2) to be applied to the entire expression or just the last term (alpha(i)). I assumed the former, so epsilon (my name) returns 0 or 1. Following are two implementations. Simple Approach
alpha := proc(i)
option remember;
    if alpha(i-1) + epsilon(i-1) + epsilon(i-2) <= 1 then
        0
    else 1
    end if;
end proc;

alpha(1) := 1;
alpha(2) := 0;

epsilon := proc(i)
global p;
option remember;
    if i < p+2 then
        modp(i,2);
    else
        epsilon(i) := modp(epsilon(i-p) - epsilon(i-1) - alpha(i), 2);
    end if;
end proc;

p := 4:
seq(epsilon(i),i=1..20);
          1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0
Modular Approach This is nearly the same, but uses a constructor that takes the argument p and returns a module with two exports, alpha and epsilon.
conjecture := proc(p)
    module()
    export alpha,epsilon;

        alpha := proc(i)
        option remember;
            if alpha(i-1) + epsilon(i-1) + epsilon(i-2) <= 1 then
                0
            else 1
            end if;
        end proc;

        alpha(1) := 1;
        alpha(2) := 0;

        epsilon := proc(i)
        option remember;
            if i < p+2 then
                modp(i,2);
            else
                epsilon(i) := modp(epsilon(i-p) - epsilon(i-1) - alpha(i), 2);
            end if;
        end proc;

    end module;

end proc:

conj := conjecture(4);
seq(conj:-epsilon(i), i = 1..200);
I'm adding a closing bold tag here
On reflection, if expr is a product, then there is no point in first splitting it into a list and recombining. Just do
breakApart := proc(expr::`*`, dep_var, const_name)
local dep_term, indep_term;
    (dep_term, indep_term) := selectremove(depends, expr, dep_var);
    return ((const_name = indep_term)
            ,const_name*dep_term);
end proc:
First 183 184 185 186 187 188 189 Last Page 185 of 195