Carl Love

Carl Love

28035 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

Perhaps you're looking for a more-automatic way, such as a method that tells you that (-1+d1) and (-2+d1) are the best subexpressions to substitute for.


restart:

y:=
     -8*C*d1^2*(-2+d1)*(-1+d1)^3*r*L*R^3 + (d1^4*(-2+d1)^2*L^2 -
     4*C*(-2+d1)*(4*d1^3-13*d1^2+16*d1-8)*(-1+d1)^2*r^2*L+4*C^2*(-2+d1)^2*
     (-1+d1)^4*r^4)*R^2+(2*d1^4*(-2+d1)^2*r*L^2-2*C*(-2+d1)*
     (5*d1^3-24*d1^2+32*d1-16)*(-1+d1)^2*r^3*L+4*C^2*(-2+d1)^2*
     (-1+d1)^4*r^5)*R+d1^4*(-2+d1)^2*r^2*L^2-2*C*(-2+d1)*
     (d1^3-6*d1^2+8*d1-4)*(-1+d1)^2*r^4*L+C^2*(-2+d1)^2*(-1+d1)^4*r^6
:

Find the most frequently occuring subexpressions of type `+`. There's no need to limit this to type `+`, but I wanted an easy way to exclude trivial subexpressions like constants and names.

AllE:= [indets(y, `+`)[]]:

All:= [indets~(AllE, `+`)[]]:

MCS:= sort(AllE, key= (e-> -nops(select(x-> member(e,x), All))))[..-2];

[-2+d1, -1+d1, 4*d1^3-13*d1^2+16*d1-8, 5*d1^3-24*d1^2+32*d1-16, d1^4*(-2+d1)^2*L^2-4*C*(-2+d1)*(4*d1^3-13*d1^2+16*d1-8)*(-1+d1)^2*r^2*L+4*C^2*(-2+d1)^2*(-1+d1)^4*r^4, 2*d1^4*(-2+d1)^2*r*L^2-2*C*(-2+d1)*(5*d1^3-24*d1^2+32*d1-16)*(-1+d1)^2*r^3*L+4*C^2*(-2+d1)^2*(-1+d1)^4*r^5, d1^3-6*d1^2+8*d1-4]

We see from the list MCS that the two most commonly occuring subexpressions are those that you expected. We substitute names A and B for those.

Y:= eval(y, MCS[1..2]=~ [A,B]);

-8*C*d1^2*A*B^3*r*L*R^3+(d1^4*A^2*L^2-4*C*A*(4*d1^3-13*d1^2+16*d1-8)*B^2*r^2*L+4*C^2*A^2*B^4*r^4)*R^2+(2*d1^4*A^2*r*L^2-2*C*A*(5*d1^3-24*d1^2+32*d1-16)*B^2*r^3*L+4*C^2*A^2*B^4*r^5)*R+d1^4*A^2*r^2*L^2-2*C*A*(d1^3-6*d1^2+8*d1-4)*B^2*r^4*L+C^2*A^2*B^4*r^6

Attempt some further compactification.

collect(Y, [A,B]);

((4*C^2*R^2*r^4+4*C^2*R*r^5+C^2*r^6)*B^4+d1^4*L^2*R^2+2*d1^4*r*L^2*R+d1^4*r^2*L^2)*A^2+(-8*C*d1^2*B^3*r*L*R^3+(-4*C*(4*d1^3-13*d1^2+16*d1-8)*r^2*L*R^2-2*C*(5*d1^3-24*d1^2+32*d1-16)*r^3*L*R-2*C*(d1^3-6*d1^2+8*d1-4)*r^4*L)*B^2)*A

Another alternative.

collect(Y, [B,A]);

(4*C^2*R^2*r^4+4*C^2*R*r^5+C^2*r^6)*A^2*B^4-8*C*d1^2*A*B^3*r*L*R^3+(-4*C*(4*d1^3-13*d1^2+16*d1-8)*r^2*L*R^2-2*C*(5*d1^3-24*d1^2+32*d1-16)*r^3*L*R-2*C*(d1^3-6*d1^2+8*d1-4)*r^4*L)*A*B^2+(L^2*R^2*d1^4+2*L^2*R*d1^4*r+L^2*d1^4*r^2)*A^2

 

