Carl Love

## 26877 Reputation

11 years, 305 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

## Bits:-Split; recursion...

Here's a significantly faster procedure:

```OEIS_A219954:= proc(n::posint)
option remember;
local L:= ilog2(n), k:= n-1;
`if`(n=1, 0, thisproc(k) + `if`(2^L=n, 3^L - n/2, 3^add(Bits:-Split(k))))
end proc
:
```

Edit: Changed the procedure from an arrow operator to a proc ... end proc to accomodate 2D Input (and I hope that I don't live to regret it) or older Maple.

## Side-by-side real and imaginary plots...

First, I want to caution you to make functionList a proper Maple list by using square brackets [ ] instead of curly braces { }. The braces make it a set, and you can't control its order. (This is the only reason to make it a list rather than a set.)

The procedure below will take any of your functions and return side-by-side 3D plots of its real and imaginary parts.

```ReIm_plot:= proc(f::algebraic, Zr::(name=range(complexcons)))
local z:= lhs(Zr), R:= rhs(Zr), x, y, RI:= [Re,Im];
plots:-display(
<
plot3d~(
RI(eval(f, z= x+I*y)), (x,y)=~ map~(RI, R)[],
labels=~ `[]`~(RI[](z), RI(evaln(:-f)(z))),
title=~ typeset~(["Real", "Imaginary"], " part of ", f),
_rest
)
>^%T
)
end proc
:```

functionList:= [2 + z, z^2 - 3*z, -z^3 + 4];

You can do one function via

ReIm_plot(functionList[3], z= -1-I..1+I));

Or you can do them all at once via
print~(ReIm_plot~(functionList, z= -1-I..1+I)):  #Note the colon terminator

 > functionList:= [2 + z, z^2 - 3*z, -z^3 + 4]:
 > ReIm_plot:= proc(f::algebraic, Zr::(name=range(complexcons))) local z:= lhs(Zr), R:= rhs(Zr), x, y, RI:= [Re,Im];     plots:-display(         <             plot3d~(                 RI(eval(f, z= x+I*y)), (x,y)=~ map~(RI, R)[],                 labels=~ `[]`~(RI[](z), RI(evaln(:-f)(z))),                 title=~ typeset~(["Real", "Imaginary"], " part of ", f),                 _rest             )         >^%T     ) end proc :
 > print~(ReIm_plot~(functionList, z= -1-I..1+I)):

 >

## Cylindrical coordinates...

I think that you want cylindrical coordinates. But showing the discontinuity (division by 0) elegantly is more difficult in 3d than 2d, regardless of your coordinate system.

plot3d(
[r, theta, 1/(r^2*sin(theta)^2)], r= 0..2, theta= -Pi..Pi,
coords= cylindrical, shading= zhue, view= [-2..2, -2..2, 0..99]
);

## eval...

To get just the number, use

eval(phi1, S2[2])

Edit: corrected argument order.

## Deconstruction with op and nops...

Here is a procedure for it:

```IsRectangular:= proc(L::anything, max::posint:= infinity)
local d, Op:= op@{op}, Nops:= nops, Op0:= curry(op, 0), t;
if not L::':-list' then return false, 0 fi;
for d to max do until
(t:= (Op0:= Op@eval(Op0)~)(L)) <> ':-list' or
nops({(Nops:= Op@eval(Nops)~)}(L)) <> 1
;
d=max or max=infinity and not ':-list' in {t}, d
end proc
:```

## Remove ^ from exp^...

You still have exp^(-3*x[i]). You need to remove the ^ from that.

## Repeat 1st point in plot command...

It can be done by repeating the first point in the plot command, like this:

Matrice:= [[-2,-3],[-1,2],[3,4],[1,-2]]:
plot([Matrice[], Matrice[1]]);

## hastype...

You need to use hastype instead of has, like this:

```if pdext_simplified::`+` then
(expr, other):= 0, pdext_simplified;
for k from -1 to 1 do
(yes, other):=
selectremove(hastype, other, u(anything\$2, identical(n+k)));
expr:= expr + ``(yes)
od
fi:
expr;```

