## 50 Reputation

11 years, 266 days

## Fastest way to apply a list of list of n...

Maple 17

Hi MaplePrimers,

I've written a function that needs to be evaluated at a bunch of different points.  Evaluating it in a loop works. I'm wondering if there was a faster way to do this because I'm evaluating a list of 400k+ sets of points.

Right now I'm using a loop with the following code.

for i from 1 to 500000 do
Results(i):= f(L[i][])[1];
od;

If I have a function f, which has 5 arguements, f(y, w, x,y,z).

I also have a list of those arguements:  L = [[1,2,3,2,3],[4,5,6,2,3],[7,8,9,2,3]]

What would be the fastest way to get a list of results?  Also, is there a way that I could preallocate memory for this list?

Ideally, I would like to get the output as as list.  ie:

Results:= [[f(1,2,3,2,3)], [f(4,5,6,2,3)], [f(7,8,9,2,3)]]

## Solve can't find analytically solution.....

Maple 17

Hi Maple-Prime-ers!

I have a system of equations, containing 18 variables and 13 equations, making this a 5 degree of freedom (DOF) system.  I would like to analytically solve each of the equations in terms of each of these DOFs.  Normally I would use solve(system, dof_variables) to accomplish this, but it doesn't return anything.  Not even [].

I can solve this system by hand.  I've included a hand-solution involving isolate() and subs() in the attached worksheet.  I'm looking to incorporate this in an optimization algorithm with varying system, so I would like an automated way of doing this.

Does anybody have any suggestions to get solve to work as intended?

3driversys_FD_BRAKE_ICE_GEN.mw

Here is the system I am talking about:

The free variables are:  {FD_T, FD_W, ICE_T, EM2_T, BRAKE_T}

I'm looking for a solution in this form:

## Solve taking forever then not finding a ...

Hi MaplePrime-ers,

I'm using the following piece of code to (i) solve the system of symbolically, so I can (ii) evaluate equations quickly at many points of time.  This works quite well for 4 defined values, but I'm having problems adding a 5th defined value.  Specifically, solve leaves the "solution may be lost" message after taking forever.  As the symbolic solution will be run mulitple times by a optimziation algorithm, I'd ideally like to get the solve time under 2 minutes.  I've attached both executed worksheets.  Is there anything I can do to have solve work as I intend?

This first code snippet achieves what I would like to do Series_noGear.mw:

#Interconnection Equations
eq2[1] := FD_T + EM2_T = 0;
eq2[2] := ICE_T + GEN_T = 0;
eq2[3] := EM2_A + GEN_A + BAT_A = 0;
eq2[4] := -FD_W + EM2_W = 0;
eq2[5] := -ICE_W + GEN_W = 0;
eq2[6] := -EM2_V + GEN_V = 0;
eq2[7] := -EM2_V + BAT_V = 0;

#ICE
eq_c[1] := ICE_mdot_g= ICE_T * ICE_W;

#BAT
eq_c[2] := BAT_V = 271;

#EM2
EM2_ReqPow_eq := (-148.3) + (4.267)*abs(EM2_W) + (12.77)*abs(EM2_T) + (-0.0364)*abs(EM2_W)^2 + ( 1.16)*abs(EM2_W)*abs(EM2_T) + (-0.258)*abs(EM2_T)^2 + ( 0.0001181)*abs(EM2_W)^3 + (-0.0005994)*abs(EM2_W)^2*abs(EM2_T) + ( 0.0001171)*abs(EM2_W)*abs(EM2_T)^2 + (0.001739 )*abs(EM2_T)^3 + (-1.245e-07 )*abs(EM2_W)^4 + ( 1.2e-06)*abs(EM2_W)^3*abs(EM2_T) + ( -1.584e-06)*abs(EM2_W)^2*abs(EM2_T)^2 + ( 4.383e-07)*abs(EM2_W)*abs(EM2_T)^3 + (-2.947e-06)*abs(EM2_T)^4;
eq_c[3] := EM2_P = piecewise( EM2_T * EM2_W = 0, 0, EM2_W*EM2_T < 0,-1 * EM2_ReqPow_eq, EM2_ReqPow_eq);
eq_c[4] := EM2_A = EM2_P/EM2_V;