Download Most-common_subexpressions.mw

The model is linear in the parameters a and b, which is all that's needed for it to be a linear fit problem. The implicit nature of the model can be handled by subtracting one side from the other and using 0 as the dependent variable values.

restart:

NMP:= <0.530, 0.555, 0.572, 0.592>:
ETOH:= <0.136, 0.153, 0.163, 0.170>:

Model:= ln(etoh/(1-etoh-nmp)) = a*ln(nmp/(1-etoh-nmp)) + b:

S:= Statistics:-Fit(
     (lhs-rhs)(Model), <ETOH|NMP|0~(NMP)>, [etoh,nmp],
     summarize, output= parametervalues
):

Summary:
----------------
Model: -1.2558617*ln(nmp/(1.-1.*etoh-1.*nmp))+1.4660801+ln(etoh/(1.-1.*etoh-1.*nmp))
----------------
Coefficients:
    Estimate  Std. Error  t-value  P(>|t|)
a    1.2559    0.0580      21.6682   0.0021
b   -1.4661    0.0415     -35.3513   0.0008
----------------
R-squared: 0.9958, Adjusted R-squared: 0.9936

eval(Model, S);

ln(etoh/(1-etoh-nmp)) = HFloat(1.255861653096077)*ln(nmp/(1-etoh-nmp))-HFloat(1.466080124258709)

 

Download Implicit_linear_fit.mw

It looks like you have a very good fit. The p-values are very impressive, especially considering that there's only four data and two degrees of freedom.

I guess that it bothers you to need to select the variable with respect to which to eliminate. Your first inclination is f__F; Kitonum's is L. It turns out that there are an amazing five choices of variable that lead to the same simplest possible final form. The following line of code finds all possible injective substitutions (there are seven!) and tries them all:

map2(simplify@eval, nb, op~(select(s-> nops(s)=1, map2(solve, hh, `[]`~([indets(hh)[]])))));

[(gamma*upsilon-delta__1122^k*tau)*rho, (gamma*upsilon-delta__1122^k*tau)*rho, (gamma*upsilon-delta__1122^(L*lambda*upsilon*tau*v*(sigma-1)/(L*lambda*tau*upsilon*v+f__F*rho*sigma*tau^2-f__F*rho*sigma*upsilon^2))*tau)*rho, (gamma*upsilon-delta__1122^k*tau)*rho, -(k-sigma+1)*lambda*L*(gamma*upsilon-delta__1122^k*tau)*upsilon*tau*v/(f__F*sigma*(tau^2-upsilon^2)*k), (gamma*upsilon-delta__1122^k*tau)*rho, (gamma*upsilon-delta__1122^k*tau)*rho]

When you look at it with 2-D ouput, it's obvious that there are five in the simplest form. If you just want the simplest form selected automatically, do

sort(%, key= length)[1];

(gamma*upsilon-delta__1122^k*tau)*rho

 

First, some case corrections: Ln should be ln, pi should be Pi, and S should be s. Second, it's poor style (though not an error) to use .5 as an exponent; I changed these to sqrt. Using exponent (1/2) would also be okay.

The plot can be done by substituting z^(1/3) for R/R[0] and then using plots:-implicitplot.

eq:=
     ln(2*s*t+2*s*sqrt(t/Pi)*z^(1/3)+z^(2/3)) =
     2*s/sqrt(2*Pi*s-s^2)*(arctan((s+z^(1/3)*sqrt(Pi/t))/sqrt(2*Pi*s-s^2)) - Pi/2);

plots:-implicitplot(
     map2(eval, eq, s=~ [.01, .005, .003]), t= .01..99, z= 0..2, gridrefine= 3,
     color= [red, green, blue], labels= [t, typeset([R/R__0]^3)],
     legend= (s=~ [.01, .005, .003])
);

R:= solve(Fx, x);
a:= `+`(R)/2;
b:= (R[1]-a)^2;

I find it easiest to put the information into a Matrix and then put the Matrix into a spreadsheet. Here's an example:

restart:
R:= RandomTools:-Generate(float(range= -2..2), makeproc):
f:= randpoly([p,x,y]);

