mmcdara

5635 Reputation

17 Badges

7 years, 189 days

MaplePrimes Activity


These are replies submitted by mmcdara

@AHSAN 

Right, I updated my answer, please check it again

@jalal 

Read carefuly the ImageTools:-Read help page
First syntax:

Read( file, img, opts )

A few lines below it's written

file    -   string; the pathname of the image file to read


Ask yourself the question: is  this:///Images/Maple.jpg a string?
Obviously not, so Read will fail (for the moment your "Read" is cornered by the special symbol ":" , see the its help page).

Now, is "this:///Images/Maple.jpg" a valid path?
I assume you use Windows and that this: is a the name of some disk? Unfortunately Maple doesn't accept this ,otation: you must enter the full path of Maple.jpg, not a shortcut.
A full path is something like "//../../../Images/Maple.jpg", to get it:

  • go to the directiory Images,
  • copy paste the name full path which should appear in the address bar,
  • paste it in your worksheet,
  • add  /Maple.jpg,
  • enclose all this by double quotes,
  • change all the backslashes by slashes (maybe it's no longer necessary in recent Maple versions?).

Now you have a correct full path.

@jalal 

I thought you would have understood that my image had full path 

cat(currentdir(), "/Desktop/Maple.jpg"):

I f you have some image whose full path is My_Image_is_here, just do 

img := Read(My_Image_is_here):

I updated my previous answer and attached the mw file.

@shashi598 

UPDATED

You will find in this new file another point of view to solve numerically the Emden and Lane-Emden equations.
This is simpler than what was done before but (is it a drawback for you?) the numerical solution of the Lane-Emden equation can be computed ONLY in the range 0..Pi

ANALYSIS
Looking to the exact solution (n=1) of the Lane-Emden equation shows it contains the integration constant _C2 when initial conditions 

gamma(0) = 0, D(gamma)(0) = 0

are used.
Said otherwise these IC do not determine completely the solution, so the idea to write instead (epsilon is a priori a small strictly positive value):

gamma(epsilon) = 0, D(gamma)(epsilon) = 0

Unfortunately this leads to a  solution which doesn't verify 

gamma(Pi) + Pi * D(gamma)(Pi) = 0

Thus the quite complex procedure I used to identify two extra parameters eta and tau in this new Initial Value Problem:

ic := gamma(epsilon) = eta, D(gamma)(epsilon) = tau;
sol := dsolve({ode2(1), ic}), ....):

# find eta and ray such that 
sol(Pi) + Pi * D(sol)(Pi) = 0


CONSEQUENCE
The other approach presented here starts from the observation that 

gamma(0) = 0, D(gamma)(0) = 0

does not define a unique solution of the Lane-Emden equation.
So the idea is now to replace these initial conditions by these boundary conditions (see the attached file)

bc := D(gamma)(0) = 0, gamma(Pi) + Pi * D(gamma)(Pi) = 0

The correctness of this approach can be verified by doing this

rhs(dsolve({ode2E(1), bc}));
      (cos(xi alpha) xi alpha - sin(xi alpha)) Pi
      -------------------------------------------
            /     2    \                    2    
         xi \alpha  - 1/ sin(Pi alpha) alpha     

           /     /      2    \   2\                       
           \-2 + \-alpha  + 1/ xi / sin(xi) + 2 xi cos(xi)
         + -----------------------------------------------
                                  2            2          
                    xi (alpha - 1)  (alpha + 1)           

and by checking that this expression is equal to the one we get from the original approach (identification of _C2).

Once this understood, getting the numerical solution of the Lane-Emden equation (with a numerical solution of the Emden equation) becomes obvious (note that this require using the option method=bvp[midrich] in dsolve/numeric).
The whole procedure is much simpler than the one previously used but forbids computing the numerical solution of the Lane-Emden equation beyond xi=Pi.

Here is the file

restart

 

If you want to compute the numerical solution of the Lane-Emden equation ONLY in the range 0..Pi
you can do the things in a simpler way.

 

local gamma:

Exact solutions of Emden and Lane-Emden (n=1) equations.

 

ode1 := n -> (diff(xi^2*(diff(theta__0(xi), xi)), xi))/xi^2 = -theta__0(xi)^n;

proc (n) options operator, arrow; (diff(xi^2*(diff(theta__0(xi), xi)), xi))/xi^2 = -theta__0(xi)^n end proc

(1.1)

EmdenE := unapply( rhs(dsolve({ode1(1), theta__0(0) = 1, (D(theta__0))(0) = 0}, theta__0(xi))), xi);

proc (xi) options operator, arrow; sin(xi)/xi end proc

(1.2)

ode2E := n -> diff(gamma(xi), xi, xi)-2*gamma(xi)/xi^2+alpha^2*gamma(xi) = -EmdenE(xi)^n*xi^2:
ode2E(1)

diff(diff(gamma(xi), xi), xi)-2*gamma(xi)/xi^2+alpha^2*gamma(xi) = -xi*sin(xi)

(1.3)

LaneEmdenE := rhs(dsolve({ode2E(1), gamma(0) = 0, D(gamma)(0) = 0}));
 

(cos(xi*alpha)*xi*alpha-sin(xi*alpha))*_C2/xi+((-2+(-alpha^2+1)*xi^2)*sin(xi)+2*xi*cos(xi))/(xi*(alpha-1)^2*(alpha+1)^2)

(1.4)

AtPi := eval(LaneEmdenE+xi*diff(LaneEmdenE, xi), xi=Pi);

(cos(Pi*alpha)*Pi*alpha-sin(Pi*alpha))*_C2/Pi-2/((alpha-1)^2*(alpha+1)^2)+Pi*(-(cos(Pi*alpha)*Pi*alpha-sin(Pi*alpha))*_C2/Pi^2-sin(Pi*alpha)*_C2*alpha^2-(-alpha^2+1)*Pi/((alpha-1)^2*(alpha+1)^2)+2/(Pi*(alpha-1)^2*(alpha+1)^2))

(1.5)

HereIs_C2 := isolate(AtPi, _C2);

_C2 = (-Pi^2*alpha^2+Pi^2)/(-Pi*sin(Pi*alpha)*alpha^6+2*Pi*sin(Pi*alpha)*alpha^4-Pi*sin(Pi*alpha)*alpha^2)

(1.6)

LaneEmdenE := eval(LaneEmdenE, HereIs_C2);

(cos(xi*alpha)*xi*alpha-sin(xi*alpha))*(-Pi^2*alpha^2+Pi^2)/(xi*(-Pi*sin(Pi*alpha)*alpha^6+2*Pi*sin(Pi*alpha)*alpha^4-Pi*sin(Pi*alpha)*alpha^2))+((-2+(-alpha^2+1)*xi^2)*sin(xi)+2*xi*cos(xi))/(xi*(alpha-1)^2*(alpha+1)^2)

(1.7)

The strategy I used in my previous file
(and the same of yours  if I understood correctly what you did)

 

LaneEmdenE := rhs(dsolve({ode2E(1), gamma(0) = 0, D(gamma)(0) = 0}));
 

(cos(xi*alpha)*xi*alpha-sin(xi*alpha))*_C2/xi+((-2+(-alpha^2+1)*xi^2)*sin(xi)+2*xi*cos(xi))/(xi*(alpha-1)^2*(alpha+1)^2)

(1.1.1)

AtPi := eval(LaneEmdenE+xi*diff(LaneEmdenE, xi), xi=Pi);

(cos(Pi*alpha)*Pi*alpha-sin(Pi*alpha))*_C2/Pi-2/((alpha-1)^2*(alpha+1)^2)+Pi*(-(cos(Pi*alpha)*Pi*alpha-sin(Pi*alpha))*_C2/Pi^2-sin(Pi*alpha)*_C2*alpha^2-(-alpha^2+1)*Pi/((alpha-1)^2*(alpha+1)^2)+2/(Pi*(alpha-1)^2*(alpha+1)^2))

(1.1.2)

HereIs_C2 := isolate(AtPi, _C2);

_C2 = (-Pi^2*alpha^2+Pi^2)/(-Pi*sin(Pi*alpha)*alpha^6+2*Pi*sin(Pi*alpha)*alpha^4-Pi*sin(Pi*alpha)*alpha^2)

(1.1.3)

LaneEmdenE := eval(LaneEmdenE, HereIs_C2);

(cos(xi*alpha)*xi*alpha-sin(xi*alpha))*(-Pi^2*alpha^2+Pi^2)/(xi*(-Pi*sin(Pi*alpha)*alpha^6+2*Pi*sin(Pi*alpha)*alpha^4-Pi*sin(Pi*alpha)*alpha^2))+((-2+(-alpha^2+1)*xi^2)*sin(xi)+2*xi*cos(xi))/(xi*(alpha-1)^2*(alpha+1)^2)

(1.1.4)

A simpler strategy where the condition gamma(0) is discarded and
replaced by gamma(Pi)+Pi*D(gamma)(Pi) = 0.

(Note this substitution is going to transform the Initial Value Problem into a
Boundary Value Problem)

 

Alternative := rhs(dsolve({ode2E(1), D(gamma)(0) = 0, gamma(Pi)+Pi*D(gamma)(Pi) = 0}));

(cos(xi*alpha)*xi*alpha-sin(xi*alpha))*Pi/(xi*(alpha^2-1)*sin(Pi*alpha)*alpha^2)+((-2+(-alpha^2+1)*xi^2)*sin(xi)+2*xi*cos(xi))/(xi*(alpha-1)^2*(alpha+1)^2)

(1.2.1)

simplify(LaneEmdenE-Alternative);

0

(1.2.2)

What happens at xi=Pi?

 

LaneEmdenE := eval(LaneEmdenE, alpha=1.4);

-1.755562286*(1.4*cos(1.4*xi)*xi-sin(1.4*xi))/xi+1.085069444*((-2-.96*xi^2)*sin(xi)+2*xi*cos(xi))/xi

(1.3.1)

eval(diff(LaneEmdenE, xi), xi=Pi);

.6181907324

(1.3.2)

Numerical solutions of Emden and Lane-Emden equations.

 

ode2 := n -> diff(gamma(xi), xi, xi)-2*gamma(xi)/xi^2+alpha^2*gamma(xi) = -f(xi)^n*xi^2;

proc (n) options operator, arrow; diff(gamma(xi), xi, xi)-2*gamma(xi)/xi^2+alpha^2*gamma(xi) = -f(xi)^n*xi^2 end proc

(2.1)

EmdenN := dsolve({ode1(1), theta__0(0) = 1, (D(theta__0))(0) = 0}, theta__0(xi), numeric);

