Carl Love

## 20954 Reputation

8 years, 211 days
Natick, Massachusetts, United States
My name was formerly Carl Devore.

## Converting piecewise to Fortran...

Here is a procedure to convert a piecewise expression into a Fortran-compatible procedure that can be differentiated with D. This uses a feature new to Maple 2021, so if you're not using Maple 2021, let me know.

```(*---------------------------------------------------------------------
This procedure converts a piecewise *expression* into a procedure that
1. can be differentiated or partial differentiated with D,
2. can be converted to Fortran and its derivatives can be
converted to Fortran.

The optional 2nd argument is a list of the names (optionally with type
specifiers) that will be used as
the parameters of the procedure, and hence the potential variables of
differentiation. Its default value is the nonconstant names that
appear in the piecewise conditions with hfloat as the type.

This procedure uses a feature new to Maple 2021.
----------------------------------------------------------------------*)
`convert/pwproc`:= proc(
P::specfunc(piecewise),
V::list({name, name::type}):=
[indets(indets(P, boolean), And(name, Not(constant)))[]]::~hfloat
)
option `Author: Carl Love <carl.j.love@gmail.com> 2021-May-9`;
subsop(
8= hfloat, #procedure return type
unapply(
subsindets(
convert(
if op(0,P)::symbol or nops(P)::odd then P
else op(0,P)(op(P), op([0,1],P))
fi,
ifelse, ':-recurse'
),
specfunc(ifelse), convert, `if`
),
V
)
)
end proc
:
#The piecewise expression from your Question:
P:= piecewise(
x <= 0, x^2+x,
x < 3*Pi, sin(x),
x^2 - 6*x*Pi + 9*Pi^2 - x + 3*Pi
):
PP:= convert(P, pwproc);
PP := proc (x::hfloat)::hfloat; options operator, arrow; if x
<= 0 then x^2+x else if x < 3*Pi then sin(x) else
x^2-6*x*Pi+9*Pi^2-x+3*Pi end if end if end proc

dPP:= D(PP); #or simply D(PP)
dPP := proc (x::hfloat) options operator, arrow; if x <= 0 then
2*x+1 else if x < 3*Pi then cos(x) else -6*Pi+2*x-1 end if
end if end proc

Digits:= 18: #for correct value of Pi as a Fortran "double"
CodeGeneration:-Fortran(dPP);
Warning, procedure/module options ignored
doubleprecision function dPP (x)
doubleprecision x
if (x .le. 0.0D0) then
dPP = 0.2D1 * x + 0.1D1
return
else
if (x .lt. 0.3D1 * 0.314159265358979324D1) then
dPP = cos(x)
return
else
dPP = -0.6D1 * 0.314159265358979324D1 + 0.2D1 * x - 0.1D1
return
end if
end if
end

```

## 15 methods...

Here are 5 methods for your map_with_index:

```map_with_index1:= (f,a)-> (L-> f~(L, [\$1..nops(L)]))(convert(a, list)):
map_with_index2:= (f,a)-> [for local i,x in a do f(x,i) od]:
map_with_index3:= (f,a)-> local i:= 0; map(x-> f(x, ++i), a):
map_with_index4:= (f,a)->
(L-> zip(f, L, [\$1..nops(L)]))(convert(a, list))
:
map_with_index5:= (f,a)-> local i; [seq](f(a[i], i), i= 1..nops(a)):

L:= ["a", "b", "c"]:
(print@~map_with_index||(1..5))(`[]`, L);
[["a", 1], ["b", 2], ["c", 3]]
[["a", 1], ["b", 2], ["c", 3]]
[["a", 1], ["b", 2], ["c", 3]]
[["a", 1], ["b", 2], ["c", 3]]
[["a", 1], ["b", 2], ["c", 3]]

```

What you call flat_map is not worth writing or even giving a name to in Maple. Instead, in the calling of the 5 procedures above, just change `[]` to1@@0.

Here are 10 ways to extract a sublist based on a predicate:

```(
remove(`>`, L, "b"),
remove(x-> x > "b", L),
remove(`<`, "b", L).
select(`<=`, L, "b"),
select(`>=`, "b", L),
map(x-> `if`(x > "b", [][], x), L),
(x-> `if`(x > "b", [][], x))~(L),
map(x-> if x > "b" then else x fi, L),
[seq](`if`(x > "b", [][], x), x= L),
[for x in L do if x > "b" then else x fi od]
);

["a", "b"], ["a", "b"], ["a", "b"] . ["a", "b"], ["a", "b"],
["a", "b"], ["a", "b"], ["a", "b"], ["a", "b"], ["a", "b"]

```

## Convert to polynomials, then use elimina...

It'll be useful if we can assume that all variables represent positive numbers. If that assumption is not valid, let me know, and I think that some modification of the techique below will still work.

Outline of my steps coded below:

1. Replace all variables with the squares of new variables. The new variables have names beginning z. This step removes all radicals from your equations. They are now equations of rational functions.
2. Subtract one side from the other in each equation. They are now just rational functions implicitly equated to 0.
3. Extract the numerators. Now we have a system of polynomials implicitly equated to (which I called sysP).
4. Use the eliminate command to express everything in terms of the two variables that you mentioned.
5. Replace the new variables with the square roots of the original variables.
```sys:= sys1 union sys2:
v:= [indets(sys, name)[]];
vz:= subsop~(0= z, v);
sysP:= (numer~@simplify)(subs(v=~ vz^~2, (lhs-rhs)~(sys)), assume= vz::~positive);
#Let's see how complicated those polynomials are.
[degree, length]~([sysP[]]);
[[2, 101], [2, 109], [26, 418], [26, 414]]

#They're much simpler than I expected, and much simpler than the original
#system! We have 4 short polynomials: 2 of degree 2 and 2 of degree 26.

Sols:= map(
s-> (lhs^2=rhs^2)~(s) union s =~ 0,
subs(vz=~ v^~(1/2), [eliminate](sysP, {z[4,1], z[4,2]}))
):
length~(Sols);
[35, 122754, 205504, 205504]

```

