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 above ideas can be made into a custom convert procedure:

`convert/decimalfraction`:= (x::float)-> op(1,x)/``(10^(-op(2,x))):

convert(.25, decimalfraction);

x^(1/3) is complex valued for negative because Maple uses the principal branch with `^`. The real-valued alternative is surd.

plot(surd(x,3), thickness= 5);

You can generate the procedure by calling LinearAlgebra:-Eigenvectors on a symbolic 2x2 matrix A = < < a, b > | < c, d > > and then reverse engineering the results. Doing that, I got the following

Eigen:= proc(A::'Matrix'(2,2))
local
     a:= A[1,1], b:= A[2,1], c:= A[1,2], d:= A[2,2],
     s:= sqrt((a-d)^2+4*b*c)/2,
     e1:= (a+d)/2 + s, e2:= (a+d)/2 - s,
     ev1:= < c, (d-a)/2+s >, ev2:= < c, (d-a)/2-s >
;
     if s=0 then [] else [e1, e2, ev1, ev2] end if
end proc:

This procedure needs a little modification. There are cases where s=0 but there are still two linearly independent eigenvectors. For example, any nonzero vector is an eigenvector of the zero matrix.

Simply include z=t with the equations that you pass to solve and include z in the output variables.

L1:= {4*x + 3*y + z = 0, x + y - z - 15 = 0}:
solve(L1 union {z=t}, {x,y,z});

BinomialMod2:= (n,k)-> `*`(Bits:-Split(Bits:-Implies(k,n))[]);

This is about 32,000 times faster than binomial(n,k) mod 2 when the input is 5-digit integers. For larger integers, it is even faster than that.

 

Note that the gridlines do not appear in the actual plot. They are an artifact of this forum, MaplePrimes.

restart: #Put restart on its own line, in its own execution group.

hgk:= 0.9:  vgk:= 5:  vb:= 25:  rg:= 0.15:

tb:= 22*sqrt(1+tan(x*Pi/180)^2+tan(y*Pi/180)^2)/vb;

(22/25)*(1+tan((1/180)*x*Pi)^2+tan((1/180)*y*Pi)^2)^(1/2)

tgk:= 2*sqrt(121*tan(x*Pi/180)^2+(11*tan(y*Pi/180)-hgk)^2)/vgk+0.15;

(2/5)*(121*tan((1/180)*x*Pi)^2+(11*tan((1/180)*y*Pi)-.9)^2)^(1/2)+.15

X:= -18..18:  Y:= 0..12:

Pts:= select(
     P-> evalf(eval(tgk-tb, [x,y]=~P)) < 0,
     [seq(seq([x,y], x= X), y= Y)]
):

plot(
     Pts,
     style= point, symbol= cross,
     view= [X,Y], axes= boxed, labels= [x,y]
);

 


Download tan_pointplot.mw

Your procedure does not work because you did not specify a return value. The last line of the procedure should be simply S to return S.

You have the right idea, but your procedure is much more complicated than it needs to be. You never need to have something like

do "statement"; break; od;

because it is equivalent to just

"statement";

Also, there's no need to do S:= S union {}. That statement does not change S. You only need to account for the ones. Here's all that you need:

m:= proc(n::list)
local S:= {}, i;
     for i to nops(n) do
          if n[i]=1 then
               S:= S union {i}
          fi
     od;
     S

end proc;

The above code is for educational purposes only, because it is not an efficient way to perform the task. I just wanted to illustrate the correct loop structure. For an efficient way, see Acer's Answer.


Solved 2014-Feb-21 by Carl Love

restart:
read "C:/Users/Carl/desktop/logic_problems.mpl":

Alan's Birthday Seating logic problem

 

It's Alan’s birthday and he's having a party. Seven other people will attend: Amy, Brad, Beth, Charles, Debbie, Emily and Frances.

 

Everyone will sit around the circular dining table. The seating arrangement must meet the following conditions:

 

• Amy and Alan sit together

• Brad and Beth sit together

• Charles sits next to either Debbie or Emily

• Frances sits next to Debbie

• Amy and Alan do not sit next to either Brad or Beth

• Brad does not sit next to Charles or Frances

• Debbie and Emily do not sit next to each other

• Alan does not sit next to either Debbie or Emily

• Amy does not sit next to Charles

 

List the numeric variable first so that answers are reported in order by that.

Vars:= [PN,Name]:

Name:= [Alan, Amy, Brad, Beth, Charles, Debbie, Emily, Frances]:

PN:=[$1..8]:

Birthday:= LogicProblem(Vars):

with(Birthday);

