Carl Love

Carl Love

28035 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

Why have you put m3 in square brackets? That's the problem.

Also with*matrix and with*linearalgebra don't mean anything. Get rid of those.

Try changing Do(HD= HuffmanCoding(HT)) to
Do(HD= [HuffmanCoding(HT)]).

The reason is that I doubt that Do can handle the assignment of an expression sequence with more than one term, which is what the output of HuffmanCoding will be.

Here's an example:

restart:

 

First I'll choose some random words as our example strings.

L||(1..3):= 'combinat:-randcomb(EssayTools:-GetWordList(), 10)' $ 3:

 

Construct the DataFrame.

ds:= DataFrame(<<L2> | <L3>>, rows= L1, columns= [$1..2]);

ds := Matrix(10, 3, {(1, 1) = ``, (1, 2) = 1, (1, 3) = 2, (2, 1) = "alphard's", (2, 2) = "gathering's", (2, 3) = "understatement's", (3, 1) = "fronde", (3, 2) = "recharges", (3, 3) = "pterodactyl's", (4, 1) = "estimations", (4, 2) = "headlock", (4, 3) = "axial", (5, 1) = "cannoned", (5, 2) = "balded", (5, 3) = "pickaxed", (6, 1) = "num5", (6, 2) = "underlay", (6, 3) = "stewing", (7, 1) = "pagination's", (7, 2) = "quizzed", (7, 3) = "curmudgeon", (8, 1) = "elma's", (8, 2) = "periwinkle", (8, 3) = "outerwear's", (9, 1) = "keller", (9, 2) = "changchun", (9, 3) = "orion's", (10, 1) = `...`, (10, 2) = `...`, (10, 3) = `...`})

Select all rows with a column 1 entry greater than "n":

ds[ds[..,1] >~ "n"];

Matrix([[``, 1, 2], ["fronde", "recharges", "pterodactyl's"], ["num5", "underlay", "stewing"], ["pagination's", "quizzed", "curmudgeon"], ["elma's", "periwinkle", "outerwear's"], ["overeats", "wahhabi", "coaxial"], ["shopping's", "sideswipes", "reformation's"]])

Select all rows whose column 2 entry ends with "'s"

ds[(w-> evalb(w[-2..-1]="'s"))~(ds[..,2])];

Matrix([[``, 1, 2], ["alphard's", "gathering's", "understatement's"], ["fronde", "recharges", "pterodactyl's"], ["elma's", "periwinkle", "outerwear's"], ["keller", "changchun", "orion's"], ["shopping's", "sideswipes", "reformation's"]])

 

 

Download DataFrame_string_filter.mw

However, it's a disappointment that the commands select, remove, and selectremove haven't been overloaded to work with DataSeries and DataFrames.

Here's an easy example that you should be able to execute on your own computer.

restart:

filename:= FileTools:-JoinPath(["example/address.json"], base= datadir);

"C:\Program Files\Maple 2016\data\example/address.json"

T:= JSON:-ParseFile(filename, output= table);

table( [( "phoneNumbers" ) = [table( [( "type" ) = "local", ( "number" ) = "+1 (519) 747-2373" ] ), table( [( "type" ) = "tollfree", ( "number" ) = "+1 (800) 267-6583" ] ), table( [( "type" ) = "fax", ( "number" ) = "+1 (519) 747-5284" ] )], ( "companyName" ) = "Maplesoft", ( "address" ) = table( [( "city" ) = "Waterloo", ( "country" ) = "Canada", ( "streetAddress" ) = "615 Kumpf Drive", ( "province" ) = "ON", ( "postalCode" ) = "N2V 1K8" ] ), ( "founded" ) = 1988 ] )

 

