mmcdara

7891 Reputation

22 Badges

9 years, 54 days

MaplePrimes Activity


These are replies submitted by mmcdara

@KIRAN SAJJAN 

Before going further, read my first answer and correct your ic/bc for H(Theta) accordingly: as the corresponding ode is 

diff(H(Theta), Theta)-.5*h(Theta)+.5*H(Theta)

you need ONLY ONE condition, not two!

Is that clear?

Second point now: three points boundary problem is pure nonsense from a mathematical point of view.
If you want to find a function f(Theta) whose boundary conditions are  f(-Pi/4)=0 and f(Pi/4)=0 and which veryfies f(0)=1, f(0)=1 CANNOT BE A 3rd BOUNDARY CONDITION.
f(0)=1 is a constraint on f.
To find f(Theta) add for instance the boundary condition D(f)(-Pi/4)=something and solve an optimization problem such that for some value of something you get f(0)=1.

The procedire is described inthe attached file (Case M=Mvals[1] only, I suppose you will be capable to do the rest).

restart; with(plots)

_local(gamma):

_local(Re):

R := 1:

Pr := .71:````beta := 1:

OdeSys := diff(f(Theta), Theta, Theta, Theta)-R*(diff(f(Theta), Theta, Theta))+2*Re*f(Theta)*(diff(f(Theta), Theta))+(-M^2+4)*(diff(f(Theta), Theta))+L*beta*(diff(g(Theta), Theta)-(diff(f(Theta), Theta)))-Gr*(diff(h(Theta), Theta))/Re, diff(h(Theta), Theta, Theta)-R*Pr*(diff(h(Theta), Theta))+Q*Pr*h(Theta)+L*gamma*Pr*betaT*(H(Theta)-h(Theta)), diff(g(Theta), Theta, Theta)-2*Re*g(Theta)*(diff(g(Theta), Theta))/R-beta*(diff(f(Theta), Theta)-(diff(g(Theta), Theta)))/R+Gr*(diff(H(Theta), Theta))/(R*Re), diff(H(Theta), Theta)-betaT*(h(Theta)-H(Theta))/R:

Cond := f(-(1/4)*Pi) = 0, g(-(1/4)*Pi) = 0, h(-(1/4)*Pi) = 1, H(-(1/4)*Pi) = 1, f(0) = 1, f((1/4)*Pi) = 0, g((1/4)*Pi) = 0, h((1/4)*Pi) = 1, H((1/4)*Pi) = 1:

print~(eval([OdeSys, Cond], M = MVals[1])):

diff(diff(diff(f(Theta), Theta), Theta), Theta)-(diff(diff(f(Theta), Theta), Theta))+1.0*f(Theta)*(diff(f(Theta), Theta))+(-MVals[1]^2+4)*(diff(f(Theta), Theta))+diff(g(Theta), Theta)-(diff(f(Theta), Theta))-10.00000000*(diff(h(Theta), Theta))

 

diff(diff(h(Theta), Theta), Theta)-.71*(diff(h(Theta), Theta))+.1775*h(Theta)+.1775*H(Theta)

 

diff(diff(g(Theta), Theta), Theta)-1.0*g(Theta)*(diff(g(Theta), Theta))-(diff(f(Theta), Theta))+diff(g(Theta), Theta)+10.00000000*(diff(H(Theta), Theta))

 

diff(H(Theta), Theta)-.5*h(Theta)+.5*H(Theta)

 

f(-(1/4)*Pi) = 0

 

g(-(1/4)*Pi) = 0

 

h(-(1/4)*Pi) = 1

 

H(-(1/4)*Pi) = 1

 

f(0) = 1

 

f((1/4)*Pi) = 0

 

g((1/4)*Pi) = 0

 

h((1/4)*Pi) = 1

 

H((1/4)*Pi) = 1

(1)

MVals := [.5, 1.5, 2.5, 3];

[.5, 1.5, 2.5, 3]

(2)

# Case M = MVals[1] alone: it's up to you to do the rest


# I removed this stupid condition H(Pi/4)=1 for the highesy derivation order for H is 1.

MyCond := remove(has, [Cond[1..-2]], f(0));

[f(-(1/4)*Pi) = 0, g(-(1/4)*Pi) = 0, h(-(1/4)*Pi) = 1, H(-(1/4)*Pi) = 1, f((1/4)*Pi) = 0, g((1/4)*Pi) = 0, h((1/4)*Pi) = 1]

