Carl Love

Carl Love

28070 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

You're not ruining my vacation. I love nothing more than showing people how to use my LogicProblem package, and I only worked on these at night when there wasn't anything local to do. Besides, I did most of this one while waiting for my flight in the San Jose, Costa Rica airport.

You made two mistakes in coding this problem (the "Meet the Challenge" or "Book Swap" problem). The first is that you need to make the surnames a separate variable. The second is that all of the constants' names need to be unique. So, even though the books brought are the same as the books borrowed, I make the names distinct by prepending br_ or bo_.

After defining the variables and constants thus, all of the constraints except #20 are straightforward equalities. #20 needs to be specified in a procedural form accepted by the package. I too tired right now to explain in detail how these procedures work, but I urge you to read my comments to the code of the package, especially the long comment preceding procedure `Know/Proc`.

Please make a separate thread for each new logic problem.

 

Solved 2014-Feb-17

restart:

"Book Swap" logic problem

Eight married couples meet to lend one another some books. Couples have the same surname, employment and car. Each couple has a favorite color. Furthermore we know the following facts:

 

1. Daniella Black and her husband work as Shop-Assistants.

2. The book "The Seadog" was brought by a couple who drive a Fiat and love the color red.

3. Owen and his wife Victoria like the color brown.

4. Stan Horricks and his wife Hannah like the color white.

5. Jenny Smith and her husband work as Warehouse Managers and they drive a Wartburg.

6. Monica and her husband Alexander borrowed the book "Grandfather Joseph".

7. Mathew and his wife like the color pink and brought the book "Mulatka Gabriela".

8. Irene and her husband Oto work as Accountants.

9. The book "We Were Five" was borrowed by a couple driving a Trabant.

10. The Cermaks are both Ticket-Collectors who brought the book "Shed Stoat".

11. Mr and Mrs Kuril are both Doctors who borrowed the book "Slovacko Judge".

12. Paul and his wife like the color green.

13. Veronica Dvorak and her husband like the color blue.

14. Rick and his wife brought the book "Slovacko Judge" and they drive a Ziguli.

15. One couple brought the book "Dame Commissar" and borrowed the book "Mulatka Gabriela".

16. The couple who drive a Dacia, love the color violet.

17. The couple who work as Teachers borrowed the book "Dame Commissar".

18. The couple who work as Agriculturalists drive a Moskvic.

19. Pamela and her husband drive a Renault and brought the book "Grandfather Joseph".

20. Pamela and her husband borrowed the book that Mr and Mrs Zajac brought.

21. Robert and his wife like the color yellow and borrowed the book "The Modern Comedy".

22. Mr and Mrs Swain work as Shoppers.

23. "The Modern Comedy" was brought by a couple driving a Skoda.

 

Who likes Violet? And can you find out everything about everyone from this?

 

Vars:= [Wife, Husband, Surname, Occupation, Car, Color, Brought, Borrowed]:

 

Wife:= [Daniella,Hannah,Jenny,Monica,Victoria,Irene,Veronica,Pamela]:

Husband:= [Stan,Owen,Alex,Mathew,Oto,Robert,Paul,Rick]:
Surname:= [Black, Kuril, Horricks, Smith, Cermak, Zajac, Swain, Dvorak]:
Occupation:= [shop,ware,acc,ticket,docs,teach,agri,shoppers]:

Car:= [fiat,wartburg,trabant,ziguli,moskvic,skoda,renault,dacia]:

Color:= [red,brown,white,pink,green,blue,violet,yellow]:

Books:= [mulatka, modern, dame, we, shed, seadog, grandfather, slovacko]:

Brought:= map2(cat, br_, Books);

[br_mulatka, br_modern, br_dame, br_we, br_shed, br_seadog, br_grandfather, br_slovacko]

Borrowed:= map2(cat, bo_, Books):