M:= Matrix([
     seq(
          [S[1], S[2][1], eval([x,y], S[2][2])[]],
          S= [seq(
               [
                     p,
                     Optimization:-Maximize(
                          f, x= -2..2, y= -2..2,
                          initialpoint= ([x,y]=~ ['R()' $ 2])
                     )
               ],
               p= 0..1, 0.1
          )]
     )
]);

Spread:-CreateSpreadsheet(MySpreadsheet);
Spread:-SetMatrix(MySpreadsheet, M, 3, 3); #(3,3) is starting (row,column) in spread sheet.

Code edited to randomize the initial point.

Good Question. Do the legended and unlegended plots in separate plot commands and paste the results together with plots:-display, like this:

plots:-display(
     plot(
          [cos(x)^2, 1-(1/2)*x],
          x = -Pi .. Pi,
          legend = [typeset("Curve: ", cos(x)^2), typeset("Curve: ", 1-(1/2)*x)]
     ),
     plot(x^2, x= -Pi..Pi)
);

You're misinterpretting the placement of the horizontal axis. Maple doesn't always place it at y = 0. In this case, it placed it at the minimum y-value, 1.2. Look at the numbers on the vertical axis to confirm this. If you want to see the axes with their normal placement, use

plot({3.8+2.6*sin(50*Pi*t), 5}, t= 0..0.05, axes= normal, view= [0..0.05, -1..7]);

You can also "probe" values on the graph with the mouse. Right-click on the graph, select Manipulator, then Point Probe. Right-click again, select Probe Info, then Nearest Point on Line. Now you can trace your mouse along the curve and see the values.

There's an undocumented command inner for dot products that won't use the conjugate:

inner(vector([x,y,z]), vector([a,b,c]));

Use plottools:-sphere to plot an actual small sphere at each point.

You must be using Maple's garbage 2-D input mode. You original code probably contained the statement

ans(ind):= [op([1], f1max), roll];

If you had been using Maple's 1-D input, this statement only has one possible interpretation (in context): You are assigning an Array element. With garbage 2-D input, Maple tells you that the statement can be interpretted as either a remember table assignment or a function definition and asks you to choose which. (Note that the correct interpretation isn't even one of the choices; however, remember table assignment is effectively the same as Array assignment for the purpose of this question.) Two weeks ago, you selected "remember table assignment"; today you selected "function definition".

If you use 1-D input (aka Maple Input), you'll avoid this problem, and you won't have to answer a stupid question every time you run your code. 2-D input is garbage: No reasonable language asks the end user to provide an interpretation for the code every time a program is run.

 

There's a command that does the image conversion and the file writing in one step: plotsetup. You can use it like this:

currentdir(kernelopts(homedir)); #Set directory where file will go.

ExportJPGMyPlot:= proc(MyPlot, Name::string)
     plotsetup(jpeg, plotoutput= cat(Name, ".jpg"));
     Threads:-Sleep(2); #2 secs to synchronize I/O.
     print(MyPlot);
     plotsetup(default);
     Threads:-Sleep(2);
     MyPlot
end proc:

ExportJPGMyPlot(plot(x^2, x= -2..2), "XSqr");

Note that the API (call sequence, arguments, etc.) is completely different from any other Maple command. It works entirely by side effects which will redirect plots that otherwise would've been displayed in the GUI.

The list of supported image file types is documented (minimally) at ?plot,device. The list of image file types that are available on your system is returned by plotsetup(help). Unfortunately, this latter list is much longer than that documented by the former.

The Threads:-Sleep(2) isn't necessarily needed, and the 2 can possibly be reduced; it's system dependent. I think that the file I/O in this situation isn't synchronized with the Maple kernel. Maybe someone else can confirm or provide further clarification of that.

The command for filtering a set is select. The command fsolve will, by default, return all real roots (and only real roots) for a polynomial. So what you want can be done by

select(x-> g(x,a) > 0, {fsolve(f(x,a))});

To display a 2-D Array A as if it were a Matrix, use <A>.

e:= g^((2*(-sigma+k+1))/(-1+sigma))-tau^2:
ex:= op([1,2], e)/2;
thaw(factor(expand(simplify(e, {ex= freeze(ex)}))));

First 212 213 214 215 216 217 218 Last Page 214 of 395