Carl Love

Carl Love

28015 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

The spectral radius is also the maximal singular value[*1]. Regardless of whether you use command Eigenvalues or SingularValues from LinearAlgebra, numeric results can be quickly obtained by converting the Matrix to datatype= hfloat.

restart:
SpectralRadius:= proc(G::Graph)
uses LA= LinearAlgebra, GT= GraphTheory;
    (max @ LA:-SingularValues)(
        rtable(GT:-AdjacencyMatrix(G), 'datatype'= 'hfloat')
    )
end proc
:
GT:= GraphTheory: SG:= GT:-SpecialGraphs:
M22:= SG:-M22Graph(); #This graph is 16-regular.
      Graph 1: an undirected unweighted graph with 77 vertices 
      and 616 edge(s)

CodeTools:-Usage(SpectralRadius(M22));
memory used=241.62KiB, alloc change=0 bytes, 
cpu time=16.00ms, real time=6.00ms, gc time=0ns

                        16.0000000000000

[*1] The singular values of a real or complex matrix A (not necessarily square) are the square roots of the eigenvalues of A . HermitianTranspose(A). These are necessarily nonnegative reals. In the square floating-point case, these can be computed more efficiently and more stably than the eigenvalues of itself.

The _Y could be replaced by any otherwise unused variable. It's what logicians and computer scientists call a bound variable, and here it's bound to the DESol expression. Mathematicians sometimes call it a dummy variable, but I disdain that term. So, the important thing is what DESol means rather than what _Y(x) means.

So, Maple can't solve your ODE, but it can simplify it a tiny bit. It's saying, essentially, "Here's another, perhaps simpler, ODE (the 1st argument of the DESol equated to 0) whose solution is the same as that of your original ODE." Now, you may think that this new ODE is such a trivial change from your original that it's hardly worth stating, and if all DESols returned by dsolve were that trivial, then I'd agree. But sometimes they're not so trivial.

You could try exporting the worksheet as PDF and then copy-and-paste from the PDF. From the worksheet's File menu, select Export As, then select PDF from the "Files of type" pull-down.

A variation of Tom's Answer:

select(j-> j[[4,7]] = [[C,Z], [B,Y]], Perms)

One way:

solve(2*a +~ (b+2)*~[0,1] =~ [1,1]);