(3)

# Solve formally the h-H subsystem

hH_no_icbc := dsolve(eval([OdeSys[[2, 4]]], M = MVals[1]), [h(Theta), H(Theta)]):

hH_icbc := evalf({ eval(hH_no_icbc, Theta=-Pi/4)[], h(Pi/4) = eval(eval(h(Theta), hH_no_icbc), Theta=Pi/4) });

hH_Cond := select(has, MyCond, {h, H});

eval(hH_icbc, hH_Cond);
__C := solve(%);

hH := evalf(eval(hH_no_icbc, __C));

{H(-(1/4)*Pi) = -.2096814913*_C2+.6986587824*_C3+1.593620104*_C1, h(-(1/4)*Pi) = -.2974973141*_C1+.1406054663*_C2+1.415601411*_C3, h((1/4)*Pi) = -.1171422196*_C1+1.685539735*_C2+2.075281454*_C3}

 

[h(-(1/4)*Pi) = 1, H(-(1/4)*Pi) = 1, h((1/4)*Pi) = 1]

 

{1 = -.2974973141*_C1+.1406054663*_C2+1.415601411*_C3, 1 = -.1171422196*_C1+1.685539735*_C2+2.075281454*_C3, 1 = -.2096814913*_C2+.6986587824*_C3+1.593620104*_C1}

 

{_C1 = .2324476830, _C2 = -.3651152746, _C3 = .7915291381}

 

{H(Theta) = -.3651152746*exp(.4016700491*Theta)*sin(.3712345142*Theta)+.7915291381*cos(.3712345142*Theta)*exp(.4016700491*Theta)+.2324476830*exp(-.5933400983*Theta), h(Theta) = -0.4339337913e-1*exp(-.5933400983*Theta)-1.246112886*exp(.4016700491*Theta)*sin(.3712345142*Theta)+1.156309451*cos(.3712345142*Theta)*exp(.4016700491*Theta)}

(4)

# Plug solution hH into the two remaining odes

fg_plugged := eval([OdeSys[[1, 3]]], hH)[];

diff(diff(diff(f(Theta), Theta), Theta), Theta)-(diff(diff(f(Theta), Theta), Theta))+1.0*f(Theta)*(diff(f(Theta), Theta))+(-M^2+4)*(diff(f(Theta), Theta))+diff(g(Theta), Theta)-(diff(f(Theta), Theta))-.2574703184*exp(-.5933400983*Theta)+9.297882014*exp(.4016700491*Theta)*sin(.3712345142*Theta)-0.1854762100e-1*cos(.3712345142*Theta)*exp(.4016700491*Theta), diff(diff(g(Theta), Theta), Theta)-1.0*g(Theta)*(diff(g(Theta), Theta))-(diff(f(Theta), Theta))+diff(g(Theta), Theta)-4.404988054*exp(.4016700491*Theta)*sin(.3712345142*Theta)+1.823901562*cos(.3712345142*Theta)*exp(.4016700491*Theta)-1.379205311*exp(-.5933400983*Theta)

(5)

# Add a fictitious condition on f

fg_Cond := select(has, MyCond, {f, g})[], D(f)(-Pi/4) = guess

f(-(1/4)*Pi) = 0, g(-(1/4)*Pi) = 0, f((1/4)*Pi) = 0, g((1/4)*Pi) = 0, (D(f))(-(1/4)*Pi) = guess

(6)

# Define the target

target := f(0) = 1:
 

# Define a solution procedure

fg_sol := proc(__guess)
  local s, e:
  s := dsolve(
         eval([fg_plugged, fg_Cond], {M = MVals[1], guess = __guess})
         , numeric
         , output = listprocedure
       ):
  e := abs(eval(f(Theta), s)(0) - rhs(target))
end proc:

# Example

fg_sol(1)

HFloat(0.4423483629961802)

(7)

# Find guess such that f(0)=1

opt := Optimization:-NLPSolve(fg_sol, -3..3)

opt := [3.11647152528849*10^(-10), Vector(1, {(1) = 1.6323713951917918}, datatype = float[8])]

(8)

# Check

fg_sol(opt[2][1]) + rhs(target);