#GEN
GEN_ReqPow_eq:= (-5.28e-12) + ( 3.849e-14)*abs(GEN_W) + (-71.9)*abs(GEN_T) + (-1.168e-16)*abs(GEN_W)^2 +(1.296)*abs(GEN_W)*abs(GEN_T) + (2.489)*abs(GEN_T)^2 + (1.451e-19)*abs(GEN_W)^3 + (0.0001326)*abs(GEN_W)^2*abs(GEN_T) + (-0.008141)*abs(GEN_W)*abs(GEN_T)^2 + (-0.004539)*abs(GEN_T)^3 +(-6.325e-23)*abs(GEN_W)^4 + (-2.091e-07)*abs(GEN_W)^3*abs(GEN_T) + ( 3.455e-06)*abs(GEN_W)^2*abs(GEN_T)^2 + ( 2.499e-05)*abs(GEN_W)*abs(GEN_T)^3 + (-5.321e-05)*abs(GEN_T)^4;

eq_c[5] := GEN_P = piecewise(GEN_T * GEN_W = 0, 0, GEN_W*GEN_T < 0,-1 * GEN_ReqPow_eq, GEN_ReqPow_eq);
eq_c[6] := GEN_A = GEN_P/GEN_V;

#assumptions
assume(BAT_V::nonnegative);
assume(FD_W::nonnegative);

termeqs := {eq_c[1],eq_c[2],eq_c[3],eq_c[4],eq_c[5],eq_c[6]};

sys_eqs2 := termeqs union convert(eq2,set);

drivers2:= {ICE_T,ICE_W,FD_T,FD_W};
symvarnames2:=select(type,indets(convert(sys_eqs2,list)),name);
notdrivers2:=symvarnames2 minus drivers2;

sol2:=solve(sys_eqs2,notdrivers2) assuming real;

symb_sol2:=unapply(sol2,[drivers2[]]);

symb_sol2(1,2,3,5);

#Enumerate (there will generally be about 40, not 3)

count := 0;
for i1 from 1 to 3 do
for i2 from 1 to 3 do
for i3 from 1 to 3 do
for i4 from 1 to 3 do
count := count + 1;
solsol2(count) := symb_sol2(i1,i2,i3,i4);
od;
od;
od;
od;
count;

This second code snippet includes the changes in bold, which make solve take forever Series_addGear.mw:

#Interconnection Equations
eq2[1] := FD_T + EM2_T = 0;
eq2[2] := ICE_T + GBb_T = 0;
eq2[3] := EM2_A + GEN_A + BAT_A = 0;
eq2[4] := -FD_W + EM2_W = 0;
eq2[5] := -ICE_W + GBb_W = 0;
eq2[6] := -EM2_V + GEN_V = 0;
eq2[7] := -EM2_V + BAT_V = 0;
eq2[8] := GBa_T + GEN_T = 0;
eq2[9] := -GBa_W + GEN_W = 0;

#ICE
eq_c[1] := ICE_mdot_g= ICE_T * ICE_W;

#BAT
eq_c[2] := BAT_V = 271;

#EM2
EM2_ReqPow_eq := (-148.3) + (4.267)*abs(EM2_W) + (12.77)*abs(EM2_T) + (-0.0364)*abs(EM2_W)^2 + ( 1.16)*abs(EM2_W)*abs(EM2_T) + (-0.258)*abs(EM2_T)^2 + ( 0.0001181)*abs(EM2_W)^3 + (-0.0005994)*abs(EM2_W)^2*abs(EM2_T) + ( 0.0001171)*abs(EM2_W)*abs(EM2_T)^2 + (0.001739 )*abs(EM2_T)^3 + (-1.245e-07 )*abs(EM2_W)^4 + ( 1.2e-06)*abs(EM2_W)^3*abs(EM2_T) + ( -1.584e-06)*abs(EM2_W)^2*abs(EM2_T)^2 + ( 4.383e-07)*abs(EM2_W)*abs(EM2_T)^3 + (-2.947e-06)*abs(EM2_T)^4;
eq_c[3] := EM2_P = piecewise( EM2_T * EM2_W = 0, 0, EM2_W*EM2_T < 0,-1 * EM2_ReqPow_eq, EM2_ReqPow_eq);
eq_c[4] := EM2_A = EM2_P/EM2_V;

