Carl Love

Carl Love

28070 Reputation

25 Badges

13 years, 27 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

The derivative of a curve is the tangent of the angle that the tangent line makes with the positive x-axis. You need to find the points where the curves intersect, and find the difference of the arctans of the derivatives at those points. It is not clear whether your g should be cos(x^2) or cos(x)^2. I'll use the latter; the solution process is identical for either.

f:= x-> sqrt(x^4+5)/(sin(x)+5):
g:= x-> cos(x)^2:
plot(f-g);

r:= map2(fsolve, f-g, [-2..0, 0..2]): #Ranges determined from plot.
`-`(arctan@~(D(f),D(g)))~(r);

[-.958033052751182, .830203838366253]

That gives you the two angles in radians.

Those last two lines of code in expanded and perhaps more-readable form are:

r1:= fsolve(f-g, -2..0);
r2:= fsolve(f-g, 0..2);
arctan(D(f)(r1)) - arctan(D(g)(r1));
arctan(D(f)(r2)) - arctan(D(g)(r2));

But I write the way that I do because I disdain repetitive code.

Edit: Corrected "arctan of difference" to "difference of arctans".

Edit: Added code expansion, following VV's suggestion.

I believe that the interrupt button may only work when executing Maple-level code. That means that it doesn't work while kernel code is executing or while results are displaying after the computation is finished.

You shouldn't kill the Java process in Task Manager---that is a huge waste of time---plus you'll need to recover your unsaved worksheets (and those recoveries are not always complete). Instead, using the "More details" view of Task Manager, find the mserver.exe process that is running. It'll be the one with a larger number in the CPU column. You can click on the column header to sort by CPU. Kill that mserver.exe. Your worksheet will now have a "Kernel connection has been lost" pop-up. Ignore what the pop-up says---it is incorrect (and BS). Dismiss the pop-up. Save your worksheet. Close your worksheet. Reopen your worksheet (using the Recent Documents menu item). You're done.

That may sound like a lot of steps, but once you learn them, they'll save you much time over killing the Java process, especially if you have multiple worksheets open. It is just generally a good idea, not just with respect to Maple, to have Task Manager running at all times, in the "More details" view, sorted by the CPU column.

In the definition of Gr you use square brackets [ ]. In Maple, you can't use square brackets for algebraic grouping; you must use parentheses ( ). Square brackets have many uses in Maple, mostly dealing with indexable structures.

Here's an example:
 

f:= randpoly(x):

Student:-Calculus1:-FunctionChart(f, -2..2, pointoptions= [symbolsize= 24, color= red]);

#These are the x-coords of the red circles on the graph (roots or x-intercepts):
fsolve(f, x= -2..2);

-1., .918559742308401

#This is the x-coord of the interior red diamond on the graph (extremum):
fsolve(diff(f,x), x= -2..2);

.233921026056907

#These are the x-coords of the red crosses on the graph (inflection points):
fsolve(diff(f,x,x), x= -2..2);

-.297635283207037, -.101434768857792, 1.54573671873150


 

Download FunctionChart.mw

This is exactly what Records are for.

MyCars:= {
   Record('make'= Honda, 'color'= green, year= 2008),
   Record('make'= Honda, 'color'= red, 'model'= Civic, 'year'= 2004),
   etc.
};

See ?Record.

Then you can do things like

select(C-> C:-color = red, MyCars);

Here's a procedure to handle this which behaves correctly when there are multiple distinct names beginning with _. The key is the use of type suffixed.

Change_:= proc(e, new::name)
local 
   N:= indets(e, suffixed(_)),
   n:= nops(N), k
;
   subs(N=~ `if`(n=1, new, {new||(1..n)}), e)
end proc:  

Usage:

Change_(symbolic, n);

The direct answer to your Question is to execute this after the loop:

Result_Eva_GB_X2:= (x-> `if`(x=[], [0], x))~(Result_Eva_GB_X2);

However, pedagogically, I don't think that this is a good thing to do. The answer is not 0. One possibility is to use undefined instead of 0.

I took the information from the StackExchange article that you referenced and use it to write the procedure LinearizeAbsObj below. Then I used that to resolve the same problem as an ILP with Optimization:-LPSolve. Now the solution is almost instantaneous (16 ms).
 

restart:

Data:= <name, x, y, pop;
        A, 3, 7, 5;  B, 6, 5, 2;  C, 5, 3, 2;
        D, 1, 2, 1;  E, 7, 1, 3              >;

Data := Matrix(6, 4, {(1, 1) = name, (1, 2) = x, (1, 3) = y, (1, 4) = pop, (2, 1) = A, (2, 2) = 3, (2, 3) = 7, (2, 4) = 5, (3, 1) = B, (3, 2) = 6, (3, 3) = 5, (3, 4) = 2, (4, 1) = C, (4, 2) = 5, (4, 3) = 3, (4, 4) = 2, (5, 1) = D, (5, 2) = 1, (5, 3) = 2, (5, 4) = 1, (6, 1) = E, (6, 2) = 7, (6, 3) = 1, (6, 4) = 3})

