mmcdara

4276 Reputation

17 Badges

6 years, 237 days

MaplePrimes Activity


These are Posts that have been published by mmcdara


This post is inspired by a serie of questions from @JAMET.
I wondered if it was possible to prove plane geometry theorems with the geometry package.

Here is an illustration for the Poncelet's theorem for the triangle (French designation), see for instance
https://en.wikipedia.org/wiki/Poncelet%27s_closure_theorem

Are any of you interested in challenging the geometry package to proof other plane geometry theorems?
 

restart:


Poncelet's theorem for the triangle

Let ABC a triangle, c its incircle (center omega, radius r) and C its circumcircle (center Omega, radius R).
Let D the distance between omega and Omega.

then R^2 - D^2 - 2*r*R = 0


Proof

Without loss of generality one sets :

    x(A) = y(A) = 0
   and  y(B) = 0

ABC is a non degerated triangle provided x(B) <> 0 and y(C) <> 0
 

with(geometry):

kernelopts(version);

`Maple 2020.2, X86 64 WINDOWS, Nov 11 2020, Build ID 1502365`

(1)

assume(x__B <> 0):
assume(y__C <> 0):

point(A, 0, 0);
point(B, x__B, 0);
point(C, x__C, y__C);

A

 

B

 

C

(2)

triangle(T, [A, B, C])

T

(3)

bisector(bA, A, T);
bisector(bB, B, T);

eA := isolate(Equation(bA, [x, y]), y):
eB := isolate(Equation(bB, [x, y]), y):

xc := solve(rhs(eA)=rhs(eB), x):
yc := eval(rhs(eA), x=xc):

point(omega, xc, yc):
r := distance(omega, line(lAB, [A, B]))

bA

 

bB

 

(abs(x__B)^2)^(1/2)*abs(x__B*y__C/((x__B^2-2*x__B*x__C+x__C^2+y__C^2)^(1/2)+(x__C^2+y__C^2)^(1/2)+(x__B^2)^(1/2)))/(x__B^2)^(1/2)

(4)

circumcircle(C, T, 'centername' = Omega);
R := radius(C);

C

 

((1/4)*x__B^2+(1/4)*(-x__B*(x__C^2+y__C^2)+x__C*x__B^2)^2/(x__B^2*y__C^2))^(1/2)

(5)

Oo := distance(Omega, omega)

(((1/2)*x__B-(x__B*(x__C^2+y__C^2)^(1/2)+x__C*(x__B^2)^(1/2))/((x__B^2-2*x__B*x__C+x__C^2+y__C^2)^(1/2)+(x__C^2+y__C^2)^(1/2)+(x__B^2)^(1/2)))^2+(-(1/2)*(-x__B*(x__C^2+y__C^2)+x__C*x__B^2)/(x__B*y__C)-y__C*(x__B^2)^(1/2)/((x__B^2-2*x__B*x__C+x__C^2+y__C^2)^(1/2)+(x__C^2+y__C^2)^(1/2)+(x__B^2)^(1/2)))^2)^(1/2)

(6)

S := simplify(R^2 - Oo^2 - 2*r*R)  assuming x__B::real, x__C::real, y__C::real

((x__B^2*(-abs(y__C)*signum(y__C)+y__C)*(x__C^2+y__C^2)^(1/2)+x__C^2*(y__C*abs(x__B)-signum(y__C)*abs(x__B*y__C)))*(x__B^2-2*x__B*x__C+x__C^2+y__C^2)^(1/2)+(-signum(y__C)*(x__C-x__B)^2*abs(x__B*y__C)+(abs(x__B)^2+x__C*(x__C-2*x__B))*abs(x__B)*y__C)*(x__C^2+y__C^2)^(1/2))/(y__C*((x__B^2-2*x__B*x__C+x__C^2+y__C^2)^(1/2)+(x__C^2+y__C^2)^(1/2)+abs(x__B))^2)

(7)

simplify(S) assuming x__B > 0, y__C > 0;
simplify(S) assuming x__B > 0, y__C < 0;
simplify(S) assuming x__B < 0, y__C > 0;
simplify(S) assuming x__B < 0, y__C < 0;