#GEN
GEN_ReqPow_eq:= (-5.28e-12) + ( 3.849e-14)*abs(GEN_W) + (-71.9)*abs(GEN_T) + (-1.168e-16)*abs(GEN_W)^2 +(1.296)*abs(GEN_W)*abs(GEN_T) + (2.489)*abs(GEN_T)^2 + (1.451e-19)*abs(GEN_W)^3 + (0.0001326)*abs(GEN_W)^2*abs(GEN_T) + (-0.008141)*abs(GEN_W)*abs(GEN_T)^2 + (-0.004539)*abs(GEN_T)^3 +(-6.325e-23)*abs(GEN_W)^4 + (-2.091e-07)*abs(GEN_W)^3*abs(GEN_T) + ( 3.455e-06)*abs(GEN_W)^2*abs(GEN_T)^2 + ( 2.499e-05)*abs(GEN_W)*abs(GEN_T)^3 + (-5.321e-05)*abs(GEN_T)^4;

eq_c[5] := GEN_P = piecewise(GEN_T * GEN_W = 0, 0, GEN_W*GEN_T < 0,-1 * GEN_ReqPow_eq, GEN_ReqPow_eq);
eq_c[6] := GEN_A = GEN_P/GEN_V;

#GB
eq_c[7] := GBb_T = -1/GB_R * GBa_T;
eq_c[8] := GBb_W = GB_R * GBa_W;

assume(BAT_V::nonnegative);
assume(FD_W::nonnegative);
assume(GB_R::nonnegative);

termeqs := {eq_c[1],eq_c[2],eq_c[3],eq_c[4],eq_c[5],eq_c[6],eq_c[7],eq_c[8]};

sys_eqs2 := termeqs union convert(eq2,set);

drivers2:= {GB_R,ICE_T,ICE_W,FD_T,FD_W};
symvarnames2:=select(type,indets(convert(sys_eqs2,list)),name);
notdrivers2:=symvarnames2 minus drivers2;

sol2:=solve(sys_eqs2,notdrivers2) assuming real;

symb_sol2:=unapply(sol2,[drivers2[]]);

Does assume make solve work faster, or just complicate things?  Any help is greatly appreciated!

Series_noGear.mw

## System of equations solve, solutions may...

Hi MaplePrime-ers!

I've been using the Maple(17) toolbox in Matlab(2012b) to quickly enumerate systems of equations by: (i) solving them symbolically, (ii) using unapply to make them functions, (iii) then supplying the points (driver equations) to get the system solution.  Speed is a must, because there may be 3 million+ systems to solve.  Symbolics is also very important because I am evaluating topology, so the structure of the equations may change, and therefore a functional approach will not work.

I have had success (seen in the first code snippet).  I would like similiar behaviour in the second code snippet, but sometimes I get 'solutions may be lost' as an error message,  or 'Error, (in unapply) variables must be unique and of type name'

The system of equations include:  Linear equations, 5th order polynomials, absolute functions, and pieceiwse functions.

Here is code with a topology that solves:

#Interconnection Equations
eq2[1] := FD_T + EM2_T = 0;
eq2[2] := ICE_T + GEN_T = 0;
eq2[3] := EM2_A + GEN_A + BAT_A = 0;
eq2[4] := -FD_W + EM2_W = 0;
eq2[5] := -ICE_W + GEN_W = 0;
eq2[6] := -EM2_V + GEN_V = 0;
eq2[7] := -EM2_V + BAT_V = 0;