Another (this one doesn't use elementwise operations):

x:= t-> 2*a+b*t+2*t:  solve({x(0)=1, x(1)=1});

Another:

solve((t-> 2*a+b*t+2*t)~([0,1]) =~ [1,1]);

 

Here's an exercise for you. By using the help pages and asking here for pointers to the correct help pages, figure out what this code does and how. It's very much related to your questions about odds, evens, and squares. (My code is dense, but nothing has been intentionally obfuscated.)

EvenSqrSeries:= proc(N::And(posint, odd))
local 
    n:= N, e, 
    sq:= proc(n) 
    local r:= isqrt(n); 
        if r^2 >= n then r-- fi; 
        `if`(r::odd, r-1, r)
    end proc
;
    add([while (n-= (e:= sq(n))^2) > 4 do e od]%^~2) + n
end proc
:

n:= 2*rand(10^99..10^100-1)() + 1;
   [output omitted]
EvenSqrSeries(n);
   [output omitted]
value(%);
   [output omitted]

This code needs to be run in 1D input (plaintext input).

Sort the DataFrame by the date field before you extract the TimeSeries. Let's say the DataFrame is named DF, and the date field is named mydate. Then do, quite simply,

newDF:= sort(DF, mydate);

Then do whatever you did before to extract the TimeSeries, but do it to newDF rather than to DF.

See help page ?DataFrame,sort.

@nm You wrote:

  • The program can't call dsolve twice each time in order to make sure it gets the smaller infolevel output. That will be too inefficient.

That's not true, due to the efficiency provided by the remember tables. Using the ode from your worksheet posted above, I did the following:

  • I ran the dsolve without setting infolevel, which took 17.64 seconds;
  • then I set infolevel[dsolve]:= 2 and reran the dsolve to get the shorter userinfo output, which took 0.141 seconds.

Thus, the 2nd running was 125 times faster than the 1st.

Then I timed the longer userinfo output: I did restart, set infolevel[dsolve]:= 2, and ran the dsolve again. This took 19.5 seconds, which is 10% more than the sum of the previous two runs. So, in a sense you'd actually be saving time by running it twice.

I'm sorry that I didn't step into this discussion earlier. The code below does what that Matlab code was intended to do (implement a discrete simulation of a 1D Wiener process), and it gives you a function (e.g., B) that can be evaluated at any value in a specified interval in the usual way, such as B(1.2).

This requires Maple 2018 or later. If you have an earlier version of Maple, let me know, and I can easily adjust this.

restart:
Wiener1D:= proc(
    T::range(realcons), N::And(posint, Not(1)), W0::realcons:= 0
)
description "Discrete simulation of 1D Wiener process";
uses AT= ArrayTools, In= Interpolation, St= Statistics;
local 
    a:= lhs(T), b:= rhs(T), dt:= evalf((b-a)/(N-1)),
    t:= rtable(1..N, i-> a+(i-1)*dt, datatype= hfloat),
    W:= evalf(W0) +~ AT:-ScanAlongDimension(
        `+`, St:-Sample(Normal(0, sqrt(dt)), N), 'inplace', evalhf
    )
;
    W[1]:= W0;
    In:-LinearInterpolation(t, W)
end proc
:

Example usage:

randomize():
B:= Wiener1D(0..2, 10^5):
B(1.2);

                       
-0.488707422425162

plot(B, 0..2, numpoints= 1000);

This and other stochastic integration simulations can also be done by the Finance package, as I mentioned before.

You might as well use the command specifically made for extracting a sublist based on a boolean condition of the elements: select:

points2:= select(p-> andmap(x-> abs(x)<5, p), points);

A slightly slicker variation is

points2:= select[2](andmap, x-> abs(x)<5, points);

These methods require no indexing and no counting, neither of the overall list nor of its list/point elements; they'll work, unchanged, for any list of lists of explicit real numbers.

The k-values need not be evenly spaced nor in order. Like this:

#any real-valued algebraic expression depending only on x and k:
y:= (x,k)-> x+k:
 
#any real numbers; order doesn't matter:
K:= [1.1, 1.3, 1.7, 2.0]:
 
plot(y~(x,K), x= -2..2, legend= K, caption= "Legend shows k-value");

Maple chooses the colors, but you can choose your own colors and linestyles (dash, dot, etc.) to ensure sufficient visual variation (especially helpful for a large number of lines or curves).

The command parse is by far the most common way (and I think also the most efficient way) to convert a string of digits to its corresponding integer. And parse also has other far more arcane uses for which you can ignore the help for now.

However, an easy way to completely avoid the issue of strings (i.e., to do the work with pure arithmetic) is

convert(x, base, 1000)

This will order the 3-digit integers from least significant (on the left) to most significant (on the right). This is usually the most convenient order for programming (despite looking weird for purely cultural non-mathematical reasons), but if you don't like it that way, do

ListTools:-Reverse(convert(x, base, 1000))

It's a much more fundamental bug in evaluating 0*infinity cases. The following two also return undefined:

int(0, [x= 0..1, y= 0..infinity]);
int(0, [y= 0..infinity, x= 0..1]);

Having cos in the exponent in your code line

x := (u, v) -> a^cos(u)*sin(v);

seems unusual to me. Didn't you intend that to be

x := (u, v) -> a*cos(u)*sin(v);

?

The following uses the permutation selection criterion that no player from either team appears in consecutive matches. There are 9! = 362,880 "raw" permutations of the 9 matches. There are only 1512 that meet that criterion, and this lists them all:

T:= Array(0..8, [[A, X], [A, Y], [A, Z], [B, X], [B, Y], [B, Z], [C, X], [C, Y], [C, Z]]):
Perms:= [
    for p in Iterator:-Permute([$0..8]) do 
        q:= iquo~(p,3); r:= irem~(p,3);
        for k to 8 while q[k]<>q[k+1] and r[k]<>r[k+1] do od;
        if k=9 then [seq](T[[seq](p)]) fi
    od
];

This code relies on the specific order of the "master permutation" T, so don't change that. Specifically, if two indices of T have the same quotient when divided by 3, then the first team member is the same; if the remainder is the same, then the second team member is the same.

First 52 53 54 55 56 57 58 Last Page 54 of 394