fg_sol_3pts := dsolve(
                 eval([fg_plugged, fg_Cond], {M = MVals[1], guess = opt[2][1]})
                 , numeric
                 , output = listprocedure
               ):

display(
  odeplot(fg_sol_3pts, [Theta, f(Theta)], Theta=-Pi/4..Pi/4, color=red),
  plot(rhs(target), Theta=-Pi/4..Pi/4, color=blue),
  gridlines=true
);

HFloat(1.0000000003116472)

 

 

NULL

Download Ode_New_TWO_phase-2_mmcdara.mw

@KIRAN SAJJAN 

The error you get can be avoided ithis way (illustration for Ans[1] only)

Ans[1](-Pi/4)
Error, (in unknown) solution is only defined in the range HFloat(-0.785398163397447)..HFloat(0.0)

Ans[1](evalf(-Pi/4))
Error, (in unknown) solution is only defined in the range HFloat(-0.785398163397447)..HFloat(0.0)

LeftBound := evalf(-Pi/4) + 1e-8;
Ans[1](LeftBound)
                         -0.7853981535
[                                      
[Theta(-0.7853981535) = -0.7853981535, 
[                                      

  H(Theta)(-0.7853981535) = HFloat(0.9999999999999999), 

  f(Theta)(-0.7853981535) = HFloat(1.2939666992754303e-16), 

  /   d            \(-0.7853981535)                              
  |------- f(Theta)|                = HFloat(2.6146695267427543e\
  \ dTheta         /                                             

       /   d    /   d            \\                              
  -8), |------- |------- f(Theta)||(-0.7853981535) = HFloat(2.64\
       \ dTheta \ dTheta         //                              

  1761642312108), 

  g(Theta)(-0.7853981535) = HFloat(-3.57443270369908e-9), 

  /   d            \                                               
  |------- g(Theta)|(-0.7853981535) = HFloat(-0.3611469438350275), 
  \ dTheta         /                                               

  h(Theta)(-0.7853981535) = HFloat(1.0000000012643802), 

  /   d            \                                             
  |------- h(Theta)|(-0.7853981535) = HFloat(0.12774816275307546)
  \ dTheta         /                                             

  ]
  ]
  ]


The fact plot accepts the lft boundary -Pi/4 is likely due to the way the plotting algorithm determines the points, which seems to be accredited by the following commands

display(
   odeplot(Ans[1],[Theta,f(Theta)], Theta=-Pi/4..-Pi/4+1e-3, numpoints=1000, color=cols[1])
   , 'axes'= 'boxed'
   , labels=[Theta,'f']
  );
Warning, left boundary of range exceeds domain of the problem, it has been adjusted

# No warning here
display(
   odeplot(Ans[1],[Theta,f(Theta)], Theta=LeftBound..-Pi/4+1e-3, numpoints=1000, color=cols[1])
   , 'axes'= 'boxed'
   , labels=[Theta,'f']
  );

@dharr 

"Your C and C' can just be combined into a single combined C that I deal with - the counts for C={1}, C'={2,3,4} are the same as for C={1,2}, C'={3,4}, so I just count {1,2,3,4}."
Good catch!

The rest is just as good: briliant.