Con0:= (Brought <>~ Borrowed)[]:
Con1:= Daniella = Black, Black = shop:
Con2:= br_seadog = fiat, fiat = red:
Con3:= Owen = Victoria, Owen = brown:
Con4:= Stan = Horricks, Stan = Hannah, Stan = white:
Con5:= Jenny = Smith, Jenny = ware, Jenny = wartburg:
Con6:= Monica = Alex, Monica = bo_grandfather:
Con7:= Mathew = pink, pink = br_mulatka:
Con8:= Irene = Oto, Oto = acc:
Con9:= bo_we = trabant:
Con10:= Cermak = ticket, ticket = br_shed:
Con11:= Kuril = docs, docs = bo_slovacko:
Con12:= Paul = green:
Con13:= Veronica = Dvorak, Dvorak = blue:
Con14:= Rick = br_slovacko, Rick = ziguli:
Con15:= br_dame = bo_mulatka:
Con16:= dacia = violet:
Con17:= teach = bo_dame:
Con18:= agri = moskvic:
Con19:= Pamela = renault, Pamela = br_grandfather:

Constraint 20 is tricky. It means at least what I have below. The current version of my package does not directly handle relative dependent clauses ("that Mr and Mrs Zajac brought"). I am working on a version that does handle them. But the current version does handle any type of constraint that can be specified with a procedure in a certain format that I designed for the package. It is easier to write these procedures after the with of the module has been executed (so that you can use the module's methods in the procedure), so I defer the rest of the specification of Constraint 20 until then.

Con20:= Pamela <> Zajac:
Con21:= Robert = yellow, yellow = bo_modern:
Con22:= Swain = shoppers:
Con23:= br_modern = skoda:

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

BookSwap:= LogicProblem(Vars):

with(BookSwap);

Warning, BookSwap 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]

Con20:=
     Pamela <> Zajac,
     Proc(proc()
     local
          #"The book that Pamela borrowed."
          book1:= ConstNum(Pamela) &Soln VarNum(Borrowed),
          #"The book that the Zajacs brought."
          book2:= ConstNum(Zajac) &Soln VarNum(Brought)
     ;
          if book1 = 0 and book2 = 0 then #Nothing known yet
               false, false
          elif book1 > 0 and book2 = 0 then
               false, false, [Zajac = Consts[book1 - CPV]]
          elif book2 > 0 and book1 = 0 then
               false, false, [Pamela = Consts[book2 + CPV]]
          elif book1-CPV <> book2 then #Contradicts this constraint.
               true
          else #This constraint is already satisfied.
               false, true
          end if
     end proc, []):
      

Reinitialize();

Satisfy([Con||(0..23)]);

NULL

`Unique solution:`

Matrix(8, 8, {(1, 1) = Daniella, (1, 2) = Mathew, (1, 3) = Black, (1, 4) = shop, (1, 5) = trabant, (1, 6) = pink, (1, 7) = br_mulatka, (1, 8) = bo_we, (2, 1) = Hannah, (2, 2) = Stan, (2, 3) = Horricks, (2, 4) = agri, (2, 5) = moskvic, (2, 6) = white, (2, 7) = br_dame, (2, 8) = bo_mulatka, (3, 1) = Jenny, (3, 2) = Robert, (3, 3) = Smith, (3, 4) = ware, (3, 5) = wartburg, (3, 6) = yellow, (3, 7) = br_we, (3, 8) = bo_modern, (4, 1) = Monica, (4, 2) = Alex, (4, 3) = Cermak, (4, 4) = ticket, (4, 5) = dacia, (4, 6) = violet, (4, 7) = br_shed, (4, 8) = bo_grandfather, (5, 1) = Victoria, (5, 2) = Owen, (5, 3) = Kuril, (5, 4) = docs, (5, 5) = skoda, (5, 6) = brown, (5, 7) = br_modern, (5, 8) = bo_slovacko, (6, 1) = Irene, (6, 2) = Oto, (6, 3) = Zajac, (6, 4) = acc, (6, 5) = fiat, (6, 6) = red, (6, 7) = br_seadog, (6, 8) = bo_shed, (7, 1) = Veronica, (7, 2) = Rick, (7, 3) = Dvorak, (7, 4) = teach, (7, 5) = ziguli, (7, 6) = blue, (7, 7) = br_slovacko, (7, 8) = bo_dame, (8, 1) = Pamela, (8, 2) = Paul, (8, 3) = Swain, (8, 4) = shoppers, (8, 5) = renault, (8, 6) = green, (8, 7) = br_grandfather, (8, 8) = bo_seadog})

 