0

 

0

 

0

 

0

(8)

 

 

Download PoTh_proof.mw


Improvements of the geometry package                                                                                           

It already appears that (some) assumptions are not (always) correctly taken into account. This is a weak point which requires, as in the attached mw, to use an indirect approache to construct the incircle.

As a matter of fact, the procedure incircle, whose first lines are

showstat(incircle)

geometry:-incircle := proc(inci, T)
local cname, d, A, B, l1, l2, dis, x, y, tmp, msg;
   1   if nargs < 2 or 3 < nargs then
   2     error "wrong number of arguments"
       end if;
   3   if geometry:-form(T) <> ('triangle2d') then
   4     error "wrong type of arguments"
       end if;
   5   if nargs = 3 and op(1,args[3]) = ('centername') and type(op(2,args[3]),'name') then
   6     cname := op(2,args[3])
       else
   7     cname := cat('center_',inci)
       end if;
   8   if geometry:-method(T) = (':-points') then
   9     d := geometry:-DefinedAs(T);
  10     A := op(1,d);
  11     B := op(2,d);
  12     msg := sprintf("find the bisector of %a at vertex %a",T,A);
  13     userinfo(2,geometry,msg);
  14     geometry:-bisector('l1',A,T);
  15     msg := sprintf("find the bisector of %a at vertex %a",T,B);
  16     userinfo(2,geometry,msg);
  17     geometry:-bisector('l2',B,T);
  18     msg := sprintf("find the intersection of the two bisectors");
  19     userinfo(2,geometry,msg);
  20     geometry:-intersection(cname,l1,l2);

requires that the two bissectors are not parallel(call to geometry:-intersection).

Since the non-parallelism of bisectors is trivial for all non-degenerate triangles, why doesn't incircle inherit this property rather than not being able to decide if the bisectors are parallel or not?)

Here is a detail of what happens and the endless loop in which incircle seems to be caught

kernelopts(version);
                  Maple 2015.2, APPLE UNIVERSAL OSX, Dec 20 2015, Build ID 1097895

AreParallel(bA, bB, 'condition'):
        AreParallel: hint: cannot determine if -y__C*(x__B^2)^(1/2)*(x__C*(x__B^2)^(1/2)-x__B*(x__B^2)^(1/2)-x__B*((x__B-x__C)^2+y__C^2)^(1/2))+y__C*(x__B^2)^(1/2)*(x__B*(x__C^2+y__C^2)^(1/2)+x__C*(x__B^2)^(1/2)) is zero

assume(lhs(condition) <> 0);
AreParallel(bA, bB);
                             false
intersection(J, bA, bB);
                               J


infolevel[geometry] := 4:
incircle(inc, T);
incircle: find the bisector of T at vertex A
incircle: find the bisector of T at vertex B
incircle: find the intersection of the two bisectors
intersection: find the intersection between two lines l1 and l2
intersection: one point of intersection
incircle: find the radius of the incircle
line: define the line from two points
circle: define the circle from its center and radius
circle: hint: abs(x__B^2*y__C/(csgn(x__B)*x__B+(x__B^2-2*x__B*x__C+x__C^2+y__C^2)^(1/2)+(x__C^2+y__C^2)^(1/2)))/(x__B^2)^(1/2) > 0
Error, (in geometry:-circle) not enough information: the radius might not be positive
assume(abs(x__B^2*y__C/(csgn(x__B)*x__B+(x__B^2-2*x__B*x__C+x__C^2+y__C^2)^(1/2)+(x__C^2+y__C^2)^(1/2)))/(x__B^2)^(1/2) > 0):