By the way my poor algorithm required 45s to get the result with L=5 and N=10^5 (cpu time = 2.7s for yours on my machine, which should be significantly reduced using @acer's ideas)

@FDS 

Why not converting in seconds or days?
It seems quite natural to me for Maple to wait for your choice before making the corresponding conversion, doesn't it?

Suppose you were handling yards, feet and inches: would you have asked why Maple doesn't automatically convert to inches, or feet?

@acer 

I'm going to do all these tests and see what is best for me.
Thanks again.

@acer 

Your first solution based on evalhf and @dharr's idea is indeed a great improvement of what I did.

Concerning the use of Compile the result seems biased because the generation of the compile code is very lengthy:


t0 := time[real]():
cpattern:=Compiler:-Compile(pattern, 'inmem'=false):
time[real]()-t0;

t0 := time[real]():
cpattern(N,nC,M8,C4,P4):
time[real]()-t0;
                             5.912
                             0.009

Nevertheless, as I said in my initial question and detailed in my last reply to @dharr, I need do to this kind of "counting" for a large number of values of C and P. So the compilation time should indeed become marginal at last.

@dharr 

Good idea! 

I thought about using some "embedded property" for instance:

  • Let C and C' two sets of columns such that C is strictly included in C'.
     
  • Let P a list of length |C| of  zeros and ones and P' a "completion" of P obtained by adding "in place" zeros or ones, for instance
    C  = [1, 3]
    P  = [0, 1]
    C' = [1, 2, 3, 5]
    P' = [0, 1, 1, 1]
  • Let  M(P) is the submatrix of M whose columns C match the pattern P, then the submatrix M(P') of M whose columns C' match the pattern P', is a submatrix of M(P).
    With L=5 and M built as I said in my initial question, M(P) has about N/4 rows and thus the search of the columns C' which match the pattern P', ia about 4 times faster when done with M(P).


In some sense it is the Reverse_Idea.mw of yours


By the way, in case you would wonder where this problem comes from, here is little explanation.
The true problemis that one; I have  L detectors, each sending a signal ("1") when some event occurs (each detector is assigned to record a particular event).  
For any event Ei=1..L its probability of occurrence pi is constant over the whole record time.
Then the record history of detector Di can be modelled by a (stationary) Bernoulli random process with parameter pi.
One question is: are the events Ei=1..L mutually independent or not?

Generally one uses a restricted notion of independence named pairwise independence.
Let X, Y, Z three random variables: saying that they are pairwise independent means that X and Y are independent, X and Z are independent and Y and Z are independent.

Mutual independence is a stronger constraint: it states that any subset C of the proper powerset Q of {X, Y, Z} is independent of any other subset C' of P if C and C' are disjoint.
Obviously mutual independece implies pairwise independence while the reciprocal is generally false.

The independence of C and C' means that Prob(C = P | C' = P') = Prob(C = P) where P and P' are any of all the possible outcomes of C and C' respectively.
As I deal with Bernoulli random variables, such outcomes are sequences of 0 and 1. For instance, if C refers to detectors 2 and 3, P is one of {[0, 0], [0, 1], [1, 0],[1, 1]}. The same way, if C' refers to, lets say K detectors (detectors 2 and 3 excluded), there exists 2K possible outcomes for P'.
Checking for the independence of C and C' then means assessing if the 2K+2 conditions Prob(C = P | C' = P') = Prob(C = P) can be reasonnably accepted.

The number of conditions to check in order to assess the mutual independence is equal to 232 for L=4 detectors, and to 1320 for L=5 detectors. This explains why one usually check pairwise independence (L*(L-1)/2 conditions to check).

 

@C_R 

Thanks for your reply.

@C_R 

I had started to write a reply before you published yours, but I posted it after yours.
Sorry for the inconvenience and feel free to delete mine.

@dharr 

It's a good way to react. I should follow your advice.


do you systematically refuse to upload your worksheet while using the big green arrow?

@JAMET 

No, I still don't understand but you don't help me while not giving substantial informations.
Has this something to do with what is persented here ? 

I still wonder what your last reply has to do with your original question

Nevertheless there is a piece of code which does three things:

  1. Build a Pythagorean Triple (PT) by applying a random composition of the three matrices given in the above reference to the primitive PT < 3, 4, 5>.
     
  2. Build the the ternary tree of depth N of all PTs constructed from  < 3, 4, 5> by applying at most (N-1) of these three same matrices.
     
  3. Find what matrices were applied to get a known pythagorean triple.

PTs.mw

PS: as I use Maple 2015.2 the graphic is quite poor but more recent Maple's version will enable you to do more beautiful things.

@acer 

Thanks

@JAMET 

I still don't understand.: where those 6 triples come from?
What is the triple you are starting from and what is the relations between them?

Finally, what does this question have in common with your first one?

 

what do you want to achieve?
Q1: Do you want to find all Pythagorean Triples (PT) (a, b, c) such that a < A, b < B, c < C where (A, B, C) is a PT?

Q2: Is (A, B, C) a "reduced" PT in the sense that the common divisor of A, B, C is 1?

Here is procedure which finds (within one arbitrary permutation) all the PT (a, b, c) assuming the answer to question Q1 is YES and (A, B, C) is "reduced".

 

restart

PrimitivePythagoreanTriple := proc(r, s) isolve({u=r^2-s^2, v=2*r*s, w=r^2+s^2}) end proc:

SPT := proc(exemple)
  local sorted, check, dc, rPT, M, se, re, rel, rs, SPT, r, a, s:

  # Verifiy is "exemple" is a Pythagorean Triple (PT)

  sorted := sort(exemple);
  check  := add(sorted[1..2]^~2) - sorted[3]^2;

  if check <> 0 then
    error cat(exemple, " is not a pythagorean triple")
  else
    # Reduce PT
  
    dc  := igcd(exemple[]);
    rPT := exemple /~ dc:
    if dc > 1 then
      printf("Attention, %a n'est pas sous forme réduite (diviseur commun = %d)\n", exemple, dc);
      printf("On considere ARBITRAIREMENT le triplet pythagoricien réduit %a\n", rPT);
    end if:
  
    # Rearrange PPT in a convenient order
  
    M := max(rPT):
    se, re := selectremove(type, rPT, odd);
    if M::odd then
      rPT := [min(se), re[1], max(se)]
    else
      rPT := [min(re), se[1], max(re)]
    end if;
  
    # Find "generators"
  
    rel := rPT =~ [r^2-s^2, 2*r*s, r^2+s^2];
    rs  := solve({rel[1]+rel[3], r > 0});
    rs  := rs union {s = solve(eval(rel[2], rs))};
  
    # Generate PT (a, b, c) such that a < rPT[1], b < rPT[2], c < rPT[3]
  
    SPT := NULL:
    for r from 2 to eval(r, rs) do
      if r::even then a := 1 else a := 2 end if:
      for s from a to eval(s, rs) by 2 do
        SPT := SPT, (abs@rhs)~(PrimitivePythagoreanTriple(r, s)):
      end do:
    end do:
  
    return {SPT}
  end if:
end proc:

SPT([275, 252, 373])

{{3, 4, 5}, {5, 12, 13}, {7, 24, 25}, {8, 15, 17}, {9, 40, 41}, {11, 60, 61}, {12, 35, 37}, {13, 84, 85}, {15, 112, 113}, {16, 63, 65}, {17, 144, 145}, {19, 180, 181}, {20, 21, 29}, {20, 99, 101}, {21, 220, 221}, {23, 264, 265}, {24, 143, 145}, {25, 312, 313}, {27, 36, 45}, {27, 364, 365}, {28, 45, 53}, {28, 195, 197}, {29, 420, 421}, {31, 480, 481}, {32, 255, 257}, {33, 56, 65}, {33, 544, 545}, {35, 612, 613}, {36, 77, 85}, {36, 323, 325}, {37, 684, 685}, {39, 80, 89}, {44, 117, 125}, {45, 108, 117}, {48, 55, 73}, {51, 140, 149}, {52, 165, 173}, {57, 176, 185}, {60, 91, 109}, {60, 221, 229}, {63, 216, 225}, {65, 72, 97}, {68, 285, 293}, {69, 260, 269}, {72, 135, 153}, {75, 100, 125}, {75, 308, 317}, {81, 360, 369}, {84, 187, 205}, {85, 132, 157}, {87, 416, 425}, {88, 105, 137}, {93, 476, 485}, {95, 168, 193}, {96, 247, 265}, {99, 540, 549}, {104, 153, 185}, {105, 208, 233}, {105, 608, 617}, {108, 315, 333}, {111, 680, 689}, {115, 252, 277}, {117, 756, 765}, {119, 120, 169}, {120, 209, 241}, {125, 300, 325}, {133, 156, 205}, {135, 352, 377}, {136, 273, 305}, {140, 171, 221}, {145, 408, 433}, {147, 196, 245}, {155, 468, 493}, {160, 231, 281}, {161, 240, 289}, {165, 532, 557}, {175, 288, 337}, {175, 600, 625}, {180, 189, 261}, {180, 299, 349}, {185, 672, 697}, {189, 340, 389}, {195, 748, 773}, {203, 396, 445}, {204, 253, 325}, {205, 828, 853}, {207, 224, 305}, {225, 272, 353}, {243, 324, 405}, {252, 275, 373}}

(1)

SPT([30, 16, 34])

 

Attention, [30, 16, 34] n'est pas sous forme réduite (diviseur commun = 2)
On considere ARBITRAIREMENT le triplet pythagoricien réduit [15, 8, 17]

 

{{3, 4, 5}, {5, 12, 13}, {7, 24, 25}, {8, 15, 17}}

(2)

 


 

Download SPT.mw

First 25 26 27 28 29 30 31 Last Page 27 of 154