This produces an expression with subgroupings enforced by ``(...), which should be adequate for display purposes. The ``(...can be removed by expand(expr), but this will return it to its original term order. It is possible to sort, but it's a bit more complicated.

## is, SetOf, RealRange, Open, subset...

Like this:

is(SetOf(RealRange(-5,2)) subset SetOf(RealRange(-10, Open(infinity))));
true

is(SetOf(RealRange(-5,2)) subset SetOf(RealRange(Open(-5), Open(infinity))));
false

## Markov chain...

@Newie It looks like you're trying to find the stationary vector of a Markov chain. So, I'll assume that M is a 7x7 stochastic matrix (all entries nonnegative; each row sums to 1). For convenience, I'd like to also assume that no entry is exactly 1. (It's not forbidden for an entry to be 1; it just might require a different solution approach.)

Let's ignore that equation for the moment. The remaining 8 equations in 7 variables should be uniquely solvable. One way is to get an eigenvector of MT corresponding to eigenvalue 1 (see help page ?LinearAlgebra,Eigenvectors). Then divide that vector by the sum of its entries to enforce the 8th equation.

Now for z: It's not really an "equation" (in the sense of a constraint); rather, it's a function definition. Knowing the values of the other 7 variables, just compute z.

## 4 syntax errors...

Only considering upto the first loop, you have 3 syntax errors:

1. In Cond, the last item is spelled Thata instead of Theta.
2. notation rather than diff must be used in initial and boundary conditions. So, the last item in Cond should be
D[1](Theta)(1,tau) = 0. The [1] here indicates differentiation with respect to the 1​​​st variable, which is X.
3. In the loop, the eval command should be
eval([OdeSys, Cond], Nr= NrVals[j])[].

In the next loop (the generation of the numeric points), the eval needs to be replaced with
Ans[k]:-value, and you need to specify values of X and tau to use. Remove the (j); you can't do evaluations of pdsolve(..., numeric) solutions as if they were ordinary functions. The value when used in this way is documented on help page ?pdsolve,numeric.

## Sometimes 'solve' returns empty...

Yes, as you said, sometimes there's no feasible solution. In those cases, the solve returns [] (the empty list), which cannot be indexed by [1]. Your procedure needs to be prepared to handle that. I recommend that you change your command with eval and solve to this, where the "..."s represent what you already have:

...:= eval(..., solve(...)[])[];

Now the procedure will always return 9 values, and in the infeasible cases those values will be symbolic.

## Workaround using 'display'...

That's curious. I don't know what causes the problem, nor have I investigated it, because there's an easy workaround by which you can add "global" options to any plot or group of plots. By "global", I mean options that apply to the overall plot rather than the mathematical options that specify how to calculate points and colors for the plot. The command that does this workaround is plots:-display:

display(
fieldplot3d(
[0, 0, -y], x= -2..2, y= -2..2, z= -2..2, arrows= `3-D`, fieldstrength= maximal(0.5),
font= [Times, bold, 16], grid= [4, 4, 4], labels= ['x', 'y', 'z']
),
#end of fieldplot command
labelfont= [Times, bold, 40]  #additional global option
)

The axes labels being "cut off" immediately above is just caused by the transfer of the plot from my worksheet to MaplePrimes. They appear normal in the worksheet.

I could've also considered the options font and labels to be "additional global options", but I didn't because they were working as is.

The display command can also be used to combine multiple plots into one presentation. See help page ?plots,display.

## Graph => Matrix => ExportMatrix => .mtx...

The ".mtx" (aka MatrixMarket) file format is a matrix format, not necessarily a graph format. Since the entire mathematical structure of a graph is encoded in its weight matrix (or adjacency matrix if you don't care about weights), this doesn't present any serious difficulties; you just need the tiny additional step of creating a Matrix when going from Graph to ".mtx" file or vice versa.

The example file that you linked is a sparse representation likely suitable for the vast majority of graphs. It's not necessary that you understand the representaion in order to use this. Only nonzeros are represented. The 1st column is the row numbers, the 2nd is the column numbers, and the 3rd is the weights. As you said, the 3rd column could be all 1s, making it an adjacency matrix.

To go from a Maple Graph G to a ".mtx' file, use