assume(abs(x__B^2*y__C/(csgn(x__B)*x__B+(x__B^2-2*x__B*x__C+x__C^2+y__C^2)^(1/2)+(x__C^2+y__C^2)^(1/2)))/(x__B^2)^(1/2) > 0): 
incircle(inc, T);
incircle: find the bisector of T at vertex A
incircle: find the bisector of T at vertex B
incircle: find the intersection of the two bisectors
intersection: find the intersection between two lines l1 and l2
AreParallel: hint: cannot determine if -y__C*(x__B^2)^(1/2)*(x__C*(x__B^2)^(1/2)-x__B*(x__B^2)^(1/2)-x__B*((x__B-x__C)^2+y__C^2)^(1/2))+y__C*(x__B^2)^(1/2)*(x__B*(x__C^2+y__C^2)^(1/2)+x__C*(x__B^2)^(1/2)) is zero
intersection: two given lines intersect each other if -y__C*(x__B^2)^(1/2)*(x__C*(x__B^2)^(1/2)-x__B*(x__B^2)^(1/2)-x__B*((x__B-x__C)^2+y__C^2)^(1/2))+y__C*(x__B^2)^(1/2)*(x__B*(x__C^2+y__C^2)^(1/2)+x__C*(x__B^2)^(1/2)) <> 0
Error, (in geometry:-intersection) not enough information

assume(-y__C*(x__B^2)^(1/2)*(x__C*(x__B^2)^(1/2)-x__B*(x__B^2)^(1/2)-x__B*((x__B-x__C)^2+y__C^2)^(1/2))+y__C*(x__B^2)^(1/2)*(x__B*(x__C^2+y__C^2)^(1/2)+x__C*(x__B^2)^(1/2)) <> 0):
incircle(inc, T);
incircle: find the bisector of T at vertex A
incircle: find the bisector of T at vertex B
incircle: find the intersection of the two bisectors
intersection: find the intersection between two lines l1 and l2
intersection: one point of intersection
incircle: find the radius of the incircle
line: define the line from two points
circle: define the circle from its center and radius
circle: hint: abs(x__B^2*y__C/(csgn(x__B)*x__B+(x__C^2+y__C^2)^(1/2)+(x__B^2-2*x__B*x__C+x__C^2+y__C^2)^(1/2)))/(x__B^2)^(1/2) > 0
Error, (in geometry:-circle) not enough information: the radius might not be positive


 

 

A simple suggestion...

I would appreciate being able to open multiple help pages simultaneously instead of just one.
This seems to me particularly interesting when you have to browse back and forth between several related items.

In two recent questions

dnaviaux  raised concerns about saving informations he could include in a typeset report.

In the first question these informations were mainly printf terminal outputs, while the second question was oriented to informations of type Table (in the sense of DocumentTools:-Layout:-Table).

Acer has given a direct answer to this last question.
On my side I proposed a less direct one based on a LaTeXapproach.


Since several years I use to generate a LaTeX source code for typeset reports directly from Maple (this is not very difficult once you know LaTeX).
These reports often contain data tables Mbut latex(M)is not the good way to code these data tables into LaTeX.
That is why I turned towards the LaTeX/tabularstructure

\begin{tabular}{...}
...
\end{tabular]