n:= op([1,1], Data) - 1;

5

City:= Vector(
     n,
     k-> Record(convert(Data[1,..]=~ Data[k+1,..], list)[])
);

City := Vector(5, {(1) = Record(name = A, x = 3, y = 7, pop = 5), (2) = Record(name = B, x = 6, y = 5, pop = 2), (3) = Record(name = C, x = 5, y = 3, pop = 2), (4) = Record(name = D, x = 1, y = 2, pop = 1), (5) = Record(name = E, x = 7, y = 1, pop = 3)})

Dist:= (P1,P2)-> LinearAlgebra:-Norm(P1-P2, 1): #1 is taxi metric.

P:= <x,y>:

Obj:= add(Dist(<City[k]:-x, City[k]:-y>, P)*City[k]:-pop, k= 1..n);   

5*abs(x-3)+5*abs(y-7)+2*abs(x-6)+2*abs(y-5)+2*abs(x-5)+2*abs(y-3)+abs(x-1)+abs(y-2)+3*abs(x-7)+3*abs(y-1)

Procedure to linearize objectives with absolute values:

LinearizeAbsObj:= proc(obj::algebraic, t::name:= :-_t)
local k:= 0, NewCons;
   subsindets(
      obj,
      specfunc(abs),
      proc(ABS)
         k:= k+1;
         NewCons[t[k] >=~ (-1,1)*~op(ABS), t[k] >= 0]:= [][];
         t[k]
      end proc
   ),
   {indices(NewCons, 'nolist')}
end proc:

infolevel[Optimization]:= 2:

CodeTools:-Usage(Optimization:-LPSolve(LinearizeAbsObj(Obj), assume= integer));

LPSolve: calling ILP solver

SolveInteger: number of problem variables 12
SolveInteger: number of general linear constraints 30
memory used=435.90KiB, alloc change=0 bytes, cpu time=16.00ms, real time=11.00ms, gc time=0ns

[51, [x = 5, y = 5, _t[1] = 2, _t[2] = 2, _t[3] = 1, _t[4] = 0, _t[5] = 0, _t[6] = 2, _t[7] = 4, _t[8] = 3, _t[9] = 2, _t[10] = 4]]

 

 


 

Download Taxi_metric_ILP.mw

If A is any real square nonsingular matrix, then A times A-transpose will be symmetric positive definite (i.e., all eigenvalues are positive), which is suitable for Cholesky. The probability that a random matrix is singular is infintesimal, so just take any random real square matrix and muliply it by its transpose. In Maple, the short form for A-transpose is A^+ or A^%T (the former syntax is newer).

A:= LinearAlgebra:-RandomMatrix(6$2):
M:= A.A^+:
L:= LinearAlgebra:-LUDecomposition(M, method= Cholesky);

 

The issue isn't so much that A is nonsquare; it's that any Matrix/Vector/Array operation (not just LinearSolve) that uses option inplace is not allowed to change the size of the container which is supposedly being operated on "in place". So, it fills unused spots with 0. I suppose that a more-logical option might be to use NULL as the fill value, but that wouldn't work if the container were declared sparse.

Addressing your second question: There any many possibilities. Are you looking for an exact solution? Is the system overdetermined (as one would ordinarily expect when rows > columns)? If so, are you looking for a solution in the least-squares sense?

Using matrices/vectors to store the variables Y[i,j] and X[j] makes it easier to enter the objective and the constraints and to display the final results.
 

restart:

p:= 3:

#The demand array, d:
d:= <<2870, 572, 8450, 350, 901, 333, 306, 723, 610> * 1e3>:

n:= rtable_dims(d)[1]:

#The distance matrix:
dist:= Matrix(
   (n$2), shape= symmetric, scan= triangular[upper],
   (r-> [0, r[]])~(
      [[720, 790, 297, 283, 296, 461, 769, 996],
           [884, 555, 722, 461, 685, 245, 1099],
                 [976, 614, 667, 371, 645, 219],
                     [531, 359, 602, 715, 1217],
                           [263, 286, 629, 721],
                                [288, 479, 907],
                                     [448, 589],
                                          [867]
      ]
   )
):

Y:= Matrix((n$2), symbol= y):
X:= Vector(n, symbol= x):
ones:= Matrix((n,1), fill= 1):


#We seek to minimize the objective function, z:
z:= add(dist*~Y.d):


#subject to the constraints:
constraints:= {
   convert(Y.ones =~ 1, set)[],
   add(X) <= p,
   seq(seq(y[i,j] <= x[j], i= n), j= n)
}:

Sol:= Optimization:-LPSolve(z, constraints, assume= binary):

labels:= convert~([$n], symbol):
'z' = Sol[1],
'Y' = DataFrame(eval(Y,Sol[2]), (rows,columns)=~ 'labels'),
'X' = DataSeries(eval(X,Sol[2]), 'labels'= labels);

z = 6.75328*10^8, Y = (Matrix(10, 10, {(1, 1) = ``, (1, 2) = `1`, (1, 3) = `2`, (1, 4) = `3`, (1, 5) = `4`, (1, 6) = `5`, (1, 7) = `6`, (1, 8) = `7`, (1, 9) = `8`, (1, 10) = `9`, (2, 1) = `1`, (2, 2) = 0, (2, 3) = 0, (2, 4) = 0, (2, 5) = 0, (2, 6) = 0, (2, 7) = 1, (2, 8) = 0, (2, 9) = 0, (2, 10) = 0, (3, 1) = `2`, (3, 2) = 0, (3, 3) = 1, (3, 4) = 0, (3, 5) = 0, (3, 6) = 0, (3, 7) = 0, (3, 8) = 0, (3, 9) = 0, (3, 10) = 0, (4, 1) = `3`, (4, 2) = 0, (4, 3) = 0, (4, 4) = 0, (4, 5) = 0, (4, 6) = 0, (4, 7) = 0, (4, 8) = 0, (4, 9) = 0, (4, 10) = 1, (5, 1) = `4`, (5, 2) = 0, (5, 3) = 0, (5, 4) = 0, (5, 5) = 0, (5, 6) = 0, (5, 7) = 1, (5, 8) = 0, (5, 9) = 0, (5, 10) = 0, (6, 1) = `5`, (6, 2) = 0, (6, 3) = 0, (6, 4) = 0, (6, 5) = 0, (6, 6) = 0, (6, 7) = 1, (6, 8) = 0, (6, 9) = 0, (6, 10) = 0, (7, 1) = `6`, (7, 2) = 0, (7, 3) = 0, (7, 4) = 0, (7, 5) = 0, (7, 6) = 0, (7, 7) = 1, (7, 8) = 0, (7, 9) = 0, (7, 10) = 0, (8, 1) = `7`, (8, 2) = 0, (8, 3) = 0, (8, 4) = 0, (8, 5) = 0, (8, 6) = 0, (8, 7) = 1, (8, 8) = 0, (8, 9) = 0, (8, 10) = 0, (9, 1) = `8`, (9, 2) = 0, (9, 3) = 1, (9, 4) = 0, (9, 5) = 0, (9, 6) = 0, (9, 7) = 0, (9, 8) = 0, (9, 9) = 0, (9, 10) = 0, (10, 1) = `9`, (10, 2) = 0, (10, 3) = 0, (10, 4) = 0, (10, 5) = 0, (10, 6) = 0, (10, 7) = 0, (10, 8) = 0, (10, 9) = 0, (10, 10) = 1})), X = (Matrix(9, 2, {(1, 1) = `1`, (1, 2) = 0, (2, 1) = `2`, (2, 2) = 1, (3, 1) = `3`, (3, 2) = 0, (4, 1) = `4`, (4, 2) = 0, (5, 1) = `5`, (5, 2) = 0, (6, 1) = `6`, (6, 2) = 1, (7, 1) = `7`, (7, 2) = 0, (8, 1) = `8`, (8, 2) = 0, (9, 1) = `9`, (9, 2) = 1}))

 


 

Download logistics_ILP.mw

To see the prime factorization in its raw list form, use ifactors instead of ifactor:

ifactors(8650368);

     [1, [[2, 7], [3, 3], [2503, 1]]]

ifactors(8650368)[2];

     [[2, 7], [3, 3], [2503, 1]]

To see that expressed as a product of powers, do

InertForm:-Typeset(`%*`((p-> ``(p[1])^p[2])~(ifactors(8650368)[2])[]));

     (2)^7 * (3)^3 * (2503)

This isn't really a question about Maple because any operating system should have utilities that track and log the usage of any and every program. Any half-way competent sysadmin knows how to access this information . 

Assuming that you're solving an integer linear program (ILP) via Optimization:-LPSolve, you would specify that the variables x[2,3] and x[1,4] (for example) can only take the values 0 or  1 by including the option binaryvariables= {x[2,3], x[1,4]} in the command. See ?LPSolve.

Every time that you have L by itself on the left side of := it changes what L is. So, after your second line, L is no longer an Array, it's a sequence. Here's how to add a new element position:

L:= Array([one, two, three, four, five]):
L(numelems(L)+1):= six:

The spelled out numbers could be anything, and the left side of the second line could be simply L(6).

 

First 201 202 203 204 205 206 207 Last Page 203 of 395