We get 4 solutions (and it's done with amazing speed---6 seconds in Maple 2021). The first solution is trivial, and also irrelevant under our positivity assumption. The remaining solutions are extremely long, but they do satisfy exactly what you asked for: expression of everything in terms of y[2,1] and y[2,2]. And, amazingly, this has been done without the use of RootOf.

## Embedded for loops...

As of Maple 2019 (or perhaps 2018), embedded for loops, such as you show in your Question, are allowed (in 1D input only!). The syntax is:

n:= 7:
a:= [\$1..7]:
P:= piecewise(for i to n do x < a[i], y[i](x) od);

This avoids the op([...]) syntax required by seq.

## Heaviside equivalent to piecewise...

Yes, you are correct that your piecewise expression can be converted to an equivalent form that doesn't use piecewise and this makes some improvement to the time of the integration. After defining the piecewise via f:= piecewise(...), do

f:= convert(f, Heaviside):

Other recommendations:

Change Digits:= 30 to Digits:= 15, which is the largest value for which hardware-float computations will be used, which are much faster. As far as I can see, this doesn't change the final result, but my observations of this aspect have been superficial thus far.

Remove Digits:= 4. In my opinion, that's too low to ever be used as the global value of Digits. There are other ways to control the precision of a numeric integration, which are discussed in the next paragraph.

Inside the int command, after keyword numeric, add the options epsilon= 0.5e-4, digits= 7. The returned result will be guaranteed to 4 significant digits. There is some chance that the integral will be returned unevaluated, which means that it wasn't able to control the accuracy sufficiently. If this happens, let me know.

If you make these changes, then I think that the majority of the computation time is consumed by solve, not by int. If this amount of time is still too much for you, let me know.

## What does "correct" mean?...

The sequence of iterates in Newton's method can be extremely sensitive to the number of digits of precision at which the calculations are performed. This is totally normal.

## Convexity...

You may already know this, but that is not conveyed in your Question or worksheet. Since the second derivative of exp(x) never changes sign (in other words, there are no inflection points), there can be at most two intersections between the curve and any straight line, and if an intersection is also a point of tangency, then it's necessarily the only intersection. The cases of 0 intersections and 1 intersection are relatively easy to classify, as has already been done. Thus, all remaining cases are 2 intersections.

## color= COLOR(HUE, ...)...

Do you mean this?:

```data:= convert(
Statistics:-Sample(Uniform(-2, 2), [100, 2]),
list, nested
):
plot(
`[]`~(data),
color= COLOR~(
HUE,
.5 -~ argument~(-(Complex@op)~(data)^~2)/~(2*Pi)
),
style= point, symbolsize= 20, symbol= solidbox
);
``` ## Grid:-Map...

Maple has two packages for parallelization: Threads and Grid:

• With Threads, the procedures running on different cores share all memory except for variables that you explicitly tell them not to share.
• With Grid, the procedures in different cores run as distinct "processes", each with its own memory, and they can only communicate with each other (if any communication is needed at all) via variables or messages that you explicitly send between them.

The vast majority of Maple's high-level symbolic library commands make extensive use of global variables, which makes them unsuitable for the shared-memory environment of Threads. I suspect that Query is in this category.

Try using the command Grid:-Map for this.

## Some non-Maple (by hand) help...

I'm guessing that you might want some tutorial help with doing this problem "by hand".

The first step is to "complete the square" in the denominator, thus obtaining 1/((s+4)^2 + 4). We recognize this as the composition f@g of the two functions g:= s-> s+4 and f:= s-> 1/(s^2+2^2). The inner function g is a "shift": just adding a constant a to s. That means the inverse transform will contain exp(-a*t) as a factor (not as a function composition). The remaining part is of the form 1/(s^2+b^2). From a table of transforms (even the most-basic tables will likely have this one), the inverse transform of that is sin(b*t)/b. This is the other factor, making the answer exp(-4*t)*sin(2*t)/2.

A second approach to the problem is much more straightforward---it doesn't require much recognizing of things as being of certain special forms---but it does delve into complex numbers. We factor the denominator over the complex numbers to obtain (s+(4+2*I))*(s+(4-2*I)). Compute the partial fraction decomposition of the rational function from that factorization to obtain

I/4*(1/(s + (4+2*I)) - 1/(s + (4-2*I))

Each fraction is a "shift", so the inverse transform is

I/4*(exp(-(4+2*I)*t) - exp(-(4-2*I)*t))

If you convert that to real form (which can be done with Maple command evalc) you get the same thing as by the previous method.

## Avoiding undefined when differentiating ...

Your transcription of your code is so chock full of typos that the only reason that I can understand your Question is because I worked on an earlier Question of yours to which this one is closely related. I suspect that very few, perhaps 0, other readers can understand it at all, which may be why it's taken a long time for you to get an Answer.

In addition to the typos, the expressions -Pi/2 + 10 and Pi/2 - 10 are surely wrong. By 10, did you perhaps mean 10 degrees? In that case, you need to change 10 to Pi/18.

Now let's address your undefined issue: As is well known and explicitly taught---even in first-semester calculus---the derivatives of piecewise functions are often undefined at points where one piece changes to the next. So the fact that you're getting undefined in your piecewises is not an error or a bug (and I'm not saying that you were claiming that it was an error or a bug). But I understand that some further symbolic processing that you're doing with these expressions is having problems because of the undefineds. That's totally believable. The following example serves to illustrate where these undefineds come from, and also shows a method to avoid them. Please execute the code:

```f1:= abs(x):
f2:= convert(f1, piecewise, x);
diff(f2, x);
#Note the correct presence of undefined in previous result.

f3:= convert(f2, Heaviside);
convert(diff(f3, x), piecewise, x);
#Same result as before

Heaviside(0):= 0:
convert(diff(f3, x), piecewise, x);
#Now there's no undefined.
```

There are many subtleties involved in working symbolically with piecewise expressions, especially when there is more than one independent variable (it looks like you have both x and y), so I'm not claiming that the above will be a complete solution to the undefined issue. See if you can make *some* progress with it, and please report back.

## assuming x::real...

I agree that the result 1 doesn't make sense and should be corrected. But compare your result with this:

convert(abs(1,x), Heaviside) assuming x::real;
-1 + 2 Heaviside(x)

## Parameters vs. Arguments...

```f1:= proc(x, y) x^2+y^2 end proc:
f1(a, b, c);
2    2
a  + b

f2:= proc(x, y, \$) x^2+y^2 end proc:
f2(a, b, c);
Error, invalid input: too many and/or wrong type of
arguments passed to f2; first unused argument is c
```

The x and y in those examples are called parameters, not arguments. The ab, and c are the arguments.

## Compose color function with projection...

Do not think in terms of grids or cells! Coloring this plot is a purely algebraic problem, and I can completely describe it at the following high level of abstraction so that it'll work for any two (suitably smooth and close-enough-to-homeomorphic) parameterized surfaces, in any two coordinate systems, on any two distinct grids (perhaps even of different size). The beauty of plot3d's syntax (especially the part related to color functions) is that it handles those grid-and-cell-level details for you in the background so that you can focus on the algebra.

Only the algebraic details matter here, so I'm no longer going to mention any topological (e.g., "homeomorphic") or analytic ("smooth") details. The algebraic details that matter most are the number of input and output dimensions of all of the following functions and the order in which those dimensions are given: the coordinate transformations, the parameterized surfaces, the projections, and the coloring functions.

Define a 3D coordinate system as a surjective function C: R-> R3 such that C(a, b, c) =  <x(a,b,c), y(a,b,c), z(a,b,c)>, where <x, y, z> are the usual cartesian coordinates and <a, b, c> are the new coordinates. (In practice, most useful coordinate systems are named, predefined, and known to Maple, so we don't need to actually supply the function C.)  Define a parameterized surface S in coordinates C as a function S: A -> R3 such that S(u,t) = <a(u,t), b(u,t), c(u,t)>, where A is some 2D subset of R2, <a, b, c> are the coordinates of C, and <u, t> are the parameters of S.

For the time being at least, I'll only use 1 color system; let's say HSV, because that's what I used for the final example. Like most color systems, it has 3 coordinates, which has nothing to do with geometry; rather, it's based on the biology of (the most-common version of) human retinae; other animals often have a different number of dimensions of color vision. Naturally, I'll call the coordinates of HSV <h, s, v> (hue, saturation, value). For the purpose of use with plot3d, all 3 are bounded in the interval 0..1[*2]. Define a coloring function F for surface S as F: A -> (0..1)3 such that F(u,t)= <h(u,t), s(u,t), v(u,t)>, where A, u, t are as they were defined for S.

Given two surfaces S1 defined on A1 and S2 defined on A2, define a projection P from S2 to S1 as a (sufficiently bijective) function from A2 to A1[*3]. Note very carefully that the projections that I'm using here have 2 (not 3) input and output dimensions because surfaces are fundamentally 2D objects embedded in 3D space. Now let's say that we have S1, S2, and P (from S2 to S1), and we've decided on a coloring function F for S1. The algebraic trick that this entire article has been leading up to is that The coloring function to use for S2 is the functional composition F@P. It's as simple as that!

Now, onto your sphere-on-the-plane example. I chose to represent the sphere in spherical coordinates and the plane in cylindrical coordinates because that makes the algebra for all the functions easy enough to do in my head. For the sake of having 6 distinct[*1] coordinate variables, I represent spherical coordinates as <rho, psi, phi> where rho is the radius, psi is the longitude (in range -Pi..Pi), and phi is the angle between the radial vector and the positive z-axis (in range 0..Pi); and cylindrical coordinates as <r, theta, z> (which I probably don't need to define for you). The surface functions and the projection from the plane to the sphere are

Sph:= (psi, phi)-> <1, psi, phi>:
Pl:= (r, theta)-> <r, theta, -1>:
P__pl_to_sph:= (r, theta)-> (theta, Pi-2*arctan(r)):

In other words, the projection is simply psi = theta, phi = Pi - 2*arctan(r). [Edit: I now know that this is not the projection that you intended; however it still works as an example here, and my algebraic/functional formulation allows this one-line detail to be changed without changing anything else.] Note that it's essential to be consistent about the orders of the 2 parameters (..., ...) on the left sides of -> and the orders of the 3 coordinates <..., ..., ...> on the right sides of ->, and this is usually (for me at least) the most difficult part of working with alternate coordinate systems. (However, the vectors <...> could be changed to lists [...]; that makes no difference to (modern versions of) plot3d. Also, Maple's main plotting commands don't care what variable names you use for the coordinates in any coordinate system.) Furthermore, the order of the coordinates for any of Maple's predefined coordinate systems must be obeyed, although it's poorly documented.

For the coloring function of the sphere, I'm using

Color__sph:= (psi, phi)->
COLOR(HSV, (psi/Pi+1)/2., 1.-phi/Pi, 1.-phi/Pi)  #note the decimal points
:

I expect that you'll make that more elaborate, but it's the only one of these functions that you should change. [Edit: See the Edit comment in the previous paragraph.] Finally, the plotting command is

```plots:-display(
plot3d~(
[Sph(psi, phi), Pl(theta, r)],
[psi, theta]=~ -Pi..Pi, [phi= 0..Pi, r= 0..4],
coords=~ [spherical, cylindrical],
color=~ [
Color__sph(psi,  phi),
(Color__sph @ P__pl_to_sph)(r, theta)
]
),
lightmodel= none, scaling= constrained, axes= none,
viewpoint= circleleft
);```

I've tried several times to upload the resulting animation as a GIF file, but MaplePrimes is having a problem with that.

[*1] It's not actually necessary that the coordinate names between the coordinate systems be distinct. Making them distinct simply helps with my mental organization of the problem.

[*2] Bounding the color coordinates in the interval 0..1 is not strictly required. Maple will usually normalize the values for you. However, I don't like to rely on that; sometimes I've had problems if I don't normalize them myself.

[*3] This is perhaps not a standard definition of projection, and it's certainly not how that word is used in linear algebra, but it does conform both to the way that you've already used the term (as in "stereographic projection") and to how it's used in relation to geographic maps.

## Create an index...

A good solution is to create a Maple table-based index mapping the first two columns (which'll henceforth be called the key fields) to the row numbers (rows are henceforth called records). This solution is general, efficient, and remarkably simple---the comments for the code below are far longer than the code itself. In using this method, you'll never need to think about, see, or specify any record numbers; that's handled completely by the code. I know that you literally said that you wanted "to print the row numbers"; however, after you see this method you may realize that that's a totally unnecessary intermediate step rather than your final goal. Anyway, the code below will still easily let you see the row numbers if you really want to. Just do

(DB:-index@op)~(comb)

where DB is the return value of the procedure Index, below, and comb is exactly like you had in your Question.

Disambiguation of index and its inflected forms: In database theory, the usage of index and its inflected forms is more akin to normal English (as in, "the index of a book") than it is in general computer science and mathematics ("an index of a matrix or array"). Throughout this article, the noun index refers to the entire table-stored mapping between keys and record numbers; whereas in Maple (and other computer languages) it usually refers to a specific key value, as in the Maple command indices. The verbs index and reindex refers to the creation of an index rather than storing or looking up a specific key value (as in the Maple command index), for which the verbs store or look up will be used. The past participle indexed can be applied either to the entirety or to a specific case. The common-English verb key, its past participle keyed, and the adjective key are, to my knowledge, not used in any ways specific to databases, mathematics, or computer science; only the noun key is used.

Importing your data: I'll assume that you can import your data file into Maple as a Matrix. If you need help with that aspect, then ask. If the file is too big to fit into main memory as a Matrix, let me know. As long as just the index can be somehow squeezed into memory, a modification of this code can still work.

A matrix-based database implementation
We're given a Maple Matrix M each row of which represents a record of data. The columns are the fields. Some fields are key fields---those by which the records can be specified, found, or indexed. The key fields have a specified order, which needn't be their order as columns. So, if the key fields are represented by a list K of column numbers, then the key for the record at row r is M[r, K].  (The italicized words in this and the preceding paragraphs are usages specific to database theory.)

This implementation allows for a key to specify multiple records. The code below provides the option (multikey) of creating the index such that it'll retrieve all records that have a given key. Using this option incurs a substantial performance penalty when the index is created, although I think it's still fast. There is no penalty for the retrievals.

This implementation is designed to be efficient---regardless of the number of records---as long as adding new records or changing keys are relatively rare compared to retrieving records. There's no performance penalty for changing the data in non-key fields. Record deletion can be done efficiently by marking certain non-key fields as invalid data. Adding records or changing keys requires reindexing with Index.

```(*------------------------------------------------------------------
Create an index for a database represented as a matrix M with each
row representing a record.  K is a list of the key columns.

The return value is a Maple "Record"---essentially an ordered pair
[Data, Index] where Data is the matrix M and Index is the index
created by this procedure.  Such an ordered pair will be
called a "Database".  Note that "Record" is a stock Maple data
structure, and in this case it doesn't mean "record" in the sense
of database theory as used in the introduction.

Keyword option 'multikey':
Keys are allowed to point to multiple records.  If 'multikey' is
specified, then they will be indexed accordingly.  Otherwise, only
the last record for each key is indexed.  This option incurs a
substantial performance penalty during the indexing, but no penalty
for the retrievals.
-------------------------------------------------------------------*)

(*  This procedure creates a so-called 'sparse' table as the primary
data structure for the index.  A 'sparse' table simply means one
that returns 0 instead of an unassigned reference when it's passed
a key that hasn't been stored.  You'll see two references to these
0s in the following procedure, 'Get'.  This works as an "invalid
key" flag because 0 is never a valid row number.  *)

Index:= proc(
M::Matrix, K::list(posint):= , {multikey::truefalse:= false}
)
local i, J, n:= upperbound(M); #n is # of rows
if multikey then
J:= table(':-sparse');
for i to n do J[seq(M[i,K])][i]:= () od;
J:= indices~(J, ':-nolist')
else
table(':-sparse', [seq](seq(M[i,K])= i, i= 1..n))
fi;
Record("data"= M, "index"= (()-> J[args]))
end proc
:
(*----------------------------------------------------------------------
Get the records (rows) in database D corresponding to the keys in K.
Each entry in K must be a "tuple" (i.e., a list) [key1, key2, ...].

The return value is the submatrix of the data corresponding to the keys.
If K is a list, then the rows are ordered accordingly.  If K is a set,
then natural order is used.

Keyword option 'verify':
If the optional keyword 'verify' is given, then if any of the keys
the return is the same as usual.  If there's likely to be unfound
keys, and 'verify' is specified, then it's slightly more efficient to
make K a set.
----------------------------------------------------------------------*)
Get:= proc(
K::{list,set}(list),
D::record(data::Matrix, index::procedure),
{verify::truefalse:= false}
)
local R:= (D:-index@op)~(K); #rows corresponding to the keys K
if verify then
`if`(0 in R, select(k-> D:-index(k[])=0, {K[]}), D:-data[[R[]]])
else
D:-data[subs(0= (), [R[]])] #The subs removes 0s, if any
fi
end proc
:

#Examples:
r:= 2^16: #number of rows
c:= 5: #number of non-key data columns
(k1,k2):= (1..2^9, 1..2^9): #ranges for keys 1 & 2

K:= LinearAlgebra:-RandomMatrix((r,2), generator= rand(k1)):
NK:= LinearAlgebra:-RandomMatrix((r,c), generator= rand(-2. .. 2.)):
M:= <K | NK>:

DB1:= CodeTools:-Usage(Index(M, [1,2], multikey)):
memory used=172.38MiB, alloc change=238.00MiB,
cpu time=2.19s, real time=1.47s, gc time=1.86s

DB2:= CodeTools:-Usage(Index(M, [1,2])):
memory used=28.84MiB, alloc change=-8.00MiB,
cpu time=468.00ms, real time=393.00ms, gc time=312.50ms

(* Creating multiple indices for the data does not duplicate the data. All
data remains stored in, and only in, its original matrix M, and it can be
edited in M. *)

#Create some keys to lookup:
combos:= convert(
LinearAlgebra:-RandomMatrix((999,2), generator= rand(k1)),
list, nested
):
CodeTools:-Usage(Get(combos, DB1, verify)):
nops(%); #count invalid keys.
memory used=4.74MiB, alloc change=0 bytes,
cpu time=15.00ms, real time=15.00ms, gc time=0ns

786

CodeTools:-Usage(Get(combos, DB1));
memory used=4.60MiB, alloc change=0 bytes,
cpu time=0ns, real time=14.00ms, gc time=0ns
[240-row matrix output omitted]

CodeTools:-Usage(Get(combos, DB2));
memory used=4.59MiB, alloc change=0 bytes,
cpu time=16.00ms, real time=13.00ms, gc time=0ns
[213-row matrix output omitted]

```

 1 2 3 4 5 6 7 Last Page 1 of 328
﻿