Data tables are very simple structures and converting them programmatically from Maple Matrix into  LaTeX tabular is rather simple: the most important thing you must remember is that some characters have a special role in LaTeX  (for instance "\" , "_" and "$") while having a special meaning in Maple too: so beware of conflicts.

The procedure Tabular in the attached file takes as argument a Matrix and returns the LaTeX code of its tabular counterpart.
Note that it could be easily extendend to accept a DataFrameas input.
This is a very simple simple version and fonts (\rm, \bf, \it... or others), styles (\scriptsize, \large, ...) and much more customization features could be accounted for without any difficulty.

For this particular example inspired by dnaviaux first question, it would be probably better to replace the LaTeX code by one of thes two:

\begin{tabular}{|c @{ $\pm$  } c|}
....
Asdfsdg & +5.190e+01 & 2.5950e+00 \\ 

or :

\begin{tabular}{|c|c|}
....
Asdfsdg & \multicolumn{2}{c|}{$+8.680e-01 \pm 0.9190$} \\ 





dnaviaux second question concerned a DocumentTools:-Layout:-Table object.
For what I understood this table has a simple structure (no rowspan neither columnspan used).

But, knowing that tabular can manage merged columns (\multicolumn) and merged rows (\multirow, provided the ad hoc pasckage is used), I wondered if it would be possible to generate the LaTeX/tabular code corresponding to a Table using rowspan and columnspan?

This is done by the procedure Tabular_Table in the attached file.
Here again this is a very simple procedure which doesn't exploit all the informations a Table structure contains (backgroundstylefillcoloralignalignmentseparator [Tabular_Table separates all columns and rows] ...).

The "raw" rendering is not bad but could be improved:

  • adjust the way \multirowcenters the content of a cell (Xis badly placed in example 3)
  • adjust the spaces between line/clineand the text
    • by using the package cellspace 
    • or my modufying \arraystretch with \renewcommand.
  • one could also add a legend with \caption
  • parameterize Tabular_Table to accept other column separators
  • manage the colors (for instance a blue for a math expression and a black for a text)
  • ...

Finally Tabularand Tabular_Tablecould have an optional argument file:

{file::{symbol, string}:= terminal} 

set by default to terminalor wich could be the name of a  .texfile 


This work is still under developpement and I would be happy to exchange with you on this topic.
Happy New Year to all of you

 

Tabular_from_Maple.mw

Here is the content of the .tex file (I used Welcome-to-CoCalc.texto create/compile it)

\documentclass{article}

% set font encoding for PDFLaTeX, XeLaTeX, or LuaTeX
\usepackage{ifxetex,ifluatex}
\if\ifxetex T\else\ifluatex T\else F\fi\fi T%
  \usepackage{fontspec}
\else
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{lmodern}
\fi

\usepackage{hyperref}

% DO NOT FORGET THIS PACKAGE !!!
\usepackage{multirow}

\title{Tabulars from Maple}
\author{mmcdara}


\begin{document}
\maketitle

\begin{itemize}
\item[\bf{Example 1}]

\begin{tabular}{|c|c|c|}
\hline 
\hline 
Quantity & Nominal value & Uncertainty \\ 
\hline 
Asdfsdg & +9.060e+01 & 4.5298e+00 \\ 
\hline 
Bdfg & +1.437e+01 & 7.1870e-01 \\ 
\hline 
C123 & +8.025e+01 & 4.0125e+00 \\ 
\hline 
Ddf sdfg dsfg  & +9.614e+00 & 4.8072e-01 \\ 
\hline 
\hline 
\end{tabular}

\item[]\vspace*{10mm}

\item[\bf{Example 3}]

\begin{tabular}{| c | c | c | c | c |}
\hline 
\multicolumn{1}{|c|}{\multirow{4}{*}{X}} & \multicolumn{2}{c|}{\multirow{1}{*}{A1}} & \multicolumn{1}{c|}{\multirow{2}{*}{A2}} & \multicolumn{1}{c|}{\multirow{1}{*}{A3}} \\ 
\cline{2-2} 
\cline{3-3} 
\cline{5-5} 
 & \multicolumn{1}{c|}{\multirow{2}{*}{B1}} &\multicolumn{1}{c|}{\multirow{1}{*}{B2}} & & \multicolumn{1}{c|}{\multirow{3}{*}{B3}} \\ 
\cline{3-3} 
\cline{4-4} 
 &  & \multicolumn{2}{c|}{${\cos \left( \omega\,t+\phi \right) }^{\mathstrut}_{\atop{}{\mathstrut}}$} &  \\ 
\cline{2-2} 
\cline{3-3} 
\cline{4-4} 
 & \multicolumn{3}{c|}{${{\frac {1}{\Gamma  \left( x \right) }\sqrt {{{\rm e}^{-{\frac {{t}^{2}}{\pi }}}}}}}^{\mathstrut}_{\atop{}{\mathstrut}}$} &  \\ 
\hline 
\end{tabular} 

\end{itemize}

\end{document}


Image of the resulting PDF:

 

For comparison here is the Tableof Example 3 that Mapledisplays in the worksheet

Recently I came back on the general problem of drawing the syntactic graph of a mathematical expression.
Probably some of you have already done this as students for it is a classic when you learn recursive procedures, chained lists or graphs.

I wasn't interested in doing this with Maple, because Maple had already done  a part of the job thanks to the procedure ToInert.
More of this, the package GraphTheory seemed to possess all the required features to obtain quickly this syntactic graph.
Nevertheless it took me a lot of time to fix (almost all) the problems.
The issues are mainly of two orders:

  1. ToInert is very verbose: a necessary feature when you want to have a non ambiguous syntax of an expression, but partly useless for simple visualization.
    Here is an example
    ToInert(f(x))
    _Inert_FUNCTION(_Inert_NAME("f"), _Inert_EXPSEQ(_Inert_NAME("x")))

     

  2. GraphTheory 
    Once the inert form of the expression is known, it is necessary to put it in a form that can be manipulated by the procedures of the GraphTheory package.
    More precisely one needs to transform this inert form into a set of lists [a, b], where a and b are two neighboring vertices of the syntactic graph and [a, b] the directed arc from a to b.
    As the syntactic graph is a tree, this implies using edges {a, b} instead of arcs [a, b].
    The problem is that some operators are commutative while others are not: for the latter this means that the edges and vertices on the syntactic graph must appear in an order that respects the non-commutativity.
    Here his a toy example where I manually buid the syntactic graphs of a/b and b/a: the two graphs are identical and this comes from the fact that edges in Graph( edges )  must be a set, thus an ordered structure whose order doesn't care about non-commutativity.

    restart:
    with(GraphTheory):
    # The first is aimed to represent the expression a/b
    # while the second is aimed to represent the expression b/a
    Gdiv := Graph({{"/", "a"}, {"/", "b"}}):
    g1 := DrawGraph(Gdiv, style=tree, root="/", title=a/b):
    
    Gdiv := Graph({{"/", "b"}, {"/", "a"}}):
    g2 :=DrawGraph(Gdiv, style=tree, root="/", title=b/a):
    
    plots:-display(<g1 | g2>)
    

    Download ab_ba.mw

     

After several attempts, I decided to discard the GraphTheory package, that is to deprive myself of all the interesting features one needs to manipulate a graph.

The result is given on the attached file (... and the content of the worksheet can't be loaded as usual).

Download Syntactic_Graph.mw

Here is an example


Twelve test cases are given, all the corresponding syntactic graphs are correct, but one of them (test case iexpr=1) seems incorrect because the right child of a parent P is located to the right of the left child of a parent P', even though P' is to the right of P.
This could be corrected by modifying the way the posiitons are computed in procedure Place.


PS : It doesn't seem that Maple has a built-in procedure to construct the syntactic graph of a mathematical expression.
But maybe I'm wrong?


 

 

Hi,
This post is inspired by a recent question maple least square fit error... where the OP was simulating what appeared to be a stochastic process known as the Drunkard's walk (see for instance The_Drunkard's_Walk).


In the case of the PO, the drunk took a step forward or a step backward (say along a narrow, long corridor) with equal probabilities. In addition one assumes that the step the drunkhard takes is independent of all the steps he did before.
His move is what is called a (1D) Random_walk

This little application based on MAPLETS (ok, I know that some people see them as old-fashioned technology).

It draws a sequence of several drunkard's walks, all of identical number of steps, and interactively plots the current histogram of the arrival point (the point where he is at the end of his walk -which should be the door if the same pub he started from if he is an inveterate drunkhard or if he knows a little about statistics- ).

The code contains 2 procedures :

  • f_step_by_step (n, Discrete=false/true) 
    n : number of steps
    Discrete = false (default value) plot the histogram of the arrivals point as if these points were realizarions of a continuous random variable
    In this simple model these arrivals can take only integer values between -n and +n included; the Discrete=true option is recommended but it takes more walks for it to converge to the asymptotic distribution (see below).

    Once launched, f_step_by_step opens a maplet containing a Plotter and 2 buttons. A first walk is displayed, clic the "Plot" button to draw another and repeat the operation as many times as you want.
     
  • f_automatic (n, m, Discrete=false/true) 
    d and Discrete both have the same meaning than for f_step_by_step.
    m is the number of random walk you want to draw
    The code is set to draw 1000 walks of 1000 steps ; this correspong roughly to 250 Mb of memory used.

    f_automatic contains a call to Threads:-Sleep to delay the display, the argument of Sleep is set to 0.25 second and must be modified within the procedure (its value could be passed to f_automatic as an argument).

    In my opinion it is the more interesting of the two procedures.
     

The values of the current mean and standard deviation are displayed as title.

The purpose is before all educative and can be seen as an illustration of the (one of) Central Limit Theorem(s) (CLT)

A little bit of theory:
Let X[n] the position of the drunkhard after n steps; his position X[n+1] is either X[n]-1 or X[n]+1 with equal probabilities.
The displacement X[n+1]-X[n] is a discrete random variable S with outcomes -1 and +1 and it's easy to find its variance is equal to 1.
The position of the trunkard after n steps is just a realization of the n independant and identically distributed, random variables S1, ...Sn whose distribibution is equal to the one of S.
Thus :

  • Expectation (S1 + ... +Sn)  = 0 
  • Variance (S1 + ... +Sn)  = n 
    For n=1000 steps, the standard deviation of the arrivals is about 31.6)