proc (x_rkf45) local _res, _dat, _vars, _solnproc, _xout, _ndsol, _pars, _n, _i; option `Copyright (c) 2000 by Waterloo Maple Inc. All rights reserved.`; if 1 < nargs then error "invalid input: too many arguments" end if; _EnvDSNumericSaveDigits := Digits; Digits := 15; if _EnvInFsolve = true then _xout := evalf[_EnvDSNumericSaveDigits](x_rkf45) else _xout := evalf(x_rkf45) end if; _dat := Array(1..4, {(1) = proc (_xin) local _xout, _dtbl, _dat, _vmap, _x0, _y0, _val, _dig, _n, _ne, _nd, _nv, _pars, _ini, _par, _i, _j, _k, _src; option `Copyright (c) 2002 by Waterloo Maple Inc. All rights reserved.`; table( [( "complex" ) = false ] ) _xout := _xin; _pars := []; _dtbl := array( 1 .. 4, [( 1 ) = (array( 1 .. 24, [( 1 ) = (datatype = float[8], order = C_order, storage = rectangular), ( 2 ) = (datatype = float[8], order = C_order, storage = rectangular), ( 3 ) = ([0, 0, 0, Array(1..0, 1..2, {}, datatype = float[8], order = C_order)]), ( 4 ) = (Array(1..54, {(1) = 2, (2) = 2, (3) = 0, (4) = 0, (5) = 0, (6) = 0, (7) = 1, (8) = 0, (9) = 0, (10) = 0, (11) = 0, (12) = 0, (13) = 0, (14) = 0, (15) = 0, (16) = 0, (17) = 0, (18) = 1, (19) = 30000, (20) = 0, (21) = 0, (22) = 1, (23) = 4, (24) = 0, (25) = 1, (26) = 15, (27) = 1, (28) = 0, (29) = 1, (30) = 3, (31) = 3, (32) = 0, (33) = 1, (34) = 0, (35) = 0, (36) = 0, (37) = 0, (38) = 0, (39) = 0, (40) = 0, (41) = 0, (42) = 0, (43) = 1, (44) = 0, (45) = 0, (46) = 0, (47) = 0, (48) = 0, (49) = 0, (50) = 50, (51) = 1, (52) = 0, (53) = 0, (54) = 0}, datatype = integer[8])), ( 5 ) = (Array(1..28, {(1) = .0, (2) = 0.10e-5, (3) = .0, (4) = 0.500001e-14, (5) = .0, (6) = 0.15142976267524639e-1, (7) = .0, (8) = 0.10e-5, (9) = .0, (10) = .0, (11) = .0, (12) = .0, (13) = 1.0, (14) = .0, (15) = .49999999999999, (16) = .0, (17) = 1.0, (18) = 1.0, (19) = .0, (20) = .0, (21) = 1.0, (22) = 1.0, (23) = .0, (24) = .0, (25) = 0.10e-14, (26) = .0, (27) = .0, (28) = .0}, datatype = float[8], order = C_order)), ( 6 ) = (Array(1..2, {(1) = 1.0, (2) = .0}, datatype = float[8], order = C_order)), ( 7 ) = ([Array(1..4, 1..7, {(1, 1) = .0, (1, 2) = .203125, (1, 3) = .3046875, (1, 4) = .75, (1, 5) = .8125, (1, 6) = .40625, (1, 7) = .8125, (2, 1) = 0.6378173828125e-1, (2, 2) = .0, (2, 3) = .279296875, (2, 4) = .27237892150878906, (2, 5) = -0.9686851501464844e-1, (2, 6) = 0.1956939697265625e-1, (2, 7) = .5381584167480469, (3, 1) = 0.31890869140625e-1, (3, 2) = .0, (3, 3) = -.34375, (3, 4) = -.335235595703125, (3, 5) = .2296142578125, (3, 6) = .41748046875, (3, 7) = 11.480712890625, (4, 1) = 0.9710520505905151e-1, (4, 2) = .0, (4, 3) = .40350341796875, (4, 4) = 0.20297467708587646e-1, (4, 5) = -0.6054282188415527e-2, (4, 6) = -0.4770040512084961e-1, (4, 7) = .77858567237854}, datatype = float[8], order = C_order), Array(1..6, 1..6, {(1, 1) = .0, (1, 2) = .0, (1, 3) = .0, (1, 4) = .0, (1, 5) = .0, (1, 6) = 1.0, (2, 1) = .25, (2, 2) = .0, (2, 3) = .0, (2, 4) = .0, (2, 5) = .0, (2, 6) = 1.0, (3, 1) = .1875, (3, 2) = .5625, (3, 3) = .0, (3, 4) = .0, (3, 5) = .0, (3, 6) = 2.0, (4, 1) = .23583984375, (4, 2) = -.87890625, (4, 3) = .890625, (4, 4) = .0, (4, 5) = .0, (4, 6) = .2681884765625, (5, 1) = .1272735595703125, (5, 2) = -.5009765625, (5, 3) = .44921875, (5, 4) = -0.128936767578125e-1, (5, 5) = .0, (5, 6) = 0.626220703125e-1, (6, 1) = -0.927734375e-1, (6, 2) = .626220703125, (6, 3) = -.4326171875, (6, 4) = .1418304443359375, (6, 5) = -0.861053466796875e-1, (6, 6) = .3131103515625}, datatype = float[8], order = C_order), Array(1..6, {(1) = .0, (2) = .386, (3) = .21, (4) = .63, (5) = 1.0, (6) = 1.0}, datatype = float[8], order = C_order), Array(1..6, {(1) = .25, (2) = -.1043, (3) = .1035, (4) = -0.362e-1, (5) = .0, (6) = .0}, datatype = float[8], order = C_order), Array(1..6, 1..5, {(1, 1) = .0, (1, 2) = .0, (1, 3) = .0, (1, 4) = .0, (1, 5) = .0, (2, 1) = 1.544, (2, 2) = .0, (2, 3) = .0, (2, 4) = .0, (2, 5) = .0, (3, 1) = .9466785280815533, (3, 2) = .25570116989825814, (3, 3) = .0, (3, 4) = .0, (3, 5) = .0, (4, 1) = 3.3148251870684886, (4, 2) = 2.896124015972123, (4, 3) = .9986419139977808, (4, 4) = .0, (4, 5) = .0, (5, 1) = 1.2212245092262748, (5, 2) = 6.019134481287752, (5, 3) = 12.537083329320874, (5, 4) = -.687886036105895, (5, 5) = .0, (6, 1) = 1.2212245092262748, (6, 2) = 6.019134481287752, (6, 3) = 12.537083329320874, (6, 4) = -.687886036105895, (6, 5) = 1.0}, datatype = float[8], order = C_order), Array(1..6, 1..5, {(1, 1) = .0, (1, 2) = .0, (1, 3) = .0, (1, 4) = .0, (1, 5) = .0, (2, 1) = -5.6688, (2, 2) = .0, (2, 3) = .0, (2, 4) = .0, (2, 5) = .0, (3, 1) = -2.4300933568337584, (3, 2) = -.20635991570891224, (3, 3) = .0, (3, 4) = .0, (3, 5) = .0, (4, 1) = -.10735290581452621, (4, 2) = -9.594562251021896, (4, 3) = -20.470286148096154, (4, 4) = .0, (4, 5) = .0, (5, 1) = 7.496443313968615, (5, 2) = -10.246804314641219, (5, 3) = -33.99990352819906, (5, 4) = 11.708908932061595, (5, 5) = .0, (6, 1) = 8.083246795922411, (6, 2) = -7.981132988062785, (6, 3) = -31.52159432874373, (6, 4) = 16.319305431231363, (6, 5) = -6.0588182388340535}, datatype = float[8], order = C_order), Array(1..3, 1..5, {(1, 1) = .0, (1, 2) = .0, (1, 3) = .0, (1, 4) = .0, (1, 5) = .0, (2, 1) = 10.126235083446911, (2, 2) = -7.487995877607633, (2, 3) = -34.800918615557414, (2, 4) = -7.9927717075687275, (2, 5) = 1.0251377232956207, (3, 1) = -.6762803392806898, (3, 2) = 6.087714651678606, (3, 3) = 16.43084320892463, (3, 4) = 24.767225114183653, (3, 5) = -6.5943891257167815}, datatype = float[8], order = C_order)]), ( 9 ) = ([Array(1..2, {(1) = .1, (2) = .1}, datatype = float[8], order = C_order), Array(1..2, {(1) = .0, (2) = .0}, datatype = float[8], order = C_order), Array(1..2, {(1) = .0, (2) = .0}, datatype = float[8], order = C_order), Array(1..2, {(1) = .0, (2) = .0}, datatype = float[8], order = C_order), Array(1..2, {(1) = .0, (2) = .0}, datatype = float[8], order = C_order), Array(1..2, 1..2, {(1, 1) = .0, (1, 2) = .0, (2, 1) = .0, (2, 2) = .0}, datatype = float[8], order = C_order), Array(1..2, 1..2, {(1, 1) = .0, (1, 2) = .0, (2, 1) = .0, (2, 2) = .0}, datatype = float[8], order = C_order), Array(1..2, 1..6, {(1, 1) = .0, (1, 2) = .0, (1, 3) = .0, (1, 4) = .0, (1, 5) = .0, (1, 6) = .0, (2, 1) = .0, (2, 2) = .0, (2, 3) = .0, (2, 4) = .0, (2, 5) = .0, (2, 6) = .0}, datatype = float[8], order = C_order), Array(1..2, {(1) = 0, (2) = 0}, datatype = integer[8]), Array(1..2, {(1) = .0, (2) = .0}, datatype = float[8], order = C_order), Array(1..2, {(1) = .0, (2) = .0}, datatype = float[8], order = C_order), Array(1..2, {(1) = .0, (2) = .0}, datatype = float[8], order = C_order), Array(1..2, {(1) = .0, (2) = .0}, datatype = float[8], order = C_order), Array(1..2, {(1) = .0, (2) = .0}, datatype = float[8], order = C_order)]), ( 8 ) = ([Array(1..2, {(1) = 1.0, (2) = .0}, datatype = float[8], order = C_order), Array(1..2, {(1) = .0, (2) = .0}, datatype = float[8], order = C_order), Array(1..2, {(1) = .0, (2) = -.3333333333333333}, datatype = float[8], order = C_order), 0, 0]), ( 11 ) = (Array(1..6, 0..2, {(1, 1) = .0, (1, 2) = .0, (2, 0) = .0, (2, 1) = .0, (2, 2) = .0, (3, 0) = .0, (3, 1) = .0, (3, 2) = .0, (4, 0) = .0, (4, 1) = .0, (4, 2) = .0, (5, 0) = .0, (5, 1) = .0, (5, 2) = .0, (6, 0) = .0, (6, 1) = .0, (6, 2) = .0}, datatype = float[8], order = C_order)), ( 10 ) = ([proc (N, xi, Y, YP) option `[Y[1] = theta__0(xi), Y[2] = diff(theta__0(xi),xi)]`; if xi = 0 then if abs(Y[2]) <= 0. then YP[1] := 0; YP[2] := -(1/3)*Y[1] else error "system with provided initial conditions is singular" end if else YP[1] := Y[2]; YP[2] := -Y[1]-2*Y[2]/xi end if; 0 end proc, -1, 0, 0, 0, 0, 0, 0]), ( 13 ) = (), ( 12 ) = (), ( 15 ) = ("rkf45"), ( 14 ) = ([0, 0]), ( 18 ) = ([]), ( 19 ) = (0), ( 16 ) = ([0, 0, 0, []]), ( 17 ) = ([proc (N, xi, Y, YP) option `[Y[1] = theta__0(xi), Y[2] = diff(theta__0(xi),xi)]`; if xi = 0 then if abs(Y[2]) <= 0. then YP[1] := 0; YP[2] := -(1/3)*Y[1] else error "system with provided initial conditions is singular" end if else YP[1] := Y[2]; YP[2] := -Y[1]-2*Y[2]/xi end if; 0 end proc, -1, 0, 0, 0, 0, 0, 0]), ( 22 ) = (0), ( 23 ) = (0), ( 20 ) = ([]), ( 21 ) = (0), ( 24 ) = (0)  ] ))  ] ); _y0 := Array(0..2, {(1) = 0., (2) = 1.}); _vmap := array( 1 .. 2, [( 1 ) = (1), ( 2 ) = (2)  ] ); _x0 := _dtbl[1][5][5]; _n := _dtbl[1][4][1]; _ne := _dtbl[1][4][3]; _nd := _dtbl[1][4][4]; _nv := _dtbl[1][4][16]; if not type(_xout, 'numeric') then if member(_xout, ["start", "left", "right"]) then if _Env_smart_dsolve_numeric = true or _dtbl[1][4][10] = 1 then if _xout = "left" then if type(_dtbl[2], 'table') then return _dtbl[2][5][1] end if elif _xout = "right" then if type(_dtbl[3], 'table') then return _dtbl[3][5][1] end if end if end if; return _dtbl[1][5][5] elif _xout = "method" then return _dtbl[1][15] elif _xout = "storage" then return evalb(_dtbl[1][4][10] = 1) elif _xout = "leftdata" then if not type(_dtbl[2], 'array') then return NULL else return eval(_dtbl[2]) end if elif _xout = "rightdata" then if not type(_dtbl[3], 'array') then return NULL else return eval(_dtbl[3]) end if elif _xout = "enginedata" then return eval(_dtbl[1]) elif _xout = "enginereset" then _dtbl[2] := evaln(_dtbl[2]); _dtbl[3] := evaln(_dtbl[3]); return NULL elif _xout = "initial" then return procname(_y0[0]) elif _xout = "laxtol" then return _dtbl[`if`(member(_dtbl[4], {2, 3}), _dtbl[4], 1)][5][18] elif _xout = "numfun" then return `if`(member(_dtbl[4], {2, 3}), _dtbl[_dtbl[4]][4][18], 0) elif _xout = "parameters" then return [seq(_y0[_n+_i], _i = 1 .. nops(_pars))] elif _xout = "initial_and_parameters" then return procname(_y0[0]), [seq(_y0[_n+_i], _i = 1 .. nops(_pars))] elif _xout = "last" then if _dtbl[4] <> 2 and _dtbl[4] <> 3 or _x0-_dtbl[_dtbl[4]][5][1] = 0. then error "no information is available on last computed point" else _xout := _dtbl[_dtbl[4]][5][1] end if elif _xout = "function" then if _dtbl[1][4][33]-2. = 0 then return eval(_dtbl[1][10], 1) else return eval(_dtbl[1][10][1], 1) end if elif _xout = "map" then return copy(_vmap) elif type(_xin, `=`) and type(rhs(_xin), 'list') and member(lhs(_xin), {"initial", "parameters", "initial_and_parameters"}) then error "initial conditions cannot be changed for systems with removable singularities"; _ini, _par := [], []; if lhs(_xin) = "initial" then _ini := rhs(_xin) elif lhs(_xin) = "parameters" then _par := rhs(_xin) elif select(type, rhs(_xin), `=`) <> [] then _par, _ini := selectremove(type, rhs(_xin), `=`) elif nops(rhs(_xin)) < nops(_pars)+1 then error "insufficient data for specification of initial and parameters" else _par := rhs(_xin)[-nops(_pars) .. -1]; _ini := rhs(_xin)[1 .. -nops(_pars)-1] end if; _xout := lhs(_xout); if _par <> [] then `dsolve/numeric/process_parameters`(_n, _pars, _par, _y0) end if; if _ini <> [] then `dsolve/numeric/process_initial`(_n-_ne, _ini, _y0, _pars, _vmap) end if; `dsolve/numeric/SC/reinitialize`(_dtbl, _y0, _n, procname, _pars); if _Env_smart_dsolve_numeric = true and type(_y0[0], 'numeric') and _dtbl[1][4][10] <> 1 then procname("right") := _y0[0]; procname("left") := _y0[0] end if; if _xout = "initial" then return [_y0[0], seq(_y0[_vmap[_i]], _i = 1 .. _n-_ne)] elif _xout = "parameters" then return [seq(_y0[_n+_i], _i = 1 .. nops(_pars))] else return [_y0[0], seq(_y0[_vmap[_i]], _i = 1 .. _n-_ne)], [seq(_y0[_n+_i], _i = 1 .. nops(_pars))] end if elif _xin = "eventstop" then if _nv = 0 then error "this solution has no events" end if; _i := _dtbl[4]; if _i <> 2 and _i <> 3 then return 0 end if; if _dtbl[_i][4][10] = 1 and assigned(_dtbl[5-_i]) and _dtbl[_i][4][9] < 100 and 100 <= _dtbl[5-_i][4][9] then _i := 5-_i; _dtbl[4] := _i; _j := round(_dtbl[_i][4][17]); return round(_dtbl[_i][3][1][_j, 1]) elif 100 <= _dtbl[_i][4][9] then _j := round(_dtbl[_i][4][17]); return round(_dtbl[_i][3][1][_j, 1]) else return 0 end if elif _xin = "eventstatus" then if _nv = 0 then error "this solution has no events" end if; _i := [selectremove(proc (a) options operator, arrow; _dtbl[1][3][1][a, 7] = 1 end proc, {seq(_j, _j = 1 .. round(_dtbl[1][3][1][_nv+1, 1]))})]; return ':-enabled' = _i[1], ':-disabled' = _i[2] elif _xin = "eventclear" then if _nv = 0 then error "this solution has no events" end if; _i := _dtbl[4]; if _i <> 2 and _i <> 3 then error "no events to clear" end if; if _dtbl[_i][4][10] = 1 and assigned(_dtbl[5-_i]) and _dtbl[_i][4][9] < 100 and 100 < _dtbl[5-_i][4][9] then _dtbl[4] := 5-_i; _i := 5-_i end if; if _dtbl[_i][4][9] < 100 then error "no events to clear" elif _nv < _dtbl[_i][4][9]-100 then error "event error condition cannot be cleared" else _j := _dtbl[_i][4][9]-100; if irem(round(_dtbl[_i][3][1][_j, 4]), 2) = 1 then error "retriggerable events cannot be cleared" end if; _j := round(_dtbl[_i][3][1][_j, 1]); for _k to _nv do if _dtbl[_i][3][1][_k, 1] = _j then if _dtbl[_i][3][1][_k, 2] = 3 then error "range events cannot be cleared" end if; _dtbl[_i][3][1][_k, 8] := _dtbl[_i][3][1][_nv+1, 8] end if end do; _dtbl[_i][4][17] := 0; _dtbl[_i][4][9] := 0; if _dtbl[1][4][10] = 1 then if _i = 2 then try procname(procname("left")) catch:  end try else try procname(procname("right")) catch:  end try end if end if end if; return  elif type(_xin, `=`) and member(lhs(_xin), {"eventdisable", "eventenable"}) then if _nv = 0 then error "this solution has no events" end if; if type(rhs(_xin), {('list')('posint'), ('set')('posint')}) then _i := {op(rhs(_xin))} elif type(rhs(_xin), 'posint') then _i := {rhs(_xin)} else error "event identifiers must be integers in the range 1..%1", round(_dtbl[1][3][1][_nv+1, 1]) end if; if select(proc (a) options operator, arrow; _nv < a end proc, _i) <> {} then error "event identifiers must be integers in the range 1..%1", round(_dtbl[1][3][1][_nv+1, 1]) end if; _k := {}; for _j to _nv do if member(round(_dtbl[1][3][1][_j, 1]), _i) then _k := `union`(_k, {_j}) end if end do; _i := _k; if lhs(_xin) = "eventdisable" then _dtbl[4] := 0; _j := [evalb(assigned(_dtbl[2]) and member(_dtbl[2][4][17], _i)), evalb(assigned(_dtbl[3]) and member(_dtbl[3][4][17], _i))]; for _k in _i do _dtbl[1][3][1][_k, 7] := 0; if assigned(_dtbl[2]) then _dtbl[2][3][1][_k, 7] := 0 end if; if assigned(_dtbl[3]) then _dtbl[3][3][1][_k, 7] := 0 end if end do; if _j[1] then for _k to _nv+1 do if _k <= _nv and not type(_dtbl[2][3][4][_k, 1], 'undefined') then userinfo(3, {'events', 'eventreset'}, `reinit #2, event code `, _k, ` to defined init `, _dtbl[2][3][4][_k, 1]); _dtbl[2][3][1][_k, 8] := _dtbl[2][3][4][_k, 1] elif _dtbl[2][3][1][_k, 2] = 0 and irem(iquo(round(_dtbl[2][3][1][_k, 4]), 32), 2) = 1 then userinfo(3, {'events', 'eventreset'}, `reinit #2, event code `, _k, ` to rate hysteresis init `, _dtbl[2][5][24]); _dtbl[2][3][1][_k, 8] := _dtbl[2][5][24] elif _dtbl[2][3][1][_k, 2] = 0 and irem(iquo(round(_dtbl[2][3][1][_k, 4]), 2), 2) = 0 then userinfo(3, {'events', 'eventreset'}, `reinit #2, event code `, _k, ` to initial init `, _x0); _dtbl[2][3][1][_k, 8] := _x0 else userinfo(3, {'events', 'eventreset'}, `reinit #2, event code `, _k, ` to fireinitial init `, _x0-1); _dtbl[2][3][1][_k, 8] := _x0-1 end if end do; _dtbl[2][4][17] := 0; _dtbl[2][4][9] := 0; if _dtbl[1][4][10] = 1 then procname(procname("left")) end if end if; if _j[2] then for _k to _nv+1 do if _k <= _nv and not type(_dtbl[3][3][4][_k, 2], 'undefined') then userinfo(3, {'events', 'eventreset'}, `reinit #3, event code `, _k, ` to defined init `, _dtbl[3][3][4][_k, 2]); _dtbl[3][3][1][_k, 8] := _dtbl[3][3][4][_k, 2] elif _dtbl[3][3][1][_k, 2] = 0 and irem(iquo(round(_dtbl[3][3][1][_k, 4]), 32), 2) = 1 then userinfo(3, {'events', 'eventreset'}, `reinit #3, event code `, _k, ` to rate hysteresis init `, _dtbl[3][5][24]); _dtbl[3][3][1][_k, 8] := _dtbl[3][5][24] elif _dtbl[3][3][1][_k, 2] = 0 and irem(iquo(round(_dtbl[3][3][1][_k, 4]), 2), 2) = 0 then userinfo(3, {'events', 'eventreset'}, `reinit #3, event code `, _k, ` to initial init `, _x0); _dtbl[3][3][1][_k, 8] := _x0 else userinfo(3, {'events', 'eventreset'}, `reinit #3, event code `, _k, ` to fireinitial init `, _x0+1); _dtbl[3][3][1][_k, 8] := _x0+1 end if end do; _dtbl[3][4][17] := 0; _dtbl[3][4][9] := 0; if _dtbl[1][4][10] = 1 then procname(procname("right")) end if end if else for _k in _i do _dtbl[1][3][1][_k, 7] := 1 end do; _dtbl[2] := evaln(_dtbl[2]); _dtbl[3] := evaln(_dtbl[3]); _dtbl[4] := 0; if _dtbl[1][4][10] = 1 then if _x0 <= procname("right") then try procname(procname("right")) catch:  end try end if; if procname("left") <= _x0 then try procname(procname("left")) catch:  end try end if end if end if; return  elif type(_xin, `=`) and lhs(_xin) = "eventfired" then if not type(rhs(_xin), 'list') then error "'eventfired' must be specified as a list" end if; if _nv = 0 then error "this solution has no events" end if; if _dtbl[4] <> 2 and _dtbl[4] <> 3 then error "'direction' must be set prior to calling/setting 'eventfired'" end if; _i := _dtbl[4]; _val := NULL; if not assigned(_EnvEventRetriggerWarned) then _EnvEventRetriggerWarned := false end if; for _k in rhs(_xin) do if type(_k, 'integer') then _src := _k elif type(_k, 'integer' = 'anything') and type(evalf(rhs(_k)), 'numeric') then _k := lhs(_k) = evalf[max(Digits, 18)](rhs(_k)); _src := lhs(_k) else error "'eventfired' entry is not valid: %1", _k end if; if _src < 1 or round(_dtbl[1][3][1][_nv+1, 1]) < _src then error "event identifiers must be integers in the range 1..%1", round(_dtbl[1][3][1][_nv+1, 1]) end if; _src := {seq(`if`(_dtbl[1][3][1][_j, 1]-_src = 0., _j, NULL), _j = 1 .. _nv)}; if nops(_src) <> 1 then error "'eventfired' can only be set/queried for root-finding events and time/interval events" end if; _src := _src[1]; if _dtbl[1][3][1][_src, 2] <> 0. and _dtbl[1][3][1][_src, 2]-2. <> 0. then error "'eventfired' can only be set/queried for root-finding events and time/interval events" elif irem(round(_dtbl[1][3][1][_src, 4]), 2) = 1 then if _EnvEventRetriggerWarned = false then WARNING(`'eventfired' has no effect on events that retrigger`) end if; _EnvEventRetriggerWarned := true end if; if _dtbl[_i][3][1][_src, 2] = 0 and irem(iquo(round(_dtbl[_i][3][1][_src, 4]), 32), 2) = 1 then _val := _val, undefined elif type(_dtbl[_i][3][4][_src, _i-1], 'undefined') or _i = 2 and _dtbl[2][3][1][_src, 8] < _dtbl[2][3][4][_src, 1] or _i = 3 and _dtbl[3][3][4][_src, 2] < _dtbl[3][3][1][_src, 8] then _val := _val, _dtbl[_i][3][1][_src, 8] else _val := _val, _dtbl[_i][3][4][_src, _i-1] end if; if type(_k, `=`) then if _dtbl[_i][3][1][_src, 2] = 0 and irem(iquo(round(_dtbl[_i][3][1][_src, 4]), 32), 2) = 1 then error "cannot set event code for a rate hysteresis event" end if; userinfo(3, {'events', 'eventreset'}, `manual set event code `, _src, ` to value `, rhs(_k)); _dtbl[_i][3][1][_src, 8] := rhs(_k); _dtbl[_i][3][4][_src, _i-1] := rhs(_k) end if end do; return [_val] elif type(_xin, `=`) and lhs(_xin) = "direction" then if not member(rhs(_xin), {-1, 1, ':-left', ':-right'}) then error "'direction' must be specified as either '1' or 'right' (positive) or '-1' or 'left' (negative)" end if; _src := `if`(_dtbl[4] = 2, -1, `if`(_dtbl[4] = 3, 1, undefined)); _i := `if`(member(rhs(_xin), {1, ':-right'}), 3, 2); _dtbl[4] := _i; _dtbl[_i] := `dsolve/numeric/SC/IVPdcopy`(_dtbl[1], `if`(assigned(_dtbl[_i]), _dtbl[_i], NULL)); if 0 < _nv then for _j to _nv+1 do if _j <= _nv and not type(_dtbl[_i][3][4][_j, _i-1], 'undefined') then userinfo(3, {'events', 'eventreset'}, `reinit #4, event code `, _j, ` to defined init `, _dtbl[_i][3][4][_j, _i-1]); _dtbl[_i][3][1][_j, 8] := _dtbl[_i][3][4][_j, _i-1] elif _dtbl[_i][3][1][_j, 2] = 0 and irem(iquo(round(_dtbl[_i][3][1][_j, 4]), 32), 2) = 1 then userinfo(3, {'events', 'eventreset'}, `reinit #4, event code `, _j, ` to rate hysteresis init `, _dtbl[_i][5][24]); _dtbl[_i][3][1][_j, 8] := _dtbl[_i][5][24] elif _dtbl[_i][3][1][_j, 2] = 0 and irem(iquo(round(_dtbl[_i][3][1][_j, 4]), 2), 2) = 0 then userinfo(3, {'events', 'eventreset'}, `reinit #4, event code `, _j, ` to initial init `, _x0); _dtbl[_i][3][1][_j, 8] := _x0 else userinfo(3, {'events', 'eventreset'}, `reinit #4, event code `, _j, ` to fireinitial init `, _x0-2*_i+5.0); _dtbl[_i][3][1][_j, 8] := _x0-2*_i+5.0 end if end do end if; return _src elif _xin = "eventcount" then if _dtbl[1][3][1] = 0 or _dtbl[4] <> 2 and _dtbl[4] <> 3 then return 0 else return round(_dtbl[_dtbl[4]][3][1][_nv+1, 12]) end if else return "procname" end if end if; if _xout = _x0 then return [_x0, seq(evalf(_dtbl[1][6][_vmap[_i]]), _i = 1 .. _n-_ne)] end if; _i := `if`(_x0 <= _xout, 3, 2); if _xin = "last" and 0 < _dtbl[_i][4][9] and _dtbl[_i][4][9] < 100 then _dat := eval(_dtbl[_i], 2); _j := _dat[4][20]; return [_dat[11][_j, 0], seq(_dat[11][_j, _vmap[_i]], _i = 1 .. _n-_ne-_nd), seq(_dat[8][1][_vmap[_i]], _i = _n-_ne-_nd+1 .. _n-_ne)] end if; if not type(_dtbl[_i], 'array') then _dtbl[_i] := `dsolve/numeric/SC/IVPdcopy`(_dtbl[1], `if`(assigned(_dtbl[_i]), _dtbl[_i], NULL)); if 0 < _nv then for _j to _nv+1 do if _j <= _nv and not type(_dtbl[_i][3][4][_j, _i-1], 'undefined') then userinfo(3, {'events', 'eventreset'}, `reinit #5, event code `, _j, ` to defined init `, _dtbl[_i][3][4][_j, _i-1]); _dtbl[_i][3][1][_j, 8] := _dtbl[_i][3][4][_j, _i-1] elif _dtbl[_i][3][1][_j, 2] = 0 and irem(iquo(round(_dtbl[_i][3][1][_j, 4]), 32), 2) = 1 then userinfo(3, {'events', 'eventreset'}, `reinit #5, event code `, _j, ` to rate hysteresis init `, _dtbl[_i][5][24]); _dtbl[_i][3][1][_j, 8] := _dtbl[_i][5][24] elif _dtbl[_i][3][1][_j, 2] = 0 and irem(iquo(round(_dtbl[_i][3][1][_j, 4]), 2), 2) = 0 then userinfo(3, {'events', 'eventreset'}, `reinit #5, event code `, _j, ` to initial init `, _x0); _dtbl[_i][3][1][_j, 8] := _x0 else userinfo(3, {'events', 'eventreset'}, `reinit #5, event code `, _j, ` to fireinitial init `, _x0-2*_i+5.0); _dtbl[_i][3][1][_j, 8] := _x0-2*_i+5.0 end if end do end if end if; if _xin <> "last" then if 0 < 0 then if `dsolve/numeric/checkglobals`(op(_dtbl[1][14]), _pars, _n, _y0) then `dsolve/numeric/SC/reinitialize`(_dtbl, _y0, _n, procname, _pars, _i) end if end if; if _dtbl[1][4][7] = 0 then error "parameters must be initialized before solution can be computed" end if end if; _dat := eval(_dtbl[_i], 2); _dtbl[4] := _i; try _src := `dsolve/numeric/SC/IVPrun`(_dat, _xout) catch: userinfo(2, `dsolve/debug`, print(`Exception in solnproc:`, [lastexception][2 .. -1])); error  end try; if _src = 0 and 100 < _dat[4][9] then _val := _dat[3][1][_nv+1, 8] else _val := _dat[11][_dat[4][20], 0] end if; if _src <> 0 or _dat[4][9] <= 0 then _dtbl[1][5][1] := _xout else _dtbl[1][5][1] := _val end if; if _i = 3 and _val < _xout then Rounding := -infinity; if _dat[4][9] = 1 then error "cannot evaluate the solution further right of %1, probably a singularity", evalf[8](_val) elif _dat[4][9] = 2 then error "cannot evaluate the solution further right of %1, maxfun limit exceeded (see ?dsolve,maxfun for details)", evalf[8](_val) elif _dat[4][9] = 3 then if _dat[4][25] = 3 then error "cannot evaluate the solution past the initial point, problem may be initially singular or improperly set up" else error "cannot evaluate the solution past the initial point, problem may be complex, initially singular or improperly set up" end if elif _dat[4][9] = 4 then error "cannot evaluate the solution further right of %1, accuracy goal cannot be achieved with specified 'minstep'", evalf[8](_val) elif _dat[4][9] = 5 then error "cannot evaluate the solution further right of %1, too many step failures, tolerances may be too loose for problem", evalf[8](_val) elif _dat[4][9] = 6 then error "cannot evaluate the solution further right of %1, cannot downgrade delay storage for problems with delay derivative order > 1, try increasing delaypts", evalf[8](_val) elif _dat[4][9] = 10 then error "cannot evaluate the solution further right of %1, interrupt requested", evalf[8](_val) elif 100 < _dat[4][9] then if _dat[4][9]-100 = _nv+1 then error "constraint projection failure on event at t=%1", evalf[8](_val) elif _dat[4][9]-100 = _nv+2 then error "index-1 and derivative evaluation failure on event at t=%1", evalf[8](_val) elif _dat[4][9]-100 = _nv+3 then error "maximum number of event iterations reached (%1) at t=%2", round(_dat[3][1][_nv+1, 3]), evalf[8](_val) else if _Env_dsolve_nowarnstop <> true then `dsolve/numeric/warning`(StringTools:-FormatMessage("cannot evaluate the solution further right of %1, event #%2 triggered a halt", evalf[8](_val), round(_dat[3][1][_dat[4][9]-100, 1]))) end if; Rounding := 'nearest'; _xout := _val end if else error "cannot evaluate the solution further right of %1", evalf[8](_val) end if elif _i = 2 and _xout < _val then Rounding := infinity; if _dat[4][9] = 1 then error "cannot evaluate the solution further left of %1, probably a singularity", evalf[8](_val) elif _dat[4][9] = 2 then error "cannot evaluate the solution further left of %1, maxfun limit exceeded (see ?dsolve,maxfun for details)", evalf[8](_val) elif _dat[4][9] = 3 then if _dat[4][25] = 3 then error "cannot evaluate the solution past the initial point, problem may be initially singular or improperly set up" else error "cannot evaluate the solution past the initial point, problem may be complex, initially singular or improperly set up" end if elif _dat[4][9] = 4 then error "cannot evaluate the solution further left of %1, accuracy goal cannot be achieved with specified 'minstep'", evalf[8](_val) elif _dat[4][9] = 5 then error "cannot evaluate the solution further left of %1, too many step failures, tolerances may be too loose for problem", evalf[8](_val) elif _dat[4][9] = 6 then error "cannot evaluate the solution further left of %1, cannot downgrade delay storage for problems with delay derivative order > 1, try increasing delaypts", evalf[8](_val) elif _dat[4][9] = 10 then error "cannot evaluate the solution further right of %1, interrupt requested", evalf[8](_val) elif 100 < _dat[4][9] then if _dat[4][9]-100 = _nv+1 then error "constraint projection failure on event at t=%1", evalf[8](_val) elif _dat[4][9]-100 = _nv+2 then error "index-1 and derivative evaluation failure on event at t=%1", evalf[8](_val) elif _dat[4][9]-100 = _nv+3 then error "maximum number of event iterations reached (%1) at t=%2", round(_dat[3][1][_nv+1, 3]), evalf[8](_val) else if _Env_dsolve_nowarnstop <> true then `dsolve/numeric/warning`(StringTools:-FormatMessage("cannot evaluate the solution further left of %1, event #%2 triggered a halt", evalf[8](_val), round(_dat[3][1][_dat[4][9]-100, 1]))) end if; Rounding := 'nearest'; _xout := _val end if else error "cannot evaluate the solution further left of %1", evalf[8](_val) end if end if; if _EnvInFsolve = true then _dig := _dat[4][26]; _dat[4][26] := _EnvDSNumericSaveDigits; _Env_dsolve_SC_native := true; if _dat[4][25] = 1 then _i := 1; _dat[4][25] := 2 else _i := _dat[4][25] end if; _val := `dsolve/numeric/SC/IVPval`(_dat, _xout, _src); _dat[4][25] := _i; _dat[4][26] := _dig; [_xout, seq(_val[_vmap[_i]], _i = 1 .. _n-_ne)] else Digits := _dat[4][26]; _val := `dsolve/numeric/SC/IVPval`(eval(_dat, 2), _xout, _src); [_xout, seq(_val[_vmap[_i]], _i = 1 .. _n-_ne)] end if end proc, (2) = Array(0..0, {}), (3) = [xi, theta__0(xi), diff(theta__0(xi), xi)], (4) = []}); _vars := _dat[3]; _pars := map(rhs, _dat[4]); _n := nops(_vars)-1; _solnproc := _dat[1]; if not type(_xout, 'numeric') then if member(x_rkf45, ["start", 'start', "method", 'method', "left", 'left', "right", 'right', "leftdata", "rightdata", "enginedata", "eventstop", 'eventstop', "eventclear", 'eventclear', "eventstatus", 'eventstatus', "eventcount", 'eventcount', "laxtol", 'laxtol', "numfun", 'numfun', NULL]) then _res := _solnproc(convert(x_rkf45, 'string')); if 1 < nops([_res]) then return _res elif type(_res, 'array') then return eval(_res, 1) elif _res <> "procname" then return _res end if elif member(x_rkf45, ["last", 'last', "initial", 'initial', "parameters", 'parameters', "initial_and_parameters", 'initial_and_parameters', NULL]) then _xout := convert(x_rkf45, 'string'); _res := _solnproc(_xout); if _xout = "parameters" then return [seq(_pars[_i] = _res[_i], _i = 1 .. nops(_pars))] elif _xout = "initial_and_parameters" then return [seq(_vars[_i+1] = [_res][1][_i+1], _i = 0 .. _n), seq(_pars[_i] = [_res][2][_i], _i = 1 .. nops(_pars))] else return [seq(_vars[_i+1] = _res[_i+1], _i = 0 .. _n)] end if elif type(_xout, `=`) and member(lhs(_xout), ["initial", 'initial', "parameters", 'parameters', "initial_and_parameters", 'initial_and_parameters', NULL]) then _xout := convert(lhs(x_rkf45), 'string') = rhs(x_rkf45); if type(rhs(_xout), 'list') then _res := _solnproc(_xout) else error "initial and/or parameter values must be specified in a list" end if; if lhs(_xout) = "initial" then return [seq(_vars[_i+1] = _res[_i+1], _i = 0 .. _n)] elif lhs(_xout) = "parameters" then return [seq(_pars[_i] = _res[_i], _i = 1 .. nops(_pars))] else return [seq(_vars[_i+1] = [_res][1][_i+1], _i = 0 .. _n), seq(_pars[_i] = [_res][2][_i], _i = 1 .. nops(_pars))] end if elif type(_xout, `=`) and member(lhs(_xout), ["eventdisable", 'eventdisable', "eventenable", 'eventenable', "eventfired", 'eventfired', "direction", 'direction', NULL]) then return _solnproc(convert(lhs(x_rkf45), 'string') = rhs(x_rkf45)) elif _xout = "solnprocedure" then return eval(_solnproc) elif _xout = "sysvars" then return _vars end if; if procname <> unknown then return ('procname')(x_rkf45) else _ndsol; _ndsol := pointto(_dat[2][0]); return ('_ndsol')(x_rkf45) end if end if; try _res := _solnproc(_xout); [seq(_vars[_i+1] = _res[_i+1], _i = 0 .. _n)] catch: error  end try end proc

