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

Is your goal to re-import the Matrix into Maple? If so, then use the save command. For example:

A:= LinearAlgebra:-RandomMatrix(31);
save A, "C:/users/owner/desktop/MyMatrix.txt";

To get the Matrix back into Maple, perhaps in another session, do

read "C:/users/owner/desktop/MyMatrix.txt";

This process can be applied to any data structure, not just Matrices.

If your goal is to create a text file that can be read by another program, use the command ExportMatrix. For example,

ExportMatrix("C:/users/owner/desktop/MyMatrix.csv", A, target= csv);

will export your Matrix as comma-separated values (csv). See ?ExportMatrix for the other formats supported.

If you can get the data in a Matrix, it's then trivial to extract the columns from that Matrix. Let's say that the Matrix is named M. If you want the third column of M, you could do

X:= M[.., 3];

 

See ?printlevel. In particular, setting

printlevel:= 2:

will let you see the results of statements within two nested loops or within a loop and a conditional statement.

Edit: I changed to value from 3 to 2.

The command is evalf[2], for example, evalf[2](2.5999).

You need multiplication operators for xyxz, and yz. They should be x*y, etc.

It's better to use subscripted variables x[k] rather than concatenated variables xk when there are an indefinite number of them.

fx:= x-> theta*exp(-theta*x);
prod:= product(fx(x[k]), k= 1..n);