#ICE
eq_c[1] := ICE_mdot_g=((671.5) + (-21.94)*ICE_T + (0.1942)*ICE_W + (0.5113)*ICE_T^2 + (-0.01271)*ICE_T*ICE_W + ( -0.0008761)*ICE_W^2 + (-0.006071)*ICE_T^3 + (9.867e-07)*ICE_T^2*ICE_W + (5.616e-05)*ICE_T*ICE_W^2 + (1.588e-06)*ICE_W^3 + (3.61e-05)*ICE_T^4 + (8.98e-07)*ICE_T^3*ICE_W + (-2.814e-07)*ICE_T^2*ICE_W^2 + (-8.121e-08)*ICE_T*ICE_W^3 + ( -8.494e-08 )*ICE_T^5 + (-2.444e-09)*ICE_T^4*ICE_W + (-9.311e-10)*ICE_T^3*ICE_W^2 + ( 5.835e-10)*ICE_T^2*ICE_W^3 ) *1/3600/1000 * ICE_T * ICE_W;

#BAT
eq_c[2] := BAT = 271;

#EM2
EM2_ReqPow_eq := (-148.3) + (4.267)*abs(EM2_W) + (12.77)*abs(EM2_T) + (-0.0364)*abs(EM2_W)^2 + ( 1.16)*abs(EM2_W)*abs(EM2_T) + (-0.258)*abs(EM2_T)^2 + ( 0.0001181)*abs(EM2_W)^3 + (-0.0005994)*abs(EM2_W)^2*abs(EM2_T) + ( 0.0001171)*abs(EM2_W)*abs(EM2_T)^2 + (0.001739 )*abs(EM2_T)^3 + (-1.245e-07 )*abs(EM2_W)^4 + ( 1.2e-06)*abs(EM2_W)^3*abs(EM2_T) + ( -1.584e-06)*abs(EM2_W)^2*abs(EM2_T)^2 + ( 4.383e-07)*abs(EM2_W)*abs(EM2_T)^3 + (-2.947e-06)*abs(EM2_T)^4;
eq_c[3] := EM2_P = piecewise( EM2_T = 0, 0, EM2_W = 0, 0, EM2_W*EM2_T < 0,-1 * EM2_ReqPow_eq, EM2_ReqPow_eq);
eq_c[4] := EM2_A = EM2_P/EM2_V;

#GEN
GEN_ReqPow_eq:= (-5.28e-12) + ( 3.849e-14)*abs(GEN_W) + (-71.9)*abs(GEN_T) + (-1.168e-16)*abs(GEN_W)^2 +(1.296)*abs(GEN_W)*abs(GEN_T) + (2.489)*abs(GEN_T)^2 + (1.451e-19)*abs(GEN_W)^3 + (0.0001326)*abs(GEN_W)^2*abs(GEN_T) + (-0.008141)*abs(GEN_W)*abs(GEN_T)^2 + (-0.004539)*abs(GEN_T)^3 +(-6.325e-23)*abs(GEN_W)^4 + (-2.091e-07)*abs(GEN_W)^3*abs(GEN_T) + ( 3.455e-06)*abs(GEN_W)^2*abs(GEN_T)^2 + ( 2.499e-05)*abs(GEN_W)*abs(GEN_T)^3 + (-5.321e-05)*abs(GEN_T)^4;
eq_c[5] := GEN_P = piecewise( GEN_T = 0, 0, GEN_W = 0, 0, GEN_W*GEN_T < 0,-1 * GEN_ReqPow_eq, GEN_ReqPow_eq);
eq_c[6] := GEN_A = GEN_P/GEN_V;

#ASSUMPTIONS
assume(BAT_V::nonnegative);
assume(FD_W::nonnegative);

#FINAL EQUATIONS

sys_eqs2 := convert(eq2,set) union {eq_c[1],eq_c[2],eq_c[3],eq_c[4],eq_c[5],eq_c[6]};

#Selecting which variables to solve for:

drivers2:= { ICE_T,ICE_W,FD_T,FD_W};
symvarnames2:=select(type,indets(convert(sys_eqs2,list)),name);
notdrivers2:=symvarnames2 minus drivers2;

#Symbolic solve

sol2:=solve(sys_eqs2,notdrivers2) assuming real:
symb_sol2:=unapply(sol2,convert(drivers2,list)):