Warning, Birthday is not a correctly formed package - option `package' is missing

[`&!!`, `&-`, `&<`, `&>`, `&?`, `&G`, `&Soln`, AutoGuess, CPV, CollectStats, ConstNum, Consts, ConstsInV, DifferentBlock, Equation, FreeGuess, GoBack, Guess, InternalRep, IsComplete, IsUnique, NC, NV, PrintConst, Quiet, Reinitialize, SameBlock, Satisfy, Separated, UniquenessProof, VarNum, VarNumC, X_O]

Define some abbreviation functions for NextTo and "not NextTo".

NT:= (a,b)-> NextTo(a, b, PN):

NNT:= (a,b)-> Rel(Separated, a, b, PN, [1]):


Reinitialize():

Con1:= Alan=1, Amy=8:

Con2:= NT(Brad, Beth):

Con3:= OR([NT(Charles, Debbie), NT(Charles, Emily)]):

Con4:= NT(Frances, Debbie):

Con5:= NNT(Amy, Brad), NNT(Amy, Beth), NNT(Alan, Brad), NNT(Alan, Beth):

Con6:= NNT(Brad, Charles), NNT(Brad, Frances):

Con7:= NNT(Debbie, Emily):

Con8:= OR([NNT(Alan, Debbie), NNT(Alan, Emily)]):

Con9:= NNT(Amy, Charles):


Satisfy([Con||(1..9)]);

NULL

`Multiple solutions.  Two of the possibilities:`

Matrix(8, 2, {(1, 1) = 1, (1, 2) = Alan, (2, 1) = 2, (2, 2) = Charles, (3, 1) = 3, (3, 2) = Debbie, (4, 1) = 4, (4, 2) = Frances, (5, 1) = 5, (5, 2) = Beth, (6, 1) = 6, (6, 2) = Brad, (7, 1) = 7, (7, 2) = Emily, (8, 1) = 8, (8, 2) = Amy}), Matrix(8, 2, {(1, 1) = 1, (1, 2) = Alan, (2, 1) = 2, (2, 2) = Emily, (3, 1) = 3, (3, 2) = Brad, (4, 1) = 4, (4, 2) = Beth, (5, 1) = 5, (5, 2) = Charles, (6, 1) = 6, (6, 2) = Debbie, (7, 1) = 7, (7, 2) = Frances, (8, 1) = 8, (8, 2) = Amy})

 


Download Birthday_LP.mw

If f and g are lists, then you may do, simply,

plot([f,g], style= point);

For f and g any indexed structures (list, array, table, sequence), you can do

plot([[seq(f[k], k= 1..10)], [seq(g[k], k= 1..10)]], style= point);

You may add nondefault colors, point shapes, and point sizes by using the options color, symbol, and symbolsize, respectively. For example,

plot([f,g], style= point, color= [green, red], symbol= [cross, diamond], symbolsize= 24);

Every occurence of c() in your code is a separate invocation (or call) of the random-number-generating procedure created by rand(0..1). If you need a stable value, then assign it to a variable (such as your d) and just use that variable.

It's impossible. Applying factor to the polynomial proves that it has no rational roots.

Try this. I've made several simplifications to your code.

restart:
X:= Vector([1, 2, 3, 4, 5, 6], datatype= float[8]):
Y:= Vector([2, 3, 4, 3.5, 5.8, 7], datatype= float[8]):

SLRrepeatedsample:= proc(X,Y,N,CI)
uses ArrayTools, Statistics, RandomTools;
local x, y, n, ymu, ySE, yvar, x2, b, bCI, i;
     n:= Size(X,1);
     b:= LinearFit([1,t], X, Y, t, output= parametervalues);
     ySE:= LinearFit([1,t], X, Y, t, output= residualstandarddeviation);
     x2:= X^~2;
     for i from 1 by 1 to N do
          x:= Generate(float(range= min(X)..max(X)));
          ymu:= b[1]+b[2]*x;
          ySE:= (1+1/n+sqrt(((x-Mean(X))^2)/(add(x2[j],j=1..n)-n*(Mean(X))^2)));
          yvar:= RandomVariable(Normal(ymu, ySE));
          y:= Sample(yvar, 1)[1];
          X(n+1):= x;
          x2:= X^~2;
          Y(n+1):= y;
          b:= LinearFit([1,t], X, Y, t, output= parametervalues);
          ySE:= LinearFit([1,t], X, Y, t, output= residualstandarddeviation);
          n:= Size(X, 1);
     od;
     bCI:= LinearFit([1, t], X, Y, t, confidencelevel= CI, output= confidenceintervals);
     return b, bCI;
end proc:

SLRrepeatedsample(X, Y, 2, .95);

For large A, it's much faster to compute the orbit without computing the powers of A. Also, this solution avoids the list-building-by-appending technique, which takes time O(n^2) where n is the list length.

Orbit:= proc(A::Matrix, V::Vector, n::nonnegint)
# returns [seq(A^k.V, k= 0..n)] without explicitly computing powers of A
local O:= Array(0..n, [V]), k;
     for k to n do O[k]:= A.O[k-1] end do;
     convert(O, list)
end proc:

If you want to apply it to a list of Vectors, use map2. I don't see any significant reason why returning a list of lists is better than returning a list of Vectors; plots:-pointplot will accept both.

Here's one way to do it. While building each A[k+1], I limit lookup access to A[k] to a procedure that traps index-out-of-bounds errors, which are diverted to value 0.

restart:
a:= 1:  b:= -1: #For example
N:= 100:
A:= Array(1..N):
AA:= proc(k,i,j) try A[k][i,j] catch: 0 end try end proc:
A[1]:= < < -1, 1 > | < 0, 1 > >:
for k to N-1 do
     A[k+1]:= Matrix(
          k+2, k+2,
          (i,j)-> a*(k+1)*AA(k,i,j) + b*((1+j)*AA(k,i-1,j) - j*AA(k, i-1, j-1))
     )
end do:    

You are making a mistake with your for-loop syntax. You have

for i= 1..r do ... end do;

but that should be

for i from 1 to r do ... end do;

First 319 320 321 322 323 324 325 Last Page 321 of 395