(2.2)

f := proc(z)
  if not type(evalf(z),'numeric') then
    'procname'(z);
  else
    eval(theta__0(xi), EmdenN(z));
  end if;
end proc;

proc (z) if not type(evalf(z), 'numeric') then ('procname')(z) else eval(theta__0(xi), EmdenN(z)) end if end proc

(2.3)

Alternative_ic := D(gamma)(0) = 0, gamma(Pi)+Pi*D(gamma)(Pi) = 0;
 

(D(gamma))(0) = 0, gamma(Pi)+Pi*(D(gamma))(Pi) = 0

(2.4)

LaneEmdenN := dsolve(eval({ode2(1), Alternative_ic}, alpha=1.4), known=f, numeric, method=bvp[midrich])

proc (x_bvp) local res, data, solnproc, _ndsol, outpoint, i; option `Copyright (c) 2000 by Waterloo Maple Inc. All rights reserved.`; _EnvDSNumericSaveDigits := Digits; Digits := 15; if _EnvInFsolve = true then outpoint := evalf[_EnvDSNumericSaveDigits](x_bvp) else outpoint := evalf(x_bvp) end if; data := Array(1..4, {(1) = proc (outpoint) local X, Y, YP, yout, errproc, L, V, i; option `Copyright (c) 2000 by Waterloo Maple Inc. All rights reserved.`; X := Vector(18, {(1) = .0, (2) = .14884213845739128, (3) = .32754863602589657, (4) = .5553084957262711, (5) = .7874800522107529, (6) = 1.0114291771803472, (7) = 1.2281996584903694, (8) = 1.438555827608721, (9) = 1.6436269389285274, (10) = 1.843741672234072, (11) = 2.0356648863077784, (12) = 2.2190020522594196, (13) = 2.3928937001575576, (14) = 2.555840246895762, (15) = 2.7096623121878944, (16) = 2.8576046271658075, (17) = 3.0014751512407893, (18) = 3.14159265358979}, datatype = float[8], order = C_order); Y := Matrix(18, 2, {(1, 1) = 0.2786846054231255e-7, (1, 2) = .0, (2, 1) = -0.3562572816784358e-2, (2, 2) = -0.4832095921316075e-1, (3, 1) = -0.17868565989373517e-1, (3, 2) = -.11377780998943095, (4, 1) = -0.5534975350912328e-1, (4, 2) = -.22068153465207146, (5, 1) = -.12274162831839573, (5, 2) = -.3664658707825326, (6, 1) = -.2239855581175878, (6, 2) = -.5430683213569868, (7, 1) = -.36254846097727617, (7, 2) = -.7379430559057298, (8, 1) = -.5382439065177911, (8, 2) = -.9312804778184831, (9, 1) = -.7470589251749571, (9, 2) = -1.0996668783787409, (10, 1) = -.9799484902510673, (10, 2) = -1.2179960038641242, (11, 1) = -1.2193058265376713, (11, 2) = -1.2630876988713104, (12, 1) = -1.4485143202128798, (12, 2) = -1.2220235472580003, (13, 1) = -1.651039887759744, (13, 2) = -1.0913902571154173, (14, 1) = -1.8127715268717053, (14, 2) = -.87862476185007, (15, 1) = -1.9270661556123319, (15, 2) = -.5937689062690407, (16, 1) = -1.9899027566941012, (16, 2) = -.24350437545446935, (17, 1) = -1.9963430919121599, (17, 2) = .16449389883037516, (18, 1) = -1.9421029187950043, (18, 2) = .6181905590388456}, datatype = float[8], order = C_order); YP := Matrix(18, 2, {(1, 1) = .0, (1, 2) = .0, (2, 1) = -0.4832095921316075e-1, (2, 2) = -.33670881250390655, (3, 1) = -.11377780998943095, (3, 2) = -.40345252236385715, (4, 1) = -.22068153465207146, (4, 2) = -.5432617760541774, (5, 1) = -.3664658707825326, (5, 2) = -.7132779795740161, (6, 1) = -.5430683213569868, (6, 2) = -.8561708038360336, (7, 1) = -.7379430559057298, (7, 2) = -.9269107344453392, (8, 1) = -.9312804778184831, (8, 2) = -.891219994796424, (9, 1) = -1.0996668783787409, (9, 2) = -.7281018078946603, (10, 1) = -1.2179960038641242, (10, 2) = -.4313339037798736, (11, 1) = -1.2630876988713104, (11, 2) = -0.1827909461105226e-1, (12, 1) = -1.2220235472580003, (12, 2) = .48181813126717943, (13, 1) = -1.0913902571154173, (13, 2) = 1.030541306873292, (14, 1) = -.87862476185007, (14, 2) = 1.5850805235477605, (15, 1) = -.5937689062690407, (15, 2) = 2.117795333897795, (16, 1) = -.24350437545446935, (16, 2) = 2.6121804586532793, (17, 1) = .16449389883037516, (17, 2) = 3.0504532010039105, (18, 1) = .6181905590388456, (18, 2) = 3.4129704628571087}, datatype = float[8], order = C_order); errproc := proc (x_bvp) local outpoint, X, Y, yout, L, V, i; option `Copyright (c) 2000 by Waterloo Maple Inc. All rights reserved.`; Digits := 15; outpoint := evalf(x_bvp); X := Vector(18, {(1) = .0, (2) = .14884213845739128, (3) = .32754863602589657, (4) = .5553084957262711, (5) = .7874800522107529, (6) = 1.0114291771803472, (7) = 1.2281996584903694, (8) = 1.438555827608721, (9) = 1.6436269389285274, (10) = 1.843741672234072, (11) = 2.0356648863077784, (12) = 2.2190020522594196, (13) = 2.3928937001575576, (14) = 2.555840246895762, (15) = 2.7096623121878944, (16) = 2.8576046271658075, (17) = 3.0014751512407893, (18) = 3.14159265358979}, datatype = float[8], order = C_order); Y := Matrix(18, 2, {(1, 1) = -0.22082030673701116e-7, (1, 2) = .0, (2, 1) = -0.9944513737277786e-8, (2, 2) = -0.11057779209061155e-6, (3, 1) = -0.14410205429922384e-8, (3, 2) = -0.11010106923926161e-7, (4, 1) = 0.2918002684149543e-9, (4, 2) = -0.23446875508006e-10, (5, 1) = 0.29403986193387315e-9, (5, 2) = 0.7876589794225318e-10, (6, 1) = 0.32044005831683494e-9, (6, 2) = 0.5620829939223527e-9, (7, 1) = 0.3290674401843645e-9, (7, 2) = -0.25199071697519517e-9, (8, 1) = 0.32483387148487913e-9, (8, 2) = -0.6820623647731694e-10, (9, 1) = 0.42100127427678564e-9, (9, 2) = 0.5501713716759299e-9, (10, 1) = 0.6628642032505866e-9, (10, 2) = 0.12883903805148475e-8, (11, 1) = 0.8967553299891075e-9, (11, 2) = 0.7931840724266883e-9, (12, 1) = 0.8243613779364523e-9, (12, 2) = -0.9085565142647998e-9, (13, 1) = 0.6918527523036618e-9, (13, 2) = -0.10248382884407784e-8, (14, 1) = 0.5123617942366622e-9, (14, 2) = -0.9400126394238837e-9, (15, 1) = 0.35684735113600963e-9, (15, 2) = -0.10496443794348476e-8, (16, 1) = 0.20945462046900524e-9, (16, 2) = -0.9440809572015473e-9, (17, 1) = 0.1416488809215075e-9, (17, 2) = -0.12098226665738297e-10, (18, 1) = 0.13759710219293286e-9, (18, 2) = -0.4379728738825265e-10}, datatype = float[8], order = C_order); if not type(outpoint, 'numeric') then if outpoint = "start" or outpoint = "left" then return X[1] elif outpoint = "right" then return X[18] elif outpoint = "order" then return 8 elif outpoint = "error" then return HFloat(1.1057779209061155e-7) elif outpoint = "errorproc" then error "this is already the error procedure" elif outpoint = "rawdata" then return [2, 18, [gamma(xi), diff(gamma(xi), xi)], X, Y] else return ('procname')(x_bvp) end if end if; if outpoint < X[1] or X[18] < outpoint then error "solution is only defined in the range %1..%2", X[1], X[18] end if; V := array([1 = 4, 2 = 0]); if Digits <= trunc(evalhf(Digits)) then L := Vector(4, 'datatype' = 'float'[8]); yout := Vector(2, 'datatype' = 'float'[8]); evalhf(`dsolve/numeric/lagrange`(18, 2, X, Y, outpoint, var(yout), var(L), var(V))) else L := Vector(4, 'datatype' = 'sfloat'); yout := Vector(2, 'datatype' = 'sfloat'); `dsolve/numeric/lagrange`(18, 2, X, Y, outpoint, yout, L, V) end if; [xi = outpoint, seq('[gamma(xi), diff(gamma(xi), xi)]'[i] = yout[i], i = 1 .. 2)] end proc; if not type(outpoint, 'numeric') then if outpoint = "start" or outpoint = "left" then return X[1] elif outpoint = "method" then return "bvp" elif outpoint = "right" then return X[18] elif outpoint = "order" then return 8 elif outpoint = "error" then return HFloat(1.1057779209061155e-7) elif outpoint = "errorproc" then return eval(errproc) elif outpoint = "rawdata" then return [2, 18, "depnames", X, Y, YP] else error "non-numeric value" end if end if; if outpoint < X[1] or X[18] < outpoint then error "solution is only defined in the range %1..%2", X[1], X[18] end if; if Digits <= trunc(evalhf(Digits)) and (_EnvInFsolve <> true or _EnvDSNumericSaveDigits <= trunc(evalhf(Digits))) then V := array( 1 .. 6, [( 1 ) = (7), ( 2 ) = (0), ( 3 ) = (false), ( 4 ) = (false), ( 5 ) = (true), ( 6 ) = (false)  ] ); L := Matrix(7, 2, {(1, 1) = .0, (1, 2) = .0, (2, 1) = .0, (2, 2) = .0, (3, 1) = .0, (3, 2) = .0, (4, 1) = .0, (4, 2) = .0, (5, 1) = .0, (5, 2) = .0, (6, 1) = .0, (6, 2) = .0, (7, 1) = .0, (7, 2) = .0}, datatype = float[8], order = C_order); yout := Vector(2, {(1) = .0, (2) = .0}, datatype = float[8]); evalhf(`dsolve/numeric/hermite`(18, 2, X, Y, YP, outpoint, var(yout), var(L), var(V))) else if _EnvInFsolve = true then Digits := _EnvDSNumericSaveDigits end if; V := array( 1 .. 6, [( 1 ) = (7), ( 2 ) = (0), ( 3 ) = (false), ( 4 ) = (false), ( 5 ) = (true), ( 6 ) = (false)  ] ); L := Matrix(7, 2, {(1, 1) = 0., (1, 2) = 0., (2, 1) = 0., (2, 2) = 0., (3, 1) = 0., (3, 2) = 0., (4, 1) = 0., (4, 2) = 0., (5, 1) = 0., (5, 2) = 0., (6, 1) = 0., (6, 2) = 0., (7, 1) = 0., (7, 2) = 0.}, order = C_order); yout := Vector(2, {(1) = 0., (2) = 0.}); `dsolve/numeric/hermite`(18, 2, X, Y, YP, outpoint, yout, L, V) end if; [outpoint, seq(yout[i], i = 1 .. 2)] end proc, (2) = Array(0..0, {}), (3) = [xi, gamma(xi), diff(gamma(xi), xi)], (4) = 0}); solnproc := data[1]; if not type(outpoint, 'numeric') then if outpoint = "solnprocedure" then return eval(solnproc) elif member(outpoint, ["start", "left", "right", "errorproc", "rawdata", "order", "error"]) then return solnproc(x_bvp) elif outpoint = "sysvars" then return data[3] elif procname <> unknown then return ('procname')(x_bvp) else _ndsol := pointto(data[2][0]); return ('_ndsol')(x_bvp) end if end if; try res := solnproc(outpoint); [xi = res[1], seq('[gamma(xi), diff(gamma(xi), xi)]'[i] = res[i+1], i = 1 .. 2)] catch: error  end try end proc