#Enumerate (there will generally be about 40, not 6)

count := 0;
for i1 from 1 to 40 do
for i2 from 1 to 40 do
for i3 from 1 to 40 do
for i4 from 1 to 40 do
count := count + 1;
solsol2(count) := symb_sol2(i1,i2,i3,i4);
od;
od;
od;
od;
count;

This works great!  I would like simliar output in my second code snippet, but this time with more inputs to symb_sol.  However, if I try and change the interconnection equations a little, and add a piecewise function, and another driver... (differences in bold)

#Interconnection Equations
eq1[1] := FD_T+EM2_T = 0;
eq1[2] := ICE_T+GBb_T = 0;
eq1[3] := GEN_T+GBa_T = 0;
eq1[4] := EM2_A+GEN_A+BAT_A = 0;
eq1[5] := -FD_W+EM2_W = 0;
eq1[6] := -GEN_W+GBa_W = 0;
eq1[7] := -ICE_W+GBb_W = 0;
eq1[8] := -EM2_V+GEN_V = 0;
eq1[9] := -EM2_V+BAT_V = 0;

#ICE
eq_c[1] := ICE_mdot_g=((671.5) + (-21.94)*ICE_T + (0.1942)*ICE_W + (0.5113)*ICE_T^2 + (-0.01271)*ICE_T*ICE_W + ( -0.0008761)*ICE_W^2 + (-0.006071)*ICE_T^3 + (9.867e-07)*ICE_T^2*ICE_W + (5.616e-05)*ICE_T*ICE_W^2 + (1.588e-06)*ICE_W^3 + (3.61e-05)*ICE_T^4 + (8.98e-07)*ICE_T^3*ICE_W + (-2.814e-07)*ICE_T^2*ICE_W^2 + (-8.121e-08)*ICE_T*ICE_W^3 + ( -8.494e-08 )*ICE_T^5 + (-2.444e-09)*ICE_T^4*ICE_W + (-9.311e-10)*ICE_T^3*ICE_W^2 + ( 5.835e-10)*ICE_T^2*ICE_W^3 ) *1/3600/1000 * ICE_T * ICE_W;

#BAT
eq_c[2] := BAT = 271;

#EM2
EM2_ReqPow_eq := (-148.3) + (4.267)*abs(EM2_W) + (12.77)*abs(EM2_T) + (-0.0364)*abs(EM2_W)^2 + ( 1.16)*abs(EM2_W)*abs(EM2_T) + (-0.258)*abs(EM2_T)^2 + ( 0.0001181)*abs(EM2_W)^3 + (-0.0005994)*abs(EM2_W)^2*abs(EM2_T) + ( 0.0001171)*abs(EM2_W)*abs(EM2_T)^2 + (0.001739 )*abs(EM2_T)^3 + (-1.245e-07 )*abs(EM2_W)^4 + ( 1.2e-06)*abs(EM2_W)^3*abs(EM2_T) + ( -1.584e-06)*abs(EM2_W)^2*abs(EM2_T)^2 + ( 4.383e-07)*abs(EM2_W)*abs(EM2_T)^3 + (-2.947e-06)*abs(EM2_T)^4;
eq_c[3] := EM2_P = piecewise( EM2_T = 0, 0, EM2_W = 0, 0, EM2_W*EM2_T < 0,-1 * EM2_ReqPow_eq, EM2_ReqPow_eq);
eq_c[4] := EM2_A = EM2_P/EM2_V;