CLT says that the distribution of S1 + ... +Sn  tends to a Gaussian distribution as n tends to infinity.

What is the exact distribution of the arrivals?
Another way to represent S is to write S = 2*B-1 where B is a Bernoulli random variable with parameter 1/2. The random variable "Arrival" is  twice the sum of N indpendent rabdom variables such like S 
and thus its distribution is 2*Binomial(N, 1/2)-N.

What is the rate of convergence of the histogram to the true probability function?
For a sample of size N drawn from a continuous random variables, its histogram has:

  • a bias error of order 1/K (K being the number of bins)
  • a Linfinity error of order  K*sqrt( log(K) / M )  (M number of drunkhard's walks)
    see for instance Lec2_density.pdf

Using the option Discrete=true corresponds to the choice K=2*N+1, in this case the choice with the highest Linfinity error (the larger K the smaller the bias but the larger the  Linfinity error).
This is the reason I introduced the possibility to graw histograms and bar (column) graphs: for the same value of M the Linfinity error of the histogram (for instance with the default number of bins Maple uses) is nuch slower than the one of the comumn graph.



A few "internal" parameters.
I already spoke about the delay to display txo succesive walks.
Other parameters could be:

  • The value of minbins in the case discrete=false (default)
    This value is fixed to 2*sqrt(M).
     
  • The width in the "view" option of the plot: it's left part displays the drunkhard's walk and it's right one the histogram of the arrivals (after a rotation of -Pi/2). 
    This value si fixed to 5/4*M.
    Note that the histogram is dynamically rescaled in order it's height is always 1/4*number_of_steps.
     
  • The height of the view option is set to -Q..Q where Q is equal to 4 standard deviations of the theoritical distribution of the arrivals.
    The continuous envelope of this distribution is red plotted in red (its height is normalized to M/4, see above). 
    One can show this standard deviation iverifies sqrt(M).
    In my opinion using a full vertical scale (-M..M) doesn't give pretty drunkward's walkes because they seem to more concentrated around the value 0.
     



WATCHOUT
Starting from 0 any walk with an odd number of steps will give an odd arrival and any walk with an even number of steps will give an even arrival. Thus the exact number of outcomes for the arrivals are:

  • 2*n if n is odd
  • 2*n-1 if n is even
     


Other application
This maplet can be used as illustration of the Galton Board (also known as the Bean_machine)


Why using maplets?
Another solution could have been to use animate. But to draw the M drunkard's walks, you would have had to use M frames. An excessive task that I'm not even sure Maple would have been able to handle.
I guess that imbeded components could do the job too, but I'm not as comfortable with them as I am with maplets.

To illustrate what the code does an image of the final result is given below.

Drunkard_walk.mw

1 2 3 4 Page 1 of 4