(2.5)

DocumentTools:-Tabulate(
  [
    plots:-display(
      plots:-odeplot(LaneEmdenN, [xi, gamma(xi)], xi=0..Pi, color=red, legend=numeric),
      plot(eval(LaneEmdenE, alpha=1.4), xi=0..Pi, color=blue, legend=exact),
      title = Lane-Emden
    ),
    plots:-odeplot(
      LaneEmdenN, [xi, gamma(xi)+xi*diff(gamma(xi),xi)], xi=0..Pi, color=red,
      legend=typeset(gamma(xi)+xi*diff(gamma(xi),xi)),
      gridlines=true
    )
  ], width=60
)

Note that you cannot obtain the numecical solution of the Lane-Emden equation
right to xi=Pi:

 

plots:-odeplot(LaneEmdenN, [xi, gamma(xi)], xi=0..5, color=red, legend=numeric)

Warning, right boundary of range exceeds domain of the problem, it has been adjusted

 

 

 

Add-on 1

 

# A boundary value problem solved formally

rhs(dsolve({ode2E(1), D(gamma)(0) = 0, gamma(Pi)+Pi*D(gamma)(Pi) = 0}));

(cos(xi*alpha)*xi*alpha-sin(xi*alpha))*Pi/(xi*(alpha^2-1)*sin(Pi*alpha)*alpha^2)+((-2+(-alpha^2+1)*xi^2)*sin(xi)+2*xi*cos(xi))/(xi*(alpha-1)^2*(alpha+1)^2)