In the command I show below, the part Typesetting:-ms(...is undocumented, and thus it could change in a future Maple release.

plot(
     x^2, x= -2..2,
     title= Typesetting:-ms("My favorite plot", color= green),
     titlefont= [TIMES,ROMAN,24]
);

(FC,Fr):= selectremove(has, F, C):
(FL,Fx1):= selectremove(has, Fr, L):
for v in [C, L, x1] do k||v:= collect(F||v/v, v) end do;

Verify correctness:

expand(kC*C + kL*L + kx1*x1 - F);

Your function f, as presented above, is garbage to Maple because it's missing a multiplication sign between y^3) and (5+. If you correct that, then the command

F:= [op(f)];

will split f into its seven factors, which can be individually accessed as F[1], ..., F[7].

The command

(f1,f2):= selectremove(type, f, `+`);

will split f into two parts, the first being the factored product of all the parts in brackets, and the second being the factored product of the other parts.

Here's how the length of 9 for x+2*y is counted:

Running dismantle(x+2*y) shows:

SUM(5)
   NAME(4): x
   INTPOS(2): 1
   NAME(4): y
   INTPOS(2): 2

(The numbers that appear in parentheses after the keywords are irrelevant to this discussion.) This shows that the expression has four operands: x, 1, y, 2. The 1 is the implied coefficient of x. Each of those operands has length 1, as you can verify. So, the expression x+2*y is stored in memory as

DA:= disassemble(addressof(x+2*y));

DA:= 16, 18446744562576677534, 18446744073709551617, 18446744562576677566, 18446744073709551619

The 16 is a tag that indicates that this is a SUM:

kernelopts(dagtag= 16);

SUM

The four long numbers are the memory addresses of x, 1, y, and 2. This can be verified by

seq(pointto(k), k= DA[2..]);

x, 1, y, 2

These addesses are, of course, session dependent. So, the expression x+2*y is stored as five words (that includes one word for the dag-tag 16). The recursive length rule, which you quoted, says that this is added to the lengths of the operands, which is 4 (each operand has length 1, as noted above). Adding 5 and 4 makes the length 9.

Note that multiplication by a numeric coefficient is not considered as a separate multiplication operation.

If you simply want the length in characters of an expression e, this can be easily obtained as 

length(convert(e, string));

(as already mentioned by Axel).

For a command that uses algebra rather than nitty-gritty internal representations to measure expressions, look at SolveTools:-Complexity. Another option is to convert your expression to a procedure (just wrap it with unapply) and use SoftwareMetrics:-HalsteadMetrics. This'll measure the complexity in several ways that attempt to account for the psychology of comphrehending expressions. Two rather simplistic metrics are the MmaTranslator:-Mma:-LeafCount mentioned by Axel, and a simple operation count done by codegen:-cost

To my mind, common subexpressions shouldn't count towards the complexity of an expression. Thus, my favorite way to measure the complexity of an expression is to pass it to codegen:-optimize with the tryhard option and to then measure the length (in characters!) of the resulting procedure, after compression. This can be done in one line with

length(sprintf("%m", codegen:-optimize(unapply(e), tryhard)));

where e is the expression. For trivial expressions, this returns a result that's a bit high because there's the overhead of the procedure header. But, you're not dealing with trivial expressions. You can normalize for this effect by simply subtracting 31 from the result, which is the count when the raw metric is applied to the simplest possible algebraic expression, 0.

 

All inequality constraints should go in the first set; in your case, that'd be the empty set. All equality constraints should go in the second set. So the NLPSolve command should be

sol := Optimization:-NLPSolve(
     f, {}, {p1, p2, p3}, 0 .. 1, 0 .. 1, 0 .. 1, 0 .. 1, 0 .. 1, 0 .. 1, initialpoint = [.5, .5, .5, .5, .5, .5]
);

which can be shortened to

sol:= Optimization:-NLPSolve(f, {}, {p||(1..3)}, (0..1)$6, initialpoint= [.5$6]);

This generates a new error message:

Error, (in Optimization:-NLPSolve) matrix dimensions don't match: 3 x 6 vs 3 x 2

This seems to be a bug in NLPSolve. Running trace(f) shows that your procedure f was never called, so the dimensions problem is not in your code.

Use command march (short for Maple archive). Use march(create, ...) to create an archive. Use march(add, ...to add the .m file to the archive. Use march(list, ...to list the contents of the archive.

You use a global variable t symbolically; it's never given a value. Thus, the value returned from f is an expression depending on t[1], t[2], and t[3]. The value returned by f needs to be numeric if it's to be used by NLPSolve. If you do trace(f) before calling NLPSolve, you can see this.

Don't set warnlevel to 0 before your code is debugged. That's just asking for trouble. If the warning tells you that the variables are local, then declare them local.

You are using some very old coding styles. Perhaps you are emulating a textbook. It's giving you bad coding habits. There's no need for with(linalg). Change array to Array.

Don't rely on the with command to allow the use of so-called "short form" names inside a procedure. Instead, use a uses clause in the procedure header, or just use the "long form" name.

The following worksheet solves your original problem using Statistics:-NonlinearFit. Then it solves the same problem using NonlinearFit with procedural input. You can rewrite the procedure for your more-complicated model.

An example of the two ways to use Statistics:-NonlinearFit: with algebraic input (for simple models) and with procedural input (for more-complicated models)

 

Simple problem from Bronstejn-Semengyajev math book.

 

 

restart:

X:= ExcelTools:-Import("C:/users/owner/desktop/BroSzem_Data.xlsx", "XY", "A2:A13"):

X^+;

Matrix([[.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0, 1.1, 1.2]])

Y:= ExcelTools:-Import("C:/users/owner/desktop/BroSzem_Data.xlsx", "XY", "B2:B13"):

Y^+;

Matrix([[1.78, 3.18, 3.19, 2.54, 1.77, 1.14, .69, .4, .23, .13, 0.7e-1, 0.4e-1]])

XY:= <X|Y>:

fig1:= plot(XY, style= point, symbolsize= 16, view= [0..1.5, 0..3.5], gridlines= false);

Solution method 1: Algebraic input

Model:= a*(x^b)*exp(c*x);

a*x^b*exp(c*x)

MinSol:= Statistics:-NonlinearFit(
     Model, XY, x, initialvalues= [a=375, b=3, c=-7.5],
     output= [residualsumofsquares, parametervalues]
);

[0.916398873982076e-4, [a = HFloat(396.6019850867548), b = HFloat(1.9980985400641966), c = HFloat(-8.05357304777323)]]

fig2:= plot(eval(Model, MinSol[2]), x= 0..1.5):

plots:-display([fig||(1..2)], gridlines= false);

Solution method 2: Procedural input

Model:= proc(x, a, b, c)
     a*(x^b)*exp(c*x)
end proc:
   

MinSol:= Statistics:-NonlinearFit(
     Model, XY, initialvalues= [375, 3, -7.5],
     output= [residualsumofsquares, parametervector]
);

MinSol := [0.916398873982076e-4, Vector(3, {(1) = 396.6019850867548, (2) = 1.9980985400641966, (3) = -8.05357304777323}, datatype = float[8])]

params:= convert(MinSol[2], list)[];

HFloat(396.6019850867548), HFloat(1.9980985400641966), HFloat(-8.05357304777323)

ModelSubs:= subs(_P= params, x-> Model(x, _P));
     

proc (x) options operator, arrow; Model(x, HFloat(396.6019850867548), HFloat(1.9980985400641966), HFloat(-8.05357304777323)) end proc

fig3:= plot(ModelSubs, 0..1.5):

plots:-display([fig1,fig3], gridlines= false);

``

NULL

``

``

``

``

``

``


Download NonlinearFit_proc_input.mw

The default symbol for the imaginary unit in Maple is uppercase I, not the lowercase i that is more usual in mathematics and which you are using above. That default can be changed to any other symbol:

interface(imaginaryunit= i);

But beware that whatever symbol you use, you won't be able to use it as a variable. The use of i as a variable is so common in most programming languages that Maple decided to use the less commomly used symbol for the imaginary unit.

First 217 218 219 220 221 222 223 Last Page 219 of 395