Download BookSwap_LP.mw

If you try to pass a sequence to convert it mistakes that as mulitple arguments, which it doesn't know how to handle. It is easier to work with lists than sequences.

a:= [seq(taylor(cos(x),x=0,i),i=[2,4,6])]:
convert(a, polynom);

The soccer problem is very easy with my LogicProblem package because all of the constraints translate directly to equalities and inequalities.  Your file did not attach, so I can't see what you did wrong.

 

Solved 2014-Feb-15 from Puerto Viejo de Talamanca, Limon, Costa Rica by Carl Love

 

restart:

Soccer Logic Problem

 

There are 7 boys on a soccer team. Each boy has a different position, jersey number, and height. Find out each person's height, position, and number.

 

* Justin is the goalie

* The right forward is #10

* The goalie is 6' 4''

* Joe is 6' 1''

* Ryan is right forward

* The person next to Ryan is #14

* The person who is 6' 4'' is #16

* #10 is 5' 8''

* The left forward is #15

* Brad is right defense

* Brendan is #20

* Michael is 7' 1''

* Miguel is #6

* Michael is left forward

* #42 is right defense

* Brad is 6' 2''

* #6 is center defense

* #15 is 7' 1''

* #20 is 6' 7''

* Miguel is 5' 10''

* #14 is 6' 1''

* The person who is 6' 7'' is left defense

* Joe is center forward

 

Vars:= [Name,Pos,Number,Height]:

Name:= [justin, joe, ryan, brad, brendan, michael, miguel]:

Pos:= [goalie, rt_for, lt_for, rt_def, cen_def, lt_def, cen_for]:

Number:= [10, 14, 16, 15, 20, 6, 42]:

Height:= [`5'8"`, `5'10"`, `6'1"`, `6'2"`, `6'4"`, `6'7"`, `7'1"`]:

There is some trouble in deciding what "next to" means in the sixth constraint, "The person next to Ryan is #14". It certainly means at least that Ryan <> #14. This is the least-constraining option, and we'll use it as a start. If we get a unique solution, then we're done.

Cons:= [justin=goalie, rt_for=10, goalie=`6'4"`, joe=`6'1"`, ryan=rt_for, ryan<>14,
     `6'4"`=16, 10=`5'8"`, lt_for=15, brad= rt_def, brendan=20, michael=`7'1"`,
     miguel=6, michael=lt_for, 42=rt_def, brad=`6'2"`, 6=cen_def, 15=`7'1"`,
     20=`6'7"`, miguel=`5'10"`, 14=`6'1"`, `6'7"`=lt_def, joe=cen_for
]:

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

Soccer:= LogicProblem(Vars):

with(Soccer):

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

Satisfy(Cons);

NULL

`Unique solution:`

Matrix(7, 4, {(1, 1) = justin, (1, 2) = goalie, (1, 3) = 16, (1, 4) = `6'4"`, (2, 1) = joe, (2, 2) = cen_for, (2, 3) = 14, (2, 4) = `6'1"`, (3, 1) = ryan, (3, 2) = rt_for, (3, 3) = 10, (3, 4) = `5'8"`, (4, 1) = brad, (4, 2) = rt_def, (4, 3) = 42, (4, 4) = `6'2"`, (5, 1) = brendan, (5, 2) = lt_def, (5, 3) = 20, (5, 4) = `6'7"`, (6, 1) = michael, (6, 2) = lt_for, (6, 3) = 15, (6, 4) = `7'1"`, (7, 1) = miguel, (7, 2) = cen_def, (7, 3) = 6, (7, 4) = `5'10"`})

 

 

Download Soccer_LP.mw

 

I can't reproduce your problem, so I am not sure this will fix it. In procedure `*`, you should refer to global `*` as :-`*`. You should also make i local.

m:= module() option package; export `*`;
    `*`:= proc( a::list, b::list) option overload;
     local i;
        return add(i, i  in zip(:-`*`, a, b));
    end proc;    
