Carl Love

Carl Love

27343 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

Just guessing here, because I don't have time to test this right now. I think that you need to divide each color number by 256, and possibly do an evalf also, to put it in the 0..1 floating-point form that Maple plots expect.

Code occuring inside procedure or function definitions is not evaluated until the procedure is called. In your case, the i is not being evaluated at the time you create the arror (->) expressions. The way around this is to use ?unapply , which evaluates the expression first and then makes it into a procedure.

procVec3:= proc(U::~Vector(realcons))::list(procedure);
local i::posint, u::symbol;
 
     [seq](unapply(piecewise(u < U[i-1], 0, u < U[i], 1), u), i= 2..numelems(U));
end proc;

Some other pointers:

  1. It is a bad practice (very inefficient) to assign to list entries, although Maple will begrudingly allow it for lists with fewer than 101 elements. The alternative is to assign to a Vector, table, or Array. These can be easily converted to a list when you're done assigning.
  2. There is no need to contruct a list or to initialize its entries to 0 before assigning it or to its entries. Vectors and Arrays need to be constructed (and are automatically initialized to 0). It's a good idea to construct a table before assigning, though not strictly necesary. Tables are not initialized to 0 unless sparse is specified in the constructor.
  3. There is no need to return something which was the last expression evaluated; it is automatically returned.
  4. It is a bad idea to use explicit compound conditions (e.g. conditions with and or or) directly in piecewise. Compound conditions can be expressed in inert form, And(..., ...), or just use the natural left-to-right number-line order like I did above (see ?piecewise ).
  5. There is no need to declare the type of every variable, although it will help with debugging. In the above procedure, I went a bit overboard with the type declarations just to show what is possible.

How about a single command rather than a do loop?

f||(1..99):= 'randpoly([y||(1..99)])+`+`(y||(1..99))' $ 99:

Now your functions are named f1, f2, ..., f99.

As Markiyan said, these variables are not subscripted, strictly speaking.

If you expand the expressions "by hand", you will see that all the t's cancel.

t*x + (1-t)*x = t*x + x - t*x = x.

I will give an example of inputting a piecewise function, but I don't know what you mean by "solve a piecewise function". The general pattern is

piecewise(cond1, piece1, cond2, piece2, ..., condN, pieceN, piece_otherwise);

where cond1, ..., condN are Boolean expressions (typically simple inequalities in one variable), and piece1, ..., pieceN, and piece_otherwise are algebraic expressions (typically with the same variable being considered the independent variable).

Example:

f:= piecewise(x < 0, x^2, x < 1, x+1/2, x < 2, 1, 1.25);
plot(f, x= -1..3, discont, symbol= solidcircle, symbolsize= 15);

Note that at each condition, going left to right in the command, it is assumed that the conditions to the left have already been rejected, so the second condition in the example can be simply x < 1; there's no need to make it a compound condition such as 0 <= x and x < 1. (If you must enter compound conditions, see ?piecewise for details on the inert form required.)

Once you have f input, you can use it like any other algebraic expression in Maple: You can incorporate it into a more-complicated expression, and you can apply commands such as solve, diff, int, limit etc., and, to a limited extent, dsolve.

Assign the solution from QPSolve to a variable. For example,

Sol:= QPSolve(...);

Now, whenever you want one of the variables from Sol, use eval(... , Sol[2]); For example,

x1:= eval(x1, Sol[2]);

or

eval(y, Sol[2]) + eval(z, Sol[2]);

Just place a list of 6 colors in the cuboid command:

rand256:= rand(0..255):
RandColor:= ()-> COLOR(RGB, evalf(['rand256()/256' $ 3])[]):
C:= plottools:-cuboid([0$3], [1$3], color= ['RandColor'() $ 6]):
plots:-display(C, transparency= 0.5, axes= none);

In your eq16 you missed transcribing the leading minus sign from eq15.

Continuing from that point, you should have

eq16:= subs(C= -hmax*ln(-hmax), eq13);
eq17:= solve(eq16, h);

#Include initial condition in dsolve solution.
eq11_1:= dsolve({eq10, h(0)=0}, implicit);
eq12_1:= solve(eq11_1, h(t));

simplify(eq12_1 - eq17);

And you should get 0 (I did) indicating that the two methods are equivalent.

@esbestan 

M is a matrix that you can work with. But the interface, by default, does not display large matrices, even though the matrix still exists in the computer's memory. You can change the default behavior with the command

interface(rtablesize= infinity);
M;

and you will see all of M. You can also view submatrices. For example, to see the 1st through 10th rows and the 10th through 20th columns,