#GEN
GEN_ReqPow_eq:= (-5.28e-12) + ( 3.849e-14)*abs(GEN_W) + (-71.9)*abs(GEN_T) + (-1.168e-16)*abs(GEN_W)^2 +(1.296)*abs(GEN_W)*abs(GEN_T) + (2.489)*abs(GEN_T)^2 + (1.451e-19)*abs(GEN_W)^3 + (0.0001326)*abs(GEN_W)^2*abs(GEN_T) + (-0.008141)*abs(GEN_W)*abs(GEN_T)^2 + (-0.004539)*abs(GEN_T)^3 +(-6.325e-23)*abs(GEN_W)^4 + (-2.091e-07)*abs(GEN_W)^3*abs(GEN_T) + ( 3.455e-06)*abs(GEN_W)^2*abs(GEN_T)^2 + ( 2.499e-05)*abs(GEN_W)*abs(GEN_T)^3 + (-5.321e-05)*abs(GEN_T)^4;
eq_c[5] := GEN_P = piecewise( GEN_T = 0, 0, GEN_W = 0, 0, GEN_W*GEN_T < 0,-1 * GEN_ReqPow_eq, GEN_ReqPow_eq);
eq_c[6] := GEN_A = GEN_P/GEN_V;

#GB
FiveSpeedGearbox_R := proc(ig)
local i ,eq;
i[1]:=3.32;
i[2]:=2;
i[3]:=1.36;
i[4]:=1.01;
i[5]:=0.82;
eq:= piecewise(ig=1,i[1],ig=2, i[2],ig=3,i[3],ig=4,i[4],ig=5,i[5],1);
return eq(ig);
end proc;

eq_c[7] := GBb_T = -1/GB_R * GBa_T;
eq_c[8] := GBb_W = GB_R * GBa_W;
eq_c[9] := GB_R = FiveSpeedGearbox_R(ig);

#System Equations
sys_eqs := convert(eq1,set) union convert(eq_c,set);

#Solve for variables
symvarnames:=select(type,indets(convert(sys_eqs,list)),name);
drivers:= {ig, ICE_T,ICE_W,FD_T,FD_W};
not_drivers := symvarnames minus drivers;

#Assumptinons

assume(BAT_V::nonnegative);
assume(FD_W::nonnegative);

sol:=(solve(sys_eqs,not_drivers) assuming real);

symb_sol:=unapply(sol,convert(drivers,list)): ---> Error, (in unapply) variables must be unique and of type name

Subsequent parts don't work...

count := 0;
for i1 from 1 to 40 do
for i2 from 1 to 40 do
for i3 from 1 to 40 do
for i4 from 1 to 40 do
for i5 from 1 to 40 do
count := count + 1;
solsol2(count) := symb_sol2(i1,i2,i3,i4,5);
od;
od;
od;
od;
od;
count;

While running the last line sol:, 1 of 2 things will happen, depending on the solver. Maple17 will take a long time (30+ minutes) to solve, then report nothing, or sol will solve, but will report "some solutions have been lost".

Afterwards, evaluating symb_sol(0,0,0,0,0) will return a viable solution (real values for each of the variables).  Whereas evaluating symb_sol(0,X,0,0,0), where X <> 0, will return and empty list [].

Does anyone know how to (i) speed up the symbolic solve time?  (ii) Return ALL of the solutions?

Thanks in advance for reading this.  I've really no idea why this isn't working.  I've also attached two worksheets with the code: noGB.mw   withGB.mw

## Efficient method of repeatedly solving e...

Maple 17

Hi Maple Prime-ers!

I have a question about efficiency.  I have a set of algebraic equations with some polynomials, that I would like to solve at different points.  I've tried using a for-loop and a map-loop.  Here is a example:

n:=10000;  #Number of solving points
eq1:={b = ''a^2'', c = b^3/2, d = c^(1/2)*4 + b^2}; #Equation to solve

a := convert([seq(i,i=1..n)],Vector);  #timesteps

ans := Vector[column](n)

## Try solving in a for-next loop
t1 := time():
for q from 1 to n do
ans(q):=solve(subs({'a' = a(q)},eq1)):
od:
t2 := time() - t1;

## try solving in a map loop
t1s := time():
ans_s := map(q->solve(subs({'a' = a(q)},eq1)),a);
t2s := time() - t1s;

On my computer (2.2Ghz, 2 cores), these both take 115s to solve.  Using Map over For-Next did not speed up computational speed.

The problem I wish to tackle has 12 equations, invovles 5th order polynomials, and n ~= 300000.  Solving this set of equations takes 2-3 hours.

Anyone know a more efficient method?  Thanks for reading :D

 1 2 3 Page 2 of 3
﻿