Carl Love

Carl Love

28055 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

If your goal from having a list is to recover the position numbers of items in that list, then it'd be many, many times faster to use a table whose indices are the list entries and whose entries are the positions. Both the time to create the table and the time to do the lookups are insignificant compared with the time to use a list. Here's a time comparison:

 

(L,K):= 'RandomTools:-Generate(list(posint(range= 2^17), 2^16))' $ 2:


st:= time():
for k in K do member(k,L,'p') end do:
time()-st;

19.750

#Time to create table:
st:= time():  T:= table(L=~[$1..nops(L)]):  time()-st;

0.94e-1

#Time to use table:
st:= time():
for k in K do p:= T[k] end do:
time()-st;

0.31e-1

 

 

Download table_lookup.mw

If there are multiple copies of any list entries, the table above only records one instance of that entry. The building of the table can be modified to accomodate multiple entries. This'll cause no change in the lookup time. 

 

There's no need for fea and no need for recursion. Try this:

MilRab := proc(n)
local N:= n-1, F:= FactorInteger(N), i, C:= rand(N)() &^ (N/F[1,1]^F[1,2]) mod n;
     if C = 1 or C = N then return true elif k = 1 then return false fi;
     for i to F[1,2]-1 do C:= C^2 mod n;  if C = 1 then return false elif C = N then return true fi od;
     printf("All tests exhausted.")
end proc;

I don't know whether to return true or false or FAIL if the loop completes.

Your attempt to attach a plot did not work.

One option is to use option gridlines, for example

plot(x, x= -10..10, gridlines);

However, you may consider this to be too visually intrusive.

For your second question, to sort by descending indices, you can do

ex:= a[2]+a[3]+a[1]:
sort(ex, sort([op(ex)], key= op));

This Answer is to address the OP's followup question. There are several naive ways to implement your FirstMatch with very brief code; however, these mostly have quadratic running time. The following method avoids repeated linear searches of A by first converting it to a set.

A:= [5,10,15,20,25,30,35,40,45,50]:
B:= [7,14,21,28,35,42]:

FirstMatch:= proc(B::list, A::list)
local AS:= {A[]}, i, j;
     for i to nops(B) do if B[i] in AS then member(B[i], A, j); return i,j fi od
end proc:

FirstMatch(B,A);

     5, 7

I prefer to achieve results similar to Kitonum's simply by using the Statistics:-Histogram command. In the code below, the 10000 can be any similar large number, and the .5 in the binbounds can be adjusted as needed.

X:= [1,     3,      5,     7     ]:
P:= [1/8, 1/2, 1/4, 1/8]:
Statistics:-Histogram(
     zip(`$`, X, round~(10000*P)),
     binbounds= (x-> (x-.5, x+.5))~(X), 
     tickmarks= [X, P =~ P]
);

See ?Statistics,Histogram, ?$, ?zip, ?plot,tickmarks, and ?plot,typesetting.

There are many things that can be done. Do you want an exact result, or would a decimal result be okay? If a decimal result is okay, use evalf like this:

Ans:= dsolve([sys_ode, ics]);
evalf(Ans);

If you want to maintain the exactness of the result, do

simplify(expand(Ans), size);

These approaches can be combined in any order, but note that conversion to decimal form is essentially a one-way operation:

evalf(simplify(expand(Ans), size));

In the future, please post your code as an attached worksheet rather than an attached picture. That way we can work with it. Also do this if you want more help on this problem.

Use applyrule like this,

rule:= abs(''a''::algebraic)*abs(1,''a''::algebraic)= ''a'':

(Note that those quotes are double single quotes, not single double quotes.) Example of use:

ex:= sin(x)*abs(sin(y))*abs(1,sin(y)):
applyrule(rule, ex);

Your simplify with side relations didn't work because that only works with polynomial relations.

int(t-> N(i, n, t, T)*N(j, n, t, T), 0..1, numeric):

If Phi is your matrix, then do

subsindets(
     plots:-sparsematrixplot(Phi, 'matrixview'),
     specfunc(AXESTICKS),
     subsindets,
     list,
     L-> `if`(L::list(`=`), [seq(lhs(E) = Eq||(rhs(E)), E= L)], L =~ [q||(L[])])
);

This'll produce decent results even if the matrix is large. If you try to explicitly set the tickmarks (as in Kitonum's solution) for a large matrix, they may be too crowded to view. My solution modifies the tickmarks that are already in the plot rather than creating new tickmarks.

indets(eq, function(identical(t)));

The lhs and rhs are superfluous.

It's fairly straightforward:

f:= cos(2*t/m) + cos(2*(t+5)/m):
plot('maximize'(f), m= 1..10);

It's quite an interesting function! I wouldn't have expected the points of nondifferentiability (one is at m=Pi I believe).

Note that the command plot('maximize(f)', m= 1..10) won't work because that makes it treat f as unassigned.

Yes, it is easy to use a color gradient in a 2D point plot. In the following, assume that

  • XY is a list of pairs of (x,y)-values (each pair itself being a list), 
  • f is a function of two variables that returns your f-value for any point as a floating-point number (force it to floating point with evalf if you need to),
  • Mf is the maximum value of f,
  • mf is the minimum value of f.

Then do

R:= Mf - mf:
plot(`[]`~(XY), style= point, color= [seq(COLOR(HUE, (f(xy[]) - mf)/R), xy= XY)]);

For example, here I take 2^12 random points in [-9,9] x [-9,9] and color them based on the sine of the x-coordinate:

XY:= RandomTools:-Generate(list(list(float(range= -9..9), 2), 2^12)):
plot(`[]`~(XY), style= point, color= [seq(COLOR(HUE, (1+sin(xy[1]))/2), xy= XY)]);

 It is also possible to do a gradient between any two given colors.

Here is the command to create the graph, where and are the vertices of degree 2 that I added.

K:= GraphTheory:-Graph({{1,A},{1,2},{1,4},{1,B},{2,3},{3,4},{2,A},{4,B}, {1,3}}):

Now you can work with the graph object using other commands in the GraphTheory package. For example, you can display it:

GraphTheory:-DrawGraph(K, style= planar);

You can't (safely) use implied multiplication, and you need parentheses for all function calls. So, you need to change your definition of r to

r:= t-> <4*cos(2*t)+4*sin(t), 5*cos(3*t)+4*cos(t),(sin(3*t)+2*sin(t))^2>;

Then your spacecurve command will work.

First 234 235 236 237 238 239 240 Last Page 236 of 395