(3.1)

simplify(%-Alternative);

0

(3.2)

Add-on 2

 

# The no-condition formal solution of the Lane-Emden solution

FreeLaneEmden := rhs(dsolve(ode2E(1)));

(cos(xi*alpha)*xi*alpha-sin(xi*alpha))*_C2/xi+(cos(xi*alpha)+sin(xi*alpha)*xi*alpha)*_C1/xi+((-2+(-alpha^2+1)*xi^2)*sin(xi)+2*xi*cos(xi))/(xi*(alpha-1)^2*(alpha+1)^2)

(4.1)

# Serie expansions at xi=0


S0 := series(FreeLaneEmden , xi=0, 4);
S1 := series(diff(FreeLaneEmden, xi), xi=0, 4);

series(_C1/xi+((1/2)*_C1*alpha^2)*xi+(-(1/3)*_C2*alpha^3+(-alpha^2+1/3)/((alpha-1)^2*(alpha+1)^2))*xi^2+O(xi^3),xi,3)

 

series(-_C1/xi^2+(1/2)*_C1*alpha^2+(-(2/3)*_C2*alpha^3+(-3*alpha^2+1)/((alpha-1)^2*(alpha+1)^2)-(-alpha^2+1/3)/((alpha-1)^2*(alpha+1)^2))*xi+O(xi^2),xi,2)