The following transformation makes the table easier to visualize. (This command is very advanced, and I don't expect you to understand it.)

(T-> subsindets(subsindets(`<,>`(op(T)), table, thisproc), list, `<,>`))(T);

Vector(1, {(1) = Vector(4, {(1) = "phoneNumbers" = (Vector(3, {(1) = Vector(2, {(1) = "type" = "local", (2) = "number" = "+1 (519) 747-2373"}), (2) = Vector(2, {(1) = "type" = "tollfree", (2) = "number" = "+1 (800) 267-6583"}), (3) = Vector(2, {(1) = "type" = "fax", (2) = "number" = "+1 (519) 747-5284"})})), (2) = "companyName" = "Maplesoft", (3) = "address" = (Vector(5, {(1) = "city" = "Waterloo", (2) = "country" = "Canada", (3) = "streetAddress" = "615 Kumpf Drive", (4) = "province" = "ON", (5) = "postalCode" = "N2V 1K8"})), (4) = "founded" = 1988})})

To extract a particular datum:

T["phoneNumbers"][1]["number"];

"+1 (519) 747-2373"

T["address"]["city"];

"Waterloo"

T["founded"];

1988

 

Download JSON.mw

If you want further explanation of any of the syntax (except the part that I said was advanced), let me know.

I'm nearly certain at this point, so I'll make this an Answer:

I think that the last statement of OrderB is of the form print(x). You need to change that to either return(x), return x, or simply x. It doesn't matter which one. A procedure should never use print to return its final value. The purpose of print is to display supplementary information on the screen.

The differential equations and the initial conditions need to be in a single set. So, you need to

dsolve(IVP union ICs, numeric);

Delta:= D[2,1,1] + D[2,1] + D[2,3,3] + D[1,1] + D[3,1,1]:

To use it:

Delta(f)(x,y,z);

Here's a workaround.


restart:

assume(k::positive);

I1:= int(exp(-I*k*x)/cosh(x), x= -infinity..infinity):

I2:= evalc(I1);

int(cos(k*x)/cosh(x), x = -infinity .. infinity)

The integrand is obviously even, so...

I3:= 2*eval(I2, -infinity= 0);

Pi/cosh((1/2)*Pi*k)


Download cosh_integral.mw

Obviously the result is true for negative k also.

I wrote a procedure to compute an approximation to the limit that you gave in your most-recent Reply:

lambda(x0) = limit(sum(ln(abs(f'(x[i]))), i= 0..n-1)/n, n= infinity),

where x[i] = f(x[i-1]), x[0]= x0.

(I figured this out from this Wikipedia article on Lyapunov exponents.)

For your sample function f:= x-> exp(x^2*(a-x)), the convergence is very slow for most of the x0 that I tried. Does anyone here know any numeric acceleration techniques for a sequence of this type? It's almost a series. Fortunately, everything except the initial differentiation of f can be done under evalhf.

LyapunovExponent:= proc(
     f::procedure,
     x0::complexcons,
     {epsilon::positive:= 1e-7}, #Absolute error tolerance
     {max_iters::posint:= 2^21}   #Infinite loop prevention
)
option `Author: Carl J. Love, 2016-May-10`;
description
"Computes a numeric approximation to "
"lambda(x0) = limit(sum(ln(abs(f'(x[i]))), i= 0..n-1)/n, n= infinity), "
"where x[i] = f(x[i-1]), x[0]= x0. "
"See: https://en.wikipedia.org/wiki/Lyapunov_exponent"
;
local f1:= D(f);
     evalhf(
          proc(f, f1, x0, epsilon, max_iters)
          local
               S:= 0,
               L:= epsilon,
               newL:= 0,
               x:= x0,
               n  #loop index
          ;
               for n to max_iters while abs(newL-L) >= epsilon do
                    S:= S+ln(abs(f1(x)));
                    L:= newL;
                    newL:= S/n;
                    x:= f(x)
               end do;
               if n > max_iters then
                    error
                         "Didn't converge. Abs. err. = %1. "
                         "Increasing epsilon or max_iters might help.",
                         abs(newL-L)
               end if;
               userinfo(1, LyapunovExponent, "Converged; iterations =", n);
               newL
          end proc
          (f, f1, x0, epsilon, max_iters)
     )
end proc:

Here's your function done with a sample a and x0.

infolevel[LyapunovExponent]:= 1:

f:= exp(x^2*(a-x)): x0:= 2:
LyapunovExponent(unapply(eval(f, a= .1), x), x0);
LyapunovExponent: Converged; iterations = 2036767.
                     -0.0552719105995118365

So, would you like some plots using this? There are five dimensions to work with: three real and two imaginary since both a and x0 can be complex. This function exp(x^2*(a-x)) is your baby; I'm at a loss for coming up with good ranges for a and x0.

 

 

Use seq to solve your problem:

solve({seq(eqc[j], j= 1..2*M-1, 2)}, {seq(C[j], j= 1..2*M-1, 2)});

By the way, your title is understandable; however, correct English would be How to solve for a variable number of variables? The word number, in this context, is used for quantities that are necessarily nonnegative integers; the word amount is reserved for quantities that aren't necessarily nonnegative integers. The for is required because the phrasal verb solve for has a different meaning than solve. One solves an equation; one solves for a variable in an equation.

Below, I've simplified and modernized your code. I agree with Mac Dude that unassigning delta is useless because it was never assigned a value in the first place.

I thought that it would be useful to you to be able to see the solution set returned by solve, so I added code to print that out. Then maybe you can figure what's happening to your delta.

derivation := proc(A::Array)
local
     i, j, k, m, s, D, delta, n:= op([2,2,2], A), _D:= Matrix((n,n), symbol= D),
     sols:= [solve(
          {seq(
               seq(seq(add(A[k,j,m]*D[k,i]+delta*A[i,k,m]*D[k,j], k= 1..n), m= 1..n), j= 1..n),  i= 1..n)
          },
          indets(_D)
     )]
;
     userinfo(1, ':-sols', NoName, print('sols'= sols));
     seq(eval(_D, s), s= sols)
end proc:

AS1:= Array(((1..2)$3), {(1,1,2)= 1}):
infolevel[sols]:= 1:
derivation(AS1);


     sols = [{D[1, 1] = 0, D[1, 2] = 0, D[2, 1] = D[2, 1], D[2, 2] = D[2, 2]}]
     Matrix(2, 2, [[0, 0], [D[2, 1], D[2, 2]]])

It's different if I change the solved-for variables from indets(_D) to indets(_D) union {delta}. Then there are two solutions and consequently two matrices.

derivation(AS1);

     sols = [{delta = delta, D[1, 1] = 0, D[1, 2] = 0, D[2, 1] = D[2, 1], D[2, 2] = D[2, 2]},
          {delta = -1, D[1, 1] = D[1, 1], D[1, 2] = 0, D[2, 1] = D[2, 1], D[2, 2] = D[2, 2]}]

     Matrix(2, 2, [[0, 0], [D[2, 1], D[2, 2]]]), Matrix(2, 2, [[D[1, 1], 0], [D[2, 1], D[2, 2]]])

If you use add instead of sum, then you can use a step of 2.

X:= m-> (1-cos(m*Pi*x/2/a))/2;
w:= unapply(add(C[i]*X(i), i= 1..M, 2), x);

This assumes that M has a definite numeric value. If you want M to remain indefinite, then let me know. It's fairly easy to re-arrange it for that.

Here's a very large start to the problem. You can also make 2-D plots by changing [x,y,z] in the plot command to [x,y], [x,z], or [y,z].

I don't exactly know what you mean by "code for bifurcation by varying tau vs x(t)." Perhaps you could point me to an article about it.

restart:
ODEs:=
     diff(x(t),t) = y(t)-b*x(t)^3+a*x(t)^2-z(t-tau)+J,
     diff(y(t),t) = c-d*x(t)^2-y(t),
     diff(z(t),t) = r*(s*(x(t)-beta)-z(t))
:
ICs:= x(0)=2.6, y(0)=1.7, z(0)=0.8:
params:= {a= 3, b= 1, c= 1, d= 5, s= 4, beta= 1.6, r= 0.6e-2, J= 3.0}:

#Plotting parameters:
taumax:= 10:
tmax:= 20:
ntau:= 5:

Sol:= tau-> dsolve(eval({ODEs, ICs}, params union {:-tau= tau}), numeric):

plots:-display(
     seq(
          plots:-odeplot(
               Sol(tau), [x,y,z](t), t= 0..tmax,
               numpoints= trunc(50*tmax), color= COLOR(HUE, tau/taumax)
          ),
          tau= 0..taumax, taumax/ntau
     ),
     thickness= 0
);

Excellent Question. Here's a workaround. The explanation for why what you tried didn't work follows.

F_if:= unapply(evalindets(F(x,y), specfunc(anything, piecewise), convert, `if`), (x,y));

     F_if:= (x,y)-> if x < 0 then if y < 0 then 0 else 1 end if else if y < 0 then 1 else 0 end if end if

That's word-for-word what you were expecting!

Explanation: The command convert(..., `if`) is, as far as I can see, undocumented. However, it's natural that you would try it because the documented command convert(..., piecewise) does the inverse conversion. The code for this command is in the procedure `convert/if`, which can be displayed with

showstat(`convert/if`);

(I don't display the actual code here due to copyright issues.) We see that this is a one-line procedure that relies on Maple's "hackware package": the commands pointto, assemble, addressof, and kernelopts(dagtag= ...) (these commands all have help pages). Even without knowing what those do, it's easy to see that it only works on the top level (see footnote *). The design is flawed in that the procedure doesn't apply itself recursively to piecewise substructures. This can be corrected with command evalindets. That'll extract all substructures of a given type and recursively apply a conversion to them all.

*Expert-level observation: We can see from this procedure that the internal structure of an if statement has its arguments in the same order as a piecewise command.

These Mathematica commands for finite-element method with adaptive multigrid (mesh refinement) are light-years ahead of what's possible for the numeric solution of PDEs in Maple. There are no equivalent Maple commands, so no direct translation is possible.

Of course Maple does provide all the low-level programming capability required to program these methods from scratch. Searching the Maple Applications Center for "finite element method PDE" turns up one hit. There may be other third-party resources on the web.

First 226 227 228 229 230 231 232 Last Page 228 of 395