Joe Riel

9530 Reputation

23 Badges

20 years, 24 days

MaplePrimes Activity


These are answers submitted by Joe Riel

M := <<1|2>,<3|4>>:
LinearAlgebra:-MatrixPower(M,4);
                                     [199    290]
                                     [          ]
                                     [435    634]


String matching might be the best way (I used 10,000 digits here):

module()
local pi := cat("3",convert(evalf[10\000](Pi),'string')[3..-1]);
    TypeTools:-AddType(inpi
                       , proc(n)
                           n :: 'nonnegint'
                           and (StringTools:-Search(convert(n,'string'),pi) <> 0)
                       end proc
                      );
end module:

type(14122,inpi);
                                true
type(141222,inpi);
                                false

Note that there is a typo in ID4dof.mws:

Test := MatrixInverse(G11-P1):

should be

Test := MatrixInverse(G11-Pt):

also the commented-out line that follows has a typo, "Multply". 

That didn't solve your problem...

Use the unapply procedure to convert an expression into an operator:

with(stats):
does_work:= proc(data)
local d0,d1,x,y;
    unapply(rhs(fit['leastsquare'[[x,y], y=d0+d1*x]](data)),x);
end proc:
f := does_work([[0,1,2],[3,4,5]]);                        
                       f := x -> 3 + x

f(a);
                       3+a

Note that the stats package has been supreceded by the Statistics package.  Using that package you could do

does_work:= proc(data)
local x;
    unapply(Statistics:-LinearFit([1,x],data[],x),x);
end proc:

f := does_work([[0,1,2],[3,4,5]]);
             f := x -> 3.00000000000000044 + 0.99999999999999988 x


f(a);
                  3.00000000000000044 + 0.99999999999999988 a

If you don't like the floating point values, you could call convert/rational before unapply:
 
does_work:= proc(data)
local f,x;
    f :=Statistics:-LinearFit([1,x],data[],x);
    unapply(convert(f,'rational'),x);
end proc:

f := does_work([[0,1,2],[3,4,5]]);
                                f := x -> 3 + x


f(a);
                                     3 + a

Assign L the list of values (1's and 0's). Then do

remove(Testzero, map(nops, [ListTools:-Split(`=`, L, 1)]));

That returns a list of integers corresponding to the length of each sequence of consecutive 0's.
 

You can do

interface(prettyprint=0):

The downside is that the output is then left aligned, and harder to read. If you only want to convert a particular expression, you can use the procedure lprint.

Weird...my browser won't permit copying the Maple code that you entered.  I never had that problem before.

I don't understand why you are specifying a range from 0 to infinity.  The solution is near 0. 

The subsindets procedure can be used here:

isolate(subsindets(eq,integer,``),x);

ImageTools:-View uses maplets to do just that:
 
use ImageTools in View(Checkerboard()) end use;

To seed Maple's random number generator, use the randomize function:

randomize(100):                                      
RandomTools:-Generate(list(integer(range=1..10),10));
                                    [9, 9, 4, 8, 8, 1, 5, 3, 6, 3]

RandomTools:-Generate(list(integer(range=1..10),10));
                                    [3, 3, 2, 1, 9, 5, 1, 10, 7, 3]
# reseed, so result is identical to first call
randomize(100):                                      
RandomTools:-Generate(list(integer(range=1..10),10));
                                    [9, 9, 4, 8, 8, 1, 5, 3, 6, 3]

alias(u = u(x,y)):
 diff(u,x);
                                                 d
                                                 -- u
                                                 dx

diff(u,y);
                                                 d
                                                 -- u
                                                 dy

There is a Bits package:

Bits:-And(12,6);  
                                                   4

You could use patmatch:

ans := 401/2 - 1880/(3*Pi);
patmatch(ans, ab::numeric + cd::numeric/Pi, 's');
                                                       true
(a,b) := (numer,denom)(eval(ab, s));
                                   a, b := 401, 2
(c,d) := (numer,denom)(eval(cd, s));
                                   c, d := -1880, 3

LongestIncreasingSeq := proc(L::list)
local n,G,C;
uses GraphTheory;
    n := nops(L);
    G := Digraph(n
                 , {seq(seq(`if`(L[i]<L[j]
                                 , [i,j]
                                 , NULL
                                )
                            , j = i..n)
                        , i = 1..n-1)}
                );
    C := MaximumClique(G);
    map(op, C, L);
end proc:
Because L is restricted to distinct integers, we can slightly shorten this:
LongestIncreasingSeq := proc(L::list)
local i,j,n;
uses GraphTheory;
    n := nops(L);
    MaximumClique(Digraph(L, {seq(seq(`if`(L[i]<L[j]
                                           , [L[i],L[j]]
                                           , NULL
                                          )
                                      , j = i..n)
                                  , i = 1..n-1)}
                         ));
end proc:

This seems straightforward, that is, no recursion and should be O(n).  I only implemented a test for increasing sequences, but the decreasing one is obvious.  This can be simplified, since it actually returns the sequence and not just the length.

LongestSeq := proc(L :: list(numeric))
local leng,maxlengthth,i,x,xp,endpos;

    if L = [] then return []; end if;
    leng := 1;
    maxlength := 1;
    x := L[1];

    for i from 2 to nops(L) do
        xp := x;
        x := L[i];
        if xp < x then
            leng := leng+1;
        else
            if leng > maxlength then
                maxlength := leng;
                endpos := i-1;
            end if;
            leng := 1;
        end if;
    end do;

    if leng > maxlength then
        maxlength := leng;
        endpos := -1;
    end if;

    L[endpos-maxlength+1..endpos];

end proc:

First 97 98 99 100 101 102 103 Last Page 99 of 114