(4.2)

# FreeLaneEmden is finite at xi=0 iif _C1=0

LeftFixedLaneEmden := eval(FreeLaneEmden, _C1=0)

(cos(xi*alpha)*xi*alpha-sin(xi*alpha))*_C2/xi+((-2+(-alpha^2+1)*xi^2)*sin(xi)+2*xi*cos(xi))/(xi*(alpha-1)^2*(alpha+1)^2)

(4.3)

# The series expansion of diff(LeftFixedLaneEmden, xi) at xi=0
# shows this quantity is equal to 0 WHATEVER the value of _C2.

series(diff(LeftFixedLaneEmden, xi), xi=0, 2)

series((-(2/3)*_C2*alpha^3+(-3*alpha^2+1)/((alpha-1)^2*(alpha+1)^2)-(-alpha^2+1/3)/((alpha-1)^2*(alpha+1)^2))*xi+O(xi^3),xi,3)

(4.4)

# Thus _C1=0 implies that both FreeLaneEmden and its derivative will be
# equal to 0 at xi=0.
# So taking these initial conditions:

ic := gamma(0) = 0, D(gamma)(0) = 0;

# doesn't completely define the solution, which is why an integration constant
# appears now:

rhs(dsolve({ode2E(1), ic}));

gamma(0) = 0, (D(gamma))(0) = 0

 