end module;

Let me know if that helps. And if you can get a reproducible example of the problem, please post it.

 

Your data are too large. Try scaling down by a factor of 1000.

F1 := Vector([1500, 1750, 2250, 4000, 4300, 5000, 7000]):
Statistics:-MaximumLikelihoodEstimate(Weibull(beta, eta), F1 /~ 1000);

Weibull has a linear scaling property for the first parameter, so the answer using your original data is

applyop(x-> 1000*x, [1,2], %);

I used solve to solve the resulting n^2 x n^2 system of linear equations. There may be a more-efficient method.

restart:

The procedure:
Solve:= proc(A::Matrix(square), B::Matrix(square), C::Matrix(square))
# Solves matrix equation A.X + X.B = C for X.
local
     i, j, x,
     X:= Matrix(rtable_dims(A), symbol= x),
     xs:= {seq(seq(x[i,j], j= rtable_dims(X)[2]), i= rtable_dims(X)[1])}
;
     if [rtable_dims(A)] <> [rtable_dims(B)] or [rtable_dims(B)] <> [rtable_dims(C)] then
          error "Incompatible matrix dimensions"
     end if;
     eval(X, solve(convert(A.X+X.B =~ C, set), xs));
end proc:

Example of use:
(A,B,C):= ('LinearAlgebra:-RandomMatrix(3,3)' $ 3);

x:= Solve(A,B,C);

 

Verification:
A.x + x.B;

LinearAlgebra:-Equal(%, C);      

                                       true

Download Matrix_equation.mw

restart:
5*sqrt(1-sb^2) = 6*sqrt(1-sb^2)*sa*sqrt(1-sa^2)+4*sb,
42*sqrt(1-sa^2) = 3*sqrt(1-sa^2)*sb*sqrt(1-sb^2)+4*sa:
fsolve({%}, {sa=.995, sb=.743});

 

Next time, please post your equations in plaintext form so that I do not need to type them in.

There is a huge variety of nonsensical input that you can give to Maple for which it does not issue an error message. Don't try to ascribe meaning to the output. (There is an old saying of computer science: GIGO---Garbage In, Garbage Out.) As far as I know, there is never a reason to give FAIL as input. See ?FAIL and ?boolean .

According to the technical definitions of bug and feature, a situation which is not described in a program's documentation cannot be considered as either one. A bug is a situation where a program acts contrary to its documentation; a feature is a useful situation where a program acts in accordance with its documentation.