M[1..10, 10..20];

Well, obviously, you can't just use ... and expect Maple to figure out the pattern; you have to use seq. But there is a problem with using the seq with the vertical bars | ... |. To get around this, we use the vertical bar Matrix constructor in prefix form, `<|>`(...), like this:

restart:
m:= 32:
for i from 0 to m-1 do  h[i]:= unapply(t^i)  end do:
H:= unapply(< seq(h[i](t), i= 0..m-1) >, t):
M:= `<|>`(seq(H(i/m), i= 0..m-1));

Erik,

Here's a complete example of using the recursive procedure and generating the output with evalhf. I used the sum-of-squares for the first real-valued evaluation function, and the sum-of-cubes for the second.

Combi:= module()
export
    boxContent,  #A vector whose i'th entry holds the number of marbles in the i'th box
    Output       #Matrix of Output value
;
local
    Combi:= proc(
        k::integer, Combi:= module()
        boxContent::Vector,
        activeBox::integer,
        marblesRemaining::integer,
        Output::Matrix,
        Index::Vector
    )       
    local i::integer;
        
        if activeBox < k then
            for i from 0 to marblesRemaining do
                boxContent[activeBox]:= i;
                thisproc(k, boxContent, activeBox+1, marblesRemaining-i, Output, Index)
            end do
        else
            boxContent[activeBox]:= marblesRemaining;
            Index[1]:= Index[1] + 1;
            #Something:
            Output[1, Index[1]]:= add(boxContent[i]^2, i= 1..k);
            Output[2, Index[1]]:= add(boxContent[i]^3, i= 1..k);        
        end if;
    end proc,

    ModuleApply:= proc(n::posint, k::posint)
    local
        Index,
        boxContent  #A vector whose i'th entry holds the number of marbles in the i'th box
    ;
        boxContent:= Vector(k, datatype= float[8]);
        Output:= Matrix(2, binomial(n+k-1, k-1), datatype= float[8]);
        Index:= Vector(1);
        evalhf(Combi(k, boxContent, 1, n, Output, Index));
        [][]
    end proc
;
end module;

And the worksheet:


A recursive procedure

restart

with(Statistics):

n := 40:

k := 6:

 

module () local Combi, ModuleApply; export boxContent, Output; end module

NULL

B := CodeTools[Usage](Combi(n, k));

memory used=18.65MiB, alloc change=18.64MiB, cpu time=3.45s, real time=3.45s

Combi:-Output[1, 1221759];

HFloat(1600.0)

Combi:-Output[2, 1221759];

HFloat(64000.0)

``


Download A_recursive_procedu.mw

From your worksheet, I think that you've already noticed that this is a bug related to the strange way that you created V, which is equivalent to this shortened form:

restart:
v1:= <a>: v2:= <b>:
V:= Vector(2, [v1,v2]):
subs(a=5, V);

The workaround is to create V like this:

V:= <v1,v2>:
subs(a=5, V);

RootPlot:= (p::polynom)->
     plot(map([Re,Im], [fsolve](p, indets(p,name)[], complex)), style= point,  _rest):

RootPlot(1+randpoly(x, degree= 2^7), color= green, symbol= diamond);

The command StringTools:-ParseTime (see ?StringTools,ParseTime ) can be used on the timestamps, like this (ignoring the time zone):

restart:
timestamp:= "2013-08-13T00:29:24+0000": #Note the quotes
timerecord:= StringTools:-ParseTime("%Y-%m-%dT%T", timestamp):

This produces a structure called a ?Record which holds then numeric values for year, month, etc.

baseyear:= 2012:  basemonth:= 9:  #For example
CountMonths:= TR-> 12*(TR:-year - baseyear) + TR:-month - basemonth:
CountMonths(timerecord);
                               11
Now this gives you a way to measure distance on the horizontal axis.

The command ListTools:-Classify can be used to count the photos, texts, etc., for each month (see ?ListTools,Classify ).

See ?plot,tickmarks for information about changing the tickmarks on the horizontal axis.

See ?plots,polygonplot to plot the colored rectangles.

 

The problem is that you did not declare an Array, so gg became a table. But, gg cannot be a Vector (or a list) because it is indexed from 0 instead of 1. It has to be an Array or a table.

You can create an Array and fill it in one step:

gg:= Array(0..7, i-> t-> t^i):

If you want to apply all the functions in gg at once, you can do this (I am using 2 as the argument):

gg2:= map(`?()`, gg, [2]);

gg2[5];
                          32

First 350 351 352 353 354 355 356 Last Page 352 of 390