(cos(xi*alpha)*xi*alpha-sin(xi*alpha))*_C2/xi+((-2+(-alpha^2+1)*xi^2)*sin(xi)+2*xi*cos(xi))/(xi*(alpha-1)^2*(alpha+1)^2)

(4.5)

Add-on 3:

Which one of the two initial conditions at xi=0 must be replaced by
gamma(Pi)+Pi*D(gamma)(Pi)=0 to get a uniquely defined solution?

 

# Replace D(gamma)(0)
# (no solution found)
ic := gamma(0) = 0, gamma(Pi)+Pi*D(gamma)(Pi)=0;

dsolve({ode2E(1), ic});

gamma(0) = 0, gamma(Pi)+Pi*(D(gamma))(Pi) = 0

(5.1)

# Replace gamma(0)
# (no solution found)


ic := D(gamma)(0) = 0, gamma(Pi)+Pi*D(gamma)(Pi)=0;

dsolve({ode2E(1), ic});

(D(gamma))(0) = 0, gamma(Pi)+Pi*(D(gamma))(Pi) = 0

 

gamma(xi) = (cos(xi*alpha)*xi*alpha-sin(xi*alpha))*Pi/(xi*(alpha^2-1)*sin(Pi*alpha)*alpha^2)+((-2+(-alpha^2+1)*xi^2)*sin(xi)+2*xi*cos(xi))/(xi*(alpha-1)^2*(alpha+1)^2)

(5.2)

 

Download LaneEmdenNumeric_Another_POV.mw

@shashi598 


UPDATED
 

(I answer you from my personal account, not my professional one @sand15).

By the same procedure as before here is the file corresponding to the case alpha=1.4.
You will find also the explanation of the different values you get at xi=Pi (essentially a numerical precision issue, see the lines after the text  Check the value of f(xi)+xi*diff(f(xi), xi) at point xi=Pi) and a way to find the initial conditions at xi=epsilon such that the solution verifies

gamma(xi)+xi*diff(gamma(xi), xi)=0

at point  xi =Pi (see after the text Attempt to circumvent using the exact solution to define the initial conditions at xi=epsilon.).
The main idea is

  • to relax the initial conditions by writting gamma(epsilon)=eta and D(gamma)(epsilon)=tau (the numerical solution of the Lane-Emden equation then is a function of xi parameterized by eta and tau)
    I used 
    epsilon = 10-8

     

  • and then to minimize some functional of eta and tau in order that
     gamma(xi ; eta, tau)+xi*diff(gamma(xi ; eta, tau), xi)=0  for xi=Pi

    The optimal solution  gamma(xi ; etaopt, tauopt) is equal to the exact solution up to an error of the order of 10-6 (a value that could be improved if necessary).
    I got the optimal values

    eta = -2.410160197*10-17
    tau = -3.174864904*10-11

    You may observe their are extremely close to the original initial condition eta=tau=0 we would write for the limiting case  epsilon=0;  but these small variationse have significant consequences:

    eta = tau = 0:
    gamma(Pi) + Pi*D(gamma)(Pi) = -1.01987         (instead of 0)
    gamma(Z) + Z*D(gamma)(Z)    = 0  if Z = 3.2267 (instead of Pi)
    
    eta = -2.410160197*10-17 and tau = -3.174864904*10-11
    gamma(Pi) + Pi*D(gamma)(Pi) = 3.223690001*10-14  

     


Here is the file (link to DropBox)  LaneEmden_1dot4.txt

LaneEmdenNumeric_1dot4.mw

@Carl Love 


Thanks for your reply.

You wrote "I didn't expect my technique to be the fastest possible", yes I understood that. 
My opinion is that your technique has the strong advantage to manage a list of columns of any longer, as my procedure would be quite complex to generalize to a list of length larger than 2 (a recursive formulation could be useful).

You are also right, your technique is "reasonably fast and intuitive", (It needs to compare the lebgth of our respective codes).

Here are the performances I got with your last reply.

restart:

kernelopts(version)

`Maple 2015.2, APPLE UNIVERSAL OSX, Dec 20 2015, Build ID 1097895`

(1)

MultipleSC := proc(M)
  local cols, col, SC, tri, e, c, r, s, k:

  uses ListTools:
  
  SC := (M, j) -> M[sort(M[.., j], output=permutation), ..];
  cols := [_passed[2..-1]]:
  if numelems(cols) > 2 then
    error "sorting wrt more than 2 column not yet implemented"
  end if:

  tri := sort(M[.., cols[1]], output=permutation);
  if numelems(cols) = 2 then
    e := [{entries(M[.., cols[1]], nolist)}[]];
    c := convert(M[.., cols[1]], list);
    r := [
           0,
           PartialSums(
             map(u -> Occurrences(u, c), e)
           )[]
         ];
    s := NULL:
    for k from 1 to numelems(e) do
      s := s, tri[r[k]+1..r[k+1]][sort(T[tri[r[k]+1..r[k+1]], cols[2]], output=permutation)][];
    end do;
    tri := [s];
  end if:
  return M[tri];
end proc:



# Car Love's procedure

CL := proc(M, L)
local r, c;
(r,c):= op(1, M):
M[sort[inplace](
    rtable(1..r, i-> ArrayTools:-Alias(M, c*(i-1), [1..c])),
    key= (R-> [seq](R[L])),
    output= permutation
)]
end proc:
 

N := 10^5:

T := `<|>`(
  seq(
    `<|>`(
      LinearAlgebra:-RandomVector(N, generator=1..4)
      , Vector(N, i -> StringTools:-Random(1, "ABCD"))
    )
    , i=1..20
  )
):

CodeTools:-Usage( MultipleSC(T, 1, 2), iterations=10 ):

CodeTools:-Usage( CL(T ,[1, 2]), iterations=10 ):

memory used=57.10MiB, alloc change=184.39MiB, cpu time=480.20ms, real time=260.10ms, gc time=275.48ms

memory used=115.26MiB, alloc change=320.00MiB, cpu time=1.97s, real time=1.12s, gc time=1.11s

 

N := 10^5:

T := `<|>`(
  seq(
    `<|>`(
      LinearAlgebra:-RandomVector(N, generator=1..4)
      , Vector(N, i -> StringTools:-Random(2, 'alpha'))
    )
    , i=1..20
  )
):

CodeTools:-Usage( MultipleSC(T, 1, 2), iterations=10 ):

CodeTools:-Usage( CL(T ,[1, 2]), iterations=10 ):

memory used=56.95MiB, alloc change=213.64MiB, cpu time=296.90ms, real time=199.80ms, gc time=74.06ms

memory used=115.26MiB, alloc change=-152.60MiB, cpu time=2.07s, real time=1.20s, gc time=1.14s

 

N := 10^6:

T := `<|>`(
      LinearAlgebra:-RandomVector(N, generator=1..4)
      , Vector(N, i -> StringTools:-Random(2, 'alpha'))
):

CodeTools:-Usage( MultipleSC(T, 1, 2) ):

CodeTools:-Usage( CL(T ,[1, 2]) ):

memory used=280.21MiB, alloc change=95.49MiB, cpu time=2.26s, real time=1.48s, gc time=414.64ms

memory used=0.84GiB, alloc change=124.71MiB, cpu time=21.11s, real time=11.91s, gc time=10.82s

 

 


 

Download ColumnSorting_4.mw

@Carl Love 

What follows in no way diminishes the interest of your solution (see the reply I sent you earlier).
I improved  my procedure MultipleSC to avoid building submatrices and and managing only indices.
Here are compared performances for MultipleSC and your proposal.

restart:

MultipleSC := proc(M)
  local cols, col, SC, tri, e, c, r, s, k:

  uses ListTools:
  
  SC := (M, j) -> M[sort(M[.., j], output=permutation), ..];
  cols := [_passed[2..-1]]:
  if numelems(cols) > 2 then
    error "sorting wrt more than 2 column not yet implemented"
  end if:

  tri := sort(M[.., cols[1]], output=permutation);
  if numelems(cols) = 2 then
    e := [{entries(M[.., cols[1]], nolist)}[]];
    c := convert(M[.., cols[1]], list);
    r := [
           0,
           PartialSums(
             map(u -> Occurrences(u, c), e)
           )[]
         ];
    s := NULL:
    for k from 1 to numelems(e) do
      s := s, tri[r[k]+1..r[k+1]][sort(T[tri[r[k]+1..r[k+1]], cols[2]], output=permutation)][];
    end do;
    tri := [s];
  end if:
  return M[tri];