There are two steps to convert a list of points into piecewise form. First, apply the procedure JoggedLine (which can be found in this post by Kitonum, the procedure's author) to the list of points. This will give you an expression in absolute value form. Then apply convert(..., piecewise) to that expression. Example:

JoggedLine([[0,2],[3,-2],[7,6]]);

convert(%, piecewise);

 

Adri's Answer suffers from a common flaw in Maple code: It builds a sequence or list by iteratively appending each new element onto the end of an existing sequence or list. This is very inefficient in Maple because on each iteration it creates a new sequence or list. Thus the time complexity of this operation is O(n^2) where n is the number of items in the sequence or list.

There are two main alternatives: (1) Build with a seq command, or (2) build using a table or dynamic Vector or Array and convert to a sequence or list as the last step. The seq option is often not possible, but it is in this case. The are also several other ways to simplify the code.


restart:

S:= proc(a, b, s)

local x, y;
     Array([seq([y, min(select(`>`, [fsolve(f(x,y))], 0))], y= a..min(b,1), s)])

end proc:

 

Example of use:

f:= unapply(randpoly([x,y]), x,y);

proc (x, y) options operator, arrow; -92*x^3*y^2+6*x^2*y^3+74*x*y^4+23*x^4+75*x*y^3-50*x end proc

R:= S(.2, 2, .1);

R := Matrix(9, 2, {(1, 1) = .2, (1, 2) = 1.34423469254842, (2, 1) = .3, (2, 2) = 1.40236242650571, (3, 1) = .4, (3, 2) = 1.48386699558845, (4, 1) = .5, (4, 2) = 1.59489085298900, (5, 1) = .6, (5, 2) = 1.75109495862678, (6, 1) = .7, (6, 2) = 1.98664891453434, (7, 1) = .8, (7, 2) = .697772717030054, (8, 1) = .9, (8, 2) = 1.08004688099483, (9, 1) = 1.0, (9, 2) = 1.31587196629247})

 

``


Download Array_build.mw


restart:

The procedure:

Solve:= (A::Matrix(square), B::Matrix(square), C::Matrix(square))->
     LinearAlgebra:-LinearSolve(A+B, C):

Example of use:

(A,B,C):= ('LinearAlgebra:-RandomMatrix(2,2)' $ 3);

A, B, C := Matrix(2, 2, {(1, 1) = 44, (1, 2) = -31, (2, 1) = 92, (2, 2) = 67}), Matrix(2, 2, {(1, 1) = 8, (1, 2) = 99, (2, 1) = 69, (2, 2) = 29}), Matrix(2, 2, {(1, 1) = -32, (1, 2) = -4, (2, 1) = -74, (2, 2) = 27})

x:= Solve(A,B,C);

x := Matrix(2, 2, {(1, 1) = -490/1489, (1, 2) = 555/1489, (2, 1) = -326/1489, (2, 2) = -512/1489})

Verification of correctness:

A.x + B.x;

Matrix(2, 2, {(1, 1) = -32, (1, 2) = -4, (2, 1) = -74, (2, 2) = 27})

LinearAlgebra:-Equal(%, C);

true

``


Download Matrix_solve.mw

You can trap the error with the try statement. Here's an example. It is not necessary to use parse; any code can come after the try. The parse statement is useful if you want to set up your 50 problems as strings of code to be evaluated.

restart:
Problems:= Vector(50, k-> sprintf("50/(25-%d)", k)):
Answers:= Vector(50):
for k to 50 do
     try
          Answers[k]:= parse(Problems[k])
     catch:
          Answers[k]:= sprintf("k = %d: %s", k, lasterror);
          print('k'= k, "Error found")
     end try
end do;

Answers[25];

"k = 25: numeric exception: division by zero"

Answers[50];

−2

See ?try .

For your second question T(..., ...) is called "programmer indexing". See ?rtable_indexing .

The following is a hack that temporarily patches the deficiency/bug in PDEtools:-dchange that Alejandro pointed out. It gets you what you aked for: the application of dchange, but with unexpanded/unevaluated derivatives.

 

restart:

Reynolds:= Diff(p(x)*h(x)^3/(12*mu)
     *Diff(p(x),x),x)-u(x)/2*Diff(p(x)*h(x),x)
     +Diff(p(x)*h(x)^3/(12*mu)*Diff(p(x),z),z)
     =Diff(p(x)*h(x),t);

Diff((1/12)*p(x)*h(x)^3*(Diff(p(x), x))/mu, x)-(1/2)*u(x)*(Diff(p(x)*h(x), x))+Diff((1/12)*p(x)*h(x)^3*(Diff(p(x), z))/mu, z) = Diff(p(x)*h(x), t)

Start of hack.

unprotect(diff);  sav_diff:= eval(diff):  diff:= fiff: #Nonsense name.

ans1:= PDEtools:-dchange({p(x)=P(X)*Pa,x=Lx*X,h(x)=H(X)*h2},Reynolds,{P,h,X,u,H});

fiff(fiff((1/12)*P(X)*Pa*H(X)^3*h2^3*fiff(fiff(P(X)*Pa, X)/fiff(Lx*X, X), [])/mu, X)/fiff(Lx*X, X), [])-(1/2)*u(X, Lx)*fiff(fiff(P(X)*Pa*H(X)*h2, X)/fiff(Lx*X, X), [])+fiff((1/12)*P(X)*Pa*H(X)^3*h2^3*fiff(P(X)*Pa, [z])/mu, [z]) = fiff(P(X)*Pa*H(X)*h2, [t])

diff:= eval(sav_diff):  protect(diff):

ans2:= eval(ans1, [fiff= ((f,x)-> `if`(x=[], f, Diff(f,x)))]);

(Diff((1/12)*P(X)*Pa*H(X)^3*h2^3*(Diff(P(X)*Pa, X))/(mu*(Diff(Lx*X, X))), X))/(Diff(Lx*X, X))-(1/2)*u(X, Lx)*(Diff(P(X)*Pa*H(X)*h2, X))/(Diff(Lx*X, X))+Diff((1/12)*P(X)*Pa*H(X)^3*h2^3*(Diff(P(X)*Pa, [z]))/mu, [z]) = Diff(P(X)*Pa*H(X)*h2, [t])

fiff:= eval(diff):

End of hack.

ans2 is the desired answer with unevaluated derivatives. The rest of this worksheet is simply a proof of the correctness of ans2.

save Reynolds, ans2, "C:/Users/Carl/desktop/dchange_hack.mpl";


restart:

read "C:/Users/Carl/desktop/dchange_hack.mpl":

Use regular PDEtools:-dchange.

ans3:= PDEtools:-dchange({p(x)=P(X)*Pa,x=Lx*X,h(x)=H(X)*h2},Reynolds,{P,h,X,u,H});

((1/12)*(diff(P(X), X))^2*Pa^2*H(X)^3*h2^3/(mu*Lx)+(1/4)*P(X)*Pa^2*H(X)^2*h2^3*(diff(P(X), X))*(diff(H(X), X))/(mu*Lx)+(1/12)*P(X)*Pa^2*H(X)^3*h2^3*(diff(diff(P(X), X), X))/(mu*Lx))/Lx-(1/2)*u(X, Lx)*((diff(P(X), X))*Pa*H(X)*h2+P(X)*Pa*(diff(H(X), X))*h2)/Lx = 0

value(ans2);

((1/12)*(diff(P(X), X))^2*Pa^2*H(X)^3*h2^3/(mu*Lx)+(1/4)*P(X)*Pa^2*H(X)^2*h2^3*(diff(P(X), X))*(diff(H(X), X))/(mu*Lx)+(1/12)*P(X)*Pa^2*H(X)^3*h2^3*(diff(diff(P(X), X), X))/(mu*Lx))/Lx-(1/2)*u(X, Lx)*((diff(P(X), X))*Pa*H(X)*h2+P(X)*Pa*(diff(H(X), X))*h2)/Lx = 0

simplify(lhs(ans3) - lhs(%));

0

 

``

 

Download dchange_hack.mw

I have a feeling that there's a more efficient algorithm to generate a round-robin tournament schedule than what I have below. I end up throwing out 2/3 of the partitions. Hopefully a more-skilled combinatorist can comment on the efficiency. Nonetheless, it does get you the answer that you need.

Other interesting questions are:

  1. How do you count how many essentially distinct round-robin tournament schedules there are?
  2. Is it possible to generate them efficiently with an iterator?
  3. How do the answers change when the number of teams is odd? (The code below will not work when the number of teams is odd.)

In the code below, remember to use compile= false as an option to SetPartitions if you don't have a Maple compiler.

restart:
T:= {}:
night:= 0:
for P in Iterator:-SetPartitions({A,B,C,D,E,F}, [[2,3]]) do
     p:= {P[]}:
     if p intersect T = {} then
          T:= T union p;
          night:= night+1:
          printf("\nNight %d:", night);
          for s in p do
               printf(" %a vs. %a ", s[])
          end do
      end if
end do:

Night 1: A vs. B  C vs. D  E vs. F
Night 2: A vs. C  B vs. E  D vs. F
Night 3: A vs. D  B vs. F  C vs. E
Night 4: A vs. F  B vs. C  D vs. E
Night 5: A vs. E  B vs. D  C vs. F

Solve symbolically first. Use unapply to turn the symbolic solution into a procedure with paramter a. Then map that procedure over Vector a. Doing it this way, I accomplished the entire job in 0.2 seconds.

restart:
n:= 10^4:
eq1:={b = a^2, c = b^3/2, d = c^(1/2)*4 + b^2}: #No quote marks on a!
st:= time():
Sol:= unapply(solve(eq1, {b,c,d}), a);

a:= Vector(n, i-> i):
ans:= map(Sol, a):
time()-st;
                            
0.188

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