end proc:
  

if false then

T := `<|>`(
  LinearAlgebra:-RandomVector(10, generator=1..4)
  , Vector(10, i -> StringTools:-Random(1, "ABCD"))
  , Vector(10, i -> convert(StringTools:-Random(1, "uvwxyz"), name))
):

`T worted wrt col 1` = MultipleSC(T, 1),

`T sorted wrt col 1 and col 2` = MultipleSC(T, 1, 2),

`T sorted wrt col 2 and col 1` = MultipleSC(T, 2, 1):

end if;

N := 10^5:

T := `<|>`(
  seq(
    `<|>`(
      LinearAlgebra:-RandomVector(N, generator=1..4)
      , Vector(N, i -> StringTools:-Random(1, "ABCD"))
    )
    , i=1..20
  )
):

CodeTools:-Usage( MultipleSC(T, 1, 2), iterations=10 ):

CodeTools:-Usage( <sort(convert(T, listlist), key= (R-> [seq](R[[1,2]])))[]> ):

memory used=57.10MiB, alloc change=184.39MiB, cpu time=490.90ms, real time=267.00ms, gc time=276.44ms

memory used=225.25MiB, alloc change=231.45MiB, cpu time=5.00s, real time=3.65s, gc time=1.84s

 

TL := convert(T, listlist):
CodeTools:-Usage( <sort(TL, key= (R-> [seq](R[[1,2]])))[]> ):

memory used=187.76MiB, alloc change=0 bytes, cpu time=3.33s, real time=2.52s, gc time=1.08s

 

N := 10^5:

T := `<|>`(
  seq(
    `<|>`(
      LinearAlgebra:-RandomVector(N, generator=1..4)
      , Vector(N, i -> StringTools:-Random(2, 'alpha'))
    )
    , i=1..20
  )
):

CodeTools:-Usage( MultipleSC(T, 1, 2), iterations=10 ):

TL := convert(T, listlist):
CodeTools:-Usage( <sort(TL, key= (R-> [seq](R[[1,2]])))[]> ):

memory used=56.95MiB, alloc change=91.56MiB, cpu time=313.70ms, real time=213.60ms, gc time=78.80ms

memory used=187.76MiB, alloc change=-90.06MiB, cpu time=3.22s, real time=2.48s, gc time=926.57ms

 

N := 10^6:

T := `<|>`(
      LinearAlgebra:-RandomVector(N, generator=1..4)
      , Vector(N, i -> StringTools:-Random(2, 'alpha'))
):

CodeTools:-Usage( MultipleSC(T, 1, 2) ):

TL := convert(T, listlist):
CodeTools:-Usage( <sort(TL, key= (R-> [seq](R[[1,2]])))[]> ):

memory used=280.21MiB, alloc change=71.07MiB, cpu time=2.26s, real time=1.44s, gc time=440.63ms

memory used=1.76GiB, alloc change=105.98MiB, cpu time=30.01s, real time=22.78s, gc time=9.04s

 

 

Download ColumnSorting_2.mw

It is likely that one can reach even better performances with a more subtle coding.

@Carl Love 

I've just seen your reply, it is definitely excellent !!!
You deserve a gold star.

@sursumCorda 

Here is a way to sort a matrix wrt to the order induced by the elements of some column c1, and next (if required) wrt to the order induced by the elements of an other column c2.
This is very partial work for

  • the procedure MultipleSC is restricted to sort wrt 1 or 2 columns (a recursive formulation seems necessary for a larger number of sorting columns;
  • the column arguments could be gathered into a list;
  • there is no checking of the input types nor of the values of the colums;
  • the sorting acts only on columns, not on rows;
  • I didn't assess the efficienvy of this procedire;
  • ...

On the partial hospital[1..5, 1..3] test case displayed here  sortrows/sortcols, MultipleSC seems to return the expected result.

restart:

MultipleSC := proc(M)
  local cols, col, SC, S1, e, c, r, k:

  uses LinearAlgebra, ListTools:
  
  SC := (M, j) -> M[sort(M[.., j], output=permutation), ..];
  cols := [_passed[2..-1]]:
  if numelems(cols) > 2 then
    error "sorting wrt more than 2 column not yet implemented"
  end if:

  S1 := SC(M, cols[1]):
  if numelems(cols) = 2 then
    e := [{entries(S1[.., cols[1]], nolist)}[]]:
    c := convert(S1[.., cols[1]], list):
    r := [
           0,
           PartialSums(
             map(u -> Occurrences(u, c), e)
           )[]
         ]:
   `<,>`(
     seq(
       SC(
         SubMatrix(S1, [r[k]+1..r[k+1]], [1..-1])
         , cols[2]
       )
       , k=1..numelems(e)
     )
   ):
   S1 := %:
  end if:
  return S1;
end proc:
  

T := `<|>`(
  LinearAlgebra:-RandomVector(10, generator=1..4)
  , Vector(10, i -> StringTools:-Random(1, "ABCD"))
  , Vector(10, i -> convert(StringTools:-Random(1, "uvwxyz"), name))
);

`T worted wrt col 1` = MultipleSC(T, 1),

`T sorted wrt col 1 and col 2` = MultipleSC(T, 1, 2),

`T sorted wrt col 2 and col 1` = MultipleSC(T, 2, 1)

T := Matrix(10, 3, {(1, 1) = 4, (1, 2) = "B", (1, 3) = z, (2, 1) = 1, (2, 2) = "C", (2, 3) = u, (3, 1) = 2, (3, 2) = "B", (3, 3) = y, (4, 1) = 1, (4, 2) = "B", (4, 3) = u, (5, 1) = 3, (5, 2) = "D", (5, 3) = x, (6, 1) = 4, (6, 2) = "D", (6, 3) = z, (7, 1) = 3, (7, 2) = "D", (7, 3) = w, (8, 1) = 1, (8, 2) = "B", (8, 3) = w, (9, 1) = 1, (9, 2) = "B", (9, 3) = u, (10, 1) = 3, (10, 2) = "A", (10, 3) = z})

 

`T worted wrt col 1` = (Matrix(10, 3, {(1, 1) = 1, (1, 2) = "C", (1, 3) = u, (2, 1) = 1, (2, 2) = "B", (2, 3) = u, (3, 1) = 1, (3, 2) = "B", (3, 3) = w, (4, 1) = 1, (4, 2) = "B", (4, 3) = u, (5, 1) = 2, (5, 2) = "B", (5, 3) = y, (6, 1) = 3, (6, 2) = "D", (6, 3) = x, (7, 1) = 3, (7, 2) = "D", (7, 3) = w, (8, 1) = 3, (8, 2) = "A", (8, 3) = z, (9, 1) = 4, (9, 2) = "B", (9, 3) = z, (10, 1) = 4, (10, 2) = "D", (10, 3) = z})), `T sorted wrt col 1 and col 2` = (Matrix(10, 3, {(1, 1) = 1, (1, 2) = "B", (1, 3) = u, (2, 1) = 1, (2, 2) = "B", (2, 3) = w, (3, 1) = 1, (3, 2) = "B", (3, 3) = u, (4, 1) = 1, (4, 2) = "C", (4, 3) = u, (5, 1) = 2, (5, 2) = "B", (5, 3) = y, (6, 1) = 3, (6, 2) = "A", (6, 3) = z, (7, 1) = 3, (7, 2) = "D", (7, 3) = w, (8, 1) = 3, (8, 2) = "D", (8, 3) = x, (9, 1) = 4, (9, 2) = "B", (9, 3) = z, (10, 1) = 4, (10, 2) = "D", (10, 3) = z})), `T sorted wrt col 2 and col 1` = (Matrix(10, 3, {(1, 1) = 3, (1, 2) = "A", (1, 3) = z, (2, 1) = 1, (2, 2) = "B", (2, 3) = u, (3, 1) = 1, (3, 2) = "B", (3, 3) = w, (4, 1) = 1, (4, 2) = "B", (4, 3) = u, (5, 1) = 2, (5, 2) = "B", (5, 3) = y, (6, 1) = 4, (6, 2) = "B", (6, 3) = z, (7, 1) = 1, (7, 2) = "C", (7, 3) = u, (8, 1) = 3, (8, 2) = "D", (8, 3) = w, (9, 1) = 3, (9, 2) = "D", (9, 3) = x, (10, 1) = 4, (10, 2) = "D", (10, 3) = z}))

(1)

 

Download ColumnSorting.mw

REMARK: I always found this lack of column/row sorting was painful (with R, which I use to use, it is really easy to do this, likely as with Matlab). 
I suggest that you submit a specific question on this subject.

@sursumCorda 

I realized as I woke up this morning "Time taken to sort 1000000 elements 300 times" and I was about to connect on Mapleprimes in order to remove my last claim as I read your reply.

About sortrows/sortcols
There is no such things for matrices.
Nevertheless, as I use Maple 2015 right now, I advice you to give a look to DataFrames if tou use a more recent version: if something like sortrows/sortcols do exist it should be there.

For the record, to sort a matrix wrt to the order induced by some row or column I use to do this (but I guess you were capable to find this by yourself :-)

restart
# sort all the columns wrt the order induced by column 1
SC := (M, j) -> M[sort(M[.., j], output=permutation), ..];

# sort all the lines wrt the order induced by line 1
SR := (M,i) -> M[.., sort(M[i, ..], output=permutation)];

T := LinearAlgebra:-RandomMatrix(5, 3, generator=0..4):
SC(T, 1);
SR(T, 2);

@sursumCorda 

X is a vector of 105 floating points, Y is the same vector converted into a list.
PartialSorting_perf.mw

Main results:

  • sort@Trim and Trim both are almost identical (memory used/allocated, cpi/real/gc times.
  • Operating on a vector is six times faster then operating on a list and uses three times less memory.
  • The performances are almost independent on the number (K) of elements in the partial vectors/lists.
  • The cpu times are extremely small compared to those obtained with Matlab 

@sursumCorda 

Yeah, I noticed that.
What is strange is that if you execute 

CodeTools:-Usage( sort(Trim(X, 0, 100*k/N)) )

before 

CodeTools:-Usage( Trim(X, 0, 100*k/N) ):

you get reversed times and Trim is now logicallly faster than sort@Trim.
See Look_at_this.mw
 

@Axel Vogt 

Yes, our answers intersected.
It is only after I submitted mine that I saw yours, I hope you don't mind?

@Sky-Bj 

Integrating without numerical bounds mean means integrate formally and I doubt that a closed form expression does exist.
So you have to integrate numerically, which means there must be numerical bounds.

Look here: How_to_integrate_formal.mw
 

Please upload your mw file while clicking on  the green up-arrow in the menu bar if you want someone to help you

3 4 5 6 7 8 9 Last Page 5 of 119