1\documentclass[a4paper,11pt]{book} 2%\textwidth 11,8 cm 3%\textheight 17 cm 4\textheight 23 cm 5\usepackage[dvipdfmx]{graphicx} \usepackage{bmpsize} %\usepackage{graphicx} 6\usepackage{amsmath} 7\usepackage{amsfonts} 8\usepackage{amssymb} 9\usepackage{stmaryrd} 10\usepackage{makeidx} 11\usepackage{times} 12\usepackage{hevea} 13%\usepackage{mathptmx} 14%Uncomment next line for pdflatex and use includegraphics with eps file 15% for latex2html don't use the option [width=\textwidth] 16% check that xfig files are exported magnif 100% 17%\usepackage[francais]{babel} 18\usepackage{ifpdf} 19\ifpdf 20 \usepackage[pdftex,colorlinks]{hyperref} 21\else 22 \usepackage[ps2pdf,breaklinks=true,colorlinks=true,linkcolor=red,citecolor=green]{hyperref} 23 \usepackage{pst-plot} 24\fi 25 26%\def\@evenhead{\thepage\hfill{\footnotesize\textit{\leftmark}}} 27%\def\@oddhead{\footnotesize{\textit{\rightmark}}\hfill\thepage} 28%\usepackage{hp} 29\usepackage[utf8]{inputenc} 30\usepackage[T1]{fontenc} 31%\usepackage[francais]{babel} 32\usepackage{latexsym} 33 34% Inline commands; use \texttt 35% Displayed commands and output, use this 36 37% For graphics 38\newcommand{\includeimage}[1] 39{\includegraphics[width=0.75\textwidth]{#1}} 40 41\newcommand{\R}{{\mathbb{R}}} 42\newcommand{\C}{{\mathbb{C}}} 43\newcommand{\Z}{{\mathbb{Z}}} 44\newcommand{\N}{{\mathbb{N}}} 45 46%\usepackage[pdftex]{hyperref} 47\title {Symbolic algebra and Mathematics with {\tt Xcas}} 48\author{Ren\'ee De Graeve, Bernard Parisse\footnote{Universit\'e de Grenoble, initial translation of parts of the French user manual},\\ 49Jay Belanger\footnote{Full translation and improvements}\\ 50Sections written by Luka Marohnić\footnote{Optimization, signal processing. The graph theory is in a separate manual.}} 51\date{} 52\makeindex 53\usepackage{cutting} 54\begin{document} 55\newcommand{\asinh}{\,\,\mbox{asinh\,}} 56\newcommand{\atanh}{\,\,\mbox{atanh\,}} 57\maketitle 58% {\bf \centerline{Remerciements}} 59 60% \vspace{1cm} 61 62% Je remercie: 63% \begin{itemize} 64% \item Bernard Parisse for ses pr\'ecieux conseils andses remarques sur ce texte, 65 66% \end{itemize} 67 68\vfill 69 70 71\copyright\ 2002, 2007 Ren\'ee De Graeve, Bernard Parisse\\ 72\verb|renee.degraeve@wanadoo.fr|\\ 73\verb|bernard.parisse@ujf-grenoble.fr|\\ 74 75 76 77\newpage 78\tableofcontents 79 80\chapter{Index} 81\printindex 82 83\chapter{Introduction} 84 85\section{Notations used in this manual} 86 87In this manual, the information that you enter will be typeset in 88typewriter font. User input typically takes one of three forms: 89\begin{itemize} 90\item Commands that you enter on the command line.\\ 91For example, to compute the sin of $\pi/4$, you can type 92\begin{center} 93{\tt sin(pi/4)} 94\end{center} 95 96\item Commands requiring a prefix key.\\ 97These will be indicated by separating 98the prefix key and the standard key with a plus \texttt{+}. For 99example, to exit an \texttt{Xcas} session, you can type the control 100key along with the \texttt{q} key, which will be denoted 101\begin{center} 102{\tt Ctrl+Q} 103\end{center} 104 105\item Menu commands.\\ 106When denoting menu items, submenus will be connected using 107$\blacktriangleright$. For example, from within \texttt{Xcas} you 108can choose the \texttt{File} menu, then choose the \texttt{Open} 109submenu, and then choose the \texttt{File} item. This will be 110indicated by 111\begin{center} 112 {\tt File $\blacktriangleright$ Open $\blacktriangleright$ File} 113\end{center} 114\end{itemize} 115 116The index will use different typefaces for different parts of 117the language. The commands themselves will be written with normal 118characters, command options will be written in italics and values of 119commands or options will be written in typewriter font. For example 120(as you will see later), you can draw a blue parabola with the command 121\begin{center} 122 {\tt plotfunc(x\^{}2,color = blue)} 123\end{center} 124In the index, you will see 125\begin{itemize} 126\item 127 plotfunc, the command, written in normal text. 128\item 129 \textit{color}, the command option, written in italics. 130\item 131 \texttt{blue}, the value given to the option, written in typewriter 132 font. 133\end{itemize} 134 135\section{Interfaces for the \texttt{giac} library} 136 137The \texttt{giac} library is a C++ mathematics library. 138It comes with two interfaces for users to use it directly; a graphical 139interface and a command-line interface. 140 141The graphical interface is called \texttt{Xcas}, and is the most 142full-featured interface. As well being able to do symbolic and numeric 143calculations, it has its own programming language, it can draw graphs, 144it has a built-in spreadsheet, it can do dynamic geometry 145and turtle graphics. 146 147The command-line interface can be run inside a terminal. It can also 148do symbolic and numeric calculations and works with the programming 149language. In a graphical environment, the command-line interface can 150also be used to draw graphs. 151 152There is also a web version, which can be run through a browser, 153either over the internet or from local files. Other programs (for 154example, \texttt{TeXmacs}) have interfaces for the command-line 155version. 156 157\subsection{The \texttt{Xcas} interface} 158 159How you run \texttt{Xcas} in a graphical environment depends on which 160operating system you are using. 161\begin{itemize} 162 \item 163If you are using Unix, you can usually find 164an entry for the program in a menu provided by the environment. 165Otherwise, you can start it from a terminal by typing 166\begin{center} 167 {\tt xcas \&} 168\end{center} 169If for some reason \texttt{Xcas} becomes unresponsive, you can open a 170terminal and type 171\begin{center} 172 {\tt killall xcas} 173\end{center} 174That will kill any running \texttt{Xcas} processes. When you restart 175\texttt{Xcas}, you will be asked if you want to resume where you left 176off using an automatic backup file. 177\item 178If you are running Windows, you can use the explorer to go to the 179directory where \texttt{Xcas} is installed. In that directory will be 180a file called \texttt{xcas.bat}. Clicking on that file will start 181\texttt{Xcas}. 182\item 183If you are running Mac OS, you can use the Finder to go to the 184\texttt{xcas\_image.dmg} file and double-click it. Then double-click 185the \texttt{Xcas} disk icon. Finally, to launch \texttt{Xcas}, 186double-click the \texttt{Xcas} program. 187\end{itemize} 188When you start \texttt{Xcas}, a 189window will pop up with menu entries across the top, a bar 190indicating information about the current \texttt{Xcas} configuration, 191and an entry line you can use to enter commands. This interface will 192be described in more detail later, and you can get help 193from within \texttt{Xcas} with the menu item 194\begin{center} 195\texttt{Help$\blacktriangleright$Interface} 196\end{center} 197 198\subsection{The command-line interface} 199 200In Unix and MacOS you can run \texttt{giac} from a terminal with 201the command \texttt{icas} (the command \texttt{giac} also works). 202There are two ways to use the command-line interface. 203 204If you just want to evaluate one expression, you can give 205\texttt{icas} the expression (in quotes) as a command line argument. 206For example, to factor the polynomial $x^2-1$, you can type 207\begin{center} 208 {\tt icas 'factor(x\^{}2-1)'} 209\end{center} 210at a command prompt. The result will be 211\begin{center} 212 {\tt (x-1)*(x+1)} 213\end{center} 214and you will be returned to the operating system command line. 215 216If you want to evaluate several commands, you can enter an interactive 217\texttt{giac} session by entering the command 218\texttt{icas} (or \texttt{giac}) by itself at a command prompt. You 219will then be given a prompt specifically for \texttt{giac} commands, 220which will look like 221\begin{verbatim} 222 0>> 223\end{verbatim} 224You can enter a giac command at this prompt and get the result. 225\begin{verbatim} 226 0>> factor(x^2-1) 227 (x-1)*(x+1) 228 1>> 229\end{verbatim} 230After the result, you will be given another prompt for \texttt{giac} 231commands. You can exit this interactive session by typing 232\texttt{Ctrl+D}. 233 234You can also run \texttt{icas} in batch mode; that is, 235you can have \texttt{icas} run \texttt{giac} commands stored in a 236file. This can be done in Windows as well as Unix and Mac OS. 237To do this, simply enter 238\begin{verbatim} 239 icas filename 240\end{verbatim} 241at a command prompt, where \textit{filename} is the name of the file 242containing the \texttt{giac} commands. 243 244\subsection{The Firefox interface} 245 246You can run \texttt{giac} without installing it by using a 247javascript-enabled web browser. Using Firefox for this is highly 248recommended; Firefox will run \texttt{giac} several times faster than 249Chrome, for example, and Firefox also supports MathML natively. 250 251%This version won't have as much functionality as \texttt{Xcas}, but it 252%doesn't require any software beyond a web browser to be installed. 253 254To run \texttt{giac} through Firefox, you can open the url 255\url{https://www-fourier.ujf-grenoble.fr/~parisse/giac/xcasen.html}. 256At the top of this page is a button which will open a quick tutorial; 257the tutorial will also tell you how to install the necessary files to 258run \texttt{giac} through Firefox without being connected to the 259internet. 260 261\subsection{The TeXmacs interface} 262 263TeXmacs (\url{http://www.texmacs.org}) is a sophisticated word 264processor with special mathematical features. As well as being 265designed to nicely typeset mathematics, it can be used as a frontend 266for various mathematics programs, such as \texttt{giac}. 267 268Once you've started TeXmacs, you can interactively run \texttt{giac} 269within TeXmacs with the menu command 270\texttt{Insert$\blacktriangleright$Session$\blacktriangleright$Giac}. 271Once started, you can enter \texttt{giac} commands as you would in the 272command-line interface. The TeXmacs interface will also have a menu 273specifically for \texttt{giac} commands. 274 275Within TeXmacs, you can combine \texttt{giac} commands and output with 276ordinary text. To enter normal text within a \texttt{giac} session, 277use the menu item \texttt{Focus$\blacktriangleright$Insert Text Field 278Above}. You can reenter a \texttt{giac} entry line by clicking on it 279with a mouse. 280 281\subsection{Checking the version of \texttt{giac} that you are using: 282\texttt{version, giac}} 283\index{version} 284\index{giac} 285 286The \texttt{version} (or \texttt{giac}) command returns the version of \texttt{giac} that 287is running.\\ 288Input: 289\begin{center} 290 \tt 291 version() 292\end{center} 293Output: 294\begin{center} 295 \tt 296 "giac 1.5.0, (c) B. Parisse and R. De Graeve, Institut Fourier, Universite de Grenoble I" 297\end{center} 298 299% \subsection{The Emacs interface} 300 301% You can run a \texttt{giac} session using \texttt{mupacs} 302% (\url{http://mupacs.sourceforge.net}), an Emacs mode for MuPAD, a 303% computer algebra system no longer independently available. Since this 304% mode was created for MuPAD, you 305 306\chapter{The \texttt{Xcas} interface} 307 308\section{The entry levels} 309 310The \texttt{Xcas} interface can run several independent calculation 311sessions, each session will be contained in a separate tab. Before 312you understand the \texttt{Xcas} interface, it would help to be 313familiar with the components of a session. 314 315Each session can have any number of input levels. Each input level 316will have a number to the left of it; the number is used to identify 317the input level. Each level can have one of the following: 318\begin{itemize} 319 \item A command line.\\ 320 This is the default; you can open a new 321 command line with \texttt{Alt+N}.\\ 322 You can enter a \texttt{giac} command (or a series of commands 323 separated by semicolons) on a command line and send it to be 324 evaluated by hitting enter. You can also scroll through the command 325 history with \texttt{Shift+Up} and \texttt{Shift+Down}. 326 327 If the output is a number or an expression, then it will appear in 328 blue text in a small area below the input region; this area is an 329 expression editor. There will be a scrollbar and a small \texttt{M} 330 to the right of this area; the \texttt{M} is a menu which gives you 331 various options. 332 333 If the output is a graphic, then it will appear in a graphing area 334 below the input region. To the right of the graphic will be a 335 control panel allowing you to manipulate the graphic. 336 337 \item An expression editor.\\ 338 You can open an expression editor with 339 \texttt{Alt+E}. 340 341 \item A two-dimensional geometry screen.\\ 342 You can open up such a 343 screen with \texttt{Alt+G}. 344 This level will have a screen, as well as a control panel, menus and a 345 command line to control the screen. 346 347 \item A three-dimensional geometry screen.\\ 348 You can open up such a 349 screen with \texttt{Alt+H}. 350 This level will have a screen, as well as a control panel, menus and 351 a command line to control the screen. 352 353 \item A turtle graphics screen.\\ 354 You can open up such a screen with 355 \texttt{Alt+D}. 356 This level will have a screen, as well as a program editor and 357 command line. 358 359 \item A spreadsheet.\\ 360 You can open up a spreadsheet with \texttt{Alt+T}. 361 A spreadsheet will be able to open a graphic screen. 362 363 \item A program editor.\\ 364 You can open up a program editor with 365 \texttt{Alt+P}. 366 367 \item A comment line. You can open up a comment line with 368 \texttt{Alt+C}. 369\end{itemize} 370Using commands discussed later, different types of levels can be 371combined to form a single hybrid level. Levels can also be moved up 372or down in a session, or even moved to a different session. 373 374The level containing the cursor is the \emph{current level}. The 375current level can be evaluated or re-evaluated by typing enter. 376 377A level can be selected (for later operations) by clicking on the 378number in the white box to the left of the level. Once selected, the 379box containing the number will turn black. You can select a range of 380levels by clicking on the number for the beginning level, and then 381holding the shift key while you click on the number for the ending level. 382 383You can copy the instructions in a range of levels by selecting the 384range, and then clicking the middle mouse button on the number of the 385target level. 386 387\section{The starting window} 388 389When you first start \texttt{Xcas}, you will be given a largely blank window. 390\begin{center} 391 \includeimage{xcas-open.png} 392\end{center} 393The first row will be the main menus; you can 394save and load \texttt{Xcas} sessions, configure \texttt{Xcas} and its 395interface and run various commands with entries from these menus. 396 397The second row will be tabs; one tab for each session that you are 398running in \texttt{Xcas}. The tabs will contain the name of the 399sessions, or \texttt{Unnamed} if a session has no name. The first 400time you start \texttt{Xcas}, there will be only one unnamed session. 401 402The third row will contain various buttons. 403\begin{itemize} 404 \item The first button, \framebox{\tt ?}, will open the help index. 405 (The same as the \texttt{Help$\blacktriangleright$Index} menu entry.) 406 If there is a command on the command line, the help index 407 (see \ahrefloc{helpindex}{help index} 408 \begin{latexonly},p.\pageref{sssec:helpind}\end{latexonly}) 409 will open at this command. 410 411 \item The second button \framebox{\tt Save}, will save the session 412 in a file. The first time you click on it, you will be prompted for 413 a file name ending in \texttt{.xws} to save the session in. The 414 button will be pink if the session is not saved or if it has changed 415 since the last change, it will be green once the session is saved. 416 The name in the title will be the name of the file used to save the 417 session. 418 419\item The third button, which in the picture above is\\ 420 \framebox{\tt Config : exact real RAD 12 xcas 6.2148M}, is a status 421 line indicating the current \texttt{Xcas} configuration. 422 (See section \ref{sec:config}.) 423 If the session is unsaved, it will begin with \texttt{Config :}; if the 424 session is saved in a file \textit{filename.xws}, this button will 425 begin with \texttt{\texttt{Config }\textit{filename.xws}\texttt{ :}}. 426 Other information on this status line: 427 \begin{itemize} 428 \item \texttt{exact} or \texttt{approx}. 429 (See subsection \ref{ssec:approx}.) 430 This tells you whether 431 \texttt{Xcas} will give you exact values, such as 432 $\sqrt{2}$, when possible or to give you decimal approximations. 433 434 \item \texttt{real}, \texttt{cplx} or \texttt{CPLX}. 435 (See subsections \ref{ssec:complex} and \ref{ssec:cvars}.) 436 When this shows \texttt{real}, then (for example) \texttt{Xcas} will by 437 default only find real solutions of equations. When this shows 438 \texttt{cplx}, the \texttt{Xcas} will find complex solutions of 439 equations. When this shows \texttt{CPLX}, then \texttt{Xcas} will 440 regard variables as complex; for example, it won't simplify 441 \texttt{re(z)} (the real part of the variable $z$) to \texttt{z}. 442 443 \item \texttt{RAD} or \texttt{DEG}. 444 (See subsection \ref{ssec:angles}.) 445 This tells you whether 446 angles, as in trigonometric arguments, are measured in radians 447 or degrees. 448 449 \item An integer. (See subsection \ref{ssec:sigdig}, indicating 450 how many significant digits will be used in floating point calculations. 451 452 \item \texttt{xcas}, \texttt{maple}, \texttt{mupad} or \texttt{ti89}. 453 (See subsection \ref{ssec:lang}.) This tells you what syntax 454 \texttt{Xcas} will use. \texttt{Xcas} can be set to emulate the 455 languages of Maple, MuPAD or the TI89 series of calculators. 456 457 \item The last item indicates how much memory \texttt{Xcas} is using. 458 \end{itemize} 459 Clicking on this status line button will open a window where 460 you can configure the settings shown on this line as well as some 461 other settings; you can do the same with the menu item 462 \texttt{Cfg$\blacktriangleright$CAS Configuration}. 463 (See subsection \ref{ssec:confcomp}.) 464 465 \item The fourth button, \framebox{\tt STOP} (in red), can be used 466 to halt a computation which is running on too long. 467 468 \item The fifth button, \framebox{\tt Kbd}, can be used to toggle an 469 on-screen scientific keyboard at the bottom of the window. 470 \begin{center} 471 \includeimage{xcas-scientific-keyboard.png} 472 \end{center} 473 Along the right hand side of the keyboard are some keys that can 474 be used to change the keyboard. 475 \begin{itemize} 476 \item The \texttt{X} key will hide the 477 keyboard, just like pressing the \framebox{\tt Kbd} button again. 478 479 \item The \texttt{cmds} key will toggle a menu bar at the bottom of the 480 screen which can be used as an alternate menu or persistent 481 submenu. This bar will contain buttons, \texttt{home}, 482 \texttt{<{}<}, some menu titles, \texttt{>{}>}, \texttt{var}, 483 \texttt{cust} and \texttt{X}. 484 485 The \texttt{<{}<} and \texttt{>{}>} buttons will scroll through menu 486 items. Clicking on one of the menu buttons will perform the appropriate 487 action or replace the menu items by submenu items. When submenu 488 items appear, there will also be a \texttt{BACK} button to return to 489 the previous menu. Clicking on the \texttt{home} button returns the 490 menu buttons to the main menu. 491 492 After the menu buttons is a \texttt{var} button. This will replace 493 the menu buttons by buttons representing the variables that you have 494 defined. After that is a \texttt{cust} button, which will display 495 commands that you store in a list variable \texttt{CST} (see section 496 \ref{ssec:CST}). 497 498 The last button, \texttt{X}, will close the menu bar. 499 500 \item The \texttt{msg} key will bring up a message window at the bottom 501 of the window which will give you helpful messages; for example, if 502 you save a graphic, it will tell you the name of the file it is 503 saved in and how to include it in a \LaTeX{} file. 504 505 \item The \texttt{abc} key will toggle the keyboard between the 506 scientific keyboard and an alphabetic keyboard. 507 \end{itemize} 508 509 \item The fifth button, \framebox{\tt X}, will close the current 510 session. 511\end{itemize} 512 513\section{Getting help} 514 515\texttt{Xcas} is an extensive program, but you can get help in several 516different ways. The help menu (see section \ref{ssec:helpmenu}) has 517several submenus for various forms of help, some of which are 518mentioned below. 519 520\subsubsection{Tooltips} 521 522If you hover the mouse cursor over certain parts of the \texttt{Xcas} 523window, a temporary window will appear with information about the 524part. For example, if you move the mouse cursor over the status line, 525you will get a message saying \texttt{Current CAS status. Click to 526modify.} 527 528If you type a function name into the \texttt{Xcas} command line, a 529similar temporary window will appear with information about the 530function. 531 532\subsubsection{HTML help} 533 534If you hit the \texttt{F12} button, you will be given a window in 535which you can use to search the html version of the manual. If you 536type a string in the search area, you will be given a list of help 537topics that contain the string. If you choose a topic and click 538\texttt{View}, your web browser will show the appropriate page of 539the manual. 540 541You can also get HTML help with the menu entry 542\texttt{Help$\blacktriangleright$Find word in HTML help}. 543 544\subsubsection{The help index} 545\label{sssec:helpind}\aname{helpindex}{} 546 547 548If you click on the \framebox{?} button on the status line you will 549get the help index. 550 551The help index is a list of the \texttt{giac} function and variable 552names. Along with the list, the help index window has an area listing 553words related to any chosen word and words synonymous to the chosen word. 554 555You can scroll through the help index items and click on the word that 556you want. There is also a line in the help index window that you can 557use to search the index; you can enter some text and be taken to the 558part of the index with words beginning with that text. The \texttt{?} 559button next to this search line will open the HTML help window. 560 561Below the search line, there is an area which will have a description 562of the chosen command, and below that is an area which will have 563examples of the command being used. If the command is a function, 564then between the description and examples will be some boxes in which 565you can enter arguments for the command. Filling in these boxes and 566hitting enter will put the function on the command line. 567 568At the top of the help index window is a \texttt{Details} button. If 569you click on that, a web page will open up in your browser with the 570relevant portion of the manual. If you click on the 571\framebox{\texttt{?}} next to the search line, you will be taken to 572the HTML help window. 573 574Besides clicking on the \framebox{\texttt{?}} on the status line, 575there are other ways to get to the help index. 576\begin{itemize} 577 \item 578 You can get to the help index by using the menu item 579 \texttt{Help$\blacktriangleright$Index}. 580 581 \item 582 You can press the tab button while at the \texttt{Xcas} 583 command line to get to the help index. If you have 584 entered part of a command name, you will be at the part of the index 585 with words beginning with the text that you entered. 586 587 \item 588 If you select a command from the menu, then as well as putting the 589 command on the command line, you will be taken to the help index 590 window with the command chosen. 591\end{itemize} 592 593\subsubsection{\texttt{findhelp}} 594 595You can get help from \texttt{Xcas} by using the 596\texttt{findhelp} function. If you enter 597\texttt{findhelp(}\textit{function}\texttt{)} (or equivalently 598\texttt{?}\textit{function}) at the command input, where 599\textit{function} is the name of a \texttt{giac} function, then some 600notes on \texttt{function} will appear in the answer portion and the 601appropriate page of the manual will appear in your web browser. 602 603\section{The menus} 604 605\subsection{The \texttt{File} menu} 606 607The \texttt{File} menu contains commands that are used to save 608sessions and parts of sessions and load previously saved sessions. 609This menu contains the following entries: 610\begin{itemize} 611 \item 612 \texttt{New Session}\\ 613 This will create and open a new session. 614 This session will be in a new tab labeled \texttt{Unnamed} until you save it 615 (using the menu item \texttt{File$\blacktriangleright$Save} or the 616 keystroke \texttt{Alt+S}). 617 618 \item 619 \texttt{Open}\\ 620 This will open a previously saved session. 621 There will be a submenu with a list of saved session files in the 622 primary directory that you can open, as well as a \texttt{File} item 623 which will open a directory browser you can use to find a session 624 file. This directory browser can also be opened with \texttt{Alt-O}. 625 626 \item 627 \texttt{Import}\\ 628 This will allow you to open a session that 629 was created with the Maple CAS, a TI89 calculator or a Voyage200 630 calculator. These sessions can then be executed with the 631 \texttt{Edit$\blacktriangleright$Execute Session} menu entry, but it 632 may be better to execute the commands one at a time to see if any 633 modifications need to be done. 634 635 \item 636 \texttt{Clone}\\ 637 This will create a copy of the current session in a Firefox 638 interface; either using the server at 639 \url{http://www-fourier.ujf-grenoble.fr/~parisse/xcasen.html} 640 (\texttt{Online}) or a local copy (\texttt{Offline}). 641 642 \item 643 \texttt{Insert}\\ 644 This allows you to insert a previously saved session, a link to a 645 Firefox session, or a previously saved figure, spreadsheet or program. 646 647 \item 648 \texttt{Save} (\texttt{Alt+S})\\ 649 This will save the current session. 650 651 \item 652 \texttt{Save as}\\ 653 This will save the current session under a 654 different name. 655 656 \item 657 \texttt{Save all}\\ 658 This will save all of the sessions. 659 660 \item 661 \texttt{Export as}\\ 662 This will allow you to save the current session in different 663 formats; either standard \texttt{Xcas} format, Maple format, MuPAD 664 format or TI89 format. 665 666 \item 667 \texttt{Kill}\\ 668 This will kill the current session. 669 670 \item 671 \texttt{Print}\\ 672 This will allow you to save the session in various ways. 673 \texttt{preview} will save an image of the current session in a file 674 that you name. \texttt{print} will send an image of the current 675 session to the printer. \texttt{preview selected levels} will save the images of 676 the commands and outputs of the current session, each in a separate 677 file. 678 679 \item 680 \texttt{LaTeX}\\ 681 This will render the session in \LaTeX{} and give you the 682 result in various ways. \texttt{latex preview} will display a 683 compiled \LaTeX{} version of the current session. \texttt{latex 684 print} will send a copy of the \LaTeX{}ed session to a printer. 685 \texttt{latex print selection} will save a copy. 686 687 \item 688 \texttt{Screen capture}\\ 689 This will create a screenshot that will be saved in various formats. 690 691 \item \texttt{Quit and update Xcas}\\ 692 This will quit \texttt{Xcas} after checking for a newer version. 693 694 \item \texttt{Quit} (\texttt{Ctrl+Q})\\ 695 This will quit \texttt{Xcas}. 696\end{itemize} 697 698\subsection{The \texttt{Edit} menu} 699 700The \texttt{Edit} menu contains commands that are used to execute and 701undo parts of the current session. This menu contains the following 702entries: 703\begin{itemize} 704 \item \texttt{Execute worksheet} (\texttt{Ctrl-F9})\\ 705 This will recalculate each level in the session. 706 707 \item \texttt{Execute worksheet with pauses}\\ 708 This will recalculate each level in the session, pausing between 709 calculations. 710 711 \item \texttt{Execute below}\\ 712 This will recalculate the current level and each level below it. 713 714 \item \texttt{Remove answers below}\\ 715 This will remove the answers to the current level and the levels 716 below it. 717 718 \item \texttt{Undo} (\texttt{Ctrl+Z})\\ 719 This will undo the latest edit done to the levels, including the 720 deletion of levels. It can be repeated to undo more than one edit. 721 722 \item \texttt{Redo} (\texttt{Ctrl+Y})\\ 723 This will redo the undone editing. 724 725 \item \texttt{Paste}\\ 726 This will paste the contents of the system clipboard to the cursor 727 position. 728 729 \item \texttt{Del selected levels}\\ 730 This will delete any entry levels that you have selected. 731 732 \item \texttt{selection -> LaTeX} (\texttt{Ctrl+T})\\ 733 If you select a level, part of a level, or answer with the mouse 734 (click and drag), this menu item will put a \LaTeX{} version of the 735 selection on the system clipboard. 736 737 \item \texttt{New entry} (\texttt{Alt+N})\\ 738 This will insert a new entry level above the current one. 739 740 \item \texttt{New parameter} (\texttt{Ctrl+P})\\ 741 This will bring up a window in which you can enter a name and 742 conditions for a new parameter. 743 744 \item \texttt{Insert newline}. 745 This will insert a newline below the cursor. Note that simply 746 typing return will cause the current entry to be evaluated rather 747 than inserting a newline. 748 749 \item \texttt{Merge selected levels}. 750 This will merge the selected levels into a single level. 751\end{itemize} 752 753\subsection{The \texttt{Cfg} menu} 754 755The \texttt{Cfg} menu contains commands that are used to set the 756behaviour of \texttt{Xcas}. This menu contains the following entries: 757\begin{itemize} 758 \item \texttt{Cas configuration}\\ 759 This will open a window that you can use to configure how 760 \texttt{Xcas} performs calculations. This is the same window you 761 get when you click on the status line. 762 763 \item \texttt{Graph configuration}\\ 764 This will open a window that you can use to configure the default 765 settings for a graph. This includes such things as the initial 766 ranges of the variables. Each graph will also have a \texttt{cfg} 767 button to configure the settings on a per graph basis. 768 769 \item \texttt{General configuration}\\ 770 This will open a window that you can use to configure various 771 non-computational aspects of \texttt{Xcas}, such as the fonts, the 772 default paper size, and the like. 773 774 \item \texttt{Mode (syntax)}\\ 775 This will allow you to change the default syntax. To begin with, it 776 is \texttt{Xcas} syntax, but you can change it to Maple syntax, 777 MuPAD syntax or TI89 syntax. 778 779 \item \texttt{Show}\\ 780 This will allow you to control parts of \texttt{Xcas} to show. 781 \begin{itemize} 782 \item \texttt{DispG}\\ 783 This will show the graphics display screen. 784 This screen will show all graphical commands from the session 785 together. 786 787 \item \texttt{keyboard}\\ 788 This will show the on-screen keyboard; the same as clicking on the 789 \texttt{Kbd} button on the status line. 790 791 \item \texttt{bandeau}\\ 792 This will show the menu buttons at the bottom of the window; 793 the same as clicking on \texttt{cmds} on the on-screen keyboard. 794 795 \item \texttt{msg}\\ 796 This will show the messages window; the same as clicking on 797 \texttt{msg} on the on-screen keyboard. 798 \end{itemize} 799 800 \item \texttt{Hide}\\ 801 This will hide the same items that you can show with \texttt{Show}. 802 803 \item \texttt{Index language}\\ 804 This will let you choose a language in which to display the help index. 805 806 \item \texttt{Colors}\\ 807 This will let you choose colors for various parts of the display. 808 809 \item \texttt{Session font}\\ 810 This will let you choose a font for the sessions. 811 812 \item \texttt{All fonts}\\ 813 This will let you choose a font for the session, the main menu and 814 the keyboard. 815 816 \item \texttt{browser}\\ 817 This will let you choose a browser that \texttt{Xcas} will use when 818 needed. If this is blank, then \texttt{Xcas} will use its own 819 internal browser. 820 821 \item \texttt{Save configuration}\\ 822 This will save the configurations that you chose with the 823 \texttt{Cfg} menu or by clicking on the status line. 824\end{itemize} 825 826\subsection{The \texttt{Help} menu} 827\label{ssec:helpmenu} 828 829The \texttt{Help} menu contains commands that let you get information 830about \texttt{Xcas} from various sources. 831This menu contains the following entries: 832\begin{itemize} 833 \item \texttt{Index}\\ 834 This will bring up the help index. 835 (See \ahrefloc{helpindex}{help index} 836 \begin{latexonly}, p.\pageref{sssec:helpind}\end{latexonly}) 837 838 \item \texttt{Find word in HTML help} (\texttt{F12})\\ 839 This will bring up a page which will help you search for keywords in 840 the html documentation that came with \texttt{Xcas}. The help will 841 be displayed in your browser. 842 843 \item \texttt{Interface}\\ 844 This will bring up a tutorial for the \texttt{Xcas} interface. The 845 tutorial will be displayed in your browser. 846 847 \item \texttt{Reference card, fiches}\\ 848 This will bring up (in your browser) a pdf reference card for 849 \texttt{Xcas}. 850 851 \item \texttt{Manuals}\\ 852 This will let you choose from a variety of manuals for \textsc{Xcas}. 853 They will appear in your browser unless otherwise noted. 854 \begin{itemize} 855 \item \texttt{CAS reference}\\ 856 This will bring up a manual for \texttt{Xcas}. 857 858 \item \texttt{Algorithmes (HTML)}\\ 859 This will bring up a manual for the algorithms used by \texttt{Xcas}. 860 861 \item \texttt{Algorithmes (PDF)}\\ 862 This will bring up a pdf version of the manual for the algorithms 863 used by \texttt{Xcas}. 864 865 \item \texttt{Geometry}\\ 866 This will bring up a manual for two-dimensional geometry in 867 \texttt{Xcas}. 868 869 \item \texttt{Programmation}\\ 870 This will bring up a manual for programming in \texttt{Xcas}. 871 872 \item \texttt{Simulation}\\ 873 This will bring up a manual for statistics and using the 874 \texttt{Xcas} spreadsheet. 875 876 \item \texttt{Turtle}\\ 877 This will bring up a manual for using the \texttt{Turtle} drawing 878 screen in \texttt{Xcas}. 879 880 \item \texttt{Exercices}\\ 881 This will bring up a page of exercises that you can do with 882 \texttt{Xcas}. 883 884 \item \texttt{Amusement}\\ 885 This will bring up a page of mathematical amusements that you can 886 work through with \texttt{Xcas}. 887 888 \item \texttt{PARI-GP}\\ 889 This will bring up documentation for the GP/PARI functions. 890 \end{itemize} 891 892 \item \texttt{Internet}\\ 893 The \texttt{Internet} menu contains commands that take you to 894 various web pages related to \texttt{Xcas}. Among them are the 895 following entries: 896 \begin{itemize} 897 \item \texttt{Forum}\\ 898 This will take you to the \texttt{Xcas} forum. 899 900 % \item \texttt{Aide-memoire lycee}. 901 % This will take you to a paper discussing \texttt{Xcas} and high 902 % school. 903 904 % \item \texttt{Documents pedagogiques lycee}. 905 % This will take you to a page on the \texttt{Xcas} website with a 906 % list of useful links. 907 908 % \item \texttt{Documents algorithmique}. 909 % This will take you to a page on the \texttt{Xcas} website with a 910 % list of links. 911 912 % \item \texttt{Site Lycee de G. Connan}. 913 % This will take you to a page about a free book written by 914 % Guillaume Connan teaching algorithms to high school students. 915 916 % \item \texttt{Site Lycee de L. Briel}. 917 % This will take you to a website about \texttt{Xcas} for high 918 % school students. 919 920 % \item \texttt{Calcul formel au lycee, par D. Chevallair}. 921 % This will take you to a pdf file discussing the use of 922 % \texttt{Xcas} in high school. 923 924 % \item \texttt{Site de F. Han}. 925 % This will take you to a website by Frederic Han about 926 % \texttt{Xcas} and a QT frontent for \texttt{giac}. 927 928 % \item \texttt{Ressources Capes} 929 % This will take you to a website with various external sources. 930 931 % \item \texttt{Ressources Agregation externe}. 932 % This will take you to a collection of external resources. 933 934 % \item \texttt{Ressources Agregation interne}. 935 % This will take you to a page on the \texttt{Xcas} website. 936 937 \item \texttt{Update help}\\ 938 This will install updated help files (retrieved from the 939 \texttt{Xcas} website). 940 \end{itemize} 941 942 \item \texttt{Start with CAS}\\ 943 This menu has the following entries. 944 945 \begin{itemize} 946 \item \texttt{Tutorial}\\ 947 This opens up the tutorial. 948 949 \item \texttt{solutions}\\ 950 This opens up the solutions to the exercises in the tutorial. 951 \end{itemize} 952 953 \item \texttt{Tutoriel algo}\\ 954 This opens up a tutorial on algorithms and programming with 955 \texttt{Xcas}. 956 957 \item \texttt{Rebuild help cache}\\ 958 This will rebuild the help index. 959 960 \item \texttt{About}\\ 961 This will display a message window with information about \texttt{Xcas}. 962 963 \item \texttt{Examples}\\ 964 This will allow you to choose from a variety of example worksheets, 965 which will then be copied to your current directory and opened. 966\end{itemize} 967 968\subsection{The \texttt{Toolbox} menu} 969 970The \texttt{Toolbox} menu contains commands that are used to insert 971operators into the session. This menu includes the following entries: 972\begin{itemize} 973 \item \texttt{New entry} (\texttt{Alt+N})\\ 974 This will insert a new level after the current one. 975 976 \item \texttt{New comment} (\texttt{Alt+C})\\ 977 This will insert a new comment level after the current level. 978\end{itemize} 979The other entries allow you to insert mathematical operations into 980the current level. When you do that, you will also be taken to the 981help index 982(See \ahrefloc{helpindex}{help index} 983\begin{latexonly}, p.\pageref{sssec:helpind}\end{latexonly}) 984with help on the chosen command. 985 986\subsection{The \texttt{Expression} menu} 987 988The \texttt{Expression} menu contains commands that are used to 989transform expressions. The first entry is \texttt{New expression} 990(which is equivalent to \texttt{Alt+E}), 991which will insert a new level above the current level and bring up the 992on-screen keyboard. The rest of the entries can be used to insert a 993transformation. 994 995\subsection{The \texttt{Cmds} menu} 996 997The \texttt{Cmds} menu contains various \texttt{giac} functions and 998constants. 999 1000\subsection{The \texttt{Prg} menu} 1001 1002The \texttt{Prg} menu contains commands that are used to write 1003\texttt{giac} programs. The first entry, 1004\texttt{Prg$\blacktriangleright$New program} (equivalent to 1005\texttt{Alt+P}) , will insert a program level and bring up the program 1006editor. The other entries are useful commands for writing 1007\texttt{giac} programs. 1008 1009\subsection{The \texttt{Graphic} menu} 1010 1011The \texttt{Graphic} menu contains commands that are used to create 1012graphs. The first entry, 1013\texttt{Graphic$\blacktriangleright$Attributs} (equivalent 1014to \texttt{Alt+K}) , will bring up a window 1015contains different attributes of the graph (such as line width, color, 1016etc.) The other entries are commands for creating and manipulating 1017graphs. 1018 1019\subsection{The \texttt{Geo} menu} 1020 1021The \texttt{Geo} menu contains commands that are used to work with 1022two- and three-dimensional geometric figures. The first two entries, 1023\texttt{Geo$\blacktriangleright$New figure 2d} (equivalent to 1024\texttt{Alt+G}) and \texttt{Geo$\blacktriangleright$New figure 3d} 1025(equivalent to \texttt{Alt+H}) 1026will create a level for 1027creating two- and three-dimensional figures, respecitively. The other 1028menu items are for working with the figures. 1029 1030\subsection{The \texttt{Spreadsheet} menu} 1031 1032The \texttt{Spreadsheet} menu contains commands that are used to work with 1033spreadsheets. The first menu item, 1034\texttt{Spreadsheet$\blacktriangleright$New spreadsheet} 1035(equivalent to \texttt{Alt+T}), will bring 1036up a window where you can set the size and other attributes of a 1037spreadsheet and then one will be created. The submenus 1038contain commands for working with spreadsheets. Notice that the 1039spreadsheet itself will have menus that are the same as these submenus. 1040 1041\subsection{The \texttt{Phys} menu} 1042 1043The \texttt{Phys} menu contains submenus with various categories of 1044constants, as well as functions for converting units. 1045 1046\subsection{The \texttt{Highschool} menu} 1047 1048The \texttt{Highschool} menu contains computer algebra commands that 1049are useful at different levels of highschool. There is also a 1050\texttt{Program} submenu with some program control functions. 1051 1052\subsection{The \texttt{Turtle} menu} 1053 1054The \texttt{Turtle} menu contains the commands that are used to in a 1055Turtle screen. The first menu item, 1056\texttt{Turtle$\blacktriangleright$New turtle}, will create a Turtle 1057drawing screen, the other menu items contain commands for working with 1058the screen. 1059 1060\section{Configuring \texttt{Xcas}} 1061\label{sec:config} 1062 1063\subsection{The number of significant digits: \texttt{Digits DIGITS}}\index{Digits}\index{DIGITS} 1064\label{ssec:sigdig} 1065 1066By default \texttt{Xcas} uses and displays \texttt{12} significant 1067digits, but you can set the number of digits to other positive 1068integers. If you set the number of significant digits to a number 1069less than \texttt{14}, then \texttt{Xcas} will use the computer's 1070floating point hardware, and so calculations will be done to more 1071significant digits than you asked for, but only the number of digits 1072that you asked for will be displayed. If you set the number of 1073significant digits to \texttt{14} or higher, then both the 1074computations and the display will use that number of digits. 1075 1076You can set the number of significant digits for \texttt{Xcas} by 1077using the CAS configuration screen (see subsection \ref{ssec:confcomp}). 1078The number of significant 1079digits is stored in the variable \texttt{DIGITS} or \texttt{Digits}, 1080so you can also set it by giving the variable \texttt{DIGITS} a new 1081value, as in \texttt{DIGITS:= 20}. The value will be stored in the 1082configuration file (see subsection \ref{ssec:conffile}), and so can also be set there. 1083 1084\subsection{The language mode: \texttt{xcas\_mode}}\index{xcas\_mode} 1085\label{ssec:lang} 1086 1087\texttt{Xcas} has its own language which it uses by default, but you 1088can have it use the language used by \texttt{Maple}, \texttt{MuPAD} or 1089the \texttt{TI89} calculator. 1090 1091You can set which language \texttt{Xcas} uses in the CAS configuration 1092screen (see subsection \ref{ssec:confcomp}). You can also use the 1093function \texttt{xcas\_mode}. If 1094you give it an argument of \texttt{0}, \texttt{xcas\_mode(0)}, then 1095\texttt{Xcas} will use its own language. If you give it an argument 1096of \texttt{1}, \texttt{xcas\_mode(1)}, then \texttt{Xcas} will use the 1097\texttt{Maple} language. If you give it an argument of \texttt{2}, 1098\texttt{xcas\_mode(2)}, then \texttt{Xcas} will use the \texttt{MuPAD} 1099language. Finally, if you give it an argument of \texttt{3}, 1100\texttt{xcas\_mode(3)}, then \texttt{Xcas} will use the \texttt{TI89} 1101language. 1102 1103The language you want to use will be stored in the configuration file 1104(see subsection \ref{ssec:conffile}), and so can also be set there. 1105 1106\subsection{The units for angles: \texttt{angle\_radian}}\index{angle\_radian} 1107\label{ssec:angles} 1108 1109By default, \texttt{Xcas} will assume that any angles you give (for 1110example, as the argument to a trigonometric function) is being 1111measured in radians. If you want, you can have \texttt{Xcas} use 1112degrees. 1113 1114You can set which angle measure \texttt{Xcas} uses in the CAS 1115configuration screen (see subsection \ref{ssec:confcomp}). 1116Your choice will be stored in the variable 1117\texttt{angle\_radian}; this will be \texttt{1} if you measure your 1118angles in radians and \texttt{0} if you measure your angles in 1119degrees. You can also change which angle measure you use by setting 1120the variable \texttt{angle\_radian} to the appropriate value. The 1121angle measure you want to use will be stored in the configuration file 1122(see subsection \ref{ssec:conffile}), and so can also be set there. 1123 1124\subsection{Exact or approximate values: \texttt{approx\_mode}}\index{approx\_mode} 1125\label{ssec:approx} 1126 1127Some number, such as $\pi$ and $\sqrt{2}$, can't be written down 1128exactly as a decimal number. When computing with such numbers, 1129\texttt{Xcas} will leave them in exact, symbolic form. If you want, 1130you can have \texttt{Xcas} automatically give you decimal 1131approximations for these numbers. 1132 1133You can set whether or not \texttt{Xcas} will give you exact or 1134approximate values from the CAS configuration screen. Your choice will 1135be stored in the variable \texttt{approx\_mode}, where a value of 0 1136means that \texttt{Xcas} should give you exact answers when possible 1137and a value of 1 means that \texttt{Xcas} should give you decimal 1138approximations. Your choice will be stored in the configuration file 1139(see subsection \ref{ssec:conffile}), and so can also be set there. 1140 1141\subsection{Complex numbers: \texttt{complex\_mode}} 1142\index{complex\_mode} 1143\index{cfactor} 1144\label{ssec:complex} 1145 1146When factoring polynomials, \texttt{Xcas} won't introduce complex 1147numbers if they aren't already being used. For example, 1148\begin{center} 1149 \texttt{factor(x\^{}2 + 2)} 1150\end{center} 1151will simply return 1152\begin{center} 1153 \texttt{x\^{}2 + 2} 1154\end{center} 1155but if an expression already involves complex numbers then 1156\texttt{Xcas} will use them; 1157\begin{center} 1158 \texttt{factor(i*x\^{}2 + 2*i)} 1159\end{center} 1160will return 1161\begin{center} 1162 \texttt{(x - i*sqrt(2))*(i*x - sqrt(2))} 1163\end{center} 1164\texttt{Xcas} also has ways of finding complex roots even when complex 1165numbers are not present; for example, the command \texttt{cfactor} 1166will factor over the complex numbers 1167\begin{center} 1168 \texttt{cfactor(x\^{}2 + 2)} 1169\end{center} 1170will return 1171\begin{center} 1172 \texttt{(x - i*sqrt(2))*(x + i*sqrt(2))} 1173\end{center} 1174 1175If you want \texttt{Xcas} to use complex numbers by default, you can 1176turn on complex mode. In complex mode, 1177\begin{center} 1178 \texttt{factor(x\^{}2 + 2)} 1179\end{center} 1180will return 1181\begin{center} 1182 \texttt{(x - i*sqrt(2))*(x + i*sqrt(2))} 1183\end{center} 1184 1185You can turn on complex mode from the CAS configuration screen. This 1186mode is determined by the value of \texttt{complex\_mode}; if this is 11871 then complex mode is on, if this variable is 0 then complex mode is 1188off. This option will be stored in the configuration file (see subsection \ref{ssec:conffile}), and 1189so can also be set there. 1190 1191\subsection{Complex variables: \texttt{complex\_variables}}\index{complex\_variables} 1192\label{ssec:cvars} 1193 1194New variables will be assumed to be real; functions which work with 1195the real and imaginary parts of variables will assume that a variable 1196is real. For example, \texttt{re} returns the real part of its 1197argument and \texttt{im} returns the imaginary part, and so 1198\begin{center} 1199 \texttt{re(z)} 1200\end{center} 1201returns 1202\begin{center} 1203 \texttt{z} 1204\end{center} 1205and 1206\begin{center} 1207 \texttt{im(z)} 1208\end{center} 1209returns 1210\begin{center} 1211 \texttt{0} 1212\end{center} 1213 1214If you want variables to be complex by default, you can have 1215\texttt{Xcas} use complex variable mode. 1216You can set this from the CAS configuration screen. Your choice will 1217be stored in the variable \texttt{complex\_variables}, where a value of 0 1218means that \texttt{Xcas} will assume that variables are real and 1219and a value of 1 means that \texttt{Xcas} will assume that values are 1220complex. Your choice will be stored in the configuration file 1221(see subsection \ref{ssec:conffile}), and so can also be set there. 1222 1223\subsection{Configuring the computations} 1224\label{ssec:confcomp} 1225 1226You can configure how \texttt{Xcas} computes by using the menu item 1227\texttt{Cfg$\blacktriangleright$Cas configuration} or by clicking on 1228the status line. You will then be given a window in which you can 1229change the following options: 1230\begin{itemize} 1231 \item \texttt{Prog style} (default: \texttt{Xcas})\\ 1232 You will have a menu from which you can choose a different language 1233 to program in; you can choose from \texttt{Xcas}, \texttt{Xcas 1234 (Python)}, \texttt{Maple}, \texttt{Mupad} and \texttt{TI89/92}. 1235 1236 \item \texttt{eval} (default: 25)\\ 1237 You can type in a positive integer indicating the maximum number of 1238 recursions allowed when evaluating expressions. 1239 1240 \item \texttt{prog} (default: 1)\\ 1241 You can type in a positive integer indicating the maximum number of 1242 recursions allowed when executing programs. 1243 1244 \item \texttt{recurs} (default: 100)\\ 1245 You can type in a positive integer indicating the maximum number of 1246 recursive calls. 1247 1248 \item \texttt{debug} (default: 0)\\ 1249 You can type in an integer, 0 or 1. If this is 1, then 1250 \texttt{Xcas} will display intermediate information on the 1251 algorithms used by \texttt{giac}. If this number is 0, then no such 1252 information is displayed. 1253 1254 \item \texttt{maxiter} (default: 20)\\ 1255 You can type in an integer indicating the maximum number of 1256 iterations in Newton's method. 1257 1258 \item \texttt{Float format} (default: \texttt{standard})\\ 1259 You will have a menu from which you can choose how to display 1260 decimal numbers. Your choices will be: 1261 \begin{itemize} 1262 \item \texttt{standard} In standard notation, a number will be 1263 written out completely without using exponentials; for example, 1264 \texttt{15000.12} will be displayed as \texttt{15000.12}. 1265 \item \texttt{scientific} In scientific notation, a number will be 1266 written as a number between 1 and 10 times a power of ten; for example, 1267 \texttt{15000.12} will be displayed as \texttt{1.500012000000e+04} 1268 (where the number after \texttt{e} indicates the power of 10). 1269 \item \texttt{engineer} In engineer notation, a number will be 1270 written as a number between 1 and 1000 times a power of ten, where 1271 the power of 10 is a multiple of three. For example, 1272 \texttt{15000.12} will be displayed as \texttt{15.00012e3}. 1273 \end{itemize} 1274 1275 \item \texttt{Digits} (default: 12)\\ 1276 You can enter a positive integer which will indicate the number of 1277 significant digits. 1278 1279 \item \texttt{epsilon} (default: 1e-12)\\ 1280 You can enter a floating point number which will be the value of 1281 epsilon used by \texttt{epsilon2zero}, which is a function which 1282 replaces numbers with absolute value less than epsilon by 0. 1283 1284 \item \texttt{proba} (default: 1e-15)\\ 1285 You can enter a floating point number. If this number is greater 1286 than zero, then in some cases \texttt{giac} can use 1287 probabilistic algorithms and give a result with probability of being 1288 false less than this value. (One such example of a probabilistic 1289 algorithm that \texttt{giac} can use is the algorithm to compute the 1290 determinant of a large matrix with integer coefficients.) 1291 1292 \item \texttt{approx} (default: unchecked)\\ 1293 You will be given a checkbox. If the box is checked, then exact 1294 numbers such as $\sqrt{2}$ will be given a floating point approximation. If 1295 the box in unchecked, then exact values will be used when possible. 1296 1297 \item \texttt{autosimplify} (default: 1)\\ 1298 You can enter a simplification level of 0, 1 or 2. A value of 0 1299 means no automatic simplification will be done, a value of 1 means 1300 grouped simplification will be automatic. A value of 2 means that 1301 all simplification will be automatic. 1302 1303 \item \texttt{threads} (default: 1)\\ 1304 You can enter a positive integer to indicate the number of threads 1305 (for a possible future threaded version). 1306 1307 \item \texttt{Integer basis} (default: 10)\\ 1308 You will be given a menu from which you can choose an integer base 1309 to work in; your choices will be 8, 10 and 16. 1310 1311 \item \texttt{radian} (default: \texttt{checked})\\ 1312 You will be given a checkbox. If the box is checked, then angles 1313 will be measured in radians, otherwise they will be measured in 1314 degrees. 1315 1316 \item \texttt{Complex} (default: \texttt{unchecked})\\ 1317 You will be given a checkbox. If this box is checked, then 1318 \texttt{giac} will work in complex mode, meaning, for example, that 1319 polynomials will be factored with complex numbers if necessary. 1320 1321 \item \texttt{Cmplx\_var} (default: \texttt{unchecked})\\ 1322 You will be given a checkbox. If this box is checked, then 1323 variables will by default be assumed to be complex. For example, 1324 the expression \texttt{re(z)} won't be simplified to simply 1325 \texttt{z}. If this box is unchecked, then \texttt{re(z)} will be 1326 simplified to \texttt{z}. 1327 1328 \item \texttt{increasing power} (default: \texttt{unchecked})\\ 1329 You will be given a checkbox. If this box is checked, then 1330 polynomials will be written out in increasing powers of the 1331 variable; otherwise they will be written in decreasing powers. 1332 1333 \item \texttt{All\_trig\_sol} (default: \texttt{unchecked})\\ 1334 You will be given a checkbox. If this box is unchecked, then only 1335 the primary solutions of trigonometric equations will be given. For 1336 example, the solutions of \texttt{cos(x)=0} will be the pair 1337 \texttt{[-pi/2,pi/2]}. If this box is checked, then the solutions of 1338 \texttt{cos(x)=0} will be \texttt{[(2*n\_0*pi + pi)/2]}, where 1339 \texttt{n\_0} can be any integer. 1340 1341 \item \texttt{Sqrt} (default: \texttt{checked})\\ 1342 You will be given a checkbox. If this box is checked, then the 1343 \texttt{factor} command will factor second degree polynomials, even 1344 when the roots are not in the field determined by the coefficients. 1345 For example, \texttt{factor(x\^{}2 - 3)} will return 1346 \texttt{(x - sqrt(3))*(x + sqrt(3))}. If this box is unchecked, then 1347 \texttt{factor(x\^{}2 - 3)} will return \texttt{x\^{}2 - 3}. 1348\end{itemize} 1349This page will also have buttons for applying the settings, saving the 1350settings for future sessions, canceling any new settings, or restoring 1351the default settings. 1352 1353 1354\subsection{Configuring the graphics} 1355\label{ssec:confgraph} 1356 1357You can configure each graphics screen by clicking on the \texttt{cfg} 1358button on the graphics screen's control panel to the right of the 1359graph. You can also change the default graphical configuration using 1360the the menu item 1361\texttt{Cfg$\blacktriangleright$Graph configuration}. 1362You will then be given a window in which you can 1363change the following options: 1364\begin{itemize} 1365 \item \texttt{X-} and \texttt{X+}\\ 1366 These will determine the $x$ values for which calculations will be 1367 done. 1368 1369 \item \texttt{Y-} and \texttt{Y+}\\ 1370 These will determine the $y$ values for which calculations will be done. 1371 1372 \item \texttt{Z-} and \texttt{Z+}\\ 1373 These will determine the $z$ values for which calculations will be done. 1374 1375 \item \texttt{t-} and \texttt{t+}\\ 1376 These will determine the $t$ values for which calculations will be 1377 done, when plotting parametric curves, for example. 1378 1379 \item \texttt{WX-} and \texttt{WX+}\\ 1380 These will determine the range of $x$ values for the viewing window. 1381 done. 1382 1383 \item \texttt{WY-} and \texttt{WY+}\\ 1384 These will determine the range of $y$ values for the viewing window. 1385 1386% \item \texttt{TX} and \texttt{TY}\\ 1387 1388 \item \texttt{class\_min}\\ 1389 This will determine the minimum size of a statistics class. 1390 1391 \item \texttt{class\_size}\\ 1392 This will determine the default size of a statistics class. 1393 1394 \item \texttt{autoscale}\\ 1395 When checked, the the graphic will be autoscaled. 1396 1397 \item \texttt{ortho}\\ 1398 When checked, all axes of the graphic will be scaled equally. 1399 1400 \item \texttt{>W} and \texttt{W>}\\ 1401 These are convenient shortcuts to copy the \texttt{X-}, \texttt{X+}, 1402 \texttt{Y-} and \texttt{Y+} values to \texttt{WX-}, \texttt{WX+}, 1403 \texttt{WY-} and \texttt{WY+}, or the other way around. 1404\end{itemize} 1405This page will also have buttons for applying the settings, saving the 1406settings for future sessions, or canceling any new settings. 1407 1408\subsection{More configuration} 1409\label{sec:mconf} 1410 1411You can configure other aspects of \texttt{Xcas} (besides the 1412computatioonal aspects and graphics) using the the menu item 1413\texttt{Cfg$\blacktriangleright$General configuration}. 1414You will then be given a window in which you can 1415change the following options: 1416\begin{itemize} 1417 \item \texttt{Font}\\ 1418 This lets you choose a session font, the same as choosing the menu 1419 item \texttt{Cfg$\blacktriangleright$Session font}. 1420 1421 \item \texttt{Level}\\ 1422 This will determine what type of level should be open when you start 1423 a new session. 1424 1425 \item \texttt{browser}\\ 1426 This will determine what browser \texttt{Xcas} should use when it 1427 requires one, for example when displaying help. If this is empty, 1428 \texttt{Xcas} will use its built-in browser. 1429 1430 \item \texttt{Auto HTML help}\\ 1431 If this box is checked, then whenever you choose a function from a menu, 1432 a help page for that function will appear in your browser. 1433 Regardless of whether this box is checked or not, the help page will 1434 also appear in your browser if you type \texttt{?}\textit{function} 1435 in a command box. 1436 1437 \item \texttt{Auto index help} 1438 If this box is checked, then whenever you choose a function from a 1439 menu, the help index page for that function will appear. This is 1440 the same page you would get from choosing the function from the help 1441 index. 1442 1443 \item \texttt{Print format}\\ 1444 This will determine the paper size for printing and saving files. 1445 There is also a button you can use to have the printing done in 1446 landscape mode; if this button is not checked, the printing will be 1447 done in portrait. 1448 1449 \item \texttt{Disable Tool tips}\\ 1450 If this is checked, \texttt{Xcas} will stop displaying tool tips. 1451 1452 \item \texttt{rows} and \texttt{columns}\\ 1453 These will determind the default number of rows and columns for the 1454 matrix editor and spreadsheet. 1455 1456 \item \texttt{PS view}\\ 1457 This determines what program will be used to preview Postscript files. 1458 1459% \item \texttt{Step by step}\\ 1460 1461% \item \texttt{Proxy} 1462\end{itemize} 1463 1464\subsection{The configuration file: \texttt{widget\_size cas\_setup xcas\_mode xyztrange}} 1465\index{widget\_size}\index{cas\_setup}\index{xcas\_mode}\index{xyztrange} 1466\label{ssec:conffile} 1467 1468When you save changes to your configuration, this is stored in a 1469configuration file, which will be \texttt{.xcasrc} in your home 1470directory in Unix and \texttt{xcas.rc} in Windows. This file will 1471have four functions -- \texttt{widget\_size}, \texttt{cas\_setup}, 1472\texttt{xcas\_mode} and \texttt{xyztrange} -- which determine the 1473configuration and which are evaluated when \texttt{Xcas} starts. 1474 1475The \texttt{widget\_size} function has between 1 and 12 arguments. 1476The arguments (in order) are: 1477\begin{itemize} 1478 \item The first argument is a postive integer specifying the font 1479 size. Optionally, this can be a bracketed list whose first number 1480 indicates the font and the second the font size. 1481 \item The second and third arguments are horizontal and vertical 1482 distances in pixels from the upper left hand corner of the screen. 1483 They specify where the upper left corner of the \texttt{Xcas} 1484 window is when it opens. 1485 \item The fourth and fifth arguments specify the width and height of the 1486 \texttt{Xcas} window when it opens. 1487 \item The sixth argument is either 0 or 1; a 1 indicates that the 1488 on-screen keyboard should be open when \texttt{Xcas} starts, a 0 1489 indicates that the keyboard should be hidden. 1490 \item The seventh argument is either 0 or 1; a 1 indicates that the 1491 browser should be automatically opened to display help for the 1492 selected command in the menu or index, a 0 indicates that the 1493 browser should not be automatically opened. 1494 \item The eighth argument is either 0 or 1; a 1 indicates that 1495 \texttt{Xcas} should open with the message window, a 0 indicates that 1496 \texttt{Xcas} should open without the message window. 1497 \item The ninth argument is currently not used. 1498 \item The tenth argument is a string with the name of the browser to 1499 use to read the help pages. A value of \texttt{"builtin"} means 1500 that \texttt{Xcas} should use a small browser built into 1501 \texttt{Xcas}. 1502 \item The eleventh argument indicates what level \texttt{Xcas} 1503 should start at; a 0 means command line, a 1 means program editor, a 1504 2 means spreadsheet, and a 3 means a 2-d geometry screen. 1505 \item The twelfth argument is a string with the name of a program 1506 for postscript previews; for example, \texttt{"gv"}. 1507 \end{itemize} 1508 1509The \texttt{cas\_setup} function has nine arguments. 1510The arguments (in order) are: 1511\begin{itemize} 1512 \item \texttt{approx}. A 1 means \texttt{Xcas} works in 1513 approximate mode, a 0 means numeric mode. 1514 \item \texttt{complex\_var}. A 1 means work with complex 1515 variables, a 0 means real variables. 1516 \item \texttt{complex}. A 1 means work with in complex 1517 mode, a 0 means real mode. 1518 \item \texttt{radian}. A 1 means work in radians, a 1519 0 means work in degrees. 1520 \item \texttt{display\_format}. A 0 means use the standard 1521 format to display numbers, a 1 means use scientific format, 1522 a 2 means use engineering format, and a 3 means use floating 1523 hexademical format (which is standardized with a non-zero first 1524 digit). 1525 \item \texttt{epsilon}. This is the value of \texttt{epsilon} used 1526 by \texttt{Xcas}. 1527 \item \texttt{Digits}. This is the number of digits to use to 1528 display a float. 1529 \item \texttt{tasks}. This will be used in the future for 1530 parallelism. 1531 \item \texttt{increasing\_power}. This is 0 to display polynomials 1532 in increasing power, 1 to display polynomials in decreasing powers. 1533\end{itemize} 1534 1535The \texttt{xcas\_mode} function has one argument; a 0 to work in 1536\texttt{Xcas} mode, a 1 to work in \texttt{Maple} mode, a 2 to work in 1537\texttt{MuPAD} mode, and a 3 to work in \texttt{TI89} mode. 1538 1539The \texttt{xyztrange} function inserts or removes the axes of a 1540geometric screen; it has 15 parameters, which are the parameters which 1541can be set with the graphics configuration screen (see section 1542\ref{ssec:confgraph}).\\ 1543Input: 1544\begin{center} 1545 \tt 1546 xyztrange (-5.5, -5.2, -10.10, -1.6, -5.5, -1.2384,2,1,0,1) 1547\end{center} 1548(or enter the information in the configuration screen) will result in 1549a visible graphics window of [-5,5] by [-1.2384,2]. Note that the 1550visible window is not the same as the calculation window; if the 1551calculation window is larger than the visible window, then you can 1552scroll to bring other parts of the calculation window into view. 1553 1554\section{Printing and saving} 1555 1556\subsection{Saving a session} 1557 1558Each tab above the status line represents a session, the active tab 1559will be yellow. The label of each tab will be the name of the file 1560that the session is saved in; if the session hasn't been saved the tab 1561will read \texttt{Unnamed}. 1562 1563You can save your current session by clicking on the \texttt{Save} 1564button on the status line. If the session contains unsaved changes 1565the \texttt{Save} button will be red; the button will be green when 1566nothing needs to be saved. The first time that you save a session you 1567will be prompted for a file name; you should choose a name that ends 1568in \texttt{.xws}. Subsequent times that you save a session it will be 1569saved in the same file; to save a session in a different file you can 1570use the menu item \texttt{File$\blacktriangleright$Save as}. 1571 1572If you have a session saved in a file and you want to load it in a 1573tab, you can use the menu item \texttt{File$\blacktriangleright$Open}. 1574From there you can choose a specific file from a list or open a 1575directory browser that you can use to choose a file. The directory 1576browser can also be opened with \texttt{Alt-O}. 1577 1578\subsection{Saving a spreadsheet} 1579 1580If you have a spreadsheet in one of the levels, you can save it 1581separately from the rest of the session. 1582 1583Once a spreadsheet is inserted, it will have menus right next to the 1584level number. If you select the \texttt{Table$\blacktriangleright$Save 1585sheet as text} menu, you will be prompted for a file name. You should 1586choose a file name that ends in \texttt{.tab}. Once you save the 1587spreadsheet, there will be a button to the right of the menus which 1588you can use to save any changes you make. If you want to save the 1589spreadsheet under a different name, you can use the 1590\texttt{Table$\blacktriangleright$Save as alternate filename} menu entry. 1591You can also use the 1592\texttt{Table$\blacktriangleright$Save as CSV} and 1593\texttt{Table$\blacktriangleright$Save as mathml} menu entries 1594to save the spreadsheet in other formats. 1595 1596You can use the \texttt{Table} menu to insert previously saved 1597spreadsheets; the menu item \texttt{Table$\blacktriangleright$Insert} 1598will bring up a directory browser you can use to select a file to 1599enter. 1600 1601\subsection{Saving a program} 1602 1603You can open up a level in which to write an \texttt{Xcas} program 1604with the menu item \texttt{Prg$\blacktriangleright$New program} (which 1605is equivalent to \texttt{Alt-P}). If you select this item, you will 1606be prompted for information to fill out a template for a program and 1607then be left in the program editor. 1608 1609At the top of the program editor there will be menus and buttons, at 1610the far right will be a \texttt{Save} button that you can press to 1611save the program. The first time you save a program, you will be 1612prompted for a file name, you should choose a name ending in 1613\texttt{.cxx}. Once a program is saved, the file name will appear to 1614the right of the \texttt{Save} button. If you want to save the 1615program under a different name, you can use the 1616\texttt{Prog$\blacktriangleright$Save as} item from the program editor 1617menu. 1618 1619To insert a previously saved program, you can use the 1620\texttt{Prog$\blacktriangleright$Load} item from the program editor menu. 1621 1622\subsection{Printing a session} 1623 1624You can print a session with the 1625\texttt{File$\blacktriangleright$Print$\blacktriangleright$to printer} 1626menu item. 1627 1628If you prefer to save the printed form as a file, you can use the 1629\texttt{File$\blacktriangleright$Print$\blacktriangleright$preview} 1630menu item. You will prompted for a file name to save the printed form 1631in; the file will be a PostScript file, so the name should end in 1632\texttt{.ps}.If you only want to save certain levels in printable 1633form, you can use the 1634\texttt{File$\blacktriangleright$Print$\blacktriangleright$preview 1635selected levels} menu item; this file will be encapsulated PostScript, 1636so the name should end in \texttt{.eps}. 1637 1638\section{Translating to other computer languages} 1639 1640\texttt{Xcas} can translate a session, or parts of a session, to other 1641computer languages; notably \LaTeX{} and MathML. 1642 1643\subsection{Translating an expression to \LaTeX{}: \texttt{latex}}\index{LaTeX}\index{latex} 1644 1645The command \texttt{latex} will translate an expression to a \LaTeX{} 1646expression. If you enter \texttt{latex(\textit{expression})}, then 1647the expression will be evaluated and the result will be given to you 1648in the \LaTeX{} typesetting language. For example, if you enter 1649\begin{center} 1650 {\tt latex(1+1/2)} 1651\end{center} 1652you will get 1653\begin{center} 1654 {\tt $\backslash $frac\{3\}\{2\}} 1655\end{center} 1656 1657\subsection{Translating the entire session to \LaTeX{}} 1658 1659If you want to save your entire document as a complete \LaTeX{} file, 1660you can use the menu item \texttt{File$\blacktriangleright$laTeX 1661preview selection} 1662 1663\subsection{Translating graphical output to \LaTeX{}: \texttt{graph2tex graph3d2tex}}\index{graph2tex}\index{graph3d2tex} 1664 1665You can see all of your graphic output at once on the 1666\texttt{DispG\index{DispG}} screen, which you can bring up with the 1667command \texttt{DispG()}. (This screen can be cleared with the 1668command line command \texttt{erase()}.) On the \texttt{DispG} screen 1669there will be a \texttt{Print} menu; the 1670\texttt{Print$\blacktriangleright$latex print} will give you several 1671in files \texttt{DispG.tex}, \texttt{DispG.ps}, \texttt{DispG.ps} and 1672\texttt{DispG.png} with the graphics in different formats. To save it 1673without using the \texttt{DispG()} command you can use the 1674\texttt{graph2tex} command, which will save all graphic output to a 1675\LaTeX{} file of your choosing. For example, to save your graphs to 1676\texttt{myfile.tex}, you can enter the command 1677\begin{center} 1678 {\tt graph2tex("myfile.tex")} 1679\end{center} 1680to get a \LaTeX{} file \texttt{myfile.tex} with the graphs. To save a 1681three-dimensional graph, you can use the command \texttt{graph3d2tex}. 1682 1683To save a single graph as a \LaTeX{} file, you can use the \texttt{M} 1684menu to the right of the graph. Selecting 1685\texttt{M$\blacktriangleright$Export Print$\blacktriangleright$Print 1686(LaTeX)} will save the current graph. You can also save a single 1687graph by selecting that level, then use the menu item 1688\texttt{File$\blacktriangleright$LaTeX$\blacktriangleright$LaTeX print 1689selection}. This method will save the graph in several formats; 1690\texttt{session0.tex}, \texttt{session0.dvi}, \texttt{session0.ps} 1691and \texttt{session0.png}, or with \texttt{session0} replaced by the 1692session name. 1693 1694\subsection{Translating an expression to MathML: \texttt{mathml}}\index{MathML}\index{mathml} 1695 1696The \texttt{mathmml} command will take an expression 1697and return the result in MathML. For example, if you enter 1698\begin{center} 1699 {\tt mathml(1/4 + 1/4)} 1700\end{center} 1701you will get 1702\begin{verbatim} 1703<?xml version="1.0" encoding="iso-8859-1"?> 1704<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" 1705"http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd" [ 1706<!ENTITY mathml "http://www.w3.org/1998/Math/MathML"> 1707]> 1708<html xmlns="http://www.w3.org/1999/xhtml"> 1709<body> 1710 1711<math mode="display" xmlns="http://www.w3.org/1998/Math/MathML"> 1712 1713<mfrac><mrow><mn>1</mn></mrow><mrow><mn>2</mn></mrow></mfrac> 1714 1715</math><br/> 1716 1717</body> 1718</html> 1719\end{verbatim} 1720which is the number $1/2$ in MathML form, along with enough 1721information to make it a complete HTML document. 1722 1723\subsection{Translating a spreadsheet to MathMML} 1724 1725You can translate an entire spreadsheet to MathML with the spreadsheet 1726menu command \texttt{Table$\blacktriangleright$Save as mathml}. 1727 1728\subsection{Export to presentation or content MathML : {\tt export\_mathml}\index{export\_mathml}} 1729The command {\tt export\_mathml} accepts one or two arguments: an expression and optionally the symbol {\tt content} or {\tt display}. If {\tt content} is specified, it converts the expression to content MathML, while for {\tt display} it converts it to presentation MathML. If the second argument is omitted, the return value contains both presentation and content MathML within a {\tt semantics} block. The return value in each case is a string containing a single {\tt math} block. 1730 1731For example, input : 1732\begin{center} 1733 \tt xml\_print(export\_mathml(a+2*b)) 1734\end{center} 1735Output : 1736\begin{verbatim} 1737<math xmlns='http://www.w3.org/1998/Math/MathML'> 1738 <semantics> 1739 <mrow xref='id5'> 1740 <mi xref='id1'>a</mi> 1741 <mo>+</mo> 1742 <mrow xref='id4'> 1743 <mn xref='id2'>2</mn> 1744 <mo>⁢</mo> 1745 <mi xref='id3'>b</mi> 1746 </mrow> 1747 </mrow> 1748 <annotation-xml encoding='MathML-Content'> 1749 <apply id='id5'> 1750 <plus/> 1751 <ci id='id1'>a</ci> 1752 <apply id='id4'> 1753 <times/> 1754 <cn id='id2' type='integer'>2</cn> 1755 <ci id='id3'>b</ci> 1756 </apply> 1757 </apply> 1758 </annotation-xml> 1759 <annotation encoding='Giac'>a+2*b</annotation> 1760 </semantics> 1761</math> 1762\end{verbatim} 1763Input : 1764\begin{center} 1765 \tt xml\_print(export\_mathml(a+2*b,content)) 1766\end{center} 1767Output : 1768\begin{verbatim} 1769<math xmlns='http://www.w3.org/1998/Math/MathML'> 1770 <apply id='id5'> 1771 <plus/> 1772 <ci id='id1'>a</ci> 1773 <apply id='id4'> 1774 <times/> 1775 <cn id='id2' type='integer'>2</cn> 1776 <ci id='id3'>b</ci> 1777 </apply> 1778 </apply> 1779</math> 1780\end{verbatim} 1781Input : 1782\begin{center} 1783 \tt xml\_print(export\_mathml(a+2*b,display)) 1784\end{center} 1785Output : 1786\begin{verbatim} 1787<math xmlns='http://www.w3.org/1998/Math/MathML'> 1788 <mrow> 1789 <mi>a</mi> 1790 <mo>+</mo> 1791 <mrow> 1792 <mn>2</mn> 1793 <mo>⁢</mo> 1794 <mi>b</mi> 1795 </mrow> 1796 </mrow> 1797</math> 1798\end{verbatim} 1799 1800\subsection{Indent a XML string : {\tt xml\_print}\index{xml\_print}} 1801{\tt xml\_print} takes a string as its only argument, assumes that it contains XML code and indents it for better readability. The indented code is returned as string. The default indent is two spaces. 1802 1803For example, input : 1804\begin{center} 1805 \tt xml\_print("<?xml version='1.0'?><root><child1>some content</child1><child2></child2><child3/></root>") 1806\end{center} 1807Output : 1808\begin{verbatim} 1809<?xml version='1.0'?> 1810<root> 1811 <child1>some content</child1> 1812 <child2></child2> 1813 <child3/> 1814</root> 1815\end{verbatim} 1816Input : 1817\begin{center} 1818 \tt s:=export\_mathml(1/(x\^{}2+1),display):;\\ 1819 xml\_print(s) 1820\end{center} 1821Output : 1822\begin{verbatim} 1823<math mode='display' xmlns='http://www.w3.org/1998/Math/MathML'> 1824 <mfrac> 1825 <mn>1</mn> 1826 <mrow> 1827 <msup> 1828 <mi>x</mi> 1829 <mn>2</mn> 1830 </msup> 1831 <mo>+</mo> 1832 <mn>1</mn> 1833 </mrow> 1834 </mfrac> 1835</math> 1836\end{verbatim} 1837 1838\subsection{Translating a Maple\index{Maple} file to Xcas: \texttt{maple2xcas}}\index{maple2xcas} 1839 1840You can translate a file of Maple commands to the Xcas language with 1841the \texttt{maple2xcas} command, as in 1842\begin{center} 1843 {\tt maple2xcas("MapleFile","XcasFile")} 1844\end{center} 1845This command takes two arguments, the name of the Maple input file and 1846the name of the file where you want to save the Xcas commands. 1847 1848\chapter{Entry in \texttt{Xcas}} 1849 1850\section{Suppressing output} 1851 1852If you enter a command into \texttt{Xcas}, the result will appear in 1853the output box below the input. If you enter 1854\begin{center} 1855 {\tt a := 2+2} 1856\end{center} 1857then 1858\begin{center} 1859 {\tt 4} 1860\end{center} 1861will appear in the output box. You can evaluate the input and 1862suppress the output with the \texttt{nodisp}\index{nodisp} command. 1863If you enter 1864\begin{center} 1865 {\tt nodisp(a := 2+2)} 1866\end{center} 1867then \texttt{a} will still be set to \texttt{4}, but the result will 1868not appear in the output box. Instead, 1869\begin{center} 1870 {\tt Done} 1871\end{center} 1872will appear. 1873 1874An alternate way of suppressing the output is to end the input with 1875\texttt{:;}\index{:;}, if you enter 1876\begin{center} 1877 {\tt b := 3+3:;} 1878\end{center} 1879then \texttt{b} will be set to \texttt{6} but it won't be displayed. 1880 1881\section{Entering comments}\index{comments} 1882 1883You can annotate an \texttt{Xcas} session by adding comments. 1884You can enter a comment on the current line at any time by typing 1885\texttt{Alt+C}. The line will appear in green text and conclude when 1886you type \texttt{Enter}. Comments are not evaluated and so have no 1887output. If you have begun entering a command when you begin a 1888comment, the command line be pushed down so that you can finish it 1889when you complete the comment. 1890 1891You can open the browser in a comment line by entering the web address 1892beginning with the \texttt{@} sign. If you enter the comment line 1893\begin{center} 1894{\tt The Xcas homepage is at\\ 1895@www-fourier.ujf-grenoble.fr/\~{}parisse/giac.html} 1896\end{center} 1897then the browser will open to the \texttt{Xcas} home page. 1898 1899To add a comment to a program, rather than a session, you can use the 1900\texttt{comment\index{comment}} command, which takes a string as an argument. 1901Alternatively, any part of a program between \texttt{//}\index{//} and the end 1902of the line is a comment. So both 1903\begin{center} 1904{\tt bs() := \{comment("Hello"); return "Hi there!";\}} 1905\end{center} 1906and 1907\begin{center} 1908{\tt bs() := \{ // Hello\\ 1909 return "Hi there!";\}} 1910\end{center} 1911are programs with the comment "Hello". 1912 1913\section{Editing expressions} 1914 1915You can enter expressions on the command line, but \texttt{Xcas} also 1916has a built-in expression editor that you can use to enter expressions 1917in two dimensions, the way they normally look when typeset. When you 1918have an expression in the editor, you can also manipulate 1919subexpressions apart from the entire expression. 1920 1921\subsection{Entering expressions in the editor} 1922 1923The expression 1924\[\frac{x+2}{x^2-4}\] 1925can be entered on the command line with 1926\begin{center} 1927{\tt (x+2)/(x\^{}2-4)} 1928\end{center} 1929You also can use the expression editor\index{expression editor} to 1930enter it visually, as $x+2$ on top of $x^2 - 4$. To do this, you can 1931start the expression editor with the \texttt{Alt+E} keystroke (or the 1932\texttt{Expression $\blacktriangleright$ New Expression} menu 1933command). There will be a small \texttt{M} on the right side of the 1934expression line, which is a menu with some commands you can use on the 1935expressions. There will also be a \texttt{0} selected on the 1936expression line and an on-screen keyboard at the bottom. If you type 1937\texttt{x + 2}, it will overwrite the \texttt{0}. To make this the 1938top of the fraction, you can select it with the mouse (you can also 1939make selections with the keyboard, as will be discussed later) and 1940then type \texttt{/}. This will leave the \texttt{x + 2} on the top 1941and the cursor on the bottom. To enter $x^2 - 4$ on the bottom, begin 1942by typing \texttt{x}. Selecting this \texttt{x} and typing 1943\texttt{\^{}2} will put on the superscript. Finally, selecting the 1944$\texttt{x}^{\texttt{2}}$ and typing \texttt{- 4} will finish the 1945bottom. If you then hit \texttt{Enter}, the expression will be 1946evaluated and will appear on the output line. 1947 1948\subsection{Subexpressions}\index{subexpressions} 1949 1950\texttt{Xcas} can operate on expressions in the expression editor or 1951subexpressions of the expression. To understand subexpressions and 1952how to select them, it helps to know that \texttt{Xcas} stores 1953expressions as \textsl{trees}. 1954 1955A tree, in this sense, consists of objects 1956called nodes. A node can be connected to lower nodes, called the 1957children of the first node. Each node (except one) will be connected 1958to exactly one node above it, called the parent node. One special 1959node, called the root node, won't have a parent node. Two nodes with 1960the same parent nodes are called siblings. Finally, if a node doesn't 1961have any children, it is called a leaf. This terminology comes from a 1962visual representation of a tree, 1963\begin{center} 1964 \includegraphics[width=0.4\textwidth]{xcas-tree.png} 1965\end{center} 1966which looks like an upside-down tree; the root is at the 1967top and the leaves are at the bottom. 1968 1969Given an expression, the nodes of the corresponding 1970tree\index{expression tree} are the functions, operators, variables 1971and constants. The children of a function node are its arguments, the 1972children of an operator node are its operands, and the constants and 1973variables will be the leaves. For example, the tree for $\sin(2*x + 1974y)$ will look like 1975\begin{center} 1976 \includegraphics[width=0.4\textwidth]{xcas-expr-tree.png} 1977\end{center} 1978A subexpression\index{subexpression} of an expression will be a 1979selected node together with the nodes below it. For example, both 1980$2*x$ and $2*x+y$ are subexpressions of $\sin(2*x+y)$, but $x+y$ is 1981not. 1982 1983A subexpression of the contents of the expression editor can be 1984selected with the mouse; the selection will appear white on a black 1985background. A subexpression can also be chosen with the keyboard 1986using the arrow keys. Given a selection: 1987\begin{itemize} 1988 \item 1989 The up arrow will go to the parent node. 1990 \item 1991 The down arrow will go to the leftmost child node. 1992 \item 1993 The right and left arrows will go to the right and left sibling nodes. 1994 \item 1995 The control key with the right and left arrows will switch the 1996 selection with the corresponding sibling. 1997 \item 1998 If a constant or variable is selected, the backspace key will delete 1999 it. For other selections, backspace will delete the function or 2000 operator, and another backspace will delete the arguments or operands. 2001\end{itemize} 2002 2003You can use the arrow keys to navigate the tree structure of an 2004expression, which isn't always evident by looking at the expression 2005itself. For example, suppose you enter \texttt{x*y*z} in the editor. 2006The two multiplications will be a different levels; the tree will look 2007like 2008\begin{center} 2009 \includegraphics[width=0.4\textwidth]{xcas-xyz-tree.png} 2010\end{center} 2011If you select the entire expression with the up arrow and then go to 2012the \texttt{M} menu to the right of the line and choose eval, then the 2013expression will look the same but, as you can check by navigating it 2014with the arrow keys, the tree will look like 2015\begin{center} 2016 \includegraphics[width=0.4\textwidth]{xcas-xyz-tree2.png} 2017\end{center} 2018 2019\subsection{Manipulating subexpressions\index{subexpressions}} 2020 2021If a subexpression is selected in the expression editor, then any menu 2022command will be applied to that subexpression. 2023 2024For example, suppose that you enter the expression 2025\begin{center} 2026 {\tt (x+1)*(x+2)*(x-1)} 2027\end{center} 2028in the expression editor. Note that you can use the abilities of the 2029editor to make this easier. First, enter \texttt{x+1}. Select this 2030with the up arrow, then type \texttt{*} followed by \texttt{x+2}. Select 2031the \texttt{x+2} with the up arrow and then type \texttt{*} followed 2032by \texttt{x-1}. Using the up arrow again will select the \texttt{x-1}. 2033Select the entire expression with the up arrow, and then select 2034\texttt{eval} from the \texttt{M} menu. This will put all factors at 2035the same level. Suppose you want the factors \texttt{(x+1)*(x+2)} to 2036be expanded. You could select \texttt{(x+1)*(x+2)} with the mouse and 2037do one of the following: 2038\begin{itemize} 2039 \item 2040 Select the 2041 \texttt{Expression$\blacktriangleright$Misc$\blacktriangleright$normal} 2042 menu item. You will then have \texttt{normal((x+1)*(x+2))*(x-1)} in 2043 the editor. If you hit enter, the result $(x^2 + 3x + 2)*(x-1)$ will 2044 appear in the output window. 2045 \item 2046 Again, select the 2047 \texttt{Expression$\blacktriangleright$Misc$\blacktriangleright$normal} 2048 menu item, so again you have \texttt{normal((x+1)*(x+2))*(x-1)} in 2049 the editor. Now if you select \texttt{eval} from the \texttt{M} 2050 menu, then the expression in the editor will become the result 2051 $(x^2 + 3x + 2)*(x-1)$, which you can continue editing. 2052 \item 2053 Choose \texttt{normal} from the \texttt{M} menu. This will apply 2054 normal to the selection, and again you will have the result 2055 $(x^2 + 3x + 2)*(x-1)$ in the editor. 2056\end{itemize} 2057 2058There are also keystroke commands that you can use to operate 2059on subexpressions that you've selected. There are the usual 2060\texttt{Ctrl+Z} and \texttt{Ctrl+Y} for undoing and redoing. Some of 2061the others are given in the following table. 2062 2063\begin{center} 2064\begin{tabular}{|p{.20\textwidth}|p{.6\textwidth}|} 2065\hline 2066\textbf{Key} & \textbf{Action on selection}\\ 2067%\multicolumn{2}{|c|}{\textbf{Conversions}}\\ 2068\hline\hline 2069\texttt{Ctrl+D} & differentiate\\ 2070\texttt{Ctrl+F} & factor\\ 2071\texttt{Ctrl+L} & limit\\ 2072\texttt{Ctrl+N} & normalize\\ 2073\texttt{Ctrl+P} & partial fraction\\ 2074\texttt{Ctrl+R} & integrate\\ 2075\texttt{Ctrl+S} & simplify\\ 2076\texttt{Ctrl+T} & copy \LaTeX{} version to clipboard\\ 2077\hline 2078\end{tabular} 2079\end{center} 2080 2081\section{Previous results} 2082 2083The \texttt{ans\index{ans}} command will return the results of previous commands. 2084The input to \texttt{ans} is the number of the command, beginning with 20850. If the first command that you enter is 2086\begin{center} 2087 {\tt 2+5} 2088\end{center} 2089resulting in 2090\begin{center} 2091 {\tt 7} 2092\end{center} 2093then later references to \texttt{ans(0)} will evaluate to \texttt{7}. 2094 2095Note that the argument to \texttt{ans} doesn't correspond to the line 2096number in \texttt{Xcas}. For one thing, the line numbers begin at 1. 2097What's more, if you go back and re-evaluate a previous line, then that 2098will become part of the commands that \texttt{ans} keeps track of. 2099 2100If you give \texttt{ans} a negative number, then it counts backwards 2101from the current input. To get the latest output, for 2102example, you can use \texttt{ans(-1)}. With no argument, 2103\texttt{ans()} wil also return the latest output. 2104 2105Similarly, \texttt{quest\index{quest}} will return the previous inputs. Since 2106these will often be simplified to be the same as the output, 2107\texttt{quest($n$)} sometimes has the same value as \texttt{ans($n$)}. 2108 2109You can also use \texttt{Ctrl} plus the arrow keys to scroll through 2110previous inputs. With the cursor on the command line, 2111\texttt{Ctrl+uparrow} will go backwards in the list of previous 2112commands and \texttt{Ctrl+downarrow} will go forwards. 2113 2114\section{Spreadsheet} 2115\index{spreadsheet} 2116 2117\subsection{Opening a spreadsheet} 2118 2119You can open a spreadsheet (or a matrix editor) with the 2120\texttt{Spreadsheet$\blacktriangleright$New Spreadsheet} menu item or 2121with the key \texttt{Alt+T}. 2122 2123When you open a new spreadsheet, you will be given a configuration screen. 2124The configuration screen allows you to set the following options: 2125\begin{itemize} 2126 \item 2127 \texttt{Variable} 2128 The name of the file where the spreadsheet will be 2129 saved. 2130 \item 2131 \texttt{Rows} and \texttt{Columns} 2132 The number of rows and columns in the spreadsheet. 2133 \item 2134 \texttt{Eval} 2135 Whether or not to automatically re-evaluate the entries in the 2136 spreadsheet after each change. If this is not checked, then you can 2137 re-evaluate the spreadsheet with the \texttt{eval} button on the 2138 spreadsheet menu bar. 2139 \item 2140 \texttt{Distribute} 2141 Whether or not entering a matrix into a cell will keep the entry in 2142 a single cell or distribute it across an appropriate array of cells. 2143 \item 2144 \texttt{Landscape} 2145 Whether the graphical representation of the spreadsheet should be 2146 displayed below the spreadsheet or to the right of the spreadsheet. 2147 If this is checked, it will be displayed below the spreadsheet. 2148 \item 2149 \texttt{Move right} 2150 Whether or not to move to the cell to the right of the current cell 2151 when data is entered. If this is not checked, you will be moved to 2152 the cell below the current cell. 2153 \item 2154 \texttt{Spreadsheet} 2155 Whether to format a spreadsheet or a matrix. 2156 \item 2157 \texttt{Graph} 2158 Whether or not to display the graphical representation of the 2159 spreadsheet. 2160 % \item 2161 % \texttt{Undo history} 2162 % \item 2163 % \texttt{Init sheet} 2164\end{itemize} 2165The configuration screen can be reopened with the 2166\texttt{Edit$\blacktriangleright$Configuration$\blacktriangleright$Cfg 2167window} menu attached to the spreadsheet. 2168 2169\subsection{The spreadsheet window} 2170 2171When you open a spreadsheet, the input line will become the 2172spreadsheet. 2173\begin{center} 2174\includeimage{xcas-spreadsheet.png} 2175\end{center} 2176The top will be a menu bar with 2177\texttt{Table}, \texttt{Edit} and \texttt{Maths} menus as well as 2178\texttt{eval}, \texttt{val}, \texttt{init}, \texttt{2-d} and 2179\texttt{3-d} buttons. To the right will be the name of 2180the file the spreadsheet will be saved into. Below the menu bar will 2181be two boxes; a box which displays the active cell (and can be used to 2182choose a cell) and a command line to enter information into the cell. 2183Below that will be a status line, you can click on this to return to 2184the configuration screen. 2185 2186%% \section{Deletions}? 2187 2188\section{Variables} 2189 2190\subsection{Variable names} 2191 2192A variable\index{variable} or function name is a sequence of letters, 2193numbers and underscores that begins with a letter. If you define your 2194own variable or function, you can't use the names of built-in 2195variables or functions, or other keywords reserved by \texttt{Xcas}. 2196 2197\subsection{The \texttt{CST} variable}\index{CST} 2198\label{ssec:cst} 2199 2200The menu available with the \texttt{cust} button on the onscreen 2201keyboard is defined with the \texttt{CST} variable. It is a list 2202where each list item determines a menu item; a list item is either a 2203builtin command name or a list itself consisting of a string to be 2204displayed in the menu and the input to be entered when the item is 2205selected. 2206 2207For example, to create a custom defined menu with the builtin function 2208\texttt{diff}, a user defined function \texttt{foo}, and a menu item 2209to insert the number $22/7$, you can set 2210\begin{center} 2211 \tt 2212 CST := [diff,["foo",foo],["My pi approx",22/7]] 2213\end{center} 2214 2215Note that if the input to be entered is a variable and the variable 2216has a value when \texttt{CST} is defined, then \texttt{CST} will 2217contain the value of the variable. For example,\\ 2218Input: 2219\begin{center} 2220 \tt 2221 app := 22/7\\ 2222 CST := [diff,["foo",foo],["My pi approx",app]] 2223\end{center} 2224will be equivalent to the previous definition of \texttt{CST}. 2225However, if the variable does not have a value when \texttt{CST} is 2226defined, for example, 2227Input: 2228\begin{center} 2229 \tt 2230 CST := [diff,["foo",foo],["My pi approx",app]]\\ 2231 app := 22/7 2232\end{center} 2233will behave as the previous values, to begin with, but in this case if 2234the variable \texttt{app} is changed, so will the the result of 2235pressing the \texttt{My pi approx} button. 2236 2237Since \texttt{CST} is a list, a function can be added to the 2238\texttt{cust} menu with the \texttt{concat} command;\\ 2239Input: 2240\begin{center} 2241 \tt 2242 CST := concat(CST,evalc) 2243\end{center} 2244will add the \texttt{evalc} command to the \texttt{cust} menu. 2245 2246\subsection{Assigning values: \texttt{:= => = assign sto Store}}\index{:=}\index{=>}\index{=}\index{assign}\index{sto}\index{Store} 2247 2248You can assign a value to a variable with the \texttt{:=} 2249operator. For example, to give the variable \texttt{a} the value of 2250\texttt{4}, you can enter 2251\begin{center} 2252 {\tt a := 4} 2253\end{center} 2254Alternatively, you can use the \texttt{=>} operator; when 2255you use this operator, the value comes before the variable; 2256\begin{center} 2257 {\tt 4 => a} 2258\end{center} 2259The function \texttt{sto} or \texttt{Store} 2260can also be used; again, the value comes before the variable 2261\begin{center} 2262 {\tt sto(4,a)} 2263\end{center} 2264After any one of these commands, any time you use the variable 2265\texttt{a} in an expression, it will be replaced by \texttt{4}. 2266 2267You can use sequences or lists to make multiple assignments at the 2268same time. For example, 2269\begin{center} 2270{\tt (a,b,c) := (1,2,3)} 2271\end{center} 2272will assign \texttt{a} the value \texttt{1}, \texttt{b} the value 2273\texttt{2} and \texttt{c} the value \texttt{3}. Note that this can 2274be used to switch the values of two variables; with \texttt{a} and 2275\texttt{b} as above, the command 2276\begin{center} 2277{\tt (a,b) := (b,a)} 2278\end{center} 2279will set \texttt{a} equal to \texttt{b}'s original value, namely 2280\texttt{2}, and will set \texttt{b} equal to \texttt{a}'s original 2281value, namely \texttt{1}. 2282 2283Another way to assign values to variables, useful in Maple mode, is 2284with the \texttt{assign} command. If you enter 2285\begin{center} 2286 {\tt assign(a,3)} 2287\end{center} 2288or 2289\begin{center} 2290 {\tt assign(a = 3)} 2291\end{center} 2292then \texttt{a} will have the value \texttt{3}. You can assign 2293multiple values at once; if you enter 2294\begin{center} 2295 {\tt assign([a = 1, b = 2])} 2296\end{center} 2297then \texttt{a} will have the value \texttt{1} and \texttt{b} will 2298have the value \texttt{2}. This command can be useful in Maple mode, 2299where solutions of equations are returned as equations. For example, 2300if you enter (in Maple mode) 2301\begin{center} 2302 {\tt sol := solve([x + y = 1, y = 2])} 2303\end{center} 2304you will get 2305\begin{center} 2306 {\tt [x = -1, y = 2]} 2307\end{center} 2308If you then enter 2309\begin{center} 2310 {\tt assign(sol)} 2311\end{center} 2312the variable \texttt{x} will have value \texttt{-1} and \texttt{y} 2313will have the value \texttt{2}. This same effect can be achieved in 2314standard \texttt{Xcas} mode, where 2315\begin{center} 2316 {\tt sol := solve([x + y = 1, y = 2])} 2317\end{center} 2318will return 2319\begin{center} 2320 {\tt [[x = -1, y = 2]]} 2321\end{center} 2322In this case, the command 2323\begin{center} 2324 {\tt [x,y] := sol[0]} 2325\end{center} 2326will assign \texttt{x} the value \texttt{-1} and \texttt{y} the value 2327\texttt{2}. 2328 2329\subsection{Assignment by reference: \texttt{=<}}\index{=<} 2330\label{subsec:refassign} 2331A list is simply a sequence of values separated by commas and 2332delimited by \texttt{[} and \texttt{]} (see section \ref{sec:seq}). 2333Suppose you give the variable \texttt{a} the value \texttt{[1,1,3,4,5]}, 2334\begin{center} 2335 {\tt a := [1,2,3,4,5]} 2336\end{center} 2337If you later assign to \texttt{a} the value \texttt{[1,2,3,4,5]}, then 2338a new list is created. It may be better to just change the second 2339value in the original list by reference. This can be done with the 2340\texttt{=<} command. Recalling that lists are indexed beginning at 0, 2341the command 2342\begin{center} 2343 {\tt a[1] =< 2} 2344\end{center} 2345will simply change the value of the second element of the list instead 2346of creating a new list, and is a more efficient way to change the 2347value of \texttt{a} to \texttt{[1,2,3,4,5]}. 2348 2349\subsection{Copying values of list: \texttt{copy}}\index{copy} 2350 2351If you enter 2352\begin{center} 2353 {\tt list1 := [1,2,3]} 2354\end{center} 2355and then 2356\begin{center} 2357 {\tt list2 := list1} 2358\end{center} 2359then \texttt{list1} and \texttt{list2} will be equal to the same list, 2360not simply two lists with the same elements. In particular, if you 2361change (by reference) the value of an element of \texttt{list1}, then 2362the change will also be reflected in \texttt{list2}. For example, if 2363you enter 2364\begin{center} 2365 {\tt list1[1] =< 5} 2366\end{center} 2367then both \texttt{list1} and \texttt{list2} will be equal to 2368\texttt{[1,5,3]}. 2369 2370The \texttt{copy} command will create a copy of a list (or 2371vector or matrix) which is equal to the original list, but distinct 2372from it. For example, if you enter 2373\begin{center} 2374 {\tt list1 := [1,2,3]} 2375\end{center} 2376and then 2377\begin{center} 2378 {\tt list2 := copy(list1)} 2379\end{center} 2380then \texttt{list1} and \texttt{list2} will both be \texttt{[1,2,3]}, 2381but now if you enter 2382\begin{center} 2383 {\tt list1[1] =< 5} 2384\end{center} 2385then both \texttt{list1} will be equal to \texttt{[1,5,3]} but 2386\texttt{list2} will still be \texttt{[1,2,3]}. 2387 2388\subsection{Incrementing variables: \texttt{+= -= *= /=}}\index{+=}\index{-=}\index{*=}\index{/=} 2389 2390You can increase the value of a variable \texttt{a} by \texttt{4}, for 2391example, with 2392\begin{center} 2393 {\tt a := a + 4} 2394\end{center} 2395If beforehand \texttt{a} were equal to \texttt{4}, it would now be 2396equal to \texttt{8}. A shorthand way of doing this is with the 2397\texttt{+=}\index{+=} operator; 2398\begin{center} 2399 {\tt a += 4} 2400\end{center} 2401will also increase the value of \texttt{a} by \texttt{4}. 2402 2403Similar shorthands exist for subtraction, multiplication and division. 2404If \texttt{a} is equal to \texttt{8} and you enter 2405\begin{center} 2406 {\tt a -= 2} 2407\end{center} 2408then \texttt{a} will be equal to \texttt{6}. If you follow this with 2409\begin{center} 2410 {\tt a *= 3} 2411\end{center} 2412then \texttt{a} will be equal to \texttt{18}, and finally 2413\begin{center} 2414 {\tt a /= 9} 2415\end{center} 2416will end with \texttt{a} equal to \texttt{2}. 2417 2418 2419\subsection{Storing and recalling variables and their values: \texttt{archive unarchive}}\index{archive}\index{unarchive} 2420 2421You can store variables and their values for later use in a file of 2422your choosing with the \texttt{archive} function. This 2423function takes two arguments, a filename to store the variables in and 2424a variable or list of variables. 2425 2426If you have given the variable \texttt{a} the value \texttt{2} and the 2427variable \texttt{bee} the value \texttt{"letter"} (a string), then 2428entering 2429\begin{center} 2430 {\tt archive("foo",[a,bee])} 2431\end{center} 2432will create a file named ``\texttt{foo}'' which contains the values 2433\texttt{2} and \texttt{"letter"} in a format meant to be efficiently 2434read by \texttt{Xcas}. 2435 2436You can recall the values stored by \texttt{archive} with the 2437\texttt{unarchive} command, which takes a file name 2438as argument. If the file ``\texttt{foo}'' is as above, then 2439\begin{center} 2440 {\tt unarchive("foo")} 2441\end{center} 2442will result in 2443\begin{center} 2444 {\tt [2, letter]} 2445\end{center} 2446If you want to reassign these values to \texttt{a} and \texttt{bee}, 2447you can enter 2448\begin{center} 2449 {\tt [a,bee] := unarchive("foo")} 2450\end{center} 2451 2452\subsection{Copying variables: \texttt{CopyVar}}\index{CopyVar} 2453\label{subsec:copyvar} 2454 2455If a variable has a value, such as 2456\begin{center} 2457 {\tt a := 1} 2458\end{center} 2459and you set a second variable to the first variable 2460\begin{center} 2461 {\tt b := a} 2462\end{center} 2463the new variable will have the same value as the first; in this case 2464\texttt{b} will be equal to \texttt{1}. If you later give the first 2465variable a new value; 2466\begin{center} 2467 {\tt a := 5} 2468\end{center} 2469the new value will still have the old value, in this case, \texttt{b} 2470will still be equal to \texttt{1}. 2471 2472The \texttt{CopyVar} command will copy one variable to 2473another without evaluating the first variable; the new variable will 2474simply be a copy of the first. With \texttt{a} having the value of 2475\texttt{5}, as above, the command 2476\begin{center} 2477 {\tt CopyVar(a,c)} 2478\end{center} 2479will make \texttt{c} a copy of the variable \texttt{a}, so it will 2480have the value \texttt{5} also. If you now change the value of 2481\texttt{a} 2482\begin{center} 2483 {\tt a := 10} 2484\end{center} 2485then the value of \texttt{c} will also change; here, \texttt{c} will 2486now have the value \texttt{10}. 2487 2488\subsection{Assumptions on variables: \texttt{about additionally assume purge supposons and or}}\index{about}\index{assume}\index{purge}\index{supposons}\index{additionally}\index{and}\index{or} 2489 2490If you enter 2491\begin{center} 2492 {\tt abs(var)} 2493\end{center} 2494the \texttt{Xcas} will return it unevaluated, since \texttt{Xcas} 2495doesn't know what type of value the variable is supposed to represent. 2496 2497The \texttt{assume} (or 2498\texttt{supposons}) command will let you tell 2499\texttt{Xcas} some properties of a variable without giving the 2500variable a specific value. For example, if you enter 2501\begin{center} 2502 {\tt assume(var > 0)} 2503\end{center} 2504then \texttt{Xcas} will assume that \texttt{var} is a positive real 2505number, and so for example 2506\begin{center} 2507 {\tt abs(var)} 2508\end{center} 2509will be evaluated to 2510\begin{center} 2511 {\tt var} 2512\end{center} 2513 2514You can put one or more conditions in the \texttt{assume} command by 2515combining them with \texttt{and} and \texttt{or}. 2516For example, if you want the variable \texttt{a} to be in $[2,4) \cup 2517(6,\infty)$, you can enter 2518\begin{center} 2519 {\tt assume((a >= 2 and a < 4) or a > 6)} 2520\end{center} 2521 2522If a variable has attached assumptions, then making another assumption 2523with \texttt{assume} will remove the original assumptions. To add 2524extra assumptions, you can either use the 2525\texttt{additionally} command or give 2526\texttt{assume} a second argument of 2527\texttt{additionally\index{additionally@\textit{additionally}}}. If 2528you assume that $b > 0$ with 2529\begin{center} 2530 {\tt assume(b > 0)} 2531\end{center} 2532and you want to add the condition that $b < 1$, you can either enter 2533\begin{center} 2534 {\tt assume(b < 1, additionally)} 2535\end{center} 2536or 2537\begin{center} 2538 {\tt additionally(b < 1)} 2539\end{center} 2540 2541As well as equalities and inequalities, you can make assumptions about 2542the domain of a variable. If you want \texttt{n} to represent a 2543positive integer\index{integer@\textit{integer}}, for example, you can enter 2544\begin{center} 2545 {\tt assume(n, integer)} 2546\end{center} 2547If you want \texttt{n} to be a positive integer, you can add the 2548condition 2549\begin{center} 2550 {\tt additionally(n > 0)} 2551\end{center} 2552 2553You can use the \texttt{about} command to check the assumptions on a 2554variable; for the above positive integer \texttt{n}, if you enter 2555\begin{center} 2556 {\tt about(n)} 2557\end{center} 2558you will get 2559\begin{center} 2560 {\tt assume[integer,[line[0,+infinity]],[0]]} 2561\end{center} 2562The first element tells you that \texttt{n} is an integer, the second 2563element tells you that \texttt{n} is between \texttt{0} and 2564\texttt{+infinity}, and the third element tells you that the value 2565\texttt{0} is excluded. 2566 2567If you assume that a variable is equal to a specific value, such as 2568\begin{center} 2569 {\tt assume(c = 2)} 2570\end{center} 2571then by default the variable \texttt{c} will remain unevaluated in 2572later levels. If you want an expression involving \texttt{c} to be 2573evaluated, you would need to put the expression inside the 2574\texttt{evalf\index{evalf}} command; if you enter 2575\begin{center} 2576 {\tt evalf(c\^{}2 + 3)} 2577\end{center} 2578then you will get 2579\begin{center} 2580 {\tt 7.0} 2581\end{center} 2582Right below the \texttt{assume(c = 2)} command line 2583there will be a slider, namely arrows pointing left and right with the 2584value \texttt{2} between them. These 2585can be used to change the values of \texttt{c}. If you click on the 2586right arrow, the \texttt{assume(c = 2)} command will transform to 2587\begin{center} 2588 {\tt assume(c=[2.2,-10.0,10.0,0.0])} 2589\end{center} 2590and the value between the arrows will be \texttt{2.2}. Also, any 2591later levels where the variable \texttt{c} is evaluated will be 2592re-evaluated with the value of \texttt{c} now \texttt{2.2}. The 2593output to \texttt{evalf(c\^{}2 + 3} will become 2594\begin{center} 2595 {\tt 7.84} 2596\end{center} 2597The \texttt{-10.0} and \texttt{10.0} in the \texttt{assume} line 2598represent the smallest and largest values that \texttt{c} can become 2599using the sliders. You can set them yourself in the \texttt{assume} 2600command, as well as the increment that the value will change; if you 2601want \texttt{c} to start with the value \texttt{5} and vary between 2602\texttt{2} and \texttt{8} in increments of \texttt{0.05}, then you can 2603enter 2604\begin{center} 2605 {\tt assume(c = [5,2,8,0.05])} 2606\end{center} 2607 2608You can remove any assumptions you have made about a variable with the 2609\texttt{purge} command; if you enter 2610\begin{center} 2611 {\tt purge(a)} 2612\end{center} 2613then \texttt{a} will no longer have any assumptions made about it. 2614You can remove assumptions from more than one variable at a time; 2615\begin{center} 2616 {\tt purge(a,b)} 2617\end{center} 2618will remove any assumptions about \texttt{a} and \texttt{b}. 2619 2620\subsection{Unassigning variables: \texttt{VARS purge DelVar del restart rm\_a\_z rm\_all\_vars}}\index{VARS}\index{purge}\index{restart}\index{rm\_a\_z}\index{rm\_all\_vars}\index{DelVar}\index{del} 2621 2622The \texttt{VARS()} command will list the variables to which you have 2623assigned values or assumptions. If you begin by entering 2624\begin{center} 2625 {\tt a := 1} 2626\end{center} 2627and 2628\begin{center} 2629 {\tt anothervar := 2} 2630\end{center} 2631then 2632\begin{center} 2633 {\tt VARS()} 2634\end{center} 2635will return 2636\begin{center} 2637 {\tt [a, anothervar]} 2638\end{center} 2639 2640The \texttt{purge} command will clear the values and 2641assumptions you make on variables. To clear the values and 2642assumptions on \texttt{a}, for example, you can enter\\ 2643Input: 2644\begin{center} 2645 \tt 2646 purge(a) 2647\end{center} 2648For \texttt{TI} compatibility, you can also enter\\ 2649Input: 2650\begin{center} 2651 \tt 2652 DelVar a 2653\end{center} 2654and for Python compatibility, you can also enter 2655Output: 2656\begin{center} 2657 \tt 2658 del a 2659\end{center} 2660 2661To clear the values and assumptions you have made on all variables you 2662can use the 2663\begin{center} 2664 {\tt restart} 2665\end{center} 2666or 2667\begin{center} 2668 {\tt rm\_all\_vars()} 2669\end{center} 2670command. The command \texttt{rm\_a\_z} will clear the values and 2671assumptions of the variables with single lowercase letter names. If 2672you have variables names \texttt{A,B,a,b,myvar}, then after\\ 2673Input: 2674\begin{center} 2675 \tt 2676 rm\_a\_z() 2677\end{center} 2678you will only have the variables named \texttt{A,B,myvar}. 2679 2680\section{Functions} 2681 2682\subsection{Defining functions} 2683 2684You can use the \texttt{:=}\index{:=} and \texttt{=>}\index{=>} 2685operators to define functions; both 2686\begin{center} 2687 {\tt f(x) := x\^{}2} 2688\end{center} 2689and 2690\begin{center} 2691 {\tt x\^{}2 => f(x)} 2692\end{center} 2693give the name \texttt{f} to the function which takes a value and 2694returns the square of the value. If you then enter 2695\begin{center} 2696 {\tt f(3)} 2697\end{center} 2698you will get 2699\begin{center} 2700 {\tt 9} 2701\end{center} 2702 2703You can give \texttt{Xcas} a function without a name with the 2704\texttt{->}\index{->} operator; the squaring function can be written 2705without a name as 2706\begin{center} 2707 {\tt x -> x\^{}2} 2708\end{center} 2709You can use this form of the function to assign it to a name; both 2710\begin{center} 2711 {\tt f := x -> x\^{}2} 2712\end{center} 2713and 2714\begin{center} 2715 {\tt x -> x\^{}2 => f} 2716\end{center} 2717are alternate ways to define \texttt{f} as the squaring function. 2718 2719You can similarly define functions of more than one variable. For 2720example, to define a function which takes the lengths of the two legs 2721of a right triangle and returns the hypotenuse, you could enter 2722\begin{center} 2723 {\tt hypot(a,b) := sqrt(a\^{}2 + b\^{}2)} 2724\end{center} 2725or 2726\begin{center} 2727 {\tt hypot := (a,b) -> sqrt(a\^{}2 + b\^{}2)} 2728\end{center} 2729 2730\subsection{Defining piecewise defined functions} 2731\index{piecewise defined functions} 2732\label{subsec:piecewise} 2733 2734You can use \texttt{Xcas}'s control structures to define functions not 2735given by a single simple formula. Notably, you can use the 2736\texttt{ifte\index{ifte}} command or \texttt{? :\index{? :}} operator 2737to define piecewise-defined functions. 2738 2739The \texttt{ifte} command takes three arguments; the first argument is 2740a condition, the second argument tells the command what to return when 2741the condition is true, and the third argument tells the command what 2742to return when the condition is false. For example, you could define 2743your own absolute value function with 2744\begin{center} 2745 {\tt myabs(x) := ifte(x >= 0, x -1*x)} 2746\end{center} 2747Afterwards, for example, entering 2748\begin{center} 2749 {\tt myabs(-4)} 2750\end{center} 2751will return 2752\begin{center} 2753 {\tt 4} 2754\end{center} 2755However, this will return an error if it can't evaluate the 2756conditional. For example, if you enter 2757\begin{center} 2758 {\tt myabs(x)} 2759\end{center} 2760you will get the error 2761\begin{center} 2762 {\tt Ifte: Unable to check test Error: Bad Argument Value} 2763\end{center} 2764 2765The \texttt{? :}\index{? :} construct behaves similarly to \texttt{ifte} but is 2766structured differently. Here, the condition comes first, followed by 2767\texttt{?}, then what to return if the condition is true, followed by 2768the \texttt{:}, and then what to return if the condition is false. 2769You could define your absolute value function with 2770\begin{center} 2771 {\tt myabs(x) := (x >= 0)? x: -1*x} 2772\end{center} 2773If you enter 2774\begin{center} 2775 {\tt myabs(-4)} 2776\end{center} 2777you will again get 2778\begin{center} 2779 {\tt 4} 2780\end{center} 2781but now if the conditional can't be evaluated, you won't get an error. 2782\begin{center} 2783 {\tt myabs(x)} 2784\end{center} 2785will return 2786\begin{center} 2787 {\tt ((x >= 0)? x: -x)} 2788\end{center} 2789 2790The \texttt{when\index{when}} and \texttt{IFTE\index{IFTE}} commands 2791are synonyms for the \texttt{? :} construct; 2792\begin{center} 2793 {\tt (\textit{condition})? \textit{true-result}: \textit{false-result}} 2794\end{center} 2795\begin{center} 2796 {\tt when(\textit{condition}, \textit{true-result}, \textit{false-result})} 2797\end{center} 2798and 2799\begin{center} 2800 {\tt IFTE(\textit{condition}, \textit{true-result}, \textit{false-result})} 2801\end{center} 2802all represent the same expression. 2803 2804If you want to define a function with several pieces, it may be 2805simpler to use the \texttt{piecewise\index{piecewise}} function. The 2806arguments to this function are alternately conditions and results to 2807return if the condition is true, with the last argument being what to 2808return if none of the conditions are true. For example, to define the 2809function given by 2810\[ 2811f(x) = 2812\begin{cases} 2813-2 & \text{if } x < -2\\ 28143x+4 & \text{if } -2 \le x < -1\\ 28151 & \text{if } -1 \le x < 0\\ 2816x + 1 & \text{if } x \ge 0 2817\end{cases} 2818\] 2819you can enter 2820\begin{center} 2821 {\tt f(x) := piecewise(x < -2, -2, x < -1, 3*x+4, x < 0, 1, x + 1)} 2822\end{center} 2823 2824\section{Directories} 2825\index{directories} 2826 2827\subsection{Working directories} 2828 2829\texttt{Xcas} has a working directory that it uses to store files that 2830it creates; typically the user's home directory. You can print the 2831name of the current working directory with the 2832\texttt{pwd()}\index{pwd} command; if you enter 2833\begin{center} 2834 {\tt pwd()} 2835\end{center} 2836you might get something like 2837\begin{center} 2838 {\tt /home/username} 2839\end{center} 2840 2841You can change the working directory with the \texttt{cd\index{cd}} 2842command; if you enter 2843\begin{center} 2844 {\tt cd("foo")} 2845\end{center} 2846or (on a Unix system) 2847\begin{center} 2848 {\tt cd("/home/username/foo")} 2849\end{center} 2850will change to the directory \texttt{foo}, if it exists. 2851Afterwards, any files that you save from \texttt{Xcas} will be in that 2852directory. 2853 2854If you have values saved in a file, then you'll need to be in that 2855working directory to load it. Note that if you have the same file 2856name in different directories, then the result of loading the file 2857name will depend on which directory you are in. 2858 2859\subsection{Reading files: \texttt{read load}}\index{read}\index{load} 2860 2861If you have a function or other \texttt{Xcas} information in a file, 2862you can load it with the \texttt{read} function. If the 2863file is named \texttt{myfunction.cxx}, then 2864\begin{center} 2865 {\tt read("myfunction.cxx")} 2866\end{center} 2867will load the file, as long as the directory is in the current working 2868directory. If the file is in a different directory, you can still 2869load it by giving the path to the file, 2870\begin{center} 2871 {\tt read("/path/to/file/myfunction.cxx")} 2872\end{center} 2873 2874While \texttt{read} can be used to load files containing \texttt{Xcas} 2875functions, which typically end in \texttt{.cxx}, if you want to load a 2876saved session you should use the \texttt{load} function; 2877\begin{center} 2878 {\tt load("mysession.cas")} 2879\end{center} 2880 2881\subsection{Internal directories: \texttt{NewFold SetFold GetFold DelFold VARS}}\index{NewFold}\index{SetFold}\index{GetFold}\index{DelFold}\index{VARS}\index{internal directories} 2882 2883You can create a directory that isn't actually on your hard drive but 2884is treated like one from \texttt{Xcas}. You can create such an 2885internal directory with the \texttt{NewFold} command, 2886which takes a variable name as an argument. If you enter 2887\begin{center} 2888 {\tt NewFold(MyIntDir)} 2889\end{center} 2890then there will be a new internal directory named \texttt{MyIntDir}. 2891Internal directories will also be listed with the 2892\texttt{VARS()} 2893command. To actually use this directory, you'll have to use the 2894\texttt{SetFold} command; 2895\begin{center} 2896 {\tt SetFold(MyIntDir)} 2897\end{center} 2898Finally, we can print out the internal directory that we are in with 2899the \texttt{GetFold} command; entering 2900\begin{center} 2901 {\tt GetFold()} 2902\end{center} 2903will result in 2904\begin{center} 2905 {\tt MyIntDir} 2906\end{center} 2907Afterwards, if this directory is empty, you can delete it with the 2908\texttt{DelFold} command; 2909\begin{center} 2910 {\tt DelFold(MyIntDir)} 2911\end{center} 2912 2913\chapter{The CAS functions}\label{sec:cas} 2914 2915\section{Symbolic constants : {\tt e pi infinity inf i euler\_gamma}} 2916\index{e}\index{\%e}\index{pi}\index{\%pi}\index{i}\index{\%i} 2917\index{+infinity}\index{-infinity}\index{infinity} 2918\index{euler\_gamma}\index{inf}\index{-inf} 2919 2920\noindent {\tt e} (or \texttt{\%e}) is the number $\exp(1)$;\\ 2921{\tt pi} (or \texttt{\%pi}) is the number $\pi$.\\ 2922{\tt infinity} is unsigned $\infty$.\\ 2923{\tt +infinity} or {\tt inf} is $+\infty$.\\ 2924{\tt -infinity} or {\tt -inf} is $-\infty$.\\ 2925{\tt i} (or \texttt{\%i}) is the complex number $i$.\\ 2926{\tt euler\_gamma} is Euler's constant $\gamma$; namely, 2927\texttt{limit(sum(1/k,k,1,n)-ln(n),n,+infinity)} 2928 2929\section{Booleans} 2930\label{sec:boolean} 2931\subsection{The values of a boolean : {\tt true false}}\index{true}\index{false}\index{TRUE}\index{FALSE} 2932The value of a boolean is {\tt true} or {\tt false}.\\ 2933The synonyms are :\\ 2934{\tt true} or {\tt TRUE} or {\tt 1},\\ 2935{\tt false} or {\tt FALSE} or {\tt 0}.\\ 2936Tests or conditions are boolean functions. 2937 2938\subsection{Tests : {\tt == != > >= < =<}}\index{==}\index{>}\index{<}\index{>=}\index{<=}\index{\symbol{33}=} 2939{\tt ==, !=, >, >=, <, =<} are infixed operators.\\ 2940{\tt a==b} tests the equality between {\tt a} and {\tt b} and returns {\tt 1} 2941if {\tt a} is equal to {\tt b} and {\tt 0} otherwise.\\ 2942{\tt a!=b} returns {\tt 1} if {\tt a} and {\tt b} are different and {\tt 0} 2943otherwise.\\ 2944 {\tt a>=b} returns {\tt 1} if {\tt a} is greater than or equal to {\tt b} 2945and {\tt 0} otherwise.\\ 2946{\tt a>b} returns {\tt 1} if {\tt a} is strictly greater than {\tt b} 2947and {\tt 0} otherwise.\\ 2948{\tt a<=b} returns {\tt 1} if {\tt a} is less than or equal to {\tt b} and 2949{\tt 0} otherwise.\\ 2950{\tt a<b} returns {\tt 1} if {\tt a} is strictly less than {\tt b} 2951and {\tt 0} otherwise.\\ 2952To write an algebraic function having the same result as an 2953{\tt if...then...else}, we use the boolean function {\tt ifte}.\\ 2954For example : 2955\begin{center}{\tt f(x):=ifte(x>0,true,false)}\end{center} 2956defines the boolean function $f$ such that {\tt f(x)= true} if 2957$x \in (0;+\infty[$ and {\tt f(x)=false} if $x \in (-\infty;0]$.\\ 2958Input : 2959\begin{center}{\tt f(0)==0}\end{center} 2960Output : 2961\begin{center}{\tt 1}\end{center} 2962{\bf Look out !}\\ 2963{\tt a=b} is not a boolean !!!!\\ 2964{\tt a==b} is a boolean.\\ 2965 2966\subsection{Boolean operators : {\tt or xor and not}}\index{or|textbf}\index{not|textbf}\index{and|textbf}\index{$\bigparallel$}\index{\&\&|textbf}\index{\symbol{33}=|textbf}\index{xor|textbf} 2967{\tt or} (or {\tt ||}), {\tt xor}, {\tt and} (or {\tt \&\&}) are infixed 2968operators.\\ 2969{\tt not} is a prefixed operators.\\ 2970If {\tt a} and {\tt b} are two booleans :\\ 2971{\tt (a or b)} {\tt (a || b)} returns {\tt 0} (or {\tt false}) if {\tt a} and 2972{\tt b} are equal to 0 and returns {\tt 1} (or {\tt true}) otherwise.\\ 2973{\tt (a xor b)} returns {\tt 1} if {\tt a} is equal to 1 and {\tt b} is 2974equal to 0 or if {\tt a} is equal to 0 and {\tt b} is equal to 1 and returns 0 2975 if {\tt a} and {\tt b} are equal to 0 2976 or if {\tt a} and {\tt b} are equal to 1 (it is the "exclusive or").\\ 2977{\tt (a and b)} or {\tt (a \&\& b)} returns {\tt 1} (or {\tt true}) if {\tt a} 2978 and {\tt b} are equal to 1 and {\tt 0} (or {\tt false}) otherwise.\\ 2979{\tt not(a)} returns {\tt 1} (or {\tt true}) if {\tt a} is equal to 0 (or 2980{\tt false}), and {\tt 0} (or {\tt false}) if {\tt a} is equal to 1 (or 2981{\tt true}).\\ 2982Input : 2983\begin{center}{\tt 1>=0 or 1<0}\end{center} 2984Output : 2985\begin{center}{\tt 1}\end{center} 2986Input : 2987\begin{center}{\tt 1>=0 xor 1>0}\end{center} 2988Output : 2989\begin{center}{\tt 0}\end{center} 2990Input : 2991\begin{center}{\tt 1>=0 and 1>0}\end{center} 2992Output : 2993\begin{center}{\tt 1}\end{center} 2994Input : 2995\begin{center}{\tt not(0==0)}\end{center} 2996Output : 2997\begin{center}{\tt 0}\end{center} 2998 2999\subsection{Transform a boolean expression to a list : {\tt exp2list}}\index{exp2list} 3000\noindent{\tt exp2list} returns the list {\tt [expr0,expr1]} when the argument 3001is {\tt (var=expr0) or (var=expr1)}.\\ 3002{\tt exp2list} is used in TI mode for easier processing of the answer to a 3003{\tt solve} command.\\ 3004Input : 3005\begin{center}{\tt exp2list((x=2) or (x=0))}\end{center} 3006Output : 3007\begin{center}{\tt [2,0]}\end{center} 3008Input : 3009\begin{center}{\tt exp2list((x>0) or (x<2))}\end{center} 3010Output : 3011\begin{center}{\tt [0,2]}\end{center} 3012In TI mode input : 3013\begin{center}{\tt exp2list(solve((x-1)*(x-2)))}\end{center} 3014Output : 3015\begin{center}{\tt [1,2]}\end{center} 3016 3017\subsection{Transform a list into a boolean expression: {\tt list2exp}}\index{list2exp} 3018\noindent 3019The \texttt{list2exp} command is the inverse of \texttt{exp2list}. It 3020takes two arguments; a list \texttt{[val1, val2, ...]} of values and a variable name 3021\texttt{var}.\\ 3022\texttt{list2exp} returns the boolean expression \texttt{((var = val1) 3023or (var = val2) or ...)}.\\ 3024Input: 3025\begin{center} 3026 \tt 3027 list2exp([0,1],a) 3028\end{center} 3029Output: 3030\begin{center} 3031 \tt 3032 ((a=0) or (a=1)) 3033\end{center} 3034Input: 3035\begin{center} 3036 \tt 3037 list2exp(solve(x\^{}2-1=0,x),x) 3038\end{center} 3039Output: 3040\begin{center} 3041 \tt 3042 ((x=-1) or (x=1)) 3043\end{center} 3044 3045Alternatively, each element of the list could be a list with $n$ values, followed by a 3046list of $n$ variables. The output would be boolean expressions of the 3047form \texttt{((var1 = val1) and (var2 = val2) ...)} for each 3048list of $n$ values, combined with \texttt{or}s. 3049Input: 3050\begin{center} 3051 \tt 3052 list2exp ([[3,9], [-1,1]], [x, y]) 3053\end{center} 3054Output: 3055\begin{center} 3056 \tt 3057 ((((x=3) and (y=9))) or (((x=-1) and (y=1)))) 3058\end{center} 3059 3060\subsection{Evaluate booleans : {\tt evalb}}\index{evalb} 3061\noindent Inside Maple, {\tt evalb} evaluates an boolean expression. 3062Since {\tt Xcas} evaluates booleans automatically, {\tt evalb} is only 3063here for compatibility and is equivalent to {\tt eval}\\ 3064Input : 3065\begin{center}{\tt evalb(sqrt(2)>1.41)}\end{center} 3066or : 3067\begin{center}{\tt sqrt(2)>1.41}\end{center} 3068Output : 3069\begin{center}{\tt 1}\end{center} 3070Input : 3071\begin{center}{\tt evalb(sqrt(2)>1.42)}\end{center} 3072or : 3073\begin{center}{\tt sqrt(2)>1.42}\end{center} 3074Output : 3075\begin{center}{\tt 0}\end{center} 3076 3077\section{Bitwise operators} 3078\subsection{Operators {\tt bitor bitxor bitand}}\index{bitor|textbf}\index{bitxor|textbf}\index{bitand|textbf} 3079The integers may be written using hexadecimal notation 0x... 3080for example 0x1f represents 16+15=31 in decimal. 3081Integers may also be output in hexadecimal notation 3082(click on the red CAS status button and select {\tt Base (Integers)}).\\ 3083{\tt bitor} is the logical inclusive {\tt or} (bitwise).\\ 3084Input : 3085\begin{center}{\tt bitor(0x12,0x38)}\end{center} 3086or : 3087\begin{center}{\tt bitor(18,56)}\end{center} 3088Output : 3089\begin{center}{\tt 58}\end{center} 3090because :\\ 3091{\tt 18} is written {\tt 0x12} in base 16 or {\tt 0b010010} in base 2,\\ 3092{\tt 56} is written {\tt 0x38} in base 16 or {\tt 0b111000} in base 2,\\ 3093hence {\tt bitor(18,56)} is {\tt 0b111010} in base 2 and so is equal to 3094{\tt 58}.\\ 3095 3096{\tt bitxor} is the logical exclusive {\tt or} (bitwise).\\ 3097Input : 3098\begin{center}{\tt bitxor(0x12,0x38)}\end{center} 3099or : 3100\begin{center}{\tt bitxor(18,56)}\end{center} 3101Output : 3102\begin{center}{\tt 42}\end{center} 3103because :\\ 3104{\tt 18} is written {\tt 0x12} in base 16 and {\tt 0b010010} in base 2,\\ 3105{\tt 56} is written {\tt 0x38} in base 16 and {\tt 0b111000} in base 2,\\ 3106{\tt bitxor(18,56)} is written {\tt 0b101010} in base 2 and so, is equal to 3107{\tt 42}.\\ 3108 3109{\tt bitand} is the logical {\tt and} (bitwise).\\ 3110Input : 3111\begin{center}{\tt bitand(0x12,0x38)}\end{center} 3112or : 3113\begin{center}{\tt bitand(18,56)}\end{center} 3114Output : 3115\begin{center}{\tt 16}\end{center} 3116because :\\ 3117{\tt 18} is written {\tt 0x12} in base 16 and {\tt 0b010010} in base 2,\\ 3118{\tt 56} is written {\tt 0x38} in base 16 and {\tt 0b111000} in base 2,\\ 3119{\tt bitand(18,56)} is written {\tt 0b010000} in base 2 and so is equal to 3120{\tt 16}. 3121 3122\subsection{Bitwise Hamming distance : {\tt hamdist}}\index{hamdist|textbf} 3123The Hamming distance is the number of differences 3124of the bits of the two arguments.\\ 3125Input : 3126\begin{center}{\tt hamdist(0x12,0x38)}\end{center} 3127or : 3128\begin{center}{\tt hamdist(18,56)}\end{center} 3129Output : 3130\begin{center}{\tt 3}\end{center} 3131because :\\ 3132{\tt 18} is written {\tt 0x12} in base 16 and {\tt 0b010010} in base 2,\\ 3133{\tt 56} is written {\tt 0x38} in base 16 and {\tt 0b111000} in base 2,\\ 3134{\tt hamdist(18,56)} is equal to {\tt 1+0+1+0+1+0} and so is equal to {\tt 3}. 3135 3136\section{Strings} 3137\subsection{Character and string : {\tt "}}\index{\symbol{34}|textbf} 3138\noindent {\tt "} is used to delimit a string. 3139A character is a string of length one.\\ 3140Do not confuse {\tt "} with 3141{\tt '} (or {\tt quote}) which is used to avoid evaluation 3142of an expression \index{quote}. For example, 3143{\tt "a"} returns a string of one character 3144but {\tt 'a'} or {\tt quote(a)} returns 3145the variable {\tt a} unevaluated.\\ 3146 3147When a string is input in a command line, it is evaluated to itself 3148hence the output is the same string. Use {\tt +} 3149to concatenate two strings or a string and another object.\\ 3150Example :\\ 3151Input : 3152\begin{center}{\tt "Hello"}\end{center} 3153{\tt "Hello"} is the input and also the output.\\ 3154Input : 3155\begin{center}{\tt "Hello"+", how are you?"}\end{center} 3156Output : 3157\begin{center}{\tt "Hello, how are you?"}\end{center} 3158Index notation is used to get the n-th character of a string, 3159(as for lists). Indices begin at 0 in Xcas mode, 1 in other modes.\\ 3160Example :\\ 3161Input : 3162\begin{center}{\tt "Hello"[1]}\end{center} 3163Output : 3164\begin{center}{\tt "e"}\end{center} 3165 3166\subsection{The newline character: \texttt{\symbol{92}n}}\index{\symbol{92}n} 3167 3168A newline can be inserted into a string with \texttt{\symbol{92}n}.\\ 3169Input: 3170\begin{center} 3171 \tt 3172 Hello\symbol{92}nHow are you? 3173\end{center} 3174Output: 3175\begin{center} 3176 \tt 3177 Hello\\ 3178 How are you? 3179\end{center} 3180 3181\subsection{The length of a string: \texttt{size length}}\index{size}\index{length} 3182The \texttt{size} (or \texttt{length}) command can take a string as an 3183argument. It will return the length of the string.\\ 3184Input: 3185\begin{center} 3186 \tt 3187 size("hello") 3188\end{center} 3189Output: 3190\begin{center} 3191 \tt 3192 5 3193\end{center} 3194 3195\subsection{The left and right parts of a string: \texttt{left right}}\index{left}\index{right} 3196 3197The \texttt{left} command takes two arguments, a string \texttt{s} and a 3198non-negative integer \texttt{n}.\\ 3199\texttt{left} returns the first \texttt{n} characters of the string.\\ 3200Input: 3201\begin{center} 3202 \tt 3203 left("hello",3) 3204\end{center} 3205Output: 3206\begin{center} 3207 \tt 3208 "hel" 3209\end{center} 3210 3211Similarly, the \texttt{right} command returns the last \texttt{n} 3212characters.\\\\ 3213Input: 3214\begin{center} 3215 \tt 3216 right("hello",4) 3217\end{center} 3218Output: 3219\begin{center} 3220 \tt 3221 "ello" 3222\end{center} 3223 3224\subsection{First character, middle and end of a string : {\tt head mid tail}}\index{head|textbf} \index{tail|textbf}\index{mid} 3225\begin{itemize} 3226\item {\tt head(s)} returns the first character of the string {\tt s}.\\ 3227 Input : 3228\begin{center}{\tt head("Hello")}\end{center} 3229Output : 3230\begin{center}{\tt "H"}\end{center} 3231\item {\tt mid(s,p,q)} returns the part of the string 3232{\tt s} of size {\tt q} beginning with the character at index {\tt p}.\\ 3233Remember that the first index is 0 in Xcas mode.\\ 3234Input : 3235\begin{center}{\tt mid("Hello",1,3)}\end{center} 3236Output : 3237\begin{center}{\tt "ell"}\end{center} 3238\item {\tt tail(s)} returns the string {\tt s} without its first character.\\ 3239Input : 3240\begin{center}{\tt tail("Hello")}\end{center} 3241Output : 3242\begin{center}{\tt "ello"}\end{center} 3243\end{itemize} 3244 3245\subsection{Concatenation of a sequence of words : {\tt cumSum}}\index{cumSum} 3246\noindent{\tt cumSum} works on strings like it does on expressions by 3247doing partial concatenation.\\ 3248{\tt cumSum} takes as argument a list of strings.\\ 3249{\tt cumSum} returns a list of strings where the element of index $k$ is the 3250concatenation of the strings with indices 0 to $k$ .\\ 3251Input : 3252\begin{center}{\tt cumSum("Hello, ","is ","that ","you?")}\end{center} 3253Output : 3254\begin{center}{\tt "Hello, ","Hello, is ","Hello, is that ","Hello, is that you?}\end{center} 3255 3256\subsection{ASCII code of a character : {\tt ord}}\index{ord|textbf} 3257\noindent {\tt ord} takes as argument a string {\tt s} (resp. 3258a list {\tt l} of 3259strings).\\ 3260{\tt ord} returns the ASCII code of the first character of {\tt s} (resp. the 3261list of the ASCII codes of the first character of the elements of {\tt l}).\\ 3262Input : 3263\begin{center}{\tt ord("a")}\end{center} 3264Output : 3265\begin{center}{\tt 97}\end{center} 3266Input : 3267\begin{center}{\tt ord("abcd")}\end{center} 3268Output : 3269\begin{center}{\tt 97}\end{center} 3270Input : 3271\begin{center}{\tt ord(["abcd","cde"])}\end{center} 3272Output : 3273\begin{center}{\tt [97,99]}\end{center} 3274Input : 3275\begin{center}{\tt ord(["a","b","c","d"])}\end{center} 3276Output : 3277\begin{center}{\tt [97,98,99,100]}\end{center} 3278 3279\subsection{ASCII code of a string : {\tt asc}}\index{asc} 3280\noindent {\tt asc} takes as argument a string {\tt s}.\\ 3281{\tt asc} returns the list of the ASCII codes of the characters of {\tt s}.\\ 3282Input : 3283\begin{center}{\tt asc("abcd")}\end{center} 3284Output : 3285\begin{center}{\tt [97,98,99,100]}\end{center} 3286Input : 3287\begin{center}{\tt asc("a")}\end{center} 3288Output : 3289\begin{center}{\tt [97]}\end{center} 3290 3291\subsection{String defined by the ASCII codes of its characters : {\tt char}}\index{char} 3292\noindent {\tt char} takes as argument a list {\tt l} of ASCII codes.\\ 3293{\tt char} returns the string whose characters have as ASCII codes the 3294elements of the list {\tt l}.\\ 3295Input : 3296\begin{center}{\tt char([97,98,99,100])}\end{center} 3297Output : 3298\begin{center}{\tt "abcd"}\end{center} 3299Input : 3300\begin{center}{\tt char(97)}\end{center} 3301Output : 3302\begin{center}{\tt "a"}\end{center} 3303Input : 3304\begin{center}{\tt char(353)}\end{center} 3305Output : 3306\begin{center}{\tt "a"}\end{center} 3307because:\\ 3308$353-256=97$. 3309 3310\subsection{Find a character in a string : {\tt inString}}\index{inString} 3311\noindent {\tt inString} takes two arguments : a string {\tt S} and a 3312character {\tt c}.\\ 3313{\tt inString} tests if the character {\tt c} is in the string {\tt S}.\\ 3314 {\tt inString} returns the index of its first occurrence 3315or {\tt -1} if {\tt c} is not in {\tt S}.\\ 3316Input : 3317\begin{center}{\tt inString("abcded","d")}\end{center} 3318Output : 3319\begin{center}{\tt 3}\end{center} 3320Input : 3321\begin{center}{\tt inString("abcd","e")}\end{center} 3322Output : 3323\begin{center}{\tt -1}\end{center} 3324 3325\subsection{Concat objects into a string : {\tt cat}}\index{cat|textbf} 3326\noindent {\tt cat} takes as argument a sequence of objects.\\ 3327{\tt cat} concatenates these objects into a string.\\ 3328Input : 3329\begin{center}{\tt cat("abcd",3,"d")}\end{center} 3330Output : 3331\begin{center}{\tt "abcd3d"}\end{center} 3332Input : 3333\begin{center}{\tt c:=5}\end{center} 3334\begin{center}{\tt cat("abcd",c,"e")}\end{center} 3335Output : 3336\begin{center}{\tt "abcd5e"}\end{center} 3337Input : 3338\begin{center}{\tt purge(c)}\end{center} 3339\begin{center}{\tt cat(15,c,3)}\end{center} 3340Output : 3341\begin{center}{\tt "15c3"}\end{center} 3342 3343\subsection{Add an object to a string : {\tt +}}\index{+} 3344\noindent {\tt +} is an infixed operator (resp. {\tt '+'} is a prefixed 3345operator).\\ 3346If {\tt +} (resp. {\tt '+'}) takes as argument a string (resp. 3347a sequence of objects with a string as first or second argument), 3348the result is the concatenation of these objects into a string.\\ 3349{\bf warning}\\ 3350{\tt +} is infixed and {\tt '+'} is prefixed.\\ 3351Input : 3352\begin{center}{\tt '+'("abcd",3,"d")}\end{center} 3353Output : 3354\begin{center}{\tt "abcd"+3+"d"}\end{center} 3355Output : 3356\begin{center}{\tt "abcd3d"}\end{center} 3357Input : 3358\begin{center}{\tt c:=5}\end{center} 3359Then input: 3360\begin{center}{\tt "abcd"+c+"e"}\end{center} 3361or : 3362\begin{center}{\tt '+'("abcd",c,"d")}\end{center} 3363Output : 3364\begin{center}{\tt "abcd5e"}\end{center} 3365 3366\subsection{Transform an integer into a string : {\tt cat +}}\index{+}\index{cat} 3367\noindent Use {\tt cat} with the integer as argument, or add the integer 3368to an empty string\\ 3369Input : 3370\begin{center}{\tt ""+123}\end{center} 3371or : 3372\begin{center}{\tt cat(123)}\end{center} 3373Output : 3374\begin{center}{\tt "123"}\end{center} 3375 3376\subsection{Transform a string into a number : {\tt expr}}\index{expr|textbf}\label{sec:expr1} 3377Use {\tt expr}, the parser with a string representing a number. 3378\begin{itemize} 3379\item For integers, enter the string representing the integer without 3380leading 0 for basis 10, with prefix {\tt 0x} for basis 16, 3381{\tt 0} for basis 8 or {\tt 0b} for basis 2. 3382Input : 3383\begin{center}{\tt expr("123")}\end{center} 3384Output : 3385\begin{center}{\tt 123}\end{center} 3386Input : 3387\begin{center}{\tt expr("0123")}\end{center} 3388Output : 3389\begin{center}{\tt 83}\end{center} 3390because :\\ 3391$1*8^2+2*8+3=83$\\ 3392Input : 3393\begin{center}{\tt expr("0x12f")}\end{center} 3394Output : 3395\begin{center}{\tt 303}\end{center} 3396Because : $1*16^2+2*16+15=303$ 3397\item For decimal numbers, use a string with a {\tt .} or {\tt e} inside.\\ 3398Input : 3399\begin{center}{\tt expr("123.4567")}\end{center} 3400Output : 3401\begin{center}{\tt 123.4567}\end{center} 3402Input : 3403\begin{center}{\tt expr("123e-5")}\end{center} 3404Output : 3405\begin{center}{\tt 0.00123}\end{center} 3406\item Note that {\tt expr} more generally transforms a string 3407into a command if the command exists.\\ 3408Input : 3409\begin{center}{\tt expr("a:=1")}\end{center} 3410Output : 3411\begin{center}{\tt 1}\end{center} 3412Then, input : 3413\begin{center}{\tt a}\end{center} 3414Output : 3415\begin{center}{\tt 1}\end{center} 3416\end{itemize} 3417 3418\section{Write an integer in base $b$: {\tt convert}}\index{convert}\index{base@{\sl base}|textbf} 3419\label{sec:convertbase} 3420 3421\noindent{\tt convert} or {\tt convertir} can do different kind 3422of conversions depending on the option given as the second argument. 3423 3424To convert an integer {\tt n} into the list of its coefficients in 3425base {\tt b}, the option is {\tt base}. The arguments of {\tt convert} or 3426{\tt convertir} are an integer {\tt n}, {\tt base} and {\tt b}, the value of the 3427 basis.\\ 3428{\tt convert} or {\tt convertir} returns the list of coefficients in a {\tt b} 3429basis of the integer {\tt n}.\\ 3430Input : 3431\begin{center}{\tt convert(123,base,8)}\end{center} 3432Output : 3433\begin{center}{\tt [3,7,1]}\end{center} 3434To check the answer, 3435input {\tt expr("0173")} or {\tt horner(revlist([3,7,1]),8)} 3436or {\tt convert([3,7,1],base,8)}, the output is {\tt 123}\\ 3437Input : 3438\begin{center}{\tt convert(142,base,12)}\end{center} 3439Output : 3440\begin{center}{\tt [10,11]}\end{center} 3441 3442To convert the list of coefficients of an integer {\tt n} in base {\tt b}, 3443the option is also {\tt base}. 3444{\tt convert} or {\tt convertir} returns the integer {\tt n}.\\ 3445Input : 3446\begin{center}{\tt convert([3,7,1],base,8)}\end{center} 3447or : 3448\begin{center}{\tt horner(revlist([3,7,1]),8)}\end{center} 3449Output : 3450\begin{center}{\tt 123}\end{center} 3451Input : 3452\begin{center}{\tt convert([10,11],base,12)}\end{center} 3453or : 3454\begin{center}{\tt horner(revlist([10,11]),12)}\end{center} 3455Output : 3456\begin{center}{\tt 142}\end{center} 3457 3458\section{Integers (and Gaussian Integers)} 3459 For all functions in this section, you can use Gaussian integers (numbers of 3460the form $a+ib$, where $a$ and $b$ are in $\mathbb Z$) in place of integers. 3461 3462\subsection{The factorial : {\tt factorial}}\index{factorial} 3463{\tt Xcas} can manage integers with unlimited precision, such as the 3464following:\\ 3465Input : 3466\begin{center}{\tt factorial(100)}\end{center} 3467Output : 3468\begin{verbatim} 3469 9332621544394415268169923885626670049071596826438162 3470 1468592963895217599993229915608941463976156518286253 3471 697920827223758251185210916864000000000000000000000000 3472\end{verbatim} 3473\subsection{GCD : {\tt gcd igcd}}\index{gcd|textbf}\index{igcd|textbf}\label{sec:igcd} 3474\noindent{\tt gcd} or {\tt igcd} denotes the gcd (greatest common divisor) 3475of several integers (for polynomials, see also \ref{sec:gcd}).\\ 3476{\tt gcd} or {\tt igcd} returns the {\tt GCD} of integers.\\ 3477Input : 3478\begin{center}{\tt gcd(18,15)}\end{center} 3479Output : 3480\begin{center}{\tt 3}\end{center} 3481Input : 3482\begin{center}{\tt gcd(18,15,21,36) }\end{center} 3483Output : 3484\begin{center}{\tt 3}\end{center} 3485Input : 3486\begin{center}{\tt gcd([18,15,21,36])}\end{center} 3487Output : 3488\begin{center}{\tt 3}\end{center} 3489We can also put as parameters two lists of same size (or a matrix with 2 3490rows), in this case {\tt gcd} returns the greatest common divisor of 3491the elements with same index (or in the same column).\\ 3492Input : 3493\begin{center}{\tt gcd([6,10,12],[21,5,8])}\end{center} 3494or : 3495\begin{center}{\tt gcd([[6,10,12],[21,5,8]])}\end{center} 3496Output : 3497\begin{center}{\tt [3,5,4]}\end{center} 3498{\bf An example}\\ 3499Find the greatest common divisor of $4n+1$ and $5n+3$ when $n \in \mathbb N$.\\ 3500Input :\\ 3501\begin{center}{\tt f(n):=gcd(4*n+1,5*n+3)}\end{center} 3502Then, input :\\ 3503\begin{verbatim} 3504 essai(n):={ 3505 local j,a,L; 3506 L:=NULL; 3507 for (j:=-n;j<n;j++) { 3508 a:=f(j); 3509 if (a!=1) { 3510 L:=L,[j,a]; 3511 } 3512 } 3513 return L; 3514 } 3515\end{verbatim} 3516Then, input :\\ 3517\begin{center}{\tt essai(20)}\end{center} 3518Output : 3519\begin{center}{\tt [-16,7],[-9,7],[-2,7],[5,7],[12,7],[19,7]}\end{center} 3520So we now have to prove that :\\ 3521If $n\not=5+k*7$ (for $k \in \mathbb Z$), $4n+1$ and $5n+3$ are mutually prime, 3522and $n=5+k*7$ (for $k \in \mathbb Z$), then the greatest common divisor of $4n+1$ 3523and $5n+3$ is 7. 3524\subsection{GCD : {\tt Gcd}}\index{Gcd|textbf} 3525\noindent{\tt Gcd} is the inert form of {\tt gcd}. See the section 3526\ref{sec:gcd} for polynomials with coefficients in $\Z/p\Z$ 3527 for using this instruction.\\ 3528Input : 3529\begin{center}{\tt Gcd(18,15)}\end{center} 3530Output : 3531\begin{center}{\tt gcd(18,15)}\end{center} 3532 3533 3534\subsection{GCD of a list of integers : {\tt lgcd}}\index{lgcd} 3535\noindent{\tt lgcd} has a list of integers (or of a list of polynomials) 3536as argument.\\ 3537{\tt lgcd} returns the {\tt gcd} of all integers of the list (or 3538the {\tt gcd} of all polynomials of the list).\\ 3539Input : 3540\begin{center}{\tt lgcd([18,15,21,36])}\end{center} 3541Output : 3542\begin{center}{\tt 3}\end{center} 3543{\bf Remark}\\ 3544{\tt lgcd} does not accept two lists (even if they have the same size) 3545as arguments. 3546 3547\subsection{The least common multiple : {\tt lcm}}\index{lcm|textbf}\label{sec:ilcm} 3548\noindent {\tt lcm} returns the least common multiple of two integers (or of 3549two polynomials, see also \ref{sec:lcm}).\\ 3550Input : 3551\begin{center}{\tt lcm(18,15) }\end{center} 3552Output : 3553\begin{center}{\tt 90}\end{center} 3554 3555\subsection{Decomposition into prime factors : {\tt ifactor}}\index{ifactor} 3556\noindent{\tt ifactor} has an integer as parameter.\\ 3557{\tt ifactor} decomposes an integer into its prime factors.\\ 3558Input : 3559\begin{center}{\tt ifactor(90) }\end{center} 3560Output : 3561\begin{center}{\tt 2*3\verb|^|2*5}\end{center} 3562Input : 3563\begin{center}{\tt ifactor(-90) }\end{center} 3564Output : 3565\begin{center}{\tt (-1)*2*3\verb|^|2*5}\end{center} 3566 3567\subsection{List of prime factors : {\tt ifactors}}\index{ifactors} 3568\noindent{\tt ifactors} has an integer (or a list of integers) as parameter.\\ 3569{\tt ifactors} decomposes the integer (or the integers of the list) into prime 3570factors, but the result 3571 is given as a list (or a list of lists) in which each prime factor is 3572followed by its multiplicity.\\ 3573Input : 3574\begin{center}{\tt ifactors(90) }\end{center} 3575Output : 3576\begin{center}{\tt [2,1,3,2,5,1] }\end{center} 3577Input : 3578\begin{center}{\tt ifactors(-90) }\end{center} 3579Output : 3580\begin{center}{\tt [-1,1,2,1,3,2,5,1] }\end{center} 3581Input : 3582\begin{center}{\tt ifactor([36,52]) }\end{center} 3583Output : 3584\begin{center}{\tt [[2,2,3,2],[2,2,13,1]]}\end{center} 3585\subsection{Matrix of factors : {\tt maple\_ifactors}}\index{maple\_ifactors} 3586\noindent{\tt maple\_ifactors} has an integer $n$ (or a list of integers) 3587as parameter.\\ 3588{\tt maple\_ifactors} decomposes the integer (or the integers of the list) into 3589prime factors, but the output follows the Maple syntax :\\ 3590it is a list with +1 or -1 (for the sign) and a matrix with 2 columns and 3591 where the lines are the prime factors and their multiplicity (or a list of 3592lists...).\\ 3593Input : 3594\begin{center}{\tt maple\_ifactors(90) }\end{center} 3595Output : 3596\begin{center}{\tt [1,[[2,1],[3,2],[5,1]]]}\end{center} 3597Input : 3598\begin{center}{\tt maple\_ifactor([36,52]) }\end{center} 3599Output : 3600\begin{center}{\tt [[1,[[2,2],[3,2]]],[1,[[2,2],[13,1]]]]}\end{center} 3601 3602\subsection{The divisors of a number : {\tt idivis divisors}} \index{idivis}\index{divisors} 3603\noindent{\tt idivis} or {\tt divisors} gives the list of the divisors of a 3604number (or of a list of numbers).\\ 3605Input : 3606\begin{center}{\tt idivis(36) }\end{center} 3607Output : 3608\begin{center}{\tt [1,2,4,3,6,12,9,18,36] }\end{center} 3609Input : 3610\begin{center}{\tt idivis([36,22]) }\end{center} 3611Output : 3612\begin{center}{\tt [[1,2,4,3,6,12,9,18,36],[1,2,11,22]]}\end{center} 3613 3614\subsection{The integer Euclidean quotient : {\tt iquo intDiv div}}\index{iquo}\index{intDiv}\index{div} 3615\noindent{\tt iquo} (or {\tt intDiv}) returns the integer quotient $q$ of the 3616Euclidean division of two integers $a$ and $b$ given as arguments. 3617($a=b*q+r$ with $0\leq r< b$).\\ 3618For Gaussian integers, we choose $q$ so that $b*q$ is as near by $a$ as 3619possible and it can be proved that $r$ may be chosen so that 3620$|r|^2 \leq |b|^2/2$.\\ 3621Input : 3622\begin{center}{\tt iquo(148,5) }\end{center} 3623Output : 3624\begin{center}{\tt 29}\end{center} 3625{\tt iquo} works with integers or with Gaussian integers.\\ 3626Input : 3627\begin{center}{\tt iquo(factorial(148),factorial(145)+2 )}\end{center} 3628Output : 3629\begin{center}{\tt 3176375}\end{center} 3630Input : 3631\begin{center}{\tt iquo(25+12*i,5+7*i) }\end{center} 3632Output : 3633\begin{center}{\tt 3-2*i}\end{center} 3634Here $a-b*q=-4+i$ and $|-4+i|^2=17<|5+7*i|^2/2=74/2=37$ 3635 3636The infixed version of this command is \texttt{div}.\\ 3637Input: 3638\begin{center} 3639 \tt 3640 148 div 5 3641\end{center} 3642Output: 3643\begin{center} 3644 \tt 3645 29 3646\end{center} 3647 3648\subsection{The integer Euclidean remainder : {\tt irem remain smod mods mod \%}}\index{irem}\index{remain} 3649\noindent{\tt irem} (or {\tt remain}) returns the integer remainder $r$ from 3650the Euclidean division of two integers $a$ and $b$ given as arguments 3651($a=b*q+r$ with $0\leq r< b$).\\ 3652For Gaussian integers, we choose $q$ so that $b*q$ is as near to $a$ as 3653possible and it can be proved that $r$ may be chosen so that 3654$|r|^2 \leq |b|^2/2$.\\ 3655Input : 3656\begin{center}{\tt irem(148,5) }\end{center} 3657Output : 3658\begin{center}{\tt 3}\end{center} 3659{\tt irem} works with long integers or with Gaussian integers.\\ 3660Example : 3661\begin{center}{\tt irem(factorial(148),factorial(45)+2 )}\end{center} 3662Output : 3663\begin{center}{\tt 111615339728229933018338917803008301992120942047239639312}\end{center} 3664Another example 3665\begin{center}{\tt irem(25+12*i,5+7*i) }\end{center} 3666Output : 3667\begin{center}{\tt -4+i}\end{center} 3668Here $a-b*q=-4+i$ and $|-4+i|^2=17<|5+7*i|^2/2=74/2=37$ 3669 3670{\tt smod} or {\tt mods}\index{smod|textbf}\index{mods|textbf} is a prefixed 3671function and has two integers $a$ and $b$ as arguments.\\ 3672{\tt smod} or {\tt mods} returns the 3673symmetric remainder $s$ of the Euclidean division of the 3674arguments $a$ and $b$ ($a=b*q+s$ with $-b/2<s \leq b/2$).\\ 3675Input : 3676\begin{center}{\tt smod(148,5) }\end{center} 3677Output : 3678\begin{center}{\tt -2}\end{center} 3679 3680{\tt mod} (or {\tt \%}) is an infixed function 3681and has two integers $a$ and $b$ 3682as arguments.\\ 3683{\tt mod} (or {\tt \%}) returns $r\% b$ of $Z/bZ$ where $r$ is the remainder of 3684the Euclidean division of the arguments $a$ and $b$.\\ 3685Input :\index{mod}\index{\%} 3686\begin{center}{\tt 148\ mod\ 5 }\end{center} 3687or : 3688\begin{center}{\tt 148 \% 5 }\end{center} 3689Output : 3690\begin{center}{\tt 3 \% 5}\end{center} 3691Note that the answer {\tt 3 \% 5} is not an integer (3) but 3692an element of $Z/5Z$ (see \ref{sec:modulaire} to have 3693the possible operations in $Z/5Z$). 3694 3695\subsection{Euclidean quotient and euclidean remainder of two integers : {\tt iquorem}}\index{iquorem}\label{sec:iquorem} 3696\noindent{\tt iquorem} returns the list of the quotient $q$ and the 3697remainder $r$ of the Euclidean division between two integers $a$ and $b$ given 3698as arguments ($a=b*q+r$ with $0\leq r< b$).\\ 3699Input : 3700\begin{center}{\tt iquorem(148,5) }\end{center} 3701Output : 3702\begin{center}{\tt [29,3] }\end{center} 3703 3704\subsection{Test of evenness : {\tt even}}\index{even} 3705\noindent {\tt even} takes as argument an integer {\tt n}.\\ 3706{\tt even} returns {\tt 1} if {\tt n} is even and returns {\tt 0} if {\tt n} 3707is odd.\\ 3708Input : 3709\begin{center}{\tt even(148) }\end{center} 3710Output : 3711\begin{center}{\tt 1 }\end{center} 3712Input : 3713\begin{center}{\tt even(149) }\end{center} 3714Output : 3715\begin{center}{\tt 0}\end{center} 3716 3717 3718\subsection{Test of oddness : {\tt odd}}\index{odd} 3719\noindent {\tt odd} takes as argument an integer {\tt n}.\\ 3720{\tt odd} returns {\tt 1} if {\tt n} is odd and returns {\tt 0} if {\tt n} is 3721even.\\ 3722Input : 3723\begin{center}{\tt odd(148) }\end{center} 3724Output : 3725\begin{center}{\tt 0 }\end{center} 3726Input : 3727\begin{center}{\tt odd(149) }\end{center} 3728Output : 3729\begin{center}{\tt 1}\end{center} 3730 3731\subsection{Test of pseudo-primality : {\tt is\_pseudoprime}}\index{is\_pseudoprime} 3732\noindent If {\tt is\_pseudoprime(n)} returns {\tt 2} (true), then 3733{\tt n} is prime.\\ 3734If it returns 1, then {\tt n} is pseudo-prime (most 3735probably prime).\\ 3736 If it returns 0, then {\tt n} is not prime. \\ 3737{\sc Definition}: For numbers less than $10^{14}$, pseudo-prime and prime 3738are equivalent. But for numbers greater than $10^{14}$, a pseudo-prime 3739 is a number with a large probability of being prime (cf. Rabin's Algorithm and 3740Miller-Rabin's Algorithm in the Algorithmic part (menu 3741{\tt Help->Manuals->Programming})).\\ 3742Input : 3743\begin{center}{\tt is\_pseudoprime(100003) }\end{center} 3744Output : 3745\begin{center}{\tt 2}\end{center} 3746Input : 3747\begin{center}{\tt is\_pseudoprime(9856989898997) }\end{center} 3748Output : 3749\begin{center}{\tt 2}\end{center} 3750Input : 3751\begin{center}{\tt is\_pseudoprime(14) }\end{center} 3752Output : 3753\begin{center}{\tt 0}\end{center} 3754Input : 3755\begin{center}{\tt is\_pseudoprime(9856989898997789789) }\end{center} 3756Output : 3757\begin{center}{\tt 1}\end{center} 3758 3759\subsection{Test of primality : {\tt is\_prime isprime isPrime}} 3760\index{is\_prime} 3761\index{is\_Prime} 3762 3763\noindent {\tt is\_prime(n)} returns {\tt 1} (true) if {\tt n} is prime and 3764{\tt 0} (false) if {\tt n} is not prime.\\ 3765{\tt isprime} returns {\tt true} or {\tt false}.\\ 3766Use the command {\tt pari("isprime",n,1)} 3767to have a primality certificate (see the documentation 3768 PARI/GP with the menu {\tt Help->Manuals->PARI-GP}) and 3769{\tt pari("isprime",n,2)} to use the APRCL test. 3770 3771Input : 3772\begin{center}{\tt is\_prime(100003)}\end{center} 3773Output : 3774\begin{center}{\tt 1}\end{center} 3775Input : 3776\begin{center}{\tt isprime(100003)}\end{center} 3777Output : 3778\begin{center}{\tt true}\end{center} 3779Input : 3780\begin{center}{\tt is\_prime(98569898989987)}\end{center} 3781Output : 3782\begin{center}{\tt 1}\end{center} 3783Input : 3784\begin{center}{\tt is\_prime(14)}\end{center} 3785Output : 3786\begin{center}{\tt 0}\end{center} 3787Input : 3788\begin{center}{\tt isprime(14)}\end{center} 3789Output : 3790\begin{center}{\tt false}\end{center} 3791Input : 3792\begin{center}{\tt pari("isprime",9856989898997789789,1)}\end{center} 3793This returns the coefficients giving the proof of primality by the 3794$p-1$ Selfridge-Pocklington-Lehmer test~: 3795\begin{center} 3796{\tt [[2,2,1],[19,2,1],[941,2,1],[1873,2,1],[94907,2,1]]} 3797\end{center} 3798Input : 3799\begin{center}{\tt isprime(9856989898997789789)}\end{center} 3800Output : 3801\begin{center}{\tt true}\end{center} 3802 3803\subsection{The smallest pseudo-prime greater than {\tt n} : {\tt nextprime}}\index{nextprime} 3804\noindent{\tt nextprime(n)} returns the smallest pseudo-prime (or prime) 3805greater than {\tt n}. \\ 3806Input : 3807\begin{center}{\tt nextprime(75) }\end{center} 3808Output : 3809\begin{center}{\tt 79}\end{center} 3810 3811\subsection{The greatest pseudo-prime less than {\tt n} : {\tt prevprime}}\index{prevprime} 3812\noindent{\tt prevprime(n)} returns the greatest pseudo-prime (or prime) less 3813than {\tt n}.\\ 3814Input : 3815\begin{center}{\tt prevprime(75)}\end{center} 3816Output : 3817\begin{center}{\tt 73}\end{center} 3818 3819\subsection{The {\tt n}-th pseudo-prime number : {\tt ithprime}}\index{ithprime} 3820\noindent{\tt ithprime(n)} returns the {\tt n}-th pseudo-prime 3821number.\\ 3822Input : 3823\begin{center}{\tt ithprime(75)}\end{center} 3824Output : 3825\begin{center}{\tt 379}\end{center} 3826Input : 3827\begin{center}{\tt ithprime(k) \$ (k=1..20) }\end{center} 3828Output : 3829\begin{center}{\tt 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71}\end{center} 3830 3831\subsection{The number of pseudo-primes less than or equal to {\tt n}: {\tt nprimes}}\index{nprimes} 3832\noindent{\tt nprimes(n)} returns the number of pseudo-primes (or primes) less 3833than or equal to {\tt n}.\\ 3834Input : 3835\begin{center}{\tt nprimes(5)}\end{center} 3836Output : 3837\begin{center}{\tt 3}\end{center} 3838Input : 3839\begin{center}{\tt nprimes(10)}\end{center} 3840Output : 3841\begin{center}{\tt 4}\end{center} 3842 3843\subsection{B\'ezout's Identity : {\tt iegcd igcdex}} 3844\index{iegcd}\index{igcdex} 3845 3846\noindent{\tt iegcd(a,b)} or {\tt igcdex(a,b)} 3847returns the coefficients of the B\'ezout's Identity for two integers given 3848as arguments.\\ 3849{\tt iegcd(a,b)} or {\tt igcdex(a,b)} returns {\tt [u,v,d]} such that 3850{\tt au+bv=d} and {\tt d=gcd(a,b)}.\\ 3851Input : 3852\begin{center}{\tt iegcd(48,30) }\end{center} 3853Output : 3854\begin{center}{\tt [2,-3,6]}\end{center} 3855In other words : 3856$$2 \cdot 48+ (-3) \cdot 30 =6$$ 3857 3858\subsection{Solving au+bv=c in $\Z$: {\tt iabcuv}}\index{iabcuv} 3859\noindent{\tt iabcuv(a,b,c)} returns {\tt [u,v]} so that {\tt au+bv=c}.\\ 3860{\tt c} must be a multiple of {\tt gcd(a,b)} for the existence of 3861a solution.\\ 3862Input : 3863\begin{center}{\tt iabcuv(48,30,18) }\end{center} 3864Output : 3865\begin{center}{\tt [6,-9]}\end{center} 3866 3867\subsection{Chinese remainders : {\tt ichinrem ichrem}}\index{ichinrem}\index{ichrem} 3868\noindent{\tt ichinrem([a,p],[b,q])} or {\tt ichrem([a,p],[b,q])} returns a 3869list {\tt [c,lcm(p,q)]} of 2 integers.\\ 3870The first number {\tt c} is such that 3871\[ \forall k \in \mathbb Z, \quad d=c+ k \times \mbox{lcm}(p,q) \] 3872has the properties 3873\[ d=a \pmod p, \quad d=b \pmod q \] 3874If {\tt p} and {\tt q} are coprime, a solution {\tt d} always exists 3875and all the solutions are congruent modulo {\tt p*q}.\\ 3876{\bf Examples} : \\ 3877Solve : 3878$${\tt \left \{ \begin{array}{rcl} x&=&3\ (\bmod\ 5)\\ 3879x&=&9\ (\bmod\ 13) \end{array}\right.}$$ 3880Input : 3881\begin{center}{\tt ichinrem([3,5],[9,13])}\end{center} 3882or : 3883\begin{center}{\tt ichrem([3,5],[9,13])}\end{center} 3884Output : 3885\begin{center}{\tt [-17,65] }\end{center} 3886so {\tt x=-17 (mod 65)}\\ 3887We can also input : 3888\begin{center}{\tt ichrem(3\%5,9\%13)}\end{center} 3889Output : 3890\begin{center}{\tt -17\%65 }\end{center} 3891Solve : 3892$${\tt \left \{ \begin{array}{rcl} x&=&3\ (\bmod\ 5)\\ 3893x&=&4\ (\bmod\ 7) \\ 3894x&=&1\ (\bmod\ 9)\end{array}\right.}$$ 3895First input : 3896\begin{center}{\tt tmp:=ichinrem([3,5],[4,7])}\end{center} 3897or : 3898\begin{center}{\tt tmp:=ichrem([3,5],[4,7])}\end{center} 3899Output : 3900\begin{center}{\tt [-17,35] }\end{center} 3901Then input : 3902\begin{center}{\tt ichinrem([1,9],tmp)}\end{center} 3903or : 3904\begin{center}{\tt ichrem([1,9],tmp)}\end{center} 3905Output : 3906\begin{center}{\tt [-17,315] }\end{center} 3907hence {\tt x=-17 (mod 315)}\\ 3908Alternative input:\\ 3909\begin{center}{\tt ichinrem([3\%5,4\%7,1\%9])}\end{center} 3910Output : 3911\begin{center}{\tt -17\%315 }\end{center} 3912 3913{\bf Remark}\\ 3914{\tt ichrem} (or{\tt ichinrem})may be used to find the coefficients of a polynomial 3915whose equivalence classes are known modulo several integers, for example find 3916$ax+b$ modulo $315=5 \times 7 \times 9$ under the assumptions: 3917$${\tt \left \{ \begin{array}{rl} a=&3\ (\bmod\ 5)\\ 3918a=&4\ (\bmod\ 7) \\ 3919a=&1\ (\bmod\ 9) \end{array}\right.}, 3920\quad 3921{\tt \left \{ \begin{array}{rl} b=&1\ (\bmod\ 5)\\ 3922b=&2\ (\bmod\ 7) \\ 3923b=&3\ (\bmod\ 9) \end{array}\right.}$$ 3924Input : 3925\begin{center}{\tt ichrem((3x+1)\%5,(4x+2)\%7,(x+3)\%9)}\end{center} 3926Output : 3927\begin{center}{\tt (-17\%315$\times$ x+156\%315 }\end{center} 3928hence {\tt a=-17 (mod 315)} and {\tt b=156 (mod 315)}. 3929 3930\subsection{Chinese remainders for lists of integers : {\tt chrem}}\index{chrem} 3931\noindent{\tt chrem} takes as argument 2 lists of integers of the same size.\\ 3932{\tt chrem} returns a list of 2 integers.\\ 3933For example, {\tt chrem([a,b,c],[p,q,r])} returns the list 3934{\tt [x,lcm(p,q,r)]} where 3935{\tt x=a mod p} and {\tt x=b mod q} and {\tt x=c mod r}.\\ 3936A solution {\tt x} always exists if {\tt p, q, r} 3937are mutually primes, and all the solutions are equal modulo {\tt p*q*r}. \\ 3938{\sc Be careful} with the order of the parameters, indeed :\\ 3939{\tt chrem([a,b],[p,q])=ichrem([a,p],[b,q])=\\ 3940ichinrem([a,p],[b,q])}\\ 3941{\bf Examples} : \\ 3942Solve : 3943$${\tt \left \{ \begin{array}{rl} x=&3\ (\bmod\ 5)\\ 3944x=&9\ (\bmod\ 13) \end{array}\right.}$$ 3945Input : 3946\begin{center}{\tt chrem([3,9],[5,13])}\end{center} 3947Output : 3948\begin{center}{\tt [-17,65] }\end{center} 3949so, {\tt x=-17 (mod 65)}\\ 3950Solve : 3951$${\tt \left \{ \begin{array}{rl} x=&3\ (\bmod\ 5)\\ 3952x=&4\ (\bmod\ 6) \\ 3953x=&1\ (\bmod\ 9)\end{array}\right.}$$ 3954Input : 3955\begin{center}{\tt chrem([3,4,1],[5,6,9])}\end{center} 3956Output : 3957\begin{center}{\tt [28,90] }\end{center} 3958so {\tt x=28 (mod 90)}\\ 3959{\bf Remark}\\ 3960{\tt chrem} may be used to find the coefficients of a polynomial whose 3961equivalence classes are known modulo several integers, for example find 3962$ax+b$ modulo $315=5 \times 7 \times 9$ under the assumptions: 3963$${\tt \left \{ \begin{array}{rl} a=&3\ (\bmod\ 5)\\ 3964a=&4\ (\bmod\ 7) \\ 3965a=&1\ (\bmod\ 9) \end{array}\right.}, \quad 3966{\tt \left \{ \begin{array}{rl} b=&1\ (\bmod\ 5)\\ 3967b=&2\ (\bmod\ 7) \\ 3968b=&3\ (\bmod\ 9) \end{array}\right.}$$ 3969Input : 3970\begin{center}{\tt chrem([3x+1,4x+2,x+3],[5,7,9])}\end{center} 3971Output : 3972\begin{center}{\tt [-17x+156,315] }\end{center} 3973hence, {\tt a=-17 (mod 315)} and {\tt b=156 (mod 315)}. 3974 3975\subsection{Solving $a^2+b^2=p$ in $\Z$ : {\tt pa2b2}}\index{pa2b2} 3976\noindent{\tt pa2b2} decompose a prime integer $p$ congruent to 1 modulo 4, 3977as a sum of squares : $p= a^2+b^2$. 3978The result is the list {\tt [a,b]}.\\ 3979Input : 3980\begin{center}{\tt pa2b2(17)}\end{center} 3981Output : 3982\begin{center}{\tt [4,1] }\end{center} 3983indeed $17=4^2+1^2$ 3984 3985\subsection{The Euler indicatrix : {\tt euler phi}}\index{euler}\index{phi} 3986\noindent{\tt euler} (or {\tt phi}) returns the Euler indicatrix 3987for a integer. \\ 3988{\tt euler(n)} (or {\tt phi(n)}) is equal to the number of integers less 3989than {\tt n} and prime with {\tt n}. \\ 3990Input : 3991\begin{center}{\tt euler(21)}\end{center} 3992Output : 3993\begin{center}{\tt 12}\end{center} 3994In other words 3995 E=\{2,4,5,7,8,10,11,13,15,16,17,19\} is the set of integers less than 21 3996and coprime with 21. There are 12 members in this set, hence Cardinal(E)=12. 3997 3998Euler has introduced this function to generalize the little Fermat theorem:\\ 3999\centerline{If $a$ and $n$ are mutually prime then $a^{euler(n)}=1\ \bmod \ n$} 4000 4001\subsection{Legendre symbol : {\tt legendre\_symbol}}\index{legendre\_symbol} 4002If $n$ is prime, we define the Legendre symbol of $a$ 4003written $\left(\frac{a}{n}\right)$ by :\\ 4004$$\left(\frac{a}{n}\right)=\left\{\begin{array}{rl} 40050 & \mbox{if }a=0\ \bmod n \\ 40061 & \mbox{if } a \neq 0 \bmod n \mbox{ and if } a=b^2 \bmod n\\ 4007-1 & \mbox{if } a \neq 0 \bmod n \mbox{ and if } a \neq b^2 \bmod n\\ 4008\end{array} 4009\right.$$ 4010Some properties 4011\begin{itemize} 4012\item 4013If $n$ is prime : 4014\[ a^{\frac{n-1}{2}}=\left(\frac{a}{n}\right) \bmod n \] 4015\item 4016\begin{eqnarray*} 4017\left(\frac{p}{q}\right).\left(\frac{q}{p}\right) 4018&=&(-1)^{\frac{p-1}{2}}.(-1)^{\frac{q-1}{2}} 4019\mbox{ if $p$ and $q$ are odd and positive} \\ 4020\left(\frac{2}{p}\right)&=&(-1)^{\frac{p^2-1}{8}} \\ 4021\left(\frac{-1}{p}\right)&=&(-1)^{\frac{p-1}{2}} 4022\end{eqnarray*} 4023\end{itemize} 4024{\tt legendre\_symbol} takes two arguments $a$ and $n$ and returns the Legendre 4025symbol $\left(\frac{a}{n}\right)$.\\ 4026Input : 4027\begin{center}{\tt legendre\_symbol(26,17)}\end{center} 4028Output : 4029\begin{center}{\tt 1}\end{center} 4030Input : 4031\begin{center}{\tt legendre\_symbol(27,17)}\end{center} 4032Output : 4033\begin{center}{\tt -1}\end{center} 4034Input : 4035\begin{center}{\tt legendre\_symbol(34,17)}\end{center} 4036Output : 4037\begin{center}{\tt 0}\end{center} 4038 4039\subsection{Jacobi symbol : {\tt jacobi\_symbol}}\index{jacobi\_symbol} 4040If $n$ is not prime, the Jacobi symbol of $a$, 4041denoted as $\left(\frac{a}{n}\right)$, is defined 4042from the Legendre symbol and from the 4043decomposition of $n$ into prime factors. 4044Let 4045\[ n=p_1^{\alpha _1}..p_k^{\alpha _k} \] 4046where $p_j$ is prime and $\alpha _j$ is an integer for $j=1..k$. 4047The Jacobi symbol of $a$ is defined by : 4048\[ \left(\frac{a}{n}\right)=\left(\frac{a}{p_1}\right)^{\alpha _1}...\left(\frac{a}{p_k}\right)^{\alpha _k} \] 4049{\tt jacobi\_symbol} takes two arguments $a$ and $n$, and it returns the Jacobi 4050symbol $\left(\frac{a}{n}\right)$.\\ 4051Input : 4052\begin{center}{\tt jacobi\_symbol(25,12)}\end{center} 4053Output : 4054\begin{center}{\tt 1}\end{center} 4055Input : 4056\begin{center}{\tt jacobi\_symbol(35,12)}\end{center} 4057Output : 4058\begin{center}{\tt -1}\end{center} 4059Input : 4060\begin{center}{\tt jacobi\_symbol(33,12)}\end{center} 4061Output : 4062\begin{center}{\tt 0}\end{center} 4063 4064\subsection{Listing all compositions of an integer into $k$ parts : {\tt icomp\index{icomp}}} 4065{\tt icomp} accepts two or three arguments : a positive integer $n$, a positive integer $k$ not larger than $n$ and optionally {\tt zeros=true} or {\tt zeros=false}. The return value is the list of all compostions of $n$ into $k$ parts. Each composition is a list of nonnegative integers which sum up to $n$. If the option {\tt zeros} is set to {\tt true} (which is the default), a part can have zero value. Else, each part has nonzero (positive) value. 4066 4067For example, input : 4068\begin{center} 4069 \tt icomp(4,2) 4070\end{center} 4071Output : 4072\begin{center} 4073 \tt [[4,0],[3,1],[2,2],[1,3],[0,4]] 4074\end{center} 4075Input : 4076\begin{center} 4077 \tt icomp(6,3,zeros=false) 4078\end{center} 4079Output : 4080\begin{center} 4081 \tt [[4,1,1],[3,2,1],[2,3,1],[1,4,1],[3,1,2], [2,2,2],[1,3,2],[2,1,3],[1,2,3],[1,1,4]] 4082\end{center} 4083 4084\section{Combinatorial analysis} 4085\subsection{Factorial : {\tt factorial \ !}}\index{factorial|textbf}\index{\symbol{33}|textbf} 4086\noindent{\tt factorial} (prefix) or {\tt !} (postfix) 4087takes as argument an integer $n$.\\ 4088{\tt factorial(n)} or {\tt n!} returns $n!$.\\ 4089Input : 4090\begin{center}{\tt factorial(10)}\end{center} 4091or 4092\begin{center}{\tt 10!}\end{center} 4093Output : 4094\begin{center}{\tt 3628800}\end{center} 4095 4096\subsection{Binomial coefficients : {\tt binomial comb nCr}}\index{binomial}\index{comb|textbf}\index{nCr|textbf} 4097\noindent{\tt comb} or {\tt nCr} or {\tt binomial} takes as argument two 4098integers {\tt n} and {\tt p}.\\ 4099{\tt comb(n,p)} or {\tt nCr(n,p)} or {\tt binomial(n,p)} returns 4100$\left(^n_p\right) =C_n^p$.\\ 4101Input : 4102\begin{center}{\tt comb(5,2)}\end{center} 4103Output : 4104\begin{center}{\tt 10}\end{center} 4105{\bf Remark}\\ 4106{\tt binomial} (unlike {\tt comb, nCr}) 4107may have a third real argument, 4108in this case {\tt binomial(n,p,a)} returns 4109$\left(^n_p\right) a^p(1-a)^{n-p}$. 4110 4111\subsection{Permutations : {\tt perm nPr}}\index{perm}\index{nPr} 4112\noindent{\tt perm} or {\tt nPr} takes as arguments two integers $n$ and $p$.\\ 4113{\tt perm(n,p)} or {\tt nPr(n,p)} returns $P_n^p$.\\ 4114Input : 4115\begin{center}{\tt perm(5,2)}\end{center} 4116Output : 4117\begin{center}{\tt 20}\end{center} 4118 4119\subsection{Random integers : {\tt rand}}\index{rand} 4120\index{hasard} 4121(See also subsection \ref{ssec:rand}.) 4122 4123\noindent{\tt rand} takes as argument an integer $n$ or no argument. 4124\begin{itemize} 4125\item {\tt rand(n)} returns a random integer $p$ such that $0 \leq p<n$.\\ 4126Input : 4127\begin{center}{\tt rand(10)}\end{center} 4128Output for example : 4129\begin{center}{\tt 8}\end{center} 4130 4131\item {\tt rand()} returns a random integer $p$ such that $0 \leq p<2^{31}$ 4132(or on 64 bits architecture $0 \leq p<2^{63}$).\\ 4133Input : 4134\begin{center}{\tt rand()}\end{center} 4135Output for example : 4136\begin{center}{\tt 846930886}\end{center} 4137\end{itemize} 4138 4139\subsection{Wilf-Zeilberger pairs: \texttt{wz\_certificate}}\index{wz\_certificate} 4140 4141The \texttt{wz\_certificate} takes four arguments; an expression 4142\texttt{U(n,k)} in two variables, an expression \texttt{res(k)} in one of the 4143variables, the variable \texttt{n} and the variable \texttt{k}.\\ 4144\texttt{wz\_certificate} returns the Wilf-Zeilberger certificate 4145\texttt{R(n,k)} for the identity 4146\texttt{sum(U(n,k),k=-infinity..+infinity) = res(n)}. 4147 4148The Wilf-Zeilberger certificate $R(n,k)$ is used to prove the identity 4149\[\sum_{k} U(n,k) = C res(n)\] 4150for some constant $C$ (typically 1) whose value can be determined by 4151evaluating both sides for some value of $k$. To see how that works, 4152note that the above identity is equivalent to 4153\[ \sum_{k} F(n,k)\] 4154being constant, where $F(n,k) = U(n,k)/res(n)$. The Wilf-Zeilberger 4155certificate is a rational function $R(n,k)$ that make $F(n,k)$ and 4156$G(n,k) = R(n,k) F(n,k)$ a Wilf-Zeilberger pair, meaning 4157\begin{itemize} 4158 \item $F(n+1,k) - F(n,k) = G(n,k+1) - G(n,k)$ for integers $n \ge 4159 0$, $k$. 4160 \item $\lim_{k \to \pm\infty} G(n,k) = 0$ for each $n\ge 0$. 4161\end{itemize} 4162To see how this helps, adding the first equation from $k=-M$ to $k=N$ 4163gives us 4164$\sum_{k=-M}^{N}(F(n+1,k)-F(n,k)) = \sum_{k=-M}{N}(G(n,k+1) - 4165G(n,k))$. The right-hand side is a telescoping series, and so the 4166equality can be written 4167\[\sum_{k=-M}^{N} F(n+1,k) - \sum_{k=-M}^{N} F(n,k) = 4168G(n,N+1)-G(n,-M).\] 4169Taking the limit as $N,M \to \infty$ and using the second condition of 4170Wilf-Zeilberger pairs, we get 4171\[\sum_{k}F(n+1,k) = \sum_{k}F(n,k)\] 4172and so $\sum_{k}F(n,k)$ does not depend on $n$, and so is a constant. 4173 4174For example, to show 4175\[ \sum_{k} (-1)^k \binom{n}{k}\binom{2k}{k} 4^{n-k} = \binom{2n}{n}\] 4176Input: 4177\begin{center} 4178 \tt 4179 wz\_certificate((-1)\^{}k*comb(n,k)*comb(2k,k)*4\^{}(n-k),comb(2n,n),n,k) 4180\end{center} 4181Output: 4182\begin{center} 4183 \tt 4184 (2*k-1)/(2*n+1) 4185\end{center} 4186This means that $R(n,k) = (2k-1)/(2n+1)$ is a Wilf-Zeilberger 4187certificate; in other words 4188$F(n,k) = (-1)^k \binom{n}{k}\binom{2k}{k} 4^{n-k}/\binom{2n}{n}$ and 4189$G(n,k) = R(n,k)F(n,k)$ are a Wilf-Zeilberger pair. So 4190$\sum_{k} F(n,k)$ is a constant. Since $F(0,0)=1$ and $F(0,k) = 0$ 4191for $k>0$, 4192$\sum_{k} F(0,k) = 1$ 4193and so $\sum_{k} F(n,k) = 1$ for all $n$, showing 4194\[ \sum_{k} (-1)^k \binom{n}{k}\binom{2k}{k} 4^{n-k} = \binom{2n}{n}.\] 4195 4196\section{Rationals} 4197\subsection{Transform a floating point number into a rational : {\tt exact float2rational}}\index{float2rational|textbf}\index{exact|textbf}\index{evalf} 4198\noindent {\tt float2rational} or {\tt exact} takes as argument a 4199floating point number {\tt d} and returns 4200a rational number {\tt q} close to 4201{\tt d} such that {\tt abs(d-q)<epsilon}. 4202{\tt epsilon} is defined in the {\tt cas} configuration 4203({\tt Cfg} menu) or with the {\tt cas\_setup} command.\\ 4204Input : 4205\begin{center}{\tt float2rational(0.3670520231)}\end{center} 4206Output when {\tt epsilon=1e-10}: 4207\begin{center}{\tt 127/346}\end{center} 4208% Input : 4209% \begin{center}{\tt 123/12+57/21}\end{center} 4210% Output : 4211% \begin{center}{\tt 363/28}\end{center} 4212% Then 4213Input : 4214\begin{center}{\tt evalf(363/28)}\end{center} 4215Output : 4216\begin{center}{\tt 12.9642857143}\end{center} 4217Input : 4218\begin{center}{\tt float2rational(12.9642857143)}\end{center} 4219Output : 4220\begin{center}{\tt 363/28}\end{center} 4221If two representations are mixed, for example : 4222\begin{center}{\tt 1/2+0.7}\end{center} 4223the rational is converted to a float, output : 4224\begin{center}{\tt 1.2}\end{center} 4225Input : 4226\begin{center}{\tt 1/2+float2rational(0.7)}\end{center} 4227Output : 4228\begin{center}{\tt 6/5}\end{center} 4229 4230\subsection{Integer and fractional part : {\tt propfrac propFrac}}\index{propfrac}\index{propFrac}\label{sec:ipropfrac} 4231\noindent{\tt propfrac(A/B)} or {\tt propFrac(A/B)} returns 4232$$q+\frac{r}{b}\ \mbox{ with } \ 0\leq r<b$$ 4233if $\displaystyle \frac{A}{B}=\frac{a}{b}$ with $\mbox{gcd}(a,b)=1$ 4234and $a=bq+r$.\\ 4235For rational fractions, cf. \ref{sec:propfrac}.\\ 4236Input : 4237\begin{center}{\tt propfrac(42/15)}\end{center} 4238Output : 4239\begin{center}{\tt 2+4/5}\end{center} 4240Input : 4241\begin{center}{\tt propfrac(43/12)}\end{center} 4242Output : 4243\begin{center}{\tt 3+7/12}\end{center} 4244 4245\subsection{Numerator of a fraction after simplification : {\tt numer}{\tt getNum}}\index{numer|textbf}\index{getNum|textbf}\label{sec:inumer} 4246\noindent{\tt numer} or {\tt getNum} takes as argument a fraction and returns 4247the numerator of this fraction after simplification (for rational fractions, 4248see \ref{sec:numer}).\\ 4249Input : 4250\begin{center}{\tt numer(42/12)}\end{center} 4251or : 4252\begin{center}{\tt getNum(42/12)}\end{center} 4253Output : 4254\begin{center}{\tt 7}\end{center} 4255To avoid simplifications, the argument must 4256be quoted (for rational fractions see \ref{sec:getnum}).\\ 4257Input : 4258\begin{center}{\tt numer('42/12')}\end{center} 4259or : 4260\begin{center}{\tt getNum('42/12')}\end{center} 4261Output : 4262\begin{center}{\tt 42}\end{center} 4263 4264 4265\subsection{Denominator of a fraction after simplification : {\tt denom getDenom}}\index{denom|textbf}\index{getDenom|textbf}\label{sec:idenom} 4266\noindent{\tt denom} or {\tt getDenom} takes as argument a fraction and 4267returns the denominator of this fraction after simplification (for rational 4268fractions see \ref{sec:denom}).\\ 4269Input : 4270\begin{center}{\tt denom(42/12)}\end{center} 4271or : 4272\begin{center}{\tt getDenom(42/12)}\end{center} 4273Output : 4274\begin{center}{\tt 2}\end{center} 4275To avoid simplifications, the argument must 4276be quoted (for rational fractions see \ref{sec:getdenom}).\\ 4277Input : 4278\begin{center}{\tt denom('42/12')}\end{center} 4279or : 4280\begin{center}{\tt getDenom('42/12')}\end{center} 4281Output : 4282\begin{center}{\tt 12}\end{center} 4283 4284\subsection{Numerator and denominator of a fraction : {\tt f2nd fxnd}}\index{fxnd}\index{f2nd}\label{sec:ifxnd} 4285\noindent{\tt f2nd} (or {\tt fxnd}) takes as argument a fraction and returns 4286the list of the numerator and denominator of this fraction after simplification 4287(for rational fractions see \ref{sec:fxnd}).\\ 4288Input : 4289\begin{center}{\tt f2nd(42/12)}\end{center} 4290Output : 4291\begin{center}{\tt [7,2]}\end{center} 4292 4293\subsection{Simplification of a pair of integers : {\tt simp2}}\index{simp2|textbf}\label{sec:isimp2} 4294\noindent{\tt simp2} takes as argument two integers or a list of two integers 4295 which represent a fraction (for two polynomials see \ref{sec:simp2}).\\ 4296{\tt simp2} returns the list of the numerator and the denominator of 4297an irreducible representation of this fraction 4298(i.e. after simplification).\\ 4299Input : 4300\begin{center}{\tt simp2(18,15) }\end{center} 4301Output : 4302\begin{center}{\tt [6,5]}\end{center} 4303Input : 4304\begin{center}{\tt simp2([42,12])}\end{center} 4305Output : 4306\begin{center}{\tt [7,2]}\end{center} 4307 4308\subsection{Continued fraction representation of a real : {\tt dfc}}\index{dfc}\label{sec:convertdfc}\index{confrac@{\sl confrac}|textbf} 4309\noindent {\tt dfc} takes as argument a real or a rational or a 4310floating point number {\tt a} and an integer {\tt n} 4311(or a real {\tt epsilon}).\\ 4312{\tt dfc} returns the list of the continued fraction representation 4313of {\tt a} of order {\tt n} (or with precision {\tt epsilon} i.e. 4314the continued fraction representation which 4315approximates {\tt a} or {\tt evalf(a)} with precision 4316{\tt epsilon}, by default {\tt epsilon} is the value of the {\tt epsilon} 4317defined in the {\tt cas} configuration with the menu 4318{\tt Cfg$\blacktriangleright$Cas Configuration}).\\ 4319{\tt convert} with the option {\tt confrac} has a similar 4320functionality: in that case 4321the value of {\tt epsilon} is the value of the {\tt epsilon} 4322defined in the {\tt cas} configuration with the menu 4323{\tt Cfg$\blacktriangleright$Cas Configuration} (see 4324\ref{sec:convert}) 4325and the answer may be stored in an optional third argument. 4326 4327{\bf Remarks} 4328\begin{itemize} 4329\item If the last element of the result is a list, the representation is 4330ultimately periodic, and the last element is the period. It means 4331that the real is a root of an equation of order 2 with integer 4332coefficients. 4333\item if the last element of the result is not an integer, it 4334represents a remainder $r$ ($a=a0+1/....+1/an+1/r$). Be aware 4335that this remainder has lost most of its accuracy. 4336\end{itemize} 4337If {\tt dfc(a)=[a0,a1,a2,[b0,b1]]} that means : 4338\[ 4339a=a0+\frac{1}{a1+\frac{1}{a2+\frac{1}{b0+\frac{1}{b1+\frac{1}{b0+...}}}}} 4340\] 4341If {\tt dfc(a)=[a0,a1,a2,r]} that means : 4342\[ a=a0+\frac{1}{a1+\frac{1}{a2+\frac{1}{r}}} \] 4343Input : 4344\begin{center}{\tt dfc(sqrt(2),5)}\end{center} 4345Output : 4346\begin{center}{\tt [1,2,[2]]}\end{center} 4347Input : 4348\begin{center}{\tt dfc(evalf(sqrt(2)),1e-9)}\end{center} 4349or : 4350\begin{center}{\tt dfc(sqrt(2),1e-9)}\end{center} 4351Output : 4352\begin{center}{\tt [1,2,2,2,2,2,2,2,2,2,2,2,2]}\end{center} 4353Input : 4354\begin{center}{\tt convert(sqrt(2),confrac,'dev')}\end{center} 4355Output (if in the {\tt cas} configuration {\tt epsilon=1e-9}) : 4356\begin{center}{\tt [1,2,2,2,2,2,2,2,2,2,2,2,2]}\end{center} 4357and {\tt [1,2,2,2,2,2,2,2,2,2,2,2,2]} is stored in {\tt dev}.\\ 4358Input : 4359\begin{center}{\tt dfc(9976/6961,5)}\end{center} 4360Output : 4361\begin{center}{\tt [1,2,3,4,5,43/7]}\end{center} 4362Input to verify: 4363\begin{center}{\tt 1+1/(2+1/(3+1/(4+1/(5+7/43))))}\end{center} 4364Output : 4365\begin{center}{\tt 9976/6961}\end{center} 4366Input : 4367\begin{center}{\tt convert(9976/6961,confrac,'l')}\end{center} 4368Output (if in the {\tt cas} configuration {\tt epsilon=1e-9}) : 4369\begin{center}{\tt [1,2,3,4,5,6,7]}\end{center} 4370and {\tt [1,2,3,4,5,6,7]} is stored in {\tt l}\\ 4371Input : 4372\begin{center}{\tt dfc(pi,5)}\end{center} 4373Output : 4374\begin{center}{\tt [3,7,15,1,292,(-113*pi+355)/(33102*pi-103993)]}\end{center} 4375Input : 4376\begin{center}{\tt dfc(evalf(pi),5)}\end{center} 4377Output (if floats are hardware floats, e.g. for Digits=12) : 4378\begin{center}{\tt [3,7,15,1,292,1.57581843574]}\end{center} 4379Input : 4380\begin{center}{\tt dfc(evalf(pi),1e-9)}\end{center} 4381or : 4382\begin{center}{\tt dfc(pi,1e-9)}\end{center} 4383or (if in the {\tt cas} configuration {\tt epsilon=1e-9}) : 4384\begin{center}{\tt convert(pi,confrac,'ll')}\end{center} 4385Output : 4386\begin{center}{\tt [3,7,15,1,292]}\end{center} 4387 4388\subsection{Transform a continued fraction representation into a real : {\tt dfc2f}}\index{dfc2f} 4389\noindent {\tt dfc2f} takes as argument a list representing a continued 4390fraction, namely 4391\begin{itemize} 4392\item a list of integers for a rational number 4393\item a list whose last element is a list for an 4394ultimately periodic representation, i.e. 4395a quadratic number, that is a root of a second order equation with 4396integer coefficients. 4397\item or a list with a remainder $r$ as last element 4398 ($a=a0+1/....+1/an+1/r$). 4399\end{itemize} 4400{\tt dfc2f} returns the rational number or the quadratic number with the 4401argument as continued fraction representation.\\ 4402Input : 4403\begin{center}{\tt dfc2f([1,2,[2]])}\end{center} 4404Output : 4405\begin{center}{\tt 1/(1/(1+sqrt(2))+2)+1}\end{center} 4406After simplification with {\tt normal} : 4407\begin{center}{\tt sqrt(2)}\end{center} 4408Input : 4409\begin{center}{\tt dfc2f([1,2,3])}\end{center} 4410Output : 4411\begin{center}{\tt 10/7}\end{center} 4412Input : 4413\begin{center}{\tt normal(dfc2f([3,3,6,[3,6]]))}\end{center} 4414Output : 4415\begin{center}{\tt sqrt(11)}\end{center} 4416Input : 4417\begin{center}{\tt dfc2f([1,2,3,4,5,6,7])}\end{center} 4418Output : 4419\begin{center}{\tt 9976/6961}\end{center} 4420Input to verify : 4421\begin{center}{\tt 1+1/(2+1/(3+1/(4+1/(5+1/(6+1/7)))))}\end{center} 4422Output : 4423\begin{center}{\tt 9976/6961}\end{center} 4424Input : 4425\begin{center}{\tt dfc2f([1,2,3,4,5,43/7])}\end{center} 4426Output : 4427\begin{center}{\tt 9976/6961}\end{center} 4428Input to verify : 4429\begin{center}{\tt 1+1/(2+1/(3+1/(4+1/(5+7/43))))}\end{center} 4430Output : 4431\begin{center}{\tt 9976/6961}\end{center} 4432 4433\subsection{The $n$-th Bernoulli number : {\tt bernoulli}}\index{bernoulli} 4434\noindent {\tt bernoulli} takes as argument an integer $n$.\\ 4435{\tt bernoulli} returns the $n$-th Bernoulli number $B(n)$.\\ 4436The Bernoulli numbers are defined by : 4437\[ \frac{t}{e^t-1}=\sum_{n=0}^{+\infty} \frac{B(n)}{n!}t^n \] 4438Bernoulli polynomials $B_k$ are defined by : 4439\[ B_0=1, \quad B_k{'}(x)=kB_{k-1}(x), \quad \int_0^1B_k(x)dx=0 \] 4440and the relation $B(n)=B_n(0)$ holds.\\ 4441Input : 4442\begin{center}{\tt bernoulli(6)}\end{center} 4443Output : 4444\begin{center}{\tt 1/42}\end{center} 4445 4446\subsection{Access to PARI/GP commands: {\tt pari}}\index{pari} 4447\begin{itemize} 4448\item 4449{\tt pari} with a string as first argument (the PARI command name) 4450execute the corresponding PARI command with the remaining arguments. 4451For example {\tt pari("weber",1+i)} executes the PARI command 4452{\tt weber} with the argument {\tt 1+i}. 4453\item 4454{\tt pari} without argument exports all PARI/GP functions 4455\begin{itemize} 4456\item with the same command name if they are not already defined inside {\tt 4457 Xcas} 4458\item with their original command name with the prefix {\tt pari\_} 4459\end{itemize} 4460For example, after calling {\tt pari()}, {\tt pari\_weber(1+i)} or 4461{\tt weber(1+i)} will execute the PARI command 4462{\tt weber} with the argument {\tt 1+i}. 4463\end{itemize} 4464 4465The documentation of PARI/GP is available with the menu 4466Help->Manuals. 4467 4468\section{Real numbers} 4469\subsection{Eval a real at a given precision : {\tt evalf} and {\tt 4470 Digits}, {\tt DIGITS}}\index{evalf}\index{Digits}\index{DIGITS} 4471\begin{itemize} 4472\item A real number is an exact number and its numeric evaluation at a given 4473precision is a floating number represented in base 2.\\ 4474The precision of a floating number is the number of bits of its 4475mantissa, which is at least 53 (hardware float numbers, also known as {\tt 4476 double}). Floating numbers are displayed in base 10 with a number 4477of digits controlled by the user either by assigning the {\tt Digits} 4478variable or by modifying the Cas configuration. 4479By default {\tt Digits} is equal to 12. 4480The number of digits displayed controls the number of bits of the 4481mantissa, if Digits is less than 15, 53 bits are used, if Digits is 4482strictly greater than 15, the number of bits is a roundoff of 4483Digits times the log of 10 in base 2. 4484\item 4485An expression is coerced into a floating number with the {\tt evalf} 4486command. {\tt evalf} may have an optional second argument which will 4487be used to evaluate with a given precision. 4488\item 4489Note that if an expression contains a floating number, evaluation will try 4490to convert other arguments to floating point numbers in order 4491to coerce the whole expression to a single floating number. 4492\end{itemize} 4493Input : 4494\begin{center}{\tt 1+1/2}\end{center} 4495Output : 4496\begin{center}{\tt 3/2}\end{center} 4497Input : 4498\begin{center}{\tt 1.0+1/2}\end{center} 4499Output : 4500\begin{center}{\tt 1.5}\end{center} 4501Input: 4502\begin{center}{\tt exp(pi*sqrt(20))}\end{center} 4503Output : 4504\begin{center}{\tt exp(pi*2*sqrt(5)) }\end{center} 4505With {\tt evalf}, input : 4506\begin{center}{\tt evalf(exp(pi*2*sqrt(5)))}\end{center} 4507Output : 4508\begin{center}{\tt 1263794.75367}\end{center} 4509Input : 4510\begin{center}{\tt 1.1\verb|^|{20}}\end{center} 4511Output : 4512\begin{center}{\tt 6.72749994933}\end{center} 4513Input : 4514\begin{center}{\tt sqrt(2)\verb|^|21}\end{center} 4515Output : 4516\begin{center}{\tt sqrt(2)*2\verb|^|10}\end{center} 4517Input for a result with 30 digits : 4518\begin{center}{\tt Digits:=30}\end{center} 4519Input for the numeric value of $e^{\pi\sqrt{163}}$: 4520\begin{center}{\tt evalf(exp(pi*sqrt(163)))}\end{center} 4521Output : 4522\begin{center}{\tt 0.262537412640768743999999999985e18}\end{center} 4523Note that {\tt Digits} is now set to 30. If you don't want to change 4524the value of {\tt Digits} you may input 4525\begin{center}{\tt evalf(exp(pi*sqrt(163)),30)}\end{center} 4526 4527\subsection{Usual infixed functions on reals : {\tt +,-,*,/,\^\ }} 4528\index{+,-,*,/,\^\ } 4529\noindent {\tt +,-,*,/,\^\ } are the usual operators to do 4530additions, subtractions, multiplications, divisions and for raising to a 4531power.\\ 4532Input : 4533\begin{center}{\tt 3+2}\end{center} 4534Output : 4535\begin{center}{\tt 5}\end{center} 4536Input : 4537\begin{center}{\tt 3-2}\end{center} 4538Output : 4539\begin{center}{\tt 1}\end{center} 4540Input : 4541\begin{center}{\tt 3*2}\end{center} 4542Output : 4543\begin{center}{\tt 6}\end{center} 4544Input : 4545\begin{center}{\tt 3/2}\end{center} 4546Output : 4547\begin{center}{\tt 3/2}\end{center} 4548Input : 4549\begin{center}{\tt 3.2/2.1}\end{center} 4550Output : 4551\begin{center}{\tt 1.52380952381}\end{center} 4552Input : 4553\begin{center}{\tt 3\verb|^|2}\end{center} 4554Output : 4555\begin{center}{\tt 9}\end{center} 4556Input : 4557\begin{center}{\tt 3.2\verb|^|2.1}\end{center} 4558Output : 4559\begin{center}{\tt 11.5031015682}\end{center} 4560 4561{\bf Remark}\\ 4562You may use the square key or the cube key if your keyboard has one, 4563for example : ${\tt 3^2}$ returns 9. 4564 4565{\bf Remark on non integral powers } 4566\begin{itemize} 4567\item If $x$ is not an integer, then $a^x=\exp(x \* \ln(a))$, hence 4568$a^x$ is well-defined only for $a>0$ if $x$ is not rational. If $x$ 4569is rational and $a<0$, the principal determination of the logarithm 4570is used, leading to a complex number. 4571\item Hence be aware of the difference between $\sqrt[n]{a}$ and $a^{\frac{1}{n}}$ 4572when $n$ is an odd integer.\\ 4573For example, to draw the graph of $y=\sqrt[3]{x^3-x^2}$, input : 4574\begin{center} 4575{\tt plotfunc(ifte(x>0,(x\verb|^|3-x\verb|^|2)\verb|^|(1/3),\\ 4576-(x\verb|^|2-x\verb|^|3)\verb|^|(1/3)),x,xstep=0.01)} 4577\end{center} 4578You might also input : 4579\begin{center}{\tt plotimplicit(y\verb|^|3=x\verb|^|3-x\verb|^|2)}\end{center} 4580but this is much slower and much less accurate. 4581\end{itemize} 4582 4583\subsection{Usual prefixed functions on reals : {\tt rdiv}}\index{rdiv} 4584{\tt rdiv} is the prefixed form of the division function.\\ 4585Input : 4586\begin{center}{\tt rdiv(3,2)}\end{center} 4587Output : 4588\begin{center}{\tt 3/2}\end{center} 4589Input : 4590\begin{center}{\tt rdiv(3.2,2.1)}\end{center} 4591Output : 4592\begin{center}{\tt 1.52380952381}\end{center} 4593 4594\subsection{$n$-th root : {\tt root}}\index{root} 4595\noindent{\tt root} takes two arguments : an integer $n$ and a number $a$.\\ 4596{\tt root} returns the $n$-th root of $a$ (i.e. $a^{1/n}$). 4597If $a<0$, the $n$-th root is a complex number of argument $2\pi/n$.\\ 4598Input : 4599\begin{center}{\tt root(3,2)}\end{center} 4600Output : 4601\begin{center}{\tt 2\verb|^|(1/3)}\end{center} 4602\noindent Input : 4603\begin{center}{\tt root(3,2.0)}\end{center} 4604Output : 4605\begin{center}{\tt 1.259921049892}\end{center} 4606Input : 4607\begin{center}{\tt root(3,sqrt(2))}\end{center} 4608Output : 4609\begin{center}{\tt 2\verb|^|(1/6)}\end{center} 4610 4611\subsection{The exponential integral function: \texttt{Ei}}\index{Ei} 4612 4613The \texttt{Ei} command takes as argument a complex number.\\ 4614\texttt{Ei} returns the value of the exponential integral at the 4615argument. 4616 4617For non-zero real numbers $x$, 4618\[ Ei(x) = \int_{t=-\infty}^{x} \frac{\exp(t)}{t} dt.\] 4619For $x>0$, this integral is improper but the principal value exists. 4620This function satisfies $Ei(0) = -\infty, Ei(-\infty) = 0$. 4621 4622Since 4623\[ 4624\frac{\exp(x)}{x} = \frac{1}{x} + 1 + \frac{x}{2!} + \frac{x^2}{3!} + 4625\dots,\] 4626the $Ei$ function can be extended to $\C - \{0\}$ (with a branch cut 4627on the positive real axis) by 4628\[ Ei(z) = \ln(z) + \gamma + x + \frac{x^2}{2\cdot 2!} + 4629\frac{x^3}{3\cdot 3!} + \dots\] 4630where $\gamma = 0.57721566490\dots$ is Euler's constant.\\ 4631Input: 4632\begin{center} 4633 \tt 4634 Ei(1.0) 4635\end{center} 4636Output: 4637\begin{center} 4638 \tt 4639 1.89511781636 4640\end{center} 4641Input: 4642\begin{center} 4643 \tt 4644 Ei(-1.0) 4645\end{center} 4646Output: 4647\begin{center} 4648 \tt 4649 -0.219383934396 4650\end{center} 4651Input: 4652\begin{center} 4653 \tt 4654 Ei(1.)-Ei(-1.) 4655\end{center} 4656Output: 4657\begin{center} 4658 \tt 4659 2.11450175075 4660\end{center} 4661Input: 4662\begin{center} 4663 \tt 4664 int((exp(x)-1)/x,x=-1..1.) 4665\end{center} 4666Output: 4667\begin{center} 4668 \tt 4669 2.11450175075 4670\end{center} 4671The input\\ 4672Input: 4673\begin{center} 4674 \tt 4675 evalf(Ei(-1)-sum((-1)\^{}n/n/n!,n=1..100)) 4676\end{center} 4677approximates Euler's constant\\ 4678Output: 4679\begin{center} 4680 \tt 4681 0.577215664902 4682\end{center} 4683 4684The \texttt{Ei} command can also take two arguments, where the second 4685argument is a positive integer indicating other types of exponential 4686integrals; $Ei(x,n) = E_n(x)$. Specifically:\\ 4687\texttt{Ei(a,1) = -Ei(-a)}\\ 4688\texttt{Ei(a,2) = exp(-a) + a*Ei(-a) = exp(-a) - a*Ei (a, 1)} 4689and for $n \ge 2$, 4690\texttt{Ei(a,n)=(exp(-a) - a*Ei(a,n-1))/(n-1)} 4691 4692\subsection{The logarithmic integral function:\texttt{Li}}\index{Li} 4693 4694The \texttt{Li} command takes as argument a complex number.\\ 4695\texttt{Li} returns the value of the logarithmic integral function 4696$Li$ at the point, where 4697\[ Li(x) = Ei(\ln(x)) = \int_{t=0}^{\exp(x)} \frac{1}{\ln(t)} dt\] 4698Input: 4699\begin{center} 4700 \tt 4701 Li(2.0) 4702\end{center} 4703Output: 4704\begin{center} 4705 \tt 4706 1.04516378012 4707\end{center} 4708 4709\subsection{The cosine integral function:\texttt{Ci}}\index{Ci} 4710 4711The \texttt{Ci} command takes as argument a complex number.\\ 4712\texttt{Ci} returns the value of the cosine integral function $Ci$ at 4713the point, where 4714\[ Ci(x) = \int_{+\infty}^{x} \frac{\cos(t)}{t} dt = \ln(t) + \gamma + 4715\int_{t=0}^{x} \frac{\cos(t) - 1}{t} dt\] 4716This satisfies $Ci(0) = -\infty, Ci(-\infty) = i\pi$ and $Ci(+\infty) 4717= 0$.\\ 4718Input: 4719\begin{center} 4720 \tt 4721 Ci(1.0) 4722\end{center} 4723Output: 4724\begin{center} 4725 \tt 4726 0.337403922901 4727\end{center} 4728Input: 4729\begin{center} 4730 \tt 4731 Ci(-1.0) 4732\end{center} 4733Output: 4734\begin{center} 4735 \tt 4736 0.337403922901+3.14159265359*i 4737\end{center} 4738Input: 4739\begin{center} 4740 \tt 4741 Ci(1.0) - Ci(-1.0) 4742\end{center} 4743Output: 4744\begin{center} 4745 \tt 4746 -3.14159265359*i 4747\end{center} 4748 4749\subsection{The sine integral function:\texttt{Si}}\index{Si} 4750 4751The \texttt{Si} command takes as argument a complex number.\\ 4752\texttt{Si} returns the value of the sine integral function $Si$ at 4753the point, where 4754\[ Si(x) = \int_{0}^{x} \frac{\sin(t)}{t} dt\] 4755This satisfies $Si(0) = 0, Si(-\infty) = -\pi/2$ and $Si(+\infty) 4756= \pi/2$. Also note that $Si$ is an odd function.\\ 4757Input: 4758\begin{center} 4759 \tt 4760 Si(1.0) 4761\end{center} 4762Output: 4763\begin{center} 4764 \tt 47650.946083070367 4766\end{center} 4767Input: 4768\begin{center} 4769 \tt 4770 Si(-1.0) 4771\end{center} 4772Output: 4773\begin{center} 4774 \tt 4775 -0.946083070367 4776\end{center} 4777 4778\subsection{The Heaviside function: \texttt{Heaviside}}\index{Heaviside} 4779 4780The \texttt{Heaviside} command takes as argument a real number.\\ 4781\texttt{Heaviside} returns the value of the Heaviside function; namely 47820 if the input is negative, 1 otherwise.\\ 4783Input: 4784\begin{center} 4785 \tt 4786 Heaviside(2) 4787\end{center} 4788Output: 4789\begin{center} 4790 \tt 4791 1 4792\end{center} 4793Input: 4794\begin{center} 4795 \tt 4796 Heaviside(-4) 4797\end{center} 4798Output: 4799\begin{center} 4800 \tt 4801 0 4802\end{center} 4803 4804\subsection{The Dirac distribution: \texttt{Dirac}}\index{Dirac} 4805 4806The \texttt{Dirac} command takes as input a number.\\ 4807\texttt{Dirac} returns \texttt{infinity} if the number is 0, it 4808returns 0 otherwise. 4809 4810\texttt{Dirac} represents the distribution which is the derivative of 4811the Heaviside function. This means that 4812\[ \int_{-\infty}^{\infty} Dirac(x) dx = 1\] 4813and, in fact, $\int_{a}^{b} Dirac(x) dx$ is 1 if $[a,b]$ contains 0 and 4814the integral is 0 otherwise. The defining property of the Dirac 4815distribution is that 4816\[ \int_{-\infty}^{\infty} Dirac(x) f(x) dx = f(0)\] 4817and consequently 4818\[ \int_{a}^{b} Dirac(x-c) f(x) dx = f(c)\] 4819as long as $c$ is in $[a,b]$.\\ 4820Input: 4821\begin{center} 4822 \tt 4823 int(Dirac(x)*sin(x),x,-1,2) 4824\end{center} 4825Output: 4826\begin{center} 4827 \tt 4828 sin(0) 4829\end{center} 4830Input: 4831\begin{center} 4832 \tt 4833 int(Dirac(x-1)*sin(x),x,-1,2) 4834\end{center} 4835Output: 4836\begin{center} 4837 \tt 4838 sin(1) 4839\end{center} 4840 4841\subsection{Error function : {\tt erf}}\index{erf} 4842\noindent{\tt erf} takes as argument a number $a$.\\ 4843{\tt erf} returns the floating point value of the error function at $x=a$, 4844where the error function is defined by~: 4845\[ \mbox{erf}(x)=\frac{2}{\sqrt{\pi}}\int_0^{x}e^{-t^2}dt \] 4846The normalization is chosen so that: 4847\[ \mbox{erf}(+\infty)=1, \quad \mbox{erf}(-\infty)=-1 \] 4848since : 4849\[ \int_0^{+\infty}e^{-t^2}dt=\frac{\sqrt{\pi}}{2} \] 4850Input : 4851\begin{center}{\tt erf(1)}\end{center} 4852Output : 4853\begin{center}{\tt 0.84270079295}\end{center} 4854Input : 4855\begin{center}{\tt erf(1/(sqrt(2)))*1/2+0.5}\end{center} 4856Output : 4857\begin{center}{\tt 0.841344746069}\end{center} 4858{\bf Remark}\\ 4859The relation between {\tt erf} and {\tt normal\_cdf} is :\\ 4860\[ \mbox{\tt normal\_cdf}(x)=\frac{1}{2}+\frac{1}{2}\*\mbox{\tt 4861 erf}(\frac{x}{\sqrt{2}}) \] 4862Indeed, making the change of variable $t=u*\sqrt{2}$ in 4863\[ \mbox{normal\_cdf}(x)=\frac{1}{2}+\frac{1}{\sqrt{2\pi}}\int_0^{x}e^{-t^2/2}dt\] 4864gives~: 4865\[ \mbox{normal\_cdf}(x)=\frac{1}{2}+\frac{1}{\sqrt{\pi}}\int_0^{\frac{x}{\sqrt{2}}}e^{-u^2}du=\frac{1}{2}+\frac{1}{2}\*\mbox{erf}(\frac{x}{\sqrt{2}})\] 4866Check :\\ 4867{\tt normal\_cdf(1)=0.841344746069} 4868 4869\subsection{Complementary error function: {\tt erfc}}\index{erfc} 4870\noindent{\tt erfc} takes as argument a number $a$.\\ 4871{\tt erfc} returns the value of the complementary error function at 4872$x=a$, this function is defined by~: 4873\[ 4874\mbox{erfc}(x)=\frac{2}{\sqrt{\pi}}\int_x^{+\infty}e^{-t^2}dt=1-\mbox{erf}(x) 4875\] 4876Hence erfc$(0)=1$, since~: 4877\[ \int_0^{+\infty}e^{-t^2}dt=\frac{\sqrt{\pi}}{2} \] 4878Input : 4879\begin{center}{\tt erfc(1)}\end{center} 4880Output : 4881\begin{center}{\tt 0.15729920705}\end{center} 4882Input : 4883\begin{center}{\tt 1- erfc(1/(sqrt(2)))*1/2}\end{center} 4884Output : 4885\begin{center}{\tt 0.841344746069}\end{center} 4886{\bf Remark}\\ 4887The relation between {\tt erfc} and {\tt normal\_cdf} is : 4888\[ \mbox{\tt normal\_cdf}(x)=1-\frac{1}{2}\*\mbox{\tt erfc} 4889(\frac{x}{\sqrt{2}}) \] 4890Check :\\ 4891{\tt normal\_cdf(1)=0.841344746069} 4892 4893\subsection{The $\Gamma$ function : {\tt Gamma}}\index{Gamma} 4894\noindent{\tt Gamma} takes as argument a number $a$.\\ 4895{\tt Gamma} returns the value of the $\Gamma$ function in $a$, defined by~: 4896\[ \Gamma(x)=\int_0^{+\infty}e^{-t}t^{x-1}dt, \mbox{ if } x>0 \] 4897If $x$ is a positive integer, $\Gamma$ is computed by applying 4898the recurrence~: 4899\[ \Gamma(x+1)=x*\Gamma(x), \quad \Gamma(1)=1 \] 4900Hence~: 4901\[ \Gamma(n+1)=n! \] 4902Input : 4903\begin{center}{\tt Gamma(5)}\end{center} 4904Output : 4905\begin{center}{\tt 24}\end{center} 4906% Input : 4907% \begin{center}{\tt Gamma(1/2)}\end{center} 4908% Output : 4909% \begin{center}{\tt sqrt(pi)}\end{center} 4910Input : 4911\begin{center}{\tt Gamma(0.7)}\end{center} 4912Output : 4913\begin{center}{\tt 1.29805533265}\end{center} 4914Input : 4915\begin{center}{\tt Gamma(-0.3)}\end{center} 4916Output : 4917\begin{center}{\tt -4.32685110883}\end{center} 4918Indeed : {\tt Gamma(0.7)=-0.3*Gamma(-0.3)}\\ 4919Input : 4920\begin{center}{\tt Gamma(-1.3)}\end{center} 4921Output : 4922\begin{center}{\tt 3.32834700679}\end{center} 4923Indeed {\tt Gamma(0.7)=-0.3*Gamma(-0.3)=(-0.3)*(-1.3)*Gamma(-1.3)} 4924 4925\subsection{The upper incomplete $\gamma$ function: 4926\texttt{ugamma}}\index{ugamma} 4927 4928The \texttt{ugamma} command takes two arguments, a number \texttt{a} 4929and a number \texttt{b}$\ge 0$.\\ 4930\texttt{ugamma} returns the value of the upper incomplete $\gamma$ 4931function, 4932\[\Gamma(a,b) = \int_{b}^{+\infty} e^{-t}t^{a-1} dt.\] 4933Input: 4934\begin{center} 4935 \tt 4936 ugamma(3.0,2.0) 4937\end{center} 4938Output: 4939\begin{center} 4940 \tt 4941 1.35335283237 4942\end{center} 4943Input: 4944\begin{center} 4945 \tt 4946 ugamma(-1.3,2) 4947\end{center} 4948Output: 4949\begin{center} 4950 \tt 4951 0.0142127568837 4952\end{center} 4953 4954\subsection{The lower incomplete $\gamma$ function: 4955\texttt{igamma}}\index{igamma} 4956 4957The \texttt{igamma} command takes two mandatory arguments and an 4958optional third argument. The mandatory arguments are a number \texttt{a} 4959and a number \texttt{b}$\ge 0$. An optional third argument of 1 will 4960return a normalized version of the function.\\ 4961\texttt{igamma} returns the value of the incomplete $\gamma$ 4962function, 4963\[\gamma(a,b) = \int_{0}^{b} e^{-t}t^{a-1} dt.\] 4964With a third argument of 1, the value returned will be normalized; 4965namely divided by $\Gamma(a)$.\\ 4966Input: 4967\begin{center} 4968 \tt 4969 igamma(2.0,3.0) 4970\end{center} 4971Output: 4972\begin{center} 4973 \tt 4974 0.800851726529 4975\end{center} 4976Input: 4977\begin{center} 4978 \tt 4979 igamma(4.0,3.0) 4980\end{center} 4981Output: 4982\begin{center} 4983 \tt 4984 2.11660866731 4985\end{center} 4986Input: 4987\begin{center} 4988 \tt 4989 igamma(4.0,3.0,1) 4990\end{center} 4991Output: 4992\begin{center} 4993 \tt 4994 0.352768111218 4995\end{center} 4996since $\Gamma(4) = 6$ and $2.11660866731/6 = 0.352768111218$. 4997 4998\subsection{The $\beta$ function : {\tt Beta}}\index{Beta} 4999\noindent{\tt Beta} takes as argument two reals $a,b$.\\ 5000{\tt Beta} returns the value of the $\beta$ function at $a,b \in 5001\mathbb R$, defined by~: 5002\[ \beta(x,y)=\int_0^1 t^{x-1} (1-t)^{y-1} 5003=\frac{\Gamma(x)*\Gamma(y)}{\Gamma(x+y)} \] 5004Remarkable values~: 5005\[ \beta(1,1)=1, \quad \beta(n,1)=\frac{1}{n}, \quad 5006\beta(n,2)=\frac{1}{n(n+1)} \] 5007{\tt Beta(x,y)} is defined for $x$ and $y$ positive reals 5008(to ensure the convergence of the integral) and by 5009prolongation for $x$ and $y$ if they are not negative integers.\\ 5010Input : 5011\begin{center}{\tt Beta(5,2)}\end{center} 5012Output : 5013\begin{center}{\tt 1/30}\end{center} 5014Input : 5015\begin{center}{\tt Beta(x,y)}\end{center} 5016Output : 5017\begin{center}{\tt Gamma(x)*Gamma(y)/Gamma(x+y)}\end{center} 5018Input : 5019\begin{center}{\tt Beta(5.1,2.2)}\end{center} 5020Output : 5021\begin{center}{\tt 0.0242053671402}\end{center} 5022 5023\subsection{Derivatives of the DiGamma function : {\tt Psi}}\index{Psi} 5024\noindent{\tt Psi} takes as arguments a real $a$ and an integer $n$ (by 5025default $n=0$).\\ 5026{\tt Psi} returns the value of the $n$-th derivative of the DiGamma function 5027at $x=a$, where the DiGamma function is the first derivative 5028of $\ln(\Gamma(x))$. This function is used to evaluated sums of 5029rational functions having poles at integers.\\ 5030Input : 5031\begin{center}{\tt Psi(3,1)}\end{center} 5032Output : 5033\begin{center}{\tt pi\verb|^|2/6-5/4}\end{center} 5034 5035If {\tt n=0}, you may use {\tt Psi(a)} instead of {\tt Psi(a,0)} 5036to compute the value of the DiGamma function at $x=a$.\\ 5037Input : 5038\begin{center}{\tt Psi(3)}\end{center} 5039Output : 5040\begin{center}{\tt Psi(1)+3/2}\end{center} 5041Input : 5042\begin{center}{\tt evalf(Psi(3))}\end{center} 5043Output : 5044\begin{center}{\tt .922784335098}\end{center} 5045 5046\subsection{The $\zeta$ function : {\tt Zeta}}\index{zeta} 5047\noindent{\tt Zeta} takes as argument a real $x$.\\ 5048{\tt Zeta} returns for $x>1$ : 5049\[ \zeta(x)= \sum_{n=1}^{+\infty} \frac{1}{n^x} \] 5050and for $x<1$ its meromorphic continuation.\\ 5051Input : 5052\begin{center}{\tt Zeta(2)}\end{center} 5053Output : 5054\begin{center}{\tt pi\verb|^|2/6}\end{center} 5055Input : 5056\begin{center}{\tt Zeta(4)}\end{center} 5057Output : 5058\begin{center}{\tt pi\verb|^|4/90}\end{center} 5059 5060\subsection{Airy functions : {\tt Airy\_Ai} and {\tt Airy\_Bi}}\index{Airy\_Ai}\index{Airy\_Bi} 5061\noindent{\tt Airy\_Ai} and {\tt Airy\_Bi} take as arguments a real $x$.\\ 5062{\tt Airy\_Ai} and {\tt Airy\_Bi} are two independent solutions 5063of the equation 5064\[ y^{\prime\prime}-x*y=0 \] 5065They are defined by~: 5066\begin{eqnarray*} 5067\mbox{Airy\_Ai}(x) &=& (1/\pi) \int_0^\infty \cos(t^3/3 + x*t) dt \\ 5068\mbox{Airy\_Bi}(x) &=& (1/\pi) \int_0^\infty (e^{- t^3/3} + \sin( t^3/3 + 5069x*t)) dt 5070\end{eqnarray*} 5071Properties :\\ 5072\begin{eqnarray*} 5073 \tt \mbox{Airy\_Ai}(x)&=&\mbox{Airy\_Ai}(0)*f(x)+ 5074\mbox{Airy\_Ai}^\prime (0)*g(x) \\ 5075\tt \mbox{Airy\_Bi}(x)&=&\sqrt{3}(\mbox{Airy\_Ai}(0)*f(x) 5076-\mbox{Airy\_Ai}^\prime (0)*g(x) ) 5077\end{eqnarray*} 5078where $f$ and $g$ are two entire series solutions of 5079\[ w^{\prime\prime}-x*w=0 \] 5080more precisely~: 5081\begin{eqnarray*} 5082f(x)&=&\sum_{k=0}^\infty 3^k\left (\frac{\Gamma(k+\frac{1}{3})}{\Gamma(\frac{1}{3})}\right ) \frac{x^{3k}}{(3k)!}\\ 5083g(x)&=&\sum_{k=0}^\infty 3^k\left 5084 (\frac{\Gamma(k+\frac{2}{3})}{\Gamma(\frac{2}{3})}\right ) 5085\frac{x^{3k+1}}{(3k+1)!} 5086\end{eqnarray*} 5087Input : 5088\begin{center}{\tt Airy\_Ai(1)}\end{center} 5089Output : 5090\begin{center}{\tt 0.135292416313}\end{center} 5091Input : 5092\begin{center}{\tt Airy\_Bi(1)}\end{center} 5093Output : 5094\begin{center}{\tt 1.20742359495}\end{center} 5095Input : 5096\begin{center}{\tt Airy\_Ai(0)}\end{center} 5097Output : 5098\begin{center}{\tt 0.355028053888}\end{center} 5099Input : 5100\begin{center}{\tt Airy\_Bi(0)}\end{center} 5101Output : 5102\begin{center}{\tt 0.614926627446}\end{center} 5103 5104\section{Permutations} 5105A permutation $p$ of size $n$ is a bijection from $[0..n-1]$ on 5106$[0..n-1]$ and is represented by the list : 5107 $[p(0),p(1),p(2)...p(n-1)]$.\\ 5108For example, the permutation $p$ represented by $[1,3,2,0]$ is 5109the application from $[0,1,2,3]$ on $[0,1,2,3]$ defined by : 5110\[ p(0)=1,\ p(1)=3,\ p(2)=2,\ p(3)=0 \] 5111A cycle $c$ of size $p$ is represented by the list 5112$[a_0,...,a_{p-1}]$ ($0\leq a_k\leq n-1$) it is the permutation such that 5113\[ c(a_i)=a_{i+1} \mbox{ for }(i=0..p-2), \quad 5114c(a_{p-1})=a_0, \quad 5115 c(k)=k \mbox{ otherwise }\] 5116A cycle $c$ is represented by a list and a cycle decomposition 5117is represented by a list of lists.\\ 5118For example, the cycle $c$ represented by the list $[3,2,1]$ is the 5119permutation $c$ defined by $c(3)=2,\ c(2)=1,\ c(1)=3,\ c(0)=0$ (i.e. the 5120permutation represented by the list $[0,3,1,2]$). 5121 5122\subsection{Random permutation : {\tt randperm, shuffle}} 5123\index{randperm} 5124\index{shuffle} 5125 5126\noindent{\tt randperm} (or \texttt{shuffle}) takes as argument an integer $n$.\\ 5127{\tt randperm} returns a random permutation of $[0..n-1]$.\\ 5128Input : 5129\begin{center}{\tt randperm(3)}\end{center} 5130Output : 5131\begin{center}{\tt [2,0,1]}\end{center} 5132 5133\subsection{Previous permutation: \texttt{prevperm}}\index{prevperm} 5134 5135The \texttt{prevperm} takes as argument a permutation.\\ 5136\texttt{prevperm} returns the previous permutation in lexicographic 5137order, or \texttt{undef} if there is no previous permutation.\\ 5138Input: 5139\begin{center} 5140 \tt 5141 prevperm([0,3,1,2]) 5142\end{center} 5143Output: 5144\begin{center} 5145 \tt 5146 [0,2,3,1] 5147\end{center} 5148 5149\subsection{Next permutation: \texttt{nextperm}}\index{nextperm} 5150 5151The \texttt{nextperm} takes as argument a permutation.\\ 5152\texttt{nextperm} returns the next permutation in lexicographic 5153order, or \texttt{undef} if there is no next permutation.\\ 5154Input: 5155\begin{center} 5156 \tt 5157 prevperm([0,2,3,1]) 5158\end{center} 5159Output: 5160\begin{center} 5161 \tt 5162 [0,2,1,3] 5163\end{center} 5164 5165\subsection{Decomposition as a product of disjoint cycles : 5166{\tt permu2cycles}}\index{permu2cycles} 5167\noindent {\tt permu2cycles} takes as argument a permutation.\\ 5168{\tt permu2cycles} returns its decomposition as a product of 5169disjoint cycles.\\ 5170Input : 5171\begin{center}{\tt permu2cycles([1,3,4,5,2,0])}\end{center} 5172Output : 5173\begin{center}{\tt [[0,1,3,5],[2,4]]}\end{center} 5174In the answer the cycles of size 1 are omitted, except if $n-1$ is a 5175fixed point of the permutation (this is required to find the value of 5176$n$ from the cycle decomposition).\\ 5177Input : 5178\begin{center}{\tt permu2cycles([0,1,2,4,3,5])}\end{center} 5179Output : 5180\begin{center}{\tt [[5],[3,4]]}\end{center} 5181Input : 5182\begin{center}{\tt permu2cycles([0,1,2,3,5,4])}\end{center} 5183Output : 5184\begin{center}{\tt [[4,5]]}\end{center} 5185 5186\subsection{Product of disjoint cycles to permutation: {\tt cycles2permu}}\index{cycles2permu} 5187\noindent{\tt cycles2permu} takes as argument a list of cycles.\\ 5188{\tt cycles2permu} returns the permutation (of size $n$ chosen as small as 5189possible) that is the product of the given cycles 5190(it is the inverse of {\tt permu2cycles}).\\ 5191Input : 5192\begin{center}{\tt cycles2permu([[1,3,5],[2,4]])}\end{center} 5193Output : 5194\begin{center}{\tt [0,3,4,5,2,1]}\end{center} 5195Input : 5196\begin{center}{\tt cycles2permu([[2,4]])}\end{center} 5197Output : 5198\begin{center}{\tt [0,1,4,3,2]}\end{center} 5199Input : 5200\begin{center}{\tt cycles2permu([[5],[2,4]])}\end{center} 5201Output : 5202\begin{center}{\tt [0,1,4,3,2,5]}\end{center} 5203 5204\subsection{Transform a cycle into permutation : {\tt cycle2perm}}\index{cycle2perm} 5205\noindent{\tt cycle2perm} takes on cycle as argument.\\ 5206{\tt cycle2perm} returns the permutation of size $n$ corresponding 5207to the cycle given as argument, where $n$ is chosen as small 5208as possible (see also {\tt permu2cycles} and {\tt cycles2permu}).\\ 5209Input : 5210\begin{center}{\tt cycle2perm([1,3,5])}\end{center} 5211Output : 5212\begin{center}{\tt [0,3,2,5,4,1]}\end{center} 5213 5214\subsection{Transform a permutation into a matrix : {\tt permu2mat}}\index{permu2mat} 5215\noindent{\tt permu2mat} takes as argument a permutation $p$ of size $n$.\\ 5216{\tt permu2mat} returns the matrix of the permutation, that is 5217the matrix obtained by permuting the rows of the identity matrix of size $n$ 5218with the permutation $p$.\\ 5219Input : 5220\begin{center}{\tt permu2mat([2,0,1])}\end{center} 5221Output : 5222\begin{center}{\tt [[0,0,1],[1,0,0],[0,1,0]]}\end{center} 5223 5224\subsection{Checking for a permutation : {\tt is\_permu}}\index{is\_permu} 5225\noindent{\tt is\_permu} is a boolean function.\\ 5226{\tt is\_permu} takes as argument a list.\\ 5227{\tt is\_permu} returns 1 if the argument is a permutation and returns 0 if the 5228argument is not a permutation.\\ 5229Input : 5230\begin{center}{\tt is\_permu([2,1,3]) }\end{center} 5231Output : 5232\begin{center}{\tt 0}\end{center} 5233Input : 5234\begin{center}{\tt is\_permu([2,1,3,0]) }\end{center} 5235Output : 5236\begin{center}{\tt 1}\end{center} 5237 5238\subsection{Checking for a cycle : {\tt is\_cycle}}\index{is\_cycle} 5239\noindent{\tt is\_cycle} is a boolean function.\\ 5240{\tt is\_cycle} takes a list as argument.\\ 5241{\tt is\_cycle} returns 1 if the argument is a cycle and returns 0 if the 5242argument is not a cycle.\\ 5243Input : 5244\begin{center}{\tt is\_cycle([2,1,3]) }\end{center} 5245Output : 5246\begin{center}{\tt 1}\end{center} 5247Input : 5248\begin{center}{\tt is\_cycle([2,1,3,2]) }\end{center} 5249Output : 5250\begin{center}{\tt 0}\end{center} 5251 5252\subsection{Product of two permutations : {\tt p1op2}}\index{p1op2} 5253\noindent{\tt p1op2} takes as arguments two permutations.\\ 5254{\tt p1op2} returns the permutation obtained by composition : 5255\[ 1^{\mbox{st}}\mbox{arg} \circ 2^{\mbox{nd}} \mbox{arg} \] 5256Input : 5257\begin{center}{\tt p1op2([3,4,5,2,0,1],[2,0,1,4,3,5])}\end{center} 5258Output : 5259\begin{center}{\tt [5,3,4,0,2,1]}\end{center} 5260{\bf Warning}\\ 5261Composition is done using the standard mathematical notation, 5262that is the permutation given as the second argument is performed first. 5263 5264\subsection{Composition of a cycle and a permutation : {\tt c1op2}}\index{c1op2} 5265\noindent{\tt c1op2} takes as arguments a cycle and a permutation.\\ 5266{\tt c1op2} returns the permutation obtained by composition : 5267\[ 1^{\mbox{st}}\mbox{arg} \circ 2^{\mbox{nd}} \mbox{arg} \] 5268Input : 5269\begin{center}{\tt c1op2([3,4,5],[2,0,1,4,3,5])}\end{center} 5270Output : 5271\begin{center}{\tt [2,0,1,5,4,3]}\end{center} 5272{\bf Warning}\\ 5273Composition is done using the standard mathematical notation, 5274that is the permutation given as the second argument is performed first. 5275 5276\subsection{Composition of a permutation and a cycle : {\tt p1oc2}}\index{p1oc2} 5277\noindent{\tt p1oc2} takes as arguments a permutation and a cycle.\\ 5278{\tt p1oc2} returns the permutation obtained by composition : 5279\[ 1^{\mbox{st}}\mbox{arg} \circ 2^{\mbox{nd}} \mbox{arg} \] 5280Input : 5281\begin{center}{\tt p1oc2([3,4,5,2,0,1],[2,0,1])}\end{center} 5282Output : 5283\begin{center}{\tt [4,5,3,2,0,1]}\end{center} 5284{\bf Warning}\\ 5285Composition is done using the standard mathematical notation, 5286that is the cycle given as second argument is performed first. 5287 5288\subsection{Product of two cycles : {\tt c1oc2}}\index{c1oc2} 5289\noindent {\tt c1oc2} takes as arguments two cycles.\\ 5290{\tt c1oc2} returns the permutation obtained by composition : 5291\[ 1^{\mbox{st}}\mbox{arg} \circ 2^{\mbox{nd}} \mbox{arg} \] 5292Input : 5293\begin{center}{\tt c1oc2([3,4,5],[2,0,1])}\end{center} 5294Output : 5295\begin{center}{\tt [1,2,0,4,5,3]}\end{center} 5296{\bf Warning}\\ 5297Composition is done using the standard mathematical notation, 5298that is the cycle given as second argument is performed first. 5299 5300\subsection{Signature of a permutation : {\tt signature}}\index{signature} 5301\noindent{\tt signature} takes as argument a permutation.\\ 5302{\tt signature} returns the signature of the permutation given as argument.\\ 5303The signature of a permutation is equal to : 5304\begin{itemize} 5305\item 1 if the permutation is equal to an even product of transpositions, 5306\item -1 if the permutation is equal to an odd product of transpositions. 5307\end{itemize} 5308The signature of a cycle of size $k$ is : $(-1)^{k+1}$.\\ 5309 Input : 5310\begin{center}{\tt signature([3,4,5,2,0,1])}\end{center} 5311Output : 5312\begin{center}{\tt -1}\end{center} 5313Indeed {\tt permu2cycles([3,4,5,2,0,1])=[[0,3,2,5,1,4]]}. 5314 5315\subsection{Inverse of a permutation : {\tt perminv}}\index{perminv} 5316\noindent{\tt perminv} takes as argument a permutation.\\ 5317{\tt perminv} returns the permutation that is the inverse of the permutation 5318given as argument.\\ 5319Input : 5320\begin{center}{\tt perminv([1,2,0])}\end{center} 5321Output 5322\begin{center}{\tt [2,0,1]}\end{center} 5323 5324\subsection{Inverse of a cycle : {\tt cycleinv}}\index{cycleinv} 5325\noindent{\tt cycleinv} takes as argument a cycle.\\ 5326{\tt cycleinv} returns the cycle that is the inverse of the cycle given as 5327argument.\\ 5328Input : 5329\begin{center}{\tt cycleinv([2,0,1])}\end{center} 5330Output 5331\begin{center}{\tt [1,0,2]}\end{center} 5332 5333\subsection{Order of a permutation : {\tt permuorder}}\index{permuorder} 5334\noindent{\tt permuorder} takes as argument a permutation.\\ 5335{\tt permuorder} returns the order $k$ of the permutation $p$ given as 5336argument, that is the smallest integer $m$ such that $p^m$ is the identity.\\ 5337Input : 5338\begin{center}{\tt permuorder([0,2,1])}\end{center} 5339Output 5340\begin{center}{\tt 2}\end{center} 5341Input : 5342\begin{center}{\tt permuorder([3,2,1,4,0])}\end{center} 5343Output 5344\begin{center}{\tt 6}\end{center} 5345 5346\subsection{Group generated by two permutations : {\tt groupermu}}\index{groupermu} 5347\noindent{\tt groupermu} takes as argument two permutations {\tt a} and 5348{\tt b}.\\ 5349{\tt groupermu} returns the group of the permutations generated by {\tt a} and 5350{\tt b}.\\ 5351Input : 5352\begin{center}{\tt groupermu([0,2,1,3],[3,1,2,0])}\end{center} 5353Output 5354\begin{center}{\tt [[0,2,1,3],[3,1,2,0],[0,1,2,3],[3,2,1,0]]}\end{center} 5355 5356\section{Complex numbers} 5357Note that complex numbers are also used to represent a point in the 5358plane or a 1-d function graph. 5359 5360\subsection{Usual complex functions : {\tt +,-,*,/,\^\ }}\index{+}\index{'+'}\index{-}\index{'-'}\index{\^\ } 5361\noindent {\tt +,-,*,/,\^\ } are the usual operators to perform 5362additions, subtractions, multiplications, divisions and for raising to an 5363integer or a fractional power.\\ 5364Input : 5365\begin{center}{\tt (1+2*i)\verb|^|2}\end{center} 5366Output : 5367\begin{center}{\tt -3+4*i}\end{center} 5368 5369\subsection{Real part of a complex number : {\tt re real}}\index{re}\index{real} 5370\noindent{\tt re} (or {\tt real}) takes as argument a complex number (resp. a 5371point $A$).\\ 5372{\tt re} (or {\tt real}) returns the real part of this complex number (resp. 5373the projection on the $x$ axis of $A$).\\ 5374Input : 5375\begin{center}{\tt re(3+4*i)}\end{center} 5376Output : 5377\begin{center}{\tt 3}\end{center} 5378 5379\subsection{Imaginary part of a complex number : {\tt im imag}}\index{im}\index{imag} 5380\noindent{\tt im} (or {\tt imag}) takes as argument a complex number (resp. a 5381point $A$).\\ 5382{\tt im} (or {\tt imag}) returns imaginary part of this complex number (resp. 5383the projection on the $y$ axis of $A$).\\ 5384Input : 5385\begin{center}{\tt im(3+4*i)}\end{center} 5386Output : 5387\begin{center}{\tt 4}\end{center} 5388 5389\subsection{Write a complex as {\tt re(z)+i*im(z)} : {\tt evalc}}\index{evalc} 5390\noindent{\tt evalc} takes as argument a complex number {\tt z}.\\ 5391{\tt evalc} returns this complex number, written as 5392{\tt re(z)+i*im(z)}.\\ 5393Input : 5394\begin{center}{\tt evalc(sqrt(2)*exp(i*pi/4))}\end{center} 5395Output : 5396\begin{center}{\tt 1+i}\end{center} 5397 5398\subsection{Modulus of a complex number : {\tt abs}}\index{abs} 5399\noindent{\tt abs} takes as argument a complex number.\\ 5400{\tt abs} returns the modulus of this complex number.\\ 5401Input : 5402\begin{center}{\tt abs(3+4*i)}\end{center} 5403Output : 5404\begin{center}{\tt 5}\end{center} 5405 5406\subsection{Argument of a complex number : {\tt arg}}\index{arg|textbf} 5407\noindent{\tt arg} takes as argument a complex number.\\ 5408{\tt arg} returns the argument of this complex number.\\ 5409Input : 5410\begin{center}{\tt arg(3+4*i)}\end{center} 5411Output : 5412\begin{center}{\tt atan(4/3)}\end{center} 5413 5414\subsection{The normalized complex number : {\tt normalize unitV}}\index{unitV}\index{normalize} 5415\noindent{\tt normalize} or {\tt unitV} takes as argument a complex number.\\ 5416{\tt normalize} or {\tt unitV} returns the complex number divided by the 5417modulus of this complex number.\\ 5418Input : 5419\begin{center}{\tt normalize(3+4*i)}\end{center} 5420Output : 5421\begin{center}{\tt (3+4*i)/5}\end{center} 5422 5423\subsection{Conjugate of a complex number : {\tt conj}}\index{conj|textbf} 5424\noindent{\tt conj} takes as argument a complex number.\\ 5425{\tt conj} returns the complex conjugate of this complex number.\\ 5426Input : 5427\begin{center}{\tt conj(3+4*i)}\end{center} 5428Output : 5429\begin{center}{\tt 3-4*i}\end{center} 5430 5431\subsection{Multiplication by the complex conjugate : 5432 {\tt mult\_c\_conjugate}}\index{mult\_c\_conjugate} 5433\noindent {\tt mult\_c\_conjugate} takes as argument an complex expression.\\ 5434If this expression has a complex denominator, 5435{\tt mult\_c\_conjugate} multiplies the numerator and the denominator of this 5436 expression by the complex conjugate of the denominator.\\ 5437If this expression does not have a complex denominator, 5438{\tt mult\_c\_conjugate} multiplies the numerator and the denominator of this 5439expression by the complex conjugate of the numerator.\\ 5440Input : 5441\begin{center}{\tt mult\_c\_conjugate((2+i)/(2+3*i))}\end{center} 5442Output : 5443\begin{center}{\tt (2+i)*(2+3*(-i))/((2+3*(i))*(2+3*(-i)))}\end{center} 5444Input : 5445\begin{center}{\tt mult\_c\_conjugate((2+i)/2)}\end{center} 5446Output : 5447\begin{center}{\tt (2+i)*(2+-i)/(2*(2+-i))}\end{center} 5448 5449\subsection{Barycenter of complex numbers : {\tt barycenter}}\index{barycenter}\label{sec:baryc} 5450%{\bf See also :} \ref{sec:barycentre2} and \ref{sec:barycentre3}.\\ 5451\noindent{\tt barycenter} takes as argument two lists of the same size 5452(resp. a matrix with two columns): 5453\begin{itemize} 5454\item the elements of the first list (resp. column) 5455are points $A_j$ or complex numbers $a_j$ (the affixes of the points), 5456\item the elements of the second list (resp. column) are real coefficients 5457$\alpha_j$ such that $\sum \alpha_j \neq 0$. 5458\end{itemize} 5459{\tt barycenter} returns the barycenter point of the points $A_j$ 5460weighted by the real coefficients $\alpha_j$. 5461If $\sum \alpha_j = 0$, {\tt barycenter} returns an 5462error.\\ 5463{\bf Warning} To have a complex number in the output, the input must be :\\ 5464{\tt affix(barycenter(...,...))} 5465because {\tt barycenter(...,...)} returns a point, not a complex number.\\ 5466Input : 5467\begin{center}{\tt affix(barycenter([1+i,1-i],[1,1]))}\end{center} 5468or : 5469\begin{center}{\tt affix(barycenter([[1+i,1],[1-i,1]]))}\end{center} 5470Output : 5471\begin{center}{\tt i}\end{center} 5472 5473\section{Algebraic numbers} 5474 5475\subsection{Definition} 5476 5477A real algebraic number is a real root of a polynomial with integer 5478coefficients. 5479 5480A complex algebraic number is a root of a polynomial with coefficients 5481which are Gaussian integers. 5482 5483\subsection{Minimum polynomial of an algebraic number:\texttt{pmin}} 5484\index{pmin} 5485 5486The \texttt{pmin} command takes as argument an algebraic number, and 5487an optional second argument of a variable name.\\ 5488\texttt{pmin} returns the the monic polynomial of smallest degree with integer 5489coefficents which has the algebraic number as a root. If there is a 5490second argument, the polynomial will use that as a variable.\\ 5491Input: 5492\begin{center} 5493 \tt 5494 pmin(sqrt(2) + sqrt(3)) 5495\end{center} 5496Output: 5497\begin{center} 5498 \tt 5499 poly1[1,0,-10,0,1] 5500\end{center} 5501Input: 5502\begin{center} 5503 \tt 5504 pmin(sqrt(2) + sqrt(3),x) 5505\end{center} 5506Output: 5507\begin{center} 5508 \tt 5509 x\^{}4-10*x\^{}2+1 5510\end{center} 5511Note that $(\sqrt{2} + \sqrt{3})^2 = 5 + 2\sqrt{6}$ and so 5512$((\sqrt{2} + \sqrt{3})^2 - 5)^2 = 24$, which can be rewritten as 5513$(\sqrt{2} + \sqrt{3})^4 - 10 (\sqrt{2} + \sqrt{3})^2 + 1 = 0$. 5514 5515\noindent 5516Input: 5517\begin{center} 5518 \tt 5519 pmin(sqrt(2) + i*sqrt(3)) 5520\end{center} 5521Output: 5522\begin{center} 5523 \tt 5524 poly1[1,0,2,0,25] 5525\end{center} 5526Input: 5527\begin{center} 5528 \tt 5529 pmin(sqrt(2) + i*sqrt(3),z) 5530\end{center} 5531Output: 5532\begin{center} 5533 \tt 5534 z\^{}4+2*z\^{}2+25 5535\end{center} 5536Input: 5537\begin{center} 5538 \tt 5539 pmin(sqrt(2) + 2*i) 5540\end{center} 5541Output: 5542\begin{center} 5543 \tt 5544 poly1[1,0,4,0,36] 5545\end{center} 5546Input: 5547\begin{center} 5548 \tt 5549 pmin(sqrt(2) + 2*i,z) 5550\end{center} 5551Output: 5552\begin{center} 5553 \tt 5554 z\^{}4+4*z\^{}2+36 5555\end{center} 5556 5557\section{Algebraic expressions} 5558\subsection{Evaluate an expression : {\tt eval}}\index{eval} 5559\noindent {\tt eval} is used to evaluate an expression. Since 5560{\tt Xcas} always evaluate expressions entered in the command line, 5561{\tt eval} is mainly used 5562to evaluate a sub-expression in the equation writer.\\ 5563Input : 5564\begin{center}{\tt a:=2}\end{center} 5565Output : 5566\begin{center}{\tt 2}\end{center} 5567Input : 5568\begin{center}{\tt eval(2+3*a)}\end{center} 5569or 5570\begin{center}{\tt 2+3*a}\end{center} 5571Output : 5572\begin{center}{\tt 8}\end{center} 5573 5574\subsection{Change the evaluation level: \texttt{eval\_level}}\index{eval\_level} 5575 5576The evaluation level is the maximum number of recursions when 5577evaluating expressions, which is 25 by default. It can be set with 5578the \texttt{eval} box in the CAS configuration screen (see section 5579\ref{ssec:confcomp}). 5580 5581The \texttt{eval\_level} command takes either zero or one argument. 5582The single argument is a non-negative integer.\\ 5583With no argument, \texttt{eval\_level} returns the current evaluation 5584level. With argument $n$, \texttt{eval\_level} sets the evaluation 5585level to $n$. 5586 5587\noindent 5588Input: 5589\begin{center} 5590 \tt 5591 purge(a,b,c); a:=b+1; b:=c+1; c:=3; 5592\end{center} 5593Input: 5594\begin{center} 5595 \tt 5596 eval\_level(0) 5597\end{center} 5598Input: 5599\begin{center} 5600 \tt 5601 a,b,c 5602\end{center} 5603Output: 5604\begin{center} 5605 \tt 5606 a,b,c 5607\end{center} 5608Input: 5609\begin{center} 5610 \tt 5611 eval\_level(1) 5612\end{center} 5613Input: 5614\begin{center} 5615 \tt 5616 a,b,c 5617\end{center} 5618Output: 5619\begin{center} 5620 \tt 5621 b+1,c+1,3 5622\end{center} 5623Input: 5624\begin{center} 5625 \tt 5626 eval\_level(2) 5627\end{center} 5628Input: 5629\begin{center} 5630 \tt 5631 c+2,4,3 5632\end{center} 5633Input: 5634\begin{center} 5635 \tt 5636 eval\_level(3) 5637\end{center} 5638Output: 5639\begin{center} 5640 \tt 5641 a,b,c 5642\end{center} 5643Input: 5644\begin{center} 5645 \tt 5646 a,b,c 5647\end{center} 5648Output: 5649\begin{center} 5650 \tt 5651 a,b,c 5652\end{center} 5653Input: 5654\begin{center} 5655 \tt 5656 a,b,c 5657\end{center} 5658Output: 5659\begin{center} 5660 \tt 5661 a,b,c 5662\end{center} 5663 5664\subsection{Evaluate algebraic expressions : {\tt evala}} 5665\index{evala} 5666\noindent In Maple, {\tt evala} is used to evaluate an expression with 5667algebraic extensions. In {\tt Xcas}, {\tt evala} is not necessary, it 5668behaves like {\tt eval}.\\ 5669 5670\subsection{Prevent evaluation : {\tt quote hold '}}\index{quote|textbf}\index{hold|textbf}\index{'|textbf} 5671A quoted subexpression (either with {\tt '} or with the 5672{\tt quote} or {\tt hold}) command will not be evaluated.\\ 5673{\bf Remark} 5674{\tt a:=quote(a)} (or {\tt a:=hold(a)}) is equivalent to {\tt purge(a)} 5675(for the sake of Maple compatibility). It returns 5676the value of this variable (or the hypothesis done on this variable). \\ 5677Input : 5678\begin{center}{\tt a:=2;quote(2+3*a)}\end{center} 5679or 5680\begin{center}{\tt a:=2;'2+3*a'}\end{center} 5681Output : 5682\begin{center}{\tt (2,2+3*a)}\end{center} 5683 5684\subsection{Force evaluation : {\tt unquote}}\index{unquote} 5685{\tt unquote} is used to evaluate inside a quoted expression.\\ 5686For example in an affectation, the variable is automatically quoted 5687(not evaluated) so that the user does not have to quote it explicitly 5688each time he want to modify its value. In some circumstances, you 5689might however want to evaluate it. 5690\\ 5691Input: 5692\begin{center}{\tt purge(b);a:=b;unquote(a):=3}\end{center} 5693Output : 5694\begin{center}{\tt b contains 3, hence a evals to 3}\end{center} 5695 5696\subsection{Distribution : {\tt expand fdistrib}}\index{fdistrib}\index{expand} 5697\noindent {\tt expand} or {\tt fdistrib} takes as argument an expression.\\ 5698{\tt expand} or {\tt fdistrib} returns the expression where 5699multiplication is distributed with respect to the addition.\\ 5700Input : 5701\begin{center}{\tt expand((x+1)*(x-2))}\end{center} 5702or : 5703\begin{center}{\tt fdistrib((x+1)*(x-2))}\end{center} 5704Output : 5705\begin{center}{\tt x\verb|^|2-2*x+x-2}\end{center} 5706 5707\subsection{Canonical form : {\tt canonical\_form}}\index{canonical\_form} 5708\noindent{\tt canonical\_form} takes as argument a trinomial of second 5709degree.\\ 5710{\tt canonical\_form} returns the canonical form of the argument.\\ 5711Example :\\ 5712Find the canonical form of : 5713$$x^2-6x+1$$ 5714Input : 5715\begin{center}{\tt canonical\_form(x\verb|^|2-6*x+1)}\end{center} 5716Output : 5717\begin{center}{\tt (x-3)\verb|^|2-8}\end{center} 5718 5719\subsection{Multiplication by the conjugate quantity : 5720{\tt mult\_conjugate}}\index{mult\_conjugate} 5721\noindent {\tt mult\_conjugate} takes as argument an expression with a 5722denominator or a numerator supposed to contain a square root : 5723\begin{itemize} 5724\item if the denominator contains a square root,\\ 5725{\tt mult\_conjugate} multiplies the numerator and the denominator 5726of the expression by the conjugate quantity of the denominator. 5727\item otherwise, if the numerator contains a square root,\\ 5728{\tt mult\_conjugate} multiplies the numerator and the denominator of this 5729expression by the conjugate quantity of the numerator. 5730\end{itemize} 5731Input : 5732\begin{center}{\tt mult\_conjugate((2+sqrt(2))/(2+sqrt(3)))}\end{center} 5733Output : 5734\begin{center}{\tt (2+sqrt(2))*(2-sqrt(3))/((2+sqrt(3))*(2-sqrt(3)))}\end{center} 5735Input : 5736\begin{center}{\tt mult\_conjugate((2+sqrt(2))/(sqrt(2)+sqrt(3)))}\end{center} 5737Output : 5738\begin{center}{\tt (2+sqrt(2))*(-sqrt(2)+sqrt(3))/}\end{center} 5739\begin{center}{\tt ((sqrt(2)+sqrt(3))*(-sqrt(2)+sqrt(3)))}\end{center} 5740Input : 5741\begin{center}{\tt mult\_conjugate((2+sqrt(2))/2)}\end{center} 5742Output : 5743\begin{center}{\tt (2+sqrt(2))*(2-sqrt(2))/(2*(2-sqrt(2)))}\end{center} 5744 5745\subsection{Separation of variables : {\tt split}}\index{split} 5746\noindent{\tt split} takes two arguments : an expression depending 5747on two variables and the list of these two variables.\\ 5748If the expression may be factorized into two factors 5749where each factor depends 5750only on one variable, {\tt split} returns the list of this two 5751factors, otherwise it returns the list {\tt [0]}.\\ 5752Input : 5753\begin{center}{\tt split((x+1)*(y-2),[x,y])}\end{center} 5754or : 5755\begin{center}{\tt split(x*y-2*x+y-2,[x,y])}\end{center} 5756Output : 5757\begin{center}{\tt [x+1,y-2]}\end{center} 5758Input : 5759\begin{center}{\tt split((x\verb|^|2*y\verb|^|2-1,[x,y])}\end{center} 5760Output : 5761\begin{center}{\tt [0]}\end{center} 5762 5763\subsection{Factorization : {\tt factor}}\index{factor|textbf}\label{sec:factore} 5764\noindent{\tt factor} takes as argument an expression.\\ 5765{\tt factor} factorizes this expression on the field of its coefficients, 5766with the addition of $i$ in complex mode. If {\tt sqrt} is enabled 5767in the Cas configuration, polynomials of order 2 are factorized in 5768complex mode or in real mode if the discriminant is positive.\\ 5769{\bf Examples} 5770\begin{enumerate} 5771\item Factorize $x^4-1$ over $\mathbb Q$.\\ 5772Input : 5773\begin{center}{\tt factor(x\verb|^|4-1)}\end{center} 5774Output : 5775\begin{center}{\tt (x\verb|^|2+1)*(x+1)*(x-1)}\end{center} 5776The coefficients are rationals, hence the factors are polynomials with 5777rationals coefficients.\\ 5778\item Factorize $x^4-1$ over $\mathbb Q[i]$ \\ 5779To have a complex factorization, check {\tt complex} in the {\tt cas} 5780configuration (red button displaying the status line).\\ 5781Input : 5782\begin{center}{\tt factor(x\verb|^|4-1)}\end{center} 5783Output : 5784\begin{center}{\tt -i*(-x+-i)*(i*x+1)*(-x+1)*(x+1)}\end{center} 5785\item Factorize $x^4+1$ over $\mathbb Q$\\ 5786Input : 5787\begin{center}{\tt factor(x\verb|^|4+1)}\end{center} 5788Output : 5789\begin{center}{\tt x\verb|^|4+1}\end{center} 5790Indeed $ x^4+1$ has no factor with rational coefficients.\\ 5791\item Factorize $x^4+1$ over $\mathbb Q[i]$\\ 5792Check {\tt complex} in the {\tt cas} 5793configuration (red button rouge displaying the status line).\\ 5794Input : 5795\begin{center}{\tt factor(x\verb|^|4-1)}\end{center} 5796Output : 5797\begin{center}{\tt (x\verb|^|2+i)*(x\verb|^|2+-i)}\end{center} 5798\item Factorize $x^4+1$ over $\mathbb R$.\\ 5799You have to provide the square root required for extending the 5800rationals. In order to do that with the help of {\tt Xcas}, 5801first check {\tt complex} in the {\tt cas} 5802configuration and input :\\ 5803\begin{center}{\tt solve(x\verb|^|4+1,x)}\end{center}\index{solve}\index{resoudre} 5804Output : 5805\begin{center}{\tt [sqrt(2)/2+(i)*sqrt(2)/2,sqrt(2)/2+(i)*(-(sqrt(2)/2)), 5806 -sqrt(2)/2+(i)*sqrt(2)/2,-sqrt(2)/2+(i)*(-(sqrt(2)/2))]}\end{center} 5807The roots depends on $\sqrt 2$. Uncheck complex mode in the Cas configuration 5808and input : 5809\begin{center}{\tt factor(x\verb|^|4+1,sqrt(2))}\end{center} 5810Output : 5811\begin{center}{\tt (x\verb|^|2+sqrt(2)*x+1)*(x\verb|^|2+(-(sqrt(2)))*x+1)}\end{center} 5812To factorize over $\mathbb C$, check {\tt complex} in the 5813{\tt cas} configuration or input {\tt cFactor(x\verb|^|4+1,sqrt(2))} 5814(cf {\tt cFactor}). 5815\end{enumerate} 5816 5817\subsection{Complex factorization : {\tt cFactor}}\index{cFactor} 5818\noindent{\tt cFactor} takes as argument an expression.\\ 5819{\tt cFactor} factorizes this expression on the field 5820$\mathbb Q[i] \subset \mathbb C$ (or over the complexified field of 5821the coefficients of the argument) even if you are in real mode.\\ 5822{\bf Examples} 5823\begin{enumerate} 5824\item Factorize $x^4-1$ over $\mathbb Z[i]$.\\ 5825Input : 5826\begin{center}{\tt cFactor(x\verb|^|4-1)}\end{center} 5827Output : 5828\begin{center}{\tt -((x+-i)*((-i)*x+1)*((-i)*x+i)*(x+1))}\end{center} 5829\item Factorize $x^4+1$ over $\mathbb Z[i]$.\\ 5830Input : 5831\begin{center}{\tt cFactor(x\verb|^|4+1)}\end{center} 5832Output : 5833\begin{center}{\tt (x\verb|^|2+i)*(x\verb|^|2+-i)}\end{center} 5834\item For a complete factorization of $x^4+1$, 5835check the sqrt box in the Cas configuration or input : 5836\begin{center}{\tt cFactor(x\verb|^|4+1,sqrt(2))}\end{center} 5837Output : 5838\begin{center}{\tt sqrt(2)*1/2*(sqrt(2)*x+1-i)*(sqrt(2)*x-1+i)*sqrt(2)* 1/2*(sqrt(2)*x+1+i)*(sqrt(2)*x-1-i)}\end{center} 5839\end{enumerate} 5840 5841\subsection{Zeros of an expression : {\tt zeros}}\index{zeros} 5842\noindent{\tt zeros} takes as argument an expression depending on $x$.\\ 5843{\tt zeros} returns a list of values of $x$ where the expression 5844vanishes. The list may be incomplete in exact mode if the expression 5845is not polynomial or if intermediate 5846factorizations have irreducible factors of order strictly 5847greater than 2.\\ 5848In real mode, (complex box unchecked in the Cas configuration 5849or {\tt complex\_mode:=0}), only reals zeros are returned. In 5850({\tt complex\_mode:=1}) reals and complex zeros are returned. See 5851also {\tt cZeros} to get complex zeros in real mode.\\ 5852Input in real mode : 5853\begin{center}{\tt zeros(x\verb|^|2+4)}\end{center} 5854Output : 5855\begin{center}{\tt []}\end{center} 5856Input in complex mode : 5857\begin{center}{\tt zeros(x\verb|^|2+4)}\end{center} 5858Output : 5859\begin{center}{\tt [-2*i,2*i]}\end{center} 5860Input in real mode : 5861\begin{center}{\tt zeros(ln(x)\verb|^|2-2)}\end{center} 5862Output : 5863\begin{center}{\tt [exp(sqrt(2)),exp(-(sqrt(2)))]}\end{center} 5864Input in real mode : 5865\begin{center}{\tt zeros(ln(y)\verb|^|2-2,y)}\end{center} 5866Output : 5867\begin{center}{\tt [exp(sqrt(2)),exp(-(sqrt(2)))]}\end{center} 5868Input in real mode : 5869\begin{center}{\tt zeros(x*(exp(x))\verb|^|2-2*x-2*(exp(x))\verb|^|2+4)}\end{center} 5870Output : 5871 \begin{center}{\tt [[log(sqrt(2)),2]}\end{center} 5872 5873\subsection{Complex zeros of an expression : {\tt cZeros}}\index{cZzeros} 5874\noindent{\tt cZeros} takes as argument an expression depending on $x$.\\ 5875{\tt cZeros} returns a list of complex values of $x$ where the expression 5876vanishes. The list may be incomplete in exact mode if the expression 5877is not polynomial or if intermediate 5878factorizations have irreducible factors of order strictly 5879greater than 2.\\ 5880Input in real or complex mode : 5881\begin{center}{\tt cZeros(x\verb|^|2+4)}\end{center} 5882Output : 5883\begin{center}{\tt [-2*i,2*i]}\end{center} 5884Input : 5885\begin{center}{\tt cZeros(ln(x)\verb|^|2-2)}\end{center} 5886Output : 5887\begin{center}{\tt [exp(sqrt(2)),exp(-(sqrt(2)))]}\end{center} 5888Input : 5889\begin{center}{\tt cZeros(ln(y)\verb|^|2-2,y)}\end{center} 5890Output : 5891\begin{center}{\tt [exp(sqrt(2)),exp(-(sqrt(2)))]}\end{center} 5892Input : 5893\begin{center}{\tt cZeros(x*(exp(x))\verb|^|2-2*x-2*(exp(x))\verb|^|2+4)}\end{center} 5894Output : 5895\begin{center}{\tt [log(sqrt(2)),log(-sqrt(2)),2]}\end{center} 5896 5897\subsection{Regrouping expressions: \texttt{regroup}}\index{regroup} 5898 5899The \texttt{regroup} command takes as parameter an expression.\\ 5900\texttt{regroup} returns the expression with obvious simplifications.\\ 5901Input: 5902\begin{center} 5903 \tt 5904 regroup(x + 3 * x + 5 * 4 / x) 5905\end{center} 5906Output: 5907\begin{center} 5908 \tt 5909 4*x+20/x 5910\end{center} 5911 5912\subsection{Normal form : {\tt normal}}\index{normal|textbf} 5913\noindent{\tt normal} takes as argument an expression. 5914The expression is considered as a rational fraction with respect 5915to generalized identifiers 5916(either true identifiers or transcendental functions replaced by 5917a temporary identifiers) with coefficients in $\mathbb Q$ or $\mathbb Q[i]$ 5918or in an algebraic extension (e.g. $\mathbb Q[\sqrt{2}]$). 5919{\tt normal} returns the expanded irreducible representation 5920of this rational fraction. See also {\tt ratnormal} for pure rational 5921fractions or {\tt simplify} if the transcendental functions are 5922not algebraically independent.\\ 5923Input : 5924\begin{center}{\tt normal((x-1)*(x+1))}\end{center} 5925Output : 5926 \begin{center}{\tt x\verb|^|2-1}\end{center} 5927{\bf Remarks} 5928\begin{itemize} 5929\item Unlike {\tt simplify}, 5930{\tt normal} does not try to find algebraic relations between 5931transcendental functions like $\cos(x)^2+\sin(x)^2=1$. 5932\item 5933It is sometimes necessary to run the {\tt normal} command twice to 5934get a fully irreducible representation of an expression 5935containing algebraic extensions. 5936\end{itemize} 5937%% Input : 5938%% \begin{center}{\tt normal(3-54*sqrt(1/162))}\end{center} 5939%% Output : 5940%% \begin{center}{\tt (-9*sqrt(2)+9)/3}\end{center} 5941%% Input : 5942%% \begin{center}{\tt normal((-9*sqrt(2)+9)/3)}\end{center} 5943%% Output : 5944%% \begin{center}{\tt -(3*sqrt(2))+3}\end{center} 5945 5946\subsection{Simplify : {\tt simplify}}\index{simplify|textbf} 5947\noindent{\tt simplify} simplifies an expression. It behaves 5948like {\tt normal} for rational fractions and algebraic extensions. 5949For expressions 5950containing transcendental functions, {\tt simplify} tries first to rewrite 5951them in terms of algebraically independent transcendental functions. 5952For trigonometric expressions, this requires radian mode 5953(check {\tt radian} in the {\tt cas} 5954configuration or input {\tt angle\_radian:=1}).\\ 5955Input : 5956\begin{center}{\tt simplify((x-1)*(x+1))}\end{center} 5957Output : 5958 \begin{center}{\tt x\verb|^|2-1}\end{center} 5959Input : 5960\begin{center}{\tt simplify(3-54*sqrt(1/162))}\end{center} 5961Output : 5962 \begin{center}{\tt -3*sqrt(2)+3}\end{center} 5963Input : 5964\begin{center}{\tt simplify((sin(3*x)+sin(7*x))/sin(5*x))}\end{center} 5965Output : 5966\begin{center}{\tt 4*(cos(x))\verb|^|2-2}\end{center} 5967 5968\subsection{Automatic simplification: \texttt{autosimplify}}\index{autosimplify} 5969 5970The \texttt{autosimplify} command takes a single argument; a command 5971that will be used to rewrite the results in \texttt{Xcas}, such as 5972\texttt{simplify}, \texttt{factor}, \texttt{regroup}, or for no 5973simplification, \texttt{nop}. When \texttt{Xcas} starts, the 5974autosimplify command is \texttt{regroup}. 5975 5976To change the simplification mode during a session, the 5977\texttt{autosimplify} command should be on its own line.\\ 5978Input: 5979\begin{center} 5980 \tt 5981 autosimplify(nop) 5982\end{center} 5983then: 5984\begin{center} 5985 \tt 5986 1 + x\^{}2 - 2 5987\end{center} 5988Output: 5989\begin{center} 5990 \tt 5991 1+x\^{}2-2 5992\end{center} 5993Input: 5994\begin{center} 5995 \tt 5996 autosimplify(simplify) 5997\end{center} 5998then: 5999\begin{center} 6000 \tt 6001 1 + x\^{}2 - 2 6002\end{center} 6003Output: 6004\begin{center} 6005 \tt 6006 x\^{}2 - 1 6007\end{center} 6008Input: 6009\begin{center} 6010 \tt 6011 autosimplify(factor) 6012\end{center} 6013then: 6014\begin{center} 6015 \tt 6016 1 + x\^{}2 - 2 6017\end{center} 6018Output: 6019\begin{center} 6020 \tt 6021 (x-1)*(x+1) 6022\end{center} 6023Input: 6024\begin{center} 6025 \tt 6026 autosimplify(regroup) 6027\end{center} 6028then: 6029\begin{center} 6030 \tt 6031 1 + x\^{}2 - 2 6032\end{center} 6033Output: 6034\begin{center} 6035 \tt 6036 x\^{}2 - 1 6037\end{center} 6038 6039\subsection{Normal form for rational fractions : {\tt ratnormal}}\index{ratnormal} 6040\noindent{\tt ratnormal} rewrites an expression using 6041its irreducible representation. The expression is viewed 6042as a multivariate rational fraction with 6043coefficients in $\mathbb Q$ (or $\mathbb Q[i]$). The variables are 6044generalized identifiers which are assumed to be algebraically independent. 6045Unlike with {\tt normal}, an algebraic extension 6046is considered as a generalized identifier. Therefore {\tt ratnormal} 6047is faster but might miss some simplifications if 6048the expression contains radicals or algebraically dependent transcendental 6049functions.\\ 6050Input : 6051\begin{center}{\tt ratnormal((x\verb|^|3-1)/(x\verb|^|2-1))}\end{center} 6052Output : 6053 \begin{center}{\tt (x\verb|^|2+x+1)/(x+1)}\end{center} 6054Input : 6055\begin{center}{\tt ratnormal((-2x\verb|^|3+3x\verb|^|2+5x-6)/(x\verb|^|2-2x+1))}\end{center} 6056Output : 6057 \begin{center}{\tt (-2*x\verb|^|2+x+6)/(x-1)}\end{center} 6058 6059\subsection{Substitute a variable by a value: \texttt{|}}\index{|} 6060 6061The \texttt{|} operator is infixed. The left hand side is 6062an expression depending on one or more parameters, the right hand side 6063is an equality or several equalities (parameter = value, parameter = 6064value, \dots).\\ 6065The \texttt{|} operator returns the expression with the parameters 6066replaced by the given values.\\ 6067Input: 6068\begin{center} 6069 \tt 6070 a\^{}2 + 1 | a = 2 6071\end{center} 6072Output (even if \texttt{a} has been assigned a value): 6073\begin{center} 6074 \tt 6075 5 6076\end{center} 6077Input: 6078\begin{center} 6079 \tt 6080 a\^{}2 + b | a = 2, b = 3 6081\end{center} 6082Output (even if \texttt{a} or \texttt{b} had been assigned a value): 6083\begin{center} 6084 \tt 6085 7 6086\end{center} 6087 6088\subsection{Substitute a variable by a value : {\tt subst}}\index{subst|textbf}\label{sec:subst} 6089\noindent{\tt subst} takes two or three arguments : 6090\begin{itemize} 6091\item an expression depending on a variable, 6092an equality (variable=value of substitution) or a list of equalities. 6093\item an expression depending on a variable, a variable or a list 6094of variables, a value or a list of values for substitution. 6095\end{itemize} 6096{\tt subst} returns the expression with the substitution done. 6097Note that {\tt subst} does not quote its argument, hence 6098in a normal evaluation process, the substitution variable should 6099be purged otherwise it will be replaced by its assigned value 6100before substitution is done.\\ 6101Input : 6102\begin{center}{\tt subst(a\verb|^|2+1,a=2)}\end{center} 6103or : 6104\begin{center}{\tt subst(a\verb|^|2+1,a,2)}\end{center} 6105Output (if the variable {\tt a} is purged else first input {\tt purge(a)}) : 6106\begin{center}{\tt 5}\end{center} 6107Input : 6108\begin{center}{\tt subst(a\verb|^|2+b,[a,b],[2,1])}\end{center} 6109or : 6110\begin{center}{\tt subst(a\verb|^|2+b,[a=2,b=1])}\end{center} 6111Output (if the variables {\tt a} and {\tt b} are purged else first input 6112{\tt purge(a,b)}) : 6113 \begin{center}{\tt 5}\end{center} 6114{\tt subst} may also be used to make a change of variable in an integral. 6115In this case the {\tt integrate} command should be quoted 6116(otherwise, the integral would be computed before substitution) or 6117the inert form {\tt Int} should be used. 6118In both cases, the name of the integration variable must be given as 6119argument of {\tt Int} or {\tt integrate} even you are integrating 6120with respect to {\tt x}.\\ 6121Input : 6122\begin{center}{\tt subst('integrate(sin(x\verb|^|2)*x,x,0,pi/2)',x=sqrt(t))}\end{center} 6123or : 6124\begin{center}{\tt subst(Int(sin(x\verb|^|2)*x,x,0,pi/2),x=sqrt(t))}\end{center} 6125Output 6126\begin{center}{\tt integrate(sin(t)*sqrt(t)*1/2*1/t*sqrt(t),t,0,(pi/2)\verb|^|2)}\end{center} 6127Input : 6128\begin{center}{\tt subst('integrate(sin(x\verb|^|2)*x,x)',x=sqrt(t))}\end{center} 6129or : 6130\begin{center}{\tt subst(Int(sin(x\verb|^|2)*x,x),x=sqrt(t))}\end{center} 6131Output 6132\begin{center}{\tt integrate(sin(t)*sqrt(t)*1/2*1/t*sqrt(t),t)}\end{center} 6133 6134\subsection{Substitute a variable by a value: \texttt{()}}\index{()} 6135 6136Given an expression with variables, you can substitute a variable by a 6137value with the \texttt{|} operator or the \texttt{subst} command.\\ 6138Input: 6139\begin{center} 6140 \tt 6141 Expr := x + 2*y + 3*z 6142\end{center} 6143then: 6144\begin{center} 6145 \tt 6146 subst(Expr,[x=1,y=2]) 6147\end{center} 6148or: 6149\begin{center} 6150 \tt 6151 Expr | x=1, y=2 6152\end{center} 6153Output: 6154\begin{center} 6155 \tt 6156 5+3*z 6157\end{center} 6158 6159One other way to do this is with something akin to functional 6160notation; following the expression with equalities of the form 6161variable = value.\\ 6162Input: 6163\begin{center} 6164 \tt 6165 Expr(x=1,y=2) 6166\end{center} 6167Output: 6168\begin{center} 6169 \tt 6170 5+3*z 6171\end{center} 6172Input: 6173\begin{center} 6174 \tt 6175 (h*k*t\^{}2+h\^{}3*t\^{}3)(t=2) 6176\end{center} 6177Output: 6178\begin{center} 6179 \tt 6180 h*k*4+8*h\^{}3 6181\end{center} 6182 6183\subsection{Substitute a variable by a value (Maple and Mupad compatibility) : {\tt subs}}\index{subs}\label{sec:subs} 6184\noindent In {\tt Maple} and in {\tt Mupad}, one would use the {\tt subs} 6185command to substitute a variable 6186by a value in an expression. But the order of the arguments differ 6187between {\tt Maple} and {\tt Mupad}. Therefore, to achieve compatibility, 6188{\tt Xcas} {\tt subs} command arguments order depends on the mode 6189\begin{itemize} 6190\item 6191In {\tt Maple} mode, {\tt subs} takes two arguments : an equality 6192(variable=substitution value) and the expression.\\ 6193To substitute several variables in an expression, use a list of equality 6194(variable names {\tt =} substitution value) as first argument. 6195\item In {\tt Mupad} or {\tt Xcas} or {\tt TI}, {\tt subs} 6196takes two or three arguments : 6197an expression and an equality (variable=substitution value) or 6198an expression, a variable name and the substitution value.\\ 6199To substitute several variables, {\tt subs} takes two or three arguments : 6200\begin{itemize} 6201\item an expression of variables and a list of 6202(variable names {\tt =} substitution value), 6203\item 6204an expression of variables, a list of variables and a list of their 6205substitution values. 6206\end{itemize} 6207\end{itemize} 6208{\tt subs} returns the expression with the substitution done. 6209Note that {\tt subs} does not quote its argument, hence 6210in a normal evaluation process, the substitution variable should 6211be purged otherwise it will be replaced by its assigned value 6212before substitution is done.\\ 6213Input in {\tt Maple} mode (if the variable {\tt a} is purged else input 6214{\tt purge(a)}) : 6215\begin{center}{\tt subs(a=2,a\verb|^|2+1)}\end{center} 6216Output 6217\begin{center}{\tt 2\verb|^|2+1}\end{center} 6218Input in {\tt Maple} mode (if the variables {\tt a} and {\tt b} are purged 6219 else input {\tt purge(a,b)}): 6220\begin{center}{\tt subs([a=2,b=1],a\verb|^|2+b)}\end{center} 6221Output : 6222\begin{center}{\tt 2\verb|^|2+1}\end{center} 6223Input : 6224\begin{center}{\tt subs(a\verb|^|2+1,a=2)}\end{center} 6225or : 6226\begin{center}{\tt subs(a\verb|^|2+1,a,2)}\end{center} 6227Output (if the variable {\tt a} is purged else input {\tt purge(a)}) : 6228\begin{center}{\tt 5}\end{center} 6229Input : 6230\begin{center}{\tt subs(a\verb|^|2+b,[a=2,b=1])}\end{center} 6231or : 6232\begin{center}{\tt subs(a\verb|^|2+b,[a,b],[2,1])}\end{center} 6233Output (if the variables {\tt a} and {\tt b} are purged else input 6234{\tt purge(a,b)}) : 6235\begin{center}{\tt 2\verb|^|2+1}\end{center} 6236 6237\subsection{Substitute a subexpression by another expression: \texttt{algsubs}}\index{algsubs} 6238 6239The \texttt{algsubs} command takes two arguments, an equation 6240\texttt{expr1 = expr2} between two expressions and another expression.\\ 6241\texttt{algsubs} returns the last expression with \texttt{expr1} 6242replaced by \texttt{expr2}.\\ 6243Input: 6244\begin{center} 6245 \tt 6246 algsubs (x\^{}2 = u, 1 + x\^{}2 + x\^{}4) 6247\end{center} 6248Output: 6249\begin{center} 6250 \tt 6251 u\^{}2 + u + 1 6252\end{center} 6253Input: 6254\begin{center} 6255 \tt 6256 algsubs (a*b/c = d, 2*a*b\^{}2/c) 6257\end{center} 6258Output: 6259\begin{center} 6260 \tt 6261 2*b*d 6262\end{center} 6263Input: 6264\begin{center} 6265 \tt 6266 algsubs (2a = p\^{}2-q\^{}2, algsubs (2c = p\^{}2 + q\^{}2, c\^{}2-a\^{}2)) 6267\end{center} 6268Output: 6269\begin{center} 6270 \tt 6271 p\^{}2*q\^{}2 6272\end{center} 6273 6274\subsection{Eliminate one or more variables from a list of equations: \texttt{eliminate}}\index{eliminate} 6275 6276The \texttt{eliminate} commands takes two arguments; a list of 6277equations and the variable (or list of variables) to eliminate.\\ 6278\texttt{eliminate} returns the equations with the requested variables 6279eliminated. (The equations will be given as expressions, assumed to 6280be equal to 0.) 6281 6282Assuming the variables used haven't been set to any values:\\ 6283Input: 6284\begin{center} 6285 \tt 6286 eliminate ([x = v0*t, y = y0-g*t\^{}2], t) 6287\end{center} 6288Output : 6289\begin{center} 6290 \tt 6291 [v0\^{}2*y0-x\^{}2*g-v0\^{}2*y] 6292\end{center} 6293Input: 6294\begin{center} 6295 \tt 6296 eliminate ([x = 2*t, y = 1 - 10*t\^{}2, z = x + y - t], t) 6297\end{center} 6298Output: 6299\begin{center} 6300 \tt 6301 [10*y\^{}2-20*y*z+10*z\^{}2+y-1,x+2*y-2*z] 6302\end{center} 6303Input: 6304\begin{center} 6305 \tt 6306 eliminate([x+y+z+t-2,x*y*t=1,x\^{}2+t\^{}2=z\^{}2],[x,z]) 6307\end{center} 6308Output: 6309\begin{center} 6310 \tt 6311 [2*t\^{}2*y\^{}2+t*y\^{}3-4*t\^{}2*y-4*t*y\^{}2+4*t*y+2*t+2*y-4] 6312\end{center} 6313 6314If the variable(s) can't be eliminated, then \texttt{eliminate} 6315returns \texttt{[1]} or \texttt{[-1]}. If \texttt{eliminate} returns 6316\texttt{[]}, that means the equations determine the values of the 6317variables to be eliminated. 6318 6319\noindent 6320Input: 6321\begin{center} 6322 \tt 6323 x:=2;y:=-5\\ 6324 eliminate([x=2*t,y=1-10*t\^{}2],t) 6325\end{center} 6326Output: 6327\begin{center} 6328 \tt 6329 [1] 6330\end{center} 6331since \texttt{t} cannot be eliminated from both equations. 6332Input: 6333\begin{center} 6334 \tt 6335 x:=2;y:=-9 6336 eliminate([x=2*t,y=1-10*t\^{}2],t) 6337\end{center} 6338Output: 6339\begin{center} 6340 \tt 6341 [] 6342\end{center} 6343since the first equation gives \texttt{t}$=1$, which satisfies the 6344second equation.\\ 6345Input: 6346\begin{center} 6347 \tt 6348 x := 2; y := -9\\ 6349 eliminate ([x = 2*t, y = 1-10*t\^{}2, z = x + y - t], t) 6350\end{center} 6351Output: 6352\begin{center} 6353 \tt 6354 [z+8] 6355\end{center} 6356since the first equation gives \texttt{t}$=1$, which satisfies the 6357second equation, and so that leaves \texttt{z = 2 - 9 - 1 = -8}, or 6358\texttt{z + 8 = 0}. 6359 6360\subsection{Evaluate a primitive at boundaries: {\tt preval}}\index{preval} 6361\noindent{\tt preval} takes three arguments : an expression {\tt F} 6362depending on 6363the variable {\tt x}, and two expressions {\tt a} and {\tt b}.\\ 6364{\tt preval} computes $F_{|x=b}-F_{|x=a}$.\\ 6365 {\tt preval} is used to compute a definite integral 6366when the primitive $F$ of the integrand $f$ is known. Assume 6367for example that {\tt F:=int(f,x)}, then {\tt preval(F,a,b)} is equivalent 6368to {\tt int(f,x,a,b)} but does not require to compute again {\tt F} 6369from {\tt f} if you change the values of $a$ or $b$.\\ 6370Input : 6371\begin{center}{\tt preval(x\verb|^|2+x,2,3)}\end{center} 6372Output : 6373\begin{center}{\tt 6}\end{center} 6374 6375\subsection{Sub-expression of an expression : {\tt part}}\index{part} 6376\noindent{\tt part} takes two arguments : an expression and an integer $n$.\\ 6377{\tt part} evaluate the expression and then returns the $n$-th sub-expression 6378of this expression.\\ 6379Input : 6380\begin{center}{\tt part(x\verb|^|2+x+1,2)}\end{center} 6381Output : 6382\begin{center}{\tt x}\end{center} 6383Input : 6384\begin{center}{\tt part(x\verb|^|2+(x+1)*(y-2)+2,2)}\end{center} 6385Output : 6386\begin{center}{\tt (x+1)*(y-2)}\end{center} 6387Input : 6388\begin{center}{\tt part((x+1)*(y-2)/2,2)}\end{center} 6389Output : 6390\begin{center}{\tt y-2}\end{center} 6391 6392\section{Values of $u_n$} 6393 6394\subsection{Array of values of a sequence : {\tt tablefunc}}\index{tablefunc} 6395{\tt tablefunc} is a command that should be used inside a spreadsheet 6396(opened with {\tt Alt+t}), 6397it returns a template to fill two columns, with 6398the table of values of a function. If the step value is 1, 6399{\tt tablefunc(ex,n,n0,1)}, where {\tt ex} is an expression 6400depending on {\tt n}, will fill the spreadsheet with 6401the values of the sequence $u_n=ex$ for $n=n0,\ n0+1,\ n0+2,....$. 6402 6403{\bf Example} : display the values of the sequence $u_n=\sin(n)$\\ 6404Select a cell of a spreadsheet (for example {\tt C0}) 6405and input in the command line : 6406\begin{center}{\tt tablefunc(sin(n),n,0,1)}\end{center} 6407Output : 6408\begin{center}{\tt two columns : {\tt n} and {\tt sin(n)}}\end{center} 6409\begin{itemize} 6410\item in the column C: the variable name {\tt n}, the value of the step 6411(this value should be equal to 1 for a sequence), 6412the value of {\tt n0} (here 0), then a recurrence 6413formula ({\tt C2+C\$1}, ...). 6414\item in the column D: {\tt sin(n)}, {\tt "Tablefunc"}, then a 6415recurrence formula. 6416\item For each row, 6417the values of the sequence ${\tt u_n=\sin(n)}$ correspond to 6418the values of {\tt n} starting from {\tt n=n0} (here 0). 6419\end{itemize} 6420 6421\subsection{Values of a recurrence relation or a system: \texttt{seqsolve}}\index{seqsolve} 6422\label{ssec:seqsolve} 6423 6424See also section \ref{ssec:rsolve}. 6425 6426The \texttt{seqsolve} command takes three arguments; an expression or 6427list of expressions that define a recurrence relation, the variables 6428used, and the starting values. For example, if a recurrence relation 6429is defined by $u_{n+1} = f(u_n,n)$ with $u_0 = a$, the arguments to 6430\texttt{seqsolve} will be \texttt{f(x,n)}, \texttt{[x,n]} and 6431\texttt{a}. If the recurrence relation 6432is defined by $u_{n+2} = g(u_n,u_{n+1},n)$ with $u_0 = a$ and $u_1 = b$, the arguments to 6433\texttt{seqsolve} will be \texttt{g(x,y,n)}, \texttt{[x,y,n]} and 6434\texttt{[a,b]}. The recurrence relation must have a homogeneous 6435linear part, the nonhomogeneous part must be a linear combination of a 6436polynomials in \texttt{n} times geometric terms in \texttt{n}.\\ 6437\texttt{seqsolve} returns the sequence, as a function of \texttt{n}. 6438 6439\noindent 6440\textbf{Examples:}\\ 6441\begin{itemize} 6442\item 6443To find $u_n$, given that $u_{n+1} = 2u_n + n$ and $u_0=3$: 6444Input: 6445\begin{center} 6446 \tt 6447 seqsolve(2x+n,[x,n],3) 6448\end{center} 6449Output: 6450\begin{center} 6451 \tt 6452 -n-1+4*2\^{}n 6453\end{center} 6454 6455\item 6456To find $u_n$, given that $u_{n+1} = 2u_n + n 3^n$ and $u_0=3$: 6457Input: 6458\begin{center} 6459 \tt 6460 seqsolve(2x+n*3\^{}n,[x,n],3) 6461\end{center} 6462Output: 6463\begin{center} 6464 \tt 6465 (n-3)*3\^{}n+6*2\^{}n 6466\end{center} 6467 6468\item 6469To find $u_n$, given that $u_{n+1} = u_n + u_{n-1}$, $u_0 = 0$ and 6470$u_1=1$: 6471Input: 6472\begin{center} 6473 \tt 6474 seqsolve(x+y,[x,y,n],[0,1]) 6475\end{center} 6476Output: 6477\begin{center} 6478 \tt 6479 (5+sqrt(5))/10*((sqrt(5)+1)/2)\^{}(n-1)+ (5-(sqrt(5)))/10*((-sqrt(5)+1)/2)\^{}(n-1) 6480\end{center} 6481 6482\item 6483To find $u_n$ and $v_n$, given that $u_{n+1}=u_n + 2v_n$, $v_{n+1} = 6484u_n + n + 1$ with $u_0 = 1, v_0 = 1$:\\ 6485Input: 6486\begin{center} 6487 \tt 6488 seqsolve([x+2*y,n+1+x],[x,y,n],[0,1]) 6489\end{center} 6490Output: 6491\begin{center} 6492 \tt 6493 [(-2*n-(-1)\^{}n+4*2\^{}n-3)/2,((-1)\^{}n+2*2\^{}n-1)/2] 6494\end{center} 6495\end{itemize} 6496 6497 6498\subsection{Values of a recurrence relation or a system: \texttt{rsolve}} 6499\index{rsolve} 6500\label{ssec:rsolve} 6501 6502See also section \ref{ssec:seqsolve} 6503 6504The \texttt{rsolve} command takes three arguments; an equation or 6505list of equations that define a recurrence relation, the functions 6506(with their variables) used, and equations for the starting values. 6507For example, if a recurrence relation is defined by $u_{n+1} = 6508f(u_n,n)$ with $u_0 = a$, the arguments to \texttt{rsolve} will be 6509\texttt{u(n+1) = f(u(n),n)}, \texttt{u(n)} and 6510\texttt{u(0)=a}. The recurrence relation must either be a homogeneous 6511linear part with a nonhomogeneous part being a linear combination of 6512polynomials in \texttt{n} times geometric terms in \texttt{n} (such as 6513$u_{n+1} = 2 u_{n} + n 3^n$), or a 6514linear fractional transformation (such as $u_{n+1} = 6515(u_{n}-1)/(u_{n}-2)$).\\ 6516\texttt{rsolve} returns a matrix whose rows are the values of the 6517sequence as functions of \texttt{n}. 6518 6519Note that \texttt{rsolve} is more flexible than \texttt{seqsolve} since: 6520\begin{itemize} 6521 \item the sequence doesn't have to start with $u_0$. 6522 \item the sequence can have several starting values, such as initial 6523 condition $u_0^2 = 1$, which is why \texttt{rsolve} returns a list. 6524 \item the notation for the recurrence relation is similar to how it 6525 is written in mathematics. 6526\end{itemize} 6527 6528 6529\noindent 6530\textbf{Examples:}\\ 6531\begin{itemize} 6532\item 6533To find $u_n$, given that $u_{n+1} = 2u_n + n$ and $u_0=3$: 6534Input: 6535\begin{center} 6536 \tt 6537 rsolve(u(n+1) = 2*u(n) + n, u(n), u(0)=3) 6538\end{center} 6539Output: 6540\begin{center} 6541 \tt 6542 [-n+4*2\^{}n-1] 6543\end{center} 6544 6545\item 6546To find $u_n$, given that $u_{n+1} = 2u_n + n$ and $u_1^2 = 1$: 6547Input: 6548\begin{center} 6549 \tt 6550 rsolve(u(n+1) = 2*u(n) + n, u(n), u(1)\^{}2 = 1) 6551\end{center} 6552Output: 6553\begin{center} 6554 \tt 6555 [-n+3/2*2\^{}n-1,-n+1/2*2\^{}n-1] 6556\end{center} 6557 6558\item 6559To find $u_n$, given that $u_{n+1} = 2u_n + n 3^n$ and $u_0=3$: 6560Input: 6561\begin{center} 6562 \tt 6563 rsolve(u(n+1) = 2*u(n) + n*3\^{}n,u(n), u(0)=3) 6564\end{center} 6565Output: 6566\begin{center} 6567 \tt 6568 [n*3\^{}n+6*2\^{}n-3*3\^{}n] 6569\end{center} 6570 6571\item 6572To find $u_n$, given that $u_{n+1} = (u_n - 1)/(u_n -2)$ and $u_0 = 65734$:\\ 6574Input: 6575\begin{center} 6576 \tt 6577 rsolve(u(n+1) = (u(n)-1)/(u(n)-2),u(n), u(0)=4) 6578\end{center} 6579Output: 6580\begin{center} 6581 \tt 6582 [((10*sqrt(5)+30)*((sqrt(5)-3)/2)\^{}n+30*sqrt(5)-70)/(20*((sqrt(5)-3)/2)\^{}n+10*sqrt(5)-30)] 6583\end{center} 6584 6585\item 6586To find $u_n$ given that $u_{n+1} = u_{n} + u_{n-1}$ with $u_0 = 0$, 6587$u_1=1$:\\ 6588Input: 6589\begin{center} 6590 \tt 6591 rsolve(u(n+1) = u(n) + u(n-1), u(n), u(0) = 0, u(1) = 1) 6592\end{center} 6593Output: 6594\begin{center} 6595 \tt 6596 [(-sqrt(5)/5)*((-sqrt(5)+1)/2)\^{}n+sqrt(5)/5*((sqrt(5)+1)/2)\^{}n] 6597\end{center} 6598 6599\item 6600To find $u_n$ and $v_n$, given that $u_{n+1}=u_n + v_n$, $v_{n+1} = 6601u_n - v_n$ with $u_0 = 0, v_0 = 1$:\\ 6602Input: 6603\begin{center} 6604 \tt 6605 rsolve([u(n+1) = u(n) + v(n), v(n+1) = u(n) - v(n)], [u(n),v(n)], [u(0)=1, v(0)=1]) 6606\end{center} 6607Output: 6608\begin{center} 6609 \tt 6610 [[(-sqrt(2)+1)/2*(-sqrt(2))\^{}(n+1-1)+(sqrt(2)+1)/2*2\^{}(1/2*(n+1-1)),\\ 6611 1/2*(-sqrt(2))\^{}(n+1-1)+1/2*2\^{}(1/2*(n+1-1))]] 6612\end{center} 6613\end{itemize} 6614 6615\subsection{Table of values and graph of a recurrent sequence : {\tt tableseq} and {\tt plotseq}}\index{tableseq|textbf}\index{plotseq} 6616{\tt tableseq} is a command that should be used inside a spreadsheet 6617(opened with {\tt Alt+t}), 6618it returns a template to fill one column with 6619${\tt u_0, \ u_{n+1}=f(u_{n})}$ (one-term recurrence) or 6620more generally $u_0,...,u_k, \ \ u_{n+k+1}=f(u_n,u_{n+1},...,u_{n+k})$. 6621The template fills the column starting from the selected cell, or 6622starting from 0 if the whole column was selected.\\ 6623See also {\tt plotseq} (section \ref{sec:plotseq}) for a graphic representation 6624of a one-term recurrence sequence. 6625 6626{\bf Examples} : 6627\begin{itemize} 6628\item display the values of the sequence $u_0=3.5, \ u_n=\sin(u_{n-1})$\\ 6629Select a cell of the spreadsheet (for example {\tt B0}) and 6630 input in the command line : 6631\begin{center}{\tt tableseq(sin(n),n,3.5)}\end{center} 6632Output : 6633\begin{center}{\tt a column with sin(n), n, 3.5 6634and the formula evalf(subst(B\$0,B\$1,B2))} 6635\end{center} 6636You get the values of the sequence 6637${\tt u_0=3.5,\ u_n=sin(u_{n-1})}$ in the column 6638{\tt B}. 6639\item 6640display the values of the Fibonacci sequence 6641$u_0=1, u_1=1 \ u_{n+2}=u_n+u_{n+1}$\\ 6642Select a cell, say {\tt B0}, and input in the command line 6643\begin{center}{\tt tableseq(x+y,[x,y],[1,1])}\end{center} 6644This fills the B column sheet with 6645\begin{center}{\tt \begin{tabular}{|l|l|} 6646\hline 6647row &B\\ 6648\hline 66490 &x+y \\ 6650\hline 66511 & x\\ 6652\hline 66532 & y\\ 6654\hline 66553 & 1\\ 6656\hline 66574 & 1\\ 6658\hline 66595 & 2\\ 6660\hline 6661.. &..\\ 6662\hline 66637 & 5\\ 6664\hline 6665.. &..\\ 6666\hline 6667\end{tabular}}\end{center} 6668\end{itemize} 6669 6670\section{Operators or infixed functions} 6671An operator is an infixed function. 6672 6673\subsection{Usual operators :{\tt +, -, *, /, \^\ }}\index{+}\index{'+'}\index{-}\index{'-'}\index{\^\ } 6674{\tt +, -, *, /, \^\ } are the operators to do 6675additions, subtractions, multiplications, divisions and for raising to a 6676power. 6677 6678\subsection{{\tt Xcas} operators }\index{\@}\index{\@\@}\index{\$}\index{\%} 6679\begin{itemize} 6680\item 6681{\tt \$ } is the infixed version of {\tt seq}, for example :\\ 6682{\tt (2\verb|^|k)\$(k=0..3)= seq(2\verb|^|k,k=0..3)=(1,2,4,8)} (do not forget 6683to put parenthesis around the arguments), 6684\item 6685{\tt mod} or {\tt \%} to define a modular number, 6686\item 6687{\tt @} to compose functions for example : 6688{\tt (f@g)(x)=f(g(x))}, 6689\item 6690{\tt @@ } to compose a function many times (like a power, replacing 6691multiplication by composition), for example : 6692{\tt (f@@3)(x)=f(f(f(x)))}, 6693\item 6694{\tt minus union intersect} to get the difference, the union and the 6695intersection of two sets, 6696\item 6697{\tt ->} to define a function, 6698\item 6699{\tt := =>} to store an expression in a variable (it is the infixed 6700version of {\tt sto} and the argument order is permuted for {\tt :=}), 6701for example : {\tt a:=2} or {\tt 2=>a} or {\tt sto(2,a)}. 6702\item 6703{\tt =<} to store an expression in a variable, but the storage is 6704done by reference if the target is a matrix element or a list element. 6705This is faster if you modify objects inside an existing list or matrix 6706of large size, because no copy is made, the change is done in place. 6707Use with care, all objects pointing to this matrix or list will 6708be modified. 6709\end{itemize} 6710 6711 6712\subsection{Define an operator: {\tt user\_operator}}\index{user\_operator}\index{Binary@{\sl Binary}|textbf}\index{Delete@{\sl Delete}|textbf} 6713\noindent {\tt user\_operator} takes as argument : 6714\begin{itemize} 6715\item a string : the name of the operator, 6716\item a function of two variables with values in $\mathbb R$ or in 6717{\tt true, false}, 6718\item an option {\tt Binary} for the definition or {\tt Delete} to delete 6719this definition. 6720\end{itemize} 6721{\tt user\_operator} returns 1 if the definition is done and else returns 0. 6722 6723{\bf Example 1}\\ 6724Let $R$ be defined on $\mathbb R$ by $x\ R \ y= x*y+x+y$.\\ 6725To define the law $R$, input : 6726\begin{center}{\tt user\_operator("R",(x,y)->x*y+x+y,Binary)}\end{center} 6727Output : 6728\begin{center}{\tt 1}\end{center} 6729Input : 6730\begin{center}{\tt 5 R 7}\end{center} 6731Do not forget to put spaces around {\tt R}.\\ 6732Output : 6733\begin{center}{\tt 47}\end{center} 6734 6735{\bf Example 2}\\ 6736Let $S$ be defined on $\mathbb N$ by :\\ 6737for $x$ and $y$ integers, $x\ S \ y <=> x$ and $y$ are not coprime.\\ 6738To define the law $S$, input : 6739\begin{center}{\tt user\_operator("S",(x,y)->(gcd(x,y))!=1,Binary)}\end{center} 6740Output : 6741\begin{center}{\tt 1}\end{center} 6742Input : 6743\begin{center}{\tt 5 S 7}\end{center} 6744Do not forget to put spaces around {\tt S}.\\ 6745Output : 6746\begin{center}{\tt 0}\end{center} 6747Input : 6748\begin{center}{\tt 8 S 12}\end{center} 6749Do not forget to put spaces around {\tt S}.\\ 6750Output : 6751\begin{center}{\tt 1}\end{center} 6752 6753\section{Functions and expressions with symbolic variables} 6754\subsection{The difference between a function and an expression}\index{->}\index{:=} 6755A function {\tt f} is defined for example by :\\ 6756{\tt f(x):=x\verb|^|2-1} or by {\tt f:=x->x\verb|^|2-1} \\ 6757that is to say, for all $x$, $f(x)$ is equal to the expression 6758$x^2-1$. In that case, to have the value of $f$ for $x=2$, input :{\tt f(2)}.\\ 6759But if the input is 6760{\tt g:=x\verb|^|2-1}, then {\tt g} is a variable where the 6761expression $x^2-1$ is stored. In that case, to have the value of $g$ for $x=2$, 6762input : {\tt subst(g,x=2)} ($g$ is an expression depending on $x$). 6763 6764When a command expects a function as argument, this argument should 6765be either the definition of the function (e.g. {\tt x->x\verb|^|2-1}) 6766or a variable name assigned to a function (e.g. {\tt f} 6767previously defined by e.g. {\tt f(x):=x\verb|^|2-1}).\\ 6768When a command expects an expression as argument, this argument should 6769be either the definition of the expression (for example {\tt x\verb|^|2-1}), 6770or a variable name assigned to an expression (e.g. 6771{\tt g} previously defined, for example, by 6772{\tt g:=x\verb|^|2-1}), or the evaluation of a function. e.g. 6773{\tt f(x)} if {\tt f} is a previously defined function, 6774for example, by {\tt f(x):=x\verb|^|2-1}). 6775 6776\subsection{Transform an expression into a function : {\tt unapply}}\index{unapply} 6777\noindent {\tt unapply} is used to transform an expression into a function.\\ 6778{\tt unapply} takes two arguments an expression and the name of a variable.\\ 6779{\tt unapply} returns the function defined by this expression and 6780this variable. 6781 6782{\bf Warning} when a function is defined, 6783the right member of the assignment is not evaluated, 6784hence \verb|g:=sin(x+1); f(x):=g| does not defined the function 6785$f: x \rightarrow \sin(x+1)$ but defines the function 6786$f: x \rightarrow g$. To defined the former function, {\tt unapply} 6787should be used, like in the following example:\\ 6788Input : 6789\begin{center}{\tt g:= sin(x+1); f:=unapply(g,x)}\end{center} 6790Output : 6791\begin{center}{\tt (sin(x+1), (x)->sin(x+1))}\end{center} 6792hence, the variable {\tt g} is assigned to a symbolic expression 6793and the variable {\tt f} is assigned to a function.\\ 6794Input : 6795\begin{center}{\tt unapply(exp(x+2),x)}\end{center} 6796Output : 6797\begin{center}{\tt (x)->exp(x+2)}\end{center} 6798Input : 6799\begin{center}{\tt f:=unapply(lagrange([1,2,3],[4,8,12]),x)}\end{center} 6800Output : 6801\begin{center}{\tt (x)->4+4*(x-1)}\end{center} 6802Input : 6803\begin{center}{\tt f:=unapply(integrate(log(t),t,1,x),x)}\end{center} 6804Output : 6805\begin{center}{\tt (x)->x*log(x)-x+1}\end{center} 6806Input : 6807\begin{center}{\tt f:=unapply(integrate(log(t),t,1,x),x)}\end{center} 6808\begin{center}{\tt f(x)}\end{center} 6809Output : 6810\begin{center}{\tt x*log(x)-x+1}\end{center} 6811{\bf Remark} 6812Suppose that $f$ is a function of 2 variables $f:(x,w)\rightarrow f(x,w)$, 6813and that $g$ is the function defined by 6814$g: w \rightarrow h_w$ where $h_w$ is the function defined by 6815$h_w(x)=f(x,w)$.\\ 6816{\tt unapply} is also used to define $g$ with {\tt Xcas}.\\ 6817Input : 6818\begin{center}{\tt f(x,w):=2*x+w}\end{center} 6819\begin{center}{\tt g(w):=unapply(f(x,w),x)}\end{center} 6820\begin{center}{\tt g(3)}\end{center} 6821Output : 6822\begin{center}{\tt x->2$\cdot$x+3}\end{center} 6823 6824\subsection{Top and leaves of an expression : {\tt sommet feuille op}}\index{sommet|textbf}\index{feuille|textbf}\index{op|textbf}\label{sec:op} 6825An operator is an infixed function : for example '+' is an 6826operator and 'sin' is a function.\\ 6827An expression can be represented by a tree. The top of the tree is 6828either an operator, or a function and the leaves of the tree are the 6829arguments of the operator or of the function 6830(see also \ref{sec:makesuiteop}).\\ 6831The instruction {\tt sommet} (resp. {\tt feuille} (or {\tt op})) 6832returns the top (resp. the list of the leaves) of an expression.\\ 6833Input : 6834\begin{center}{\tt sommet(sin(x+2))}\end{center} 6835Output : 6836\begin{center}{\tt 'sin'}\end{center} 6837Input : 6838\begin{center}{\tt sommet(x+2*y)}\end{center} 6839Output : 6840\begin{center}{\tt '+'}\end{center} 6841Input : 6842\begin{center}{\tt feuille(sin(x+2))}\end{center} 6843or : 6844\begin{center}{\tt op(sin(x+2))}\end{center} 6845Output : 6846\begin{center}{\tt x+2}\end{center} 6847Input : 6848\begin{center}{\tt feuille(x+2*y)}\end{center} 6849or : 6850\begin{center}{\tt op(x+2*y)}\end{center} 6851Output : 6852\begin{center}{\tt (x,2*y) }\end{center} 6853{\bf Remark}\\ 6854Suppose that a function is defined by a program, for example 6855let us define the {\tt pgcd} function : 6856\begin{center}{\tt pgcd(a,b):=\{local r; while (b!=0) \{r:=irem(a,b);a:=b;b:=r;\} return a;\}}\end{center} 6857Then input : 6858\begin{center}{\tt sommet(pgcd)}\end{center} 6859Output : 6860\begin{center}{\tt 'program'}\end{center} 6861Then input : 6862\begin{center}{\tt feuille(pgcd)[0]}\end{center} 6863Output : 6864\begin{center}{\tt (a,b)}\end{center} 6865Then input : 6866\begin{center}{\tt feuille(pgcd)[1]}\end{center} 6867Output : 6868\begin{center}{\tt (0,0) or (15,25) if the last input was pgcd(15,25)}\end{center} 6869Then input : 6870\begin{center}{\tt feuille(pgcd)[2]}\end{center} 6871Output : 6872\begin{center}{\tt The body of the program : \{local r;....return(a);\}}\end{center} 6873 6874 6875\section{Functions} 6876\subsection{Context-dependent functions.} 6877\subsubsection{Operators {\tt +} and {\tt -}}\index{+}\index{'+'}\index{-}\index{'-'} 6878 \noindent{\tt +} (resp. {\tt -}) is an infixed function and {\tt '+'} (resp. 6879{\tt '-'}) is a prefixed function. The result depends on the nature of its 6880arguments.\\ 6881Examples with {\tt +} (all examples except the last one work also 6882with {\tt -} instead of {\tt +}) : 6883\begin{itemize} 6884\item input (1,2)+(3,4) or (1,2,3)+4 or 1+2+3+4 or '+'(1,2,3,4), output 10, 6885\item input 1+i+2+3*i or '+'(1,i,2,3*i), output 3+4*i, 6886\item input [1,2,3]+[4,1] or [1,2,3]+[4,1,0] or '+'([1,2,3],[4,1]), output 6887[5,3,3], 6888\item input [1,2]+[3,4] or '+'([1,2],[3,4]), output [4,6], 6889\item input [[1,2],[3,4]]+ [[1,2],[3,4]], output [[2,4],[6,8]], 6890\item input [1,2,3]+4 or '+'([1,2,3],4), output poly1[1,2,7], 6891\item input [1,2,3]+(4,1) or '+'([1,2,3],4,1), output poly1[1,2,8], 6892\item input "Hel"+"lo" or '+'("Hel","lo"), output "Hello". 6893\end{itemize} 6894 6895\subsubsection{Operator {\tt *}}\index{*}\index{'*'} 6896\noindent{\tt *} is an infixed function and {\tt '*'} 6897is a prefixed function. The result depends on the nature of its arguments.\\ 6898Examples with {\tt *} : 6899\begin{itemize} 6900\item input (1,2)*(3,4) or (1,2,3)*4 or 1*2*3*4 or '*'(1,2,3,4), output 24, 6901\item input 1*i*2*3*i or '*'(1,i,2,3*i), output -6, 6902\item input [10,2,3]*[4,1] or [10,2,3]*[4,1,0] or '*'([10,2,3],[4,1]), 6903output 42 (scalar product), 6904\item input [1,2]*[3,4] or '*'([1,2],[3,4]), output 11 (scalar product), 6905\item input [[1,2],[3,4]]* [[1,2],[3,4]], output [[7,10],[15,22]], 6906\item input [1,2,3]*4 or '*'([1,2,3],4), output [4,8,12], 6907\item input [1,2,3]*(4,2) or '*'([1,2,3],4,2) or [1,2,3]*8, output [8,16,24], 6908\item input (1,2)+i*(2,3) or 1+2+i*2*3, output 3+6*i. 6909\end{itemize} 6910 6911\subsubsection{Operator {\tt /}}\index{/}\index{'/'} 6912\noindent{\tt /} is an infixed function and {\tt '/'} 6913is a prefixed function. The result depends of the nature of its arguments.\\ 6914Examples with {\tt /} : 6915\begin{itemize} 6916\item input [10,2,3]/[4,1], output invalid dim 6917\item input [1,2]/[3,4] or '/'([1,2],[3,4]), output [1/3,1/2], 6918\item input 1/[[1,2],[3,4]] or '/'(1,[[1,2],[3,4]], output 6919[[-2,1],[3/2,(-1)/2]], 6920\item input [[1,2],[3,4]]*1/ [[1,2],[3,4]], output [[1,0],[0,1]], 6921\item input [[1,2],[3,4]]/ [[1,2],[3,4]], output [[1,1],[1,1]] (division term 6922by term), 6923%\item input [1,2,3]*4 or '*'([1,2,3],4), output [4,8,12], 6924%\item input [1,2,3]/(4,2) or '*'([1,2,3],4,2), output [1,2,3]*8=[8,16,24]. 6925%\item (1,2)+i/(2,3)=1+2+i*2*3=3+6*i 6926\end{itemize} 6927 6928\subsection{Usual functions} 6929\begin{itemize} 6930\item {\tt max}\index{max|textbf} takes as argument two real numbers and 6931returns their maximum, 6932\item 6933{\tt min}\index{min|textbf} takes as argument two real numbers and returns 6934their minimum, 6935\item 6936{\tt abs}\index{abs} takes as argument a complex number and returns the 6937modulus of the complex parameter (the absolute value if the complex is 6938real), 6939\item 6940{\tt sign}\index{sign|textbf} takes as argument a real number and returns its 6941sign (+1 if it is positive, 0 if it is null, and -1 if it is 6942negative), 6943\item 6944{\tt floor}\index{floor|textbf} (or {\tt iPart}\index{iPart|textbf}) 6945takes as argument a real number $r$, 6946and returns the largest integer $\leq r$, 6947\item 6948{\tt round}\index{round|textbf} takes as argument a real number and returns 6949its nearest integer, 6950\item 6951{\tt ceil} or {\tt ceiling}\index{ceil|textbf}\index{ceiling|textbf} takes as 6952argument a real number and returns the smallest integer $\geq r$ 6953\item 6954{\tt frac}\index{frac|textbf} (or {\tt fPart}\index{fPart|textbf}) takes as 6955argument a real number and returns its fractional part, 6956\item 6957{\tt trunc}\index{trunc|textbf} takes as 6958argument a real number and returns the integer equal to the real without its 6959fractional part, 6960\item 6961{\tt id}\index{id|textbf} is the identity function, 6962\item 6963{\tt sq}\index{sq|textbf} is the square function, 6964\item 6965{\tt sqrt}\index{sqrt|textbf} is the squareroot function, 6966\item 6967{\tt surd}\index{surd|textbf} takes two arguments, numbers $x$ and 6968$n$ and returns the $n$th root of $x$. 6969\item 6970{\tt exp}\index{exp|textbf} is the exponential function, 6971\item 6972{\tt log}\index{log|textbf} or {\tt ln}\index{ln|textbf} is the 6973natural logarithm function, 6974\item 6975{\tt log10}\index{log10|textbf} is the base-10 logarithm function, 6976\item 6977{\tt logb}\index{logb|textbf} is the logarithm function where the second 6978argument is the base of the logarithm: 6979{\tt logb(7,10)=log10(7)=log(7)/log(10)}, 6980\item 6981{\tt sin}\index{sin|textbf} (resp. {\tt cos}\index{cosh|textbf}, 6982{\tt tan}\index{tan|textbf}) 6983is the sinus function, cosinus function, tangent function, 6984\item {\tt cot, sec, csc} are the cotangent, secant, cosecant function 6985\item {\tt asin} (or {\tt arcsin})\index{asin|textbf}\index{arcsin|textbf},{ 6986\tt acos} (or {\tt arccos})\index{acos|textbf}\index{arccos|textbf}, 6987{\tt atan} (or {\tt arctan})\index{atan|textbf}\index{arctan|textbf}, 6988{\tt acot, asec, acsc} are the inverse trigonometric functions (see 6989section \ref{sec:trigo} for more info on trigonometric functions) 6990\item 6991{\tt sinh}\index{sinh|textbf} (resp. {\tt cosh}\index{cosh|textbf}, 6992{\tt tanh}\index{tanh|textbf}) 6993is the hyperbolic sinus function, cosinus function, tangent function, 6994\item 6995{\tt asinh} or {\tt arcsinh}\index{asinh|textbf}\index{arcsinh|textbf} (resp. 6996{\tt acosh} or {\tt arccosh}\index{acosh|textbf}\index{arccosh|textbf}, 6997{\tt atanh} or {\tt arctanh}\index{atanh|textbf}\index{arctanh|textbf}) 6998is the inverse function of {\tt sinh} (resp. {\tt cosh}, {\tt tanh}) 6999\end{itemize} 7000 7001\subsection{Defining algebraic functions} 7002\subsubsection{Defining a function from $\mathbb{R}^p$ to $\mathbb{R}$} 7003\noindent For $p=1$, e.g. for $f\ :\ (x)\rightarrow x*\sin(x)$, input : 7004\begin{center}{\tt f(x):=x*sin(x)}\end{center} 7005or : 7006\begin{center}{\tt f:=x->x*sin(x)}\end{center} 7007Output : 7008\begin{center}{\tt (x)->x*sin(x)}\end{center} 7009If $p>1$, e.g. for $f\ :\ (x,y)\rightarrow x*\sin(y)$, input : 7010\begin{center}{\tt f(x,y):=x*sin(y)}\end{center} 7011or : 7012\begin{center}{\tt f:=(x,y)->x*sin(y)}\end{center} 7013Output : 7014\begin{center}{\tt (x,y)->x*sin(y)}\end{center} 7015{\bf Warning !!!} the expression after {\tt -> } is not evaluated. 7016You should use {\tt unapply} if you expect the second member to 7017be evaluated before the function is defined. 7018 7019\subsubsection{Defining a function from $\mathbb{R}^p$ to $\mathbb{R}^q$} 7020For example: 7021\begin{itemize} 7022\item To define the function $h\ :\ (x,y)\rightarrow (x*\cos(y),x*\sin(y))$.\\ 7023 Input : 7024\begin{center}{\tt h(x,y):=(x*cos(y),x*sin(y))}\end{center} 7025Output : 7026\begin{center}{\tt (x,y)->\{ \\ 7027 x*cos(y),x*sin(y);\\ 7028\}"}\end{center} 7029\item To define the function $h\ :\ (x,y)\rightarrow [x*\cos(y),x*\sin(y)]$.\\ 7030 Input : 7031\begin{center}{\tt h(x,y):=[x*cos(y),x*sin(y)];}\end{center} 7032or : 7033\begin{center}{\tt h:=(x,y)->[x*cos(y),x*sin(y)];}\end{center} 7034or : 7035\begin{center}{\tt h(x,y):=\{[x*cos(y),x*sin(y)]\};}\end{center} 7036or : 7037\begin{center}{\tt h:=(x,y)->return[x*cos(y),x*sin(y)];}\end{center} 7038or : 7039\begin{center}{\tt h(x,y):=\{return [x*cos(y),x*sin(y)];\}}\end{center} 7040Output : 7041\begin{center}{\tt (x,y)->\{return([x*cos(y),x*sin(y)]);\}}\end{center} 7042\end{itemize} 7043{\bf Warning !!!} The expression after {\tt -> } is not evaluated. 7044 7045\subsubsection{Defining families of function from $\mathbb{R}^{p-1}$ 7046to $\mathbb{R}^q$ using a function from $\mathbb{R}^p$ to $\mathbb{R}^q$} 7047Suppose that the function $f: (x,y) \rightarrow f(x,y)$ is defined, 7048and we want to define a family of functions $g(t)$ such 7049that $g(t)(y):=f(t,y)$ (i.e. $t$ is viewed as a parameter). 7050Since the expression after {\tt -> } (or {\tt :=}) 7051is not evaluated, we should not define $g(t)$ by {\tt g(t):=y->f(t,y)}, 7052we have to use the {\tt unapply} command. 7053 7054For example, assuming that $f:(x,y)\rightarrow x\sin(y)$ and $g(t): y\rightarrow f(t,y)$, input : 7055\begin{center}{\tt f(x,y):=x*sin(y);g(t):=unapply(f(t,y),y)}\end{center} 7056Output : 7057\begin{center}{\tt ((x,y)->x*sin(y), (t)->unapply(f(t,y),y))}\end{center} 7058Input : 7059\begin{center}{\tt g(2)}\end{center} 7060Output : 7061\begin{center}{\tt y->2$\cdot$ sin(y)}\end{center} 7062Input : 7063\begin{center}{\tt g(2)(1)}\end{center} 7064Output : 7065\begin{center}{\tt 2$\cdot$ sin(1)}\end{center} 7066 7067Next example, suppose that the function 7068$h: (x,y) \rightarrow [x*\cos(y),x*\sin(y)]$ is defined, and 7069we want to define the family of functions $k(t)$ having $t$ as 7070parameter such that $k(t)(y):=h(t,y)$. 7071To define the function $h(x,y)$, input : 7072\begin{center}{\tt h(x,y):=(x*cos(y),x*sin(y))}\end{center} 7073To define properly the function $k(t)$, input : 7074\begin{center}{\tt k(t):=unapply(h(x,t),x)}\end{center} 7075Output : 7076\begin{center}{\tt (t)->unapply(h(x,t),x)}\end{center} 7077Input : 7078\begin{center}{\tt k(2)}\end{center} 7079Output : 7080\begin{center}{\tt (x)->(x*cos(2),x*sin(2))}\end{center} 7081Input : 7082\begin{center}{\tt k(2)(1)}\end{center} 7083Output : 7084\begin{center}{\tt (2*cos(1),2*sin(1))}\end{center} 7085 7086\subsection{Composition of two functions: {\tt @}}\index{\@|textbf} 7087With {\tt Xcas}, the composition of functions is done with the infixed operator 7088{\tt @}.\\ 7089Input : 7090\begin{center}{\tt (sq@sin+id)(x)}\end{center} 7091Output : 7092\begin{center}{\tt (sin(x))\verb|^|2+x}\end{center} 7093Input : 7094\begin{center}{\tt (sin@sin)(pi/2)}\end{center} 7095Output : 7096\begin{center}{\tt sin(1)}\end{center} 7097 7098\subsection{Repeated function composition: {\tt @@}}\index{\@\@|textbf} 7099With {\tt Xcas}, the repeated composition of a function with itself 7100 $n \in {\mathbb N}$ times is done with the infixed operator {\tt @@}.\\ 7101Input : 7102\begin{center}{\tt (sin@@3)(x)}\end{center} 7103Output : 7104\begin{center}{\tt sin(sin(sin(x)))}\end{center} 7105Input : 7106\begin{center}{\tt (sin@@2)(pi/2)}\end{center} 7107Output : 7108\begin{center}{\tt sin(1)}\end{center} 7109 7110\subsection{Define a function with the history : {\tt as\_function\_of}}\index{as\_function\_of} 7111\noindent If an entry defines the variable {\tt a} and if a later 7112entry defines the variable {\tt b} (supposed to be dependent on 7113{\tt a}), then 7114{\tt c:=as\_function\_of(b,a)} will define a function {\tt c} such that 7115{\tt c(a)=b}.\\ 7116Input : 7117\begin{center}{\tt a:=sin(x)}\end{center} 7118Output : 7119\begin{center}{\tt sin(x)}\end{center} 7120Input : 7121\begin{center}{\tt b:=sqrt(1+a\verb|^|2)}\end{center} 7122Output : 7123\begin{center}{\tt sqrt(1+sin(x)\verb|^|2)}\end{center} 7124Input : 7125\begin{center}{\tt c:=as\_function\_of(b,a)}\end{center} 7126Output : 7127\begin{flushleft}{\tt (a)-> \\ 7128\{ local NULL;\\ 7129 return(sqrt(1+a\verb|^|2));\\ 7130\}}\end{flushleft} 7131Input : 7132\begin{center}{\tt c(x)}\end{center} 7133Output : 7134\begin{center}{\tt sqrt(1+x\verb|^|2)}\end{center} 7135Input : 7136\begin{center}{\tt a:=2}\end{center} 7137Output : 7138\begin{center}{\tt 2}\end{center} 7139Input : 7140\begin{center}{\tt b:=1+a\verb|^|2}\end{center} 7141Output : 7142\begin{center}{\tt 5}\end{center} 7143Input : 7144\begin{center}{\tt c:=as\_function\_of(b,a)}\end{center} 7145Output : 7146\begin{flushleft}{\tt (a)-> \\ 7147\{ local NULL;\\ 7148 return(sqrt(1+a\verb|^|2));\\ 7149\}}\end{flushleft} 7150Input : 7151\begin{center}{\tt c(x)}\end{center} 7152Output : 7153\begin{center}{\tt 1+x\verb|^|2}\end{center} 7154 7155{\bf Warning !!}\\ 7156If the variable {\tt b} has been assigned 7157several times, the first assignment of {\tt b} following the last 7158assignment of {\tt a} will be used. Moreover, the order used is the 7159order of validation of the commandlines, 7160which may not be reflected by the Xcas interface 7161if you reused previous commandlines.\\ 7162Input for example :\\ 7163{\tt a:=2} then\\ 7164{\tt b:=2*a+1} then\\ 7165{\tt b:=3*a+2} then\\ 7166{\tt c:=as\_function\_of(b,a)}\\ 7167Output : 7168\begin{center}{\tt (a)-> \{local NULL; return(2*a+1);\}}\end{center} 7169i.e. {\tt c(x)} is equal to {\tt 2*x+1}. \\ 7170But, input :\\ 7171{\tt a:=2} then\\ 7172{\tt b:=2*a+1} then\\ 7173{\tt a:=2} then\\ 7174{\tt b:=3*a+2} then\\ 7175{\tt c:=as\_function\_of(b,a)}\\ 7176Output : 7177\begin{center}{\tt (a)-> \{local NULL; return(3*a+2);\}}\end{center} 7178i.e. {\tt c(x)} is equal to {\tt 3*x+2}. \\ 7179Hence the line where {\tt a} is defined must be reevaluated before the good 7180definition of {\tt b}. 7181 7182\section{Functions from $\R$ to $\R$} 7183 7184\subsection{The domain of a function: \texttt{domain}}\index{domain} 7185 7186The \texttt{domain} command takes one or two arguments. The first 7187argument is an expression involving a single variable. If the 7188variable is not \texttt{x}, the variable used should be the second 7189argument.\\ 7190\texttt{domain} returns the domain of the function defined by the 7191expression.\\ 7192Input: 7193\begin{center} 7194 \tt 7195 domain(ln(x+1)) 7196\end{center} 7197Output: 7198\begin{center} 7199 \tt 7200 x>-1 7201\end{center} 7202Input: 7203\begin{center} 7204 \tt 7205 domain(asin(2*t),t) 7206\end{center} 7207Output: 7208\begin{center} 7209 \tt 7210 ((t>=(-1/2)) and (t<=(1/2))) 7211\end{center} 7212 7213\subsection{Table of variations of a function: 7214\texttt{tabvar}}\index{tabvar} 7215 7216The \texttt{tabvar} command takes one mandatory argument and one 7217optional argument. The mandatory argument is an expression of a 7218single variable, and the second argument is the variable.\\ 7219\texttt{tabvar} returns the table of variations of the function 7220\texttt{f(}\textit{x}\texttt{) = expr} and draws the graph on the 7221\texttt{DispG} screen, accessible with the menu 7222\texttt{Cfg$\blacktriangleright$Show$\blacktriangleright$DispG}. 7223 7224\noindent 7225Input: 7226\begin{center} 7227 \tt 7228 tabvar(x\^{}2 - x - 2,x) 7229\end{center} 7230Output: 7231\begin{center} 7232 \includegraphics[width=0.75\textwidth]{xcas-tabvar1.png} 7233\end{center} 7234\begin{itemize} 7235 \item The first row, the \texttt{x} row, gives the endpoint of 7236 subintervals of the domain. In this case, the subintervals go from 7237 $-\infty$ to $1/2$ and from $1/2$ to $\infty$. 7238 \item The second row, the \texttt{y'} row, gives the values of the 7239 derivative at the values in the first row (or limits, in the case of 7240 $\pm\infty$), and between them the sign ($+$ or $-$) of the 7241 derivative in the corresponding subinterval. 7242 \item The third row, the \texttt{y} row, gives the values of the 7243 function at the values in the first row, and between them whether 7244 the function is increasing or decreasing in the corresponding 7245 subinterval. 7246 \item The fourth row, the \texttt{y''} 7247 row, gives the values of the second derivative at the values in the 7248 first row, and between them whether the graph is concave up or 7249 concave down in the subinterval. 7250\end{itemize} 7251 7252\noindent 7253Input: 7254\begin{center} 7255 \tt 7256 tabvar((2*t-1)/(t-1),t) 7257\end{center} 7258Output: 7259\begin{center} 7260 \includegraphics[width=0.75\textwidth]{xcas-tabvar2.png} 7261\end{center} 7262Note that in this case, the value 1 appears twice in the first row, so 7263that both one-sided limits of \texttt{y} can be displayed at the 7264vertical asymptote $t=1$. The values of 2 for \texttt{y} at $-\infty$ 7265and $\infty$ indicate a horizontal asymptote of $y=2$. 7266 7267\section{Derivation and applications.} 7268\subsection{Functional derivative : {\tt function\_diff}}\index{function\_diff} 7269{\tt function\_diff} takes a function as argument.\\ 7270{\tt function\_diff} returns the derivative function of this function.\\ 7271Input : 7272\begin{center}{\tt function\_diff(sin)}\end{center} 7273Output : 7274\begin{center}{\tt (` x`)->cos(` x`)}\end{center} 7275Input : 7276\begin{center}{\tt function\_diff(sin)(x)}\end{center} 7277Output : 7278\begin{center}{\tt cos(x)}\end{center} 7279Input : 7280\begin{center}{\tt f(x):=x\verb|^|2+x*cos(x)}\end{center} 7281\begin{center}{\tt function\_diff(f)}\end{center} 7282Output : 7283\begin{center}{\tt (` x`)->2*` x`+cos(` x`)+` x`*(-(sin(` x`)))}\end{center} 7284Input : 7285\begin{center}{\tt function\_diff(f)(x)}\end{center} 7286Output : 7287\begin{center}{\tt cos(x)+x*(-(sin(x)))+2*x}\end{center} 7288To define the function $g$ as $f'$, input :\\ 7289\begin{center}{\tt g:=function\_diff(f)}\end{center} 7290The {\tt function\_diff} instruction has the same effect as 7291using the expression derivative in conjunction with {\tt unapply} : 7292\begin{center}{\tt g:=unapply(diff(f(x),x),x)}\end{center} 7293\begin{center}{\tt g(x)}\end{center} 7294Output : 7295\begin{center}{\tt cos(x)+x*(-(sin(x)))+2*x}\end{center} 7296{\bf Warning !!!}\\ 7297In {\tt Maple} mode, for compatibility, 7298{\tt D} may be used in place of {\tt function\_diff}. 7299For this reason, it is impossible to assign a variable named 7300{\tt D} in {\tt Maple} mode (hence you can not name a 7301geometric object {\tt D}). 7302 7303\subsection{Length of an arc : {\tt arcLen}}\index{arcLen} 7304\label{sec:arclen} 7305\noindent {\tt arcLen} takes four arguments : an expression $ex$ (resp. a list 7306of two expressions $[ex1,ex2]$), the name of a parameter and two values $a$ 7307and $b$ of this parameter.\\ 7308{\tt arcLen} computes the length of the curve define by the equation 7309$y=f(x)=ex$ (resp. by $x=ex1,y=ex2$) when the parameter values varies from $a$ 7310to $b$, using the formula 7311{\tt arcLen(f(x),x,a,b)=}\\ 7312{\tt integrate(sqrt(diff(f(x),x)\verb|^|2+1),x,a,b)}\\ 7313or \\ 7314{\tt integrate(sqrt(diff(x(t),t)\verb|^|2+diff(y(t),t)\verb|^|2),t,a,b)}.\\ 7315 7316{\bf Examples} 7317\begin{itemize} 7318\item Compute the length of the parabola $y=x^2$ from $x=0$ to $x=1$.\\ 7319Input : 7320\begin{center}{\tt arcLen(x\verb|^|2,x,0,1)}\end{center} 7321or 7322\begin{center}{\tt arcLen([t,t\verb|^|2],t,0,1)}\end{center} 7323Output : 7324\begin{center}{\tt -1/4*log(sqrt(5)-2)-(-(sqrt(5)))/2}\end{center} 7325\item Compute the length of the curve $y=\cosh(x)$ from $x=0$ to 7326$x=\ln(2)$.\\ 7327Input : 7328\begin{center}{\tt arcLen(cosh(x),x,0,log(2))}\end{center} 7329Output : 7330\begin{center}{\tt 3/4}\end{center} 7331\item Compute the length of the circle $x=\cos(t),y=\sin(t)$ from $t=0$ to 7332$t=2*\pi$.\\ 7333Input : 7334\begin{center}{\tt arcLen([cos(t),sin(t)],t,0,2*pi)}\end{center} 7335Output : 7336\begin{center}{\tt 2*pi}\end{center} 7337\end{itemize} 7338 7339Alternatively, the \texttt{arcLen} command can take a single argument of 7340a geometric curve defined in one of the graphics chapters (chapters 7341\ref{chap:2dgraphics} and \ref{chap:3dgraphics}).\\ 7342Input: 7343\begin{center} 7344 \tt 7345 arcLen(circle(0,1,0,pi/2)) 7346\end{center} 7347Output: 7348\begin{center} 7349 \tt 7350 1/2*pi 7351\end{center} 7352Input: 7353\begin{center} 7354 \tt 7355 arcLen(arc(0,1,pi/2)) 7356\end{center} 7357Output: 7358\begin{center} 7359 \tt 7360 sqrt(2)/4*pi 7361\end{center} 7362 7363\subsection{Maximum and minimum of an expression: {\tt fMax fMin}}\index{fMax}\index{fMin} 7364\noindent{\tt fMax} and {\tt fMin} take one or two arguments : an expression 7365of a variable and the name of this variable (by default {\tt x}).\\ 7366{\tt fMax} returns the abscissa 7367of a maximum of the expression.\\ 7368{\tt fMin} returns the abscissa 7369of a minimum of the expression.\\ 7370Input : 7371\begin{center}{\tt fMax(sin(x),x)}\end{center} 7372Or : 7373\begin{center}{\tt fMax(sin(x))}\end{center} 7374Or : 7375\begin{center}{\tt fMax(sin(y),y)}\end{center} 7376Output : 7377\begin{center}{\tt pi/2}\end{center} 7378Input : 7379\begin{center}{\tt fMin(sin(x),x)}\end{center} 7380Or : 7381\begin{center}{\tt fMin(sin(x))}\end{center} 7382Or : 7383\begin{center}{\tt fMin(sin(y),y)}\end{center} 7384Output : 7385\begin{center}{\tt -pi/2}\end{center} 7386Input : 7387\begin{center}{\tt fMin(sin(x)\verb|^|2,x)}\end{center} 7388Output : 7389\begin{center}{\tt 0}\end{center} 7390 7391{\tt fMax} and {\tt fMin} can also compute the maximum resp.~minimum of a nonlinear multivariate expression subject to a set of nonlinear equality and/or inequality constraints. Both functions in such cases take four to six arguments: 7392\begin{itemize} 7393 \item objective function (an expression) 7394 \item list of constraints (equalities and inequalities) 7395 \item list of problem variables 7396 \item initial guess (must be a list of nonzero reals representing a feasible point) 7397 \item precision (optional), if not given the default epsilon value is used 7398 \item maximum number of iterations (optional) 7399\end{itemize} 7400The objective function does not need to be differentiable. 7401Both {\tt fMin} and {\tt fMax} return the optimal solution as a vector. Note that the actual optimal value of the objective is not returned. 7402 7403Although the initial point is required to be feasible, the algorithm will sometimes succeed even if it is infeasible. Note that the initial value of a variable must not be zero. 7404 7405For example, input : 7406\begin{center} 7407 \tt fMin((x-5)\verb|^|2+y\verb|^|2-25,[y>=x\verb|^|2],[x,y],[1,1]) 7408\end{center} 7409Output : 7410\begin{center} 7411 \tt [1.2347728624961,1.5246640219568] 7412\end{center} 7413Input : 7414\begin{center} 7415 \tt fMax((x-2)\verb|^|2+(y-1)\verb|^|2,[-.25x\verb|^|2-y\verb|^|2+1>=0,x-2y+1=0], 7416 [x,y],[.5,.75]) 7417\end{center} 7418Output : 7419\begin{center} 7420 \tt [-1.82287565553,-0.411437827766] 7421\end{center} 7422 7423\subsection{Table of values and graph : {\tt tablefunc} and {\tt plotfunc}}\index{tablefunc|textbf}\index{plotfunc} 7424{\tt tablefunc} is a special command that should be run from inside 7425the spreadsheet. It returns the evaluation of an expression $ex$ 7426depending on a variable $x$ for $x=x_0,\ x_0+h,....$~: 7427\begin{center} 7428{\tt tablefunc(ex,x,x\_0,h)} or {\tt tablefunc(ex,x)} 7429\end{center} 7430In the latter case, the default value for ${\tt x_0}$ 7431is the default minimum value of $x$ from the graphic configuration 7432and the default value for the step $h$ is 0.1 times the difference 7433between the default maximum and minimum values of $x$ (from the 7434graphic configuration).\\ 7435Example: type {\tt Alt+t} to open a spreadsheet if none are open. 7436Then select a cell of the spreadsheet (for example {\tt C0}) and to get 7437the table of {\tt "sinus"}, input in the command line of the spreadsheet : 7438\begin{center}{\tt tablefunc(sin(x),x)}\end{center} 7439This will fill two columns with the numeric value of {\tt x} and 7440{\tt sin(x)} : 7441\begin{itemize} 7442\item in the first column the variable {\tt x}, 7443the value of the step {\tt h} 7444(1.0), the minimum value of $x$ (-5.0), then a formula, for example 7445{\tt=C2+C\$1}, and the remaining rows 7446of the column is filled by pasting this formula. 7447\item in the next column the function {\tt sin(x)}, the word 7448"Tablefunc", a formula, 7449for example {\tt =evalf(subst(D\$0,C\$0,C2))}, and the remaining rows 7450of the column are filled by pasting this formula. 7451\end{itemize} 7452Hence the values of {\tt sin(x)} are on the same rows as the values 7453of {\tt x}. Note that the step and begin value and the expression 7454may be easily changed by modifying the correspondent cell. 7455 7456The graphic representation may be plotted with the {\tt plotfunc} command 7457(see \ref{sec:plotfunc}). 7458 7459\subsection{Derivative and partial derivative}\index{diff|textbf}\index{derive|textbf}\index{deriver|textbf} 7460\noindent{\tt diff} or {\tt derive} may have one or two arguments 7461to compute a first order derivative (or first order partial 7462derivative) of an expression or of a list of expressions, 7463or several arguments to compute 7464the $n$-th partial derivative of an expression or list of expressions. 7465 7466\subsubsection{Derivative and first order partial derivative : {\tt diff derive deriver}} 7467{\tt diff} (or {\tt derive}) takes two arguments : an expression and a variable 7468(resp. a vector of variable names) (see several variable functions in 7469 \ref{sec:plusvar}). If only one argument is provided, the derivative 7470is taken with respect to {\tt x}\\ 7471{\tt diff} (or {\tt derive}) returns the derivative (resp. a vector of 7472derivatives) of the expression with respect to the variable (resp. with respect 7473to each variable) given as second argument.\\ 7474Examples : 7475\begin{itemize} 7476\item Compute : 7477$$\frac {\partial (x y^2 z^3+x y z)}{\partial z}$$ 7478Input : 7479\begin{center}{\tt diff(x*y\verb|^|2*z\verb|^|3+x*y*z,z)}\end{center} 7480Output : 7481\begin{center}{\tt x*y\verb|^|2*3*z\verb|^|2+x*y}\end{center} 7482\item Compute the 3 first order partial derivatives of $x*y^2*z^3+x*y*z$.\\ 7483Input : 7484\begin{center}{\tt diff(x*y\verb|^|2*z\verb|^|3+x*y,[x,y,z])}\end{center} 7485Output : 7486\begin{center}{\tt [y\verb|^|2*z\verb|^|3+y*z, x*2*y*z\verb|^|3+x*z, x*y\verb|^|2*3*z\verb|^|2+x*y]}\end{center} 7487% \item Compute : 7488% $$\frac {\partial^3 (x.y^2.z^3+x.y.z)}{\partial y\partial^2 z}$$ 7489% Input : 7490% \begin{center}{\tt diff(x*y \verb|^|2*z\verb|^|3+x*y*z,y,z\$2)}\end{center} 7491% Output : 7492% \begin{center}{\tt x*2*y*3*2*z}\end{center} 7493\end{itemize} 7494 7495\subsubsection{Derivative and $n$-th order 7496partial derivative : {\tt diff derive deriver}}\index{\$} 7497\noindent {\tt derive} (or {\tt diff}) may take more than two arguments : an 7498expression and the names of the derivation variables (each variable 7499may be followed by \$$n$ to indicate the number $n$ of derivations).\\ 7500{\tt diff} returns the partial derivative of the expression with respect to 7501the variables given after the first argument. 7502 7503The notation \$ is useful if you want to derive $k$ times with 7504respect to the same variable, instead of entering $k$ times the 7505same variable name, one enters the variable name followed by {\tt \$k}, 7506for example {\tt x\$3} instead of {\tt(x,x,x)}. 7507Each variable may be followed by a \$, for example 7508{\tt diff(exp(x*y),x\$3,y\$2,z)} is the same as 7509{\tt diff(exp(x*y),x,x,x,y,y,z)}\\ 7510{\bf Examples} 7511\begin{itemize} 7512\item Compute : 7513$$\frac {\partial^2 (x y^2 z^3+x y z)}{\partial x\partial z}$$ 7514Input : 7515\begin{center}{\tt diff(x*y\verb|^|2*z\verb|^|3+x*y*z,x,z)}\end{center} 7516Output : 7517\begin{center}{\tt y\verb|^|2*3*z\verb|^|2+y}\end{center} 7518\item Compute : 7519$$\frac {\partial^3 (x y^2 z^3+x y z)}{\partial x\partial^2 z}$$ 7520Input : 7521\begin{center}{\tt diff(x*y\verb|^|2*z\verb|^|3+x*y*z,x,z,z)}\end{center} 7522or : 7523\begin{center}{\tt diff(x*y\verb|^|2*z\verb|^|3+x*y*z,x,z\$2)}\end{center} 7524Output : 7525\begin{center}{\tt y\verb|^|2*3*2*z}\end{center} 7526\item Compute the third derivative of : 7527$$\frac{1}{x^2+2}$$ 7528Input : 7529\begin{center}{\tt normal(diff((1)/(x\verb|^|2+2),x,x,x))}\end{center} 7530or : 7531\begin{center}{\tt normal(diff((1)/(x\verb|^|2+2),x\$3))}\end{center} 7532Output : 7533\begin{center}{\tt (-24*x\verb|^|3+48*x)/(x\verb|^|8+8*x\verb|^|6+24*x\verb|^|4+32*x\verb|^|2+16)}\end{center} 7534\end{itemize} 7535{\bf Remark} 7536\begin{itemize} 7537\item 7538Note the difference between {\tt diff(f,x,y)} and {\tt diff(f,[x,y])} :\\ 7539{\tt diff}$(f,x,y)$ returns $\displaystyle \frac{\partial^2(f)}{\partial x\partial y}$ and\\ 7540{\tt diff}$(f,[x,y])$ returns 7541$\displaystyle[\frac{\partial(f)}{\partial x},\frac{\partial 7542 (f)}{\partial y}]$ 7543\item Never define a derivative function with {\tt 7544 f1(x):=diff(f(x),x)}. 7545Indeed, {\tt x} would mean two different things Xcas is unable to 7546deal with: the variable name to 7547define the $f_1$ function and the differentiation variable. 7548The right way to define a derivative is either with {\tt 7549 function\_diff} or: 7550\begin{center} 7551{\tt f1:=unapply(diff(f(x),x),x)} 7552\end{center} 7553\end{itemize} 7554 7555\subsection{Implicit differentiation : {\tt implicitdiff}} 7556{\tt implicitdiff} is called with one of the following three sets of parameters : 7557\begin{enumerate} 7558\item {\tt expr}, {\tt constr}, {\tt depvars}, {\tt diffvars} 7559\item {\tt constr}, {\tt [depvars]}, {\tt y}, {\tt diffvars} 7560\item {\tt expr}, {\tt constr}, {\tt vars}, {\tt order=k}, {\tt [pt]} 7561\end{enumerate} 7562Details on parameters : 7563\begin{itemize} 7564\item {\tt expr} : differentiable expression $ f(x_1,x_2,\dots,x_n,y_1,y_2,\dots,y_m) $ 7565\item {\tt constr} : (list of) equality constraint(s) $ g_i(x_1,\dots,x_n,y_1,\dots,y_m)=0 $ or vanishing expression(s) $ g_i $, where $ i=1,2,\dots,m $ 7566\item {\tt depvars} : (list of) dependent variable(s) $ y_1,y_2,\dots,y_m $, each of which may be entered as a symbol, e.g.~{\tt yi}, or a function of independent variable(s), e.g.~{\tt yi(x1,x2,..,xn)} 7567\item {\tt diffvars} : sequence of variables $ x_{i_1},x_{i_2},\dots,x_{i_k} $ with respect to which is {\tt expr} differentiated 7568\item {\tt vars} : independent and dependent variables entered as symbols in single list such that dependent variables come last, e.g.~{\tt [x1,..,xn,y1,..,ym]} 7569\item {\tt y} : (list of) dependent variable(s) $ y_{j_1},y_{j_2},\dots,y_{j_l} $ that need to be differentiated 7570\end{itemize} 7571Dependent variables $ y_1,y_2,\dots,y_m $ are implicitly defined with $ m $ constraints in {\tt constr}. By implicit function theorem, the Jacobian matrix of $ \mathbf{g}=(g_1,g_2,\dots,g_m) $ has to be full rank. 7572 7573When calling {\tt implicitdiff}, first two sets of parameters are used when specific partial derivative is needed. In the first case, {\tt expr} is differentiated with respect to {\tt diffvars}. 7574 7575\noindent Input : 7576\begin{center} 7577{\tt implicitdiff(x*y,-2x\verb|^|3+15x\verb|^|2*y+11y\verb|^|3-24y=0,y(x),x)} 7578\end{center} 7579Output : 7580\begin{center} 7581{\tt (2*x\verb|^|3-5*x\verb|^|2*y+11*y\verb|^|3-8*y)/(5*x\verb|^|2+11*y\verb|^|2-8)} 7582\end{center} 7583In the second case (elements of) {\tt y} is differentiated. If {\tt y} is a list of symbols, a list containing their derivatives will be returned. The following examples compute $ \frac{\mathrm{d}\,y}{\mathrm{d}\,x} $.\\ 7584Input : 7585\begin{center} 7586{\tt implicitdiff(x\verb|^|2*y+y\verb|^|2=1,y,x)} 7587\end{center} 7588Output : 7589\begin{center} 7590{\tt -2*x*y/(x\verb|^|2+2*y)} 7591\end{center} 7592Input : 7593\begin{center} 7594{\tt implicitdiff([x\verb|^|2+y=z,x+y*z=1],[y(x),z(x)],y,x)} 7595\end{center} 7596Output : 7597\begin{center} 7598{\tt (-2*x*y-1)/(y+z)} 7599\end{center} 7600In the next example, $ \frac{\mathrm{d}\,y}{\mathrm{d}\,x} $ and $ \frac{\mathrm{d}\,z}{\mathrm{d}\,x} $ are computed.\\ 7601Input : 7602\begin{center} 7603{\tt implicitdiff([-2x*z+y\verb|^|2=1,x\verb|^|2-exp(x*z)=y],}\\ 7604{\tt [y(x),z(x)],[y,z],x)} 7605\end{center} 7606Output : 7607\begin{center} 7608{\tt [2*x/(y*exp(x*z)+1),}\\ 7609{\tt (2*x*y-y*z*exp(x*z)-z)/(x*y*exp(x*z)+x)]} 7610\end{center} 7611 7612For the third case of input syntax, all partial derivatives of order equal to {\tt order}, i.e.~$ k $, are computed. If $ k=1 $ they are returned in a single list, which represents the gradient of {\tt expr} with respect to independent variables. For $ k=2 $ the corresponding hessian matrix is returned. When $ k>2 $, a table with keys in form {\tt [k1,k2,..,kn]}, where $ \sum_{i=1}^nk_i=k $, is returned. Such key corresponds to \[ \frac{\partial^k f}{\partial x_1^{k_1}\,\partial x_2^{k_2}\,\cdots\,\partial x_n^{k_n}}. \] 7613Input : 7614\begin{center} 7615{\tt f:=x*y*z; g:=-2x\verb|^|3+15x\verb|^|2*y+11y\verb|^|3-24y=0;}\\ 7616{\tt implicitdiff(f,g,[x,z,y],order=1)} 7617\end{center} 7618Output : 7619\begin{center} 7620{\tt [(2*x\verb|^|3*z-5*x\verb|^|2*y*z+11*y\verb|^|3*z-8*y*z)/(5*x\verb|^|2+11*y\verb|^|2-8),}\\ 7621{\tt x*y]} 7622\end{center} 7623Input : 7624\begin{center} 7625{\tt implicitdiff(f,g,order=2,[1,-1,0])} 7626\end{center} 7627Output : 7628\begin{center} 7629{\tt [[64/9,-2/3],[-2/3,0]]} 7630\end{center} 7631In the next example, the value of $ \frac{\partial^4 f}{\partial x^4} $ is computed at point $ (x=0,y=0,z) $.\\ 7632Input : 7633\begin{center} 7634{\tt pd:=implicitdiff(f,g,[x,z,y],order=4,[0,z,0]);}\\ 7635{\tt pd[4,0]} 7636\end{center} 7637Output : 7638\begin{center} 7639{\tt -2*z} 7640\end{center} 7641 7642 7643\section{Integration} 7644\subsection{Antiderivative and definite integral : {\tt integrate int Int}}\index{integrate}\index{Int}\index{int} 7645\noindent{\tt integrate} (or {\tt int}) computes a primitive 7646or a definite integral. A difference between the two 7647commands is that if you input {\tt quest()} just after the evaluation of 7648{\tt integrate}, the answer is written with the $\int$ symbol. 7649 7650{\tt integrate} (or {\tt int} or {\tt Int}) takes one, two or four arguments. 7651\begin{itemize} 7652\item with one or two arguments\\ 7653an expression or an expression and 7654the name of a variable (by default {\tt x}),\\ 7655{\tt integrate} (or {\tt int}) returns a primitive of the expression with 7656respect to the variable given as second argument.\\ 7657Input : 7658\begin{center}{\tt integrate(x\verb|^|2)}\end{center} 7659Output : 7660\begin{center}{\tt x\verb|^|3/3}\end{center} 7661Input : 7662\begin{center}{\tt integrate(t\verb|^|2,t)}\end{center} 7663Output : 7664\begin{center}{\tt t\verb|^|3/3}\end{center} 7665\item with four arguments :\\ 7666an expression, a name of a variable and the bounds of the definite integral,\\ 7667{\tt integrate} (or {\tt int}) returns the exact 7668value of the definite integral if the computation was successful or 7669an unevaluated integral otherwise.\\ 7670Input : 7671\begin{center}{\tt integrate(x\verb|^|2,x,1,2)}\end{center} 7672Output : 7673\begin{center}{\tt 7/3}\end{center} 7674Input : 7675\begin{center}{\tt integrate(1/(sin(x)+2),x,0,2*pi)}\end{center} 7676Output after simplification (with the {\tt simplify} command) : 7677\begin{center}{\tt 2*pi*sqrt(3)/3}\end{center} 7678\end{itemize} 7679 7680 7681{\tt Int} is the inert form of {\tt integrate}, it prevents evaluation 7682for example to avoid a symbolic computation that might not be 7683successful if you just want a numeric 7684evaluation.\\ 7685Input : 7686\begin{center}{\tt evalf(Int(exp(x\verb|^|2),x,0,1))}\end{center} 7687or : 7688\begin{center}{\tt evalf(int(exp(x\verb|^|2),x,0,1))}\end{center} 7689Output : 7690\begin{center}{\tt 1.46265174591}\end{center} 7691 7692{\bf Exercise 1}\\ 7693Let $$f(x)=\frac {x}{x^2-1}+\ln(\frac {x+1}{x-1})$$ 7694Find a primitive of $f$.\\ 7695Input : 7696\begin{center}{\tt int(x/(x\verb|^|2-1)+ln((x+1)/(x-1)))}\end{center} 7697Output : 7698\begin{center}{\tt x*log((x+1)/(x-1))+log(x\verb|^|2-1)+1/2*log(2*x\verb|^|2/2-1)}\end{center} 7699Or define the function {\tt f}, input : 7700\begin{center}{\tt f(x):=x/(x\verb|^|2-1)+ln((x+1)/(x-1))}\end{center} 7701then input : 7702\begin{center}{\tt int(f(x))}\end{center} 7703Output of course the same result.\\ 7704{\bf Warning}\\ 7705For {\tt Xcas}, {\tt log} is the natural logarithm (like {\tt ln}), 7706as {\tt log10} is 10-basis logarithm 7707 7708{\bf Exercise 2}\\ 7709Compute : 7710$$\int \frac {2}{x^6+2 \cdot x^4+x^2} \ dx $$ 7711Input : 7712\begin{center}{\tt int(2/(x\verb|^|6+2*x\verb|^|4+x\verb|^|2))}\end{center} 7713Output : 7714\begin{center}{\tt 2*((3*x\verb|^|2+2)/(-(2*(x\verb|^|3+x)))+-3/2*atan(x))}\end{center} 7715 7716{\bf Exercise 3}\\ 7717Compute : 7718$$\int \frac {1}{\sin(x)+\sin(2 \cdot x )} \ dx $$ 7719Input : 7720\begin{center}{\tt integrate(1/(sin(x)+sin(2*x )))}\end{center} 7721Output : 7722\begin{center}{\tt (1/-3*log((tan(x/2))\verb|^|2-3)+1/12*log((tan(x/2))\verb|^|2))*2}\end{center} 7723 7724\subsection{Primitive and definite integral : {\tt risch}}\index{risch} 7725 7726\noindent 7727The {\tt risch} command takes one mandatory argument and three 7728optional arguments. The first argument is an expression to be 7729integrated. If the variable is not \texttt{x}, then the second 7730argument is the variable. The third and fourth arguments are the 7731limits of integration for when you want a definite integral.\\ 7732\texttt{risch} returns a primitive (with one or two arguments) or a 7733definite integral (with four arguments).\\ 7734Input: 7735\begin{center}{\tt risch(x\verb|^|2)}\end{center} 7736Output: 7737\begin{center}{\tt x\verb|^|3/3}\end{center} 7738Input: 7739\begin{center}{\tt risch(x\verb|^|2,x,0,1)}\end{center} 7740Output: 7741\begin{center}{\tt 1/3}\end{center} 7742Input: 7743\begin{center}{\tt risch(t\verb|^|2,t)}\end{center} 7744Output: 7745\begin{center}{\tt t\verb|^|3/3}\end{center} 7746Input : 7747\begin{center}{\tt risch(exp(-x\verb|^|2))}\end{center} 7748Output : 7749\begin{center}{\tt integrate(exp(x\verb|^|2),x)}\end{center} 7750that is to say that $\exp(-x^2)$ has no primitive expressed 7751with usual functions. 7752 7753\subsection{Discrete summation: {\tt sum}}\index{sum|textbf} 7754\noindent{\tt sum} takes two or four arguments : 7755\begin{itemize} 7756\item four arguments\\ 7757an expression, the name of the variable (for 7758example {\tt n}), and the bounds (for example {\tt a} and {\tt b}).\\ 7759{\tt sum} returns the discrete sum of this expression with respect to 7760the variable from $a$ to $b$.\\ 7761Input : 7762\begin{center}{\tt sum(1,k,-2,n) }\end{center} 7763Output : 7764\begin{center}{\tt n+1+2}\end{center} 7765Input : 7766\begin{center}{\tt normal(sum(2*k-1,k,1,n))}\end{center} 7767Output : 7768\begin{center}{\tt n\verb|^|2}\end{center} 7769Input : 7770\begin{center}{\tt sum(1/(n\verb|^|2),n,1,10)}\end{center} 7771Output : 7772\begin{center}{\tt 1968329/1270080}\end{center} 7773Input : 7774\begin{center}{\tt sum(1/(n\verb|^|2),n,1,+(infinity)) }\end{center} 7775Output : 7776\begin{center}{\tt pi\verb|^|2/6}\end{center} 7777Input : 7778\begin{center}{\tt sum(1/(n\verb|^|3-n),n,2,10) }\end{center} 7779Output : 7780\begin{center}{\tt 27/110}\end{center} 7781Input : 7782\begin{center}{\tt sum(1/(n\verb|^|3-n),n,1,+(infinity)) }\end{center} 7783Output : 7784\begin{center}{\tt 1/4}\end{center} 7785This result comes from the decomposition of ${\tt 1/(n\verb|^|3-n)}$.\\ 7786Input : 7787\begin{center}{\tt partfrac(1/(n\verb|^|3-n)) }\end{center} 7788Output : 7789\begin{center}{\tt 1/(2*(n+1))-1/n+1/(2*(n-1))}\end{center} 7790Hence :\\ 7791$\displaystyle \sum_{n=2}^N -\frac{1}{n}=-\sum_{n=1}^{N-1} \frac{1}{n+1}=-\frac{1}{2}-\sum_{n=2}^{N-2} \frac{1}{n+1}-\frac{1}{N}$\\ 7792$\displaystyle \frac{1}{2}*\sum_{n=2}^N \frac{1}{n-1}=\frac{1}{2}*(\sum_{n=0}^{N-2} \frac{1}{n+1})=\frac{1}{2}*(1+\frac{1}{2}+\sum_{n=2}^{N-2}\frac{1}{n+1})$\\ 7793$\displaystyle \frac{1}{2}*\sum_{n=2}^N \frac{1}{n+1}=\frac{1}{2}*(\sum_{n=2}^{N-2} \frac{1}{n+1}+\frac{1}{N}+\frac{1}{N+1})$\\ 7794After simplification by $\sum_{n=2}^{N-2}$, it remains :\\ 7795 $\displaystyle -\frac{1}{2}+\frac{1}{2}*(1+\frac{1}{2})-\frac{1}{N}+\frac{1}{2}*(\frac{1}{N}+\frac{1}{N+1})=\frac{1}{4}-\frac{1}{2N(N+1)}$\\ 7796Therefore : 7797\begin{itemize} 7798\item for $N=10$ the sum is equal to : $1/4-1/220=27/110$ 7799\item for $N=+\infty$ the sum is equal to : $1/4$ because $\frac{1}{2N(N+1)}$ 7800approaches zero when $N$ approaches infinity. 7801\end{itemize} 7802 7803\item two arguments \\ 7804an expression of one variable (for example $f$) and the name of this 7805 variable (for example $x$).\\ 7806{\tt sum} returns the discrete antiderivative of this expression, i.e. 7807an expression $G$ such that $G_{|x=n+1}-G_{|x=n}=f_{|x=n}$.\\ 7808Input : 7809\begin{center}{\tt sum(1/(x*(x+1)),x)}\end{center} 7810Output : 7811\begin{center}{\tt -1/x}\end{center} 7812\end{itemize} 7813 7814\subsection{Riemann sum : {\tt sum\_riemann}}\index{sum\_riemann} 7815\noindent{\tt sum\_riemann} takes two arguments : an expression depending on 7816two variables and the list of the name of these two variables.\\ 7817{\tt sum\_riemann(expression(n,k),[n,k])} returns in the neighborhood of 7818 $ n=+\infty$ an equivalent of $\sum_{k=1}^n expression(n,k)$ (or of 7819$ \sum_{k=0}^{n-1} expression(n,k)$ or of $ \sum_{k=1}^{n-1} expression(n,k)$) 7820when the sum is looked on as a Riemann sum associated to a continuous 7821function defined on [0,1] or returns 7822{\tt "it is probably not a Riemann sum"} when the no result is found.\\ 7823{\bf Exercise 1}\\ 7824Suppose $\displaystyle S_n=\sum_{k=1}^n \frac{k^2}{n^3}$.\\ 7825Compute $\displaystyle \lim_{n \rightarrow +\infty} S_n$.\\ 7826Input : 7827\begin{center}{\tt sum\_riemann(k\verb|^|2/n\verb|^|3,[n,k])}\end{center} 7828Output : 7829\begin{center}{\tt 1/3}\end{center} 7830{\bf Exercise 2}\\ 7831Suppose $\displaystyle S_n=\sum_{k=1}^n \frac{k^3}{n^4}$.\\ 7832Compute $\displaystyle \lim_{n \rightarrow +\infty} S_n$.\\ 7833Input : 7834\begin{center}{\tt sum\_riemann(k\verb|^|3/n\verb|^|4,[n,k])}\end{center} 7835Output : 7836\begin{center}{\tt 1/4}\end{center} 7837{\bf Exercise 3}\\ 7838Compute 7839$\displaystyle \lim_{n \rightarrow +\infty}(\frac{1}{n+1}+\frac{1}{n+2}+...+\frac{1}{n+n})$.\\ 7840Input : 7841\begin{center}{\tt sum\_riemann(1/(n+k),[n,k])}\end{center} 7842Output : 7843\begin{center}{\tt log(2)}\end{center} 7844{\bf Exercise 4}\\ 7845Suppose $\displaystyle S_n=\sum_{k=1}^n \frac{32n^3}{16n^4-k^4}$.\\ 7846Compute $\displaystyle \lim_{n \rightarrow +\infty} S_n$.\\ 7847Input : 7848\begin{center}{\tt sum\_riemann(32*n\verb|^|3/(16*n\verb|^|4-k\verb|^|4),[n,k])}\end{center} 7849Output : 7850\begin{center}{\tt 2*atan(1/2)+log(3)}\end{center} 7851 7852\subsection{Integration by parts : {\tt ibpdv} and {\tt ibpu}} 7853\subsubsection{\tt ibpdv}\index{ibpdv} 7854\noindent{\tt ibpdv} is used to search the primitive of an expression written 7855as $u(x).v'(x)$.\\ 7856{\tt ibpdv} takes two arguments : 7857\begin{itemize} 7858\item an expression 7859 $u(x) * v'(x)$ and $v(x)$ (or a list of two expressions 7860$[F(x), u(x)*v'(x)]$ and $v(x)$), 7861\item or an expression $g(x)$ and $0$ (or a list of two expressions 7862$[F(x), g(x)]$ and $0$). 7863\end{itemize} 7864{\tt ibpdv} returns : 7865\begin{itemize} 7866\item if $v(x) \neq 0$, the list $[u(x) v(x),-v(x) u'(x)]$ (or 7867$[F(x)+u(x) v(x),-v(x) u'(x)]$), 7868\item if the second argument is zero, a primitive of the first argument 7869$g(x)$ (or $F(x)$+a primitive of $g(x)$) :\\ 7870hence, {\tt ibpdv(g(x),0)} returns a primitive {\tt G(x)} of {\tt g(x)} or \\ 7871{\tt ibpdv([F(x),g(x)],0)} returns {\tt F(x)+G(x)} where {\tt diff(G(x))=g(x)}. 7872\end{itemize} 7873Hence, {\tt ibpdv} returns the terms computed in an integration by parts, 7874with the possibility of doing several {\tt ibpdv}s successively.\\ 7875When the answer of {\tt ibpdv(u(x)*v'(x),v(x))} is computed, to obtain a 7876primitive of $u(x) v'(x)$, it remains to 7877compute the integral of the second term of this answer and then, to sum this 7878integral with the first term of this answer : to do this, just use 7879{\tt ibpdv} command with the answer as first argument and 7880a new $v(x)$ (or $0$ to terminate the integration) as second argument.\\ 7881Input : 7882\begin{center}{\tt ibpdv(ln(x),x) }\end{center} 7883Output : 7884\begin{center}{\tt [x ln(x),-1]}\end{center} 7885then 7886\begin{center}{\tt ibpdv([x ln(x),-1],0) }\end{center} 7887Output : 7888\begin{center}{\tt -x+x ln(x)}\end{center} 7889{\bf Remark}\\ 7890 When the first argument of {\tt ibpdv} is a list of two elements, {\tt ibpdv} 7891works only on the last element of this list and adds the integrated term to 7892the first element of this list. 7893(therefore it is possible to do several {\tt ibpdv}s successively).\\ 7894For example :\\ 7895{\tt ibpdv((log(x))\verb|^|2,x) = [x*(log(x))\verb|^|2,-(2*log(x))]}\\ 7896it remains to integrate {\tt -(2*log(x))}, the input :\\ 7897{\tt ibpdv(ans(),x)} or input :\\ 7898{\tt ibpdv([x*(log(x))\verb|^|2,-(2*log(x))],x)}\\ 7899Output :\\ 7900{\tt [x*(log(x))\verb|^|2+x*(-(2*log(x))),2]}\\ 7901and it remains to integrate {\tt 2}, hence input {\tt ibpdv(ans(),0)} or\\ 7902{\tt ibpdv([x*(log(x))\verb|^|2+x*(-(2*log(x))),2],0)}.\\ 7903Output : 7904{\tt x*(log(x))\verb|^|2+x*(-(2*log(x)))+2*x} 7905\subsubsection{\tt ibpu}\index{ibpu} 7906\noindent{\tt ibpu} is used to search the primitive of an expression written 7907as $u(x).v'(x)$ 7908{\tt ibpu} takes two arguments : 7909\begin{itemize} 7910\item an expression $u(x)*v'(x)$ and $u(x)$ (or a list of two expressions 7911$[F(x), u(x)*v'(x)]$ and $u(x)$), 7912\item an expression $g(x)$ and $0$ (or a list of two expressions $[F(x), g(x)]$ 7913and $0$). 7914\end{itemize} 7915{\tt ibpu} returns : 7916\begin{itemize} 7917\item if $u(x) \neq 0$, the list $[u(x)*v(x),-v(x)*u'(x)]$ 7918(or returns the list $[F(x)+u(x)*v(x),-v(x)*u'(x)]$), 7919\item if the second argument is zero, a primitive of the first argument $g(x)$ 7920(or $F(x)$+a primitive of $g(x)$):\\ 7921{\tt ibpu(g(x),0)} returns {\tt G(x)} where {\tt diff(G(x))=g(x)} or\\ 7922 {\tt ibpu([F(x),g(x)],0)} returns {\tt F(x)+G(x)} where {\tt diff(G(x))=g(x)}. 7923\end{itemize} 7924Hence, {\tt ibpu} returns the terms computed in an integration by parts, 7925with the possibility of doing several {\tt ibpu}s successively.\\ 7926When the answer of {\tt ibpu(u(x)*v'(x),u(x))} is computed, to obtain a 7927primitive of $u(x) v'(x)$, it remains to 7928compute the integral of the second term of this answer and then, to sum this 7929integral with the first term of this answer : to do this, just use 7930{\tt ibpu} command with the answer as first argument and 7931a new $u(x)$ (or $0$ to terminate the integration) as second argument.\\ 7932Input : 7933\begin{center}{\tt ibpu(ln(x),ln(x)) }\end{center} 7934Output : 7935\begin{center}{\tt [x*ln(x),-1]}\end{center} 7936then 7937\begin{center}{\tt ibpu([x*ln(x),-1],0) }\end{center} 7938Output : 7939\begin{center}{\tt -x+x*ln(x)}\end{center} 7940{\bf Remark}\\ 7941When the first argument of {\tt ibpu} is a list of two elements, {\tt ibpu} 7942works only on the last element of this list and adds the integrated term to 7943the first element of this list. 7944(therefore it is possible to do several {\tt ibpu}s successively).\\ 7945For example :\\ 7946{\tt ibpu((log(x))\verb|^|2,log(x)) = [x*(log(x))\verb|^|2,-(2*log(x))]}\\ 7947it remains to integrate {\tt -(2*log(x))}, hence input : \\ 7948{\tt ibpu(ans(),log(x))} 7949 or input :\\ 7950{\tt ibpu([x*(log(x))\verb|^|2,-(2*log(x))],log(x))}\\ 7951Output :\\ 7952{\tt [x*(log(x))\verb|^|2+x*(-(2*log(x))),2]}\\ 7953it remains to integrate {\tt 2}, hence input :\\ 7954{\tt ibpu(ans(),0)} or input :\\ 7955{\tt ibpu([x*(log(x))\verb|^|2+x*(-(2*log(x))),2],0)}.\\ 7956Output : 7957{\tt x*(log(x))\verb|^|2+x*(-(2*log(x)))+2*x} 7958 7959\subsection{Change of variables : {\tt subst}} 7960See the {\tt subst} command in the section \ref{sec:subst}. 7961 7962\section{Calculus of variations} 7963 7964\subsection{Determining whether a function is convex : {\tt convex\index{convex}}} 7965{\tt convex} takes two mandatory arguments, an at least twice differentiable function(al) $f:\mathbb{R}^n\to\mathbb{R}$ and a variable or list of variables. Some variables may depend on a common independent parameter, say $t$, when entered as e.g.~$x(t)$ instead of $x$. The first derivatives of such variables, when encountered in $f$, are treated as independent parameters of $f$. 7966 7967The command returns a condition or list of conditions under which $f$ is convex. If $f$ is convex on the entire domain, the return value is {\tt true}. If it is nowhere convex, the return value is {\tt false}. Otherwise, the conditions are returned as inequalities which depend on the parameters of $f$. The returned inequalities are not necessarily independent. 7968 7969An optional third argument {\tt simplify=false} or {\tt simplify=true} may be given. By default is {\tt simplify=true}, which means that simplification is applied when generating convexity conditions. If {\tt simplify=false}, only rational normalization is performed (using the {\tt ratnormal} command). 7970 7971The command operates by computing the Hessian $H_f$ of $f$ and its principal minors (in total $2^n$ of them where $n$ is the number of parameters) and checks their signs. If all minors are nonnegative, then $H_f$ is positive semidefinite and $f$ is therefore convex. 7972 7973The function $f$ is said to be \emph{concave} if the function $g=-f$ is convex. 7974 7975For example, input : 7976\begin{center} 7977 \tt convex(3*exp(x)+5x\verb|^|4-ln(x),x) 7978\end{center} 7979Output : 7980\begin{center} 7981 \tt true 7982\end{center} 7983Input : 7984\begin{center} 7985 \tt convex(x\verb|^|2+y\verb|^|2+3z\verb|^|2-x*y+2x*z+y*z,[x,y,z]) 7986\end{center} 7987Output : 7988\begin{center} 7989 \tt true 7990\end{center} 7991Input : 7992\begin{center} 7993 \tt convex(x1\verb|^|3+2x1\verb|^|2+2*x1*x2+x2\verb|^|2/2-8x1-2x2-8,[x1,x2]) 7994\end{center} 7995Output : 7996\begin{center} 7997 \tt [(3*x1+2)>=0,x1>=0] 7998\end{center} 7999In the example below, the function $f(x,y,z)=x^2+x\,z+a\,y\,z+z^2$ is not convex regardless of the value $a\in\mathbb{R}$ : 8000\begin{center} 8001 \tt convex(x\verb|^|2+x*z+a*y*z+z\verb|^|2,[x,y,z]) 8002\end{center} 8003Output : 8004\begin{center} 8005 \tt false 8006\end{center} 8007In the next example we find all values $a\in\mathbb{R}$ for which the function \[f(x,y,z)=x^2+2\,y^2+a\,z^2-2\,x\,y+2\,x\,z-6\,y\,z\] is convex on $\mathbb{R}^3$. Input : 8008\begin{center} 8009 \tt cond:=convex(x\verb|^|2+2y\verb|^|2+a*z\verb|^|2-2x*y+2x*z-6y*z,[x,y,z]) 8010\end{center} 8011Output : 8012\begin{center} 8013 \tt [a>=0,(a-1)>=0,(2*a-9)>=0,(a-5)>=0] 8014\end{center} 8015The returned inequalities are simplified by {\tt solve} : 8016\begin{center} 8017 \tt solve(cond,a) 8018\end{center} 8019Output : 8020\begin{center} 8021 \tt list[a>=5] 8022\end{center} 8023Therefore $f$ is convex for $a\geq 5$. 8024 8025Let's find the set $S\subset\mathbb{R}^2$ on which the function $f:\mathbb{R}^2\to\mathbb{R}$ defined by 8026\[ f(x_1,x_2)=\exp(x_1)+\exp(x_2)+x_1\,x_2 \] 8027is convex. Input : 8028\begin{center} 8029 \tt cond:=convex(exp(x1)+exp(x2)+x1*x2,[x1,x2] 8030\end{center} 8031Output : 8032\begin{center} 8033 \tt (exp(x1)*exp(x2)-1)>=0 8034\end{center} 8035Input : 8036\begin{center} 8037 \tt lin(cond) 8038\end{center} 8039Output : 8040\begin{center} 8041 \tt (exp(x1+x2)-1)>=0 8042\end{center} 8043From here we conclude that $f$ is convex when $x_1+x_2\geq 0$. The sought set $S$ is therefore the half-space defined by this inequality. 8044 8045The algorithm respects the assumptions that may be set upon variables. Therefore, the convexity of a given function can be checked only on a particular domain. For example, input : 8046\begin{center} 8047 \tt assume(x1>0),assume(x2>0):; convex(exp(x1)+exp(x2)+x1*x2,[x1,x2] 8048\end{center} 8049Output : 8050\begin{center} 8051 \tt true 8052\end{center} 8053Input : 8054\begin{center} 8055 \tt assume(x>=0 and x<=pi/4):; convex(exp(y)*sec(x)\verb|^|3-z,[x,y,z]) 8056\end{center} 8057Output : 8058\begin{center} 8059 \tt true 8060\end{center} 8061 8062\paragraph{\label{brachistochrone}The Brachistochrone Problem.} 8063We want to minimize the objective functional \[T(y)=\int_0^{x_1}L(t,y(t),y'(t))\,\mathrm{d} t\] where the Lagrangian $L$ is defined by 8064\[ L(t,y(t),y'(t))=\sqrt{\frac{1+y'(t)^2}{2\,g\,y(t)}} \] 8065for $y:[0,x_1]\to\mathbb{R}$ such that $y(0)=y_0$ and $y(x_1)=0$ where $x_1>0$ and $y_0>0$ are fixed (the constant $g$ is the gravitational acceleration). This is called the \emph{brachistochrone problem} (the problem of shortest travel by own weight from the point $(0,y_0)$ to $(x_1,0)$). 8066By solving Euler-Lagrange equation one obtains a cycloid $\overline{y}(t)$ as the only stationary function for $L$. The problem is to prove that it minimizes $T$, which would be easy if the integrand $L$ was convex. However, it's not the case here : 8067\begin{center} 8068 \tt assume(y>=0):; assume(g>0):; convex(sqrt((1+y'\verb|^|2)/(2*g*y)),y(t)) 8069\end{center} 8070Output : 8071\begin{center} 8072 \tt (-diff(y(t),t)\verb|^|2+3)>=0 8073\end{center} 8074This is equivalent to $|y'(t)|\leq\sqrt{3}$, which is certainly not satisfied by the cycloid $\overline{y}$ near the point $x=0$. 8075 8076Using the substitution $y(t)=z(t)^2/2$ we obtain $y'(t)=z'(t)\,z(t)$ and 8077\[ L(t,y(t),y'(t))=P(t,z(t),z'(t))=\sqrt{\frac{z(t)^{-2}+z'(t)^2}{g}}. \] 8078The function $P$ is convex : 8079\begin{center} 8080 \tt assume(z>=0):; convex(sqrt((z\verb|^|-2+z'\verb|^|2)/g),z(t)) 8081\end{center} 8082Output : 8083\begin{center} 8084 \tt true 8085\end{center} 8086Hence the function $\overline{z}(t)=\sqrt{2\,\overline{y}(t)}$, stationary for $P$ (which is verified directly), minimizes the objective functional \[U(z)=\int_0^{x_1}P(t,z(t),z'(t))\,\mathrm{d} t.\] From here and $U(z)=T(y)$ it easily follows that $\overline{y}$ minimizes $T$ and therefore the brachistochrone. For details see John L.~Troutman, \emph{Variational Calculus and Optimal Control} (second edition), page 257. 8087 8088\subsection{Euler-Lagrange equation(s) : {\tt euler\_lagrange\index{euler\_lagrange}}} 8089{\tt euler\_lagrange} takes from one to three arguments : 8090\begin{itemize} 8091 \item expression $f(x,y,y')$, 8092 \item independent variable (optional, by default $x$), 8093 \item dependent variable (optional, by default $y$). 8094\end{itemize} 8095If $y\in\mathbb{R}^n$ is required (by default $n=1$), one can enter $y=(y_1,y_2,\dots,y_n)$ as a vector $[y_1,y_2,\dots,y_n]$. In that case, $y':=(y_1',y_2',\dots,y_n')$. Alternatively, one can specify two arguments, $f$ and either $y(x)$ or $[y_1(x),y_2(x),\dots,y_n(x)]$. 8096 8097The return value is a system of differential Euler-Lagrange equations, which represent necessary conditions for extremum of the functional 8098\[ F(y)=\int_a^bf(x,y,y')\,\mathrm{d}x,\quad y\in C^2[a,b] \] 8099with boundary conditions $y(a)=A$ and $y(b)=B$ where $A,B\in\mathbb{R}$. If $n=1$, a single equation is returned : 8100\begin{equation}\label{eq:euler-lagrange}\frac{\partial f}{\partial y}=\frac{\mathrm{d}}{\mathrm{d}x}\,\frac{\partial f}{\partial y'}.\end{equation} 8101If $n>1$, there are $n$ Euler-Lagrange equations : 8102\[ \frac{\partial f}{\partial y_k}=\frac{\mathrm{d}}{\mathrm{d}x}\,\frac{\partial f}{\partial y_k'},\quad k=1,2,\dots,n. \] 8103The degrees of these differential equations are kept as low as possible. If, for example, $\frac{\partial f}{\partial y}=0$, the equation $\frac{\partial f}{\partial y'}=K$ is returned, where $K\in\mathbb{R}$ is an arbitrary constant. Similarly, using the Hamiltonian 8104\[ H(x,y,y')=y'\,\frac{\partial}{\partial y'}\,f(x,y,y')-f(x,y,y') \] 8105the Euler-Lagrange equation is simplified in case $n=1$ and $\frac{\partial f}{\partial t}=0$ to : 8106\begin{equation}\label{eq:hamiltonian}H(x,y,y')=K,\end{equation} 8107since it can be shown that $\frac{\mathrm{d}}{\mathrm{d}x}\,H(y,y',x)=0$. Therefore the Euler-Lagrange equations, which are generally of order two in $y$, are returned in simpler form of order one in the aforementioned cases. If $n=1$ and $\frac{\partial f}{\partial t}=0$ then both equations~\eqref{eq:euler-lagrange} and~\eqref{eq:hamiltonian} are returned, each of them being sufficient to determine $y$ (one of the returned equations is usually simpler than the other). 8108 8109It can be proven that if $f$ is convex (as a function of three independent variables), then a solution $y$ to Euler-Lagrange equations minimizes the functional $F$. 8110 8111For example, input : 8112\begin{center} 8113 \tt euler\_lagrange(sqrt(x'(t)\verb|^|2+y'(t)\verb|^|2),[x(t),y(t)]) 8114\end{center} 8115We obtain a system of two differential equations of order one : 8116\[ \begin{cases}\frac{x'(t)}{\sqrt{x'(t)^2+y'(t)^2}}&=K_0,\\\frac{y'(t)}{\sqrt{x'(t)^2+y'(t)^2}}&=K_1\end{cases} \] 8117where $K_0,K_1\in\mathbb{R}$ are arbitrary (these constants are generated automatically). 8118 8119In the following example we find the Euler-Lagrange equation for the brachistochrone problem, in which the functional 8120\[ F(y)=\frac{1}{\sqrt{2\,g}}\int_0^{x_1}\sqrt{\frac{1+y'(x)^2}{y(x)}}\,\mathrm{d}x \] 8121for some function $y\geq 0$ such that $y(0)=0$ and $y(x_1)=h>0$. It represents a curve alongside which an object travels, forced by the force of gravity (its vector pointing upwards), from the point $(0,0)$ to the point $(x_1,h)$ in shortest possible time. To obtain the corresponding Euler-Lagrange equation, input : 8122\begin{center} 8123 \tt assume(y>=0):; euler\_lagrange(sqrt((1+y\verb|'^|2)/y),t,y) 8124\end{center} 8125Output : 8126\begin{center} 8127 \tt [-1/sqrt(y(t)*(1+diff(y(t),t)\verb|^|2))=K\_2, 8128 diff(y(t),t,2)=(diff(y(t),t)\verb|^|2+1)/(2*y(t))] 8129\end{center} 8130It is easier to solve the first equation for $y$, since it is first-order and separable. 8131 8132In the next example we minimize the functional $F$ for $0<a<b$ and 8133\[ f(x,y,y')=x^2\,y'(x)^2+y(x)^2. \] 8134Input : 8135\begin{center} 8136 \tt f:=x\verb|^|2*diff(y(x),x)\verb|^|2+y\verb|^|2:; eq:=euler\_lagrange(f) 8137\end{center} 8138We obtain the following Euler-Lagrange equation : 8139\[ y''=\frac{1}{x^2}\,(y-2\,x\,y'). \] 8140It can be solved by assuming $y(x)=x^r$ for some $r\in\mathbb{R}$. Input : 8141\begin{center} 8142 \tt solve(subs(eq,y(x)=x\verb|^|r),r) 8143\end{center} 8144Output : 8145\begin{center} 8146 \tt [-(sqrt(5)+1)/2,(sqrt(5)-1)/2] 8147\end{center} 8148Note that a pair of independent solutions is also returned by {\tt kovacicsols} command : 8149\begin{center} 8150 \tt assume(x>=0):; kovacicsols(y\verb|''|=(y-2x*y')/x\verb|^|2,x,y) 8151\end{center} 8152Output : 8153\begin{center} 8154 \tt [sqrt(x\verb|^|(sqrt(5)-1)),sqrt(x\verb|^|(-(sqrt(5))-1))] 8155\end{center} 8156Anyway, we conclude that $y=C_1\,x^{-\frac{\sqrt{5}+1}{2}}+C_2\,x^{\frac{\sqrt{5}+1}{2}}$. The values of $C_1$ and $C_2$ are determined from the boundary conditions. Finally we prove that $f$ is convex : 8157\begin{center} 8158 \tt convex(f,y(x)) 8159\end{center} 8160Output : 8161\begin{center} 8162 \tt true 8163\end{center} 8164Therefore, $y$ minimizes $F$ on $[a,b]$. 8165 8166In the example below we find the function \[y\in\left\{y\in C^1\left[\frac{1}{2},1\right]:y\left(\frac{1}{2}\right)=-\frac{\sqrt{3}}{2},y(1)=0\right\}\] which minimizes the functional 8167\[ F(y)=\int_{1/2}^1\frac{\sqrt{1+y'(x)^2}}{x}\,\mathrm{d}x. \] 8168To obtain the corresponding Euler-Lagrange equation, input : 8169\begin{center} 8170 \tt f:=sqrt(1+diff(y(x),x)\verb|^|2)/x:; eq:=euler\_lagrange(f) 8171\end{center} 8172Output : 8173\begin{center} 8174 \tt diff(y(x),x)/(sqrt(diff(y(x),x)\verb|^|2+1)*x)=K\_3 8175\end{center} 8176Input : 8177\begin{center} 8178 \tt sol:=dsolve(eq) 8179\end{center} 8180Output : 8181\begin{center} 8182 \tt [c\_0-(sqrt(-K\_3\verb|^|2*x\verb|^|2+1))/K\_3] 8183\end{center} 8184The sought solution is the function of the above form which satisfies the boundary conditions. Input : 8185\begin{center} 8186 \tt y0:=sol[0]:; c:=[K\_3,c\_0]:; v:=solve([subs(y0,x=1/2)=-sqrt(3)/2,subs(y0,x=1)=0],c) 8187\end{center} 8188Output : 8189\begin{center} 8190 \tt [[1,0]] 8191\end{center} 8192Input : 8193\begin{center} 8194 \tt y0:=normal(subs(y0,c,v[0]) 8195\end{center} 8196Output : 8197\begin{center} 8198 \tt -sqrt(1-x\verb|^|2) 8199\end{center} 8200To prove that $y_0(x)=-\sqrt{1-x^2}$ is indeed a minimizer for $F$, we show that the integrand in $F(y)$ is convex. Input : 8201\begin{center} 8202 \tt convex(sqrt(1+y'\verb|^|2)/x,y(x)) 8203\end{center} 8204Output : 8205\begin{center} 8206 \tt x>=0 8207\end{center} 8208Hence the integrand is convex for $x\in\left[\frac{1}{2},1\right]$. 8209 8210Similarly, we find the minimizer for 8211\[ F(y)=\int_0^{\pi}\left(2\,\sin(x)\,y(x)+y'(x)^2\right)\,\mathrm{d}x \] 8212where $y\in C^1[0,\pi]$ and $y(0)=y(\pi)=0$. Input : 8213\begin{center} 8214 \tt f:=2*sin(x)*y(x)+diff(y(x),x)\verb|^|2:; eq:=euler\_lagrange(f) 8215\end{center} 8216Output : 8217\begin{center} 8218 \tt diff(y(x),x,2)=sin(x) 8219\end{center} 8220Input : 8221\begin{center} 8222 \tt dsolve(eq and y(0)=0 and y(pi)=0,x,y) 8223\end{center} 8224Output : 8225\begin{center} 8226 \tt -sin(x) 8227\end{center} 8228The above function is the sought minimizer as the integrand $f$ is convex : 8229\begin{center} 8230 \tt convex(f,y(x)) 8231\end{center} 8232Output : 8233\begin{center} 8234 \tt true 8235\end{center} 8236 8237In the next example we minimize the functional $F(y)=\int_0^1(y'(x)^4-4\,y(x))\,\mathrm{d}x$ on $C^1[0,1]$ with boundary conditions $y(0)=1$ and $y(1)=2$. First we solve the associated Euler-Lagrange equation : 8238\begin{center} 8239 \tt eq:=euler\_lagrange(y'\verb|^|4-4y,x,y) 8240\end{center} 8241Output : 8242\begin{center} 8243 \tt [(3*diff(y(x),x)\verb|^|4+4*y(x))=K\_4, diff(y(x),x,2)=-1/(3*diff(y(x),x)\verb|^|2)] 8244\end{center} 8245Input : 8246\begin{center} 8247 \tt dsolve(eq[1] and y(0)=1 and y(1)=2,x,y) 8248\end{center} 8249Output : 8250\begin{center} 8251 \tt [-3*(-x+1.52832425067)\verb|^|(4/3)/4+2.32032831141] 8252\end{center} 8253We find that the integrand in $F(y)$ is convex : 8254\begin{center} 8255 \tt convex(y\verb|'^|4-4y,[x,y]) 8256\end{center} 8257Output : 8258\begin{center} 8259 \tt true 8260\end{center} 8261Hence the minimizer is 8262\[ y_0(x)=\frac{3}{4}\,(1.52832425067-x)^{4/3}+2.32032831141,\quad 0\leq x\leq 1. \] 8263 8264\subsection{Jacobi equation : {\tt jacobi\_equation\index{jacobi\_equation}}} 8265{\tt jacobi\_equation} takes five or six arguments : 8266\begin{itemize} 8267 \item expression $f(y,y',x)$, 8268 \item independent variable $x$, 8269 \item dependent variable $y$ (this argument and the previous one can be combined to a single argument $y(x)$, in which case the call has five arguments), 8270 \item expression $y_0\in C^1[a,b]$ which is stationary for the functional $F(y)=\int_a^bf(y,y',x)\,\mathrm{d}x$, 8271 \item symbol $h$ for the unknown function in Jacobi equation, 8272 \item point $a\in\mathbb{R}$, which is the lower bound for $x$. 8273\end{itemize} 8274The return value contains the Jacobi equation 8275\begin{equation}\label{eq:jacobi-equation} 8276 -\frac{\mathrm{d}}{\mathrm{d}t}\,\left(f_{y'\,y'}(y_0,y_0',t)\,h'\right)+\left(f_{y\,y}(y_0,y_0',t)-\frac{\mathrm{d}}{\mathrm{d}t}\,f_{y\,y'}(y_0,y_0',t)\right)\,h=0. 8277\end{equation} 8278If the Jacobi equation has a solution such that $h(a)=0$, $h(c)=0$ for some $c\in(a,b]$ and $h$ not identically zero on $[a,c]$, then $y_0$ does not minimize the functional $F$. It is said that $c$ is \emph{conjugate} to $a$. The function $y_0$ minimizes $F$ if $f_{y'\,y'}(y_0,y_0',t)>0$ for all $t\in[a,b]$ and there are no points conjugate to $a$ in $(a,b]$. 8279 8280If the Jacobi equation can be solved by {\tt dsolve}, a sequence containing the equation~\eqref{eq:jacobi-equation} and its solution is returned. Otherwise, if~\eqref{eq:jacobi-equation} cannot be solved immediately, only the Jacobi equation is returned. 8281 8282For example, input : 8283\begin{center} 8284 \tt jacobi\_equation(-1/2*y'(t)\verb|^|2+y(t)\verb|^|2/2,t,y,sin(t),h,0) 8285\end{center} 8286Output : 8287\begin{center} 8288 \tt (-diff(h(t),t,2)-h(t))=0, c\_0*sin(t) 8289\end{center} 8290 8291\subsection{Finding conjugate points : {\tt conjugate\_equation\index{conjugate\_equation}}} 8292{\tt conjugate\_equation} takes four arguments : 8293\begin{itemize} 8294 \item expression $y_0$ which depends on the independent variable and two parameters, 8295 \item list $[\alpha,\beta]$ of parameters which $y_0$ depends on, 8296 \item list $[A,B]$ of the values of parameters $\alpha$ and $\beta$, respectively, 8297 \item independent variable $x$, 8298 \item real number $a$ equal to the lower or to the upper bound for $x$. 8299\end{itemize} 8300The function $y_0(x)$ is assumed to be stationary for the problem of minimizing some functional $F(y)=\int_a^bf(x,y,y')\,\mathrm{d}x$. The return value is the expression 8301\begin{equation}\label{eq:conjugate-equation} 8302 \frac{\partial y_0(t)}{\partial\alpha}\,\frac{\partial y_0(a)}{\partial\beta}- 8303 \frac{\partial y_0(a)}{\partial\alpha}\,\frac{\partial y_0(t)}{\partial\beta}, 8304\end{equation} 8305at $\alpha=A$ and $\beta=B$, which is zero if and only if $t$ is conjugate to $a$. To find any conjugate points, set the returned expression to zero and solve. 8306 8307For example, we find a minimum for the functional 8308\[ F(y)=\int_0^{\frac{\pi}{2}}\left(y'(x)^2-x\,y(x)-y(x)^2\right)\,\mathrm{d}x \] 8309on $D=\{y\in C^1[0,\pi/2]:y(0)=y(\pi/2)=0\}$. The corresponding Euler-Lagrange equation is : 8310\begin{center} 8311 \tt eq:=euler\_lagrange(y'(x)\verb|^|2-x*y(x)-y(x)\verb|^|2,y(x)) 8312\end{center} 8313Output : 8314\begin{center} 8315 \tt diff(y(x),x,2)=((-2*y(x)-x)/2) 8316\end{center} 8317The general solution is : 8318\begin{center} 8319 \tt y0:=dsolve(eq,x,y) 8320\end{center} 8321Output : 8322\begin{center} 8323 \tt c\_0*cos(x)+c\_1*sin(x)-x/2 8324\end{center} 8325The stationary function depends on two parameters $c_0$ and $c_1$ which are fixed by the boundary conditions : 8326\begin{center} 8327 \tt c:=solve([subs(y0,x,0)=0,subs(y0,x,pi/2)=0],[c\_0,c\_1]) 8328\end{center} 8329Output : 8330\begin{center} 8331 \tt [[0,pi/4]] 8332\end{center} 8333Input : 8334\begin{center} 8335 \tt conjugate\_equation(y0,[c\_0,c\_1],c[0],x,0) 8336\end{center} 8337Output : 8338\begin{center} 8339 \tt sin(x) 8340\end{center} 8341The above expression obviously has no zeros in $(0,\pi/2]$, hence there are no points conjugate to $0$. Since $f_{y'\,y'}=2>0$, where $f(y,y',x)$ is the integrand in $F(y)$ (the strong Legendre condition), $y_0$ minimizes $F$ on $D$. To obtain $y_0$ explicitly, input : 8342\begin{center} 8343 \tt subs(y0,[c\_0,c\_1],c[0]) 8344\end{center} 8345Output : 8346\begin{center} 8347 \tt pi*sin(x)/4-x/2 8348\end{center} 8349 8350\subsection{An example : finding the surface of revolution with minimal area} 8351In this section we find the function 8352\[ y_0\in D=\{y\in C^1[0,1]:y(0)=1,y(1)=2/3\} \] 8353for which the area of the corresponding surface of revolution is minimal. The result is not necessarily intuitive. 8354 8355The area of the surface of revolution is measured by the functional 8356\[ F(y)=2\,\pi\,\int_0^1y(x)\,\sqrt{1+y'(x)^2}\,\mathrm{d}x. \] 8357We set $f(y,y',x)=y(x)\,\sqrt{1+y'(x)^2}$ and compute the associated Euler-Lagrange equation : 8358\begin{center} 8359 \tt dy:=diff(y(x),x):; f:=y*sqrt(1+dy\verb|^|2):; eq:=euler\_lagrange(f) 8360\end{center} 8361Output : 8362\begin{center} 8363 \tt [-(y(x))/(sqrt(diff(y(x),x)\verb|^|2+1))=K\_0, diff(y(x),x,2)=((diff(y(x),x)\verb|^|2+1)/(y(x)))] 8364\end{center} 8365We obtain the stationary function by finding the general solution of the first equation. Input : 8366\begin{center} 8367 \tt sol:=collect(simplify(dsolve(eq[0],x,y))) 8368\end{center} 8369Output : 8370\begin{center} 8371 \tt [-K\_0,K\_0*(-exp((x-c\_1)/K\_0)\verb|^|2-1)/(2*exp((x-c\_1)/K\_0))] 8372\end{center} 8373Obviously the constant solution $-K_0$ is not in $D$, so we set $y_0$ to be the second element of the above list. That function, which can be written as \[ y_0(x)=-K_0\,\cosh\left(\frac{x-c_1}{K_0}\right), \] is called a \emph{catenary}. Input : 8374\begin{center} 8375 \tt y0:=sol[1]:; p:=[K\_0,c\_1]:; 8376\end{center} 8377To find the values of $K_0$ and $c_1$ from the boundary conditions, we first plot the curves $y_0(0)=1$ and $y_0(1)=\frac{2}{3}$ for $K_0\in[-1,1]$ and $c_1\in[-1,2]$ to see where they intersect each other. Input : 8378\begin{center} 8379 \tt eq1:=subs(y0,x=0)=1:; eq2:=subs(y0,x=1)=2/3:; implicitplot([eq1,eq2],K\_0=-1..1,c\_1=-1..2) 8380\end{center} 8381Output : 8382\begin{center} 8383 \includegraphics[width=0.75\textwidth]{catenoid_intersections.png} 8384\end{center} 8385We observe that there are exactly two catenaries satisfying the Euler-Lagrange necessary conditions and the given boundary conditions : the first with $K_0\approx -0.5$ and $c_1\approx 0.6$ resp.~the second with $K_0\approx -0.3$ and $c_1\approx 0.5$. We obtain the values of these constants more precisely by using {\tt fsolve}. Input : 8386\begin{center} 8387 \tt p1:=fsolve([eq1,eq2],p,[-0.5,0.6]); 8388 p2:=fsolve([eq1,eq2],p,[-0.3,0.5]) 8389\end{center} 8390Output : 8391\begin{center} 8392 \tt [-0.56237423894,0.662588703113], [-0.30613431407,0.567138261119] 8393\end{center} 8394We check, for each catenary, whether the strong Legendre condition \[f_{y'\,y'}(x,y_k,y_k')>0\] holds for $k=1,2$. Input : 8395\begin{center} 8396 \tt y1:=subs(y0,p,p1):; y2:=subs(y0,p,p2):; 8397 D2f:=diff(f,diff(y(x),x),2):; 8398 solve([eval(subs(D2f,y=y1,y(x)=y1))<=0,x>=0,x<=1],x); 8399 solve([eval(subs(D2f,y=y2,y(x)=y2))<=0,x>=0,x<=1],x) 8400\end{center} 8401Output : 8402\begin{center} 8403 \tt [],[] 8404\end{center} 8405We conclude that the strong Legendre condition is satisfied in both cases, so we proceed by attempting to find the points conjugate to 0 for each catenary. The function $y_0$ depends on two parameters, so we use {\tt conjugate\_equation} to find these points easily. Input : 8406\begin{center} 8407 \tt fsolve(conjugate\_equation(y0,p,p1,x,0)=0,x=0..1) 8408 fsolve(conjugate\_equation(y0,p,p2,x,0)=0,x=0..1) 8409\end{center} 8410Output : 8411\begin{center} 8412 \tt [0.0], [0.0,0.799514772606] 8413\end{center} 8414We conclude that there are no points conjugate to $0$ in $(0,1]$ for the catenary $y_1$, so it minimizes the functional $F$. However, for the other catenary there is a conjugate point in the relevant interval, therefore $y_2$ is not a minimizer. 8415 8416We can verify the above conclusions by computing the surface area for catenaries $y_1$ and $y_2$ and comparing them. Input : 8417\begin{center} 8418 \tt int(y1*sqrt(1+diff(y1,x)\verb|^|2),x=0..1); int(y2*sqrt(1+diff(y2,x)\verb|^|2),x=0..1) 8419\end{center} 8420Output : 8421\begin{center} 8422 \tt 0.81396915825,0.826468466845 8423\end{center} 8424We see that the surface formed by rotating the curve $y_1$ is indeed smaller than the area of the surface formed by rotating the curve $y_2$. Finally, we visualize both surfaces for convenience. Input : 8425\begin{center} 8426 \tt plot3d([y1*cos(t),y1*sin(t),x],x=0..1,t=0..2*pi, display=yellow+filled) 8427\end{center} 8428Output : 8429\begin{center} 8430 \includegraphics[width=0.75\textwidth]{catenoid_1.png} 8431\end{center} 8432Input : 8433\begin{center} 8434 \tt plot3d([y2*cos(t),y2*sin(t),x],x=0..1,t=0..2*pi, display=yellow+filled) 8435\end{center} 8436Output : 8437\begin{center} 8438 \includegraphics[width=0.75\textwidth]{catenoid_2.png} 8439\end{center} 8440 8441\section{Limits} 8442\subsection{Limits : {\tt limit}}\index{limit|textbf}\label{sec:limit} 8443 8444{\tt limit} computes the limit of an expression at a finite or infinite point. 8445It is also possible with an optional argument to compute a one-sided 8446limit (1 for the right limit and -1 for the left limit).\\ 8447{\tt limit} takes three or four arguments :\\ 8448an expression, the name of a variable (for example {\tt x}), the limit point 8449(for example {\tt a}) and an optional argument, by default {\tt 0}, to 8450indicate if the limit is unidirectional. 8451This argument is equal to 8452{\tt -1} for a left limit ({\tt x<a}) or is equal to {\tt 1} 8453for a right limit ({\tt x>a}) or is equal to {\tt 0} for a limit.\\ 8454{\tt limit} returns the limit of the expression when the variable (for example 8455{\tt x}) approaches the limit point (for example {\tt a}).\\ 8456{\bf Remark}\\ 8457It is also possible to put {\tt x=a} as argument instead of {\tt x,a}, hence : 8458{\tt limit} takes also as arguments an expression depending of a variable, 8459an equality (variable =value of the limit point) and perhaps 1 or -1 to 8460indicate the direction.\\ 8461Input : 8462\begin{center}{\tt limit(1/x,x,0,-1)}\end{center} 8463or : 8464\begin{center}{\tt limit(1/x,x=0,-1)}\end{center} 8465Output : 8466\begin{center}{\tt -(infinity)}\end{center} 8467Input : 8468\begin{center}{\tt limit(1/x,x,0,1)}\end{center} 8469or : 8470\begin{center}{\tt limit(1/x,x=0,1)}\end{center} 8471Output : 8472\begin{center}{\tt +(infinity)}\end{center} 8473Input : 8474\begin{center}{\tt limit(1/x,x,0,0)}\end{center} 8475or : 8476\begin{center}{\tt limit(1/x,x,0)}\end{center} 8477or : 8478\begin{center}{\tt limit(1/x,x=0)}\end{center} 8479Output : 8480\begin{center}{\tt infinity}\end{center} 8481Hence, {\tt abs(1/x)} approaches $+\infty$ when $x$ approaches $0$. 8482 8483{\bf Exercises} : 8484\begin{itemize} 8485\item Find for $n>2$, the limit when $x$ approaches $0$ of : 8486$$ \frac{n\tan(x)-\tan(nx)}{\sin(nx)-n\sin(x)}$$ 8487Input : 8488\begin{center}{\tt limit((n*tan(x)-tan(n*x))/(sin(n*x)-n*sin(x)),x=0)}\end{center} 8489Output : 8490\begin{center}{\tt 2 }\end{center} 8491\item Find the limit when $x$ approaches $+\infty$ of : 8492$$\sqrt{x+\sqrt{x+\sqrt x}}-\sqrt x$$ 8493Input : 8494\begin{center}{\tt limit(sqrt(x+sqrt(x+sqrt(x)))-sqrt(x),x=+infinity)}\end{center} 8495Output : 8496\begin{center}{\tt 1/2 }\end{center} 8497\item Find the limit when $x$ approaches 0 of : 8498$$\frac{\sqrt{1+x+x^2/2}-\exp(x/2)}{(1-\cos(x))\sin(x)}$$ 8499Input : 8500\begin{center}{\tt limit((sqrt(1+x+x\verb|^|2/2)-exp(x/2))/((1-cos(x))*sin(x)),x,0)}\end{center} 8501Output : 8502\begin{center}{\tt -1/6 }\end{center} 8503\end{itemize} 8504 8505{\bf Remark}\\ 8506To compute limits, it is better sometimes to quote the first argument.\\ 8507Input : 8508\begin{center}{\tt limit('(2*x-1)*exp(1/(x-1))',x=+infinity)}\end{center} 8509Note that the first argument is quoted, because it is better that 8510this argument is not simplified (i.e. not evaluated).\\ 8511Output : 8512\begin{center}{\tt +(infinity)}\end{center} 8513 8514\subsection{Integral and limit}\index{limit} \index{limit} 8515Just two examples :\\ 8516\begin{itemize} 8517\item Find the limit, when $a$ approaches $+\infty$, of : 8518$$ \int _2^a \frac {1}{x^2}\ dx$$ 8519Input : 8520\begin{center}{\tt limit(integrate(1/(x\verb|^|2),x,2,a),a,+(infinity))}\end{center} 8521Output (if {\tt a} is assigned then input {\tt purge(a)}) : 8522\begin{center}{\tt 1/2}\end{center} 8523\item Find the limit, when $a$ approaches $+\infty$, of : 8524$$ \int _2^a (\frac {x}{x^2-1}+\ln(\frac {x+1}{x-1}))\ dx$$ 8525Input : 8526\begin{center}{\tt limit(integrate(x/(x\verb|^|2-1)+log((x+1)/(x-1)),x,2,a),}\end{center} 8527\begin{center}{\tt a,+(infinity))}\end{center} 8528Output (if {\tt a} is assigned then input {\tt purge(a)}) : 8529\begin{center}{\tt +(infinity)}\end{center} 8530\end{itemize} 8531 8532\section{Rewriting transcendental and trigonometric expressions} 8533\subsection{Expand a transcendental and trigonometric expression : {\tt texpand tExpand}}\index{texpand|textbf}\index{tExpand|textbf} 8534\noindent{\tt texpand} or {\tt tExpand} takes as argument an 8535expression containing transcendental or trigonometric functions.\\ 8536{\tt texpand} or {\tt tExpand} expands these functions, like simultaneous 8537calling {\tt expexpand}, {\tt lnexpand} and {\tt trigexpand}, 8538for example, $\ln(x^n)$ becomes $\ n\ln(x)$, $\exp(nx)$ 8539becomes $\ \exp(x)^n$, $\sin(2x)$ becomes $\ 2\sin(x)\cos(x)$...\\ 8540{\bf Examples} :\\ 8541\begin{itemize} 8542\item 8543\begin{enumerate} 8544\item Expand $\cos(x+y)$.\\ 8545Input : 8546\begin{center}{\tt texpand(cos(x+y))}\end{center} 8547Output : 8548\begin{center}{\tt cos(x)*cos(y)-sin(x)*sin(y)}\end{center} 8549\item Expand $\cos(3x)$.\\ 8550Input : 8551\begin{center}{\tt texpand(cos(3*x))}\end{center} 8552Output : 8553\begin{center}{\tt 4*(cos(x))\verb|^| 3-3*cos(x)}\end{center} 8554\item Expand $\displaystyle \frac{\sin(3*x)+\sin(7*x)}{\sin(5*x)}$.\\ 8555Input : 8556\begin{center}{\tt texpand((sin(3*x)+sin(7*x))/sin(5*x))}\end{center} 8557Output 8558\begin{center}{\tt (4*(cos(x))\verb|^|2-1)*(sin(x)/(16*(cos(x))\verb|^|4- 12*(cos(x))\verb|^|2+1))/sin(x)+(64*(cos(x))\verb|^|6- 80*(cos(x))\verb|^|4+24*(cos(x))\verb|^|2-1)*sin(x)/ (16*(cos(x))\verb|^|4-12*(cos(x))\verb|^|2+1)/sin(x)}\end{center} 8559Output, after a simplification with {\tt normal(ans())} : 8560\begin{center}{\tt 4*(cos(x))\verb|^|2-2}\end{center} 8561\end{enumerate} 8562\item \begin{enumerate} 8563\item Expand $\exp(x+y)$.\\ 8564Input : 8565\begin{center}{\tt texpand(exp(x+y))}\end{center} 8566Output : 8567\begin{center}{\tt exp(x)*exp(y)}\end{center} 8568\item Expand $\ln(x\times y)$.\\ 8569Input : 8570\begin{center}{\tt texpand(log(x*y))}\end{center} 8571Output : 8572\begin{center}{\tt log(x)+log(y)}\end{center} 8573\item Expand $\ln(x^n)$.\\ 8574Input : 8575\begin{center}{\tt texpand(ln(x\verb|^|n))}\end{center} 8576Output : 8577\begin{center}{\tt n*ln(x)}\end{center} 8578\item Expand $\ln((e^2)+\exp(2*\ln(2))+\exp(\ln(3)+\ln(2)))$.\\ 8579Input : 8580\begin{center}{\tt texpand(log(e\verb|^|2)+exp(2*log(2))+exp(log(3)+log(2)))}\end{center} 8581Output : 8582\begin{center}{\tt 6+3*2}\end{center} 8583Or input : 8584\begin{center}{\tt texpand(log(e\verb|^|2)+exp(2*log(2)))+ lncollect(exp(log(3)+log(2)))}\end{center} 8585Output : 8586\begin{center}{\tt 12}\end{center} 8587\end{enumerate} 8588\item 8589Expand $\exp(x+y)+\cos(x+y)+\ln(3x^2)$.\\ 8590Input : 8591\begin{center}{\tt texpand(exp(x+y)+cos(x+y)+ln(3*x\verb|^|2))}\end{center} 8592Output : 8593\begin{center}{\tt cos(x)*cos(y)-sin(x)*sin(y)+exp(x)*exp(y)+ ln(3)+2*ln(x)}\end{center} 8594\end{itemize} 8595 8596\subsection{Combine terms of the same type : {\tt combine}}\index{combine}\index{exp@{\sl exp}|textbf}\index{log@{\sl log}|textbf}\index{ln@{\sl ln}|textbf}\index{sin@{\sl sin}|textbf}\index{cos@{\sl cos}|textbf}\index{trig@{\sl trig}|textbf} 8597\noindent{\tt combine} takes two arguments : an expression and 8598the name of a function or class of functions 8599{\tt exp,log,ln, sin,cos,trig}.\\ 8600Whenever possible, {\tt combine} put together subexpressions corresponding 8601to the second argument: 8602\begin{itemize} 8603\item {\tt combine(expr,ln)} or {\tt combine(expr,log)} gives the same result 8604as {\tt lncollect(expr)} 8605\item 8606{\tt combine(expr,trig)} or {\tt combine(expr,sin)} or {\tt combine(expr,cos)} 8607gives the same result as {\tt tcollect(expr)}. 8608\end{itemize} 8609Input : 8610\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),exp)}\end{center} 8611Output : 8612\begin{center}{\tt exp(x+y)+sin(x)*cos(x)+ln(x)+ln(y)}\end{center} 8613Input : 8614\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),trig)}\end{center} 8615or 8616\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),sin)}\end{center} 8617or 8618\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),cos)}\end{center} 8619Output : 8620\begin{center}{\tt exp(y)*exp(x)+(sin(2*x))/2+ln(x)+ln(y)}\end{center} 8621Input : 8622\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),ln)}\end{center} 8623or 8624\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),log)}\end{center} 8625Output : 8626\begin{center}{\tt exp(x)*exp(y)+sin(x)*cos(x)+ln(x*y)}\end{center} 8627 8628\section{Trigonometry} 8629\subsection{Trigonometric functions}\label{sec:trigo} 8630\begin{itemize} 8631\item {\tt sin} \index{sin} is the sine function, 8632\item {\tt cos} \index{cos} is the cosine function, 8633\item {\tt tan} \index{tan} is the tangent function ({\tt tan(x)= 8634 sin(x)/cos(x)}), 8635\item 8636{\tt cot} \index{cot|textbf} is the cotangent function ({\tt cot(x)= 8637 cos(x)/sin(x)}), 8638\item 8639{\tt sec} \index{sec|textbf} is the secant function ({\tt sec(x)= 8640 1/cos(x)}), 8641\item 8642{\tt csc} \index{csc|textbf} is the cosecant function ({\tt csc(x) = 8643 1/sin(x)}), 8644\item 8645{\tt asin} or {\tt arcsin}\index{asin}\index{arcsin}, {\tt acos} or {\tt arccos}\index{acos}\index{arccos}, {\tt atan} or {\tt arctan}\index{atan}\index{arctan}, {\tt acot}\index{acot|textbf}, {\tt asec}\index{asec|textbf}, {\tt acsc}\index{acsc|textbf} 8646are the inverse trigonometric functions. The latter are defined by: 8647\begin{enumerate} 8648\item {\tt asec(x) = acos(1/x)}, 8649\item 8650{\tt acsc(x) = asin(1/x)}, 8651\item 8652{\tt acot(x) = atan(1/x)}. 8653\end{enumerate} 8654\end{itemize} 8655 8656 8657\subsection{Expand a trigonometric expression : {\tt trigexpand}}\index{trigexpand} 8658\noindent{\tt trigexpand} takes as argument an expression 8659containing trigonometric functions.\\ 8660{\tt trigexpand} expands sums, differences and products by an integer 8661inside the trigonometric functions \\ 8662Input : 8663\begin{center}{\tt trigexpand(cos(x+y))}\end{center} 8664Output : 8665\begin{center}{\tt cos(x)*cos(y)-sin(x)*sin(y)}\end{center} 8666 8667 8668\subsection{Linearize a trigonometric expression : {\tt tlin}}\index{tlin} 8669\noindent{\tt tlin} takes as argument an expression 8670containing trigonometric functions.\\ 8671{\tt tlin} linearizes products and integer powers of the trigonometric 8672functions (e.g. in terms of $\sin(n*x)$ and 8673$\cos(n*x)$)\\ 8674{\bf Examples} 8675\begin{itemize} 8676\item Linearize $\cos(x)*\cos(y)$.\\ 8677Input : 8678\begin{center}{\tt tlin(cos(x)*cos(y))}\end{center} 8679Output : 8680\begin{center}{\tt 1/2*cos(x-y)+1/2*cos(x+y)}\end{center} 8681\item Linearize $\cos(x)^3$.\\ 8682Input : 8683\begin{center}{\tt tlin(cos(x)\verb|^|3)}\end{center} 8684Output : 8685\begin{center}{\tt 3/4*cos(x)+1/4*cos(3*x)}\end{center} 8686\item Linearize $4\cos(x)^2-2$.\\ 8687Input : 8688\begin{center}{\tt tlin(4*cos(x)\verb|^|2-2)}\end{center} 8689Output : 8690\begin{center}{\tt 2*cos(2*x)}\end{center} 8691\end{itemize} 8692 8693\subsection{Increase the phase by $\pi/2$ in a trigonometric 8694expression: \texttt{shift\_phase}}\index{shift\_phase} 8695 8696The \texttt{shift\_phase} command takes as argument a trigonometric 8697expression.\\ 8698\texttt{shift\_phase} returns the expression with phase increased by 8699$\pi/2$ (after the automatic simplification).\\ 8700Input: 8701\begin{center} 8702 \tt 8703 shift\_phase(x + sin(x)) 8704\end{center} 8705Output: 8706\begin{center} 8707 \tt 8708 x-cos((pi+2*x)/2) 8709\end{center} 8710Input: 8711\begin{center} 8712 \tt 8713 shift\_phase(x + cos(x)) 8714\end{center} 8715Output: 8716\begin{center} 8717 \tt 8718 x+sin((pi+2*x)/2) 8719\end{center} 8720Input: 8721\begin{center} 8722 \tt 8723 shift\_phase(x + tan(x)) 8724\end{center} 8725Output: 8726\begin{center} 8727 \tt 8728 x-1/tan((pi+2*x)/2) 8729\end{center} 8730 8731Quoting the argument will prevent the automatic simplification.\\ 8732Input: 8733\begin{center} 8734 \tt 8735 shift\_phase('sin(x + pi/2)') 8736\end{center} 8737Output: 8738\begin{center} 8739 \tt 8740 -cos((pi+2*x+2*pi/2)/2) 8741\end{center} 8742With an unquoted sine, we get:\\ 8743Input 8744\begin{center} 8745 \tt 8746 shift\_phase(sin(x + pi/2)) 8747\end{center} 8748Output: 8749\begin{center} 8750 \tt 8751 sin((pi+2*x)/2) 8752\end{center} 8753since \texttt{sin(x+pi/2)} is evaluated (in this case simplified) 8754before \texttt{shift\_phase} is called, and 8755\texttt{shift\_phase(cos(x))} returns \texttt{sin((pi+2*x)/2)} 8756 8757 8758\subsection{Put together sine and cosine of the same angle : {\tt tcollect tCollect}}\index{tcollect}\index{tCollect} 8759\noindent{\tt tcollect} or {\tt tCollect} takes as argument 8760an expression containing trigonometric functions.\\ 8761{\tt tcollect} first linearizes this expression 8762(e.g. in terms of $\sin(n*x)$ and $\cos(n*x)$), 8763then, puts together sine and cosine of the same angle.\\ 8764Input : 8765\begin{center}{\tt tcollect(sin(x)+cos(x))}\end{center} 8766Output : 8767\begin{center}{\tt sqrt(2)*cos(x-pi/4)}\end{center} 8768Input : 8769\begin{center}{\tt tcollect(2*sin(x)*cos(x)+cos(2*x))}\end{center} 8770Output : 8771\begin{center}{\tt sqrt(2)*cos(2*x-pi/4)}\end{center} 8772 8773\subsection{Simplify : {\tt simplify}}\index{simplify} 8774\noindent{\tt simplify} simplifies the expression.\\ 8775As with all automatic simplifications, do not expect miracles, 8776you will have to use specific rewriting rules if it does not work.\\ 8777Input : 8778\begin{center}{\tt simplify((sin(3*x)+sin(7*x))/sin(5*x))}\end{center} 8779Output : 8780\begin{center}{\tt 4*(cos(x))\verb|^|2-2}\end{center} 8781{\bf Warning} {\tt simplify} is more efficient in {\tt radian} mode (check 8782{\tt radian} in the {\tt cas} configuration 8783 or input {\tt angle\_radian:=1}). 8784 8785 8786\subsection{Simplify trigonometric expressions : {\tt trigsimplify}}\index{trigsimplify} 8787\noindent{\tt trigsimplify} simplifies trigonometric expressions 8788by combining {\tt simplify}, {\tt texpand}, {\tt tlin}, {\tt tcollect}, {\tt trigsin}, {\tt trigcos} and {\tt trigtan} commands in a certain order.\\ 8789Input : 8790\begin{center}{\tt trigsimplify((sin(x+y)-sin(x-y))/(cos(x+y)+cos(x-y)))}\end{center} 8791Output : 8792\begin{center}{\tt tan(y)}\end{center} 8793Input : 8794\begin{center}{\tt trigsimplify(1-1/4*sin(2a)\verb|^|2-sin(b)\verb|^|2-cos(a)\verb|^|4)}\end{center} 8795Output : 8796\begin{center}{\tt sin(a)\verb|^|2-sin(b)\verb|^|2}\end{center} 8797 8798\subsection{Transform arccos into arcsin : {\tt acos2asin}}\index{acos2asin} 8799\noindent{\tt acos2asin} takes as argument an expression containing 8800inverse trigonometric functions.\\ 8801{\tt acos2asin} replaces $\arccos(x)$ by 8802$\displaystyle \frac{\pi}{2}-\arcsin(x)$ in this expression.\\ 8803Input : 8804\begin{center}{\tt acos2asin(acos(x)+asin(x))}\end{center} 8805Output after simplification : 8806\begin{center}{\tt pi/2}\end{center} 8807 8808\subsection{Transform arccos into arctan : {\tt acos2atan}}\index{acos2atan} 8809\noindent{\tt acos2atan} takes as argument an expression containing 8810inverse trigonometric functions.\\ 8811{\tt acos2atan} replaces $\arccos(x)$ by 8812$\displaystyle \frac{\pi}{2}-\arctan(\frac{x}{\sqrt{1-x^2}})$ in this 8813expression.\\ 8814Input : 8815\begin{center}{\tt acos2atan(acos(x))}\end{center} 8816Output : 8817\begin{center}{\tt pi/2-atan(x/sqrt(1-x\verb|^|2))}\end{center} 8818 8819\subsection{Transform arcsin into arccos : {\tt asin2acos}}\index{asin2acos} 8820\noindent{\tt asin2acos} takes as argument an 8821expression containing inverse trigonometric functions.\\ 8822{\tt asin2acos} replaces $\arcsin(x)$ by 8823$\displaystyle \frac{\pi}{2}-\arccos(x)$ in this expression.\\ 8824Input : 8825\begin{center}{\tt asin2acos(acos(x)+asin(x))}\end{center} 8826Output after simplification : 8827\begin{center}{\tt pi/2}\end{center} 8828 8829\subsection{Transform arcsin into arctan : {\tt asin2atan}}\index{asin2atan} 8830\noindent{\tt asin2atan} takes as argument an expression containing 8831inverse trigonometric functions.\\ 8832{\tt asin2atan} replaces $\arcsin(x)$ by 8833$\displaystyle \arctan(\frac{x}{\sqrt{1-x2}})$ in this expression.\\ 8834Input : 8835\begin{center}{\tt asin2atan(asin(x))}\end{center} 8836Output : 8837\begin{center}{\tt atan(x/sqrt(1-x\verb|^|2))}\end{center} 8838 8839\subsection{Transform arctan into arcsin : {\tt atan2asin}}\index{atan2asin} 8840\noindent{\tt atan2asin} takes as argument an expression 8841containing inverse trigonometric functions. 8842{\tt atan2asin} replaces $\arctan(x)$ by 8843$\displaystyle \arcsin(\frac{x}{\sqrt{1+x2}})$ in this expression.\\ 8844Input : 8845\begin{center}{\tt atan2asin(atan(x))}\end{center} 8846Output : 8847\begin{center}{\tt asin(x/sqrt(1+x\verb|^|2))}\end{center} 8848 8849\subsection{Transform arctan into arccos : {\tt atan2acos}}\index{atan2acos} 8850\noindent{\tt atan2acos} takes as argument an expression containing 8851inverse trigonometric functions.\\ 8852{\tt atan2acos} replaces $\arctan(x)$ by 8853$\displaystyle \frac{\pi}{2}-\arccos(\frac{x}{\sqrt{1+x2}})$ 8854in this expression.\\ 8855Input : 8856\begin{center}{\tt atan2acos(atan(x))}\end{center} 8857Output : 8858\begin{center}{\tt pi/2-acos(x/sqrt(1+x\verb|^|2))}\end{center} 8859 8860\subsection{Transform complex exponentials into sin and cos : {\tt sincos exp2trig}}\index{sincos}\index{exp2trig} 8861\noindent{\tt sincos} or {\tt exp2trig} takes as argument an expression 8862containing complex exponentials.\\ 8863{\tt sincos} or {\tt exp2trig} rewrites this expression in terms of 8864$\sin$ and $\cos$.\\ 8865Input : 8866\begin{center}{\tt sincos(exp(i*x))}\end{center} 8867Output : 8868\begin{center}{\tt cos(x)+(i)*sin(x)}\end{center} 8869Input : 8870\begin{center}{\tt exp2trig(exp(-i*x))}\end{center} 8871Output : 8872\begin{center}{\tt cos(x)+(i)*(-(sin(x)))}\end{center} 8873Input : 8874\begin{center}{\tt simplify(sincos(((i)*(exp((i)*x))\verb|^|2-i)/(2*exp((i)*x))))}\end{center} 8875or : 8876\begin{center}{\tt simplify(exp2trig(((i)*(exp((i)*x))\verb|^|2-i)/(2*exp((i)*x))))}\end{center} 8877Output : 8878\begin{center}{\tt -sin(x)}\end{center} 8879 8880\subsection{Transform tan(x) into sin(x)/cos(x) : {\tt tan2sincos}}\index{tan2sincos} 8881\noindent{\tt tan2sincos} takes as argument an expression containing 8882trigonometric functions.\\ 8883{\tt tan2sincos} replaces $\tan(x)$ by 8884 $\displaystyle \frac{\sin(x)}{\cos(x)}$ in this expression.\\ 8885Input : 8886\begin{center}{\tt tan2sincos(tan(2*x))}\end{center} 8887Output : 8888\begin{center}{\tt sin(2*x)/cos(2*x)}\end{center} 8889 8890\subsection{Transform sin(x) into cos(x)*tan(x): 8891\texttt{sin2costan}}\index{sin2costan} 8892 8893The \texttt{sin2costan} command takes as argument a trigonometric 8894expression.\\ 8895\texttt{sin2costan} returns the expression with $\sin(x)$ replaced by 8896$\cos(x)\tan(x)$.\\ 8897Input: 8898\begin{center} 8899 \tt 8900 sin2costan(sin(2*x)) 8901\end{center} 8902Output: 8903\begin{center} 8904 \tt 8905 tan(2*x)*cos(2*x) 8906\end{center} 8907 8908\subsection{Transform cos(x) into sin(x)/tan(x): 8909\texttt{cos2sintan}}\index{cos2sintan} 8910 8911The \texttt{cos2sintan} command takes as argument a trigonometric 8912expression.\\ 8913\texttt{cos2sintan} returns the expression with $\cos(x)$ replaced by 8914$\sin(x)/\tan(x)$.\\ 8915Input: 8916\begin{center} 8917 \tt 8918 cos2sintan(cos(2*x)) 8919\end{center} 8920Output: 8921\begin{center} 8922 \tt 8923 sin(2*x)/tan(2*x) 8924\end{center} 8925 8926\subsection{Rewrite tan(x) with sin(2x) and cos(2x) : {\tt tan2sincos2}}\index{tan2sincos2} 8927\noindent{\tt tan2sincos2} takes as argument an expression containing 8928trigonometric functions.\\ 8929{\tt tan2sincos2} replaces $\tan(x)$ by 8930$\displaystyle \frac{\sin(2x)}{1+\cos(2x)}$ in this expression.\\ 8931Input : 8932\begin{center}{\tt tan2sincos2(tan(x))}\end{center} 8933Output : 8934\begin{center}{\tt sin(2*x)/(1+cos(2*x))}\end{center} 8935 8936\subsection{Rewrite tan(x) with cos(2x) and sin(2x) : {\tt tan2cossin2}}\index{tan2cossin2} 8937\noindent{\tt tan2cossin2} takes as argument an expression 8938containing trigonometric functions.\\ 8939{\tt tan2cossin2} replaces $\tan(x)$ by 8940$\displaystyle \frac{1-\cos(2x)}{\sin(2x)}$ , in this expression.\\ 8941Input : 8942\begin{center}{\tt tan2cossin2(tan(x))}\end{center} 8943Output : 8944\begin{center}{\tt (1-cos(2*x))/sin(2*x)}\end{center} 8945 8946\subsection{Rewrite sin, cos, tan in terms of tan(x/2) : {\tt halftan}}\index{halftan} 8947\noindent{\tt halftan} takes as argument an expression 8948containing trigonometric functions.\\ 8949{\tt halftan} rewrites $\sin(x),\ \cos(x)$ and $ \tan(x)$ 8950 in terms of $\tan(\frac{x}{2})$.\\ 8951Input : 8952\begin{center}{\tt halftan(sin(2*x)/(1+cos(2*x)))}\end{center} 8953Output : 8954\begin{center}{\tt 2*tan(2*x/2)/((tan(2*x/2))\verb|^|2+1)/}\end{center} 8955\begin{center}{\tt (1+(1-(tan(2*x/2))\verb|^|2)/((tan(2*x/2))\verb|^|2+1))}\end{center} 8956Output, after simplification with {\tt normal(ans())} : 8957\begin{center}{\tt tan(x)}\end{center} 8958Input : 8959\begin{center}{\tt halftan(sin(x)\verb|^|2+cos(x)\verb|^|2)}\end{center} 8960Output : 8961\begin{center}{\tt (2*tan(x/2)/((tan(x/2))\verb|^|2+1))\verb|^|2+}\end{center} 8962\begin{center}{\tt ((1-(tan(x/2))\verb|^|2)/((tan(x/2))\verb|^|2+1))\verb|^|2}\end{center} 8963Output, after simplification with {\tt normal(ans())} : 8964\begin{center}{\tt 1}\end{center} 8965 8966\subsection{Rewrite trigonometric functions as function of tan(x/2) 8967and hyperbolic functions as function of exp(x): 8968{\tt halftan\_hyp2exp}}\index{halftan\_hyp2exp} 8969\noindent{\tt halftan\_hyp2exp} takes as argument a trigonometric and 8970hyperbolic expression.\\ 8971{\tt halftan\_hyp2exp} rewrites $\sin(x),\ \cos(x), \tan(x)$ 8972 in terms of $\tan(\frac{x}{2})$ 8973and $\sinh(x),\ \cosh(x), \tanh(x)$ in terms of $\exp(x)$.\\ 8974Input : 8975\begin{center}{\tt halftan\_hyp2exp(tan(x)+tanh(x))}\end{center} 8976Output : 8977\begin{center}{\tt (2*tan(x/2))/((1-(tan(x/2))\verb|^|2))+(((exp(x))\verb|^|2-1))/ (((exp(x))\verb|^|2+1))}\end{center} 8978Input : 8979\begin{center}{\tt halftan\_hyp2exp(sin(x)\verb|^|2+cos(x)\verb|^|2-sinh(x)\verb|^|2+cosh(x)\verb|^|2)}\end{center} 8980Output, after simplification with {\tt normal(ans())} : 8981\begin{center}{\tt 2}\end{center} 8982 8983\subsection{Transform inverse trigonometric functions into logarithms : {\tt atrig2ln}}\index{atrig2ln} 8984\noindent{\tt atrig2ln} takes as argument an expression containing 8985inverse trigonometric functions.\\ 8986{\tt atrig2ln} rewrites these functions with complex logarithms.\\ 8987Input : 8988\begin{center}{\tt atrig2ln(asin(x))}\end{center} 8989Output : 8990\begin{center}{\tt i*log(x+sqrt(x\verb|^|2-1))+pi/2}\end{center} 8991 8992\subsection{Transform trigonometric functions into complex exponentials : {\tt trig2exp}}\index{trig2exp} 8993\noindent{\tt trig2exp} takes as argument an expression containing 8994trigonometric functions.\\ 8995{\tt trig2exp} rewrites the trigonometric functions with complex exponentials 8996({\sc without} linearization).\\ 8997Input : 8998\begin{center}{\tt trig2exp(tan(x))}\end{center} 8999Output : 9000\begin{center}{\tt ((exp((i)*x))\verb|^|2-1)/((i)*((exp((i)*x))\verb|^|2+1))}\end{center} 9001Input : 9002\begin{center}{\tt trig2exp(sin(x))}\end{center} 9003Output : 9004\begin{center}{\tt (exp((i)*x)-1/(exp((i)*x)))/(2*i)}\end{center} 9005 9006\subsection{Simplify and express preferentially with sine : {\tt trigsin}}\index{trigsin} 9007\noindent{\tt trigsin} takes as argument an expression 9008containing trigonometric functions.\\ 9009{\tt trigsin} simplify this expression with the formula :\\ 9010$\sin(x)^2+\cos(x)^2=1$, $\displaystyle \tan(x)=\frac{\sin(x)}{\cos(x)}$ and 9011tries to rewrite the expression only with sine.\\ 9012Input : 9013\begin{center}{\tt trigsin(sin(x)\verb|^|4+cos(x)\verb|^|2+1)}\end{center} 9014Output : 9015\begin{center}{\tt sin(x)\verb|^|4-sin(x)\verb|^|2+2}\end{center} 9016 9017\subsection{Simplify and express preferentially with cosine : {\tt trigcos}}\index{trigcos} 9018\noindent{\tt trigcos} takes as argument an expression 9019containing trigonometric functions.\\ 9020{\tt trigcos} simplifies this expression with the formula :\\ 9021$\sin(x)^2+\cos(x)^2=1$, $\displaystyle \tan(x)=\frac{\sin(x)}{\cos(x)}$ and 9022tries to rewrite the expression only with cosine.\\ 9023Input : 9024\begin{center}{\tt trigcos(sin(x)\verb|^|4+cos(x)\verb|^|2+1)}\end{center} 9025Output : 9026\begin{center}{\tt cos(x)\verb|^|4-cos(x)\verb|^|2+2}\end{center} 9027 9028\subsection{Simplify and express preferentially with tangents : {\tt trigtan}}\index{trigtan} 9029\noindent{\tt trigtan} takes as argument an expression 9030containing trigonometric functions.\\ 9031{\tt trigtan} simplifies this expression with the formula :\\ 9032$\sin(x)^2+\cos(x)^2=1$, $\displaystyle \tan(x)=\frac{\sin(x)}{\cos(x)}$ and 9033tries to rewrite the expression only with tangents.\\ 9034Input : 9035\begin{center}{\tt trigtan(sin(x)\verb|^|4+cos(x)\verb|^|2+1)}\end{center} 9036Output : 9037\begin{center}{\tt((tan(x))\verb|^|2/(1+(tan(x))\verb|^|2))\verb|^|2+1/(1+(tan(x)\verb|^|2)+1}\end{center} 9038Output, after simplification with {\tt normal} : 9039\begin{center}{\tt (2*tan(x)\verb|^|4+3*tan(x)\verb|^|2+2)/(tan(x)\verb|^|4+2*tan(x))\verb|^|2+1)}\end{center} 9040 9041\subsection{Rewrite an expression with different options : 9042{\tt convert convertir =>}} 9043\index{convert|textbf}\index{convertir|textbf}\index{=>}\index{sin@{\sl sin}|textbf} 9044\index{cos@{\sl cos}|textbf}\index{sincos@{\sl sincos}|textbf} 9045\index{exp@{\sl exp}|textbf}\index{tan@{\sl tan}|textbf}\index{ln@{\sl ln}|textbf} 9046\index{expln@{\sl expln}|textbf}\index{string@{\sl string}|textbf} 9047\index{matrix@{\sl matrix}|textbf}\index{polynom@{\sl polynom}} 9048\index{parfrac@{\sl parfrac}|textbf}\index{partfrac@{\sl partfrac}|textbf} 9049\index{fullparfrac@{\sl fullparfrac}|textbf} 9050\label{sec:convert} 9051\noindent{\tt convert} takes two arguments, an expression and an 9052option. \texttt{=>} is the infixed version of \texttt{convert}.\\ 9053{\tt convert} rewrites this expression applying rules depending 9054on the option. Valid options are : 9055\begin{itemize} 9056\item{\tt sin} converts an expression like {\tt trigsin}. 9057\item{\tt cos} converts an expression like {\tt trigcos}. 9058\item{\tt sincos} converts an expression like {\tt sincos}. 9059\item{\tt trig} converts an expression like {\tt sincos}. 9060\item{\tt tan} converts an expression like {\tt halftan}. 9061\item{\tt exp} converts an expression like {\tt trig2exp}. 9062\item{\tt ln} converts an expression like {\tt trig2exp}. 9063\item{\tt expln} converts an expression like {\tt trig2exp}. 9064\item{\tt string} converts an expression into a string. 9065\item{\tt matrix} converts a list of lists into a matrix. 9066\item{\tt polynom} converts a Taylor series into a polynomial 9067by removing the remainder (cf \ref{sec:convertpoly}). 9068\item{\tt parfrac} or {\tt partfrac} or {\tt fullparfrac} converts a rational 9069fraction into its partial fraction decomposition (\ref{sec:convertparf}). 9070\end{itemize} 9071{\tt convert} can also : 9072\begin{itemize} 9073\item convert units, for example 9074{\tt convert(1000\_g,\_kg)=1.0\_kg} (cf \ref{sec:convertunit}). 9075\item write a real as a continued fraction : 9076{\tt convert(a,confrac,'fc')} writes {\tt a} as a continued fraction 9077stored in {\tt fc}. Do not forget to quote the last argument if it 9078was assigned.\\ 9079For example, {\tt convert(1.2,confrac,'fc')=[1,5]} and {\tt fc} contains the 9080continued fraction equal to 1.2 (cf \ref{sec:convertdfc}). 9081\item transform an integer into the list of its digits in a 9082base, beginning with the units digit (and reciprocally) 9083\begin{itemize} 9084\item 9085{\tt convert(n,base,b)} transforms the integer {\tt n} into the list of its 9086digits in base {\tt b} beginning with the units digit.\\ For example, 9087{\tt convert(123,base,10)=[3,2,1]} and reciprocally 9088\item 9089{\tt convert(l,base,b)} transforms the list {\tt l} into the integer {\tt n} 9090which has {\tt l} as list of its 9091digits in base {\tt b} beginning with the units digit.\\ For example, 9092{\tt convert([3,2,1],base,10)=123} (cf \ref{sec:convertbase}). 9093\end{itemize} 9094\end{itemize} 9095 9096\section{Fourier transformation} 9097\subsection{Fourier coefficients : {\tt fourier\_an} and {\tt fourier\_bn} or {\tt fourier\_cn}} \index{integer} 9098Let $f$ be a $T$-periodic continuous functions on 9099$\mathbb{R}$ except maybe at a finite number of points. 9100One can prove that if $f$ is continuous at $x$, then; 9101\begin{eqnarray*} 9102f(x)&=&\frac{a_0}{2}+\sum _{n=1}^{+\infty} a_n \cos(\frac{2\pi 9103 nx}{T})+b_n \sin(\frac{2\pi nx}{T}) \\ 9104 &=&\sum _{n=-\infty}^{+\infty} c_n e^{\frac{2i\pi nx}{T}} 9105\end{eqnarray*} 9106where the coefficients $a_n,\ b_n$, $n\in N$, (or $c_n$, $n \in Z$) are the 9107Fourier coefficients of $f$. 9108The commands{\tt fourier\_an} and {\tt fourier\_bn} or {\tt fourier\_cn} 9109compute these coefficients. 9110 9111\subsubsection{\tt fourier\_an}\index{fourier\_an}\label{sec:fourier_an} 9112\noindent{\tt fourier\_an} takes four or five arguments : an expression $expr$ 9113depending on a variable, the name of this variable (for example $x$), the 9114period $T$, an integer $n$ and a real $a$ (by default $a=0$).\\ 9115{\tt fourier\_an(expr,x,T,n,a)} returns the Fourier coefficient $a_n$ of a 9116function $f$ of 9117variable $x$ defined on $[a,a+T)$ by $f(x)=expr$ and such that 9118$f$ is periodic of period $T$: 9119$$\displaystyle a_n=\frac{2}{T}\int_a^{a+T}f(x)\cos(\frac{2\pi nx }{T})dx$$ 9120To simplify the computations, one should input {\tt assume(n,integer)} 9121before calling {\tt fourier\_an} to specify 9122that $n$ is an integer.\\ 9123{\bf Example} Let the function $f$, of period $T=2$, defined on $[-1,1)$ by 9124$f(x)=x^2$.\\ 9125Input, to have the coefficient $a_0$ : 9126\begin{center}{\tt fourier\_an(x\verb|^|2,x,2,0,-1)}\end{center} 9127Output : 9128\begin{center}{\tt 1/3}\end{center} 9129Input, to have the coefficient $a_n$ ($n\neq 0$) : 9130 \begin{center}{\tt assume(n,integer);fourier\_an(x\verb|^|2,x,2,n,-1)}\end{center} 9131Output : 9132\begin{center}{\tt 4*(-1)\verb|^|n/(pi\verb|^|2*n\verb|^|2)}\end{center} 9133 9134\subsubsection{\tt fourier\_bn}\index{fourier\_bn}\label{sec:fourier_bn} 9135\noindent{\tt fourier\_bn} takes four or five arguments : an expression $expr$ 9136depending on a variable, the name of this variable (for example $x$), the 9137period $T$, an integer $n$ and a real $a$ (by default $a=0$).\\ 9138{\tt fourier\_bn(expr,x,T,n,a)} returns the Fourier coefficient $b_n$ of a 9139function $f$ of variable $x$ defined on $[a,a+T)$ by $f(x)=expr$ and periodic 9140of period $T$: 9141$$\displaystyle b_n=\frac{2}{T}\int_a^{a+T}f(x)\sin(\frac{2\pi nx}{T})dx$$ 9142To simplify the computations, one should input {\tt assume(n,integer)} 9143before calling {\tt fourier\_bn} to specify that $n$ is an integer.\\ 9144{\bf Examples} 9145\begin{itemize} 9146\item Let the function $f$, of period $T=2$, defined on $[-1,1)$ by 9147$f(x)=x^2$.\\ 9148Input, to have the coefficient $b_n$ ($n\neq 0$) : 9149 \begin{center}{\tt assume(n,integer);fourier\_bn(x\verb|^|2,x,2,n,-1)}\end{center} 9150Output : 9151\begin{center}{\tt 0}\end{center} 9152 9153\item Let the function $f$, of period $T=2$, defined on $[-1,1)$ by 9154$f(x)=x^3$.\\ 9155Input, to have the coefficient $b_1$ : 9156 \begin{center}{\tt fourier\_bn(x\verb|^|3,x,2,1,-1)}\end{center} 9157Output : 9158\begin{center}{\tt (2*pi\verb|^|2-12)/pi\verb|^|3}\end{center} 9159\end{itemize} 9160 9161\subsubsection{\tt fourier\_cn}\index{fourier\_cn}\label{sec:fourier_cn} 9162\noindent{\tt fourier\_cn} takes four or five arguments : an expression $expr$ 9163depending of a variable, the name of this variable (for example $x$), the 9164period $T$, an integer $n$ and a real $a$ (by default $a=0$).\\ 9165{\tt fourier\_cn(expr,x,T,n,a)} returns the Fourier coefficient $c_n$ of a 9166function$f$ of variable $x$ defined on $[a,a+T)$ by $f(x)=expr$ and periodic 9167of period $T$: 9168$$\displaystyle c_n=\frac{1}{T}\int_a^{a+T}f(x)e^{\frac{-2i\pi nx}{T}}dx$$ 9169To simplify the computations, one should 9170input {\tt assume(n,integer)} before calling {\tt fourier\_cn} 9171to specify that $n$ is an integer.\\ 9172{\bf Examples} 9173\begin{itemize} 9174\item Find the Fourier coefficients $c_n$ of the periodic function $f$ of 9175period $2$ and defined on $[-1,1)$ by $ f(x)=x^2$.\\ 9176Input, to have $c_0$ : 9177\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2,0,-1)}\end{center} 9178Output: 9179\begin{center}{\tt 1/3}\end{center} 9180Input, to have $c_n$ : 9181\begin{center}{\tt assume(n,integer)}\end{center} 9182\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2,n,-1)}\end{center} 9183Output: 9184\begin{center}{\tt 2*(-1)\verb|^|n/(pi\verb|^|2*n\verb|^|2)}\end{center} 9185 9186\item Find the Fourier coefficients $c_n$ of the periodic function $f$, of 9187period $2$, and defined on $[0,2)$ by $ f(x)=x^2$.\\ 9188Input, to have $c_0$ : 9189\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2,0)}\end{center} 9190Output: 9191\begin{center}{\tt 4/3}\end{center} 9192Input, to have $c_n$ : 9193\begin{center}{\tt assume(n,integer)}\end{center} 9194\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2,n)}\end{center} 9195Output: 9196\begin{center}{\tt ((2*i)*pi*n+2)/(pi\verb|^|2*n\verb|^|2)}\end{center} 9197 9198\item Find the Fourier coefficients $c_n$ of the periodic function $f$ of 9199period $2\pi$ and defined on $[0,2\pi)$ by $ f(x)=x^2$.\\ 9200Input \index{assume} : 9201\begin{center}{\tt assume(n,integer)}\end{center} 9202\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2*pi,n)}\end{center} 9203Output : 9204\begin{center}{\tt ((2*i)*pi*n+2)/n\verb|^|2}\end{center} 9205If you don't specify {\tt assume(n,integer)}, the output will not be 9206simplified : 9207\begin{center}{\tt ((2*i)*pi\verb|^|2*n\verb|^|2*exp((-i)*n*2*pi)+2*pi*n*exp((-i)*n*2*pi)+}\end{center} 9208\begin{center}{\tt (-i)*exp((-i)*n*2*pi)+i)/(pi*n\verb|^|3)}\end{center} 9209You might simplify this expression by replacing 9210{\tt exp((-i)*n*2*pi)} by {\tt 1}, input : 9211\begin{center}{\tt subst(ans(),exp((-i)*n*2*pi)=1)}\end{center} 9212Output : 9213\begin{center}{\tt ((2*i)*pi\verb|^|2*n\verb|^|2+2*pi*n+-i+i)/pi/n\verb|^|3}\end{center} 9214This expression is then simplified with {\tt normal}, the final 9215output is : 9216\begin{center}{\tt ((2*i)*pi*n+2)/n\verb|^|2}\end{center} 9217Hence for $n \neq 0$, $\displaystyle c_n=\frac{2in\pi+2}{n^2}$. 9218As shown in this example, it is better to input {\tt 9219 assume(n,integer)} before calling {\tt fourier\_cn}.\\ 9220We must also compute $c_n$ for $n=0$, input : 9221\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2*pi,0)}\end{center} 9222Output : 9223\begin{center}{\tt 4*pi\verb|^|2/3}\end{center} 9224Hence for $n= 0$, $\displaystyle c_0=\frac{4{\pi}^2}{3}$. 9225\end{itemize} 9226{\bf Remarks} : 9227\begin{itemize} 9228\item Input {\tt purge(n)}\index{purge} to remove the hypothesis done 9229 on $n$. 9230\item 9231Input {\tt about(n)}\index{about} or {\tt assume(n)}\index{assume}, to know 9232the hypothesis done on the variable $n$. 9233\end{itemize} 9234 9235\subsection{Continuous Fourier Transform : {\tt fourier}\index{fourier}, {\tt ifourier}\index{ifourier}} 9236The command {\tt fourier} takes one to three arguments: an expression $ f(x) $ and (optionally) identifiers $ x $ and $ s $. It returns the Fourier transform $ F $ of $ f $ defined by 9237\begin{equation}\label{eq:fourier-def} 9238F(s)=\int_{-\infty}^{+\infty}\mathrm{e}^{-\mathrm{i}\,s\,x}\,f(x)\,\mathrm{d}x,\quad s\in\mathbb{R}. 9239\end{equation} 9240If $ s $ is not given, $ F $ is returned as a function of $ x $. 9241 9242The command {\tt ifourier} works the other way round: it takes arguments $ F(s) $ and (optionally) $ s $ and $ x $, and returns the expression $ f(x) $ using the formula 9243\[ f(x)=\frac{1}{2\,\pi}\,\int_{-\infty}^{+\infty}\mathrm{e}^{\mathrm{i}\,s\,x}\,F(s)\,\mathrm{d}s. \] 9244To compute the above integral, {\tt fourier} is called with input parameters $ \frac{F(s)}{2\,\pi} $, $ s $ and $ x $, replacing $ x $ with $ -x $ in the result. 9245 9246Arbitrary rational functions can be transformed. For example, to find the Fourier transform of $ f(x)=\frac{x}{x^3-19\,x+30} $, input : 9247\begin{center} 9248 \tt F:=fourier(x/(x\^{}3-19x+30),x,s) 9249\end{center} 9250Output : 9251\begin{center} 9252 \tt pi*(5*i*exp(5*i*s)+(-21*i)*exp((-3*i)*s)+16*i*exp((-2*i)*s))*sign(s)/56 9253\end{center} 9254Input : 9255\begin{center} 9256 \tt ifourier(F,s,x) 9257\end{center} 9258Output : 9259\begin{center} 9260 \tt x/(x\^{}3-19*x+30) 9261\end{center} 9262Similarly, to find the transform of $ f(x)=\frac{x^2+1}{x^2-1} $, input : 9263\begin{center} 9264 \tt F:=fourier((x\^{}2+1)/(x\^{}2-1),x,s) 9265\end{center} 9266Output : 9267\begin{center} 9268 \tt 2*pi*(Dirac(s)-sign(s)*sin(s)) 9269\end{center} 9270Input : 9271\begin{center} 9272 \tt ifourier(F,s,x) 9273\end{center} 9274Output : 9275\begin{center} 9276 \tt (x\^{}2+1)/(x\^{}2-1) 9277\end{center} 9278A range of other (generalized) functions and distributions can be transformed, as demonstrated in the following examples. If {\tt fourier} does not know how to transform a function, say $ f $, it returns the unevaluated integral~\eqref{eq:fourier-def}. In these cases one may try to evaluate the result using {\tt eval}.\\ 9279Input : 9280\begin{center} 9281 \tt fourier(3x\^{}2+2x+1,x,s) 9282\end{center} 9283Output : 9284\begin{center} 9285 \tt 2*pi*(Dirac(s)+2*i*Dirac(s,1)-3*Dirac(s,2)) 9286\end{center} 9287Input : 9288\begin{center} 9289 \tt fourier(Dirac(x-1)+Dirac(x+1),x,s) 9290\end{center} 9291Output : 9292\begin{center} 9293 \tt 2*cos(s) 9294\end{center} 9295Input : 9296\begin{center} 9297 \tt fourier(exp(-2*abs(x-1)),x,s) 9298\end{center} 9299Output : 9300\begin{center} 9301 \tt 4*exp(-i*s)/(s\^{}2+4) 9302\end{center} 9303Input : 9304\begin{center} 9305 \tt fourier(atan(1/(2x\^{}2)),x,s) 9306\end{center} 9307Output : 9308\begin{center} 9309 \tt 2*pi*sin(s/2)*exp(-abs(s)/2)/s 9310\end{center} 9311Input : 9312\begin{center} 9313 \tt fourier(BesselJ(3,x),x,s) 9314\end{center} 9315Output : 9316\begin{center} 9317 \tt -s*(4*s\^{}2-3)*(-i*sign(s+1)+i*sign(s-1))/sqrt(-s\^{}2+1) 9318\end{center} 9319Input : 9320\begin{center} 9321 \tt F:=fourier(sin(x)*sign(x),x,s) 9322\end{center} 9323Output : 9324\begin{center} 9325 \tt -2/(s\^{}2-1) 9326\end{center} 9327Input : 9328\begin{center} 9329 \tt ifourier(F,s,x) 9330\end{center} 9331Output : 9332\begin{center} 9333 \tt sign(x)*sin(x) 9334\end{center} 9335Input : 9336\begin{center} 9337 \tt fourier(log(abs(x)),x,s) 9338\end{center} 9339Output : 9340\begin{center} 9341 \tt -pi*(2*euler\_gamma*Dirac(s)*abs(s)+1)/abs(s) 9342\end{center} 9343Input : 9344\begin{center} 9345 \tt fourier(rect(x),x,s) 9346\end{center} 9347Output : 9348\begin{center} 9349 \tt 2*sin(s/2)/s 9350\end{center} 9351Input : 9352\begin{center} 9353 \tt fourier(exp(-abs(x))*sinc(x),x,s) 9354\end{center} 9355Output : 9356\begin{center} 9357 \tt atan(s+1)-atan(s-1) 9358\end{center} 9359Input : 9360\begin{center} 9361 \tt fourier(1/sqrt(abs(x)),x,s) 9362\end{center} 9363Output : 9364\begin{center} 9365 \tt sqrt(2)*sqrt(pi)/sqrt(abs(s)) 9366\end{center} 9367Input : 9368\begin{center} 9369 \tt F:=fourier(1/cosh(2x),x,s) 9370\end{center} 9371Output : 9372\begin{center} 9373 \tt pi/(exp(pi*s/4)+exp(-pi*s/4)) 9374\end{center} 9375Input : 9376\begin{center} 9377 \tt ifourier(F,s,x) 9378\end{center} 9379Output : 9380\begin{center} 9381 \tt 2/(exp(2*x)+exp(-2*x)) 9382\end{center} 9383Input : 9384\begin{center} 9385 \tt fourier(Airy\_Ai(x/2),x,s) 9386\end{center} 9387Output : 9388\begin{center} 9389 \tt 2*exp(8*i*s\^{}3/3) 9390\end{center} 9391Input : 9392\begin{center} 9393 \tt F:=fourier(Gamma(1+i*x/3),x,s) 9394\end{center} 9395Output : 9396\begin{center} 9397 \tt 6*pi*exp(-exp(-3*s)-3*s) 9398\end{center} 9399Input : 9400\begin{center} 9401 \tt ifourier(F,s,x) 9402\end{center} 9403Output : 9404\begin{center} 9405 \tt Gamma(i*x/3+1) 9406\end{center} 9407Input : 9408\begin{center} 9409 \tt F:=fourier(atan(x/4)/x,x,s) 9410\end{center} 9411Output : 9412\begin{center} 9413 \tt pi*ugamma(0,4*abs(s)) 9414\end{center} 9415Input : 9416\begin{center} 9417 \tt ifourier(F,s,x) 9418\end{center} 9419Output : 9420\begin{center} 9421 \tt atan(x/4)/x 9422\end{center} 9423Input : 9424\begin{center} 9425 \tt assume(a>0):; fourier(exp(-a*x\^{}2+b),x,s) 9426\end{center} 9427Output : 9428\begin{center} 9429 \tt sqrt(a)*sqrt(pi)*exp(-s\^{}2/(4*a)+b)/a 9430\end{center} 9431In the following example we compute the convolution of $ f(x)=\mathrm{e}^{-|x|} $ with itself using the convolution theorem. Input : 9432\begin{center} 9433 \tt F:=fourier(exp(-abs(x)),x,s) 9434\end{center} 9435Output : 9436\begin{center} 9437 \tt 2/(s\^{}2+1) 9438\end{center} 9439Input : 9440\begin{center} 9441 \tt ifourier(F\^{}2,s,x) 9442\end{center} 9443Output : 9444\begin{center} 9445 \tt x*Heaviside(x)*exp(-x)-x*Heaviside(-x)*exp(x)+exp(-abs(x)) 9446\end{center} 9447The above result is the desired convolution $ f\ast f(x)=\int_{-\infty}^{+\infty}f(t)\,f(x-t)\,\mathrm{d}t $. 9448 9449Piecewise functions can be transformed if defined as 9450\begin{center} 9451 \tt piecewise(x<a1,f1,x<a2,f2,...,x<an,fn,f0) 9452\end{center} 9453where $ a_1,a_2,\dots,a_n $ are real numbers such that $ a_1<a_2<\cdots<a_n $. Inequalities may be strict or non-strict.\\ 9454Input : 9455\begin{center} 9456 \tt f:=piecewise(x<=-1,exp(x+1),x<=1,1,exp(2-2x)):; F:=fourier(f,x,s) 9457\end{center} 9458Output : 9459\begin{center} 9460 \tt (-i*s*sin(s)+3*s*cos(s)+4*sin(s))/(s*(s-2*i)*(s+i)) 9461\end{center} 9462The original function $ f $ is obtained from the above result by applying {\tt ifourier}. Input : 9463\begin{center} 9464 \tt ifourier(F,s,x) 9465\end{center} 9466Output : 9467\begin{center} 9468 \tt Heaviside(x+1)-Heaviside(x-1)+Heaviside(x-1)*exp(-2*x+2)+ Heaviside(-x-1)*exp(x+1) 9469\end{center} 9470One may verify that the above expression is equal to $ f(x) $ by plotting. 9471 9472To transform unknown functions, one can use the command {\tt addtable}\index{addtable} which takes five arguments: {\tt fourier} or {\tt laplace} (to indicate the desired transform definition), $ f(x) $, $ F(s) $, $ x $ and $ s $, where $ f $, $ F $, $ x $ and $ s $ are identifiers. If the first argument is {\tt fourier}, this means that $ \mathcal{F}\{f(x)\}(s)=F(s) $ i.e.~that the command 9473\begin{center} 9474 \tt fourier(f(x),x,s) 9475\end{center} 9476would return {\tt F(s)}. The return value of {\tt addtable} is 1 on success and 0 on failure. The second and the third argument can also be expressions depending on several variables.\\ 9477Input : 9478\begin{center} 9479 \tt addtable(fourier,y(x),Y(s),x,s) 9480\end{center} 9481Output : 9482\begin{center} 9483 \tt 1 9484\end{center} 9485Input : 9486\begin{center} 9487 \tt fourier(y(a*x+b),x,s) 9488\end{center} 9489Output : 9490\begin{center} 9491 \tt Y(s/a)*exp(i*s*b/a)/abs(a) 9492\end{center} 9493Input : 9494\begin{center} 9495 \tt fourier(Y(x),x,s) 9496\end{center} 9497Output : 9498\begin{center} 9499 \tt 2*pi*y(-s) 9500\end{center} 9501Input : 9502\begin{center} 9503 \tt addtable(fourier,g(x,t),G(s,t),x,s) 9504\end{center} 9505Output : 9506\begin{center} 9507 \tt 1 9508\end{center} 9509Input : 9510\begin{center} 9511 \tt fourier(g(x/2,3*t),x,s) 9512\end{center} 9513Output : 9514\begin{center} 9515 \tt 2*G(2*s,3*t) 9516\end{center} 9517 9518Fourier transform can be used for solving linear differential equations with constant coefficients. For example, we obtain a particular solution to the equation 9519\[ y(x)+4\,y^{(4)}(x)=\delta(x), \] 9520where $ \delta $ is the Dirac delta function. First we transform both sides of the above equation. Input : 9521\begin{center} 9522 \tt L:=fourier(y(x)+4*diff(y(x),x,4),x,s); R:=fourier(Dirac(x),x,s) 9523\end{center} 9524Output : 9525\begin{center} 9526 \tt Y(s)-4*s\^{}4*Y(s), 1 9527\end{center} 9528Then we solve the equation $ L=R $ for $ Y(s) $. Generally, one should apply {\tt csolve} instead of {\tt solve}. Input : 9529\begin{center} 9530 \tt sol:=csolve(L=R,Y(s))[0] 9531\end{center} 9532Output : 9533\begin{center} 9534 \tt 1/(4*s\^{}4+1) 9535\end{center} 9536Finally, we apply {\tt ifourier} to obtain $ y(x) $. Input : 9537\begin{center} 9538 \tt ifourier(sol,s,x) 9539\end{center} 9540Output : 9541\begin{center} 9542 \tt (sin(abs(x)/2)+cos(abs(x)/2))*exp(-abs(x)/2)/4 9543\end{center} 9544The above solution can be combined with solutions of the corresponding homogeneous equation to obtain the general solution. 9545 9546\subsection{Discrete Fourier Transform} 9547Let $N$ be an integer. 9548The Discrete Fourier Transform (DFT) is a transformation $F_N$ defined on 9549the set of periodic sequences of period $N$, it depends on a choice 9550of a primitive $N$-th root of unity $\omega_N$. If the 9551DFT is defined on sequences with complex coefficients, we take: 9552\[ \omega_N=e^{\frac{2 i \pi}{N}}\] 9553If $x$ is a periodic sequence of period 9554$N$, defined by the vector $x=[x_0,x_1,...x_{N-1}]$ then 9555$F_N(x)=y$ is a periodic sequence of period $N$, defined by: 9556\[ {(F_{N,\omega_N}(x))}_k=y_k=\sum_{j=0}^{N-1}x_j\omega_N^{-k\cdot j}, k=0..N-1 \] 9557where $\omega_N$ is a primitive $N$-th root of unity. 9558The discrete Fourier transform may be computed faster than by 9559computing each $y_k$ individually, by the Fast Fourier Transform (FFT). 9560{\tt Xcas} implements the FFT algorithm to compute 9561the discrete Fourier transform only if $N$ is a power of 2. 9562 9563\subsubsection{The properties of the Discrete Fourier Transform} 9564The Discrete Fourier Transform $F_N$ is a bijective transformation 9565on periodic sequences such that 9566\begin{eqnarray*} 9567 F_{N,\omega_N}^{-1}&=&\frac{1}{N} F_{N,\omega_N^{-1}} \\ 9568&=&\frac{1}{N} \overline{F_{N}} \quad \mbox{ on } \mathbb C 9569\end{eqnarray*} 9570i.e. : 9571\[ {(F_N^{-1}(x))}_k=\frac{1}{N}\sum_{j=0}^{N-1}x_j\omega_N^{k\cdot j} \] 9572Inside {\tt Xcas} the discrete Fourier transform and its inverse 9573are denote by {\tt fft} and {\tt ifft}: 9574\begin{center} 9575{\tt fft(x)}=$\displaystyle F_N(x)$, \ {\tt ifft(x)}=$\displaystyle F_N^{-1}(x)$ 9576\end{center} 9577{\bf Definitions}\\ 9578Let $x$ and $y$ be two periodic sequences of period $N$. 9579\begin{itemize} 9580\item The Hadamard product (notation $\cdot$) is defined by: 9581\[ {(x \cdot y)}_k = x_k y_k \] 9582\item the convolution product (notation $*$) is defined by: 9583\[ {(x * y)}_k=\sum_{j=0}^{N-1}x_jy_{k-j} \] 9584\end{itemize} 9585{\bf Properties} : 9586\begin{eqnarray*} 9587N*F_N(x \cdot y)&=&F_N(x) * F_N(y)\\ 9588F_N(x * y)&=&F_N(x) \cdot F_N(y) 9589\end{eqnarray*} 9590 9591\subsubsection{Applications} 9592\begin{enumerate} 9593\item Value of a polynomial\\ 9594Define a polynomial $P(x)=\sum_{j=0}^{N-1}c_jx^j$ by the vector of its 9595coefficients $c:=[c_0,c_1,..c_{N-1}]$, where zeroes may be added so that 9596$N$ is a power of 2. 9597\begin{itemize} 9598\item Compute the values of $P(x)$ at 9599\[ x=a_k=\omega_N^{-k}=\exp(\frac{-2ik\pi}{N}), \quad k=0..N-1 \] 9600This is just the discrete Fourier transform of $c$ since 9601\[ P(a_k)=\sum_{j=0}^{N-1}c_j(\omega_N^{-k})^j=F_N(c)_k \] 9602Input, for example : 9603\begin{center} 9604{\tt P(x):=x+x\verb|^|2; w:=i} 9605\end{center} 9606Here the coefficients of $P$ are [0,1,1,0], 9607$N=4$ and $\omega=\exp(2i\pi/4)=i$.\\ 9608Input :\\ 9609{\tt fft([0,1,1,0])}\\ 9610Output :\\ 9611{\tt [2,-1-i,0,-1+i]}\\ 9612hence 9613\begin{itemize} 9614\item {\tt P(1)=2}, 9615\item {\tt P(-i)=P(w\verb|^|-1)=-1-i}, 9616\item {\tt P(-1)=P(w\verb|^|-2)=0}, 9617\item {\tt P(i)=P(w\verb|^|-3)=-1+i}. 9618\end{itemize} 9619 9620\item Compute the values of $P(x)$ at 9621\[ x=b_k=\omega_N^{k}=\exp(\frac{2ik\pi}{N}), \quad k=0..N-1 \] 9622This is $N$ times the inverse fourier transform of $c$ since 9623\[ P(a_k)=\sum_{j=0}^{N-1}c_j(\omega_N^{k})^j=NF_N^{-1}(c)_k \] 9624Input, for example :\\ 9625{\tt P(x):=x+x\verb|^|2} and {\tt w:=i}\\ 9626Hence, the coefficients of $P$ are [0,1,1,0], 9627$N=4$ and $\omega=\exp(2i\pi/4)=i$.\\ 9628Input :\\ 9629{\tt 4*ifft([0,1,1,0])}\\ 9630Output :\\ 9631{\tt [2,-1+i,0,-1-i]}\\ 9632hence : \begin{itemize} 9633\item {\tt P(1)=2}, 9634\item {\tt P(i)=P(w\verb|^|1)=-1+i}, 9635\item {\tt P(-1)=P(w\verb|^|2)=0}, 9636\item {\tt P(-i)=P(w\verb|^|3)=-1-i}. 9637\end{itemize} 9638We find of course the same values as above... 9639\end{itemize} 9640 9641\item Trigonometric interpolation\\ 9642Let $f$ be periodic function of period $2\pi$, assume that $f(2k\pi/N)=f_k$ 9643for $k=0..(N-1)$. Find a trigonometric polynomial $p$ that interpolates $f$ 9644at $x_k=2k\pi/N$, that is find $p_j, j=0..N-1$ such that 9645\[ p(x)= \sum_{j=0}^{N-1} p_j \exp(ijx), \quad p(x_k)=f_k\] 9646Replacing $x_k$ by its value in $p(x)$ we get: 9647\[ \sum_{j=0}^{N-1} p_j \exp(i\frac{j2k\pi}{N}) = f_k\] 9648In other words, $(f_k)$ is the inverse DFT of $(p_k)$, hence 9649\[ (p_k)= \frac{1}{N} F_N( \ (f_k) \ ) \] 9650If the function $f$ is real, $p_{-k}=\overline p_k$, hence depending 9651whether $N$ is even or odd: 9652\begin{eqnarray*} 9653p(x)&=&p_0+ 96542 \Re(\sum_{k=0}^{\frac{N}{2}-1}p_k\exp(ikx))+\Re(p_{\frac{N}{2}} \exp(i\frac{Nx}{2})) \\ 9655p(x)&=&p_0+ 2 \Re(\sum_{k=0}^{\frac{N-1}{2}}p_k\exp(ikx)) 9656\end{eqnarray*} 9657 9658\item Fourier series\\ 9659Let $f$ be a periodic function of period $2\pi$, such that 9660\[ f(x_k)=y_k, \quad x_k=\frac{2k\pi}{N}, k=0..N-1 \] 9661Suppose that the Fourier series of $f$ converges to $f$ (this will 9662be the case if for example $f$ is continuous). If $N$ is large, 9663a good approximation of $f$ will be given by: 9664\[ \sum_{-\frac{N}{2} \leq n<\frac{N}{2}} c_n \exp(inx) \] 9665Hence we want a numeric approximation of 9666\[ c_n=\frac{1}{2\pi} \int_0^{2\pi}f(t)\exp(-int)dt \] 9667The numeric value of the integral $\int_0^{2\pi}f(t)\exp(-int)dt$ may be 9668computed by the trapezoidal rule 9669(note that the Romberg algorithm would not work here, 9670because the Euler Mac Laurin development 9671has its coefficients equal to zero, since the integrated function is 9672periodic, hence all its derivatives have the same value at $0$ and at $2\pi$). 9673If $\tilde{c_n}$ is the numeric value of $c_n$ obtained by the 9674trapezoidal rule, then 9675\[ 9676\tilde{c_n}=\frac{1}{2\pi}\frac{2\pi}{N}\sum_{k=0}^{N-1}y_k\exp(-2i\frac{nk\pi}{N}), 9677\quad -\frac{N}{2} \leq n<\frac{N}{2} \] 9678Indeed, since $x_k=2k\pi/N$ and $f(x_k)=y_k$: 9679\begin{eqnarray*} 9680f(x_k)\exp(-inx_k)&=&y_k\exp(-2i\frac{nk\pi}{N}), \\ 9681f(0)\exp(0)=f(2\pi)\exp(-2i\frac{nN\pi}{N})&=&y_0=y_N 9682\end{eqnarray*} 9683Hence : 9684\[ 9685[\tilde{c}_0,..\tilde{c}_{\frac{N}{2}-1},\tilde{c}_{\frac{N}{2}+1},..c_{N-1}]= 9686\frac{1}{N}F_N([y_0,y_1...y_{(N-1)}]) \] 9687since 9688\begin{itemize} 9689\item if $n\geq0$, $\tilde{c}_n=y_n$ 9690\item if $n<0$ $\tilde{c}_n=y_{n+N}$ 9691\item $\omega_N=\exp(\frac{2i\pi}{N})$, 9692then $\omega_N^n=\omega_N^{n+N}$ 9693\end{itemize} 9694 9695{\bf Properties} 9696\begin{itemize} 9697\item The coefficients of the trigonometric polynomial that interpolates $f$ 9698at $x=2k\pi/N$ are 9699\[ p_n=\tilde{c}_n, \quad -\frac{N}{2} \leq n<\frac{N}{2} \] 9700\item 9701If $f$ is a trigonometric polynomial $P$ of degree $m\leq \frac{N}{2}$, 9702then 9703\[ f(t)=P(t)=\sum_{k=-m}^{m-1}c_k\exp(2ik\pi t) \] 9704the trigonometric polynomial that interpolate $f=P$ is $P$, the numeric 9705approximation of the coefficients are in fact exact ($\tilde{c}_n=c_n$). 9706\item More generally, we can compute $\tilde{c}_n-c_n$.\\ 9707Suppose that $f$ is equal to its Fourier series, i.e. that :\\ 9708\[ f(t)=\sum_{m=-\infty}^{+\infty}c_m\exp(2i\pi mt), \quad 9709\sum_{m=-\infty}^{+\infty}|c_m|<\infty \] 9710Then : 9711\[ f(x_k)=f(\frac{2k\pi}{N})=y_k=\sum_{m=-\infty}^{+\infty}c_m\omega_N^{km}, 9712\quad 9713\tilde{c_n}=\frac{1}{N}\sum_{k=0}^{N-1}y_k\omega_N^{-kn} \] 9714Replace $y_k$ by its value in $\tilde{c_n}$: 9715\[ 9716\tilde{c_n}=\frac{1}{N}\sum_{k=0}^{N-1}\sum_{m=-\infty}^{+\infty} 9717c_m\omega_N^{km}\omega_N^{-kn} \] 9718If $m\neq n \pmod N$, $\omega_N^{m-n}$ is an $N$-th root of unity different 9719from 1, hence: 9720\[ \omega_N^{(m-n)N}=1, \quad \sum_{k=0}^{N-1}\omega_N^{(m-n)k}=0 \] 9721Therefore, if $m-n$ is a multiple of $N$ ($m=n+l\cdot N$) then 9722$\sum_{k=0}^{N-1}\omega_N^{k(m-n)}=N$, otherwise 9723$\sum_{k=0}^{N-1}\omega_N^{k(m-n)}=0$. 9724By reversing the two sums, we get 9725\begin{eqnarray*} 9726\tilde{c_n}&=&\frac{1}{N}\sum_{m=-\infty}^{+\infty}c_m\sum_{k=0}^{N-1}\omega_N^{k(m-n)} \\ 9727&=&\sum_{l=-\infty}^{+\infty}c_{(n+l\cdot N)} \\ 9728&=&...c_{n-2\cdot N}+c_{n-N}+c_{n}+c_{n+N}+c_{n+2\cdot 9729 N}+..... 9730\end{eqnarray*} 9731Conclusion: if $|n|<N/2$, $\tilde{c_n}-c_n$ is a sum of $c_j$ of large indexes 9732(at least $N/2$ in absolute value), hence is small (depending on the 9733rate of convergence of the Fourier series). 9734\end{itemize} 9735{\bf Example} 9736Input :\\ 9737\begin{center} 9738{\tt f(t):=cos(t)+cos(2*t)}\\ 9739{\tt x:=f(2*k*pi/8)\$(k=0..7)}\\ 9740\end{center} 9741Then : 9742\begin{center} 9743 {\tt x=\{2,sqrt(2)/2,-1,(-sqrt(2)/2,0,(-sqrt(2))/2,-1,sqrt(2)/2\} }\\ 9744{\tt fft(x)=[0.0,4.0,4.0,0.0,0.0,0.0,4.0,4.0]} 9745\end{center} 9746After a division by $N=8$, we get 9747\begin{center} $c_0=0,c_1=4.0/8,c_2=4.0/8,c_3=0.0$,\\ 9748$c_{-4}=0.0,c_{-3}=0.0,c_{-2}=4.0/8,=c_{-1}=4.0/8$ 9749\end{center} 9750Hence $b_k=0$ and $a_k=c_{-k}+c_k$ is equal to 1 if $k=1,2$ and 0 otherwise. 9751 9752\item Convolution Product\\ 9753If $P(x)=\sum_{j=0}^{n-1}a_jx^j$ 9754and $Q(x)=\sum_{j=0}^{m-1}b_jx^j$ 9755are given by the vector of their coefficients 9756$a=[a_0,a_1,..a_{n-1}]$ and $b=[b_0,b_1,..b_{m-1}]$, we may 9757compute the product of these two polynomials using the DFT. 9758The product of polynomials is the convolution product 9759of the periodic sequence of their coefficients 9760if the period is greater or equal to 9761$(n+m)$. Therefore we complete $a$ (resp. $b$) with $m+p$ 9762(resp. $n+p$) zeros, where 9763$p$ is chosen such that $N=n+m+p$ is a power of 2. 9764If $a=[a_0,a_1,..a_{n-1},0..0]$ and $b=[b_0,b_1,..b_{m-1},0..0]$, then: 9765\[ P(x)Q(x)=\sum_{j=0}^{n+m-1}(a*b)_jx^j \] 9766We compute $F_N(a)$, $F_N(b)$, then $ab=F_N^{-1}(F_N(a)\cdot F_N(b))$ 9767using the properties 9768\[ NF_N(x \cdot y)=F_N(x) * F_N(y), \quad 9769F_N(x * y)=F_N(x) \cdot F_N(y) \] 9770\end{enumerate} 9771 9772\subsection{Fast Fourier Transform : {\tt fft}}\index{fft} 9773\noindent{\tt fft} takes as argument a list (or a sequence) 9774${\tt [a_0,..a_{N-1}]}$ where {\tt N} is a power of two.\\ 9775{\tt fft} returns the list ${\tt [b_0,..b_{N-1}]}$ such that, 9776for {\tt k=0..N-1} 9777\[ {\tt 9778 {fft([a_0,..a_{N-1}])}[k]=b_k=\sum_{j=0}^{N-1}x_j\omega_N^{-k\cdot 9779 j}} \] 9780where $\omega_N$ is a primitive $N$-th root of the unity.\\ 9781Input : 9782\begin{center}{\tt fft(0,1,1,0)}\end{center} 9783Output : 9784\begin{center}{\tt [2.0, -1-i, 0.0, -1+i]}\end{center} 9785 9786\subsection{Inverse Fast Fourier Transform : {\tt ifft}}\index{ifft} 9787\noindent{\tt ifft} takes as argument a list ${\tt [b_0,..b_{N-1}]}$ where 9788{\tt N} is a power of two.\\ 9789{\tt ifft} returns the list ${\tt [a_0,..a_{N-1}]}$ such that 9790\[ {\tt fft([a_0,..a_{N-1}])=[b_0,..b_{N-1}]} \] 9791Input : 9792\begin{center}{\tt ifft([2,-1-i,0,-1+i])}\end{center} 9793Output : 9794\begin{center}{\tt [0.0, 1.0, 1.0, 0.0]}\end{center} 9795 9796\subsection{An {\bf exercise} with {\tt fft}} 9797Here are the temperatures $T$, in Celsius degree, at time $t$ : 9798\begin{center} 9799\begin{tabular}{|r|rrrrrrrr|} 9800\hline 9801t & 0 & 3 & 6 & 9 &12 & 15 & 19 & 21\\ 9802\hline 9803T & 11 & 10 & 17 & 24 & 32 & 26 & 23 & 19\\ 9804\hline 9805\end{tabular} 9806\end{center} 9807What was the temperature at 13h45 ? 9808 9809Here $N=8=2*m$. The interpolation polynomial is 9810\[ p(t)=\frac{1}{2} p_{-m}(\exp(-2i\frac{\pi mt}{24})+ 9811\exp(2i\frac{\pi mt}{24}))+ 9812\sum_{k=-m+1}^{m-1}p_k \exp(2i\frac{\pi kt}{24}) \] 9813and 9814\[ p_k=\frac{1}{N} \sum_{k=j}^{N-1}T_k \exp(2i\frac{\pi k}{N}) \] 9815Input :\\ 9816{\tt q:=1/8*fft([11,10,17,24,32,26,23,19])}\\ 9817Output :\\ 9818{\tt q:=[20.25,-4.48115530061+1.72227182413*i,-0.375+0.875*i,\\ 9819-0.768844699385+0.222271824132*i,0.5,\\ 9820-0.768844699385-0.222271824132*i,\\ 9821-0.375-0.875*i,-4.48115530061-1.72227182413*i]}\\ 9822hence: 9823\begin{itemize} 9824\item $p_0=20.25$ 9825\item $p_1=-4.48115530061+1.72227182413*i=\overline{p_{-1}}$, 9826\item $p_2=0.375+0.875*i=\overline{p_{-2}}$, 9827\item $p_3=-0.768844699385+0.222271824132*i=\overline{p_{-3}}$, 9828\item $p_{-4}=0.5$ 9829\end{itemize} 9830Indeed 9831\[ 9832q=[q_0,...q_{N-1}]=[p_0,..p_{\frac{N}{2}-1},p_{-\frac{N}{2}},..,p_{-1}]=\frac{1}{N}F_N([y_0,..y_{N-1}])={\tt 9833 \frac{1}{N}fft(y)} \] 9834Input :\\ 9835{\tt pp:=[q[4],q[5],q[6],q[7],q[0],q[1],q[2],q[3]]}\\ 9836Here, $p_k=pp[k+4]$ for $k=-4...3$. 9837It remains to compute the value of the interpolation polynomial at point 9838$t0=13.75=55/4$.\\ 9839Input: 9840\begin{center} 9841{\tt t0(j):=exp(2*i*pi*(13+3/4)/24*j)}\\ 9842{\tt T0:=1/2*pp[0]*(t0(4)+t0(-4))+sum(pp[j+4]*t0(j),j,-3,3)}\\ 9843{\tt evalf(re(T0))} 9844\end{center} 9845Output : 9846\begin{center} 9847{\tt 29.4863181684} 9848\end{center} 9849The temperature is predicted to be equal to 29.49 Celsius degrees.\\ 9850Input : 9851\begin{center} 9852{\tt q1:=[q[4]/2,q[3],q[2],q[1],q[0]/2]}\\ 9853{\tt a:=t0(1)} (or {\tt a:=-exp(i*pi*7/48)})\\ 9854{\tt g(x):=r2e(q1,x)}\\ 9855{\tt evalf(2*re(g(a)))} 9856\end{center} 9857or : 9858\begin{center} 9859{\tt 2.0*re(q[0]/2+q[1]*t0(1)+q[2]*t0(2)+q[3]*t0(3)+q[4]/2*t0(4))} 9860\end{center} 9861Output : 9862\begin{center} 9863{\tt 29.4863181684} 9864\end{center} 9865 9866{\bf Remark}\\ 9867Using the Lagrange interpolation polynomial (the polynomial is not periodic), 9868input : 9869\begin{center} 9870{\tt l1:=[0,3,6,9,12,15,18,21]}\\ 9871{\tt l2:=[11,10,17,24,32,26,23,19]}\\ 9872{\tt subst(lagrange(l1,l2,13+3/4),x=13+3/4)}\\ 9873\end{center} 9874Output : 9875\begin{center} 9876${\tt \displaystyle \frac{8632428959}{286654464}\simeq 30.1144061688}$ 9877\end{center} 9878 9879\section{Audio Tools} 9880\subsection{Creating audio clips : {\tt createwav}\index{createwav}} 9881{\tt createwav} takes the following arguments (all optional), in no particular order: 9882\begin{itemize} 9883 \item {\tt size=n} resp.~{\tt duration=T}, where $n$ resp.~$T$ is the total number of samples resp.~the length in seconds, 9884 \item {\tt bit\_depth=b}, where $b$ is the number of bits reserved for each sample value and may be 8 or 16 (by default 16), 9885 \item {\tt samplerate=r}, where $r$ is the number of samples per second (by default 44100), 9886 \item {\tt channels=c} where $c$ is the number of channels (by default 1), 9887 \item {\tt D} or {\tt channel\_data=D}, where {\tt D} is a list or a matrix, 9888 \item {\tt normalize=db}, where {\tt db}$\leq 0$ is a real number representing the amplitude peak level in dB~FS (decibel "full scale") units. 9889\end{itemize} 9890Additionally, passing the desired number of samples $n$ as a single argument produces a single-channel clip on 16~bits/44100~Hz containing $n$ samples initialized to zero. 9891 9892Data matrix should contain the $k$-th sample in the $j$-th channel at position $(j,k)$. The value of each sample must be a real number in range $[-1.0,1.0]$. Any value outside this interval is clamped to it (the resulting effect is called \emph{clipping}). If the data is provided as a single list, it is copied across channels. If the number of samples or seconds is provided alongside the data list/matrix, the rows are truncated or padded with zeros to match the desired length. 9893 9894If the option {\tt normalize} is given, audio data is normalized to the specified level prior to conversion. This can be used to avoid clipping. 9895 9896For example, input : 9897\begin{center} 9898 {\tt s:=createwav(duration=3.5):; playsnd(s)} 9899\end{center} 9900Output : 9901\begin{center} 9902 {\tt three and a half seconds of silence at rate 44100} 9903\end{center} 9904Input : 9905\begin{center} 9906 {\tt wave:=sin(2*pi*440*soundsec(2)):;\\s:=createwav(channel\_data=wave,samplerate=48000):;\\playsnd(s)} 9907\end{center} 9908Output : 9909\begin{center} 9910 {\tt two seconds of the 440 Hz sine wave at rate 48000} 9911\end{center} 9912Input : 9913\begin{center} 9914 {\tt t:=soundsec(3):;\\L,R:=sin(2*pi*440*t),sin(2*pi*445*t):;\\s:=createwav([L,R]):; playsnd(s)} 9915\end{center} 9916Output : 9917\begin{center} 9918 {\tt 3 secs of a vibrato effect on a sine wave (stereo)} 9919\end{center} 9920% 9921\subsection{Reading WAV files from disk : {\tt readwav}\index{readwav}} 9922{\tt readwav} takes a string containing the name of a WAV file as its only argument and loads the file. The return value is an audio clip object. 9923 9924For example, assume that the file {\tt example.wav} is stored in the directory {\tt sounds}. Input: 9925\begin{center} 9926 {\tt s:=readwav("/path/to/sounds/example.wav"):; playsnd(s)} 9927\end{center} 9928 9929\subsection{Writing WAV files to disk : {\tt writewav}\index{writewav}} 9930{\tt writewav} takes two arguments, a string containing a file name and an audio clip object, and writes the clip to disk as a WAV file with the specified name. It returns {\tt 1} on success and {\tt 0} on failure. 9931 9932For example, input : 9933\begin{center} 9934 {\tt s:=createwav(sin(2*pi*440*soundsec(1))):;\\writewav("sounds/sine.wav",s)} 9935\end{center} 9936Output : 9937\begin{center} 9938 {\tt 1} 9939\end{center} 9940 9941\subsection{Audio playback : {\tt playsnd}\index{playsnd}} 9942{\tt playsnd} takes an audio clip as its argument and plays it back. 9943 9944For example, input : 9945\begin{center} 9946 {\tt playsnd(createwav(sin(2*pi*440*soundsec(3))))} 9947\end{center} 9948 9949\subsection{Averaging channel data : {\tt stereo2mono}\index{stereo2mono}} 9950{\tt stereo2mono} takes a multichannel audio clip as its argument and returns a clip with input channels mixed down to a single channel. Every sample in the output is the arithmetic mean of the samples at the same position in the input channels. 9951 9952For example, input : 9953\begin{center} 9954 {\tt t:=soundsec(3):;\\L,R:=sin(2*pi*440*t),sin(2*pi*445*t):;\\s:=stereo2mono(createwav([L,R])):; playsnd(s)} 9955\end{center} 9956 9957\subsection{Audio clip properties : {\tt channels}\index{channels}, {\tt bit\_depth}\index{bit\_depth}, {\tt samplerate}\index{samplerate}, {\tt duration}\index{duration}} 9958Each of the above commands takes an audio clip as an argument. {\tt channels} returns the number of channels, {\tt bit\_depth} returns the number of bits reserved for each sample value (8 or 16), {\tt samplerate} returns the number of samples per second and {\tt duration} returns the duration of the clip in seconds. 9959 9960\subsection{Extracting samples from audio clips : {\tt channel\_data}\index{channel\_data}} 9961{\tt channel\_data} takes an audio clip as the first argument and optionally the following arguments (in no particular order) : 9962\begin{itemize} 9963 \item channel number (positive integer) or the option {\tt matrix}, 9964 \item {\tt range=[m,n]} or {\tt range=m..n} or {\tt range=a..b}, where $m$, $n$ are nonnegative integers and $a$, $b$ are floating point values. 9965\end{itemize} 9966By default, the data from all channels is extracted and returned as a sequence of lists. If the option {\tt matrix} is specified, the lists representing channel data are returned as the rows of a matrix. If channel number is specified (or if there is only one channel), the data is returned in a single list. If a range is specified, only the samples from $n$-th to $m$-th (inclusive) are extracted. If a real interval {\tt a..b} is given, it is assumed that the bounds $a$ and $b$ are in seconds and must be given as floating point values. 9967 9968The returned sample values are all within the interval $[-1.0,1.0]$, i.e.~the amplitude of the returned signal is relative. The maximum possible amplitude is represented by the value $1.0$. 9969 9970For example, assume that the directory {\tt sounds} contains a WAV file {\tt example.wav} with 3 seconds of stereo sound. Input : 9971\begin{center} 9972 {\tt s:=readwav("/path/to/sounds/example.wav"):;\\L,R:=channel\_data(s,range=1.2..1.5)} 9973\end{center} 9974The output is a list {\tt L} resp.~{\tt R} containing the data between 1.2 and 1.5 seconds in the left resp.~right channel of the original file. 9975 9976\subsection{Changing the sampling rate : {\tt resample}\index{resample}} 9977{\tt resample} takes an audio clip as its first argument. The target sample rate can be passed as the second argument (by default 44100 Hz), optionally followed by a quality level specification (an integer). The return value is the input audio clip resampled to the desired rate. The quality level can range from 0 (poor) to 4 (best). By default, it is set to 2. 9978 9979Giac does resampling by using \href{http://www.mega-nerd.com/libsamplerate/}{\tt libsamplerate} library written by Erik de Castro Lopo. For more information see the library documentation. 9980 9981For example, assume that the directory {\tt sounds} contains a WAV file {\tt example.wav}. Input : 9982\begin{center} 9983 \tt clip:=readwav("/path/to/sounds/example.wav"):; samplerate(clip) 9984\end{center} 9985Output : 9986\begin{center} 9987 \tt 44100 9988\end{center} 9989Input : 9990\begin{center} 9991 \tt res:=resample(clip,48000):; samplerate(res) 9992\end{center} 9993Output : 9994\begin{center} 9995 \tt 48000 9996\end{center} 9997 9998\subsection{Visualizing waveforms : {\tt plotwav}\index{plotwav}} 9999{\tt plotwav} accepts an audio clip as its first argument and optionally a range in form {\tt range=[m,n]} or {\tt range=a..b} as its second argument, where $m$, $n$ are integers and $a$, $b$ are real numbers. The command displays the waveform on the specified range (by default in its entirety). It is assumed that the values $m$, $n$ are in sample units and $a$, $b$ in seconds. 10000 10001For example, assume that the directory {\tt sounds} contains two files, {\tt example1.wav} (a man speaking, stereo) and {\tt example2.wav} (guitar playing, mono). Input : 10002\begin{center} 10003 \tt clip1:=readwav("/path/to/sounds/example1.wav"):;\\ 10004 plotwav(clip1) 10005\end{center} 10006Output : 10007\begin{center} 10008 \includegraphics[width=0.75\textwidth]{sound_wav3.png} 10009\end{center} 10010Input : 10011\begin{center} 10012 \tt clip2:=readwav("/path/to/sounds/example2.wav"):;\\ 10013 plotwav(clip2) 10014\end{center} 10015Output : 10016\begin{center} 10017 \includegraphics[width=0.75\textwidth]{sound_wav1.png} 10018\end{center} 10019Input : 10020\begin{center} 10021 \tt plotwav(clip2,range=0.5..0.52) 10022\end{center} 10023Output : 10024\begin{center} 10025 \includegraphics[width=0.75\textwidth]{sound_wav2.png} 10026\end{center} 10027 10028\subsection{Visualizing power spectra : {\tt plotspectrum}\index{plotspectrum}} 10029{\tt plotspectrum} takes an audio clip as its first argument and optionally a range in form {\tt range=[lf,uf]} or {\tt range=lf..uf}, where {\tt lf} is the lower bound and {\tt uf} the upper bound of the desired frequency band, as its second argument. The command displays the power spectrum of the audio data on the specified frequency range (by default $[0,s/2]$, where $s$ is the sampling rate). If the audio clip has more than one channel, the channels are mixed down to a single channel before computing the spectrum. 10030 10031For example, assume that a male voice is recorded in the file {\tt example1.wav}. Input : 10032\begin{center} 10033 \tt clip:=readwav("/path/to/sounds/example1.wav"):;\\ 10034 plotspectrum(clip,range=[0,1500]) 10035\end{center} 10036Output : 10037\begin{center} 10038 \includegraphics[width=0.75\textwidth]{sound_spectrum.png} 10039\end{center} 10040One can observe that the dominant frequency is around 220~Hz, which is the middle of tenor range. This is consistent with the fact that a man is speaking in the clip. 10041 10042\section{Signal Processing} 10043\subsection{Boxcar function : {\tt boxcar}\index{boxcar}}\label{sec:boxcar} 10044The {\tt boxcar} command takes three arguments: real numbers $ a $, $ b $ and an identifier or an expression $ x $. It returns $ u(x-a)-u(x-b) $, where $ u $ is the Heaviside function. The resulting expression defines a function which is zero everywhere except within the segment $ [a,b] $, where its value is equal to 1. 10045 10046For example, input : 10047\begin{center} 10048 \tt boxcar(1,2,x) 10049\end{center} 10050Output : 10051\begin{center} 10052 \tt Heaviside(x-1)-Heaviside(x-2) 10053\end{center} 10054Input : 10055\begin{center} 10056 \tt boxcar(1,2,3/2) 10057\end{center} 10058Output : 10059\begin{center} 10060 \tt 1 10061\end{center} 10062Input : 10063\begin{center} 10064 \tt boxcar(1,2,0) 10065\end{center} 10066Output : 10067\begin{center} 10068 \tt 0 10069\end{center} 10070 10071\subsection{Rectangle function : {\tt rect}\index{rect}}\label{sec:rect} 10072The {\tt rect} command takes an identifier or an expression $ x $ and returns the value of the rectangle function at $ x $, which is defined by $ \Pi(x)=u(x+1/2)-u(x-1/2) $ where $ u $ is the Heaviside function. The rectangle function is a special case of boxcar function (see Section~\ref{sec:boxcar}) for $ a=-\frac{1}{2} $ and $ b=\frac{1}{2} $. 10073 10074For example, input : 10075\begin{center} 10076 \tt rect(x/2) 10077\end{center} 10078Output : 10079\begin{center} 10080 \tt Heaviside(x/2+1/2)-Heaviside(x/2-1/2) 10081\end{center} 10082 10083To compute the convolution of the rectangle function with itself, use the convolution theorem. Input : 10084\begin{center} 10085 \tt R:=fourier(rect(x),x,s):; ifourier(R\^{}2,s,x) 10086\end{center} 10087Output : 10088\begin{center} 10089 \tt Heaviside(x+1)-Heaviside(x-1)-2*x*Heaviside(x)+ x*Heaviside(x+1)+x*Heaviside(x-1) 10090\end{center} 10091The above result is the triangle function $ \mathrm{tri}(x) $ (see Section~\ref{sec:tri}). 10092 10093\subsection{Triangle function : {\tt tri}\index{tri}}\label{sec:tri} 10094The {\tt tri} command takes an expression $ x $ as its only argument and returns the value of triangle function at $ x $, defined by 10095\[ \Lambda(x)=\begin{cases}1-|x|,&|x|<1,\\0,&\text{otherwise}.\end{cases} \] 10096The above expression is equal to the convolution of rectangle function with itself (see Section~\ref{sec:rect}). 10097 10098For example, input : 10099\begin{center} 10100 \tt tri(x-1) 10101\end{center} 10102Output : 10103\begin{center} 10104 \tt x*(Heaviside(-x+1)-Heaviside(-x))+ (Heaviside(x-1)-Heaviside(x-2))*(-x+2) 10105\end{center} 10106 10107\subsection{Cardinal sine function : {\tt sinc}\index{sinc}}\label{sec:sinc} 10108The {\tt sinc} command takes an expression $ x $ as its only argument and returns the value of sinc function at $ x $, defined by 10109\[ \mathrm{sinc}(x)=\begin{cases}\frac{\sin(x)}{x},&x\neq 0,\\1,&x=0.\end{cases} \] 10110 10111For example, input : 10112\begin{center} 10113 \tt sinc(pi*x) 10114\end{center} 10115Output : 10116\begin{center} 10117 \tt sin(pi*x)/(pi*x) 10118\end{center} 10119Input : 10120\begin{center} 10121 \tt sinc(0) 10122\end{center} 10123Output : 10124\begin{center} 10125 \tt 1 10126\end{center} 10127 10128\subsection{Cross-correlation of two signals : {\tt cross\_correlation}\index{cross\_correlation}} 10129\label{sec:crosscorr} 10130{\tt cross\_correlation} takes two arguments, a complex vector $ \mathbf{v} $ of length $ n $ and a complex vector $ \mathbf{w} $ of length $ m $. The returned value is the complex vector $ \mathbf{z}=\mathbf{v}\star\mathbf{w} $ of length $ N=n+m-1 $ which is the cross-correlation of the two input vectors, i.e.~such that the following holds : 10131\[ z_k=\sum_{i=k}^{N-1}\overline{v_{i-k}^\ast}\,w_i^\ast,\quad k=0,1,\dots,N-1, \] 10132where 10133\[ \mathbf{v}^\ast=[v_0,v_1,\dots,v_{n-1},\underbrace{0,0,\dots,0}_{m-1}]\quad\text{and}\quad\mathbf{w}^\ast=[\underbrace{0,0,\dots,0}_{n-1},w_0,w_1,\dots,w_{m-1}]. \] 10134 10135Cross-correlation is typically used for measuring similarity between signals. 10136 10137For example, input : 10138\begin{center} 10139 \tt cross\_correlation([1,2],[3,4,5]) 10140\end{center} 10141Output : 10142\begin{center} 10143 {\tt [6.0,11.0,14.0,5.0]} 10144\end{center} 10145Input : 10146\begin{center} 10147 \tt v:=[2,1,3,2]:; w:=[1,-1,1,2,2,1,3,2,1]:; 10148 round(cross\_correlation(v,w)) 10149\end{center} 10150Output : 10151\begin{center} 10152 \tt [2,1,0,8,9,12,15,18,13,11,5,2] 10153\end{center} 10154Observe that the cross-correlation of {\tt v} and {\tt w} is peaking at position 8 with the value 18, indicating that the two signals are best correlated when the last sample in {\tt v} is aligned with the eighth sample in {\tt w}. Indeed, there is an occurrence of {\tt v} in {\tt w} precisely at that point. 10155 10156\subsection{Auto-correlation of a signal : {\tt auto\_correlation}\index{auto\_correlation}} 10157{\tt auto\_correlation} takes as argument a complex vector $ \mathbf{v} $ of length $ n $ and returns its cross-correlation with itself as the vector $ \mathbf{v}\star\mathbf{v} $ of length $ 2\,n-1 $ (see the {\tt cross\_correlation} command, section~\ref{sec:crosscorr}). For example, input : 10158\begin{center} 10159 {\tt auto\_correlation([2,3,4,3,1,4,5,1,3,1])} 10160\end{center} 10161Output : 10162\begin{center} 10163 {\tt [2.0,9.0,15.0,28.0,37.0,44.0,58.0,58.0,68.0,\\ 10164 91.0,68.0,58.0,58.0,44.0,37.0,28.0,15.0,9.0,2.0]} 10165\end{center} 10166 10167\subsection{Convolution of two signals or functions : {\tt convolution}\index{convolution}} 10168{\tt convolution} takes two arguments, a real vector $ \mathbf{v} $ of length $ n $ and a real vector $ \mathbf{w} $ of length $ m $, and returns their convolution $ \mathbf{z}=\mathbf{v}\ast\mathbf{w} $ which is the vector of length $ N=n+m-1 $ defined as : 10169\[ z_k=\sum_{i=0}^{k}v_i\,w_{k-i},\quad k=0,1,\dots,N-1, \] 10170such that $ v_j=0 $ for $ j\geq n $ and $ w_j=0 $ for $ j\geq m $. The two arguments may also be real functions $ f(x) $ and $ g(x) $, with the variable $ x $ as an optional third argument, in which case the integral 10171\[ \int_{-\infty}^{+\infty}f(t)\,g(x-t)\,\mathrm{d}t \] 10172is computed. It is assumed that $ f $ and $ g $ are causal functions, i.e.~$ f(x)=g(x)=0 $ for $ x<0 $. Therefore both $ f $ and $ g $ are multiplied by Heaviside function prior to integration. 10173 10174For example, input : 10175\begin{center} 10176 {\tt convolution([1,2,3],[1,-1,1,-1])} 10177\end{center} 10178Output : 10179\begin{center} 10180 {\tt [1.0,1.0,2.0,-2.0,1.0,-3.0]} 10181\end{center} 10182 10183To compute the convolution of $ f(x)=25\,\mathrm{e}^{2\,x}\,u(x) $ and $ g(x)=x\,\mathrm{e}^{-3\,x}\,u(x) $, where $ u $ is the Heaviside function, input : 10184\begin{center} 10185 \tt convolution(25*exp(2x),x*exp(-3x)) 10186\end{center} 10187Output : 10188\begin{center} 10189 \tt Heaviside(x)*(-5*x*exp(-3*x)+exp(2*x)-exp(-3*x)) 10190\end{center} 10191 10192To compute the convolution of $ f(t)=\ln(1+t)\,u(t) $ and $ g(t)=\frac{1}{\sqrt{t}} $, input : 10193\begin{center} 10194 \tt convolution(ln(1+t),1/sqrt(t),t) 10195\end{center} 10196Output : 10197\begin{center} 10198 \tt 2*Heaviside(t)*((t+1)*ln(abs(sqrt(t)-sqrt(t+1))/(sqrt(t)+sqrt(t+1)))+ 2*sqrt(t\^{}2+t))/sqrt(t+1) 10199\end{center} 10200 10201In the following example convolution is used for reverberation. Assume that the directory {\tt sounds} contains two files, a dry, mono recording of a guitar stored in {\tt guitar.wav} and a two-channel impulse response recorded in a French 18th century salon and stored in {\tt salon-ir.wav}. Files are loaded with the following command lines : 10202\begin{center} 10203 \tt clip:=readwav("/path/to/sounds/guitar.wav"):; ir:=readwav("/path/to/sounds/salon-ir.wav"):; 10204\end{center} 10205Input : 10206\begin{center} 10207 \tt plotwav(clip) 10208\end{center} 10209Output : 10210\begin{center} 10211 \includegraphics[width=0.75\textwidth]{sound_guitar.png} 10212\end{center} 10213Input : 10214\begin{center} 10215 \tt plotwav(ir) 10216\end{center} 10217Output : 10218\begin{center} 10219 \includegraphics[width=0.75\textwidth]{sound_salon.png} 10220\end{center} 10221Convolving the data from {\tt clip} with both channels in {\tt ir} produces a reverberated variant of the recording, in stereo. Input : 10222\begin{center} 10223 \tt data:=channel\_data(clip):; L:=convolution(data,channel\_data(ir,1)):; R:=convolution(data,channel\_data(ir,2)):; 10224\end{center} 10225The convolved signals {\tt L} and {\tt R} now become the left and right channel of a new audio clip, respectively. The {\tt normalize} option is used because convolution usually results in a huge increase of sample values (which is clear from the definition). Input : 10226\begin{center} 10227 \tt spatial:=createwav([L,R],normalize=-3):; playsnd(spatial) 10228\end{center} 10229The result sounds as it was recorded in the same salon as the impulse response. Furthermore, it is a true stereo sound. To visualize it, input : 10230\begin{center} 10231 \tt plotwav(spatial) 10232\end{center} 10233Output : 10234\begin{center} 10235 \includegraphics[width=0.75\textwidth]{sound_reverb.png} 10236\end{center} 10237Note that the resulting audio is longer than the input (for the length of the impulse response). 10238 10239\subsection{Low-pass filtering : {\tt lowpass}\index{lowpass}} 10240{\tt lowpass} takes two or three arguments: an audio clip or a real vector $ \mathbf{v} $ representing the sampled signal, a real number $ c $ specifying the cutoff frequency and optionally a samplerate (which defaults to 44100). The command returns the input data after applying a simple first-order lowpass RC filter. 10241 10242For example, input : 10243\begin{center} 10244 {\tt f:=unapply(periodic(sign(x),x,-1/880,1/880),x);\\ 10245 s:=apply(f,soundsec(3)):;\\ 10246 playsnd(lowpass(createwav(s),1000))} 10247\end{center} 10248 10249\subsection{High-pass filtering : {\tt highpass}\index{highpass}} 10250{\tt highpass} takes two or three arguments: an audio clip or a real vector $ \mathbf{v} $ representing the sampled signal, a real number $ c $ specifying the cutoff frequency and optionally a samplerate (which defaults to 44100). The command returns the input data after applying a simple first-order highpass RC filter. 10251 10252For example, input : 10253\begin{center} 10254 {\tt f:=unapply(periodic(sign(x),x,-1/880,1/880),x);\\ 10255 s:=apply(f,soundsec(3)):;\\ 10256 playsnd(highpass(createwav(s),5000))} 10257\end{center} 10258 10259\subsection{Apply a moving average filter to a signal : {\tt moving\_average\index{moving\_average}}} 10260{\tt moving\_average} takes two arguments: an array $A$ of numeric values representing the sampled signal and a positive integer $n$. It returns an array $B$ obtained by applying a moving average filter of length $n$ to $A$. The elements of $B$ are defined by 10261\[ B[i]=\frac{1}{n}\,\sum_{j=0}^{n-1}A[i+j] \] 10262for $i=0,1,\dots,L-n$, where $L$ is the length of $A$. 10263 10264Moving average filters are fast and useful for smoothing time-encoded signals. For example, input : 10265\begin{center} 10266 {\tt snd:=soundsec(2):;\\ 10267 noise:=randvector(length(snd),normald,0,0.05):;\\ 10268 data:=0.5*threshold(3*sin(2*pi*220*snd),[-1.0,1.0])+noise:;\\ 10269 plotwav(createwav(data),range=[1000,1500])} 10270\end{center} 10271Output : 10272\begin{center} 10273 \includegraphics[width=0.75\textwidth]{signalproc_mavg1.png} 10274\end{center} 10275Input : 10276\begin{center} 10277 {\tt fdata:=moving\_average(data,25):;\\ 10278 plotwav(createwav(fdata),range=[1000,1500])} 10279\end{center} 10280Output : 10281\begin{center} 10282 \includegraphics[width=0.75\textwidth]{signalproc_mavg2.png} 10283\end{center} 10284 10285\subsection{Perform thresholding operations on an array : {\tt threshold}\index{threshold}} 10286{\tt threshold} changes the data in an array which does not meet some kind of minimality criterion. It takes the following parameters : 10287\begin{itemize} 10288 \item vector $ \mathbf{v} $ of real or complex numbers 10289 \item bound specification {\tt bnd} 10290 \item comparison operator (optional) 10291 \item {\tt abs[=true,false]} (optional) 10292\end{itemize} 10293Bound specification may be either a single real number $ b $ (or an equation {\tt b=value}) or a list of two real numbers $ l,u $ (or equations {\tt l=lvalue}, {\tt u=uvalue}). In the latter case a vector $ \mathbf{w} $ is returned, as defined by : 10294\[ w_k=\begin{cases} 10295\text{{\tt uvalue} (defaults to $ u $)},&v_k>u,\\ 10296\text{{\tt lvalue} (defaults to $ l $)},&v_k<l,\\ 10297v_k,&\text{otherwise} 10298\end{cases} \] 10299for $ k=0,1,\dots,n-1 $ where $ n= ${\tt size}($ \mathbf{v} $) when the element $ v_k $ is a real number. If $ v_k $ is complex, then $ |v_k| $ is compared with $ u $ resp.~$ l $ and the value {\tt uvalue} resp.~{\tt lvalue} is multiplied by $ \frac{v_k}{|v_k|} $. 10300 10301In the first case where {\tt bnd} is a number or an equation, the return vector $ \mathbf{w} $ is defined by : 10302\[ w_k=\begin{cases} 10303\text{{\tt value} (defaults to $ b $)},&v_k<b,\\ 10304v_k,&\text{otherwise} 10305\end{cases} \] 10306if $ v_k\in\mathbb{R} $ (if $ v_k $ is complex, then $ |v_k| $ is compared with $ b $ and the {\tt value} is multiplied by $ \frac{v_k}{|v_k|} $), for $ k=0,1,\dots,n-1 $. If comparison operator is specified (one of {\tt >}, {\tt <=} or {\tt >=}, must be quoted), it is used instead of {\tt <} (which is the default) in the above formula. If the fourth argument is specified, the data in $ \mathbf{v} $ must be real and the following formula is used for $ w_k $, $ k=0,1,\dots,n-1 $ : 10307\[ w_k=\begin{cases} 10308\text{{\tt value}},& \text{$ v_k\geq 0 $ and $ |v_k|<b $},\\ 10309-\text{{\tt value}},& \text{$ v_k<0 $ and $ |v_k|<b $},\\ 10310v_k,&\text{otherwise}. 10311\end{cases} \] 10312As before, {\tt value} defaults to $ b $ and the comparison operator used to test $ |v_k| $ against $ b $ (by default {\tt <}) is specified by the third argument. 10313 10314For example, input : 10315\begin{center} 10316 {\tt threshold([2,3,1,2,5,4,3,7],3)} 10317\end{center} 10318Output : 10319\begin{center} 10320 {\tt [3,3,3,3,5,4,3,7]} 10321\end{center} 10322Input : 10323\begin{center} 10324 {\tt threshold([2,3,1,2,5,4,3,7],3=a,'>=')} 10325\end{center} 10326Output : 10327\begin{center} 10328 {\tt [2,a,1,2,a,a,a,a]} 10329\end{center} 10330Input : 10331\begin{center} 10332 {\tt threshold([-2,-3,1,2,5,-4,3,-1],3=0,abs=true)} 10333\end{center} 10334Output : 10335\begin{center} 10336 {\tt [0,-3,0,0,5,-4,3,0]} 10337\end{center} 10338Input : 10339\begin{center} 10340 {\tt threshold([-2,-3,1,2,5,-4,3,-1],3=0,'<=',abs=true)} 10341\end{center} 10342Output : 10343\begin{center} 10344 {\tt [0,0,0,0,5,-4,0,0]} 10345\end{center} 10346Input : 10347\begin{center} 10348 {\tt threshold([-120,-11,-3,0,7,27,111,234],[-100,100])} 10349\end{center} 10350Output : 10351\begin{center} 10352 {\tt [-100,-11,-3,0,7,27,100,100]} 10353\end{center} 10354Input : 10355\begin{center} 10356 {\tt threshold([-120,-11,-3,0,7,27,111,234],[-100=-inf,100=inf])} 10357\end{center} 10358Output : 10359\begin{center} 10360 {\tt [-infinity,-11,-3,0,7,27,+infinity,+infinity]} 10361\end{center} 10362 10363In the following example, a square-like wave is created from a single sine wave by clipping sample values. Input : 10364\begin{center} 10365 \tt data:=threshold(3*sin(2*pi*440*soundsec(2)),[-1.0,1.0]):;\\s:=createwav(data):; playsnd(s) 10366\end{center} 10367Output : 10368\begin{center} 10369 \tt 1 10370\end{center} 10371Input : 10372\begin{center} 10373 \tt plotwav(s,range=[1000,2000]) 10374\end{center} 10375Output : 10376\begin{center} 10377 \includegraphics[width=0.75\textwidth]{sound_wav4.png} 10378\end{center} 10379 10380\subsection{Bartlett-Hann window function : {\tt bartlett\_hann\_window}\index{bartlett\_hann\_window}} 10381{\tt bartlett\_hann\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10382\[ w_k=a_0+a_1\,\left|\frac{k}{N-1}-\frac{1}{2}\right|-a_2\,\cos\left(\frac{2\,k\,\pi}{N-1}\right) \] 10383for $ k=0,1,\dots,N-1 $, where $ a_0=0.62 $, $ a_1=0.48 $ and $ a_2=0.38 $. For example, input : 10384\begin{center} 10385 {\tt L:=bartlett\_hann\_window(randvector(1000,0..1)):;} 10386\end{center} 10387followed by {\tt scatterplot(L)}. 10388 10389\subsection{Blackman-Harris window function : {\tt blackman\_harris\_window}\index{blackman\_harris\_window}} 10390{\tt blackman\_harris\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10391\[ w_k=a_0-a_1\,\cos\left(\frac{2\,k\,\pi}{N-1}\right)+a_2\,\cos\left(\frac{4\,k\,\pi}{N-1}\right)-a_3\,\cos\left(\frac{6\,k\,\pi}{N-1}\right) \] 10392for $ k=0,1,\dots,N-1 $, where $ a_0=0.35875 $, $ a_1=0.48829 $, $ a_2=0.14128 $ and $ a_3=0.01168 $. For example, input : 10393\begin{center} 10394 {\tt L:=blackman\_harris\_window(randvector(1000,0..1)):;} 10395\end{center} 10396followed by {\tt scatterplot(L)}. 10397 10398\subsection{Blackman window function : {\tt blackman\_window}\index{blackman\_window}} 10399{\tt blackman\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally a real number $ \alpha $ (by default $ \alpha=0.16 $) and/or an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10400\[ w_k=\frac{1-\alpha}{2}-\frac{1}{2}\,\cos\left(\frac{2\,k\,\pi}{N-1}\right)+\frac{\alpha}{2}\,\cos\left(\frac{4\,k\,\pi}{N-1}\right) \] 10401for $ k=0,1,\dots,N-1 $. For example, input : 10402\begin{center} 10403 {\tt L:=blackman\_window(randvector(1000,0..1)):;} 10404\end{center} 10405followed by {\tt scatterplot(L)}. 10406 10407\subsection{Bohman window function : {\tt bohman\_window}\index{bohman\_window}} 10408{\tt bohman\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10409\[ w_k=\left(1-x_k\right)\,\cos\left(\pi\,x_k\right)+\frac{1}{\pi}\,\sin\left(\pi\,x_k\right), \] 10410where $ x_k=\left|\frac{2\,k}{N-1}-1\right| $, for $ k=0,1,\dots,N-1 $. For example, input : 10411\begin{center} 10412 {\tt L:=bohman\_window(randvector(1000,0..1)):;} 10413\end{center} 10414followed by {\tt scatterplot(L)}. 10415 10416\subsection{Cosine window function : {\tt cosine\_window}\index{cosine\_window}} 10417{\tt cosine\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally a positive real number $ \alpha $ (by default $ \alpha=1 $) and/or an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10418\[ w_k=\sin^\alpha\left(\frac{k\,\pi}{N-1}\right) \] 10419for $ k=0,1,\dots,N-1 $. For example, input : 10420\begin{center} 10421 {\tt L:=cosine\_window(randvector(1000,0..1),1.5):;} 10422\end{center} 10423followed by {\tt scatterplot(L)}. 10424 10425\subsection{Gaussian window function : {\tt gaussian\_window}\index{gaussian\_window}} 10426{\tt gaussian\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally a positive real number $ \alpha\leq 0.5 $ (by default $ \alpha=0.1 $) and/or an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10427\[ w_k=\exp\left(-\frac{1}{2}\,\left(\frac{k-(N-1)/2}{\alpha\,(N-1)/2}\right)^2\right) \] 10428for $ k=0,1,\dots,N-1 $. For example, input : 10429\begin{center} 10430 {\tt L:=gaussian\_window(randvector(1000,0..1),0.4):;} 10431\end{center} 10432followed by {\tt scatterplot(L)}. 10433 10434\subsection{Hamming window function : {\tt hamming\_window}\index{hamming\_window}} 10435{\tt hamming\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10436\[ w_k=\alpha-\beta\,\cos\left(\frac{2\,k\,\pi}{N-1}\right) \] 10437for $ k=0,1,\dots,N-1 $, where $ \alpha=0.54 $ and $ \beta=1-\alpha=0.46 $. For example, input : 10438\begin{center} 10439 {\tt L:=hamming\_window(randvector(1000,0..1)):;} 10440\end{center} 10441followed by {\tt scatterplot(L)}. 10442 10443\subsection{Hann-Poisson window function : {\tt hann\_poisson\_window}\index{hann\_poisson\_window}} 10444{\tt hann\_poisson\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally a real number $ \alpha $ (by default $ \alpha=1 $) and/or an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10445\[ w_k=\frac{1}{2}\,\left(1-\cos\frac{2\,k\,\pi}{N-1}\right)\,\exp\left(-\frac{\alpha\,|N-1-2\,k|}{N-1}\right) \] 10446for $ k=0,1,\dots,N-1 $. For example, input : 10447\begin{center} 10448 {\tt L:=hann\_poisson\_window(randvector(1000,0..1),2):;} 10449\end{center} 10450followed by {\tt scatterplot(L)}. 10451 10452\subsection{Hann window function : {\tt hann\_window}\index{hann\_window}} 10453{\tt hann\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10454\[ w_k=\sin^2\left(\frac{k\,\pi}{N-1}\right) \] 10455for $ k=0,1,\dots,N-1 $. For example, input : 10456\begin{center} 10457 {\tt L:=hann\_window(randvector(1000,0..1)):;} 10458\end{center} 10459followed by {\tt scatterplot(L)}. 10460 10461\subsection{Parzen window function : {\tt parzen\_window}\index{parzen\_window}} 10462{\tt parzen\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10463\[ w_k=\begin{cases} 10464\left(1-6\,x_k^2\,\left(1-x_k\right)\right),&\left|\frac{N-1}{2}-k\right|\leq\frac{N-1}{4},\\ 104652\,\left(1-x_k\right)^3,&\text{otherwise}, 10466\end{cases} \] 10467where $ x_k=\left|1-\frac{2\,k}{N-1}\right| $, for $ k=0,1,\dots,N-1 $. For example, input : 10468\begin{center} 10469 {\tt L:=parzen\_window(randvector(1000,0..1)):;} 10470\end{center} 10471followed by {\tt scatterplot(L)}. 10472 10473\subsection{Poisson window function : {\tt poisson\_window}\index{poisson\_window}} 10474{\tt poisson\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally a real number $ \alpha $ (by default $ \alpha=1 $) and/or an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10475\[ w_k=\exp\left(-\alpha\,\left|\frac{2\,k}{N-1}-1\right|\right) \] 10476for $ k=0,1,\dots,N-1 $. For example, input : 10477\begin{center} 10478 {\tt L:=poisson\_window(randvector(1000,0..1),2):;} 10479\end{center} 10480followed by {\tt scatterplot(L)}. 10481 10482\subsection{Riemann window function : {\tt riemann\_window}\index{riemann\_window}} 10483{\tt riemann\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10484\[ w_k=\begin{cases} 104851,&k=\frac{N-1}{2},\\ 10486\frac{\sin(\pi\,x_k)}{\pi\,x_k},&\text{otherwise}, 10487\end{cases} \] 10488where $ x_k=\frac{2\,k}{N-1}-1 $, for $ k=0,1,\dots,N-1 $. For example, input : 10489\begin{center} 10490 {\tt L:=riemann\_window(randvector(1000,0..1)):;} 10491\end{center} 10492followed by {\tt scatterplot(L)}. 10493 10494\subsection{Triangular window function : {\tt triangle\_window}\index{triangle\_window}} 10495{\tt triangle\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally an integer $ d\in\{-1,0,1\} $ (by default $ d=0 $) and/or an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10496\[ w_k=1-\left|\frac{n-\frac{N-1}{2}}{\frac{N+d}{2}}\right| \] 10497for $ k=0,1,\dots,N-1 $ (the case $ d=-1 $ is called the Bartlett window function). For example, input : 10498\begin{center} 10499 {\tt L:=triangle\_window(randvector(1000,0..1),1):;} 10500\end{center} 10501followed by {\tt scatterplot(L)}. 10502 10503\subsection{Tukey window function : {\tt tukey\_window}\index{tukey\_window}} 10504{\tt tukey\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally a real number $ \alpha\in[0,1] $ (by default $ \alpha=0.5 $) and/or an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10505\[ w_k=\begin{cases} 10506\frac{1}{2}\,\left(1+\cos\left(\pi\,\left(\frac{k}{\beta}-1\right)\right)\right),&k<\beta,\\ 105071,&\beta\leq k\leq(N-1)\,\left(1-\frac{\alpha}{2}\right),\\ 10508\frac{1}{2}\,\left(1+\cos\left(\pi\,\left(\frac{k}{\beta}-\frac{2}{\alpha}+1\right)\right)\right),&\text{otherwise}, 10509\end{cases} \] 10510where $ \beta=\frac{\alpha\,(N-1)}{2} $, for $ k=0,1,\dots,N-1 $. When $ \alpha=0 $ the rectangular window function (on-off windowing) is obtained, and the case $ \alpha=1 $ corresponds to the Hann window function. For example, input : 10511\begin{center} 10512 {\tt L:=tukey\_window(randvector(1000,0..1),0.4):;} 10513\end{center} 10514followed by {\tt scatterplot(L)}. 10515 10516\subsection{Welch window function : {\tt welch\_window}\index{welch\_window}} 10517{\tt welch\_window} takes as arguments a real vector $ \mathbf{v} $ of length $ n $ and optionally an interval $ n_1 ${\tt ..}$ n_2 $ (with default values $ n_1=0 $ and $ n_2=n-1 $), and returns the elementwise product of the vector $ [v_{n_1},\dots,v_{n_2}] $ and the vector $ \mathbf{w} $ of length $ N=n_2-n_1+1 $ defined by 10518\[ w_k=1-\left(\frac{k-\frac{N-1}{2}}{\frac{N-1}{2}}\right)^2 \] 10519for $ k=0,1,\dots,N-1 $. For example, input : 10520\begin{center} 10521 {\tt L:=welch\_window(randvector(1000,0..1)):;} 10522\end{center} 10523followed by {\tt scatterplot(L)}. 10524 10525\subsection{An example : static noise removal\index{noise removal} by spectral subtraction} 10526In this section we use Xcas to inplement a simple algorithm for static noise removal based on the spectral subtraction method. For a theoretical overview see the paper "Noise Reduction Based on Modified Spectral Subtraction Method" by Ekaterina Verteletskaya and Boris Simak (2011), \emph{International Journal of Computer Science}, 38:1 (\href{https://pdfs.semanticscholar.org/c212/84207dcf8e95b8b44d0ce703f9fe23b28f2a.pdf}{PDF}). 10527 10528Efficiency of the spectral subtraction method is largely dependent on a good noise spectrum estimate. Below is the code for a function {\tt noiseprof} that takes {\tt data} and {\tt wlen} as its arguments. These are, respectively, a signal chunk containing only noise and the window length for signal segmentation (the best values are powers of two, such as 256, 512 or 1024). The function returns an estimate of the noise power spectrum obtained by averaging the power spectra of a (not too large) number of distinct chunks of {\tt data} of length {\tt wlen}. Hamming window function is applied prior to FFT. 10529\begin{verbatim} 10530noiseprof(data,wlen):={ 10531 local N,h,dx,x,v,cnt; 10532 N:=length(data); 10533 h:=wlen/2; 10534 dx:=min(h,max(1,(N-wlen)/100)); 10535 v:=[0$wlen]; 10536 cnt:=0; 10537 for (x:=h;x<N-h;x+=dx) { 10538 v+=abs(fft(hamming_window( 10539 mid(data,floor(x)-h,wlen)))).^2; 10540 cnt++; 10541 }; 10542 return 1.0/cnt*v; 10543}:; 10544\end{verbatim} 10545 10546The main function is {\tt noisered}, which takes three arguments: the input signal {\tt data}, the noise power spectrum {\tt np} and the "spectral floor" parameter {\tt beta} ($\beta$, the minimum power level). The function performs subtraction of the noise spectrum in chunks of length {\tt wlen} (the length of list {\tt np}) using the overlap-and-add approach with Hamming window function. For details see Section 3A of the paper "Speech Enhancement using Spectral Subtraction-type Algorithms: A Comparison and Simulation Study" by Navneet Upadhyay and Abhijit Karmakar (2015), \emph{Procedia Computer Science}, vol.~54, pp.~574--584 (\href{https://core.ac.uk/download/pdf/81218023.pdf}{PDF}). 10547\begin{verbatim} 10548noisered(data,np,beta):={ 10549 local wlen,h,N,L,padded,out,j,k,s,ds,r,alpha; 10550 wlen:=length(np); 10551 N:=length(data); 10552 h:=wlen/2; 10553 L:=0; 10554 repeat L+=wlen; until L>=N; 10555 padded:=concat(data,[0$(L-N)]); 10556 out:=[0$L]; 10557 for (k:=0;k<L-wlen;k+=h) { 10558 s:=fft(hamming_window(mid(padded,k,wlen))); 10559 alpha:=max(1,4-3*sum(abs(s).^2)/(20*sum(np))); 10560 r:=ifft(zip(max,abs(s).^2-alpha*np,beta*np).^(1/2) 10561 .*exp(i*arg(s))); 10562 for (j:=0;j<wlen;j++) { 10563 out[k+j]+=re(r[j]); 10564 }; 10565 }; 10566 return mid(out,0,N); 10567}:; 10568\end{verbatim} 10569 10570To demonstrate the efficiency of the algorithm, we test it on a small speech sample with an audible amount of static noise. Assume that the corresponding WAV file {\tt noised.wav} is stored in the directory {\tt sounds}. Input : 10571\begin{center} 10572 \tt clip:=readwav("/path/to/sounds/noised.wav"):; plotwav(clip) 10573\end{center} 10574Output : 10575\begin{center} 10576 \includegraphics[width=0.75\textwidth]{sound_noise.png} 10577\end{center} 10578Speech starts after approximately 0.2 seconds of pure noise. We use that part of the clip for obtaining an estimate of the noise power spectrum with {\tt wlen} set to 256. Input : 10579\begin{center} 10580 \tt noise:=channel\_data(clip,range=0.0..0.15):; np:=noiseprof(noise,256):; 10581\end{center} 10582Now we call the {\tt noisered} function with $\beta=0.03$ : 10583\begin{center} 10584 \tt c:=noisered(channel\_data(clip),np,0.03):; cleaned:=createwav(c):; plotwav(cleaned) 10585\end{center} 10586Output : 10587\begin{center} 10588 \includegraphics[width=0.75\textwidth]{sound_clean.png} 10589\end{center} 10590It is clearly visible that the noise level is significantly lower than in the original clip. One can also use the {\tt playsnd} command to compare the input with the output by hearing, which reveals that the noise is still present but in a lesser degree (the parameter $\beta$ controls how much noise is "left in"). 10591 10592The algorithm implemented in this section is not particularly fast (removing the noise from a two and a half seconds long recording took 20 seconds of computation time), but serves as a proof of concept and demonstrates the efficiency of noise removal. 10593 10594%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 10595 10596\section{Exponentials and Logarithms} 10597\subsection{Rewrite hyperbolic functions as exponentials : {\tt hyp2exp}}\index{hyp2exp} 10598\noindent{\tt hyp2exp} takes as argument an hyperbolic expression.\\ 10599{\tt hyp2exp} rewrites each hyperbolic functions with exponentials 10600(as a rational fraction of one exponential, 10601i.e. {\sc without} linearization).\\ 10602Input : 10603\begin{center}{\tt hyp2exp(sinh(x))}\end{center} 10604Output : 10605\begin{center}{\tt (exp(x)-1/(exp(x)))/2}\end{center} 10606 10607\subsection{Expand exponentials : {\tt expexpand}}\index{expexpand} 10608\noindent{\tt expexpand} takes as argument an expression with exponentials.\\ 10609{\tt expexpand} expands this expression (rewrites exp of sums as 10610product of exp).\\ 10611Input : 10612\begin{center}{\tt expexpand(exp(3*x)+exp(2*x+2))}\end{center} 10613Output : 10614\begin{center}{\tt exp(x)\verb|^|3+exp(x)\verb|^|2*exp(2)}\end{center} 10615 10616\subsection{Expand logarithms : {\tt lnexpand}}\index{lnexpand} 10617\noindent{\tt lnexpand} takes as argument an expression with logarithms.\\ 10618{\tt lnexpand} expands this expression (rewrites ln of products 10619as sum of ln).\\ 10620Input : 10621\begin{center}{\tt lnexpand(ln(3*x\verb|^|2)+ln(2*x+2))}\end{center} 10622Output : 10623\begin{center}{\tt ln(3)+2*ln(x)+ln(2)+ln(x+1)}\end{center} 10624 10625\subsection{Linearize exponentials : {\tt lin}}\index{lin} 10626\noindent{\tt lin} takes as argument an expression with 10627exponentials.\\ 10628{\tt lin} rewrites hyperbolic functions as exponentials if required, 10629then linearizes this expression (i.e. replace product of 10630exponentials by exponential of sums).\\ 10631{\bf Examples} 10632\begin{itemize} 10633\item Input : 10634\begin{center}{\tt lin(sinh(x)\verb|^|2)}\end{center} 10635Output : 10636\begin{center}{\tt 1/4*exp(2*x)+1/-2+1/4*exp(-(2*x))}\end{center} 10637 10638\item Input : 10639\begin{center}{\tt lin((exp(x)+1)\verb|^|3)}\end{center} 10640Output : 10641\begin{center}{\tt exp(3*x)+3*exp(2*x)+3*exp(x)+1}\end{center} 10642\end{itemize} 10643 10644\subsection{Collect logarithms : {\tt lncollect}}\index{lncollect} 10645\noindent{\tt lncollect} takes as argument an expression with logarithms.\\ 10646{\tt lncollect} collects the logarithms (rewrites sum of ln 10647as ln of products). 10648It may be a good idea to factor the 10649expression with {\tt factor} before collecting by {\tt lncollect}).\\ 10650Input : 10651\begin{center}{\tt lncollect(ln(x+1)+ln(x-1))}\end{center} 10652Output : 10653\begin{center}{\tt log((x+1)*(x-1))}\end{center} 10654Input : 10655\begin{center}{\tt lncollect(exp(ln(x+1)+ln(x-1)))}\end{center} 10656Output : 10657\begin{center}{\tt (x+1)*(x-1)}\end{center} 10658{\bf Warning!!!} For {\tt Xcas}, {\tt log=ln} (use {\tt log10} 10659for 10-base logarithm). 10660 10661\subsection{Expand powers : {\tt powexpand}}\index{powexpand} 10662\noindent{\tt powexpand} rewrites a power of a sum as a product of powers.\\ 10663Input : 10664\begin{center}{\tt powexpand(a\verb|^|(x+y))}\end{center} 10665Output : 10666\begin{center}{\tt a\verb|^|x*a\verb|^|y}\end{center} 10667 10668 10669\subsection{Rewrite a power as an exponential : {\tt pow2exp}}\index{pow2exp} 10670\noindent{\tt pow2exp} rewrites a power as an exponential.\\ 10671Input : 10672\begin{center}{\tt pow2exp(a\verb|^|(x+y))}\end{center} 10673Output : 10674\begin{center}{\tt exp((x+y)*ln(a))}\end{center} 10675 10676\subsection{Rewrite exp(n*ln(x)) as a power : {\tt exp2pow}}\index{exp2pow} 10677\noindent{\tt exp2pow} rewrites expression of the form $\exp(n*\ln(x))$ 10678as a power of $x$.\\ 10679Input : 10680\begin{center}{\tt exp2pow(exp(n*ln(x)))}\end{center} 10681Output : 10682\begin{center}{\tt x\verb|^|n}\end{center} 10683Note the difference with {\tt lncollect} :\\ 10684{\tt lncollect(exp(n*ln(x))) = exp(n*log(x))}\\ 10685{\tt lncollect(exp(2*ln(x))) = exp(2*log(x))}\\ 10686{\tt exp2pow(exp(2*ln(x))) = x\verb|^|2 }\\ 10687But :\\ 10688{\tt lncollect(exp(ln(x)+ln(x))) = x\verb|^|2}\\ 10689{\tt exp2pow(exp(ln(x)+ln(x))) = x\verb|^|(1+1)}\\ 10690 10691\subsection{Simplify complex exponentials : {\tt tsimplify}}\index{tsimplify} 10692\noindent{\tt tsimplify} simplifies transcendental expressions 10693by rewriting the expression with complex exponentials.\\ 10694It is a good idea to try other simplification instructions 10695and call {\tt tsimplify} if they do not work.\\ 10696Input : 10697\begin{center}{\tt tsimplify((sin(7*x)+sin(3*x))/sin(5*x))}\end{center} 10698Output : 10699\begin{center}{\tt ((exp((i)*x))\verb|^|4+1)/(exp((i)*x))\verb|^|2 }\end{center} 10700 10701\section{Polynomials} 10702\label{sec:polynomials} 10703 10704\subsection{Polynomials of a single variable: \texttt{poly1}} 10705\index{poly1} 10706A polynomial of one variable is represented either 10707by a symbolic expression or by the list of its 10708coefficients in decreasing powers order (dense representation). 10709In the latter case, to avoid confusion with other kinds of list 10710\begin{itemize} 10711\item use \verb|poly1[...]| as delimiters in inputs 10712\item check for $\talloblong \ \talloblong$ in {\tt Xcas} output. 10713\end{itemize} 10714Note that polynomials represented as lists of coefficients 10715are always written in decreasing powers order even if 10716{\tt increasing power} is checked in {\tt cas} configuration. 10717 10718\subsection{Polynomials of several variables: 10719\texttt{\%\%\%\{ \%\%\%\}}} 10720\index{\%\%\%\{ \%\%\%\}} 10721 10722A polynomial of several variables is represented 10723\begin{itemize} 10724\item by a symbolic expression 10725\item or by a dense recursive 1-d representation like above 10726\item or by a sum of 10727monomials with non-zero coefficients (distributed sparse 10728representation).\\ 10729A monomial with several variables is represented by a coefficient and a 10730list of integers (interpreted as powers of a variable list). The 10731delimiters for monomials are 10732{\tt \%\%\%\{} and {\tt \%\%\%\}}, for example $3x^2y$ is represented by 10733{\tt \%\%\%\{3,[2,1]\%\%\%\}} with respect to the variable list 10734{\tt [x,y]}). 10735\end{itemize} 10736 10737\subsection{Convert to a symbolic polynomial : {\tt r2e poly2symb}}\index{r2e}\index{poly2symb} 10738\noindent{\tt r2e} or {\tt poly2symb} takes as argument 10739\begin{itemize} 10740\item a list of 10741coefficients of a polynomial (by decreasing order) and a symbolic 10742variable name 10743(by default {\tt x}) 10744\item or a sum of monomials {\tt \%\%\%\{coeff,[n1,....nk] \%\%\%\}} 10745and a vector of symbolic variables {\tt [x1,...,xk]}. 10746\end{itemize} 10747{\tt r2e} or {\tt poly2symb} transforms the argument into a symbolic 10748polynomial.\\ 10749Example with univariate polynomials, input : 10750\begin{center}{\tt r2e([1,0,-1],x)}\end{center} 10751or : 10752\begin{center}{\tt r2e([1,0,-1])}\end{center} 10753or : 10754\begin{center}{\tt poly2symb([1,0,-1],x)}\end{center} 10755Output : 10756\begin{center}{\tt x*x-1}\end{center} 10757Example with sparse multivariate polynomials, input: 10758\begin{center}{\tt poly2symb(\%\%\%\{1,[2]\%\%\%\}+\%\%\%\{-1,[0]\%\%\%\},[x])}\end{center} 10759or : 10760\begin{center}{\tt r2e(\%\%\%\{1,[2]\%\%\%\}+\%\%\%\{-1,[0]\%\%\%\},[x])}\end{center} 10761Output : 10762\begin{center}{\tt x\verb|^2|-1}\end{center} 10763Input : 10764\begin{center}{\tt r2e(\%\%\%\{1,[2,0]\%\%\%\}+\%\%\%\{-1,[1,1]\%\%\%\}+\%\%\%\{2,[0,1]\%\%\%\},[x,y])}\end{center} 10765or : 10766\begin{center}{\tt poly2symb(\%\%\%\{1,[2,0]\%\%\%\}+\%\%\%\{-1,[1,1]\%\%\%\}+\%\%\%\{2,[0,1]\%\%\%\},[x,y])}\end{center} 10767Output : 10768\begin{center}{\tt x\verb|^|2-x*y+2*y}\end{center} 10769 10770\subsection{Convert from a symbolic polynomial : {\tt e2r symb2poly}}\index{e2r}\index{symb2poly} 10771\noindent{\tt e2r} or {\tt symb2poly} takes as argument a symbolic polynomial 10772and either a symbolic variable name (by default {\tt x}) or 10773a list of symbolic variable names.\\ 10774{\tt e2r} or {\tt symb2poly} transforms the polynomial into a list 10775(dense representation of the univariate polynomial, coefficients 10776written by decreasing order) or into a sum of monomials (sparse 10777representation of multivariate polynomials).\\ 10778Input : 10779\begin{center}{\tt e2r(x\verb|^|2-1)}\end{center} 10780or : 10781\begin{center}{\tt symb2poly(x\verb|^|2-1)}\end{center} 10782or : 10783\begin{center}{\tt symb2poly(y\verb|^|2-1,y)}\end{center} 10784or : 10785\begin{center}{\tt e2r(y\verb|^|2-1,y)}\end{center} 10786Output : 10787\begin{center}{\tt $\talloblong$1,0,-1$\talloblong$}\end{center} 10788Input : 10789\begin{center}{\tt e2r(x\verb|^|2-x*y+y, [x,y])}\end{center} 10790or : 10791\begin{center}{\tt symb2poly(x\verb|^|2-x*y+2*y, [x,y])}\end{center} 10792Output : 10793\begin{center}{\tt \%\%\%\{1,[2,0]\%\%\%\}+\%\%\%\{-1,[1,1]\%\%\%\}+\%\%\%\{2,[0,1]\%\%\%\}}\end{center} 10794 10795\subsection{Transform a polynomial in internal format into a list, and 10796conversely: \texttt{convert}}\index{convert} 10797 10798The \texttt{convert} command can take a polynomial in the internal 10799format as a first argument and the \texttt{list} option as the second 10800argument. Here, the \texttt{list} option can be omitted.\\ 10801In this case, \texttt{convert} returns a list representing the 10802polynomial. \\ 10803Input: 10804\begin{center} 10805 \tt 10806 p := symb2poly(x\^{}2 - x*y + 2y, [x,y]) 10807\end{center} 10808Output: 10809\begin{center} 10810 \tt 10811 \%\%\%\{1,[2,0]\%\%\%\}+\%\%\%\{-1,[1,1]\%\%\%\}+\%\%\%\{2,[0,1]\%\%\%\} 10812\end{center} 10813Input: 10814\begin{center} 10815 \tt 10816 l := convert(p,list) 10817\end{center} 10818or: 10819\begin{center} 10820 \tt 10821 l := convert(p) 10822\end{center} 10823Output: 10824\begin{center} 10825 \tt 10826 [[1,[2,0]],[-1,[1,1]],[2,[0,1]]] 10827\end{center} 10828which is a list of the coefficients followed by a list of the variable 10829powers. 10830 10831The \texttt{convert} command can also take a list as the first argument and 10832the \texttt{polynom} option as the second argument.\\ 10833In this case, \texttt{convert} returns the corresponding polynomial in 10834internal format.\\ 10835Input (\texttt{l} from above): 10836\begin{center} 10837 \tt 10838 l 10839\end{center} 10840Output: 10841\begin{center} 10842 \tt 10843 [[1,[2,0]],[-1,[1,1]],[2,[0,1]]] 10844\end{center} 10845Input: 10846\begin{center} 10847 \tt 10848 convert(l,polynom) 10849\end{center} 10850Output: 10851\begin{center} 10852 \tt 10853 \%\%\%\{1,[2,0]\%\%\%\}+\%\%\%\{-1,[1,1]\%\%\%\}+\%\%\%\{2,[0,1]\%\%\%\} 10854\end{center} 10855 10856\subsection{Coefficients of a polynomial: {\tt coeff coeffs}}\index{coeff}\index{coeffs} 10857\noindent{\tt coeff} or {\tt coeffs} takes three arguments : the polynomial, 10858the name of the variable (or the list of the names of variables) and 10859the degree (or the list of the degrees of the variables).\\ 10860{\tt coeff} or {\tt coeffs} returns the coefficient of the polynomial 10861of the degree given as third argument. 10862If no degree was specified, {\tt coeffs} return 10863the list of the coefficients of the polynomial, including 0 in the 10864univariate dense case and excluding 0 in the multivariate sparse case.\\ 10865Input : 10866\begin{center}{\tt coeff(-x\verb|^|4+3*x*y\verb|^|2+x,x,1)}\end{center} 10867Output : 10868\begin{center}{\tt 3*y\verb|^|2+1}\end{center} 10869Input : 10870\begin{center}{\tt coeff(-x\verb|^|4+3x*y\verb|^|2+x,y,2)}\end{center} 10871Output : 10872\begin{center}{\tt 3*x}\end{center} 10873Input : 10874\begin{center}{\tt coeff(-x\verb|^|4+3x*y\verb|^|2+x,[x,y],[1,2])}\end{center} 10875Output : 10876\begin{center}{\tt 3}\end{center} 10877 10878\subsection{Polynomial degree : {\tt degree}}\index{degree} 10879\noindent{\tt degree} takes as argument a polynomial given by its symbolic 10880representation or by the list of its coefficients.\\ 10881{\tt degree} returns the degree of this polynomial (highest 10882degree of its non-zero monomials).\\ 10883Input : 10884\begin{center}{\tt degree(x\verb|^|3+x)}\end{center} 10885Output : 10886\begin{center}{\tt 3}\end{center} 10887Input : 10888\begin{center}{\tt degree([1,0,1,0])}\end{center} 10889Output : 10890\begin{center}{\tt 3}\end{center} 10891 10892\subsection{Polynomial valuation : {\tt valuation ldegree}}\index{valuation}\index{ldegree} 10893\noindent{\tt valuation} or {\tt ldegree} takes as argument a polynomial given 10894by a symbolic expression or by the list of its coefficients.\\ 10895{\tt valuation} or {\tt ldegree} returns the valuation of this 10896polynomial, that is the lowest degree of its non-zero monomials.\\ 10897Input : 10898\begin{center}{\tt valuation(x\verb|^|3+x)}\end{center} 10899Output : 10900\begin{center}{\tt 1}\end{center} 10901Input : 10902\begin{center}{\tt valuation([1,0,1,0])}\end{center} 10903Output : 10904\begin{center}{\tt 1}\end{center} 10905 10906\subsection{Leading coefficient of a polynomial : {\tt lcoeff}}\index{lcoeff} 10907\noindent{\tt lcoeff} takes as argument a polynomial given by a 10908symbolic expression or by the list of its coefficients.\\ 10909{\tt lcoeff} returns the leading coefficient of this polynomial, 10910that is the coefficient of the monomial of highest degree.\\ 10911Input : 10912\begin{center}{\tt lcoeff([2,1,-1,0])}\end{center} 10913Output : 10914\begin{center}{\tt 2}\end{center} 10915Input : 10916\begin{center}{\tt lcoeff(3*x\verb|^|2+5*x,x)}\end{center} 10917Output : 10918\begin{center}{\tt 3}\end{center} 10919Input : 10920\begin{center}{\tt lcoeff(3*x\verb|^|2+5*x*y\verb|^|2,y)}\end{center} 10921Output : 10922\begin{center}{\tt 5*x}\end{center} 10923 10924\subsection{Trailing coefficient degree of a polynomial : {\tt tcoeff}}\index{tcoeff} 10925\noindent{\tt tcoeff} takes as argument a polynomial given by a 10926symbolic expression 10927or by the list of its coefficients.\\ 10928{\tt tcoeff} returns the coefficient of the monomial of lowest degree 10929of this polynomial ({\tt tcoeff}=trailing coefficient).\\ 10930Input : 10931\begin{center}{\tt tcoeff([2,1,-1,0])}\end{center} 10932Output : 10933\begin{center}{\tt -1}\end{center} 10934 Input : 10935\begin{center}{\tt tcoeff(3*x\verb|^|2+5*x,x)}\end{center} 10936Output : 10937\begin{center}{\tt 5}\end{center} 10938 Input : 10939\begin{center}{\tt tcoeff(3*x\verb|^|2+5*x*y\verb|^|2,y)}\end{center} 10940Output : 10941\begin{center}{\tt 3*x\verb|^|2}\end{center} 10942 10943\subsection{Evaluation of a polynomial : {\tt peval polyEval}}\index{peval} 10944\index{polyEval} 10945\noindent{\tt peval} or {\tt polyEval} takes as argument a polynomial 10946{\tt p} given by the list of its coefficients and a real {\tt a} .\\ 10947{\tt peval} or {\tt polyEval} returns the exact or numeric value of 10948{\tt p(a)} using Horner's method.\\ 10949 Input : 10950\begin{center}{\tt peval([1,0,-1],sqrt(2))}\end{center} 10951Output : 10952\begin{center}{\tt sqrt(2)*sqrt(2)-1}\end{center} 10953Then : 10954\begin{center}{\tt normal(sqrt(2)*sqrt(2)-1)}\end{center} 10955Output : 10956\begin{center}{\tt {\tt 1}}\end{center} 10957Input : 10958\begin{center}{\tt peval([1,0,-1],1.4)}\end{center} 10959Output : 10960\begin{center}{\tt 0.96}\end{center} 10961 10962\subsection{Factorize $x^n$ in a polynomial : {\tt factor\_xn}}\index{factor\_xn} 10963\noindent{\tt factor\_xn} takes as argument a polynomial {\tt P}.\\ 10964{\tt factor\_xn} returns the polynomial {\tt P} written 10965as the product of its monomial of largest degree $x^n$ ({\tt n=degree(P)}) 10966with a rational fraction having a non-zero finite limit at infinity.\\ 10967 Input : 10968\begin{center}{\tt factor\_xn(-x\verb|^|4+3)}\end{center} 10969Output : 10970\begin{center}{\tt x\verb|^|4*(-1+3*x\verb|^|-4)}\end{center} 10971 10972\subsection{GCD of the coefficients of a polynomial : {\tt content}}\index{content|textbf} 10973\noindent{\tt content} takes as argument a polynomial {\tt P} given by 10974a symbolic expression or by the list of its coefficients.\\ 10975{\tt content} returns the content of {\tt P}, 10976that is the GCD (greatest common divisor) of the coefficients of 10977{\tt P}.\\ 10978Input : 10979\begin{center}{\tt content(6*x\verb|^|2-3*x+9)}\end{center} 10980or: 10981\begin{center}{\tt content([6,-3,9],x))}\end{center} 10982Output : 10983\begin{center}{\tt 3}\end{center} 10984 10985\subsection{Primitive part of a polynomial : {\tt primpart}}\index{primpart} 10986\noindent{\tt primpart} takes as argument a polynomial {\tt P} given by a 10987symbolic expression or by the list of its coefficients.\\ 10988{\tt primpart} returns the primitive part of {\tt P}, 10989that is {\tt P} divided by the GCD 10990(greatest common divisor) of its coefficients.\\ 10991Input : 10992\begin{center}{\tt primpart(6x\verb|^|2-3x+9)}\end{center} 10993or: 10994\begin{center}{\tt primpart([6,-3,9],x))}\end{center} 10995Output : 10996\begin{center}{\tt 2*x\verb|^|2-x+3}\end{center} 10997 10998\subsection{Factorization : {\tt collect}}\index{collect} 10999\noindent{\tt collect} takes as argument a polynomial or a list of 11000polynomials and optionally an algebraic extension like {\tt sqrt(n)} 11001(for $\sqrt{n}$).\\ 11002{\tt collect} factorizes the polynomial (or the polynomials in the 11003list) on the field of its coefficient (for example $\mathbb Q$) 11004or on the smallest extension containing the optional second argument (e.g. 11005$\mathbb Q[\sqrt{n}]$). In complex mode, the field is complexified.\\ 11006{\bf Examples} : 11007\begin{itemize} 11008\item Factorize $x^2-4$ over the integers, 11009input : 11010\begin{center}{\tt collect(x\verb|^|2-4)}\end{center} 11011Output in real mode : 11012\begin{center}{\tt (x-2)*(x+2)}\end{center} 11013\item Factorize $x^2+4$ over the integers, input : 11014\begin{center}{\tt collect(x\verb|^|2+4)}\end{center} 11015Output in real mode : 11016\begin{center}{\tt x\verb|^|2+4}\end{center} 11017Output in complex mode : 11018\begin{center}{\tt (x+2*i)*(x-2*i)}\end{center} 11019\item Factorize $x^2-2$ over the integers, input : 11020\begin{center}{\tt collect(x\verb|^|2-2)}\end{center} 11021Output in real mode : 11022\begin{center}{\tt x\verb|^|2-2}\end{center} 11023But if you input : 11024\begin{center}{\tt collect(sqrt(2)*(x\verb|^|2-2))}\end{center} 11025Output : 11026\begin{center}{\tt sqrt(2)*(x-sqrt(2))*(x+sqrt(2))}\end{center} 11027\item Factorize over the integers : 11028$$x^3-2x^2+1 \mbox{ and } x^2-x$$ 11029Input : 11030 \begin{center}{\tt collect([x\verb|^|3-2*x\verb|^|2+1,x\verb|^|2-x])}\end{center} 11031Output : 11032\begin{center}{\tt [(x-1)*(x\verb|^|2-x-1),x*(x-1)]}\end{center} 11033But, input : 11034 \begin{center}{\tt collect((x\verb|^|3-2*x\verb|^|2+1)*sqrt(5))}\end{center} 11035Output : 11036\begin{center}{\tt ((19*sqrt(5)-10)*((sqrt(5)+15)*x+7*sqrt(5)-5)* ((sqrt(5)+25)*x-13*sqrt(5)-15)*(x-1))/6820}\end{center} 11037Or, input : 11038\begin{center}{\tt collect(x\verb|^|3-2*x\verb|^|2+1,sqrt(5))}\end{center} 11039Output : 11040\begin{center}{\tt ((2*sqrt(5)-19)*((sqrt(5)+25)*x-}\\ 11041 {\tt 13*sqrt(5)-15)*(-x+1)*((sqrt(5)+15)*x+7*sqrt(5)-5))/6820} 11042\end{center} 11043\end{itemize} 11044 11045\subsection{Factorization : {\tt factor factoriser}}\index{factor}\index{factoriser}\label{sec:factor} 11046\noindent {\tt factor} takes as argument a polynomial or a list of 11047polynomials and optionally an algebraic extension, e.g. {\tt sqrt(n)}.\\ 11048{\tt factor} factorizes the polynomial (or the polynomials in the list) on the 11049field of its coefficients (the field is complexified in complex mode) 11050or on the smallest extension containing the optional second argument. 11051Unlike {\tt collect}, 11052{\tt factor} will further factorize each factor of degree 2 11053if {\tt Sqrt} is checked in the {\tt cas} configuration 11054(see also \ref{sec:factore}). 11055You can check the current configuration in the status button under 11056{\tt Xcas} and change the configuration by hitting this status button.\\ 11057Input : 11058 \begin{center}{\tt factor(x\verb|^|2+2*x+1)}\end{center} 11059Output : 11060\begin{center}{\tt (x+1)\verb|^|2}\end{center} 11061Input : 11062\begin{center}{\tt factor(x\verb|^|4-2*x\verb|^|2+1)}\end{center} 11063Output : 11064\begin{center}{\tt (-x+1)\verb|^|2*(x+1)\verb|^|2}\end{center} 11065Input : 11066 \begin{center}{\tt factor(x\verb|^|3-2*x\verb|^|2+1)}\end{center} 11067Output if {\tt Sqrt} is not checked in the {\tt cas} configuration : 11068\begin{center}{\tt (x-1)*(x\verb|^|2-x-1)}\end{center} 11069Output if {\tt Sqrt} is checked in the {\tt cas} configuration : 11070\begin{center}{\tt (x-1)*(x+(sqrt(5)-1)/2)*(x+(-sqrt(5)-1)/2)}\end{center} 11071Input : 11072 \begin{center}{\tt factor(x\verb|^|3-2*x\verb|^|2+1,sqrt(5))}\end{center} 11073Output : 11074\begin{center}{\tt ((2*sqrt(5)-19)*((sqrt(5)+15)*x+}\\ 11075{\tt 7*sqrt(5)-5)*(-x+1)*((sqrt(5)+25)*x-13*sqrt(5)-15))/6820} 11076\end{center} 11077Input : 11078 \begin{center}{\tt factor(x\verb|^|2+1)}\end{center} 11079Output in real mode : 11080\begin{center}{\tt x\verb|^|2+1}\end{center} 11081Output in complex mode : 11082\begin{center}{\tt ((-i)*x+1)*((i)*x+1)}\end{center} 11083 11084\subsection{Square-free factorization : {\tt sqrfree}}\index{sqrfree} 11085\noindent{\tt sqrfree} takes as argument a polynomial.\\ 11086{\tt sqrfree} factorizes this polynomial as a product of 11087powers of coprime factors, where each factor has roots of multiplicity 1 11088(in other words, a factor and its derivative are coprime).\\ 11089Input : 11090\begin{center}{\tt sqrfree((x\verb|^|2-1)*(x-1)*(x+2))}\end{center} 11091Output : 11092 \begin{center}{\tt (x\verb|^|2+3*x+2)*(x-1)\verb|^|2}\end{center} 11093Input : 11094\begin{center}{\tt sqrfree((x\verb|^|2-1)\verb|^|2*(x-1)*(x+2)\verb|^|2)}\end{center} 11095Output : 11096 \begin{center}{\tt (x\verb|^|2+3*x+2)*(x-1)\verb|^|3}\end{center} 11097 11098\subsection{List of factors : {\tt factors}}\index{factors|textbf} 11099\noindent{\tt factors} has either a polynomial or a list of polynomials as 11100argument.\\ 11101{\tt factors} returns a list containing the factors of the polynomial 11102and their exponents.\\ 11103Input : 11104 \begin{center}{\tt factors(x\verb|^|2+2*x+1)}\end{center} 11105Output : 11106\begin{center}{\tt [x+1,2]}\end{center} 11107Input : 11108 \begin{center}{\tt factors(x\verb|^|4-2*x\verb|^|2+1)}\end{center} 11109Output : 11110\begin{center}{\tt [x+1,2,x-1,2]}\end{center} 11111Input : 11112 \begin{center}{\tt factors([x\verb|^|3-2*x\verb|^|2+1,x\verb|^|2-x])}\end{center} 11113Output : 11114\begin{center}{\tt [[x-1,1,x\verb|^|2-x-1,1],[x,1,x-1,1]]}\end{center} 11115Input : 11116 \begin{center}{\tt factors([x\verb|^|2,x\verb|^|2-1])}\end{center} 11117Output : 11118\begin{center}{\tt [[x,2],[x+1,1,x-1,1]]}\end{center} 11119 11120\subsection{Evaluate a polynomial : {\tt horner}}\index{horner} 11121\noindent{\tt horner} takes two arguments : a polynomial {\tt P} given by its 11122symbolic expression or by the list of its coefficients and a number {\tt a}.\\ 11123{\tt horner} returns {\tt P(a)} computed using Horner's method.\\ 11124Input : 11125\begin{center}{\tt horner(x\verb|^|2-2*x+1,2)}\end{center} 11126or : 11127\begin{center}{\tt horner([1,-2,1],2)}\end{center} 11128Output : 11129\begin{center}{\tt 1}\end{center} 11130 11131\subsection{Rewrite in terms of the powers of (x-a) : {\tt ptayl}}\index{ptayl} 11132{\tt ptayl} is used to rewrite a polynomial {\tt P} depending of {\tt x} 11133in terms of the powers of {\tt (x-a)} 11134({\tt ptayl} means polynomial Taylor)\\ 11135{\tt ptayl} takes two arguments: a polynomial {\tt P} given by a 11136symbolic expression or by the list of its coefficients and 11137a number {\tt a}.\\ 11138{\tt ptayl} returns the polynomial {\tt Q} such that {\tt Q(x-a)=P(x)}\\ 11139Input : 11140\begin{center}{\tt ptayl(x\verb|^|2+2*x+1,2)}\end{center} 11141Output, the polynomial Q: 11142\begin{center}{\tt x\verb|^|2+6*x+9}\end{center} 11143Input : 11144\begin{center}{\tt ptayl([1,2,1],2)}\end{center} 11145Output : 11146\begin{center}{\tt [1,6,9]}\end{center} 11147{\bf Remark} 11148\begin{center}{\tt P(x)=Q(x-a)}\end{center} 11149i.e. for the example :\\ 11150$x^2+2x+1=(x-2)^2+6(x-2)+9$ 11151 11152\subsection{Compute with the exact root of a polynomial : {\tt rootof}}\index{rootof} 11153Let $P$ and $Q$ be two polynomials given by the list of their coefficients 11154then {\tt rootof(P,Q)} gives the value $P(\alpha)$ where $\alpha$ is the 11155root of $Q$ with largest real part (and largest imaginary part in 11156case of equality).\\ 11157In exact computations, {\tt Xcas} will rewrite rational evaluations 11158of {\tt rootof} as a unique {\tt rootof} with degree$(P)<$degree$(Q)$. 11159If the resulting rootof is the solution of a second degree equation, 11160it will be simplified. 11161 11162{\bf Example}\\ 11163Let $\alpha$ be the root with largest imaginary 11164part of $Q(x)=x^4+10x^2+1$ (all roots of $Q$ have real part equal to 0). 11165\begin{itemize} 11166\item Compute $\displaystyle \frac{1}{\alpha}$. Input : 11167\begin{center}{\tt normal(1/rootof([1,0],[1,0,10,0,1])) }\end{center} 11168$P(x)=x$ is represented by [1,0] and $\alpha$ 11169by {\tt rootof([1,0],[1,0,10,0,1])}.\\ 11170Output : 11171\begin{center}{\tt rootof([[-1,0,-10,0],[1,0,10,0,1]])}\end{center} 11172i.e. : 11173\[ \frac{1}{\alpha}=-\alpha^3-10\alpha \] 11174\item Compute $\alpha2$. Input : 11175\begin{center}{\tt 11176 normal(rootof([1,0],[1,0,10,0,1])\verb|^|2)}\end{center} 11177or (since $P(x)=x^2$ is represented by [1,0,0]) input 11178\begin{center}{\tt normal(rootof([1,0,0],[1,0,10,0,1]))}\end{center} 11179Output : 11180\begin{center}{\tt -5-2*sqrt(6)}\end{center} 11181\end{itemize} 11182 11183\subsection{Exact roots of a polynomial : {\tt roots}}\index{roots} 11184\noindent{\tt roots} takes as arguments a symbolic 11185polynomial expression and the name of its variable.\\ 11186{\tt roots} returns a 2 columns matrix : each row is 11187the list of a root of the polynomial and its multiplicity.\\ 11188{\bf Examples} 11189\begin{itemize} 11190\item Find the roots of $P(x)=x^5-2x^4+x^3$.\\ 11191Input : 11192\begin{center}{\tt roots(x\verb|^|5-2*x\verb|^|4+x\verb|^|3) }\end{center} 11193Output : 11194\begin{center}{\tt [[8+3*sqrt(7),1],[8-3*sqrt(7),1],[0,3]]}\end{center} 11195\item Find the roots of 11196$x^{10}-15x^8+90x^6-270x^4+405x^2-243=(x^2-3)^5$.\\ 11197 Input : 11198\begin{center}{\tt roots(x\verb|^|10-15*x\verb|^|8+90*x\verb|^|6-270*x\verb|^|4+405*x\verb|^|2-243)}\end{center} 11199Output : 11200\begin{center}{\tt[[sqrt(3),5],[-(sqrt(3)),5]]}\end{center} 11201\item Find the roots of $t^3-1$.\\ 11202Input : 11203\begin{center}{\tt roots(t\verb|^|3-1,t)}\end{center} 11204Output : 11205\begin{center}{\tt[[(-1+(i)*sqrt(3))/2,1],[(-1-(i)*sqrt(3))/2,1],[1,1]]}\end{center} 11206\end{itemize} 11207 11208\subsection{Coefficients of a polynomial defined by its roots : {\tt pcoeff pcoef}}\index{pcoeff}\index{pcoef} 11209\noindent{\tt pcoeff} (or {\tt pcoef}) takes as argument a list of 11210the roots of a polynomial $P$.\\ 11211{\tt pcoeff} (or {\tt pcoef}) returns a univariate polynomial having 11212these roots, 11213represented as the list of its coefficients by decreasing order.\\ 11214Input : 11215\begin{center}{\tt pcoef([1,2,0,0,3])}\end{center} 11216Output : 11217\begin{center}{\tt [1,-6,11,-6,0,0]}\end{center} 11218i.e. $(x-1)(x-2)(x^2)(x-3)=x^5-6x^4+11x^3-6x^2$. 11219 11220\subsection{Truncate of order $n$ : {\tt truncate}}\index{truncate} 11221\noindent{\tt truncate} takes as argument, a polynomial and an integer 11222{\tt n}.\\ 11223{\tt truncate} truncates this polynomial at order {\tt n} (removing 11224all terms of order greater or equal to {\tt n+1}).\\ 11225{\tt truncate} may be used to transform a series expansion into a 11226polynomial or to compute a series expansion step by step.\\ 11227Input : 11228\begin{center}{\tt truncate((1+x+x\verb|^|2/2)\verb|^|3,4)}\end{center} 11229Output : 11230\begin{center}{\tt (9*x\verb|^|4+16*x\verb|^|3+18*x\verb|^|2+12*x+4)/4}\end{center} 11231Input : 11232\begin{center}{\tt truncate(series(sin(x)),4)}\end{center} 11233Output : 11234\begin{center}{\tt (-x\verb|^|3-(-6)*x)/6}\end{center} 11235Note that the returned polynomial is normalized. 11236 11237\subsection{Convert a series expansion into a polynomial : {\tt convert convertir}}\index{convert}\index{convertir}\index{polynom@{\sl polynom}|textbf}\label{sec:convertpoly} 11238\noindent{\tt convert}, with the option {\tt polynom}, converts a Taylor series 11239into a polynomial. It should be used for operations like drawing 11240the graph of the Taylor series of a function near a point.\\ 11241{\tt convert} takes two arguments : an expression 11242and the option {\tt polynom}.\\ 11243{\tt convert} replaces the {\tt order\_size} functions by 0 inside the 11244expression.\\ 11245Input : 11246\begin{center}{\tt convert(taylor(sin(x)),polynom)}\end{center} 11247Output : 11248\begin{center}{\tt x+1/-6*x\verb|^|3+1/120*\verb|x^|5+x\verb|^|6*0}\end{center} 11249Input : 11250\begin{center}{\tt convert(series(sin(x),x=0,6),polynom)}\end{center} 11251Output : 11252\begin{center}{\tt x+1/-6*x\verb|^|3+1/120*\verb|x^|5+x\verb|^|7*0}\end{center} 11253 11254\subsection{Random polynomial : {\tt randpoly randPoly}}\index{randpoly}\index{randPoly} 11255\noindent{\tt randpoly} (or {\tt randPoly}) takes two arguments: the name of a 11256variable (by default {\tt x}) and an integer {\tt n} (the order of the 11257arguments is not important).\\ 11258{\tt randpoly} returns a polynomial with respect to the variable 11259given argument (or {\tt x} if none was provided), 11260of degree the second argument, having as coefficients 11261random integers evenly distributed on -99..+99.\\ 11262Input : 11263\begin{center}{\tt randpoly(t,4)}\end{center} 11264Output for example: 11265\begin{center}{\tt -8*t\verb|^|4-87*t\verb|^|3-52*t\verb|^|2+94*t+80}\end{center} 11266Input : 11267\begin{center}{\tt randpoly(4)}\end{center} 11268Output for example: 11269\begin{center}{\tt 70*x\verb|^|4-46*x\verb|^|3-7*x\verb|^|2-24*x+52}\end{center} 11270Input : 11271\begin{center}{\tt randpoly(4,u)}\end{center} 11272Output for example: 11273\begin{center}{\tt 2*u\verb|^|4+33*u\verb|^|3-6*u\verb|^|2-92*u-12}\end{center} 11274 11275\subsection{Change the order of variables : {\tt reorder}}\index{reorder} 11276\noindent{\tt reorder} takes two arguments : an expression and a vector 11277of variable names.\\ 11278{\tt reorder} expands the expression according to the order of variables 11279given as second argument.\\ 11280Input : 11281\begin{center}{\tt reorder(x\verb|^|2+2*x*a+a\verb|^|2+z\verb|^|2-x*z,[a,x,z])}\end{center} 11282Output : 11283\begin{center}{\tt a\verb|^|2+2*a*x+x\verb|^|2-x*z+z\verb|^|2}\end{center} 11284{\bf Warning} :\\ 11285The variables must be symbolic (if not, purge them before calling 11286{\tt reorder}) 11287 11288\subsection{Random list : {\tt ranm}}\index{ranm}\label{sec:ranm1} 11289\noindent{\tt ranm} takes as argument an integer {\tt n}.\\ 11290{\tt ranm} returns a list of {\tt n} random integers (between -99 and +99). 11291This list can be seen as the coefficients of an univariate 11292polynomial of degree {\tt n-1} 11293(see also \ref{sec:ranm2}).\\ % and \ref{sec:ranm3}).\\ 11294Input : 11295\begin{center}{\tt ranm(3)}\end{center} 11296Output : 11297\begin{center}{\tt [68,-21,56]}\end{center} 11298 11299\subsection{Lagrange's polynomial : {\tt lagrange interp}}\index{lagrange}\index{interp} 11300\noindent{\tt lagrange} takes as argument two lists of size {\tt n} (resp. a 11301matrix with two rows and {\tt n} columns) and the name of a variable 11302{\tt var} (by default {\tt x}).\\ 11303The first list (resp. row) corresponds to the abscissa values $x_k$ ($k=1..n$), 11304and the second list (resp. row) corresponds to ordinate values $y_k$ 11305($k=1..n$).\\ 11306{\tt lagrange} returns a polynomial expression {\tt P} 11307with respect to {\tt var} of degree 11308{\tt n-1}, such that $P(x_i)=y_i$.\\ 11309Input : 11310\begin{center}{\tt lagrange([[1,3],[0,1]])}\end{center} 11311or : 11312\begin{center}{\tt lagrange([1,3],[0,1])}\end{center} 11313Output : 11314\begin{center}{\tt (x-1)/2}\end{center} 11315since $\frac{x-1}{2}=0$ for $x=1$, and $\frac{x-1}{2}=1$ for $x=3$.\\ 11316Input : 11317\begin{center}{\tt lagrange([1,3],[0,1],y)}\end{center} 11318Output : 11319\begin{center}{\tt (y-1)/2}\end{center} 11320{\bf Warning}\\ 11321{\tt f:=lagrange([1,2],[3,4],y)} does not return a function 11322but an expression with respect to $y$. 11323To define $f$ as a function, input 11324\begin{center} 11325{\tt f:=unapply(lagrange([1,2],[3,4],x),x)} 11326\end{center} 11327Avoid {\tt f(x):=lagrange([1,2],[3,4],x)} since 11328the Lagrange polynomial would be computed each time {\tt f} is called 11329(indeed in a function definition, the second member of the assignment 11330is not evaluated). 11331Note also that \\ 11332{\tt g(x):=lagrange([1,2],[3,4])} would not work 11333since the default argument of {\tt lagrange} 11334would be global, hence not the same as the local 11335variable used for the definition of {\tt g}. 11336 11337\subsection{Trigonometric interpolation : {\tt triginterp}} 11338{\tt triginterp(y,x=a..b)} or {\tt triginterp(y,a,b,x)} returns the trigonometric polynomial that interpolates data given in the list $ \textbf{y} $. It is assumed that the list $ \textbf{y} $ contains ordinate components of the points with equidistant abscissa components between $ a $ and $ b $ such that the first element from $ \textbf{y} $ corresponds to $ a $ and the last element to $ b $. 11339 11340For example, $ \textbf{y} $ may be a list of experimental measurements of some quantity taken in regular intervals, with the first observation in the moment $ t=a $ and the last observation in the moment $ t=b $. The resulting trigonometric polynomial has the period 11341\[ T=\frac{n\,(b-a)}{n-1}, \] 11342where $ n $ is the number of observations ($ n $={\tt size(y)}). For example, assume that the following data is obtained by measuring the temperature every three hours: 11343\begin{center} 11344 \begin{tabular}{|r|c|c|c|c|c|c|c|c|} 11345 \hline hour of the day&0&3&6&9&12&15&18&21\\ 11346 \hline temperature (deg~C)&11&10&17&24&32&26&23&19\\\hline 11347 \end{tabular} 11348\end{center} 11349Furthermore, assume that an estimate of the temperature at 13:45 is required. To obtain a trigonometric interpolation of the data, input : 11350\begin{center} 11351 {\tt tp:=triginterp([11,10,17,24,32,26,23,19],x=0..21)} 11352\end{center} 11353Output : 11354\begin{center} 11355 {\tt 81/4+(-21*sqrt(2)-42)/8*cos(pi/12*x)+\\ 11356 (-11*sqrt(2)-12)/8*sin(pi/12*x)+3/4*cos(pi/6*x)\\ 11357 -7/4*sin(pi/6*x)+(21*sqrt(2)-42)/8*cos(pi/4*x)\\ 11358 +(-11*sqrt(2)+12)/8*sin(pi/4*x)+1/2*cos(pi/3*x)} 11359\end{center} 11360Now a temperature at 13:45 hrs can be approximated with the value of {\tt tp} for $ x=13.75 $. Input : 11361\begin{center} 11362 {\tt tp | x=13.75} 11363\end{center} 11364Output : 11365\begin{center} 11366 {\tt 29.4863181684} 11367\end{center} 11368 11369If one of the input parameters is inexact, the result will be inexact too. For example, input : 11370\begin{center} 11371 {\tt Digits:=3;\\ 11372 triginterp([11,10,17,24,32,26,23,19],x=0..21.0)} 11373\end{center} 11374Output : 11375\begin{center} 11376 {\tt 0.5*cos(1.05*x)-1.54*cos(0.785*x)+0.75*cos(0.524*x)\\ 11377 -8.96*cos(0.262*x)-0.445*sin(0.785*x)-1.75*sin(0.524*x)\\ 11378 -3.44*sin(0.262*x)+20.2} 11379\end{center} 11380 11381\subsection{Natural splines: {\tt spline}}\index{spline|textbf} 11382\subsubsection{Definition} 11383Let $\sigma_n$ be a subdivision of a real interval $[a,b]$~: 11384\[ a=x_0,\quad x_1,\quad...,\quad x_n=b \] 11385$s$ is a spline function of degree $l$, if $s$ is a function from $[a,b]$ 11386to $\mathbb R$ such that~: 11387\begin{itemize} 11388\item $s$ has continuous derivatives up to the order $l-1$, 11389\item on each interval of the subdivision, $s$ 11390is a polynomial of degree less or equal than $l$. 11391\end{itemize} 11392 11393\subsubsection{Theorem} 11394The set of spline functions of degree $l$ on $\sigma_n$ is an 11395$\mathbb R$-vector subspace of dimension $n+l$. 11396 11397{\bf Proof}\\ 11398On $[a,x_1]$, $s$ is a polynomial $A$ of degree less or equal to 11399$l$, hence on $[a,x_1]$, $s=A(x)=a_0+a_1x+...a_lx^l$ and $A$ is a linear 11400combination of $1,x,...x^l$.\\ 11401On $[x_1,x_2]$, $s$ is a polynomial $B$ of degree less or equal to 11402$l$, hence on $[x_1,x_2]$, $s=B(x)=b_0+b_1x+...b_lx^l$.\\ 11403$s$ has continuous derivatives up to order $l-1$, hence : 11404\[ \forall 0 \leq j \leq l-1, \quad B^{(j)}(x_1)-A^{(j)}(x_1)=0\] 11405therefore $B(x)-A(x)=\alpha_1(x-x_1)^l$ or $B(x)=A(x)+\alpha_1(x-x_1)^l$.\\ 11406Define the function : 11407\[\mbox{q}_1(x) \mbox{ = } 11408\left\{ 11409\begin{array}{rcl} 114100 & \mbox{on} & [a,x_1] \\ 11411(x-x_1)^l & \mbox{on} & [x_1,b]\\ 11412\end{array} 11413\right. 11414\] 11415Hence : 11416\[ s|_{[a,x_2]}=a_0+a_1x+...a_lx^l+\alpha_1q_1(x) \] 11417On $[x_2,x_3]$, $s$ is a polynomial $C$ of degree less or equal than 11418$l$, hence on $[x_2,x_3]$, $s=C(x)=c_0+c_1x+...c_lx^l$.\\ 11419$s$ has continuous derivatives until $l-1$, hence : 11420\[ \forall 0 \leq j \leq l-1, \quad C^{(j)}(x_2)-B^{(j)}(x_2)=0\] 11421therefore $C(x)-B(x)=\alpha_2(x-x_2)^l$ or $C(x)=B(x)+\alpha_2(x-x_2)^l$.\\ 11422Define the function : 11423\[\mbox{q}_2(x) \mbox{ = } 11424\left\{ 11425\begin{array}{rcl} 114260 & \mbox{on} & [a,x_2] \\ 11427(x-x_2)^l & \mbox{on} & [x_2,b]\\ 11428\end{array} 11429\right. 11430\] 11431Hence : 11432$s|_{[a,x_3]}=a_0+a_1x+...a_lx^l+\alpha_1q_1(x)+\alpha_2q_2(x)$\\ 11433And so on, the functions are defined by : 11434\[\forall 1 \leq j \leq n-1, \mbox{q}_j(x) \mbox{ = } 11435\left\{ 11436\begin{array}{rcl} 114370 & \mbox{on} & [a,x_j] \\ 11438(x-x_j)^l & \mbox{on} & [x_j,b]\\ 11439\end{array} 11440\right. 11441\] 11442hence, 11443\[ s|_{[a,b]}=a_0+a_1x+...a_lx^l+\alpha_1q_1(x)+....+\alpha_{n-1}q_{n-1}(x) \] 11444and $s$ is a linear combination of $n+l$ independent functions 11445$1,x,..x^l,q_1,..q_{n-1}$. 11446 11447\subsubsection{Interpolation with spline functions} 11448If we want to interpolate a function $f$ on $\sigma_n$ by a spline function 11449 $s$ of degree $l$, then $s$ must verify $s(x_k)=y_k=f(x_k)$ for all 11450$0\leq k\leq n$. Hence there are $n+1$ conditions, and $l-1$ degrees of 11451liberty. We can therefore add $l-1$ conditions, these conditions are on the 11452derivatives of $s$ at $a$ and $b$. 11453 11454Hermite interpolation, natural interpolation and periodic interpolation 11455are three kinds of interpolation obtained by specifying three kinds 11456of constraints. The unicity of the 11457solution of the interpolation problem can be proved 11458for each kind of constraints. 11459 11460If $l$ is odd ($l=2m-1$), there are $2m-2$ degrees of 11461freedom. The constraints are defined by : 11462\begin{itemize} 11463\item Hermite interpolation 11464\[ \forall 1\leq j\leq m-1, \quad s^{(j)}(a)=f^{(j)}(a), 11465s^{(j)}(b)=f^{(j)}(b) \] 11466\item Natural interpolation 11467\[ \forall m \leq j \leq 2m-2, \quad s^{(j)}(a)=s^{(j)}(b)=0 \] 11468\item periodic interpolation 11469\[\forall 1\leq j\leq 2m-2, \quad s^{(j)}(a)=s^{(j)}(b) \] 11470\end{itemize} 11471 11472If $l$ is even ($l=2m$), there are $2m-1$ degrees of 11473liberty. The constraints are defined by : 11474\begin{itemize} 11475\item Hermite interpolation 11476\[ \forall 1\leq j\leq m-1, \quad s^{(j)}(a)=f^{(j)}(a), 11477s^{(j)}(b)=f^{(j)}(b) \] 11478and 11479\[s^{(m)}(a)=f^{(m)}(a)\] 11480\item Natural interpolation 11481\[ \forall m \leq j \leq 2m-2, \quad s^{(j)}(a)=s^{(j)}(b)=0 \] 11482and 11483\[s^{(2m-1)}(a)=0\] 11484\item Periodic interpolation 11485\[\forall 1\leq j\leq 2m-1, \quad s^{(j)}(a)=s^{(j)}(b) \] 11486\end{itemize} 11487A natural spline 11488is a spline function which verifies the natural interpolation constraints. 11489 11490{\tt spline} takes as arguments a list of abscissa (by increasing order), 11491a list of ordinates, a variable name, and a degree.\\ 11492{\tt spline} returns the natural spline function (with the specified degree 11493and crossing points) as a list of polynomials, each 11494polynomial being valid on an interval. 11495 11496Examples: 11497\begin{enumerate} 11498\item a natural spline of degree 3, crossing through the points 11499$x_0=0,y_0=1$, $x_1=1,y_1=3$ and $x_2=2, y_2=0$, input~: 11500\begin{center} 11501{\tt spline([0,1,2],[1,3,0],x,3)} 11502\end{center} 11503Output is a list of two polynomial expressions of $x$~: 11504\[[ -5*x^3/4+13*x/4+1, \quad 5*(x-1)^3/4-15*(x-1)^2/4+(x-1)/-2+3 ]\] 11505defined respectively on the intervals $[0,1]$ and $[1,2]$. 11506\item a natural spline of degree 4, crossing through the points 11507$x_0=0,y_0=1$, $x_1=1,y_1=3$, $x_2=2, y_2=0$ and $x_3=3, y_3=-1$, 11508input~: 11509\begin{center} 11510{\tt spline([0,1,2,3],[1,3,0,-1],x,4)} 11511\end{center} 11512Output is a list of three polynomial functions of $x$~: 11513\[ [(-62*x^4+304*x)/121+1,\] 11514\[(201*(x-1)^4-248*(x-1)^3-372*(x-1)^2+56*(x-1))/121+3,\] 11515\[(-139*(x-2)^4+556*(x-2)^3+90*(x-2)^2+-628*(x-2))/121]\] 11516defined respectively on the intervals $[0,1]$, $[1,2]$ and $[2,3]$. 11517\item The natural spline interpolation of $\cos$ on 11518$[0,\pi/2,3\pi/2]$, input~: 11519\begin{center} 11520{\tt spline([0,pi/2,3*pi/2],cos([0,pi/2,3*pi/2]),x,3)} 11521\end{center} 11522Output~: 11523\[ 11524[((3*\pi^3+(-7*\pi^2)*x+4*x^3)*1/3)/(\pi^3),\] 11525\[((15*\pi^3+(-46*\pi^2)*x+36*\pi*x^2-8*x^3)*1/12)/(\pi^3)] 11526\] 11527\end{enumerate} 11528 11529\subsection{Rational interpolation : {\tt thiele}}\index{thiele} 11530\noindent {\tt thiele} takes as the first argument a matrix {\tt data} of type $ n\times 2 $ where that $ i $-th row holds coordinates $ x $ and $ y $ of $ i $-th point, respectively. The second argument is {\tt v}, which may be an identifier, number or any symbolic expression. Function returns $ R(v) $ where $ R $ is the rational interpolant. Instead of a single matrix {\tt data}, two vectors $ \mathbf{x}=(x_1,x_2,\dots,x_n) $ and $ \mathbf{y}=(y_1,y_2,\dots,y_n) $ may be given (in this case, {\tt v} is given as the third argument). 11531 11532This method computes Thiele interpolated continued fraction based on the concept of reciprocal differences. 11533 11534It is not guaranteed that $ R $ is continuous, i.e.~it may have singularities in the shortest segment which contains all components of $ \mathbf{x} $. 11535 11536\subsubsection{Examples} 11537\noindent Input : 11538\begin{center}{\tt thiele([[1,3],[2,4],[4,5],[5,8]],x)}\end{center} 11539Output : 11540\begin{center}{\tt (19*x\verb|^|2-45*x-154)/(18*x-78)}\end{center} 11541Input : 11542\begin{center}{\tt thiele([1,2,a],[3,4,5],3)}\end{center} 11543Output : 11544\begin{center}{\tt (13*a-29)/(3*a-7)}\end{center} 11545 11546In the following example, data is obtained by sampling the function $ f(x)=(1-x^4)\,\mathrm{e}^{1-x^3} $. 11547 11548\noindent Input : 11549\begin{center} 11550{\tt data\_x:=[-1,-0.75,-0.5,-0.25,0,}\\ 11551{\tt 0.25,0.5,0.75,1,1.25,1.5,1.75,2];} 11552{\tt data\_y:=[0.0,2.83341735599,2.88770329586,}\\ 11553{\tt 2.75030303645,2.71828182846,2.66568510781,}\\ 11554{\tt 2.24894558809,1.21863761951,0.0,-0.555711613283,} 11555{\tt -0.377871362418,-0.107135851128,-0.0136782294833];} 11556{\tt thiele(data\_x,data\_y,x)} 11557\end{center} 11558Output : 11559\begin{center} 11560{\tt (-1.55286115659*x\verb|^|6+5.87298387514*x\verb|^|5-5.4439152812*x\verb|^|4}\\ 11561{\tt +1.68655817708*x\verb|^|3-2.40784868317*x\verb|^|2-7.55954205222*x}\\ 11562{\tt +9.40462512097)/(x\verb|^|6-1.24295718965*x\verb|^|5-1.33526268624*x\verb|^|4}\\ 11563{\tt +4.03629272425*x\verb|^|3-0.885419321*x\verb|^|2-2.77913222418*x}\\ 11564{\tt +3.45976823393)} 11565\end{center} 11566 11567\section{Arithmetic and polynomials} 11568Polynomials are represented by expressions or by list of coefficients 11569by decreasing power order. In the first case, for instructions requiring 11570a main variable (like extended gcd computations), the variable 11571used by default is $x$ if not specified. For modular coefficients 11572in $\Z/n\Z$, use {\tt \% n } for each coefficient of the list 11573or apply it to the expression defining the polynomial. 11574 11575\subsection{The divisors of a polynomial : {\tt divis}}\index{divis} 11576\noindent{\tt divis} takes as argument a polynomial (or a list of 11577polynomials) and returns the list 11578 of the divisors of the polynomial(s).\\ 11579Input : 11580\begin{center}{\tt divis(x\verb|^|4-1)}\end{center} 11581Output : 11582\begin{center}{\tt [1,x\verb|^|2+1,x+1,(x\verb|^|2+1)*(x+1),x-1,(x\verb|^|2+1)*(x-1),}\end{center} 11583\begin{center}{\tt (x+1)*(x-1),(x\verb|^|2+1)*(x+1)*(x-1)]}\end{center} 11584Input : 11585\begin{center}{\tt divis([x\verb|^|2,x\verb|^|2-1])}\end{center} 11586Output : 11587\begin{center}{\tt [[1,x,x\verb|^|2],[1,x+1,x-1,(x+1)*(x-1)]]}\end{center} 11588 11589\subsection{Euclidean quotient : {\tt quo}}\index{quo|textbf} 11590\noindent{\tt quo} returns the euclidean quotient $q$ of the 11591Euclidean division between two polynomials (decreasing power order). 11592If the polynomials are represented as 11593expressions, the variable may be specified as a third 11594argument.\\ 11595Input : 11596\begin{center}{\tt quo(x\verb|^|2+2*x +1,x)}\end{center} 11597Output : 11598\begin{center}{\tt x+2}\end{center} 11599Input : 11600\begin{center}{\tt quo(y\verb|^|2+2*y +1,y,y)}\end{center} 11601Output : 11602\begin{center}{\tt y+2}\end{center} 11603In list representation, the quotient of $x^2+2x+4$ by $x^2+x+2$ 11604one can also input : 11605\begin{center}{\tt quo([1,2,4],[1,1,2])}\end{center} 11606Output : 11607\begin{center}{\tt [1]}\end{center} 11608that is to say the polynomial {\tt 1}. 11609 11610\subsection{Euclidean quotient : {\tt Quo}}\index{Quo|textbf} 11611\noindent {\tt Quo} is the inert form of {\tt quo}.\\ 11612{\tt Quo} returns the euclidean quotient between two polynomials 11613(decreasing power division) without evaluation. 11614It is used when {\tt Xcas} is in Maple mode to compute 11615the euclidean quotient of the division of two 11616polynomials with coefficients in $\Z/p\Z$ using Maple-like syntax.\\ 11617In {\tt Xcas} mode, input : 11618\begin{center}{\tt Quo(x\verb|^|2+2*x+1,x)}\end{center} 11619Output : 11620\begin{center}{\tt quo(x\verb|^|2+2*x+1,x)}\end{center} 11621In {\tt Maple} mode, input : 11622\begin{center}{\tt Quo(x\verb|^|3+3*x,2*x\verb|^|2+6*x+5) mod 5}\end{center} 11623Output : 11624\begin{center}{\tt -(2)*x+1}\end{center} 11625The division was done using modular arithmetic, unlike with 11626\begin{center}{\tt quo(x\verb|^|3+3*x,2*x\verb|^|2+6*x+5) mod 5}\end{center} 11627where the division is done in $\Z[X]$ and reduced after to: 11628\begin{center}{\tt 3*x-9}\end{center} 11629If {\tt Xcas} is not in Maple mode, polynomial division 11630in $\Z/p\Z[X]$ is done e.g. by~: 11631\begin{center} 11632\verb|quo((x^3+3*x)% 5,(2x^2+6x+5)%5)| 11633\end{center} 11634 11635\subsection{Euclidean remainder : {\tt rem}}\index{rem|textbf} 11636\noindent{\tt rem} returns the euclidean remainder between two polynomials 11637(decreasing power division). If the polynomials are represented as 11638expressions, the variable may be specified as a third 11639argument.\\ 11640Input : 11641\begin{center}{\tt rem(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 11642Output : 11643\begin{center}{\tt x-1}\end{center} 11644To have the remainder of $x^2+2x+4$ by $x^2+x+2$ we can also input : 11645\begin{center}{\tt rem([1,2,4],[1,1,2])}\end{center} 11646Output : 11647\begin{center}{\tt [1,2]}\end{center} 11648i.e. the polynomial $x+2$. 11649 11650\subsection{Euclidean remainder: {\tt Rem}}\index{Rem|textbf} 11651\noindent{\tt Rem} is the inert form of {\tt rem}.\\ 11652{\tt Rem} returns the euclidean remainder between two polynomials 11653(decreasing power division) without evaluation. 11654It is used when {\tt Xcas} is in Maple mode to compute 11655the euclidean remainder of the division of two 11656polynomials with coefficients in $\Z/p\Z$ using Maple-like syntax.\\ 11657In {\tt Xcas} mode, input : 11658\begin{center}{\tt Rem(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 11659Output : 11660\begin{center}{\tt rem(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 11661In {\tt Maple} mode, input : 11662\begin{center}{\tt Rem(x\verb|^|3+3*x,2*x\verb|^|2+6*x+5) mod 5}\end{center} 11663Output : 11664\begin{center}{\tt 2*x}\end{center} 11665The division was done using modular arithmetic, unlike with 11666\begin{center}{\tt rem(x\verb|^|3+3*x,2*x\verb|^|2+6*x+5) mod 5}\end{center} 11667where the division is done in $\Z[X]$ and reduced after to: 11668\begin{center}{\tt 12*x}\end{center} 11669If {\tt Xcas} is not in Maple mode, polynomial division 11670in $\Z/p\Z[X]$ is done e.g. by~: 11671\begin{center}\verb|rem((x^3+3*x)% 5,(2x^2+6x+5)%5)| 11672\end{center} 11673 11674\subsection{Quotient and remainder : {\tt quorem divide}}\index{quorem|textbf}\index{divide|textbf}\label{sec:quorem} 11675\noindent{\tt quorem} (or {\tt divide}) returns the list of the quotient and 11676the remainder of the euclidean division (by decreasing power) of two 11677polynomials.\\ 11678Input : 11679\begin{center}{\tt quorem([1,2,4],[1,1,2]) }\end{center} 11680Output : 11681\begin{center}{\tt [poly1[1],poly1[1,2]]}\end{center} 11682Input : 11683\begin{center}{\tt quorem(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 11684Output : 11685\begin{center}{\tt [x,x-1]}\end{center} 11686 11687\subsection{GCD of two polynomials with the Euclidean algorithm: {\tt gcd}}\index{gcd}\label{sec:gcd} 11688\noindent{\tt gcd} denotes the gcd (greatest common divisor) of two polynomials 11689(or of a list of polynomials or of a sequence of polynomials) 11690(see also \ref{sec:igcd} for GCD of integers). 11691 11692{\bf Examples}\\ 11693Input : 11694\begin{center}{\tt gcd(x\verb|^|2+2*x+1,x\verb|^|2-1)}\end{center} 11695Output : 11696\begin{center}{\tt x+1 }\end{center} 11697Input : 11698\begin{center}{\tt gcd(x\verb|^|2-2*x+1,x\verb|^|3-1,x\verb|^|2-1,x\verb|^|2+x-2)}\end{center} 11699or 11700\begin{center}{\tt gcd([x\verb|^|2-2*x+1,x\verb|^|3-1,x\verb|^|2-1,x\verb|^|2+x-2])}\end{center} 11701Output : 11702\begin{center}{\tt x-1}\end{center} 11703 11704For polynomials with modular coefficients, input e.g. : 11705\begin{center}{\tt gcd((x\verb|^|2+2*x+1) mod 5,(x\verb|^|2-1) mod 5)}\end{center} 11706Output : 11707\begin{center}{\tt x \% 5}\end{center} 11708Note that : 11709\begin{center}{\tt gcd(x\verb|^|2+2*x+1,x\verb|^|2-1) mod 5}\end{center} 11710will output : 11711\begin{center}{\tt 1}\end{center} 11712since the mod operation is done after the GCD is computed in $\Z[X]$. 11713 11714\subsection{GCD of two polynomials with the Euclidean algorithm : {\tt Gcd}}\index{Gcd} 11715\noindent{\tt Gcd} is the inert form of {\tt gcd}. 11716{\tt Gcd} returns the gcd (greatest common divisor) of two polynomials 11717(or of a list of polynomials or of a sequence of polynomials) without 11718evaluation. It is used when {\tt Xcas} is in Maple mode to compute 11719the gcd of polynomials with coefficients in $\Z/p\Z$ using Maple-like syntax.\\ 11720Input in {\tt Xcas} mode : 11721\begin{center}{\tt Gcd(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 11722Output : 11723\begin{center}{\tt gcd(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 11724Input in {\tt Maple} mode : 11725\begin{center}{\tt Gcd(x\verb|^|2+2*x,x\verb|^|2+6*x+5) mod 5}\end{center} 11726Output : 11727\begin{center}{\tt 1}\end{center} 11728 11729\subsection{Choosing the GCD algorithm of two polynomials : 11730{\tt ezgcd heugcd modgcd psrgcd}}\index{ezgcd}\index{psrgcd}\index{modgcd}\index{heugcd} 11731\noindent{\tt ezgcd heugcd modgcd psrgcd} denote the gcd 11732(greatest common divisor) of two univariate or multivariate 11733polynomials with coefficients 11734in $\Z$ or $\Z[i]$ using a specific algorithm~: 11735\begin{itemize} 11736\item {\tt ezgcd} ezgcd algorithm, 11737\item {\tt heugcd} heuristic gcd algorithm, 11738\item {\tt modgcd} modular algorithm, 11739\item {\tt psrgcd} sub-resultant algorithm. 11740\end{itemize} 11741Input : 11742\begin{center}{\tt ezgcd(x\verb|^|2-2*x*y+y\verb|^|2-1,x-y)}\end{center} 11743or : 11744\begin{center}{\tt heugcd(x\verb|^|2-2*x*y+y\verb|^|2-1,x-y)}\end{center} 11745or : 11746\begin{center}{\tt modgcd(x\verb|^|2-2*x*y+y\verb|^|2-1,x-y)}\end{center} 11747or : 11748\begin{center}{\tt psrgcd(x\verb|^|2-2*x*y+y\verb|^|2-1,x-y)}\end{center} 11749Output : 11750\begin{center}{\tt 1 }\end{center} 11751Input : 11752\begin{center}{\tt ezgcd((x+y-1)*(x+y+1),(x+y+1)\verb|^|2)}\end{center} 11753or : 11754\begin{center}{\tt heugcd((x+y-1)*(x+y+1),(x+y+1)\verb|^|2)}\end{center} 11755or : 11756\begin{center}{\tt modgcd((x+y-1)*(x+y+1),(x+y+1)\verb|^|2)}\end{center} 11757Output : 11758\begin{center}{\tt x+y+1}\end{center} 11759Input : 11760\begin{center}{\tt psrgcd((x+y-1)*(x+y+1),(x+y+1)\verb|^|2)}\end{center} 11761Output : 11762\begin{center}{\tt -x-y-1}\end{center} 11763Input : 11764\begin{center}{\tt ezgcd((x+1)\verb|^|4-y\verb|^|4,(x+1-y)\verb|^|2)}\end{center} 11765Output : 11766\begin{center}{\tt "GCD not successful Error: Bad Argument Value"}\end{center} 11767 But input : 11768\begin{center}{\tt heugcd((x+1)\verb|^|4-y\verb|^|4,(x+1-y)\verb|^|2)}\end{center} 11769or : 11770\begin{center}{\tt modgcd((x+1)\verb|^|4-y\verb|^|4,(x+1-y)\verb|^|2)}\end{center} 11771or : 11772\begin{center}{\tt psrgcd((x+1)\verb|^|4-y\verb|^|4,(x+1-y)\verb|^|2)}\end{center} 11773Output : 11774\begin{center}{\tt x-y+1 }\end{center} 11775 11776\subsection{LCM of two polynomials : {\tt lcm}}\index{lcm}\label{sec:lcm} 11777\noindent{\tt lcm} returns the LCM (Least Common Multiple) of two polynomials 11778(or of a list of polynomials or of a sequence of polynomials) 11779(see \ref{sec:ilcm} for LCM of integers).\\ 11780Input : 11781\begin{center}{\tt lcm(x\verb|^|2+2*x+1,x\verb|^|2-1)}\end{center} 11782Output : 11783\begin{center}{\tt (x+1)*(x\verb|^|2-1)}\end{center} 11784Input : 11785\begin{center}{\tt lcm(x,x\verb|^|2+2*x+1,x\verb|^|2-1)}\end{center} 11786or 11787\begin{center}{\tt lcm([x,x\verb|^|2+2*x+1,x\verb|^|2-1])}\end{center} 11788Output : 11789\begin{center}{\tt (x\verb|^|2+x)*(x\verb|^|2-1)}\end{center} 11790 11791\subsection{B\'ezout's Identity : {\tt egcd gcdex}}\index{egcd}\index{gcdex} 11792This function computes the polynomial coefficients of B\'ezout's 11793Identity (also known as Extended Greatest Common Divisor). 11794Given two polynomials $A(x),B(x)$, {\tt egcd} computes 3 polynomials 11795$U(x),V(x)$ and $D(x)$ such that~: 11796\[ U(x)*A(x)+V(x)*B(x)=D(x)=GCD(A(x),B(x)) \] 11797{\tt egcd} takes 2 or 3 arguments: the polynomials $A$ and $B$ as 11798expressions in terms of a variable, if the variable is not specified 11799it will default to $x$. Alternatively, $A$ and $B$ may be given 11800as list-polynomials.\\ 11801Input : 11802\begin{center}{\tt egcd(x\verb|^|2+2*x+1,x\verb|^|2-1)}\end{center} 11803Output : 11804\begin{center}{\tt [1,-1,2*x+2]}\end{center} 11805Input : 11806\begin{center}{\tt egcd([1,2,1],[1,0,-1])}\end{center} 11807Output : 11808\begin{center}{\tt [[1],[-1],[2,2]]}\end{center} 11809Input : 11810\begin{center}{\tt egcd(y\verb|^|2-2*y+1,y\verb|^|2-y+2,y)}\end{center} 11811Output : 11812\begin{center}{\tt [y-2,-y+3,4]}\end{center} 11813Input : 11814\begin{center}{\tt egcd([1,-2,1],[1,-1,2])}\end{center} 11815Output : 11816\begin{center}{\tt [[1,-2],[-1,3],[4]]}\end{center} 11817 11818\subsection{Solving au+bv=c over polynomials: {\tt abcuv}}\index{abcuv} 11819{\tt abcuv} solves the polynomial equation 11820\[ C(x)=U(x)*A(x)+V(x)*B(x) \] 11821where $A,B,C$ are given polynomials and $U$ and $V$ are unknown 11822polynomials. $C$ must be a multiple of the gcd of $A$ and $B$ 11823for a solution to exist. {\tt abcuv} takes 3 expressions as argument, 11824and an optional variable specification (which defaults to $x$) 11825and returns a list of 2 expressions ($U$ and $V$). Alternatively, the 11826polynomials $A,B,C$ may be entered as list-polynomials. 11827 11828Input : 11829\begin{center}{\tt abcuv(x\verb|^|2+2*x+1 ,x\verb|^|2-1,x+1)}\end{center} 11830Output : 11831\begin{center}{\tt [1/2,1/-2]}\end{center} 11832Input : 11833\begin{center}{\tt abcuv(x\verb|^|2+2*x+1 ,x\verb|^|2-1,x\verb|^|3+1)}\end{center} 11834Output : 11835\begin{center}{\tt [1/2*x\verb|^|2+1/-2*x+1/2,-1/2*x\verb|^|2-1/-2*x-1/2]}\end{center} 11836Input : 11837\begin{center}{\tt abcuv([1,2,1],[1,0,-1],[1,0,0,1])}\end{center} 11838Output : 11839\begin{center}{\tt [poly1[1/2,1/-2,1/2],poly1[1/-2,1/2,1/-2]]}\end{center} 11840 11841% 11842 11843\subsection{Chinese remainders : {\tt chinrem}}\index{chinrem} 11844\noindent{\tt chinrem} takes two lists as argument, each list being made of 2 11845polynomials (either expressions or as a list of coefficients in decreasing 11846order). If the polynomials are expressions, an optional third 11847argument may be provided to specify the main variable, by default 11848{\tt x} is used. 11849{\tt chinrem([A,R],[B,Q])} returns the list of two polynomials 11850{\tt P} and {\tt S} such that : 11851\[ S=R Q, \quad P=A \pmod R, \quad P=B \pmod Q \] 11852If {\tt R} and {\tt Q} are coprime, a solution {\tt P} always exists 11853and all the solutions are congruent modulo {\tt S=R*Q}. 11854For example, assume we want to solve : 11855\[ {\tt \left\{ \begin{array}{rlr} P(x)=&x\ &\bmod\ (x^2+1)\\ 11856 P(x)=&x-1\ &\bmod\ (x^2-1) \end{array}\right.} \] 11857Input : 11858\begin{center}{\tt chinrem([[1,0],[1,0,1]],[[1,-1],[1,0,-1]])}\end{center} 11859Output : 11860\begin{center}{\tt [[1/-2,1,1/-2],[1,0,0,0,-1]]}\end{center} 11861or : 11862\begin{center}{\tt chinrem([x,x\verb|^|2+1],[x-1,x\verb|^|2-1])}\end{center} 11863Output : 11864\begin{center}{\tt [1/-2*x\verb|^|2+x+1/-2,x\verb|^|4-1]}\end{center} 11865hence $\displaystyle P(x)=-\frac{x^2-2.x+1}{2} \ (\bmod\ x^4-1)$\\ 11866Another example, input : 11867\begin{center}{\tt chinrem([[1,2],[1,0,1]],[[1,1],[1,1,1]])}\end{center} 11868Output : 11869\begin{center}{\tt [[-1,-1,0,1],[1,1,2,1,1]]}\end{center} 11870or : 11871\begin{center}{\tt chinrem([y+2,y\verb|^|2+1],[y+1,y\verb|^|2+y+1],y)}\end{center} 11872Output : 11873\begin{center}{\tt [-y\verb|^|3-y\verb|^|2+1,y\verb|^|4+y\verb|^|3+2*y\verb|^|2+y+1]}\end{center} 11874 11875\subsection{Cyclotomic polynomial : {\tt cyclotomic}}\index{cyclotomic} 11876\noindent{\tt cyclotomic} takes an integer $n$ as argument and 11877returns the list of the coefficients of the cyclotomic 11878polynomial of index $n$. This 11879is the polynomial having the $n$-th primitive roots of unity 11880as zeros (an $n$-th root of unity is primitive if the set of its 11881powers is the set of all the $n$-th roots of unity). 11882 11883For example, let $n=4$, the fourth roots of unity are: 11884$\{ 1,i,-1,-i\}$ and the primitive roots are: $\{i,-i\}$. 11885Hence, the cyclotomic polynomial of index $4$ is $(x-i).(x+i)=x^2+1$. 11886Verification: 11887\begin{center}{\tt cyclotomic(4)}\end{center} 11888Output : 11889\begin{center}{\tt [1,0,1]}\end{center} 11890Another example, input : 11891\begin{center}{\tt cyclotomic(5)}\end{center} 11892Output : 11893\begin{center}{\tt [1,1,1,1,1]}\end{center} 11894Hence, the cyclotomic polynomial of index $5$ is $x^4+x^3+x^2+x+1$ 11895which divides $x^5-1$ since $(x-1)*(x^4+x^3+x^2+x+1)=x^5-1$.\\ 11896Input : 11897\begin{center}{\tt cyclotomic(10)}\end{center} 11898Output : 11899\begin{center}{\tt [1,-1,1,-1,1]}\end{center} 11900Hence, the cyclotomic polynomial of index $10$ is $x^4-x^3+x^2-x+1$ and 11901\[ (x^5-1)*(x+1)*(x^4-x^3+x^2-x+1)=x^{10}-1 \] 11902Input : 11903\begin{center}{\tt cyclotomic(20)}\end{center} 11904Output : 11905\begin{center}{\tt [1,0,-1,0,1,0,-1,0,1]}\end{center} 11906Hence, the cyclotomic polynomial of index $20$ is $x^8-x^6+x^4-x^2+1$ and 11907\[ (x^{10}-1)*(x^2+1)*(x^8-x^6+x^4-x^2+1)=x^{20}-1 \] 11908 11909\subsection{Sturm sequences and number of sign changes 11910of $P$ on $(a,\ b]$ : {\tt sturm}}\index{sturm} 11911\noindent{\tt sturm} takes two or four arguments : $P$ a polynomial expression 11912or $P/Q$ a rational fraction and a variable name or $P$ a polynomial 11913expression, a variable name and two real or complex numbers $a$ and $b$. 11914 11915If {\tt sturm} takes two arguments, {\tt sturm} returns the list of the Sturm 11916sequences and multiplicities of the square-free factors of $P$ (or 11917$P/Q$) (in this case {\tt sturm} behaves like {\tt sturmseq}). 11918 11919If {\tt sturm} takes four arguments, it behaves like {\tt sturmab}~: 11920\begin{itemize} 11921\item if $a$ and $b$ are reals, 11922{\tt sturm} returns the number of sign changes of $P$ on $(a,\ b]$ 11923\item if $a$ or $b$ are complex, 11924{\tt sturm} returns the number of complex roots of $P$ in the rectangle 11925having $a$ and $b$ as opposite vertices. 11926\end{itemize} 11927Input : 11928\begin{center}{\tt sturm(2*x\verb|^|3+2,x)}\end{center} 11929Output : 11930\begin{center}{\tt [2,[[1,0,0,1],[3,0,0],-9],1]}\end{center} 11931Input : 11932\begin{center}{\tt sturm((2*x\verb|^|3+2)/(x+2),x)}\end{center} 11933Output : 11934\begin{center}{\tt [2,[[1,0,0,1],[3,0,0],-9],1,[[1,2],1]]}\end{center} 11935Input : 11936\begin{center}{\tt sturm(x\verb|^|2*(x\verb|^|3+2),x,-2,0)}\end{center} 11937Output : 11938\begin{center}{\tt 1}\end{center} 11939 11940\subsection{Number of zeros in $[a,b)$ : {\tt sturmab}}\index{sturmab} 11941\noindent{\tt sturmab} takes four arguments: a polynomial expression $P$, a 11942variable name and two real or complex numbers $a$ and $b$ 11943\begin{itemize} 11944\item if $a$ and $b$ are reals, 11945{\tt sturmab} returns the number of sign changes of $P$ on $(a,\ b]$. 11946In other words, it returns the number of zeros in $[a,b)$ of the 11947polynomial $P/G$ where $G=\mbox{gcd}(P,\mbox{diff}(P))$. 11948\item if $a$ or $b$ are complex, 11949{\tt sturmab} returns the number of complex roots of $P$ in the rectangle 11950having $a$ and $b$ as opposite vertices. 11951\end{itemize} 11952Input : 11953\begin{center}{\tt sturmab(x\verb|^|2*(x\verb|^|3+2),x,-2,0)}\end{center} 11954Output : 11955\begin{center}{\tt 1}\end{center} 11956Input : 11957\begin{center}{\tt sturmab(x\verb|^|3-1,x,-2-i,5+3i)}\end{center} 11958Output : 11959\begin{center}{\tt 3}\end{center} 11960Input : 11961\begin{center}{\tt sturmab(x\verb|^|3-1,x,-i,5+3i)}\end{center} 11962Output : 11963\begin{center}{\tt 1}\end{center} 11964{\bf Warning !!!!}\\ 11965 $P$ is defined by its symbolic expression.\\ 11966Input :\\ 11967{\tt sturmab([1,0,0,2,0,0],x,-2,0)},\\ 11968Output :\\ 11969{\tt Bad argument type}. 11970 11971\subsection{Sturm sequences : {\tt sturmseq}}\index{sturmseq} 11972\noindent{\tt sturmseq} takes as argument, a polynomial expression $P$ or a 11973rational fraction $P/Q$ and returns the list of the Sturm sequences 11974of the square-free factors of odd multiplicity of $P$ (or of $P/Q$). 11975For $F$ a square-free factor of odd multiplicity, the Sturm 11976sequence $R_1,R_2,...$ is made from $F$, $F'$ by a recurrence 11977relation~: 11978\begin{itemize} 11979\item 11980$R_1$ is the opposite of the euclidean division remainder of $F$ by 11981$F'$ then, 11982\item 11983$R_2$ is the opposite of the euclidean division remainder of $F'$ by 11984$R_1$, 11985\item ... 11986\item and so on until $R_k=0$. 11987\end{itemize} 11988Input : 11989\begin{center}{\tt sturmseq(2*x\verb|^|3+2)}\end{center} 11990or 11991\begin{center}{\tt sturmseq(2*y\verb|^|3+2,y)}\end{center} 11992Output : 11993\begin{center}{\tt [2,[[1,0,0,1],[3,0,0],-9],1]}\end{center} 11994The first term gives the content of the numerator (here 2), 11995then the Sturm sequence (in list representation) $[x^3+1,3x^2,-9]$.\\ 11996Input : 11997\begin{center}{\tt sturmseq((2*x\verb|^|3+2)/(3*x\verb|^|2+2),x)}\end{center} 11998Output : 11999\begin{center}{\tt [2,[[1,0,0,1],[3,0,0],-9],1,[1,[[3,0,2],[6,0],-72]]}\end{center} 12000The first term gives the content of the numerator (here 2), 12001then the Sturm sequence of the numerator ([[1,0,0,1],[3,0,0],-9]), 12002then the content of the denominator (here 1) and the Sturm 12003sequence of the denominator ([[3,0,2],[6,0],-72]). As expressions, 12004$[x^3+1,3x^2, -9]$ is the Sturm sequence of the numerator and 12005$[3x^2+2,6x,-72]$ is the Sturm sequence of the denominator.\\ 12006Input : 12007\begin{center}{\tt sturmseq((x\verb|^|3+1)\verb|^|2,x)}\end{center} 12008Output : 12009\begin{center}{\tt [1,1]}\end{center} 12010Indeed $F=1$.\\ 12011Input : 12012\begin{center}{\tt sturmseq(3*(3*x\verb|^|3+1)/(2*x+2),x)}\end{center} 12013Output : 12014\begin{center}{\tt[3,[[3,0,0,1],[9,0,0],-81],2,[[1,1],1]]}\end{center} 12015The first term gives the content of the numerator 12016(here {\tt 3}),\\ 12017the second term gives the Sturm sequence of the numerator 12018(here {\tt 3x\verb|^|3+1, 9x\verb|^|2, -81}),\\ 12019the third term gives the content of the denominator (here 12020{\tt 2}),\\ 12021the fourth term gives the Sturm sequence of the denominator 12022({\tt x+1,1}).\\ 12023{\bf Warning !!!!}\\ 12024$P$ is defined by its symbolic expression.\\ 12025Input : 12026\begin{center} 12027{\tt sturmseq([1,0,0,1],x)} 12028\end{center} 12029Output : 12030\begin{center} 12031{\tt Bad argument type}. 12032\end{center} 12033 12034\subsection{Sylvester matrix of two polynomials : {\tt sylvester}}\index{sylvester} 12035\noindent{\tt sylvester} takes two polynomials as arguments.\\ 12036{\tt sylvester} returns the Sylvester matrix $S$ of these polynomials.\\ 12037If $A(x)=\sum_{i=0}^{i=n} a_ix^i$ and 12038$B(x)=\sum_{i=0}^{i=m}b_ix^i$ are 2 polynomials, their Sylvester matrix 12039$S$ is a square matrix of size {\tt m+n} where {\tt m=degree(B(x))} and 12040{\tt n=degree(A(x))}. The {\tt m} first lines are made with the $A(x)$ 12041coefficients, so that : 12042$$\left(\begin{array}{ccccccc} 12043s_{11}=a_n & s_{12}=a_{n-1}& \cdots & s_{1(n+1)}=a_0 & 0 & \cdots & 0\\ 12044s_{21}=0 & s_{22}=a_{n}& \cdots & s_{2(n+1)}=a_1 & s_{2(n+2)}=a_0 & \cdots & 0\\ 12045\vdots &\vdots &\vdots &\ddots &\vdots &\ddots &\vdots\\ 12046s_{m1}=0 & s_{m2}=0& \cdots & s_{m(n+1)}=a_{m-1} & s_{m(n+2)}=a_{m-2} & \cdots&a_0 12047\end{array}\right)$$ 12048and the {\tt n} further lines are made with the $B(x)$ 12049coefficients, so that : 12050$$\left(\begin{array}{ccccccc} 12051s_{(m+1)1}=b_m & s_{(m+1)2}=b_{m-1}& \cdots & s_{(m+1)(m+1)}=b_0 & 0 & \cdots & 0\\ 12052\vdots &\vdots &\vdots &\ddots &\vdots &\ddots &\vdots\\ 12053s_{(m+n)1}=0 & s_{(m+n)2}=0& \cdots & s_{(m+n)(m+1)}=b_{n-1} & b_{n-2} &\cdots&b_0 12054\end{array}\right)$$ 12055Input : 12056\begin{center}{\tt sylvester(x\verb|^|3-p*x+q,3*x\verb|^|2-p,x)}\end{center} 12057Output : 12058\begin{center}{\tt [[1,0,-p,q,0],[0,1,0,-p,q],[3,0,-p,0,0], [0,3,0,-p,0],[0,0,3,0,-p]]}\end{center} 12059Input : 12060\begin{center}{\tt det([[1,0,-p,q,0],[0,1,0,-p,q],[3,0,-p,0,0], [0,3,0,-p,0],[0,0,3,0,-p]])}\end{center} 12061Output : 12062\begin{center}{\tt -4*p\verb|^|3--27*q\verb|^|2}\end{center} 12063 12064\subsection{Resultant of two polynomials : {\tt resultant}}\index{resultant} 12065\noindent{\tt resultant} takes as argument two polynomials and 12066returns the resultant of the two polynomials.\\ 12067The resultant of two polynomials is the determinant of their 12068Sylvester matrix $S$. 12069The Sylvester matrix $S$ of two polynomials $A(x)=\sum_{i=0}^{i=n} a_ix^i$ 12070and $B(x)=\sum_{i=0}^{i=m} b_ix^i$ 12071is a square matrix with $m+n$ rows and columns; its first $m$ rows 12072are made from the coefficients of $A(X)$: 12073$$\left(\begin{array}{ccccccc} 12074s_{11}=a_n & s_{12}=a_{n-1}& \cdots & s_{1(n+1)}=a_0 & 0 & \cdots & 0\\ 12075s_{21}=0 & s_{22}=a_{n}& \cdots & s_{2(n+1)}=a_1 & s_{2(n+2)}=a_0 & \cdots & 0\\ 12076\vdots &\vdots &\vdots &\ddots &\vdots &\ddots &\vdots\\ 12077s_{m1}=0 & s_{m2}=0& \cdots & s_{m(n+1)}=a_{m-1} & s_{m(n+2)}=a_{m-2} & \cdots&a_0 12078\end{array}\right)$$ 12079and the following $n$ rows are made in the same way from the 12080coefficients of $B(x)$ : 12081$$\left(\begin{array}{ccccccc} 12082s_{(m+1)1}=b_m & s_{(m+1)2}=b_{m-1}& \cdots & s_{(m+1)(m+1)}=b_0 & 0 & \cdots & 0\\ 12083\vdots &\vdots &\vdots &\ddots &\vdots &\ddots &\vdots\\ 12084s_{(m+n)1}=0 & s_{(m+n)2}=0& \cdots & s_{(m+n)(m+1)}=b_{n-1} & b_{n-2} &\cdots&b_0 12085\end{array}\right)$$ 12086 12087If $A$ and $B$ have integer coefficients with non-zero resultant $r$, 12088then the polynomials equation 12089\[ AU+BV=r\] 12090has a unique solution $U,V$ such that degree$(U)<$degree$(B)$ and 12091degree$(V)<$degree$(A)$, and this solution has integer coefficients. 12092 12093Input : 12094\begin{center}{\tt resultant(x\verb|^|3-p*x+q,3*x\verb|^|2-p,x)}\end{center} 12095Output : 12096\begin{center}{\tt -4*p\verb|^|3--27*q\verb|^|2}\end{center} 12097{\bf Remark}\\ 12098discriminant(P)=resultant(P,P'). 12099 12100{\bf An example using the resultant}\\ 12101Let, $F1$ and $F2$ be 2 fixed points in the plane and 12102$A$, a variable point on the circle of center $F1$ and radius $2a$. 12103Find the cartesian equation of the set of points $M$, intersection of 12104the line $F1A$ and of the perpendicular bisector of $F2A$. 12105 12106Geometric answer~: 12107\[ MF1+MF2=MF1+MA=F1A=2a\] 12108hence $M$ is on an ellipse with focus $F1,F2$ and major axis $2a$. 12109 12110Analytic answer~: 12111In the Cartesian coordinate system with center $F1$ 12112and $x$-axis having the same 12113direction as the vector $F1F2$, the coordinates of $A$ are : 12114\[ A= (2a\cos(\theta),2a\sin(\theta)) \] 12115where $\theta$ is the $(Ox,OA)$ angle. 12116Now choose $t=\tan(\theta/2)$ as parameter, so that the coordinates 12117of $A$ are rational functions with respect to $t$. 12118More precisely~: 12119\[ A=(ax,ay)=(2a\frac{1-t^2}{1+t^2},2a\frac{2t}{1+t^2}) \] 12120If $F1F2=2c$ and if $I$ is the midpoint of $AF2$, 12121since the coordinates of $F2$ are $F2=(2c,0)$, the coordinates 12122of $I$ 12123\[ I=(c+ax/2;ay/2)=(c+a\frac{1-t^2}{1+t^2};a\frac{2t}{1+t^2}) \] 12124$IM$ is orthogonal to $AF2$, hence $M=(x;y)$ satisfies the equation 12125$eq1=0$ where 12126\[ eq1:=(x-ix)*(ax-2*c)+(y-iy)*ay \] 12127But $M=(x,y)$ is also on $F1A$, hence $M$ satisfies the equation $eq2=0$ 12128\[ eq2:=y/x-ay/ax \] 12129The resultant of both equations with respect to $t$ 12130{\tt resultant(eq1,eq2,t)} is a polynomial $eq3$ depending on the 12131variables $x,y$, independent of $t$ which is the cartesian equation 12132of the set of points $M$ when $t$ varies.\\ 12133Input : 12134\begin{center} 12135{\tt ax:=2*a*(1-t\verb|^|2)/(1+t\verb|^|2);ay:=2*a*2*t/(1+t\verb|^|2);}\\ 12136{\tt ix:=(ax+2*c)/2; iy:=(ay/2)}\\ 12137{\tt eq1:=(x-ix)*(ax-2*c)+(y-iy)*ay}\\ 12138{\tt eq2:=y/x-ay/ax}\\ 12139{\tt factor(resultant(eq1,eq2,t))} 12140\end{center} 12141Output gives as resultant :\\ 12142\begin{center} 12143${\tt -(64\cdot(x^2+y^2)\cdot(x^2\cdot a^2-x^2\cdot c^2+-2\cdot x\cdot a^2\cdot 12144c+2\cdot x\cdot c^3-a^4+2\cdot a^2\cdot c^2+}$\\ 12145${\tt a^2\cdot y^2-c^4))}$ 12146\end{center} 12147The factor ${\tt -64\cdot (x^2+y^2)}$ is always different from zero, 12148hence the locus equation of $M$~: 12149\[ {\tt x^2a^2-x^2c^2+-2xa^2c+2xc^3-a^4+2a^2c^2+a^2y^2-c^4=0} \] 12150If the frame origin is $O$, the middle point of $F1F2$, 12151we find the cartesian equation of an ellipse. 12152To make the change of origin 12153$\overrightarrow{F1M}=\overrightarrow{F1O}+\overrightarrow{OM}$, input : 12154\[ {\tt normal(subst(x^2\cdot a^2-x^2\cdot c^2+-2\cdot x\cdot a^2\cdot 12155c+2\cdot x\cdot c^3-a^4+2\cdot a^2\cdot c^2+} \] 12156\[ {\tt a^2\cdot y^2-c^4,[x,y]=[c+X,Y]))} \] 12157Output : 12158\[ {\tt -c^2*X^2+c^2*a^2+X^2*a^2-a^4+a^2*Y^2} \] 12159or if $b^2=a^2-c^2$, input : 12160\[ {\tt 12161 normal(subst(-c^2*X^2+c^2*a^2+X^2*a^2-a^4+a^2*Y^2,c^2=a^2-b^2))} \] 12162Output : 12163\[ {\tt -a^2*b^2+a^2*Y^2+b^2*X^2} \] 12164that is to say, after division by $a^2*b^2$, $M$ verifies the equation : 12165\[ \frac{X^2}{a^2}+\frac{Y^2}{b^2}=1 \] 12166 12167{\bf Another example using the resultant}\\ 12168Let $F1$ and $F2$ be fixed points and $A$ a variable point on the 12169circle of center $F1$ and radius $2a$. 12170Find the cartesian equation of the hull of $D$, the segment bisector 12171of $F2A$. 12172 12173The segment bisector of $F2A$ is tangent to the ellipse of focus 12174$F1,F2$ and major axis $2a$. 12175 12176In the Cartesian coordinate system of center $F1$ and $x$-axis having the same 12177direction than the vector $F1F2$, the coordinates of $A$ are : 12178\[ A= (2a\cos(\theta);2a\sin(\theta)) \] 12179where $\theta$ is the $(Ox,OA)$ angle. 12180Choose $t=\tan(\theta/2)$ as parameter such that the coordinates of $A$ are 12181rational functions with respect to $t$. 12182More precisely~: 12183\[ A=(ax;ay)=(2a\frac{1-t^2}{1+t^2};2a\frac{2t}{1+t^2}) \] 12184If $F1F2=2c$ and if $I$ is the middle point of $AF2$:\\ 12185\[ F2=(2c,0), \quad 12186I=(c+ax/2;ay/2)=(c+a\frac{1-t^2}{1+t^2};a\frac{2t}{1+t^2}) 12187\] 12188Since $D$ is orthogonal to $AF2$, the equation of $D$ is 12189$eq1=0$ where 12190\[ eq1:=(x-ix)*(ax-2*c)+(y-iy)*ay \] 12191So, the hull of $D$ is the locus of $M$, the intersection point of $D$ 12192and $D'$ where $D'$ has equation $eq2:=diff(eq1,t)=0$. 12193Input : 12194\begin{center} 12195{\tt ax:=2*a*(1-t\verb|^|2)/(1+t\verb|^|2);ay:=2*a*2*t/(1+t\verb|^|2);}\\ 12196{\tt ix:=(ax+2*c)/2; iy:=(ay/2)}\\ 12197{\tt eq1:=normal((x-ix)*(ax-2*c)+(y-iy)*ay)}\\ 12198{\tt eq2:=normal(diff(eq1,t))}\\ 12199{\tt factor(resultant(eq1,eq2,t))} 12200\end{center} 12201Output gives as resultant : 12202\begin{center} 12203${\tt (-(64\cdot a\verb|^|2))\cdot(x\verb|^|2+y\verb|^|2)\cdot(x\verb|^|2\cdot a\verb|^|2-x\verb|^|2\cdot c\verb|^|2+-2\cdot x\cdot a\verb|^|2\cdot c+}$\\ 12204${\tt 2\cdot x\cdot c\verb|^|3-a\verb|^|4+2\cdot a\verb|^|2\cdot c\verb|^|2+a\verb|^|2\cdot y\verb|^|2-c\verb|^|4)}$\\ 12205\end{center} 12206The factor ${\tt -64\cdot (x^2+y^2)}$ is always different from zero, 12207therefore the locus equation is : 12208\[ {\tt x^2a^2-x^2c^2+-2xa^2c+2xc^3-a^4+2a^2c^2+a^2y^2-c^4=0} \] 12209If $O$, the middle point of $F1F2$, is chosen as origin, 12210we find again the cartesian equation of the ellipse~: 12211\[ \frac{X^2}{a^2}+\frac{Y^2}{b^2}=1 \] 12212 12213\section{Orthogonal polynomials} 12214\subsection{Legendre polynomials: {\tt legendre}}\index{legendre} 12215\noindent{\tt legendre} takes as argument an integer $n$ and 12216optionally a variable name (by default $x$).\\ 12217{\tt legendre} returns the Legendre polynomial of degree $n$ : it is 12218 a polynomial $L(n,x)$, solution of the differential equation: 12219$$(x^2-1) y''-2 x y'-n(n+1) y=0$$ 12220The Legendre polynomials verify the following recurrence relation: 12221\[ L(0,x)=1, \quad 12222L(1,x)=x, \quad 12223L(n,x)=\frac{2n-1}{n}x L(n-1,x)-\frac{n-1}{n}L(n-2,x) 12224\] 12225These polynomials are orthogonal for the scalar product : 12226\[ <f,g>=\int_{-1}^{+1}f(x)g(x)\ dx \] 12227Input : 12228\begin{center}{\tt legendre(4)}\end{center} 12229Output : 12230\begin{center}{\tt (35*x\verb|^|4+-30*x\verb|^|2+3)/8}\end{center} 12231Input : 12232\begin{center}{\tt legendre(4,y)}\end{center} 12233Output : 12234\begin{center}{\tt (35*y\verb|^|4+-30*y\verb|^|2+3)/8}\end{center} 12235 12236\subsection{Hermite polynomial : {\tt hermite}}\index{hermite} 12237\noindent{\tt hermite} takes as argument an integer $n$ and 12238 optionally a variable name (by default $x$).\\ 12239{\tt hermite} returns the Hermite polynomial of degree $n$.\\ 12240If $H(n,x)$ denotes the Hermite polynomial of degree $n$, 12241the following recurrence relation holds: 12242\[ H(0,x)=1, \quad 12243H(1,x)=2x, \quad 12244H(n,x)=2xH(n-1,x)-2(n-1)H(n-2,x) \] 12245These polynomials are orthogonal for the scalar product: 12246\[ <f,g>=\int_{-\infty}^{+\infty}f(x)g(x)e^{-x^2}dx \] 12247Input : 12248\begin{center}{\tt hermite(6)}\end{center} 12249Output : 12250\begin{center}{\tt 64*x\verb|^|6+-480*x\verb|^|4+720*x\verb|^|2-120}\end{center}Input : 12251\begin{center}{\tt hermite(6,y)}\end{center} 12252Output : 12253\begin{center}{\tt 64*y\verb|^|6+-480*y\verb|^|4+720*y\verb|^|2-120}\end{center} 12254 12255\subsection{Laguerre polynomials: {\tt laguerre}}\index{laguerre} 12256\noindent{\tt laguerre} takes as argument an integer $n$ and optionally 12257 a variable name (by default $x$) and a parameter name (by default $a$).\\ 12258{\tt laguerre} returns the Laguerre polynomial of degree $n$ and of 12259parameter $a$.\\ 12260If $L(n,a,x)$ denotes the Laguerre polynomial of degree $n$ and 12261parameter $a$, the following recurrence relation holds: 12262\[ L(0,a,x)=1, \quad 12263L(1,a,x)=1+a-x, \quad 12264L(n,a,x)=\frac{2n+a-1-x}{n}L(n-1,a,x)-\frac{n+a-1}{n}L(n-2,a,x) \] 12265These polynomials are orthogonal for the scalar product 12266\[ <f,g>=\int_{0}^{+\infty}f(x)g(x)x^ae^{-x}dx \] 12267Input : 12268\begin{center}{\tt laguerre(2)}\end{center} 12269Output : 12270\begin{center}{\tt (a\verb|^|2+-2*a*x+3*a+x\verb|^|2+-4*x+2)/2}\end{center} 12271Input : 12272\begin{center}{\tt laguerre(2,y)}\end{center} 12273Output : 12274\begin{center}{\tt (a\verb|^|2+-2*a*y+3*a+y\verb|^|2+-4*y+2)/2}\end{center} 12275Input : 12276\begin{center}{\tt laguerre(2,y,b)}\end{center} 12277Output : 12278\begin{center}{\tt (b\verb|^|2+-2*b*y+3*b+y\verb|^|2+-4*y+2)/2}\end{center} 12279 12280\subsection{Tchebychev polynomials of the first kind: {\tt tchebyshev1}}\index{tchebyshev1} 12281\noindent{\tt tchebyshev1} takes as argument an integer $n$ and optionally a 12282variable name (by default $x$).\\ 12283{\tt tchebyshev1} returns the Tchebychev polynomial of first kind 12284of degree $n$.\\ 12285The Tchebychev polynomial of first kind $T(n,x)$ is defined by 12286\[ T(n,x)= \cos(n \arccos(x)) \] 12287and satisfy the recurrence relation: 12288\[ T(0,x)=1, \quad 12289T(1,x)=x, \quad T(n,x)=2xT(n-1,x)-T(n-2,x) \] 12290The polynomials $T(n,x)$ are orthogonal for the scalar product 12291\[ <f,g>=\int_{-1}^{+1}\frac{f(x)g(x)}{\sqrt{1-x^2}}dx \] 12292Input : 12293\begin{center}{\tt tchebyshev1(4)}\end{center} 12294Output : 12295\begin{center}{\tt 8*x\verb|^|4+-8*x\verb|^|2+1}\end{center} 12296Input : 12297\begin{center}{\tt tchebyshev1(4,y)}\end{center} 12298Output : 12299\begin{center}{\tt 8*y\verb|^|4+-8*y\verb|^|2+1}\end{center} 12300Indeed 12301\begin{eqnarray*} 12302\cos( 4 x)&=&Re((\cos(x)+i \sin(x))^4) \\ 12303 &=&\cos(x)^4-6.\cos(x)^2 (1-\cos(x)^2)+((1-\cos(x)^2)^2 \\ 12304 &=&T(4,\cos(x)) 12305\end{eqnarray*} 12306 12307\subsection{Tchebychev polynomial of the second kind: {\tt tchebyshev2}}\index{tchebyshev2} 12308\noindent{\tt tchebyshev2} takes as argument an integer $n$ and optionally 12309a variable name (by default $x$).\\ 12310{\tt tchebyshev2} returns the Tchebychev polynomial of second kind 12311of degree $n$.\\ 12312The Tchebychev polynomial of second kind $U(n,x)$ is defined by: 12313$$U(n,x)=\frac{\sin((n+1).\arccos(x))}{\sin(\arccos(x))}$$ 12314or equivalently: 12315$$\sin((n+1)x)=\sin(x)*U(n,\cos(x))$$ 12316Then $U(n,x)$ satisfies the recurrence relation: 12317\[ U(0,x)=1, \quad 12318U(1,x)=2x, \quad 12319U(n,x)=2xU(n-1,x)-U(n-2,x) \] 12320The polynomials $U(n,x)$ are orthogonal for the scalar product 12321\[ <f,g>=\int_{-1}^{+1}f(x)g(x)\sqrt{1-x^2}dx \] 12322Input : 12323\begin{center}{\tt tchebyshev2(3)}\end{center} 12324Output : 12325\begin{center}{\tt 8*x\verb|^|3+-4*x}\end{center} 12326Input : 12327\begin{center}{\tt tchebyshev2(3,y)}\end{center} 12328Output : 12329\begin{center}{\tt 8*y\verb|^|3+-4*y}\end{center} 12330Indeed: 12331\[ \sin(4 x)=\sin(x)*(8*\cos(x)^3-4 \cos(x))=\sin(x)*U(3,\cos(x)) \] 12332 12333 12334 12335\section{Gr\"obner basis and Gr\"obner reduction} 12336 \subsection{Gr\"obner basis : {\tt gbasis}}\index{gbasis} 12337\label{sec:gbasis} 12338\noindent{\tt gbasis} takes at least two arguments 12339\begin{itemize} 12340\item a vector of multivariate polynomials 12341\item a vector of variables names, 12342\end{itemize} 12343Optional arguments may be used to specify the ordering and 12344algorithms. By default, the ordering is lexicographic (with respect to the 12345list of variable names ordering) 12346and the polynomials are written in decreasing power orders with 12347respect to this order. 12348For example, the output will be like 12349$...+x^2 y^4 z^3+x^2 y^3 z^4+...$ if the second argument is $[x,y,z]$ because 12350$(2,4,3)>(2,3,4)$ but the output would be like 12351$...+x^2 y^3z^4+x^2 y^4 z^3+...$ if the second argument is $[x,z,y]$.\\ 12352{\tt gbasis} returns a Gr\"obner basis of the polynomial ideal spanned 12353by these polynomials. 12354 12355{\bf Property}\\ 12356If $I$ is an ideal and if $(G_k)_{k \in K}$ is a Gr\"obner basis of this 12357ideal $I$ then, if $F$ is a non-zero polynomial in $I$, the greatest monomial 12358of $F$ is divisible by the greatest monomial of one of the $G_k$. 12359In other words, if you do an euclidean division of $F\neq 0$ 12360by the corresponding $G_k$, take the remainder of this division, do 12361again the same and so on, at some point you get a null remainder. 12362 12363Input : 12364\begin{center}{\tt gbasis([2*x*y-y\verb|^|2,x\verb|^|2-2*x*y],[x,y])} 12365\end{center} 12366Output : 12367\begin{center}{\tt 12368 [4*x\verb|^|2+-4*y\verb|^|2,2*x*y-y\verb|^|2,-(3*y\verb|^|3)]} 12369\end{center} 12370 12371As indicated above, {\tt gbasis} may have more than 2 arguments~: 12372\begin{itemize} 12373\item {\tt plex} (lexicographic only), {\tt tdeg} (total degree then 12374lexicographic order), 12375{\tt revlex} (total degree then inverse lexicographic order), to 12376specify an order on the monomials 12377({\tt plex} is the order by default), 12378\item {\tt with\_cocoa=true} or {\tt with\_cocoa=false}, if you want to use 12379the {\tt CoCoA} library to compute the Gr\"obner basis (recommended, 12380requires that {\tt CoCoA} support compiled in) 12381 12382\item {\tt with\_f5=true} or {\tt with\_f5=false} for using the F5 algorithm 12383of the {\tt CoCoA} library . 12384In this case the specified order is not used (the polynomials are 12385homogenized). 12386\end{itemize} 12387Input~: 12388\begin{center} 12389{\tt gbasis([x1+x2+x3,x1*x2+x1*x3+x2*x3,x1*x2*x3-1], [x1,x2,x3],tdeg,with\_cocoa=false)} 12390\end{center} 12391Output 12392\begin{center} 12393\verb|[x3^3-1,-x2^2-x2*x3-x3^2,x1+x2+x3]| 12394\end{center} 12395 12396\subsection{Gr\"obner reduction : {\tt greduce}}\index{greduce} 12397\noindent{\tt greduce} has three arguments : a multivariate 12398polynomial, 12399a vector made of polynomials which is supposed to be a Gr\"obner 12400basis, and a vector of variable names.\\ 12401{\tt greduce} returns the reduction of 12402the polynomial given as first argument 12403with respect to the Gr\"obner basis given as the second argument. 12404It is 0 if and only if the polynomial belongs to the ideal. 12405 12406Input: 12407\begin{center}{\tt greduce(x*y-1,[x\verb|^|2-y\verb|^|2,2*x*y-y\verb|^|2,y\verb|^|3],[x,y])}\end{center} 12408Output: 12409\begin{center}{\tt y\verb|^|2-2}\end{center} 12410that is to say $xy-1=\frac{1}{2}(y^2-2)\ \bmod I$ where $I$ is the ideal 12411generated by the Gr\"obner basis $[x^2-y^2,2xy-y^2,y^3]$, because 12412$ y^2-2$ is the euclidean division remainder of $2(xy-1)$ by $G_2=2x y-y^2$.\\ 12413% {\bf Remark}\\ 12414% The multiplcative constant can be fixed by observing how the constant 12415% coefficient is converted. In the example, the constant coefficient 12416% {\tt -1} is converted into the constant coefficient {\tt -2}, so 12417% the multiplcative constant is {\tt 1/2}. 12418 12419Like {\tt gbasis} (cf. \ref{sec:gbasis}), 12420{\tt greduce} may have more than 3 arguments to specify ordering and 12421algorithm if they differ from the default (lexicographic ordering).\\ 12422Input~: 12423\begin{center} 12424{\tt greduce(x1\verb|^|2*x3\verb|^|2,[x3\verb|^|3-1,-x2\verb|^|2-x2*x3-x3\verb|^|2,x1+x2+x3], [x1,x2,x3],tdeg)} 12425\end{center} 12426Output 12427\begin{center} 12428\verb|x2| 12429\end{center} 12430 12431\subsection{Test if a polynomial or list of polynomials belongs to an 12432ideal given by a Gr\"obner basis: \texttt{in\_ideal}}\index{in\_ideal} 12433 12434The \texttt{in\_ideal} command takes three mandatory arguments and one 12435optional argument. The mandatory arguments are a polynomial (or list 12436of polynomials), a list giving a Gr\"obner basis, and the list of 12437polynomial variables. The optional fourth argument can be an optional 12438argument from \texttt{gbasic} (see section \ref{sec:gbasis}), such as 12439\texttt{plex} or \texttt{tdeg}. By default it will be \texttt{plex}. 12440If a Gr\"obner basis is computed with a different order from the 12441default, then \texttt{in\_ideal} must use the same order.\\ 12442\texttt{in\_basis} returns the value \texttt{true} (1) or 12443\texttt{false} (0), 12444or a list of \texttt{true}s and \texttt{false}s, indicating whether or 12445not the polynomial(s) in the first argument are in the ideal generated 12446by the Gr\"obner basis in the second argument, using the variables 12447from the third argument.\\ 12448Input: 12449\begin{center} 12450 \tt 12451 in\_ideal((x+y)\^{}2,[y\^{}2,x\^{}2 + 2*x*y],[x,y]) 12452\end{center} 12453Output: 12454\begin{center} 12455 \tt 12456 1 12457\end{center} 12458Input: 12459\begin{center} 12460 \tt 12461 in\_ideal([(x+y)\^{}2,x+y],[y\^{}2,x\^{}2+2*x*y],[x,y]) 12462\end{center} 12463Output: 12464\begin{center} 12465 \tt 12466 [1,0] 12467\end{center} 12468Input: 12469\begin{center} 12470 \tt 12471 in\_ideal(x+y,[y\^{}2,x\^{}2+2*x*y],[x,y]) 12472\end{center} 12473Output: 12474\begin{center} 12475 \tt 12476 0 12477\end{center} 12478 12479\subsection{Build a polynomial from its evaluation : {\tt genpoly}}\i 12480ndex{genpoly} 12481\noindent{\tt genpoly} takes three arguments : a polynomial $P$ with $n-1$ 12482variables, an integer $b$ and the name of a variable {\tt var}.\\ 12483{\tt genpoly} returns the polynomial $Q$ with $n$ variables (the $P$ variables 12484and the variable {\tt var} given as second argument), such that~: 12485\begin{itemize} 12486\item {\tt subst(Q,var=b)==P} 12487\item the coefficients of $Q$ belongs to the interval $(-b/2 \ , \ b/2]$ 12488\end{itemize} 12489In other words, $P$ is written in base $b$ but using the convention 12490that the euclidean remainder belongs to $]-b/2 \ ; \ b/2]$ 12491(this convention is also known as s-mod representation). 12492Input : 12493\begin{center}{\tt genpoly(61,6,x) }\end{center} 12494Output : 12495\begin{center}{\tt 2*x\verb|^|2-2*x+1}\end{center} 12496Indeed 61 divided by 6 is 10 with remainder 1, then 10 divided by 6 is 2 12497with remainder -2 (instead of the usual quotient 1 and remainder 4 out of bounds), 12498\[ 61=2*6^2-2*6+1 \] 12499Input : 12500\begin{center}{\tt genpoly(5,6,x) }\end{center} 12501Output : 12502\begin{center}{\tt x-1}\end{center} 12503Indeed : $5=6-1$\\ 12504Input : 12505\begin{center}{\tt genpoly(7,6,x) }\end{center} 12506Output : 12507\begin{center}{\tt x+1}\end{center} 12508Indeed : $7=6+1$\\ 12509Input : 12510\begin{center}{\tt genpoly(7*y+5,6,x) }\end{center} 12511Output : 12512\begin{center}{\tt x*y+x+y-1}\end{center} 12513Indeed : $x*y+x+y-1=y(x+1)+(x-1)$\\ 12514Input : 12515\begin{center}{\tt genpoly(7*y+5*z\verb|^2|,6,x)}\end{center} 12516Output : 12517\begin{center}{\tt x*y+x*z+y-z}\end{center} 12518Indeed : $x*y+x*z+y-z=y*(x+1)+z*(x-1)$ 12519 12520\section{Rational fractions} 12521\subsection{Numerator : {\tt getNum}}\index{getNum}\label{sec:getnum} 12522\noindent {\tt getNum} takes as argument a rational fraction 12523and returns the numerator of this fraction. Unlike {\tt numer}, 12524{\tt getNum} does not simplify the fraction before extracting 12525the numerator.\\ 12526Input : 12527\begin{center}{\tt getNum((x\verb|^|2-1)/(x-1)) }\end{center} 12528Output : 12529\begin{center}{\tt x\verb|^|2-1}\end{center} 12530Input : 12531\begin{center}{\tt getNum((x\verb|^|2+2*x+1)/(x\verb|^|2-1)) }\end{center} 12532Output : 12533\begin{center}{\tt x\verb|^|2+2*x+1}\end{center} 12534 12535\subsection{Numerator after simplification : {\tt numer}}\index{numer}\label{sec:numer} 12536\noindent{\tt numer} takes as argument a rational fraction 12537and returns the numerator of the irreducible representation of 12538this fraction (see also \ref{sec:inumer}).\\ 12539Input : 12540\begin{center}{\tt numer((x\verb|^|2-1)/(x-1)) }\end{center} 12541Output : 12542\begin{center}{\tt x+1}\end{center} 12543Input : 12544\begin{center}{\tt numer((x\verb|^|2+2*x+1)/(x\verb|^|2-1)) }\end{center} 12545Output : 12546\begin{center}{\tt x+1}\end{center} 12547 12548\subsection{Denominator : {\tt getDenom}}\index{getDenom}\label{sec:getdenom} 12549\noindent{\tt getDenom} takes as argument a rational fraction and returns the 12550denominator of this fraction. Unlike {\tt denom}, 12551{\tt getDenom} does not simplify the fraction before extracting 12552the denominator.\\ 12553Input : 12554\begin{center}{\tt getDenom((x\verb|^|2-1)/(x-1)) }\end{center} 12555Output : 12556\begin{center}{\tt x-1}\end{center} 12557Input : 12558\begin{center}{\tt getDenom((x\verb|^|2+2*x+1)/(x\verb|^|2-1)) }\end{center} 12559Output : 12560\begin{center}{\tt x\verb|^|2-1}\end{center} 12561 12562\subsection{Denominator after simplification : {\tt denom}}\index{denom}\label{sec:denom} 12563\noindent{\tt denom} (or {\tt getDenom}) takes as argument a rational fraction 12564and returns the denominator of an irreducible representation 12565of this fraction (see also \ref{sec:idenom}).\\ 12566Input : 12567\begin{center}{\tt denom((x\verb|^|2-1)/(x-1)) }\end{center} 12568Output : 12569\begin{center}{\tt 1}\end{center} 12570Input : 12571\begin{center}{\tt denom((x\verb|^|2+2*x+1)/(x\verb|^|2-1)) }\end{center} 12572Output : 12573\begin{center}{\tt x-1}\end{center} 12574 12575\subsection{Numerator and denominator : {\tt f2nd fxnd}}\index{fxnd|textbf}\index{f2nd|textbf}\label{sec:fxnd} 12576\noindent{\tt f2nd} (or {\tt fxnd}) takes as argument a rational fraction and 12577returns the list of the numerator and the denominator of the irreducible 12578representation of this fraction (see also \ref{sec:ifxnd}).\\ 12579Input : 12580\begin{center}{\tt f2nd((x\verb|^|2-1)/(x-1)) }\end{center} 12581Output : 12582\begin{center}{\tt [x+1,1]}\end{center} 12583Input : 12584\begin{center}{\tt f2nd((x\verb|^|2+2*x+1)/(x\verb|^|2-1)) }\end{center} 12585Output : 12586\begin{center}{\tt [x+1,x-1]}\end{center} 12587 12588\subsection{Simplify : {\tt simp2}}\index{simp2}\label{sec:simp2} 12589\noindent{\tt simp2} takes as argument two polynomials (or two integers\ see 12590\ref{sec:isimp2}). 12591These two polynomials are seen as the numerator and denominator 12592of a rational fraction.\\ 12593{\tt simp2} returns a list of two polynomials seen as the numerator 12594and denominator of the irreducible representation of 12595this rational fraction.\\ 12596 Input : 12597\begin{center}{\tt simp2(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 12598Output : 12599\begin{center}{\tt [x\verb|^|2+x+1,x+1]}\end{center} 12600 12601\subsection{Common denominator : {\tt comDenom}}\index{comDenom|textbf} 12602\noindent{\tt comDenom} takes as argument a sum of rational fractions.\\ 12603{\tt comDenom} rewrite the sum as a unique rational fraction. 12604The denominator of this rational fraction is the common denominator of the 12605rational fractions given as argument.\\ 12606Input : 12607\begin{center}{\tt comDenom(x-1/(x-1)-1/(x\verb|^|2-1))}\end{center} 12608Output : 12609\begin{center}{\tt (x\verb|^|3+-2*x-2)/(x\verb|^|2-1)}\end{center} 12610 12611\subsection{Integer and fractional part : {\tt propfrac}}\index{propfrac}\label{sec:propfrac} 12612\noindent{\tt propfrac} takes as argument a rational fraction.\\ 12613{\tt propfrac} rewrites this rational fraction as the sum of its 12614integer part and proper fractional part.\\ 12615{\tt propfrac(A(x)/B(x))} writes the fraction $\frac{A(x)}{B(x)}$ (after 12616reduction), as : 12617\[ Q(x)+\frac{R(x)}{B(x)} \quad \mbox{ where } R(x)=0 12618\mbox{ or } 0\leq \mbox{degree}(R(x))< \mbox{degree}(B(x)) \] 12619Input : 12620\begin{center}{\tt propfrac((5*x+3)*(x-1)/(x+2))}\end{center} 12621Output : 12622\begin{center}{\tt 5*x-12+21/(x+2)}\end{center} 12623 12624\subsection{Partial fraction expansion : {\tt partfrac}}\index{partfrac|textbf}\label{sec:convertparf} 12625{\tt partfrac} takes as argument a rational fraction.\\ 12626{\tt partfrac} returns the partial fraction expansion of this rational 12627fraction.\\ 12628The {\tt partfrac} command is equivalent to the {\tt convert} command with 12629{\tt parfrac} (or {\tt partfrac} or {\tt fullparfrac}) as option 12630(see also \ref{sec:convert}).\\ 12631{\bf Example} :\\ 12632Find the partial fraction expansion of : 12633$$\frac{x^5-2x^3+1}{x^4-2x^3+2x^2-2x+1}$$ 12634Input : 12635\begin{center}{\tt partfrac((x\verb|^|5-2*x\verb|^|3+1)/(x\verb|^|4-2*x\verb|^|3+2*x\verb|^|2-2*x+1))}\end{center} 12636Output in real mode : 12637\begin{center}{\tt x+2-1/(2*(x-1))+(x-3)/(2*(x\verb|^|2+1)) }\end{center} 12638Output in complex mode: 12639\begin{center}{\tt x+2+(-1+2*i)/((2-2*i)*((i)*x+1))+1/(2*(-x+1))+}\end{center} 12640\begin{center}{\tt (-1-2*i)/((2-2*i)*(x+i))}\end{center} 12641 12642\subsection{Partial fraction expansion over $\C$: {\tt 12643cpartfrac}}\index{cpartfrac} 12644{\tt cpartfrac} takes as argument a rational fraction.\\ 12645{\tt cpartfrac} returns the partial fraction expansion of this rational 12646fraction over the complex numbers, whether \texttt{Xcas} is in real or 12647complex mode. 12648 12649{\bf Example} :\\ 12650Find the partial fraction expansion of : 12651$$\frac{x^5-2x^3+1}{x^4-2x^3+2x^2-2x+1}$$ 12652Input : 12653\begin{center}{\tt 12654cpartfrac((x\^{}5-2*x\^{}3+1)/(x\^{}4-2*x\^{}3+2*x\^{}2-2*x+1)) 12655}\end{center} 12656Output: 12657%x+2-1/2/(x-1)+(-1-2*i)/(2-2*i)/(x+i)+(2+i)/(2-2*i)/(x-i) 12658\begin{center}{\tt x+2+(-1+2*i)/((2-2*i)*((i)*x+1))+1/(2*(-x+1))+}\end{center} 12659\begin{center}{\tt (-1-2*i)/((2-2*i)*(x+i))}\end{center} 12660 12661\section{Exact roots of a polynomial} 12662\subsection{Exact bounds for complex roots of a polynomial : 12663{\tt complexroot}}\index{complexroot} 12664\noindent {\tt complexroot} takes 2 or 4 arguments : a polynomial and a real 12665number $\epsilon$ and optionally two complex numbers $\alpha,\beta$.\\ 12666{\tt complexroot} returns a list of vectors. 12667\begin{itemize} 12668\item If {\tt complexroot} has 2 arguments, 12669the elements of each vector are 12670\begin{itemize} 12671\item either an interval (the 12672boundaries of this interval are the opposite vertices of a rectangle with sides 12673parallel to the axis and containing a complex root of the polynomial) and the 12674multiplicity of this root.\\ 12675Let the interval be $[a_1+ib_1,a_2+ib_2]$ then $|a_1-a_2|<\epsilon$, 12676$|b_1-b_2|<\epsilon$ and the root $a+ib$ verifies 12677$a_1\leq a \leq a_2$ and $b_1\leq b \leq b_2$. 12678\item or the value of an exact complex root of 12679the polynomial and the multiplicity of this root 12680\end{itemize} 12681\item If {\tt complexroot} has 4 arguments, {\tt complexroot} returns a list of 12682vectors as above, but only for the roots lying in 12683the rectangle with sides parallel to the axis having $\alpha,\beta$ as 12684opposite vertices.\\ 12685\end{itemize} 12686To find the roots of $x^3+1$, input: 12687\begin{center}{\tt complexroot(x\verb|^|3+1,0.1)}\end{center} 12688Output : 12689\begin{center}{\tt [[-1,1],[[(4-7*i)/8,(8-13*i)/16],1],[[(8+13*i)/16,(4+7*i)/8],1]]}\end{center} 12690Hence, for $x^3+1$ : 12691\begin{itemize} 12692\item -1 is a root of multiplicity 1, 12693\item 1/2+i*$b$ is a root of multiplicity 1 with $-7/8\leq b \leq 12694 -13/16$, 12695\item 1/2+i*$c$ is a root of multiplicity 1 with $13/16\leq c \leq 12696 7/8$. 12697\end{itemize} 12698To find the roots of $x^3+1$ lying inside the rectangle 12699of opposite vertices $-1,1+2*i$, input: 12700\begin{center}{\tt complexroot(x\verb|^|3+1,0.1,-1,1+2*i)}\end{center} 12701Output : 12702\begin{center}{\tt [[-1,1],[[(8+13*i)/16,(4+7*i)/8],1]]}\end{center} 12703 12704\subsection{Exact bounds for real roots of a polynomial : {\tt realroot}}\index{realroot} 12705\noindent {\tt realroot} has 2 or 4 arguments : a polynomial and a real number 12706$\epsilon$ and optionally two reals numbers $\alpha,\beta$.\\ 12707{\tt realroot} returns a list of vectors. 12708\begin{itemize} 12709\item If {\tt realroot} has 2 arguments, the elements of each vector are 12710\begin{itemize} 12711\item 12712either a real interval containing a real root of the polynomial 12713and the multiplicity of this root. 12714Let the interval be $[a_1,a_2]$ then $|a_1-a_2|<\epsilon$ and 12715the root $a$ verifies $a_1\leq a \leq a_2$. 12716\item or the value of an exact real root of the 12717polynomial and the multiplicity of this root. 12718\end{itemize} 12719\item If {\tt realroot} has 4 arguments, {\tt realroot} returns a list of 12720vectors as above, but only for the roots inside 12721the interval $[\alpha,\beta]$. 12722\end{itemize} 12723To find the real roots of $x^3+1$, input: 12724\begin{center}{\tt realroot(x\verb|^|3+1, 0.1)}\end{center} 12725Output : 12726\begin{center}{\tt [[-1,1]] }\end{center} 12727To find the real roots of $x^3-x^2-2x+2$, input: 12728\begin{center}{\tt realroot(x\verb|^|3-x\verb|^|2-2*x+2, 0.1)}\end{center} 12729Output : 12730\begin{center}{\tt [[1,1],[[(-3)/2,(-45)/32],1],[[45/32,3/2],1]]}\end{center} 12731To find the real roots of $x^3-x^2-2x+2$ in the interval $[0;2]$, input: 12732\begin{center}{\tt realroot(x\verb|^|3-x\verb|^|2-2*x+2, 0.1,0,2)}\end{center} 12733Output : 12734\begin{center}{\tt [[1,1],[[11/8,23/16],1]]}\end{center} 12735 12736\subsection{Exact bounds for real roots of a polynomial: {\tt VAS}}\index{VAS} 12737The {\tt VAS} command takes one argument, a polynomial.\\ 12738{\tt VAS} returns a list of intervals which contain the real roots of 12739the polynomial using the Vincent-Akritas-Strzebonski algorithm. Each 12740interval will contain exactly one root.\\ 12741Input: 12742\begin{center} 12743 \tt 12744 VAS(x\^{}3 - 7*x + 7) 12745\end{center} 12746Output: 12747\begin{center} 12748 \tt 12749 [[-4,0],[1,3/2],[3/2,2]] 12750\end{center} 12751Input: 12752\begin{center} 12753 \tt 12754 VAS(x\^{}5 + 2*x\^{}4 - 6*x\^{}3 - 7*x\^{}2 + 7*x + 7) 12755\end{center} 12756Output: 12757\begin{center} 12758 \tt 12759[[-5,-1],-1,[1,3/2],[3/2,2]] 12760\end{center} 12761Input: 12762\begin{center} 12763 \tt 12764 VAS(x\^{}3 - x\^{}2 -2*x + 2) 12765\end{center} 12766Output: 12767\begin{center} 12768 \tt 12769 [[-3,0],1,[1,3]] 12770\end{center} 12771 12772\subsection{Exact bounds for positive real roots of a polynomial: {\tt 12773VAS\_positive}}\index{VAS\_positive} 12774The {\tt VAS\_positive} command takes one argument, a polynomial.\\ 12775{\tt VAS\_positive} returns a list of intervals which contain the 12776positive real roots of the polynomial using the 12777Vincent-Akritas-Strzebonski algorithm. Each interval will contain 12778exactly one interval.\\ 12779 12780Input: 12781\begin{center} 12782 \tt 12783 VAS\_positive(x\^{}3 - 7*x + 7) 12784\end{center} 12785Output: 12786\begin{center} 12787 \tt 12788 [[1,3/2],[3/2,2]] 12789\end{center} 12790Input: 12791\begin{center} 12792 \tt 12793 VAS\_positive(x\^{}5 + 2*x\^{}4 - 6*x\^{}3 - 7*x\^{}2 + 7*x + 7) 12794\end{center} 12795Output: 12796\begin{center} 12797 \tt 12798[[1,3/2],[3/2,2]] 12799\end{center} 12800Input: 12801\begin{center} 12802 \tt 12803 VAS\_positive(x\^{}3 - x\^{}2 -2*x + 2) 12804\end{center} 12805Output: 12806\begin{center} 12807 \tt 12808 [1,[1,3]] 12809\end{center} 12810 12811\subsection{An upper bound for the positive real roots of a 12812polynomial: {\tt posubLMQ}}\index{posubLMQ} 12813The {\tt posubLMQ} command takes one argument, a polynomial.\\ 12814{\tt posubLMQ} returns a (non-optimal) upper bound for the positive real 12815roots of the polynomial using the Local Max Quadratic (LMQ) 12816Akritas-Strzebonski-Vigklas algorithm.\\ 12817Input: 12818\begin{center} 12819 \tt 12820 posubLMQ(x\^{}3 - 7*x + 7) 12821\end{center} 12822Output: 12823\begin{center} 12824 \tt 128254 12826\end{center} 12827Input: 12828\begin{center} 12829 \tt 12830 posubLMQ(x\^{}5 + 2*x\^{}4 - 6*x\^{}3 - 7*x\^{}2 + 7*x + 7) 12831\end{center} 12832Output: 12833\begin{center} 12834 \tt 128354 12836\end{center} 12837Input: 12838\begin{center} 12839 \tt 12840 posubLMQ(x\^{}3 - x\^{}2 -2*x + 2) 12841\end{center} 12842Output: 12843\begin{center} 12844 \tt 128453 12846\end{center} 12847 12848\subsection{A lower bound for the positive real roots of a 12849polynomial: {\tt poslbdLMQ}}\index{poslbdLMQ} 12850The {\tt poslbdLMQ} command takes one argument, a polynomial.\\ 12851{\tt poslbdLMQ} returns a (non-optimal) lower bound for the positive real 12852roots of the polynomial using the Local Max Quadratic (LMQ) 12853Akritas-Strzebonski-Vigklas algorithm.\\ 12854Input: 12855\begin{center} 12856 \tt 12857 poslbdLMQ(x\^{}3 - 7*x + 7) 12858\end{center} 12859Output: 12860\begin{center} 12861 \tt 128621/2 12863\end{center} 12864Input: 12865\begin{center} 12866 \tt 12867 poslbdLMQ(x\^{}5 + 2*x\^{}4 - 6*x\^{}3 - 7*x\^{}2 + 7*x + 7) 12868\end{center} 12869Output: 12870\begin{center} 12871 \tt 128721/2 12873\end{center} 12874Input: 12875\begin{center} 12876 \tt 12877 poslbdLMQ(x\^{}3 - x\^{}2 -2*x + 2) 12878\end{center} 12879Output: 12880\begin{center} 12881 \tt 128821/2 12883\end{center} 12884 12885\subsection{Exact values of rational roots of a polynomial : 12886{\tt rationalroot}}\index{rationalroot} 12887\noindent {\tt rationalroot} takes 1 or 3 arguments : a polynomial and 12888optionally two real numbers $\alpha,\beta$. 12889\begin{itemize} 12890\item If {\tt rationalroot} has 1 argument, {\tt rationalroot} returns the list 12891of the value of the rational roots of the polynomial without multiplicity. 12892\item If {\tt rationalroot} has 3 arguments, {\tt rationalroot} returns only 12893the rational roots of the polynomial which are in the interval 12894$[\alpha,\beta]$. 12895\end{itemize} 12896To find the rational roots of $2*x^3-3*x^2-8*x+12$, input: 12897\begin{center}{\tt rationalroot(2*x\verb|^|3-3*x\verb|^|2-8*x+12)}\end{center} 12898Output : 12899\begin{center}{\tt [2,3/2,-2]}\end{center} 12900To find the rational roots of $2*x^3-3*x^2-8*x+12$ in $[1;2]$, input: 12901\begin{center}{\tt rationalroot(2*x\verb|^|3-3*x\verb|^|2-8*x+12,1,2)}\end{center} 12902Output : 12903\begin{center}{\tt [2,3/2]}\end{center} 12904To find the rational roots of $2*x^3-3*x^2+8*x-12$, input: 12905\begin{center}{\tt rationalroot(2*x\verb|^|3-3*x\verb|^|2+8*x-12)}\end{center} 12906Output : 12907\begin{center}{\tt [3/2]}\end{center} 12908To find the rational roots of $2*x^3-3*x^2+8*x-12$, input: 12909\begin{center}{\tt rationalroot(2*x\verb|^|3-3*x\verb|^|2+8*x-12)}\end{center} 12910Output : 12911\begin{center}{\tt [3/2]}\end{center} 12912To find the rational roots of $(3*x-2)^2*(2x+1)=18*x^3-15*x^2-4*x+4$, input: 12913\begin{center}{\tt rationalroot(18*x\verb|^|3-15*x\verb|^|2-4*x+4)}\end{center} 12914Output : 12915\begin{center}{\tt [(-1)/2,2/3]}\end{center} 12916 12917\subsection{Exact values of the complex rational roots of a polynomial: {\tt crationalroot}}\index{crationalroot} 12918\noindent {\tt crationalroot} takes 1 or 3 arguments : a polynomial and 12919optionally two complex numbers $\alpha,\beta$. 12920\begin{itemize} 12921\item If {\tt crationalroot} has 1 argument, {\tt crationalroot} returns the 12922list of the complex rational roots of the 12923polynomial without multiplicity. 12924\item if {\tt crationalroot} has 3 arguments, {\tt crationalroot} returns only 12925the complex rational roots of the 12926polynomial which are in the rectangle with sides parallel to the 12927axis having $[\alpha,\beta]$ as opposite vertices. 12928\end{itemize} 12929To find the rational complex roots of 12930$(x^2+4)*(2x-3)=2*x^3-3*x^2+8*x-12$, input : 12931\begin{center}{\tt crationalroot(2*x\verb|^|3-3*x\verb|^|2+8*x-12)}\end{center} 12932Output : 12933\begin{center}{\tt [2*i,3/2,-2*i]}\end{center} 12934 12935\section{Exact roots and poles} 12936\subsection{Roots and poles of a rational function : {\tt froot}}\index{froot} 12937\noindent{\tt froot} takes a rational function $F(x)$ as argument.\\ 12938{\tt froot} returns a vector whose components are the roots and the poles 12939of $F[x]$, each one followed by its multiplicity.\\ 12940If {\tt Xcas} can not find the exact values of the roots or poles, 12941it tries to find approximate values if $F(x)$ has numeric coefficients.\\ 12942Input : 12943\begin{center}{\tt froot((x\verb|^|5-2*x\verb|^|4+x\verb|^|3)/(x-2)) }\end{center} 12944Output : 12945\begin{center}{\tt [1,2,0,3,2,-1]}\end{center} 12946Hence, for $\displaystyle F(x)=\frac{x^5-2.x^4+x^3}{x-2}$ : 12947\begin{itemize} 12948\item $1$ is a root of multiplicity 2, 12949\item $0$ is a root of multiplicity 3, 12950\item $2$ is a pole of order 1. 12951\end{itemize} 12952Input : 12953\begin{center}{\tt froot((x\verb|^|3-2*x\verb|^|2+1)/(x-2)) }\end{center} 12954Output : 12955\begin{center}{\tt [1,1,(1+sqrt(5))/2,1,(1-sqrt(5))/2,1,2,-1]}\end{center} 12956{\bf Remark} : to have the complex roots and poles, check {\tt Complex} in 12957the {\tt cas} configuration (red button giving the state line).\\ 12958Input : 12959\begin{center}{\tt froot((x\verb|^|2+1)/(x-2)) }\end{center} 12960Output : 12961\begin{center}{\tt [-i,1,i,1,2,-1]}\end{center} 12962 12963\subsection{Rational function given by roots and poles : {\tt fcoeff}}\index{fcoeff} 12964\noindent{\tt fcoeff} has as argument a vector 12965whose components are the roots and poles of a rational function 12966$F[x]$, each one followed by its multiplicity.\\ 12967{\tt fcoeff} returns the rational function $F(x)$.\\ 12968Input : 12969\begin{center}{\tt fcoeff([1,2,0,3,2,-1]) }\end{center} 12970Output : 12971\begin{center}{\tt (x-1)\verb|^|2*x\verb|^|3/(x-2)}\end{center} 12972 12973 12974 12975\section{Computing in $\Z/p\Z$ or in $\Z/p\Z[x]$}\index{\%|textbf}\label{sec:modulaire} 12976The way to compute over $\Z/p\Z$ or over $\Z/p\Z[x]$ depends 12977on the syntax mode : 12978\begin{itemize} 12979\item In {\tt Xcas} mode, an object $n$ over $\Z/p\Z$ is written 12980$n \% p$. Some examples of input for 12981\begin{itemize} 12982\item an integer {\tt n} in $\Z/13\Z$\\ 12983{\tt n:=12\%13}. 12984\item a vector {\tt V} in $\Z/13\Z$ \\ 12985{\tt V:=[1,2,3]\%13} or 12986{\tt V:=[1\%13,2\%13,3\%13]}. 12987\item a matrix {\tt A} in $\Z/13\Z$ \\ 12988{\tt A:=[[1,2,3],[2,3,4]]\%13} or \\ 12989{\tt A:=[[1\%13,2\%13,3\%13],[[2\%13,3\%13,4\%13]]}. 12990\item 12991a polynomial {\tt A} in $\Z/13\Z[x]$ in symbolic representation\\ 12992{\tt A:=(2*x\verb|^|2+3*x-1)\%13} or \\ 12993{\tt A:=2\%13*x\verb|^|2+3\%13*x-1\%13}. 12994\item 12995a polynomial {\tt A} in $\Z/13\Z[x]$ in list representation\\ 12996{\tt A:=poly1[1,2,3]\%13} or 12997{\tt A:=poly1[1\%13,2\%13,3\%13]}. 12998\end{itemize} 12999To recover an object {\tt o} with integer coefficients instead of modular 13000coefficients, input {\tt o \% 0}. For example, input {\tt o:=4\%7} and 13001 {\tt o\%0},then output is {\tt -3}. 13002\item 13003In {\tt Maple} mode, integers modulo $p$ are represented like 13004usual integers instead of using specific modular integers. 13005To avoid confusion with normal commands, modular 13006commands are written with a capital letter (inert form) and followed 13007by the mod command (see also the next section). 13008\end{itemize} 13009{\bf Remark} 13010\begin{itemize} 13011\item For some commands in $\Z/p\Z$ or in $\Z/p\Z[x]$, {\tt p} must be 13012a prime integer. 13013\item The representation is the symmetric representation :\\ 13014{\tt 11\%13} returns {\tt -2\%13}. 13015\end{itemize} 13016 13017\subsection{Expand and reduce : {\tt normal}}\index{normal} 13018\noindent{\tt normal} takes as argument a polynomial expression.\\ 13019{\tt normal} expands and reduces this expression in $\Z/p\Z[x]$.\\ 13020Input : 13021\begin{center}{\tt normal(((2*x\verb|^|2+12)*( 5*x-4))\%13)}\end{center} 13022Output : 13023\begin{center}{\tt (-3\%13)*x\verb|^|3+(5\%13)*x\verb|^|2+(-5\%13)*x+4\%13}\end{center} 13024 13025\subsection{Addition in $\Z/p\Z$ or in $ \Z/p\Z[x]$ : {\tt +}}\index{+} 13026\noindent{\tt +} adds two integers in $\Z/p\Z$, or 13027two polynomials in $\Z/p\Z[x]$. For polynomial expressions, 13028use the {\tt normal} command to simplify\index{normal}.\\ 13029For integers in $\Z/p\Z$, input : 13030\begin{center}{\tt 3\%13+10\%13}\end{center} 13031Output : 13032\begin{center}{\tt 0\%13}\end{center} 13033For polynomials with coefficients in $\Z/p\Z$, input : 13034\begin{center}{\tt normal((11*x+5 )\% 13+(8*x+6)\%13)}\end{center} 13035or 13036\begin{center}{\tt normal(11\% 13*x+5\%13+8\% 13*x+6\%13)}\end{center} 13037Output : 13038\begin{center}{\tt (6\%13)*x+-2\%13}\end{center} 13039 13040\subsection{Subtraction in $\Z/p\Z$ or in $ \Z/p\Z[x]$ : {\tt -}}\index{-|textbf} 13041\noindent{\tt -} subtracts two integers in $\Z/p\Z$ or 13042two polynomials in $\Z/p\Z[x]$. For polynomial expressions, 13043use the {\tt normal} command to simplify\index{normal}.\\ 13044For integers in $\Z/p\Z$, input : 13045\begin{center}{\tt 31\%13-10\%13}\end{center} 13046Output : 13047\begin{center}{\tt -5\%13}\end{center} 13048For polynomials with coefficients in $\Z/p\Z$, input : 13049\begin{center}{\tt normal((11*x+5)\%13-(8*x+6)\%13)}\end{center} 13050or : 13051\begin{center}{\tt normal(11\% 13*x+5\%13-8\% 13*x+6\%13)}\end{center} 13052Output : 13053\begin{center}{\tt (3\%13)*x+-1\%13}\end{center} 13054 13055\subsection{Multiplication in $\Z/p\Z$ or in $ \Z/p\Z[x]$ : {\tt *}}\index{*} 13056\noindent {\tt *} multiplies two integers in $\Z/p\Z$ or 13057two polynomials in $\Z/p\Z[x]$. For polynomial expressions, 13058use the {\tt normal} command to simplify\index{normal}.\\ 13059For integers in $\Z/p\Z$, input : 13060\begin{center}{\tt 31\%13*10\%13}\end{center} 13061Output : 13062\begin{center}{\tt -2\%13}\end{center} 13063For polynomials with coefficients in $\Z/p\Z$, input : 13064\begin{center}{\tt normal((11*x+5)\%13*(8*x+6 )\% 13)}\end{center} 13065or : 13066\begin{center}{\tt normal((11\% 13*x+5\%13)*(8\% 13*x+6\%13))}\end{center} 13067Output : 13068\begin{center}{\tt (-3\%13)*x\verb|^|2+(2\%13)*x+4\%13}\end{center} 13069 13070\subsection{Euclidean quotient : {\tt quo}}\index{quo} 13071\noindent{\tt quo} takes as arguments 13072two polynomials $A$ and $B$ with coefficients in $\Z/p\Z$, where 13073$A$ and $B$ are list polynomials or symbolic polynomials with 13074respect to $x$ or to an optional third argument.\\ 13075{\tt quo} returns the quotient of the euclidean division 13076of $A$ by $B$ in $\Z/p\Z[x]$.\\ 13077Input : 13078\begin{center}{\tt quo((x\verb|^|3+x\verb|^|2+1)\%13,(2*x\verb|^|2+4)\%13)}\end{center} 13079or : 13080\begin{center}{\tt quo((x\verb|^|3+x\verb|^|2+1,2*x\verb|^|2+4)\%13)}\end{center} 13081Output: 13082\begin{center}{\tt (-6\%13)*x+-6\%13}\end{center} 13083Indeed $\displaystyle x^3+x^2+1=(2x^2+4)(\frac{x+1}{2})+\frac{5x-4}{4}$ 13084and $-3*4=-6*2=1 \ \bmod 13$. 13085 13086\subsection{Euclidean remainder : {\tt rem}}\index{rem} 13087\noindent{\tt rem} takes as arguments 13088two polynomials $A$ and $B$ with coefficients in $\Z/p\Z$, where 13089$A$ and $B$ are list polynomials or symbolic polynomials with 13090respect to $x$ or to an optional third argument.\\ 13091{\tt rem} returns the remainder of the euclidean division 13092of $A$ by $B$ in $\Z/p\Z[x]$.\\ 13093Input : 13094\begin{center}{\tt rem((x\verb|^|3+x\verb|^|2+1)\%13,(2*x\verb|^|2+4)\%13)}\end{center} 13095or : 13096\begin{center}{\tt rem((x\verb|^|3+x\verb|^|2+1,2*x\verb|^|2+4)\%13)}\end{center} 13097Output: 13098\begin{center}{\tt (-2\%13)*x+-1\%13}\end{center} 13099Indeed $\displaystyle x^3+x^2+1=(2x^2+4)(\frac{x+1}{2})+\frac{5x-4}{4}$ 13100and $-3*4=-6*2=1 \ \bmod 13$. 13101 13102\subsection{Euclidean quotient and euclidean remainder : {\tt quorem}}\index{quorem} 13103\noindent{\tt quorem} takes as arguments 13104two polynomials $A$ and $B$ with coefficients in $\Z/p\Z$, where 13105$A$ and $B$ are list polynomials or symbolic polynomials with 13106respect to $x$ or to an optional third argument.\\ 13107{\tt quorem} returns the list of the quotient and remainder of the 13108euclidean division of $A$ by $B$ in $\Z/p\Z[x]$ 13109(see also \ref{sec:iquorem} and \ref{sec:quorem}).\\ 13110Input : 13111\begin{center}{\tt quorem((x\verb|^|3+x\verb|^|2+1)\%13,(2*x\verb|^|2+4)\%13)}\end{center} 13112or : 13113\begin{center}{\tt quorem((x\verb|^|3+x\verb|^|2+1,2*x\verb|^|2+4)\%13)}\end{center} 13114Output: 13115\begin{center}{\tt [(-6\%13)*x+-6\%13,(-2\%13)*x+-1\%13]}\end{center} 13116Indeed 13117$\displaystyle x^3+x^2+1=(2x^2+4)(\frac{x+1}{2})+\frac{5x-4}{4}$\\ 13118and $-3*4=-6*2=1 \ \bmod 13$. 13119 13120\subsection{Division in $\Z/p\Z$ or in $\Z/p\Z[x]$ : {\tt /}}\index{/} 13121\noindent{\tt /} divides two integers in $\Z/p\Z$ or 13122two polynomials $A$ and $B$ in $\Z/p\Z[x]$.\\ 13123For polynomials, the result is the irreducible representation 13124of the fraction $\frac{A}{B}$ in $\Z/p\Z[x]$.\\ 13125For integers in $\Z/p\Z$, input : 13126\begin{center}{\tt 5\%13/2\% 13}\end{center} 13127Since $2$ is invertible in $Z/13\Z$, we get the output : 13128\begin{center}{\tt -4\%13}\end{center} 13129For polynomials with coefficients in $\Z/p\Z$, input : 13130\begin{center}{\tt (2*x\verb|^|2+5)\%13/(5*x\verb|^|2+2*x-3)\%13}\end{center} 13131Output : 13132\begin{center}{\tt ((6\%13)*x+1\%13)/((2\%13)*x+2\%13)}\end{center} 13133 13134\subsection{Power in $\Z/p\Z$ and in $\Z/p\Z[x]$ : {\tt \^\ }}\index{\^\ } 13135To compute {\tt a} to the power {\tt n} in $\Z/p\Z$, we use the operator 13136\verb|^|. {\tt Xcas} implementation is the binary power algorithm.\\ 13137Input : 13138\begin{center}{\tt (5\%13)\verb|^|2}\end{center} 13139Output : 13140\begin{center}{\tt -1\%13}\end{center} 13141To compute {\tt A} to the power {\tt n} in $\Z/p\Z[x]$, we use the operator 13142\verb|^| and the {\tt normal} command \index{normal}.\\ 13143Input : 13144\begin{center}{\tt normal(((2*x+1)\%13)\verb|^|5)}\end{center} 13145Output : 13146\begin{center}{\tt (6\%13)*x\verb|^|5+(2\%13)*x\verb|^|4+(2\%13)*x\verb|^|3+(1\%13)*x\verb|^|2+(-3\%13)*x+1\%13}\end{center} 13147because 13148 $10=-3 \ (\bmod\ 13), \ \ 40=1\ (\bmod\ 13),\ \ 80=2 \ (\bmod\ 13),\ \ 32=6\ (\bmod\ 13)$. 13149 13150\subsection{Compute $a^n\ \bmod \ p$ : {\tt powmod powermod}}\index{powmod}\index{powermod} 13151\noindent{\tt powmod} (or {\tt powermod}) takes as argument $a,n,p$.\\ 13152{\tt powmod} (or {\tt powermod}) returns $a^n\ \bmod \ p$ in $[0;p-1]$.\\ 13153Input : 13154\begin{center}{\tt powmod(5,2,13)}\end{center} 13155Output : 13156\begin{center}{\tt 12}\end{center} 13157Input : 13158\begin{center}{\tt powmod(5,2,12)}\end{center} 13159Output : 13160\begin{center}{\tt 1}\end{center} 13161 13162\subsection{Inverse in $\Z/p\Z$ : {\tt inv inverse} or {\tt /}}\index{/}\index{inv} 13163To compute the inverse of an integer {\tt n} in $\Z/p\Z$, input {\tt 1/n\%p} 13164or {\tt inv(n\%p)} or {\tt inverse(n\%p)}.\\ 13165Input : 13166\begin{center}{\tt inv(3\%13) }\end{center} 13167Output : 13168\begin{center}{\tt -4\%13}\end{center} 13169Indeed $3\times-4=-12=1\ (\bmod\ 13)$. 13170 13171\subsection{Rebuild a fraction from its value modulo $p$ : {\tt 13172fracmod iratrecon}}\index{fracmod}\index{iratrecon} 13173\noindent {\tt fracmod} (or \texttt{iratrecon} for Maple 13174compatibility) takes two arguments, an integer $n$ 13175(representing a fraction) and an integer $p$ (the modulus).\\ 13176If possible, {\tt fracmod} returns a fraction $a/b$ such that 13177\[ -\frac{\sqrt{p}}{2} < a \leq \frac{\sqrt{p}}{2}, \quad 13178 0 \leq b < \frac{\sqrt{p}}{2}, \quad 13179 n \times b =a \pmod p \] 13180In other words $n=a/b\pmod p$.\\ 13181Input : 13182\begin{center}{\tt fracmod(3,13) }\end{center} 13183Output : 13184\begin{center}{\tt -1/4}\end{center} 13185Indeed : $3*-4=-12=1\ (\bmod\ 13)$, hence $3=-1/4\%13$.\\ 13186Input : 13187\begin{center}{\tt fracmod(13,121)}\end{center} 13188Output : 13189\begin{center}{\tt -4/9}\end{center} 13190Indeed : $13\times-9=-117=4\ (\bmod\ 121)$ hence $13=-4/9\%13$. 13191 13192\subsection{GCD in $\Z/p\Z[x]$ : {\tt gcd}}\index{gcd}\label{sec:gcdm} 13193\noindent {\tt gcd} takes as arguments two polynomials with 13194coefficients in $\Z/p\Z$ ($p$ must be prime).\\ 13195{\tt gcd} returns the GCD of these polynomials 13196computed in $\Z/p\Z[x]$ (see also 13197\ref{sec:gcd} for polynomials with non modular coefficients).\\ 13198Input : 13199\begin{center}{\tt gcd((2*x\verb|^|2+5)\%13,(5*x\verb|^|2+2*x-3)\%13)}\end{center} 13200Output : 13201\begin{center}{\tt (-4\%13)*x+5\%13}\end{center} 13202Input : 13203\begin{center}{\tt gcd((x\verb|^|2+2*x+1,x\verb|^|2-1)) mod 5)}\end{center} 13204Output : 13205\begin{center}{\tt x\%5}\end{center} 13206Note the difference with a gcd computation in $\Z[X]$ followed 13207by a reduction modulo 5, input: 13208\begin{center}{\tt gcd(x\verb|^|2+2*x+1,x\verb|^|2-1) mod 5}\end{center} 13209Output : 13210\begin{center}{\tt 1}\end{center} 13211 13212\subsection{Factorization over $\Z/p\Z[x]$ : {\tt factor factoriser}}\index{factor}\index{factoriser} 13213\noindent{\tt factor} takes as argument a polynomial 13214with coefficients in $\Z/p\Z[x]$.\\ 13215{\tt factor} factorizes this polynomial in $\Z/p\Z[x]$ ($p$ must 13216be prime).\\ 13217Input : 13218\begin{center}{\tt factor((-3*x\verb|^|3+5*x\verb|^|2-5*x+4)\%13)}\end{center} 13219Output : 13220\begin{center}{\tt ((1\%13)*x+-6\%13)*((-3\%13)*x\verb|^|2+-5\%13)}\end{center} 13221 13222\subsection{Determinant of a matrix in $\Z/p\Z$ : {\tt det}}\index{det} 13223\noindent{\tt det} takes as argument a matrix $A$ with coefficients in 13224$Z/pZ$.\\ 13225{\tt det} returns the determinant of this matrix $A$.\\ 13226Computations are done in $\Z/p\Z$ by Gauss reduction.\\ 13227Input : 13228\begin{center}{\tt det([[1,2,9]\%13,[3,10,0]\%13,[3,11,1]\%13])}\end{center} 13229or : 13230\begin{center}{\tt det([[1,2,9],[3,10,0],[3,11,1]]\%13)}\end{center} 13231Output : 13232\begin{center}{\tt 5\%13}\end{center} 13233hence, in $\Z/13\Z$, the determinant of 13234$A=[[1,2,9],[3,10,0],[3,11,1]]$ is {\tt 5\%13} (in $\Z$, {\tt det(A)=31}). 13235 13236\subsection{Inverse of a matrix with coefficients in $\Z/p\Z$ : {\tt inv inverse}}\index{inv}\index{inverse} 13237\noindent{\tt inverse} (or {\tt inv}) takes as argument a matrix $A$ in 13238$\Z/p\Z$.\\ 13239{\tt inverse} (or {\tt inv}) returns the inverse of the matrix 13240$A$ in $Z/p\Z$.\\ 13241Input : 13242\begin{center}{\tt inverse([[1,2,9]\%13,[3,10,0]\%13,[3,11,1]\%13])}\end{center} 13243or : 13244\begin{center}{\tt inv([[1,2,9]\%13,[3,10,0]\%13,[3,11,1]\%13])}\end{center} 13245or : 13246\begin{center}{\tt inverse([[1,2,9],[3,10,0],[3,11,1]]\%13)}\end{center} 13247or : 13248\begin{center}{\tt inv([[1,2,9],[3,10,0],[3,11,1]]\%13)}\end{center} 13249Output : 13250\begin{center}{\tt [[2\%13,-4\%13,-5\%13],[2\%13,0\%13,-5\%13], [-2\%13,-1\%13,6\%13]]}\end{center} 13251it is the inverse of $A=[[1,2,9],[3,10,0],[3,11,1]]$ in $\Z/13\Z$. 13252 13253\subsection{Row reduction to echelon form in $\Z/p\Z$ : {\tt rref}}\index{rref}\label{sec:rrefm} 13254\noindent{\tt rref} finds the row reduction to echelon form of 13255a matrix with coefficients in $\Z/p\Z$. 13256 13257This may be used 13258to solve a linear system of equations with coefficients in $\Z/p\Z$ 13259by rewriting it in matrix form (see also \ref{sec:rref}) : 13260\begin{center}{\tt A*X=B}\end{center} 13261{\tt rref} takes as argument the augmented matrix 13262of the system (the matrix obtained by augmenting matrix {\tt A} to the 13263right with the column vector {\tt B}).\\ 13264{\tt rref} returns a matrix {\tt [A1,B1]} : {\tt A1} has 1 on 13265its principal diagonal, and zeros outside, and the 13266solutions in $\Z/p\Z$, of : 13267\begin{center}{\tt A1*X=B1}\end{center} 13268are the same as the solutions of: 13269\begin{center}{\tt A*X=B}\end{center} 13270Example, solve in $\Z/13\Z$ 13271$$\left \{\begin{array}{lcr}\ \ x\ +\ \ 2 \cdot y & = &9 \\3 \cdot x +10 \cdot y & =& 0 \end{array}\right.$$ 13272Input : 13273\begin{center}{\tt rref([[1, 2, 9]\%13,[3,10,0]\%13])}\end{center} 13274or : 13275\begin{center}{\tt rref([[1, 2, 9],[3,10,0]])\%13}\end{center} 13276Output : 13277\begin{center}{\tt [[1\%13,0\%13,3\%13],[0\%13,1\%13,3\%13]]}\end{center} 13278hence {\tt x=3\%13} and {\tt y=3\%13}. 13279 13280\subsection{Construction of a Galois field : {\tt GF}}\index{GF} 13281\noindent{\tt GF} takes as arguments a prime integer $p$ 13282and an integer $n>1$.\\ 13283{\tt GF} returns a Galois field of characteristic $p$ having $p^n$ 13284elements.\\ 13285Elements of the field and the field itself 13286are represented by {\tt GF(...)} where {\tt ...} is the following 13287sequence: 13288\begin{itemize} 13289\item the characteristic $p$ ($px=0$), 13290\item an irreducible primitive minimal polynomial generating an 13291ideal $I$ in $\Z/p\Z[X]$, the Galois field being the quotient 13292of $\Z/p\Z[X]$ by $I$, 13293\item the name of the polynomial variable, by default {\tt x}, 13294\item a polynomial (a remainder modulo the minimal polynomial) 13295for an element of the field 13296(field elements are represented with the additive representation) 13297or {\tt undef} for the field itself. 13298\end{itemize} 13299You should give a name to this field (for example {\tt G:=GF(p,n)}), 13300in order to build elements of the field from a polynomial in 13301$\Z/p\Z[X]$, for example {\tt G(x\verb|^|3+x)}. Note that {\tt G(x)} 13302is a generator of the multiplicative group {\tt $G^*$}.\\ 13303Input : 13304\begin{center}{\tt G:=GF(2,8)}\end{center} 13305Output : 13306\begin{center}{\tt GF(2,x\verb|^|8-x\verb|^|6-x\verb|^|4-x\verb|^|3-x\verb|^|2-x-1,x,undef)}\end{center} 13307The field $G$ has $2^8=256$ elements and 13308$x$ generates the multiplicative group 13309of this field ($\{ 1,x,x^2,...x^{254} \}$).\\ 13310Input : 13311\begin{center}{\tt G(x\verb|^|9)}\end{center} 13312Output : 13313\begin{center}{\tt GF(2,x\verb|^|8-x\verb|^|6-x\verb|^|4-x\verb|^|3-x\verb|^|2-x-1,x,x\verb|^|7+x\verb|^|5+x\verb|^|4+x\verb|^|3+x\verb|^|2+x)}\end{center} 13314indeed $x^8=x^6+x^4+x^3+x^2+x+1$, hence $x^9=x^7+x^5+x^4+x^3+x^2+x$.\\ 13315Input : 13316\begin{center}{\tt G(x)\verb|^|255}\end{center} 13317Output should be the unit, indeed: 13318\begin{center} 13319{\tt GF(2,x\verb|^|8-x\verb|^|6-x\verb|^|4-x\verb|^|3-x\verb|^|2-x-1,x,1)}\end{center} 13320As one can see in these examples, the output contains many times the same 13321information that you would prefer not to see 13322if you work many times with the same field. For this reason, 13323the definition of a Galois field may have an optional argument, 13324a variable name which will be used thereafter to represent elements 13325of the field. Since you will also most 13326likely want to modify the name of the indeterminate, the field 13327name is grouped with the variable name in a list 13328passed as third argument to {\tt GF}. 13329Note that these two variable names must be quoted.\\ 13330Example,\\ 13331Input : 13332\begin{center}{\tt G:=GF(2,2,['w','G']):; G(w\verb|^|2)}\end{center} 13333Output : 13334\begin{center}{\tt Done, G(w+1)}\end{center} 13335Input : 13336\begin{center}{\tt G(w\verb|^|3)}\end{center} 13337Output : 13338\begin{center}{\tt G(1)}\end{center} 13339Hence, the elements of {\tt GF(2,2)} are 13340{\tt G(0),G(1),G(w),G(w\verb|^|2)=G(w+1)}. 13341 13342We may also impose the irreducible primitive polynomial that we wish 13343to use, by putting it as second argument (instead of $n$), 13344for example : 13345\begin{center}\verb|G:=GF(2,w^8+w^6+w^3+w^2+1,['w','G'])|\end{center} 13346If the polynomial is not primitive, {\tt Xcas} will replace it 13347automatically by a primitive polynomial, for example :\\ 13348Input : 13349\begin{center}\verb|G:=GF(2,w^8+w^7+w^5+w+1,['w','G'])|\end{center} 13350Output : 13351\begin{center}\verb|G:=GF(2,w^8-w^6-w^3-w^2-1,['w','G'],undef)|\end{center} 13352 13353\subsection{Factorize a polynomial with coefficients in a Galois field : {\tt factor}}\index{factor} 13354\noindent{\tt factor} can also factorize a univariate 13355polynomial with coefficients in a Galois field.\\ 13356Input for example to have {\tt G=}$\mathbb F_4$:\\ 13357\begin{center}{\tt G:=GF(2,2,['w','G'])}\end{center} 13358Output : 13359\begin{center}{\tt GF(2,w\verb|^|2+w+1,[w,G],undef)}\end{center} 13360Input for example : 13361\begin{center}{\tt a:=G(w)}\end{center} 13362\begin{center}{\tt factor(a\verb|^|2*x\verb|^|2+1)}\end{center} 13363Output : 13364\begin{center}{\tt (G(w+1))*(x+G(w+1))\verb|^|2}\end{center} 13365 13366\section{Compute in $\Z/p\Z[x]$ using Maple syntax}\index{mod}\index{\%}\label{sec:modulmap} 13367\subsection{Euclidean quotient : {\tt Quo}}\index{Quo} 13368\noindent {\tt Quo} is the inert form of {\tt quo}.\\ 13369{\tt Quo} returns the euclidean quotient between two polynomials 13370 without evaluation.\\ 13371It is used in conjunction with {\tt mod} in Maple syntax mode to compute 13372the euclidean quotient of the division of two 13373polynomials with coefficients in $\Z/p\Z$.\\ 13374Input in {\tt Xcas} mode: 13375\begin{center}{\tt Quo((x\verb|^|3+x\verb|^|2+1) mod 13,(2*x\verb|^|2+4) mod 13)}\end{center} 13376Output : 13377\begin{center}{\tt quo((x\verb|^|3+x\verb|^|2+1)\%13,(2*x\verb|^|2+4)\%13)}\end{center} 13378you need to {\tt eval(ans())} to get : 13379\begin{center}{\tt (-6\%13)*x+-6\%13}\end{center} 13380Input in {\tt Maple} mode : 13381\begin{center}{\tt Quo(x\verb|^|3+x\verb|^|2+1,2*x\verb|^|2+4) mod 13}\end{center} 13382Output : 13383\begin{center}{\tt (-6)*x-6}\end{center} 13384Input in {\tt Maple} mode : 13385\begin{center}{\tt Quo(x\verb|^|2+2*x,x\verb|^|2+6*x+5) mod 5}\end{center} 13386Output : 13387\begin{center}{\tt 1}\end{center} 13388 13389\subsection{Euclidean remainder: {\tt Rem}}\index{Rem} 13390\noindent{\tt Rem} is the inert form of {\tt rem}.\\ 13391{\tt Rem} returns the euclidean remainder between two polynomials 13392without evaluation. 13393It is used in conjunction with {\tt mod} in Maple syntax mode to compute 13394the euclidean remainder of the division of two 13395polynomials with coefficients in $\Z/p\Z$.\\ 13396Input in {\tt Xcas} mode : 13397\begin{center}{\tt Rem((x\verb|^|3+x\verb|^|2+1) mod 13,(2*x\verb|^|2+4) mod 13)}\end{center} 13398Output : 13399\begin{center}{\tt rem((x\verb|^|3+x\verb|^|2+1)\%13,(2*x\verb|^|2+4)\%13)}\end{center} 13400you need to {\tt eval(ans())} to get : 13401\begin{center}{\tt (-2\%13)*x+-1\%13}\end{center} 13402Input in {\tt Maple} mode : 13403\begin{center}{\tt Rem(x\verb|^|3+x\verb|^|2+1,2*x\verb|^|2+4) mod 13}\end{center} 13404Output : 13405\begin{center}{\tt (-2)*x-1}\end{center} 13406Input in {\tt Maple} mode : 13407\begin{center}{\tt Rem(x\verb|^|2+2*x,x\verb|^|2+6*x+5) mod 5}\end{center} 13408Output : 13409\begin{center}{\tt 1*x}\end{center} 13410 13411\subsection{GCD in $\Z/p\Z[x]$ : {\tt Gcd}}\index{Gcd} 13412\noindent{\tt Gcd} is the inert form of {\tt gcd}.\\ 13413{\tt Gcd} returns the gcd (greatest common divisor) of two polynomials 13414(or of a list of polynomials or of a sequence of polynomials) without 13415evaluation.\\ 13416It is used in conjunction with {\tt mod} in Maple syntax mode to compute 13417the gcd of two polynomials with coefficients in $\Z/p\Z$ with $p$ prime 13418(see also \ref{sec:gcd}).\\ 13419Input in {\tt Xcas} mode : 13420\begin{center}{\tt Gcd((2*x\verb|^|2+5,5*x\verb|^|2+2*x-3)\%13)}\end{center} 13421Output : 13422\begin{center}{\tt gcd((2*x\verb|^|2+5)\%13,(5*x\verb|^|2+2*x-3)\%13)}\end{center} 13423you need to {\tt eval(ans())} to get : 13424\begin{center}{\tt (1\%13)*x+2\%13}\end{center} 13425Input in {\tt Maple} mode : 13426\begin{center}{\tt Gcd(2*x\verb|^|2+5,5*x\verb|^|2+2*x-3) mod 13}\end{center} 13427Output : 13428\begin{center}{\tt 1*x+2}\end{center} 13429Input: 13430\begin{center}{\tt Gcd(x\verb|^|2+2*x,x\verb|^|2+6*x+5) mod 5}\end{center} 13431Output : 13432\begin{center}{\tt 1*x}\end{center} 13433 13434\subsection{Factorization in $\Z/p\Z[x]$ : {\tt Factor}}\index{Factor} 13435\noindent{\tt Factor} is the inert form of {\tt factor}.\\ 13436{\tt Factor} takes as argument a polynomial.\\ 13437{\tt Factor} returns {\tt factor} without evaluation. 13438It is used in conjunction with {\tt mod} in Maple syntax mode to 13439factorize a polynomial with coefficients in $\Z/p\Z$ 13440where $p$ must be prime.\\ 13441Input in {\tt Xcas} mode : 13442\begin{center}{\tt Factor((-3*x\verb|^|3+5*x\verb|^|2-5*x+4)\%13)}\end{center} 13443Output : 13444\begin{center}{\tt factor((-3*x\verb|^|3+5*x\verb|^|2-5*x+4)\%13)}\end{center} 13445you need to {\tt eval(ans())} to get : 13446\begin{center}{\tt ((1\%13)*x+-6\%13)*((-3\%13)*x\verb|^|2+-5\%13)}\end{center} 13447Input in {\tt Maple} mode : 13448\begin{center}{\tt Factor(-3*x\verb|^|3+5*x\verb|^|2-5*x+4) mod 13}\end{center} 13449Output : 13450\begin{center}{\tt -3*(1*x-6)*(1*x\verb|^|2+6)}\end{center} 13451 13452\subsection{Determinant of a matrix with coefficients in $\Z/p\Z$ : {\tt Det}}\index{Det} 13453\noindent{\tt Det} is the inert form of {\tt det}.\\ 13454{\tt Det} takes as argument a matrix with coefficients in $\Z/p\Z$.\\ 13455{\tt Det} returns {\tt det} without evaluation. 13456It is used in conjunction with {\tt mod} in Maple syntax mode to 13457find the determinant of a matrix with coefficients in $\Z/p\Z$.\\ 13458Input in {\tt Xcas} mode : 13459\begin{center}{\tt Det([[1,2,9] mod 13,[3,10,0] mod 13,[3,11,1] mod 13])}\end{center} 13460Output : 13461\begin{center}{\tt det([[1\%13,2\%13,-4\%13],[3\%13,-3\%13,0\%13], [3\%13,-2\%13,1\%13]])}\end{center} 13462you need to {\tt eval(ans())} to get : 13463\begin{center}{\tt 5\%13}\end{center} 13464hence, in $\Z/13\Z$, the determinant of 13465$A=[[1, 2, 9],[3,10,0],[3,11,1]]$ is {\tt 5\%13} (in $\Z$, {\tt det(A)=31}).\\ 13466Input in {\tt Maple} mode : 13467\begin{center}{\tt Det([[1,2,9],[3,10,0],[3,11,1]]) mod 13}\end{center} 13468Output : 13469\begin{center}{\tt 5}\end{center} 13470 13471\subsection{Inverse of a matrix in $\Z/p\Z$ : {\tt Inverse}}\index{Inverse} 13472\noindent{\tt Inverse} is the inert form of {\tt inverse}.\\ 13473{\tt Inverse} takes as argument a matrix with coefficients in $\Z/p\Z$.\\ 13474{\tt Inverse} returns {\tt inverse} without evaluation. 13475It is used in conjunction with {\tt mod} in Maple syntax mode to 13476find the inverse of a matrix with coefficients in $\Z/p\Z$.\\ 13477Input in {\tt Xcas} mode : 13478\begin{center}{\tt Inverse([[1,2,9] mod 13,[3,10,0] mod 13,[3,11,1] mod13])}\end{center} 13479Output : 13480\begin{center}{\tt inverse([[1\%13,2\%13,9\%13],[3\%13,10\%13,0\%13], [3\%13,11\%13,1\%13]])}\end{center} 13481you need to {\tt eval(ans())} to get : 13482\begin{center}{\tt [[2\%13,-4\%13,-5\%13],[2\%13,0\%13,-5\%13], [-2\%13,-1\%13,6\%13]]}\end{center} 13483which is the inverse of $A=[[1,2,9],[3,10,0],[3,11,1]]$ in $\Z/13\Z$.\\ 13484Input in {\tt Maple} mode : 13485\begin{center}{\tt Inverse([[1,2,9],[3,10,0],[3,11,1]]) mod 13}\end{center} 13486Output : 13487\begin{center}{\tt [[2,-4,-5],[2,0,-5],[-2,-1,6]]}\end{center} 13488 13489\subsection{Row reduction to echelon form in $\Z/p\Z$ : {\tt Rref}}\index{Rref} 13490\noindent{\tt Rref} is the inert form of {\tt rref}.\\ 13491{\tt Rref} returns {\tt rref} without 13492evaluation. 13493It is used in conjunction with {\tt mod} in Maple syntax mode to 13494find the row reduction to echelon form 13495of a matrix with coefficients in $\Z/p\Z$ (see 13496also \ref{sec:rref}).\\ 13497Example, solve in $\Z/13\Z$ 13498$$\left \{\begin{array}{lcr}\ \ x\ +\ \ 2 \cdot y & = &9 \\3 \cdot x +10 \cdot y & =& 0 \end{array}\right.$$ 13499Input in {\tt Xcas} mode : 13500\begin{center}{\tt Rref([[1,2,9] mod 13,[3,10,0] mod 13])}\end{center} 13501Output : 13502\begin{center}{\tt rref([[1\%13, 2\%13, 9\%13],[3\%13,10\%13,0\%13]])}\end{center} 13503you need to {\tt eval(ans())} to get : 13504\begin{center}{\tt [[1\%13,0\%13,3\%13],[0\%13,1\%13,3\%13]]}\end{center} 13505and conclude that {\tt x=3\%13} and {\tt y=3\%13}.\\ 13506Input in {\tt Maple} mode : 13507\begin{center}{\tt Rref([[1,2,9],[3,10,0],[3,11,1]]) mod 13}\end{center} 13508Output : 13509\begin{center}{\tt [[1,0,0],[0,1,0],[0,0,1]]}\end{center} 13510 13511 13512\section{Taylor and asymptotic expansions} 13513\subsection{Division by increasing power order : {\tt divpc}}\index{divpc} 13514\noindent{\tt divpc} takes three arguments : two polynomials 13515expressions $A,\ B$ depending on $x$, 13516such that the constant term of $B$ is not 0, and an integer $n$.\\ 13517{\tt divpc} returns the quotient $Q$ of the division of $A$ by $B$ 13518by increasing power order, with {\tt degree}$(Q)\leq n$ or $ Q=0$. 13519In other words, $Q$ is the Taylor expansion of order $n$ of 13520$\displaystyle \frac{A}{B}$ in the vicinity of $x=0$. \\ 13521Input : 13522\begin{center}{\tt divpc(1+x\verb|^|2+x\verb|^|3,1+x\verb|^|2,5)}\end{center} 13523Output : 13524\begin{center}{\tt -x\verb|^|5+x\verb|^|3+1}\end{center} 13525Note that this command does not work on polynomials written 13526as a list of coefficients. 13527 13528\subsection{Taylor expansion : {\tt taylor}}\index{taylor}\index{order\_size|textbf} 13529\noindent{\tt taylor} takes from one to four arguments : 13530\begin{itemize} 13531\item an expression depending of a variable (by default {\tt x}), 13532\item an equality variable=value (e.g. $x=a$) where to compute 13533the Taylor expansion, by default {\tt x=0}, 13534\item an integer $n$, the order of the series expansion, 13535by default {\tt 5} 13536\item a direction {\tt -1, 1} (for unidirectional series expansion) 13537 or {\tt 0} (for bidirectional series expansion) (by default {\tt 13538 0}). 13539\end{itemize} 13540Note that the syntax {\tt ...,x,$n$,$a$,...} 13541(instead of {\tt ...,x=$a$,$n$,...}) is also accepted.\\ 13542{\tt taylor} returns a polynomial in {\tt x-a}, plus a remainder 13543of the form:\\ 13544 {\tt (x-a)\verb|^|n*order\_size(x-a)}\\ 13545where {\tt order\_size} is a function such that, 13546\[ \forall r>0, \quad \lim_{x\rightarrow 0} x^r \mbox{order\_size}(x) = 0 \] 13547For regular series expansion, {\tt order\_size} is a bounded function, 13548but for non regular series expansion, it might tend slowly to 13549infinity, for example like a power of $\ln(x)$.\\ 13550Input : 13551\begin{center}{\tt taylor(sin(x),x=1,2)}\end{center} 13552Or (be careful with the order of the arguments !) : 13553\begin{center}{\tt taylor(sin(x),x,2,1)}\end{center} 13554Output : 13555\begin{center}{\tt sin(1)+cos(1)*(x-1)+(-(1/2*sin(1)))*(x-1)\verb|^|2+ (x-1)\verb|^|3*order\_size(x-1)}\end{center} 13556{\bf Remark}\\ 13557The order returned by {\tt taylor} may be smaller than $n$ if 13558cancellations between numerator and denominator occur, for example 13559\[ \mbox{taylor}(\frac{x^3+\sin(x)^3}{x-\sin(x)}) \] 13560Input : 13561\begin{center}{\tt taylor(x\verb|^|3+sin(x)\verb|^|3/(x-sin(x)))}\end{center} 13562The output is only a 2nd-order series expansion : 13563\begin{center}{\tt 13564 6+-27/10*x\verb|^2|+x\verb|^|3*order\_size(x)}\end{center} 13565Indeed the numerator and denominator valuation is 3, hence we lose 3 13566orders. To get order 4, we should use $n=7$.\\ 13567Input : 13568\begin{center}{\tt taylor(x\verb|^|3+sin(x)\verb|^|3/(x-sin(x)),x=0,7)}\end{center} 13569Output is a 4th-order series expansion : 13570\begin{center}{\tt 6+-27/10*x\verb|^|2+x\verb|^|3+711/1400*x\verb|^|4+x\verb|^|5*order\_size(x)}\end{center} 13571 13572\subsection{Series expansion : {\tt series}}\index{series}\index{order\_size} 13573\noindent{\tt series} takes from one to four arguments : 13574\begin{itemize} 13575\item an expression depending of a variable (by default {\tt x}), 13576\item an equality variable=value (e.g. $x=a$) where to compute 13577the series expansion, by default {\tt x=0}, 13578\item an integer $n$, the order of the series expansion, 13579by default {\tt 5} 13580\item a direction {\tt -1, 1} (for unidirectional series expansion) 13581 or {\tt 0} (for bidirectional series expansion) (by default {\tt 13582 0}). 13583\end{itemize} 13584Note that the syntax {\tt ...,x,$a$,$n$,...} 13585(instead of {\tt ...,x=$a$,$n$,...}) is also accepted.\\ 13586{\tt series} returns a polynomial in {\tt x-a}, plus a remainder 13587of the form: 13588\begin{center} 13589 {\tt (x-a)\verb|^|n*order\_size(x-a)} 13590\end{center} 13591where {\tt order\_size} is a function such that, 13592\[ \forall r>0, \quad \lim_{x\rightarrow 0} x^r \mbox{order\_size}(x) = 0 \] 13593The order returned by {\tt series} may be smaller than $n$ if 13594cancellations between numerator and denominator occur. 13595 13596Examples~: 13597\begin{itemize} 13598\item series expansion in the vicinity of {\tt x=0}\\ 13599 Find an series expansion of 13600$\displaystyle\frac{x^3+\sin(x)^3}{x-\sin(x)}$ 13601in the vicinity of {\tt x=0}.\\ 13602Input : 13603\begin{center}{\tt series(x\verb|^|3+sin(x)\verb|^|3/(x-sin(x)))}\end{center} 13604Output is only a 2nd-order series expansion : 13605\begin{center}{\tt 6+-27/10*x\verb|^2|+x\verb|^|3*order\_size(x)}\end{center} 13606We have lost 3 orders because the valuation of the numerator and 13607denominator is 3. To get a 4-th order expansion, we must therefore 13608take $n=7$.\\ 13609Input : 13610\begin{center}{\tt series(x\verb|^|3+sin(x)\verb|^|3/(x-sin(x)),x=0,7)}\end{center} 13611or : 13612\begin{center}{\tt series(x\verb|^|3+sin(x)\verb|^|3/(x-sin(x)),x,0,7)}\end{center} 13613Output is a 4th-order series expansion : 13614\begin{center}{\tt 6+-27/10*x\verb|^|2+x\verb|^|3+711/1400*x\verb|^|4+ 13615x\verb|^|5*order\_size(x)}\end{center} 13616\item series expansion in the vicinity of {\tt x=a}\\ 13617Find a series 4th-order expansion of $\cos(2x)^2$ in the vicinity of 13618$x=\frac{\pi}{6}$. \\ 13619Input: 13620\begin{center}{\tt series(cos(2*x)\verb|^|2,x=pi/6, 4)}\end{center} 13621Output : 13622\begin{center}{\tt 1/4+(-(4*sqrt(3)))/4*(x-pi/6)+(4*3-4)/4*(x-pi/6)\verb|^|2+ 32*sqrt(3)/3/4*(x-pi/6)\verb|^|3+(-16*3+16)/3/4*(x-pi/6)\verb|^|4+ (x-pi/6)\verb|^|5*order\_size(x-pi/6)}\end{center} 13623\item series expansion in the vicinity of {\tt x=+$\infty$} or {\tt 13624 x=-$\infty$} 13625\begin{enumerate} 13626\item 13627Find a 5th-order series expansion of $\arctan(x)$ in the vicinity of 13628{\tt x=+$\infty$}.\\ 13629 Input : 13630\begin{center}{\tt series(atan(x),x=+infinity,5)}\end{center} 13631Output : 13632\begin{center}{\tt pi/2-1/x+1/3*(1/x)\verb|^|3+1/-5*(1/x)\verb|^|5+ 13633(1/x)\verb|^|6*order\_size(1/x)}\end{center} 13634Note that the expansion variable and the argument of the 13635{\tt order\_size} function is 13636$\displaystyle h=\frac{1}{x} \rightarrow_{x\rightarrow + \infty} 0 $. 13637\item 13638Find a series 2nd-order expansion of $(2x-1)e^{\frac{1}{x-1}}$ in the vicinity of 13639{\tt x=+$\infty$}. \\ 13640Input : 13641\begin{center}{\tt series((2*x-1)*exp(1/(x-1)),x=+infinity,3)}\end{center} 13642Output is only a 1st-order series expansion : 13643\begin{center}{\tt 2*x+1+2/x+(1/x)\verb|^|2*order\_size(1/x)}\end{center} 13644To get a 2nd-order series expansion in $1/x$, input: 13645\begin{center}{\tt series((2*x-1)*exp(1/(x-1)),x=+infinity,4)}\end{center} 13646Output : 13647\begin{center}{\tt 13648 2*x+1+2/x+17/6*(1/x)\verb|^|2+(1/x)\verb|^|3*order\_size(1/x)}\end{center} 13649\item 13650Find a 2nd-order series expansion of $(2x-1)e^{\frac{1}{x-1}}$ in the vicinity 13651of {\tt x=-$\infty$}.\\ 13652Input : 13653\begin{center}{\tt series((2*x-1)*exp(1/(x-1)),x=-infinity,4)}\end{center} 13654Output : 13655\begin{center}{\tt -2*(-x)+1-2*(-1/x)+17/6*(-1/x)\verb|^|2+\\ 13656(-1/x)\verb|^|3*order\_size(-1/x)}\end{center} 13657\end{enumerate} 13658\item unidirectional series expansion.\\ 13659The fourth parameter indicates the direction : 13660\begin{itemize} 13661\item {\tt 1} to do an series expansion in the vicinity of $x=a$ with 13662$ \ x>a$, 13663\item{\tt -1} to do an series expansion in the vicinity of $x=a$ with 13664$ \ x<a$, 13665\item{\tt 0} to do an series expansion in the vicinity of $x=a$ with 13666$ \ x \neq a$. 13667\end{itemize} 13668For example, 13669find a 2nd-order series expansion of $\ \frac{(1+x)^{\frac{1}{x}}}{x^3}\ $ in 13670the vicinity of $x=0^+$.\\ 13671Input : 13672\begin{center}{\tt series((1+x)\verb|^|(1/x)/x\verb|^|3,x=0,2,1)}\end{center} 13673Output : 13674\begin{center}{\tt exp(1)/x\verb|^|3+(-(exp(1)))/2/x\verb|^|2+1/x*order\_size(x)}\end{center} 13675\end{itemize} 13676 13677\subsection{The inverse of a series: \texttt{revert}}\index{revert} 13678 13679The \texttt{revert} command takes as argument an expression which 13680represents the beginning of a power series centered at 0 for a 13681function $f$. By default, the variable is \texttt{x}, if a different 13682variable is used, then that variable should be the second argument.\\ 13683\texttt{revert} returns the beginning of the power series for the 13684inverse of $f$, namely the beginning of the power series for 13685$g(f(0)+x)$ where the function $g$ satisfies $g(f(x))=x$.\\ 13686Input: 13687\begin{center} 13688 \tt 13689 revert (x + x\^{}2 + x\^{}4) 13690\end{center} 13691Output: 13692\begin{center} 13693 \tt 13694 x-x\^{}2+2*x\^{}3-6*x\^{}4 13695\end{center} 13696Note that if the power series of a function $f$ begins with 13697$x + x^2 + x^4$, then $f(0)=0$, $f'(0)=1$, $f''(0)=2$, $f'''(0)=0$ and 13698$f^{(4)}(0) = 24$. The function $g$ with $g(f(x))=x$ will then satisfy 13699$g(0)=0$, $g'(0)=1/f'(0) = 1$, $g''(0) = -2$, $g'''(0) = 12$ and 13700$g^{(4)}(0) = -144$. The power series for $g$ will then begin 13701$x - x^2 + 2x^3 - 6x^4$. 13702 13703Entering the beginning of the power series for $\exp(x)$,\\ 13704Input: 13705\begin{center} 13706 \tt 13707 revert(1 + x + x\^{}2/2 + x\^{}3/6 + x\^{}4/24) 13708\end{center} 13709Output: 13710\begin{center} 13711 \tt 13712 x-1/2*x\^{}2+1/3*x\^{}3-1/4*x\^{}4 13713\end{center} 13714returns the beginning of the power series for $\ln(1+x)$. 13715 13716\subsection{The residue of an expression at a point : {\tt residue}}\index{residue} 13717{\tt residue} takes as argument an expression depending on a variable, 13718the variable name and a complex $a$ or an expression 13719depending on a variable and the equality : variable\_name=$a$. 13720{\tt residue} returns the residue of this expression at the point $a$.\\ 13721Input : 13722\begin{center}{\tt residue(cos(x)/x\verb|^|3,x,0)}\end{center} 13723or : 13724\begin{center}{\tt residue(cos(x)/x\verb|^|3,x=0)}\end{center} 13725Output : 13726\begin{center}{\tt (-1)/2}\end{center} 13727 13728\subsection{Pad\'e expansion: {\tt pade}}\index{pade} 13729{\tt pade} takes 4 arguments 13730\begin{itemize} 13731\item an expression, 13732\item the variable name the expression depends on, 13733\item an integer $n$ or a polynomial $N$, 13734\item an integer $p$. 13735\end{itemize} 13736{\tt pade} returns a rational fraction $P/Q$ such that {\tt 13737 degree(P)}$<p$ and $P/Q=f \pmod{x^{n+1}}$ or $P/Q=f \pmod{N}$. 13738In the first case, it means that $P/Q$ and $f$ have the same 13739Taylor expansion at 0 up to order $n$.\\ 13740Input : 13741\begin{center}{\tt pade(exp(x),x,5,3)}\end{center} 13742or : 13743\begin{center}{\tt pade(exp(x),x,x\verb|^|6,3)}\end{center} 13744Output : 13745\begin{center}{\tt (3*x\verb|^|2+24*x+60)/(-x\verb|^|3+9*x\verb|^|2-36*x+60)}\end{center} 13746To verify input : 13747\begin{center}{\tt taylor((3*x\verb|^|2+24*x+60)/(-x\verb|^|3+9*x\verb|^|2-36*x+60))}\end{center} 13748Output : 13749\begin{center}{\tt 1+x+1/2*x\verb|^|2+1/6*x\verb|^|3+1/24*x\verb|^|4+1/120*x\verb|^|5+x\verb|^|6*order\_size(x)}\end{center} 13750which is the 5th-order series expansion of {\tt exp(x)} at $x=0$.\\ 13751Input : 13752\begin{center}{\tt pade((x\verb|^|15+x+1)/(x\verb|^|12+1),x,12,3)}\end{center} 13753or : 13754\begin{center}{\tt pade((x\verb|^|15+x+1)/(x\verb|^|12+1),x,x\verb|^|13,3)}\end{center} 13755Output : 13756\begin{center}{\tt x+1}\end{center} 13757Input : 13758\begin{center}{\tt pade((x\verb|^|15+x+1)/(x\verb|^|12+1),x,14,4)}\end{center} 13759or : 13760\begin{center}{\tt pade((x\verb|^|15+x+1)/(x\verb|^|12+1),x,x\verb|^|15,4)}\end{center} 13761Output : 13762\begin{center}{\tt (-2*x\verb|^|3-1)/(-x\verb|^|11+x\verb|^|10-x\verb|^|9+x\verb|^|8-x\verb|^|7+x\verb|^|6-x\verb|^|5+x\verb|^|4- x\verb|^|3-x\verb|^|2+x-1)}\end{center} 13763To verify, input : 13764\begin{center}{\tt series(ans(),x=0,15)}\end{center} 13765Output : 13766\begin{center}{\tt 1+x-x\verb|^|{12}-x\verb|^|{13}+2x\verb|^|{15}+x\verb|^|{16}*order\_size(x)}\end{center} 13767then input : 13768\begin{center}{\tt series((x\verb|^|15+x+1)/(\verb|x^|12+1),x=0,15)}\end{center} 13769Output : 13770\begin{center}{\tt 1+x-x\verb|^|{12}-x\verb|^|{13}+x\verb|^|{15}+x\verb|^|{16}*order\_size(x)}\end{center} 13771These two expressions have the same 14th-order series expansion at $x=0$. 13772 13773\section{Ranges of values} 13774\subsection{Definition of a range of values: {\tt a1..a2}}\index{..|textbf} 13775A range of values is represented by two real numbers 13776separated by {\tt ..}, for example 13777\begin{center}{\tt 1..4}\\ 13778{\tt 1.2..sqrt(2)} 13779\end{center} 13780Input : 13781\begin{center}{\tt A:=1..4}\end{center} 13782\begin{center}{\tt B:=1.2..sqrt(2)}\end{center} 13783{\bf Warning!}\\ 13784The order of the boundaries of the range is significant. For 13785example, if you input 13786\begin{center} 13787{\tt B:=2..3; C:=3..2}, 13788\end{center} 13789then {\tt B} and {\tt C} are different, {\tt B==C} returns {\tt 0}. 13790 13791\subsection{Boundaries of a range of values: {\tt left right}}\index{[]}\index{sommet}\index{feuille}\index{op}\index{left}\index{right} 13792\noindent {\tt left} (resp. {\tt right}) takes as argument a range of 13793values.\\ 13794{\tt left} (resp. {\tt right}) returns the left (resp. right) boundary 13795of this range. \\ 13796Note that {\tt ..} is an infixed operator, therefore: 13797\begin{itemize} 13798\item {\tt sommet(1..5)} is equal to {\tt '..'} and {\tt feuille(1..5)} 13799 is equal to {\tt (1,5)}. 13800\item the name of the range followed by 13801{\tt [0]} returns the operator {\tt ..} 13802\item 13803the name of the range followed by {\tt [1]} 13804(or the {\tt left} command) returns the left boundary. 13805\item 13806The name of the range followed by {\tt [2]} 13807(or the {\tt right} command) 13808returns the right boundary. 13809\end{itemize} 13810Input : 13811\begin{center}{\tt (3..5)[0]}\end{center} 13812or : 13813\begin{center}{\tt sommet(3..5)}\end{center} 13814Output : 13815\begin{center}{\tt '..'}\end{center} 13816Input : 13817\begin{center}{\tt left(3..5)}\end{center} 13818or : 13819\begin{center}{\tt (3..5)[1]}\end{center} 13820or : 13821\begin{center}{\tt feuille(3..5)[0]}\end{center} 13822or : 13823\begin{center}{\tt op(3..5)[0]}\end{center} 13824Output : 13825\begin{center}{\tt 3}\end{center} 13826Input : 13827\begin{center}{\tt right(3..5)}\end{center} 13828or : 13829\begin{center}{\tt (2..5)[2]}\end{center} 13830or : 13831\begin{center}{\tt feuille(3..5)[1]}\end{center} 13832or : 13833\begin{center}{\tt op(3..5)[1]}\end{center} 13834Output : 13835\begin{center}{\tt 5}\end{center} 13836{\bf Remark}\\ 13837{\tt left} (resp. {\tt right}) returns also the left (resp. right) member of an 13838equation (for example {\tt left(2*x+1=x+2)} returns {\tt 2*x+1}). 13839 13840\subsection{Center of a range of values: {\tt interval2center}}\index{interval2center} 13841\noindent {\tt interval2center} takes as argument a range of values interval or a list of 13842ranges of values.\\ 13843{\tt interval2center} returns the center of this range or the list of 13844centers of these ranges.\\ 13845Input : 13846\begin{center}{\tt interval2center(3..5)}\end{center} 13847Output : 13848\begin{center}{\tt 4}\end{center} 13849Input : 13850\begin{center}{\tt interval2center([2..4,4..6,6..10])}\end{center} 13851Output : 13852\begin{center}{\tt [3,5,8]}\end{center} 13853 13854\subsection{Ranges of values defined by their center : {\tt center2interval}}\index{center2interval} 13855\noindent {\tt center2interval} takes as argument a vector {\tt V} of reals 13856and optionally a real as second argument 13857(by default {\tt V[0]-(V[1]-V[0])/2}).\\ 13858{\tt center2interval} returns a vector of ranges of values having 13859the real values of the first argument as centers, where 13860the value of the second argument is 13861the left boundary of the first range.\\ 13862Input : 13863\begin{center}{\tt center2interval([3,5,8])}\end{center} 13864Or (since the default value is 3-(5-3)/2=2) : 13865\begin{center}{\tt center2interval([3,5,8],2)}\end{center} 13866Output : 13867\begin{center}{\tt [2..4,4..6,6..10]}\end{center} 13868Input : 13869\begin{center}{\tt center2interval([3,5,8],2.5)}\end{center} 13870Output : 13871\begin{center}{\tt [2.5..3.5,3.5..6.5,6.5..9.5]}\end{center} 13872 13873\section{Intervals} 13874 13875\subsection{Defining intervals: \texttt{i[]}}\index{i[]} 13876 13877An interval is a range of real numbers, whose end points will be 13878floats with at least 15 significant digits. The interval from $a$ to 13879$b$ is created with \texttt{i[}$a$\texttt{,}$b$\texttt{]}.\\ 13880Input: 13881\begin{center} 13882 \tt 13883 i[1,13/11] 13884\end{center} 13885Output: 13886\begin{center} 13887 \tt 13888 [1.00000000000000..1.18181818181819] 13889\end{center} 13890If $a > b$, then \texttt{i[a,b]} returns 13891\texttt{i[evalf(b,15)-epsilon,evalf(a,15)+epsilon]}.\\ 13892Input: 13893\begin{center} 13894 \tt 13895 i[pi,sqrt(3)] 13896\end{center} 13897Output: 13898\begin{center} 13899 \tt 13900 [1.73205080756886..3.14159265358980] 13901\end{center} 13902 13903Intervals can also be created by following a decimal number with a 13904question mark. If the decimal number contains $n$ digits, the 13905interval will be centered at $a$ and have width $2\cdot 10^{-n}$.\\ 13906Input: 13907\begin{center} 13908 \tt 13909 0.123? 13910\end{center} 13911Output: 13912\begin{center} 13913 \tt 13914 [0.121999999999999..0.124000000000000] 13915\end{center} 13916Input: 13917\begin{center} 13918 \tt 13919 789.123456? 13920\end{center} 13921Output: 13922\begin{center} 13923 \tt 13924 [0.789123454999990e3..0.789123456999998e3] 13925\end{center} 13926 13927\subsection{The endpoints of an interval: 13928\texttt{left},\texttt{right}}\index{left}\index{right} 13929 13930The \texttt{left} and \texttt{right} commands take an interval as an 13931argument.\\ 13932\texttt{left} and \texttt{right} return the left and right endpoints 13933of the interval, respectively.\\ 13934Input: 13935\begin{center} 13936 \tt 13937 left(i[2,5]) 13938\end{center} 13939Output: 13940\begin{center} 13941 \tt 13942 2.00000000000000 13943\end{center} 13944Input: 13945\begin{center} 13946 \tt 13947 right(i[2,5]) 13948\end{center} 13949Output: 13950\begin{center} 13951 \tt 13952 5.00000000000000 13953\end{center} 13954 13955\subsection{Adding intervals} 13956 13957Intervals are added by adding the left end points and adding the right 13958end points.\\ 13959Input: 13960\begin{center} 13961 \tt 13962 i[1,4] + i[2,3] 13963\end{center} 13964Output: 13965\begin{center} 13966 \tt 13967 [3.00000000000000..7.00000000000000] 13968\end{center} 13969 13970\subsection{The negative of an interval} 13971 13972The negative of an interval is computed by taking the negative of the 13973end points of the interval. The new end points will have to be switched.\\ 13974Input: 13975\begin{center} 13976 \tt 13977 -i[2,3] 13978\end{center} 13979Output: 13980\begin{center} 13981 \tt 13982 [-3.00000000000000..-2.00000000000000] 13983\end{center} 13984 13985\subsection{Multiplying intervals} 13986 13987Intervals are multiplied by multiplying both end points of the first 13988interval by both end points of the second interval. The smallest product 13989will be the left end point of the product interval, and the largest 13990product will be the right end point of the product interval.\\ 13991Input: 13992\begin{center} 13993 \tt 13994 i[1,4]*i[2,3] 13995\end{center} 13996Output: 13997\begin{center} 13998 \tt 13999 [2.00000000000000..0.120000000000000e2] 14000\end{center} 14001Input: 14002\begin{center} 14003 \tt 14004 i[-2,4]*i[3,5] 14005\end{center} 14006Output: 14007\begin{center} 14008 \tt 14009 [-0.100000000000000e2..0.200000000000000e2] 14010\end{center} 14011 14012\subsection{The reciprocal of an interval} 14013 14014The reciprocal of an interval the interval determined by the 14015reciprocals of the end points.\\ 14016Input: 14017\begin{center} 14018 \tt 14019 1/i[2,3] 14020\end{center} 14021Output: 14022\begin{center} 14023 \tt 14024 [0.333333333333333..0.500000000000000] 14025\end{center} 14026Input: 14027\begin{center} 14028 \tt 14029 1/i[-6,-3] 14030\end{center} 14031Output: 14032\begin{center} 14033 \tt 14034 [-0.333333333333333..-0.166666666666667] 14035\end{center} 14036 14037If the original interval has zero as an end point, then the reciprocal 14038interval will have infinity or minus infinity as one of the end points.\\ 14039Input: 14040\begin{center} 14041 \tt 14042 1/i[0,2] 14043\end{center} 14044Output: 14045\begin{center} 14046 \tt 14047 [0.500000000000000..+infinity] 14048\end{center} 14049Input: 14050\begin{center} 14051 \tt 14052 1/i[-1,0] 14053\end{center} 14054Output: 14055\begin{center} 14056 \tt 14057 [-infinity..-1.00000000000000] 14058\end{center} 14059If one end point is positive and the other is negative, then the 14060reciprocal will simply be the interval from -infinity to infinity.\\ 14061Input: 14062\begin{center} 14063 \tt 14064 1/i[-2,3] 14065\end{center} 14066Output: 14067\begin{center} 14068 \tt 14069 [-infinity..+infinity] 14070\end{center} 14071 14072\subsection{The midpoint of an interval: 14073\texttt{midpoint}}\index{midpoint} 14074 14075The \texttt{midpoint} takes an interval as argument.\\ 14076\texttt{midpoint} returns the midpoint of the interval.\\ 14077Input: 14078\begin{center} 14079 \tt 14080 midpoint(i[2,3]) 14081\end{center} 14082Output: 14083\begin{center} 14084 \tt 14085 2.50000000000000 14086\end{center} 14087 14088\subsection{The union of intervals: \texttt{union}}\index{union} 14089 14090The \texttt{union} operator is an infixed operator.\\ 14091\texttt{union} takes two intervals and returns their convex hull.\\ 14092Input: 14093\begin{center} 14094 \tt 14095 i[1,3] union i[2,4] 14096\end{center} 14097Output: 14098\begin{center} 14099 \tt 14100 [1.00000000000000..4.00000000000000] 14101\end{center} 14102Input: 14103\begin{center} 14104 \tt 14105 i[2,4] union i[6,9] 14106\end{center} 14107Output: 14108\begin{center} 14109 \tt 14110 [2.00000000000000..9.00000000000000] 14111\end{center} 14112 14113\subsection{The intersection of intervals: \texttt{intersect}}\index{intersect} 14114 14115The \texttt{intersect} operator is an infixed operator.\\ 14116\texttt{intersect} takes two intervals and returns their intersection.\\ 14117Input: 14118\begin{center} 14119 \tt 14120 i[1,3] intersect i[2,4] 14121\end{center} 14122Output: 14123\begin{center} 14124 \tt 14125 [2.00000000000000..3.00000000000000] 14126\end{center} 14127 14128\subsection{Test if an object is in an interval: 14129\texttt{contains}}\index{contains} 14130 14131The \texttt{contains} command takes two arguments; an interval and an 14132object to test.\\ 14133\texttt{contains} returns 1 if the object is contained in the 14134interval; i.e., if the object is a number then it must be an element 14135of the interval, if the object is another interval it must be a subset 14136of the interval. \texttt{contains} returns 0 otherwise.\\ 14137Input: 14138\begin{center} 14139 \tt 14140 contains(i[0,2],1) 14141\end{center} 14142Output: 14143\begin{center} 14144 \tt 14145 1 14146\end{center} 14147Input: 14148\begin{center} 14149 \tt 14150 contains(i[0,2],3) 14151\end{center} 14152Output: 14153\begin{center} 14154 \tt 14155 0 14156\end{center} 14157Input: 14158\begin{center} 14159 \tt 14160 contains(i[0,2],i[1,2]) 14161\end{center} 14162Output: 14163\begin{center} 14164 \tt 14165 1 14166\end{center} 14167 14168\subsection{Convert a number to an interval: 14169\texttt{convert}}\index{convert} 14170 14171To convert a number to an interval, the \texttt{convert} command takes 14172two mandatory arguments and one optional argument. The first argument 14173is an expression which evaluates to the desired number, and the second 14174argument is the reserved word \texttt{interval}. The optional 14175argument is an integer greater than 15 giving the desired number of 14176digits.\\ 14177\texttt{convert} returns the smallest interval containing the value of 14178the expression.\\ 14179Input: 14180\begin{center} 14181 \tt 14182 convert(sin(3)+1, interval) 14183\end{center} 14184Output: 14185\begin{center} 14186 \tt 14187 [1.14112000805985 .. 1.14112000805990] 14188\end{center} 14189Input: 14190\begin{center} 14191 \tt 14192 convert(sin(3)+1, interval,20) 14193\end{center} 14194Output: 14195\begin{center} 14196 \tt 14197 [1.1411200080598672220 .. 1.1411200080598672222] 14198\end{center} 14199 14200\section{Sequences} 14201\label{sec:seq} 14202\subsection{Definition : {\tt seq[] ()}}\index{seq[]}\index{()} 14203A sequence is represented by 14204a sequence of elements separated by commas, without 14205delimiters or with either {\tt ( )} or {\tt seq[...]} 14206as delimiters, for example 14207\begin{center} 14208{\tt (1,2,3,4)}\\ 14209{\tt seq[1,2,3,4]} 14210\end{center} 14211Input : 14212\begin{center}{\tt A:=(1,2,3,4)} or {\tt A:=seq[1,2,3,4]}\end{center} 14213\begin{center}{\tt B:=(5,6,3,4)} or {\tt B:=seq[5,6,3,4]}\end{center} 14214{\bf Remarks} 14215\begin{itemize} 14216\item The order of the elements of the sequence is significant. 14217For example, if {\tt B:=(5,6,3,4)} and {\tt C:=(3,4,5,6)}, then 14218{\tt B==C} returns {\tt 0}. 14219\item 14220(see also \ref{sec:seq})\\ 14221{\tt seq([0,2])=(0,0)} and {\tt seq([0,1,1,5])=[0,0,0,0,0]} but\\ 14222{\tt seq[0,2]=(0,2)} and {\tt seq[0,1,1,5]=(0,1,1,5)} 14223\end{itemize} 14224 14225\subsection{Concat two sequences : {\tt ,}}\index{,} 14226The infix operator {\tt ,} concatenates two sequences.\\ 14227Input : 14228\begin{center}{\tt A:=(1,2,3,4)}\end{center} 14229\begin{center}{\tt B:=(5,6,3,4)}\end{center} 14230\begin{center}{\tt A,B}\end{center} 14231Output : 14232\begin{center}{\tt (1,2,3,4,5,6,3,4)}\end{center} 14233 14234\subsection{Get an element of a sequence : {\tt [], [[]]}} 14235\index{[]}\index{[[]]} 14236The elements of a sequence have indexes beginning at 0 in {\tt Xcas} 14237mode or 1 in other modes.\\ 14238A sequence or a variable name assigned to a sequence 14239followed by {\tt [n]} returns the element of index {\tt n} of the sequence.\\ 14240Input : 14241\begin{center}{\tt (0,3,2)[1]}\end{center} 14242Output : 14243\begin{center}{\tt 3}\end{center} 14244 14245\subsection{Sub-sequence of a sequence : {\tt []}}\index{[]}\index{..} 14246A sequence or a variable name assigned to a sequence followed by 14247{\tt [n1..n2]} returns the sub-sequence of this sequence 14248starting at index {\tt n1} and ending at index {\tt n2}.\\ 14249Input : 14250\begin{center}{\tt (0,1,2,3,4)[1..3]}\end{center} 14251Output : 14252\begin{center}{\tt (1,2,3)}\end{center} 14253 14254\subsection{Make a sequence or a list : {\tt seq \$}}\index{seq|textbf}\index{\$|textbf} 14255\noindent{\tt seq} takes two, three, four or five arguments : the first 14256argument is an expression depending of a parameter (for example $j$) and 14257the remaining argument(s) describe which values of $j$ will be used to 14258generate the sequence. More precisely $j$ is assumed to move 14259from $a$ to $b$: 14260\begin{itemize} 14261\item with a default step of 1 or -1: {\tt j=a..b} or 14262{\tt j,a..b} (Maple-like syntax), {\tt j,a,b} (TI-like syntax) 14263\item or with a specific step: 14264{\tt j=a..b,p} (Maple-like syntax), {\tt j,a,b,p} (TI-like syntax). 14265\end{itemize} 14266If the Maple-like syntax is used, {\tt seq} returns a sequence, 14267if the TI-like syntax is used, {\tt seq} returns a list. 14268 14269{\tt \$} is the infixed version of {\tt seq} when {\tt seq} has only two 14270arguments and always returns a sequence.\\ 14271{\bf Remark:} 14272\begin{itemize} 14273\item In {\tt Xcas} mode, the precedence of {\tt \$} is not the same as 14274for example in {\tt Maple}, in case of doubt 14275put the arguments of {\tt \$} in parenthesis. 14276For example, the equivalent of {\tt seq(j\verb|^|2,j=-1..3)} is 14277{\tt (j\verb|^|2)\$(j=-1..3)} and 14278returns {\tt (1,0,1,4,9)}. 14279The equivalent of {\tt seq(4,3)} is {\tt 4\$3} and returns 14280{\tt (4,4,4)}. 14281\item 14282With {\tt Maple} syntax, {\tt j,a..b,p} is not valid. 14283To specify a step $p$ for the variation of 14284$j$ from $a$ to $b$, use {\tt j=a..b,p} or use the {\tt TI} syntax 14285{\tt j,a,b,p} and get the sequence from the list with {\tt op(...)}. 14286\end{itemize} 14287In summary, the different way to build a sequence are : 14288\begin{itemize} 14289\item with {\tt Maple}-like {\bf syntax} 14290\begin{enumerate} 14291\item {\tt seq} has two arguments, 14292either an expression depending on a parameter 14293(for example $j$) and $j=a..b$ where $a$ and $b$ are reals, 14294or a constant expression and an integer $n$.\\ 14295{\tt seq} returns the sequence where $j$ is replaced in the 14296expression by $a$, $a+1$,...,$b$ if $b>a$ and by $a$, $a-1$,...,$b$ if $b<a$, 14297or {\tt seq} returns the sequence made by copying the constant $n$ times. 14298\item {\tt seq} has three arguments, an expression depending on a parameter 14299(for example $j$) and $j=a..b,p$ where $a$, $b$ are reals and $p$ is a 14300real number.\\ 14301{\tt seq} returns the sequence where $j$ is replaced in the 14302expression by $a$, $a+p$,...,$b$ if $b>a$ and by $a$, $a-p$,...,$b$ 14303if $b<a$.\\ 14304Note that $j,a..b$ is also valid but $j,a..b,p$ is not valid. 14305\end{enumerate} 14306\item {\tt TI} {\bf syntax} 14307\begin{enumerate} 14308\item {\tt seq} has four arguments, an expression depending on a parameter (for 14309example $j$), the name of the parameter (for example $j$), $a$ and $b$ where 14310$a$ and $b$ are reals.\\ 14311{\tt seq} returns the list where $j$ is replaced in the 14312expression by $a$, $a+1$,...,$b$ if $b>a$ and by $a$, $a-1$,...,$b$ if $b<a$. 14313\item {\tt seq} has five arguments, an expression depending on a parameter (for 14314example $j$), the name of the parameter (for example $j$), $a$, $b$ and $p$ 14315where $a$, $b$ and $p$ are reals.\\ 14316{\tt seq} returns the list where 14317$j$ is substituted in the 14318expression by $a$, $a+p$,...,$a+k*p$ ($a+k*p \leq b <a+(k+1)*p$ or 14319$a+k*p \geq b> a+(k+1)*p$). 14320By default, $p$=1 if $b>a$ and $p$=-1 if $b<a$. 14321%If the sign of $p$ is not correct, {\tt Xcas} corrects it ! 14322\end{enumerate} 14323\end{itemize} 14324{\bf Note} that 14325in {\tt Maple} syntax, {\tt seq} takes no more than 3 arguments and 14326returns a sequence, 14327while in {\tt TI} syntax, {\tt seq} takes at least 4 arguments 14328and returns a list.\\ 14329Input to have a sequence with same elements : 14330\begin{center}{\tt seq(t,4)}\end{center} 14331or : 14332\begin{center}{\tt seq(t,k=1..4)}\end{center} 14333or : 14334\begin{center}{\tt t\$4}\end{center} 14335Output : 14336\begin{center}{\tt (t,t,t,t)}\end{center} 14337Input to have a sequence : 14338\begin{center}{\tt seq(j\verb|^|3,j=1..4)}\end{center} 14339or : 14340\begin{center}{\tt (j\verb|^|3)\$(j=1..4)}\end{center} 14341or : 14342\begin{center}{\tt seq(j\verb|^|3,j,1..4)}\end{center} 14343Output : 14344\begin{center}{\tt (1,8,27,64)}\end{center} 14345Input to have a sequence : 14346\begin{center}{\tt seq(j\verb|^|3,j=-1..4,2)}\end{center} 14347Output : 14348\begin{center}{\tt (-1,1,27)}\end{center} 14349Or to have a list,\\ 14350Input : 14351\begin{center}{\tt seq(j\verb|^|3,j,1,4)}\end{center} 14352Output : 14353\begin{center}{\tt [1,8,27,64]}\end{center} 14354Input : 14355\begin{center}{\tt seq(j\verb|^|3,j,0,5,2)}\end{center} 14356Output : 14357\begin{center}{\tt [0,8,64]}\end{center} 14358Input : 14359\begin{center}{\tt seq(j\verb|^|3,j,5,0,-2)}\end{center} 14360or 14361\begin{center}{\tt seq(j\verb|^|3,j,5,0,2)}\end{center} 14362Output : 14363\begin{center}{\tt [125,27,1]}\end{center} 14364Input : 14365\begin{center}{\tt seq(j\verb|^|3,j,1,3,0.5)}\end{center} 14366Output : 14367\begin{center}{\tt [1,3.375,8,15.625,27]}\end{center} 14368Input : 14369\begin{center}{\tt seq(j\verb|^|3,j,1,3,1/2)}\end{center} 14370Output : 14371\begin{center}{\tt [1,27/8,8,125/8,27]}\end{center} 14372{\bf Examples} 14373\begin{itemize} 14374\item Find the third derivative of $\ \ln(t)$, input: 14375\begin{center}{\tt diff(log(t),t\$3)}\end{center} 14376Output : 14377\begin{center}{\tt -((-(2*t))/t\verb|^|4)}\end{center} 14378\item Input : 14379\begin{center}{\tt l:=[[2,3],[5,1],[7,2]]}\end{center} 14380\begin{center}{\tt seq((l[k][0])\$(l[k][1]),k=0 .. size(l)-1)}\end{center} 14381Output : 14382\begin{center}{\tt 2,2,2,seq[5],7,7}\end{center} 14383then {\tt eval(ans())} returns: 14384\begin{center}{\tt 2,2,2,5,7,7}\end{center} 14385\item Input to transform a string into the list of its characters : 14386\begin{verbatim} 14387f(chn):={ 14388 local l; 14389 l:=size(chn); 14390 return seq(chn[j],j,0,l-1); 14391} 14392\end{verbatim} 14393then input: 14394\begin{center}{\tt f("abracadabra")}\end{center} 14395Output : 14396\begin{center}{\tt 14397 ["a","b","r","a","c","a","d","a","b","r","a"]}\end{center} 14398\end{itemize} 14399 14400\subsection{Transform a sequence into a list : {\tt [] nop}}\index{[]}\index{nop} 14401To transform a sequence into list, just put square brackets ({\tt []}) around 14402the sequence or use the command {\tt nop}.\\ 14403Input : 14404\begin{center}{\tt [seq(j\verb|^|3,j=1..4)]}\end{center} 14405or : 14406\begin{center}{\tt seq(j\verb|^|3,j,1,4)}\end{center} 14407or : 14408\begin{center}{\tt [(j\verb|^|3)\$(j=1..4)]}\end{center} 14409Output : 14410\begin{center}{\tt [1,4,9,16]}\end{center} 14411Input : 14412\begin{center}{\tt nop(1,4,9,16)}\end{center} 14413Output : 14414\begin{center}{\tt [1,4,9,16]}\end{center} 14415 14416\subsection{The {\tt +} operator applied on sequences}\index{+} 14417The infixed operator {\tt +}, with two sequences as argument, 14418returns the total sum of the elements of the two sequences.\\ 14419Note the difference with the lists, where 14420the term by term sums of the elements of the two lists would 14421be returned.\\ 14422Input : 14423\begin{center}{\tt (1,2,3,4,5,6)+(4,3,5)}\end{center} 14424or : 14425\begin{center}{\tt '+'((1,2,3,4,5,6),(4,3,5))}\end{center} 14426Output : 14427\begin{center}{\tt 33}\end{center} 14428But input : 14429\begin{center}{\tt [1,2,3,4,5,6]+[4,3,5]}\end{center} 14430Output : 14431\begin{center}{\tt [5,5,8,4,5,6]}\end{center} 14432{\bf Warning}\\ 14433When the operator {\tt +} is prefixed, it has to be quoted ({\tt '+'}). 14434 14435\section{Sets} 14436\subsection{Definition : {\tt set[]}}\index{\%\{ \%\}}\index{set[]} 14437To define a set of elements, put the elements separated by a comma, with 14438{\tt \%\{ ... \%\}} or {\tt set[ ... ]} as delimiters.\\ 14439Input : 14440\begin{center} 14441{\tt \%\{1,2,3,4\%\}}\\ 14442{\tt set[1,2,3,4]} 14443\end{center} 14444In the {\tt Xcas} answers, the set delimiters are displayed 14445as $\llbracket$ and $\rrbracket$ in order 14446not to confuse sets with lists. 14447For example, $\llbracket$1,2,3$\rrbracket$ is the set {\tt \%\{1,2,3\%\}}, 14448unlike [1,2,3] (normal brackets) which is the list {\tt [1,2,3]}.\\ 14449Input : 14450\begin{center}{\tt A:=\%\{1,2,3,4\%\}} or {\tt A:=set[1,2,3,4]}\end{center} 14451Output : 14452\begin{center}{\tt $\llbracket$1,2,3,4$\rrbracket$ }\end{center} 14453Input : 14454\begin{center}{\tt B:=\%\{5,5,6,3,4\%\}} or {\tt B:=set[5,5,6,3,4]}\end{center} 14455Output : 14456\begin{center}{\tt $\llbracket$5,6,3,4$\rrbracket$ }\end{center} 14457{\bf Remark}\\ 14458The order in a set is not significant and 14459the elements in a set are all distinct. If you input 14460{\tt B:=\%\{5,5,6,3,4\%\}} and {\tt C:=\%\{3,4,5,3,6\%\}}, then 14461{\tt B==C} will return {\tt 1}. 14462 14463\subsection{Union of two sets or of two lists : {\tt union}}\index{union} 14464\noindent{\tt union} is an infixed operator.\\ 14465{\tt union} takes as argument two sets or two lists, 14466{\tt union} returns the union set of the arguments.\\ 14467Input : 14468\begin{center}{\tt set[1,2,3,4] union set[5,6,3,4]}\end{center} 14469or : 14470\begin{center}{\tt \%\{1,2,3,4\%\} union \%\{5,6,3,4\%\}}\end{center} 14471Output : 14472\begin{center}{\tt $\llbracket$1,2,3,4,5,6$\rrbracket$}\end{center} 14473Input : 14474\begin{center}{\tt [1,2,3] union [2,5,6]}\end{center} 14475Output : 14476\begin{center}{\tt $\llbracket$1,2,3,5,6$\rrbracket$}\end{center} 14477 14478\subsection{Intersection of two sets or of two lists : {\tt intersect}}\index{intersect} 14479\noindent {\tt intersect} is an infixed operator.\\ 14480{\tt intersect} takes as argument two sets or two lists.\\ 14481{\tt intersect} returns the intersection set of the arguments.\\ 14482Input : 14483\begin{center}{\tt set[1,2,3,4] intersect set[5,6,3,4]}\end{center} 14484or : 14485\begin{center}{\tt \%\{1,2,3,4\%\} intersect \%\{5,6,3,4\%\}}\end{center} 14486Output : 14487\begin{center}{\tt $\llbracket$3,4$\rrbracket$}\end{center} 14488Input : 14489\begin{center}{\tt [1,2,3,4] intersect [5,6,3,4]}\end{center} 14490Output : 14491\begin{center}{\tt $\llbracket$3,4$\rrbracket$}\end{center} 14492 14493\subsection{Difference of two sets or of two lists : {\tt minus}}\index{minus} 14494\noindent{\tt minus} is an infixed operator.\\ 14495{\tt minus} takes as argument two sets or two lists.\\ 14496{\tt minus} returns the difference set of the arguments.\\ 14497Input : 14498\begin{center}{\tt set[1,2,3,4] minus set[5,6,3,4]}\end{center} 14499or : 14500\begin{center}{\tt \%\{1,2,3,4\%\} minus \%\{5,6,3,4\%\}}\end{center} 14501Output : 14502\begin{center}{\tt $\llbracket$1,2$\rrbracket$}\end{center} 14503Input : 14504\begin{center}{\tt [1,2,3,4] minus [5,6,3,4]}\end{center} 14505Output : 14506\begin{center}{\tt $\llbracket$1,2$\rrbracket$}\end{center} 14507 14508\subsection{Defining an $n$-tuple: \texttt{tuple}}\index{tuple} 14509 14510To define an $n$-tuple, rather than a list of $n$ objects, the objects 14511should be put inside the delimiters \texttt{tuple[} and \texttt{]}. 14512For example, the set consisting of the points \texttt{[1,3,4]}, 14513\texttt{[1,3,5]}, \texttt{[2,3,4} and \texttt{[2,3,5]} is written 14514\begin{center} 14515 \tt 14516 set[tuple[1,3,4],tuple[1,3,5],tuple[2,3,4],tuple[2,3,5]] 14517\end{center} 14518 14519\texttt{The Cartesian product of two sets: \texttt{*}}\index{*} 14520 14521The Cartesian product of two sets is computed with the infixed 14522operator \texttt{*}.\\ 14523Input: 14524\begin{center} 14525 \tt 14526 set[1,2] * set[3,4] 14527\end{center} 14528Output: 14529\begin{center} 14530 \tt 14531 set[tuple[1,3],tuple[1,4],tuple[2,3],tuple[2,4]] 14532\end{center} 14533Input: 14534\begin{center} 14535 \tt 14536 set[1,2] * set[3,4] * set[5,6] 14537\end{center} 14538Output: 14539\begin{center} 14540 \tt 14541 set[tuple[1,3,5],tuple[1,3,6],tuple[1,4,5],tuple[1,4,6], 14542 tuple[2,3,5],tuple[2,3,6],tuple[2,4,5],tuple[2,4,6]] 14543\end{center} 14544 14545\section{Lists\index{lists} and vectors\index{vectors}}\index{[]|textbf} 14546 14547\subsection{Definition} 14548A list (or a vector) is delimited by {\tt [ ]}, 14549its elements must be separated by commas. 14550For example, {\tt [1,2,5]} is a list of three integers. 14551 14552Lists can contain lists (for example, a matrix is a list of lists 14553of the same size). Lists may be used to represent vectors 14554(list of coordinates), matrices, univariate polynomials 14555(list of coefficients by decreasing order). 14556 14557Lists are different from sequences, because sequences are flat : an element 14558of a sequence cannot be a sequence. 14559Lists are different from sets, because for a list, the order is 14560important and the same element can be repeated in a list (unlike in 14561a set where each element is unique). 14562 14563In {\tt Xcas} output : 14564\begin{itemize} 14565\item vector (or list) delimiters are displayed as {\tt []}, 14566\item matrix delimiters are displayed as {\bf []}, 14567\item polynomial delimiters are displayed as $\talloblong \ \talloblong$, 14568\item set delimiters are displayed as $\llbracket \ \rrbracket$. 14569\end{itemize} 14570 14571The list elements are indexed starting from 0 in Xcas syntax mode 14572and from 1 in all other syntax modes. To access an element of a list, 14573follow the list with the index between square brackets.\\ 14574Input: 14575\begin{center} 14576 \tt 14577 L := [2,5,1,4] 14578\end{center} 14579Output: 14580\begin{center} 14581 \tt 14582 [2,5,1,4] 14583\end{center} 14584Input: 14585\begin{center} 14586 \tt 14587 L[1] 14588\end{center} 14589Output: 14590\begin{center} 14591 \tt 14592 5 14593\end{center} 14594To access the last element of a list, you can put \texttt{-1} between 14595square brackets.\\ 14596Input: 14597\begin{center} 14598 \tt 14599 L[-1] 14600\end{center} 14601Output: 14602\begin{center} 14603 \tt 14604 4 14605\end{center} 14606If you want the indices to start from in Xcas syntax mode, you can 14607enter the index between double brackets.\\ 14608Input: 14609\begin{center} 14610 \tt 14611 L[[1]] 14612\end{center} 14613Output: 14614\begin{center} 14615 \tt 14616 2 14617\end{center} 14618 14619 14620\subsection{Define a list: \texttt{makelist}}\index{makelist} 14621 14622See also section \ref{ssec:makelist}. 14623 14624A list can be defined by listing its elements, separated by commas, 14625between square brackets. \\ 14626Input: 14627\begin{center} 14628 \tt 14629 L1 := [1,2,3] 14630\end{center} 14631To define the empty list, simply enter the 14632brackets.\\ 14633Input: 14634\begin{center} 14635 \tt 14636 L0 := [] 14637\end{center} 14638\texttt{L0} is now the empty list. 14639 14640The \texttt{subsop} command (see section \ref{ssec:subsop}) can be 14641used to modify lists. You can also redefine elements (or define new 14642elements) with \texttt{:=}.\\ 14643Input: 14644\begin{center} 14645 \tt 14646 L1 14647\end{center} 14648Output: 14649\begin{center} 14650 \tt 14651 [1,2,3] 14652\end{center} 14653Input: 14654\begin{center} 14655 \tt 14656 L1[2] := 16 14657\end{center} 14658then: 14659\begin{center} 14660 \tt 14661 L1 14662\end{center} 14663Output: 14664\begin{center} 14665 \tt 14666 [1,2,16] 14667\end{center} 14668Input: 14669\begin{center} 14670 \tt 14671 L0 14672\end{center} 14673Output: 14674\begin{center} 14675 \tt 14676 [] 14677\end{center} 14678Input: 14679\begin{center} 14680 \tt 14681 L0[5] := 16 14682\end{center} 14683then: 14684\begin{center} 14685 \tt 14686 L0 14687\end{center} 14688Output: 14689\begin{center} 14690 \tt 14691 [0,0,0,0,0,16] 14692\end{center} 14693 14694You can also define a list with the \texttt{makelist} command.\\ 14695Input: 14696\begin{center} 14697 \tt 14698 makelist(4,1,3) 14699\end{center} 14700creates a list with entries 4, from integers 1 to 3. This is the same 14701as \texttt{[4 \$ 3]}.\\ 14702Output: 14703\begin{center} 14704 \tt 14705 [4,4,4] 14706\end{center} 14707Input: 14708\begin{center} 14709 \tt 14710 makelist(4,2,7) 14711\end{center} 14712creates a list with entries 4, from integers 2 to 7. This is the same 14713as \texttt{[4 \$ 6]}.\\ 14714Output: 14715\begin{center} 14716 \tt 14717 [4,4,4,4,4,4] 14718\end{center} 14719Input: 14720\begin{center} 14721 \tt 14722 makelist(x -> x\^{}2,1,10,2) 14723\end{center} 14724creates a list of the squares of the numbers, starting at 1, ending at 1472510, and going in steps of 2. This is the same as 14726\texttt{[(k\^{}2) \$ (k = 1..10,2)]}. 14727Output: 14728\begin{center} 14729 \tt 14730 [1,9,25,49,81] 14731\end{center} 14732 14733\subsection{Flatten a list: \texttt{flatten}}\index{flatten} 14734 14735The \texttt{flatten} takes a list as argument.\\ 14736\texttt{flatten} returns a list which is the result of recursively 14737replacing any elements that are lists by the elements, resulting in a 14738list with no lists as elements.\\ 14739Input: 14740\begin{center} 14741 \tt 14742 flatten([[1,[2,3],4],[5,6]]) 14743\end{center} 14744Output: 14745\begin{center} 14746 \tt 14747 [1,2,3,4,5,6] 14748\end{center} 14749If the original list is a matrix, you can use the \texttt{mat2list} 14750command for this (see section \ref{ssec:mat2list}). 14751 14752\subsection{Get an element or a sub-list of a list : {\tt at []}}\index{at|textbf}\label{sec:at} 14753\subsubsection{Get an element} 14754\noindent The $n$-th element of a list {\tt l} of size $s$ 14755is addressed by {\tt l[n]} where $n$ is in $[0..s-1]$ or $[1..s]$. 14756The equivalent prefixed function is 14757{\tt at}, which takes as argument a list and an integer {\tt n}.\\ 14758{\tt at} returns the element of the list at index {\tt n}.\\ 14759Input : 14760\begin{center}{\tt [0,1,2][1]}\end{center} 14761or : 14762\begin{center}{\tt at([0,1,2],1)}\end{center} 14763Output : 14764\begin{center}{\tt 1}\end{center} 14765 14766\subsubsection{Extract a sub-list} 14767If $l$ is a list of size $s$, {\tt l[n1..n2]} returns the list 14768extracted from {\tt l} containing the elements of indexes $n_1$ to $n_2$ 14769where $0 \leq n_1\leq n_2 < s$ (in Xcas syntax mode) or 14770$0 < n_1\leq n_2 \leq s$ in other syntax modes. 14771The equivalent prefixed function is 14772{\tt at} with a list and an interval of integers ({\tt n1..n2}) 14773as arguments.\\ 14774{\bf See also} : {\tt mid}, section \ref{sec:mid}.\\ 14775Input : 14776\begin{center}{\tt [0,1,2,3,4][1..3]}\end{center} 14777or : 14778\begin{center}{\tt at([0,1,2,3,4],1..3)}\end{center} 14779Output : 14780\begin{center}{\tt [1,2,3]}\end{center} 14781{\bf Warning}\\ 14782{\tt at} can not be used for sequences, index notation 14783must be used, as in {\tt (0,1,2,3,4,5)[2..3]}. 14784 14785 14786\subsection{Extract a sub-list : {\tt mid}}\index{mid}\label{sec:mid} 14787{\bf See also :} {\tt at} section \ref{sec:at}.\\ 14788\noindent{\tt mid} is used to extract a sub-list of a list\index{mid}.\\ 14789{\tt mid} takes as argument a list, the index of the beginning of the 14790sub-list and the length of the sub-list.\\ 14791{\tt mid} returns the sub-list.\\ 14792Input : 14793\begin{center}{\tt mid([0,1,2,3,4,5],2,3)}\end{center} 14794Output : 14795\begin{center}{\tt [1,2,3]}\end{center} 14796{\bf Warning}\\ 14797{\tt mid} can not be used to extract a subsequence of a sequence, 14798because the arguments of {\tt mid} would be merged with the sequence. 14799Index notation must be used, like e.g.{\tt (0,1,2,3,4,5)[2..3]}. 14800 14801\subsection{Get the first element of a list : {\tt head}}\index{head} 14802\noindent{\tt head} takes as argument a list.\\ 14803{\tt head} returns the first element of this list.\\ 14804Input : 14805\begin{center}{\tt head([0,1,2,3])}\end{center} 14806Output : 14807\begin{center}{\tt 0}\end{center} 14808{\tt a:=head([0,1,2,3])} does the same thing as {\tt a:=[0,1,2,3][0]} 14809 14810\subsection{Remove an element in a list : {\tt suppress}}\index{suppress} 14811\noindent{\tt suppress} takes as argument a list and an integer {\tt n}.\\ 14812{\tt suppress} returns the list where the element of index {\tt n} is 14813removed.\\ 14814Input : 14815\begin{center}{\tt suppress([3,4,2],1)}\end{center} 14816Output : 14817\begin{center}{\tt [3,2]}\end{center} 14818 14819\subsection{Insert an element into a list or a string: 14820\texttt{insert}}\index{insert} 14821 14822The \texttt{insert} command takes three arguments, a list (or string), 14823and index, and an element.\\ 14824\texttt{insert} returns the list (or string) with the element inserted 14825into the position given by the index, with all other elements shifted 14826to the right.\\ 14827Input: 14828\begin{center} 14829 \tt 14830 insert([3,4,2],2,5) 14831\end{center} 14832Output: 14833\begin{center} 14834 \tt 14835 [3,4,5,2] 14836\end{center} 14837Input: 14838\begin{center} 14839 \tt 14840 insert("342",2,"5") 14841\end{center} 14842Output: 14843\begin{center} 14844 \tt 14845 "3452" 14846\end{center} 14847\texttt{insert} returns an error if the index is too large.\\ 14848Input: 14849\begin{center} 14850 \tt 14851 insert([3,4,2],4,5) 14852\end{center} 14853Output: 14854\begin{center} 14855 \tt 14856 insert([3,4,2],4,5) 14857 Error: Invalid dimension 14858\end{center} 14859 14860\subsection{Remove the first element : {\tt tail}}\index{tail} 14861\noindent{\tt tail} takes as argument a list. 14862{\tt tail} returns the list without its first element.\\ 14863Input : 14864\begin{center}{\tt tail([0,1,2,3])}\end{center} 14865Output : 14866\begin{center}{\tt [1,2,3]}\end{center} 14867{\tt l:=tail([0,1,2,3])} does the same thing as 14868{\tt l:=suppress([0,1,2,3],0)}\\ 14869 14870\subsection{The right and left portions of a list: \texttt{right, 14871left}}\index{right}\index{left} 14872 14873The \texttt{right} command takes two arguments, a list and an integer $n$.\\ 14874\texttt{right} returns the last $n$ elements of the list.\\ 14875Input: 14876\begin{center} 14877 \tt 14878 right([0,1,2,3,4,5,6,7,8],4) 14879\end{center} 14880Output: 14881\begin{center} 14882 \tt 14883 [5,6,7,8] 14884\end{center} 14885 14886Similarly, \texttt{left} returns the first part of a list.\\ 14887Input: 14888\begin{center} 14889 \tt 14890 left([0,1,2,3,4,5,6,7,8],3) 14891\end{center} 14892Output: 14893\begin{center} 14894 \tt 14895 [0,1,2] 14896\end{center} 14897 14898\subsection{Reverse order in a list : {\tt revlist}}\index{revlist} 14899\noindent{\tt revlist} takes as argument a list (resp. sequence).\\ 14900{\tt revlist} returns the list (resp. sequence) in the reverse order.\\ 14901Input : 14902\begin{center}{\tt revlist([0,1,2,3,4])}\end{center} 14903Output : 14904\begin{center}{\tt [4,3,2,1,0]}\end{center} 14905Input : 14906\begin{center}{\tt revlist([0,1,2,3,4],3)}\end{center} 14907Output : 14908\begin{center}{\tt 3,[0,1,2,3,4]}\end{center} 14909 14910\subsection{Reverse a list starting from its n-th element : {\tt rotate}}\index{rotate} 14911\noindent{\tt rotate} takes as argument a list and an integer {\tt n} (by 14912default {\tt n=-1}).\\ 14913{\tt rotate} rotates the list by {\tt n} places to the left if {\tt n>0} 14914or to the right if {\tt n<0}. Elements leaving the list from one 14915side come back on the other side. 14916By default {\tt n=-1} and the last element becomes first.\\ 14917Input : 14918\begin{center}{\tt rotate([0,1,2,3,4])}\end{center} 14919Output : 14920\begin{center}{\tt [4,0,1,2,3]}\end{center} 14921Input : 14922\begin{center}{\tt rotate([0,1,2,3,4],2)}\end{center} 14923Output : 14924\begin{center}{\tt [2,3,4,0,1]}\end{center} 14925Input : 14926\begin{center}{\tt rotate([0,1,2,3,4],-2)}\end{center} 14927Output : 14928\begin{center}{\tt [3,4,0,1,2]}\end{center} 14929 14930\subsection{Permuted list from its n-th element : {\tt shift}}\index{shift} 14931\noindent{\tt shift} takes as argument a list {\tt l} and an integer {\tt n} 14932(by default {\tt n}=-1).\\ 14933{\tt shift} rotates the list to the left if {\tt n>0} or to 14934the right if {\tt n<0}. Elements leaving the list from one side 14935are replaced by {\tt undef} on the other side.\\ 14936Input : 14937\begin{center}{\tt shift([0,1,2,3,4])}\end{center} 14938Output : 14939\begin{center}{\tt [undef,0,1,2,3]}\end{center} 14940Input : 14941\begin{center}{\tt shift([0,1,2,3,4],2)}\end{center} 14942Output : 14943\begin{center}{\tt [2,3,4,undef,undef]}\end{center} 14944Input : 14945\begin{center}{\tt shift([0,1,2,3,4],-2)}\end{center} 14946Output : 14947\begin{center}{\tt [undef,undef,0,1,2]}\end{center} 14948 14949\subsection{Modify an element in a list : {\tt subsop}}\index{subsop} 14950\label{ssec:subsop} 14951 14952\noindent {\tt subsop} modifies an element in a list. 14953%? directly (it is not necessary to store this element in a variable).\\ 14954{\tt subsop} takes as argument a list and an equality (an index=a new 14955value) in all syntax modes, but in {\tt Maple} syntax mode 14956the order of the arguments is reversed. \\ 14957{\bf Remark} If the second argument is {\tt 'k=NULL'}, the element of index 14958{\tt k} is removed of the list.\\ 14959Input in {\tt Xcas} mode (the index of the first element is 0) : 14960\begin{center}{\tt subsop([0,1,2],1=5)}\end{center} 14961or : 14962\begin{center}{\tt L:=[0,1,2];L[1]:=5}\end{center} 14963Output : 14964\begin{center}{\tt [0,5,2]}\end{center} 14965Input in {\tt Xcas} mode (the index of the first element is 0) : 14966\begin{center}{\tt subsop([0,1,2],'1=NULL')}\end{center} 14967Output : 14968\begin{center}{\tt [0,2]}\end{center} 14969 14970Input in {\tt Mupad TI} mode (the index of the first element is 1) : 14971\begin{center}{\tt subsop([0,1,2],2=5)}\end{center} 14972or : 14973\begin{center}{\tt L:=[0,1,2];L[2]:=5}\end{center} 14974Output : 14975\begin{center}{\tt [0,5,2]}\end{center} 14976In {\tt Maple} mode the arguments are permuted and the index of 14977the first element is 1.\\ 14978Input : 14979\begin{center}{\tt subsop(2=5,[0,1,2])}\end{center} 14980or : 14981\begin{center}{\tt L:=[0,1,2];L[2]:=5}\end{center} 14982Output : 14983\begin{center}{\tt [0,5,2]}\end{center} 14984 14985\subsection{Transform a list into a sequence : {\tt op makesuite}}\index{op}\index{makesuite} 14986\noindent{\tt op} or {\tt makesuite} takes as argument a list.\\ 14987{\tt op} or {\tt makesuite} transforms this list into a sequence. 14988\label{sec:makesuiteop}\\ 14989See \ref{sec:op} for other usages of {\tt op}.\\ 14990Input : 14991\begin{center}{\tt op([0,1,2])}\end{center} 14992or : 14993\begin{center}{\tt makesuite([0,1,2])}\end{center} 14994Output : 14995\begin{center}{\tt (0,1,2)}\end{center} 14996 14997\subsection{Transform a sequence into a list : {\tt makevector []}}\index{makevector}\index{[]} 14998Square brackets put around a sequence transform this sequence into a list 14999or vector. The equivalent prefixed function is 15000{\tt makevector} which takes a sequence as argument.\\ 15001{\tt makevector} transforms this sequence into a list or vector.\\ 15002Input : 15003\begin{center}{\tt makevector(0,1,2)}\end{center} 15004Output : 15005\begin{center}{\tt [0,1,2]}\end{center} 15006Input : 15007\begin{center}{\tt a:=(0,1,2)}\end{center} 15008Input : 15009\begin{center}{\tt [a]}\end{center} 15010or : 15011\begin{center}{\tt makevector(a)}\end{center} 15012Output : 15013\begin{center}{\tt [0,1,2]}\end{center} 15014 15015\subsection{Length of a list : {\tt size nops length}}\index{size}\index{nops}\index{length} 15016\noindent{\tt size} or {\tt nops} or {\tt length} takes as argument a list 15017(resp. sequence).\\ 15018{\tt size} or {\tt nops} or {\tt length} returns the length of this list (resp. 15019 sequence).\\ 15020Input : 15021\begin{center}{\tt nops([3,4,2])}\end{center} 15022or : 15023\begin{center}{\tt size([3,4,2])}\end{center} 15024or : 15025\begin{center}{\tt length([3,4,2])}\end{center} 15026Output : 15027\begin{center}{\tt 3}\end{center} 15028 15029\subsection{Sizes of a list of lists : {\tt sizes}}\index{sizes} 15030\noindent {\tt sizes} takes as argument a list of lists.\\ 15031{\tt sizes} returns the list of the lengths of these lists.\\ 15032Input : 15033\begin{center}{\tt sizes([[3,4],[2]])}\end{center} 15034Output : 15035\begin{center}{\tt [2,1]}\end{center} 15036 15037\subsection{Concatenate two lists or a list and an element : {\tt concat augment}}\index{concat|textbf}\index{augment|textbf} 15038\noindent{\tt concat} (or {\tt augment}) takes as argument a list and an 15039element or two lists.\\ 15040{\tt concat} (or {\tt augment}) concats this list and this element, or concats 15041these two lists.\\ 15042Input : 15043\begin{center}{\tt concat([3,4,2],[1,2,4])}\end{center} 15044or : 15045\begin{center}{\tt augment([3,4,2],[1,2,4])}\end{center} 15046Output : 15047\begin{center}{\tt [3,4,2,1,2,4]}\end{center} 15048Input : 15049\begin{center}{\tt concat([3,4,2],5)}\end{center} 15050or : 15051\begin{center}{\tt augment([3,4,2],5)}\end{center} 15052Output : 15053\begin{center}{\tt [3,4,2,5]}\end{center} 15054{\bf Warning} 15055If you input : 15056\begin{center}{\tt concat([[3,4,2]],[[1,2,4]])}\end{center} 15057or 15058\begin{center}{\tt augment([[3,4,2]],[[1,2,4]])}\end{center} 15059the output will be: 15060\begin{center}{\tt [[3,4,2,1,2,4]]}\end{center} 15061 15062\subsection{Append an element at the end of a list : {\tt append}}\index{append} 15063\noindent{\tt append} takes as argument a list and an element.\\ 15064{\tt append} puts this element at the end of this list.\\ 15065Input : 15066\begin{center}{\tt append([3,4,2],1)}\end{center} 15067Output : 15068\begin{center}{\tt [3,4,2,1]}\end{center} 15069Input : 15070\begin{center}{\tt append([1,2],[3,4])}\end{center} 15071Output : 15072\begin{center}{\tt [1,2,[3,4]]}\end{center} 15073 15074\subsection{Prepend an element at the beginning of a list : {\tt prepend}}\index{prepend} 15075\noindent{\tt prepend} takes as argument a list and an element.\\ 15076{\tt prepend} puts this element at the beginning of this list.\\ 15077Input : 15078\begin{center}{\tt prepend([3,4,2],1)}\end{center} 15079Output : 15080\begin{center}{\tt [1,3,4,2]}\end{center} 15081Input : 15082\begin{center}{\tt prepend([1,2],[3,4])}\end{center} 15083Output : 15084\begin{center}{\tt [[3,4],1,2]}\end{center} 15085 15086\subsection{Sort : {\tt sort}}\index{sort} 15087\noindent{\tt sort} takes as argument a list or an expression. 15088\begin{itemize} 15089\item For a list,\\ 15090{\tt sort} returns the list sorted in increasing order.\\ 15091Input : 15092\begin{center}{\tt sort([3,4,2])}\end{center} 15093Output : 15094\begin{center}{\tt [2,3,4]}\end{center} 15095 15096\item For an expression,\\ 15097 {\tt sort} sorts and collects terms in sums and products.\\ 15098Input : 15099\begin{center}{\tt sort(exp(2*ln(x))+x*y-x+y*x+2*x)}\end{center} 15100Output : 15101\begin{center}{\tt 2*x*y+exp(2*ln(x))+x}\end{center} 15102Input : 15103\begin{center}{\tt simplify(exp(2*ln(x))+x*y-x+y*x+2*x)}\end{center} 15104Output : 15105\begin{center}{\tt x\verb|^|2+2*x*y+x}\end{center} 15106\end{itemize} 15107{\tt sort} accepts an optional second argument, which is a bivariate 15108function returning 0 or 1. If provided, this function 15109will be used to sort the list, for example 15110{\tt (x,y)->x>=y} may be used as second argument 15111to sort the list in decreasing order. 15112This may also be used to sort list of lists 15113(that {\tt sort} with one argument does not know how to sort).\\ 15114Input : 15115\begin{center}{\tt sort([3,4,2],(x,y)->x>=y)}\end{center} 15116Output : 15117\begin{center}{\tt [4,3,2]}\end{center} 15118 15119\subsection{Sort a list by increasing order : {\tt SortA}}\index{SortA} 15120\noindent{\tt SortA} takes as argument a list.\\ 15121{\tt SortA} returns this list sorted by increasing order.\\ 15122Input : 15123\begin{center}{\tt SortA([3,4,2])}\end{center} 15124Output : 15125\begin{center}{\tt [2,3,4]}\end{center} 15126{\tt SortA} may have a matrix as argument and in this case, 15127{\tt SortA} modifies the order of columns by sorting the first matrix 15128row by increasing order.\\ 15129Input : 15130\begin{center}{\tt SortA([[3,4,2],[6,4,5]])}\end{center} 15131Output : 15132\begin{center}{\tt [[2,3,4],[5,6,4]]}\end{center} 15133 15134\subsection{Sort a list by decreasing order : {\tt SortD}}\index{SortD} 15135\noindent{\tt SortD} takes a list as argument.\\ 15136{\tt SortD} returns this list sorted by decreasing order.\\ 15137Input : 15138\begin{center}{\tt SortD([3,4,2])}\end{center} 15139Output : 15140\begin{center}{\tt [2,3,4]}\end{center} 15141{\tt SortD} may have a matrix as argument and in this case, 15142{\tt SortD} modifies the order of columns by sorting the first matrix 15143row by decreasing order.\\ 15144Input : 15145\begin{center}{\tt SortD([[3,4,2],[6,4,5]])}\end{center} 15146Output : 15147\begin{center}{\tt [[4,3,2],[4,6,5]]}\end{center} 15148 15149\subsection{Select the elements of a list : {\tt select}}\index{select} 15150\noindent{\tt select} takes as arguments : a boolean function {\tt f} 15151 and a list {\tt L}.\\ 15152{\tt select} selects in the list {\tt L}, the elements {\tt c} such that 15153{\tt f(c)==true}.\\ 15154Input : 15155\begin{center}{\tt select(x->(x>=2),[0,1,2,3,1,5])}\end{center} 15156Output : 15157\begin{center}{\tt [2,3,5]}\end{center} 15158 15159\subsection{Remove elements of a list : {\tt remove}}\index{remove} 15160\noindent{\tt remove} takes as argument : a boolean function {\tt f} and 15161a list {\tt L}.\\ 15162{\tt remove} removes in the list {\tt L}, the elements {\tt c} such that 15163{\tt f(c)==true}.\\ 15164Input : 15165\begin{center}{\tt remove(x->(x>=2),[0,1,2,3,1,5])}\end{center} 15166Output : 15167\begin{center}{\tt [0,1,1]}\end{center} 15168{\bf Remark} The same applies on strings, for example, 15169to remove all the "a" of a string:\\ 15170Input : 15171\begin{center}{\tt ord("a")}\end{center} 15172Output : 15173\begin{center}{\tt 97}\end{center} 15174Input : 15175\begin{verbatim} 15176f(chn):={ 15177 local l:=length(chn)-1; 15178 return remove(x->(ord(x)==97),seq(chn[k],k,0,l)); 15179} 15180\end{verbatim} 15181Then, input : 15182\begin{center}{\tt f("abracadabra")}\end{center} 15183Output : 15184\begin{center}{\tt ["b","r","c","d","b","r"]}\end{center} 15185To get a string, input : 15186\begin{center}{\tt char(ord(["b","r","c","d","b","r"])}\end{center} 15187Output : 15188\begin{center}{\tt "brcdbr"}\end{center} 15189 15190\subsection{Test if a value is in a list : {\tt member}}\index{member|textbf} 15191\noindent{\tt member} takes as argument a value {\tt c} and a list 15192(or a set) {\tt L}.\\ 15193{\tt member} is a function that tests if {\tt c} is an element of the 15194list {\tt L}.\\ 15195{\tt member} returns {\tt 0} if {\tt c} is not in {\tt L}, or 15196a strictly positive integer which is 151971 plus the index of the first occurrence of {\tt c} in {\tt L}.\\ 15198Note the order of the arguments (required for compatibility reasons)\\ 15199Input : 15200\begin{center}{\tt member(2,[0,1,2,3,4,2])}\end{center} 15201Output : 15202\begin{center}{\tt 3}\end{center} 15203Input : 15204\begin{center}{\tt member(2,\%\{0,1,2,3,4,2\%\})}\end{center} 15205Output : 15206\begin{center}{\tt 3}\end{center} 15207 15208\subsection{Test if a value is in a list : {\tt contains}}\index{contains|textbf} 15209\noindent{\tt contains} takes as argument a list (or a set) 15210{\tt L} and a value {\tt c}.\\ 15211{\tt contains} tests if {\tt c} is an element of the list {\tt L}.\\ 15212 {\tt contains} returns {\tt 0} if {\tt c} is not in {\tt L}, 15213or a strictly positive integer which is 152141+the index of the first occurrence of {\tt c} in {\tt L}.\\ 15215Input : 15216\begin{center}{\tt contains([0,1,2,3,4,2],2)}\end{center} 15217Output : 15218\begin{center}{\tt 3}\end{center} 15219Input : 15220\begin{center}{\tt contains(\%\{0,1,2,3,4,2\%\},2)}\end{center} 15221Output : 15222\begin{center}{\tt 3}\end{center} 15223 15224\subsection{Sum of list (or matrix) elements 15225transformed by a function : {\tt count}}\index{count|textbf} 15226\noindent{\tt count} takes as argument : a real function {\tt f} and a list 15227{\tt l} of length {\tt n} (or a matrix {\tt A} of dimension {\tt p*q}).\\ 15228{\tt count} applies the function to the list (or matrix) elements and returns 15229their sum, i.e. :\\ 15230{\tt count(f,l)} returns {\tt f(l[0])+f(l[1])+...+f(l[n-1])} or\\ 15231{\tt count(f,A)} returns {\tt f(A[0,0])+....+f(A[p-1,q-1])}.\\ 15232If {\tt f} is a boolean function {\tt count} returns the number of elements 15233of the list (or of the matrix) for which the boolean function is true.\\ 15234Input : 15235\begin{center}{\tt count((x)->x,[2,12,45,3,7,78])}\end{center} 15236Output : 15237\begin{center}{\tt 147}\end{center} 15238because : 2+12+45+3+7+78=147.\\ 15239Input : 15240\begin{center}{\tt count((x)->x<12,[2,12,45,3,7,78])}\end{center} 15241Output : 15242\begin{center}{\tt 3}\end{center} 15243Input : 15244\begin{center}{\tt count((x)->x==12,[2,12,45,3,7,78])}\end{center} 15245Output : 15246\begin{center}{\tt 1}\end{center} 15247Input : 15248\begin{center}{\tt count((x)->x>12,[2,12,45,3,7,78])}\end{center} 15249Output : 15250\begin{center}{\tt 2}\end{center} 15251Input : 15252\begin{center}{\tt count(x->x\verb|^|2,[3,5,1])}\end{center} 15253Output : 15254\begin{center}{\tt 35}\end{center} 15255Indeed $3^2+5^2+1^1=35$.\\ 15256Input : 15257\begin{center}{\tt count(id,[3,5,1])}\end{center} 15258Output : 15259\begin{center}{\tt 9}\end{center} 15260Indeed, {\tt id} is the identity functions and 3+5+1=9.\\ 15261Input : 15262\begin{center}{\tt count(1,[3,5,1])}\end{center} 15263Output : 15264\begin{center}{\tt 3}\end{center} 15265Indeed, {\tt 1} is the constant function equal to 1 and 1+1+1=3. 15266 15267\subsection{Number of elements equal to a given value : {\tt count\_eq}}\index{count\_eq|textbf} 15268\noindent{\tt count\_eq} takes as argument : a real and a real list 15269(or matrix).\\ 15270{\tt count\_eq} returns the number of elements of the list (or matrix) 15271which are equal to the first argument.\\ 15272Input : 15273\begin{center}{\tt count\_eq(12,[2,12,45,3,7,78])}\end{center} 15274Output : 15275\begin{center}{\tt 1}\end{center} 15276 15277\subsection{Number of elements smaller than a given value : {\tt count\_inf}}\index{count\_inf|textbf} 15278\noindent{\tt count\_inf} takes as argument : a real and a real list 15279(or matrix).\\ 15280{\tt count\_inf} returns the number of elements of the list (or matrix) which 15281are strictly less than the first argument.\\ 15282Input : 15283\begin{center}{\tt count\_inf(12,[2,12,45,3,7,78])}\end{center} 15284Output : 15285\begin{center}{\tt 3}\end{center} 15286 15287\subsection{Number of elements greater than a given value : {\tt count\_sup}}\index{count\_sup|textbf} 15288\noindent{\tt count\_sup} takes as argument : a real and a real list 15289(or matrix).\\ 15290{\tt count\_sup} returns the number of elements of the list 15291(or matrix) which are strictly greater than the first argument.\\ 15292Input : 15293\begin{center}{\tt count\_sup(12,[2,12,45,3,7,78])}\end{center} 15294Output : 15295\begin{center}{\tt 2}\end{center} 15296 15297\subsection{Sum of elements of a list : {\tt sum add}}\index{sum}\index{add} 15298\noindent{\tt sum} or {\tt add} takes as argument a list {\tt l} (resp. 15299sequence) of reals.\\ 15300{\tt sum} or {\tt add} returns the sum of the elements of {\tt l}.\\ 15301Input : 15302\begin{center}{\tt sum(2,3,4,5,6)}\end{center} 15303Output : 15304\begin{center}{\tt 20}\end{center} 15305 15306\subsection{Cumulated sum of the elements of a list : {\tt cumSum}}\index{cumSum|textbf} 15307\noindent{\tt cumSum} takes as argument a list {\tt l} (resp. sequence) 15308of numbers or of strings.\\ 15309{\tt cumSum} returns the list (resp. sequence) with same length as {\tt l} and 15310 with $k$-th element the sum (or concatenation) of the 15311elements ${\tt l[0],..,l[k]}$.\\ 15312Input : 15313\begin{center}{\tt cumSum(sqrt(2),3,4,5,6)}\end{center} 15314Output : 15315\begin{center}{\tt sqrt(2),3+sqrt(2),3+sqrt(2)+4,3+sqrt(2)+4+5,}\end{center} 15316\begin{center}{\tt 3+sqrt(2)+4+5+6}\end{center} 15317Input : 15318\begin{center}{\tt normal(cumSum(sqrt(2),3,4,5,6))}\end{center} 15319Output : 15320\begin{center}{\tt sqrt(2),sqrt(2)+3,sqrt(2)+7,sqrt(2)+12,sqrt(2)+18}\end{center}Input : 15321\begin{center}{\tt cumSum(1.2,3,4.5,6)}\end{center} 15322Output : 15323\begin{center}{\tt 1.2,4.2,8.7,14.7}\end{center} 15324Input : 15325\begin{center}{\tt cumSum([0,1,2,3,4])}\end{center} 15326Output : 15327\begin{center}{\tt [0,1,3,6,10]}\end{center} 15328Input : 15329\begin{center}{\tt cumSum("a","b","c","d")}\end{center} 15330Output : 15331\begin{center}{\tt "a","ab","abc","abcd"}\end{center} 15332Input : 15333\begin{center}{\tt cumSum("a","ab","abc","abcd")}\end{center} 15334Output : 15335\begin{center}{\tt "a","aab","aababc","aababcabcd"}\end{center} 15336 15337\subsection{Product : {\tt product mul}}\index{product|textbf}\index{mul|textbf} 15338See also \ref{sec:product}, \ref{sec:product1} and 15339\ref{sec:product2}). 15340 15341\subsubsection{Product of values of an expression : {\tt product}}\label{sec:product0} 15342\noindent{\tt product(expr,var,a,b,p)} or {\tt mul(expr,var,a,b,p)} returns the 15343product of values of an expression {\tt ex} when the variable {\tt var} goes 15344from {\tt a} to {\tt b} with a step {\tt p} (by default p=1) : this syntax is 15345for compatibility with Maple.\\ 15346Input : 15347\begin{center}{\tt product(x\verb|^|2+1,x,1,4)}\end{center} 15348or: 15349\begin{center}{\tt mul(x\verb|^|2+1,x,1,4)}\end{center} 15350Output : 15351\begin{center}{\tt 1700}\end{center} 15352Indeed $2*5*10*17=1700$\\ 15353Input : 15354\begin{center}{\tt product(x\verb|^|2+1,x,1,5,2)}\end{center} 15355or: 15356\begin{center}{\tt mul(x\verb|^|2+1,x,1,5,2)}\end{center} 15357Output : 15358\begin{center}{\tt 520}\end{center} 15359Indeed $2*10*26=520$ 15360 15361\subsubsection{Product of elements of a list : {\tt product}} 15362\noindent{\tt product} or {\tt mul} takes as argument a list {\tt l} 15363of reals (or floating numbers) or two lists of the same size (see 15364also \ref{sec:product0}, \ref{sec:product1} and \ref{sec:product2}). 15365\begin{itemize} 15366\item if {\tt product} or {\tt mul} has a list {\tt l} 15367as argument, {\tt product} or 15368{\tt mul} returns the product of the elements of {\tt l}\label{sec:product}.\\ 15369Input : 15370\begin{center}{\tt product([2,3,4])}\end{center} 15371or : 15372\begin{center}{\tt mul([2,3,4])}\end{center} 15373Output : 15374\begin{center}{\tt 24}\end{center} 15375Input : 15376\begin{center}{\tt product([[2,3,4],[5,6,7]])}\end{center} 15377Output : 15378\begin{center}{\tt [10,18,28]}\end{center} 15379\item if {\tt product} or {\tt mul} takes as arguments 15380{\tt l1} and {\tt l2} 15381(two lists or two matrices), {\tt product} or {\tt mul} returns 15382the term by term product of the elements of {\tt l1} and 15383{\tt l2}.\\ 15384Input : 15385\begin{center}{\tt product([2,3,4],[5,6,7])}\end{center} 15386or : 15387\begin{center}{\tt mul([2,3,4],[5,6,7])}\end{center} 15388Output : 15389\begin{center}{\tt [10,18,28]}\end{center} 15390Input : 15391\begin{center}{\tt product([[2,3,4],[5,6,7]],[[2,3,4],[5,6,7]])}\end{center} 15392or : 15393\begin{center}{\tt mul([[2,3,4],[5,6,7]],[[2,3,4],[5,6,7]])}\end{center} 15394Output : 15395\begin{center}{\tt [[4,9,16],[25,36,49]]}\end{center} 15396\end{itemize} 15397 15398\subsection{Apply a function of one variable to the elements of a list : {\tt map apply of}}\index{map}\index{apply}\index{of} 15399\noindent{\tt map} or {\tt apply} or {\tt of} applies a function to a list 15400of elements.\\ 15401{\tt of} is the prefixed function equivalent to the parenthesis : 15402{\tt Xcas} translates {\tt f(x)} internally to {\tt of(f,x)}. 15403It is more natural to call {\tt map} 15404or {\tt apply} than {\tt of}. Be careful with the order of arguments 15405(that is required for compatibility reasons).\\ 15406Note that {\tt apply} returns a list ({\tt []}) 15407even if the second argument is not a list.\\ 15408Input : 15409\begin{center}{\tt apply(x->x\verb|^|2,[3,5,1])}\end{center} 15410or : 15411\begin{center}{\tt of(x->x\verb|^|2,[3,5,1])}\end{center} 15412or : 15413\begin{center}{\tt map([3,5,1],x->x\verb|^|2)}\end{center} 15414or first define the function $h(x)=x^2$, input : 15415\begin{center}{\tt h(x):=x\verb|^|2}\end{center} 15416then : 15417\begin{center}{\tt apply(h,[3,5,1])}\end{center} 15418or : 15419\begin{center}{\tt of(h,[3,5,1])}\end{center} 15420or : 15421\begin{center}{\tt map([3,5,1],h)}\end{center} 15422Output : 15423\begin{center}{\tt [9,25,1]}\end{center} 15424Next example, define the function $g(x)=[x,x^2,x^3]$, input : 15425\begin{center}{\tt g:=(x)->[x,x\verb|^|2,x\verb|^|3]}\end{center} 15426then : 15427\begin{center}{\tt apply(g,[3,5,1])}\end{center} 15428or : 15429\begin{center}{\tt of(g,[3,5,1])}\end{center} 15430or : 15431\begin{center}{\tt map([3,5,1],g)}\end{center} 15432Output : 15433\begin{center}{\tt [[3,9,27],[5,25,125],[1,1,1]]}\end{center} 15434{\bf Warning!!!} first purge {\tt x} if {\tt x} is not symbolic.\\ 15435Note that if {\tt l1,l2,l3} are lists 15436{\tt sizes([l1,l2,l3])} is equivalent to {\tt map(size,[l1,l2,l3])}. 15437 15438\subsection{Apply a bivariate function to the elements of two lists : {\tt zip}}\index{zip} 15439\noindent{\tt zip} applies a bivariate function to the elements of 2 lists.\\ 15440Input : 15441\begin{center}{\tt zip('sum',[a,b,c,d],[1,2,3,4])}\end{center} 15442Output : 15443\begin{center}{\tt [a+1,b+2,c+3,d+4]}\end{center} 15444Input : 15445\begin{center}{\tt zip((x,y)->x\verb|^|2+y\verb|^|2,[4,2,1],[3,5,1])}\end{center} 15446or : 15447\begin{center}{\tt f:=(x,y)->x\verb|^|2+y\verb|^|2}\end{center} 15448then, 15449\begin{center}{\tt zip(f,[4,2,1],[3,5,1])}\end{center} 15450Output : 15451\begin{center}{\tt [25,29,2]}\end{center} 15452Input : 15453\begin{center}{\tt f:=(x,y)->[x\verb|^|2+y\verb|^|2,x+y]}\end{center} 15454then : 15455\begin{center}{\tt zip(f,[4,2,1],[3,5,1])}\end{center} 15456Output : 15457\begin{center}{\tt [[25,7],[29,7],[2,2]]}\end{center} 15458 15459\subsection{Fold operators : {\tt foldl\index{foldl}}, {\tt foldr\index{foldr}}} 15460The fold operators {\tt foldl} and {\tt foldr} both take a binary operator, identifier or function $R$ as the first argument followed by an argument $I$ and an arbitrary number of arguments $a$, $b$, $c$, \ldots, like for example {\tt foldl(R,I,a,b,c,...)}. 15461 15462The left-fold operator {\tt foldl} composes a binary operator $R$ with initial value $I$ onto the arguments $a$, $b$, \ldots (which may be zero in number), associating from the left. For example, input : 15463\begin{center} 15464 \tt foldl(R,I,a,b,c) 15465\end{center} 15466Output : 15467\begin{center} 15468 \tt R(R(R(I,a),b),c) 15469\end{center} 15470 15471The right-fold operator {\tt foldr} is similar but associates operands from the right. For example, input : 15472\begin{center} 15473 \tt foldr(R,I,a,b,c) 15474\end{center} 15475Output : 15476\begin{center} 15477 \tt R(a,R(b,R(c,I))) 15478\end{center} 15479 15480\subsection{Make a list with zeros : {\tt newList}}\index{newList} 15481\noindent{\tt newList(n)} makes a list of {\tt n} zeros.\\ 15482Input : 15483\begin{center}{\tt newList(3)}\end{center} 15484Output : 15485\begin{center}{\tt [0,0,0]}\end{center} 15486 15487\subsection{Make a list of integers: \texttt{range}}\index{range} 15488 15489The \texttt{range} command takes one, two or three arguments. 15490\begin{itemize} 15491 \item 15492 For one argument, the argument must be a non-negative integer $n$. 15493 \texttt{range} will then return the list \texttt{[0,1,\dots,$n-1$]}\\ 15494 Input: 15495 \begin{center} 15496 \tt 15497 range(5) 15498 \end{center} 15499 Output: 15500 \begin{center} 15501 \tt 15502 [0,1,2,3,4] 15503 \end{center} 15504 \item 15505 For two arguments, the arguments must be two numbers $a$ and $b$, 15506 with $a \le b$. \texttt{range} will then return the list 15507 \texttt{[$a$, $a+1$,\dots]} up to, but not including, $b$.\\ 15508 Input: 15509 \begin{center} 15510 \tt 15511 range(4,10) 15512 \end{center} 15513 Output: 15514 \begin{center} 15515 \tt 15516 [4,5,6,7,8,9] 15517 \end{center} 15518 \item 15519 For three arguments, the arguments must be three numbers $a$, $b$ 15520 and a non-zero $p$. \texttt{range} will then return the list 15521 \texttt{[$a$, $a+p$,$a+2p$,\dots]} up to, but not including, $b$. 15522 If $p > 0$, then $a \le b$; if $p < 0$, then $a \ge b$.\\ 15523 Input: 15524 \begin{center} 15525 \tt 15526 range(4,13,2) 15527 \end{center} 15528 Output: 15529 \begin{center} 15530 \tt 15531 [4,6,8,10,12] 15532 \end{center} 15533 Input: 15534 \begin{center} 15535 \tt 15536 range(10,4,-1) 15537 \end{center} 15538 Output: 15539 \begin{center} 15540 \tt 15541 [10,9,8,7,6,5] 15542 \end{center} 15543\end{itemize} 15544 15545The \texttt{range} command can be used to create a list of values 15546$f(k)$, where $k$ is an integer satisfying a certain condition. 15547\begin{itemize} 15548 \item You can list the values of an expression in a variable which 15549 goes over a range defined by \texttt{range}.\\ 15550 Input: 15551 \begin{center} 15552 \tt 15553 [k\^{}2 + k for k in range(10)] 15554 \end{center} 15555 Output: 15556 \begin{center} 15557 \tt 15558 [0,2,6,12,20,30,42,56,72,90] 15559 \end{center} 15560 \item YOu can list the values of an expression in a variable which 15561 goes over a range defined by \texttt{range} and which satisfies a 15562 given condition.\\ 15563 Input: 15564 \begin{center} 15565 \tt 15566 [k for k in range (4,10) if isprime(k)] 15567 \end{center} 15568 Output: 15569 \begin{center} 15570 \tt 15571 [5,7] 15572 \end{center} 15573 Input: 15574 \begin{center} 15575 \tt 15576 [k\^{}2 + k for k in range (1,10,2) if isprime(k)] 15577 \end{center} 15578 Output: 15579 \begin{center} 15580 \tt 15581 [12,30,56] 15582 \end{center} 15583\end{itemize} 15584 15585\subsection{Make a list with a function : {\tt makelist}}\index{makelist} 15586\label{ssec:makelist} 15587 15588\noindent{\tt makelist} takes as argument a function {\tt f}, 15589the bounds {\tt a,b} of an index variable and a step {\tt p} 15590(by default 1 or -1 depending on the bounds order).\\ 15591{\tt makelist} makes the list {\tt [f(a),f(a+p)...f(a+k*p)]} with $k$ such 15592that~: $a<a+k*p \leq b <a+(k+1)*p$ or $a>a+k*p \geq b >a+(k+1)*p$.\\ 15593Input : 15594\begin{center}{\tt makelist(x->x\verb|^|2,3,5)}\end{center} 15595or 15596\begin{center}{\tt makelist(x->x\verb|^|2,3,5,1)}\end{center} 15597or first define the function $h(x)=x^2$ by {\tt h(x):=x\verb|^|2} 15598then input 15599\begin{center}{\tt makelist(h,3,5,1)}\end{center} 15600Output : 15601\begin{center}{\tt [9,16,25]}\end{center} 15602Input : 15603\begin{center}{\tt makelist(x->x\verb|^|2,3,6,2)}\end{center} 15604Output : 15605\begin{center}{\tt [9,25]}\end{center} 15606{\bf Warning!!!} purge {\tt x} if {\tt x} is not symbolic. 15607 15608\subsection{Make a random vector or list : {\tt randvector}}\index{randvector} 15609\label{sec:ranm4} 15610\noindent{\tt randvector} takes as argument an integer $n$ and optionally a 15611second argument, either an integer $k$ or the quoted name of 15612a random distribution law 15613(see also \ref{sec:ranm1}).\\ %, \ref{sec:ranm4} and \ref{sec:ranm3}).\\ 15614{\tt randvector} returns a vector of size $n$ containing random integers 15615uniformly distributed between -99 and +99 (default), or between 0 and $k-1$ 15616or containing random 15617integers according to the law put between quotes.\\ 15618Input : 15619\begin{center}{\tt randvector(3)}\end{center} 15620Output : 15621\begin{center}{\tt [-54,78,-29]}\end{center} 15622Input : 15623\begin{center}{\tt randvector(3,5)}\end{center} 15624or : 15625\begin{center}{\tt randvector(3,'rand(5)')}\end{center} 15626Output : 15627\begin{center}{\tt [1,2,4]}\end{center} 15628Input : 15629\begin{center}{\tt randvector(3,'randnorm(0,1)')}\end{center} 15630Output : 15631\begin{center}{\tt [1.39091705476,-0.136794772167,0.187312440336]}\end{center} 15632Input : 15633\begin{center}{\tt randvector(3,2..4)}\end{center} 15634Output : 15635\begin{center}{\tt [3.92450003885,3.50059241243,2.7322040787]}\end{center} 15636 15637\subsection{List of differences of consecutive terms : {\tt deltalist}}\index{deltalist} 15638\noindent{\tt deltalist} takes as argument a list.\\ 15639{\tt deltalist} returns the list of the difference of all 15640pairs of consecutive terms of this list.\\ 15641Input : 15642\begin{center}{\tt deltalist([5,8,1,9])}\end{center} 15643Output : 15644\begin{center}{\tt [3,-7,8]}\end{center} 15645 15646\subsection{Make a matrix with a list : {\tt list2mat}}\index{list2mat} 15647\noindent{\tt list2mat} takes as argument a list {\tt l} and an integer 15648{\tt p}.\\ 15649{\tt list2mat} returns a matrix having {\tt p} columns 15650by cutting the list {\tt l} in rows of length {\tt p}. 15651The matrix is filled with {\tt 0}s if the size of {\tt l} is not 15652a multiple of {\tt p}.\\ 15653Input : 15654\begin{center}{\tt list2mat([5,8,1,9,5,6],2)}\end{center} 15655Output : 15656\begin{center}{\tt [[5,8],[1,9],[5,6]]}\end{center} 15657Input : 15658\begin{center}{\tt list2mat([5,8,1,9],3)}\end{center} 15659Output : 15660\begin{center}{\tt [[5,8,1],[9,0,0]]}\end{center} 15661{\bf Remark} \\ 15662{\tt Xcas} displays matrix with {\bf[} and {\bf]} and lists with $[$ and $]$ 15663as delimiters (the vertical bar of the brackets are thicker for matrices). 15664 15665\subsection{Make a list with a matrix : {\tt mat2list}}\index{mat2list} 15666\label{ssec:mat2list} 15667\noindent{\tt mat2list} takes as argument a matrix.\\ 15668{\tt mat2list} returns the list of the coefficients of this matrix.\\ 15669Input : 15670\begin{center}{\tt mat2list([[5,8],[1,9]])}\end{center} 15671Output : 15672\begin{center}{\tt [5,8,1,9]}\end{center} 15673 15674\section{Functions for vectors} 15675\subsection{Norms of a vector : {\tt maxnorm l1norm l2norm 15676norm}}\index{norm|textbf}\label{ssec:vectornorms} 15677The instructions to compute the different norm of a vector are : 15678\begin{itemize} 15679\item{\tt maxnorm} returns the ${\mathnormal{l}}^\infty$ norm of a 15680vector, 15681defined as the maximum of the absolute values of its 15682coordinates\index{maxnorm|textbf}\label{sec:maxnormv}.\\ 15683Input : 15684\begin{center}{\tt maxnorm([3,-4,2])}\end{center} 15685Output : 15686\begin{center}{\tt 4}\end{center} 15687Indeed : {\tt x=3, y=-4, z=2} and {\tt 4=max(|x|,|y|,|z|)}. 15688\item{\tt l1norm} returns the ${\tt {\mathnormal{l}}^1}$ norm of a 15689vector defined as the sum of the absolute values of its 15690coordinates\index{l1norm}\label{sec:l1normv}.\\ 15691Input : 15692\begin{center}{\tt l1norm([3,-4,2])}\end{center} 15693Output : 15694\begin{center}{\tt 9}\end{center} 15695Indeed : {\tt x=3, y=-4, z=2} and {\tt 9=|x|+|y|+|z|}. 15696\item{\tt norm} or {\tt l2norm} returns the 15697 ${\mathnormal{l}}^2$ norm of a vector defined as the square root 15698of the sum of the squares of its 15699coordinates\index{l2norm}\label{sec:l2normv}.\\ 15700Input : 15701\begin{center}{\tt norm([3,-4,2])}\end{center} 15702Output : 15703\begin{center}{\tt sqrt(29)}\end{center} 15704Indeed : {\tt x=3, y=-4, z=2} and $ 29=|x|^2+|y|^2+|z|^2$. 15705\end{itemize} 15706 15707\subsection{Normalize a vector : {\tt normalize 15708unitV}}\index{normalize|textbf}\index{unitV|textbf} 15709\noindent {\tt normalize} or {\tt unitV} takes as argument a vector.\\ 15710 {\tt normalize} or {\tt unitV} normalizes this vector for the 15711${\mathnormal{l}}^2$ norm 15712(the square root of the sum of the squares of its coordinates).\\ 15713Input : 15714\begin{center}{\tt normalize([3,4,5])}\end{center} 15715Output : 15716\begin{center}{\tt 15717[3/(5*sqrt(2)),4/(5*sqrt(2)),5/(5*sqrt(2))]}\end{center} 15718Indeed : {\tt x=3, y=4, z=5} and $ 50=|x|^2+|y|^2+|z|^2$. 15719 15720\subsection{Term by term sum of two lists : {\tt + 15721.+}}\index{+|textbf} 15722\index{.+|textbf} 15723The infixed operator {\tt +} or {\tt .+} and the prefixed operator 15724 {\tt '+'} returns the term by term sum of two lists.\\ 15725If the two lists do not have the same size, the smaller list is 15726completed with 15727zeros.\\ 15728Note the difference with sequences : if the infixed operator {\tt +} 15729or the 15730prefixed operator {\tt '+'} takes as arguments two sequences, it 15731merges the sequences, hence return the 15732sum of all the terms of the two sequences.\\ 15733Input : 15734\begin{center}{\tt [1,2,3]+[4,3,5]}\end{center} 15735or : 15736\begin{center}{\tt [1,2,3] .+[4,3,5]}\end{center} 15737or : 15738\begin{center}{\tt '+'([1,2,3],[4,3,5])}\end{center} 15739or : 15740\begin{center}{\tt '+'([[1,2,3],[4,3,5]])}\end{center} 15741Output : 15742\begin{center}{\tt [5,5,8]}\end{center} 15743Input : 15744\begin{center}{\tt [1,2,3,4,5,6]+[4,3,5]}\end{center} 15745or : 15746\begin{center}{\tt '+'([1,2,3,4,5,6],[4,3,5])}\end{center} 15747or : 15748\begin{center}{\tt '+'([[1,2,3,4,5,6],[4,3,5]])}\end{center} 15749Output : 15750\begin{center}{\tt [5,5,8,4,5,6]}\end{center} 15751{\bf Warning !}\\ 15752When the operator {\tt +} is prefixed, it should be quoted ({\tt 15753'+'}). 15754 15755\subsection{Term by term difference of two lists : {\tt - 15756.-}}\index{-|textbf} 15757\index{.-|textbf} 15758The infixed operator {\tt -} or {\tt .-} and the prefixed operator 15759{\tt '-'} returns the term by term difference of two lists.\\ 15760If the two lists do not have the same size, the smaller list is 15761completed with 15762zeros.\\ 15763Input : 15764\begin{center}{\tt [1,2,3]-[4,3,5]}\end{center} 15765or : 15766\begin{center}{\tt [1,2,3] .+ [4,3,5]}\end{center} 15767or : 15768\begin{center}{\tt '-'([1,2,3],[4,3,5])}\end{center} 15769or : 15770\begin{center}{\tt '-'([[1,2,3],[4,3,5]])}\end{center} 15771Output : 15772\begin{center}{\tt [-3,-1,-2]}\end{center} 15773{\bf Warning !}\\ 15774When the operator {\tt -} is prefixed, it should be quoted ({\tt 15775'-'}). 15776 15777\subsection{Term by term product of two lists : {\tt 15778.*}}\index{.*|textbf} 15779The infixed operator {\tt .*} returns the term by term product of two 15780lists of 15781the same size.\\ 15782Input : 15783\begin{center}{\tt [1,2,3] .* [4,3,5]}\end{center} 15784Output : 15785\begin{center}{\tt [4,6,15]}\end{center} 15786 15787\subsection{Term by term quotient of two lists : {\tt 15788./}}\index{./|textbf} 15789The infixed operator {\tt ./} returns the term by term quotient of two 15790lists 15791of the same size.\\ 15792Input : 15793\begin{center}{\tt [1,2,3] ./ [4,3,5]}\end{center} 15794Output : 15795\begin{center}{\tt [1/4,2/3,3/5]}\end{center} 15796 15797\subsection{Scalar product : {\tt scalar\_product * dotprod dot dotP 15798scalar\_Product}}\index{dot}\index{dotP}\index{dotprod}\index{scalar\_product}\index{*|textbf}\index{scalarProduct} 15799{\tt dot} or {\tt dotP} or {\tt dotprod} or {\tt scalar\_product} or 15800{\tt scalarProduct} or the infixed operator {\tt *} takes as argument 15801two vectors.\\ 15802{\tt dot} or {\tt dotP} or {\tt dotprod} or {\tt scalar\_product} or 15803{\tt scalarProduct} or {\tt *} returns the scalar product of these 15804two 15805vectors.\\ 15806Input : 15807\begin{center}{\tt dot([1,2,3],[4,3,5])}\end{center} 15808or : 15809\begin{center}{\tt scalar\_product([1,2,3],[4,3,5])}\end{center} 15810or : 15811\begin{center}{\tt [1,2,3]*[4,3,5]}\end{center} 15812or : 15813\begin{center}{\tt '*'([1,2,3],[4,3,5])}\end{center} 15814Output : 15815\begin{center}{\tt 25}\end{center} 15816Indeed {\tt 25=1*4+2*3+3*5}. 15817 15818Note that {\tt *} may be used to find the product of two polynomials 15819represented as list of their coefficients, but to avoid ambiguity, 15820the polynomial lists must be {\tt poly1[...]}. 15821 15822\subsection{Cross product : {\tt cross crossP 15823crossproduct}}\index{cross}\index{crossP}\index{crossproduct} 15824{\tt cross} or {\tt crossP} or {\tt crossproduct} takes as argument 15825two vectors.\\ 15826{\tt cross} or {\tt crossP} or {\tt crossproduct} returns the cross 15827product 15828of these two vectors.\\ 15829Input : 15830\begin{center}{\tt cross([1,2,3],[4,3,2])}\end{center} 15831Output : 15832\begin{center}{\tt [-5,10,-5]}\end{center} 15833Indeed : 15834$-5=2*2-3*3$, $ 10=-1*2+4*3$, $ -5=1*3-2*4$. 15835 15836\section{Statistics functions : {\tt mean,variance,stddev, stddevp,median,quantile,quartiles,boxwhisker}} 15837\index{mean} \index{stddev}\index{variance}\index{median}\index{stddevp}\index{quantile}\index{boxwhisker}\index{quartiles}\label{sec:statlist} 15838 15839The functions described here may be used if the statistics series 15840is contained in a list. See also section \ref{sec:statmat} for matrices. 15841%and chapter \ref{sec:stat} for weighted lists. 15842\begin{itemize} 15843\item{\tt mean} computes the arithmetic mean of a list\\ 15844Input : 15845\begin{center}{\tt mean([3,4,2])}\end{center} 15846Output : 15847\begin{center}{\tt 3}\end{center} 15848Input : 15849\begin{center}{\tt mean([1,0,1])}\end{center} 15850Output 15851\begin{center}{\tt 2/3}\end{center} 15852\item{\tt stddev} computes the standard deviation of a population, 15853if the argument is the population.\\ 15854Input : 15855\begin{center}{\tt stddev([3,4,2])}\end{center} 15856Output : 15857\begin{center}{\tt sqrt(2/3)}\end{center} 15858\item{\tt stddevp} computes an unbiased estimate of 15859the standard deviation of the population, 15860if the argument is a sample. The following 15861relation holds: 15862\begin{center} 15863 {\tt stddevp(l)\verb|^|2=size(l)*stddev(l)\verb|^|2/(size(l)-1)}. 15864\end{center} 15865Input : 15866\begin{center}{\tt stddevp([3,4,2])}\end{center} 15867Output : 15868\begin{center}{\tt 1}\end{center} 15869\item{\tt variance} computes the variance of a list, that is 15870the square of {\tt stddevp}\\ 15871Input : 15872\begin{center}{\tt variance([3,4,2])}\end{center} 15873Output : 15874\begin{center}{\tt 2/3}\end{center} 15875\item{\tt median} computes the median of a list.\\ 15876Input : 15877\begin{center}{\tt median([0,1,3,4,2,5,6])}\end{center} 15878Output : 15879\begin{center}{\tt 3.0}\end{center} 15880\item{\tt quantile} computes the deciles of a list given as first 15881argument, where the decile is the second argument.\\ 15882Input : 15883\begin{center}{\tt quantile([0,1,3,4,2,5,6],0.25)}\end{center} 15884Output the first quartile : 15885\begin{center}{\tt [1.0]}\end{center} 15886Input : 15887\begin{center}{\tt quantile([0,1,3,4,2,5,6],0.5)}\end{center} 15888Output the median : 15889\begin{center}{\tt [3.0]}\end{center} 15890Input : 15891\begin{center}{\tt quantile([0,1,3,4,2,5,6],0.75)}\end{center} 15892Output the third quartile : 15893\begin{center}{\tt [4.0]}\end{center} 15894\item{\tt quartiles} computes the minimum, the first quartile, the 15895median, the third quartile and the maximum of a list.\\ 15896Input : 15897\begin{center}{\tt quartiles([0,1,3,4,2,5,6])}\end{center} 15898Output : 15899\begin{center}{\tt [[0.0],[1.0],[3.0],[4.0],[6.0]]}\end{center} 15900\item{\tt boxwhisker} draws the whisker box of a statistics series 15901stored in a list.\\ 15902Input : 15903\begin{center}{\tt boxwhisker([0,1,3,4,2,5,6])}\end{center} 15904Output 15905\begin{center}{\tt the graph of the whisker box of this statistic list}\end{center} 15906\end{itemize} 15907{\bf Example}\\ 15908Define the list {\tt A} by: 15909\begin{center} 15910{\tt A:=[0,1,2,3,4,5,6,7,8,9,10,11]} 15911\end{center} 15912Outputs : 15913\begin{enumerate} 15914\item {\tt 11/2} for {\tt mean(A)} 15915\item 15916{\tt sqrt(143/12)} for {\tt stddev(A)} 15917\item 15918{\tt 0} for {\tt min(A)} 15919\item 15920{\tt [1.0]} for {\tt quantile(A,0.1)} 15921\item 15922{\tt [2.0]} for {\tt quantile(A,0.25)} 15923\item 15924{\tt [5.0]} for {\tt median(A)} or for {\tt quantile(A,0.5)} 15925\item 15926{\tt [8.0]} for {\tt quantile(A,0.75)} 15927\item 15928{\tt [9.0]} for {\tt quantile(A,0.9)} 15929\item 15930{\tt 11} for {\tt max(A)} 15931\item 15932{\tt [[0.0],[2.0],[5.0],[8.0],[11.0]]} for {\tt quartiles(A)} 15933\end{enumerate} 15934 15935\section{Table with strings as indexes : {\tt table}}\index{table} 15936A table is an associative container (or map), it is used to store information 15937associated to indexes which are much more general than integers, 15938like strings or sequences. It may be used for example to store 15939a table of phone numbers indexed by names.\\ 15940In {\tt Xcas}, the indexes in a table may be any kind of {\tt Xcas} 15941objects. Access is done by a binary search algorithm, where the 15942sorting function first sorts by {\tt type} then uses an order for 15943each type (e.g. $<$ for numeric types, lexicographic order for 15944strings, etc.)\\ 15945{\tt table} takes as argument a list or a sequence of equalities 15946{\tt index\_name=element\_value}.\\ 15947{\tt table} returns this table.\\ 15948Input : 15949\begin{center}{\tt T:=table(3=-10,"a"=10,"b"=20,"c"=30,"d"=40)}\end{center} 15950Input : 15951\begin{center}{\tt T["b"]}\end{center} 15952Output : 15953\begin{center}{\tt 20}\end{center} 15954Input : 15955\begin{center}{\tt T[3]}\end{center} 15956Output : 15957\begin{center}{\tt -10}\end{center} 15958{\bf Remark}\\ 15959If you assign {\tt T[n]:= ...} where {\tt T} is a variable name 15960and {\tt n} an integer 15961\begin{itemize} 15962\item if the variable name was assigned to a list or a sequence, then the 15963$n$-th element of {\tt T} is modified, 15964\item if the variable name was not assigned, a table {\tt T} 15965is created with one entry (corresponding to the index $n$). Note 15966that after the assignation {\tt T} is not a list, despite the fact that $n$ 15967was an integer. 15968\end{itemize} 15969 15970\section{Usual matrix} 15971A matrix is represented by a list of lists, all having the same size. 15972In the {\tt Xcas} answers, the matrix delimiters are {\bf []} (bold brackets). 15973For example, {\bf [}1,2,3{\bf ]} is the matrix [[1,2,3]] with only one row, 15974unlike [1,2,3] (normal brackets) which is the list [1,2,3].\\ 15975In this document, the input notation ([[1,2,3]]) will be used for input 15976and output. 15977 15978\subsection{Identity matrix : {\tt idn identity}}\index{idn}\index{identity} 15979\noindent{\tt idn} takes as argument an integer $n$ or a square matrix.\\ 15980{\tt idn} returns the identity matrix of size $n$ or of the same size 15981as the matrix argument.\\ 15982Input : 15983\begin{center}{\tt idn(2)}\end{center} 15984Output : 15985\begin{center}{\tt [[1,0],[0,1]]}\end{center} 15986Input : 15987\begin{center}{\tt idn(3)}\end{center} 15988Output : 15989\begin{center}{\tt [[1,0,0],[0,1,0],[0,0,1]]}\end{center} 15990 15991\subsection{Zero matrix : {\tt newMat matrix}}\index{newMat} 15992\noindent{\tt newMat(n,p)} or {\tt matrix(n,p)} 15993takes as argument two integers.\\ 15994{\tt newMat(n,p)} returns the zero matrix with {\tt n} rows and 15995{\tt p} columns.\\ 15996Input : 15997\begin{center}{\tt newMat(4,3)}\end{center} 15998Output : 15999\begin{center}{\tt[[0,0,0],[0,0,0],[0,0,0],[0,0,0]]}\end{center} 16000 16001\subsection{Random matrix : {\tt ranm randMat randmatrix}}\index{ranm}\index{randMat}\index{randmatrix}\label{sec:ranm2} 16002\noindent{\tt ranm} or {\tt randMat} or {\tt randmatrix} takes as argument an 16003integer $n$ or two integers $n,m$ and optionally a third argument, either an 16004integer $k$ or the quoted name of a random distribution law 16005(see also \ref{sec:ranm1} and \ref{sec:ranm4}.\\ % and \ref{sec:ranm3}).\\ 16006{\tt ranm} returns a vector of size $n$ or a matrix of size $n\times m$ 16007 containing random integers uniformly distributed between -99 and +99 16008(default), or between 0 and $k-1$ or a matrix of size $n\times m$ 16009containing random integers according to the law put between quotes.\\ 16010Input : 16011\begin{center}{\tt ranm(3)}\end{center} 16012Output : 16013\begin{center}{\tt [-54,78,-29]}\end{center} 16014Input : 16015\begin{center}{\tt ranm(2,4)}\end{center} 16016Output : 16017\begin{center}{\tt [[27,-29,37,-66],[-11,76,65,-33]]}\end{center} 16018Input : 16019\begin{center}{\tt ranm(2,4,3)}\end{center} 16020or : 16021\begin{center}{\tt ranm(2,4,'rand(3)')}\end{center} 16022Output : 16023\begin{center}{\tt [[0,1,1,0],[0,1,2,0]]}\end{center} 16024Input : 16025\begin{center}{\tt ranm(2,4,'randnorm(0,1)')}\end{center} 16026Output : 16027\begin{center}{\tt [[1.83785427742,0.793007112053,-0.978388964902,-1.88602023857], [-1.50900874199,-0.241173369698,0.311373795585,-0.532752431454]]}\end{center} 16028Input : 16029\begin{center}{\tt ranm(2,4,2..4)}\end{center} 16030Output : 16031\begin{center}{\tt [[2.00549363438,3.03381264955,2.06539073586,2.04844321217], 16032 [3.88383254968,3.28664474655,3.76909781061,2.39113253355]]}\end{center} 16033 16034 16035\subsection{Diagonal of a matrix or matrix of a diagonal : {\tt BlockDiagonal diag}}\index{diag}\index{BlockDiagonal} 16036\noindent{\tt diag} or {\tt BlockDiagonal} takes as argument a matrix $A$ or 16037a list $l$.\\ 16038{\tt diag} returns the diagonal of $A$ or the diagonal matrix with the list 16039$l$ on the diagonal (and 0 elsewhere).\\ 16040Input : 16041\begin{center}{\tt diag([[1,2],[3,4]])}\end{center} 16042Output : 16043\begin{center}{\tt [1,4]}\end{center} 16044Input : 16045\begin{center}{\tt diag([1,4])}\end{center} 16046Output : 16047\begin{center}{\tt [[1,0],[0,4]]}\end{center} 16048 16049\subsection{Jordan block : {\tt JordanBlock}}\index{JordanBlock} 16050\noindent {\tt JordanBlock} takes as argument an expression $a$ and an integer 16051$n$.\\ 16052{\tt JordanBlock} returns a square matrix of size $n$ with $a$ 16053on the principal diagonal, 1 above this diagonal and 0 elsewhere.\\ 16054Input : 16055\begin{center}{\tt JordanBlock(7,3)}\end{center} 16056Output : 16057\begin{center}{\tt [[7,1,0],[0,7,1],[0,0,7]]}\end{center} 16058 16059\subsection{Hilbert matrix : {\tt hilbert}}\index{hilbert} 16060\noindent{\tt hilbert} takes as argument an integer $n$.\\ 16061{\tt hilbert} returns the Hilbert matrix.\\ 16062 A Hilbert matrix is a square matrix of size $n$ whose elements 16063$a_{j,k}$ are : 16064\[ a_{j,k}=\frac{1}{j+k+1}, \quad 0\leq j, 0 \leq k \] 16065Input : 16066\begin{center}{\tt hilbert(4)}\end{center} 16067Output : 16068\begin{center}{\tt [[1,1/2,1/3,1/4],[1/2,1/3,1/4,1/5],[1/3,1/4,1/5,1/6], [1/4,1/5,1/6,1/7]]}\end{center} 16069 16070\subsection{Vandermonde matrix : {\tt vandermonde}}\index{vandermonde} 16071\noindent{\tt vandermonde} takes as argument a vector whose components are 16072denoted by $x_j$ for $j=0..n-1$.\\ 16073{\tt vandermonde} returns the corresponding Vandermonde matrix 16074(the $k$-th row of the matrix is the vector whose components are 16075$x_i^{k}$ for $i=0..n-1$ and $k=0..n-1$).\\ 16076{\bf Warning !}\\ 16077The indices of the rows and columns begin at 0 with {\tt Xcas}.\\ 16078Input : 16079\begin{center}{\tt vandermonde([a,2,3])}\end{center} 16080Output (if {\tt a} is symbolic else purge(a)) : 16081\begin{center}{\tt [[1,1,1],[a,2,3],[a*a,4,9]]}\end{center} 16082 16083\section{Creating matrices and extracting elements} 16084 16085\subsection{Creating matrices and modifying elements by assignment} 16086 16087You can give a matrix a name with assignment.\\ 16088Input: 16089\begin{center} 16090 \tt 16091 A := [[1,2,6], [3,4,8], [1,0,1]] 16092\end{center} 16093The rows are then accessed with indices.\\ 16094Input: 16095\begin{center} 16096 \tt 16097 A[0] 16098\end{center} 16099Output: 16100\begin{center} 16101 \tt 16102 [1,2,6] 16103\end{center} 16104Individual elements are simply elements of the rows.\\ 16105Input: 16106\begin{center} 16107 \tt 16108 A[0][1] 16109\end{center} 16110Output: 16111\begin{center} 16112 \tt 16113 2 16114\end{center} 16115This can be abbreviated by listing the row and column separated by a 16116comma.\\ 16117Input: 16118\begin{center} 16119 \tt 16120 A[0,1] 16121\end{center} 16122Output: 16123\begin{center} 16124 \tt 16125 2 16126\end{center} 16127The indexing begins with 0; if you want the indexing to begin with 1 16128by enclosing them in double brackets.\\ 16129Input: 16130\begin{center} 16131 \tt 16132 A[[1,2]] 16133\end{center} 16134Output: 16135\begin{center} 16136 \tt 16137 2 16138\end{center} 16139You can use a range of indices to get submatrices.\\ 16140Input: 16141\begin{center} 16142 \tt 16143 A[0..2,1] 16144\end{center} 16145Output: 16146\begin{center} 16147 \tt 16148 [2,4,0] 16149\end{center} 16150Input: 16151\begin{center} 16152 \tt 16153 A[0..2,1..2] 16154\end{center} 16155Output: 16156\begin{center} 16157 \tt 16158 [[2,6],[4,8],[0,1]] 16159\end{center} 16160Input: 16161\begin{center} 16162 \tt 16163 A[0..1,1..2] 16164\end{center} 16165Output: 16166\begin{center} 16167 \tt 16168 [[2,6],[4,8]] 16169\end{center} 16170An index of -1 returns the last element of a list, an index of -2 the 16171second to last element, etc.\\ 16172Input: 16173\begin{center} 16174 \tt 16175 A[-1] 16176\end{center} 16177Output: 16178\begin{center} 16179 \tt 16180 [1,0,1] 16181\end{center} 16182Input: 16183\begin{center} 16184 \tt 16185 A[1,-1] 16186\end{center} 16187Output: 16188\begin{center} 16189 \tt 16190 8 16191\end{center} 16192 16193Individual elements of a matrix can be changed by assignment.\\ 16194Input: 16195\begin{center} 16196 \tt 16197 A[0,1] := 5 16198\end{center} 16199then: 16200\begin{center} 16201 \tt 16202 A 16203\end{center} 16204Output: 16205\begin{center} 16206 \tt 16207 [[1,5,6],[3,4,8],[1,0,1]] 16208\end{center} 16209 16210\subsection{Changing a matrix by multi-assigment} 16211 16212You can use assignment to change several entries of a matrix at one. 16213For example, to create a diagonal matrix with a 16214diagonal of \texttt{[1,2,3]}:\\ 16215Input: 16216\begin{center} 16217 \tt 16218 M := matrix(3,3) 16219\end{center} 16220Output: 16221\begin{center} 16222 \tt 16223 [[0,0,0],[0,0,0],[0,0,0]] 16224\end{center} 16225Input: 16226\begin{center} 16227 \tt 16228 M[0..2,0..2] := [1,2,3] 16229\end{center} 16230Output: 16231\begin{center} 16232 \tt 16233 matrix[[1,0,0],[0,2,0],[0,0,3]] 16234\end{center} 16235To make the last column \texttt{[4,5,6]}:\\ 16236Input: 16237\begin{center} 16238 \tt 16239 M[0..2,2] := [4,5,6] 16240\end{center} 16241Output: 16242\begin{center} 16243 \tt 16244 matrix[[1,0,4],[0,2,5],[0,0,6]] 16245\end{center} 16246 16247\subsection{Build a matrix with a function : {\tt makemat}}\index{makemat} 16248\noindent{\tt makemat} takes three arguments : 16249\begin{itemize} 16250\item a function of two variables {\tt j} and {\tt k} which 16251should return the value of $a_{j,k}$, the element of 16252row index {\tt j} and column index {\tt k} of the matrix to be built. 16253\item two integers $n$ and $p$. 16254\end{itemize} 16255{\tt makemat} returns the matrix $A=(a_{j,k})$ 16256($j=0..n-1$ and $k=0..p-1$) of dimension $n \times p$.\\ 16257Input : 16258\begin{center}{\tt makemat((j,k)->j+k,4,3)}\end{center} 16259or first define the $h$ function: 16260\begin{center}{\tt h(j,k):=j+k}\end{center} 16261then, input: 16262\begin{center}{\tt makemat(h,4,3)}\end{center} 16263Output : 16264\begin{center}{\tt [[0,1,2],[1,2,3],[2,3,4],[3,4,5]]}\end{center} 16265Note that the indices are counted starting from 0. 16266 16267\subsection{Define a matrix : {\tt matrix}}\index{matrix} 16268\noindent{\tt matrix} takes three arguments : 16269\begin{itemize} 16270\item two integers $n$ and $p$. 16271\item a function of two variables {\tt j} and {\tt k} which 16272should return the value of $a_{j,k}$, the element of 16273row index {\tt j} and column index {\tt k} of the matrix to be build. 16274\end{itemize} 16275{\tt matrix} returns the matrix $A=(a_{j,k})$ ($j=1..n$ and $k=1..p$) of 16276dimension $n \times p$.\\ 16277Input : 16278\begin{center}{\tt matrix(4,3,(j,k)->j+k)}\end{center} 16279or first define the $h$ function: 16280\begin{center} {\tt h(j,k):=j+k}\end{center} 16281then, input: 16282\begin{center}{\tt matrix(4,3,h)}\end{center} 16283Output : 16284\begin{center}{\tt [[2,3,4],[3,4,5],[4,5,6],[5,6,7]]}\end{center} 16285Note the argument order and the fact that the indices are counted 16286starting from 1. If the last argument is not provided, it defaults to 0. 16287 16288\subsection{Modify an element or row of a matrix assigned to a 16289variable: \texttt{::=, =<}}\index{::=}\index{=<} 16290 16291For named matrices, the elements can be changed by assignment. 16292Recall the elements are indexed starting at 0, using double brackets 16293allows you to use indices starting at 1.\\ 16294Input: 16295\begin{center} 16296 \tt 16297 A := [[1,2,3],[4,5,6]] 16298\end{center} 16299Output: 16300\begin{center} 16301 \tt 16302 [[1,2,3],[4,5,6]] 16303\end{center} 16304Input: 16305\begin{center} 16306 \tt 16307 A[0,2] := 7 16308\end{center} 16309then: 16310\begin{center} 16311 \tt 16312 A 16313\end{center} 16314Output: 16315\begin{center} 16316 \tt 16317 [[1,2,7],[4,5,6]] 16318\end{center} 16319Input: 16320\begin{center} 16321 \tt 16322 A[[1,2]] := 9 16323\end{center} 16324then: 16325\begin{center} 16326 \tt 16327 A 16328\end{center} 16329Output: 16330\begin{center} 16331 \tt 16332 [[1,9,7],[4,5,6]] 16333\end{center} 16334 16335When an element of a matrix is changed with the \texttt{:=} 16336assignment, a new copy of the matrix is created with the modified 16337element. Particularly for large matrices, it is more efficient to use 16338the \texttt{=<} assignment, which will change the element of the 16339matrix without making a copy. For example, defining \texttt{A} as\\ 16340Input: 16341\begin{center} 16342 \tt 16343 A := [[4,5],[2,6]] 16344\end{center} 16345the following commands will all return the matrix \texttt{A} with the 16346element in the second row, first column, changed to 3.\\ 16347Input: 16348\begin{center} 16349 \tt 16350 A[1,0] := 3 16351\end{center} 16352or: 16353\begin{center} 16354 \tt 16355 A[1,0] =< 3 16356\end{center} 16357or: 16358\begin{center} 16359 \tt 16360 A[[2,1]] := 3 16361\end{center} 16362or: 16363\begin{center} 16364 \tt 16365 A[[2,1]] =< 3 16366\end{center} 16367then: 16368\begin{center} 16369 \tt 16370 A 16371\end{center} 16372Output: 16373\begin{center} 16374 \tt 16375 [[4,5],[3,6]] 16376\end{center} 16377 16378Larger parts of a matrix can be changed simultaneously. Letting 16379\texttt{A := [[4,5],[2,6]]} again, the following commands will change 16380the second row to \texttt{[3,7]}\\ 16381Input: 16382\begin{center} 16383 \tt 16384 A[1] := [3,7] 16385\end{center} 16386or: 16387\begin{center} 16388 \tt 16389 A[1] =< [3,7] 16390\end{center} 16391or: 16392\begin{center} 16393 \tt 16394 A[[2]] := [3,7] 16395\end{center} 16396or: 16397\begin{center} 16398 \tt 16399 A[[2]] =< [3,7] 16400\end{center} 16401 16402The \texttt{=<} assignment must be used carefully, since it not only 16403modifies a matrix \texttt{A}, it modifies all objects pointing to 16404the matrix. In a program, initialization should contain a line like 16405\texttt{A := copy(B)}, so modifications done on \texttt{A} don't 16406affect \texttt{B}, and modifications done on \texttt{B} don't affect 16407\texttt{A}. For example,\\ 16408Input: 16409\begin{center} 16410 \tt 16411 B := [[4,5],[2,6]] 16412\end{center} 16413then: 16414\begin{center} 16415 \tt 16416 A := B 16417\end{center} 16418or\\ 16419Input: 16420\begin{center} 16421 \tt 16422 A =< B 16423\end{center} 16424creates two matrices equal to \texttt{[[4,5],[2,6]]}. 16425Then\\ 16426Input: 16427\begin{center} 16428 \tt 16429 A[1] =< [3,7] 16430\end{center} 16431or: 16432\begin{center} 16433 \tt 16434 B[1] =< [3,7] 16435\end{center} 16436will transform both \texttt{A} and \texttt{B} to \texttt{[[4,5],[3,7]]}. 16437On the other hand, creating \texttt{A} and \texttt{B} with\\ 16438Input: 16439\begin{center} 16440 \tt 16441 B := [[4,5],[2,6]]\\ 16442 A := copy(B) 16443\end{center} 16444will again create two matrices equal to \texttt{[[4,5],[2,6]]}. But\\ 16445Input: 16446\begin{center} 16447 \tt 16448 A[1] =< [3,7] 16449\end{center} 16450will change \texttt{A} to \texttt{[[4,5],[3,7]]}, but \texttt{B} will 16451still be \texttt{[[4,5],[2,6]]}. 16452 16453\section{Arithmetic and matrices} 16454\subsection{Evaluate a matrix : {\tt evalm}}\index{evalm} 16455\noindent {\tt evalm} is used in {\tt Maple} to evaluate a matrix. 16456In {\tt Xcas}, matrices are evaluated by default, the command 16457{\tt evalm} is only available for compatibility, it is equivalent 16458to {\tt eval}. 16459 16460\subsection{Addition and subtraction of two matrices : {\tt + - .+ .-}}\index{+}\index{-}\index{.+}\index{.-} 16461\noindent The infixed operator {\tt +} or {\tt .+} (resp. {\tt -} or {\tt .-}) 16462are used for the addition (resp. subtraction) of two matrices.\\ 16463Input : 16464\begin{center}{\tt [[1,2],[3,4]] + [[5,6],[7,8]]}\end{center} 16465Output : 16466\begin{center}{\tt [[6,8],[10,12]]}\end{center} 16467Input : 16468\begin{center}{\tt [[1,2],[3,4]] - [[5,6],[7,8]]}\end{center} 16469Output : 16470\begin{center}{\tt [[-4,-4],[-4,-4]]}\end{center} 16471{\bf Remark}\\ 16472{\tt +} can be used as a prefixed operator, in that case 16473{\tt +} must be quoted ({\tt '+'}).\\ 16474Input : 16475\begin{center}{\tt '+'([[1,2],[3,4]],[[5,6],[7,8]],[[2,2],[3,3]])}\end{center} 16476Output : 16477\begin{center}{\tt [[8,10],[13,15]]}\end{center} 16478 16479\subsection{Multiplication of two matrices : {\tt * \&*}}\index{*}\index{\&*} 16480\noindent The infixed operator {\tt *} (or {\tt \&*}) is used for the 16481multiplication of two matrices.\\ 16482Input : 16483\begin{center}{\tt [[1,2],[3,4]] * [[5,6],[7,8]]}\end{center} 16484or : 16485\begin{center}{\tt [[1,2],[3,4]] \&* [[5,6],[7,8]]}\end{center} 16486Output : 16487\begin{center}{\tt [[19,22],[43,50]]}\end{center} 16488 16489\subsection{Addition of elements of a column of a matrix : {\tt sum}}\index{sum} 16490\noindent {\tt sum} takes as argument a matrix $A$.\\ 16491{\tt sum} returns the list whose elements are the sum of the elements of each 16492column of the matrix $A$.\\ 16493Input : 16494\begin{center}{\tt sum([[1,2],[3,4]])}\end{center} 16495Output : 16496\begin{center}{\tt [4,6]}\end{center} 16497 16498\subsection{Cumulated sum of elements of each column of a matrix : {\tt cumSum}}\index{cumSum} 16499\noindent {\tt cumSum} takes as argument a matrix $A$.\\ 16500{\tt cumSum} returns the matrix whose columns are the cumulated sum of the 16501elements of the corresponding column of the matrix $A$.\\ 16502Input : 16503\begin{center}{\tt cumSum([[1,2],[3,4],[5,6]])}\end{center} 16504Output : 16505\begin{center}{\tt [[1,2],[4,6],[9,12]]}\end{center} 16506since the cumulated sums are : 1, 1+3=4, 1+3+5=9 and 2, 2+4=6, 2+4+6=12. 16507 16508\subsection{Multiplication of elements of each column of a matrix : {\tt product}}\index{product}\label{sec:product1} 16509\noindent {\tt product} takes as argument a matrix $A$.\\ 16510{\tt product} returns the list whose elements are the product of the elements 16511of each column of the matrix $A$ (see also \ref{sec:product} and 16512\ref{sec:product2}).\\ 16513Input : 16514\begin{center}{\tt product([[1,2],[3,4]])}\end{center} 16515Output : 16516\begin{center}{\tt [3,8]}\end{center} 16517 16518\subsection{Power of a matrix :\ \^\ \ \&\^\ }\index{\^\ |textbf}\index{\&\^\ } 16519The infixed operator {\tt \verb|^|} (or {\tt \&\verb|^|}) is used to 16520raise a matrix to an integral power.\\ 16521Input : 16522\begin{center}{\tt [[1,2],[3,4]] \verb|^| 5}\end{center} 16523or : 16524\begin{center}{\tt [[1,2],[3,4]] \&\verb|^| 5}\end{center} 16525Output : 16526\begin{center}{\tt [[1069,1558],[2337,3406]]}\end{center} 16527 16528\subsection{Hadamard product : {\tt hadamard, product}}\index{hadamard}\index{product}\label{sec:product2} 16529\noindent{\tt hadamard} (or {\tt product}) takes as arguments two matrices $A$ 16530and $B$ of the same size.\\ 16531{\tt hadamard} (or {\tt product}) returns the matrix where each term is the 16532term by term product of $A$ and $B$.\\ 16533Input : 16534\begin{center}{\tt hadamard([[1, 2],[3,4]],[[5, 6],[7, 8]])}\end{center} 16535Output : 16536\begin{center}{\tt [[5,12],[21,32]]}\end{center} 16537See also \ref{sec:product} and \ref{sec:product1} for {\tt product}. 16538 16539\subsection{Hadamard product (infixed version): {\tt .*}}\index{.*} 16540\noindent{\tt .*} takes as arguments two matrices or two lists $A$ and $B$ 16541of the same size.\\ 16542{\tt .*} is an infixed operator that returns the matrix or the list 16543where each term is the term by term product of the corresponding 16544terms of $A$ and $B$.\\ 16545Input : 16546\begin{center}{\tt [[1, 2],[3,4]] .* [[5, 6],[7, 8]]}\end{center} 16547Output : 16548\begin{center}{\tt [[5,12],[21,32]]}\end{center} 16549Input : 16550\begin{center}{\tt [1,2,3,4] .* [5,6,7,8]}\end{center} 16551Output : 16552\begin{center}{\tt [5,12,21,32]}\end{center} 16553 16554\subsection{Hadamard division (infixed version): {\tt ./}}\index{./} 16555\noindent{\tt ./} takes as arguments two matrices or two lists $A$ and 16556$B$ of the same size.\\ 16557{\tt ./} is an infixed operator that returns the matrix or the list 16558where each term is the term by term division of the corresponding 16559terms of $A$ and $B$.\\ 16560Input : 16561\begin{center}{\tt [[1, 2],[3,4]] ./ [[5, 6],[7, 8]]}\end{center} 16562Output : 16563\begin{center}{\tt [[1/5,1/3],[3/7,1/2]]}\end{center} 16564 16565\subsection{Hadamard power (infixed version): {\tt .\^\ }}\index{.\^\ } 16566\noindent{\tt .\verb|^|} takes as arguments a matrix or a list 16567$A$ and a real $b$.\\ 16568{\tt .\verb|^|} is an infixed operator that returns the matrix 16569or the list where each term is the corresponding 16570term of $A$ raised to the power $b$.\\ 16571Input : 16572\begin{center}{\tt [[1, 2],[3,4]] .\verb|^| 2}\end{center} 16573Output : 16574\begin{center}{\tt [[1,4],[9,16]]}\end{center} 16575 16576\subsection{Extracting element(s) of a matrix : {\tt [] at}}\index{at} 16577Recall that a matrix is a list of lists with the same size.\\ 16578Input : 16579\begin{center}{\tt A:=[[3,4,5],[1,2,6]]}\end{center} 16580Output : 16581\begin{center}{\tt [[3,4,5],[1,2,6]]}\end{center} 16582The prefixed function {\tt at} or the 16583index notation {\tt [...]} is used to access 16584to an element or a row or a column of a matrix: 16585\begin{itemize} 16586\item To extract an element, put the matrix and then, between square 16587brackets put its row index, a comma, and its column index. 16588In {\tt Xcas} mode the first index is 0, in other modes the first 16589index is 1.\\ 16590Input : 16591\begin{center}{\tt [[3,4,5],[1,2,6]][0,1]}\end{center} 16592or : 16593\begin{center}{\tt A[0,1]}\end{center} 16594or : 16595\begin{center}{\tt A[0][1]}\end{center} 16596or : 16597\begin{center}{\tt at(A,[0,1])}\end{center} 16598Output : 16599\begin{center}{\tt 4}\end{center} 16600 16601\item To extract a row of the matrix {\tt A}, 16602put the matrix and then, between 16603square brackets put the row index, input : 16604\begin{center}{\tt [[3,4,5],[1,2,6]][0]}\end{center} 16605or : 16606\begin{center}{\tt A[0]}\end{center} 16607or : 16608\begin{center}{\tt at(A,0)}\end{center} 16609Output : 16610\begin{center}{\tt [3,4,2]}\end{center} 16611 16612\item To extract a part of a row, put two arguments 16613between the square brackets : 16614the row index and an interval to designate the selected columns.\\ 16615Input : 16616\begin{center}{\tt A[1,0..2]}\end{center} 16617Output : 16618\begin{center}{\tt [1,2,6]}\end{center} 16619Input : 16620\begin{center}{\tt A[1,1..2]}\end{center} 16621Output : 16622\begin{center}{\tt [2,6]}\end{center} 16623 16624\item To extract a column of the matrix {\tt A}, first transpose 16625{\tt A} ({\tt transpose(A)}) then extract the row like above.\\ 16626Input : 16627\begin{center}{\tt tran(A)[1]}\end{center} 16628or : 16629\begin{center}{\tt at(tran(A),1)}\end{center} 16630Output : 16631\begin{center}{\tt [4,2]}\end{center} 16632 16633\item To extract a part of a column of the matrix {\tt A} 16634as a list, put two arguments 16635between the square brackets : an index interval to 16636designate the selected rows and the column index.\\ 16637Input : 16638\begin{center}{\tt A[0..0,1]}\end{center} 16639Output : 16640\begin{center}{\tt [4]}\end{center} 16641 16642This may be used to extract a full column, by specifying all the rows 16643as an index interval.\\ 16644Input : 16645\begin{center}{\tt A[0..1,1]}\end{center} 16646Output : 16647\begin{center}{\tt [4,2]}\end{center} 16648 16649\item 16650To extract a sub-matrix of a matrix, put between the square brackets two 16651intervals : one interval for the selected rows and one interval for the 16652selected columns.\\ 16653To define the matrix {\tt A}, input : 16654\begin{center}{\tt A:=[[3,4,5],[1,2,6]]}\end{center} 16655Input : 16656\begin{center}{\tt A[0..1,1..2]}\end{center} 16657Output : 16658\begin{center}{\tt [[4,5],[2,6]]}\end{center} 16659Input : 16660\begin{center}{\tt A[0..1,1..1]}\end{center} 16661Output : 16662\begin{center}{\tt [[4],[2]]}\end{center} 16663{\bf Remark} 16664If the second interval is omitted, the sub-matrix is made with the consecutive 16665rows given by the first interval.\\ 16666Input : 16667\begin{center}{\tt A[1..1]}\end{center} 16668Output : 16669\begin{center}{\tt [[1,2,6]]}\end{center} 16670\end{itemize} 16671 16672You may also assign an element of a matrix using index notation, 16673if you assign with {\tt :=} a new copy of the matrix is created 16674and the element is modified, if you assign with {\tt =<}, 16675the matrix is modified in place. 16676 16677\subsection{Modify an element or a row of a matrix : {\tt subsop}}\index{subsop|textbf} 16678\noindent {\tt subsop} modifies an element or a row of a matrix. 16679It is used mainly for {\tt Maple} and {\tt MuPAD} compatibility. 16680Unlike {\tt :=} or {\tt =<}, 16681it does not require the matrix to be stored in a variable.\\ 16682{\tt subsop} takes two or three arguments, 16683{\bf these arguments are permuted} in {\tt Maple} mode. 16684\begin{enumerate} 16685\item Modify an element 16686\begin{itemize} 16687\item In {\tt Xcas} mode, the first index is 0\\ 16688{\tt subsop} has two (resp. three) arguments: a matrix {\tt A} and an 16689equality {\tt [r,c]=v} (resp. a matrix {\tt A}, a list of indexes {\tt [r,c]}, 16690a value {\tt v}).\\ 16691{\tt subsop} replaces the element {\tt A[r,c]} by {\tt v}.\\ 16692Input in {\tt Xcas} mode : 16693\begin{center}{\tt subsop([[4,5],[2,6]],[1,0]=3)}\end{center} 16694or : 16695\begin{center}{\tt subsop([[4,5],[2,6]],[1,0],3)}\end{center} 16696Output : 16697\begin{center}{\tt [[4,5],[3,6]]}\end{center} 16698{\bf Remark}\\ 16699If the matrix is stored in a variable, for example 16700{\tt A:=[[4,5],[2,6]]}, it is easier to input {\tt A[1,0]:=3} 16701which modifies {\tt A} into the matrix\\ {\tt [[4,5],[3,6]]}. 16702 16703\item In {\tt Mupad, TI} mode, the first index is 1\\ 16704{\tt subsop} has two (resp. three) arguments: a matrix {\tt A} and an 16705equality {\tt [r,c]=v} (resp. a matrix {\tt A}, a list of index {\tt [r,c]}, 16706a value {\tt v}).\\ 16707{\tt subsop} replaces the element {\tt A[r,c]} by {\tt v}.\\ 16708Input in {\tt Mupad, TI} mode : 16709\begin{center}{\tt subsop([[4,5],[2,6]],[2,1]=3)}\end{center} 16710or : 16711\begin{center}{\tt subsop([[4,5],[2,6]],[2,1],3)}\end{center} 16712Output : 16713\begin{center}{\tt [[4,5],[3,6]]}\end{center} 16714{\bf Remark}\\ 16715If the matrix is stored in a variable, for example 16716{\tt A:=[[4,5],[2,6]]}, it is easier to input {\tt A[2,1]:=3} which 16717modifies {\tt A} into the matrix \\{\tt [[4,5],[3,6]]}. 16718 16719\item In {\tt Maple} mode, 16720the arguments are permuted and the first index is 1\\ 16721{\tt subsop} has two arguments: an equality {\tt [r,c]=v} and a matrix 16722{\tt A}.\\ 16723{\tt subsop} replaces the element {\tt A[r,c]} by {\tt v}.\\ 16724Input in {\tt Maple} mode 16725\begin{center}{\tt subsop([2,1]=3,[[4,5],[2,6]])}\end{center} 16726Output : 16727\begin{center}{\tt [[4,5],[3,6]]}\end{center} 16728{\bf Remark}\\ 16729If the matrix is stored in a variable, for example 16730{\tt A:=[[4,5],[2,6]]}, it is easier to input {\tt A[2,1]:=3} which 16731modifies {\tt A} into the matrix\\ {\tt [[4,5],[3,6]]}. 16732\end{itemize} 16733 16734\item Modify a row 16735\begin{itemize} 16736\item in {\tt Xcas} mode, the first index is 0\\ 16737{\tt subsop} takes two arguments : a matrix and an 16738equality (the index of the row to be modified, the {\tt =} sign and the new 16739row value).\\ 16740Input in {\tt Xcas} mode : 16741\begin{center}{\tt subsop([[4,5],[2,6]],1=[3,3])}\end{center} 16742Output : 16743\begin{center}{\tt [[4,5],[3,3]]}\end{center} 16744{\bf Remark}\\ 16745If the matrix is stored in a variable, for example 16746{\tt A:=[[4,5],[2,6]]}, is is easier to input {\tt A[1]:=[3,3]} 16747which modifies {\tt A} into the matrix\\ {\tt [[4,5],[3,3]]}. 16748 16749\item In {\tt Mupad, TI} mode, the first index is 1 \\ 16750{\tt subsop} takes two arguments : a matrix and an 16751equality (the index of the row to be modified, the {\tt =} sign and the new 16752row value).\\ 16753Input in {\tt Mupad, TI} mode : 16754\begin{center}{\tt subsop([[4,5],[2,6]],2=[3,3])}\end{center} 16755Output : 16756\begin{center}{\tt [[4,5],[3,3]]}\end{center} 16757{\bf Remark}\\ 16758If the matrix is stored in a variable, for example 16759{\tt A:=[[4,5],[2,6]]}, it is easier to input {\tt A[2]:=[3,3]} which modifies 16760{\tt A} into the matrix\\ {\tt [[4,5],[3,3]]}. 16761 16762\item in {\tt Maple} mode, the arguments are permuted and the first index 16763is 1 :\\ 16764{\tt subsop} takes two arguments : an 16765equality (the index of the row to be modified, the {\tt =} sign and the new 16766row value) and a matrix.\\ 16767Input in {\tt Maple} mode : 16768\begin{center}{\tt subsop(2=[3,3],[[4,5],[2,6]])}\end{center} 16769Output : 16770\begin{center}{\tt [[4,5],[3,3]]}\end{center} 16771{\bf Remark}\\ 16772If the matrix is stored in a variable, for example 16773{\tt A:=[[4,5],[2,6]]}, it is easier to input {\tt A[2]:=[3,3]} which modifies 16774{\tt A} into the matrix\\ {\tt [[4,5],[3,3]]}. 16775\end{itemize} 16776\end{enumerate} 16777{\bf Remark}\\ 16778Note also that {\tt subsop} with a {\tt 'n=NULL'} argument 16779deletes row number {\tt n}. 16780In {\tt Xcas} mode input : 16781\begin{center}{\tt subsop([[4,5],[2,6]],'1=NULL')}\end{center} 16782Output : 16783\begin{center}{\tt [[4,5]]}\end{center} 16784 16785\subsection{Extract rows or columns of a matrix (Maple compatibility) : {\tt row col}} 16786\index{row} 16787\index{col} 16788\noindent{\tt row} (resp. {\tt col}) extracts one or several rows (resp. columns) 16789of a matrix.\\ 16790{\tt row} (resp. {\tt col}) takes 2 arguments : a matrix $A$, and an integer $n$ 16791or an interval $n_1..n_2$.\\ 16792{\tt row} (resp. {\tt col}) returns the row (resp. column) of index $n$ of $A$, 16793or the sequence of rows (resp. columns) of index from $n_1$ to $n_2$ of $A$.\\ 16794Input : 16795\begin{center}{\tt row([[1,2,3],[4,5,6],[7,8,9]],1)}\end{center} 16796Output : 16797\begin{center}{\tt [4,5,6]}\end{center} 16798Input : 16799\begin{center}{\tt row([[1,2,3],[4,5,6],[7,8,9]],0..1)}\end{center} 16800Output : 16801\begin{center}{\tt ([1,2,3],[4,5,6])}\end{center} 16802Input : 16803\begin{center}{\tt col([[1,2,3],[4,5,6],[7,8,9]],1)}\end{center} 16804Output : 16805\begin{center}{\tt [2,5,8]}\end{center} 16806Input : 16807\begin{center}{\tt col([[1,2,3],[4,5,6],[7,8,9]],0..1)}\end{center} 16808Output : 16809\begin{center}{\tt ([1,4,7,[2,5,8])}\end{center} 16810 16811\subsection{Remove rows or columns of a matrix : {\tt delrows delcols}}\index{delrows}\index{delcols} 16812\noindent{\tt delrows} (resp. {\tt delcols}) removes one or several rows (resp. 16813columns) of a matrix.\\ 16814{\tt delrows} (resp. {\tt delcols}) takes 2 arguments : a matrix $A$, and 16815an interval $n_1..n_2$.\\ 16816{\tt delrows} (resp. {\tt delcols}) returns the matrix where the rows 16817(resp. columns) of index from $n_1$ to $n_2$ of $A$ are removed.\\ 16818Input : 16819\begin{center}{\tt delrows([[1,2,3],[4,5,6],[7,8,9]],1..1)}\end{center} 16820Output : 16821\begin{center}{\tt [[1,2,3],[7,8,9]]}\end{center} 16822Input : 16823\begin{center}{\tt delrows([[1,2,3],[4,5,6],[7,8,9]],0..1)}\end{center} 16824Output : 16825\begin{center}{\tt [[7,8,9]]}\end{center} 16826Input : 16827\begin{center}{\tt delcols([[1,2,3],[4,5,6],[7,8,9]],1..1)}\end{center} 16828Output : 16829\begin{center}{\tt [[1,3],[4,6],[7,9]]}\end{center} 16830Input : 16831\begin{center}{\tt delcols([[1,2,3],[4,5,6],[7,8,9]],0..1)}\end{center} 16832Output : 16833\begin{center}{\tt [[3],[6],[9]]}\end{center} 16834 16835\subsection{Extract a sub-matrix of a matrix (TI compatibility) : {\tt subMat}}\index{subMat} 16836\noindent{\tt subMat} takes 5 arguments : a matrix $A$, and 4 integers 16837$nl1,nc1,nl2,nc2$, where 16838$nl1$ is the index of the first row, $nc1$ is 16839the index of the first column, $nl2$ is the index of 16840the last row and $nc2$ is the index of the last column.\\ 16841{\tt subMat(A,nl1,nc1,nl2,nc2)} extracts the sub-matrix of the matrix {\tt A} 16842with first element {\tt A[nl1,nc1]} and last element 16843{\tt A[nl2,nc2]}.\\ 16844Define the matrix {\tt A} : 16845\begin{center}{\tt A:=[[3,4,5],[1,2,6]]}\end{center} 16846Input : 16847\begin{center}{\tt subMat(A,0,1,1,2)}\end{center} 16848Output : 16849\begin{center}{\tt [[4,5],[2,6]]}\end{center} 16850Input : 16851\begin{center}{\tt subMat(A,0,1,1,1]}\end{center} 16852Output : 16853\begin{center}{\tt [[4],[2]]}\end{center} 16854By default $nl1=0$, $nc1=0$, $nl2$={\tt nrows(A)}-1 and 16855$nc2$={\tt ncols(A)}-1\\ 16856Input : 16857\begin{center}{\tt subMat(A,1)}\end{center} 16858or : 16859\begin{center}{\tt subMat(A,1,0)}\end{center} 16860or : 16861\begin{center}{\tt subMat(A,1,0,1)}\end{center} 16862or : 16863\begin{center}{\tt subMat(A,1,0,1,2)}\end{center} 16864Output : 16865\begin{center}{\tt [[1,2,6]]}\end{center} 16866 16867\subsection{Resize a matrix or vector:\texttt{REDIM, 16868redim}}\index{REDIM}\index{redim} 16869 16870The \texttt{REDIM} (or \texttt{redim}) command takes two arguments. They are either a matrix 16871and a list of two integers, or a vector and an integer.\\ 16872\texttt{REDIM} returns the matrix or vector resized according to the 16873second argument; removing elements to make it shorter, if necessary, or 16874adding 0s to make it larger.\\ 16875Input: 16876\begin{center} 16877 \tt 16878 REDIM([[4,1,-2],[1,2,-1],[2,1,0]],[5,4]) 16879\end{center} 16880Output: 16881\begin{center} 16882 \tt 16883 [[4,1,-2,0],[1,2,-1,0],[2,1,0,0],[0,0,0,0],[0,0,0,0]] 16884\end{center} 16885Input: 16886\begin{center} 16887 \tt 16888 REDIM([[4,1,-2],[1,2,-1],[2,1,0]],[2,1]) 16889\end{center} 16890Output: 16891\begin{center} 16892 \tt 16893 [[4],[1]] 16894\end{center} 16895Input: 16896\begin{center} 16897 \tt 16898 REDIM([4,1,-2,1,2,-1],10) 16899\end{center} 16900Output: 16901\begin{center} 16902 \tt 16903 [4,1,-2,1,2,-1,0,0,0,0] 16904\end{center} 16905Input: 16906\begin{center} 16907 \tt 16908 REDIM([4,1,-2,1,2,-1],3) 16909\end{center} 16910Output: 16911\begin{center} 16912 \tt 16913 [4,1,-2] 16914\end{center} 16915 16916\subsection{Replacing part of a matrix or vector: 16917\texttt{REPLACE, replace}}\index{REPLACE}\index{replace} 16918 16919The \texttt{REPLACE} (or \texttt{replace}) command takes three arguments; either a matrix, a 16920list of two indices, and another matrix, or a vector, a single index, 16921and another vector.\\ 16922\texttt{REPLACE} returns the first matrix (or vector) with the second 16923matrix (or vector) placed at the location given by the indices, 16924replacing the elements previously there. The second matrix (or 16925vector) will be shrunk, if necessary, so that it fits in the first 16926matrix (or vector).\\ 16927Input: 16928\begin{center} 16929 \tt 16930 REPLACE([[1,2,3],[4,5,6]],[0,1],[[5,6],[7,8]]) 16931\end{center} 16932Output: 16933\begin{center} 16934 \tt 16935 [[1,5,6],[4,7,8]] 16936\end{center} 16937Input: 16938\begin{center} 16939 \tt 16940 REPLACE([[1,2,3],[4,5,6]],[1,2],[[7,8],[9,0]]) 16941\end{center} 16942Output: 16943\begin{center} 16944 \tt 16945 [[1,2,3],[4,5,7]] 16946\end{center} 16947Input: 16948\begin{center} 16949 \tt 16950 REPLACE([4,1,-2,1,2,-1],2,[10,11]) 16951\end{center} 16952Output: 16953\begin{center} 16954 \tt 16955 [4,1,10,11,2,-1] 16956\end{center} 16957Input: 16958\begin{center} 16959 \tt 16960 REPLACE([4,1,-2,1,2,-1],1,[10,11,13]) 16961\end{center} 16962Output: 16963\begin{center} 16964 \tt 16965 [4,10,11,13,2,-1] 16966\end{center} 16967 16968\subsection{Add a row to another row : {\tt rowAdd}}\index{rowAdd} 16969\noindent{\tt rowAdd} takes three arguments : a matrix $A$ and two integers 16970$n1$ and $n2$.\\ 16971{\tt rowAdd} returns the matrix obtained by replacing in $A$, the row of index 16972$n2$ by the sum of the rows of index $n1$ and $n2$.\\ 16973Input : 16974\begin{center}{\tt rowAdd([[1,2],[3,4]],0,1)}\end{center} 16975Output : 16976\begin{center}{\tt [[1,2],[4,6]]}\end{center} 16977 16978\subsection{Multiply a row by an expression : {\tt mRow, 16979scale, SCALE}}\index{mRow}\index{scale}\index{SCALE} 16980\noindent{\tt mRow} takes three arguments : an expression, a matrix $A$ and an 16981integer $n$.\\ 16982{\tt mRow} returns the matrix obtained by replacing in $A$, the row of index 16983$n$ by the product of the row of index $n$ by the expression.\\ 16984Input : 16985\begin{center}{\tt mRow(12,[[1,2],[3,4]],1)}\end{center} 16986Output : 16987\begin{center}{\tt [[1,2],[36,48]]}\end{center} 16988 16989The \texttt{scale} (or \texttt{SCALE}) command is the same as 16990\texttt{mRow} except that it takes the arguments in a different order; 16991the matrix comes first, then the expression, then the integer.\\ 16992Input: 16993\begin{center} 16994 \tt 16995 scale([[1,2],[3,4]],12,1) 16996\end{center} 16997Output: 16998\begin{center} 16999 \tt 17000 [[1,2],[36,48]] 17001\end{center} 17002 17003\subsection{Add $k$ times a row to an another row : {\tt mRowAdd, 17004scaleadd, SCALEADD}}\index{mRowAdd}\index{scaleadd}\index{SCALEADD} 17005 17006\noindent{\tt mRowAdd} takes four arguments : a real $k$, a matrix $A$ and two 17007integers $n1$ and $n2$.\\ 17008{\tt mRowAdd} returns the matrix obtained by replacing in $A$, the 17009row of index $n2$ by the sum of the row of index $n2$ and $k$ times the row of 17010index $n1$.\\ 17011Input : 17012\begin{center}{\tt mRowAdd(1.1,[[5,7],[3,4],[1,2]],1,2)}\end{center} 17013Output : 17014\begin{center}{\tt [[5,7],[3,4],[4.3,6.4]]}\end{center} 17015 17016The \texttt{scaleadd} (or \texttt{SCALEADD}) command is the same as 17017\texttt{mRowAdd}, except that it takes the arguments in a different 17018order; the matrix comes first, then the real number, then the two 17019integers.\\ 17020Input: 17021\begin{center} 17022 \tt 17023 scaleadd([[5,7],[3,4],[1,2]],1.1,1,2) 17024\end{center} 17025Output: 17026\begin{center} 17027 \tt 17028 [[5,7],[3,4],[4.3,6.4]] 17029\end{center} 17030 17031\subsection{Exchange two rows : {\tt rowSwap, rowswap, 17032swaprow}}\index{rowSwap}\index{rowswap}\index{swaprow} 17033 17034\noindent{\tt rowSwap} (or \texttt{rowswap} or \texttt{swaprow}) 17035takes three arguments : a matrix $A$ and two integers 17036$n1$ and $n2$.\\ 17037{\tt rowSwap} returns the matrix obtained by exchanging in $A$, the row of 17038index $n1$ with the row of index $n2$.\\ 17039Input : 17040\begin{center}{\tt rowSwap([[1,2],[3,4]],0,1)}\end{center} 17041Output : 17042\begin{center}{\tt [[3,4],[1,2]]}\end{center} 17043 17044\subsection{Exchange two columns : {\tt colSwap, colswap, 17045swapcol}}\index{colSwap}\index{colswap}\index{swapcol} 17046 17047\noindent{\tt colSwap} (or \texttt{colswap} or \texttt{swapcol}) 17048takes three arguments : a matrix $A$ and two integers 17049$n1$ and $n2$.\\ 17050{\tt colSwap} returns the matrix obtained by exchanging in $A$ the 17051column of index $n1$ with the column of index $n2$.\\ 17052Input : 17053\begin{center}{\tt colSwap([[1,2],[3,4]],0,1)}\end{center} 17054Output : 17055\begin{center}{\tt [[2,1],[4,3]]}\end{center} 17056 17057\subsection{Make a matrix with a list of matrices : {\tt blockmatrix}}\index{blockmatrix} 17058\noindent{\tt blockmatrix} takes as arguments two integers $n,m$ and a 17059list of size $n*m$ of matrices of the same dimension $p \times q$ 17060(or more generally such that the $m$ first matrices 17061have the same number of rows and $c$ columns, the 17062$m$ next rows have the same number of rows and $c$ columns, and so on ...). 17063In both cases, we have $n$ blocks of $c$ columns.\\ 17064{\tt blockmatrix} returns a matrix having $c$ columns 17065by putting these $n$ blocks one under another (vertical gluing). 17066If the matrix arguments 17067have the same dimension $p \times q$, the answer is a matrix of 17068dimension $p*n \times q*m$.\\ 17069Input : 17070\begin{center}{\tt blockmatrix(2,3,[idn(2),idn(2),idn(2), idn(2),idn(2),idn(2)])}\end{center} 17071Output : 17072\begin{center}{\tt [[1,0,1,0,1,0],[0,1,0,1,0,1], [1,0,1,0,1,0],[0,1,0,1,0,1]]}\end{center} 17073Input : 17074\begin{center}{\tt blockmatrix(3,2,[idn(2),idn(2), idn(2),idn(2),idn(2),idn(2)])}\end{center} 17075Output : 17076\begin{center}{\tt [[1,0,1,0],[0,1,0,1], [1,0,1,0],[0,1,0,1],[1,0,1,0],[0,1,0,1]]}\end{center} 17077Input : 17078\begin{center}{\tt blockmatrix(2,2,[idn(2),newMat(2,3), newMat(3,2),idn(3)])}\end{center} 17079Output : 17080\begin{center}{\tt [[1,0,0,0,0],[0,1,0,0,0],[0,0,1,0,0], [0,0,0,1,0],[0,0,0,0,1]] }\end{center} 17081Input : 17082\begin{center}{\tt blockmatrix(3,2,[idn(1),newMat(1,4), newMat(2,3),idn(2),newMat(1,2),[[1,1,1]]])}\end{center} 17083Output : 17084\begin{center}{\tt [[1,0,0,0,0],[0,0,0,1,0],[0,0,0,0,1],[0,0,1,1,1]]}\end{center} 17085Input : 17086\begin{center}{\tt A:=[[1,1],[1,1]];B:=[[1],[1]]}\end{center} 17087then : 17088\begin{center}{\tt blockmatrix(2,3,[2*A,3*A,4*A,5*B,newMat(2,4),6*B])}\end{center} 17089Output : 17090\begin{center}{\tt [[2,2,3,3,4,4],[2,2,3,3,4,4], [5,0,0,0,0,6],[5,0,0,0,0,6]]}\end{center} 17091 17092\subsection{Make a matrix from two matrices : {\tt semi\_augment}}\index{semi\_augment|textbf} 17093\noindent {\tt semi\_augment} concat two matrices with the same number 17094of columns.\\ 17095Input : 17096\begin{center}{\tt semi\_augment([[3,4],[2,1],[0,1]],[[1,2],[4,5]])}\end{center} 17097Output : 17098\begin{center}{\tt [[3,4],[2,1],[0,1],[1,2],[4,5]]}\end{center} 17099Input : 17100\begin{center}{\tt semi\_augment([[3,4,2]],[[1,2,4]])}\end{center} 17101Output : 17102\begin{center}{\tt [[3,4,2],[1,2,4]]}\end{center} 17103Note the difference with {\tt concat}.\\ 17104Input : 17105\begin{center}{\tt concat([[3,4,2]],[[1,2,4]]}\end{center} 17106Output : 17107\begin{center}{\tt [[3,4,2,1,2,4]]}\end{center} 17108Indeed, when the two matrices $A$ and $B$ have the same dimension, {\tt concat} 17109makes a matrix with the same number of rows as $A$ and $B$ by 17110gluing them side by side.\\ 17111Input : 17112\begin{center}{\tt concat([[3,4],[2,1],[0,1]],[[1,2],[4,5]]}\end{center} 17113Output : 17114\begin{center}{\tt [[3,4],[2,1],[0,1],[1,2],[4,5]]}\end{center} 17115but input : 17116\begin{center}{\tt concat([[3,4],[2,1]],[[1,2],[4,5]]}\end{center} 17117Output : 17118\begin{center}{\tt [[3,4,1,2],[2,1,4,5]]}\end{center} 17119 17120\subsection{Make a matrix from two matrices : {\tt augment concat}}\index{augment}\index{concat} 17121\noindent {\tt augment} or {\tt concat} concats two matrices $A$ and $B$ 17122having the same number of rows, or having the same number of columns. 17123In the first case, it returns a matrix having the same number of rows 17124as $A$ and $B$ by horizontal gluing, in the second case 17125it returns a matrix having the same number of columns by 17126vertical gluing.\\ 17127Input : 17128\begin{center}{\tt augment([[3,4,5],[2,1,0]],[[1,2],[4,5]])}\end{center} 17129Output : 17130\begin{center}{\tt [[3,4,5,1,2],[2,1,0,4,5]]}\end{center} 17131Input : 17132\begin{center}{\tt augment([[3,4],[2,1],[0,1]],[[1,2],[4,5]])}\end{center} 17133Output : 17134\begin{center}{\tt [[3,4],[2,1],[0,1],[1,2],[4,5]]}\end{center} 17135Input : 17136\begin{center}{\tt augment([[3,4,2]],[[1,2,4]]}\end{center} 17137Output : 17138\begin{center}{\tt [[3,4,2,1,2,4]]}\end{center} 17139Note that if $A$ and $B$ have the same dimension, {\tt augment} 17140makes a matrix with the same number of rows as $A$ and $B$ 17141by horizontal gluing, in that case 17142you must use {\tt semi\_augment} for vertical gluing.\\ 17143Input : 17144\begin{center}{\tt augment([[3,4],[2,1]],[[1,2],[4,5]])}\end{center} 17145Output : 17146\begin{center}{\tt [[3,4,1,2],[2,1,4,5]]]}\end{center} 17147 17148\subsection{Append a column to a matrix : {\tt border}}\index{border} 17149\noindent{\tt border} takes as argument a matrix {\tt A} of dimension $p*q$ 17150 and a list {\tt b} of size $p$ (i.e. {\tt nrows(A)=size(b)}).\\ 17151{\tt border} returns the matrix obtained by appending 17152{\tt tran(b)} as last column to the matrix {\tt A}, therefore: 17153\begin{center} 17154{\tt border(A,b)=tran([op(tran(A)),b])=tran(append(tran(A),b))} 17155\end{center} 17156Input : 17157\begin{center}{\tt border([[1,2,4],[3,4,5]],[6,7])}\end{center} 17158Output : 17159\begin{center}{\tt [[1,2,4,6],[3,4,5,7]]}\end{center} 17160Input : 17161\begin{center}{\tt border([[1,2,3,4],[4,5,6,8],[7,8,9,10]],[1,3,5])}\end{center} 17162Output : 17163\begin{center}{\tt [[1,2,3,4,1],[4,5,6,8,3],[7,8,9,10,5]]}\end{center} 17164 17165\subsection{Count the elements of a matrix verifying a property : {\tt count}}\index{count} 17166\noindent{\tt count} takes as arguments : a real function {\tt f} and 17167a real matrix {\tt A} of dimension {\tt p*q} (resp. a list {\tt l} of size 17168{\tt n}).\\ 17169{\tt count} returns {\tt f(A[0,0])+..f(A[p-1,q-1])} (resp. 17170{\tt f(l[0])+..f(l[n-1])})\\ 17171Hence, if {\tt f} is a boolean function, {\tt count} returns the number 17172of elements of the matrix {\tt A} (resp. the list {\tt l}) verifying 17173the property {\tt f}.\\ 17174Input : 17175\begin{center}{\tt count(x->x,[[2,12],[45,3],[7,78]])}\end{center} 17176Output : 17177\begin{center}{\tt 147}\end{center} 17178indeed: 2+12+45+3+7+78=147.\\ 17179Input : 17180\begin{center}{\tt count(x->x<10,[[2,12],[45,3],[7,78]])}\end{center} 17181Output : 17182\begin{center}{\tt 3}\end{center} 17183 17184\subsection{Count the elements equal to a given value : {\tt count\_eq}}\index{count\_eq} 17185\noindent{\tt count\_eq} takes as arguments: a real and a real list or 17186a real matrix.\\ 17187{\tt count\_eq} returns the number of elements of the list or matrix 17188equal to the first argument.\\ 17189Input : 17190\begin{center}{\tt count\_eq(12,[[2,12,45],[3,7,78]])}\end{center} 17191Output : 17192\begin{center}{\tt 1}\end{center} 17193 17194\subsection{Count the elements smaller than a given value : {\tt count\_inf}}\index{count\_inf} 17195\noindent{\tt count\_inf} takes as arguments: a real and a real list or a real 17196matrix.\\ 17197{\tt count\_inf} returns the number of elements of the list or 17198matrix which are strictly less than the first argument.\\ 17199Input : 17200\begin{center}{\tt count\_inf(12,[2,12,45,3,7,78])}\end{center} 17201Output : 17202\begin{center}{\tt 3}\end{center} 17203 17204\subsection{Count the elements greater than a given value : {\tt count\_sup}}\index{count\_sup} 17205\noindent{\tt count\_sup} takes as arguments: a real and a real list or a real 17206 matrix.\\ 17207{\tt count\_sup} returns the number of elements of the list or matrix 17208which are strictly greater to the first argument.\\ 17209Input : 17210\begin{center}{\tt count\_sup(12,[[2,12,45],[3,7,78]])}\end{center} 17211Output : 17212\begin{center}{\tt 2}\end{center} 17213 17214\subsection{Statistics functions acting on column matrices : {\tt mean}, {\tt stddev}, {\tt variance}, {\tt median}, {\tt quantile}, {\tt quartiles}, {\tt boxwhisker}}\label{sec:statmat} 17215\index{mean} \index{stddev}\index{variance}\index{median}\index{quartiles}\index{quantile}\index{boxwhisker} 17216The following functions work on matrices, acting column by column: 17217\begin{itemize} 17218\item{\tt mean} computes the arithmetic means of the statistical series 17219stored in the columns of a matrix.\\ 17220Input : 17221\begin{center}{\tt mean([[3,4,2],[1,2,6]])}\end{center} 17222Output is the vector of the means of each column : 17223\begin{center}{\tt [2,3,4]}\end{center} 17224Input : 17225\begin{center}{\tt mean([[1,0,0],[0,1,0],[0,0,1]])}\end{center} 17226Output 17227\begin{center}{\tt [1/3,1/3,1/3]}\end{center} 17228 17229\item{\tt stddev} computes the standard deviations of the population 17230statistical series stored in the columns of a matrix.\\ 17231Input : 17232\begin{center}{\tt stddev([[3,4,2],[1,2,6]])}\end{center} 17233Output is the vector of the standard deviations of each column : 17234\begin{center}{\tt [1,1,2]}\end{center} 17235\item{\tt variance} computes the variances of the statistical series 17236stored in the columns of a matrix.\\ 17237Input : 17238\begin{center}{\tt variance([[3,4,2],[1,2,6]])}\end{center} 17239Output is the vector of the variance of each column : 17240\begin{center}{\tt [1,1,4]}\end{center} 17241 17242\item{\tt median} computes the medians of the statistical series 17243stored in the columns of a matrix.\\ 17244Input : 17245\begin{center}{\tt median([[6,0,1,3,4,2,5],[0,1,3,4,2,5,6],[1,3,4,2,5,6,0], [3,4,2,5,6,0,1],[4,2,5,6,0,1,3],[2,5,6,0,1,3,4]])}\end{center} 17246Output is the vector of the median of each column : 17247\begin{center}{\tt [3,3,4,4,4,3,4]}\end{center} 17248 17249\item{\tt quantile} computes the deciles as specified by the second 17250argument of the statistical series stored in the columns of a matrix.\\ 17251Input :\begin{center}{\tt quantile([[6,0,1,3,4,2,5],[0,1,3,4,2,5,6],[1,3,4,2,5,6,0], [3,4,2,5,6,0,1],[4,2,5,6,0,1,3],[2,5,6,0,1,3,4]],0.25)}\end{center} 17252Output is the vector of the first quartile of each column : 17253\begin{center}{\tt [1,1,2,2,1,1,1]}\end{center} 17254Input : 17255\begin{center}{\tt quantile([[6,0,1,3,4,2,5],[0,1,3,4,2,5,6],[1,3,4,2,5,6,0], [3,4,2,5,6,0,1],[4,2,5,6,0,1,3],[2,5,6,0,1,3,4]],0.75)}\end{center} 17256Output is the vector of the third quartile of each column : 17257\begin{center}{\tt [3,3,4,4,4,3,4]}\end{center} 17258 17259\item{\tt quartiles} computes the minima, the first quartiles, the 17260medians, the third quartiles and the maxima of the statistical series 17261stored in the columns of a matrix.\\ 17262Input : 17263\begin{center}{\tt quartiles([[6,0,1,3,4,2,5],[0,1,3,4,2,5,6],[1,3,4,2,5,6,0], [3,4,2,5,6,0,1], [4,2,5,6,0,1,3], [2,5,6,0,1,3,4]])}\end{center} 17264Output is a matrix, its first row is the minima of each column, 17265its second row is the fist quartiles of each column, 17266its third row the medians 17267of each column, its fourth row the third 17268quartiles of each column and its last row the maxima of each column: 17269\begin{center}{\tt [[0,0,1,0,0,0,0],[1,1,2,2,1,1,1], [2,2,3,3,2,2,3],}\end{center} 17270\begin{center}{\tt [3,3,4,4,4,3,4],[6,5,6,6,6,6,6]]}\end{center} 17271 17272\item{\tt boxwhisker} draws the whisker boxes of the statistical series 17273stored in the columns of a matrix .\\ 17274Input : 17275\begin{center}{\tt boxwhisker([[6,0,1,3,4,2,5],[0,1,3,4,2,5,6], [1,3,4,2,5,6,0],[3,4,2,5,6,0,1], [4,2,5,6,0,1,3],[2,5,6,0,1,3,4]])}\end{center} 17276Output : 17277\begin{center}{\tt the drawing of the whisker boxes of the statistical 17278series of each column of the matrix argument}\end{center} 17279\end{itemize} 17280 17281\subsection{Dimension of a matrix : {\tt dim}}\index{dim} 17282\noindent{\tt dim} takes as argument a matrix $A$.\\ 17283{\tt dim} returns the list of the number of rows and columns 17284of the matrix $A$.\\ 17285Input : 17286\begin{center}{\tt dim([[1,2,3],[3,4,5]])}\end{center} 17287Output : 17288\begin{center}{\tt [2,3]}\end{center} 17289 17290\subsection{Number of rows : {\tt rowdim rowDim nrows}}\index{rowdim}\index{rowDim}\index{nrows} 17291\noindent{\tt rowdim} (or {\tt rowDim} or {\tt nrows}) takes as argument a 17292matrix $A$.\\ 17293{\tt rowdim} (or {\tt rowDim} or {\tt nrows}) returns the number of rows of the 17294matrix $A$.\\ 17295Input : 17296\begin{center}{\tt rowdim([[1,2,3],[3,4,5]])}\end{center} 17297or : 17298\begin{center}{\tt nrows([[1,2,3],[3,4,5]])}\end{center} 17299Output : 17300\begin{center}{\tt 2}\end{center} 17301 17302\subsection{Number of columns : {\tt coldim colDim ncols}}\index{coldim}\index{colDim}\index{ncols} 17303\noindent{\tt coldim} (or {\tt colDim} or {\tt ncols}) takes as argument a 17304matrix $A$.\\ 17305{\tt coldim} (or {\tt colDim} or {\tt ncols}) returns the number of columns of 17306the matrix $A$.\\ 17307Input : 17308\begin{center}{\tt coldim([[1,2,3],[3,4,5]])}\end{center} 17309or : 17310\begin{center}{\tt ncols([[1,2,3],[3,4,5]])}\end{center} 17311Output : 17312\begin{center}{\tt 3}\end{center} 17313 17314\section{Sparse matrices} 17315 17316\subsection{Defining sparse matrices} 17317 17318A matrix is \emph{sparse} if most of its elements are 0. To define a 17319sparse matrix, it is enough to define the non-zero elements, which can 17320be done with a table. The \texttt{matrix} command can then turn the 17321table into a matrix.\\ 17322Input: 17323\begin{center} 17324 \tt 17325 A := table((0,0)=1, (1,1)=2, (2,2)=3, (3,3) = 4, (4,4) = 5) 17326\end{center} 17327or: 17328\begin{center} 17329 \tt 17330 purge(A)\\ 17331 A[0..4,0..4]:=[1,2,3,4,5] 17332\end{center} 17333Output: 17334\begin{center} 17335 \tt 17336table((0,0) = 1, (1,1) = 2, (2,2) = 3, (3,3) = 4, (4,4) = 5) 17337\end{center} 17338 17339This table can be converted to a matrix with either the 17340\texttt{convert} command or the \texttt{matrix} command.\\ 17341Input: 17342\begin{center} 17343 \tt 17344 a := convert(A,array) 17345\end{center} 17346or: 17347\begin{center} 17348 \tt 17349 a := matrix(A) 17350\end{center} 17351Output: 17352\begin{center} 17353 \tt 17354 [[1,0,0,0,0],[0,2,0,0,0],[0,0,3,0,0],[0,0,0,4,0],[0,0,0,0,5]] 17355\end{center} 17356 17357\subsection{Operations on sparse matrices} 17358 17359All matrix operations can be done on tables that are used to define 17360sparse matrices.\\ 17361Input: 17362\begin{center} 17363 \tt 17364 purge(A), purge(B) 17365\end{center} 17366Input: 17367\begin{center} 17368 \tt 17369 A[0..2,0..2] := [1,2,3] 17370\end{center} 17371Output: 17372\begin{center} 17373 \tt 17374table((0,0) = 1, (1,1) = 2, (2,2) = 3) 17375\end{center} 17376Input: 17377\begin{center} 17378 \tt 17379 B[0..1,1..2] := [1,2] 17380\end{center} 17381Input: 17382\begin{center} 17383 \tt 17384 B[0..2,0]:=5 17385\end{center} 17386Output: 17387\begin{center} 17388 \tt 17389table((0,0) = 5, (0,1) = 1, (1,0) = 5, (1,2) = 2, (2,0) = 5) 17390\end{center} 17391The usual operations will work on \texttt{A} and \texttt{B}.\\ 17392Input: 17393\begin{center} 17394 \tt 17395 A + B 17396\end{center} 17397Output: 17398\begin{center} 17399 \tt 17400 table((0,0) = 6, (0,1) = 1, (1,0) = 5, (1,1) = 2, (1,2) = 2, (2,0) = 5, (2,2) = 3) 17401\end{center} 17402Input: 17403\begin{center} 17404 \tt 17405 A * B 17406\end{center} 17407Output: 17408\begin{center} 17409 \tt 17410 table((0,0) = 5, (0,1) = 1, (1,0) = 10, (1,2) = 4, (2,0) = 15) 17411\end{center} 17412Input: 17413\begin{center} 17414 \tt 17415 2*A 17416\end{center} 17417Output: 17418\begin{center} 17419 \tt 17420 table((0,0) = 2, (1,1) = 4, (2,2) = 6) 17421\end{center} 17422 17423\section{Linear algebra} 17424\subsection{Transpose of a matrix : {\tt tran transpose}}\index{tran}\index{transpose} 17425\noindent{\tt tran} or {\tt transpose} takes as argument a matrix $A$.\\ 17426{\tt tran} or {\tt transpose} returns the transpose matrix of $A$.\\ 17427Input : 17428\begin{center}{\tt tran([[1,2],[3,4]])}\end{center} 17429Output : 17430\begin{center}{\tt [[1,3],[2,4]]}\end{center} 17431 17432\subsection{Inverse of a matrix : {\tt inv /}}\index{inv|textbf}\index{/|textbf} 17433\noindent{\tt inv} takes as argument a square matrix $A$.\\ 17434{\tt inv} returns the inverse matrix of $A$.\\ 17435Input : 17436\begin{center}{\tt inv([[1,2],[3,4]])}\end{center} 17437or : 17438\begin{center}{\tt 1/[[1,2],[3,4]])}\end{center} 17439or : 17440\begin{center}{\tt A:=[[1,2],[3,4]];1/A}\end{center} 17441Output : 17442\begin{center}{\tt [[-2,1],[3/2,1/-2]]}\end{center} 17443 17444\subsection{Trace of a matrix : {\tt trace}}\index{trace} 17445\noindent{\tt trace} takes as argument a matrix $A$.\\ 17446{\tt trace} returns the trace of the matrix $A$, that is 17447the sum of the diagonal elements.\\ 17448Input : 17449\begin{center}{\tt trace([[1,2],[3,4]])}\end{center} 17450Output : 17451\begin{center}{\tt 5}\end{center} 17452 17453\subsection{Determinant of a matrix : {\tt det}} 17454\index{det|textbf} 17455\index{lagrange@\textit{lagrange}} 17456\index{rational\_det@\textit{rational\_det}} 17457\index{bareiss@\textit{bareiss}} 17458\index{linsolve@\textit{linsolve}} 17459\index{minor\_det@\textit{minor\_det}} 17460 17461\noindent{\tt det} takes as argument a matrix $A$.\\ 17462{\tt det} returns the determinant of the matrix $A$.\\ 17463Input : 17464\begin{center}{\tt det([[1,2],[3,4]])}\end{center} 17465Output : 17466\begin{center}{\tt -2}\end{center} 17467Input : 17468\begin{center}{\tt det(idn(3))}\end{center} 17469Output : 17470\begin{center}{\tt 1}\end{center} 17471 17472An optional argument can be used to specify with an optional argument. 17473\begin{itemize} 17474 \item \texttt{lagrange} When the matrix elements are polynomials or 17475 rational functions, this method computes the determinant by 17476 evaluating the elements and using Lagrange interpolation. 17477 \item \texttt{rational\_det} This method uses Gaussian elimination 17478 without converting to to the internal format for fractions. 17479 \item \texttt{bareiss} This uses the Gauss-Bareiss algorithm. 17480 \item \texttt{linsolve} This uses the $p$-adic algorithm for 17481 matrices with integer coefficients. 17482 \item \texttt{minor\_det} This uses expansion by minor determinants. 17483 This requires $2^n$ operations, but can stil be faster for average 17484 sized matrices (up to about $n=20$). 17485\end{itemize} 17486 17487\subsection{Determinant of a sparse matrix : {\tt det\_minor}}\index{det\_minor} 17488\noindent{\tt det\_minor} takes as argument a matrix $A$.\\ 17489{\tt det\_minor} returns the determinant of the matrix $A$ computed by 17490expanding the determinant using Laplace's algorithm.\\ 17491Input : 17492\begin{center}{\tt det\_minor([[1,2],[3,4]])}\end{center} 17493Output : 17494\begin{center}{\tt -2}\end{center} 17495Input : 17496\begin{center}{\tt det\_minor(idn(3))}\end{center} 17497Output : 17498\begin{center}{\tt 1}\end{center} 17499 17500\subsection{Rank of a matrix : {\tt rank}}\index{rank} 17501\noindent{\tt rank} takes as argument a matrix $A$.\\ 17502{\tt rank} returns the rank of the matrix $A$.\\ 17503Input : 17504\begin{center}{\tt rank([[1,2],[3,4]])}\end{center} 17505Output : 17506\begin{center}{\tt 2}\end{center} 17507Input : 17508\begin{center}{\tt rank([[1,2],[2,4]])}\end{center} 17509Output : 17510\begin{center}{\tt 1}\end{center} 17511 17512\subsection{Transconjugate of a matrix : {\tt trn}}\index{trn} 17513\noindent{\tt trn} takes as argument a matrix $A$.\\ 17514{\tt trn} returns the transconjugate of $A$ (i.e. the conjugate of the 17515transpose matrix of $A$).\\ 17516Input : 17517\begin{center}{\tt trn([[i, 1+i],[1, 1-i]])}\end{center} 17518Output after simplification: 17519\begin{center}{\tt [[-i,1],[1-i,1+i]]}\end{center} 17520 17521 17522\subsection{Equivalent matrix : {\tt changebase}}\index{changebase} 17523\noindent{\tt changebase} takes as argument a matrix $A$ and a 17524change-of-basis matrix $P$.\\ 17525{\tt changebase} returns the matrix $B$ such that $B=P^{-1}AP$.\\ 17526Input : 17527\begin{center}{\tt changebase([[1,2],[3,4]],[[1,0],[0,1]])}\end{center} 17528Output : 17529\begin{center}{\tt [[1,2],[3,4]]}\end{center} 17530Input : 17531\begin{center}{\tt changebase([[1,1],[0,1]],[[1,2],[3,4]])}\end{center} 17532Output : 17533\begin{center}{\tt [[-5,-8],[9/2,7]]}\end{center} 17534Indeed : 17535 $${\left[\begin{array}{rr} 1 & 2\\3&4\end{array}\right]}^{-1}*\left[\begin{array}{rr}1 & 1\\0&1\end{array}\right]*\left[\begin{array}{rr}1 & 2\\3&4\end{array}\right]=\left[\begin{array}{rr}-5 & -8\\\frac{9}{2}&7\end{array}\right]$$. 17536 17537\subsection{Basis of a linear subspace : {\tt basis}}\index{basis} 17538\noindent{\tt basis} takes as argument a list of vectors generating 17539a linear subspace of $\mathbb R^n$.\\ 17540{\tt basis} returns a list of vectors, that is a basis of this 17541linear subspace.\\ 17542Input : 17543\begin{center}{\tt basis([[1,2,3],[1,1,1],[2,3,4]])}\end{center} 17544Output : 17545\begin{center}{\tt [[1,0,-1],\ [0,1,2]]}\end{center} 17546 17547\subsection{Basis of the intersection of two subspaces : {\tt ibasis}}\index{ibasis} 17548\noindent{\tt ibasis} takes as argument two lists of vectors generating 17549two subspaces of $\mathbb R^n$.\\ 17550{\tt ibasis} returns a list of vectors, that is a basis of the 17551intersection of these two subspaces.\\ 17552Input : 17553\begin{center}{\tt ibasis([[1,2]],[[2,4]])}\end{center} 17554Output : 17555\begin{center}{\tt [[1,2]]}\end{center} 17556 17557\subsection{Image of a linear function : {\tt image}}\index{image} 17558\noindent{\tt image} takes as argument the matrix of a linear 17559function $f$ with respect to the canonical basis.\\ 17560{\tt image} returns a list of vectors that is a basis of the image 17561of $f$.\\ 17562Input : 17563\begin{center}{\tt image([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 17564Output : 17565\begin{center}{\tt [[-1,0,1],[0,-1,-2]]}\end{center} 17566 17567\subsection{Kernel of a linear function : {\tt kernel nullspace ker}}\index{ker}\index{kernel}\index{nullspace} 17568\noindent{\tt ker} (or {\tt kernel} or {\tt nullspace}) takes as argument the 17569matrix of an linear function $f$ with respect to the canonical basis.\\ 17570{\tt ker} (or {\tt kernel} or {\tt nullspace}) returns a list of 17571vectors that is a basis of the kernel of $f$.\\ 17572Input : 17573\begin{center}{\tt ker([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 17574Output : 17575\begin{center}{\tt [[1,1,-1]]}\end{center} 17576The kernel is generated by the vector {\tt [1,1,-1]}. 17577 17578\subsection{Kernel of a linear function : {\tt Nullspace}}\index{Nullspace} 17579\noindent {\bf Warning} This function is useful in Maple mode only 17580(hit the state line red button then {\tt Prog style}, 17581then choose Maple and Apply).\\ 17582{\tt Nullspace} is the inert form of {\tt nullspace}.\\ 17583{\tt Nullspace} takes as argument an integer matrix of a linear 17584function $f$ with respect to the canonical basis.\\ 17585{\tt Nullspace}) followed by {\tt mod p} returns a list of vectors 17586that is a basis of the kernel of $f$ computed in $\mathbb Z/p\mathbb Z[X]$.\\ 17587Input : 17588\begin{center}{\tt Nullspace([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 17589Output : 17590\begin{center}{\tt nullspace([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 17591Input (in Maple mode): 17592\begin{center}{\tt Nullspace([[1,2],[3,1]]) mod 5}\end{center} 17593Output : 17594\begin{center}{\tt [2,-1]}\end{center} 17595In Xcas mode, the equivalent input is : 17596\begin{center}{\tt nullspace([[1,2],[3,1]] \% 5)}\end{center} 17597Output : 17598\begin{center}{\tt [2\% 5,-1]}\end{center} 17599 17600\subsection{Subspace generated by the columns of a matrix : {\tt colspace}}\index{colspace} 17601\noindent{\tt colspace} takes as argument the matrix $A$ of a linear 17602function $f$ with respect to the canonical basis.\\ 17603{\tt colspace} returns a matrix. The columns of this matrix are a basis of the 17604subspace generated by the columns of $A$.\\ 17605{\tt colspace} may have a variable name as second argument, where 17606{\tt Xcas} 17607will store the dimension of the subspace generated by the columns of $A$.\\ 17608Input : 17609\begin{center}{\tt colspace([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 17610Output : 17611\begin{center}{\tt [[-1,0],[0,-1],[1,-2]]}\end{center} 17612Input : 17613\begin{center}{\tt colspace([[1,1,2],[2,1,3],[3,1,4]],dimension)}\end{center} 17614Output : 17615\begin{center}{\tt [[-1,0],[0,-1],[1,-2]]}\end{center} 17616Then input: 17617\begin{center}{\tt dimension}\end{center} 17618Output : 17619\begin{center}{\tt 2}\end{center} 17620 17621\subsection{Subspace generated by the rows of a matrix : {\tt rowspace}}\index{rowspace} 17622\noindent{\tt rowspace} takes as argument the matrix $A$ of a linear 17623function $f$ with respect to the canonical basis.\\ 17624{\tt rowspace} returns a list of vectors that is a basis of the 17625subspace generated by the rows of $A$.\\ 17626{\tt rowspace} may have a variable name as second argument where {\tt Xcas} 17627will store the dimension of the subspace generated by the rows of $A$.\\ 17628Input : 17629\begin{center}{\tt rowspace([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 17630Output : 17631\begin{center}{\tt [[-1,0,-1],[0,-1,-1]]}\end{center} 17632Input : 17633\begin{center}{\tt rowspace([[1,1,2],[2,1,3],[3,1,4]],dimension)}\end{center} 17634Output : 17635\begin{center}{\tt [[-1,0,-1],[0,-1,-1]]}\end{center} 17636Then input: 17637\begin{center}{\tt dimension}\end{center} 17638Output : 17639\begin{center}{\tt 2}\end{center} 17640 17641\section{Linear Programmation}\index{simplex\_reduce|textbf} 17642Linear programming problems are maximization problem of a linear 17643functionals under linear equality or inequality constraints. 17644The most simple case can be solved directly by the so-called simplex 17645algorithm. Most cases require to solve an auxiliary linear 17646programming problem to find an initial vertex for the simplex 17647algorithm. 17648 17649\subsection{Simplex algorithm: {\tt simplex\_reduce}} 17650{\bf The simple case}\\ 17651The function {\tt simplex\_reduce} makes the reduction 17652by the simplex algorithm to find : 17653\[ \mbox{max}(c.x), \quad A.x \leq b,\ x \geq 0,\ b\geq 0 \] 17654where $c,x$ are vectors of $\mathbb R^n$, $b\geq 0$ is a vector in 17655$\mathbb R^p$ and $A$ is a matrix of $p$ rows and $n$ columns.\\ 17656{\tt simplex\_reduce} takes as argument {\tt A,b,c} and 17657returns {\tt max(c.x)}, the augmented solution of {\tt x} 17658(augmented since the algorithm works by adding rows($A$) auxiliary 17659variables) and the reduced matrix.\\ 17660{\bf Example}\\ 17661Find \[ \mbox{max}(X+2Y) \mbox{ where } 17662\left\{ 17663\begin{array}{rcl} 17664(X,Y) & \geq & 0 \\ 17665-3X +2Y & \leq & 3\\ 17666X +Y & \leq & 4 17667\end{array} 17668\right. 17669\] 17670Input : 17671\begin{center}{\tt simplex\_reduce([[-3,2],[1,1]],[3,4],[1,2])}\end{center} 17672Output : 17673\begin{center}{\tt 7,[1,3,0,0],[[0,1,1/5,3/5,3],[1,0,(-1)/5,2/5,1], [0,0,1/5,8/5,7]]}\end{center} 17674Which means that the maximum of {\tt X+2Y} under these conditions 17675is {\tt 7}, it is obtained for {\tt X=1,Y=3} 17676because {\tt [1,3,0,0]} is the augmented solution and the reduced matrix is :\\ 17677{\tt [[0,1,1/5,3/5,3],[1,0,(-1)/5,2/5,1], [0,0,1/5,8/5,7]]}. 17678 17679{\bf A more complicated case that reduces to the simple case}\\ 17680With the former call of {\tt simplex\_reduce}, we have to : 17681\begin{itemize} 17682\item rewrite constraints to the form $x_k \geq 0$, 17683\item remove variables without constraints, 17684\item add variables such that all the constraints have positive components. 17685\end{itemize} 17686For example, find : 17687\begin{equation}\label{eq:lpexample} 17688\mbox{min}(2x+y-z+4) \quad \mbox{ where } 17689\left\{ 17690\begin{array}{rcl} 17691x & \leq & 1 \\ 17692y & \geq & 2 \\ 17693x+3y-z & = & 2 \\ 176942x-y+z & \leq & 8\\ 17695-x+y & \leq & 5 17696\end{array} 17697\right. 17698\end{equation} 17699Let $x=1-X$, $y=Y+2$, $z=5-X+3Y$ 17700the problem is equivalent to finding the minimum of 17701$(-2X+Y-(5-X+3Y)+8)$ 17702where : 17703\[ 17704\left\{ 17705\begin{array}{rcl} 17706X & \geq & 0 \\ 17707Y & \geq & 0 \\ 177082(1-X)-(Y+2)+ 5-X+3Y & \leq & 8\\ 17709-(1-X) +(Y+2) & \leq & 5 17710\end{array} 17711\right. 17712\] 17713or to find the minimum of~: 17714\[ (-X-2Y+3) \quad \mbox{ where } 17715\left\{ 17716\begin{array}{rcl} 17717X & \geq & 0 \\ 17718Y & \geq & 0 \\ 17719-3X+2Y & \leq & 3\\ 17720X +Y & \leq & 4 17721\end{array} 17722\right. 17723\] 17724i.e. to find the maximum of $-(-X-2Y+3)=X+2Y-3$ 17725under the same conditions, hence it is the same problem as 17726to find the maximum of $X+2Y$ seen before. We found {\tt 7}, 17727hence, the result here is {\tt 7-3=4}. 17728 17729{\bf The general case}\\ 17730A linear programming problem may not in general be directly 17731reduced like above to the simple case. The reason is that 17732a starting vertex must be found before applying the simplex 17733algorithm. Therefore, 17734{\tt simplex\_reduce} may be called by specifying this starting 17735vertex, in that case, all the arguments including the starting 17736vertex are grouped in a single matrix. 17737 17738We first illustrate this kind 17739of call in the simple case where the starting point does not 17740require solving an auxiliary problem. 17741If {\tt A} has $p$ rows and $n$ columns and if we define : 17742\begin{center} 17743{\tt B:=augment(A,idn(p));} {\tt C:=border(B,b);} \\ 17744{\tt d:=append(-c,0\$(p+1));} {\tt D:=augment(C,[d]);} 17745\end{center} 17746{\tt simplex\_reduce} may be called with {\tt D} as single argument.\\ 17747For the previous example, input : 17748\begin{center}{\tt A:=[[-3,2],[1,1]];B:=augment(A,idn(2)); C:=border(B,[3,4]); 17749D:=augment(C,[[-1,-2,0,0,0]])}\end{center} 17750Here 17751{\tt C=[[-3,2,1,0,3],[1,1,0,1,4]]}\\ 17752and {\tt D=[[-3,2,1,0,3],[1,1,0,1,4],[-1,-2,0,0,0]]}\\ 17753Input : 17754\begin{center}{\tt simplex\_reduce(D)}\end{center} 17755Output is the same result as before. 17756 17757{\bf Back to the general case.}\\ 17758The standard form of a linear programming problem is similar 17759to the simplest case above, but with $Ax=b$ (instead of $Ax\leq b$) 17760under the conditions $x\geq 0$. We may further assume that $b\geq 0$ 17761(if not, one can change the sign of the corresponding line). 17762\begin{itemize} 17763\item The first problem is to find an $x$ in the $Ax=b, x\geq 0$ domain. 17764Let $m$ be the number of lines of $A$. Add artificial variables 17765$y_1,...,y_m$ and maximize 17766$-\sum y_i$ under the conditions $Ax=b, x \geq 0, y \geq 0$ 17767starting with initial value $0$ for $x$ variables 17768and $y=b$ 17769(to solve this with {\tt Xcas}, call \verb|simplex_reduce| with 17770a single matrix argument obtained by augmenting $A$ by the 17771identity, $b$ unchanged and an artificial 17772$c$ with 0 under $A$ and 1 under the identity). 17773If the maximum exists and is 0, the identity submatrix above the last 17774column corresponds to an $x$ solution, we may forget the artificial 17775variables (they are 0 if the maximum is 0). 17776\item Now we make a second call to \verb|simplex_reduce| 17777with the original $c$ and the value of $x$ we found in the domain. 17778\item 17779Example~: find the minimum of $2x+3y-z+t$ with 17780$x,y,z,t\geq 0$ and~: 17781\[ \left\{ \begin{array}{rcl} 17782-x-y+t&=&1\\ 17783y-z+t&=&3 17784\end{array} 17785\right. \] 17786This is equivalent to find the opposite of the maximum of $-(2x+3y-z+t)$. 17787Let us add two artificial variables $y_1$ and $y_2$, 17788\begin{verbatim} 17789simplex_reduce([[-1,-1,0,1,1,0,1], 17790[0,1,-1,1,0,1,3], 17791[0,0,0,0,1,1,0]]) 17792\end{verbatim} 17793Output: optimum=0, artificial variables=0, and the matrix 17794\[ 17795\left(\begin{array}{ccccccc} 17796-1/2 & 0 & -1/2 & 1 & 1/2 & 1/2 & 2 \\ 177971/2 & 1 & -1/2 & 0 & -1/2 & 1/2 & 1 \\ 177980 & 0 & 0 & 0 & 1 & 1 & 0 17799\end{array}\right) 17800\] 17801Columns 2 and 4 are the columns of the identity (in lines 1 and 2). 17802Hence $x=(0,1,0,2)$ is an initial point in the domain. 17803We are reduced to solve the initial problem, after replacing the 17804lines of $Ax=b$ by the two first lines of the answer above, 17805removing the last columns corresponding to the artificial variables. 17806We add $c.x$ as last line 17807\begin{verbatim} 17808simplex_reduce([[-1/2,0,-1/2,1,2], 17809[1/2,1,-1/2,0,1],[2,3,-1,1,0]]) 17810\end{verbatim} 17811Output: maximum=-5, hence the minimum of the opposite is 5, 17812obtained for $(0,1,0,2)$, after replacement 17813$x=0$, $y=1$, $z=0$ and $t=2$. 17814\end{itemize} 17815 17816For more details, search google for \verb|simplex algorithm|. 17817 17818\subsection{Solving general linear programming problems: {\tt lpsolve}}\index{lpsolve|textbf} 17819Linear programming problems (where a multivariate linear function needs to be maximized or minimized subject to linear (in)equality constraints), as well as (mixed) integer programming problems, can be solved by using the function {\tt lpsolve}. Problems can be entered directly (in symbolic or matrix form) or loaded from a file in LP or (gzipped) MPS format. 17820 17821{\tt lpsolve} accepts four arguments : 17822\begin{enumerate} 17823\item {\tt obj} : symbolic expression representing the objective function or path to file containing LP problem (in the latter case parameter {\tt constr} should not be given) 17824\item {\tt constr} (optional) : list of linear constraints which may be equalities or inequalities or bounded expressions entered as {\tt expr=a..b} 17825\item {\tt bd} (optional) : sequence of expressions of type {\tt var=a..b} specifying that the variable {\tt var} is bounded with {\tt a} below and with {\tt b} above 17826\item {\tt opts} (optional) : sequence of solver settings in form {\tt option=value}, where {\tt option} may be one of : 17827\begin{description} 17828 \item [\tt assume] -- one of {\tt lp\_nonnegative}, {\tt lp\_integer} ({\tt integer}), {\tt lp\_binary} or {\tt lp\_nonnegint} (or {\tt nonnegint}), default : \emph{unset} 17829 \item [\tt lp\_integervariables] -- list of identifiers or indices (of integer variables), default : \emph{empty} 17830 \item [\tt lp\_binaryvariables] -- list of identifiers or indices (of binary variables), default : \emph{empty} 17831 \item [\tt lp\_maximize] (or {\tt maximize}) -- \emph{true} or \emph{false} (objective direction), default : \emph{false} 17832 \item [\tt lp\_method] -- one of {\tt exact}, {\tt float}, {\tt lp\_simplex} or {\tt lp\_interiorpoint}\\(solver type), default {\tt lp\_simplex} 17833 \item [\tt lp\_depthlimit] -- positive integer (max.~depth of branch\&bound tree), default : \emph{unlimited} 17834 \item [\tt lp\_nodelimit] -- positive integer (max.~nodes in branch\&bound tree), default : \emph{unlimited} 17835 \item [\tt lp\_iterationlimit] -- positive integer (max.~iterations of simplex algorithm), default : \emph{unlimited} 17836 \item [\tt lp\_timelimit] -- positive number (max.~solving time in milliseconds), default : \emph{unlimited} 17837 \item[\tt lp\_maxcuts] -- nonnegative integer (max.~GMI cuts per node), default : 5 17838 \item [\tt lp\_gaptolerance] -- positive number (relative integrality gap threshold), default : 0 17839 \item[\tt lp\_nodeselect] -- one of {\tt lp\_depthfirst}, {\tt lp\_breadthfirst}, {\tt lp\_hybrid} or {\tt lp\_bestprojection} (branching node selection strategy), default : {\tt lp\_hybrid} 17840 \item[\tt lp\_varselect] -- one of {\tt lp\_firstfractional}, {\tt lp\_lastfractional},\\{\tt lp\_mostfractional} or {\tt lp\_pseudocost} (branching variable selection strategy), default : {\tt lp\_pseudocost} 17841 \item [\tt lp\_verbose] -- \emph{true} or \emph{false}, default : \emph{false} 17842\end{description} 17843\end{enumerate} 17844 17845The return value is in the form {\tt [optimum,soln]} where {\tt optimum} is the minimum/maximum value of the objective function and {\tt soln} is the list of coordinates corresponding to the point at which the optimal value is attained, i.e.~the optimal solution. If there is no feasible solution, an empty list is returned. When the objective function is unbounded, {\tt optimum} is returned as {\tt +infinity} (for maximization problems) or {\tt -infinity} (for minimization problems). If an error is experienced while solving (terminating the process), {\tt undef} is returned. 17846 17847The given objective function is minimized by default. To maximize it, include the option {\tt lp\_maximize=true} or {\tt lp\_maximize} or simply {\tt maximize}. Also note that all variables are, unless specified otherwise, assumed to be continuous and unrestricted in sign. 17848 17849\subsubsection{Solving LP problems} 17850By default, {\tt lpsolve} uses primal simplex method implementation to solve LP problems. For example, to solve the problem specified in~\eqref{eq:lpexample}, input : 17851\begin{center} 17852{\tt constr:=[x<=1,y>=2,x+3y-z=2,3x-y+z<=8,-x+y<=5];}\\ 17853{\tt lpsolve(2x+y-z+4,constr)} 17854\end{center} 17855Output : 17856\begin{center} 17857\tt [-4,[x=0,y=5,z=13]] 17858\end{center} 17859Therefore, the minimum value of $ f(x,y,z)=2\,x+y-z+4 $ is equal to $ -4 $ under the given constraints. The optimal value is attained at point $ (x,y,z)=(0,5,13) $. 17860 17861Constraints may also take the form {\tt expr=a..b} for bounded linear expressions. 17862 17863\noindent Input : 17864\begin{center} 17865{\tt lpsolve(x+2y+3z,[x+y=1..5,y+z+1=2..4,x>=0,y>=0])} 17866\end{center} 17867Output : 17868\begin{center} 17869{\tt [-2,[x=0,y=5,z=-4]]} 17870\end{center} 17871 17872Use the {\tt assume=lp\_nonnegative} option to specify that all variables are nonnegative. It is easier than entering the nonnegativity constraints explicitly. 17873 17874\noindent Input: 17875\begin{center} 17876{\tt lpsolve(-x-y,[y<=3x+1/2,y<=-5x+2],}\\ 17877{\tt assume=lp\_nonnegative)} 17878\end{center} 17879Output: 17880\begin{center} 17881{\tt [-5/4,[x=3/16,y=17/16]]} 17882\end{center} 17883 17884Bounds can be added separately for some variables. They should be entered after constraints. 17885 17886\noindent Input : 17887\begin{center} 17888{\tt constr:=[5x-10y<=20,2z-3y=6,-x+3y<=3];}\\ 17889{\tt lpsolve(-6x+4y+z,constr,x=1..20,y=0..inf)} 17890\end{center} 17891Output : 17892\begin{center} 17893{\tt [-133/2,[x=18,y=7,z=27/2]]} 17894\end{center} 17895 17896Number of iterations can be limited by setting {\tt lp\_iterationlimit} to some positive integer. If maximum number of iterations is reached, the current feasible solution (not necessarily an optimal one) is returned. 17897 17898\subsubsection{Entering problems in matrix form} 17899{\tt lpsolve} supports entering linear programming problems in matrix form, where {\tt obj} is a vector of coefficients $ \mathbf{c} $ and {\tt constr} is a list $ [\mathbf{A},\mathbf{b},\mathbf{A}_{eq},\mathbf{b}_{eq}] $ such that objective function $ \mathbf{c}^T\,\mathbf{x} $ is to be minimized/maximized subject to constraints $ \mathbf{A}\,\mathbf{x}\leq\mathbf{b} $ and $ \mathbf{A}_{eq}\,\mathbf{x}=\mathbf{b}_{eq} $. If a problem does not contain equality constraints, parameters $ \mathbf{A}_{eq} $ and $ \mathbf{b}_{eq} $ may be omitted. For a problem that does not contain inequality constraints, empty lists must be entered in place of $ \mathbf{A} $ and in place of $ \mathbf{b} $. 17900 17901The parameter {\tt bd} is entered as a list of two vectors $ \mathbf{b}_l $ and $ \mathbf{b}_u $ of the same length as the vector $ \mathbf{c} $ such that $ \mathbf{b}_l\leq\mathbf{x}\leq\mathbf{b}_u $. These vectors may contain {\tt +infinity} or {\tt -infinity}. 17902 17903\noindent Input : 17904\begin{center} 17905 {\tt c:=[-2,1];A:=[[-1,1],[1,1],[-1,0],[0,-1]];}\\ 17906 {\tt b:=[3,5,0,0];lpsolve(c,[A,b])} 17907\end{center} 17908Output : 17909\begin{center} 17910 {\tt [-10,[5,0]]} 17911\end{center} 17912Input : 17913\begin{center} 17914 {\tt c:=[-2,5,-3];bl:=[2,3,1];bu:=[6,10,7/2];}\\ 17915 {\tt lpsolve(c,[],[bl,bu])} 17916\end{center} 17917Output : 17918\begin{center} 17919 {\tt [-15/2,[6,3,7/2]]} 17920\end{center} 17921Input : 17922\begin{center} 17923 {\tt c:=[4,5];Aeq:=[[-1,3/2],[-3,2]];beq:=[2,3];}\\ 17924 {\tt lpsolve(c,[[],[],Aeq,beq])} 17925\end{center} 17926Output : 17927\begin{center} 17928 {\tt [26/5,[-1/5,6/5]]} 17929\end{center} 17930 17931\subsubsection{Solving MIP (Mixed Integer Programming) problems} 17932{\tt lpsolve} allows restricting (some) variables to integer values. Such problems, called (\emph{mixed}) \emph{integer programming problems}, are solved by applying branch\&bound method. 17933 17934To solve pure integer programming problems, in which all variables are integers, use option {\tt assume=integer} or {\tt assume=lp\_integer}. 17935 17936\noindent Input : 17937\begin{center} 17938{\tt lpsolve(-5x-7y,[7x+y<=35,-x+3y<=6],assume=integer)} 17939\end{center} 17940Output : 17941\begin{center} 17942{\tt [-41,[x=4,y=3]]} 17943\end{center} 17944 17945Use option {\tt assume=lp\_binary} to specify that all variables are binary, i.e.~the only allowed values are 0 and 1. These usually represent {\tt false} and {\tt true}, respectively, giving the variable a certain meaning in logical context. 17946 17947\noindent Input : 17948\begin{center} 17949{\tt lpsolve(8x1+11x2+6x3+4x4,[5x1+7x2+4x3+3x4<=14],}\\ 17950{\tt assume=lp\_binary,maximize)} 17951\end{center} 17952Output : 17953\begin{center} 17954{\tt [21,[x1=0,x2=1,x3=1,x4=1]]} 17955\end{center} 17956 17957To solve mixed integer problems, where some variables are integers and some are continuous, use option keywords {\tt lp\_integervariables} to specify integer variables and/or {\tt lp\_binaryvariables} to specify binary variables. 17958 17959\noindent Input : 17960\begin{center} 17961{\tt lpsolve(x+3y+3z,[x+3y+2z<=7,2x+2y+z<=11],}\\ 17962{\tt assume=lp\_nonnegative,lp\_maximize,} 17963{\tt lp\_integervariables=[x,z])} 17964\end{center} 17965Output : 17966\begin{center} 17967 \tt [10,[x=1,y=0,z=3]] 17968\end{center} 17969 17970Use the {\tt assume=lp\_nonnegint} or {\tt assume=nonnegint} option to get nonnegative integer values. 17971 17972\noindent Input : 17973\begin{center} 17974{\tt lpsolve(2x+5y,[3x-y=1,x-y<=5],assume=nonnegint)} 17975\end{center} 17976Output : 17977\begin{center} 17978{\tt [12,[x=1,y=2]]} 17979\end{center} 17980 17981When specifying MIP problems in matrix form, lists corresponding to options {\tt lp\_integervariables} and {\tt lp\_binaryvariables} are populated with variable indices, like in the following example. 17982 17983\noindent Input : 17984\begin{center} 17985 {\tt c:=[2,-3,-5];A:=[[-5,4,-5],[2,5,7],[2,-3,4]];}\\ 17986 {\tt b:=[3,1,-2];lpsolve(c,[A,b],lp\_integervariables=[0,2])} 17987\end{center} 17988Output : 17989\begin{center} 17990 {\tt [19,[1,3/4,-1]]} 17991\end{center} 17992One can also specify a range of indices instead of a list when there is too much variables. Example : {\tt lp\_binaryvariables=0..99} means that all variables $ x_i $ such that $ 0\leq i\leq 99 $ are binary. 17993 17994\paragraph{Implementation details.} 17995Branch\&bound algorithm by definition generates a binary tree of subproblems by branching on integer variables with fractional values. {\tt lpsolve} features an implementation which stores only active nodes of branch\&bound tree in a list, thus saving a lot of space. Also, since variable bounds are the only parameters that change during branch\&bound algorithm, number of constraints does not rise with depth, which is the benefit of the upper-bounding technique built in the simplex algorithm. Therefore a steady speed and minimal resource usage is always maintained, no matter how long the execution time is. This allows for solving problems that require tens or hundreds of thousands of nodes to be generated before finding an optimal solution. 17996 17997\paragraph{Stopping criteria.} 17998There are several ways to force the branch\&bound algorithm to stop prematurely when the execution takes too much time. One can set {\tt lp\_timelimit} to integer number which defines the maximum number of milliseconds allowed to find an optimal solution. Other ways are to set {\tt lp\_nodelimit} or {\tt lp\_depthlimit} to limit the number of nodes generated in branch\&bound tree or its depth, respectively. Finally, one can set {\tt lp\_gaptolerance} to some positive value, say $ t>0 $, which terminates the algorithm after finding an incumbent solution and proving that the corresponding objective value differs from optimum value for less than $ t\cdot 100\,\% $. It is done by monitoring the size of integrality gap, i.e.~the difference between current incumbent objective value and the best objective value bound among active nodes. 17999 18000If branch\&bound algorithm terminates prematurely, a warning message indicating the cause is displayed. Incumbent solution, if any, is returned as the result, else the problem is declared to be infeasible. 18001 18002\paragraph{Branching strategies.} 18003At every iteration of branch\&bound algorithm, a node must be selected for branching on some variable that has a fractional optimal value for the corresponding relaxed subproblem. There exist different methods for making such decisions, called \emph{branching strategies}. Two types of branching strategies exist: \emph{node selection} and \emph{variable selection} strategy. 18004 18005Node selection strategy can be set by using the {\tt lp\_nodeselect} option. Possible values are : 18006\begin{description} 18007 \item[\tt lp\_breadthfirst] -- choose the active node which provides the best bound for the objective value, 18008 \item[\tt lp\_depthfirst] -- choose the deepest active node and break ties by selecting the node providing the best bound, 18009 \item[\tt lp\_hybrid] -- combine the above two strategies, 18010 \item[\tt lp\_bestprojection] -- choose the node with best simple projection. 18011\end{description} 18012By default, {\tt lp\_bestprojection} strategy is used. Another sophisticated strategy is {\tt lp\_hybrid} : before an incumbent solution is found, solver uses {\tt lp\_depthfirst} strategy, ``diving'' into the tree as an incumbent solution is more likely to be located deeply. When an incumbent is found, solver switches to {\tt lp\_breadthfirst} strategy trying to close the integrality gap as quickly as possible. 18013 18014Variable selection strategy can be set by using the {\tt lp\_varselect} option. Possible values are : 18015\begin{description} 18016 \item[\tt lp\_firstfractional] -- choose the first fractional variable, 18017 \item[\tt lp\_lastfractional] -- choose the last fractional variable, 18018 \item[\tt lp\_mostfractional] -- choose the variable with fractional part closest to 0.5, 18019 \item[\tt lp\_pseudocost] -- choose the variable which had the greatest impact on the objective value in previous branchings. 18020\end{description} 18021By default, {\tt lp\_pseudocost} strategy is used. However, since pseudocost-based choice cannot be made before all integer variables have been branched upon at least one time in each direction, {\tt lp\_mostfractional} strategy is used until that condition is fulfilled. 18022 18023Using the right combination of branching strategies may significantly reduce the number of subproblems needed to be examined when solving a particular MIP problem. However, what is ``right'' varies from problem to problem. Default strategies are the most sophisticated (as they use the available data most extensively) and usually the most effective ones. But that is not always the case, as illustrated by the following example : 18024\begin{center} 18025 Minimize $ \mathbf{c}^T\,\mathbf{x} $ subject to $ \mathbf{A}\,\mathbf{x}=\mathbf{b} $, where $ \mathbf{x}\in\mathbb{Z}_+^8 $ and 18026\end{center} 18027\[ \mathbf{A}=\begin{bmatrix} 1802822 & 13 & 26 & 33 & 21 & 3 & 14 & 26 \\ 1802939 & 16 & 22 & 28 & 26 & 30 & 23 & 24 \\ 1803018 & 14 & 29 & 27 & 30 & 38 & 26 & 26 \\ 1803141 & 26 & 28 & 36 & 18 & 38 & 16 & 26 18032\end{bmatrix},\quad\mathbf{b}=\begin{bmatrix} 7872 \\ 10466 \\ 11322 \\ 12058 \end{bmatrix},\quad\mathbf{c}=\begin{bmatrix} 180332 \\10 \\13 \\17 \\7 \\5 \\7 \\3 18034\end{bmatrix}. \] 18035When using the default settings, about 24000 subproblems need to be examined before an optimal solution is found. When {\tt lp\_nodeselect} is set to {\tt lp\_breadthfirst} the solver needs to examine only about 20000 subproblems, but when set to {\tt lp\_hybrid} (a strategy which in general performs better) it examines about 111000 nodes in total. 18036 18037\paragraph{Cutting planes.} 18038Strong Gomory mixed integer cuts are generated at every node of the branch\&bound tree and used to improve the objective value bound. After solving the relaxed subproblem with simplex method, at most one strong cut is generated and added to the subproblem which is subsequently reoptimized. Simplex reoptimizations are fast because they start with the last feasible basis, but applying cuts makes the simplex tableau larger, hence applying many of them may actually slow the computation down. To limit the number of cuts that can be applied to a subproblem, one can use {\tt lp\_maxcuts} option, setting it either to zero (which disables cut generation altogether) or to some positive integer. Also, one may set it to {\tt +infinity}, which means that any number of cuts may be applied to any node. By default, {\tt lp\_maxcuts} equals to 5. 18039 18040\paragraph{Displaying detailed output.} 18041By typing {\tt lp\_verbose=true} or simply {\tt lp\_verbose} when specifying options for {\tt lpsolve}, detailed messages are printed during and after solving a MIP problem. During branch\&bound algorithm a status report in form 18042\begin{verbatim} 18043<n>: <m> nodes active, lower bound: <lb>[, integrality gap: <g>] 18044\end{verbatim} 18045is displayed every 5 seconds, where {\tt n} is the number of already examined subproblems. Also, a report is printed every time incumbent solution is found or updated, as well as when the solver switches to pseudocost-based branching. After the algorithm is finished, i.e.~when an optimal solution is found, summary is displayed containing the total number of examined subproblems, the number of most nodes being active at the same time and the number of applied Gomory mixed integer cuts. 18046 18047In the following example, two nonnegative integers $ x_1 $ and $ x_2 $ are found such that $ 1867\,x_1+1913\,x_2=3618894 $ and $ x_1+x_2 $ is minimal. The solver shows all progress and summary messages. 18048 18049\noindent Input : 18050\begin{center} 18051 \tt lpsolve(x1+x2,[1867x1+1913x2=3618894],\\ 18052 assume=nonnegint,lp\_verbose=true) 18053\end{center} 18054Output : 18055\begin{verbatim} 18056Optimizing... 18057Applying branch&bound method to find integer feasible solutions... 18058 3937: Incumbent solution found 18059Summary: 18060 * 3938 subproblem(s) examined 18061 * max. tree size: 1 nodes 18062 * 0 Gomory cut(s) applied 18063\end{verbatim} 18064\begin{center} 18065 \tt [1916,[x1=1009,x2=907]] 18066\end{center} 18067 18068\subsubsection{Solving problems in floating-point arithmetic} 18069{\tt lpsolve} provides, in addition to its own exact solver implementing primal simplex method with upper-bounding technique, an interface to GLPK (GNU Linear Programming Kit) library which contains sophisticated LP/MIP solvers in floating-point arithmetic, designed to be very fast and to handle large problems. Choosing between the available solvers is done by setting {\tt lp\_method} option. 18070 18071By default, {\tt lp\_method} is set to {\tt lp\_simplex}, which solves the problem using primal simplex method, but performing exact computation only when all problem coefficients are exact. If at least one of them is approximative (a floating-point number), GLPK solver is used instead (see below). 18072 18073Setting {\tt lp\_method} to {\tt exact} forces the solver to perform exact computation even when some coefficients are inexact (they are converted to rational equivalents before applying simplex method). 18074 18075Specifying {\tt lp\_method=float} forces {\tt lpsolve} to use floating-point solver. If a MIP problem is given, it is combined with branch\&cut algorithm. GLPK simplex solver parameters can be controlled by setting {\tt lp\_timelimit}, {\tt lp\_gaptolerance} and {\tt lp\_varselect} options. If the latter is not set, Driebeek--Tomlin heuristic is used by default (see GLPK manual for details). If {\tt lp\_maxcuts} is greater than zero, GMI and MIR cut generation is enabled, else it is disabled. If the problem contains binary variables, cover and clique cut generation is enabled, else it is disabled. Finally, {\tt lp\_verbose=true} enables detailed messages. 18076 18077Setting {\tt lp\_method} to {\tt lp\_interiorpoint} uses primal-dual interior-point algorithm which is part of GLPK. The only parameter that can be controlled via options is the verbosity level. 18078 18079For example, try to solve the following LP problem using the default settings. 18080\begin{center} 18081 Minimize $ 1.06\,x_1+0.56\,x_2+3.0\,x_3 $ 18082\end{center} 18083subject to 18084\begin{align*} 180851.06\,x1+0.015\,x3 &\geq 729824.87\\ 180860.56\,x_2+0.649\,x_3 &\geq 1522188.03\\ 18087x_3 &\geq 1680.05\\ 18088x_k &\geq 0\quad\text{for}\ k=1,2,3 18089\end{align*} 18090\noindent Input : 18091\begin{center} 18092 {\tt lpsolve(1.06x1+0.56x2+3x3,[1.06x1+0.015x3>=729824.87,}\\ 18093 {\tt 0.56x2+0.649x3>=1522188.03,x3>=1680.05],}\\ 18094 {\tt assume=lp\_nonnegative)} 18095\end{center} 18096Output : 18097\begin{center} 18098\tt [2255937.4968,[x1=688490.254009,x2=2716245.85277,x3=1680.05]] 18099\end{center} 18100If {\tt assume=nonnegint} is used for the same problem, i.e.~when $ x_k\in\mathbb{Z}_+ $ for $ k=1,2,3 $, the following result is obtained by GLPK MIP solver : 18101\begin{center} 18102 \tt [2255940.66,[x1=688491.0,x2=2716245.0,x3=1681.0]] 18103\end{center} 18104The solution of the original problem can also be obtained with interior-point solver by including {\tt lp\_method=lp\_interiorpoint} after {\tt assume=lp\_nonnegative}~: 18105\begin{center} 18106 \tt [2255937.50731,[x1=688490.256652,x2=2716245.85608,\\ 18107 x3=1680.05195065]] 18108\end{center} 18109 18110\subsubsection{Loading problem from a file} 18111Linear (integer) programming problems can be loaded from MPS or CPLEX LP format files (these formats are described in GLPK manual, Appendices B and C). The file name string needs to be passed as {\tt obj} parameter. If the file name has extension ``lp'', CPLEX LP format is assumed, and if the extension is ``mps'' or ``gz'', MPS or gzipped MPS format is assumed. 18112 18113For example, assume that {\tt somefile.lp} file is stored in directory {\tt /path/to/file} contains the following lines of text : 18114\begin{verbatim} 18115Maximize 18116obj: x1 + 2 x2 + 3 x3 + x4 18117Subject To 18118c1: - x1 + x2 + x3 + 10 x4 <= 20 18119c2: x1 - 3 x2 + x3 <= 30 18120c3: x2 - 3.5 x4 = 0 18121Bounds 181220 <= x1 <= 40 181232 <= x4 <= 3 18124End 18125\end{verbatim} 18126To find an optimal solution to linear program specified in the file, one just needs to input : 18127\begin{center} 18128 \tt lpsolve("/path/to/file/somefile.lp") 18129\end{center} 18130Output : 18131\begin{verbatim} 18132Reading problem data from '/path/to/file/somefile.lp'... 181333 rows, 4 columns, 9 non-zeros 1813410 lines were read 18135\end{verbatim} 18136\begin{center} 18137 \tt [116,[x1=38,x2=9,x3=19,x4=3]] 18138\end{center} 18139 18140Additional variable bounds and options may be provided alongside the file name. Note that the original constraints (those which are read from file) cannot be removed. 18141 18142\noindent Input : 18143\begin{center} 18144 \tt lpsolve("/path/to/file/somefile.lp",x2=1..8,x3=-10..10,\\ 18145 lp\_integervariables=[x4]) 18146\end{center} 18147Output : 18148\begin{center} 18149 \tt [82,[x1=38,x2=6,x3=10,x4=2]] 18150\end{center} 18151 18152It is advisable to use only (capital) letters, digits and underscore when naming variables in a LP file, although the corresponding format allows many more characters. That is because these names are converted to Giac identifiers during the loading process. 18153 18154\textbf{Warning!} Too large problems won't be loaded. More precisely, if $ n_v\cdot n_c>10^5 $, where $ n_v $ is the number of variables and $ n_c $ is the number of constraints, loading is aborted. Many MPS files available, for example, in the Netlib repository ({\tt http://www.netlib.org/}), contain very large problems with thousands of variables and constraints. Trying to load them to Xcas without a safety limit could easily eat up huge amounts of available memory, probably freezing up the whole system. If a large LP problem needs to be solved, one may consider using GLPK standalone solver\footnote{See {\tt https://www.gnu.org/software/glpk/} for installing GLPK in Linux or {\tt http://winglpk.sourceforge.net/} for MS~Windows.}. 18155 18156\subsection{Solving transportation problems: {\tt tpsolve}}\index{tpsolve|textbf} 18157The objective of a transportation problem is to minimize the cost of distributing a product from $ m $ sources to $ n $ destinations. It is determined by three parameters : 18158\begin{itemize} 18159\item supply vector $ \mathbf{s}=(s_1,s_2,\dots,s_m) $, where $ s_k\in\mathbb{Z} $, $ s_k>0 $ is the maximum number of units that can be delivered from $ k $-th source for $ k=1,2,\dots,m $, 18160\item demand vector $ \mathbf{d}=(d_1,d_2,\dots,d_n) $, where $ d_k\in\mathbb{Z} $, $ d_k>0 $ is the minimum number of units required by $ k $-th destination for $ k=1,2,\dots,n $, 18161\item cost matrix $ \mathbf{C}=[c_{ij}]_{m\times n} $, where $ c_{ij}\in\mathbb{R} $, $ c_{ij}\geq 0 $ is the cost of transporting one unit of product from $ i $-th source to $ j $-th destination for $ i=1,2,\dots,m $ and $ j=1,2,\dots,n $. 18162\end{itemize} 18163The optimal solution is represented as matrix $ \mathbf{X}^*=[x^*_{ij}]_{m\times n} $, where $ x^*_{ij} $ is number of units that must be transported from $ i $-th source to $ j $-th destination for $ i=1,2,\dots,m $ and $ j=1,2,\dots,n $. 18164 18165Function {\tt tpsolve} accepts three arguments: supply vector, demand vector and cost matrix, respectively. It returns a sequence of two elements: the total (minimal) cost $ c=\sum_{i=1}^m\sum_{j=1}^nc_{ij}\,x^*_{ij} $ of transportation and the optimal solution $ \mathbf{X}^* $. 18166 18167\noindent Input : 18168\begin{center} 18169{\tt s:=[12,17,11];d:=[10,10,10,10];}\\ 18170{\tt C:=[[50,75,30,45],[65,80,40,60],[40,70,50,55]];}\\ 18171{\tt tpsolve(s,d,C)} 18172\end{center} 18173Output : 18174\begin{center} 18175{\tt 2020,[[0,0,2,10],[0,9,8,0],[10,1,0,0]]} 18176\end{center} 18177 18178If total supply and total demand are equal, i.e.~if $ \sum_{i=1}^ms_i=\sum_{j=1}^nd_j $ holds, transportation problem is \emph{closed} or \emph{balanced}. If total supply exceeds total demand or vice versa, the problem is \emph{unbalanced}. The excess supply/demand is covered by adding a dummy demand/supply point with zero cost of ``transportation'' from/to that point. Function {\tt tpsolve} handles such cases automatically. 18179 18180\noindent Input : 18181\begin{center} 18182{\tt s:=[7,10,8,8,9,6];d:=[9,6,12,8,10];}\\ 18183{\tt C:=[[36,40,32,43,29],[28,27,29,40,38],[34,35,41,29,31],}\\ 18184{\tt [41,42,35,27,36],[25,28,40,34,38],[31,30,43,38,40]];}\\ 18185{\tt tpsolve(s,d,C)} 18186\end{center} 18187Output : 18188\begin{center} 18189{\tt 1275,[[0,0,2,0,5],[0,0,10,0,0],[0,0,0,0,5],}\\ 18190{\tt [0,0,0,8,0],[9,0,0,0,0],[0,6,0,0,0]]} 18191\end{center} 18192 18193Sometimes it is desirable to forbid transportation on certain routes. That is usually achieved by setting very high cost to these routes, represented by symbol $ M $. If {\tt tpsolve} detects a symbol in the cost matrix, it interprets it as $ M $ and assigns 100 times larger cost than the largest numeric element of $ \mathbf{C} $ to the corresponding routes, which forces the algorithm to avoid them. 18194 18195\noindent Input : 18196\begin{center} 18197{\tt s:=[95,70,165,165];d:=[195,150,30,45,75];}\\ 18198{\tt C:=[[15,M,45,M,0],[12,40,M,M,0],}\\ 18199{\tt [0,15,25,25,0],[M,0,M,12,0]]}\\ 18200{\tt tpsolve(s,d,C)} 18201\end{center} 18202Output : 18203\begin{center} 18204{\tt 2820,[[20,0,0,0,75],[70,0,0,0,0],}\\ 18205{\tt [105,0,30,30,0],[0,150,0,15,0]]} 18206\end{center} 18207 18208\section{Nonlinear optimization} 18209 18210\subsection{Global extrema: {\tt minimize maximize}}\index{minimize|textbf}\index{maximize|textbf} 18211 18212The function {\tt minimize} takes four arguments : 18213\begin{itemize} 18214\item {\tt obj} : univariate or multivariate expression 18215\item {\tt constr} (optional) : list of equality and inequality constraints 18216\item {\tt vars} : list of variables 18217\item {\tt location} (optional) : option keyword which may be {\tt coordinates}, {\tt locus} or {\tt point} 18218\end{itemize} 18219The expression {\tt obj} is minimized on the domain specified by constraints and/or bounding variables, which can be done as specifying e.g.~{\tt x=a..b} in {\tt vars}. The domain must be closed and bounded and {\tt obj} must be continuous in every point of it. Else, the final result may be incorrect or meaningless. 18220 18221Constraints may be given as equalities or inequalities, but also as expressions which are assumed to be equal to zero. If there is only one constraint, the list delimiters may be dropped. The same applies to the specification of variables. 18222 18223{\tt minimize} returns the minimal value. If it could not be obtained, it returns {\tt undef}. If {\tt location} is specified, the list of points where the minimum is achieved is also returned as the second member in a list. Keywords {\tt locus}, {\tt coordinates} and {\tt point} all have the same effect. 18224 18225The function {\tt maximize} takes the same parameters as {\tt minimize}. The difference is that it computes global maximum of {\tt obj} on the specified domain. 18226 18227\subsubsection{Examples} 18228Input : 18229\begin{center} 18230{\tt minimize(sin(x),[x=0..4])} 18231\end{center} 18232Output : 18233\begin{center} 18234{\tt sin(4)} 18235\end{center} 18236Input : 18237\begin{center} 18238{\tt minimize(asin(x),x=-1..1)} 18239\end{center} 18240Output : 18241\begin{center} 18242{\tt -pi/2} 18243\end{center} 18244Input : 18245\begin{center} 18246{\tt minimize(x\verb|^|4-x\verb|^|2,x=-3..3,locus)} 18247\end{center} 18248Output : 18249\begin{center} 18250{\tt -1/4,[-sqrt(2)/2]} 18251\end{center} 18252Input : 18253\begin{center} 18254{\tt minimize(x-abs(x),x=-1..1)} 18255\end{center} 18256Output : 18257\begin{center} 18258{\tt -2} 18259\end{center} 18260Input : 18261\begin{center} 18262{\tt minimize(when(x==0,0,exp(-1/x\verb|^|2)),x=-1..1)} 18263\end{center} 18264Output : 18265\begin{center} 18266{\tt 0} 18267\end{center} 18268Input : 18269\begin{center} 18270{\tt minimize(sin(x)+cos(x),x=0..20,coordinates)} 18271\end{center} 18272Output : 18273\begin{center} 18274{\tt -sqrt(2),[5*pi/4,13*pi/4,21*pi/4]} 18275\end{center} 18276Input : 18277\begin{center} 18278{\tt minimize(x\verb|^|2-3x+y\verb|^|2+3y+3,[x=2..4,y=-4..-2],point)} 18279\end{center} 18280Output : 18281\begin{center} 18282{\tt -1,[[2,-2]]} 18283\end{center} 18284Input : 18285\begin{center} 18286{\tt obj:=sqrt(x\verb|^|2+y\verb|^|2)-z;}\\ 18287{\tt constr:=[x\verb|^|2+y\verb|^|2<=16,x+y+z=10];}\\ 18288{\tt minimize(obj,constr,[x,y,z])} 18289\end{center} 18290Output : 18291\begin{center} 18292{\tt -4*sqrt(2)-6} 18293\end{center} 18294Input : 18295\begin{center} 18296{\tt minimize(x\verb|^|2*(y+1)-2y,[y<=2,sqrt(1+x\verb|^|2)<=y],[x,y])} 18297\end{center} 18298Output : 18299\begin{center} 18300{\tt -4} 18301\end{center} 18302Input : 18303\begin{center} 18304{\tt maximize(cos(x),x=1..3)} 18305\end{center} 18306Output : 18307\begin{center} 18308{\tt cos(1)} 18309\end{center} 18310Input : 18311\begin{center} 18312{\tt obj:=piecewise(x<=-2,x+6,x<=1,x\verb|^|2,3/2-x/2);} 18313{\tt maximize(obj,x=-3..2)} 18314\end{center} 18315Output : 18316\begin{center} 18317{\tt 4} 18318\end{center} 18319Input : 18320\begin{center} 18321{\tt maximize(x*y*z,x\verb|^|2+2*y\verb|^|2+3*z\verb|^|2<=1,[x,y,z])} 18322\end{center} 18323Output : 18324\begin{center} 18325{\tt sqrt(2)/18} 18326\end{center} 18327Input : 18328\begin{center} 18329{\tt maximize(x*y,[x+y\verb|^|2<=2,x>=0,y>=0],[x,y],locus)} 18330\end{center} 18331Output : 18332\begin{center} 18333{\tt 4*sqrt(6)/9,[[4/3,sqrt(6)/3]]} 18334\end{center} 18335Input : 18336\begin{center} 18337{\tt maximize(y\verb|^|2-x\verb|^|2*y,y<=x,[x=0..2,y=0..2])} 18338\end{center} 18339Output : 18340\begin{center} 18341{\tt 4/27} 18342\end{center} 18343Input : 18344\begin{center} 18345{\tt assume(a>0);}\\ 18346{\tt maximize(x\verb|^|2*y\verb|^|2*z\verb|^|2,x\verb|^|2+y\verb|^|2+z\verb|^|2=a\verb|^|2,[x,y,z])} 18347\end{center} 18348Output : 18349\begin{center} 18350{\tt a\verb|^|6/27} 18351\end{center} 18352 18353\subsection{Local extrema: {\tt extrema}}\index{extrema|textbf} 18354 18355Local extrema of a univariate or multivariate differentiable expression under equality constraints can be obtained by using function {\tt extrema} which takes four arguments : 18356\begin{itemize} 18357\item {\tt expr} : differentiable expression 18358\item {\tt constr} (optional) : list of equality constraints 18359\item {\tt vars} : list of variables 18360\item {\tt order=<positive integer>} or {\tt lagrange} (optional) : upper bound for the order of derivatives examined in the process (defaults to 5) or the specifier for the method of Lagrange multipliers 18361\end{itemize} 18362Function returns a list containing two lists of points: local minima and local maxima of {\tt expr}, respectively. Saddle and unclassified points are reported in the message area. Also, information about possible (non)strict extrema is printed out. If {\tt lagrange} is passed as an optional last argument, the method of Lagrange multipliers is used. Else, the problem is reduced to an unconstrained one by applying implicit differentiation. 18363 18364A single constraint/variable can be specified without list delimiters. A constraint may be specified as an equality or expression which is assumed to be equal to zero. 18365 18366Number of constraints must be strictly less than number of variables. Additionally, denoting $ k $-th constraint by $ g_k(x_1,x_2,\dots,x_n)=0 $ for $ k=1,2,\dots,m $ and letting $ \mathbf{g}=(g_1,g_2,\dots,g_m) $, Jacobian matrix of $ \mathbf{g} $ has to be full rank (i.e.~equal to $ m $). 18367 18368Variables may be specified with bounds, e.g.~{\tt x=a..b}, which is interpreted as $ x\in(a,b) $. For semi-bounded variables one can use {\tt -infinity} for $ a $ or {\tt +infinity} for $ b $. Also, parameter {\tt vars} may be entered as e.g.~{\tt [x1=a1,x2=a2,...,xn=an]}, in which case the critical point close to $ \mathbf{a}=(a_1,a_2,\dots,a_n) $ is computed numerically, applying an iterative method with initial point $ \mathbf{a} $. 18369 18370If {\tt order=<n>} is specified as the fourth argument, derivatives up to order $ n $ are inspected to find critical points and classify them. For {\tt order=1} the function returns a single list containing all critical points found. The default is $ n=5 $. If some critical points are left unclassified one might consider repeating the process with larger value of $ n $, although the success is not guaranteed. 18371 18372 18373\subsubsection{Examples} 18374Input : 18375\begin{center} 18376{\tt extrema(-2*cos(x)-cos(x)\verb|^|2,x)} 18377\end{center} 18378Output : 18379\begin{center} 18380{\tt [0],[pi]} 18381\end{center} 18382Input : 18383\begin{center} 18384{\tt extrema(x/2-2*sin(x/2),x=-12..12)} 18385\end{center} 18386Output : 18387\begin{center} 18388{\tt [2*pi/3,-10*pi/3],[10*pi/3,-2*pi/3]} 18389\end{center} 18390Input : 18391\begin{center} 18392{\tt assume(a>=0);extrema(x\verb|^|2+a*x,x)} 18393\end{center} 18394Output : 18395\begin{center} 18396{\tt [-a/2],[]} 18397\end{center} 18398Input : 18399\begin{center} 18400{\tt extrema(exp(x\verb|^|2-2x)*ln(x)*ln(1-x),x=0.5)} 18401\end{center} 18402Output : 18403\begin{center} 18404{\tt [],[0.277769149124]} 18405\end{center} 18406Input : 18407\begin{center} 18408{\tt extrema(x\verb|^|3-2x*y+3y\verb|^|4,[x,y])} 18409\end{center} 18410Output : 18411\begin{center} 18412{\tt [[12\verb|^|(1/5)/3,(12\verb|^|(1/5))\verb|^|2/6]],[]} 18413\end{center} 18414Input : 18415\begin{center} 18416{\tt assume(a>0);extrema(x/a\verb|^|2+a*y\verb|^|2,x+y=a,[x,y])} 18417\end{center} 18418Output : 18419\begin{center} 18420{\tt [[(2*a\verb|^|4-1)/(2*a\verb|^|3),1/(2*a\verb|^|3)]],[]} 18421\end{center} 18422Input : 18423\begin{center} 18424{\tt extrema(x\verb|^|2+y\verb|^|2,x*y=1,[x=0..inf,y=0..inf])} 18425\end{center} 18426Output : 18427\begin{center} 18428{\tt [[1,1]],[]} 18429\end{center} 18430Input : 18431\begin{center} 18432{\tt extrema(x*y*z,x+y+z=1,[x,y,z],order=1)} 18433\end{center} 18434Output : 18435\begin{center} 18436{\tt [[1,0,0],[0,1,0],[0,0,1],[1/3,1/3,1/3]]} 18437\end{center} 18438 18439\subsection{Global extrema without using derivatives : {\tt nlpsolve}}\index{nlpsolve} 18440{\tt nlpsolve} computes the optimum of a (not necessarily differentiable) nonlinear (multivariate) objective function, subject to a set of nonlinear equality and/or inequality constraints, using the COBYLA algorithm. The command takes the following arguments: 18441\begin{itemize} 18442 \item {\tt obj} : objective expression 18443 \item {\tt constr} : list of equality and inequality constraints (optional) 18444 \item {\tt bd} : sequence of variable boundaries (optional) : {\tt x=a..b}, {\tt y=c..d}, \ldots 18445 \item {\tt opt} : sequence of options (optional), which may be one of: 18446 \begin{itemize} 18447 \item {\tt maximize=true} or {\tt false} (or just {\tt maximize}) 18448 \item {\tt nlp\_initialpoint=[x=x0,y=y0,...]} 18449 \item {\tt nlp\_iterationlimit=n} 18450 \item {\tt assume=nlp\_nonnegative} 18451 \item {\tt nlp\_precision=eps} 18452 \end{itemize} 18453\end{itemize} 18454 18455{\tt nlpsolve} returns a list containing the optimal value of the objective and a vector of optimal values of the decision variables. 18456 18457The objective is minimized by default, unless {\tt maximize} or {\tt maximize=true} is specified as an option. 18458 18459Initial point, if given, does not need to be feasible. Note, however, that the initial value of a variable must not be zero. If the initial point is not given or isn't feasible, a feasible starting guess is automatically generated. Note that choosing a good initial point is needed for obtaining a correct solution in some cases. 18460 18461Input syntax for {\tt nlpsolve} resembles that of Maple's {\tt NLPSolve} (entering the objective as a function (univariate case) is not supported, however). 18462 18463\subsubsection{Examples} 18464 18465Input : 18466\begin{center} 18467 \tt nlpsolve(ln(1+x1\^{}2)-x2,[(1+x1\^{}2)\^{}2+x2\^{}2=4]) 18468\end{center} 18469Output : 18470\begin{center} 18471 \tt [-1.73205080757,[x1=-4.77142305945e-08,x2=1.73205080757]] 18472\end{center} 18473Input : 18474\begin{center} 18475 \tt nlpsolve(-x1*x2*x3,[72-x1-2x2-2x3>=0], 18476 x1=0..20,x2=0..11,x3=0..42) 18477\end{center} 18478Output : 18479\begin{center} 18480 \tt [-3300.0,[x1=20.0,x2=11.0,x3=15.0]] 18481\end{center} 18482Input : 18483\begin{center} 18484 \tt nlpsolve(x\^{}3+2x*y-2y\^{}2,x=-10..10,y=-10..10, 18485 nlp\_initialpoint=[x=3,y=4],maximize) 18486\end{center} 18487Output : 18488\begin{center} 18489 \tt [1050.0,[x=10.0,y=4.99999985519]] 18490\end{center} 18491Input : 18492\begin{center} 18493 \tt nlpsolve(sin(x)/x,x=1..30) 18494\end{center} 18495Output : 18496\begin{center} 18497 \tt [-0.217233628211,[x=4.49340942383]] 18498\end{center} 18499Input : 18500\begin{center} 18501 \tt nlpsolve(2-1/120*x1*x2*x3*x4*x5, 18502 [x1<=1,x2<=2,x3<=3,x4<=4,x5<=5],assume=nlp\_nonnegative) 18503\end{center} 18504Output : 18505\begin{center} 18506 \tt [1.0,[x1=1.0,x2=2.0,x3=3.0,x4=4.0,x5=5.0]] 18507\end{center} 18508 18509\subsection{Minimax polynomial approximation: {\tt minimax}}\index{minimax|textbf} 18510 18511The function {\tt minimax} is called by entering : 18512\begin{center} 18513{\tt minimax(expr,var=a..b,n,[limit=m])} 18514\end{center} 18515where {\tt expr} is an univariate expression (e.g.~$ f(x) $) to approximate, {\tt var} is a variable (e.g.~$ x $), $ [a,b]\subset\mathbb{R} $ and $ n\in\mathbb{N} $. Expression {\tt expr} must be continuous on $ [a,b] $. The function returns minimax polynomial (e.g.~$ p(x) $) of degree $ n $ or lower that approximates {\tt expr} on $ [a,b] $. The approximation is found by applying Remez algorithm. 18516 18517If the fourth argument is specified, $ m $ is used to limit the number of iterations of the algorithm. It is unlimited by default. 18518 18519The largest absolute error of the approximation $ p(x) $, i.e.~$ \max_{a\leq x\leq b}|f(x)-p(x)| $, is printed in the message area. 18520 18521Since the coefficients of $ p $ are computed numerically, one should avoid setting $ n $ unnecessary high as it may result in a poor approximation due to the roundoff errors. 18522 18523\noindent Input : 18524\begin{center} 18525{\tt minimax(sin(x),x=0..2*pi,10)} 18526\end{center} 18527Output : 18528\begin{center} 18529{\tt 5.8514210172e-06+0.999777263385*x+0.00140015265723*x\verb|^|2}\\ 18530{\tt -0.170089663733*x\verb|^|3+0.0042684304696*x\verb|^|4+}\\ 18531{\tt 0.00525794766407*x\verb|^|5+0.00135760214958*x\verb|^|6}\\ 18532{\tt -0.000570502074548*x\verb|^|7+6.07297119422e-05*x\verb|^|8}\\ 18533{\tt -2.14787414001e-06*x\verb|^|9-2.97767481643e-15*x\verb|^|10} 18534\end{center} 18535The largest absolute error of this approximation is $ 5.85234008632\times 10^{-6} $. 18536 18537 18538\section{Different matrix norms} 18539 18540See section \ref{ssec:vectornorms} for different norms on vectors. 18541 18542\subsection{The Frobenius norm: 18543\texttt{frobenius\_norm}}\index{frobenius\_norm} 18544 18545The \texttt{frobenius\_norm} command takes a matrix \texttt{A} as an 18546argument.\\ 18547\texttt{frobenius\_norm} returns the Frobenius norm of the matrix; 18548namely $\sqrt{\sum_{i,j} (\texttt{A[}i,j\texttt{]})^2}$.\\ 18549Input: 18550\begin{center} 18551 \tt 18552 B := [[1,2,3],[3,-9,6],[4,5,6]] 18553\end{center} 18554then: 18555\begin{center} 18556 \tt 18557 frobenius\_norm(B) 18558\end{center} 18559Output: 18560\begin{center} 18561 \tt 18562 sqrt(217) 18563\end{center} 18564since $\sqrt{1^2 + 2^2 + 3^2 + 3^2 + (-9)^2 + 6^2 + 4^2 + 5^2 + 6^2} = 18565\sqrt{217}$. 18566 18567\subsection{$l^2$ matrix norm : {\tt norm l2norm}}\index{norm}\index{l2norm|textbf}\label{sec:l2normm} 18568\noindent{\tt norm} (or {\tt l2norm}) takes as argument a matrix $A=a_{j,k}$ 18569(see also \ref{sec:l2normv}).\\ 18570{\tt norm} (or {\tt l2norm}) returns 18571$\displaystyle \sqrt{\sum_{j,k} a_{j,k}^2}$.\\ 18572Input : 18573\begin{center}{\tt norm([[1,2],[3,-4]])}\end{center} 18574or : 18575\begin{center}{\tt l2norm([[1,2],[3,-4]])}\end{center} 18576Output : 18577\begin{center}{\tt sqrt(30)}\end{center} 18578 18579\subsection{$l^\infty$ matrix norm : {\tt maxnorm}}\index{maxnorm}\label{sec:maxnormm} 18580\noindent{\tt maxnorm} takes as argument a matrix $A=a_{j,k}$ (see also \ref{sec:maxnormv}).\\ 18581{\tt maxnorm} returns $ \max(|a_{j,k}|)$.\\ 18582Input : 18583\begin{center}{\tt maxnorm([[1,2],[3,-4]])}\end{center} 18584Output : 18585\begin{center}{\tt 4}\end{center} 18586 18587\subsection{Matrix row norm : {\tt rownorm rowNorm}}\index{rowNorm}\index{rownorm} 18588\noindent{\tt rownorm} (or {\tt rowNorm}) takes as argument a matrix $A=a_{j,k}$.\\ 18589{\tt rownorm} (or {\tt rowNorm}) returns $\max_k(\sum_j |a_{j,k}|)$.\\ 18590Input : 18591\begin{center}{\tt rownorm([[1,2],[3,-4]])}\end{center} 18592or : 18593\begin{center}{\tt rowNorm([[1,2],[3,-4]])}\end{center} 18594Output : 18595\begin{center}{\tt 7}\end{center} 18596Indeed : $\max(1+2,3+4)=7$ 18597 18598\subsection{Matrix column norm : {\tt colnorm colNorm}}\index{colNorm}\index{colnorm} 18599\noindent{\tt colnorm} (or {\tt colNorm}) takes as argument a matrix 18600$A=a_{j,k}$.\\ 18601{\tt colnorm} (or {\tt colNorm}) returns $\max_j(\sum_k(|a_{j,k}|))$.\\ 18602Input : 18603\begin{center}{\tt colnorm([[1,2],[3,-4]])}\end{center} 18604or : 18605\begin{center}{\tt colNorm([[1,2],[3,-4]])}\end{center} 18606Output : 18607\begin{center}{\tt 6}\end{center} 18608Indeed : $\max(1+3,2+4)=6$ 18609 18610\subsection{The operator norm of a matrix: \texttt{matrix\_norm, l1norm, 18611l2norm, norm, specnorm, 18612linfnorm}}\index{matrix\_norm}\index{l1norm}\index{l2norm} 18613\index{norm}\index{specnorm} 18614 18615The \texttt{matrix\_norm} command takes two arguments, 18616a matrix \texttt{A} and a second argument of 18617either \texttt{1}, \texttt{2} or \texttt{inf}.\\ 18618\texttt{matrix\_norm} returns the operator norm of the operator 18619associated to the matrix. (See the reminder below for a discussion of 18620operator norms.) The operator norm will be relative to the $\ell_1$, 18621$\ell_2$ or $\ell_\infty$ norm on $\R^n$, depending on the second 18622argument. 18623Note that\\ 18624\begin{itemize} 18625 \item 18626 \texttt{matrix\_norm(A,1)} is the same as 18627 \texttt{l1norm(A)} and \texttt{colnorm(A)}. 18628 \item 18629 \texttt{matrix\_norm(A,2)} is the same as \texttt{l2norm(A)} 18630 and \texttt{max(SVL(A))}. 18631 \item 18632 \texttt{matrix\_norm(A,inf)} is the same as \texttt{linfnorm(A)} and 18633 \texttt{rownorm(A)}. 18634\end{itemize} 18635Input: 18636\begin{center} 18637 \tt 18638 B := [[1,2,3],[3,-9,6],[4,5,6]] 18639\end{center} 18640then: 18641\begin{center} 18642 \tt 18643 matrix\_norm(B,1) 18644\end{center} 18645or: 18646\begin{center} 18647 \tt 18648 l1norm(B) 18649\end{center} 18650or: 18651\begin{center} 18652 \tt 18653 colNorm(B) 18654\end{center} 18655Output: 18656\begin{center} 18657 \tt 18658 16 18659\end{center} 18660since $\max(1+3+4, 2+9+5, 3+6+6)=16$. 18661 18662\noindent 18663Input: 18664\begin{center} 18665 \tt 18666 matrix\_norm(B,2) 18667\end{center} 18668or: 18669\begin{center} 18670 \tt 18671 l2norm(B) 18672\end{center} 18673or: 18674\begin{center} 18675 \tt 18676 max(SVL(B)) 18677\end{center} 18678Output: 18679\begin{center} 18680 \tt 1868111.2449175989 18682\end{center} 18683 18684\noindent 18685Input: 18686\begin{center} 18687 \tt 18688 matrix\_norm(B,inf) 18689\end{center} 18690or: 18691\begin{center} 18692 \tt 18693 linfnorm(B) 18694\end{center} 18695or: 18696\begin{center} 18697 \tt 18698 rowNorm(B) 18699\end{center} 18700Output: 18701\begin{center} 18702 \tt 18703 18 18704\end{center} 18705since $\max(1+2+3, 3+9+6, 4+5+6)=18$. 18706 18707\noindent 18708\texttt{Reminder:}\\ 18709In mathematics, particularly functional analysis, a linear function 18710between two normed spaces $f:E \to F$ is continuous exactly when there 18711is a number $K$ such that $\|f(x)\|_F \le K \|x\|$ for all $x$ in $E$. 18712For this reason, they are also called bounded linear functions. The 18713infimum of all such $K$ is defined to be the operator norm of $f$, and 18714it depends on the norms of $E$ and $F$. There are other 18715characterizations of the operator norm of $f$, such as the supremum of 18716$\|f(x)\|_F$ over all $x$ in $E$ with $\|x\|_E \le 1$. 18717 18718If $E$ and $F$ are finite dimensional, then any linear function 18719$f:E\to F$ will be bounded. 18720 18721Any $m\times n$ matrix \texttt{A} $= (a_{jk})$ corresponds to a linear 18722function $f:\R^n \to \R^m$ defined by $f(x) = Ax$. We will refer to 18723the operator norm of $f$ as the operator norm of $A$. 18724\begin{itemize} 18725 \item If $\R^n$ and $\R^m$ both have the $\ell_{1}$ norm, 18726 namely for $x = (x_1,x_2,\dots)$ the norm is $\|x\| = 18727 \sum_{j} |x_j|$, the operator norm of $A$ is 18728 \[ \max_{k} (\sum_j |a_{jk}|,\] 18729 which is given by \texttt{matrix\_norm(A,1)} and 18730 \texttt{colnorm(A)}. 18731 18732 \item If $\R^n$ and $\R^m$ both have the $\ell_{2}$ norm, 18733 namely for $x = (x_1,x_2,\dots)$ the norm is $\|x\| = 18734 \sqrt{\sum_{j} x_j^2}$ (the usual Euclidean norm), the operator norm 18735 of $A$ is the largest eigenvalue of $f^*\circ f$, where $f^*$ is the 18736 transpose of $f$, and so the largest singular value of $f$, which is 18737 given by \texttt{matrix\_norm(A,2)}, \texttt{l2norm(A)}, and 18738 \texttt{max(SVL(A))}. 18739 18740 \item If $\R^n$ and $\R^m$ both have the $\ell_{\infty}$ norm, 18741 namely for $x = (x_1,x_2,\dots)$ the norm is $|x| = 18742 \max_{j} |x_j|$, the operator norm of $A$ is 18743 \[ \max_{j} (\sum_k |a_{jk}|,\] 18744 which is given by \texttt{matrix\_norm(A,inf)} and 18745 \texttt{rownorm(A)}. 18746\end{itemize} 18747 18748\section{Matrix reduction} 18749\subsection{Eigenvalues : {\tt eigenvals}}\index{eigenvals} 18750\noindent{\tt eigenvals} takes as argument a square 18751matrix $A$ of size $n$.\\ 18752{\tt eigenvals} returns the sequence of the $n$ eigenvalues of $A$.\\ 18753{\bf Remark} : If $A$ is exact, {\tt Xcas} may not be able 18754to find the exact roots of the characteristic polynomial, 18755{\tt eigenvals} will return approximate eigenvalues of $A$ if the 18756coefficients are numeric or a subset of the eigenvalues if the 18757coefficients are symbolic.\\ 18758Input : 18759\begin{center}{\tt eigenvals([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 18760Output : 18761\begin{center}{\tt (2,2,2) }\end{center} 18762Input : 18763\begin{center}{\tt eigenvals([[4,1,0],[1,2,-1],[2,1,0]])}\end{center} 18764Output : 18765\begin{center}{\tt (0.324869129433,4.21431974338,1.46081112719)}\end{center} 18766 18767\subsection{Eigenvalues : {\tt egvl eigenvalues eigVl}}\index{egvl}\index{eigVl}\index{eigenvalues} 18768\noindent{\tt egvl} (or {\tt eigenvalues eigVl}) takes as argument a 18769square matrix $A$ of size $n$.\\ 18770{\tt egvl} (or {\tt eigenvalues eigVl}) returns the Jordan normal 18771form of $A$.\\ 18772{\bf Remark} : If $A$ is exact, {\tt Xcas} may not be able 18773to find the exact roots of the characteristic polynomial, 18774{\tt eigenvalues} will return an approximate diagonalization of $A$ if the 18775coefficients are numeric.\\ 18776Input : 18777\begin{center}{\tt egvl([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 18778Output : 18779\begin{center}{\tt [[2,1,0],[0,2,1],[0,0,2]] }\end{center} 18780Input : 18781\begin{center}{\tt egvl([[4,1,0],[1,2,-1],[2,1,0]])}\end{center} 18782Output : 18783\begin{center}{\tt [[0.324869129433,0,0],[0,4.21431974338,0],[0,0,1.46081112719]]}\end{center} 18784 18785\subsection{Eigenvectors : {\tt egv eigenvectors eigenvects 18786eigVc}}\index{egv}\index{eigenvectors}\index{eigenvects}\index{eigVc} 18787\noindent{\tt egv} (or {\tt eigenvectors eigenvects eigVc}) takes as argument 18788a square matrix $A$ of size $n$.\\ 18789If $A$ is a diagonalizable matrix, {\tt egv} (or 18790{\tt eigenvectors eigenvects eigVc}) returns a matrix whose columns are the 18791eigenvectors of the matrix $A$. Otherwise, it will fail (see also 18792{\tt jordan} for characteristic vectors).\\ 18793Input : 18794\begin{center}{\tt egv([[1,1,3],[1,3,1],[3,1,1]])}\end{center} 18795Output : 18796\begin{center}{\tt [[-1,1,1],[2,1,0],[-1,1,-1]] }\end{center} 18797Input : 18798\begin{center}{\tt egv([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 18799Output : 18800\begin{center}{\tt "Not diagonalizable at eigenvalue 2"}\end{center} 18801In complex mode, input : 18802\begin{center}{\tt egv([[2,0,0],[0,2,-1],[2,1,2]])}\end{center} 18803Output : 18804\begin{center}{\tt [0,1,0],[-1,-2,-1],[i,0,-i]]}\end{center} 18805 18806\subsection{Rational Jordan matrix : {\tt rat\_jordan}}\index{rat\_jordan} 18807\noindent {\tt rat\_jordan} takes as argument a square 18808matrix $A$ of size $n$ with exact coefficients.\\ 18809{\tt rat\_jordan} returns : 18810\begin{itemize} 18811\item in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode\\ 18812a sequence of two matrices : a matrix $P$ (the columns of $P$ are 18813the eigenvectors if $A$ is diagonalizable in the field of its coefficients) 18814and the rational Jordan matrix $J$ of $A$, that is the most reduced 18815matrix in the field of the coefficients of $A$ (or the complexified 18816field in complex mode), where 18817\[ J=P^{-1}AP \] 18818\item in {\tt Maple} mode\\ 18819the Jordan matrix $J$ of $A$. We can also have the matrix $P$ verifying 18820$J=P^{-1}AP$ in a variable 18821by passing this variable as second argument, for example 18822\begin{center} {\tt rat\_jordan([[1,0,0],[1,2,-1],[0,0,1]],'P')} 18823\end{center} 18824\end{itemize} 18825{\bf Remarks} 18826\begin{itemize} 18827\item the syntax {\tt Maple} is also valid in the other modes, for example, in 18828{\tt Xcas} mode input 18829\begin{center} {\tt rat\_jordan([[4,1,1],[1,4,1],[1,1,4]],'P')} 18830\end{center} 18831Output : 18832\begin{center} {\tt [[1,-1,1/2],[1,0,-1],[1,1,1/2]]} 18833\end{center} 18834then {\tt P} returns 18835\begin{center} {\tt [[6,0,0],[0,3,0],[0,0,3]]} 18836\end{center} 18837\item the coefficients of $P$ and $J$ belongs to the same field as the 18838coefficients of $A$.\\ 18839For example, in {\tt Xcas} mode, input : 18840\begin{center} {\tt rat\_jordan([[1,0,1],[0,2,-1],[1,-1,1]])} 18841\end{center} 18842Output : 18843\begin{center} {\tt [[1,1,2],[0,0,-1],[0,1,2]],[[0,0,-1],[1,0,-3],[0,1,4]]}\end{center} 18844Input (put {\tt -pcar(...)} because the argument of {\tt companion} is a unit 18845polynomial (see \ref{sec:compagne}) 18846\begin{center} {\tt companion(-pcar([[1,0,1],[0,2,-1],[1,-1,1]],x),x)}\end{center} 18847Output : 18848\begin{center} {\tt [[0,0,-1],[1,0,-3],[0,1,4]]}\end{center} 18849Input : 18850\begin{center} {\tt rat\_jordan([[1,0,0],[0,1,1],[1,1,-1]])}\end{center} 18851Output : 18852\begin{center} {\tt [[-1,0,0],[1,1,1],[0,0,1]],[[1,0,0],[0,0,2],[0,1,0]]}\end{center} 18853Input : 18854\begin{center} {\tt factor(pcar([[1,0,0],[0,1,1],[1,1,-1]],x))}\end{center} 18855Output : 18856\begin{center} {\tt -(x-1)*(x\verb|^|2-2)}\end{center} 18857Input : 18858\begin{center} {\tt companion((x\verb|^|2-2),x)}\end{center} 18859Output : 18860\begin{center} {\tt [[0,2],[1,0]]}\end{center} 18861 18862 18863 18864\item When $A$ is symmetric and has eigenvalues with an multiple order, 18865{\tt Xcas} returns orthogonal eigenvectors (not always of norm equal to 1) 18866i.e. {\tt tran(P)*P} is a diagonal matrix where the diagonal is the square norm 18867of the eigenvectors, for example : 18868\begin{center} {\tt rat\_jordan([[4,1,1],[1,4,1],[1,1,4]])} 18869\end{center} 18870returns : 18871\begin{center} {\tt [[1,-1,1/2],[1,0,-1],[1,1,1/2]],[[6,0,0],[0,3,0],[0,0,3]]} 18872\end{center} 18873\end{itemize} 18874Input in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode : 18875\begin{center}{\tt rat\_jordan([[1,0,0],[1,2,-1],[0,0,1]])}\end{center} 18876Output : 18877\begin{center}{\tt [[0,1,0],[1,0,1],[0,1,1]],[[2,0,0],[0,1,0],[0,0,1]]}\end{center} 18878Input in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode : 18879\begin{center}{\tt rat\_jordan([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 18880Output : 18881\begin{center}{\tt [[[1,2,1],[0,1,0],[1,2,0]],[[2,1,0],[0,2,1],[0,0,2]]]}\end{center} 18882In complex mode and in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode , input : 18883\begin{center}{\tt rat\_jordan([[2,0,0],[0,2,-1],[2,1,2]])}\end{center} 18884Output : 18885\begin{center}{\tt [[1,0,0],[-2,-1,-1],[0,-i,i]],[[2,0,0],[0,2-i,0],[0,0,2+i]]}\end{center} 18886Input in {\tt Maple} mode : 18887\begin{center}{\tt rat\_jordan([[1,0,0],[1,2,-1],[0,0,1]],'P')}\end{center} 18888Output : 18889\begin{center}{\tt [[2,0,0],[0,1,0],[0,0,1]]}\end{center} 18890then input : 18891\begin{center}{\tt P}\end{center} 18892Output : 18893\begin{center}{\tt [[0,1,0],[1,0,1],[0,1,1]]]}\end{center} 18894 18895\subsection{Jordan normal form : {\tt jordan}}\index{jordan} 18896\noindent {\tt jordan} takes as argument a square 18897matrix $A$ of size $n$.\\ 18898{\tt jordan} returns : 18899\begin{itemize} 18900\item in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode\\ 18901a sequence of two matrices : a matrix $P$ whose columns are 18902the eigenvectors and characteristic vectors 18903of the matrix $A$ and the Jordan matrix $J$ of $A$ verifying $J=P^{-1}AP$, 18904\item in {\tt Maple} mode\\ 18905the Jordan matrix $J$ of $A$. We can also have the matrix $P$ verifying 18906$J=P^{-1}AP$ in a variable 18907by passing this variable as second argument, for example 18908\begin{center} {\tt jordan([[1,0,0],[0,1,1],[1,1,-1]],'P')} 18909\end{center} 18910\end{itemize} 18911{\bf Remarks} 18912\begin{itemize} 18913\item the {\tt Maple} syntax is also valid in the other modes, for example, in 18914{\tt Xcas} mode input : 18915\begin{center} {\tt jordan([[4,1,1],[1,4,1],[1,1,4]],'P')} 18916\end{center} 18917Output : 18918\begin{center} {\tt [[1,-1,1/2],[1,0,-1],[1,1,1/2]]} 18919\end{center} 18920then {\tt P} returns 18921\begin{center} {\tt [[6,0,0],[0,3,0],[0,0,3]]} 18922\end{center} 18923\item When $A$ is symmetric and has eigenvalues with multiple orders, 18924{\tt Xcas} returns orthogonal eigenvectors (not always of norm equal to 1) 18925i.e. {\tt tran(P)*P} is a diagonal matrix where the diagonal is the square norm 18926of the eigenvectors, for example : 18927\begin{center} {\tt jordan([[4,1,1],[1,4,1],[1,1,4]])} 18928\end{center} 18929returns : 18930\begin{center} {\tt [[1,-1,1/2],[1,0,-1],[1,1,1/2]],[[6,0,0],[0,3,0],[0,0,3]]} 18931\end{center} 18932\end{itemize} 18933 Input in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode : 18934\begin{center}{\tt jordan([[1,0,0],[0,1,1],[1,1,-1]])}\end{center} 18935Output : 18936\begin{center}{\tt [[1,0,0],[0,1,1],[1,1,-1]],[[-1,0,0],[1,1,1],[0,-sqrt(2)-1,sqrt(2)-1]],[[1,0,0],[0,-(sqrt(2)),0],[0,0,sqrt(2)]]}\end{center} 18937Input in {\tt Maple} mode : 18938\begin{center}{\tt jordan([[1,0,0],[0,1,1],[1,1,-1]])}\end{center} 18939Output : 18940\begin{center}{\tt [[1,0,0],[0,-(sqrt(2)),0],[0,0,sqrt(2)]]}\end{center} 18941then input : 18942\begin{center}{\tt P}\end{center} 18943Output : 18944\begin{center}{\tt [[-1,0,0],[1,1,1],[0,-sqrt(2)-1,sqrt(2)-1]]}\end{center} 18945Input in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode : 18946\begin{center}{\tt jordan([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 18947Output : 18948\begin{center}{\tt [[[1,2,1],[0,1,0],[1,2,0]],[[2,1,0],[0,2,1],[0,0,2]]]}\end{center} 18949In complex mode and in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode , input : 18950\begin{center}{\tt jordan([[2,0,0],[0,2,-1],[2,1,2]])}\end{center} 18951Output : 18952\begin{center}{\tt [[1,0,0],[-2,-1,-1],[0,-i,i]],[[2,0,0],[0,2-i,0],[0,0,2+i]]}\end{center} 18953 18954\subsection{Powers of a square matrix: \texttt{matpow}}\index{matpow} 18955 18956The \texttt{matpow} command takes two arguments, a square matrix and 18957an integer.\\ 18958\texttt{matpow} returns the corresponding power of the matrix, 18959computed using the Jordan form.\\ 18960Input: 18961\begin{center} 18962 \tt 18963 matpow([[1,2],[2,1]],n) 18964\end{center} 18965Output: 18966\begin{center} 18967 \tt 18968 [[(3\^{}n+(-1)\^{}n)/2,(3\^{}n-(-1)\^{}n)/2],[(3\^{}n-(-1)\^{}n)/2,(3\^{}n+(-1)\^{}n)/2]] 18969 18970\end{center} 18971Notice that \texttt{jordan([[1,2],[2,1]])} returns 18972\texttt{[[1,-1],[1,1]],[[3,0],[0,-1]]}. 18973 18974\subsection{Characteristic polynomial : {\tt charpoly}}\index{pcar}\index{charpoly} 18975\noindent{\tt charpoly} (or {\tt pcar}) takes one or two argument(s), 18976 a square matrix $A$ of size $n$ and optionally 18977the name of a symbolic variable.\\ 18978{\tt charpoly} returns the characteristic polynomial 18979$P$ of $A$ written as the 18980list of its coefficients if no variable name was provided 18981or written as an expression with respect to 18982the variable name provided as second argument.\\ 18983The characteristic polynomial $P$ of $A$ is defined as 18984\[ P(x)=\det(x I-A) \] 18985Input : 18986\begin{center}{\tt charpoly([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 18987Output : 18988\begin{center}{\tt[1,-6,12,-8]}\end{center} 18989Hence, the characteristic polynomial of this matrix is 18990$x^3-6x^2+12x-8$ (input {\tt normal(poly2symb([1,-6,12,-8],x))} to get 18991its symbolic representation).\\ 18992Input : 18993\begin{center}{\tt purge(X):; 18994charpoly([[4,1,-2],[1,2,-1],[2,1,0]],X)}\end{center} 18995Output : 18996\begin{center}{\tt X\verb|^|3-6*X\verb|^|2+12*X-8}\end{center} 18997 18998\subsection{Characteristic polynomial using Hessenberg algorithm : 18999 {\tt pcar\_hessenberg}}\index{pcar\_hessenberg} 19000\noindent{\tt pcar\_hessenberg} takes as argument a square 19001matrix $A$ of size $n$ and optionally the name of a symbolic variable.\\ 19002{\tt pcar\_hessenberg} returns the characteristic polynomial $P$ of $A$ written 19003as the list of its coefficients if no variable was provided 19004or written in its symbolic form with respect to the variable name given 19005as second argument, where 19006\[ P(x)=\det(xI-A) \] 19007The characteristic polynomial is computed using the Hessenberg algorithm 19008(see e.g. Cohen) which is more efficient ($O(n^3)$ deterministic) if 19009the coefficients of $A$ are in a finite field or use a finite 19010representation like approximate numeric coefficients. Note however that 19011this algorithm behaves badly if the coefficients are e.g. in $\mathbb Q$.\\ 19012Input : 19013\begin{center}{\tt pcar\_hessenberg([[4,1,-2],[1,2,-1],[2,1,0]] \% 37)}\end{center} 19014Output : 19015\begin{center}{\tt[1 \% 37 ,-6\% 37,12 \% 37,-8 \% 37]}\end{center} 19016Input : 19017\begin{center}{\tt pcar\_hessenberg([[4,1,-2],[1,2,-1],[2,1,0]] \% 37,x)}\end{center} 19018Output : 19019\begin{center}{\tt x\verb|^|3-6 \%37 *x\verb|^|2+12 \% 37 *x-8 \% 37}\end{center} 19020Hence, the characteristic polynomial of [[4,1,-2],[1,2,-1],[2,1,0]] in 19021$\mathbb Z/37 \mathbb Z$ is 19022\[ x^3-6x^2+12x-8 \] 19023 19024\subsection{Minimal polynomial : {\tt pmin}}\index{pmin} 19025\noindent{\tt pmin} takes one (resp. two) argument(s): 19026a square matrix $A$ of size $n$ and optionally 19027the name of a symbolic variable.\\ 19028{\tt pmin} returns the minimal polynomial of $A$ written as a 19029list of its coefficients if no variable was provided, or 19030written in symbolic form with respect to the 19031variable name given as second argument. 19032The minimal polynomial of $A$ is the polynomial $P$ 19033having minimal degree such that $P(A)=0$.\\ 19034Input : 19035\begin{center}{\tt pmin([[1,0],[0,1]])}\end{center} 19036Output : 19037\begin{center}{\tt [1,-1]}\end{center} 19038Input : 19039\begin{center}{\tt pmin([[1,0],[0,1]],x)}\end{center} 19040Output : 19041\begin{center}{\tt x-1}\end{center} 19042Hence the minimal polynomial of [[1,0],[0,1]] is {\tt x-1}.\\ 19043Input : 19044\begin{center}{\tt pmin([[2,1,0],[0,2,0],[0,0,2]])}\end{center} 19045Output : 19046\begin{center}{\tt [1,-4,4]}\end{center} 19047 Input : 19048\begin{center}{\tt pmin([[2,1,0],[0,2,0],[0,0,2]],x)}\end{center} 19049Output : 19050\begin{center}{\tt x\verb|^|2-4*x+4}\end{center} 19051Hence, the minimal polynomial of [[2,1,0],[0,2,0],[0,0,2]] is $x^2-4x+4$. 19052 19053\subsection{Adjoint matrix : {\tt adjoint\_matrix}}\index{adjoint\_matrix} 19054\noindent{\tt adjoint\_matrix } takes as argument a square matrix 19055$A$ of size $n$.\\ 19056{\tt adjoint\_matrix } returns the list of the coefficients of $P$ 19057(the characteristic polynomial of $A$), and 19058the list of the matrix coefficients of $Q$ (the adjoint matrix 19059of $A$). 19060 19061The comatrix of a square matrix $A$ of size $n$ is the matrix $B$ 19062defined by $A\times B=\det(A)\times I$. The adjoint matrix of $A$ 19063is the comatrix of $xI-A$. It is a polynomial of degree $n-1$ in $x$ 19064having matrix coefficients. 19065The following relation holds: 19066\[ P(x)\times I=\det(xI-A)I=(xI-A)Q(x)\] 19067Since the polynomial $P(x)\times I-P(A)$ (with matrix coefficients) 19068is also divisible by $x\times I-A$ (by algebraic identities), 19069this proves that $P(A)=0$. 19070We also have $Q(x)\ =\ I\times x^{n-1}+...+B_0 $ 19071where $B_0=$ is the comatrix of $A$ (up to the sign if $n$ is odd).\\ 19072Input : 19073\begin{center}{\tt adjoint\_matrix([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 19074Output : 19075\begin{center} 19076{\tt [ 19077 {\bf [}1,-6,12,-8{\bf ]},\\ 19078{\bf [} [[1,0,0],[0,1,0],[0,0,1]], 19079 [[-2,1,-2], [1,-4,-1],[2,1,-6]], 19080 [[1,-2,3],[-2,4,2],[-3,-2,7]] {\bf ]} 19081] }\end{center} 19082Hence the characteristic polynomial is : 19083\[ P(x)=x^3-6*x^2+12*x-8 \] 19084The determinant of $A$ is equal to $-P(0)=8$. 19085The comatrix of $A$ is equal to : 19086\[ B=Q(0)=[[1,-2,3],[-2,4,2],[-3,-2,7]] \] 19087Hence the inverse of $A$ is equal to : 19088\[ 1/8*[[1,-2,3],[-2,4,2],[-3,-2,7]] \] 19089The adjoint matrix of $A$ is : 19090\[ [[x^2-2x+1,x-2,-2x+3],[x-2,x^2-4x+4,-x+2],[2x-3,x-2,x^2-6x+7]] \] 19091Input : 19092\begin{center}{\tt adjoint\_matrix([[4,1],[1,2]])}\end{center} 19093Output : 19094\begin{center}{\tt[[1,-6,7],[[[1,0],[0,1]],[[-2,1],[1,-4]]]]}\end{center} 19095Hence the characteristic polynomial $P$ is : 19096\[ P(x)=x^2-6*x+7 \] 19097The determinant of $A$ is equal to $+P(0)=7$. 19098The comatrix of $A$ is equal to 19099\[ Q(0)= -[[-2,1],[1,-4]] \] 19100Hence the inverse of $A$ is equal to : 19101\[ -1/7*[[-2,1],[1,-4]] \] 19102The adjoint matrix of $A$ is : 19103\[ -[[x-2,1],[1,x-4]] \] 19104 19105\subsection{Companion matrix of a polynomial : {\tt companion}}\index{companion|textbf}\label{sec:compagne} 19106\noindent{\tt companion} takes as argument an unitary polynomial $P$ and the 19107name of its variable.\\ 19108{\tt companion} returns the matrix whose characteristic polynomial is $P$.\\ 19109If $P(x)=x^n+a_{n-1}x^{n-1}+...+a_{-1}x+a_0$, 19110this matrix is equal to the unit matrix of size $n-1$ bordered with 19111$[0,0..,0,-a_0]$ as first row, and with 19112$[-a_0,-a_1,....,-a_{n-1}]$ as last column.\\ 19113Input : 19114\begin{center}{\tt companion(x\verb|^|2+5x-7,x)}\end{center} 19115Output : 19116\begin{center}{\tt [[0,7],[1,-5]]}\end{center} 19117Input : 19118\begin{center}{\tt companion(x\verb|^|4+3x\verb|^|3+2x\verb|^|2+4x-1,x)}\end{center} 19119Output : 19120\begin{center}{\tt [[0,0,0,1],[1,0,0,-4],[0,1,0,-2],[0,0,1,-3]]}\end{center} 19121 19122\subsection{Hessenberg matrix reduction : {\tt hessenberg}}\index{hessenberg} 19123\noindent{\tt hessenberg} takes as argument a matrix $A$.\\ 19124{\tt hessenberg} returns a matrix $B$ equivalent to $A$ where the 19125coefficients below the sub-principal diagonal are zero. 19126$B$ is a Hessenberg matrix.\\ 19127Input : 19128\begin{center}{\tt hessenberg([[3,2,2,2,2],[2,1,2,-1,-1],[2,2,1,-1,1], [2,-1,-1,3,1],[2,-1,1,1,2]])}\end{center} 19129Output : 19130\begin{center}{\tt [[3,8,5,10,2],[2,1,1/2,-5,-1],[0,2,1,8,2], [0,0,1/2,8,1],[0,0,0,-26,-3]]}\end{center} 19131Input 19132\begin{center} 19133{\tt A:=[[3,2,2,2,2],[2,1,2,-1,-1],[2,2,1,-1,1],}\\ 19134{\tt \ [2,-1,-1,3,1],[2,-1,1,1,2]] :;}\\ 19135{\tt B:= hessenberg(A):; pcar(A); pcar(B) 19136} 19137\end{center} 19138Output: {\tt [1,-7,-66,-24]}. 19139 19140\subsection{Hermite normal form : {\tt ihermite}}\index{ihermite} 19141\noindent {\tt ihermite} takes as argument a matrix {\tt A} with coefficients 19142in $\mathbb Z$.\\ 19143{\tt ihermite} returns two matrices {\tt U} and {\tt B} such that 19144{\tt B=U*A}, {\tt U} is invertible in $\mathbb Z$ (det$(U) = \pm 1$) 19145and {\tt B} is upper-triangular. Moreover, 19146the absolute value of the coefficients above the diagonal of {\tt B} are 19147smaller than the pivot of the column divided by 2. 19148 19149The answer is obtained by a Gauss-like reduction algorithm 19150using only operations of rows with integer coefficients 19151and invertible in $\mathbb Z$.\\ 19152Input : 19153\begin{center}{\tt A:=[[9,-36,30],[-36,192,-180],[30,-180,180]]; U,B:=ihermite(A)}\end{center} 19154Output : 19155\begin{center}{\tt [[9,-36,30],[-36,192,-180],[30,-180,180]], [[13,9,7],[6,4,3],[20,15,12]],[[3,0,30],[0,12,0],[0,0,60]]}\end{center} 19156 19157{\bf Application: Compute a $\mathbb Z$-basis of the kernel of a 19158matrix having integer coefficients}\\ 19159Let {\tt M} be a matrix with integer coefficients.\\ 19160Input : 19161\begin{center} 19162{\tt (U,A):=ihermite(transpose(M))}. 19163\end{center} 19164This returns $U$ and $A$ such that {\tt A=U*transpose(M)} hence \\ 19165{\tt transpose(A)=M*transpose(U)}.\\ 19166The columns of {\tt transpose(A)} which are identically 0 (at the right, 19167coming from the rows of $A$ which are identically 0 at the bottom) 19168correspond to columns of {\tt transpose(U)} which form a basis 19169of {\tt Ker(M)}. In other words, the rows of {\tt A} 19170which are identically 0 correspond to rows of {\tt U} 19171which form a basis of {\tt Ker(M)}.\\ 19172{\bf Example}\\ 19173Let {\tt M:=[[1,4,7],[2,5,8],[3,6,9]]}. Input 19174\begin{center}{\tt U,A:=ihermite(tran(M))}\end{center} 19175Output 19176\begin{center} 19177{\tt U:=[[-3,1,0],[4,-1,0],[-1,2,-1]] and A:=[[1,-1,-3],[0,3,6],[0,0,0]]} 19178\end{center} 19179Since {\tt A[2]=[0,0,0]}, a $\mathbb Z$-basis of {\tt Ker(M)} is 19180{\tt U[2]=[-1,2,-1]}.\\ 19181Verification {\tt M*U[2]=[0,0,0]}. 19182 19183\subsection{Smith normal form in $\Z$: {\tt ismith}}\index{ismith} 19184\noindent {\tt ismith} takes as argument a matrix with coefficients in 19185$\mathbb Z$.\\ 19186{\tt ismith} returns three matrices {\tt U,B} and {\tt V} such 19187that {\tt B=U*A*V}, {\tt U} and {\tt V} are invertible in $\mathbb Z$, 19188{\tt B} is diagonal, and {\tt B[i,i]} divides {\tt B[i+1,i+1]}. 19189The coefficients {\tt B[i,i]} are called 19190invariant factors, they are used to describe 19191the structure of finite abelian groups.\\ 19192Input : 19193\begin{center} 19194{\tt A:=[[9,-36,30],[-36,192,-180],[30,-180,180]]; 19195U,B,V:=ismith(A)} 19196\end{center} 19197Output : 19198\begin{center}{\tt 19199[[-3,0,1],[6,4,3],[20,15,12]], 19200[[3,0,0],[0,12,0],[0,0,60]], 19201[[1,24,-30],[0,1,0],[0,0,1]] } 19202\end{center} 19203The invariant factors are 3, 12 and 60. 19204 19205\subsection{Smith normal form: \texttt{smith}}\index{smith} 19206 19207The \texttt{smith} command takes one argument, a square matrix \texttt{A} with 19208elements in a field $K$.\\ 19209\texttt{smith} returns matrices \texttt{U}, \texttt{V}, and 19210\texttt{D}, where \texttt{U} and \texttt{V} are invertible, \texttt{D} 19211is diagonal, and \texttt{D = U*A*V}.\\ 19212Input: 19213\begin{center} 19214 \tt 19215 M:=([[5,-2,3,6],[1,-3,1,3],[7,-6,-4,7],[-2,-4,-3,0]]) \% 17\\ 19216 A := x*idn(4) -M 19217\end{center} 19218Output: 19219\begin{center} 19220 \tt 19221 [[x-5 \% 17,2 \% 17,-3 \% 17,-6 \% 17],[-1 \% 17,x+3 \% 17,-1 \% 17,-3 \% 17],[-7 \% 17,6 \% 17,x+4 \% 17,-7 \% 17],[2 \% 17,4 \% 17,3 \% 17,x]] 19222\end{center} 19223Input: 19224\begin{center} 19225 \tt 19226 U, D, V := smith(A) 19227\end{center} 19228then: 19229\begin{center} 19230 \tt 19231 U 19232\end{center} 19233Output: 19234\begin{center} 19235 \tt 19236 [[0 \% 17,-1 \% 17,0 \% 17,0 \% 17],[0 \% 17,0 \% 17,6 \% 17,4 \% 17],[(-2*x+5) \% 17,(-4*x-5) \% 17,(-3*x-6) \% 17,(x\^{}2-3*x+6) \% 17],[(2*x\^{}2+5*x+6) \% 17,(4*x\^{}2+8*x+2) \% 17,(3*x\^{}2+4*x+1) \% 17,(-x\^{}3-2*x\^{}2+2*x-6) \% 17]] 19237\end{center} 19238Input: 19239\begin{center} 19240 \tt 19241 V 19242\end{center} 19243Output: 19244\begin{center} 19245 \tt 19246 [[1 \% 17,(x+3) \% 17,(-6*x\^{}2-3*x-7) \% 17,(6*x\^{}5+2*x\^{}4-2*x\^{}3+x\^{}2-8*x+6) \% 17],[0 \% 17,1 \% 17,(-6*x-2) \% 17,(6*x\^{}4+x\^{}3-6*x\^{}2+5*x-6) \% 17],[0 \% 17,0 \% 17,1 \% 17,(-x\^{}3+3*x\^{}2+7) \% 17],[0 \% 17,0 \% 17,0 \% 17,1 \% 17]] 19247\end{center} 19248Input: 19249\begin{center} 19250 \tt 19251 D 19252\end{center} 19253Output: 19254\begin{center} 19255 \tt 19256 [[1 \% 17,0 \% 17,0 \% 17,0 \% 17],[0 \% 17,1 \% 17,0 \% 17,0 \% 17],[0 \% 17,0 \% 17,1 \% 17,0 \% 17],[0 \% 17,0 \% 17,0 \% 17,(-x\^{}4-2*x\^{}3+8*x\^{}2-3*x+2) \% 17]] 19257\end{center} 19258We can check this:\\ 19259Input: 19260\begin{center} 19261 \tt 19262 normal(U*A*V-D) 19263\end{center} 19264Output: 19265\begin{center} 19266 \tt 19267 [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] 19268\end{center} 19269 19270\noindent 19271Input: 19272\begin{center} 19273 \tt 19274 B:=[[x\^{}2+x-1,1,0,1],[-1,x,0,-1],[0,x\^{}2+1,x,0],[1,0,1,x\^{}2+x+1]] \% 3\\ 19275 L:=smith(B) 19276\end{center} 19277Output: 19278\begin{center} 19279 \tt 19280 [[0 \% 3,-1 \% 3,0 \% 3,0 \% 3],[1 \% 3,0 \% 3,0 \% 3,(-x\^{}2-x+1) \% 3],[0 \% 3,(x\^{}2+1) \% 3,(-x) \% 3,(x\^{}2+1) \% 3],[-1 \% 3,(-x\^{}4-x\^{}3+x+1) \% 3,(x\^{}3+x\^{}2-x+1) \% 3,(-x\^{}4-x\^{}3+x\^{}2-x) \% 3]],[[1 \% 3,0 \% 3,0 \% 3,0 \% 3],[0 \% 3,1 \% 3,0 \% 3,0 \% 3],[0 \% 3,0 \% 3,1 \% 3,0 \% 3],[0 \% 3,0 \% 3,0 \% 3,(-x\^{}6+x\^{}5+x+1) \% 3]],[[1 \% 3,x \% 3,(x\^{}3+x\^{}2-x) \% 3,(-x\^{}7+x\^{}6+x\^{}4+x\^{}3+x\^{}2+x-1) \% 3],[0 \% 3,1 \% 3,(x\^{}2+x-1) \% 3,(-x\^{}6+x\^{}5+x\^{}3+x\^{}2+x+1) \% 3],[0 \% 3,0 \% 3,1 \% 3,(-x\^{}4-x\^{}3-x\^{}2-x) \% 3],[0 \% 3,0 \% 3,0 \% 3,1 \% 3]] 19281\end{center} 19282 19283\section{Isometries} 19284\subsection{Recognize an isometry : {\tt isom}}\index{isom} 19285\noindent{\tt isom} takes as argument the matrix of a linear 19286function in dimension 2 or 3.\\ 19287{\tt isom} returns : 19288\begin{itemize} 19289\item 19290if the linear function is a direct isometry,\\ 19291the list of the characteristic elements of this isometry and {\tt +1}, 19292\item if the linear function is an indirect isometry,\\ 19293the list of the characteristic elements of this isometry and {\tt -1} 19294\item if the linear function is not an isometry,\\ 19295{\tt [0]}. 19296\end{itemize} 19297Input : 19298\begin{center}{\tt isom([[0,0,1],[0,1,0],[1,0,0]])}\end{center} 19299Output : 19300\begin{center}{\tt [[1,0,-1],-1]}\end{center} 19301which means that this isometry is a 3-d symmetry with respect to the plane 19302$x\ -\ z\ =\ 0$.\\ 19303Input : 19304\begin{center}{\tt isom(sqrt(2)/2*[[1,-1],[1,1]])}\end{center} 19305Output : 19306\begin{center}{\tt [pi/4,1]}\end{center} 19307Hence, this isometry is a 2-d rotation of angle 19308$\displaystyle \frac{\pi}{4}$.\\ 19309Input : 19310\begin{center}{\tt isom([[0,0,1],[0,1,0],[0,0,1]])}\end{center} 19311Output : 19312\begin{center}{\tt [0]}\end{center} 19313therefore this transformation is not an isometry. 19314 19315\subsection{Find the matrix of an isometry : {\tt mkisom}}\index{mkisom} 19316{\tt mkisom} takes as argument : 19317\begin{itemize} 19318\item In dimension 3, the list of characteristic elements 19319(axis direction, angle for a rotation or normal to the plane for 19320a symmetry) and {\tt +1} for a direct isometry or 19321{\tt -1} an indirect isometry. 19322\item In dimension 2, a characteristic element (an angle or a vector) and 19323{\tt +1} for a direct isometry (rotation) or {\tt -1} for an 19324indirect isometry (symmetry). 19325\end{itemize} 19326{\tt mkisom} returns the matrix of the corresponding isometry.\\ 19327Input : 19328\begin{center}{\tt mkisom([[-1,2,-1],pi],1)}\end{center} 19329Output the matrix of the rotation of axis $[-1,2,-1]$ and angle $\pi$: 19330\begin{center}{\tt [[-2/3,-2/3,1/3],[-2/3,1/3,-2/3],[1/3,-2/3,-2/3]]}\end{center} 19331Input : 19332\begin{center}{\tt mkisom([pi],-1)}\end{center} 19333Output the matrix of the symmetry with respect to $O$ : 19334\begin{center}{\tt [[-1,0,0],[0,-1,0],[0,0,-1]]}\end{center} 19335Input : 19336\begin{center}{\tt mkisom([1,1,1],-1)}\end{center} 19337Output the matrix of the symmetry with respect to the plane $x+y+z=0$ : 19338\begin{center}{\tt [[1/3,-2/3,-2/3],[-2/3,1/3,-2/3],[-2/3,-2/3,1/3]]}\end{center} 19339Input : 19340\begin{center}{\tt mkisom([[1,1,1],pi/3],-1)}\end{center} 19341Output the matrix of the product of a rotation of axis $[1,1,1]$ and angle 19342$\frac{\pi}{3}$ and of a symmetry with respect to the plane $x+y+z=0$: 19343\begin{center}{\tt [[0,-1,0],[0,0,-1],[-1,0,0]]}\end{center} 19344Input : 19345\begin{center}{\tt mkisom(pi/2,1)}\end{center} 19346Output the matrix of the plane rotation of angle $\frac{\pi}{2}$ : 19347\begin{center}{\tt [[0,-1],[1,0]]}\end{center} 19348Input : 19349\begin{center}{\tt mkisom([1,2],-1)}\end{center} 19350Output matrix of the plane symmetry with respect to the line 19351of equation $x+2y=0$: 19352\begin{center}{\tt [[3/5,-4/5],[-4/5,-3/5]]}\end{center} 19353 19354\section{Matrix factorizations}\label{sec:factormatrice} 19355Note that most matrix factorization algorithms are implemented numerically, 19356only a few of them will work symbolically. 19357 19358\subsection{Cholesky decomposition : {\tt cholesky}}\index{cholesky} 19359\noindent{\tt cholesky} takes as argument a square symmetric 19360positive definite matrix {\tt M} of size $n$.\\ 19361{\tt cholesky} returns a symbolic or numeric matrix {\tt P}. {\tt P} is a 19362lower triangular matrix such that : 19363\begin{center} 19364{\tt tran(P)*P=M} 19365\end{center} 19366Input : 19367\begin{center}{\tt cholesky([[1,1],[1,5]])}\end{center} 19368Output : 19369\begin{center}{\tt [[1,0],[1,2]]}\end{center} 19370Input : 19371\begin{center}{\tt cholesky([[3,1],[1,4]])}\end{center} 19372Output : 19373\begin{center}{\tt [[sqrt(3),0],[(sqrt(3))/3,(sqrt(33))/3]]}\end{center} 19374Input : 19375\begin{center}{\tt cholesky([[1,1],[1,4]])}\end{center} 19376Output : 19377\begin{center}{\tt [[1,0],[1,sqrt(3)]]}\end{center} 19378{\bf Warning} If the matrix argument $A$ is not a symmetric matrix, 19379{\tt cholesky} does not return an error, instead {\tt cholesky} will 19380use the symmetric matrix $B$ of the the quadratic form $q$ 19381corresponding to the (non symmetric) bilinear form of the matrix $A$.\\ 19382Input : 19383\begin{center}{\tt cholesky([[1,-1],[-1,4]])}\end{center} 19384or : 19385\begin{center}{\tt cholesky([[1,-3],[1,4]])}\end{center} 19386Output : 19387\begin{center}{\tt [[1,0],[-1,sqrt(3)]]}\end{center} 19388 19389\subsection{QR decomposition : {\tt qr}}\index{qr} 19390\noindent{\tt qr} takes as argument a numeric 19391square matrix $A$ of size $n$.\\ 19392{\tt qr} factorizes numerically 19393%(symbolically when it is possible) 19394this matrix as $Q*R$ where 19395$Q$ is an orthogonal matrix (${}^tQ*Q=I$) and $R$ is an upper triangular 19396matrix. 19397{\tt qr(A)} returns only {\tt R}, run {\tt Q=A*inv(R)} to get {\tt Q}.\\ 19398Input : 19399\begin{center}{\tt qr([[3,5],[4,5]])}\end{center} 19400Output is the matrix {\tt R} : 19401\begin{center}{\tt [[-5,-7],[0,-1]]}\end{center} 19402Input : 19403\begin{center}{\tt qr([[1,2],[3,4]])}\end{center} 19404Output is the matrix {\tt R} : 19405\begin{center}{\tt [[-3.16227766017,-4.42718872424],[0,-0.632455532034]] }\end{center} 19406 19407\subsection{QR decomposition (for TI compatibility) : {\tt QR}}\index{QR} 19408\noindent{\tt QR} takes as argument a numeric square matrix $A$ of size 19409$n$ and two variable names, {\tt var1} and {\tt var2}.\\ 19410{\tt QR} factorizes this matrix numerically as $Q*R$ where 19411$Q$ is an orthogonal matrix (${}^tQ*Q=I$) and $R$ is an upper triangular 19412matrix. {\tt QR(A,var1,var2)} returns {\tt R}, stores {\tt Q=A*inv(R)} in {\tt 19413 var1} and {\tt R} in {\tt var2}.\\ 19414Input : 19415\begin{center}{\tt QR([[3,5],[4,5]],Q,R)}\end{center} 19416Output the matrix {\tt R} : 19417\begin{center}{\tt [[-5,-7],[0,-1]]}\end{center} 19418Then input : 19419\begin{center}{\tt Q}\end{center} 19420Output the matrix {\tt Q} : 19421\begin{center}{\tt [[-0.6,-0.8],[-0.8,0.6]]}\end{center} 19422 19423\subsection{LQ decomposition (HP compatible): \texttt{LQ}}\index{LQ} 19424 19425The \texttt{LQ} command takes a matrix \texttt{A} as argument.\\ 19426\texttt{LQ} returns three matrices \texttt{L}, \texttt{Q} and 19427\texttt{P}. If \texttt{A} is an $m \times n$ matrix, then \texttt{L} 19428will be an $m\times n$ lower triangular matrix, \texttt{Q} will be an 19429$n\times n$ orthogonal matrix, and \texttt{P} will be an $n\times n$ 19430permutation matrix.\\ 19431Input: 19432\begin{center} 19433 \tt 19434 L, Q, P := LQ([[4,0,0],[8,-4,3]]) 19435\end{center} 19436Output: 19437\begin{center} 19438 \tt 19439 [[[4.0,0.0,0.0],[8.0,5.0,0.0]],[[1.0,0.0,0.0],[0.0,-0.8,0.6],[0.0,-0.6,-0.8]],[[1,0,0],[0,1,0],[0,0,1]]] 19440\end{center} 19441Here, \texttt{L*Q} is the same as \texttt{P*A}.\\ 19442Input: 19443\begin{center} 19444 \tt 19445 L,Q,P:=LQ([[24,18],[30,24]]) 19446\end{center} 19447or: 19448\begin{center} 19449 \tt 19450 [[[-30.0,0.0],[-38.4,-1.2]],[[-0.8,-0.6],[0.6,-0.8]],[[1,0],[0,1]]] 19451\end{center} 19452Again, \texttt{L*Q = P*A}. 19453 19454\subsection{LU decomposition : {\tt lu}}\index{lu} 19455\noindent{\tt lu} takes as argument a square matrix $A$ of size $n$ (numeric or 19456symbolic).\\ 19457{\tt lu(A)} returns a permutation $p$ of 0..$n-1$, 19458a lower triangular matrix $L$, with $1$s on the diagonal, 19459and an upper triangular matrix $U$, such that : 19460\begin{itemize} 19461\item $P*A=L*U$ where $P$ is the permutation matrix 19462associated to $p$ (that may be computed by {\tt P:=permu2mat(p)}), 19463\item the equation $A*x=B$ is equivalent to : 19464\[ L*U*x=P*B=p(B) \mbox{ where } p(B)=[b_{p(0)},b_{p(1)}..b_{p(n-1)}], 19465\quad B=[b_0,b_1..b_{n-1}] \] 19466\end{itemize} 19467The permutation matrix $P$ is defined from $p$ by : 19468\[ P[i, p(i)]=1, \quad P[i, j]=0 \mbox{ if } j \ \neq\ p(i) \] 19469In other words, it is the identity matrix where the rows are permuted 19470according to the permutation $p$. 19471The function {\tt permu2mat}\index{permu2mat} may be used to compute $P$ 19472({\tt permu2mat(p)} returns ${\tt P}$).\\ 19473Input : 19474\begin{center}{\tt (p,L,U):=lu([[3.,5.],[4.,5.]])}\end{center} 19475Output : 19476\begin{center}{\tt [1,0],[[1,0],[0.75,1]],[[4,5],[0,1.25]]}\end{center} 19477Here $n=2$, hence : 19478\[ P[0,p(0)]=P_2[0,1]=1, \quad P[1,p(1)]=P_2[1,0]=1, \quad 19479P=[[0,1],[1,0]] \] 19480Verification :\\ 19481Input : 19482\begin{center}{\tt permu2mat(p)*A; L*U}\end{center} 19483Output: 19484\begin{center}{\tt [[4.0,5.0],[3.0,5.0]],[[4.0,5.0],[3.0,5.0]]}\end{center} 19485Note that the permutation is different for exact input (the choice of 19486pivot is the simplest instead of the largest in absolute value).\\ 19487Input : 19488\begin{center}{\tt lu([[1,2],[3,4]])}\end{center} 19489Output : 19490\begin{center}{\tt [1,0],[[1,0],[3,1]],[[1,2],[0,-2]]}\end{center} 19491Input : 19492\begin{center}{\tt lu([[1.0,2],[3,4]])}\end{center} 19493Output : 19494\begin{center}{\tt [1,0],[[1,0],[0.333333333333,1]],[[3,4], [0,0.666666666667]]}\end{center} 19495 19496\subsection{LU decomposition (for TI compatibility) : {\tt LU}}\index{LU} 19497\noindent{\tt LU} takes as argument a numeric 19498square matrix $A$ of size $n$ and three variable names, {\tt var1}, 19499{\tt var2} and {\tt var3}.\\ 19500{\tt LU(A,var1,var2,var3)} returns $P$, a permutation matrix, and stores : 19501\begin{itemize} 19502\item a lower triangular matrix $L$, with $1$ on the diagonal, in 19503{\tt var1}, 19504\item an upper triangular matrix $U$ in {\tt var2}, 19505\item the permutation matrix $P$, result of the command {\tt LU}, in 19506{\tt var3}. 19507\end{itemize} 19508These matrices are such that 19509\begin{center} 19510the equation $A*x=B$ is equivalent to $L*U*x=P*B$. 19511\end{center} 19512Input : 19513\begin{center}{\tt LU([[3,5],[4,5]],L,U,P)}\end{center} 19514Output : 19515\begin{center}{\tt [[0,1],[1,0]]}\end{center} 19516Input : 19517\begin{center}{\tt L}\end{center} 19518Output : 19519\begin{center}{\tt [[1,0],[0.75,1]]}\end{center} 19520Input : 19521\begin{center}{\tt U}\end{center} 19522Output : 19523\begin{center}{\tt [[4,5],[0,1.25]]}\end{center} 19524Input : 19525\begin{center}{\tt P}\end{center} 19526Output : 19527\begin{center}{\tt [[0,1],[1,0]]}\end{center} 19528 19529\subsection{Singular values (HP compatible): \texttt{SVL, svl}}\index{SVL}\index{svl} 19530 19531The \texttt{SVL} (or \texttt{svl}) command takes a square matrix as 19532argument.\\ 19533\texttt{SVL} returns the singular values of the matrix. 19534 19535The singular values of a matrix $A$ are the positive square roots of 19536the eigenvalues of $A\cdot A^{T}$. So, if $A$ is symmetric, the 19537singular values are the absolute values of the eigenvalues of $A$. 19538 19539\noindent 19540Input: 19541\begin{center} 19542 \tt 19543 SVL([[1,2],[3,4]]) 19544\end{center} 19545or: 19546\begin{center} 19547 \tt 19548 svl([[1,2],[3,4]]) 19549\end{center} 19550Output: 19551\begin{center} 19552 \tt 19553 [0.365966190626,5.46498570422] 19554\end{center} 19555Input: 19556\begin{center} 19557 \tt 19558 evalf(sqrt(eigenvals([[1,2],[3,4]]*[[1,3],[2,4]]))) 19559\end{center} 19560Output: 19561\begin{center} 19562 \tt 19563 5.46498570422,0.365966190626 19564\end{center} 19565Input: 19566\begin{center} 19567 \tt 19568 SVL([[1,4],[4,1]]) 19569\end{center} 19570or: 19571\begin{center} 19572 \tt 19573 svl([[1,4],[4,1]]) 19574\end{center} 19575or: 19576\begin{center} 19577 \tt 19578 [5.0,3.0] 19579\end{center} 19580or: 19581\begin{center} 19582 \tt 19583 abs(eigenvals([[1,4],[4,1]])) 19584\end{center} 19585Output: 19586\begin{center} 19587 \tt 19588 5,3 19589\end{center} 19590 19591\subsection{Singular value decomposition : {\tt svd}} 19592\index{svd} 19593 19594\noindent{\tt svd} (singular value decomposition) takes as argument a numeric 19595square matrix of size $n$.\\ 19596{\tt svd(A)} returns an orthogonal matrix $U$, the diagonal $s$ of a diagonal 19597matrix $S$ and an orthogonal matrix $Q$ (${}^tQ*Q=I$) such that : 19598\[ A=U S{}^t Q \] 19599Input : 19600\begin{center}{\tt svd([[1,2],[3,4]])}\end{center} 19601Output : 19602\begin{center}{\tt [[-0.404553584834,-0.914514295677],[-0.914514295677, 0.404553584834]], [5.46498570422,0.365966190626], [[-0.576048436766,0.81741556047],[-0.81741556047, -0.576048436766]]}\end{center} 19603Input : 19604\begin{center}{\tt (U,s,Q):=svd([[3,5],[4,5]])}\end{center} 19605Output : 19606\begin{center}{\tt [[-0.672988041811,-0.739653361771],[-0.739653361771, 0.672988041811]],[8.6409011028,0.578643354497], [[-0.576048436766,0.81741556047],[-0.81741556047, -0.576048436766]]}\end{center} 19607Verification :\\ 19608Input : 19609\begin{center}{\tt U*diag(s)*tran(Q)}\end{center} 19610Output : 19611\begin{center}{\tt [[3.0,5.0],[4.0,5.0]]}\end{center} 19612 19613\subsection{Short basis of a lattice : {\tt lll}}\index{lll} 19614\noindent{\tt lll} takes as argument an invertible matrix $M$ with 19615integer coefficients.\\ 19616{\tt lll} returns $(S,A,L,O)$ such that: 19617\begin{itemize} 19618\item the rows of $S$ is a short basis of the $\mathbb Z$-module 19619generated by the rows of $M$, 19620\item $A$ is the change-of-basis matrix from the short basis to the basis 19621defined by the rows of $M$ ($A*M=S$), 19622\item $L$ is a lower triangular matrix, the modulus of its non diagonal 19623coefficients are less than 1/2, 19624\item $O$ is a matrix with orthogonal rows such that $L*O=S$. 19625\end{itemize} 19626% If in 2 dimension, $[a,b]$ are coordinates of a vector system in the basis 19627% defined by $M$ and if $[a1,b1]$ are its coordinates in the short basis 19628% defined by $S$ i.e. if $[a,b]*M=[a1,b1]*S$, then :\\ 19629% $[a,b]=[a1,b1]*A$\\ 19630% $[a1,b1]*S=[a1,b1]*A*M=[a,b]*M$ et\\ 19631% $[a,b]*M=[a,b]*A^{-1}*S=[a1,b1]*S$\\ 19632Input : 19633\begin{center}{\tt (S,A,L,O):=lll(M:=[[2,1],[1,2]])}\end{center} 19634Output : 19635\begin{center}{\tt [[-1,1],[2,1]], [[-1,1],[1,0]], [[1,0],[1/-2,1]], [[-1,1],[3/2,3/2]]}\end{center} 19636Hence :\\ 19637{\tt S=[[-1,1],[2,1]]}\\ 19638{\tt A=[[-1,1],[1,0]]}\\ 19639{\tt L=[[1,0],[1/-2,1]]}\\ 19640{\tt O=[[-1,1],[3/2,3/2]]}\\ 19641Hence the original basis is {\tt v1=[2,1], v2=[1,2]}\\ 19642and the short basis is {\tt w1=[-1,1], w2=[2,1]}.\\ 19643Since {\tt w1=-v1+v2} and {\tt w2=v1} then :\\ 19644{\tt A:=[[-1,1],[1,0]]}, {\tt A*M==S} and {\tt L*O==S}.\\ 19645Input : 19646\begin{center}{\tt (S,A,L,O):=lll([[3,2,1],[1,2,3],[2,3,1]])}\end{center} 19647Output : 19648\begin{center}{\tt S=[[-1,1,0],[-1,-1,2],[3,2,1]] }\end{center} 19649\begin{center}{\tt A= [[-1,0,1],[0,1,-1],[1,0,0]]}\end{center} 19650\begin{center}{\tt L= [[1,0,0],[0,1,0],[(-1)/2,(-1)/2,1]]}\end{center} 19651\begin{center}{\tt O= [[-1,1,0],[-1,-1,2],[2,2,2]]}\end{center} 19652Input :\\ 19653{\tt M:=[[3,2,1],[1,2,3],[2,3,1]]}\\ 19654Properties :\\ 19655{\tt A*M==S} and {\tt L*O==S} 19656 19657\section{Quadratic forms} 19658\subsection{Matrix of a quadratic form : {\tt q2a}}\index{q2a} 19659\noindent{\tt q2a} takes two arguments : the symbolic expression 19660of a quadratic form $q$ and a 19661vector of variable names.\\ 19662{\tt q2a} returns the matrix $A$ of $q$.\\ 19663Input : 19664\begin{center}{\tt q2a(2*x*y,[x,y])}\end{center} 19665Output : 19666\begin{center}{\tt [[0,1],[1,0]]}\end{center} 19667 19668\subsection{Transform a matrix into a quadratic form : {\tt a2q}}\index{a2q} 19669\noindent{\tt a2q} takes two arguments : the symmetric matrix $A$ 19670of a quadratic 19671form $q$ and a vector of variable names of the same size.\\ 19672{\tt a2q} returns the symbolic expression of the quadratic form $q$.\\ 19673Input : 19674\begin{center}{\tt a2q([[0,1],[1,0]],[x,y])}\end{center} 19675Output : 19676\begin{center}{\tt 2*x*y}\end{center} 19677Input : 19678\begin{center}{\tt a2q([[1,2],[2,4]],[x,y]) }\end{center} 19679Output : 19680\begin{center}{\tt x\verb|^|2+4*x*y+4*y\verb|^|2}\end{center} 19681 19682\subsection{Reduction of a quadratic form : {\tt gauss}}\index{gauss} 19683\noindent{\tt gauss} takes two arguments : a symbolic expression 19684representing a quadratic form $q$ and a 19685vector of variable names.\\ 19686{\tt gauss} returns $q$ written as sum or difference of squares 19687using Gauss algorithm.\\ 19688Input : 19689\begin{center}{\tt gauss(2*x*y,[x,y])}\end{center} 19690Output : 19691\begin{center}{\tt (y+x)\verb|^|2/2+(-(y-x)\verb|^|2)/2}\end{center} 19692 19693\subsection{The conjugate gradient algorithm: 19694\texttt{conjugate\_gradient}}\index{conjugate\_gradient} 19695 19696The \texttt{conjugate\_gradient} command takes two mandatory arguments 19697and two optional arguments. The mandatory arguments are an $n\times n$ positive 19698definite symmetric matrix $A$ and a vector $y$ of length $n$. The 19699optional arguments are a vector $x0$ of length $n$ and a positive 19700number $\epsilon$.\\ 19701\texttt{conjugate\_gradient} uses the conjugate gradient algorithm to 19702return the solution to $Ax = y$ to within $\epsilon$ or 19703\texttt{epsilon}. The vector $x0$ is an optional initial approximation.\\ 19704Input: 19705\begin{center} 19706 \tt 19707 conjugate\_gradient([[2,1],[1,5]],[1,0]) 19708\end{center} 19709Output: 19710\begin{center} 19711 \tt 19712 [5/9,-1/9] 19713\end{center} 19714Input: 19715\begin{center} 19716 \tt 19717 conjugate\_gradient([[2,1],[1,5]],[1,0],[0.55,-0.11],1e-2) 19718\end{center} 19719Output: 19720\begin{center} 19721 \tt 19722 [0.555,-0.11] 19723\end{center} 19724Input: 19725\begin{center} 19726 \tt 19727 conjugate\_gradient([[2,1],[1,5]],[1,0],[0.55,-0.11],1e-10) 19728\end{center} 19729Output: 19730\begin{center} 19731 \tt 19732 [0.555555555556,-0.111111111111] 19733\end{center} 19734 19735\subsection{Gram-Schmidt orthonormalization : {\tt gramschmidt}}\index{gramschmidt} 19736\noindent{\tt gramschmidt} takes one or two arguments : 19737\begin{itemize} 19738\item a matrix viewed as a list of row vectors, 19739the scalar product being the canonical 19740 scalar product, or 19741\item a list of elements 19742that is a basis of a vector subspace, and a function that defines a scalar 19743product on this vector space. 19744\end{itemize} 19745{\tt gramschmidt} returns an orthonormal basis for this scalar product.\\ 19746Input : 19747\begin{center}{\tt normal(gramschmidt([[1,1,1],[0,0,1],[0,1,0]]))}\end{center} 19748Or input : 19749\begin{center}{\tt normal(gramschmidt([[1,1,1],[0,0,1],[0,1,0]],dot))}\end{center} 19750Output : 19751\begin{center}{\tt 19752[[(sqrt(3))/3,(sqrt(3))/3,(sqrt(3))/3],[(-(sqrt(6)))/6,}\\ 19753 {\tt (-(sqrt(6)))/6,(sqrt(6))/3],[(-(sqrt(2)))/2,(sqrt(2))/2,0]]}\end{center} 19754 {\bf Example}\\ 19755We define a scalar product on the vector space of 19756polynomials by: 19757$$P\cdot Q=\int_{-1}^1P(x) Q(x)dx $$ 19758Input : 19759 \begin{center}{\tt gramschmidt([1,1+x],(p,q)->integrate(p*q,x,-1,1))}\end{center} 19760Or define the function {\tt p\_scal}, input :\\ 19761{\tt p\_scal(p,q):=integrate(p*q,x,-1,1)}\\ 19762then input : 19763\begin{center}{\tt gramschmidt([1,1+x],p\_scal)}\end{center} 19764Output : 19765\begin{center}{\tt [1/(sqrt(2)),(1+x-1)/sqrt(2/3)]}\end{center} 19766 19767\subsection{Graph of a conic : {\tt conic}}\index{conic} 19768\noindent{\tt conic} takes as argument the equation of a conic with 19769respect to $x,y$. You may also specify the names of the variables as 19770second and third arguments or as a vector as second argument.\\ 19771{\tt conic} draws this conic.\\ 19772Input: 19773\begin{center}{\tt 19774conic(2*x\^{}2+2*x*y+2*y\^{}2+6*x) 19775} 19776\end{center} 19777Output: 19778\begin{center} 19779Ellipsis of center (-2,1)\\ 19780\includegraphics[width=0.75\textwidth]{xcas-conic1.png} 19781\end{center} 19782% Output: 19783% \begin{center}{\tt the graph of the ellipsis of center -2+i and equation 2*x\verb|^|2+2*x*y+2*y\verb|^|2+6*x=0}\end{center} 19784{\bf Remark}:\\ 19785See also {\tt reduced\_conic} for the parametric equation of the conic. 19786 19787\subsection{Conic reduction : {\tt reduced\_conic}}\index{reduced\_conic} 19788\noindent{\tt reduced\_conic} takes two arguments : the equation of a conic 19789and a vector of variable names.\\ 19790{\tt reduced\_conic} returns a list whose elements are: 19791\begin{itemize} 19792\item the origin of the conic, 19793\item the matrix of a basis in which the conic is reduced, 19794\item 0 or 1 (0 if the conic is degenerate), 19795\item the reduced equation of the conic 19796\item a vector of its parametric equations. 19797\end{itemize} 19798Input: 19799\begin{center} 19800 {\tt 19801 reduced\_conic(2*x\^{}2+2*x*y+2*y\^{}2+5*x+3,[x,y]) 19802 } 19803\end{center} 19804Output: 19805\begin{center} 19806 {\tt 19807 [[-5/3,5/6],[[-1/(sqrt(2)),1/(sqrt(2))],[-1/(sqrt(2)), -1/(sqrt(2))]],1,3*x\verb|^|2+y\verb|^|2+-7/6,[[(-10+5*i)/6+ (1/(sqrt(2))+(i)/(sqrt(2)))*((sqrt(14)*cos(`~t`))/6+ ((i)*sqrt(42)*sin(` t`))/6),` t`,0,2*pi,(2*pi)/60]]] 19808 } 19809\end{center} 19810Which means that the conic is not degenerate, its reduced equation is 19811\[3x^2+y^2-7/6=0 \] 19812its origin is $-5/3+5*i/6$, its axes are 19813parallel to the vectors $(-1,1)$ and $(-1,-1)$. 19814Its parametric equation is 19815\[ \displaystyle \frac{-10+5*i}{6}+ 19816\frac{(1+i)}{\sqrt 2}*\frac{(\sqrt{14}*cos(t)+i*\sqrt{42}*sin(t))}{6} 19817\] 19818where the suggested parameter values for drawing are 19819$t$ from 0 to $2\pi$ with {\tt tstep}= $2\pi/60$. 19820 19821{\bf Remark} :\\ 19822Note that if the conic is degenerate and is made of 1 or 2 line(s), 19823the lines are not given by 19824their parametric equation but by the list of two points of the line.\\ 19825Input: 19826\begin{center}{\tt reduced\_conic(x\verb|^|2-y\verb|^|2+3*x+y+2)}\end{center} 19827Output: 19828\begin{center}{\tt [[(-3)/2,1/2],[[1,0],[0,1]],0,x\verb|^;|2-y\verb|^|2, [[(-1+2*i)/(1-i),(1+2*i)/(1-i)], [(-1+2*i)/(1-i),(-1)/(1-i)]]]}\end{center} 19829 19830\subsection{Graph of a quadric: {\tt quadric}}\index{quadric} 19831\noindent{\tt quadric} takes as arguments the expression of a 19832quadric with respect to $x,y,z$. You may also specify the variables 19833as a vector (second argument) or as second, third and fourth arguments.\\ 19834{\tt quadric} draws this quadric.\\ 19835Input: 19836\begin{center}{\tt 19837 quadric(7*x\^{}2+4*y\^{}2+4*z\^{}2+4*x*y- 4*x*z-2*y*z-4*x+5*y+4*z-18) 19838 } 19839\end{center} 19840Output: 19841\begin{center} 19842 Ellipsoid of center [0.407407407407,-0.962962962963,-0.537037037037]\\ 19843 \includegraphics[width=0.75\textwidth]{xcas-quadric1.png} 19844\end{center} 19845%\begin{center}{\tt the drawing of the ellipsoid of equation 7*x\verb|^|2+4*y\verb|^|2+4*z\verb|^|2+4*x*y-4*x*z-2*y*z-4*x+5*y+4*z-18=0}\end{center} 19846See also {\tt reduced\_quadric} for 19847the parametric equation of the quadric. 19848 19849\subsection{Quadric reduction : {\tt reduced\_quadric}}\index{reduced\_quadric} 19850\noindent{\tt reduced\_quadric} takes two arguments : the equation of a 19851quadric and a vector of variable names.\\ 19852{\tt reduced\_quadric} returns a list whose elements are: 19853\begin{itemize} 19854\item the origin, 19855\item the matrix of a basis where the quadric is reduced, 19856\item 0 or 1 (0 if the quadric is degenerate), 19857\item the reduced equation of the quadric 19858\item a vector with its parametric equations. 19859\end{itemize} 19860{\bf Warning !} 19861{\tt u,v} will be used as parameters of the parametric equations : 19862these variables should not be assigned ({\tt purge} them before 19863calling {\tt reduced\_quadric}).\\ 19864Input : 19865\begin{center}{\tt reduced\_quadric(7*x\verb|^|2+4*y\verb|^|2+4*z\verb|^|2+ 4*x*y-4*x*z-2*y*z-4*x+5*y+4*z-18)}\end{center} 19866Output is a list containing : 19867\begin{itemize} 19868\item The origin (center of symmetry) of the quadric 19869\begin{center}{\tt [11/27,(-26)/27,(-29)/54],}\end{center} 19870\item The matrix of the basis change: 19871\begin{center}{\tt [[(sqrt(6))/3,(sqrt(5))/5,(-(sqrt(30)))/15], 19872 [(sqrt(6))/6,0,(sqrt(30))/6], 19873 [(-(sqrt(6)))/6,(2*sqrt(5))/5,(sqrt(30))/30]],}\end{center} 19874\item 1 hence the quadric is not degenerated 19875\item the reduced equation of the quadric : 19876\begin{center}{\tt 19877 0,9*x\verb|^|2+3*y\verb|^|2+3*z\verb|^|2+(-602)/27,}\end{center} 19878\item 19879The parametric equations (in the original frame) are : 19880\begin{center}{\tt [[(sqrt(6)*sqrt(602/243)*sin(u)*cos(v))/3+ 19881 (sqrt(5)*sqrt(602/81)*sin(u)*sin(v))/5+ 19882 ((-(sqrt(30)))*sqrt(602/81)*cos(u))/15+11/27, 19883 (sqrt(6)*sqrt(602/243)*sin(u)*cos(v))/6+ 19884 (sqrt(30)*sqrt(602/81)*cos(u))/6+(-26)/27, 19885 ((-(sqrt(6)))*sqrt(602/243)*sin(u)*cos(v))/6+ 19886 (2*sqrt(5)*sqrt(602/81)*sin(u)*sin(v))/5+ 19887 (sqrt(30)*sqrt(602/81)*cos(u))/30+(-29)/54], 19888 u=(0 .. pi),v=(0.. (2*pi)),ustep=(pi/20), 19889 vstep=((2*pi)/20)]]}\end{center} 19890\end{itemize} 19891Hence the quadric is an ellipsoid and its reduced equation is : 19892\[ 9*x^2+3*y^2+3*z^2+(-602)/27 = 0\] 19893after the change of origin $[11/27,(-26)/27,(-29)/54]$, 19894the matrix of basis change {\tt P} is : 19895\[ \left[ 19896\begin{array}{ccc} 19897\displaystyle \frac{\sqrt 6}{3} & \displaystyle\frac{\sqrt 5}{5} & \displaystyle-\frac{\sqrt{30}}{15}\\ 19898\displaystyle \frac{\sqrt 6}{6} & 0 & \displaystyle \frac{\sqrt{30}}{6}\\ 19899\displaystyle -\frac{\sqrt 6}{6} & \displaystyle \frac{2\sqrt{5}}{5} & \displaystyle \frac{\sqrt{30}}{30}\\ 19900\end{array} 19901\right] \] 19902Its parametric equation is : 19903\[ \left\{ 19904\begin{array}{l} 19905x =\displaystyle \frac{\sqrt 6\sqrt{\frac{602}{243}}\sin(u)\cos(v)}{3}+\frac{\sqrt 5\sqrt{\frac{602}{81}}\sin(u)\sin(v)}{5}-\frac{\sqrt{30}\sqrt{\frac{602}{81}}\cos(u)}{15}+\frac{11}{27}\\ 19906y =\displaystyle \frac{\sqrt 6\sqrt{\frac{602}{243}}\sin(u)\cos(v)}{6}+\frac{\sqrt{30}\sqrt{\frac{602}{81}}\cos(u))}{6}-\frac{26}{27}\\ 19907z =\displaystyle \frac{-\sqrt 6\sqrt{\frac{602}{243}}*\sin(u)\cos(v)}{6}+\frac{2\sqrt 5\sqrt{\frac{602}{81}}\sin(u)\sin(v)}{5}+\frac{\sqrt{30}\sqrt{\frac{602}{81}}\cos(u)}{30}-\frac{29}{54} 19908\end{array} 19909\right. 19910\] 19911{\bf Remark} :\\ 19912Note that if the quadric is degenerate and made of 1 or 2 plane(s), 19913each plane is not given by 19914its parametric equation but by the list of a point of the plane 19915and of a normal vector to the plane.\\ 19916Input : 19917\begin{center}{\tt reduced\_quadric(x\verb|^|2-y\verb|^|2+3*x+y+2)}\end{center} 19918Output : 19919\begin{center}{\tt [[(-3)/2,1/2,0],[[1,0,0],[0,1,0],[0,0,-1]],0,x\verb|^|2-y\verb|^|2, [hyperplan([1,1,0],[(-3)/2,1/2,0]), hyperplan([1,-1,0],[(-3)/2,1/2,0])]]}\end{center} 19920 19921 \section{Multivariate calculus}\label{sec:plusvar} 19922\subsection{Gradient : {\tt derive deriver diff grad}}\index{derive}\index{diff}\index{grad}\index{deriver}\label{sec:derive}\index{solve}\index{resoudre} 19923\noindent{\tt derive} (or {\tt diff} or {\tt grad}) takes two arguments : an 19924expression $F$ of $n$ real variables and a vector of these variable names.\\ 19925{\tt derive} returns the gradient of $F$, 19926where the gradient is the vector of all partial derivatives, 19927for example in dimension $n=3$ 19928\[ \overrightarrow{\mbox{grad}}(F)= [\frac{\partial F}{\partial x},\frac{\partial F}{\partial y},\frac{\partial F}{\partial z}] \] 19929{\bf Example} \\ 19930Find the gradient of $F(x,y,z)=2x^2y-xz^3$.\\ 19931Input : 19932\begin{center}{\tt derive(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z])}\end{center} 19933or : 19934\begin{center}{\tt diff(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z])}\end{center} 19935or : 19936\begin{center}{\tt grad(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z])}\end{center} 19937Output : 19938\begin{center}{\tt [2*2*x*y-z\verb|^|3,2*x\verb|^|2,-(x*3*z\verb|^|2)]}\end{center} 19939Output after simplification with {\tt normal(ans())} : 19940\begin{center}{\tt [4*x*y-z\verb|^|3,2*x\verb|^|2,-(3*x*z\verb|^|2)]}\end{center} 19941To find the critical points of 19942$F(x,y,z)=2x^2y-xz^3$, input : 19943\begin{center}{\tt solve(derive(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z]),[x,y,z])}\end{center} 19944Output : 19945\begin{center}{\tt [[0,y,0]]}\end{center} 19946 19947\subsection{Laplacian : {\tt laplacian}}\index{laplacian} 19948\noindent{\tt laplacian} takes one or two arguments. 19949\begin{itemize} 19950\item It can take an integer or floating-point integer $n$.\\ 19951\texttt{laplacian(n)} returns the $n\times n$ discrete Laplacian 19952matrix; namely the $n \times n$ tridiagonal matrix with 2s on the 19953main diagonal, $-1$s just above and below the main diagonal.\\ 19954Input : 19955\begin{center}{\tt laplacian(3)}\end{center} 19956Output : 19957\begin{center}{\tt [[2,-1,0],[-1,2,-1],[0,-1,2]]}\end{center} 19958Input : 19959\begin{center}{\tt laplacian(2.0)}\end{center} 19960Output : 19961\begin{center}{\tt [[2.0,-1.0],[-1.0,2.0]]}\end{center} 19962 19963\item It can take an 19964expression $F$ of $n$ real variables and a vector of these variable names.\\ 19965{\tt laplacian} returns the Laplacian of$F$, that is the sum of all second 19966partial derivatives, for example in dimension $n=3$: 19967\[ \nabla^2(F)=\frac{\partial^2 F}{\partial x^2}+\frac{\partial^2 F}{\partial y^2}+\frac{\partial^2 F}{\partial z^2} \] 19968{\bf Example}\\ 19969Find the Laplacian of $F(x,y,z)=2x^2y-xz^3$.\\ 19970Input : 19971\begin{center}{\tt laplacian(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z])}\end{center} 19972Output : 19973\begin{center}{\tt 4*y+-6*x*z}\end{center} 19974\end{itemize} 19975 19976\subsection{Hessian matrix : {\tt hessian}}\index{hessian} 19977\noindent{\tt hessian} takes two arguments : an 19978expression $F$ of $n$ real variables and a vector of these variable names.\\ 19979{\tt hessian} returns the hessian matrix of $F$, that is the matrix of the 19980derivatives of order 2.\\ 19981{\bf Example}\\ 19982Find the hessian matrix of $F(x,y,z)=2x^2y-xz^3$.\\ 19983Input : 19984\begin{center}{\tt hessian(2*x\verb|^|2*y-x*z\verb|^|3 , [x,y,z])}\end{center} 19985Output : 19986\begin{center}{\tt[[4*y,4*x,-(3*z\verb|^|2)],[2*2*x,0,0],[-(3*z\verb|^|2),0,x*3*2*z]]}\end{center} 19987To have the hessian matrix at the critical points, first input : 19988\begin{center}{\tt solve(derive(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z]),[x,y,z])}\end{center} 19989Output is the critical points : 19990\begin{center}{\tt [[0,y,0]]}\end{center} 19991Then, to have the hessian matrix at this points, input : 19992\begin{center}{\tt subst([[4*y,4*x,-(3*z\verb|^|2)],[2*2*x,0,0], [-(3*z\verb|^|2),0,6*x*z]],[x,y,z],[0,y,0])}\end{center} 19993Output : 19994\begin{center}{\tt [[4*y,4*0,-(3*0\verb|^|2)],[4*0,0,0],[-(3*0\verb|^|2),0,6*0*0]]}\end{center} 19995and after simplification : 19996\begin{center}{\tt [[4*y,0,0],[0,0,0],[0,0,0]]}\end{center} 19997 19998\subsection{Divergence : {\tt divergence}}\index{divergence} 19999\noindent{\tt divergence} takes two arguments : a vector 20000field of dimension $n$ depending on $n$ real variables.\\ 20001{\tt divergence} returns the divergence of $F$ that is the sum 20002of the derivative of the $k$-th component with respect 20003to the $k$-th variable. For example in dimension $n=3$: 20004\begin{center} 20005 {\tt divergence([A,B,C],[x,y,z])}=$\displaystyle\frac{\partial A}{\partial x}+\frac{\partial B}{\partial y}+\frac{\partial C}{\partial z}$ 20006\end{center} 20007Input : 20008\begin{center}{\tt divergence([x*z,-y\verb|^|2,2*x\verb|^|y],[x,y,z])}\end{center} 20009Output : 20010\begin{center}{\tt z+-2*y}\end{center} 20011 20012\subsection{Rotational : {\tt curl}}\index{curl} 20013\noindent{\tt curl} takes two arguments : a 3-d vector field 20014depending on 3 variables.\\ 20015{\tt curl} returns the rotational of the vector, defined by: 20016\begin{center} 20017{\tt curl([A,B,C],[x,y,z])}=$\displaystyle [\frac{\partial C}{\partial y}-\frac{\partial B}{\partial z},\ \frac{\partial A}{\partial z}-\frac{\partial C}{\partial x},\ \frac{\partial B}{\partial x}-\frac{\partial A}{\partial y}]$ 20018\end{center} 20019Note that $n$ {\bf must be equal to 3}.\\ 20020Input : 20021\begin{center}{\tt curl([x*z,-y\verb|^|2,2*x\verb|^|y],[x,y,z])}\end{center} 20022Output : 20023\begin{center}{\tt [2*x\verb|^|y*log(x),x-2*y*x\verb|^|(y-1),0]}\end{center} 20024 20025\subsection{Potential : {\tt potential}}\index{potential} 20026\noindent{\tt potential} takes two arguments : a vector field 20027$\overrightarrow V$ in $R^n$ with respect to $n$ real variables 20028and the vector of these variable names.\\ 20029{\tt potential} returns, if it is possible, a function $U$ such that 20030$\overrightarrow{\mbox{grad}}(U)=\overrightarrow V$. When it is possible, we 20031say that $\overrightarrow V$ derives the potential $U$, and 20032$U$ is defined up to a constant.\\ 20033{\tt potential} is the reciprocal function of {\tt derive}.\\ 20034Input : 20035\begin{center}{\tt potential([2*x*y+3,x\verb|^|2-4*z,-4*y],[x,y,z])}\end{center} 20036Output : 20037\begin{center}{\tt 2*y*x\verb|^|2/ 200382+3*x+(x\verb|^|2-4*z-2*x\verb|^|2/2)*y}\end{center} 20039Note that in $\R^3$ 20040a vector $\overrightarrow V$ is a gradient if and only if its 20041rotational is zero i.e. if {\tt curl(V)=0}. 20042In time-independent electro-magnetism, 20043$\overrightarrow V$=$\overrightarrow E$ is the 20044electric field and $U$ is the electric potential. 20045 20046\subsection{Conservative flux field : {\tt vpotential}}\index{vpotential} 20047\noindent{\tt vpotential} takes two arguments : a vector field 20048$\overrightarrow V$ 20049in $R^n$ with respect to $n$ real variables 20050and the vector of these variable names.\\ 20051{\tt vpotential} returns, if it is possible, a vector $\overrightarrow U$ such 20052that $\overrightarrow{\mbox{curl}}(\overrightarrow U)=\overrightarrow V$. 20053When it is possible we say that $\overrightarrow V$ is a conservative flux 20054field or a solenoidal field. 20055The general solution is the sum of a particular solution and of the 20056gradient of an arbitrary function, {\tt Xcas} returns a particular 20057solution with zero as first component.\\ 20058{\tt vpotential} is the reciprocal function of {\tt curl}.\\ 20059Input : 20060\begin{center}{\tt vpotential([2*x*y+3,x\verb|^|2-4*z,-2*y*z],[x,y,z]) }\end{center} 20061Output :~ 20062\begin{center}{\tt [0,(-(2*y))*z*x,-x\verb|^|3/3-(-(4*z))*x+3*y]}\end{center} 20063In $\R^3$, a vector field $\overrightarrow V$ is a rotational 20064if and only if its 20065divergence is zero \\({\tt divergence(V,[x,y,z])=0}). 20066In time-independent electro-magnetism, 20067$\overrightarrow V$= $\overrightarrow B$ is the magnetic field and 20068$\overrightarrow U$= $\overrightarrow A$ is the potential vector. 20069 20070\section{Equations} 20071\subsection{Define an equation : {\tt equal}}\index{equal} 20072\noindent{\tt equal} takes as argument the two members of an equation.\\ 20073{\tt equal} returns this equation. It is the prefixed version of {\tt =}\\ 20074Input : 20075\begin{center}{\tt equal(2x-1,3)}\end{center} 20076Output : 20077\begin{center}{\tt (2*x-1)=3}\end{center} 20078We can also directly write {\tt (2*x-1)=3}. 20079 20080\subsection{Transform an equation into a difference : {\tt equal2diff}}\index{equal2diff} 20081\noindent{\tt equal2diff} takes as argument an equation.\\ 20082{\tt equal2diff} returns the difference of the two members of this equation.\\ 20083Input : 20084\begin{center}{\tt equal2diff(2x-1=3)}\end{center} 20085Output : 20086\begin{center}{\tt 2*x-1-3}\end{center} 20087 20088\subsection{Transform an equation into a list : {\tt equal2list}}\index{equal2list} 20089\noindent{\tt equal2list} takes as argument an equation.\\ 20090{\tt equal2list} returns the list of the two members of this equation.\\ 20091Input : 20092\begin{center}{\tt equal2list(2x-1=3)}\end{center} 20093Output : 20094\begin{center}{\tt [2*x-1,3]}\end{center} 20095 20096\subsection{The left member of an equation : {\tt left gauche lhs}}\index{left|textbf}\index{lhs|textbf}\index{gauche|textbf} 20097\noindent{\tt left} or {\tt lhs} takes as argument an equation or an 20098interval.\\ 20099{\tt left} or {\tt lhs} returns the left member of this equation or the left 20100bound of this interval.\\ 20101Input : 20102\begin{center}{\tt left(2x-1=3)}\end{center} 20103Or input: 20104\begin{center}{\tt lhs(2x-1=3)}\end{center} 20105Output : 20106\begin{center}{\tt 2*x-1}\end{center} 20107Input : 20108\begin{center}{\tt left(1..3)}\end{center} 20109Or input: 20110\begin{center}{\tt lhs(1..3)}\end{center} 20111Output : 20112\begin{center}{\tt 1}\end{center} 20113 20114\subsection{The right member of an equation : {\tt right droit rhs}}\index{right|textbf}\index{rhs|textbf} \index{droit|textbf} 20115\noindent{\tt right} or {\tt rhs} takes as argument an equation or an 20116interval.\\ 20117{\tt right} or {\tt rhs} returns the right member of this equation or the 20118right bound of this interval.\\ 20119Input : 20120\begin{center}{\tt right(2x-1=3)}\end{center} 20121or : 20122\begin{center}{\tt rhs(2x-1=3)}\end{center} 20123Output : 20124\begin{center}{\tt 3}\end{center} 20125Input : 20126\begin{center}{\tt right(1..3)}\end{center} 20127or : 20128\begin{center}{\tt rhs(1..3)}\end{center} 20129Output : 20130\begin{center}{\tt 3}\end{center} 20131 20132\subsection{Solving equation(s): {\tt solve}}\index{solve|textbf} 20133\noindent{\tt solve} solves an equation or a system of polynomial 20134equations. It takes 2 arguments: 20135\begin{itemize} 20136\item Solving an equation\\ 20137{\tt solve} takes as arguments an equation between two expressions or an 20138expression ({\tt =0} is omitted), and a variable name (by default {\tt x}).\\ 20139{\tt solve} solves this equation. 20140\item Solving a system of polynomial equations\\ 20141{\tt solve} takes as arguments two vectors : 20142a vector of polynomial equations and a 20143vector of variable names. \\ 20144{\tt solve} solves this polynomial equation system. 20145\end{itemize} 20146{\bf Remarks}: 20147\begin{itemize} 20148\item In real mode, {\tt solve} returns only real solutions. To have 20149the complex solutions, switch to complex mode, e.g. by checking 20150{\tt Complex} in the cas configuration, or use the {\tt cSolve} 20151command. 20152\item 20153For trigonometric equations, {\tt solve} returns by default the principal 20154solutions. To have all the solutions check {\tt All\_trig\_sol} in the cas 20155configuration. 20156\end{itemize} 20157{\bf Examples} : 20158\begin{itemize} 20159\item Solve $x^4-1=3$\\ 20160 Input : 20161\begin{center}{\tt solve(x\verb|^|4-1=3)}\end{center} 20162Output in real mode : 20163\begin{center}{\tt [sqrt(2),-(sqrt(2))]}\end{center} 20164Output in complex mode : 20165\begin{center}{\tt [sqrt(2),-(sqrt(2)),(i)*sqrt(2),-((i)*sqrt(2))]}\end{center} 20166\item Solve $\exp(x)=2$ \\ 20167Input : 20168\begin{center}{\tt solve(exp(x)=2)}\end{center} 20169Output in real mode : 20170\begin{center}{\tt [log(2)]}\end{center} 20171\item Find $x,y$ such that $x+y=1,x-y=0$\\ 20172 Input : 20173\begin{center}{\tt solve([x+y=1,x-y],[x,y])}\end{center} 20174Output : 20175\begin{center}{\tt [[1/2,1/2]] }\end{center} 20176\item Find $x,y$ such that $x^2+y=2,x+y^2=2$\\ 20177Input : 20178\begin{center}{\tt solve([x\verb|^|2+y=2,x+y\verb|^|2=2],[x,y])}\end{center} 20179Output : 20180\begin{center}{\tt [[-2,-2],[1,1],[(-sqrt(5)+1)/2,(1+sqrt(5))/2],}\end{center} 20181\begin{center}{\tt [(sqrt(5)+1)/2,(1-sqrt(5))/2]] }\end{center} 20182\item Find $x,y,z$ such that $x^2-y^2=0,x^2-z^2=0$\\ 20183Input : 20184\begin{center}{\tt solve([x\verb|^|2-y\verb|^|2=0,x\verb|^|2-z\verb|^|2=0],[x,y,z])}\end{center} 20185Output : 20186\begin{center}{\tt [[x,x,x],[x,-x,-x],[x,-x,x],[x,x,-x]]}\end{center} 20187\item Solve $\cos(2*x)=1/2$\\ 20188Input : 20189\begin{center}{\tt solve(cos(2*x)=1/2)}\end{center} 20190Output : 20191\begin{center}{\tt [pi/6,(-pi)/6]}\end{center} 20192Output with {\tt All\_trig\_sol} checked : 20193\begin{center}{\tt [(6*pi*n\_0+pi)/6,(6*pi*n\_0-pi)/6]}\end{center} 20194\item 20195Find the intersection of a straight line 20196(given by a list of equations) and a plane.\\ For example, 20197let $D$ be the straight line of cartesian equations 20198$[y-z=0,z-x=0]$ and let $P$ the plane of equation $x-1+y+z=0$. 20199Find the intersection of $D$ and $P$.\\ 20200Input : 20201\begin{center}{\tt solve([[y-z=0,z-x=0],x-1+y+z=0],[x,y,z])}\end{center} 20202Output : 20203\begin{center}{\tt [[1/3,1/3,1/3]]}\end{center} 20204\end{itemize} 20205 20206\subsection{Equation solving in $\mathbb C$ : {\tt cSolve}}\index{cSolve} 20207\noindent{\tt cSolve} takes two arguments and solves an equation or a system 20208of polynomial equations. 20209\begin{itemize} 20210\item solving an equation\\ 20211{\tt cSolve} takes as arguments an equation between two expressions or an 20212expression ({\tt =0} is omitted), and a variable name (by default {\tt x}).\\ 20213{\tt cSolve} solves this equation in $\mathbb C$ even if you are in 20214real mode. 20215\item solving a system of polynomial equations\\ 20216{\tt cSolve} takes as arguments two vectors : a vector of polynomial equations 20217and a vector of variable names. \\ 20218{\tt cSolve} solves this equation system in $\mathbb C$ even if you are in 20219real mode. 20220\end{itemize} 20221Input : 20222\begin{center}{\tt cSolve(x\verb|^|4-1=3)}\end{center} 20223Output : 20224\begin{center}{\tt [sqrt(2),-(sqrt(2)),(i)*sqrt(2),-((i)*sqrt(2))]}\end{center} 20225Input : 20226\begin{center}{\tt cSolve([-x\verb|^|2+y=2,x\verb|^|2+y],[x,y])}\end{center} 20227Output : 20228\begin{center}{\tt [[i,1],[-i,1]]}\end{center} 20229 20230 20231\section{Linear systems} 20232In this paragraph, we call the "augmented matrix" of the system 20233$A \cdot X=B$ (or matrix "representing" the system $A \cdot X=B$), 20234the matrix obtained by gluing the column vector $B$ or $-B$ 20235to the right of the matrix $A$, as with {\tt border(A,tran(B))}. 20236 20237\subsection{Matrix of a system : {\tt syst2mat}}\index{syst2mat} 20238\noindent{\tt syst2mat} takes two vectors as arguments. The components of the 20239first vector are the equations of a linear system and the components of the 20240second vector are the variable names.\\ 20241{\tt syst2mat} returns the augmented matrix of the system $AX=B$, 20242obtained by gluing the column vector $-B$ 20243to the right of the matrix $A$.\\ 20244Input : 20245\begin{center}{\tt syst2mat([x+y,x-y-2],[x,y])}\end{center} 20246Output : 20247\begin{center}{\tt [[1,1,0],[1,-1,-2]]}\end{center} 20248Input : 20249\begin{center}{\tt syst2mat([x+y=0,x-y=2],[x,y])}\end{center} 20250Output : 20251\begin{center}{\tt [[1,1,0],[1,-1,-2]]}\end{center} 20252{\bf Warning !!!}\\ 20253The variables (here {\tt x} and {\tt y}) must be purged. 20254 20255\subsection{Gauss reduction of a matrix : {\tt ref}}\index{ref}\label{ref} \label{sec:ref} 20256\noindent{\tt ref} is used to solve a linear system of equations written in 20257matrix form: 20258 \begin{center}{\tt A*X=B}\end{center} 20259The argument of {\tt ref} is the augmented matrix of the system 20260(the matrix obtained by augmenting the matrix {\tt A} to the right with 20261the column vector {\tt B}).\\ 20262The result is a matrix {\tt [A1,B1]} where {\tt A1} has zeros 20263under its principal diagonal, and the solutions of: 20264\begin{center}{\tt A1*X=B1}\end{center} 20265are the same as the solutions of: 20266\begin{center}{\tt A*X=B}\end{center} 20267 20268For example, solve the system : 20269\[ \left \{ 20270\begin{array}{lcr} 3x + y & = &-2 \\3x +2y & =& 2 \end{array}\right. 20271\] 20272Input : 20273\begin{center}{\tt ref([[3,1,-2],[3,2,2]])}\end{center} 20274Output : 20275\begin{center}{\tt [[1,1/3,-2/3],[0,1,4]]}\end{center} 20276Hence the solution is $y=4$ (last row) and $x=-2$ (substitute $y$ 20277in the first row). 20278 20279\subsection{Gauss-Jordan reduction: {\tt rref gaussjord}} 20280\index{rref|textbf} 20281\index{gaussjord|textbf} 20282\label{sec:rref} 20283 20284\noindent{\tt rref} solves a linear system of equations written in 20285matrix form (see also \ref{sec:rrefm}) : 20286 \begin{center}{\tt A*X=B}\end{center} 20287{\tt rref} takes one or two arguments. 20288\begin{itemize} 20289\item 20290If {\tt rref} has only one argument, this argument is the augmented matrix 20291of the system (the matrix obtained by augmenting matrix {\tt A} to the 20292right with the column vector {\tt B}).\\ 20293The result is a matrix {\tt [A1,B1]} : {\tt A1} has zeros both above and under 20294its principal diagonal and has 1 on its principal diagonal, and the solutions 20295of: 20296\begin{center}{\tt A1*X=B1}\end{center} 20297are the same as : 20298\begin{center}{\tt A*X=B}\end{center} 20299For example, to solve the system: 20300\[ 20301\left \{ 20302\begin{array}{lcr} 3x + y & = &-2 \\3x +2y & =& 2 \end{array}\right. 20303\] 20304Input : 20305\begin{center}{\tt rref([[3,1,-2],[3,2,2]])}\end{center} 20306Output : 20307\begin{center}{\tt [[1,0,-2],[0,1,4]]}\end{center} 20308Hence $x=-2$ and $y=4$ is the solution of this system. 20309 20310\noindent{\tt rref} can also solve several linear systems 20311of equations having the same first member. 20312We write the second members as a column matrix.\\ 20313Input : 20314\begin{center}{\tt rref([[3,1,-2,1],[3,2,2,2]])}\end{center} 20315Output : 20316\begin{center}{\tt [[1,0,-2,0],[0,1,4,1]]}\end{center} 20317Which means that ($x=-2$ and $y=4$) is the solution of the system 20318$$\left \{ 20319\begin{array}{lcr} 3x + y & = &-2 \\3x +2y & =& 2 \end{array}\right.$$ 20320and ($x=0$ and $y=1$) is the solution of the system 20321$$\left \{ 20322\begin{array}{lcr} 3x + y & = &1 \\3x +2y & =& 2 \end{array}\right.$$ 20323\item 20324If {\tt rref} has two parameters, the second parameter must be an integer 20325$k$, and the Gauss-Jordan reduction will be performed on (at most) 20326the first $k$ columns.\\ 20327Input : 20328\begin{center}{\tt rref([[3,1,-2,1],[3,2,2,2]],1)}\end{center} 20329Output : 20330\begin{center}{\tt [[3,1,-2,1],[0,1,4,1]]}\end{center} 20331\end{itemize} 20332 20333\subsection{Solving A*X=B : {\tt simult}}\index{simult} 20334\noindent{\tt simult} is used to solve a linear system of equations (resp. 20335several linear systems of equations with the same matrix {\tt A}) written 20336in matrix form (see also \ref{sec:rrefm}) : 20337\begin{center}{\tt A*X=b (resp. A*X=B)}\end{center} 20338{\tt simult} takes as arguments the matrix {\tt A} of the system and the 20339column vector (i.e. a one column matrix) {\tt b} of the second 20340member of the system (resp. 20341the matrix {\tt B} whose columns are the 20342vectors {\tt b} of the second members of the different systems).\\ 20343The result is a column vector solution of the system (resp. a matrix 20344whose columns are the solutions of the different systems).\\ 20345For example, to solve the system : 20346$$\left \{ 20347\begin{array}{lcr} 3x + y & = &-2 \\3x +2y & =& 2 \end{array}\right.$$ 20348Input : 20349\begin{center}{\tt simult([[3,1],[3,2]],[[-2],[2]])}\end{center} 20350Output : 20351\begin{center}{\tt [[-2],[4]]}\end{center} 20352Hence $x=-2$ and $y=4$ is the solution.\\ 20353Input : 20354\begin{center}{\tt simult([[3,1],[3,2]],[[-2,1],[2,2]])}\end{center} 20355Output : 20356\begin{center}{\tt [[-2,0],[4,1]]}\end{center} 20357Hence $x=-2$ and $y=4$ is the solution of 20358$$\left \{ 20359\begin{array}{lcr} 3x + y & = &-2 \\3x +2y & =& 2 \end{array}\right.$$ 20360whereas $x=0$ and $y=1$ is the solution of 20361$$\left \{ 20362\begin{array}{lcr} 3x + y & = &1 \\3x +2y & =& 2 \end{array}\right.$$ 20363 20364\subsection{Step by step Gauss-Jordan reduction of a matrix : {\tt pivot}}\ 20365index{pivot} 20366\label{sec:pivot} 20367 20368\noindent{\tt pivot} takes three arguments : a matrix with $n$ rows and $p$ 20369columns and two integers $l$ and $c$ such that $0\leq l<n$, $0\leq c<p$ 20370and $A_{l,c}\neq 0$.\\ 20371{\tt pivot(A,l,c)} performs one step of the Gauss-Jordan method 20372using {\tt A[l,c]} as pivot and returns an equivalent matrix 20373with zeros in the column {\tt c} of {\tt A} (except at row $l$).\\ 20374Input : 20375\begin{center}{\tt pivot([[1,2],[3,4],[5,6]],1,1)}\end{center} 20376Output : 20377\begin{center}{\tt [[-2,0],[3,4],[2,0]]}\end{center} 20378Input : 20379\begin{center}{\tt pivot([[1,2],[3,4],[5,6]],0,1)}\end{center} 20380Output : 20381\begin{center}{\tt [[1,2],[2,0],[4,0]]}\end{center} 20382 20383\subsection{Linear system solving: {\tt linsolve}}\index{linsolve} 20384\noindent{\tt linsolve} is used to solve a system of linear equations.\\ 20385{\tt linsolve} takes its arguments in two different ways.\\ 20386\begin{itemize} 20387 \item It can take two arguments, the first is a list of equations or 20388expressions (in that case the convention is that the equation 20389is $expression = 0$), and a list of variable names.\\ 20390{\tt linsolve} returns the solution of the system in a list.\\ 20391Input: 20392\begin{center}{\tt linsolve([2*x+y+z=1,x+y+2*z=1,x+2*y+z=4],[x,y,z])}\end{center} 20393Output : 20394\begin{center}{\tt [1/-2,5/2,1/-2]}\end{center} 20395Which means that 20396\[ x=-\frac{1}{2}, y=\frac{5}{2}, z=-\frac{1}{2} \] 20397is the solution of the system : 20398$$\left\{ 20399\begin{array}{rl} 204002x+y+z &=1\\ 20401x+y+2z &=1\\ 20402x+2y+z &=4 20403\end{array} 20404\right.$$ 20405\item It can take two arguments, the matrix of coefficients of a 20406system and values of the right hand side in the form of a list.\\ 20407Input: 20408\begin{center} 20409 \tt 20410 linsolve ([[2,1,1], [1,1,2], [1,2,1]], [1,1,4]) 20411\end{center} 20412Output: 20413\begin{center} 20414 \tt 20415 [-1/2,5/2,-1/2] 20416\end{center} 20417\item 20418It can take four arguments; the matrices \texttt{P}, \texttt{L}, 20419\texttt{U} from the \texttt{lu} decomposition and the values of the 20420right hand side in the form of a list. This is useful when you have 20421several systems of equations which only differ on their right hand side.\\ 20422Input: 20423\begin{center} 20424 \tt 20425 p,l,u:=lu([[2,1,1],[1,1,2],[1,2,1]])\\ 20426 linsolve(p,l,u,[1,1,4]) 20427\end{center} 20428Output: 20429\begin{center} 20430 \tt 20431 [-1/2,5/2,-1/2] 20432\end{center} 20433\end{itemize} 20434 20435If the \texttt{Step by step} option is checked in the general 20436configuration, a window will also pop up showing: 20437\begin{verbatim} 20438 Matrix [[1,1,2, -1], [0,1, -1, -3], [0, -1, -3,1]] 20439 Row operation L2 <- (1) * L1- (1) * L2 20440 Matrix [[1,0,3,2], [0,1, -1, -3], [0, -1, -3,1]] 20441 Row operation L2 <- (1) * L3 - (- 1) * L2 20442 Matrix [[1,0,3,2], [0,1, -1, -3], [0,0, -4, -2]] 20443 Reducing column 3 using pivot -4 at row 3 20444 Matrix [[1,0,3,2], [0,1, -1, -3], [0,0, -4, -2]] 20445 Row operation L3 <- (-4) * L1- (3) * L3 20446 Matrix [[-4,0,0, -2], [0,1, -1, -3], [0,0, -4, -2]] 20447 Row operation L3 <- (-4) * L2 - (- 1) * L3 20448 End reduction [[-4,0,0, -2], [0, -4,0,10], [0,0, -4, -2]] 20449\end{verbatim} 20450 20451The \texttt{linsolve} command also solves systems with coefficients in 20452$\Z/n\Z$.\\ 20453Input: 20454\begin{center} 20455 \tt 20456 linsolve([2*x+y+z-1,x+y+2*z-1,x+2*y+z-4]\%3,[x,y,z]) 20457\end{center} 20458Output: 20459\begin{center} 20460 \tt 20461 [1 \% 3,1 \% 3,1 \% 3] 20462\end{center} 20463 20464\subsection{Solving a linear system using the Jacobi iteration method: 20465\texttt{jacobi\_linsolve}}\index{jacobi\_linsolve} 20466 20467The \texttt{jacobi\_linsolve} command takes two mandatory arguments 20468and two optional arguments. The mandatory arguments are the matrix 20469of coefficients of a system and the right hand side of the system as a 20470list. The optional arguments are an integer indicating the maximum 20471number of iterations (by default \texttt{maxiter}) and a positive 20472number indicating the error tolerance (by default \texttt{epsilon}).\\ 20473\texttt{jacobi\_linsolve} uses the Jacobi iteration method to solve and 20474return the solution of the system.\\ 20475Input: 20476\begin{center} 20477 \tt 20478 A:=[[100,2],[2,100]];\\ 20479 jacobi\_linsolve(A,[0,1],1e-12); 20480\end{center} 20481Output: 20482\begin{center} 20483 \tt 20484 [-0.000200080032,0.0100040016006] 20485\end{center} 20486Input: 20487\begin{center} 20488 \tt 20489 evalf(linsolve(A,[0,1])) 20490\end{center} 20491Output: 20492\begin{center} 20493 \tt 20494 [-0.000200080032013,0.0100040016006] 20495\end{center} 20496 20497\subsection{Solving a linear system using the Gauss-Seidel iteration method: 20498\texttt{gauss\_seidel\_linsolve}}\index{gauss\_seidel\_linsolve} 20499 20500The \texttt{gauss\_seidel\_linsolve} command takes two mandatory arguments 20501and two optional arguments. The mandatory arguments are the matrix 20502of coefficients of a system and the right hand side of the system as a 20503list. The optional arguments are a positive 20504number indicating the error tolerance (by default \texttt{epsilon}) and 20505an integer indicating the maximum number of iterations (by default \texttt{maxiter}).\\ 20506\texttt{jacobi\_linsolve} uses the Gauss-Seidel iteration method to solve and 20507return the solution of the system.\\ 20508Input: 20509\begin{center} 20510 \tt 20511 A:=[[100,2],[2,100]];\\ 20512 gauss\_seidel\_linsolve(A,[0,1],1e-12); 20513\end{center} 20514Output: 20515\begin{center} 20516 \tt 20517[-0.000200080032013,0.0100040016006] 20518\end{center} 20519 20520Additionally, \texttt{gauss\_seidel\_linsolve} can take an optional 20521\emph{first} argument (by default 1) of $\omega$ used for a general 20522form of the Gauss-Seidel method (the successive overrelaxation 20523method).\\ 20524Input: 20525\begin{center} 20526 \tt 20527 gauss\_seidel\_linsolve (1.5, A, [0,1], 1e-12); 20528\end{center} 20529Output: 20530\begin{center} 20531 \tt 20532 [-0.000200080032218,0.0100040016006] 20533\end{center} 20534 20535\subsection{The least squares solution of a linear system: 20536\texttt{LSQ, lsq}} 20537\index{LSQ} 20538\index{lsq} 20539 20540The \texttt{lsq} (or \texttt{LSQ}) command takes two arguments; a 20541matrix \texttt{A} and a vector or matrix \texttt{B}.\\ 20542\texttt{lsq} returns the least squares solution to the equation 20543\texttt{A*X = B}. 20544 20545\noindent 20546Input: 20547\begin{center} 20548 \tt 20549 LSQ([[1,2],[3,4]], [5,11]) 20550\end{center} 20551Output: 20552\begin{center} 20553 \tt 20554 [[1],[2]] 20555\end{center} 20556Input: 20557\begin{center} 20558 \tt 20559 LSQ([[1,2], [3,4]], [[5,7], [11,9]]) 20560\end{center} 20561Output: 20562\begin{center} 20563 \tt 20564[[1,-5],[2,6]] 20565\end{center} 20566 20567Note that\\ 20568Input: 20569\begin{center} 20570 \tt 20571 linsolve([[1,2],[3,4],[3,6]]*[x, y] - [5,11,13],[x, y]) 20572\end{center} 20573Output: 20574\begin{center} 20575 \tt 20576 [] 20577\end{center} 20578since the linear system has no solution. We can still find the least 20579squares solution\\ 20580Input: 20581\begin{center} 20582 \tt 20583 LSQ([[1,2],[3,4],[3,6]],[5,11,13]) 20584\end{center} 20585Output: 20586\begin{center} 20587 \tt 20588 [[11/5],[11/10]] 20589\end{center} 20590The least squares solution\\ 20591Input: 20592\begin{center} 20593 \tt 20594 LSQ ([[3,4]], [12]) 20595\end{center} 20596Output: 20597\begin{center} 20598 \tt 20599 [[36/25],[48/25]] 20600\end{center} 20601represents the point on the line $3x + 4y = 12$ closest to the origin; \\ 20602Input: 20603\begin{center} 20604 \tt 20605 coordinates(projection(line(3*x+4*y=12),point(0))) 20606\end{center} 20607Output: 20608\begin{center} 20609 \tt 20610 [36/25,48/25] 20611\end{center} 20612 20613 20614\subsection{Finding linear recurrences : {\tt reverse\_rsolve}}\index{reverse\_rsolve} 20615\noindent{\tt reverse\_rsolve} takes as argument a vector 20616$v=[v_0...v_{2n-1}]$ made of the first $2n$ terms of a sequence $(v_n)$ 20617which is supposed to verify a linear recurrence relation of 20618degree smaller than $n$ 20619\[ x_n*v_{n+k}+...+x_0*v_k=0 \] 20620where the $x_j$ are $n+1$ unknowns.\\ 20621{\tt reverse\_rsolve} returns the list $x=[x_n,...,x_0]$ 20622of the $x_j$ coefficients (if $x_n\neq 0$ it is reduced to 1). 20623 20624In other words {\tt reverse\_rsolve} solves the linear system of 20625 $n$ equations : 20626\begin{eqnarray*} 20627x_n*v_{n}+...+x_0*v_0 &=&0 \\ 20628...\\ 20629x_n*v_{n+k}+...+x_0*v_k &=&0 \\ 20630...\\ 20631x_n*v_{2*n-1}+...+x_0*v_{n-1}&=&0 20632\end{eqnarray*} 20633The matrix $A$ of the system has $n$ rows and $n+1$ columns : 20634\[ A=[[v_0,v_1...v_n],[v_1,v_2,...v_{n-1}],...,[v_{n-1},v_n...v_{2n-1}]] \] 20635{\tt reverse\_rsolve} returns the list $x=[x_n,...x_1,x_0]$ with $x_n=1$ 20636and $x$ is the solution of the system $A*{\tt revlist}(x)$. 20637 20638{\bf Examples} 20639\begin{itemize} 20640\item Find a sequence satisfying a linear recurrence of degree at 20641most 2 whose first elements 1, -1, 3, 3.\\ 20642Input : 20643\begin{center}{\tt reverse\_rsolve([1,-1,3,3])}\end{center} 20644Output : 20645\begin{center}{\tt [1,-3,-6]}\end{center} 20646Hence $x_0=-6$, $x_1=-3$, $x_2=1$ and the recurrence relation is 20647 \[ v_{k+2} -3v_{k+1} -6 v_k =0\] 20648Without {\tt reverse\_rsolve}, we would write the matrix of the system :\\ 20649{\tt [[1,-1,3],[-1,3,3]]} and use the {\tt rref} command :\\ 20650{\tt rref([[1,-1,3],[-1,3,3]])}\\ 20651Output is {\tt [[1,0,6],[0,1,3]]} hence $x_0=-6$ and $x_1=-3$ 20652(because $x_2=1$). 20653 20654\item Find a sequence satisfying a linear recurrence of degree at 20655most 3 whose first elements are 1, -1, 3, 3,-1, 1.\\ 20656Input : 20657\begin{center}{\tt reverse\_rsolve([1,-1,3,3,-1,1])}\end{center} 20658Output : 20659\begin{center}{\tt [1,(-1)/2,1/2,-1]}\end{center} 20660Hence so, $x_0=-1$, $x_1=1/2$, $x_2=-1/2$, $x_3=1$, the recurrence 20661relation is 20662\[ v_{k+3} -\frac{1}{2} v_{k+2} +\frac{1}{2} v_{k+1} -v_k =0 \] 20663Without {\tt reverse\_rsolve}, we would write the matrix of the system :\\ 20664{\tt [[1,-1,3,3],[-1,3,3,-1],[3,3,-1,1]]}.\\ 20665Using {\tt rref} command, we would input :\\ 20666{\tt rref([[1,-1,3,3],[-1,3,3,-1],[3,3,-1,1]])}\\ 20667Output is {\tt [1,0,0,1],[0,1,0,1/-2],[0,0,1,1/2]]} 20668hence $x_0=-1$, $x_1=1/2$ and $x_2=-1/2$ because $x_3=1$), 20669\end{itemize} 20670 20671\section{Differential equations} 20672This section is limited to symbolic (or exact) solutions of 20673differential equations. 20674For numeric solutions of differential equations, see {\tt odesolve}. 20675For graphic representation of solutions of differential equations, 20676see {\tt plotfield}, {\tt plotode} and {\tt interactive\_plotode}. 20677 20678\subsection{Solving differential equations : {\tt desolve deSolve 20679dsolve}}\index{desolve}\index{deSolve}\index{dsolve} 20680{\tt desolve} (or {\tt deSolve}) can solve : 20681\begin{itemize} 20682\item linear differential equations with constant coefficients, 20683\item first order linear differential equations, 20684\item first order differential equations without $y$, 20685\item first order differential equations without $x$, 20686\item first order differential equations with separable variables, 20687\item first order homogeneous differential equations ($y'=F(y/x)$), 20688\item first order differential equations with integrating factor, 20689\item first order Bernoulli differential equations ($a(x)y'+b(x)y=c(x)y^n$), 20690\item first order Clairaut differential equations ($y=x*y'+f(y')$). 20691\end{itemize} 20692{\tt desolve} takes as arguments : 20693\begin{itemize} 20694\item if the independent variable is the current variable (here supposed 20695to be $x$), 20696\begin{itemize} 20697\item the differential equation (or the list of 20698the differential equation and of the initial conditions) 20699\item the unknown (usually {\tt y}). 20700\end{itemize} 20701In the differential equation, the function $y$ is denoted by $y$, 20702its first derivative $y \prime$ is denoted by 20703${\tt y'}$, and its second derivative $y'{'}$ is written 20704${\tt y''}$.\\ 20705For example {\tt desolve(y''+2*y'+y,y)} or \\ 20706{\tt desolve([y''+2*y'+y,y(0)=1,y'(0)=0],y)}. 20707\item if the independent variable is not the current variable, 20708for example $t$ instead of $x$, 20709\begin{itemize} 20710\item the differential equation (or the list of 20711the differential equation and of the initial conditions), 20712\item the variable, e.g. {\tt t} 20713\item the unknown as a variable {\tt y} or as a function {\tt y(t)}. 20714\end{itemize} 20715In the differential equation, the function $y$ is denoted by $y(t)$, 20716its derivative $y \prime$ is denoted by 20717{\tt diff(y(t),t)}, and its second derivative 20718$y'{'}$ is denoted by {\tt diff(y(t),t\$2)}.\\ 20719For example : \\ 20720{\tt desolve(diff(y(t),t\$2)+2*diff(y(t),t)+y(t),y(t))}; or\\ 20721{\tt desolve(diff(y(t),t\$2)+2*diff(y(t),t)+y(t),t,y)}; 20722and \\ 20723\begin{verbatim} 20724desolve([diff(y(t),t$2)+2*diff(y(t),t)+y(t), 20725 y(0)=1,y'(0)=0],y(t)); or 20726desolve([diff(y(t),t$2)+2*diff(y(t),t)+y(t), 20727 y(0)=1,y'(0)=0],t,y); 20728\end{verbatim} 20729\end{itemize} 20730If there is no initial conditions (or one initial condition for a second 20731order equation), 20732{\tt desolve} returns the general solution in terms of 20733constants of integration 20734{\tt c\_0, c\_1}, where {\tt y(0)=c\_0} and {\tt y'(0)=c\_1}, 20735or a list of solutions.\\ 20736{\bf Examples} 20737\begin{itemize} 20738\item Examples of second linear differential equations with constant 20739coefficients. 20740\begin{enumerate} 20741\item 20742Solve : 20743$$y''+y=\cos (x) $$ 20744Input (typing twice prime for {\tt y''}): 20745\begin{center}{\tt desolve(y''+y=cos(x),y)}\end{center} 20746or input : 20747\begin{center}{\tt desolve((diff(diff(y))+y)=(cos(x)),y)}\end{center} 20748Output : 20749\begin{center}{\tt c\_0*cos(x)+(x+2*c\_1)*sin(x)/2}\end{center} 20750{\tt c\_0, c\_1} are the constants of integration : {\tt y(0)=c\_0} and 20751{\tt y'(0)=c\_1}.\\ 20752If the variable is not {\tt x} but {\tt t}, input : 20753\begin{center} 20754{\tt desolve(derive(derive(y(t),t),t)+y(t)=cos(t),t,y)} 20755\end{center} 20756Output : 20757\begin{center}{\tt c\_0*cos(t)+(t+2*c\_1)/2*sin(t)}\end{center} 20758{\tt c\_0, c\_1} are the constants of integration : {\tt y(0)=c\_0} and 20759{\tt y'(0)=c\_1}. 20760\item 20761Solve : 20762$$y''+y=\cos (x), \; \; y(0)=1 $$ 20763Input : 20764\begin{center}{\tt desolve([y''+y=cos(x),y(0)=1],y)}\end{center} 20765Output : 20766\begin{center}{\tt [cos(x)+(x+2*c\_1)/2*sin(x)]}\end{center} 20767the components of this vector are solutions (here there is just one component, 20768so we have just one solution depending of the constant {\tt c\_1}). 20769\item 20770Solve : 20771$$y''+y=\cos (x) \; \; (y(0))^2=1 $$ 20772Input : 20773\begin{center}{\tt desolve([y''+y=cos(x),y(0)\verb|^|2=1],y)}\end{center} 20774Output : 20775\begin{center}{\tt [-cos(x)+(x+2*c\_1)/2*sin(x),cos(x)+(x+2*c\_1)/2*sin(x)]}\end{center} 20776each component of this list is a solution, 20777we have two solutions depending 20778on the constant {\tt c\_1} ($y'(0)=c_1$) 20779and corresponding to $y(0)=1$ and to $y(0)=-1$. 20780\item 20781Solve : 20782$$y''+y=\cos (x), \; \; (y(0))^2=1 \; \; y'(0)=1$$ 20783Input : 20784\begin{center}{\tt desolve([y''+y=cos(x),y(0)\verb|^|2=1,y'(0)=1],y)} 20785\end{center} 20786Output : 20787\begin{center}{\tt [-cos(x)+(x+2)/2*sin(x),cos(x)+(x+2)/2*sin(x)]}\end{center} 20788each component of this list is a solution (we have two solutions). 20789\item 20790Solve : 20791$$y''+2y'+y=0$$ 20792Input : 20793\begin{center}{\tt desolve(y''+2*y'+y=0,y)}\end{center} 20794Output : 20795\begin{center}{\tt (x*c\_0+x*c\_1+c\_0)*exp(-x)}\end{center} 20796the solution depends of 2 constants of integration : 20797{\tt c\_0, c\_1} ({\tt y(0)=c\_0} and {\tt y'(0)=c\_1}). 20798\item 20799Solve : 20800$$y''-6y'+9y=xe^{3x}$$ 20801Input: 20802\begin{center}{\tt desolve(y''-6*y'+9*y=(x*exp(3*x),y)}\end{center} 20803Output : 20804\begin{center}{\tt (x\verb|^|3+(-(18*x))*c\_0+6*x*c\_1+6*c\_0)*1/6*exp(3*x)}\end{center} 20805the solution depends on 2 constants of integration : 20806{\tt c\_0, c\_1} ({\tt y(0)=c\_0} and {\tt y'(0)=c\_1}). 20807\end{enumerate} 20808\item Examples of first order linear differential equations. 20809\begin{enumerate} 20810\item 20811Solve : 20812$$xy'+y-3x^2=0$$ 20813Input : 20814\begin{center}{\tt desolve(x*y'+y-3*x\verb|^|2,y)}\end{center} 20815Output : 20816\begin{center}{\tt(3*1/3*x\verb|^|3+c\_0)/x }\end{center} 20817\item 20818Solve : 20819$$y'+x*y=0, y(0)=1$$ 20820Input : 20821\begin{center}{\tt desolve([y'+x*y=0, y(0)=1]),y)}\end{center} 20822or : 20823\begin{center}{\tt desolve((y'+x*y=0) \&\& (y(0)=1),y)}\end{center} 20824Output : 20825\begin{center}{\tt [1/(exp(1/2*x\verb|^|2))]}\end{center} 20826\item 20827Solve : 20828$$x(x^2-1)y'+2y=0$$ 20829Input : 20830\begin{center}{\tt desolve(x*(x\verb|^|2-1)*y'+2*y=0,y)}\end{center} 20831Output : 20832\begin{center}{\tt (c\_0)/((x\verb|^|2-1)/(x\verb|^|2))}\end{center} 20833\item 20834Solve : 20835$$x(x^2-1)y'+2y=x^2$$ 20836Input : 20837\begin{center}{\tt desolve(x*(x\verb|^|2-1)*y'+2*y=x\verb|^|2,y)}\end{center} 20838Output : 20839\begin{center}{\tt (ln(x)+c\_0)/((x\verb|^|2-1)/(x\verb|^|2))}\end{center} 20840\item 20841If the variable is $t$ instead of $x$, for example : 20842$$t(t^2-1)y'(t)+2y(t)=t^2$$ 20843Input : 20844\begin{center}{\tt desolve(t*(t\verb|^|2-1)*diff(y(t),t)+2*y(t)=(t\verb|^|2),y(t))}\end{center} 20845Output : 20846\begin{center}{\tt (ln(t)+c\_0)/((t\verb|^|2-1)/(t\verb|^|2))}\end{center} 20847\item 20848Solve : 20849$$x(x^2-1)y'+2y=x^2,y(2)=0$$ 20850Input : 20851\begin{center}{\tt desolve([x*(x\verb|^|2-1)*y'+2*y=x\verb|^|2,y(0)=1],y)}\end{center} 20852Output : 20853\begin{center}{\tt [(ln(x)-ln(2))*1/(x\verb|^|2-1)*x\verb|^|2]}\end{center} 20854\item 20855Solve : 20856$$\sqrt{1+x^2}y'-x-y=\sqrt{1+x^2}$$ 20857Input : 20858\begin{center}{\tt desolve(y'*sqrt(1+x\verb|^|2)-x-y-sqrt(1+x\verb|^|2),y)}\end{center} 20859Output : 20860\begin{center}{\tt (-c\_0+ln(sqrt(x\verb|^|2+1)-x))/(x-sqrt(x\verb|^|2+1))}\end{center} 20861\end{enumerate} 20862 20863\item Examples of first differential equations with separable variables. 20864\begin{enumerate} 20865\item Solve : 20866$$y'=2\sqrt{y}$$ 20867Input : 20868\begin{center}{\tt desolve(y'=2*sqrt(y),y)}\end{center} 20869Output : 20870\begin{center}{\tt [x\verb|^|2+-2*x*c\_0+c\_0\verb|^|2]}\end{center} 20871\item 20872Solve : 20873$$xy'\ln(x)-y(3\ln(x)+1)=0$$ 20874Input : 20875\begin{center}{\tt desolve(x*y'*ln(x)-(3*ln(x)+1)*y,y)}\end{center} 20876Output : 20877\begin{center}{\tt c\_0*x\verb|^|3*ln(x)}\end{center} 20878\end{enumerate} 20879 20880\item Examples of Bernoulli differential equations 20881$a(x)y'+b(x)y=c(x)y^n$ where $n$ is a real constant.\\ 20882The method used is to divide the equation by $y^n$, 20883so that it becomes a first order linear differential equation 20884in $u=1/y^{n-1}$. 20885\begin{enumerate} 20886\item 20887Solve : 20888$$xy'+2y+xy^2=0$$ 20889Input : 20890\begin{center}{\tt desolve(x*y'+2*y+x*y\verb|^|2,y)}\end{center} 20891Output : 20892\begin{center}{\tt [1/(exp(2*ln(x))*(-1/x+c\_0))]}\end{center} 20893\item 20894Solve : 20895$$xy'-2y=xy^3$$ 20896Input : 20897\begin{center}{\tt desolve(x*y'-2*y-x*y\verb|^|3,y)}\end{center} 20898Output : 20899\begin{center}{\tt [((-2*1/5*x\verb|^|5+c\_0)*exp(-(4*log(x))))\verb|^|(1/-2),}\end{center} 20900\begin{center}{\tt -((-2*1/5*x\verb|^|5+c\_0)*exp(-(4*log(x))))\verb|^|(1/-2)]}\end{center} 20901\item 20902Solve : 20903$$x^2y'-2y=xe^{(4/x)}y^3$$ 20904Input : 20905\begin{center}{\tt desolve(x*y'-2*y-x*exp(4/x)*y\verb|^|3,y)}\end{center} 20906Output : 20907\begin{center}{\tt [((-2*ln(x)+c\_0)*exp(-(4*(-(1/x)))))\verb|^|(1/-2),}\end{center} 20908\begin{center}{\tt -(((-2*ln(x)+c\_0)*exp(-(4*(-(1/x)))))\verb|^|(1/-2))]}\end{center} 20909\end{enumerate} 20910 20911\item Examples of first order homogeneous differential equations ($y'=F(y/x)$, 20912the method of integration is to search $t=y/x$ instead of $y$). 20913\begin{enumerate} 20914\item 20915Solve : 20916$$3x^3y'=y(3x^2-y^2)$$ 20917Input : 20918\begin{center}{\tt desolve(3*x\verb|^|3*diff(y)=((3*x\verb|^|2-y\verb|^|2)*y),y)}\end{center} 20919Output : 20920\begin{center}{\tt [0,pnt[c\_0*exp((3*1/2)/(` t`\verb|^|2)),` t`*c\_0*exp((3*1/2)/(` t`\verb|^|2))]]}\end{center} 20921hence the solutions are $y=0$ and the familiy of curves of parametric 20922equation $x=c_0\exp(3/(2t^2)), y=t*c_0\exp(3/(2t^2))$ 20923(the parameter is denoted by {\tt ` t`} in the answer). 20924\item 20925Solve : 20926$$xy'=y+\sqrt{x^2+y^2}$$ 20927Input : 20928\begin{center}{\tt desolve(x*y'=y+sqrt(x\verb|^|2+y\verb|^|2),y)}\end{center} 20929Output : 20930\begin{center}{\tt [(-i)*x,(i)*x,pnt[c\_0/(sqrt(` t`\verb|^|2+1)-` t`),(` t`*c\_0)/(sqrt(` t`\verb|^|2+1)-` t`)]]}\end{center} 20931hence the solutions are : 20932$$y=ix,y=-ix$$ 20933 and the family of curves of parametric equations 20934$$x=c_0/(\sqrt{t^2+1}-t), y=t*c_0/(\sqrt{t^2+1}-t)$$ 20935(the parameter is denoted by {\tt ` t`} in the answer). 20936\end{enumerate} 20937 20938 20939\item Examples of first order differential equations with an 20940integrating factor. By multiplying the equation by a function of $x,y$, 20941it becomes a closed differential form. 20942\begin{enumerate} 20943\item 20944Solve : 20945$$yy'+x$$ 20946Input : 20947\begin{center}{\tt desolve(y*y'+x,y)}\end{center} 20948Output : 20949\begin{center}{\tt [sqrt(-2*c\_0-x\verb|^|2),-(sqrt(-2*c\_0-x\verb|^|2))]}\end{center} 20950In this example, $xdx+ydy$ is closed, the integrating factor was 1. 20951\item 20952Solve : 20953$$2xyy'+x^2-y^2+a^2=0$$ 20954Input : 20955\begin{center}{\tt desolve(2*x*y*y'+x\verb|^|2-y\verb|^|2+a\verb|^|2,y)}\end{center} 20956Output : 20957\begin{center}{\tt [sqrt(a\verb|^|2-x\verb|^|2-c\_1*x),-(sqrt(a\verb|^|2-x\verb|^|2-c\_1*x))]}\end{center} 20958In this example, the integrating factor was $1/x^2$. 20959\end{enumerate} 20960 20961\item Example of first order differential equations without $x$.\\ 20962Solve : 20963$$(y+y')^4+y'+3y=0$$ 20964This kind of equation cannot be solved directly by {\tt Xcas}, we explain 20965how to solve them with its help. 20966The idea is to find a parametric representation of 20967$F(u,v)=0$ where the equation is $F(y,y')=0$, 20968Let $u=f(t),v=g(t)$ be such a parametrization of $F=0$, then 20969$y=f(t)$ and $dy/dx=y'=g(t)$. Hence 20970\[ dy/dt=f'(t)=y'*dx/dt=g(t)*dx/dt \] 20971The solution is the curve of parametric equations 20972$x(t), y(t)=f(t)$, where $x(t)$ is solution of the differential equation 20973 $g(t)dx=f'(t)dt$.\\ 20974Back to the example, we put $y+y'=t$, hence: 20975\[ y=-t-8*t^4, \quad y'=dy/dx=3*t+8*t^4 \quad dy/dt=-1-32*t^3 20976\] 20977therefore 20978\[ (3*t+8*t^4)*dx=(-1-32*t^3)dt \] 20979Input : 20980\begin{center}{\tt desolve((3*t+8*t\verb|^|4)*diff(x(t),t)=(-1-32*t\verb|^|3),x(t))}\end{center} 20981Output : 20982\begin{center}{\tt -11*1/9*ln(8*t\verb|^|3+3)+1/-9*ln(t\verb|^|3)+c\_0}\end{center} 20983eventually the solution is the curve of parametric equation : 20984\[ x(t)=-11*1/9*\ln(8*t^3+3)+1/-9*\ln(t^3)+c_0, 20985\quad y(t)=-t-8*t^4 \] 20986 20987\item Examples of first order 20988Clairaut differential equations ($y=x*y'+f(y')$).\\ 20989The solutions are the lines $D_m$ of equation $y=mx+f(m)$ where 20990 $m$ is a real constant. 20991\begin{enumerate} 20992\item Solve : 20993$$xy'+y'^3-y=0$$ 20994Input : 20995\begin{center}{\tt desolve(x*y'+y'\verb|^|3-y),y)}\end{center} 20996Output : 20997\begin{center}{\tt c\_0*x+c\_0\verb|^|3}\end{center} 20998\item 20999Solve : 21000$$y-xy' - \sqrt{a^2+b^2*y'^2}=0$$ 21001Input : 21002\begin{center}{\tt desolve((y-x*y'-sqrt(a\verb|^|2+b\verb|^|2*y'\verb|^|2),y)}\end{center} 21003Output : 21004\begin{center}{\tt c\_0*x+sqrt(a\verb|^|2+b\verb|^|2*c\_0\verb|^|2)}\end{center} 21005\end{enumerate} 21006\end{itemize} 21007 21008\subsection{Laplace transform and inverse Laplace transform : {\tt 21009laplace ilaplace invlaplace}} 21010\index{laplace} 21011\index{ilaplace} 21012\index{invlaplace} 21013\label{sec:lap} 21014 21015{\tt laplace} and {\tt ilaplace} (or \texttt{invlaplace}) take one, two or three arguments : 21016 an expression and optionally the name(s) of the variable(s).\\ 21017The expression is an expression of the current variable (here $x$) or an 21018expression of the variable given as second argument.\\ 21019{\tt laplace} returns the Laplace transform of the expression given as argument 21020and {\tt ilaplace} the inverse Laplace transform of the expression given 21021as argument. The result of {\tt laplace} or {\tt ilaplace} is expressed 21022in terms of the variable given as third argument if supplied 21023or second argument if supplied or $x$ otherwise. 21024 21025The Laplace transform ({\tt laplace}) and inverse Laplace transform 21026({\tt ilaplace}) are useful to solve linear differential equations 21027with constant coefficients. For example : 21028$$y \prime \prime +p. y \prime+q. y \ =\ f(x)$$ $$ y(0)=a, \ y\prime(0)=b$$ 21029Denoting by ${\mathcal{L}}$ the Laplace transform, 21030the following relations hold : 21031\begin{eqnarray*} 21032{\mathcal{L}}(y)(x)&=&\int_0^{+\infty}e^{-x u}y(u)du \\ 21033{\mathcal{L}}^{-1}(g)(x)&=&\frac{1}{2i\pi}\int_C e^{z x}g(z)dz 21034\end{eqnarray*} 21035where $C$ is a closed contour enclosing the poles of {\tt g}.\\ 21036Input : 21037\begin{center}{\tt laplace(sin(x))}\end{center} 21038The expression (here $\sin(x)$) is an expression of the current variable 21039(here $x$) and the answer will also be an expression of the current variable 21040$x$.\\ 21041Output : 21042\begin{center}{\tt 1/((-x)\verb|^|2+1)}\end{center} 21043or : 21044\begin{center}{\tt laplace(sin(t),t)}\end{center} 21045here the variable name is $t$ and this name is also used in the answer.\\ 21046Output : 21047\begin{center}{\tt 1/((-t)\verb|^|2+1)}\end{center} 21048Or input : 21049\begin{center}{\tt laplace(sin(t),t,s)}\end{center} 21050here the variable name is $t$ and the variable name of the answer is $s$.\\ 21051Output: 21052\begin{center}{\tt 1/((-s)\verb|^|2+1)}\end{center} 21053The following properties hold : 21054\begin{eqnarray*} 21055{\mathcal{L}}(y')(x) &=&-y(0)+x.{\mathcal{L}}(y)(x) \\ 21056{\mathcal{L}}(y'')(x) &=&-y'(0)+x.{\mathcal{L}}(y')(x) \\ 21057 &=& -y'(0)-x.y(0)+x^2.{\mathcal{L}}(y)(x) 21058\end{eqnarray*} 21059If $y \prime \prime(x) +p y \prime(x)+q y(x) \ =\ f(x)$, then : 21060\begin{eqnarray*} 21061{\mathcal{L}}(f)(x) &=&{\mathcal{L}}(y''+p.y'+q.y)(x) \\ 21062&=& -y'(0)-x y(0)+x^2 {\mathcal{L}}(y)(x)-p y(0)+p x {\mathcal{L}}(y)(x))+q {\mathcal{L}}(y)(x) \\ 21063&=& (x^2+p x+q) {\mathcal{L}}(y)(x)-y'(0)-(x+p) y(0) 21064\end{eqnarray*} 21065Therefore, if $a=y(0)$ and $b=y'(0)$, we have 21066$${\mathcal{L}}(f)(x)=(x^2+p x+q).{\mathcal{L}}(y)(x)-(x+p) a-b$$ 21067and the solution of the differential equation is : 21068\[ y(x)= 21069{\mathcal{L}}^{-1}(({\mathcal{L}}(f)(x)+(x+p) a +b)/(x^2+p x+q)) 21070\] 21071Example :\\ 21072Solve : 21073\[ y\prime \prime -6 y\prime+9 y \ =\ x e^{3. x}, 21074\quad y(0)=c\_0, \quad y\prime(0)=c\_1 21075\] 21076Here, $p=-6,\ q=9$.\\ 21077Input : 21078\begin{center}{\tt laplace(x*exp(3*x))}\end{center} 21079Output : 21080\begin{center}{\tt 1/(x\verb|^| 2-6*x+9)}\end{center} 21081Input : 21082\begin{center}{\tt ilaplace((1/(x\verb|^|2-6*x+9)+(x-6)*c\_0+c\_1)/(x\verb|^|2-6*x+9))}\end{center} 21083Output : 21084\begin{center}{\tt (216*x\verb|^|3-3888*x*c\_0+1296*x*c\_1+1296*c\_0)*exp(3*x)/1296}\end{center} 21085After simplification and factorization ({\tt factor} command) 21086the solution $y$ is : 21087\begin{center}{\tt (-18*c\_0*x+6*c\_0+x\verb|^|3+6*x*c\_1)*exp(3*x)/6}\end{center} 21088Note that this equation could be solved directly. 21089Input : 21090\begin{center}{\tt desolve(y''-6*y'+9*y=x*exp(3*x),y)}\end{center} 21091Output : 21092\begin{center}{\tt exp(3*x)*(-18*c\_0*x+6*c\_0+x\verb|^|3+6*x*c\_1)/6}\end{center} 21093 21094\subsection{Solving linear homogeneous second-order ODE with rational coefficients : {\tt kovacicsols\index{kovacicsols}}} 21095{\tt kovacicsols} uses Kovacic's algorithm to find a Liouvillian solution of an ordinary linear homogeneous second-order differential equation 21096\begin{equation}\label{kovacic-ode}a\,y''+b\,y'+c\,y=0,\end{equation} 21097where $a$, $b$ and $c$ are rational functions of the independent variable. The command takes from one to three arguments : 21098\begin{itemize} 21099 \item equation~\eqref{kovacic-ode} as an expression (left-hand side), equality or a list of coefficients~$[a,b,c]$, 21100 \item independent variable (optional, by default $x$), 21101 \item dependent variable (optional, by default $y$). 21102\end{itemize} 21103The dependent variable should not be specified if the equation~\eqref{kovacic-ode} is entered as a list of coefficients. 21104 21105The return value can be a list or an expression. An empty list means that there are no Liouvillian solutions to the input equation. If a non-empty list is returned, it contains one or two independent solution(s) $y_1$ (and $y_2$) to the equation~\eqref{kovacic-ode}. The general solution to~\eqref{kovacic-ode} is then 21106\[ y=C_1\,y_1+C_2\,y_2, \] 21107where $C_1,C_2\in\mathbb{R}$ are arbitrary constants. However, for some equations only $y_1$ is returned, in which case $y_2$ can be obtained as (using reduction of order) : 21108\begin{equation}\label{kovacic-reduction}y_2=y_1\,\int y_1^{-2}.\end{equation} 21109If {\tt kovacicsols} returns an expression, it means that the solution to~\eqref{kovacic-ode} is given implicitly. In that case the return value is a polynomial $P$ of order $n\in\{4,6,12\}$ in the variable {\tt omega\_} (denoted here by $\omega$) with rational coefficients $r_k$, $k=0,1,2,\dots,n$. If $P(\omega_0)=0$ for some $\omega_0$, then $y=\exp\left(\int\omega_0\right)$ is a solution to the equation~\eqref{kovacic-ode}. 21110 21111\paragraph{Examples.} 21112In the first example we find the general solution to the equation 21113\[ y''=\left(\frac{1}{x}-\frac{3}{16\,x^2}\right)\,y. \] 21114Input : 21115\begin{center} 21116 \tt kovacicsols(y\verb|''|=y*(1/x-3/16x\verb|^|2)) 21117\end{center} 21118Output : 21119\begin{center} 21120 \tt [x\verb|^|(1/4)*exp(2*sqrt(x)),x\verb|^|(1/4)*exp(-2*sqrt(x))] 21121\end{center} 21122Therefore, $y=C_1\,x^{1/4}\,\mathrm{e}^{2\,\sqrt{x}}+C_2\,x^{1/4}\,\mathrm{e}^{-2\,\sqrt{x}}$ is the general solution. 21123 21124In the following example we solve the equation 21125\[ x''(t)+\frac{3\,(t^2-t+1)}{16\,(t-1)^2\,t^2}\,x(t)=0. \] 21126Input : 21127\begin{center} 21128 \tt kovacicsols(x\verb|''|+3*(t\verb|^|2-t+1)/(16*(t-1)\verb|^|2*t\verb|^|2)*x,t,x) 21129\end{center} 21130Output : 21131\begin{center} 21132 \tt [(-t*(t-1)*(2*t+2*sqrt(t\verb|^|2-t)-1))\verb|^|(1/4), (t*(t-1)*(-2*t+2*sqrt(t\verb|^|2-t)+1))\verb|^|(1/4)] 21133\end{center} 21134Now for arbitrary $C_1,C_2\in\mathbb{R}$ we have 21135\[\begin{split}x(t)=C_1\,\sqrt[4]{t\,(t-1)\,(1-2\,t-2\,\sqrt{t^2-t})}+\\C_2\,\sqrt[4]{t\,(t-1)\,(1-2\,t+2\,\sqrt{t^2-t})}.\end{split}\] 21136 21137In the next example we find a particular solution to the equation 21138\[ y''=\frac{4\,x^6-8\,x^5+12\,x^4+4\,x^3+7\,x^2-20\,x+4}{4\,x^4}\,y. \] 21139Input : 21140\begin{center} 21141 \tt r:=(4x\verb|^|6-8x\verb|^|5+12x\verb|^|4+4x\verb|^|3+7x\verb|^|2-20x+4)/(4x\verb|^|4):; kovacicsols(y\verb|''|=r*y) 21142\end{center} 21143Output : 21144\begin{center} 21145 \tt [(x\verb|^|2-1)/(x*sqrt(x))*exp((x\verb|^|3-2*x\verb|^|2-2)/(2*x))] 21146\end{center} 21147Hence $y=(x^2-1)\,x^{-3/2}\,\mathrm{e}^{\frac{x^3-2\,x^2-2}{2\,x}}$ is a solution to the given equation. 21148 21149A similar output is obtained when solving the equation 21150\[ y''+y'=\frac{6\,y}{x^2}. \] 21151Input : 21152\begin{center} 21153 \tt kovacicsols(y\verb|''|+y\verb|'|=6y/x\verb|^|2) 21154\end{center} 21155Output : 21156\begin{center} 21157 \tt [(x\verb|^|2+6*x+12)*exp(-x)/x\verb|^|2] 21158\end{center} 21159 21160To solve Titchmarsh equation 21161\[ y''+(19-x^2)\,y=0, \] 21162input : 21163\begin{center} 21164 \tt kovacicsols(y\verb|''|+(19-x\verb|^|2)*y=0,x,y) 21165\end{center} 21166We obtain a particular solution 21167\[ y=\left(x^9-18\,x^7+\frac{189\,x^5}{2}-\frac{315\,x^3}{2}+\frac{945\,x}{16}\right)\,\exp\left(-\frac{x^2}{2}\right). \] 21168 21169To find the general solution of Halm's equation 21170\[ (1+x^2)^2\,y''(x)+3\,y(x)=0, \] 21171input : 21172\begin{center} 21173 \tt sol:=kovacicsols((1+x\verb|^|2)\verb|^|2*y\verb|''|+3y=0,x,y) 21174\end{center} 21175Output : 21176\begin{center} 21177 \tt [(x\verb|^|2-1)/(sqrt(x\verb|^|2+1))] 21178\end{center} 21179The other basic solution is obtained by using~\eqref{kovacic-reduction}. Input : 21180\begin{center} 21181 \tt y1:=sol[0]; y2:=normal(y1*int(y1\verb|^|-2,x)) 21182\end{center} 21183Output : 21184\begin{center} 21185 \tt (x\verb|^|2-1)/(sqrt(x\verb|^|2+1)),-x/(sqrt(x\verb|^|2+1)) 21186\end{center} 21187Therefore, $y=C_1\,\frac{x^2-1}{\sqrt{x^2+1}}+C_2\,\frac{x}{\sqrt{x^2+1}}$, where $C_1,C_2\in\mathbb{R}$. 21188 21189In the following example we find the general solution of the non-homogeneous equation 21190\[ y''-\frac{27\,y}{36\,(x-1)^2}=x+4. \] 21191First we need to find the general solution to the corresponding homogeneous equation $y_h''-\frac{27\,y_h}{36\,(x-1)^2}=0$. Input : 21192\begin{center} 21193 \tt sols:=kovacicsols(y\verb|''|-y*27/(36*(x-1)\verb|^|2),x,y) 21194\end{center} 21195Output : 21196\begin{center} 21197 \tt [(x\verb|^|2-2*x)/(sqrt(x-1))] 21198\end{center} 21199We call the obtained solution $y_1$ and find the other basic independent solution by using~\eqref{kovacic-reduction}. Input : 21200\begin{center} 21201 \tt y1:=sols[0]:; y2:=y1*int(1/y1\verb|^|2,x) 21202\end{center} 21203Output : 21204\begin{center} 21205 \tt -1/(sqrt(x-1)*2) 21206\end{center} 21207Now the general solution of the homogeneous equation is 21208\[ y_h=C_1\,y_1+C_2\,y_2=\frac{C_1\,(x^2-2\,x)+C_2}{\sqrt{x-1}},\quad C_1,C_2\in\mathbb{R}. \] 21209A particular solution $y_p$ of the non-homogeneous equation can be obtained by variation of parameters as 21210\[ y_p=-y_1\,\int\frac{y_2\,f(x)}{W}\,\mathrm{d}x+y_2\,\int\frac{y_1\,f(x)}{W}\,\mathrm{d}x, \] 21211where $f(x)=x+4$ and $W$ is the Wronskian of $y_1$ and $y_2$, i.e.~\[W=y_1\,y_2'-y_2\,y_1'\neq 0.\] Input : 21212\begin{center} 21213 \tt W:=y1*y2'-y2*y1':; f:=x+4:; yp:=normal(-y1*int(y2*f/W,x)+y2*int(y1*f/W,x)) 21214\end{center} 21215Output : 21216\begin{center} 21217 \tt (4*x\verb|^|3+72*x\verb|^|2-156*x+80)/21 21218\end{center} 21219Hence $y_p=\frac{1}{21}\,(4\,x^3+72\,x^2-156\,x+80)$. Now $y=y_p+y_h$. We proceed by checking that it is indeed the general solution of the given equation. Input : 21220\begin{center} 21221 \tt purge(C1,C2):; ysol:=yp+C1*y1+C2*y2:; normal(diff(ysol,x,2)-27/(36*(x-1)\verb|^|2)*ysol)==f 21222\end{center} 21223Output : 21224\begin{center} 21225 \tt true 21226\end{center} 21227 21228In the next example we attempt to solve the equation from the original Kovacic's paper : 21229\[ y''=\left(\frac{3}{16\,x\,(x-1)}-\frac{2}{9\,(x-1)^2}-\frac{3}{16\,x^2}\right)\,y. \] 21230Input : 21231\begin{center} 21232 \tt r:=-3/(16x\verb|^|2)-2/(9*(x-1)\verb|^|2)+3/(16x*(x-1)):; kovacicsols(y\verb|''|=r*y) 21233\end{center} 21234Output : 21235\begin{center} 21236 \tt -omega\_\verb|^|4*x\verb|^|4*(x-1)\verb|^|4+ omega\_\verb|^|3*x\verb|^|3*(x-1)\verb|^|3*(7*x-3)/3- omega\_\verb|^|2*x\verb|^|2*(x-1)\verb|^|2*(48*x\verb|^|2-41*x+9)/24+ omega\_*x*(x-1)*(320*x\verb|^|3-409*x\verb|^|2+180*x-27)/432+ (-2048*x\verb|^|4+3484*x\verb|^|3-2313*x\verb|^|2+702*x-81)/20736 21237\end{center} 21238The solution is $y=\exp\left(\int\omega_0\right)$, where $\omega_0$ is a zero of the above expression, thus being a root of a fourth-order polynomial in $\omega$. In similar cases one can try the Ferrari method to obtain $\omega_0$. 21239 21240We get similar output while trying to solve the equation 21241\[ 48\,t\,(t+1)\,(5\,t-4)\,y''+8\,(25\,t+16)\,(t-2)\,y'-(5\,t+68)\,y=0. \] 21242Input : 21243\begin{center} 21244 \tt de:=[48t*(t+1)*(5t-4),8*(25t+16)*(t-2),-(5t+68)]:; kovacicsols(de,t) 21245\end{center} 21246Output : 21247\begin{center} 21248 \tt omega\_\verb|^|4*(135*t\verb|^|4-616*t\verb|^|3-144*t\verb|^|2+3072*t-4096)/20736- omega\_\verb|^|2*t\verb|^|2*(t+1)*(15*t\verb|^|3-80*t\verb|^|2+80*t+256)/24- t\verb|^|4*(t+1)\verb|^|2*(t+4)*(5*t+4)+ 2*omega\_*t\verb|^|3*(t+1)\verb|^|2*(t-4)*(5*t+8)/3- omega\_\verb|^|3*t*(t+1)*(23*t\verb|^|2-92*t+128)/54 21249\end{center} 21250 21251\section{The Z-transform} 21252 21253\subsection{The Z-transform of a sequence: \texttt{ztrans}}\index{ztrans} 21254 21255The Z-transform of a sequence $a_0, a_1, \dots, a_n, \dots$ is the 21256function 21257\[ f(z) = \sum_{n=0}^{\infty} \frac{a_n}{z^n}.\] 21258 21259The \texttt{ztrans} command takes one or three arguments. 21260\begin{itemize} 21261 \item A formula in the variable \texttt{x} for the general term 21262 $a_x$ of a sequence, or 21263 \item A formula for the general term of a sequence, the variable 21264 used in the formula, and a variable to be used by the resulting 21265 function. 21266\end{itemize} 21267\texttt{ztrans} returns the Z-transform of the sequence. 21268 21269For example, the Z-transform of the sequence 21270\[ 0, 1, 2, 3, \dots\] 21271is 21272\[ f(z) = 0 + 1/z + 2/z^2 + 3/z^3 + \dots\] 21273which has closed form 21274\[ f(z) = z/(z-1)^2.\] 21275\noindent 21276Input: 21277\begin{center} 21278 \tt 21279 ztrans(x) 21280\end{center} 21281Output: 21282\begin{center} 21283 \tt 21284 x/(x\^{}2-2*x+1) 21285\end{center} 21286Input: 21287\begin{center} 21288 \tt 21289 ztrans(n,n,z) 21290\end{center} 21291Output: 21292\begin{center} 21293 \tt 21294 z/(z\^{}2-2*z+1) 21295\end{center} 21296 21297Note that\\ 21298Input: 21299\begin{center} 21300 \tt 21301 ztrans(1) 21302\end{center} 21303Output: 21304\begin{center} 21305 \tt 21306 x/(x-1) 21307\end{center} 21308since 21309\[ \sum_{n=0}^{\infty} 1/x^n = 1/(1-1/x) = x/(x-1).\] 21310We also have\\ 21311Input: 21312\begin{center} 21313 \tt 21314 ztrans(1,n,z) 21315\end{center} 21316Output: 21317\begin{center} 21318 \tt 21319 z/(z-1) 21320\end{center} 21321Note that differentiating both sides of 21322\[ \sum_{n=0}^{\infty} 1/z^n = z/(z-1)\] 21323gives us 21324\[ \sum_{n=0}^{\infty} n/z^{n-1} = 1/(z-1)^2\] 21325and so, multiplying both sides by $z$, 21326\[ \sum_{n=0}^{\infty} n/z^{n} = z/(z-1)^2 = z/(z^2 - 2z + 1)\] 21327as indicated above. 21328 21329 21330\subsection{The inverse Z-transform of a rational function: 21331\texttt{invztrans}}\index{invztrans} 21332 21333The \texttt{invztrans} command takes one or three arguments. 21334\begin{itemize} 21335 \item A rational expression in the variable \texttt{x}, or 21336 \item A rational expression, the variable 21337 used in the expression, and a variable to be used by the result. 21338\end{itemize} 21339\texttt{ztrans} returns the inverse Z-transform, namely a formula for 21340the general term of a sequence with the given rational expression as 21341its Z-transform. 21342 21343Since \texttt{ztrans(1) = x/(x-1)}, we get\\ 21344Input: 21345\begin{center} 21346 \tt 21347 invztrans(x/(x-1)) 21348\end{center} 21349Output: 21350\begin{center} 21351 \tt 21352 1 21353\end{center} 21354Input: 21355\begin{center} 21356 \tt 21357 invztrans(z/(z-1),z,n) 21358\end{center} 21359Output: 21360\begin{center} 21361 \tt 21362 1 21363\end{center} 21364 21365Similarly,\\ 21366Input: 21367\begin{center} 21368 \tt 21369 invztrans(x/(x-1)\^{}2) 21370\end{center} 21371Output: 21372\begin{center} 21373 \tt 21374 x 21375\end{center} 21376Input: 21377\begin{center} 21378 \tt 21379 invztrans(z/(z-1)\^{}2,z,n) 21380\end{center} 21381Output: 21382\begin{center} 21383 \tt 21384 n 21385\end{center} 21386 21387\section{Other functions} 21388\subsection{Replace small values by 0: {\tt epsilon2zero}} 21389\index{epsilon2zero} \label{sec:epsilon2zero} 21390\noindent{\tt epsilon2zero} takes as argument an expression of {\tt x}.\\ 21391{\tt epsilon2zero} returns the expression where the values of modulus 21392less than {\tt epsilon} are replaced by zero. The expression 21393is not evaluated.\\ 21394The {\tt epsilon}\index{epsilon} value is defined in the {\tt cas} 21395configuration (by default {\tt epsilon=1e-10}).\\ 21396Input : 21397\begin{center}{\tt epsilon2zero(1e-13+x) }\end{center} 21398Output (with {\tt epsilon=1e-10}) : 21399\begin{center}{\tt 0+x}\end{center} 21400Input : 21401\begin{center}{\tt epsilon2zero((1e-13+x)*100000) }\end{center} 21402Output (with {\tt epsilon=1e-10}) : 21403\begin{center}{\tt (0+x)*100000}\end{center} 21404Input : 21405\begin{center}{\tt epsilon2zero(0.001+x) }\end{center} 21406Output (with {\tt epsilon=0.0001}) : 21407\begin{center}{\tt 0.001+x}\end{center} 21408 21409\subsection{List of variables : {\tt lname indets}}\index{lname}\index{indets} 21410\noindent{\tt lname} (or {\tt indets}) takes as argument an expression.\\ 21411{\tt lname} (or {\tt indets}) returns the list of the symbolic 21412variable names used in this expression.\\ 21413Input : 21414\begin{center}{\tt lname(x*y*sin(x))}\end{center} 21415Output : 21416\begin{center}{\tt [x,y]}\end{center} 21417Input : 21418\begin{center}{\tt a:=2;assume(b>0);assume(c=3);}\end{center} 21419\begin{center}{\tt lname(a*x\verb|^|2+b*x+c)}\end{center} 21420Output : 21421\begin{center}{\tt [x,b,c]}\end{center} 21422 21423\subsection{List of variables and of expressions : {\tt lvar}}\index{lvar}\label{sec:lvar} 21424\noindent{\tt lvar} takes as argument an expression.\\ 21425{\tt lvar} returns a list of variable names and non-rational 21426expressions such that its argument is a rational fraction 21427with respect to the variables and expressions of the list.\\ 21428Input : 21429\begin{center}{\tt lvar(x*y*sin(x)\verb|^|2)}\end{center} 21430Output : 21431\begin{center}{\tt [x,y,sin(x)]}\end{center} 21432Input : 21433\begin{center}{\tt lvar(x*y*sin(x)\verb|^|2+ln(x)*cos(y))}\end{center} 21434Output : 21435\begin{center}{\tt [x,y,sin(x),ln(x),cos(y)]}\end{center} 21436Input : 21437\begin{center}{\tt lvar(y+x*sqrt(z)+y*sin(x))}\end{center} 21438Output : 21439\begin{center}{\tt [x,y,sqrt(z),sin(x)]}\end{center} 21440 21441\subsection{List of variables of an algebraic expressions: {\tt algvar}}\index{algvar} 21442\noindent{\tt algvar} takes as argument an expression.\\ 21443{\tt algvar} returns the list of the symbolic 21444variable names used in this expression. The list is ordered 21445by the algebraic extensions required to build the original expression.\\ 21446Input : 21447\begin{center}{\tt algvar(y+x*sqrt(z))}\end{center} 21448Output : 21449\begin{center}{\tt [[y,x],[z]]}\end{center} 21450Input : 21451\begin{center}{\tt algvar(y*sqrt(x)*sqrt(z))}\end{center} 21452Output : 21453\begin{center}{\tt [[y],[z],[x]]}\end{center} 21454Input : 21455\begin{center}{\tt algvar(y*sqrt(x*z))}\end{center} 21456Output : 21457\begin{center}{\tt [[y],[x,z]]}\end{center} 21458Input : 21459\begin{center}{\tt algvar(y+x*sqrt(z)+y*sin(x))}\end{center} 21460Output : 21461\begin{center}{\tt [[x,y,sin(x)],[z]]}\end{center} 21462 21463\subsection{Test if a variable is in an expression : {\tt has}}\index{has|textbf} 21464\noindent{\tt has} takes as argument an expression and the name of a 21465variable.\\ 21466{\tt has} returns {\tt 1} if this variable is in this expression, and else 21467returns {\tt 0}.\\ 21468Input : 21469\begin{center}{\tt has(x*y*sin(x),y)}\end{center} 21470Output : 21471\begin{center}{\tt 1}\end{center} 21472Input : 21473\begin{center}{\tt has(x*y*sin(x),z)}\end{center} 21474Output : 21475\begin{center}{\tt 0}\end{center} 21476 21477\subsection{Numeric evaluation : {\tt evalf}}\index{evalf} 21478\noindent{\tt evalf} takes as argument an expression or a matrix.\\ 21479{\tt evalf} returns the numeric value of this expression or of this matrix.\\ 21480Input : 21481\begin{center}{\tt evalf(sqrt(2))}\end{center} 21482Output : 21483\begin{center}{\tt 1.41421356237}\end{center} 21484Input : 21485\begin{center}{\tt evalf([[1,sqrt(2)],[0,1]])}\end{center} 21486Output : 21487\begin{center}{\tt [[1.0,1.41421356237],[0.0,1.0]]}\end{center} 21488 21489\subsection{Rational approximation : {\tt float2rational exact}}\index{float2rational}\index{exact} 21490\noindent{\tt float2rational} (or {\tt exact}) 21491takes as argument an expression.\\ 21492{\tt float2rational} returns a rational approximation of 21493all the floating point numbers $r$ contained in this expression, such 21494that $|r-\mbox{\tt float2rational}(r)|<\epsilon$, where 21495$\epsilon$ is defined by {\tt epsilon} in the {\tt cas} configuration 21496({\tt Cfg} menu, or {\tt cas\_setup} command).\\ 21497Input : 21498\begin{center}{\tt float2rational(1.5)}\end{center} 21499Output : 21500\begin{center}{\tt 3/2}\end{center} 21501Input : 21502\begin{center}{\tt float2rational(1.414)}\end{center} 21503Output : 21504\begin{center}{\tt 707/500}\end{center} 21505Input : 21506\begin{center}{\tt float2rational(0.156381102937*2)}\end{center} 21507Output : 21508\begin{center}{\tt 5144/16447}\end{center} 21509Input : 21510\begin{center}{\tt float2rational(1.41421356237)}\end{center} 21511Output : 21512\begin{center}{\tt 114243/80782}\end{center} 21513Input : 21514\begin{center}{\tt float2rational(1.41421356237\verb|^|2)}\end{center} 21515Output : 21516\begin{center}{\tt 2}\end{center} 21517 21518\section{The day of the week: \texttt{dayofweek}}\index{dayofweek} 21519 21520The \texttt{dayofweek} command takes as arguments three integers; the 21521first represents the day of the month, the second the month, and the 21522third the year. The resulting date should be after 15 October 1582.\\ 21523\texttt{dayofweek} returns an integer from 0 to 6; 0 represents a 21524Sunday, 1 represents Monday, etc.\\ 21525Input: 21526\begin{center} 21527 \tt 21528 dayofweek(15,10,1582) 21529\end{center} 21530Output: 21531\begin{center} 21532 \tt 21533 5 21534\end{center} 21535This indicates that 15 October 1582 was on a Friday. 21536 21537The Gregorian calendar, the calendar used by most of the world, was 21538introduced on 15 October 1582. Before that, the Julian calendar was 21539used, which had a leap year every four years and so used years with an 21540average of 365.25, which is slightly off from the actual value of 21541about 365.242 days. To deal with this, the Gregorian calendar was 21542introduced, which kept years for every year divisible by 4, except if 21543a year is divisible by 100 but not 400 it is not a leap year. This 21544gives an average length of year that is accurate to within 1 day every 215453000 years. 21546 21547Many countries switched from the Julian calendar to the Gregorian 21548calendar after 4 October 1582 in the Julian calendar, and the next day 21549was 15 October 1582. 21550 21551\noindent 21552Input: 21553\begin{center} 21554 \tt 21555 dayofweek(1,10,2014) 21556\end{center} 21557Output: 21558\begin{center} 21559 \tt 21560 3 21561\end{center} 21562This means that 1 October, 2014 was a Wednesday. 21563 21564\chapter{Metric properties of curves} 21565 21566\section{The center of curvature} 21567 21568Let $\Gamma$ be a curve in space parameterized by a 21569continuously differentiable function, and $M_0$ be a point on the 21570curve. The curve will have an arclength parameterization; namely, it 21571can be parameterized by a function $M(s)$, where $M(0) = M_0$ and $|s|$ 21572is the length of the curve from $M_0$ to $M(s)$, in the direction of 21573the curve if $s>0$ and the opposite direction if $s<0$. 21574 21575For such a $\Gamma$, the vector $T(s) = M'(s)$ will be the unit tangent 21576to the curve at $M(s)$, and $N(s) = T'(s)$ will be perpendicular to 21577the tangent. The circle through $M(s)$ with center at $M(s) + N(s)$ 21578is called the \emph{osculating circle} to $\Gamma$ at $M(s)$. 21579Informally, the osculating circle is the circle through $M(s)$ which 21580most closely approximates $\Gamma$. The set of all centers of 21581curvature is another curve, called the \emph{evolute} of $\Gamma$. 21582 21583The radius of the osculating circle is $|N(s)|$ and is called the 21584\emph{radius of curvature} of $\Gamma$ at $M(s)$. The reciprocal of 21585this is called the \emph{curvature} of $\Gamma$ at $M(s)$. 21586 21587\section{Computing the curvature and related values: 21588\texttt{curvature, osculating\_circle, 21589evolute}}\index{curvature}\index{osculating\_circle}\index{evolute} 21590 21591The \texttt{curvature} command takes two arguments and an optional 21592third argument. The first 21593argument is a curve, and the second argument is a point on the curve. 21594If the curve is given by a parameterization, the second argument is the 21595parameter, and an optional third argument is a value of the parameter.\\ 21596\texttt{curvature} returns the curvature of the curve at 21597the given point.\\ 21598Input: 21599\begin{center} 21600 \tt 21601 curvature(plot(x\^{}2),point(1,1)) 21602\end{center} 21603Output: 21604\begin{center} 21605 \tt 21606 2/25*sqrt(5) 21607\end{center} 21608Input: 21609\begin{center} 21610 \tt 21611 trigsimplify(curvature([5*cos(t),5*sin(t)],t)) 21612\end{center} 21613Output: 21614\begin{center} 21615 \tt 21616 1/5 21617\end{center} 21618Input: 21619\begin{center} 21620 \tt 21621 curvature([2*cos(t),3*sin(t)],t,pi/2) 21622\end{center} 21623Output: 21624\begin{center} 21625 \tt 21626 3/4 21627\end{center} 21628 21629The \texttt{osculating\_circle} command takes two or three arguments. 21630The arguments can be either a curve in the plane and a point on the 21631curve, or the parameterization of a curve in the plane, the parameter, 21632and a value of the parameter.\\ 21633\texttt{osculating\_circle} returns and draws the osculating circle.\\ 21634Input: 21635\begin{center} 21636 \tt 21637 osculating\_circle(plot(x\^{}2),point(1,1)) 21638\end{center} 21639Output: 21640\begin{center} 21641 \includegraphics[width=0.75\textwidth]{xcas-osculatingcircle.png} 21642\end{center} 21643Input: 21644\begin{center} 21645 \tt 21646 equation(osculating\_circle(plot(x\^{}2),point(1,1))) 21647\end{center} 21648Output: 21649\begin{center} 21650 \tt 21651 (x+4)\^{}2+(y-7/2)\^{}2=(125/4) 21652\end{center} 21653Input: 21654\begin{center} 21655 \tt 21656 equation(osculating\_circle([t\^{}2,t\^{}3],t,1)) 21657\end{center} 21658Output: 21659\begin{center} 21660 \tt 21661 (x+11/2)\^{}2+(y-16/3)\^{}2=(2197/36) 21662\end{center} 21663 21664The \texttt{evolute} command takes one or two arguments. 21665The arguments can be either a curve in the plane, or 21666the parameterization of a curve in the plane and the parameter.\\ 21667\texttt{evolute} draws and returns the evolute of the curve.\\ 21668Input: 21669\begin{center} 21670 \tt 21671 evolute(plot(x\^{}2)) 21672\end{center} 21673Output: 21674\begin{center} 21675 \includegraphics[width=0.75\textwidth]{xcas-evolute.png} 21676\end{center} 21677Input: 21678\begin{center} 21679 \tt 21680 equation(evolute(plot(x\^{}2))) 21681\end{center} 21682Output: 21683\begin{center} 21684 \tt 21685 27*x\^{}2-16*y\^{}3+24*y\^{}2-12*y+2=0 21686\end{center} 21687Input: 21688\begin{center} 21689 \tt 21690 equation(evolute([t\^{}2,t],t)) 21691\end{center} 21692Output: 21693\begin{center} 21694 \tt 21695 16*x\^{}3-24*x\^{}2+12*x-27*y\^{}2-2=0 21696\end{center} 21697 21698\chapter{Graphs}\label{sec:plot} 21699 21700\section{Generalities} 21701 21702Most graph instructions take expressions as arguments. A few 21703exceptions (mostly Maple-compatibility instructions) also accept 21704functions. 21705Some optional arguments, like {\tt color, thickness}, can be used as optional 21706attributes in all graphic instructions. They are described below. 21707 21708If a graph depends on a user-defined function, you may want to define 21709the function when the parameter is a formal variable. For this, it 21710can be useful to test the type of the parameter while the function is 21711being defined. 21712 21713For example, suppose \texttt{f} and \texttt{g} are defined by\\ 21714\begin{verbatim} 21715f(x):= { 21716 if (type(x)!=DOM_FLOAT) return 'f'(x); 21717 while(x>0){ x--; } 21718 return x; 21719} 21720\end{verbatim} 21721and 21722\begin{verbatim} 21723g(x):= { 21724 while(x>0){ x--; } 21725 return x; 21726}:; 21727\end{verbatim} 21728Graphing these, with\\ 21729Input: 21730\begin{center} 21731 \tt 21732 F := plotfunc(f(x)) 21733\end{center} 21734Input: 21735\begin{center} 21736 \tt 21737 G := plotfunc(g(x)) 21738\end{center} 21739will both produce the same graph. However, here the graphic 21740\texttt{G} won't be reusable. Entering\\ 21741Input: 21742\begin{center} 21743 \tt 21744 F 21745\end{center} 21746reproduces the graph, but entering\\ 21747Input: 21748\begin{center} 21749 \tt 21750 G 21751\end{center} 21752produces the error\\ 21753Output: 21754\begin{center} 21755 \tt 21756 "Unable to eval test in loop : x>0.0 Error: Bad Argument Value Error: 21757 Bad Argument Value" 21758\end{center} 21759Internally, \texttt{F} and \texttt{G} contain the formal expressions 21760\texttt{f(x)} and \texttt{g(x)}, respectively. When \texttt{Xcas} 21761tries to evaluate \texttt{F} and \texttt{G}, \texttt{x} has no value 21762and so the test \texttt{x > 0} produces an error in \texttt{g(x)}, but 21763the line \verb|if (type(x)!=DOM_FLOAT) return 'f'(x);| avoids this 21764problem in \texttt{f(x)}. 21765 21766\section{The graphic screen} 21767 21768A graphic screen, either two- or three-dimensional as appropriate, 21769automatically opens in response to a graphic command. 21770Alternatively, a graphic screen with its own command line will open 21771with keystrokes; \texttt{Alt-g} for a two-dimensional screen and 21772\texttt{Alt-h} for a three-dimensional screen. 21773The graphic screen will have an array of buttons at the top right. 21774\begin{itemize} 21775 \item There will be red arrows for moving the image in the $x$ direction. 21776 \item There will be green arrows for moving the image in the $y$ 21777 direction. 21778 \item There will be blue arrows for zooming in and out in a 21779 two-dimensional screen, and moving the image in the $z$ direction in 21780 a three-dimensional screen. 21781 \item There will be \texttt{in} and \texttt{out} buttons for zooming 21782 in and out. 21783 \item There will be a \texttt{\_|\_} button to orthonormalize the 21784 graphic. 21785 \item There will be a \texttt{$\blacktriangleright$|} button to 21786 start and stop animations. 21787 \item There will be an \texttt{auto} button to do automatic scaling. 21788 \item There will be a \texttt{cfg} button which will bring up a 21789 configuration screen (see XXXX). 21790 \item There will be an \texttt{M} button which is a menu. The menu 21791 has submenus: 21792 \begin{itemize} 21793 \item \texttt{View} which has entries which do the same as the 21794 buttons. 21795 \item \texttt{Trace} for working with traces. 21796 \item \texttt{Animation} for working with animations. 21797 \item \texttt{3-d} for working with three-dimensional graphics. 21798 \item \texttt{Export/Print} to export and print the graphic. 21799 \end{itemize} 21800\end{itemize} 21801 21802The image can also be moved in the screen by clicking and dragging with the 21803mouse. Scrolling with the mouse will also zoom the images. 21804 21805\section{Graph and geometric objects attributes} 21806There are two kinds of attributes: global attributes of a graphic 21807scene and individual attributes. 21808 21809\subsection{Individual attributes} 21810\index{color@{\it color}|textbf} 21811\index{display@{\it display}|textbf} 21812\index{black@\texttt{black}} 21813\index{white@\texttt{white}} 21814\index{red@\texttt{red}} 21815\index{blue@\texttt{blue}} 21816\index{green@\texttt{green}} 21817\index{magenta@\texttt{magenta}} 21818\index{cyan@\texttt{cyan}} 21819\index{yellow@\texttt{yellow}} 21820\index{rhombus\_point@\texttt{rhombus\_point}} 21821\index{plus\_point@\texttt{plus\_point}} 21822\index{square\_point@\texttt{square\_point}} 21823\index{cross\_point@\texttt{cross\_point}} 21824\index{triangle\_point@\texttt{triangle\_point}} 21825\index{star\_point@\texttt{star\_point}} 21826\index{point\_point@\texttt{point\_point}} 21827\index{invisible\_point@\texttt{invisible\_point}} 21828\index{point\_width\_1@\texttt{point\_width\_1}} 21829\index{point\_width\_2@\texttt{point\_width\_2}} 21830\index{point\_width\_3@\texttt{point\_width\_3}} 21831\index{point\_width\_4@\texttt{point\_width\_4}} 21832\index{point\_width\_5@\texttt{point\_width\_5}} 21833\index{point\_width\_6@\texttt{point\_width\_6}} 21834\index{point\_width\_7@\texttt{point\_width\_7}} 21835\index{line\_width\_1@\texttt{line\_width\_1}} 21836\index{line\_width\_2@\texttt{line\_width\_2}} 21837\index{line\_width\_3@\texttt{line\_width\_3}} 21838\index{line\_width\_4@\texttt{line\_width\_4}} 21839\index{line\_width\_5@\texttt{line\_width\_5}} 21840\index{line\_width\_6@\texttt{line\_width\_6}} 21841\index{line\_width\_7@\texttt{line\_width\_7}} 21842\index{dash\_line@\texttt{dash\_line}} 21843\index{solid\_line@\texttt{solid\_line}} 21844\index{dashdot\_line@\texttt{dashdot\_line}} 21845\index{dashdotdot\_line@\texttt{dashdotdot\_line}} 21846\index{cap\_flat\_line @\texttt{cap\_flat\_line }} 21847\index{cap\_square\_line@\texttt{cap\_square\_line}} 21848\index{cap\_round\_line@\texttt{cap\_round\_line}} 21849\index{quandrant1@\texttt{quandrant1}} 21850\index{quandrant2@\texttt{quandrant2}} 21851\index{quandrant3@\texttt{quandrant3}} 21852\index{quandrant4@\texttt{quandrant4}} 21853\index{hidden\_name@\texttt{hidden\_name}} 21854\index{filled@\texttt{filled}} 21855\index{gl\_texture@\texttt{gl\_texture}} 21856\index{gl\_material@\texttt{gl\_material}} 21857 21858 21859Graphic attributes are optional arguments of the 21860form {\tt display=value}, they must be given 21861as the last argument of a graphic instruction. Attributes 21862are ordered in several categories: color, point shape, point width, 21863line style, line thickness, legend value, position and presence. 21864In addition, surfaces may be filled or not, 3-d surfaces 21865may be filled with a texture, 3-d objects may also have properties 21866with respect to the light. 21867Attributes of different categories 21868may be added, e.g. \\ 21869{\tt plotfunc($x^2+y^2$,[x,y],display=red+line\_width\_3+filled)} 21870\begin{itemize} 21871\item Colors {\tt display=} or {\tt color=} 21872\begin{itemize} 21873\item {\tt black}, {\tt white}, {\tt red}, {\tt blue}, {\tt green}, 21874{\tt magenta}, {\tt cyan}, {\tt yellow}, 21875\item a numeric value between 0 and 255, 21876\item a numeric value between 256 and 256+7*16+14 for a color of the 21877rainbow, 21878\item any other numeric value smaller than 65535, the rendering 21879is not guaranteed to be portable. 21880\end{itemize} 21881\item Point shapes {\tt display=} one of the following value 21882{\tt rhombus\_point plus\_point square\_point cross\_point 21883triangle\_point star\_point point\_point invisible\_point} 21884\item Point width: {\tt display=} one of the following value 21885{\tt point\_width\_n} where {\tt n} is an 21886integer between 1 and 7 21887\item Line thickness: {\tt thickness=n} 21888or {\tt display=line\_width\_n} where {\tt n} is an 21889integer between 1 and 7 or 21890\item Line shape: {\tt display=} one of the following values 21891{\tt dash\_line solid\_line dashdot\_line dashdotdot\_line 21892 cap\_flat\_line cap\_square\_line cap\_round\_line } 21893\item Legend, value: {\tt legend="legendname"}; 21894 position: {\tt display=} one of 21895{\tt quandrant1 quadrant2 quadrant3 quadrant4} 21896corresponding to the position of the legend of the object 21897(using the trigonometric plane conventions). 21898The legend is not displayed if the attribute 21899{\tt display=hidden\_name} is added 21900\item {\tt display=filled} specifies that surfaces will be filled, 21901\item {\tt gl\_texture="picture\_filename"} is used to fill 21902a surface with a texture. 21903Cf. the interface manual for a more complete 21904description and for {\tt gl\_material=} options. 21905\end{itemize} 21906{\bf Examples}\\ 21907Input : 21908\begin{center}{\tt polygon(-1,-i,1,2*i,legend="P")}\end{center} 21909Input : 21910\begin{center}{\tt point(1+i,legend="hello")}\end{center} 21911Input : 21912\begin{center}{\tt A:=point(1+i);B:=point(-1);display(D:=droite(A,B),hidden\_name)}\end{center} 21913Input : 21914\begin{center}{\tt color(segment(0,1+i),red)}\end{center} 21915Input : 21916\begin{center}{\tt segment(0,1+i,color=red)}\end{center} 21917 21918\subsection{Global attributes} 21919\index{title@\textit{title}} 21920\index{labels@\textit{labels}} 21921\index{gl\_x\_axis\_name@\textit{gl\_x\_axis\_name}} 21922\index{gl\_y\_axis\_name@\textit{gl\_y\_axis\_name}} 21923\index{gl\_z\_axis\_name@\textit{gl\_z\_axis\_name}} 21924\index{gl\_x\_axis\_unit@\textit{gl\_x\_axis\_unit}} 21925\index{gl\_y\_axis\_unit@\textit{gl\_y\_axis\_unit}} 21926\index{gl\_z\_axis\_unit@\textit{gl\_z\_axis\_unit}} 21927\index{legend@\textit{legend}} 21928\index{axes@\textit{axes}} 21929\index{gl\_texture@\textit{gl\_texture}} 21930\index{gl\_x@\textit{gl\_x}} 21931\index{gl\_y@\textit{gl\_y}} 21932\index{gl\_z@\textit{gl\_z}} 21933\index{gl\_x\_tick@\textit{gl\_x\_tick}} 21934\index{gl\_y\_tick@\textit{gl\_y\_tick}} 21935\index{gl\_z\_tick@\textit{gl\_z\_tick}} 21936\index{gl\_shownames@\textit{gl\_shownames}} 21937\index{gl\_rotation@\textit{gl\_rotation}} 21938\index{gl\_quaternion@\textit{gl\_quaternion}} 21939 21940These attributes are shared by all objects of the same scene 21941\begin{itemize} 21942\item {\tt title="titlename"} defines the title 21943\item {\tt labels=["xname","yname","zname"]}: names of the $x,y,z$ 21944axis 21945\item {\tt gl\_x\_axis\_name="xname"}, {\tt gl\_y\_axis\_name="yname"}, 21946{\tt gl\_z\_axis\_name=""}: individual definitions 21947of the names of the $x,y,z$ axis 21948\item {\tt legend=["xunit","yunit","zunit"]}: units for the 21949$x,y,z$ axis 21950\item {\tt gl\_x\_axis\_unit="xunit"}, {\tt gl\_y\_axis\_unit="yunit"}, 21951{\tt gl\_z\_axis\_unit=""}: individual definition 21952of the units of the $x,y,z$ axis 21953\item {\tt axes=true or false} show or hide axis 21954\item {\tt gl\_texture="filename"}: background image 21955\item {\tt gl\_x=xmin..xmax}, {\tt gl\_y=ymin..ymax}, 21956{\tt gl\_z=zmin..zmax}: set the graphic configuration 21957(do not use for interactive scenes) 21958\item {\tt gl\_xtick=}, {\tt gl\_ytick=}, {\tt gl\_ztick=}: 21959set the tick mark for the axis 21960\item {\tt gl\_shownames=true or false}: show or hide objects names 21961\item {\tt gl\_rotation=[x,y,z]}: defines the rotation axis 21962for the animation rotation of 3-d scenes. 21963\item {\tt gl\_quaternion=[x,y,z,t]}: defines the quaternion 21964for the visualization in 3-d scenes (do not use for interactive 21965scenes) 21966\item a few other OpenGL light configuration options are 21967available but not described here. 21968\end{itemize} 21969{\bf Examples}\\ 21970Input : 21971\begin{center}{\tt legend=["mn","kg"]}\end{center} 21972Input : 21973\begin{center}{\tt title="median\_line";triangle(-1-i,1,1+i);median\_line(-1-i,1,1+i);median\_line(1,-1-i,1+i);median\_line(1+i,1,-1-i)}\end{center} 21974Input : 21975\begin{center}{\tt labels=["u","v"];plotfunc(u+1,u)}\end{center} 21976 21977\section{Graph of a function : {\tt plotfunc funcplot DrawFunc Graph}}\index{plotfunc|textbf}\index{funcplot|textbf}\index{DrawFunc|textbf}\index{Graph|textbf}\index{xstep@{\sl xstep}}\index{ystep@{\sl ystep}}\index{zstep@{\sl zstep}}\index{nstep@{\sl nstep}} 21978 21979\subsection{2-d graph}\label{sec:plotfunc} 21980\noindent{\tt plotfunc(f(x),x)} draws the graph of $y=f(x)$ for $x$ in 21981the default interval, 21982{\tt plotfunc(f(x),x=a..b)} draws the graph of $y=f(x)$ for $a\leq x\leq b$. 21983{\tt plotfunc} accepts an optional \verb|xstep=...| argument to 21984specify the discretization step in $x$.\\ 21985Input : 21986\begin{center}{\tt plotfunc(x\verb|^|2-2)}\end{center} 21987or : 21988\begin{center}{\tt plotfunc(a\verb|^|2-2,a=-1..2)}\end{center} 21989Output : 21990\begin{center}{\tt the graph of y=x\verb|^|2-2}\end{center} 21991Input : 21992\begin{center}{\tt plotfunc(x\verb|^|2-2,x,xstep=1)}\end{center} 21993Output : 21994\begin{center}{\tt a polygonal line which is a bad representation of y=x\verb|^|2-2 }\end{center} 21995It is also possible to specify the number of points used for the 21996representation of the function with \verb|nstep=| instead of \verb|xstep=|. 21997For example, input~: 21998\begin{center}{\tt plotfunc(x\verb|^|2-2,x=-2..3,nstep=30)}\end{center} 21999 22000\subsection{3-d graph}\label{sec:plotfunc3} 22001\noindent{\tt plotfunc} takes two main arguments : an expression of two 22002variables or a list of several expressions of two variables and the list of 22003these two variables, where each variable may be replaced by 22004an equality variable=interval to specify the range for this variable 22005(if not specified, default values are taken from the graph configuration). 22006{\tt plotfunc} accepts two optional arguments to specify 22007the discretization step in $x$ and in $y$ by 22008{\tt xstep=...} and {\tt ystep=...}. 22009Alternatively one can specify the number of points used for the 22010representation of the function with \verb|nstep=| (instead of \verb|xstep| and 22011{\tt ystep}).\\ 22012{\tt plotfunc} draws the surface(s) defined by $z=$ the first argument.\\ 22013Input : 22014\begin{center}{\tt plotfunc( x\verb|^|2+y\verb|^|2,[x,y])}\end{center} 22015Output : 22016\begin{center}{\tt A 3D graph of z=x\verb|^|2+y\verb|^|2}\end{center} 22017Input : 22018\begin{center}{\tt plotfunc(x*y,[x,y]) }\end{center} 22019Output : 22020\begin{center}{\tt The surface z=x*y, default ranges}\end{center} 22021Input : 22022\begin{center}{\tt plotfunc([x*y-10,x*y,x*y+10],[x,y]) }\end{center} 22023Output : 22024\begin{center}{\tt The surfaces z=x*y-10, z=x*y and z=x*y+10}\end{center} 22025Input : 22026\begin{center}{\tt plotfunc(x*sin(y),[x=0..2,y=-pi..pi]) }\end{center} 22027Output : 22028\begin{center}{\tt The surface $z=x*y$ for the specified ranges}\end{center} 22029Now an example where we specify the $x$ and $y$ discretization step 22030with \verb|xstep| and \verb|ystep|.\\ 22031Input : 22032\begin{center} 22033{\tt plotfunc(x*sin(y),[x=0..2,y=-pi..pi],xstep=1,ystep=0.5) }\end{center} 22034Output : 22035\begin{center}{\tt A portion of surface $z=x*y$}\end{center} 22036Alternatively we may specify 22037the number of points used for the representation of the 22038function with \verb|nstep| instead of \verb|xstep| and \verb|ystep|.\\ 22039Input~: 22040\begin{center}{\tt plotfunc(x*sin(y),[x=0..2,y=-pi..pi],nstep=300)}\end{center} 22041Output : 22042\begin{center}{\tt A portion of surface $z=x*y$}\end{center} 22043{\bf Remarks} 22044\begin{itemize} 22045\item 22046Like any 3-d scene, the viewpoint may be modified by rotation 22047around the {\tt x} axis, the {\tt y} axis or the 22048{\tt z} axis, either by dragging the mouse inside the graphic 22049window (push the mouse outside the parallelepiped used for 22050the representation), or with the shortcuts 22051{\tt x}, {\tt X}, {\tt y}, {\tt Y}, {\tt z} and {\tt Z}. 22052\item 22053If you want to print a graph or get a \LaTeX\ translation, use the graph 22054menu\\ 22055{\tt Menu$\blacktriangleright$print$\blacktriangleright$Print(with 22056 Latex)} 22057\end{itemize} 22058 22059\subsection{3-d graph with rainbow colors}\label{sec:plotfunc3d} 22060\noindent{\tt plotfunc} represents a pure imaginary expression {\tt i*E} 22061of two variables with a rainbow color depending 22062on the value of {\tt z=E}. This gives an easy way to 22063find points having the same third coordinate.\\ 22064The first arguments of {\tt plotfunc} must be {\tt i*E} instead of {\tt E}, 22065the remaining arguments are the same 22066as for a real 3-d graph (cf \ref{sec:plotfunc3}) 22067Input : 22068\begin{center}{\tt plotfunc(i*x*sin(y),[x=0..2,y=-pi..pi]) }\end{center} 22069Output : 22070\begin{center}{\tt A piece of the surface $z=x*\sin(y)$ with rainbow colors}\end{center} 22071{\bf Remark}\\ 22072 If you want the graphic in LaTeX, you have to use :\\ 22073{\tt Menu$\blacktriangleright$print$\blacktriangleright$Print(with Latex)}. 22074 22075\subsection{4-d graph.}\label{sec:plotfunc4} 22076\noindent{\tt plotfunc} represents a complex expression {\tt E} 22077(such that {\tt re(E)} is not identically 0 on the discretization mesh) 22078by the surface {\tt z=abs(E)} where {\tt arg(E)} defines the color 22079from the rainbow. This gives an easy way to 22080see the points having the same argument. 22081Note that if {\tt re(E)==0} on the discretization mesh, 22082it is the surface {\tt z=E/i} that is represented with rainbow colors 22083(cf \ref{sec:plotfunc3d}).\\ 22084The first argument of {\tt plotfunc} is {\tt E}, 22085the remaining arguments are the same 22086as for a real 3-d graph (cf \ref{sec:plotfunc3}).\\ 22087Input : 22088\begin{center}{\tt plotfunc((x+i*y)\verb|^|2,[x,y])}\end{center} 22089Output : 22090\begin{center}{\tt A graph 3D of z=abs((x+i*y)\verb|^|2 with the same color for 22091points having the same argument}\end{center} 22092Input : 22093\begin{center}{\tt plotfunc((x+i*y)\verb|^|2x,[x,y], display=filled)}\end{center} 22094Output : 22095\begin{center}{\tt The same surface but filled}\end{center} 22096We may specify the range of variation of $x$ and $y$ and the number of 22097discretization points.\\ 22098Input : 22099\begin{center}{\tt plotfunc((x+i*y)\verb|^|2,[x=-1..1,y=-2..2], nstep=900,display=filled)}\end{center} 22100Output : 22101\begin{center}{\tt The specified part of the surface with $x$ between -1 and 1, $y$ between -2 and 2 and with 900 points}\end{center} 22102 22103\section{2d graph for Maple compatibility : {\tt plot}} 22104\index{plot} \label{sec:plot2d} 22105\noindent{\tt plot(f(x),x)} draws the graph of $y=f(x)$. 22106The second argument may specify the range of values {\tt 22107 x=xmin..xmax}. One can also plot a function instead of an 22108expression using the syntax {\tt plot(f,xmin..xmax)}. 22109{\tt plot} accepts an optional argument to specify 22110the step used in $x$ for the discretization with 22111\verb|xstep=| or the number of points of the discretization 22112with \verb|nstep=|.\\ 22113Input : 22114\begin{center}{\tt plot(x\verb|^|2-2,x)}\end{center} 22115Output : 22116\begin{center}{\tt the graph of y=x\verb|^|2-2}\end{center} 22117Input : 22118\begin{center}{\tt plot(x\verb|^|2-2,xstep=1)}\end{center} 22119or : 22120\begin{center}{\tt plot(x\verb|^|2-2,x,xstep=1)}\end{center} 22121Output : 22122\begin{center}{\tt a polygonal line which is a bad representation of 22123 y=x\verb|^|2-2 }\end{center} 22124Input! 22125\begin{center}{\tt plot(x\verb|^|2-2,x=-2..3,nstep=30)}\end{center} 22126 22127 22128\section{3d surfaces for Maple compatibility {\tt plot3d}}\index{plot3d} 22129\noindent{\tt plot3d} takes three arguments : a function of two variables or 22130an expression of two variables or a list of three functions of two variables 22131or a list of three expressions of two variables and the names of these two 22132variables with an optional range (for expressions) or the ranges 22133(for functions).\\ 22134{\tt plot3d(f(x,y),x,y)} (resp. {\tt plot3d([f(u,v),g(u,v),h(u,v)],u,v)}) draws 22135the surface $z=f(x,y)$ (resp. $x=f(u,v),y=g(u,v),z=h(u,v)$). 22136The syntax {\tt plot3d(f(x,y),x=x0..x1,y=y0..y1)} or 22137{\tt plot3d(f,x0..x1,y0..y1)} specifies which part of surface 22138will be computed (otherwise default values are taken from the graph 22139configuration).\\ 22140Input : 22141\begin{center}{\tt plot3d(x*y,x,y)}\end{center} 22142Output : 22143\begin{center}{\tt The surface $z=x*y$}\end{center} 22144Input : 22145\begin{center}{\tt plot3d([v*cos(u),v*sin(u),v],u,v) }\end{center} 22146Output : 22147\begin{center}{\tt The cone $x=v*\cos(u),y=v*\sin(u),z=v$}\end{center} 22148Input : 22149\begin{center}{\tt plot3d([v*cos(u),v*sin(u),v],u=0..pi,v=0..3)}\end{center} 22150Output : 22151\begin{center}{\tt A portion of the cone $x=v*\cos(u),y=v*\sin(u),z=v$}\end{center} 22152 22153\section{Graph of a line and tangent to a graph} 22154\subsection{Draw a line : {\tt line}}\index{line}\label{sec:doite} 22155%{\bf See also :} \ref{sec:droite2} and \ref{sec:droite3} for line usage in 22156%geometry and see \ref{sec:axe2} and \ref{sec:axe3} for axis.\\ 22157\noindent {\tt line} takes as argument cartesian equation(s) : 22158\begin{itemize} 22159\item in 2D: one line equation, 22160\item in 3D: two plane equations. 22161\end{itemize} 22162{\tt line} defines and draws the corresponding line.\\ 22163Input : 22164\begin{center}{\tt line(2*y+x-1=0)}\end{center} 22165Output : 22166\begin{center}{\tt the line 2*y+x-1=0}\end{center} 22167Input : 22168\begin{center}{\tt line(y=1)}\end{center} 22169Output : 22170\begin{center}{\tt the horizontal line y=1}\end{center} 22171Input : 22172\begin{center}{\tt line(x=1)}\end{center} 22173Output : 22174\begin{center}{\tt the vertical line x=1}\end{center} 22175Input : 22176\begin{center}{\tt line(x+2*y+z-1=0,z=2)}\end{center} 22177Output : 22178\begin{center}{\tt the line x+2*y+1=0 in the plane z=2}\end{center} 22179Input : 22180\begin{center}{\tt line(y=1,x=1)}\end{center} 22181Output : 22182\begin{center}{\tt the vertical line crossing through (1,1,0)}\end{center} 22183{\bf Remark}\\ 22184{\tt line} defines an oriented line : 22185\begin{itemize} 22186\item when the 2D line is given by an equation, it is rewritten 22187as "left\_member-right\_member={\tt ax+by+c=0}", this determines 22188its normal vector {\tt [a,b]} and the orientation is given by the vector 22189{\tt [b,-a]}) (or its orientation is defined by the 3D cross product of its 22190normal vectors (with third coordinate 0) and the vector [0,0,1]).\\ 22191For example {\tt line(y=2*x)} defines the line {\tt -2x+y=0} with as direction 22192the vector {\tt [1,2]} (or {\tt cross([-2,1,0],[0,0,1])}={\tt [1,2,0]}). 22193\item when the 3D line is given by two plane equations, its 22194direction is defined by the cross product of the normals to the planes 22195(where the plane equation is rewritten as 22196"left\_member-right\_member={\tt ax+by+cz+d=0}", so that 22197the normal is {\tt [a,b,c]}).\\ 22198For example the {\tt line(x=y,y=z)} is the line {\tt x-y=0,y-z=0} and its 22199direction is :\\ 22200{\tt cross([1,-1,0],[0,1,-1])}={\tt [1,1,1]}. 22201\end{itemize} 22202 22203\subsection{Draw an 2D horizontal line : {\tt LineHorz}}\index{LineHorz} 22204\noindent {\tt LineHorz} takes as argument an expression $a$.\\ 22205 {\tt LineHorz} draws the horizontal line $y=a$.\\ 22206Input : 22207\begin{center}{\tt LineHorz(1)}\end{center} 22208Output : 22209\begin{center}{\tt the line y=1}\end{center} 22210 22211\subsection{Draw a 2D vertical line : {\tt LineVert}}\index{LineVert} 22212\noindent {\tt LineVert} takes as argument an expression $a$.\\ 22213 {\tt LineVert} draws the vertical line $x=a$.\\ 22214Input : 22215\begin{center}{\tt LineVert(1)}\end{center} 22216Output : 22217\begin{center}{\tt the line x=1}\end{center} 22218 22219\subsection{Tangent to a 2D graph : {\tt LineTan}}\index{LineTan} 22220\noindent {\tt LineTan} takes two arguments : an expression $E_x$ of the 22221variable $x$ and a value $x0$ of $x$.\\ 22222 {\tt LineTan} draws the tangent at $x=x0$ to the graph of $y=E_x$.\\ 22223Input : 22224\begin{center}{\tt LineTan(ln(x),1)}\end{center} 22225Output : 22226\begin{center}{\tt the line y=x-1}\end{center} 22227Input : 22228\begin{center}{\tt equation(LineTan(ln(x),1))}\end{center} 22229Output : 22230\begin{center}{\tt y=(x-1)}\end{center} 22231 22232\subsection{Tangent to a 2D graph : {\tt tangent}}\index{tangent|textbf}\label{sec:tangente} 22233%{\bf See also :} \ref{sec:tangent} for plane geometry and 22234%\ref{sec:tangent3} for 3D geometry.\\ 22235\noindent {\tt tangent} takes two arguments : a geometric object and a point 22236{\tt A}.\\ 22237{\tt tangent} draws tangent(s) to this geometric object crossing through 22238{\tt A}. If the geometric object is the graph {\tt G} of a 2D function, 22239the second argument is either, a real number {\tt x0}, or a 22240point {\tt A} on {\tt G}. In that case {\tt tangent} draws a tangent to this 22241graph {\tt G} crossing through the point {\tt A} or through the 22242point of abscissa {\tt x0}.\\ 22243For example, define the function {\tt g} 22244\begin{center} \verb|g(x):=x^2|\end{center} 22245then the graph {\tt G=\{(x,y)$\in \R^2$, y=g(x)\}} 22246of $g$ and a point $A$ on the graph $G$: 22247\begin{center} 22248{\tt G:=plotfunc(g(x),x);}\\ 22249{\tt A:=point(1.2,g(1.2));} 22250\end{center} 22251If we want to draw the tangent at the point {\tt A} to the graph {\tt 22252 G}, we will input: 22253\begin{center} 22254{\tt T:=tangent(G, A)} 22255\end{center} 22256or : 22257\begin{center} 22258{\tt T:=tangent(G, 1.2)} 22259\end{center} 22260For the equation of the tangent line, input : 22261\begin{center}{\tt equation(T)}\end{center} 22262 22263\subsection{Plot a line with a point and the slope: 22264\texttt{DrawSlp}}\index{DrawSlp} 22265 22266The \texttt{DrawSlp} command takes three arguments, real numbers 22267$a$, $b$ and $m$.\\ 22268\texttt{DrawSlp} returns and draws the line through the point $(a,b)$ 22269with slope $m$.\\ 22270Input: 22271\begin{center} 22272 \tt 22273 DrawSlp(2,1,-1) 22274\end{center} 22275Output: 22276\begin{center} 22277 \includegraphics[width=0.75\textwidth]{xcas-DrawSlp.png} 22278\end{center} 22279 22280 22281\subsection{Intersection of a 2D graph with the axis}\index{solve}\index{resoudre} 22282\begin{itemize} 22283\item The ordinate of the intersection of the graph of $f$ with the 22284$y$-axis is returned by : 22285\begin{center}{\tt f(0)}\end{center} 22286indeed the point of coordinates $(0,f(0))$ is the intersection point of the 22287graph of $f$ with the $y$-axis, 22288\item Finding the intersection of the graph of $f$ with the $x$-axis 22289requires solving the equation $f(x)=0$. \\ 22290If the equation is polynomial-like, {\tt solve} will find 22291the exact values of the abscissa of these points. Input: 22292\begin{center}{\tt solve(f(x),x)}\end{center} 22293Otherwise, we can find numeric approximations of these 22294abscissa. First look at the graph for an initial guess or a 22295range with an intersection and refine with {\tt fsolve}. 22296\end{itemize} 22297 22298\section{Graph of inequalities with 2 variables : {\tt plotinequation inequationplot}} 22299\index{plotinequation|textbf} 22300\index{inequationplot|textbf} 22301 22302\noindent{\tt plotinequation([f1(x,y)<a1,...fk(x,y)<ak],[x=x1..x2,y=y1..y2])} 22303draws the points of the plane whose coordinates 22304satisfy the inequalities of 2 variables : 22305\[ \left\{ \begin{array}{ccc} 22306f1(x,y) &<&a1 \\ 22307& ... & \\ 22308fk(x,y)&<&ak 22309\end{array}\right., \quad 22310x1\leq x \leq x2, y1 \leq y \leq y2 \] 22311Input : 22312\begin{center}{\tt plotinequation(x\verb|^|2-y\verb|^|2<3, [x=-2..2,y=-2..2],xstep=0.1,ystep=0.1)}\end{center} 22313Output : 22314\begin{center}{\tt the filled portion enclosing the origin and limited by the hyperbola x\verb|^|2-y\verb|^|2=3}\end{center} 22315Input : 22316\begin{center}{\tt plotinequation([x+y>3,x\verb|^|2<y], [x-2..2,y=-1..10],xstep=0.2,ystep=0.2)}\end{center} 22317Output : 22318\begin{center}{\tt the filled portion of the plane defined by -2<x<2,y<10,x+y>3,y>x\verb|^|2}\end{center} 22319Note that if the ranges for $x$ and $y$ are not specified, 22320{\tt Xcas} takes the default values of 22321{\tt X-,X+,Y-,Y+} defined in the general graphic configuration 22322({\tt Cfg$\blacktriangleright$Graphic configuration}). 22323 22324\section{The area under a curve: \texttt{area}} 22325\index{area} 22326 22327The \texttt{area} command takes four arguments; an expression $f(x)$, 22328a range for the variable $x=a..b$, an integer $n$, and the name of the 22329approximation method. The approximation method can be one of 22330\begin{itemize} 22331 \item \texttt{trapezoid} 22332 \item \texttt{left\_rectangle} 22333 \item \texttt{right\_rectangle} 22334 \item \texttt{middle\_point} 22335 \item \texttt{simpson} 22336 \item \texttt{rombergt} (Romberg with the trapezoid method) 22337 \item \texttt{rombergm} (Romberg with the midpoint method) 22338 \item \texttt{gauss15} (The 15 point Gaussian quadrature) 22339\end{itemize} 22340\texttt{area} returns an approximation to the area under the graph 22341over the given interval, using the specified method with $n$ 22342subdivisions (or $2^n$ subdivisions for \texttt{rombert}, 22343\texttt{rombergm} and \texttt{gauss15}).\\ 22344Input: 22345\begin{center} 22346 \tt 22347 area(x\^{}2,x=0..1,8,trapezoid) 22348\end{center} 22349Output: 22350\begin{center} 22351 \tt 22352 0.3359375 22353\end{center} 22354Input: 22355\begin{center} 22356 \tt 22357 area(x\^{}2,x=0..1,8,rombergm) 22358\end{center} 22359Output: 22360\begin{center} 22361 \tt 22362 0.333333333333 22363\end{center} 22364Input: 22365\begin{center} 22366 \tt 22367 area(x\^{}2,x=0..1,3,gauss15) 22368\end{center} 22369Output: 22370\begin{center} 22371 \tt 223720.333333333333 22373\end{center} 22374Input: 22375\begin{center} 22376 \tt 22377 area(x\^{}2,x=0..1) 22378\end{center} 22379Output: 22380\begin{center} 22381 \tt 22382 1/3 22383\end{center} 22384 22385\section{Graph of the area below a curve : {\tt plotarea areaplot}} 22386\index{plotarea|textbf}\index{areaplot|textbf}\index{rectangle\_droit@{\sl rectangle\_droit}|textbf}\index{rectangle\_gauche@{\sl rectangle\_gauche}|textbf}\index{trapeze@{\sl trapeze}|textbf}\index{point\_milieu@{\sl point\_milieu}|textbf} 22387 22388\begin{itemize} 22389\item With two arguments, {\tt plotarea} shades the area below a curve.\\ 22390{\tt plotarea(f(x),x=a..b)} draws the area below the curve $y=f(x)$ for 22391$a<x<b$, i.e. the portion of the plane defined by the inequalities $a<x<b$ and 22392$0<y<f(x)$ or $0>y>f(x)$ according to the sign of $f(x)$ .\\ 22393Input : 22394\begin{center}{\tt plotarea(sin(x),x=0..2*pi)}\end{center} 22395Output : 22396\begin{center}{\tt the portion of plane locates in the two arches of sin(x)}\end{center} 22397\item With four arguments, {\tt plotarea} represents a numeric approximation 22398of the area below a curve, according to a quadrature method from the 22399following list:\\ 22400{\tt trapezoid,rectangle\_left,rectangle\_right,middle\_point}.\\ 22401For example {\tt plotarea(f(x),x=a..b,n,trapezoid)} 22402draws the area of $n$ trapezoids : the 22403third argument is an integer $n$, and the fourth argument is the name of the 22404numeric method of integration when $[a,b]$ is cut into $n$ equal parts.\\ 22405Input : 22406\begin{center}{\tt plotarea((x\verb|^|2,x=0..1,5,trapezoid)}\end{center} 22407If you want to display the graph of the curve in contrast 22408(e.g. in bold red), input : 22409\begin{center}{\tt plotarea(x\verb|^|2,x=0..1,5,trapezoid); 22410plot(x\verb|^|2,x=0..1,display=red+line\_width\_3)}\end{center} 22411Output : 22412\begin{center}{\tt the 5 trapezoids used in the trapezoid method to approach the integral}\end{center} 22413Input : 22414\begin{center}{\tt plotarea((x\verb|^|2,x=0..1,5,middle\_point)}\end{center} 22415Or with the graph of the curve in bold red, input : 22416\begin{center}{\tt plotarea(x\verb|^|2,x=0..1,5,middle\_point); plot(x\verb|^|2,x=0..1,display=red+line\_width\_3)}\end{center} 22417Output : 22418\begin{center}{\tt the 5 rectangles used in the middle\_point method 22419 to approach the integral}\end{center} 22420\end{itemize} 22421 22422\section{Contour lines: {\tt plotcontour contourplot DrwCtour}}\index{plotcontour|textbf}\index{contourplot|textbf}\index{DrwCtour|textbf}\label{sec:plotcontour} 22423\noindent{\tt plotcontour(f(x,y),[x,y])} (or {\tt DrwCtour(f(x,y),[x,y])} or \\ 22424 {\tt contourplot(f(x,y),[x,y])}) 22425draws the contour lines of the surface defined by $z=f(x,y)$ for $z=-10$, 22426$z=-8$, .., $z=0$, $z=2$, .., $z=10$. You may specify the desired contour 22427lines by a list of values of $z$ given as third argument.\\ 22428Input : 22429\begin{center}{\tt plotcontour(x\verb|^|2+y\verb|^|2,[x=-3..3,y=-3..3],[1,2,3], display=[green,red,black]+[filled\$3])}\end{center} 22430Output : 22431\begin{center}{\tt the graph of the three ellipses x\verb|^|2-y\verb|^|2=n for n=1,2,3; the zones between these ellipses are filled with the color green,red or black}\end{center} 22432Input : 22433\begin{center}{\tt plotcontour(x\verb|^|2-y\verb|^|2,[x,y])}\end{center} 22434Output : 22435\begin{center}{\tt the graph of 11 hyperbolas x\verb|^|2-y\verb|^|2=n for n=-10,-8,..10}\end{center} 22436 22437If you want to draw the surface in 3-d representation, 22438input {\tt plotfunc(f(x,y),[x,y])}, see \ref{sec:plotfunc3}): 22439\begin{center}{\tt plotfunc( x\verb|^|2-y\verb|^|2,[x,y])}\end{center} 22440Output : 22441\begin{center}{\tt A 3D representation of z=x\verb|^|2+y\verb|^|2}\end{center} 22442 22443\section{2-d graph of a 2-d function with colors : 22444{\tt plotdensity densityplot}} 22445\index{plotdensity|textbf}\index{densityplot|textbf} 22446\noindent{\tt plotdensity(f(x,y),[x,y])} or {\tt densityplot(f(x,y),[x,y])} 22447draws the graph of $z=f(x,y)$ in the plane where the values of 22448$z$ are represented by the rainbow colors. The optional argument 22449{\tt z=zmin..zmax} specifies the range of $z$ corresponding to the 22450full rainbow, if it is not specified, it is deduced from the minimum 22451and maximum value of $f$ on the discretization. The discretization 22452may be specified by optional {\tt xstep=...} and {\tt ystep=...} 22453or {\tt nstep=...} arguments.\\ 22454Input : 22455\begin{center}{\tt plotdensity(x\verb|^|2-y\verb|^|2,[x=-2..2,y=-2..2], xstep=0.1,ystep=0.1)}\end{center} 22456Output : 22457\begin{center}{\tt A 2D graph where each hyperbola defined by 22458 x\verb|^|2-y\verb|^|2=z has a color from the rainbow}\end{center} 22459{\bf Remark} : A rectangle representing the scale of colors is 22460displayed below the graph. 22461 22462\section{Implicit graph: {\tt plotimplicit implicitplot}}\index{plotimplicit}\index{implicitplot}\index{unfactored} 22463\noindent{\tt plotimplicit} or {\tt implicitplot} draws curves or 22464surfaces defined by an implicit expression or equation. 22465If the option {\tt unfactored} is given as last argument, the 22466original expression is taken unmodified. Otherwise, 22467the expression is normalized, then replaced by the 22468factorization of the numerator of its normalization. 22469 22470Each factor of the expression corresponds to a component 22471of the implicit curve or surface. For each factor, 22472Xcas tests if it is of total 22473degree less or equal to 2, in that case {\tt conic} or 22474{\tt quadric} is called. Otherwise the numeric implicit solver 22475is called. 22476 22477Optional step and ranges arguments may be passed to the numeric 22478implicit solver, note that they are dismissed for each component 22479that is a conic or a quadric. 22480 22481\subsection{2D implicit curve}\label{sec:implicitplot} 22482\begin{itemize} 22483\item {\tt plotimplicit(f(x,y),x,y)} draws the graphic representation of the 22484curve defined by the implicit equation $f(x,y)=0$ when $x$ (resp. $y$) 22485is in {\tt WX-, WX+} (resp. in {\tt WY-, WY+}) defined by {\tt cfg}, 22486 22487\item {\tt plotimplicit(f(x,y),x=0..1,y=-1..1)} draws the graphic 22488representation of the curve defined by the implicit equation $f(x,y)=0$ 22489when $0\leq x \leq 1$ and $-1\leq y \leq 1$ 22490\end{itemize} 22491It is possible to add two arguments to specify the discretization 22492steps for $x$ 22493and $y$ with {\tt xstep=...} and {\tt ystep=...}.\\ 22494Input : 22495\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,x,y)}\end{center} 22496or : 22497\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,x,y,unfactored)}\end{center} 22498Output : 22499\begin{center}{\tt The unit circle}\end{center} 22500Input : 22501\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,x,y,xstep=0.2,ystep=0.3)}\end{center} 22502or : 22503\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,[x,y],xstep=0.2,ystep=0.3)}\end{center} 22504or : 22505\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,[x,y], xstep=0.2,ystep=0.3,unfactored)}\end{center} 22506Output : 22507\begin{center}{\tt The unit circle}\end{center} 22508Input : 22509\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,x=-2..2,y=-2..2, xstep=0.2,ystep=0.3)}\end{center} 22510Output : 22511\begin{center}{\tt The unit circle}\end{center} 22512 22513\subsection{3D implicit surface}\label{sec:implicitplot3} 22514\begin{itemize} 22515\item {\tt plotimplicit(f(x,y,z),x,y,z)} draws the graphic 22516representation of the surface defined by the implicit equation $f(x,y,z)=0$, 22517\item {\tt plotimplicit(f(x,y,z),x=0..1,y=-1..1,z=-1..1)} draws the surface 22518defined by the implicit equation $f(x,y,z)=0$, 22519where $0\leq x \leq 1$, $-1\leq y \leq 1$ and $-1\leq z \leq 1$. 22520\end{itemize} 22521It is possible to add three arguments to specify the discretization 22522steps used for $x$, $y$ and $z$ with {\tt xstep=...}, {\tt ystep=...} and 22523{\tt zstep=...}.\\ 22524Input : 22525\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2+z\verb|^|2-1,x,y,z, xstep=0.2,ystep=0.1,zstep=0.3)}\end{center} 22526Input : 22527\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2+z\verb|^|2-1,x,y,z, xstep=0.2,ystep=0.1,zstep=0.3,unfactored)}\end{center} 22528Output : 22529\begin{center}{\tt The unit sphere}\end{center} 22530Input : 22531\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2+z\verb|^|2-1,x=-1..1,y=-1..1,z=-1..1)}\end{center} 22532Output : 22533\begin{center}{\tt The unit sphere}\end{center} 22534 22535 22536\section{Parametric curves and surfaces : {\tt plotparam paramplot DrawParm}}\index{plotparam|textbf}\index{paramplot|textbf}\index{DrawParm|textbf} 22537\subsection{2D parametric curve } 22538\noindent {\tt plotparam([f(t),g(t)],t)} 22539or {\tt plotparam(f(t)+i*g(t),t)} (resp. 22540{\tt plotparam(f(t)+i*g(t),t=t1..t2)}) 22541draws the parametric representation of the curve 22542defined by $x=f(t),y=g(t)$ 22543with the default range of values of $t$ (resp. for $t1 \leq t\leq t2$).\\ 22544The default range of values is taken as specified 22545in the graphic configuration ({\tt t-} and {\tt t+}, 22546cf. \ref{ssec:confgraph}). 22547{\tt plotparam} accepts an optional argument to specify the discretization 22548step for $t$ with {\tt tstep=}.\\ 22549Input : 22550\begin{center}{\tt plotparam(cos(x)+i*sin(x),x) }\end{center} 22551or : 22552\begin{center}{\tt plotparam([cos(x),sin(x)],x) }\end{center} 22553Output : 22554\begin{center}{\tt The unit circle}\end{center} 22555If in the graphic configuration {\tt t} goes from -4 to 1, input : 22556\begin{center}{\tt plotparam(sin(t)+i*cos(t))}\end{center} 22557or : 22558\begin{center}{\tt plotparam(sin(t)+i*cos(t),t=-4..1) }\end{center} 22559or : 22560\begin{center}{\tt plotparam(sin(x)+i*cos(x),x=-4..1) }\end{center} 22561Output : 22562\begin{center}{\tt the arc (sin(-4)+i*cos(-4),sin(1)+i*cos(1)) of the unit circle}\end{center} 22563If in the graphic configuration {\tt t} goes from -4 to 1, input : 22564\begin{center}{\tt plotparam(sin(t)+i*cos(t),t,tstep=0.5)}\end{center} 22565or : 22566\begin{center}{\tt plotparam(sin(t)+i*cos(t),t=-4..1,tstep=0.5)}\end{center} 22567Output : 22568\begin{center}{\tt A polygon approaching the arc (sin(-4)+i*cos(-4),sin(1)+i*cos(1)) of the unit circle}\end{center} 22569 22570 22571 22572\subsection{3D parametric surface : {\tt plotparam paramplot DrawParm}}\index{plotparam}\index{paramplot}\index{DrawParm} 22573\noindent{\tt plotparam} takes two main arguments, 22574a list of three 22575expressions of two variables and the list of these variable names 22576where each variable name may be replaced by variable=interval 22577to specify the range of the parameters. 22578It accepts an optional argument to specify 22579the discretization steps of the parameters $u$ and $v$ with 22580{\tt ustep=...} and {\tt vstep=...}.\\ 22581{\tt plotparam([f(u,v),g(u,v),h(u,v)],[u,v])} draws the surface defined by the 22582first argument : $x=f(u,v),y=g(u,v),z=h(u,v)$, where $u$ and $v$ 22583ranges default to the graphic configuration.\\ 22584Input : 22585\begin{center}{\tt plotparam([v*cos(u),v*sin(u),v],[u,v])}\end{center} 22586Output : 22587\begin{center}{\tt The cone $x=v*\cos(u),y=v*\sin(u),z=v$}\end{center} 22588To specify the range of each parameters, replace each variable 22589by an equation variable=range, like this: 22590\begin{center}{\tt plotparam([v*cos(u),v*sin(u),v],[u=0..pi,v=0..3]) }\end{center} 22591Output : 22592\begin{center}{\tt A portion of the cone $x=v*\cos(u),y=v*\sin(u),z=v$}\end{center} 22593Input : 22594\begin{center}{\tt plotparam([v*cos(u),v*sin(u),v],[u=0..pi,v=0..3],ustep=0.5,vstep=0.5)}\end{center} 22595Output : 22596\begin{center}{\tt A portion of the cone $x=v*\cos(u),y=v*\sin(u),z=v$}\end{center} 22597 22598\section{Bezier curves: \texttt{bezier}}\index{bezier} 22599 22600The \texttt{bezier} command takes as argument a sequence \texttt{L} of 22601points.\\ 22602\texttt{bezier(L,plot)} plots the Bezier curve with the given control 22603points. 22604 22605If the points are $P_0,P_1,\dots,P_n$, the Bezier curve is the curve 22606parameterized by $\sum_{j=0}^{n} \binom{n,j} t^j (1-t)^{n-j} P_{j}$. 22607 22608\noindent 22609Input: 22610\begin{center} 22611 \tt 22612 bezier(1,1+i,2+i,3-i,plot) 22613\end{center} 22614Output: 22615\begin{center} 22616 \includegraphics[width=0.75\textwidth]{xcas-bezier1.png} 22617\end{center} 22618Input: 22619\begin{center} 22620 \tt 22621 bezier(point(0,0,0),point(1,1,0),point(0,1,1),plot) 22622\end{center} 22623Output: 22624\begin{center} 22625 \includegraphics[width=0.75\textwidth]{xcas-bezier2.png} 22626\end{center} 22627 22628To get the parameterization of the curve, you can use \texttt{parameq}.\\ 22629Input: 22630\begin{center} 22631 \tt 22632 parameq(bezier(1,1+i,2+i,3-i)) 22633\end{center} 22634Output: 22635\begin{center} 22636 \tt 22637 (1-t)\^{}3+3*t*(1-t)\^{}2*(1+i)+3*t\^{}2*(1-t)*(2+i)+t\^{}3*(3-i) 22638\end{center} 22639Input: 22640\begin{center} 22641 \tt 22642 parameq(bezier(point([0,0,0]),point([1,1,0]),point([0,1,1]))) 22643\end{center} 22644Output: 22645\begin{center} 22646 \tt 22647 point[2*t*(1-t),2*t*(1-t)+t\^{}2,t\^{}2] 22648\end{center} 22649 22650\section{Curve defined in polar coordinates : {\tt plotpolar polarplot DrawPol courbe\_polaire}}\index{plotpolar|textbf}\index{polarplot|textbf}\index{DrawPol|textbf}\index{courbe\_polaire|textbf} 22651\noindent Let $E_t$ be an expression depending on the variable $t$.\\ 22652{\tt plotpolar($E_t$,t)} draws the polar representation of the 22653curve defined by $\rho=E_t$ for $\theta=t$, that is 22654in cartesian coordinates the curve $(E_t \cos(t),E_t \sin(t))$. 22655The range of the parameter may be specified by replacing the second argument 22656by {\tt t=tmin..tmax}. The discretization parameter may be specified 22657by an optional {\tt tstep=...} argument.\\ 22658Input 22659\begin{center}{\tt plotpolar(t,t)}\end{center} 22660Output : 22661\begin{center}{\tt The spiral $\rho$=t is plotted}\end{center} 22662Input 22663\begin{center}{\tt plotpolar(t,t,tstep=1)}\end{center} 22664or : 22665\begin{center}{\tt plotpolar(t,t=0..10,tstep=1)}\end{center} 22666Output : 22667\begin{center}{\tt A polygon line approaching the spiral $\rho$=t is plotted}\end{center} 22668 22669\section{Graph of a recurrent sequence : {\tt plotseq seqplot graphe\_suite}}\index{plotseq}\index{seqplot}\index{graphe\_suite}\label{sec:plotseq} 22670\noindent Let $f(x)$ be an expression depending on the variable $x$ 22671(resp. $f(t)$ an expression depending on the variable $t$).\\ 22672{\tt plotseq($f(x)$,a,n)} (resp. {\tt plotseq($f(t)$,t=a,n)}) draws the line 22673$y=x$, the graph of $y=f(x)$ (resp. $y=f(t)$) and the $n$ first terms of the 22674recurrent sequence defined by : $u_0=a,\ \ u_n=f(u_{n-1})$. 22675The $a$ value may be replaced by a list of 3 elements, $[a,x_-,x_+]$ 22676where $x_-..x_+$ will be passed as $x$ range for the graph computation.\\ 22677Input : 22678\begin{center}{\tt plotseq(sqrt(1+x),x=[3,0,5],5)}\end{center} 22679Output : 22680\begin{center}{\tt the graph of y=sqrt(1+x), of y=x and of the 5 first terms of the sequence u\_0=3 and u\_n=sqrt(1+u\_(n-1))}\end{center} 22681 22682\section{Tangent field : {\tt plotfield fieldplot}}\index{plotfield}\index{fieldplot} 22683\begin{itemize} 22684\item Let $f(t,y)$ be an expression depending on two variables $t$ and $y$, 22685then : 22686\begin{center} 22687{\tt plotfield(f(t,y),[t,y])} 22688\end{center} 22689draws the tangent field of the 22690differential equation $y'=f(t,y)$ where $y$ is a real variable and 22691where $t$ is the abscissa, 22692\item Let $V$ be 22693a vector of two expressions depending on 2 variables $x,y$ but 22694independent of the time $t$, then 22695\begin{center} 22696{\tt plotfield(V,[x,y])} 22697\end{center} 22698draws the vector field $V$, 22699\item The range of values of $t,y$ or of $x,y$ can be specified with\\ 22700{\tt t=tmin..tmax}, {\tt x=xmin..xmax}, {\tt y=ymin..ymax}\\ 22701in place of the variable name. 22702\item The discretization may be specified with optional 22703arguments {\tt xstep=...}, {\tt ystep=...}. 22704\end{itemize} 22705Input : 22706\begin{center}{\tt plotfield(4*sin(t*y),[t=0..2,y=-3..7]) }\end{center} 22707Output : 22708\begin{center}{\tt Segments with slope 4*sin(t*y), representing tangents, are plotting in different points}\end{center} 22709With two variables $x,y$, input : 22710\begin{center} 22711{\tt plotfield(5*[-y,x],[x=-1..1,y=-1..1]) } 22712\end{center} 22713 22714\section{Plotting a solution of a differential equation : {\tt plotode odeplot}}\index{plotode}\index{odeplot} 22715\noindent Let $f(t,y)$ be an expression depending on two variables 22716$t$ and $y$. 22717\begin{itemize} 22718\item {\tt plotode($f(t,y)$,[t,y],[t0,y0])} draws the solution of 22719the differential equation $y'=f(t,y)$ crossing through 22720the point {\tt (t0,y0)} (i.e. such that $y(t_0)=y_0$) 22721\item 22722By default, $t$ goes in both directions. The range of value of $t$ 22723may be specified by the optional argument 22724{\tt t=tmin..tmax}. 22725\item 22726We can also represent, in the space or in the plane, 22727the solution of a differential equation 22728$y'=f(t,y)$ where $y=(X,Y)$ is a vector of size 2. 22729Just replace $y$ by the variable names $X,Y$ 22730and the initial value $y_0$ by the two initial values of the 22731variables at time $t_0$. 22732\end{itemize} 22733Input : 22734\begin{center}{\tt plotode(sin(t*y),[t,y],[0,1]) }\end{center} 22735Output : 22736\begin{center}{The graph of the solution of y'=sin(t,y) crossing through the point (0,1)}\end{center} 22737Input~: 22738\begin{center} 22739{\tt S:=odeplot([h-0.3*h*p, 0.3*h*p-p], [t,h,p],[0,0.3,0.7])} 22740\end{center} 22741Output, the graph in the space of the solution of : 22742\[ [h,p]'=[h-0.3 h*p, 0.3 h*p-p] \quad [h,p](0)=[0.3,0.7] \] 22743To have a 2-d graph (in the plane), use the option 22744{\tt plane} 22745\begin{center} 22746{\tt S:=odeplot([h-0.3*h*p, 0.3*h*p-p], [t,h,p],[0,0.3,0.7],plane)} 22747\end{center} 22748To compute the values of the solution, see 22749the subsection \ref{ssec:odesolve}. 22750 22751\section{Interactive plotting of solutions of a differential equation : {\tt interactive\_plotode interactive\_odeplot}}\index{interactive\_plotode}\index{interactive\_odeplot} 22752\noindent Let $f(t,y)$ be an expression depending on two 22753variables $t$ and $y$.\\ 22754{\tt interactive\_plotode(f(t,y),[t,y])} draws the tangent field 22755of the differential equation $y'=f(t,y)$ in a new window. 22756In this window, one can click on a point to get the 22757plot of the solution of $y'=f(t,y)$ crossing through this point.\\ 22758You can further click to display 22759several solutions. To stop press 22760the {\tt Esc} key.\\ 22761Input : 22762\begin{center}{\tt interactive\_plotode(sin(t*y),[t,y]) }\end{center} 22763Output : 22764\begin{center}{\tt The tangent field is plotted with the 22765 solutions of y'=sin(t,y) crossing through the points defined by 22766 mouse clicks}\end{center} 22767 22768\section{Animated graphs (2D, 3D or "4D")} 22769{\tt Xcas} can display animated 2D, 3D or "4D" graphs. 22770This is done first by computing 22771a sequence of graphic objects, then after completion, 22772by displaying the sequence in a loop. 22773\begin{itemize} 22774\item To stop or start again the animation, click on the button 22775$\blacktriangleright \mid$ (at the left of {\tt Menu}). 22776\item 22777The display time of each graphic object is specified in {\tt animate} of the 22778graph configuration ({\tt cfg} button). Put a small time, 22779to have a fast animation. 22780\item 22781If {\tt animate} is {\tt 0}, the animation is frozen, 22782you can move in the sequence of objects one by one by clicking 22783on the mouse in the graphic scene. 22784\end{itemize} 22785 22786\subsection{Animation of a 2D graph~: {\tt animate}}\index{animate} 22787\noindent{\tt animate} can create a 2-d animation with graphs of functions 22788depending on a parameter. The parameter is specified as the 22789third argument of 22790{\tt animate}, the number of pictures as fourth argument with 22791{\tt frames=}\index{frames@{\sl frames}|textbf}number, 22792the remaining arguments are the same as those of the {\tt plot} command, 22793see section \ref{sec:plot2d}, p. \pageref{sec:plot2d}.\\ 22794Input : 22795\begin{center} 22796{\tt animate(sin(a*x),x=-pi..pi,a=-2..2,frames=10,color=red)} 22797\end{center} 22798Output : 22799\begin{center}{\tt a sequence of graphic representations of y=sin($a$x) for 2280011 values of $a$ between -2 and 2}\end{center} 22801 22802\subsection{Animation of a 3D graph~: {\tt animate3d}}\index{animate3d} 22803\noindent{\tt animate3d} can create a 3-d animation with 22804function graphs depending on a parameter. The parameter is specified as 22805the third argument of {\tt animate3d}, the number of pictures 22806as fourth argument with 22807{\tt frames=}\index{frames@{\sl frames}}number, the remaining arguments 22808are the same as those of the {\tt plotfunc} command, see 22809section \ref{sec:plotfunc3}, p. \pageref{sec:plotfunc3}.\\ 22810Input : 22811\begin{center} 22812{\tt animate3d(x\verb|^|2+a*y\verb|^|2,[x=-2..2,y=-2..2],a=-2..2, frames=10,display=red+filled)} 22813\end{center} 22814Output : 22815\begin{center}{\tt a sequence of graphic representations 22816 of z=x\verb|^|2+$a$*y\verb|^|2 for 11 values of $a$ between -2 and 2} 22817\end{center} 22818 22819\subsection{Animation of a sequence of graphic objects~: {\tt animation}}\index{animation} 22820\noindent{\tt animation} animates the representation of a 22821sequence of graphic objects 22822with a given display time. The sequence of objects depends most of 22823the time on a parameter and is defined using the {\tt seq} command 22824but it is not mandatory.\\ 22825{\tt animation} takes as argument the sequence of graphic objects.\\ 22826To define a sequence of graphic objects with {\tt seq}, 22827enter the definition of the graphic object (depending on 22828the parameter), the parameter name, its minimum value, its 22829 maximum value maximum and optionally a step value.\\ 22830Input : 22831\begin{center}{\tt animation(seq(plotfunc(cos(a*x),x),a,0,10))}\end{center} 22832Output : 22833\begin{center}{\tt The sequence of the curves defined by $y=\cos(ax)$, for $a=0,1,2..10$}\end{center} 22834Input : 22835\begin{center} 22836{\tt animation(seq(plotfunc(cos(a*x),x),a,0,10,0.5))} 22837\end{center} 22838or : 22839\begin{center} 22840{\tt animation(seq(plotfunc(cos(a*x),x),a=0..10,0.5))} 22841\end{center} 22842Output : 22843\begin{center}{\tt The sequence of the curves defined by $y=\cos(ax)$, for $a=0,0.5,1,1.5..10$ }\end{center} 22844Input : 22845\begin{center}{\tt animation(seq(plotfunc([cos(a*x),sin(a*x)],x=0..2*pi/a), a,1,10))}\end{center} 22846Output : 22847\begin{center}{\tt The sequence of two curves defined by $y=\cos(ax)$ and $y=\sin(ax)$, for $a=1..10$ and for $x=0..2\pi/a$ }\end{center} 22848Input : 22849\begin{center}{\tt animation(seq(plotparam([cos(a*t),sin(a*t)], t=0..2*pi),a,1,10))}\end{center} 22850Output : 22851\begin{center}{\tt The sequence of the parametric curves defined by $x=\cos(at)$ and $y=\sin(at)$, for $a=1..10$ and for $t=0..2\pi$ }\end{center} 22852Input : 22853\begin{center}{\tt animation(seq(plotparam([sin(t),sin(a*t)], t,0,2*pi,tstep=0.01),a,1,10))}\end{center} 22854Output : 22855\begin{center}{\tt The sequence of the parametric curves defined by $x=\sin(t),y=\sin(at)$, for $a=0..10$ and $t=0..2\pi$}\end{center} 22856Input : 22857\begin{center}{\tt animation(seq(plotpolar(1-a*0.01*t\verb|^|2, t,0,5*pi,tstep=0.01),a,1,10))}\end{center} 22858Output : 22859\begin{center}{\tt The sequence of the polar curves defined by $\rho=1-a*0.01*t^2$, for $a=0..10$ and $t=0..5\pi$}\end{center} 22860Input : 22861\begin{center}{\tt plotfield(sin(x*y),[x,y]); animation(seq(plotode(sin(x*y),[x,y],[0,a]),a,-4,4,0.5))}\end{center} 22862Output : 22863\begin{center}{\tt The tangent field of y'=sin(xy) and the sequence of the integral curves crossing through the point $(0,a)$ for $a$=-4,-3.5...3.5,4}\end{center} 22864Input : 22865\begin{center}{\tt animation(seq(display(square(0,1+i*a),filled),a,-5,5))}\end{center} 22866Output : 22867\begin{center}{\tt The sequence of the squares defined by the points 0 and 1+i*$a$ for $a=-5..5$}\end{center} 22868Input : 22869\begin{center}{\tt animation(seq(droite([0,0,0],[1,1,a]),a,-5,5))}\end{center} 22870Output : 22871\begin{center}{\tt The sequence of the lines defined by the points [0,0,0] and [1,1,$a$] for $a=-5..5$}\end{center} 22872Input : 22873\begin{center}{\tt animation(seq(plotfunc(x\verb|^|2-y\verb|^|a,[x,y]),a=1..3))}\end{center} 22874Output : 22875\begin{center}{\tt The sequence of the "3D" surface defined by $x^2-y^a$, for $a=1..3$ with rainbow colors}\end{center} 22876Input : 22877\begin{center}{\tt animation(seq(plotfunc((x+i*y)\verb|^|a,[x,y], display=filled),a=1..10)}\end{center} 22878Output : 22879\begin{center}{\tt The sequence of the "4D" surfaces defined by $(x+i*y)^a$, for $a=0..10$ with rainbow colors}\end{center} 22880 22881{\bf Remark} 22882We may also define the sequence with a program, 22883for example if we want to draw the 22884segments of length $1,\sqrt 2...\sqrt 20$ constructed with a 22885right triangle of side 1 and the previous segment 22886(note that there is a {\tt c:=evalf(..)} statement 22887to force approx. evaluation otherwise the computing time 22888would be too long) : 22889\begin{verbatim} 22890seg(n):={ 22891 local a,b,c,j,aa,bb,L; 22892 a:=1; 22893 b:=1; 22894 L:=[point(1)]; 22895 for(j:=1;j<=n;j++){ 22896 L:=append(L,point(a+i*b)); 22897 c:=evalf(sqrt(a^2+b^2)); 22898 aa:=a; 22899 bb:=b; 22900 a:=aa-bb/c; 22901 b:=bb+aa/c; 22902 } 22903 L; 22904} 22905\end{verbatim} 22906Then input : 22907\begin{center}{\tt animation(seg(20))}\end{center} 22908We see, each point, one to one with a display time that 22909depends of the {\tt animate} value in {\tt cfg}.\\ 22910or : 22911\begin{center}{\tt L:=seg(20); s:=segment(0,L[k])\$(k=0..20)}\end{center} 22912We see 21 segments. \\ 22913Then, input : 22914\begin{center}{\tt animation(s)}\end{center} 22915We see, each segment, one to one with a display time that 22916depends of the {\tt animate} value in {\tt cfg}. 22917 22918\chapter{Statistics}\label{sec:stat} 22919 22920\section{One variable statistics} 22921 22922\texttt{Xcas} has several functions to perform statistics; the data is 22923typically given as a list of numbers, such as \texttt{A := 22924[0,1,2,3,4,5,6,7,8,9,10,11]}. We will use this particular list in 22925several examples. Section \ref{sec:statmat} will discuss statistics on 22926matrices. 22927 22928\subsection{The mean: \texttt{mean}\index{mean}} 22929 22930Recall that the mean of a list $x_1,\dots,x_n$ is simply their numeric 22931average $(x_1 + \cdots + x_n)/n$. \texttt{Xcas} can calculate the 22932mean of a list of numbers with the \texttt{mean} command. 22933If you enter 22934\begin{center} 22935 \tt 22936 mean([1,2,3,4]) 22937\end{center} 22938then you will get 22939\begin{center} 22940 \tt 22941 5/2 22942\end{center} 22943since $(1+2+3+4)/4 = 5/2$. 22944If you give \texttt{mean} a matrix as an argument, then it will return a 22945list with the numeric average of each column; 22946\begin{center} 22947 \tt 22948 mean([[1,2,3],[5,6,7]]) 22949\end{center} 22950will return 22951\begin{center} 22952 \tt 22953 [3,4,5] 22954\end{center} 22955since $(1+5)/2 = 3$, $(2+6)/2 = 4$ and $(3 + 7)/2 = 5$. 22956 22957To get the weighted average of a list of numbers you can give 22958\texttt{mean} a second argument, which should be a list of the 22959weights. For example, 22960\begin{center} 22961 \tt 22962 mean([2,4,6,8],[2,2,3,3]) 22963\end{center} 22964will return 22965\begin{center} 22966 \tt 22967 27/5 22968\end{center} 22969since $(2\cdot 2 + 4\cdot 2 + 6\cdot 3 + 8\cdot 3)/(2 + 2 + 3 + 3) = 2297027/5$. 22971Similarly, you can find the weighted average of the columns of a 22972matrix by giving \texttt{mean} a second argument of a matrix of 22973weights. If you enter 22974\begin{center} 22975 \tt 22976 mean([[1,2],[3,4]],[[1,2],[2,1]]) 22977\end{center} 22978then you will get 22979\begin{center} 22980 \tt 22981 [7/3,8/3] 22982\end{center} 22983since $(1\cdot 1 + 3\cdot 2)/(1+2) = 7/3$ and $(2\cdot 2 + 4\cdot 1)/(2 + 229841) = 8/3$. 22985 22986\subsection{Variance and standard deviation: \texttt{variance}\index{variance} \texttt{stdev}\index{stdev}} 22987 22988The variance of a list of numbers measures how close the numbers are 22989to their mean by finding the average of the squares of the 22990differences between the numbers and the mean; specifically, 22991given a list of numbers $[x_1,\dots,x_n]$ with mean $\mu = (x_1 + 22992\cdots + x_n)/n$, the variance is 22993\[ 22994\frac{(x_1 - \mu)^2 + \dots + (x_n - \mu)^2}{n}. 22995\] 22996The squares help ensure that the numbers above the mean and those 22997below the mean don't cancel out. The variance can be computed with 22998the command \texttt{variance}, 22999 23000A potentially better way to measure how close numbers are to their 23001mean is the standard deviation, which is the square root of the 23002variance;. Note that if the list of numbers have units, then the 23003standard deviation will have the same unit. 23004The \texttt{stddev} function will compute the standard 23005deviation of a list of numbers. For example, 23006the list $[1,2,3,4]$ has mean $5/2$, and so 23007\texttt{stddev([1,2,3,4])} 23008will return 23009\begin{center} 23010 \tt 23011 2*sqrt(5)/4 23012\end{center} 23013since 23014\[ \sqrt{\frac{(1-5/2)^2 + (2-5/2)^2 + (3-5/2)^2 + (4-5/2)^2}{4}} = 23015\frac{2\sqrt{5}}{4} 23016\] 23017Like the mean, given a matrix, \texttt{stddev} will compute the 23018standard deviation of each column separately; 23019\begin{center} 23020 \tt 23021 stddev([[1,2],[3,6]]) 23022\end{center} 23023will compute 23024\begin{center} 23025 \tt 23026 [1,2] 23027\end{center} 23028Also, a second list (or matrix) as an argument will provide weights 23029when finding the standard deviation; 23030\begin{center} 23031 \tt 23032 stddev([1,2,3],[2,1,1]) 23033\end{center} 23034will return 23035\begin{center} 23036 \tt 23037 4*sqrt(11)/16 23038\end{center} 23039 23040\subsection{The population standard deviation: \texttt{stddevp}\index{stddevp} \texttt{stdDev}\index{stdDev}} 23041 23042Given a large population, rather than collecting all of the numbers it 23043might be more feasible to get a smaller collection of numbers and try 23044to extrapolate from that. For example, to get information about the 23045ages of a large population, you might get the ages of a sample of 100 23046of the people and work with that. 23047 23048If a list of numbers is a sample of data from a larger population, 23049then the \texttt{mean} function will find the mean of the sample, 23050which can be used to estimate the mean of the population. The 23051standard deviation uses the mean to find the standard 23052deviation of the sample, but since the mean of the sample is only an 23053approximation to the mean of the entire population, the standard 23054deviation of the sample doesn't provide an optimal estimate of 23055the standard deviation of the population. An unbiased estimate of the 23056standard deviation of the entire population is given by the population 23057standard deviation \texttt{stddevp} function; 23058given a list $L = [x_1,\dots,x_n]$ with mean $\mu$, the population 23059standard deviation is 23060\[ 23061\sigma = \sqrt{\frac{(x_1 - \mu)^2 + \dots + (x_n - \mu)^2}{n-1}}. 23062\] 23063Note that 23064\[ \text{stddevp}(L)^2 = \frac{n}{n-1} \text{stddev}(L)^2.\] 23065 23066For example, 23067\begin{center} 23068 \tt 23069 stddev([1,2,3,4]) 23070\end{center} 23071will return 23072\begin{center} 23073 \tt 23074 sqrt(5)/2 23075\end{center} 23076while 23077\begin{center} 23078 \tt 23079 stddevp([1,2,3,4]) 23080\end{center} 23081will return 23082\begin{center} 23083 \tt 23084 sqrt(15)/3 23085\end{center} 23086 23087Like \texttt{stddev}, the \texttt{stddevp} command can take a second 23088argument for weights. If you enter 23089\begin{center} 23090 \tt 23091 A := [0,1,2,3,4,5,6,7,8,9,10,11]\\ 23092 stddevp(A,A) 23093\end{center} 23094then you will get 23095\begin{center} 23096 \tt 23097 sqrt(66)/3 23098\end{center} 23099 23100The \texttt{stdDev} function is equivalent to \texttt{stddevp}, for TI 23101compatibility. There is no population variance function; if needed, 23102it can be computed by squaring the \texttt{stddevp} function. 23103 23104\subsection{The median: \texttt{median}\index{median}} 23105 23106Although the average of a list of numbers typically means the mean, 23107there are other notions of ``average''. Another one is the median; 23108the median of a list of numbers is the middle number when they are 23109listed in numeric order. For example, the median of the list 23110$[1,2,5,7,20]$ is simply $5$. If the length of a list of numbers is 23111even, so there isn't a middle number, the median is then the mean of 23112the two middle numbers; for example, the median of $[1,2,5,7,20,21]$ is 23113$(5+7)/2 = 6$. 23114 23115The \texttt{median} function finds the median of a list. The command 23116\begin{center} 23117 \tt 23118 median([1,2,5,7,20]) 23119\end{center} 23120will return 23121\begin{center} 23122 \tt 23123 5 23124\end{center} 23125The \texttt{median} function can take weights with a second argument, 23126where the weight of number represents how many times it is counted in 23127a list. For example, 23128\begin{center} 23129 \tt 23130 median([1,2,5,7,20],[5,3,2,1,2]) 23131\end{center} 23132will return 23133\begin{center} 23134 \tt 23135 2 23136\end{center} 23137since the median of $1,1,1,1,1,2,2,2,5,5,7,20,20$ is $2$. 23138 23139\subsection{Quartiles: \texttt{quartiles}\index{quartiles} \texttt{quartile1}\index{quartile1} \texttt{quartile3}\index{quartile3}} 23140 23141Recall that the quartiles of a list of numbers divide it into four 23142equal parts; the first quartile is the number $q_1$ such that 23143one-fourth of the list numbers fall below $q_1$; i.e., the median of 23144that part of the list which fall at or below the list median. The 23145second quartiles is the number $q_2$ such that half of the list 23146numbers fall at or below $q_2$; more specifically, the median of the 23147list. And of course the third quartile is the number $q_3$ such that 23148three-fourths of the list numbers fall at or below $q_3$. 23149 23150The function \texttt{quartiles} takes a list and returns a column 23151vector consisting of the minimum of the list, the first quartile, the second 23152quartile, the third quartile and the maximum. If you enter 23153\begin{center} 23154 \tt 23155 A := [0,1,2,3,4,5,6,7,8,9,10,11];\\ 23156 quartiles(A) 23157\end{center} 23158you will get 23159\begin{center} 23160 \tt 23161 [[0.0],[2.0],[5.0],[8.0],[11.0]] 23162\end{center} 23163 23164You can get the individual entries of this vector with the commands 23165\texttt{min}, \texttt{quartile1}, \texttt{median}, \texttt{quartile2} 23166and \texttt{max}. 23167 23168Just as with median, the \texttt{quartiles} function can take a second 23169argument consisting of weights for the first argument; for example, 23170\begin{center} 23171 \tt 23172 quartiles(A,A) 23173\end{center} 23174would return 23175\begin{center} 23176 \tt 23177 [0,6,8,10,11] 23178\end{center} 23179 23180\subsection{Quantiles: \texttt{quantile}\index{quantile}} 23181 23182Similar to quartiles, a quantile of a list is the number $q$ such that 23183a given fraction of the list numbers fall at or below $q$. The first 23184quartile, for example, is the quantile with the fraction 0.25. 23185 23186The command \texttt{quantile} takes a list of numbers and a value $p$ 23187between 0 and 1 as arguments and returns the $p$th quantile. For 23188example, 23189\begin{center} 23190 \tt 23191 A := [0,1,2,3,4,5,6,7,8,9,10,11]\\ 23192 quantile(A,0.1) 23193\end{center} 23194returns the quantile with $p = 0.1$ (the first decile): 23195\begin{center} 23196 \tt 23197 1.0 23198\end{center} 23199 23200Like \texttt{quartile}, the \texttt{quantile} command can take an 23201argument representing weights of the list; the weights can be given as 23202a second argument and then the value $p$ will be the third. The command 23203\begin{center} 23204 \tt 23205 quantile(A,A,0.25) 23206\end{center} 23207will return 23208\begin{center} 23209 \tt 23210 6 23211\end{center} 23212 23213\subsection{The boxwhisker: \texttt{boxwhisker}\index{boxwhisker} \texttt{mustache}\index{mustache}} 23214 23215A boxwhisker is a graphical view of the quartiles of a list of numbers. 23216The boxwhisker consists of a line segment from the the minimum of the 23217list to the first quartile, leading to a rectangle from the first 23218quartile to the third quartile, followed by a line segment from the 23219third quartile to the maximum of the list. The rectangle will contain 23220a vertical segment indicating the median, and the two line segments 23221will contain vertical lines indicating the first and ninth decile. 23222 23223The \texttt{boxwhisker} (or \texttt{mustache}) command will create a 23224boxwhisker for a list. For example, if you enter 23225\begin{center} 23226 \tt 23227 boxwhisker([-1,1,2,2.2,3,4,-2,5]) 23228\end{center} 23229a graphic window will appear showing the boxwhisker, 23230\begin{center} 23231 \includeimage{xcas-boxwhisker.png} 23232\end{center} 23233 23234\subsection{Classes: \texttt{classes}\index{classes}} 23235 23236The \texttt{classes} command can be used to groups a collection of 23237numbers into intervals; the result will be a list where each element 23238is an interval \texttt{a..b} followed by how many of the numbers are 23239in the interval $[a,b)$. The collection of numbers can be given as a 23240list or matrix. 23241 23242If $L$ is a collection of numbers, $a$ and $b$ are numbers, then 23243\texttt{classes($L$,$a$,$b$)} will return the list 23244\texttt{[[$a$..$a+b$,$n_1$],[$a+b$..$a+2b$,$n_2$],...]} where each number in $L$ is 23245in one of the intervals $[a+kb, a+(k+1)b)$ and $n_k$ is 23246how many numbers from $L$ are in the corresponding interval. For 23247example, 23248\begin{center} 23249 \tt 23250 classes([0,0.5,1,1.5,2,2.5,3,3.5,4],0,2) 23251\end{center} 23252will return 23253\begin{center} 23254 \tt 23255 [[0.0 .. 2.0,4],[2.0 .. 4.0,4],[4.0 .. 6.0,1]] 23256\end{center} 23257while 23258\begin{center} 23259 \tt 23260 classes([0,0.5,1,1.5,2,2.5,3,3.5,4],-1,2) 23261\end{center} 23262will return 23263\begin{center} 23264 \tt 23265 [[(-1.0) .. 1.0,2],[1.0 .. 3.0,4],[3.0 .. 5.0,3]] 23266\end{center} 23267If the numbers $a$ and $b$ are omitted, they will default to the 23268configurable values of \texttt{class\_min} and \texttt{class\_size}, 23269which default to $0$ and $1$. 23270 23271Another way to split the list $L$ into intervals is by making the 23272third argument the midpoints of the desired intervals. For example, 23273if you enter 23274\begin{center} 23275 \tt 23276 classes([0,0.5,1,1.5,2,2.5,3,3.5,4],1,[1,3,5]) 23277\end{center} 23278you will get 23279\begin{center} 23280 \tt 23281 [[0.0..2.0,4],[2.0..4.0,4],[4.0..6.0,1]] 23282\end{center} 23283 23284Finally, you can simply state the intervals that you want to use by 23285giving them as a list for the second argument. In this case, not 23286every number in the list is necessarily in one of the intervals. If 23287you enter 23288\begin{center} 23289 \tt 23290 classes([0,0.5,1,1.5,2,2.5,3,3.5,4],[1..3,3..6]) 23291\end{center} 23292you will get 23293\begin{center} 23294 \tt 23295 [[1 .. 3,4],[3 .. 6,3]] 23296\end{center} 23297 23298\subsection{Histograms: \texttt{histogram}\index{histogram} \texttt{histogramme}\index{histogramme}} 23299 23300Given a list of intervals and a number of points in each interval, 23301such as is given by the output of the \texttt{classes} command, the 23302\texttt{histogram} (or \texttt{histogramme}) command will draw a box 23303over each interval so that the height of each box is proportional to 23304the number of points and the total area of the boxes is 1. For 23305example, if you enter 23306\begin{center} 23307 \tt 23308 histogram([[1.5..1.65,50],[1.65..1.7,20],[1.7..1.8,30]]) 23309\end{center} 23310you will get 23311\begin{center} 23312 \includeimage{xcas-histogram1.png} 23313\end{center} 23314 23315If you just give the \texttt{histogram} a list of numbers, or a list 23316with values $a$ and $b$, then you will get the histogram of the result 23317of applying \texttt{classes} to the list. 23318% will start at 0 and draw boxes over intervals of width 1, such that 23319% the height of the boxes are proportional to the number of list 23320% elements in the interval and the total area will be 1. For example, 23321% if you enter 23322% \begin{center} 23323% \tt 23324% histogram([1,2,2.5,2.5,3]) 23325% \end{center} 23326% you will get 23327% PIC HISTOGRAM1 23328% You can change the starting point and interval width (from 0 and 1, 23329% respectively) by giving \texttt{histogram} second and third arguments; 23330For example, if you enter 23331\begin{center} 23332 \tt 23333 histogram([1,2,2.5,2.5,3],0.5,0.75) 23334\end{center} 23335you will get 23336\begin{center} 23337 \includeimage{xcas-histogram2.png} 23338\end{center} 23339 23340\subsection{Accumulating terms: 23341\texttt{accumulate\_head\_tail}\index{accumulate\_head\_tail}} 23342 23343The first terms and last terms of a list can be accumulated by 23344replacing them with their sum using the \texttt{accumulate\_head\_tail} 23345command. This command takes the list, the number of initial terms to 23346sum, and the number of end terms to add, and returns the list with the 23347initial terms and end terms replaced by their sums. For example, the 23348command 23349\begin{center} 23350 \tt 23351 accumulate\_head\_tail([1,2,3,4,5,6,7,8,9,10],3,4) 23352\end{center} 23353will return 23354\begin{center} 23355 \tt 23356 [6,4,5,6,34] 23357\end{center} 23358 23359\subsection{Frequencies: \texttt{frequencies}\index{frequencies} \texttt{frequences}\index{frequences}} 23360 23361Given a list of numbers, the \texttt{frequencies} (or 23362\texttt{frequences}) command will return 23363the numbers in the list with their frequencies; i.e., the fraction of 23364list items equal to the number. For example, 23365\begin{center} 23366 \tt 23367 frequencies([1,2,1,1,2,1,2,4,3,3]) 23368\end{center} 23369will return 23370\begin{center} 23371 \tt 23372 [[1,0.4],[2,0.3],[3,0.2],[4,0.1]] 23373\end{center} 23374 23375You can use this, for example, to simulate flipping a fair coin and 23376seeing how many times each side appears; to flip a coin 1000 times, 23377for example, you can enter 23378\begin{center} 23379 \tt 23380 frequencies([rand(2) \$ (k=1..1000)]) 23381\end{center} 23382and you might get 23383\begin{center} 23384 \tt 23385 [[0,0.513],[1,0.487]] 23386\end{center} 23387 23388\subsection{Cumulative frequencies: \texttt{cumulated\_frequencies}\index{cumulated\_frequencies} \texttt{frequences\_cumulees}\index{frequences\_cumulees}} 23389 23390Given a list, the \texttt{cumulated\_frequencies} command will plot 23391the cumulated frequency of the numbers in the list; i.e., the area 23392under the resulting graph at a value $x$ will be the fraction of 23393numbers less than $x$. For example, if you enter 23394\begin{center} 23395 \tt 23396 cumulated\_frequencies([1,2,1,1,2,1,2,4,3,3]) 23397\end{center} 23398then you will get 23399\begin{center} 23400 \includeimage{xcas-cumfreq1.png} 23401\end{center} 23402 23403The \texttt{cumulated\_frequencies} command can also take a matrix 23404with two columns as an argument. In this case, the first column will 23405represent values while the second column will represent the number of 23406times the values occur. For example, the above graph can be drawn 23407with the command 23408\begin{center} 23409 \tt 23410 cumulated\_frequencies([[1,4],[2,3], [3,2], [4,1]]) 23411\end{center} 23412 23413If the first column of the input matrix contains intervals 23414\texttt{$a$..$b$} instead of numbers, then the second column values will 23415be normalized to add up to one, and will represent the frequencies of 23416the intervals. If the matrix has the form 23417\[ [[a_0..a_1,f_1],...,[a_{n-1}..a_n,f_n]] \] 23418then the plot will consist of the polygonal path starting at $(a_0,0)$ 23419and moving to $(a_1,f_1)$ to $(a_2,f_1+f_2)$ and so on until 23420$(a_n,f_1+\cdots + f_n)$. For example, both 23421\begin{center} 23422 \tt 23423 cumulated\_frequencies([[1..2,30],[2..4,40],[4..5,30]]) 23424\end{center} 23425and 23426\begin{center} 23427 \tt 23428 cumulated\_frequencies([[1..2,03],[2..4,0.4],[4..5,0.3]]) 23429\end{center} 23430will give you 23431\begin{center} 23432 \includeimage{xcas-cumfreqint.png} 23433\end{center} 23434 23435If the matrix given to \texttt{cumulated\_frequencies} has more than 23436two columns, then each additional column will represent a different 23437distribution of the numbers in the first column, and each distribution 23438will be graphed. For example, if you enter 23439\begin{center} 23440 \tt 23441 cumulated\_frequencies([[1,4,1],[2,3,4], [3,2,1], [4,1,2]]) 23442\end{center} 23443then both the distributions given by 23444\texttt{[[1,4],[2,3], [3,2], [4,1]]} 23445and 23446\texttt{[[1,1],[2,4], [3,1], [4,2]]} 23447will be drawn on the same axes; the result will be 23448\begin{center} 23449 \includeimage{xcas-cumfreq2.png} 23450\end{center} 23451 23452\subsection{Bar graphs: \texttt{bar\_plot}\index{bar\_plot}} 23453 23454You can draw bar graphs with the \texttt{bar\_plot} command. You give 23455it a list, whose elements are pairs of labels and values, and the 23456result will be a bar graph with a bar for each label, whose height is 23457given by the corresponding value. For example, if you enter 23458\begin{center} 23459 \tt 23460 bar\_plot([["France", 6],["Germany", 12], ["Switzerland", 5]]) 23461\end{center} 23462you will get 23463\begin{center} 23464 \includeimage{xcas-barplot1.png} 23465\end{center} 23466 23467If you have more than one set of values for each label, you can use 23468\texttt{bar\_plot} to draw several bar graphs at the same time by 23469including all values for each label, with a list of identifiers for 23470the bar graphs given by the first argument. If you enter 23471\begin{center} 23472 \tt 23473 bar\_plot([[2,"xyz","abc"],["A",2,5],["B",5,6],["C",6,6]]) 23474\end{center} 23475you will get 23476\begin{center} 23477 \includeimage{xcas-barplot2.png} 23478\end{center} 23479 23480\subsection{Pie charts: \texttt{camembert}\index{camembert}} 23481 23482You can draw pie charts using the same structure as bar graphs, but 23483with the command \texttt{camembert}. If you enter 23484\begin{center} 23485 \tt 23486 camembert([["France", 6],["Germany", 12], ["Switzerland", 5]]) 23487\end{center} 23488you will get 23489\begin{center} 23490 \includeimage{xcas-piechart1.png} 23491\end{center} 23492and if you enter 23493\begin{center} 23494 \tt 23495 camembert([[2,"xyz","abc"],["A",2,5],["B",5,6],["C",6,6]]) 23496\end{center} 23497you will get 23498\begin{center} 23499 \includeimage{xcas-piechart2.png} 23500\end{center} 23501 23502\section{Two variable statistics} 23503 23504\subsection{Covariance and correlation: \texttt{covariance}\index{covariance} \texttt{correlation}\index{correlation} \texttt{covariance\_correlation}\index{covariance\_correlation}} 23505 23506The covariance of two random variables measures their connectedness; 23507i.e., whether they tend to change with each other. 23508If $X$ and $Y$ are two random variables, then the covariance is the 23509expected value of $(X-\bar{X})(Y-\bar{Y})$, where $\bar{X}$ and 23510$\bar{Y}$ are the means of $X$ and $Y$, respectively. You can 23511calculate covariances with the \texttt{covariance} command. 23512 23513If $X$ and $Y$ are given by lists of the same size, then 23514\texttt{covariance($X$,$Y$)} will return their covariance. For example, 23515if you enter 23516\begin{center} 23517 \tt 23518 covariance([1,2,3,4],[1,4,9,16]) 23519\end{center} 23520then you will get 23521\begin{center} 23522 \tt 23523 25/4 23524\end{center} 23525Alternatively, you could use a matrix with two columns instead of two 23526lists to enter $X$ and $Y$; the command 23527\begin{center} 23528 \tt 23529 covariance([[1,1],[2,4],[3,9],[4,16]]) 23530\end{center} 23531is another way to enter the above calculation. 23532 23533If the entries in the lists $X=[a_0,\dots,a_{n-1}]$ and 23534$Y=[b_0,\dots,b_{n-1}]$ have different weights, say $a_j$ and $b_j$ have 23535weight $w_j$, then \texttt{covariance} can be given a third list 23536$W=[w_0,\dots,w_{n-1}]$ (or alternatively, you could use a matrix with 23537three columns). For example, if you enter 23538\begin{center} 23539 \tt 23540 covariance([1,2,3,4],[1,4,9,16],[3,1,5,2]) 23541\end{center} 23542then you will get 23543\begin{center} 23544 \tt 23545 662/121 23546\end{center} 23547 23548If each pair of entries in the lists $X=[a_0,\dots,a_{m-1}]$ and 23549$Y=[b_0,\dots,b_{0}]$ have different weights, say $a_j$ and $b_k$ have 23550weight $w_{jk}$, then \texttt{covariance} can be given a third 23551argument of an $m\times n$ matrix $W=(w_{jk})$. (Note that in this 23552case the lists $X$ and $Y$ don't have to be the same length.) For 23553example, the covariance computed above could also have been computed 23554by entering 23555\begin{center} 23556 \tt 23557 covariance([1,2,3,4],[1,4,9,16], 23558 [[3,0,0,0],[0,1,0,0],[0,0,5,0],[0,0,0,2]]) 23559\end{center} 23560which would of course return 23561\begin{center} 23562 \tt 23563 662/121 23564\end{center} 23565In this case, to make it simpler to enter the data in a spreadsheet, 23566the lists $X$ and $Y$ and the matrix $W$ can be combined into a single 23567matrix, by augmenting $W$ with the list $Y$ on the top and the 23568transpose of the list $X$ on the left, with a filler in the upper left 23569hand corner; 23570\[ 23571\begin{pmatrix} 23572"XY" & Y\\ 23573X^T & W 23574\end{pmatrix} 23575\] 23576When you use this method, you need to give \texttt{covariance} a 23577second argument of -1. The above covariance can then be computed with 23578the command 23579\begin{center} 23580 \tt 23581 covariance([["XY", 1,4,9,16],[1,3,0,5,0],[2,0,1,0,0],[3,0,0,5,0],[4,0,0,0,2]],-1) 23582\end{center} 23583 23584The linear correlation coefficient of two random variables is another 23585way to measure their connectedness. Given random variables $X$ and 23586$Y$, their correlation is defined as 23587$cov(X,Y)/(\sigma(X)\sigma(Y))$, where $\sigma(X)$ and $\sigma(Y)$ are 23588the standard deviations of $X$ and $Y$, respectively. The correlation 23589can be computed with the \texttt{correlation} command, which takes the 23590same types of arguments as the \texttt{covariance} command. If you 23591enter 23592\begin{center} 23593 \tt 23594 correlation([1,2,3,4],[1,4,9,16]) 23595\end{center} 23596you will get 23597\begin{center} 23598 \tt 23599 100/(4*sqrt(645)) 23600\end{center} 23601 23602The \texttt{covariance\_correlation} command will compute both the 23603covariance and correlation simultaneously, and return a list with both 23604values. This command takes the same type of arguments as the 23605\texttt{covariance} and \texttt{correlation} commands. For example, 23606if you enter 23607\begin{center} 23608 \tt 23609 covariance\_correlation([1,2,3,4],[1,4,9,16]) 23610\end{center} 23611you will get 23612\begin{center} 23613 \tt 23614 [25/4,100/(4*sqrt(645))] 23615\end{center} 23616 23617\subsection{Scatterplots: \texttt{scatterplot}\index{scatterplot} 23618\texttt{nuaged\_points}\index{nuage\_points} \texttt{batons}\index{batons}} 23619 23620A scatter plot is simply a set of points plotted on axes. You can 23621draw a scatter plot with the \texttt{scatterplot} or 23622\texttt{nuage\_points} command. 23623 23624You can call \texttt{scatterplot} with a matrix with two columns 23625(essentially, a list of points) or a list of 23626$x$-coordinates followed by a list of $y$-coordinates. If you enter 23627\begin{center} 23628 \tt 23629 scatterplot([[0,0],[1,1],[2,4],[3,9],[4,16]]) 23630\end{center} 23631or 23632\begin{center} 23633 \tt 23634 scatterplot([0,1,2,3,4],[0,1,4,9,16]) 23635\end{center} 23636you will get 23637\begin{center} 23638 \includeimage{xcas-scatterplot.png} 23639\end{center} 23640 23641If you want the points connected to the $x$-axis, the \texttt{batons} 23642command will take the same arguments at \texttt{scatterplot} and plot 23643the points with a vertical line segment connecting them to the 23644$x$-axis. If you enter 23645\begin{center} 23646 \tt 23647 batons([[0,0],[1,1],[2,4],[3,9],[4,16]]) 23648\end{center} 23649you will get 23650\begin{center} 23651 \includeimage{xcas-batons.png} 23652\end{center} 23653 23654\subsection{Polygonal paths: \texttt{polygonplot}\index{polygonplot} 23655\texttt{ligne\_polygonale}\index{ligne\_polygonale} 23656\texttt{linear\_interpolate}\index{linear\_interpolate} 23657\texttt{listplot}\index{listplot} \texttt{plotlist}\index{plotlist}} 23658 23659You can draw a polygonal path with either \texttt{polygonplot} or 23660\texttt{listplot}. 23661 23662Given a list of points (a two-column matrix) or two lists (the $x$ 23663coordinates and the $y$-coordinates), the \texttt{polygonplot} (or 23664\texttt{polygonscatterplot}) command 23665will draw the polygonal path through the points, from left to right 23666(so the points are automatically ordered by increasing 23667$x$-coordinates). If you enter 23668\begin{center} 23669 \tt 23670 polygonplot([0,1,2,3,4],[0,1,4,9,16]) 23671\end{center} 23672or 23673\begin{center} 23674 \tt 23675 polygonplot([[0,0],[1,1],[2,4],[3,9],[4,16]]) 23676\end{center} 23677or even 23678\begin{center} 23679 \tt 23680 polygonplot([[2,4],[0,0],[3,9],[1,1],[4,16]]) 23681\end{center} 23682you will get 23683\begin{center} 23684 \includeimage{xcas-polygonplot.png} 23685\end{center} 23686 23687If you give \texttt{polygonplot} a single list of numbers, then they will 23688be taken to be the $y$-coordinates and the $x$-coordinates will be 23689assumed to be integers starting at $0$. If you enter 23690\begin{center} 23691 \tt 23692 polygonplot([0,1,4,9,16]) 23693\end{center} 23694 23695If you want to get coordinates on the polygonal path, you can use the 23696\texttt{linear\_interpolate} command. This command takes four 23697arguments; a two-row matrix consisting of the $x$-coordinates and the 23698$y$-coordinates, $x_{min}$, the minimum value of $x$ that you are 23699interested in, $x_{max}$, the maximum value of $x$, and 23700$x_{step}$, the step size you want. (The values of $x_{min}$ 23701and $x_{max}$ must be between the smallest and largest 23702$x$-coordinates of the points.) You will get a matrix with 23703two rows, the first row will be 23704$[x_{min}, x_{min}+x_{step}, x_{min}+2x_{step},...,x_{max}]$ 23705and the second row will be the corresponding $y$-coordinates of the 23706points on the polygonal path. For example, if you enter 23707\begin{center} 23708 \tt 23709 linear\_interpolate([[1,2,6,9],[3,4,6,12]],2,7,1) 23710\end{center} 23711you will get 23712\begin{center} 23713 \tt 23714 [[2.0,3.0,4.0,5.0,6.0,7.0],[4.0,4.5,5.0,5.5,6.0,8.0]] 23715\end{center} 23716 23717If you want to draw a polygonal path through points in an order 23718determined by you, you can use the \texttt{listplot} (or 23719\texttt{plotlist}) command. If you give \texttt{listplot} a list of 23720points, then you will get a polygonal path through the points in the 23721order given by the list. If you enter 23722\begin{center} 23723 \tt 23724 listplot([[2,4],[0,0],[3,9],[1,1],[4,16]]) 23725\end{center} 23726you will get 23727\begin{center} 23728 \includeimage{xcas-listplot.png} 23729\end{center} 23730 23731As with \texttt{polygonplot}, if you give \texttt{listplot} a single 23732list of numbers, then they will be taken to be the $y$-coordinates and 23733the $x$-coordinates will be assumed to be integers starting at $0$. 23734If you enter 23735\begin{center} 23736 \tt 23737 listplot([0,1,4,9,16]) 23738\end{center} 23739you will get the same graph that you got with \texttt{polygonplot}. 23740However, unlike \texttt{polygonplot}, the \texttt{listplot} command 23741can't be given two lists of numbers as arguments. 23742 23743\subsection{Linear regression: \texttt{linear\_regression}\index{linear\_regression} \texttt{linear\_regression\_plot}\index{linear\_regression\_plot}} 23744 23745Given a set of points $(x_0,y_0),\dots,(x_{n-1},y_{n-1})$, linear 23746regression finds the line $y=mx+b$ that comes 23747closest to passing through all of the points; i.e., that makes 23748$\sqrt{(y_0 - (m x_0 + b))^2 + \dots + (y_{n-1} - (m x_{n-1} + b))^2}$ 23749as small as possible. Given a set of points (a two-column matrix) or 23750two lists of numbers (the $x$- and $y$-coordinates), the 23751\texttt{linear\_regression} command will find the values of $m$ and 23752$b$ which determine the line. For example, if you enter 23753\begin{center} 23754 \tt 23755 linear\_regression([[0,0],[1,1],[2,4],[3,9],[4,16]]) 23756\end{center} 23757or 23758\begin{center} 23759 \tt 23760 linear\_regression([0,1,2,3,4],[0,1,4,9,16]) 23761\end{center} 23762you will get 23763\begin{center} 23764 \tt 23765 4, -2 23766\end{center} 23767which means that the line $y = 4x - 2$ is the best fit line. 23768 23769The best fit line can be drawn with the 23770\texttt{linear\_regression\_plot} command; if you enter 23771\begin{center} 23772 \tt 23773 linear\_regression\_plot([0,1,2,3,4],[0,1,4,9,16]) 23774\end{center} 23775you will get 23776\begin{center} 23777 \includeimage{xcas-linregplot.png} 23778\end{center} 23779This will draw the line (in this case $y=4x-2$) and give you the 23780equation at the top, as well as the $R^2$ value, which is 23781\[ R^2 = \frac{\sum_{j=0}^{n-1} (m x_j + b - \bar{y})^2}{\sum_{j=0}^{n-1} (y_j - \bar{y})^2}\] 23782(The $R^2$ value will be between 0 and 1 and is one measure of how 23783good the line fits the data; a value close to 1 indicates a good fit, 23784a value close to 0 indicates a bad fit.) 23785 23786\subsection{Exponential regression: \texttt{exponential\_regression}\index{exponential\_regression} \texttt{exponential\_regression\_plot}\index{exponential\_regression\_plot}} 23787 23788A set of points might be expected to lie on an exponential curve 23789$y=b a^x$. Given a set of points, either as a list of $x$-coordinates 23790followed by a list of $y$-coordinates, or simply by a list of points, 23791the \texttt{exponential\_regression} command will find the values of 23792$a$ and $b$ which give the best fit exponential. For example, if you 23793enter 23794\begin{center} 23795 \tt 23796 evalf(exponential\_regression([[1,1],[2,4],[3,9],[4,16]])) 23797\end{center} 23798or 23799\begin{center} 23800 \tt 23801 evalf(exponential\_regression([1,2,3,4],[1,4,9,16])) 23802\end{center} 23803(where the \texttt{evalf} is used to get a numeric approximation to an 23804exact expression) you will get 23805\begin{center} 23806 \tt 23807 2.49146187923,0.5 23808\end{center} 23809so the best fit exponential curve will be $y = 0.5*(2.49146187923)^x$. 23810 23811To plot the curve, you can use the command 23812\texttt{exponential\_regression\_plot}; if you enter 23813\begin{center} 23814 \tt 23815 exponential\_regression\_plot([1,2,3,4],[1,4,9,16]) 23816\end{center} 23817you will get 23818\begin{center} 23819 \includeimage{xcas-expregplot.png} 23820\end{center} 23821which plots the graph, and has the equation and $R^2$ value above the 23822graph. 23823 23824\subsection{Logarithmic regression: \texttt{logarithmic\_regression}\index{logarithmic\_regression} \texttt{logarithmic\_regression\_plot}\index{logarithmic\_regression\_plot}} 23825 23826A set of points might be expected to lie on a logarithmic curve 23827$y=m \ln(x) + b$. Given a set of points, either as a list of $x$-coordinates 23828followed by a list of $y$-coordinates, or simply by a list of points, 23829the \texttt{logarithmic\_regression} command will find the values of 23830$m$ and $b$ which give the best fit exponential. For example, if you 23831enter 23832\begin{center} 23833 \tt 23834 evalf(logarithmic\_regression([[1,1],[2,4],[3,9],[4,16]])) 23835\end{center} 23836or 23837\begin{center} 23838 \tt 23839 evalf(logarithmic\_regression([1,2,3,4],[1,4,9,16])) 23840\end{center} 23841(where the \texttt{evalf} is used to get a numeric approximation to an 23842exact expression) you will get 23843\begin{center} 23844 \tt 23845 10.1506450002,-0.564824055818 23846\end{center} 23847so the best fit exponential curve will be $y = 10.1506450002 \ln(x) 23848-0.564824055818$. 23849 23850To plot the curve, you can use the command 23851\texttt{exponential\_regression\_plot}; if you enter 23852\begin{center} 23853 \tt 23854 logarithmic\_regression\_plot([1,2,3,4],[1,4,9,16]) 23855\end{center} 23856you will get 23857\begin{center} 23858 \includeimage{xcas-logregplot.png} 23859\end{center} 23860which plots the graph, and has the equation and $R^2$ value above the 23861graph. 23862 23863\subsection{Power regression: \texttt{power\_regression}\index{power\_regression} \texttt{power\_regression\_plot}\index{power\_regression\_plot}} 23864 23865To find the graph $y=bx^m$ which best fits a set of data points, you 23866can use the \texttt{power\_regression} command. 23867Given a set of points, either as a list of $x$-coordinates 23868followed by a list of $y$-coordinates, or simply by a list of points, 23869the \texttt{power\_regression} command will find the values of 23870$m$ and $b$ which give the best fit curve. For example, if you 23871enter 23872\begin{center} 23873 \tt 23874 power\_regression([[1,1],[2,4],[3,9],[4,16]]) 23875\end{center} 23876or 23877\begin{center} 23878 \tt 23879 power\_regression([1,2,3,4],[1,4,9,16]) 23880\end{center} 23881you will get 23882\begin{center} 23883 \tt 23884 2.0, 1.0 23885\end{center} 23886so the best fit (in this case, exact fit) power curve will be $y = 1.0 x^2$. 23887 23888To plot the curve, you can use the command 23889\texttt{power\_regression\_plot}; if you enter 23890\begin{center} 23891 \tt 23892 power\_regression\_plot([1,2,3,4],[1,4,9,16]) 23893\end{center} 23894you will get 23895\begin{center} 23896 \includeimage{xcas-powregplot.png} 23897\end{center} 23898which plots the graph, and has the equation and $R^2$ value above the 23899graph. Note that in this case the $R^2$ value is 1, indicating that 23900the data points fall directly on the curve. 23901 23902\subsection{Polynomial regression: \texttt{polynomial\_regression}\index{polynomial\_regression} \texttt{polynomial\_regression\_plot}\index{polynomial\_regression\_plot}} 23903 23904If you want to find a more general polynomial $y=a_0x^n + \dots + a_n$ 23905which best fits a set of data points, you can use the 23906\texttt{polynomial\_regression} command. 23907Given a set of points, either as a list of $x$-coordinates 23908followed by a list of $y$-coordinates, or simply by a list of points, 23909as well as a power $n$, the \texttt{polynomial\_regression} command 23910will return the list $[a_n,\dots,a_0]$ of coefficients of the 23911polynomial. For example, if you enter 23912\begin{center} 23913 \tt 23914 polynomial\_regression([[1,1],[2,2],[3,10],[4,20]],3) 23915\end{center} 23916or 23917\begin{center} 23918 \tt 23919 polynomial\_regression([1,2,3,4],[1,2,10,20],3) 23920\end{center} 23921you will get 23922\begin{center} 23923 \tt 23924 [-5/6,17/2,-56/3,12] 23925\end{center} 23926so the best fit polynomial will be $y = (-5/6)x^3 + (17/2)x^2 - 23927(56/3)x + 12$. 23928 23929To plot the curve, you can use the command 23930\texttt{polynomial\_regression\_plot}; if you enter 23931\begin{center} 23932 \tt 23933 polynomial\_regression\_plot([1,2,3,4],[1,2,10,20],3) 23934\end{center} 23935you will get 23936\begin{center} 23937 \includeimage{xcas-polyregplot.png} 23938\end{center} 23939 23940\subsection{Logistic regression: \texttt{logistic\_regression}\index{logistic\_regression} \texttt{logistic\_regression\_plot}\index{logistic\_regression\_plot}} 23941 23942Differential equations of the form $y' = y(a*y + b)$ come up often, 23943particularly when studying bounded population growth. With the 23944initial condition $y(x0) = y0$, the solution is the logistic equation 23945\[ 23946y = \frac{-b*y0}{a*y0 - (a*y0+b)\exp(b(x0-x))} 23947\] 23948However, you often don't know the values of $a$ and $b$. You can 23949still get a ``best fit'' logisitic equation with the following 23950information: The initial value of $x$, the initial value of $y$, and 23951several values of $y'$; namely, $y'(x0),y'(x0+1),\dots,y'(x0+n-1)$ where 23952$x0$ is the initial value of $x$. \texttt{Xcas} will then take the 23953initial value $y(x0) = y0$ and the approximation $y(t+1) \approx y(t) 23954+ y'(t)$ to get the approximations $y(x0+1) \approx y0 + y'(x0)$, 23955$y(x0+2) \approx y0 + y'(x0) + y'(x0+1)$, \ldots 23956$y(x0+n) \approx y0 + y'(x0) +\dots y'(x0+n-1)$, \ldots 23957Since $y'/y = a + by$, \texttt{Xcas} will take the 23958approximate values of $y'(x0+j)/y(x0+j)$ and use linear interpolation 23959to get the best fit values of $a$ and $b$, and then solve the 23960differential equation. 23961 23962The \texttt{logistic\_regression} command will take as input a list 23963and two numbers; the list will be $[y_{10},y_{11},\dots,y_{1(n-1)}]$, 23964where $y_{1j}$ represents the value of $y'(x_0 + j)$, 23965the first number is $x_0$ and the last number is 23966$y_0=y(x_0)$. The command will return the function $y(x)$, the 23967derivative $y'(x)$, the number $C=-b/a$, $y'(x_M)$ which is the maximum value 23968of $y'$, $x_M$ which is where $y'$ has its maximum, and the linear 23969correlation coefficient $R$ of $Y=y'/y$ as a function of $y$ with $Y=a*y 23970+ b$. For example, if you enter 23971\begin{center} 23972 \tt 23973 logistic\_regression([0.0,1.0,2.5],0,1) 23974\end{center} 23975you will get 23976\begin{verbatim} 23977 Pinstant=0.132478632479*Pcumul+0.0206552706553 23978 Correlation 0.780548607383, Estimated total P=-0.155913978495 23979 Returning estimated Pcumul, Pinstant, Ptotal, Pinstantmax, tmax, R 23980 [-0.155913978495/(1+exp(-0.0554152581707*x+0.140088513344+3.14159265359*i)), 23981 -0.00161022271237/(1+cos((-i)*(-0.0554152581707*x+0.140088513344+3.14159265359*i))), 23982 -0.155913978495,-0.000805111356186,2.52797727501+56.6918346552*i,0.780548607383] 23983\end{verbatim} 23984 23985You can plot the logistic equation with the command 23986\texttt{logistic\_regression\_plot}; if you enter 23987\begin{center} 23988 \tt 23989 logistic\_regression\_plot([1,2,4,6,8,7,5],0,2.0) 23990\end{center} 23991you will get 23992\begin{center} 23993 \includeimage{xcas-logisticregplot.png} 23994\end{center} 23995 23996\section{Random numbers} 23997 23998\subsection{Producing uniformly distributed random numbers: \texttt{rand}\index{rand} \texttt{random}\index{random} \texttt{alea}\index{alea} \texttt{hasard}\index{hasard}} 23999\label{ssec:rand} 24000 24001The \texttt{rand} (or \texttt{random}) command will produce a number in 24002$[0,1)$ randomly and with equal probability. If you enter 24003\begin{center} 24004 \tt 24005 rand() 24006\end{center} 24007you might get, for example, 24008\begin{center} 24009 \tt 24010 0.93233498279 24011\end{center} 24012 24013If you want a random number in a different interval, you can give 24014\texttt{rand} two real arguments; \texttt{rand($a$,$b$)} will return a 24015random number from the interval $[a,b)$. If you enter 24016\begin{center} 24017 \tt 24018 rand(1,1.5) 24019\end{center} 24020for example, you might get 24021\begin{center} 24022 \tt 24023 1.27419309644 24024\end{center} 24025If you give \texttt{rand} an interval, then you will get function 24026which will generate a random number in the interval. If you enter 24027\begin{center} 24028 \tt 24029 r:=rand(1.0..2.5) 24030\end{center} 24031you will get 24032\begin{center} 24033 \tt 24034 // Success 24035 (NULL)->rand(1.0,2.5) 24036\end{center} 24037and you can get a random number in the interval by calling the function; 24038\begin{center} 24039 \tt 24040 r() 24041\end{center} 24042might return 24043\begin{center} 24044 \tt 24045 1.76314622024 24046\end{center} 24047 24048If you want to generate a random integer, then \texttt{rand($n$)} (for 24049integer $n$) will return a random integer in $[0,n)$ (or $(n,0]$ if 24050$n$ is negative). If you enter 24051\begin{center} 24052 \tt 24053 rand(5) 24054\end{center} 24055for example, you might get 24056\begin{center} 24057 \tt 24058 2 24059\end{center} 24060 24061You can then use \texttt{rand} to find a random integer in a specified interval; if 24062you want an random integer between 6 and 10, inclusive, for example, 24063you can enter 24064\begin{center} 24065 \tt 24066 6 + rand(11-6) 24067\end{center} 24068You might get 24069\begin{center} 24070 \tt 24071 7 24072\end{center} 24073Alternatively, the \texttt{randint} will give you a random integer in 24074a given interval; \texttt{randint($n_1$,$n_2$)} will return a random 24075integer between $n_1$ and $n_2$, inclusive; to get a random integer 24076from 6 to 10, you could enter 24077\begin{center} 24078 \tt 24079 randint(6,10) 24080\end{center} 24081 24082The \texttt{rand} command can also choose elements without 24083replacement. If you give \texttt{rand} three integer arguments, 24084\texttt{rand($p$,$n_1$,$n_2$)} then it will return $p$ distinct random 24085integers from $n_1$ to $n_2$. If you enter 24086\begin{center} 24087 \tt 24088 rand(2,1,10) 24089\end{center} 24090for example, you will get 2 distinct random numbers from 1 to 10; 24091perhaps 24092\begin{center} 24093 \tt 24094 [2,9] 24095\end{center} 24096 24097You can also choose (without replacement) random elements of a given 24098list. For this, you give \texttt{rand}, a postive integer $n$ 24099and a list $L$; \texttt{rand($n$,$L$)} will then return $n$ random 24100elements from the list. If you enter 24101\begin{center} 24102 \tt 24103 rand(3,["a","b","c","d","e","f","g","h"]) 24104\end{center} 24105you might get 24106\begin{center} 24107 \tt 24108 ["c","b","e"] 24109\end{center} 24110The list can have repeated elements; if you enter 24111\begin{center} 24112 \tt 24113 rand(4,["r","r","r","r","v","v","v"]) 24114\end{center} 24115you might get 24116\begin{center} 24117 \tt 24118 ["v","v","r","v"] 24119\end{center} 24120The \texttt{sample} command will also randomly select items from a 24121list without replacement. With the \texttt{sample} command, the list 24122comes first and then the integer. If you enter 24123\begin{center} 24124 \tt 24125 sample(["r","r","r","r","v","v","v"],4) 24126\end{center} 24127you might get 24128\begin{center} 24129 \tt 24130 ["v","r","r","r"] 24131\end{center} 24132 24133\subsection{Initializing the random number generator: \texttt{srand}\index{srand} \texttt{randseed}\index{randseed} \texttt{RandSeed}\index{RandSeed}} 24134 24135The \texttt{srand} (or \texttt{randseed}) and \texttt{RandSeed} 24136commands will initialize (or re-initialize) the random numbers given 24137by \texttt{rand}. The \texttt{RandSeed} requires an integer argument, 24138and \texttt{srand} can either take an integer argument or no argument. 24139If you don't give \texttt{srand} an argument, then it will use the 24140system clock to initialize the random numbers. 24141 24142\subsection{Producing random numbers with the binomial distribution: \texttt{randbinomial}\index{randbinomial}} 24143 24144The command \texttt{randbinomial} will take parameters an integer $n$ 24145and a number $p$ between 0 and 1, and return an integer from 0 to $n$ 24146chosen according to the binomial distribution; i.e., the number of 24147successes you might get if you did an experiment $n$ times, where the 24148probability of success each time is $p$. If you enter 24149\begin{center} 24150 \tt 24151 randbinomial(100,0.4) 24152\end{center} 24153for example, you might get 24154\begin{center} 24155 \tt 24156 34 24157\end{center} 24158 24159\subsection{Producing random numbers with a multinomial distribution: \texttt{randmultinomial}\index{randmultinomial}} 24160 24161Given a list \texttt{P=[$p_0$,...,$p_{n-1}$]} of $n$ probabilities which 24162add to 1 (representing the probability that one of several mutually 24163exclusive events occurs), the \texttt{randmultinomial} command will 24164return an index whose probability is determined by the corresponding 24165multinomial distribution. If you enter 24166\begin{center} 24167 \tt 24168 randmultinomial([1/2, 1/3, 1/6]) 24169\end{center} 24170you might get 24171\begin{center} 24172 \tt 24173 0 24174\end{center} 24175If $K$ is a list of length $n$, then \texttt{randmultinomial($P$,$K$)} 24176will return an element of the list, whose index is chosen according to 24177the multinomial distribution. If you enter 24178\begin{center} 24179 \tt 24180 randmultinomial([1/2, 1/3, 1/6],["R","V","B"]) 24181\end{center} 24182you might get 24183\begin{center} 24184 \tt 24185 "R" 24186\end{center} 24187 24188\subsection{Producing random numbers with a Poisson distribution: \texttt{randpoisson}\index{randpoisson}} 24189 24190Recall that given a number $\lambda$, the corresponding Poisson 24191distribution $P(\lambda)$ satisfies 24192\[ \text{Prob}(X \le k) = \exp(-\lambda)\lambda^k/k!\] 24193It will have mean $\lambda$ and standard deviation $\sqrt{\lambda}$. 24194 24195The \texttt{randpoisson} command will take a parameter $\lambda$ and 24196return an integer chosen at random using the Poisson distribution. 24197If you enter 24198\begin{center} 24199 \tt 24200 randpoisson(10.6) 24201\end{center} 24202you might get 24203\begin{center} 24204 \tt 24205 16 24206\end{center} 24207 24208\subsection{Producing random numbers with a normal distribution: \texttt{randnorm}\index{randnorm} \texttt{randNorm}\index{randNorm}} 24209 24210The \texttt{randnorm} (or \texttt{randNorm}) command will choose a 24211random number according to a normal distribution. Given the mean 24212$\mu$ and standard deviation $\sigma$, 24213\texttt{randnorm($\mu$,$\sigma$)} will return a number chosen 24214according the normal distribution. If you enter 24215\begin{center} 24216 \tt 24217 randnorm(2,1) 24218\end{center} 24219you might get 24220\begin{center} 24221 \tt 24222 2.45598713143 24223\end{center} 24224 24225\subsection{Producing random numbers with an exponential distribution: \texttt{randexp}\index{randexp}} 24226 24227Recall that given a positive number $a$, the corresponding exponential 24228distribution satisfies 24229\[ \text{Prob}(X \le t) = a \int_0^t exp(-a * u) du\] 24230Given a parameter $a$, the command \texttt{randexp($a$)} will return a 24231number chosen randomly according to the corresponding exponential 24232distribution. For example, if you enter 24233\begin{center} 24234 \tt 24235 randexp(1) 24236\end{center} 24237you might get 24238\begin{center} 24239 \tt 24240 0.193354391918 24241\end{center} 24242 24243\subsection{Producing random matrices: \texttt{randmatrix}\index{randmatrix} \texttt{ranm}\index{ranm} \texttt{randMat}\index{randMat}} 24244 24245You can produce a random vector or matrix with the \texttt{randmatrix} 24246(or \texttt{ranm} or \texttt{randMat}) command. (See also sections 24247\ref{sec:ranm1} and \ref{sec:ranm2}.) The \texttt{randmatrix} command 24248has the following possible arguments. 24249\begin{description} 24250\item[An integer $n$] 24251With an integer $n$, \texttt{randmatrix($n$)} will return a vector of 24252length $n$ whose elements are integers chosen randomly from 24253$[-99,-98,\dots,98,99]$ with equal probability. If you enter 24254\begin{center} 24255 \tt 24256 randmatrix(5) 24257\end{center} 24258you might get 24259\begin{center} 24260 \tt 24261 [86,-97,-82,7,-27] 24262\end{center} 24263\item[Two integers $n$ and $p$] 24264Given two integers $n$ and $p$, \texttt{randmatrix($n$,$p$)} will 24265return an $n\times p$ matrix whose elements are integers chosen randomly from 24266$[-99,99]$ with equal probability. If you enter 24267\begin{center} 24268 \tt 24269 randmatrix(2,3) 24270\end{center} 24271you might get 24272\begin{center} 24273 \tt 24274 [[26,-89,63],[-49,-86,-64]] 24275\end{center} 24276\item[Three integers $n$, $p$ and $a$] 24277Given three integers $n$, $p$ and $a$, \texttt{randmatrix($n$,$p$,$a$)} will 24278return an $n\times p$ matrix whose elements are integers chosen randomly from 24279$[0,a)$ (or $(a,0]$ is $a$ is negative) with equal probability. If you enter 24280\begin{center} 24281 \tt 24282 randmatrix(2,3,10) 24283\end{center} 24284you might get 24285\begin{center} 24286 \tt 24287 [[4,7,6],[7,4,5]] 24288\end{center} 24289\item[Two integers $n$ and $p$, and an interval $a..b$.] 24290Given two integers $n$, $p$ and an $a..b$, 24291\texttt{randmatrix($n$,$p$,$a..b$)} will 24292return an $n\times p$ matrix whose elements are real numbers chosen randomly from 24293$[a,b)$ with equal probability. If you enter 24294\begin{center} 24295 \tt 24296 randmatrix(2,3,0..1) 24297\end{center} 24298you might get 24299\begin{center} 24300 \tt 24301 [[0.90923402831,0.594602484722,0.250897713937],[0.332611694932,0.145975249354,0.543010003399]] 24302\end{center} 24303\item[Two integers $n$ and $p$ and a function (which must be quoted) 24304to produce random numbers] 24305In this case, the third argument must be 24306one of \texttt{'rand($n$)'}, \texttt{'binomial($n$,$p$)'}, 24307\texttt{'binomial,$n$,$p$'}, \texttt{'randbinomial($n$,$p$)'}, 24308\texttt{'multinomial($P$,$K$)'}, \texttt{'multinomial,$P$,$K$'}, 24309\texttt{'randmultinomial($P$,$K$)'}, \texttt{'poisson($\lambda$)'}, 24310\texttt{'poisson, $\lambda$'}, \texttt{'randpoisson($\lambda$)'}, 24311\texttt{'normald($\mu$,$\sigma$)'}, \texttt{'normald,$\mu$,$\sigma$'}, 24312\texttt{'randnorm($\mu$,$\sigma$)'}, \texttt{'exp($a$)'}, 24313\texttt{'exp,$a$'}, \texttt{'randexp($a$)'}, 24314\texttt{'fisher($n$,$m$)'}, \texttt{'fisher,$n$,$m$'}, or 24315\textrm{'randfisher($n$,$m$)'}. 24316\end{description} 24317Given such an $R$, the command \texttt{randmatrix($n$,$p$,$R$)} will 24318return an $n\times p$ matrix whose elements are numbers chosen 24319randomly according to the rule determined by $R$. 24320If you enter 24321\begin{center} 24322 \tt 24323 randmatrix(2,3,'randnorm(2,1)') 24324\end{center} 24325you might get 24326\begin{center} 24327 \tt 24328 [[2.6324726358,0.539273367446,0.793750476229],[2.24729803442,1.28189228187,2.25750809791]] 24329\end{center} 24330 24331\subsection{Random variables : {\tt random\_variable\index{random\_variable} randvar\index{randvar}}} 24332 24333{\tt randvar} (alias: {\tt random\_variable}) takes a probability distribution specification as its argument and returns an object representing a random variable. Its value(s) can be generated subsequently by calling {\tt sample}, {\tt rand}, {\tt randvector} or {\tt randmatrix}. 24334 24335The probability distribution is specified as a sequence of arguments. The supported types are : uniform, normal, binomial, multinomial, negbinomial, Poisson, Student, Fisher-Snedecor, Cauchy, Weibull, beta, gamma, chi-square, geometric, exponential and discrete. 24336 24337\paragraph{Continuous distributions.} The usual way to specify a continuous distribution is to pass the probability density function as the first argument, followed by one or more (numeric) parameters. However, it can also be defined by specifying its type and first and/or second moment (the mean and/or the standard deviation/variance); the supported types are : normal, uniform, binomial, Poisson, geometric, exponential, gamma, beta and Weibull. Additionally, a uniform distribution can be defined by specifying its range as an interval. The arguments are entered in form: 24338\begin{itemize} 24339 \item {\tt mean=}$\mu$ 24340 \item {\tt stddev=}$\sigma$ 24341 \item {\tt variance=}$\sigma^2$ 24342 \item {\tt [range=]a..b} or {\tt range=[a,b]} 24343\end{itemize} 24344 24345\paragraph{Discrete distributions.} To create a discrete random variable one can pass either 24346\begin{itemize} 24347 \item a list $W=[w_1,w_2,\dots,w_n]$ of nonnegative weights as the first argument, optionally followed by a list of values $V=[v_1,v_2,\dots,v_n]$, 24348 \item a list of of object-weight pairs : $[[v_1,w_1],[v_2,w_2],\dots,[v_n,w_n]]$, or 24349 \item a nonnegative function $f$ followed by a range specification {\tt [range=]a..b} and optionally either a positive integer $N$ (with $a,b\in\mathbb{R}$) or a list of values $V=[v_0,v_1,v_2,\dots,v_n]$ where $n=b-a$, $a<b$ and $a,b\in\mathbb{Z}$. 24350\end{itemize} 24351The weights are automatically scaled by the inverse of their sum to obtain the values of the probability mass function. If a function $f$ is given instead of a list of weights, then $w_k=f(a+k)$ for $k=0,1,\dots,b-a$ unless $N$ is given, in which case $w_k=f(x_k)$ where $x_k=a+(k-1)\,\frac{b-a}{N}$ and $k=1,2,\dots,N$. The resulting random variable $X$ has values in $\{0,1,\dots,n-1\}$ for 0-based modes (e.g.~Xcas) resp.~in $\{1,2\dots,n\}$ for 1-based modes (e.g.~Maple). If the list $V$ of custom objects is given, then $V[X]$ is returned instead of $X$. If $N$ is given, then $v_k=x_k$ for $k=1,2,\dots,N$. 24352 24353\paragraph{Examples.} To define a random variable with a Fisher-Snedecor distribution (two degrees of freedom), input : 24354\begin{center} 24355 \tt X:=random\_variable(fisher,2,3) 24356\end{center} 24357Output : 24358\begin{center} 24359 \tt fisherd(2,3) 24360\end{center} 24361To generate some values of $X$, input : 24362\begin{center} 24363 \tt rand(X) // alternative : sample(X) 24364\end{center} 24365Output : 24366\begin{center} 24367 \tt 2.0457 24368\end{center} 24369Input : 24370\begin{center} 24371 \tt randvector(5,X) // alternative : sample(X,5) 24372\end{center} 24373Output : 24374\begin{center} 24375 \tt [3.9823,0.50771,0.44836,0.79225,0.088813] 24376\end{center} 24377To define a random variable with multinomial distribution, input : 24378\begin{center} 24379 \tt M:=randvar(multinomial,[1/2,1/3,1/6],[a,b,c]) 24380\end{center} 24381Output : 24382\begin{center} 24383 \tt 'multinomial',[1/2,1/3,1/6],[a,b,c] 24384\end{center} 24385Input : 24386\begin{center} 24387 \tt randvector(10,M) 24388\end{center} 24389Output : 24390\begin{center} 24391 \tt [a,c,b,a,b,b,a,b,b,b] 24392\end{center} 24393Some continuous distributions can be defined by specifying its first and/or second moment. Input : 24394\begin{center} 24395 \tt randvector(10,randvar(poisson,mean=5)) 24396\end{center} 24397Output : 24398\begin{center} 24399 \tt [5,4,4,8,3,8,3,3,5,9] 24400\end{center} 24401Input : 24402\begin{center} 24403 \tt randvector(5,randvar(weibull,mean=5.0,stddev=1.5)) 24404\end{center} 24405Output : 24406\begin{center} 24407 \tt [3.6483,3.4194,6.8166,4.3778,2.4178] 24408\end{center} 24409Input : 24410\begin{center} 24411 \tt X:=randvar(binomial,mean=18,stddev=4) 24412\end{center} 24413Output : 24414\begin{center} 24415 \tt binomial(162,1/9) 24416\end{center} 24417Input : 24418\begin{center} 24419 \tt X:=randvar(weibull,mean=12.5,variance=1) 24420\end{center} 24421Output : 24422\begin{center} 24423 \tt weibulld(3.0857,13.98) 24424\end{center} 24425Input : 24426\begin{center} 24427 \tt mean(randvector(1000,X)) 24428\end{center} 24429Output : 24430\begin{center} 24431 \tt 12.582 24432\end{center} 24433Input : 24434\begin{center} 24435 \tt G:=randvar(geometric,stddev=2.5) 24436\end{center} 24437Output : 24438\begin{center} 24439 \tt geometric(0.32792) 24440\end{center} 24441Input : 24442\begin{center} 24443 \tt evalf(stddev(randvector(1000,G))) 24444\end{center} 24445Output : 24446\begin{center} 24447 \tt 2.4245 24448\end{center} 24449Input : 24450\begin{center} 24451 \tt randvar(gammad,mean=12,variance=4) 24452\end{center} 24453Output : 24454\begin{center} 24455 \tt gammad(36,3) 24456\end{center} 24457Uniformly distributed random variables can be defined by specifying the support as an interval. Input : 24458\begin{center} 24459 \tt randvector(5,randvar(uniform,range=15..81)) 24460\end{center} 24461Output : 24462\begin{center} 24463 \tt [61.97,76.427,37.939,69.639,40.325] 24464\end{center} 24465Input : 24466\begin{center} 24467 \tt rand(randvar(uniform,e..pi)) 24468\end{center} 24469Output : 24470\begin{center} 24471 \tt 3.0434 24472\end{center} 24473The following examples demonstrate various ways to define a discrete random variable. Input : 24474\begin{center} 24475 \tt X:=randvar([["apple",1/3],["orange",1/4], ["pear",1/5],["plum",13/60]]):;\\randvector(5,X) 24476\end{center} 24477Output : 24478\begin{center} 24479 \tt ["apple","plum","pear","orange","apple","pear"] 24480\end{center} 24481Input : 24482\begin{center} 24483 \tt W:=[1,4,5,3,1,1,1,2]:; X:=randvar(W):;\\approx(W/sum(W)) 24484\end{center} 24485Output : 24486\begin{center} 24487 \tt [0.055556,0.22222,0.27778,0.16667, 0.055556,0.055556,0.055556,0.11111] 24488\end{center} 24489Input : 24490\begin{center} 24491 \tt frequencies(randvector(10000,X)) 24492\end{center} 24493Output : 24494\begin{center} 24495 \tt [[0,0.0566],[1,0.2152],[2,0.2798],[3,0.1683], [4,0.0594],[5,0.0564],[6,0.0568],[7,0.1075]] 24496\end{center} 24497Input : 24498\begin{center} 24499 \tt X:=randvar(k->1-(k/10)\verb|^|2,range=-10..10):;\\histogram(randvector(10000,X),-10,0.33,display=filled) 24500\end{center} 24501Output : 24502\begin{center} 24503 \includegraphics[width=0.75\textwidth]{random_hist1.png} 24504\end{center} 24505Input : 24506\begin{center} 24507 \tt X:=randvar([3,1,2,5],[alpha,beta,gamma,delta]):;\\randmatrix(5,4,X) 24508\end{center} 24509Output : 24510\[ \begin{vmatrix}\alpha&\beta&\delta&\delta\\ \delta&\alpha&\alpha&\alpha\\ \delta&\gamma&\alpha&\delta\\ 24511\delta&\alpha&\delta&\alpha\\ \alpha&\beta&\delta&\delta\end{vmatrix}\] 24512Discrete random variables can be used to approximate custom continuous random variables. For example, consider a probability density function $f$ as a mixture of two normal distributions on the support $S=[-10,10]$. We sample $f$ in $N=10000$ points in $S$. Input : 24513\begin{center} 24514 \tt F:=normald(3,2,x)+normald(-5,1,x):; c:=integrate(F,x=-10..10):;\\ 24515 f:=unapply(1/c*F,x):;\\ 24516 X:=randvar(f,range=-10..10,10000):; 24517\end{center} 24518Now we generate 25000 values of $X$ and plot a histogram : 24519\begin{center} 24520 \tt R:=sample(X,25000):;\\ 24521 hist:=histogram(R,-10,0.1):;\\ 24522 PDF:=plot(f(x),display=red+line\_width\_2):; 24523 hist,PDF 24524\end{center} 24525Output : 24526\begin{center} 24527 \includegraphics[width=0.75\textwidth]{random_hist2.png} 24528\end{center} 24529Sampling from discrete distributions is fast : generating 25 million samples from the distribution of X which 24530has about 10000 outcomes takes only couple of seconds. In fact, the sampling complexity is constant. 24531Also observe that the process isn't slowed down by spreading it across 1000 calls of randvector. Input : 24532\begin{center} 24533 \tt for k from 1 to 1000 do randvector(25000,X); od:; 24534\end{center} 24535{\tt Evaluation time: 2.12} 24536 24537Independent random variables can be combined in an expression, yielding a new random variable. 24538In the example below, we define a log-normally distributed variable Y from a variable X with standard 24539normal distribution. Input : 24540\begin{center} 24541 \tt X:=randvar(normal):; mu,sigma:=1.0,0.5:;\\Y:=exp(mu+sigma*X):;\\L:=randvector(10000,Y):; histogram(L,0,0.33) 24542\end{center} 24543Output : 24544\begin{center} 24545 \includegraphics[width=0.75\textwidth]{random_hist3.png} 24546\end{center} 24547It is known that $E[Y]=\mathrm{e}^{\mu+\sigma^2/2}$. The mean of $L$ should be close to that number. Input : 24548\begin{center} 24549 \tt mean(L); exp(mu+sigma\verb|^|2/2) 24550\end{center} 24551Output: 24552\begin{center} 24553 \tt 3.0789,3.0802 24554\end{center} 24555 24556In case a compound random variable is defined as an expression containing several independent random variables $X,Y,\dots$ of the same type, it is sometimes needed to prevent its evaluation when passing it to {\tt randvector} or {\tt randmatrix}. Input : 24557\begin{center} 24558 \tt Y:=randvar(normal):; 24559\end{center} 24560$X/Y$ is wrapped by eval because otherwise it would automatically reduce to 1 as $X$ and $Y$ are both {\tt normald}$(0,1)$. Input : 24561\begin{center} 24562 \tt randvector(5,eval(X/Y,0)) 24563\end{center} 24564Output : 24565\begin{center} 24566 \tt [0.2608,-0.056913,-4.7966,-1.2622,-1.2997] 24567\end{center} 24568To save typing, one can define Z with {\tt eval}$(\ast,0)$ and pass {\tt eval}$(Z,1)$ to {\tt randvector} or {\tt randmatrix}. Input : 24569\begin{center} 24570 \tt Z:=eval(X/Y,0):; randvector(5,eval(Z,1)) 24571\end{center} 24572Output : 24573\begin{center} 24574 \tt [0.19015,-2.4509,-1.4277,-1.1452,1.2935] 24575\end{center} 24576Parameters of a distribution can be entered as symbols to allow (re)assigning them at any time. Input : 24577\begin{center} 24578 \tt purge(lambda):; X:=randvar(exp,lambda):;\\lambda:=1:; 24579\end{center} 24580Now execute the following command line several times in a row. The parameter $\lambda$ is updated in each iteration : 24581\begin{center} 24582 \tt r:=rand(X); lambda:=sqrt(r) 24583\end{center} 24584Output (by executing the above command line three times) : 24585\begin{center} 24586 \tt 8.5682,2.9272\\1.5702,1.2531\\0.53244,0.72968 24587\end{center} 24588 24589\section{Density and distribution functions} 24590 24591\subsection{The binomial distribution} 24592 24593\subsubsection{The probability density function for the binomial 24594distribution : \texttt{binomial}\index{binomial}} 24595 24596If you perform an experiment $n$ times, where the probability of 24597success each time is $p$, then the probability of exactly $k$ 24598successes is 24599\[ \texttt{binomial($n$,$k$,$p$)} = \left(^n_k\right) p^k (1-p)^{n-k}\] 24600This determines the binomial distribution, and so this is called the 24601\texttt{binomial} command. If you enter 24602\begin{center} 24603 \tt 24604 binomial(10,2,0.4) 24605\end{center} 24606you will get 24607\begin{center} 24608 \tt 24609 0.120932352 24610\end{center} 24611If no third argument $p$ is given, then \texttt{binomial} will just 24612compute $\left(^n_k\right)$, which recall is called the binomial 24613coefficient and is also computed by \texttt{comb}. If you enter 24614\begin{center} 24615 \tt 24616 binomial(10,2) 24617\end{center} 24618or 24619\begin{center} 24620 \tt 24621 comb(10,2) 24622\end{center} 24623then you will get 24624\begin{center} 24625 \tt 24626 45 24627\end{center} 24628 24629\subsubsection{The cumulative distribution function for the binomial 24630distribution: \texttt{binomial\_cdf}\index{binomial\_cdf}} 24631 24632Recall that the cumulative distribution function (cdf) for a 24633distribution is $cdf(x) = \text{Prob}(X \le x)$. For the binomial 24634distribution, this is given by the \texttt{binomial\_cdf} command; 24635\texttt{binomial\_cdf($n$,$p$,$x$)}, which in this case will equal 24636\texttt{binomial($n$,$0$,$p$) + \ldots + binomial($n$,floor($x$,$p$)}. 24637If you enter 24638\begin{center} 24639 \tt 24640 binomial\_cdf(4,0.5,2) 24641\end{center} 24642you will get 24643\begin{center} 24644 \tt 24645 0.6875 24646\end{center} 24647You can give \texttt{binomial\_cdf} an additional argument; 24648\texttt{binomial\_cdf($n$,$p$,$x$,$y$) = Prob($x \le X \le y$)}, which 24649in this case would be 24650\texttt{binomial($n$,ceil($x$),$p$) + $\cdots$ + binomial($n$,floor($y$),$p$)}. 24651If you enter 24652\begin{center} 24653 \tt 24654 binomial\_cdf(2,0.3,1,2) 24655\end{center} 24656you will get 24657\begin{center} 24658 \tt 24659 0.51 24660\end{center} 24661 24662\subsubsection{The inverse distribution function for the binomial 24663distribution: \texttt{binomial\_icdf}\index{binomial\_icdf}} 24664 24665Given a value $h$, the inverse distribution function gives the value 24666of $x$ so that Prob($X \le x$) = $h$; or for discrete distributions, 24667the smallest $x$ so that Prob($X \le x$) $\ge h$. For the binomial 24668distribution with $n$ and $p$, the \texttt{binomial\_icdf} gives the 24669inverse distribution function. If you enter 24670\begin{center} 24671 \tt 24672 binomial\_icdf(4,0.5,0.9) 24673\end{center} 24674you will get 24675\begin{center} 24676 \tt 24677 3 24678\end{center} 24679Note that \texttt{binomial\_cdf(4,0.5,3)} 24680is $0.9375$, bigger than $0.9$, while 24681\texttt{binomial\_cdf(4,0.5,2)} is $0.6875$, smaller than $0.9$. 24682 24683\subsection{The negative binomial distribution} 24684 24685\subsubsection{The probability density function for the negative 24686binomial distribution: \texttt{negbinomial}\index{negbinomial}} 24687If you repeatedly perform an experiment with probability of success 24688$p$, then, given an integer $n$, the probability of $k$ failures that occur 24689before you have $n$ successes is given by the negative binomial 24690distribution, and can be computed with 24691\texttt{negbinomial($n$,$k$,$p$)}. It is given by the formula 24692$\left(^{n+k-1}_{k}\right)p^n(1-p)^k$. If you enter 24693\begin{center} 24694 \tt 24695 negbinomial(4,2,0.5) 24696\end{center} 24697you will get 24698\begin{center} 24699 \tt 24700 0.15625 24701\end{center} 24702 24703Note that 24704\[ 24705\left(^{n}_{k}\right) = \frac{n!}{k! (n-k)!} = \frac{n (n-1 ) \dots ( 24706n-k+1)}{k!} 24707\] 24708The second formula makes sense even if $n$ is negative, and you can 24709write 24710\texttt{negbinomial}$(n,k,p) = \left(^{-n}_{k}\right)p^n (p-1)^k$, from 24711which the name negative binomial distribution comes from. This also 24712makes it simple to determine the mean ($n(1-p)/p$) and variance 24713($n(1-p)/p^2$). The negative binomial is also called the Pascal 24714distribution (after Blaise Pascal) or the P\'{o}lya distribution 24715(after George P\'{o}lya). 24716 24717\subsubsection{The cumulative distribution function for the negative 24718binomial distribution: \texttt{negbinomial\_cdf}\index{negbinomial\_cdf}} 24719 24720The cumulative distribution function for the negative binomial 24721distribution is given by the \texttt{negbinomial\_cdf} command. Given 24722parameters $n$ and $p$, as above, then 24723\texttt{negbinomial\_cdf($n$,$p$,$x$) = Prob($X \le x$) = 24724negbinomial($n$,$0$,$p$) + \ldots + negbinomial($n$,floor($x$),$p$)}, 24725and 24726\texttt{negbinomial\_cdf($n$,$p$,$x$,$y$) = Prob($x \le X \le y$) = 24727negbinomial($n$,ceil($x$),$p$) + $\cdots$ + negbinomial($n$,floor($y$),$p$)}. 24728If you enter 24729\begin{center} 24730 \tt 24731 negbinomial\_cdf(4,0.5,2) 24732\end{center} 24733for example, you will get 24734\begin{center} 24735 \tt 24736 0.34375 24737\end{center} 24738 24739\subsubsection{The inverse distribution function for the negative 24740binomial distribution: \texttt{negbinomial\_icdf}\index{negbinomial\_icdf}} 24741 24742Given a value $h$, the inverse distribution function gives 24743the smallest value 24744of $x$ so that Prob($X \le x$) $\ge h$. 24745The \texttt{negbinomial\_icdf} gives the inverse 24746distribution function for the negative binomial distribution. 24747If you enter 24748\begin{center} 24749 \tt 24750 negbinomial\_icdf(4,0.5,0.9) 24751\end{center} 24752for example, you will get 24753\begin{center} 24754 \tt 24755 8 24756\end{center} 24757 24758\subsection{The multinomial probability function: \texttt{multinomial}\index{multinomial}} 24759 24760If $X$ follows a multinomial probability distribution with 24761$P = [p_0,p_1,\dots,p_j]$ (where $p_0 + \dots + p_j = 1$), 24762then for $K=[k_0,\dots,k_j]$ with $k_0 + \dots + k_j = n$, the 24763probability that $X=K$ is given by the \texttt{multinomial} command; 24764\[\texttt{multinomial}(n,P,K)= \frac{n!}{k_0!k_1!\dots 24765k_j!}(p_0^{k_0}p_1^{k_1}\dots p_j^{k_j}.\] 24766You will get an error if $k_0 + \dots + k_j$ is not equal to $n$, 24767although you won't get one if $p_0 + \dots + p_j$ is not equal to $1$. 24768 24769For example, if you make $10$ choices, where each choice is one of 24770three items; the first has a $0.2$ probability of being chosen, the 24771second a $0.3$ probability and the third a $0.5$ probability, the 24772probability that you end up with $3$ of the first item, $2$ of the 24773second and $5$ of the third will be 24774\begin{center} 24775 \tt 24776 multinomial(10,[0.2,0.3,0.5],[3,2,5]) 24777\end{center} 24778or 24779\begin{center} 24780 \tt 24781 0.0567 24782\end{center} 24783 24784\subsection{The Poisson distribution} 24785\subsubsection{The probability density function for the Poisson 24786distribution: \texttt{poisson}\index{poisson}} 24787 24788Recall that for the Poisson distribution with parameter $\mu$, the 24789probability of a non-negative integer $k$ is $e^{-\mu}\mu^k/k!$. It 24790will mean $\mu$ and variance $\mu$. The \texttt{poisson} command will 24791find this value, given $\mu$ and $k$. For example, 24792\begin{center} 24793 \tt 24794 poisson(10.0,9) 24795\end{center} 24796is 24797\begin{center} 24798 \tt 24799 0.125110035721 24800\end{center} 24801 24802\subsubsection{The cumulative distribution function for the Poisson 24803distribution: \texttt{poisson\_cdf}\index{poisson\_cdf}} 24804 24805The cumulative distribution function for the Poisson distribution is 24806given by the \texttt{poisson\_cdf} command with arguments $\mu$ and 24807$x$; \texttt{poisson\_cdf($\mu$,$x$) = Prob($X \le x$)}. If you enter 24808\begin{center} 24809 \tt 24810 poisson\_cdf(10.0,3) 24811\end{center} 24812you will get 24813\begin{center} 24814 \tt 24815 0.0103360506759 24816\end{center} 24817 24818With another argument, \texttt{poisson\_cdf} will find the probability 24819of falling between two values; 24820\texttt{poisson\_cdf($\mu$,$x$,$y$) = Prob($x \le X \le y$)}. 24821If you enter 24822\begin{center} 24823 \tt 24824 poisson\_cdf(10.0,3,10) 24825\end{center} 24826you will get 24827\begin{center} 24828 \tt 24829 0.580270354477 24830\end{center} 24831 24832\subsubsection{The inverse distribution function for the Poisson 24833distribution: \texttt{poisson\_icdf}\index{poisson\_icdf}} 24834Given a value $h$, the inverse distribution function gives 24835the smallest value 24836of $x$ so that Prob($X \le x$) $\ge h$. 24837Given arguments of a parameter $\mu$ and a value $x$, the 24838\texttt{poisson\_icdf} gives the inverse 24839distribution function for the poisson distribution. If you enter 24840\begin{center} 24841 \tt 24842 poisson\_icdf(10.0,0.975) 24843\end{center} 24844you will get 24845\begin{center} 24846 \tt 24847 17 24848\end{center} 24849 24850\subsection{Normal distributions} 24851\subsubsection{The probability density function for a normal 24852distribution: \texttt{normald}\index{normald} \texttt{loi\_normal}\index{loi\_normal}} 24853 24854The \texttt{normald} (or \texttt{loi\_normal}) command returns the 24855value of the normal probability density function. You can give it 24856arguments of the mean $\mu$, standard deviation $\sigma$ and a value 24857$x$ then 24858\[ 24859\texttt{normald}(\mu,\sigma,x) = \frac{1}{\sqrt{2\pi} \sigma} 24860e^{(x-\mu)^2/2} 24861\] 24862If you enter 24863\begin{center} 24864 \tt 24865 normald(2,1,3) 24866\end{center} 24867you will get 24868\begin{center} 24869 \tt 24870 exp(-1/2)/sqrt(2*pi) 24871\end{center} 24872 24873If you don't give the command values for $\mu$ and $\sigma$, then 24874\texttt{normald} will use the values $\mu=0$ and $\sigma = 1$, and so 24875compute the standard normal density function. If you enter 24876\begin{center} 24877 \tt 24878 normald(2) 24879\end{center} 24880you will get 24881\begin{center} 24882 \tt 24883 1/(sqrt(2*pi)*exp(2)) 24884\end{center} 24885 24886\subsubsection{The cumulative distribution function for normal 24887distributions: \texttt{normal\_cdf}\index{normal\_cdf} \texttt{normald\_cdf}\index{normald\_cdf}} 24888 24889The command \texttt{normal\_cdf} (or \texttt{normald\_cdf}) computes 24890the cumulative distribution function for the normal distribution. 24891Like \texttt{normald}, you can give it the mean and standard deviation 24892of the distribution; if you enter 24893\begin{center} 24894 \tt 24895 normal\_cdf(1,2,1.96) 24896\end{center} 24897you will get 24898\begin{center} 24899 \tt 24900 0.684386303484 24901\end{center} 24902You can also leave off the mean and standard deviation, in which case 24903\texttt{normal\_cdf} will compute the cumulative distribution function 24904for the standard normal distribution; 24905\begin{center} 24906 \tt 24907 normal\_cdf(1,2.1,1.2) 24908\end{center} 24909you will get 24910\begin{center} 24911 \tt 24912 0.537937144066 24913\end{center} 24914 24915If you give \texttt{normal\_cdf} an extra argument (with or without 24916the mean and standard deviation), you will get the probability that 24917the random variable lies between two values; 24918$\texttt{normal\_cdf($x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 24919enter 24920\begin{center} 24921 \tt 24922 normal\_cdf(1,2.1,1.2,9) 24923\end{center} 24924you will get 24925\begin{center} 24926 \tt 24927 0.461993238584 24928\end{center} 24929 24930\subsubsection{The inverse distribution function for normal 24931distributions: \texttt{normal\_icdf}\index{normal\_icdf} \texttt{normald\_icdf}\index{normald\_icdf}} 24932Given a value $h$, the inverse distribution function gives 24933the value of $x$ with Prob($X \le x$) $\le h$. 24934The \texttt{normal\_icdf} (or \texttt{normald\_icdf}) will compute the 24935inverse distribution for the normal distribution. If no 24936mean or standard deviation are given, the standard normal distribution 24937will be used. If you enter 24938\begin{center} 24939 \tt 24940 normal\_icdf(0.975) 24941\end{center} 24942you will get 24943\begin{center} 24944 \tt 24945 1.95996398454 24946\end{center} 24947You can, of course, also give the mean and standard deviation. If you 24948enter 24949\begin{center} 24950 \tt 24951 normal\_icdf(1,2,0.495) 24952\end{center} 24953you will get 24954\begin{center} 24955 \tt 24956 0.974933060984 24957\end{center} 24958 24959\subsubsection{The upper tail cumulative function for normal 24960distributions: \texttt{UTPN}\index{UTPN}} 24961 24962The \texttt{UTPN} (the Upper Tail Probability - Normal distribution) will compute 24963$\text{Prob}(X > x)$. If you don't give it a mean and variance, then 24964it will compute the probability for the standard normal distribution. 24965If you enter 24966\begin{center} 24967 \tt 24968 UTPN(1.96) 24969\end{center} 24970you will get 24971\begin{center} 24972 \tt 24973 0.0249978951482 24974\end{center} 24975You can also specify a mean and a variance, but note that unlike 24976\texttt{normald} and \texttt{normal\_cdf}, the \texttt{UTPN} requires 24977the variance and not the standard deviation. If you enter 24978\begin{center} 24979 \tt 24980 UTPN(1,4,1.96) 24981\end{center} 24982you will get 24983\begin{center} 24984 \tt 24985 0.315613696516 24986\end{center} 24987 24988\subsection{Student's distribution} 24989 24990\subsubsection{The probability density function for Student's 24991distribution: \texttt{student}\index{student} \texttt{studentd}\index{studentd}} 24992 24993Student's distribution (also called Student's $t$-distribution or just 24994the $t$-distribution) with 24995$n$ degrees of freedom has density function given by 24996\[ 24997\texttt{student}(n,x) = 24998\frac{\Gamma((n+1)/2)}{\Gamma(n/2)\sqrt{n\pi}}\left(1 + 24999\frac{x^2}{n}\right)^{-n-1/2} 25000\] 25001where recall the Gamma function is defined for $x>0$ by $\Gamma(x) = 25002\int_0^\infty e^{-t}t^{x-1}dx$. If you enter 25003\begin{center} 25004 \tt 25005 student(2,3) 25006\end{center} 25007you will get 25008\begin{center} 25009 \tt 25010 sqrt(pi)/(11*sqrt(2*pi)*sqrt(11/2)) 25011\end{center} 25012which can be numerically approximated by 25013\begin{center} 25014 \tt 25015 evalf(student(2,3)) 25016\end{center} 25017which is 25018\begin{center} 25019 \tt 25020 0.0274101222343 25021\end{center} 25022 25023\subsubsection{The cumulative distribution function for Student's 25024distribution: \texttt{student\_cdf}\index{student\_cdf}} 25025 25026The cumulative distribution function for Student's distribution with 25027$n$ degrees of freedom at a value $x$ is 25028$\texttt{student\_cdf}(n,x) = \texttt{Prob}(X \le x)$; if you 25029enter 25030\begin{center} 25031 \tt 25032 student\_cdf(5,2) 25033\end{center} 25034you will get 25035\begin{center} 25036 \tt 25037 0.949030260585 25038\end{center} 25039 25040If you give \texttt{student\_cdf} an extra argument, 25041you will get the probability that 25042the random variable lies between two values; 25043$\texttt{student\_cdf($n$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 25044enter 25045\begin{center} 25046 \tt 25047 student\_cdf(5,-2,2) 25048\end{center} 25049you will get 25050\begin{center} 25051 \tt 25052 0.89806052117 25053\end{center} 25054 25055\subsubsection{The inverse distribution function for Student's 25056distribution: \texttt{student\_icdf}\index{student\_icdf}} 25057 25058The inverse distribution function for Student's 25059distribution with $n$ degrees of freedom is computed with 25060\texttt{student\_icdf($n$,$h$)}; recall that this will return the 25061value $x$ with $\texttt{student\_cdf}(n,x) = h$. If you enter 25062\begin{center} 25063 \tt 25064 student\_icdf(5,0.95) 25065\end{center} 25066you will get 25067\begin{center} 25068 \tt 25069 2.01504837333 25070\end{center} 25071 25072 25073\subsubsection{The upper tail cumulative function for Student's 25074distribution: \texttt{UTPT}\index{UTPT}} 25075 25076The \texttt{UTPT} (the Upper Tail Probability - T distribution) will compute 25077$\text{Prob}(X > x)$. 25078If you enter 25079\begin{center} 25080 \tt 25081 UTPT(5,2) 25082\end{center} 25083you will get 25084\begin{center} 25085 \tt 25086 0.0509697394149 25087\end{center} 25088 25089\subsection{The $\chi^2$ distribution} 25090 25091\subsubsection{The probability density function for the $\chi^2$ 25092distribution: \texttt{chisquare}\index{chisquare}} 25093 25094The $\chi^2$ distribution with 25095$n$ degrees of freedom has density function given by 25096\[ 25097\texttt{chisquare}(n,x) = 25098\frac{x^{n/2-1}e^{-x/2}}{2^{n/2}\Gamma(n/2)} 25099\] 25100If you enter 25101\begin{center} 25102 \tt 25103 chisquare(5,2) 25104\end{center} 25105you will get 25106\begin{center} 25107 \tt 25108 2*sqrt(2)/(exp(1)*sqrt(2)*3*sqrt(pi)) 25109\end{center} 25110which can be numerically approximated by 25111\begin{center} 25112 \tt 25113 evalf(chisquare(5,2)) 25114\end{center} 25115which is 25116\begin{center} 25117 \tt 25118 0.138369165807 25119\end{center} 25120 25121\subsubsection{The cumulative distribution function for the $\chi^2$ 25122distribution: \texttt{chisquare\_cdf}\index{chisquare\_cdf}} 25123 25124The cumulative distribution function for the $\chi^2$ distribution with 25125$n$ degrees of freedom at a value $x$ is 25126$\texttt{chisquare\_cdf}(n,x) = \texttt{Prob}(X \le x)$; if you 25127enter 25128\begin{center} 25129 \tt 25130 chisquare\_cdf(5,11) 25131\end{center} 25132you will get 25133\begin{center} 25134 \tt 25135 0.948620016517 25136\end{center} 25137 25138If you give \texttt{chisquare\_cdf} an extra argument, 25139you will get the probability that 25140the random variable lies between two values; 25141$\texttt{chisquare\_cdf($n$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 25142enter 25143\begin{center} 25144 \tt 25145 chisquare\_cdf(3,1,2) 25146\end{center} 25147you will get 25148\begin{center} 25149 \tt 25150 0.22884525243 25151\end{center} 25152 25153\subsubsection{The inverse distribution function for the $\chi^2$ 25154distribution: \texttt{chisquare\_icdf}\index{chisquare\_icdf}} 25155 25156The inverse distribution function for the $\chi^2$ 25157distribution with $n$ degrees of freedom is computed with 25158\texttt{chisquare\_icdf($n$,$h$)}; recall that this will return the 25159value $x$ with $\texttt{chisquare\_cdf}(n,x) = h$. If you enter 25160\begin{center} 25161 \tt 25162 chisquare\_icdf(5,0.95) 25163\end{center} 25164you will get 25165\begin{center} 25166 \tt 25167 11.0704976935 25168\end{center} 25169 25170\subsubsection{The upper tail cumulative function for the $\chi^2$ 25171distribution: \texttt{UTPC}\index{UTPC}} 25172 25173The \texttt{UTPC} (the Upper Tail Probability - Chi-square distribution) will compute 25174$\text{Prob}(X > x)$. 25175If you enter 25176\begin{center} 25177 \tt 25178 UTPC(5,11) 25179\end{center} 25180you will get 25181\begin{center} 25182 \tt 25183 0.0513799834831 25184\end{center} 25185 25186\subsection{The Fisher-Sn\'{e}d\'{e}cor distribution} 25187 25188\subsubsection{The probability density function for the Fisher-Sn\'{e}d\'{e}cor distribution: \texttt{fisher}\index{fisher} \texttt{fisherd}\index{fisherd} \texttt{snedecor}\index{snedecor} \texttt{snedecord}\index{snedecord}} 25189 25190The Fisher-Sn\'{e}d\'{e}cor distribution (also called the F-distribution) 25191with $n_1$ and $n_2$ degrees of freedom has density function given by 25192for $x \ge 0$, 25193\[ 25194\texttt{fisher}(n_1,n_2,x) = 25195\frac{(n_1/n_2)^{n_1/2}\Gamma((n_1+n_2)/2)}{\Gamma(n_1/2)\Gamma(n_2/2)} 25196\frac{x^{(n_1-2)/2}}{(1+(n_1/n_2)x)^{(n_1+n_2)/2}} 25197\] 25198(The \texttt{snecedor} command is the same as the \texttt{fisher} 25199command.) 25200If you enter 25201\begin{center} 25202 \tt 25203 fisher(5,3,2.5) 25204\end{center} 25205you will get 25206\begin{center} 25207 \tt 25208 0.10131184472 25209\end{center} 25210 25211\subsubsection{The cumulative distribution function for the Fisher-Sn\'{e}d\'{e}cor distribution: \texttt{fisher\_cdf}\index{fisher\_cdf} \texttt{snedecor\_cdf}\index{snedecor\_cdf}} 25212 25213The cumulative distribution function for the Fisher-Sn\'{e}d\'{e}cor 25214distribution with $n_1$ and $n_2$ degrees of freedom at a value $x$ is 25215$\texttt{fisher\_cdf}(n_1,n_2,x) = \texttt{snedecor}(n_1,n_2,x) = 25216\texttt{Prob}(X \le x)$; if you enter 25217\begin{center} 25218 \tt 25219 fisher\_cdf(5,3,9) 25220\end{center} 25221you will get 25222\begin{center} 25223 \tt 25224 Beta(5/2,3/2,15/16,1) 25225\end{center} 25226which can be numerically approximated with 25227\begin{center} 25228 \tt 25229 evalf(fisher\_cdf(5,3,9,10)) 25230\end{center} 25231which is 25232\begin{center} 25233 \tt 25234 0.949898927032 25235\end{center} 25236 25237\subsubsection{The inverse distribution function for the Fisher-Sn\'{e}d\'{e}cor distribution: \texttt{fisher\_icdf}\index{fisher\_icdf} \texttt{snedecor\_icdf}\index{snedecor\_icdf}} 25238 25239The inverse distribution function for the 25240Fisher-Sn\'{e}d\'{e}cor 25241distribution with $n_1$ and $n_2$ degrees of freedom is computed with 25242\texttt{fisher\_icdf($n_1$,$n_2$,$h$)}; recall that this will return the 25243value $x$ with $\texttt{fisher\_cdf}(n_1,n_2,x) = h$. If you enter 25244\begin{center} 25245 \tt 25246 fisher\_icdf(5,3,0.95) 25247\end{center} 25248you will get 25249\begin{center} 25250 \tt 25251 9.01345516752 25252\end{center} 25253 25254\subsubsection{The upper tail cumulative function for the Fisher-Sn\'{e}d\'{e}cor distribution: \texttt{UTPF}\index{UTPF}} 25255 25256The \texttt{UTPF} (the Upper Tail Probability - 25257Fisher-Sn\'{e}d\'{e}cor distribution) will compute 25258$\text{Prob}(X > x)$. 25259If you enter 25260\begin{center} 25261 \tt 25262 UTPF(5,3,9) 25263\end{center} 25264you will get 25265\begin{center} 25266 \tt 25267 0.050101072968 25268\end{center} 25269 25270\subsection{The gamma distribution} 25271\subsubsection{The probability density function for the gamma distribution: \texttt{gammad}\index{gammad}} 25272 25273The gamma distribution depends on two parameters, $a>0$ and $b>0$; the 25274value of the density function at $x \ge 0$ is 25275$\texttt{gammad}(a,b,x) = x^{a-1}e^{-bx}b^a/\Gamma(a)$. If you enter 25276\begin{center} 25277 \tt 25278 gammad(2,1,3) 25279\end{center} 25280for example, you will get 25281\begin{center} 25282 \tt 25283 3/exp(3) 25284\end{center} 25285 25286\subsubsection{The cumulative distribution function for the gamma distribution: \texttt{gammad\_cdf}\index{gammad\_cdf}} 25287 25288The cumulative distribution function for the gamma distribution with 25289parameters $a$ and $b$ at a value $x$ is 25290$\texttt{gammad\_cdf}(n,x) = \texttt{Prob}(X \le x)$. It turns out that 25291$\texttt{gammad\_cdf}(n,x) = \texttt{igamma}(a, bx, 1)$ where 25292\texttt{igamma} is the incomplete gamma function; 25293$\texttt{igamma}(a,x,1) = \int_0^x e^{-t}t^{a-1}dt/\Gamma(a)$. 25294If you 25295enter 25296\begin{center} 25297 \tt 25298 gammad\_cdf(2,1,0.5) 25299\end{center} 25300you will get 25301\begin{center} 25302 \tt 25303 0.090204010431 25304\end{center} 25305 25306If you give \texttt{gammad\_cdf} an extra argument, 25307you will get the probability that 25308the random variable lies between two values; 25309$\texttt{gammad\_cdf($a$,$b$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 25310enter 25311\begin{center} 25312 \tt 25313 gammad\_cdf(2,1,0.5,1.5) 25314\end{center} 25315you will get 25316\begin{center} 25317 \tt 25318 0.351970589198 25319\end{center} 25320 25321\subsubsection{The inverse distribution function for the gamma distribution: \texttt{gammad\_icdf}\index{gammad\_icdf}} 25322 25323The inverse distribution function for the gamma 25324distribution with parameters $a$ and $b$ is computed with 25325\texttt{gammad\_icdf($a$,$b$,$h$)}; recall that this will return the 25326value $x$ with $\texttt{gammad\_cdf}(a,b,x) = h$. If you enter 25327\begin{center} 25328 \tt 25329 gammad\_icdf(2,1,0.5) 25330\end{center} 25331you will get 25332\begin{center} 25333 \tt 25334 1.67834699002 25335\end{center} 25336 25337\subsection{The beta distribution} 25338 25339\subsubsection{The probability density function for the beta distribution: \texttt{betad}\index{betad}} 25340 25341The beta distribution depends on two parameters, $a>0$ and $b>0$; the 25342value of the density function at $x$ in $[0,1]$ is 25343$\texttt{betad}(a,b,x) = 25344\Gamma(a+b)x^{a-1}(1-x)^{b-1}/(\Gamma(a)\Gamma(b))$. If you enter 25345\begin{center} 25346 \tt 25347 betad(2,1,0.3) 25348\end{center} 25349for example, you will get 25350\begin{center} 25351 \tt 25352 0.6 25353\end{center} 25354 25355\subsubsection{The cumulative distribution function for the beta distribution: \texttt{betad\_cdf}\index{betad\_cdf}} 25356 25357The cumulative distribution function for the beta distribution with 25358parameters $a$ and $b$ at a value $x$ in $[0,1]$ is 25359$\texttt{betad\_cdf}(a,b,x) = \texttt{Prob}(X \le x)$. It turns out 25360that $\texttt{betad\_cdf}(a,b,x) = 25361\beta(a,b,x)\Gamma(a+b)/(\Gamma(a)\Gamma(b))$ where 25362$\beta(a,b,x) = \int_0^x t^{a-1}(1-t)^{b-1} dt$. 25363If you 25364enter 25365\begin{center} 25366 \tt 25367 betad\_cdf(2,3,0.2) 25368\end{center} 25369for example, you will get 25370\begin{center} 25371 \tt 25372 0.1808 25373\end{center} 25374 25375If you give \texttt{betad\_cdf} an extra argument $y$, also in $[0,1]$, 25376you will get the probability that 25377the random variable lies between the two values; 25378$\texttt{betad\_cdf($a$,$b$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. 25379If you enter 25380\begin{center} 25381 \tt 25382 betad\_cdf(2,3,0.25,.5) 25383\end{center} 25384you will get 25385\begin{center} 25386 \tt 25387 0.42578125 25388\end{center} 25389 25390\subsubsection{The inverse distribution function for the beta distribution: \texttt{betad\_icdf}\index{betad\_icdf}} 25391 25392The inverse distribution function for the beta 25393distribution with parameters $a$ and $b$ is computed with 25394\texttt{betad\_icdf($a$,$b$,$h$)}; recall that this will return the 25395value $x$ with $\texttt{betad\_cdf}(a,b,x) = h$. If you enter 25396\begin{center} 25397 \tt 25398 betad\_icdf(2,3,0.2) 25399\end{center} 25400you will get 25401\begin{center} 25402 \tt 25403 0.212317128278 25404\end{center} 25405 25406\subsection{The geometric distribution} 25407 25408\subsubsection{The probability density function for the geometric 25409distribution: \texttt{geometric}\index{geometric}} 25410 25411If an experiment with probability of success $p$ is iterated, the 25412probability that the first success occurs on the $k$th trial is 25413$(1-p)^{k-1}p$. This gives the geometric distribution (with parameter 25414$p$) on the natural numbers. Given such a $p$, the geometric density 25415function at $n$ is given by $\texttt{geometric}(p,n) = (1-p)^{n-1}p$. 25416If you enter 25417\begin{center} 25418 \tt 25419 geometric(0.2,3) 25420\end{center} 25421for example, you will get 25422\begin{center} 25423 \tt 25424 0.128 25425\end{center} 25426 25427\subsubsection{The cumulative distribution function of the geometric 25428distribution: \texttt{geometric\_cdf}\index{geometric\_cdf}} 25429 25430The cumulative distribution function for the geometric distribution with 25431parameter $p$ at a natural number $n$ is 25432$\texttt{geometric\_cdf}(p,n) = \texttt{Prob}(X \le n)$, which in this 25433case turns out to be $\texttt{geometric\_cdf}(p,n) = 254341 - (1-p)^n$. 25435If you enter 25436\begin{center} 25437 \tt 25438 geometric\_cdf(0.2,3) 25439\end{center} 25440for example, you will get 25441\begin{center} 25442 \tt 25443 0.488 25444\end{center} 25445 25446If you give \texttt{geometric\_cdf} an extra argument $k$, also a 25447natural number, you will get the probability that 25448the random variable lies between the two values; 25449$\texttt{geometric\_cdf($p$,$n$,$k$)} = \text{Prob}(n \le X \le k)$. 25450If you enter 25451\begin{center} 25452 \tt 25453 geometric\_cdf(0.2,3,5) 25454\end{center} 25455you will get 25456\begin{center} 25457 \tt 25458 0.31232 25459\end{center} 25460 25461\subsubsection{The inverse distribution function for the 25462geometric distribution: \texttt{geometric\_icdf}\index{geometric\_icdf}} 25463 25464The inverse distribution function for the geometric 25465distribution with parameter $p$ is computed with 25466\texttt{geometric\_icdf($p$,$h$)}; recall that this will return the 25467smallest natural number $n$ with $\texttt{geometric\_cdf}(p,n) \ge h$. If you enter 25468\begin{center} 25469 \tt 25470 geometric\_icdf(0.2,0.5) 25471\end{center} 25472you will get 25473\begin{center} 25474 \tt 25475 4 25476\end{center} 25477 25478\subsection{The Cauchy distribution} 25479 25480\subsubsection{The probability density function for the Cauchy 25481distribution: \texttt{cauchy}\index{cauchy} \texttt{cauchyd}\index{cauchyd}} 25482 25483The probability density function of the Cauchy distribution (sometimes 25484called the Lorentz distribution) is given by the 25485\texttt{cauchy} (or \texttt{cauchyd}) command. The Cauchy 25486distribution depends on two parameters $a$ and $b$, and the value of 25487the density function at $x$ is 25488$\texttt{cauchy}(a,b,x) = b/(\pi ((x-a)^2 + b^2))$. 25489If you enter 25490\begin{center} 25491 \tt 25492 cauchy(2.2,1.5,0.8) 25493\end{center} 25494you will get 25495\begin{center} 25496 \tt 25497 0.113412073462 25498\end{center} 25499 25500If you leave out the parameters $a$ and $b$, they will default to $0$ 25501and $1$, respectively; $\texttt{cauchy}(x) = 1/(\pi (x^2 + 1))$. If 25502you enter 25503\begin{center} 25504 \tt 25505 cauchy(0.3) 25506\end{center} 25507you will get 25508\begin{center} 25509 \tt 25510 0.292027418517 25511\end{center} 25512 25513\subsubsection{The cumulative distribution function for the Cauchy 25514distribution: \texttt{cauchy\_cdf}\index{cauchy\_cdf} \texttt{cauchyd\_cdf}\index{cauchyd\_cdf}} 25515 25516The command \texttt{cauchy\_cdf} (or \texttt{cauchyd\_cdf}) computes 25517the cumulative distribution function for the Cauchy distribution. 25518Like \texttt{cauchy}, you can give it the parameters $a$ and $b$, or 25519let them default to $0$ and $1$. The Cauchy cumulative distribution 25520function is given by the formula 25521$\texttt{cauchy\_cdf}(a,b,x) = 1/2 + \arctan((x-a)/b)/\pi$. 25522If you enter 25523\begin{center} 25524 \tt 25525 cauchy\_cdf(2,3,1.4) 25526\end{center} 25527you will get 25528\begin{center} 25529 \tt 25530 0.437167041811 25531\end{center} 25532and if you enter 25533\begin{center} 25534 \tt 25535 cauchy\_cdf(1.4) 25536\end{center} 25537you will get 25538\begin{center} 25539 \tt 25540 0.802568456711 25541\end{center} 25542 25543If you give \texttt{cauchy\_cdf} an extra argument (with or without 25544the parameters), you will get the probability that 25545the random variable lies between two values; 25546$\texttt{cauchy\_cdf($a$,$b$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 25547enter 25548\begin{center} 25549 \tt 25550 cauchy\_cdf(2,3,-1.9,1.4) 25551\end{center} 25552you will get 25553\begin{center} 25554 \tt 25555 0.228452641651 25556\end{center} 25557 25558\subsubsection{The inverse distribution function for the 25559Cauchy distribution: \texttt{cauchy\_icdf}\index{cauchy\_icdf} \texttt{cauchyd\_icdf}\index{cauchyd\_icdf}} 25560 25561Given a value $h$, the inverse distribution function gives 25562the value of $x$ with $\texttt{Prob}(X \le x) = h$. 25563The \texttt{cauchy\_icdf} will compute the 25564inverse distribution for the Cauchy distribution. (If no 25565parameters are given, they will be assumed to be 0 and 1.) 25566If you enter 25567\begin{center} 25568 \tt 25569 cauchy\_icdf(2,3,0.23) 25570\end{center} 25571you will get 25572\begin{center} 25573 \tt 25574 -1.40283204777 25575\end{center} 25576 25577\subsection{The uniform distribution} 25578\subsubsection{The probability density function for the uniform 25579distribution: \texttt{uniform}\index{uniform} \texttt{uniformd}\index{uniformd}} 25580 25581Given two values $a$ and $b$ with $a < b$, the uniform distribution on 25582$[a,b]$ has density function $1/(b-a)$ for $x$ in $[a,b]$. The 25583\texttt{uniform} (or \texttt{uniformd}) command will compute this; 25584$\texttt{uniform}(a,b,x) = 1/(b-a)$. If you enter 25585\begin{center} 25586 \tt 25587 uniform(2.2,3.5,2.8) 25588\end{center} 25589you will get 25590\begin{center} 25591 \tt 25592 0.769230769231 25593\end{center} 25594 25595\subsubsection{The cumulative distribution function for the uniform 25596distribution: \texttt{uniform\_cdf}\index{uniform\_cdf} \texttt{uniformd\_cdf}\index{uniformd\_cdf}} 25597 25598Given two values $a$ and $b$ with $a <b$, the cumulative distribution 25599function for the uniform distribution on $[a,b]$ is (for $x$ in $[a,b]$) 25600$\texttt{uniform\_cdf}(a,b,x) = \texttt{Prob}(X \le x) = (x-a)/(b-a)$. 25601If you enter 25602\begin{center} 25603 \tt 25604 uniform\_cdf(2,4,3.2) 25605\end{center} 25606you will get 25607\begin{center} 25608 \tt 25609 0.6 25610\end{center} 25611 25612With an extra argument $y$ in $[a,b]$, \texttt{uniform\_cdf} will 25613compute $\texttt{uniform\_cdf}(a,b,x,y) = \texttt{Prob}(x \le X \le y) 25614= (y-x)/(b-a)$. If you enter 25615\begin{center} 25616 \tt 25617 uniform\_cdf(2,4,3,3.2) 25618\end{center} 25619you will get 25620\begin{center} 25621 \tt 25622 0.1 25623\end{center} 25624 25625\subsubsection{The inverse distribution function for the 25626uniform distribution: \texttt{uniform\_icdf}\index{uniform\_icdf} \texttt{uniformd\_icdf}\index{uniformd\_icdf}} 25627 25628Given a value $h$, the inverse distribution function for a uniform 25629distribution is the value of $x$ with $\texttt{Prob}(X \le x) = 25630\texttt{uniform\_cdf}(a,b,x) = h$. This value is computed with the 25631\texttt{uniform\_icdf} command. If you enter 25632\begin{center} 25633 \tt 25634 uniform\_icdf(2,3,.6) 25635\end{center} 25636you will get 25637\begin{center} 25638 \tt 25639 2.6 25640\end{center} 25641 25642\subsection{The exponential distribution} 25643 25644\subsubsection{The probability density function for the exponential distribution: \texttt{exponential}\index{exponential} \texttt{exponentiald}\index{exponentiald}} 25645 25646The exponential distribution depends on one parameters, $\lambda>0$; the 25647value of the density function at $x \ge 0$ is 25648$\texttt{exponential}(\lambda,x) = 25649\lambda e^{-\lambda x}$. If you enter 25650\begin{center} 25651 \tt 25652 exponential(2.1,3.5) 25653\end{center} 25654for example, you will get 25655\begin{center} 25656 \tt 25657 0.00134944395675 25658\end{center} 25659 25660\subsubsection{The cumulative distribution function for the exponential distribution: \texttt{exponential\_cdf}\index{exponential\_cdf} \texttt{exponentiald\_cdf}\index{exponentiald\_cdf}} 25661 25662The cumulative distribution function for the exponential distribution with 25663parameter $\lambda > 0$ at a value $x \ge 0$ is 25664$\texttt{exponential\_cdf}(\lambda,x) = \texttt{Prob}(X \le x)$. 25665If you enter 25666\begin{center} 25667 \tt 25668 exponential\_cdf(2.3,3.2) 25669\end{center} 25670for example, you will get 25671\begin{center} 25672 \tt 25673 0.99936380154 25674\end{center} 25675 25676If you give \texttt{exponential\_cdf} an extra argument $y > x$, 25677you will get the probability that 25678the random variable lies between the two values; 25679$\texttt{exponential\_cdf($\lambda$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. 25680If you enter 25681\begin{center} 25682 \tt 25683 exponential\_cdf(2.3,0.9,3.2) 25684\end{center} 25685you will get 25686\begin{center} 25687 \tt 25688 0.125549583246 25689\end{center} 25690 25691\subsubsection{The inverse distribution function for the exponential distribution: \texttt{exponential\_icdf}\index{exponential\_icdf} \texttt{exponentiald\_icdf}\index{exponentiald\_icdf}} 25692 25693The inverse distribution function for the exponential 25694distribution with parameter $\lambda > 0$ is computed with 25695\texttt{exponential\_icdf($\lambda$,$h$)}; recall that this will return the 25696value $x$ with $\texttt{exponential\_cdf}(\lambda,x) = h$. If you enter 25697\begin{center} 25698 \tt 25699 exponential\_icdf(2.3,0.87) 25700\end{center} 25701you will get 25702\begin{center} 25703 \tt 25704 0.887052534142 25705\end{center} 25706 25707\subsection{The Weibull distribution} 25708\subsubsection{The probability density function for the Weibull 25709distribution: \texttt{weibull}\index{weibull} 25710\texttt{weibulld}\index{weibulld}} 25711 25712The Weibull distribution depends on three parameters; $k>0$, $\lambda 25713> 0$ and a real number $\theta$. The probability density at $x$ is 25714given by $\frac{k}{\lambda}(\frac{x - \theta}{\lambda})^2 25715e^{-((x-\theta)\lambda)^2}$. The \texttt{weibull} (or 25716\texttt{weibulld}) command computes this, where it can take arguments 25717$k$,$\lambda$,$\theta$ and $x$, where the $\theta$ can be left out and 25718will default to 0. If you enter 25719\begin{center} 25720 \tt 25721 weibull(2,1,3) 25722\end{center} 25723or 25724\begin{center} 25725 \tt 25726 weibull(2,1,0,3) 25727\end{center} 25728you will get 25729\begin{center} 25730 \tt 25731 6/exp(9) 25732\end{center} 25733 25734\subsubsection{The cumulative distribution function for the Weibull distribution: \texttt{weibull\_cdf}\index{weibull\_cdf} \texttt{weibulld\_cdf}\index{weibulld\_cdf}} 25735 25736The command \texttt{weibull\_cdf} computes 25737the cumulative distribution function for the Weibull distribution. 25738Like \texttt{weibull}, it takes parameters $k$, $\lambda$ and 25739$\theta$, where $\theta$ will default to 1 if it is omitted. 25740The Weibull cumulative distribution 25741function is given by the formula 25742$\texttt{weibull\_cdf}(k,\lambda,\theta,x) = 1 - 25743e^{-((x-\theta)/\lambda)^2}$. 25744If you enter 25745\begin{center} 25746 \tt 25747 weibull\_cdf(2,3,5) 25748\end{center} 25749or 25750\begin{center} 25751 \tt 25752 weibull\_cdf(2,3,0,5) 25753\end{center} 25754you will get 25755\begin{center} 25756 \tt 25757 1-exp(-25/9) 25758\end{center} 25759and if you enter 25760\begin{center} 25761 \tt 25762 weibull\_cdf(2.2,1.5,0.4,1.9) 25763\end{center} 25764you will get 25765\begin{center} 25766 \tt 25767 0.632120558829 25768\end{center} 25769 25770If you give \texttt{weibull\_cdf} an extra argument (which will 25771require that $\theta$ be explicitly included), you will get the probability that 25772the random variable lies between two values; 25773$\texttt{weibull\_cdf($k$,$\lambda$,$\theta$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 25774enter 25775\begin{center} 25776 \tt 25777 weibull\_cdf(2.2,1.5,0.4,1.2,1.9) 25778\end{center} 25779for example you will get 25780\begin{center} 25781 \tt 25782 0.410267239944 25783\end{center} 25784 25785\subsubsection{The inverse distribution function for the Weibull distribution: \texttt{weibull\_icdf}\index{weibull\_icdf} \texttt{weibulld\_icdf}\index{weibulld\_icdf}} 25786 25787Given a value $h$, the inverse distribution function gives 25788the value of $x$ with $\texttt{Prob}(X \le x) = h$. 25789The \texttt{weibull\_icdf} command will compute the 25790inverse distribution for the Weibull distribution. This uses the 25791arguments $k$, $\lambda$ and $\theta$ as well as $h$, although 25792$\theta$ can be omitted and will default to 0. 25793If you enter 25794\begin{center} 25795 \tt 25796 weibull\_icdf(2.2,1.5,0.4,0.632) 25797\end{center} 25798you will get 25799\begin{center} 25800 \tt 25801 1.89977657604 25802\end{center} 25803 25804\subsection{The Kolmogorov-Smirnov distribution: \texttt{kolmogorovd}\index{kolmogorovd}} 25805 25806For real $x$, the \texttt{kolmogorovd} command computes the density 25807function for the Kolmogorov-Smirnov distribution. 25808\[ 25809\texttt{kolmogorovd}(x) = 1 - 2\sum_{k=1}^{\infty} (-1)^{k-1} e^{-k^2 x^2} 25810\] 25811If you enter 25812\begin{center} 25813 \tt 25814 kolmogorovd(1.36) 25815\end{center} 25816for example, you will get 25817\begin{center} 25818 \tt 25819 0.950514123245 25820\end{center} 25821 25822\subsection{The Wilconon or Mann-Whitney distribution} 25823\subsection{The Wilconon test polynomial: \texttt{wilcoxonp}\index{wilcoxonp}} 25824 25825The \texttt{wilcoxonp} command will compute the polynomial for the 25826Wilcoxon or Mann-Whitney test; it can take one or two parameters. If 25827you enter 25828\begin{center} 25829 \tt 25830 wilcoxonp(4) 25831\end{center} 25832you will get 25833\begin{center} 25834 \tt 25835 poly1[1/16,1/16,1/16,1/8,1/8,1/8,1/8,1/8,1/16,1/16,1/16] 25836\end{center} 25837and if you enter 25838\begin{center} 25839 \tt 25840 wilcoxonp(4,3) 25841\end{center} 25842you will get 25843\begin{center} 25844 \tt 25845 poly1[1/35,1/35,2/35,3/35,4/35,4/35,1/7,4/35,4/35,3/35,2/35,1/35,1/35] 25846\end{center} 25847 25848\subsubsection{The Wilcoxon/Mann-Whitney statistic: \texttt{wilcoxons}\index{wilcoxons}} 25849 25850Given two lists, or one list and a real number (a median), the 25851\texttt{wilcoxons} command will return the Wilcoxon or Mann-Whitney statistic. 25852If you enter 25853\begin{center} 25854 \tt 25855 wilcoxons([1,3,4,5,7,8,8,12,15,17],10) 25856\end{center} 25857you will get 25858\begin{center} 25859 \tt 25860 18 25861\end{center} 25862and if you enter 25863\begin{center} 25864 \tt 25865 wilcoxons([1,3,4,5,7,8,8,12,15,17],[2,6,10,11,13,14,15,18,19,20]) 25866\end{center} 25867you will get 25868\begin{center} 25869 \tt 25870 128.5 25871\end{center} 25872 25873\subsubsection{The Wilcoxon or Mann-Whitney test: \texttt{wilcoxont}} 25874\index{wilcoxont} 25875 25876The \texttt{wilcoxont} command will perform the Wilcoxon or 25877Mann-Whitney test, given two samples or one sample and a number (a 25878median). It can additionally take an optional third argument of a 25879function and an optional fourth argument of a real number. 25880If you enter 25881\begin{center} 25882 \tt 25883 wilcoxont([1,2,3,4,5,7,8,8,12,15,17],[2,6,10,11,13,14,15,18,19,20]) 25884\end{center} 25885you will get 25886\begin{verbatim} 25887 Mann-Whitney 2-sample test, H0 same Median, H1 <> 25888 ranksum 93.0, shifted ranksum 27.0 25889 u1=83 ,u2=27, u=min(u1,u2)=27 25890 Limit value to reject H0 26 25891 P-value 9055/176358 (0.0513444244094), alpha=0.05 H0 not rejected 25892 1 25893\end{center} 25894If you enter 25895\begin{center} 25896 \tt 25897 wilcoxont([1,3,4,5,7,8,8,12,15,17],[2,6,10,11,13,14,15,18,19,20],0.3) 25898\end{center} 25899you will get 25900\begin{verbatim} 25901 Mann-Whitney 2-sample test, H0 same Median, H1 <> 25902 ranksum 81.5, shifted ranksum 26.5 25903 u1=73.5 ,u2=26.5, u=min(u1,u2)=26.5 25904 Limit value to reject H0 35 25905 P-value 316/4199 (0.0752560133365), alpha=0.3 H0 rejected 25906 0 25907\end{verbatim} 25908and if you enter 25909\begin{center} 25910 \tt 25911 wilcoxont([1,3,4,5,7,8,8,12,15,17] ,10,`>`,0.05) 25912\end{center} 25913you will get 25914\begin{verbatim} 25915 Wilcoxon 1-sample test, H0 Median=10, H1 M<>10 25916 Wilcoxon statistic: 18, p-value: 0.375, confidence level: 0.05 25917 1 25918\end{verbatim} 25919 25920\subsection{Moment generating functions for probability distributions: \texttt{mgf}\index{mgf}} 25921 25922The \texttt{mgf} command will compute the moment generating function 25923for a probability distribution (such as normal, binomial, poisson, 25924beta, gamma). It takes as arguments the name of the distribution and 25925any necessary parameters. To find the moment generating function 25926for the standard normal distribution, you can enter 25927\begin{center} 25928 \tt 25929 mgf(normald,1,0) 25930\end{center} 25931and get 25932\begin{center} 25933 \tt 25934 exp(t) 25935\end{center} 25936If you enter 25937\begin{center} 25938 \tt 25939 mgf(binomial,n,p) 25940\end{center} 25941you will get 25942\begin{center} 25943 \tt 25944 (1-p+p*exp(t))\^{}n 25945\end{center} 25946 25947\subsection{Cumulative distribution functions: \texttt{cdf}\index{cdf}} 25948 25949The \texttt{cdf} command will take as arguments the name of a 25950probability distribution, along with any needed parameters, and return 25951an expression for the cumulative distribution function. If you enter 25952\begin{center} 25953 \tt 25954 cdf(normald,0,1) 25955\end{center} 25956you will get 25957\begin{center} 25958 \tt 25959 (erf(x*sqrt(2)/2)+1)/2 25960\end{center} 25961 25962You can evaluate the cumulative distribution function at a value by 25963adding the value as an argument; if you enter 25964\begin{center} 25965 \tt 25966 cdf(binomial,10,0.5,4) 25967\end{center} 25968you will get 25969\begin{center} 25970 \tt 25971 0.376953125 25972\end{center} 25973 25974\subsection{Inverse distribution functions: \texttt{icdf}\index{icdf}} 25975 25976The \texttt{icdf} command will take as arguments the name of a 25977probability distribution, along with any needed parameters, and return 25978an expression for the inverse cumulative distribution function. This 25979is typically most useful if you evaluate the inverse cumulative 25980function at a specific value by adding it as an argument. If you enter 25981\begin{center} 25982 \tt 25983 icdf(normald,0,0.5,0.975) 25984\end{center} 25985you will get 25986\begin{center} 25987 \tt 25988 0.97998199227 25989\end{center} 25990 25991\subsection{Kernel density estimation : {\tt kernel\_density\index{kernel\_density}}, {\tt kde\index{kde}}} 25992{\tt kernel\_density} (alias : {\tt kde}) accepts a list of samples $L=[X_1,X_2,\dots,X_n]$ and optionally a sequence of options. It performs kernel density estimation\footnote{For the details on kernel density estimation and its implementation see~: Artur Gramacki, {\it Nonparametric Kernel Density Estimation and Its Computational Aspects}, Springer, 2018.} (KDE), optionally restricted to an interval $[a,b]$, to obtain an estimate $\hat{f}$ of the (unknown) probability density function $f$ from which the samples are drawn, defined by~: 25993\begin{equation}\label{eq:kde1} 25994 \hat{f}(x)=\frac{1}{n\,h}\,\sum_{i=1}^nK\left(\frac{x-X_i}{h}\right), 25995\end{equation} 25996where $K$ is the Gaussian kernel $K(u)=\frac{1}{\sqrt{2\,\pi}}\,\exp\left(-\frac{1}{2}\,u^2\right)$ and $h$ is the positive real parameter called the \emph{bandwidth}. 25997 25998The supported options are listed below. 25999\begin{itemize} 26000 \item {\tt output=<type>} or {\tt Output=<type>} : specifies the form of the return value $\hat{f}$, where {\tt <type>} may be 26001 \begin{itemize} 26002 \item {\tt exact} : $\hat{f}$ is returned as the sum of Gaussian kernels, i.e.~as the right side of~\eqref{eq:kde1}, which is usable only when the number of samples is relatively small (up to few hundreds), 26003 \item {\tt piecewise} : $\hat{f}$ is returned as a piecewise expression obtained by the spline interpolation of the specified degree (by default, the interpolation is linear) on the interval $[a,b]$ segmented to the specified number of bins, 26004 \item {\tt list} (the default) : $\hat{f}$ is returned in discrete form, as a list of values $\hat{f}\left(a+k\,\frac{b-a}{M-1}\right)$ for $k=0,1,\dots,M$, where $M$ is the number of bins. 26005 \end{itemize} 26006 \item {\tt bandwidth=<value>} : specifies the bandwidth. {\tt <value>} may be 26007 \begin{itemize} 26008 \item a positive real number $h$, 26009 \item {\tt select} (the default) : bandwidth is selected using a direct plug-in method, 26010 \item {\tt gauss} or {\tt normal} or {\tt normald} : the Silverman's rule of thumb is used for selecting bandwidth (this method is fast but the results are close to optimal ones only when $f$ is approximately normal). 26011 \end{itemize} 26012 \item {\tt bins=<posint>} (by default 100) : the number of bins for simplifying the input data. Only the number if samples in each bin is stored. Bins represent the elements of an equidistant segmentation of the interval $S$ on which KDE is performed. This allows evaluating kernel summations using convolution when {\tt output} is set to {\tt piecewise} or {\tt list}, which significantly lowers the computational burden for large values of $n$ (say, few hundreds or more). If {\tt output} is set to {\tt exact}, this option is ignored. 26013 \item {\tt [range=]a..b} or {\tt range=[a,b]} or {\tt x=a..b} : the interval $[a,b]$ on which KDE is performed. If an identifier $x$ is specified, it is used as the variable of the output. If the range endpoints are not specified, they are set to $a=\min_{1\leq i\leq n} X_i-3\,h$ and $b=\max_{1\leq i\leq n}X_i+3\,h$ (unless {\tt output} is set to {\tt exact}, in which case this option is ignored). 26014 \item {\tt interp=<posint>} (by default 1) : the degree of the spline interpolation, ignored unless {\tt output} is set to {\tt piecewise}. 26015 \item {\tt spline=<posint>} : sets {\tt option} to {\tt piecewise} and {\tt interp} to {\tt <posint>}. 26016 \item {\tt eval=x0} : only the value $\hat{f}(x_0)$ is returned (this cannot be used with {\tt output} set to {\tt list}). 26017 \item an unassigned identifier {\tt x} (by default $x$) : the variable of the output. 26018 \item {\tt exact} : the same as {\tt output=exact}. 26019 \item {\tt piecewise} : the same as {\tt output=piecewise}. 26020\end{itemize} 26021 26022\paragraph{Examples.} 26023Input : 26024\begin{center} 26025 \tt kernel\_density([1,2,3,2],bandwidth=1/4,exact) 26026\end{center} 26027Output : 26028\begin{center} 26029 \tt 0.4*(exp(-8*(x-3)\verb|^|2)+2*exp(-8*(x-2)\verb|^|2)+exp(-8*(x-1)\verb|^|2)) 26030\end{center} 26031Input : 26032\begin{center} 26033 \tt f:=unapply(normald(4,1,x)/2+normald(7,1/2,x)/2,x); plot(f(x),x=0..10) 26034\end{center} 26035Output : 26036\begin{center} 26037 \includegraphics[width=0.75\textwidth]{kde_plot1.png} 26038\end{center} 26039Input : 26040\begin{center} 26041 \tt X:=randvar(f,range=0..10,1000):; S:=sample(X,1000):; F:=kernel\_density(S,piecewise):; plot([F,f(x)],x=0..10, display=[line\_width\_2+blue,line\_width\_1+black]) 26042\end{center} 26043Output : 26044\begin{center} 26045 \includegraphics[width=0.75\textwidth]{kde_plot2.png} 26046\end{center} 26047Input : 26048\begin{center} 26049 \tt kernel\_density(S,bins=50,spline=3,eval=4.75) 26050\end{center} 26051Output : 26052\begin{center} 26053 \tt 0.14655478136 26054\end{center} 26055Input : 26056\begin{center} 26057 \tt time(kernel\_density(sample(X,1e5),piecewise)) 26058\end{center} 26059Output : 26060\begin{center} 26061 \tt "Done",[0.17,0.1653323] 26062\end{center} 26063Input : 26064\begin{center} 26065 \tt S:=sample(X,5000):; sqrt(int((f(x)-kde(S,piecewise))\verb|^|,x=0..10)) 26066\end{center} 26067Output : 26068\begin{center} 26069 \tt 0.0269841239243 26070\end{center} 26071Input : 26072\begin{center} 26073 \tt S:=sample(X,25000):; sqrt(int((f(x)-kde(S,bins=150,piecewise))\verb|^|2,x=0..10)) 26074\end{center} 26075Output : 26076\begin{center} 26077 \tt 0.0144212781377 26078\end{center} 26079 26080\subsection{Distribution fitting by maximum likelihood : {\tt fitdistr\index{fitdistr}}} 26081{\tt fitdistr} takes two arguments, a list $L$ of presumably independent and identically distributed samples and a distribution type, which may be normal, exponential, Poisson, geometric, gamma, beta, Cauchy or Weibull. The type is specified as {\tt normal} ({\tt normald}), {\tt exp} ({\tt exponential} or {\tt exponentiald}), {\tt poisson}, {\tt geometric}, {\tt gammad}, {\tt betad}, {\tt cauchy} ({\tt cauchyd}) or {\tt weibull} ({\tt weibulld}), respectively. The command returns the distribution of the specified type with parameters that fit the given samples most closely according to the method of maximum likelihood. 26082 26083For example, input : 26084\begin{center} 26085 \tt S:=:; fitdistr(randvector(1000,weibulld,1/2,1),weibull) 26086\end{center} 26087Output : 26088\begin{center} 26089 \tt weibulld(0.498920254339,0.971148738409) 26090\end{center} 26091Input : 26092\begin{center} 26093 \tt X:=randvar(normal,stddev=9.5):; Y:=randvar(normal,stddev=1.5):; S:=sample(eval(X/Y,0),1000):; Z:=fitdistr(S,cauchy) 26094\end{center} 26095Output : 26096\begin{center} 26097 \tt cauchyd(-0.13160176167,6.2569300393) 26098\end{center} 26099Input : 26100\begin{center} 26101 \tt histogram(select(x->(x>-100 and x<100),S)); plot(Z(x),x=-100..100,display=red+line\_width\_2) 26102\end{center} 26103Output : 26104\begin{center} 26105 \includegraphics[width=0.75\textwidth]{fitdistr.png} 26106\end{center} 26107Input : 26108\begin{center} 26109 \tt kolmogorovt(S,Z) 26110\end{center} 26111Output : 26112\begin{center} 26113 \tt ["D=",0.0125864995943,"K=", 0.398020064869, "1-kolmogorovd(K)=",0.997387219452] 26114\end{center} 26115The Kolmogorov-Smirnov test indicates that the samples from $S$ are drawn from $Z$ with high probability. 26116 26117Fitting a lognormal distribution to samples $x_1,x_2,\dots,x_n$ can be done by fitting a normal distribution to the sample logarithms $\log x_1,\log x_2,\dots,\log x_n$ because log-likelihood functions are the same. For example, generate some samples according to the lognormal rule with parameters $\mu=5$ and $\sigma^2=2$ : 26118\begin{center} 26119 \tt X:=randvar(normal,mean=5,variance=2):; S:=sample(exp(X),1000):; 26120\end{center} 26121Now fit normal distribution to $\log S$ : 26122\begin{center} 26123 \tt Y:=fitdistr(log(S),normal) 26124\end{center} 26125Output : 26126\begin{center} 26127 \tt normald(5.04754808715,1.42751619912) 26128\end{center} 26129The mean of $Y$ is about $5.05$ and the variance is about $2.04$. Now the variable $Z=\exp(Y)$ has the sought lognormal distribution. 26130 26131\subsection{Markov chains: \texttt{markov}\index{markov}} 26132 26133Given the transition matrix of a Markov chain, the \texttt{markov} 26134command will compute characteristic features of the chain. If $M$ is 26135a transition matrix, then \texttt{markov($M$)} will return the list of 26136the positive recurrent states, the list of corresponding 26137invariant probabilities, the list of other strong connected 26138components, the list of probabilities of ending up in the sequence of 26139recurrent states. 26140For example, if you enter 26141\begin{center} 26142 \tt 26143 markov([[0,0,1/2,0,1/2],[0,0,1,0,0],[1/4,1/4,0,1/4,1/4],[0,0,1/2,0,1/2],[0,0,0,0,1]]) 26144\end{center} 26145you will get 26146\begin{center} 26147 \tt 26148 [[4]],[[0,0,0,0,1]],[[3,1,2,0]],[[1],[1],[1],[1],[1]] 26149\end{center} 26150 26151\subsection{Generating a random walks: \texttt{randmarkov}\index{randmarkov}} 26152 26153Given the transition matrix $M$ for a Markov chain and an initial 26154state $i_0$, the command \texttt{randmarkov($M$,$i_0$,$n$)} will 26155generate a random walk (given as a vector) starting at $i_0$ and 26156taking $n$ random steps, where each step is a transition with 26157probabilities given by $M$. For example, if you enter 26158\begin{center} 26159 \tt 26160 randmarkov([[0,1/2,0,1/2],[0,1,0,0],[1/4,1/4,1/4,1/4],[0,0,1/2,1/2]],2,10) 26161\end{center} 26162you might get 26163\begin{center} 26164 \tt 26165 [2,3,2,0,3,2,2,0,3,2,0] 26166\end{center} 26167 26168Alternatively, given a vector $v = [n_1,\dots,n_p]$, the command 26169\texttt{randmatrix($v$,$i_0$)} will create a stochastic matrix with 26170$p$ recurrent loops (given by $v$) and $i_0$ transient states. If you 26171enter 26172\begin{center} 26173 \tt 26174 randmarkov([1,2],2) 26175\end{center} 26176you might get 26177\begin{center} 26178 \tt 26179 [[1.0,0.0,0.0,0.0,0.0], 26180 [0.0,0.289031975209,0.710968024791,0.0,0.0], 26181 [0.0,0.46230383289,0.53769616711,0.0,0.0], 26182 [0.259262238137,0.149948861946,0.143448150524,0.242132758802,0.205207990592], 26183 [0.231568633749,0.145429586345,0.155664673778,0.282556511895,0.184780594232]] 26184\end{center} 26185 26186\section{Hypothesis testing} 26187 26188\subsection{General} 26189 26190Given a random variable $X$, you may want to know whether some 26191effective parameter $p$ is the same as some expected value $p_0$. You 26192will then want to test the hypothesis $p = p_0$, which will be the 26193null hypothesis $H_0$. The alternative hypothesis will be $H_1$. 26194The tests are: 26195\begin{description} 26196 \item[Two-tailed test] This test will reject the hypothesis $H_0$ 26197 if the relevant statistic is outside of a determined interval. 26198 This can be denoted '!='. 26199 \item[Left-tailed test] This test will reject the hypothesis $H_0$ 26200 if the relevant statistic is less than a specific value. 26201 This can be denoted '<'. 26202 \item[Right-tailed test] This test will reject the hypothesis $H_0$ 26203 if the relevant statistic is greater than a specific value. 26204 This can be denoted '>'. 26205\end{description} 26206 26207\subsection{Testing the mean with the Z test: \texttt{normalt}\index{normalt}} 26208 26209The \texttt{normalt} command will use the Z test to test the mean of 26210data. 26211You need to provide the command with the following arguments: 26212\begin{enumerate} 26213 \item 26214 The sample data information can be given as a list $[n_s,n_e]$ 26215 consisting of the number of successes $n_s$ and the number of trials 26216 $n_e$, or a list $[m,t]$ consisting of the mean $m$ and the sample 26217 size $t$, or a data list of the sample. 26218 \item 26219 The mean of the population to or a data list from a control sample. 26220 \item 26221 The standard deviation of the population. If the data list from a 26222 control sample is provided, then this item is unnecessary. 26223 \item 26224 The type of test; "!=","<" or ">". 26225 \item 26226 The confidence level. This is optional; the default 26227 value is $0.05$. 26228\end{enumerate} 26229The \texttt{normalt} command will return the result of a Z test. It 26230will return 0 if the test fails, 1 if the test succeeds, and it will 26231display a summary of the test. 26232 26233If you enter 26234\begin{center} 26235 \tt 26236 normalt([10,30], 0.5, 0.02, '!=', 0.1) 26237\end{center} 26238you will get 26239\begin{verbatim} 26240 *** TEST RESULT 0 *** 26241 Summary Z-Test null hypothesis H0 mu1=mu2, alt. hyp. H1 mu1!=mu2. 26242 Test returns 0 if probability to observe data is less than 0.1 26243 (null hyp. mu1=mu2 rejected with less than alpha probability error) 26244 Test returns 1 otherwise (can not reject null hypothesis) 26245 Data mean mu1=10, population mean mu2=0.5 26246 alpha level 0.1, multiplier*stddev/sqrt(sample size)= 1.64485*0.02/5.47723 26247 0 26248\end{verbatim} 26249If you enter 26250\begin{center} 26251 \tt 26252 normalt([0.48,50],0.5,0.1,'<') 26253\end{center} 26254you will get 26255\begin{verbatim} 26256 *** TEST RESULT 1 *** 26257 Summary Z-Test null hypothesis H0 mu1=mu2, alt. hyp. H1 mu1<mu2. 26258 Test returns 0 if probability to observe data is less than 0.05 26259 (null hyp. mu1=mu2 rejected with less than alpha probability error) 26260 Test returns 1 otherwise (can not reject null hypothesis) 26261 Data mean mu1=0.48, population mean mu2=0.5 26262 alpha level 0.05, multiplier*stddev/sqrt(sample size)= 1.64485*0.1/7.07107 26263 1 26264\end{verbatim} 26265 26266\subsection{Testing the mean with the T test: \texttt{studentt}\index{studentt}} 26267 26268The \texttt{studentt} command will examine whether data conforms to 26269Student's distribution. For small sample sizes, the \texttt{studentt} 26270test is preferable to \texttt{normalt}. 26271You need to provide the \texttt{studentt} command with the following 26272arguments: 26273\begin{enumerate} 26274 \item 26275 The sample data information can be given as a list $[n_s,n_e]$ 26276 consisting of the number of successes $n_s$ and the number of trials 26277 $n_e$, or a list $[m,t]$ consisting of the mean $m$ and the sample 26278 size $t$, or a data list of the sample. 26279 \item 26280 The mean of the population to or a data list from a control sample. 26281 \item 26282 The standard deviation of the population. If the data list from a 26283 control sample is provided, then this item is unnecessary. 26284 \item 26285 The type of test; "!=","<" or ">". 26286 \item 26287 The confidence level. This is optional; the default 26288 value is $0.05$. 26289\end{enumerate} 26290The \texttt{studentt} command will return the result of a T test. It 26291will return 0 if the test fails, 1 if the test succeeds, and it will 26292display a summary of the test. 26293 26294If you enter 26295\begin{center} 26296 \tt 26297 studentt([10,20], 0.5, 0.02, '!=', 0.1) 26298\end{center} 26299you will get 26300\begin{verbatim} 26301 *** TEST RESULT 0 *** 26302 Summary T-Test null hypothesis H0 mu1=mu2, alt. hyp. H1 mu1!=mu2. 26303 Test returns 0 if probability to observe data is less than 0.1 26304 (null hyp. mu1=mu2 rejected with less than alpha probability error) 26305 Test returns 1 otherwise (can not reject null hypothesis) 26306 Data mean mu1=10, population mean mu2=0.5, degrees of freedom 20 26307 alpha level 0.1, multiplier*stddev/sqrt(sample size)= 1.32534*0.02/4.47214 26308 0 26309\end{verbatim} 26310If you enter 26311\begin{center} 26312 \tt 26313 studentt([0.48,20],0.5,0.1,'<') 26314\end{center} 26315you will get 26316\begin{verbatim} 26317 *** TEST RESULT 1 *** 26318 Summary T-Test null hypothesis H0 mu1=mu2, alt. hyp. H1 mu1<mu2. 26319 Test returns 0 if probability to observe data is less than 0.05 26320 (null hyp. mu1=mu2 rejected with less than alpha probability error) 26321 Test returns 1 otherwise (can not reject null hypothesis) 26322 Data mean mu1=0.48, population mean mu2=0.5, degrees of freedom 20 26323 alpha level 0.05, multiplier*stddev/sqrt(sample size)= 1.72472*0.1/4.47214 26324 1 26325\end{verbatim} 26326 26327\subsection{Testing a distribution with the $\chi^2$ distribution: \texttt{chisquaret}\index{chisquaret}} 26328 26329The \texttt{chisquaret} command will use the $\chi^2$ test to compare 26330sample data to a specified distribution. 26331You need to provide \texttt{chisquaret} with the following arguments: 26332\begin{enumerate} 26333 \item A list of sample data. 26334 \item The name of a distribution, or another list of sample data. 26335 If this is omitted, a uniform distribution will be used. 26336 \item The parameters of the distribution, if a name is given as the 26337 previous argument, or the parameter \texttt{class} followed by 26338 \texttt{class\_min} and \texttt{class\_dim} (or the default values 26339 will be used). 26340\end{enumerate} 26341The \texttt{chisquaret} command will return the result of the $\chi^2$ 26342test between the sample data and the named distribution or the two 26343sample data. 26344 26345For example, if you enter 26346\begin{center} 26347 \tt 26348 chisquaret([57,54]) 26349\end{center} 26350you will get 26351\begin{verbatim} 26352 Guessing data is the list of number of elements in each class, 26353 adequation to uniform distribution 26354 Sample adequation to a finite discrete probability distribution 26355 Chi2 test result 0.0810810810811, 26356 reject adequation if superior to chisquare_icdf(1,0.95)=3.84145882069 or chisquare_icdf(1,1-alpha) if alpha!=5% 26357 0.0810810810811 26358\end{verbatim} 26359If you enter 26360\begin{center} 26361 \tt 26362 chisquaret([1,1,1,1,1,0,0,1,0,1,1],[.4,.6]) 26363\end{center} 26364you will get 26365\begin{verbatim} 26366 Sample adequation to a finite discrete probability distribution 26367 Chi2 test result 0.742424242424, 26368 reject adequation if superior to chisquare_icdf(1,0.95)=3.84145882069 26369 or chisquare_icdf(1,1-alpha) if alpha!=5% 26370 0.742424242424 26371\end{verbatim} 26372If you enter 26373\begin{center} 26374 \tt 26375 chisquaret(ranv(1000,binomial,10,.5),binomial) 26376\end{center} 26377you will get 26378\begin{verbatim} 26379 Binomial: estimating n and p from data 10 0.5055 26380 Sample adequation to binomial(10,0.5055,.), Chi2 test result 7.77825189838, 26381 reject adequation if superior to chisquare_icdf(7,0.95)=14.0671404493 26382 or chisquare_icdf(7,1-alpha) if alpha!=5% 26383 7.77825189838 26384\end{verbatim} 26385and if you enter 26386\begin{center} 26387 \tt 26388 chisquaret(ranv(1000,binomial,10,.5),binomial,11,.5) 26389\end{center} 26390you will get 26391\begin{verbatim} 26392 Sample adequation to binomial(11,0.5,.), Chi2 test result 125.617374161, 26393 reject adequation if superior to chisquare_icdf(10,0.95)=18.3070380533 26394 or chisquare_icdf(10,1-alpha) if alpha!=5% 26395 125.617374161 26396\end{verbatim} 26397For an example using \texttt{class\_min} and \texttt{class\_dim}, let 26398\begin{center} 26399 \tt 26400 L := ranv(1000,normald,0,.2) 26401\end{center} 26402If you then enter 26403\begin{center} 26404 \tt 26405 chisquaret(L,normald,classes,-2,.25) 26406\end{center} 26407or equivalently set \texttt{class\_min} to $-2$ and 26408\texttt{class\_dim} to $-0.25$ in the graphical configuration and enter 26409\begin{center} 26410 \tt 26411 chisquaret(L,normald,classes) 26412\end{center} 26413you will get 26414\begin{verbatim} 26415 Normal density, 26416 estimating mean and stddev from data -0.00345919752912 0.201708100832 26417 Sample adequation to normald_cdf(-0.00345919752912,0.201708100832,.), 26418 Chi2 test result 2.11405080381, 26419 reject adequation if superior to chisquare_icdf(4,0.95)=9.48772903678 26420 or chisquare_icdf(4,1-alpha) if alpha!=5% 26421 2.11405080381 26422\end{verbatim} 26423In this last case, you are given the value of $d^2$ of the statistic 26424$D^2 = \sum_{j=1}^{k} (n_j - e_j)/e_j$, where $k$ is the number of 26425sample classes for \texttt{classes(L,-2,0.25)} (or 26426\texttt{classes(L)}), $n_j$ is the size of the $j$th class, and $e_j = 26427n p_j$ where $n$ is the size of \texttt{L} and $p_j$ is the 26428probability of the $j$th class interval assuming a normal distribution 26429with the mean and population standard deviation of \texttt{L}. 26430 26431\subsection{Testing a distribution with the Kolmogorov-Smirnov 26432distribution: \texttt{kolmogorovt}\index{kolmogorovt}} 26433 26434The \texttt{kolmogorovt} command will use the Kolmogorov test to compare 26435sample data to a specified continuous distribution. 26436You need to provide \texttt{kolmogorovt} with either two lists of data 26437or a list of data followed by the name of a distribution with the 26438parameters. 26439The \texttt{kolmogorovt} command will return three values: 26440\begin{itemize} 26441 \item The $D$ statistic, which is the maximum distance between the 26442 cumulative distribution functions of the samples or the sample and 26443 the given distribution. 26444 \item The $K$ value, where $K = D\sqrt{n}$ (for a single data set, 26445 where $n$ is the size of the data set) or $K=D\sqrt{n_1 n_2 /(n_1 + 26446 n_2)}$ (when there are two data sets, with sizes $n_1$ and $n_2$). 26447 The $K$ value will tend towards the Kolmogorov-Smirnov distribution 26448 as the size of the data set goes to infinity. 26449 \item \texttt{1 - kolmogorovd(K)}, which will be close to 1 when the 26450 distributions look like they match. 26451\end{itemize} 26452For example, if you enter 26453\begin{center} 26454 \tt 26455 kolmogorovt(randvector(100,normald,0,1),normald(0,1)) 26456\end{center} 26457you might get 26458\begin{center} 26459 \tt 26460 ["D=",0.112592987625,"K=",1.12592987625,"1-kolmogorovd(K)=",0.158375510292] 26461\end{center} 26462and if you enter 26463\begin{center} 26464 \tt 26465 kolmogorovt(randvector(100,normald,0,1),student(2)) 26466\end{center} 26467you might get 26468\begin{center} 26469 \tt 26470 ["D=",0.0996114067923,"K=",0.996114067923,"1-kolmogorovd(K)=",0.27418851907] 26471\end{center} 26472 26473\chapter{Numerical computations}\label{sec:numeric} 26474Real numbers may have an exact representation 26475(e.g. rationals, symbolic expressions 26476involving square roots or constants like $\pi$, ...) 26477or approximate representation, which means that the real 26478is represented by a rational (with a denominator that is a power 26479of the basis of the representation) close to the real. 26480Inside {\tt Xcas}, the standard scientific notation is used 26481for approximate representation, that is a mantissa (with a point 26482as decimal separator) optionally followed by the letter {\tt e} 26483and an integer exponent. 26484 26485Note that the real number $10^{-4}$ is an exact number but 26486$1e-4$ is an approximate representation of this number. 26487 26488\section{Floating point representation.} 26489In this section, we explain how real numbers are represented. 26490 26491\subsection{Digits} 26492The {\tt Digits} variable is used to control how real numbers 26493are represented and also how they are displayed. 26494When the specified 26495number of digits is less or equal to 14 (for example {\tt 26496 Digits:=14}), then hardware floating point 26497numbers are used and they are displayed using the specified 26498number of digits. 26499When {\tt Digits} is larger than 14, Xcas uses the MPFR 26500library, the representation is similar to hardware floats 26501(cf. infra) but the number of bits of 26502the mantissa is not fixed and the range of exponents is much larger. 26503More precisely, the number of bits of the mantissa of a created MPFR float 26504is {\tt ceil(Digits*log(10)/log(2))}. 26505 26506Note that if you change the value of {\tt Digits}, this will affect 26507the creation of new real numbers compiled from command lines 26508or programs or by instructions like {\tt approx}, but it will 26509not affect existing real numbers. Hence hardware floats may coexist 26510with MPFR floats, and even in MPFR floats, some may have 100 bits 26511of mantissa and some may have 150 bits of mantissa. If operations 26512mix different kinds of floats, the most precise kind of floats 26513are coerced to the less precise kind of floats. 26514 26515\subsection{Representation by hardware floats} 26516A real is represented by a floating number $d$, that is 26517\[ d=2^\alpha*(1+m), \quad 0<m<1, -2^{10} < \alpha < 2^{10} \] 26518If $\alpha>1-2^{10}$, then $m \geq 1/2$, and $d$ is 26519a normalized floating point number, otherwise 26520$d$ is denormalized ($\alpha=1-2^{10}$). The special exponent $2^{10}$ 26521is used to represent plus or minus infinity and NaN (Not a Number). 26522A hardware float is made of 64 bits: 26523\begin{itemize} 26524\item the first bit is for the sign of $d$ (0 for '+' and 1 for '-') 26525\item the 11 following bits represents the exponent, more precisely 26526if $\alpha$ denotes the integer from the 11 bits, 26527the exponent is $\alpha+2^{10}-1$, 26528\item the 52 last bits codes the mantissa $m$, more precisely if 26529$M$ denotes the integer from the 52 bits, then 26530$m=1/2+M/2^{53}$ for normalized floats and $m=M/2^{53}$ for 26531denormalized floats. 26532\end{itemize} 26533Examples of representations of the exponent: 26534\begin{itemize} 26535\item $\alpha=0$ is coded by 011 1111 1111 26536\item $\alpha=1$ is coded by 100 0000 0000 26537\item $\alpha=4$ is coded by 100 0000 0011 26538\item $\alpha=5$ is coded by 100 0000 0100 26539\item $\alpha=-1$ is coded by 011 1111 1110 26540\item $\alpha=-4$ is coded by 011 1111 1011 26541\item $\alpha=-5$ is coded by 011 1111 1010 26542\item $\alpha=2^{10}$ is coded by 111 1111 1111 26543\item $\alpha=2^{-10}-1$ is coded by 000 0000 000 26544\end{itemize} 26545{\bf Remark}: $2^{-52}=0.2220446049250313e-15$ 26546 26547\subsection{Examples of representations of normalized floats} 26548\begin{itemize} 26549\item 3.1 :\\ 26550We have : 26551\begin{eqnarray*} 265523.1&=&2*(1+\frac{1}{2}+\frac{1}{2^5}+\frac{1}{2^6}+ 26553\frac{1}{2^9}+\frac{1}{2^{10}}+....)\\ 26554&=&2*(1+\frac{1}{2}+\sum_{k=1}^\infty(\frac{1}{2^{4*k+1}}+\frac{1}{2^{4*k+2}}) ) 26555\end{eqnarray*} 26556hence $\alpha=1$ and 26557$m=\frac{1}{2}+\sum_{k=1}^\infty(\frac{1}{2^{4*k+1}}+\frac{1}{2^{4*k+2}})$. 26558Hence the hexadecimal and binary representation of 3.1 is: 26559\begin{verbatim} 2656040 (01000000), 8 (00001000), cc (11001100), cc (11001100), 26561cc (11001100), cc (11001100), cc (11001100), cd (11001101), 26562\end{verbatim} 26563the last octet is 1101, the last bit is 1, because the 26564following digit is 1 (upper rounding). 26565\item 3. :\\ 26566We have $3=2*(1+1/2)$. 26567Hence the hexadecimal and binary representation of 3 is: 26568\begin{verbatim} 2656940 (01000000), 8 (00001000), 0 (00000000), 0 (00000000), 265700 (00000000), 0 (00000000), 0 (00000000), 0 (00000000) 26571\end{verbatim} 26572\end{itemize} 26573 26574\subsection{Difference between the representation of (3.1-3) and of 0.1} 26575\begin{itemize} 26576\item representation of 0.1 :\\ 26577We have : 26578\[ 0.1=2^{-4}*(1+\frac{1}{2}+\frac{1}{2^4}+\frac{1}{2^5}+ 26579\frac{1}{2^8}+\frac{1}{2^9}+...)= 265802^{-4}*\sum_{k=0}^\infty (\frac{1}{2^{4*k}}+\frac{1}{2^{4*k+1}}) \] 26581hence $\alpha=1$ and $m=\frac{1}{2}+ 26582\sum_{k=1}^\infty (\frac{1}{2^{4*k}}+\frac{1}{2^{4*k+1}})$, 26583therefore the representation of 0.1 is 26584\begin{verbatim} 265853f (00111111), b9 (10111001), 99 (10011001), 99 (10011001), 2658699 (10011001), 99 (10011001), 99 (10011001), 9a (10011010), 26587\end{verbatim} 26588the last octet is 1010, indeed the 2 last bits 2658901 became 10 because the following digit is 1 (upper rounding). 26590 26591\item representation of a:=3.1-3 :\\ 26592Computing a is done by adjusting exponents (here nothing 26593to do), then subtract the mantissa, and adjust the 26594exponent of the result to have a normalized float. 26595The exponent is $\alpha=-4$ (that corresponds at $2*2^{-5}$) 26596and the bits 26597corresponding to the mantissa begin at $1/2=2*2^{-6}$ : 26598the bits of the mantissa are shifted to the left of 5 positions 26599and we have : 26600\begin{verbatim} 266013f (00111111), b9 (10111001), 99 (10011001), 99 (10011001), 2660299 (10011001), 99 (10011001), 99 (10011001), 9a (10100000), 26603\end{verbatim} 26604Therefore 26605$a>0.1$ and $a-0.1=1/2^{50}+1/2^{51}$ 26606(since 100000-11010=110) 26607\end{itemize} 26608{\bf Remark}\\ 26609This is the reason why 26610\begin{center} 26611{\tt floor(1/(3.1-3))} 26612\end{center} 26613returns {\tt 9} and not {\tt 10} when {\tt Digits:=14}. 26614 26615\section{Approx. evaluation : {\tt evalf approx} and {\tt Digits}}\index{evalf|textbf}\index{approx|textbf}\index{DIGITS}\index{Digits} 26616\noindent {\tt evalf} or {\tt approx} evaluates to a numeric 26617approximation (if possible).\\ 26618Input : 26619\begin{center}{\tt evalf(sqrt(2))}\end{center} 26620Output, if in the {\tt cas} configuration ({\tt Cfg} menu) {\tt Digits=7} 26621(that is hardware floats are used, and 7 digits are displayed) : 26622\begin{center}{\tt 1.414214}\end{center} 26623You can change the number of digits in a command line by assigning 26624the variable {\tt DIGITS} or {\tt Digits}. 26625Input : 26626\begin{center}{\tt DIGITS:=20}\end{center} 26627\begin{center}{\tt evalf(sqrt(2))}\end{center} 26628Output : 26629\begin{center}{\tt 1.4142135623730950488}\end{center} 26630Input : 26631\begin{center}{\tt evalf(10\verb|^|-5)}\end{center} 26632Output : 26633\begin{center}{\tt 1e-05}\end{center} 26634Input : 26635\begin{center}{\tt evalf(10\verb|^|15)}\end{center} 26636Output : 26637\begin{center}{\tt 1e+15}\end{center} 26638Input : 26639\begin{center}{\tt evalf(sqrt(2))*10\verb|^|-5}\end{center} 26640Output : 26641\begin{center}{\tt 1.41421356237e-05}\end{center} 26642 26643\section{Numerical algorithms} 26644\subsection{Approximate solution of an equation : {\tt newton}}\index{newton} 26645\noindent{\tt newton} takes as arguments : an expression {\tt ex}, 26646the variable 26647name of this expression (by default {\tt x}), and three values {\tt a} (by 26648default {\tt a=0}), {\tt eps} (by default {\tt eps=1e-8}) and {\tt nbiter} 26649(by default {\tt nbiter=12}).\\ 26650{\tt newton(ex,x,a,eps,nbiter)} computes an approximate 26651solution {\tt x} of the equation {\tt ex=0} 26652using the Newton algorithm with starting point 26653{\tt x=a}. The maximum number of iterations is {\tt nbiter} 26654and the precision is {\tt eps}.\\ 26655Input : 26656\begin{center}{\tt newton(x\verb|^|2-2,x,1) }\end{center} 26657Output : 26658\begin{center}{\tt 1.41421356237}\end{center} 26659Input : 26660\begin{center}{\tt newton(x\verb|^|2-2,x,-1) }\end{center} 26661Output : 26662\begin{center}{\tt -1.41421356237}\end{center} 26663Input : 26664\begin{center}{\tt newton(cos(x)-x,x,0)}\end{center} 26665Output : 26666\begin{center}{\tt0.739085133215 }\end{center} 26667 26668\subsection{Approximate computation of the derivative number : {\tt nDeriv}}\index{nDeriv} 26669\noindent{\tt nDeriv} takes as arguments : an expression {\tt ex}, the variable 26670name of this expression (by default {\tt x}), and {\tt h} (by default 26671{\tt h=0.001}).\\ 26672{\tt nDeriv(ex,x,h)} computes an approximated value of the derivative of the 26673expression {\tt ex} at the point {\tt x} and returns : 26674\begin{center}{\tt (f(x+h)-f(x+h))/2*h}\end{center} 26675Input : 26676\begin{center}{\tt nDeriv(x\verb|^|2,x)}\end{center} 26677Output : 26678\begin{center}{\tt ((x+0.001)\verb|^|2-(x+-0.001)\verb|^|2)*500.0}\end{center} 26679Input : 26680\begin{center}{\tt subst(nDeriv(x\verb|^|2,x),x=1)}\end{center} 26681Output : 26682\begin{center}{\tt 2}\end{center} 26683Input : 26684\begin{center}{\tt nDeriv(exp(x\verb|^| 2),x,0.00001)}\end{center} 26685Output : 26686\begin{center}{\tt (exp((x+1e-05)\verb|^|2)-exp((x+-1e-05)\verb|^|2))*50000}\end{center} 26687Input : 26688\begin{center}{\tt subst(exp(nDeriv(x\verb|^| 2),x,0.00001),x=1)}\end{center} 26689Output : 26690\begin{center}{\tt 5.43656365783}\end{center} 26691which is an approximate value of {\tt 2e=5.43656365692}. 26692 26693\subsection{Approximate computation of integrals : {\tt romberg nInt}}\index{romberg}\index{nInt} 26694\noindent{\tt romberg} or {\tt nInt} takes as arguments : an expression 26695{\tt ex}, the variable name of this expression (by default {\tt x}), and 26696two real values {\tt a,b}.\\ 26697{\tt romberg(ex,x,a,b)} or {\tt nInt(ex,x,a,b)} computes an approximated 26698value of the integral $\int_a^b ex\ dx$ using the Romberg method. The 26699integrand must be sufficiently regular for the approximation to 26700be accurate. Otherwise, {\tt romberg} returns a list of real values, 26701that comes from the application of the 26702Romberg algorithm (the first list element is 26703the trapezoid rule approximation, the next ones come from the application 26704of the Euler-MacLaurin formula to remove successive even powers of 26705the step of the trapezoid rule).\\ 26706Input : 26707\begin{center}{\tt romberg(exp(x\verb|^|2),x,0,1)}\end{center} 26708Output : 26709\begin{center}{\tt 1.46265174591}\end{center} 26710 26711\subsection{Approximate integral with an adaptive Gaussian quadrature 26712at 15 points: \texttt{gaussquad}}\index{gaussquad} 26713 26714The \texttt{gaussquad} command takes four arguments; an expression, 26715the variable used by the expression, and two numbers.\\ 26716\texttt{gaussquad} returns an approximation to the definite integral 26717of the expression over the limits given by the two numbers. The 26718approximation is calculated by an adaptive method by Ernst Hairer 26719which uses a 15-point Gaussian quadrature.\\ 26720Input: 26721\begin{center} 26722 \tt 26723 gaussquad(exp(x\^{}2),x,0,1) 26724\end{center} 26725Output: 26726\begin{center} 26727 \tt 26728 1.46265174591 26729\end{center} 26730Input: 26731\begin{center} 26732 \tt 26733 gaussquad(exp(-x\^{}2),x,-1,1) 26734\end{center} 26735Output: 26736\begin{center} 26737 \tt 26738 1.49364826562 26739\end{center} 26740 26741\subsection{Approximate solution of y'=f(t,y) : {\tt odesolve}} 26742\index{odesolve|textbf} 26743\index{curve@\textit{curve}} 26744\label{ssec:odesolve} 26745 26746\begin{itemize} 26747\item Let $f$ be a function from $\mathbb R^2$ to $\mathbb R$.\\ 26748 {\tt odesolve(f(t,y),[t,y],[t0,y0],t1)} or\\ 26749{\tt odesolve(f(t,y),t=t0..t1,y,y0)} or\\ 26750{\tt odesolve(t0..t1,f,y0)} or\\ 26751{\tt odesolve(t0..t1,(t,y)->f(t,y),y0)}\\ 26752returns an approximate value of $y(t1)$ where $y(t)$ is the 26753solution of: 26754\[ y'(t)=f(t,y(t)), \quad y(t0)=y0 \] 26755\item {\tt odesolve} accepts an optional argument for the 26756discretization of {\tt t} ({\tt tstep=value}). 26757This value is passed as initial tstep value to the numeric solver 26758from the GSL (Gnu Scientific Library), it may be modified 26759by the solver. It is also used to control the number of iterations 26760of the solver by {\tt 2*(t1-t0)/tstep} (if the number 26761of iterations exceeds this value, the solver will stops at a time $t<t1$). 26762\item {\tt odesolve} accepts {\tt curve} as an optional argument. 26763In that case, 26764{\tt odesolve} returns the list of all the [$t,[y(t)]$] values 26765that were computed. 26766\end{itemize} 26767Input : 26768\begin{center}{\tt odesolve(sin(t*y),[t,y],[0,1],2)}\end{center} 26769or : 26770\begin{center}{\tt odesolve(sin(t*y),t=0..2,y,1)}\end{center} 26771or : 26772\begin{center}{\tt odesolve(0..2,(t,y)->sin(t*y),1)}\end{center} 26773or define the function : 26774\begin{center}{\tt f(t,y):=sin(t*y)} \end{center} 26775and input : 26776\begin{center}{\tt odesolve(0..2,f,1)}\end{center} 26777Output : 26778\begin{center}{\tt [1.82241255675]}\end{center} 26779Input : 26780\begin{center}{\tt odesolve(0..2,f,1,tstep=0.3)}\end{center} 26781Output : 26782\begin{center}{\tt [1.82241255675]}\end{center} 26783Input : 26784\begin{center}{\tt odesolve(sin(t*y),t=0..2,y,1,tstep=0.5)}\end{center} 26785Output : 26786\begin{center}{\tt [1.82241255675]}\end{center} 26787Input : 26788\begin{center}{\tt odesolve(sin(t*y),t=0..2,y,1,tstep=0.5,curve)}\end{center} 26789Output : 26790\begin{center}{\tt [[0.760963063136,[1.30972370515]],[1.39334557388,[1.86417104853]]]}\end{center} 26791 26792 26793\subsection{Approximate solution of the system v'=f(t,v) : {\tt odesolve}}\index{odesolve} 26794\begin{itemize} 26795\item If $v$ is a vector 26796of variables $[x1,..,xn]$ and if $f$ is given by a vector of expressions 26797{\tt [e1,...,en]} depending on $t$ and of $[x1,..,xn]$, 26798if the initial value of $v$ at {\tt t0} 26799is the vector $[x10,...,xn0]$ then the instruction 26800\begin{center} 26801{\tt odesolve([e1,..,en],t=t0..t1,[x1,...,xn], 26802[x10,...,xn0])} 26803\end{center} 26804returns an approximated value of $v$ at $t=t1$. 26805With the optional argument {\tt curve}, {\tt odesolve} returns the list of 26806the intermediate values of [$t,v(t)$] computed by the solver. 26807 26808Example, to solve the system 26809\begin{eqnarray*} 26810x'(t) &=&-y(t)\\ 26811y'(t)&=&x(t) 26812\end{eqnarray*} 26813Input : 26814\begin{center} 26815{\tt odesolve([-y,x],t=0..pi,[x,y],[0,1])}\end{center} 26816Output : 26817\begin{center}{\tt [-1.79045146764e-15,-1]}\end{center} 26818 26819\item If $f$ is a function from $\mathbb R \times \mathbb R^n$ to 26820$\mathbb R^n$.\\ 26821{\tt odesolve(t0..t1,(t,v)->f(t,v),v0)} or\\ 26822{\tt odesolve(t0..t1,f,v0)}\\ 26823computes an approximate value of $v(t1)$ where the vector $v(t)$ 26824in $\mathbb R^n$ is the solution of 26825\[ v'(t)=f(t,v(t)), v(t0)=v0 \] 26826With the optional argument {\tt curve}, {\tt odesolve} returns the list of 26827the intermediate value [$t,v(t)$] computed by the solver. 26828 26829Example, to solve the system :\\ 26830\begin{eqnarray*} 26831x'(t) &=&-y(t)\\ 26832y'(t)&=&x(t) 26833\end{eqnarray*} 26834Input : 26835\begin{center}{\tt odesolve(0..pi,(t,v)->[-v[1],v[0]],[0,1])}\end{center} 26836Or define the function: 26837\begin{center}{\tt f(t,v):=[-v[1],v[0]]}\end{center} 26838then input : 26839\begin{center}{\tt odesolve(0..pi,f,[0,1])}\end{center} 26840Output : 26841\begin{center}{\tt [-1.79045146764e-15,-1]}\end{center} 26842Alternative input : 26843\begin{center}{\tt odesolve(0..pi/4,f,[0,1],curve)}\end{center} 26844Output : 26845\begin{center}{\tt [[0.1781,[-0.177159948386,0.984182072936]], [0.3781,[-0.369155338156,0.929367707805]], [0.5781,[-0.54643366953,0.837502384954]], [0.7781,[-0.701927414872,0.712248484906]]]}\end{center} 26846\end{itemize} 26847 26848\subsection{Approximate solution of a nonlinear second-order boundary value problem : {\tt bvpsolve\index{bvpsolve}} } 26849{\tt bvpsolve} finds an approximate solution of a boundary value problem 26850\[ y''=f(x,y,y'),\quad y(a)=\alpha,\ y(b)=\beta \] 26851on the interval $[a,b]$. It takes the following mandatory arguments : 26852\begin{itemize} 26853 \item expression $f(x,y,y')$, 26854 \item list {\tt [x=a..b,y]}, specifying the independent variable $x$, its range $[a,b]$ and the sought function $y$, 26855 \item list containing $\alpha$, $\beta$ and optionally an initial guess for $y'(a)$ as the third element. 26856\end{itemize} 26857One or more of the additional arguments below can optionally follow (in no particular order) : 26858\begin{itemize} 26859 \item integer $N\geq 2$ (by default 100), 26860 \item {\tt output=<type>} or {\tt Output=<type>} : the type of the output, which can be {\tt list} (the default), {\tt diff}, {\tt piecewise} or {\tt spline}, 26861 \item {\tt limit=M} : the procedure will be stopped if the number of iterations exceeds $M$, which must be a positive integer (by default there is no limit). 26862\end{itemize} 26863 26864The procedure uses the method of nonlinear shooting which is based on Newton and Runge-Kutta methods. Values of $y$ and its first derivative $y'$ are approximated at points $x_k=a+k\,\delta$, where $\delta=\frac{b-a}{N}$ and $k=0,1,\dots,N$. For the numeric tolerance (precision) threshold, the algorithm uses {\tt epsilon} specified in the session settings in Xcas. If the output type is 26865\begin{itemize} 26866 \item {\tt list}, a list of pairs $[x_k,y_k]$ is returned where $y_k\approx y(x_k)$, 26867 \item {\tt diff}, a list of lists $[x_k,y_k,y'_k]$ is returned, where $y'_k\approx y'(x_k)$, 26868 \item {\tt piecewise}, a piecewise linear interpolation of the points $(x_k,y_k)$ is returned, 26869 \item {\tt spline}, a piecewise spline interpolation of the points $(x_k,y_k)$ is returned, based on the values $y'_k$ computed in the process. 26870\end{itemize} 26871 26872Note that the shooting method is sensitive to roundoff errors and may fail to converge in some cases, especially when $y$ is a rapidly increasing function. In the absence of convergence or if the maximum number of iterations is exceeded, {\tt bvpsolve} returns {\tt undef}. However, if output type is {\tt list} or {\tt piecewise} and if $N>2$, a slower but more stable finite-difference method (which approximates only the function $y$) is tried first. 26873 26874Sometimes setting an initial guess for $y'(a)$ to a suitable value may help the shooting algorithm to converge or to converge faster. The default initial guess $y_0'$ for the value $y'(a)$ is\[ y_0'=\frac{\beta-\alpha}{b-a}. \] 26875 26876\paragraph{Examples.} 26877In the first example we solve the problem \[y''=\frac{1}{8}\,(32+2\,x^3-y\,y'),\quad 1\leq x\leq 3\] with boundary conditions $y(1)=17$ and $y(3)=\frac{43}{3}$. We use $N=20$, which gives $x$-step of $0.01$. Input : 26878\begin{center} 26879 \tt bvpsolve((32+2x\verb|^|3-y*y')/8,[x=1..3,y],[17,43/3],20) 26880\end{center} 26881The output is shown in Table~\ref{tab:bvp} (the middle two columns) alongside with the values $y(x_k)$ of the exact solution $y=x^2+16/x$ (the fourth column). 26882\begin{table} 26883 \centering\small 26884 \begin{tabular}{|r|ll|l|} 26885 \hline 26886 $k$ & $x_k$ & $y_k$ & $y(x_k)$\\\hline 26887 0 & 1.0 & 17.0 & 17.0\\ 26888 1 & 1.1 & 15.7554961579 & 15.7554545455\\ 26889 2 & 1.2 & 14.7733911821 & 14.7733333333\\ 26890 3 & 1.3 & 13.9977543159 & 13.9976923077\\ 26891 4 & 1.4 & 13.388631813 & 13.3885714286\\ 26892 5 & 1.5 & 12.9167227424 & 12.9166666667\\ 26893 6 & 1.6 & 12.5600506483 & 12.56\\ 26894 7 & 1.7 & 12.3018096101 & 12.3017647059\\ 26895 8 & 1.8 & 12.1289281414 & 12.1288888889\\ 26896 9 & 1.9 & 12.0310865274 & 12.0310526316\\ 26897 10 & 2.0 & 12.0000289268 & 12.0\\ 26898 11 & 2.1 & 12.0290719981 & 12.029047619\\ 26899 12 & 2.2 & 12.1127475278 & 12.1127272727\\ 26900 13 & 2.3 & 12.2465382803 & 12.2465217391\\ 26901 14 & 2.4 & 12.4266798825 & 12.4266666667\\ 26902 15 & 2.5 & 12.650010254 & 12.65\\ 26903 16 & 2.6 & 12.9138537834 & 12.9138461538\\ 26904 17 & 2.7 & 13.2159312426 & 13.2159259259\\ 26905 18 & 2.8 & 13.5542890043 & 13.5542857143\\ 26906 19 & 2.9 & 13.9272429048 & 13.9272413793\\ 26907 20 & 3.0 & 14.3333333333 & 14.3333333333\\\hline 26908 \end{tabular} 26909 \caption{approximate and true values of the function $y=x^2+16/x$ on $[1,3]$\label{tab:bvp}} 26910\end{table} 26911 26912In the next example we solve the problem 26913\[ y''=\frac{x^2\,(y')^2-9\,y^2+4\,x^6}{x^5},\quad 1\leq x\leq 2, \] 26914with the boundary conditions $y(1)=0$ and $y(2)=\ln 256$. We obtain the solution as a piecewise spline interpolation for $N=10$ and estimate the absolute error {\tt err} of the approximation using the exact solution $y=x^3\,\ln x$ and {\tt romberg} command for numerical integration. We also need to explicitly set an initial guess $y_0'$ for the value $y'(1)$ because the algorithm fails to converge with the default guess $y_0'=\ln 256\approx 5.545$. Therefore let $y_0'=1$ instead. Input : 26915\begin{center} 26916 \tt f:=(x\verb|^|2*diff(y(x),x)\verb|^|2-9*y(x)\verb|^|2+4*x\verb|^|6)/x\verb|^|5:; vars:=[x=1..2,y]:; yinit:=[0,ln(256),1]:; p:=bvpsolve(f,vars,yinit,10,output=spline):; err:=sqrt(romberg((p-x\verb|^|3*ln(x))\verb|^|2,x=1..2)) 26917\end{center} 26918Output : 26919\begin{center} 26920 \tt 3.27720911686e-06 26921\end{center} 26922Note that, if the output type was set to {\tt list} or {\tt piecewise}, the solution would have been found even without specifying an initial guess for $y'(1)$ because the algorithm would automatically apply the alternative finite-difference method, which converges. 26923 26924\section{Solve equations with {\tt fsolve nSolve}}\index{fsolve}\index{nSolve} 26925\noindent{\tt fsolve} or {\tt nSolve} solves numeric equations 26926(unlike {\tt solve} or {\tt proot}, it is not limited to polynomial 26927equations) of the form: 26928\[ f(x)=0, \quad x \in ]a,b[ \] 26929{\tt fsolve} or {\tt nSolve} accepts a last optional argument, 26930the name of an iterative algorithm to be used by the GSL solver. 26931The different methods are explained in the following section. 26932 26933\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt bisection\_solver}}\index{bisection\_solver@{\sl bisection\_solver}|textbf} 26934This algorithm of dichotomy is the simplest but also generically 26935the slowest. 26936It encloses the zero of a function on an interval. 26937Each iteration, cuts the interval into two parts. We compute the middle point 26938value. The function sign at this point, gives us the half-interval 26939on which the next iteration will be performed.\\ 26940Input : 26941\begin{center}{\tt fsolve((cos(x))=x,x,-1..1,bisection\_solver)}\end{center} 26942Output : 26943\begin{center}{\tt [0.739085078239,0.739085137844]}\end{center} 26944 26945\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt brent\_solver}}\index{brent\_solver{\sl brent\_solver}|textbf}\index{color@{\sl }|textbf} 26946The Brent method interpolates of $f$ at three points, finds 26947the intersection of the interpolation with the $x$ axis, computes 26948the sign of $f$ at this point and chooses the interval where the sign changes. 26949%: on prend l'intersection de la courbe d'interpolaton passant par 3 points with l'axe des $x$ 26950It is generically faster than bisection.\\ 26951Input : 26952\begin{center}{\tt fsolve((cos(x))=x,x,-1..1,brent\_solver)}\end{center} 26953Output : 26954\begin{center}{\tt [0.73908513321 5,0.739085133215]}\end{center} 26955 26956\subsection{{\tt fsolve} or {\tt nSolve} with the option 26957{\tt falsepos\_solver}}\index{falsepos\_solver@{\sl falsepos\_solver}|textbf} 26958The "false position" algorithm is an iterative algorithm based on linear 26959interpolation : we compute the value of $f$ at the intersection of the line 26960$(a,f(a))$, $(b,f(b))$ with the $x$ axis. This value gives us the part of the 26961interval containing the root, and on which a new iteration is performed.\\ 26962The convergence is linear but generically faster than bisection.\\ 26963Input : 26964\begin{center}{\tt fsolve((cos(x))=x,x,-1..1,falsepos\_solver)}\end{center} 26965Output : 26966\begin{center}{\tt [0.739085133215,0.739085133215]}\end{center} 26967 26968\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt newton\_solver}}\index{newton\_solver{\sl newton\_solver}|textbf} 26969{\tt newton\_solver} is the standard Newton method. 26970The algorithm starts at an initial value $x_0$, then we search the 26971intersection $x_1$ of the tangent at $x_0$ to the graph of $f$, with the $x$ 26972axis, the next iteration is done with $x_1$ instead of $x_0$. 26973The $x_i$ sequence is defined by 26974\[ x_0=x_0, \quad x_{n+1}=x_n-\frac{f(x_n)}{f'(x_n)} \] 26975If the Newton method converges, it is a quadratic convergence for 26976roots of multiplicity 1.\\ 26977Input : 26978\begin{center}{\tt fsolve((cos(x))=x,x,0,newton\_solver)}\end{center} 26979Output : 26980\begin{center}{\tt 0.739085133215}\end{center} 26981 26982\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt secant\_solver}}\index{secant\_solver{\sl secant\_solver}|textbf} 26983The secant method is a simplified version of the Newton method. 26984The computation of $x_1$ is done using the Newton method. 26985The computation of $f'(x_n), n>1$ is done approximately. 26986This method is used when the 26987computation of the derivative is expensive: 26988\[ x_{i+1} = x_i-\frac{ f(x_i)}{f'_{est}}, \quad 26989f'_{est} = \frac{f(x_i) - f(x_{i-1})}{(x_i - x_{i-1})} 26990\] 26991The convergence for roots of multiplicity 1 26992is of order $(1 + \sqrt5)/2 \approx 1.62... $.\\ 26993Input : 26994\begin{center}{\tt fsolve((cos(x))=x,x,-1..1,secant\_solver)}\end{center} 26995Output : 26996\begin{center}{\tt [0.739085078239,0.739085137844]}\end{center} 26997Input : 26998\begin{center}{\tt fsolve((cos(x))=x,x,0,secant\_solver)}\end{center} 26999Output : 27000\begin{center}{\tt 0.739085133215}\end{center} 27001 27002\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt steffenson\_solver}} 27003\index{steffenson\_solver@{\sl steffenson\_solver}|textbf} 27004The Steffenson method is generically the fastest method.\\ 27005It combines the Newton method with a "delta-two" Aitken acceleration : 27006with the Newton method, we obtain the sequence $x_i$ and the convergence 27007acceleration gives the Steffenson sequence 27008\[ R_i =x_i - \frac{(x_{i+1} - x_i)^2}{ (x_{i+2} - 2 x_{i+1} + x_{i})} \] 27009Input : 27010\begin{center}{\tt fsolve(cos(x)=x,x,0,steffenson\_solver)}\end{center} 27011Output : 27012\begin{center}{\tt 0.739085133215}\end{center} 27013 27014\section{Solve systems with {\tt fsolve}}\index{fsolve} 27015{\tt Xcas} provides six methods (inherited from the GSL) 27016to solve numeric systems of equations 27017of the form $f(x)=0$: 27018\begin{itemize} 27019\item Three methods use the jacobian matrix $f'(x)$ and their names are 27020terminated with {\tt j\_solver}. 27021\item 27022The three other methods use approximation for $f'(x)$ and use only 27023$f$. 27024\end{itemize} 27025All methods use an iteration of Newton kind 27026\[ x_{n+1}=x_n-{f'(x_n)}^{-1}*f(x_n) \] 27027The four methods {\tt hybrid*\_solver} use also a method of 27028gradient descent when the Newton iteration would make a too large step. 27029The length of the step is computed without scaling 27030for {\tt hybrid\_solver} and {\tt hybridj\_solver} 27031or with scaling (computed from $f'(x_n)$) for 27032{\tt hybrids\_solver} and {\tt hybridsj\_solver}. 27033 27034\subsection{{\tt fsolve} with the option {\tt dnewton\_solver}}\index{dnewton\_solver{\sl dnewton\_solver}|textbf} 27035\noindent Input : 27036\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[2,2],dnewton\_solver)}\end{center} 27037Output : 27038\begin{center}{\tt [1.0,1.0]}\end{center} 27039 27040\subsection{{\tt fsolve} with the option {\tt hybrid\_solver}}\index{hybrid\_solver{\sl hybrid\_solver}|textbf} 27041\noindent Input : 27042\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[2,2],}\end{center} 27043\begin{center}{\tt cos(x)=x,x,0,hybrid\_solver)}\end{center} 27044Output : 27045\begin{center}{\tt [1.0,1.0]}\end{center} 27046 27047\subsection{{\tt fsolve} with the option {\tt hybrids\_solver}}\index{hybrids\_solver{\sl hybrids\_solver}|textbf} 27048\noindent Input : 27049\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[2,2],hybrids\_solver)}\end{center} 27050Output : 27051\begin{center}{\tt [1.0,1.0]}\end{center} 27052 27053\subsection{{\tt fsolve} with the option {\tt newtonj\_solver}}\index{newtonj\_solver{\sl newtonj\_solver}|textbf} 27054\noindent Input : 27055\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[0,0],newtonj\_solver)}\end{center} 27056Output : 27057\begin{center}{\tt [1.0,1.0]}\end{center} 27058 27059\subsection{{\tt fsolve} with the option {\tt hybridj\_solver}}\index{hybridj\_solver{\sl hybridj\_solver}|textbf} 27060\noindent Input : 27061\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[2,2],hybridj\_solver)}\end{center} 27062Output : 27063\begin{center}{\tt [1.0,1.0]}\end{center} 27064 27065\subsection{{\tt fsolve} with the option {\tt hybridsj\_solver}}\index{hybridsj\_solver{\sl hybridsj\_solver}|textbf} 27066\noindent Input : 27067\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[2,2],hybridsj\_solver)}\end{center} 27068Output : 27069\begin{center}{\tt [1.0,1.0]}\end{center} 27070 27071\section{Solving equations or systems over $\C$: 27072\texttt{cfsolve}}\index{cfsolve} 27073 27074The \texttt{cfsolve} command gives numeric solutions to an equation or 27075system over the complex numbers, even if \texttt{Complex} is not 27076checked in the configuration screen. (The \texttt{fsolve} command 27077will return complex roots, but \texttt{Complex} needs to be checked in 27078the configuration screen.)\\ 27079Input: 27080\begin{center} 27081 \tt 27082 cfsolve(sin(x)=2) 27083\end{center} 27084Output: 27085\begin{center} 27086 \tt 27087 [1.57079632679-1.31695789692*i,1.57079632679+1.31695789692*i] 27088\end{center} 27089Input: 27090\begin{center} 27091 \tt 27092 cfsolve([x\^{}2+y+2,x+y\^{}2+2],[x,y]) 27093\end{center} 27094Output: 27095\begin{center} 27096 \tt 27097 [[0.5+1.65831239518*i,0.5-1.65831239518*i],[0.5-1.65831239518*i,0.5+1.65831239518*i], 27098 [-0.5+1.32287565553*i,-0.5+1.32287565553*i],[-0.5-1.32287565553*i,-0.5-1.32287565553*i]] 27099\end{center} 27100 27101\section{Numeric roots of a polynomial : {\tt proot}}\index{proot} 27102\noindent{\tt proot} takes as argument a squarefree polynomial, 27103either in symbolic form or as a list of 27104polynomial coefficients (written by decreasing order).\\ 27105{\tt proot} returns a list of the numeric roots of this polynomial.\\ 27106To find the numeric roots of $P(x)=x^3+1$, input : 27107\begin{center}{\tt proot([1,0,0,1]) }\end{center} 27108or : 27109\begin{center}{\tt proot(x\verb|^|3+1) }\end{center} 27110Output : 27111\begin{center}{\tt [0.5+0.866025403784*i,0.5-0.866025403784*i,-1.0]}\end{center} 27112To find the numeric roots of $x^2-3$, input : 27113\begin{center}{\tt proot([1,0,-3])}\end{center} 27114or : 27115\begin{center}{\tt proot(x\verb|^|2-3)}\end{center} 27116Output : 27117\begin{center}{\tt [1.73205080757,-1.73205080757]}\end{center} 27118%proot([1,0,-15,0,90,0,-270,0,405,0,-243]) 27119 27120 27121\section{Numeric factorization of a matrix : {\tt cholesky qr lu svd}} 27122Matrix numeric factorizations of 27123\begin{itemize} 27124\item Cholesky, 27125\item QR, 27126\item LU, 27127\item svd, 27128\end{itemize} 27129are described in section \ref{sec:factormatrice}. 27130 27131\chapter{Unit objects and physical constants}\label{sec:unit} 27132The {\tt Phys} menu contains: 27133\begin{itemize} 27134\item the physical constants ({\tt Constant} sub-menu), 27135\item the unit conversion functions 27136({\tt Unit\_convert} sub-menu), 27137\item the unit prefixes ({\tt Unit\_prefix} sub-menu) 27138\item the unit objects organized by subject 27139\end{itemize} 27140 27141\section{Unit objects} 27142\subsection{Notation of unit objects}\index{\_|textbf} 27143A unit object has two parts : a real number and a unit expression (a single 27144unit or a multiplicative combination of units). The two parts are linked by the 27145character {\tt \_} ("underscore"). For example {\tt 2\_m} for 2 meters. 27146For composite units, parenthesis must be used, e.g. {\tt 1\_(m*s)}.\\ 27147If a prefix is put before the unit then the unit is multiplied by a power of 2714810. For example {\tt k} or {\tt K} for kilo (indicate a multiplication by 27149$10^3$), {\tt D} for deca (indicate a multiplication by $10$), {\tt d} for 27150deci (indicate a multiplication by $10^{-1}$) etc...\\ 27151Input : 27152\begin{center}{\tt 10.5\_m}\end{center} 27153Output : 27154\begin{center}{\tt a unit object of value 10.5 meters}\end{center} 27155Input : 27156\begin{center}{\tt 10.5\_km}\end{center} 27157Output : 27158\begin{center}{\tt a unit object of value 10.5 kilometers}\end{center} 27159 27160\subsection{Computing with units} 27161{\tt Xcas} performs usual arithmetic operations (+, -, *, /, \verb|^|) on 27162unit objects. Different units may be used, but they must be 27163compatible for + and -. The result is an unit object 27164\begin{itemize} 27165\item for the 27166multiplication and the division of two unit objects 27167{\tt \_u1} and {\tt \_u2} the unit of the result is written 27168{\tt \_(u1*u2)} or {\tt \_(u1/u2)}. 27169\item for an addition or a subtraction of compatible unit objects, 27170the result is expressed with the same unit as the first term of the operation. 27171\end{itemize} 27172Input : 27173\begin{center}{\tt 1\_m+100\_cm}\end{center} 27174Output : 27175\begin{center}{\tt 2\_m}\end{center} 27176Input : 27177\begin{center}{\tt 100\_cm+1\_m}\end{center} 27178Output : 27179\begin{center}{\tt 200\_cm}\end{center} 27180Input : 27181\begin{center}{\tt 1\_m*100\_cm}\end{center} 27182Output : 27183\begin{center}{\tt 1\_m\verb|^|2}\end{center} 27184 27185\subsection{Convert units into MKSA units : {\tt mksa}}\index{mksa} 27186\noindent{\tt mksa} converts a unit object into a unit object 27187written with the compatible {\tt MKSA} base unit.\\ 27188\noindent Input : 27189\begin{center}{\tt mksa(15\_C)}\end{center} 27190Output : 27191\begin{center}{\tt 15\_(s*A)}\end{center} 27192 27193\subsection{Convert units : {\tt convert, =>}}\index{convert}\index{=>}\label{sec:convertunit} 27194\noindent {\tt convert} convert units : the first argument is an unit object 27195and the second argument is the new unit (which must be compatible). 27196(The \texttt{=>} operator is the infixed version of \texttt{convert}.)\\ 27197Input : 27198\begin{center}{\tt convert(1\_h,\_s) }\end{center} 27199Output : 27200\begin{center}{\tt 3600\_s}\end{center} 27201Input : 27202\begin{center}{\tt convert(3600\_s,\_h) }\end{center} 27203Output : 27204\begin{center}{\tt 1\_h}\end{center} 27205 27206\subsection{Convert between Celsius and Fahrenheit: 27207\texttt{Celsius2Fahrenheit, 27208Fahrenheit2Celsius}}\index{Celsius2Fahrenheit} 27209\index{Fahrenheit2Celsius} 27210 27211The \texttt{Celsius2Fahrenheit} command takes a number as an argument, 27212representing a temperature in degrees Celsius.\\ 27213\texttt{Celsius2Fahrenheit} returns the number representing the 27214temperature in Fahrenheit.\\ 27215Input: 27216\begin{center} 27217 \tt 27218 Celsius2Fahrenheit(a) 27219\end{center} 27220Output: 27221\begin{center} 27222 \tt 27223 a*9/5+32 27224\end{center} 27225Input: 27226\begin{center} 27227 \tt 27228 Celsius2Fahrenheit(0) 27229\end{center} 27230Output: 27231\begin{center} 27232 \tt 27233 32 27234\end{center} 27235 27236The \texttt{Fahrenheit2Celsius} command converts Fahrenheit 27237temperatures to Celsius.\\ 27238Input: 27239\begin{center} 27240 \tt 27241 Fahrenheit2Celsius(212) 27242\end{center} 27243Output: 27244\begin{center} 27245 \tt 27246 100 27247\end{center} 27248 27249\subsection{Factorize a unit : {\tt ufactor}}\index{ufactor|textbf} 27250\noindent {\tt ufactor} factorizes a unit in a unit object : the first 27251argument is a unit object and the second argument is the unit to factorize.\\ 27252The result is an unit object multiplied by the remaining {\tt MKSA} units.\\ 27253Input : 27254\begin{center}{\tt ufactor(3\_J,\_W) }\end{center} 27255Output : 27256\begin{center}{\tt 3\_(W*s)}\end{center} 27257Input : 27258\begin{center}{\tt ufactor(3\_W,\_J) }\end{center} 27259Output : 27260\begin{center}{\tt 3\_(J/s)}\end{center} 27261 27262\subsection{Simplify a unit : {\tt usimplify}}\index{usimplify} 27263\noindent {\tt usimplify} simplifies a unit in an unit object.\\ 27264Input : 27265\begin{center}{\tt usimplify(3\_(W*s))}\end{center} 27266Output : 27267\begin{center}{\tt 3\_J}\end{center} 27268 27269\subsection{Unit prefixes} 27270You can insert a unit prefix in front of a unit to indicate a power of ten.\\ 27271The following table gives the available prefixes: 27272\begin{center} 27273\begin{tabular}{|l|c|r||l|c|r|} 27274\hline 27275Prefix & Name & (*10\verb|^|) n & Prefix & Name & (*10\verb|^|) n \\ 27276\hline 27277Y & yota & 24 & d & deci & -1\\ 27278Z & zeta & 21 & c & cent & -2\\ 27279E & exa & 18 & m & mili & -3\\ 27280P & peta & 15 & mu & micro &-6\\ 27281T & tera & 12 & n & nano & -9\\ 27282G & giga & 9 & p & pico & -12\\ 27283M & mega & 6 & f & femto & -15\\ 27284k or K & kilo & 3 & a & atto & -18\\ 27285h or H & hecto & 2 & z & zepto & -21\\ 27286D & deca & 1 & y & yocto &-24\\ 27287\hline 27288\end{tabular} 27289\end{center} 27290{\bf Remark}\\ 27291You cannot use a prefix with a built-in unit if the result gives another 27292built-in unit.\\ 27293For example, 27294{\tt 1\_a} is one are, but {\tt 1\_Pa} is one pascal and not 27295{\tt 10\verb|^|15\_a}. 27296 27297\section{Constants} 27298\subsection{Notation of physical constants}\index{\_} 27299If you want to use a physical constants inside Xcas, put 27300its name between two characters {\tt \_} 27301("underscore"). Don't confuse physical constants with symbolic constants, 27302for example, $e,\pi$ are symbolic constants as {\tt \_c\_,\_NA\_} are physical 27303constants.\\ 27304Input : 27305\begin{center}{\tt \_c\_ }\end{center} 27306Output speed of light in vacuum : 27307\begin{center}{\tt 299792458\_m*s\verb|^|-1}\end{center} 27308Input : 27309\begin{center}{\tt \_NA\_ }\end{center} 27310Output Avogadro's number : 27311\begin{center}{\tt 6.0221367e23\_gmol\verb|^|-1}\end{center} 27312 27313\subsection{Constants Library} 27314The physical constants are in the {\tt Phys} menu, {\tt Constant} 27315sub-menu. 27316% or also in the {\tt Help} menu.\\ 27317The following table gives the Constants Library : 27318\begin{center} 27319\begin{tabular}{|l|l|} 27320\hline 27321Name & Description\\ 27322\hline 27323{\tt \_NA\_} &Avogadro's number\\ 27324{\tt \_k\_} &Boltzmann constant\\ 27325{\tt \_Vm\_} &Molar volume\\ 27326{\tt \_R\_} &Universal gas constant\\ 27327{\tt \_StdT\_} &Standard temperature \\ 27328{\tt \_StdP\_} &Standard pressure\\ 27329{\tt \_sigma\_} &Stefan-Boltzmann constant\\ 27330{\tt \_c\_} &Speed of light in vacuum\\ 27331{\tt \_epsilon0\_} &Permitivity of vacuum\\ 27332{\tt \_mu0\_} &Permeability of vacuum\\ 27333{\tt \_g\_} &Acceleration of gravity\\ 27334{\tt \_G\_} &Gravitational constant\\ 27335{\tt \_h\_} &Planck's constant\\ 27336{\tt \_hbar\_} &Dirac's constant\\ 27337{\tt \_q\_} &Electron charge\\ 27338{\tt \_me\_} &Electron rest mass\\ 27339{\tt \_qme\_} &q/me (Electron charge/mass)\\ 27340{\tt \_mp\_} &Proton rest mass\\ 27341{\tt \_mpme\_} &mp/me (proton mass/electron mass)\\ 27342{\tt \_alpha\_} &Fine structure constant\\ 27343{\tt \_phi\_} &Magnetic flux quantum\\ 27344{\tt \_F\_} & Faraday constant\\ 27345{\tt \_Rinfinity\_} &Rydberg constant\\ 27346{\tt \_a0\_} &Bohr radius\\ 27347{\tt \_muB\_} &Bohr magneton\\ 27348{\tt \_muN\_} &Nuclear magneton\\ 27349{\tt \_lambda0\_} &Photon wavelength (ch/e)\\ 27350{\tt \_f0\_} &Photon frequency (e/h)\\ 27351{\tt \_lambdac\_} &Compton wavelength\\ 27352{\tt \_rad\_} &1 radian\\ 27353{\tt \_twopi\_} &2*pi radians\\ 27354{\tt \_angl\_} &180 degrees angle\\ 27355{\tt \_c3\_} &Wien displacement constant\\ 27356{\tt \_kq\_} & k/q (Boltzmann/electron charge)\\ 27357{\tt \_epsilon0q\_} &epsilon0/q (permitivity /electron charge)\\ 27358{\tt \_qepsilon0\_} &q*epsilon0 (electron charge *permitivity)\\ 27359{\tt \_epsilonsi\_} &Silicium dielectric constant\\ 27360{\tt \_epsilonox\_} &Bioxyd of silicium dielectric constant\\ 27361{\tt \_I0\_} &Reference intensity\\ 27362\hline 27363\end{tabular} 27364\end{center} 27365To have the value of a constant, input the constant name in the command line 27366of {\tt Xcas} and evaluate with {\tt enter} (don't forget to put 27367{\tt \_} at the beginning and at the end of the constant name). 27368 27369\chapter{Programming} 27370 27371\section{Functions, programs and scripts} 27372 27373\subsection{The program editor} 27374 27375\texttt{Xcas} provides a program editor, which you can open with 27376\texttt{Alt+P}. This can be useful for writing small programs, but 27377for writing larger programs you may want to use your usual editor. 27378(Note that this requires an editor, such as \texttt{emacs}, and not a 27379word processor.) If you use your own editor, then you will need to 27380save the program to a file, such as \texttt{myprog.cxx}, and then load 27381it into \texttt{Xcas} with the command line command \texttt{load}, as 27382in \texttt{load("myprog.cxx")}. 27383 27384\subsection{Functions: \texttt{function, endfunction, \{ \}, local, 27385return}} 27386\index{function}\index{endfunction}\index{\{\}}\index{local}\index{return} 27387 27388You have already seen functions defined with \texttt{:=}. For 27389example, to define a function \texttt{sumprod} which takes two inputs 27390and returns a list with the sum and the product of the inputs, you can 27391enter 27392\begin{center} 27393{\tt sumprod(a,b) := [a+b,a*b]} 27394\end{center} 27395Afterwards, entering 27396\begin{center} 27397{\tt sumprod(3,5)} 27398\end{center} 27399will return 27400\begin{center} 27401{\tt [8,15]} 27402\end{center} 27403 27404You can define functions that are computed with a sequence of 27405instructions by putting the instructions between braces, where each 27406command ends with a semicolon. If any local variables will be used, 27407they can be declared with the \texttt{local} keyword, 27408followed by the variable names. The value returned by the function 27409will be indicated with the \texttt{return} keyword. For 27410example, the above function \texttt{sumprod} could also be defined by 27411\begin{verbatim} 27412sumprod(a,b) := { 27413local s, p; 27414s := a + b; 27415p := a*b; 27416return [s,p]; 27417} 27418\end{verbatim} 27419 27420Another way to use a sequence of instructions to define a function is 27421with the \texttt{function} \ldots 27422\texttt{endfunction} construction. With this 27423approach, the function name and parameters follow the 27424\texttt{function} keyword. This is otherwise like the previous 27425approach. The \texttt{sumprod} function could be defined by 27426\begin{verbatim} 27427function sumprod(a,b) 27428local s, p; 27429s := a + b; 27430p := a*b; 27431return [s,p]; 27432endfunction 27433\end{verbatim} 27434 27435\subsection{Local variables} 27436 27437Local variables in a function definition can be given initial values 27438in the line they are declared in if you put their initialization in 27439parentheses; for example, 27440\begin{verbatim} 27441local a,b; 27442a := 1; 27443\end{verbatim} 27444is the same as 27445\begin{verbatim} 27446local (a := 1), b; 27447\end{verbatim} 27448 27449Local variables should be given values within the function definition. 27450If you want to use a local variable as a symbolic variable, then you 27451can indicate that with the \texttt{assume\index{assume}} command. For 27452example, if you define a function \texttt{myroots} by 27453\begin{verbatim} 27454myroots (a) := { 27455local x; 27456return solve(x^2=a,x); 27457} 27458\end{verbatim} 27459then calling 27460\begin{center} 27461{\tt myroots(4)} 27462\end{center} 27463will simply return the empty list. You could leave \texttt{x} 27464undeclared, but that would make \texttt{x} a global variable and could 27465interact with other functions in unexpected ways. You can get the 27466behavior you probably expected by explicitly assuming \texttt{x} to be a 27467symbol\index{symbol@\textit{symbol}}; 27468\begin{verbatim} 27469myroots (a) := { 27470local x; 27471assume(x,symbol); 27472return solve(x^2=a,x); 27473} 27474\end{verbatim} 27475(Alternatively, you could use \texttt{purge(x)\index{purge}} instead 27476of \texttt{assume(x,symbol)}.) Now if you enter 27477\begin{center} 27478{\tt myroots(4)} 27479\end{center} 27480you will get 27481\begin{center} 27482 \[ [-2,2]\] 27483\end{center} 27484 27485\subsection{Default values of the parameters} 27486 27487You can give the parameters of a function default values by putting 27488\textit{parameter}\texttt{=}\textit{value} in the parameter list of 27489the function. For example, if you define a function 27490\begin{verbatim} 27491f(x,y=5,z) := { 27492return x*y*z; 27493} 27494\end{verbatim} 27495then 27496\begin{center} 27497{\tt f(1,2,3)} 27498\end{center} 27499will return the product $1*2*3=6$. If you give \texttt{f} only two 27500values as input, 27501\begin{center} 27502{\tt f(3,4)} 27503\end{center} 27504then these values will be given to the parameters which don't have default 27505values; in this case, \texttt{y} will get its default value \texttt{5} while 27506\texttt{3} and \texttt{4} will be assigned to \texttt{x} and 27507\texttt{z}, respectively. The result will be $x*y*z=3*5*4=60$. 27508 27509\subsection{Programs} 27510 27511A program\index{program} is similar to a function, and written like a 27512function without a return value. Programs are used to display results 27513or to create drawings. It is a good idea to turn a program into a 27514function by putting \texttt{return 0} at the end; this way you will 27515get a response of \texttt{0} when the program executes. 27516 27517\subsection{Scripts} 27518 27519A script is a file containing a sequence of instructions, each ending 27520with a semicolon. 27521 27522\subsection{Code blocks} 27523 27524A code block, such as used in defining functions, is a sequence of 27525statements delimited by braces or by \texttt{begin\index{begin}} and 27526\texttt{end\index{end}}. Each statement must end with a semicolon. 27527(If the block makes up a function, you can step through it one 27528statement at a time by using the debugger\index{debugger}; see section 27529\ref{sec:debug}.) 27530 27531\section{Basic instructions} 27532 27533\subsection{Comments: \texttt{//}}\index{comments}\index{//} 27534 27535The characters \texttt{//} indicate that you are writing a comment; 27536any text between \texttt{//} and the end of the line will be ignored 27537by \texttt{Xcas}. 27538 27539\subsection{Input: \texttt{input, Input, InputStr, textinput, output, 27540Output}} 27541\index{input}\index{Input}\index{InputStr}\index{textinput}\index{output} 27542\index{Output} 27543 27544You can prompt the user to enter a value for a variable with the 27545\texttt{input} (or \texttt{Input} command). If you enter 27546\begin{center} 27547{\tt input(a)} 27548\end{center} 27549the the user will be given a box where they can enter a value for the 27550variable \texttt{a}. There will be a prompt 27551indicating the name of the variable; if you want a more descriptive 27552prompt, you can give \texttt{input} a string argument before the 27553variable name. 27554\begin{center} 27555{\tt input("Set a to the value: ",a)} 27556\end{center} 27557will prompt the user with "Set a to the value: " before the input box. 27558 27559If the value that you enter for \texttt{input} is a string, it should 27560be between quotes. If you want the user to enter a string without 27561having to use the quotes, you can use 27562\texttt{InputStr} or 27563\texttt{textinput}, which will assume the input will 27564be a string and so the user won't need to use quotes. 27565 27566The \texttt{output} (or \texttt{Output}) 27567command can take strings (or variables representing strings) as 27568arguments and can be used to add information to the input window. For 27569example, if you enter 27570\begin{center} 27571{\tt input(output("Calculate p(a)"),"polynomial",p,"value",a)} 27572\end{center} 27573then you will get a window with a box containing 27574\begin{center} 27575{\tt Calculate p(a)} 27576\end{center} 27577followed by the prompts for \texttt{p} and \texttt{a}. 27578 27579\subsection{Reading a single keystroke: \texttt{getKey}}\index{getKey} 27580 27581If you want the user to enter a single key, you can use the 27582\texttt{getKey} command, which doesn't take any 27583arguments, to get the ASCII code of the next keystroke. For example, 27584if you enter 27585\begin{center} 27586{\tt asciicode := getKey()} 27587\end{center} 27588and then hit the \texttt{A} key, then the variable \texttt{asciicode} 27589will have the value $65$, which is the ASCII code of capital A. 27590 27591\subsection{Checking conditions: \texttt{assert}}\index{assert} 27592 27593You can break out of a function with an error by using the 27594\texttt{assert} command, which takes a boolean as an argument. If the 27595boolean is false, then the function will return with an error. For 27596example, if you define a function 27597\begin{verbatim} 27598 sqofpos(x) := {assert(x > 0); return x^2;} 27599\end{verbatim} 27600then if you enter 27601\begin{center} 27602{\tt sqofpos(4)} 27603\end{center} 27604you will get $16$, but 27605\begin{center} 27606{\tt sqofpos(-4)} 27607\end{center} 27608will return an error, since \texttt{-4 > 0} is false. 27609 27610\subsection{Checking the type of the argument: 27611\texttt{type, subtype, compare, getType}} 27612\index{type}\index{subtype}\index{compare}\index{getType} 27613\index{real|textit}\index{double|textit}\index{DOM\_FLOAT|textit} 27614\index{integer|textit}\index{DOM\_INT|textit} 27615\index{complex|textit}\index{DOM\_COMPLEX|textit} 27616\index{identifier|textit}\index{DOM\_IDENT|textit} 27617\index{vector|textit}\index{DOM\_LIST|textit} 27618\index{func|textit}\index{DOM\_FUNC|textit} 27619\index{expression|textit}\index{DOM\_SYMBOLIC|textit} 27620\index{rational|textit}\index{DOM\_RAT|textit} 27621\index{string|textit}\index{DOM\_STRING|textit} 27622\index{NUM|textit}\index{VAR|textit} 27623\index{STR|textit}\index{EXPR|textit} 27624\index{NONE|textit}\index{PIC|textit} 27625\index{MAT|textit}\index{FUNC|textit} 27626\index{LIST|textit} 27627 27628You can check the type of the argument of a function (or anything 27629else, for that matter) with the \texttt{type} command. 27630For example, entering 27631\begin{center} 27632{\tt type(4)} 27633\end{center} 27634will return 27635\begin{center} 27636{\tt integer} 27637\end{center} 27638 27639The output of a \texttt{type} command is actually an integer from 1 to 2764012. The output of \texttt{type(4)} is \texttt{integer}, which is a 27641constant with the value 1. Another way to represent this type is with 27642\texttt{DOM\_INT}; entering 27643\begin{center} 27644{\tt type(4) == DOM\_INT} 27645\end{center} 27646will return 27647\begin{center} 27648{\tt true} 27649\end{center} 27650Possible types (followed by the integer they represent) include: 27651\begin{itemize} 27652 \item 27653 \texttt{real}, \texttt{double} or \texttt{DOM\_FLOAT} (1). 27654 \item 27655 \texttt{integer} or \texttt{DOM\_INT} (2). 27656 \item 27657 \texttt{complex} or \texttt{DOM\_COMPLEX} (4). 27658 \item 27659 \texttt{identifier} or \texttt{DOM\_IDENT} (6). 27660 \item 27661 \texttt{vector} or \texttt{DOM\_LIST} (7). 27662 \item 27663 \texttt{func} or \texttt{DOM\_FUNC} (13). 27664 \item 27665 \texttt{expression} or \texttt{DOM\_SYMBOLIC} (8). 27666 \item 27667 \texttt{rational} or \texttt{DOM\_RAT} (10). 27668 \item 27669 \texttt{string} or \texttt{DOM\_STRING} (12). 27670\end{itemize} 27671 27672If the item being tested is a list (in \texttt{DOM\_LIST}), then the 27673\texttt{subtype} command can determine what type of 27674list it is. If the object is a sequence, then \texttt{subtype} returns $1$; 27675\begin{center} 27676{\tt subtype(1,2,3)} 27677\end{center} 27678returns 27679\begin{center} 27680{\tt 1} 27681\end{center} 27682If the object is a set, then \texttt{subtype} returns 2. If the 27683object is a polynomial represented as a list (see section 27684\ref{sec:polynomials}), then \texttt{subtype} will return 10. If the 27685object isn't one of these types of list, then \texttt{subtype} returns 0. 27686 27687The \texttt{compare} function will compare two objects 27688taking their type into account; in other words, \texttt{compare(a,b)} 27689returns \texttt{1} (true) if \texttt{a} and \texttt{b} have the same 27690type with \texttt{a} less than \texttt{b}, or if \texttt{a} and 27691\texttt{b} have different types and the integer \texttt{type(a)} is 27692less than \texttt{type(b)}. For example, 27693\begin{center} 27694{\tt compare("a","b")} 27695\end{center} 27696returns 27697\begin{center} 27698{\tt 1} 27699\end{center} 27700since \texttt{"a"} and \texttt{"b"} have the same type 27701(\texttt{string}) and \texttt{"a"} is less than \texttt{"b"} in the 27702string ordering. Also, if \texttt{b} is a formal variable, then 27703\begin{center} 27704{\tt compare("a",b)} 27705\end{center} 27706returns 27707\begin{center} 27708{\tt 0} 27709\end{center} 27710since the type of \texttt{"a"} is \texttt{string} (the integer 12) 27711while the type of \texttt{b} is \texttt{identifier} (the integer 6) 27712and \texttt{12} is not less than \texttt{6}. 27713 27714The \texttt{getType} command is similar to \texttt{type} in that it 27715takes an object and returns the type, but it has different possible 27716return values. It is included for compatibility reasons. 27717For example, 27718\begin{center} 27719{\tt getType(3.14)} 27720\end{center} 27721returns 27722\begin{center} 27723{\tt NUM} 27724\end{center} 27725and 27726\begin{center} 27727{\tt getType(x)} 27728\end{center} 27729returns 27730\begin{center} 27731{\tt VAR} 27732\end{center} 27733Other possible return values include \texttt{STR}, \texttt{EXPR}, 27734\texttt{NONE}, \texttt{PIC}, \texttt{MAT} and \texttt{FUNC}. 27735 27736\subsection{Printing: \texttt{print, Disp, ClrIO}} 27737\index{print}\index{Disp}\index{ClrIO} 27738 27739The \texttt{print} (or \texttt{Disp}) command 27740will print its arguments in a special pane and return the number 27741\texttt{1}. For example, 27742\begin{center} 27743{\tt print("Hello")} 27744\end{center} 27745will result in 27746\begin{center} 27747{\tt Hello} 27748\end{center} 27749If you enter 27750\begin{center} 27751{\tt a := 12} 27752\end{center} 27753then 27754\begin{center} 27755{\tt print("a =",a)} 27756\end{center} 27757will print 27758\begin{center} 27759{\tt "a =", 12} 27760\end{center} 27761 27762The \texttt{ClrIO} (no argument) will erase the printing 27763that was done in the level it was typed. For example, 27764\begin{center} 27765{\tt print("Hello"); ClrIO()} 27766\end{center} 27767will simply return the result \texttt{(1,1)}. 27768 27769\subsection{Displaying exponents: \texttt{printpow}}\index{printpow} 27770 27771The \texttt{printpow} command determines how the \texttt{print} 27772command will print exponents. By default, 27773\begin{center} 27774{\tt print(x\^{}3)} 27775\end{center} 27776will print 27777\begin{center} 27778{\tt x\^{}3} 27779\end{center} 27780If you use the command 27781\begin{center} 27782{\tt printpow(1)} 27783\end{center} 27784then 27785\begin{center} 27786{\tt print(x\^{}3)} 27787\end{center} 27788will print as 27789\begin{center} 27790{\tt pow(x,3)} 27791\end{center} 27792If you use the command 27793\begin{center} 27794{\tt printpow(-1)} 27795\end{center} 27796then 27797\begin{center} 27798{\tt print(x\^{}3)} 27799\end{center} 27800will print as 27801\begin{center} 27802{\tt x**3} 27803\end{center} 27804Finally, 27805\begin{center} 27806{\tt printpow(0)} 27807\end{center} 27808will restore the default form. 27809 27810\subsection{Infixed assignments: \texttt{=>, :=, =<}} 27811\index{=>}\index{:=}\index{=<} 27812 27813The infixed operators \texttt{=>}, \texttt{:=}, and \texttt{=<} can 27814all store a value in a variable, but their arguments are in different 27815order. Also, \texttt{:=} and \texttt{=<} have different effects when 27816the first argument is an element of a list stored in a variable, since 27817\texttt{=<} modifies list elements by reference. (See section 27818\ref{ssec:assignmentdiffs}.) 27819\begin{itemize} 27820 \item \texttt{=>} is the infixed version of \texttt{sto}, it stores 27821 the value in the first argument in the variable in the second 27822 argument. Both\\ 27823 Input: 27824 \begin{center} 27825 \tt 27826 4 => a 27827 \end{center} 27828 and: 27829 \begin{center} 27830 \tt 27831 sto(4,a) 27832 \end{center} 27833 store the value 4 in the variable \texttt{a}. 27834 \item \texttt{:=} and \texttt{=<} both have a variable as the first 27835 argument and the value to store in the variable as the second 27836 argument. Both\\ 27837 Input: 27838 \begin{center} 27839 \tt 27840 a := 4 27841 \end{center} 27842 and: 27843 \begin{center} 27844 \tt 27845 a =< 4 27846 \end{center} 27847 store the value 4 in the variable \texttt{a}. 27848 27849 However, suppose\\ 27850 Input: 27851 \begin{center} 27852 \tt 27853 A := [0,1,2,3,4]\\ 27854 B := A 27855 \end{center} 27856 and you want to change \texttt{A[3]}.\\ 27857 Input: 27858 \begin{center} 27859 \tt 27860 A[3] =< 33 27861 \end{center} 27862 will change both \texttt{A} and \texttt{B}\\ 27863 Input: 27864 \begin{center} 27865 \tt 27866 A, B 27867 \end{center} 27868 Output: 27869 \begin{center} 27870 \tt 27871 [0,1,2,33,4], [0,1,2,33,4] 27872 \end{center} 27873 Here, \texttt{A} pointed to the list \texttt{[0,1,2,3,4]} and 27874 setting \texttt{B} to \texttt{A}, \texttt{B} also pointed to 27875 \texttt{[0,1,2,3,4]}. Changing an element of \texttt{A} by 27876 reference changes the list that \texttt{A} points to, which 27877 \texttt{B} points to. 27878\end{itemize} 27879 27880Note that multiple assigments can be made using sequences or lists. Both\\ 27881Input: 27882\begin{center} 27883 \tt 27884 [a, b, c] := [1, 2, 3] 27885\end{center} 27886and: 27887\begin{center} 27888 \tt 27889 (a, b, c) := (1, 2, 3) 27890\end{center} 27891assign \texttt{a} the value 1, \texttt{b} the value 2, and \texttt{c} 27892the value 3. If multiple assignments are made this way and variables 27893are on the right hand side, they will be replaced by their values 27894before the assignment. If \texttt{a} contains 5, then\\ 27895Input: 27896\begin{center} 27897 \tt 27898 (a,b) := (2,a) 27899\end{center} 27900then \texttt{b} will get the previous value of \texttt{a}, 5, and not 27901the new value of \texttt{a}, 2. 27902 27903\subsection{Assignment by copying: \texttt{copy}}\index{copy} 27904 27905The \texttt{copy} command creates a copy of its argument, which is 27906typically a list of some type. If \texttt{B} is a list and \texttt{A 27907:= B}, then \texttt{A} and \texttt{B} point to the same list, and so 27908changing one will change the other. But if \texttt{A := copy(B)}, 27909then \texttt{A} and \texttt{B} will point to different lists with the 27910same values, and so can be changed individually.\\ 27911Input: 27912\begin{center} 27913 \tt 27914 B := [[4,5],[2,6]]\\ 27915 A := B\\ 27916 C := copy(B) 27917\end{center} 27918Output: 27919\begin{center} 27920 \tt 27921 A, B, C 27922\end{center} 27923Output: 27924\begin{center} 27925 \tt 27926 [[4,5],[2,6]],[[4,5],[2,6]],[[4,5],[2,6]] 27927\end{center} 27928Input: 27929\begin{center} 27930 \tt 27931 B[1] =< [0,0] 27932\end{center} 27933Input: 27934\begin{center} 27935 \tt 27936 A, B, C 27937\end{center} 27938Output: 27939\begin{center} 27940 \tt 27941[[4,5],[0,0]],[[4,5],[0,0]],[[4,5],[2,6]] 27942\end{center} 27943 27944\subsection{The difference between \texttt{:=} and \texttt{=<}} 27945\label{ssec:assignmentdiffs} 27946 27947The \texttt{:=} and \texttt{=<} assignment operators have different 27948effects when they are used to modify an element of a list contained in 27949a variable, since \texttt{=<} modifies the element by reference. 27950Otherwise, they will have the same effect. 27951 27952For example, if\\ 27953Input: 27954\begin{center} 27955 \tt 27956 A := [1,2,3] 27957\end{center} 27958then \\ 27959Input: 27960\begin{center} 27961 \tt 27962 A[1] := 5 27963\end{center} 27964and\\ 27965Input: 27966\begin{center} 27967 \tt 27968 A[1] =< 5 27969\end{center} 27970both change \texttt{A[1]} to 5, so \texttt{A} will be 27971\texttt{[1,5,3]}, but they do it in different ways. The command 27972\texttt{A[1] =< 5} changes the middle value in the list that 27973\texttt{A} originally pointed to, and so any other variable pointing 27974to the list will be changed, but \texttt{A[1] := 5} will create a 27975duplicate list with the middle element of 5, and so any other variable 27976pointing to the original list won't be affected.\\ 27977Input: 27978\begin{center} 27979 \tt 27980 A:=[0,1,2,3,4]\\ 27981 B:=A\\ 27982 B[3]=<33\\ 27983 A,B 27984\end{center} 27985Output: 27986\begin{center} 27987 \tt 27988 [0,1,2,33,4],[0,1,2,33,4] 27989\end{center} 27990Input: 27991\begin{center} 27992 \tt 27993 A:=[0,1,2,3,4]\\ 27994 B:=A\\ 27995 B[3]:=33\\ 27996 A,B 27997\end{center} 27998Output: 27999\begin{center} 28000 \tt 28001 [0,1,2,3,4],[0,1,2,33,4] 28002\end{center} 28003If \texttt{B} is set equal to a copy of \texttt{A} instead of 28004\texttt{A}, then changing \texttt{B} won't affect \texttt{A}.\\ 28005Input: 28006\begin{center} 28007 \tt 28008 A:=[0,1,2,3,4]\\ 28009 B:=copy(A)\\ 28010 B[3]=<33\\ 28011 A,B 28012\end{center} 28013Output: 28014\begin{center} 28015 \tt 28016 [0,1,2,3,4],[0,1,2,33,4] 28017\end{center} 28018 28019\section{Control structures} 28020 28021\subsection{\texttt{if} statements: \texttt{if, then, else, end, elif}} 28022\index{if}\index{then}\index{else}\index{end}\index{elif} 28023 28024The \texttt{Xcas} language has different ways of writing 28025\texttt{if\ldots then} statements (see section \ref{subsec:piecewise}). The 28026standard version of the \texttt{if\ldots then} statement consists of 28027the \texttt{if} keyword, followed by a boolean expression (see section 28028\ref{sec:boolean} in parentheses, followed by a statement block which 28029will be executed if the boolean is true. 28030 28031As an example, if the variables \texttt{a} and \texttt{b} have the 28032values \texttt{3} and \texttt{2}, respectively, and you enter 28033\begin{center} 28034 {\tt if (a > b) \{ a := a + 5; b := a - b;\}} 28035\end{center} 28036then since \texttt{a > b} will evaluate to true, the variable 28037\texttt{a} will be reset to \texttt{8} and \texttt{b} will be reset 28038to the value \texttt{6}. 28039 28040An \texttt{if} statement can include a block of statements to execute 28041when the boolean is false by putting it at the end following the 28042\texttt{else} keyword. For example, if the variable 28043\texttt{val} has a real value, then the statement 28044\begin{center} 28045 {\tt if (val > 0) \{abs := val;\} else \{abs := -1*val; \} } 28046\end{center} 28047will set \texttt{abs} to the same value as \texttt{val} if 28048\texttt{val} is positive and it will set \texttt{abs} to negative the 28049value of \texttt{val} otherwise. 28050 28051An alternate way to write an \texttt{if} statement is to 28052enclose the code block in \texttt{then} and 28053\texttt{end} instead of braces; if the variable \texttt{a} 28054is equal to \texttt{3}, then 28055\begin{center} 28056 {\tt if (a > 1) then a := a + 5; end} 28057\end{center} 28058will reset \texttt{a} to \texttt{8}. 28059An \texttt{else} block can be included by putting the else statements 28060after \texttt{else} and before the \texttt{end}. For example, with 28061\texttt{a} having the value \texttt{8} as above, 28062\begin{center} 28063 {\tt if (a > 10) then a := a + 10; else a := a - 5; end} 28064\end{center} 28065will reset \texttt{a} to the value \texttt{3}. 28066This 28067can also be written: 28068\begin{center} 28069 {\tt si (a > 10) alors a := a + 10; sinon a := a - 5; fsi} 28070\end{center} 28071 28072Several \texttt{if} statements can be nested; for example, the statement 28073\begin{center} 28074 {\tt if (a > 1) then a := 1; else if (a < 0) then a := 0; else a := 28075 0.5; end; end} 28076\end{center} 28077A simpler way is to replace the \texttt{else if} by \texttt{elif}; the 28078above statement can be written 28079\begin{center} 28080 {\tt if (a > 1) then a := 1; elif (a < 0) then a := 0; else a := 28081 0.5; end} 28082\end{center} 28083In general, such a combination can be written 28084\begin{verbatim} 28085 if (boolean 1) then 28086 block 1; 28087 elif (boolean 2) then 28088 block 2; 28089 ... 28090 elif (boolean n) then 28091 block n; 28092 else 28093 last block; 28094 end 28095\end{verbatim} 28096(where the last \texttt{else} is optional.) 28097For example, if you want to define a function $f$ by 28098\[ 28099f(x) = 28100 \begin{cases} 28101 8 & \text{if } x > 8\\ 28102 4 & \text{if } 4 < x \le 8\\ 28103 2 & \text{if } 2 < x \le 4\\ 28104 1 & \text{if } 0 < x \le 2\\ 28105 0 & \text{if } x \le 0 28106 \end{cases} 28107\] 28108you can enter 28109\begin{verbatim} 28110 f(x) := { 28111 if (x > 8) then 28112 return 8; 28113 elif (x > 4) then 28114 return 4; 28115 elif (x > 2) then 28116 return 2; 28117 elif (x > 0) then 28118 return 1; 28119 else 28120 return 0; 28121 end; 28122 } 28123\end{verbatim} 28124 28125\subsection{The switch statement: \texttt{switch, 28126case, default}}\index{switch}\index{case}\index{default} 28127 28128The \texttt{switch} statement can be used when you want the value of 28129a block to depend on an integer. It takes one argument, an 28130expression which evaluates to an integer. It should be followed by 28131a sequence of \texttt{case} statements, which takes the form 28132\texttt{case} followed by an integer and then a colon, which is 28133followed by a code block to be executed if the expression equals the 28134integer. At the end is an optional \texttt{default:} 28135statement, which is followed by a code block to be executed if the 28136expression doesn't equal any of the given integers. For example, if 28137you wanted to define a function of three variables which performed an 28138operation on the first two variables depending on the third, you could 28139enter 28140\begin{verbatim} 28141 oper(a,b,c) := { 28142 switch (c) { 28143 case 1: {a := a + b; break;} 28144 case 2: {a := a - b; break;} 28145 case 3: {a := a * b; break;} 28146 default: {a := a ^ b;} 28147 } 28148 return a; 28149 } 28150\end{verbatim} 28151Then 28152\begin{center} 28153 oper(2,3,1) 28154\end{center} 28155will return $2+3=5$, since the third argument is \texttt{1}, and 28156\begin{center} 28157 oper(2,3,2) 28158\end{center} 28159will return $2-3=-1$, since the third argument is \texttt{2}. 28160 28161\subsection{The for loop: \texttt{for, from, to, step, do, end\_for}} 28162\index{for}\index{from}\index{to}\index{step}\index{do}\index{end\_for} 28163 28164The \texttt{for} loop has three different forms, each of which uses an 28165index variable. If the \texttt{for} loop is used in a program, the 28166index variable should be declared as a local variable. (Recall that 28167\texttt{i} represents the imaginary unit, and so cannot be used as the 28168index.) 28169 28170\paragraph{The first form} 28171For the first form, the \texttt{for} is followed by the starting value 28172for the index, the end condition, and the increment step, separated by 28173semicolons and in parentheses. Afterwards is a block of code to be 28174executed for each iteration. For example, to add the even numbers 28175less than 100, you can set the running total to 0, 28176\begin{center} 28177 {\tt S := 0} 28178\end{center} 28179and the use an \texttt{for} loop to do the summing, 28180\begin{center} 28181 {\tt for (j := 0; j < 100; j := j + 2) \{S := S + j\}} 28182\end{center} 28183 28184\paragraph{The second form} 28185The second form of a \texttt{for} loop has a fixed increment for the 28186index. It is 28187written out with \texttt{for} followed by the index, followed by 28188\texttt{from}, the initial value, \texttt{to}, the ending value, 28189\texttt{step}, the size of the increment, and finally the statements 28190to be executed between \texttt{do} and \texttt{end\_for}. For 28191example, having set the variable \texttt{S} equal to 0, you can again 28192add the even numbers less than 100 with 28193\begin{center} 28194 {\tt for j from 2 to 98 step 2 do S := S + j; end\_for} 28195\end{center} 28196There is also a French version of this syntax; 28197\begin{center} 28198 {\tt pour j de 2 jusque 98 pas 2 faire S := S + j; fpour} 28199\end{center} 28200 28201\paragraph{The third form} 28202The third form of the \texttt{for} loop lets you iterate over the 28203values in a list (or a set or a range). In this form, the 28204\texttt{for} is followed by the index, then \texttt{in}, the list, and 28205then the instructions between \texttt{do} and \texttt{end\_for}. For 28206example, to add all integers from 1 to 100, you can again set the 28207running total \texttt{S} to 0, then 28208\begin{center} 28209 {\tt for j in 1..100 do S:= S + j; end\_for} 28210\end{center} 28211or 28212\begin{center} 28213 {\tt pour j in 1..100 faire S:= S + j; fpour} 28214\end{center} 28215 28216\subsection{The repeat loop: \texttt{repeat, until}} 28217\index{repeat}\index{until} 28218 28219The \texttt{repeat} loop allows you to repeat statements until a given 28220condition is met. To use it, enter \texttt{repeat}, the statements, 28221the keyword \texttt{until} followed by the condition, a 28222boolean. For example, if you want the user to enter a value for a 28223variable \texttt{x} which is greater than 4, you could have 28224\begin{verbatim} 28225 repeat 28226 input("Enter a value for x (greater than 4)",x); 28227 until (x > 4); 28228\end{verbatim} 28229This can also be written\index{repeter}\index{jusqua} 28230\begin{verbatim} 28231 repeter 28232 input("Enter a value for x (greater than 4)",x); 28233 jusqua (x > 4); 28234\end{verbatim} 28235 28236\subsection{The while loop: \texttt{while}} 28237\index{while} 28238 28239The \texttt{while} loop is used to repeat a code block as long as a 28240given condition holds. To use it, enter \texttt{while}, the 28241condition, and then a code block. For example, to add the terms of 28242the harmonic series $1 + 1/2 + 1/3 + 1/4 + \dots$ until a term is less 28243than 0.05, you could initialize the sum \texttt{S} to 0 and let 28244\texttt{j} be the first term 1. Then 28245\begin{center} 28246{\tt while (1/j >= 0.05) \{S := S + 1/j; j := j+1;\}} 28247\end{center} 28248will find the sum. This line is the same as 28249\begin{center} 28250{\tt tantque (1/j >= 0.05) faire S := S + 1/j; j := j+1; ftantque} 28251\end{center} 28252 28253Note that a \texttt{while} loop can also be written as a \texttt{for} 28254loop. For example, as long as \texttt{S} is set to 0 and \texttt{j} 28255is set to 1 , the above loop 28256can be written as 28257\begin{center} 28258{\tt for (;1/j >= 0.05;) \{S := S + 1/j; j := j+1;\}} 28259\end{center} 28260or, with only \texttt{S} set to 0, 28261\begin{center} 28262{\tt for (j := 1; 1/j >= 0.05; j++) \{S := S + 1/j;\}} 28263\end{center} 28264 28265\subsection{Breaking out a loop: \texttt{break}}\index{break} 28266 28267If your program is running a loop and you want it to exit the loop 28268without finishing it, you can use the \texttt{break} 28269command. For example, you can define a program 28270\begin{verbatim} 28271 testbreak(a,b) := { 28272 local r; 28273 while (true) { 28274 if (b == 0) {break;} 28275 r := irem(a,b); 28276 a := b; 28277 b := r; 28278 } 28279 return a; 28280 } 28281\end{verbatim} 28282If you then enter 28283\begin{center} 28284 {\tt testbreak(4,0)} 28285\end{center} 28286it will return 28287\begin{center} 28288 {\tt 4} 28289\end{center} 28290since the \texttt{while} loop is interrupted when \texttt{b} is 0 and 28291\texttt{a} is 4. 28292 28293\subsection{Going to the next iteration of a loop: \texttt{continue}} 28294 28295The \texttt{continue} command will skip the rest of the current 28296iteration of a loop and go to the next iteration. For example, 28297if you enter 28298\begin{verbatim} 28299 S := 0 28300 for (j := 1, j <= 10; j++) { 28301 if (j == 5) {continue;} 28302 S := S + j; 28303 } 28304\end{verbatim} 28305then \texttt{S} will be 50, which is the sum of the integers from 1 to 2830610 except for 5, since the loop never gets to \texttt{S := S + j} when 28307\texttt{j} is equal to 5. 28308 28309\subsection{Changing the order of execution: \texttt{goto, label}} 28310\index{goto}\index{label} 28311 28312The \texttt{goto} command will tell a program to jump to a different 28313spot in a program, where the spot needs to have been marked with 28314\texttt{label}. They both must have the same argument, which is 28315simply a sequence of characters. For example, the following program 28316will add the terms of the harmonic series until the term is less than 28317some specified value \texttt{eps} and print the result. 28318\begin{verbatim} 28319 harmsum(eps) := { 28320 local S, j; 28321 S := 0; 28322 j := 0; 28323 label(spot); 28324 j := j + 1; 28325 S := S + 1/j; 28326 if (1/j >= eps) goto (spot); 28327 print(S); 28328 return 0; 28329 } 28330\end{verbatim} 28331 28332\section{Other useful instructions} 28333 28334\subsection{Define a function with a variable number of arguments: 28335\texttt{args}}\index{args} 28336 28337The \texttt{args} (or \texttt{args(NULL)}) command returns the list of 28338arguments of a function. The element at index 0 is the name of the 28339function, the remaining are the arguments passed to the function. 28340This allows you to define functions with a variable number of arguments. 28341 28342Note that \texttt{args()} will not work, the command must be called as 28343\texttt{args} or \texttt{args(NULL)}. You can also use 28344\texttt{(args)[0]} to get the name of the function and 28345\texttt{(args)[1]} to get the first argument, etc., but the 28346parentheses about \texttt{args} is mandatory. 28347 28348\noindent 28349Input: 28350\begin{center} 28351 \tt 28352 testargs() := {local y; y := args; return y[1];} 28353\end{center} 28354then: 28355\begin{center} 28356 \tt 28357 testargs(12,5) 28358\end{center} 28359Output: 28360\begin{center} 28361 \tt 28362 12 28363\end{center} 28364 28365\noindent 28366Input: 28367\begin{verbatim} 28368total():={ 28369 local s,a; 28370 a:=args; 28371 s:=0; 28372 for (k:=1;k<size(a);k++){ 28373 s:=s+a[k]; 28374 } 28375 return s; 28376 } 28377\end{verbatim} 28378then: 28379\begin{center} 28380 \tt 28381 total(1,2,3,4) 28382\end{center} 28383Output: 28384\begin{center} 28385 \tt 28386 10 28387\end{center} 28388 28389\subsection{Assignments in a program} 28390 28391Recall that the \texttt{=<} operator will change the value of a single 28392entry in a list or matrix by reference (see subsection \ref{subsec:refassign}). 28393This make it efficient when changing many values, one at a time, in a 28394list, as might be done by a program. 28395 28396Care must be taken, since your intent might be changed when a program 28397is compiled. For example, if a program contains 28398\begin{verbatim} 28399 local a; 28400 a := [0,1,2,3,4]; 28401 ... 28402 a[3] =< 33; 28403\end{verbatim} 28404then in the compiled program, \texttt{a := [0,1,2,3,4]} will be 28405replaced by \texttt{a := [0,1,2,33,4]}. To avoid this, you can assign 28406a copy of the list to \texttt{a}; you could write 28407\begin{verbatim} 28408 local a; 28409 a := copy([0,1,2,3,4]); 28410 ... 28411 a[3] =< 33; 28412\end{verbatim} 28413Alternately, you could use a command which recreates a list every time 28414the program is run, such as \texttt{makelist\index{makelist}} or 28415\texttt{\$}, instead of copying a list; \texttt{a := 28416makelist(n,n,0,4)} or \texttt{a := [n\$(n=0..4)]} can also be used in 28417place of \texttt{a := [0,1,2,3,4]}. 28418 28419\subsection{Writing variable values to a file: \texttt{write}} 28420\index{write} 28421 28422You can save variable values to a file, to be read later, with the 28423\texttt{write} command. This command takes a string for a file name 28424and a list of variables to save. For example, if \texttt{a} has the 28425value \texttt{3.14} and \texttt{b} has the value \texttt{7}, then 28426\begin{center} 28427 {\tt write("foo",a,b)} 28428\end{center} 28429will create a file named ``foo'' with the contents 28430\begin{verbatim} 28431 a:=(3.14); 28432 b:=7; 28433\end{verbatim} 28434If you wanted to store the first million digits of $\pi$ to a file, 28435you could set it equal to a variable 28436\begin{center} 28437 {\tt pidec := evalf(pi,10\^{}6):;} 28438\end{center} 28439and then store it in a file 28440\begin{center} 28441 {\tt write("pi1million",pidec)} 28442\end{center} 28443 28444If you want to restore the values of variables saved this way, for 28445example in a different session or if you have purged the 28446variables, then you can use the \texttt{read} command, which simply 28447takes a file name as a string. If, in a different session, you want 28448to use the values of \texttt{a} and \texttt{b} above, the command 28449\begin{center} 28450 {\tt read("foo")} 28451\end{center} 28452will give them the values \texttt{3.14} and \texttt{7} again. Note 28453that this will silently overwrite any values that \texttt{a} and 28454\texttt{b} might have had. 28455 28456\subsection{Writing output to a file: \texttt{fopen, fclose, fprint}} 28457\index{fopen}\index{fclose}\index{fprint} 28458 28459You can use the \texttt{fopen}, \texttt{fprint} and \texttt{fclose} 28460commands to write output to a file instead of the screen. 28461 28462To begin, you need to open the file and associate it with a variable. 28463You use \texttt{fopen} for this, which takes a file name as argument. 28464For example, 28465\begin{center} 28466 {\tt f := fopen("bar")} 28467\end{center} 28468will create a file named ``bar'' (and so erase it if it already 28469exists). You can use the \texttt{fprint} command to write to the 28470file; it takes the variable representing the file as the first 28471argument, followed by what you want to write to the file. For 28472example, if \texttt{x} is equal to 9, then 28473\begin{center} 28474 {\tt fprint(f,"x + 1 is ", x+1)} 28475\end{center} 28476will put 28477\begin{verbatim} 28478 "x + 1 is "10 28479\end{verbatim} 28480in the file. Note that the quotation marks are inserted with the 28481string. If you want to insert strings without quotes, then you can 28482give \texttt{fprint} a second argument of 28483\texttt{Unquoted\index{Unquoted@\texttt{Unquoted}}}. If instead of 28484the above \texttt{printf} you entered 28485\begin{center} 28486 {\tt fprint(f,Unquoted,"x + 1 is ", x+1)} 28487\end{center} 28488then the file would contain 28489\begin{verbatim} 28490 x + 1 is 10 28491\end{verbatim} 28492Finally, after you have finished writing what you want into the file, 28493you close the file with the \texttt{fclose} command, 28494\begin{center} 28495 {\tt fclose(f)} 28496\end{center} 28497 28498\subsection{Using strings as names: \texttt{\#}}\index{\#} 28499 28500Variable and function names are symbols, namely sequences of characters, which are 28501different from strings. For example, you can have a variable named \texttt{abc}, but 28502not \texttt{"abc"}. The \texttt{\#} operator will turn a 28503string into a symbol; for example \texttt{(\#"abc")} is the symbol 28504\texttt{abc}. 28505 28506If you enter 28507\begin{center} 28508 {\tt a := "abc"; (\#a) := 3} 28509\end{center} 28510or 28511\begin{center} 28512 {\tt (\#"abc") := 3} 28513\end{center} 28514then the variable \texttt{abc} will have the value \texttt{3}. 28515Entering \texttt{\#a} will still give you \texttt{abc}; you can get 28516\texttt{3} with \texttt{eval(\#a)}. 28517 28518Similarly for functions. If you enter 28519\begin{center} 28520 {\tt b := "sin"; (\#b)(pi/4)} 28521\end{center} 28522or 28523\begin{center} 28524 {\tt (\#"sin")(pi/4)} 28525\end{center} 28526you will get 28527\begin{center} 28528 {\tt 1/sqrt(2)} 28529\end{center} 28530which is {\tt sin(pi/4)}. 28531 28532\subsection{Using strings as commands: \texttt{expr}} 28533\index{expr} 28534 28535The \texttt{expr} command will let you use a string as a 28536command. Given a string that expresses a valid command, \texttt{expr} 28537will convert the string to the command and evaluate it. 28538For example, if you enter 28539\begin{center} 28540 {\tt expr("c := 1")} 28541\end{center} 28542then the variable \texttt{c} will be set to 1. 28543Similarly, if you enter 28544\begin{center} 28545 {\tt a := "ifactor(54)"} 28546\end{center} 28547then 28548\begin{center} 28549 {\tt expr(a)} 28550\end{center} 28551will return 28552\begin{center} 28553 {\tt 2 * 3\^{}3} 28554\end{center} 28555which is the same thing as entering \texttt{ifactor(54)} directly. 28556 28557You can also use \texttt{expr} to convert a string to a number. 28558If a string is simply a number enclosed by quotation marks, then 28559\texttt{expr} will return the number. For example, 28560\begin{center} 28561 {\tt expr("123")} 28562\end{center} 28563will return 28564\begin{center} 28565 {\tt 123} 28566\end{center} 28567In particular, the following strings will be converted to the 28568appropriate number. 28569\begin{itemize} 28570 \item 28571 A string consisting of the digits 0 through 9 which doesn't start 28572 with 0 will be converted to an integer. For example, 28573 \begin{center} 28574 {\tt expr("2133")} 28575 \end{center} 28576 will return 28577 \begin{center} 28578 {\tt 2133} 28579 \end{center} 28580 28581 \item 28582 A string consisting of the digits 0 through 9 which contains a 28583 single decimal point will be converted to a decimal. 28584 For example, 28585 \begin{center} 28586 {\tt expr("123.4")} 28587 \end{center} 28588 will return 28589 \begin{center} 28590 {\tt 123.4} 28591 \end{center} 28592 28593 \item 28594 A string consisting of the digits 0 through 9, possibly containing a 28595 single decimal point, followed by \texttt{e} and then more digits 0 28596 through 9, will be read as a decimal in exponential notation. 28597 For example, 28598 \begin{center} 28599 {\tt expr("1.23e4")} 28600 \end{center} 28601 will return 28602 \begin{center} 28603 {\tt 12300.0} 28604 \end{center} 28605 28606 \item 28607 A string consisting of the digits 0 through 7 which starts 28608 with 0 will be read as an integer base 8. For example, 28609 \begin{center} 28610 {\tt expr("0176")} 28611 \end{center} 28612 will return 28613 \begin{center} 28614 {\tt 126} 28615 \end{center} 28616 since 176 base 8 equals 126 base 10. 28617 28618 \item 28619 A string starting with \texttt{0x} followed by digits 0 through 9 28620 and letters \texttt{a} through \texttt{f} will be read as an integer 28621 base 16. For example, 28622 \begin{center} 28623 {\tt expr("0x2a3f")} 28624 \end{center} 28625 will return 28626 \begin{center} 28627 {\tt 10815} 28628 \end{center} 28629 since \texttt{2a3f} base 16 equals 10815 base 10. 28630 28631 \item 28632 A string starting with \texttt{0b} followed by digits 0 and 1 will 28633 be read as a binary integer. For example, 28634 \begin{center} 28635 {\tt expr("0b1101")} 28636 \end{center} 28637 will return 28638 \begin{center} 28639 {\tt 13} 28640 \end{center} 28641 since 1101 base 2 equals 13 base 10. 28642\end{itemize} 28643 28644\subsection{Converting an expression to a string: \texttt{string}}\index{string} 28645 28646The \texttt{string} command can be used to convert an expression 28647to a string. If you give it an expression as an argument, the 28648expression will be evaluated and then converted to a string. For 28649example, if you enter 28650\begin{center} 28651 {\tt string(ifactor(6))} 28652\end{center} 28653the result will be the string 28654\begin{center} 28655 {\tt "2 * 3"} 28656\end{center} 28657This is the same thing as adding the empty string to the expression; 28658\begin{center} 28659 {\tt ifactor(6) + ""} 28660\end{center} 28661 28662If you want to convert an unevaluated expression to a string, you can 28663quote the expression. If you enter 28664\begin{center} 28665 {\tt string(quote(ifactor(6)))} 28666\end{center} 28667then you will get the string 28668\begin{center} 28669 {\tt "ifactor(6)"} 28670\end{center} 28671 28672\subsection{Converting a real number into a string: \texttt{format}}\index{format} 28673 28674The \texttt{format} command takes two arguments, a real number and a 28675string used for formatting.\\ 28676\texttt{format} returns the real number as a string with the requested 28677formatting. 28678 28679The formatting string can be one of the following: 28680\begin{itemize} 28681 \item \texttt{f} (for \emph{floating} format) followed by the number 28682 of digits to put after the decimal point.\\ 28683 Input: 28684 \begin{center} 28685 \tt 28686 format(sqrt(2)*10\^{}10,"f13") 28687 \end{center} 28688 Output: 28689 \begin{center} 28690 \tt 28691 "14142135623.7308959960938" 28692 \end{center} 28693 \item \texttt{s} (for \emph{scientific} format) followed by the 28694 number of significant digits.\\ 28695 Input: 28696 \begin{center} 28697 \tt 28698 format(sqrt(2)*10\^{}10,"s13") 28699 \end{center} 28700 Output: 28701 \begin{center} 28702 \tt 28703 "14142135623.73" 28704 \end{center} 28705 \item \texttt{e} (for \emph{engineering} format) followed by the 28706 number of digits to put after the decimal point, with one digit 28707 before the decimal points.\\ 28708 Input: 28709 \begin{center} 28710 \tt 28711 format(sqrt(2)*10\^{}10,"e13") 28712 \end{center} 28713 Output: 28714 \begin{center} 28715 \tt 28716 "1.4142135623731e+10" 28717 \end{center} 28718\end{itemize} 28719 28720\subsection{Working with the graphics screen: \texttt{DispG, DispHome, 28721ClrGraph, ClrDraw}} 28722\index{DispG}\index{DispHome}\index{ClrGraph}\index{ClrDraw} 28723 28724Recall that the \texttt{DispG} screen contains the 28725graphical output of \texttt{Xcas}. You can use the \texttt{DispG} 28726command (without parentheses) to bring it up; entering 28727\begin{center} 28728 {\tt DispG;} 28729\end{center} 28730will open the graphics screen. 28731 28732To clear the graphics screen, you can use the 28733\texttt{ClrGraph} command (equivalently, the 28734\texttt{ClrDraw} command). If you enter 28735\begin{center} 28736 {\tt ClrGraph} 28737\end{center} 28738or 28739\begin{center} 28740 {\tt ClrGraph()} 28741\end{center} 28742then the \texttt{DispG} screen will be erased. 28743 28744You can close the \texttt{DispG} screen with the \texttt{DispHome} 28745command; entering 28746\begin{center} 28747 {\tt DispHome;} 28748\end{center} 28749will make the graphics screen go away. 28750 28751\subsection{Pausing a program: \texttt{Pause, WAIT}} 28752\index{Pause}\index{WAIT} 28753 28754The \texttt{Pause} command (written without parentheses) 28755will bring up a Pause informational window and pause \texttt{Xcas} 28756until you click \texttt{Close} in the Pause window. If you enter 28757\texttt{Pause} followed by a number, then \texttt{Xcas} will pause for 28758that number of seconds; entering 28759\begin{center} 28760 {\tt Pause 10} 28761\end{center} 28762will pause \texttt{Xcas} for 10 seconds. 28763 28764The \texttt{WAIT} command will also pause \texttt{Xcas}; 28765it requires an argument in parentheses. To pause \texttt{Xcas} for 10 28766seconds, you can enter 28767\begin{center} 28768 {\tt WAIT(10)} 28769\end{center} 28770 28771\subsection{Dealing with errors: \texttt{try, catch, throw, error, ERROR}} 28772\index{try}\index{catch}\index{throw}\index{error}\index{ERROR} 28773 28774Some commands produce errors, and if your program tries to run such a 28775command it will halt with an error. To avoid this, you can use the 28776\texttt{try} and \texttt{catch} commands. To 28777use these, you put any potentially problematic statements in a block 28778following \texttt{try}, and immediately after the block put 28779\texttt{catch} with an argument of an unused symbol. If the 28780\texttt{try} block doesn't produce an error, then \texttt{catch} and 28781the block following \texttt{catch} will be ignored. If the 28782\texttt{try} block does produce an error, then a string describing the 28783error is assigned to the argument to \texttt{catch}, and the block 28784following catch is evaluated. For example, the command 28785\begin{center} 28786 {\tt [[1,1]]*[[2,2]]} 28787\end{center} 28788will produce an error saying \textit{Error: Invalid dimension}. 28789However, 28790\begin{verbatim} 28791 try {[[1,1]]*[[2,2]]} 28792 catch (err) { 28793 print("The error is " + err) 28794 } 28795\end{verbatim} 28796will not produce an error; instead it will print 28797\begin{center} 28798 {\tt The error is Error: Invalid dimension} 28799\end{center} 28800 28801With the following program 28802\begin{verbatim} 28803 test(x) := { 28804 local y, str, err; 28805 try { y := [[1,1]]*x; str := "This produced a product.";} 28806 catch (err) 28807 {y := x; 28808 str := "This produced an error " + err + " The input is returned.";} 28809 print(str); 28810 return y; 28811 } 28812\end{verbatim} 28813if you enter 28814\begin{center} 28815 {\tt test([[2],[2]])} 28816\end{center} 28817then 28818\begin{center} 28819 {\tt This produced a product.} 28820\end{center} 28821will be printed and the result will be 28822\begin{center} 28823 {\tt [4]} 28824\end{center}. 28825If you enter 28826\begin{center} 28827 {\tt test([[2,2]])} 28828\end{center} 28829then 28830\begin{center} 28831 {\tt This produced an error Error: Invalid dimension The input is 28832 returned.} 28833\end{center} 28834will be printed and the result will be 28835\begin{center} 28836 {\tt [[2,2]]} 28837\end{center} 28838 28839You can use the \texttt{throw} command (or equivalently, 28840the \texttt{error} or \texttt{ERROR} 28841command) to generate an error and error string, possibly to be caught 28842by \texttt{catch}. The \texttt{throw} command takes as argument a 28843string, which will be used as the error message. For example, suppose 28844you have the program 28845\begin{verbatim} 28846 f(x) := { 28847 if (type(x) != DOM_INT) 28848 throw("Not an integer"); 28849 else 28850 return x; 28851 } 28852\end{verbatim} 28853Then 28854\begin{center} 28855 {\tt f(12)} 28856\end{center} 28857will simply return 28858\begin{center} 28859 {\tt 12} 28860\end{center} 28861since 12 is an integer, but 28862\begin{center} 28863 {\tt f(1.2)} 28864\end{center} 28865will signal an error 28866\begin{center} 28867 {\tt Not an integer Error: Bad Argument Value} 28868\end{center} 28869since 1.2 in not an integer. You can catch this error in other 28870programs; the program 28871\begin{verbatim} 28872 g(x) := { 28873 try(f(x)) catch(err) {x := 0;} 28874 return x; 28875 } 28876\end{verbatim} 28877will return \texttt{x} is \texttt{x} is an integer, but if \texttt{x} 28878is not an integer, \texttt{f(x)} will give an error and so 28879\texttt{g(x)} will return 0. 28880 28881\section{Debugging} 28882\label{sec:debug} 28883 28884\subsection{Starting the debugger: 28885\texttt{debug, sst, in, sst\_in, cont, kill, break, breakpoint, 28886halt, rmbrk, rmbreakpoint, watch, rmwtch}} 28887\index{debug}\index{sst}\index{in}\index{sst\_in} 28888\index{cont}\index{kill}\index{break}\index{breakpoint} 28889\index{halt}\index{rmbrk}\index{rmbreakpoint} 28890\index{watch}\index{rmwtch} 28891 28892To start the debugger, you give the \texttt{debug} command an argument 28893of a function and its argument. That will bring up a debug window 28894which contains a pane with the program with the current line highlighted, an 28895\texttt{eval} entry box, a pane with the program including the 28896breakpoints, a row of buttons, and a pane keeping track of 28897the values of variables. By default, the value of all variables in 28898the program are in this pane. The buttons are shortcuts 28899for entering commands in the \texttt{eval} box, but you can enter 28900other commands in the \texttt{eval} box to change the values of 28901variables or to run a command in the context of the program. 28902 28903\paragraph{The \texttt{sst} button} 28904This button will run the \texttt{sst} command, which takes no 28905arguments and runs the highlighted line in the program before moving 28906to the next line. 28907 28908\paragraph{The \texttt{in} button} 28909This button will run the \texttt{sst\_in} command, which takes no 28910argument and runs one step in the program or a user defined 28911function used in the program. 28912 28913\paragraph{The \texttt{cont} button} 28914This button will run the \texttt{cont} command, which takes no 28915arguments and runs the commands from the highlighted line to a breakpoint. 28916 28917\paragraph{The \texttt{kill} button} 28918This button will run the \texttt{kill} command, which exits the debugger. 28919 28920\paragraph{The \texttt{break} button} 28921This button will put the command \texttt{breakpoint} 28922in the \texttt{eval} box, with default 28923arguments of the current program and the current line. It sets a 28924breakpoint at the given line of the given program. 28925Alternatively, if you click on a line in the program in the top pane, 28926you will get the \texttt{breakpoint} command with that program and the 28927line you clicked on. 28928 28929You can set a breakpoint when you write a program with the 28930\texttt{halt()} command. When a program has a 28931\texttt{halt} command, then running the program will bring up the 28932debugger. If you want to debug the program, though, it is still 28933better to use the debug command. Also, you should remove any 28934\texttt{halt} commands when you are done debugging. 28935 28936\paragraph{The \texttt{rmbrk} button} 28937This button will put the command 28938\texttt{rmbreakpoint} in the \texttt{eval} box , with default 28939arguments of the current program and the current line. It removes a 28940breakpoint at the given line of the given program. Alternatively, you 28941can click on the line in the program in the top pane with the bookmark 28942you want to remove. 28943 28944\paragraph{The \texttt{watch} button} 28945This button will put the command \texttt{watch} in the 28946\texttt{eval} box, without the arguments 28947filled in. It takes a list of variables as arguments, and will keep 28948track of the values of these variables in the variable pane. 28949 28950\paragraph{The \texttt{rmwtch} button} 28951This button will put the command \texttt{rmwatch\index{rmwatch}} in 28952the \texttt{eval} box without the arguments filled in. The arguments 28953are the variables you want to remove from the watch list. 28954 28955\chapter{Two-dimensional Graphics} 28956\label{chap:2dgraphics} 28957 28958\section{Introduction} 28959 28960\subsection{Points, vectors and complex numbers} 28961 28962A point in the Cartesian plane is described with an ordered pair 28963$(a,b)$. It has $x$-coordinate (abscissa) $a$ and 28964$y$-coordinate (ordinate) $b$. 28965 28966A vector from one point $(a_{1},b_{1})$ to another $(a_{2},b_{2})$ 28967has associated ordered pair $(a_{2}-a_{1},b_{2}-b_{1})$; so the 28968abscissa is $a_{2}-a_{1}$ and the ordinate is $b_{2}-b_{1}$. 28969 28970A complex number $a + bi$ can be associated with the point $(a,b)$ in 28971the Cartesian plane. The complex number is called the \emph{affix} of the 28972point. 28973 28974A point in \texttt{Xcas} is specified with the 28975\texttt{point} command (see section \ref{sec:2dpoint}), which takes as 28976argument either two real numbers $a,b$ or a complex number $a + bi$. 28977In this chapter, when a command take a point as an argument, the 28978point can either be the result of the \texttt{point} command or 28979simply a complex number. 28980 28981An interactive graphic screen opens whenever a geometric object is 28982drawn, or with the command \texttt{Alt+G}. The objects on the screen 28983can also be created and manipulated with the mouse. 28984 28985As an example (to be explained in more detail later), the 28986\texttt{triangle} command draws a triangle; the result will be a 28987graphics screen containing axes, the triangle and a 28988control panel on the right.\\ 28989Input: 28990\begin{center} 28991 \tt 28992 triangle(0,1,1+i) 28993\end{center} 28994Output: 28995\begin{center} 28996 \includegraphics[width=0.75\textwidth]{xcas-tr.png} 28997\end{center} 28998 28999\section{Basic commands} 29000 29001\subsection{Clear the \texttt{DispG} screen: \texttt{erase}\index{erase}} 29002 29003The \texttt{DispG} screen records all graphic commands 29004since the beginning of the session or the screen was last erased. The 29005\texttt{Alt-D} command (or the menu command \texttt{Cfg 29006$\blacktriangleright$ Show $\blacktriangleright$ DispG}) brings up 29007this screen. 29008 29009The \texttt{erase} command clears the \texttt{DispG} screen without 29010restarting the session. Entering\\ 29011Input: 29012\begin{center} 29013 \tt 29014 erase 29015\end{center} 29016or: 29017\begin{center} 29018 \tt 29019 erase() 29020\end{center} 29021clears the \texttt{DispG} screen. This can be useful for commands 29022such as \texttt{graph2tex}, which only takes into account the objects 29023on the \texttt{DispG} screen. 29024 29025\subsection{Toggle the axes: \texttt{switch\_axes}\index{switch\_axes}} 29026 29027The \texttt{switch\_axes} command shows, hides or toggles the 29028coordinate axes on the graphics screen depending on whether the 29029argument is \texttt{1}, \texttt{0} or empty. 29030(This can also be controlled by a \texttt{show axes} checkbox in the 29031configuration panel brought up with the \texttt{cfg} button on the 29032graphic screen control panel.) 29033 29034Entering\\ 29035Input: 29036\begin{center} 29037 \tt 29038 switch\_axes() 29039\end{center} 29040toggles whether or not the coordinate axes are shown in subsequent 29041graphic screens. Rather than toggling,\\ 29042Input: 29043\begin{center} 29044 \tt 29045 switch\_axes(1) 29046\end{center} 29047causes all later graphic screens to have the axes, and\\ 29048Input: 29049\begin{center} 29050 \tt 29051 switch\_axes(0) 29052\end{center} 29053causes all later graphic screens to omit the axes. 29054 29055When the axes are visible, they have tick marks whose 29056separation is determined by the X-tick and Y-tick values on the 29057graphic configuration screen. Setting these values to 0 removes the 29058axes. 29059 29060\subsection{Draw unit vectors in the plane: 29061\texttt{Ox\_2d\_unit\_vector}\index{Ox\_2d\_unit\_vector} 29062\texttt{Oy\_2d\_unit\_vector}\index{Oy\_2d\_unit\_vector} 29063\texttt{frame\_2d}\index{frame\_2d}} 29064 29065The \texttt{Ox\_2d\_unit\_vector} command draws the unit vector in 29066the $x$-direction on a plane.\\ 29067Input: 29068\begin{center} 29069 \tt 29070 Ox\_2d\_unit\_vector() 29071\end{center} 29072Output: 29073\begin{center} 29074 \includegraphics[width=0.75\textwidth]{xcas-unit-vector.png} 29075\end{center} 29076Similarly, the \texttt{Oy\_2d\_unit\_vector} command 29077draws the unit vector in the $y$ direction. 29078The \texttt{frame\_2d} command simultaneously draws both unit 29079vectors. 29080 29081\subsection{Draw dotted paper: \texttt{dot\_paper}\index{dot\_paper}} 29082 29083The \texttt{dot\_paper} command draws dotted paper. This 29084command takes three mandatory arguments and two optional arguments. 29085The first argument is the spacing in the $x$ direction, the second 29086argument is the angle from the horizontal to draw the dots, and the 29087third argument is the spacing in the $y$ direction. By default, the 29088dots extend in the $x$ and $y$ directions for the distances given in 29089the graphic configuration page accessible from the main menu; the 29090optional fourth and fifth 29091arguments \texttt{x=xmin..xmax} and \texttt{y=ymin..ymax} change the 29092size of the dotted paper.\\ 29093Input: 29094\begin{center} 29095 \tt 29096 dot\_paper(0.6,pi/2,0.6) 29097\end{center} 29098Output: 29099\begin{center} 29100 \includegraphics[width=0.75\textwidth]{xcas-dot-paper.png} 29101\end{center} 29102Unchecking \texttt{Show Axes} on the \texttt{cfg} screen removes 29103the axes. 29104 29105\subsection{Draw lined paper: \texttt{line\_paper}\index{line\_paper}} 29106 29107The \texttt{line\_paper} command draws lined paper. This 29108command takes two mandatory arguments and two optional arguments. 29109The mandatory arguments are 29110the spacing in the $x$ direction and the angle 29111from the horizontal to draw the lines. The optional third and fourth 29112arguments, \texttt{x=xmin..xmax} and \texttt{y=ymin..ymax}, 29113determine the size of the lined paper.\\ 29114Input: 29115\begin{center} 29116 \tt 29117 line\_paper(0.6,pi/3) 29118\end{center} 29119Output: 29120\begin{center} 29121 \includegraphics[width=0.75\textwidth]{xcas-line-paper.png} 29122\end{center} 29123Unchecking \texttt{Show Axes} on the \texttt{cfg} screen removes 29124the axes. 29125 29126\subsection{Draw grid paper: \texttt{grid\_paper}\index{grid\_paper}} 29127 29128The \texttt{grid\_paper} command draws grid paper. This 29129command takes three mandatory arguments and two optional arguments. 29130The mandatory arguments are 29131the spacing in the $x$ direction, the the angle 29132from the horizontal to draw the grid, and the spacing 29133in the $y$ direction. The optional fourth and fifth arguments, 29134\texttt{x=xmin..xmax} and \texttt{y=ymin..ymax}, restrict the 29135size of the grid.\\ 29136Input: 29137\begin{center} 29138 \tt 29139 grid\_paper(1,pi/2,1) 29140\end{center} 29141Output: 29142\begin{center} 29143 \includegraphics[width=0.75\textwidth]{xcas-grid-paper.png} 29144\end{center} 29145Unchecking \texttt{Show Axes} on the \texttt{cfg} screen removes 29146the axes. 29147 29148\subsection{Draw triangular paper: \texttt{triangle\_paper}} 29149\index{triangle\_paper} 29150 29151The \texttt{triangle\_paper} command draws triangular paper. This 29152command takes three mandatory and two optional arguments. The 29153mandatory arguments are 29154the spacing in the $x$ direction, the angle 29155from the horizontal, and the spacing 29156in the $y$ direction. The optional fourth and fifth arguments, 29157\texttt{x=xmin..xmax} and \texttt{y=ymin..ymax}, restrict the 29158size of the grid.\\ 29159Input: 29160\begin{center} 29161 \tt 29162 triangle\_paper(1,pi/2,1) 29163\end{center} 29164Output: 29165\begin{center} 29166 \includegraphics[width=0.75\textwidth]{xcas-triangle-paper.png} 29167\end{center} 29168Unchecking \texttt{Show Axes} on the \texttt{cfg} screen removes the 29169axes. 29170 29171\section{Display features of graphics} 29172\label{sec:dispfeat} 29173 29174\subsection{Graphic features} 29175 29176Graphic objects and graphic screens can have features, such as 29177labels and colors, that are only included when requested, and other 29178features, such as line width, which are configurable. Some features 29179will be global, meaning that they will apply to the entire graphic 29180screen, and some will be local, meaning that they will only apply to 29181individual objects. 29182 29183\subsection{Parameters for changing features} 29184 29185Graphical features are changed by giving appropriate values to 29186certain parameters. Several values can be given at once with an 29187expression of the form \texttt{\textit{feature} = value1 + value2 + \dots}. 29188Some values can be set using optional arguments to graphic commands, 29189which will set the feature locally; namely, it will only apply to that 29190particular graphic object. Some values can be specified at the 29191beginning of a line, which will set the feature globally; it will 29192apply to all the graphic objects created on that line. For some 29193features, both options are available. 29194 29195\subsubsection{Parameters for local features} 29196\index{thickness@\textit{thickness}} 29197\index{nstep@\textit{nstep}} 29198\index{tstep@\textit{tstep}} 29199\index{ustep@\textit{ustep}} 29200\index{vstep@\textit{vstep}} 29201\index{xstep@\textit{xstep}} 29202\index{ystep@\textit{ystep}} 29203\index{zstep@\textit{zstep}} 29204\index{frames@\textit{frames}} 29205\index{gl\_texture@\textit{gl\_texture}} 29206\label{sec:localfeatures} 29207 29208Commands which create graphic objects, such as \texttt{triangle}, can 29209have optional arguments to change a features of the object. 29210For example, the argument \texttt{color = red} will make an object red.\\ 29211Input: 29212\begin{center} 29213 \tt 29214 triangle(0,1,1+i,color=red) 29215\end{center} 29216Output: 29217\begin{center} 29218 \includegraphics[width=0.75\textwidth]{xcas-red-triangle.png} 29219\end{center} 29220 29221The features and their possible values are: 29222\begin{description} 29223 \item[\texttt{display} or \texttt{color}] These two parameter names 29224 have the same effect. They control the following features. 29225 \begin{description} 29226 \item[Color] The following values will change the color: 29227 \begin{itemize} 29228 \item An integer from \texttt{0} to \texttt{381}.\\ 29229 Integers from 0 to 255 correspond to the color palette, integers from 29230 256 to 381 will be the spectrum of colors.\\ 29231 The program 29232\begin{verbatim} 29233 rainbow() := { 29234 local j, C; 29235 C := []; 29236 for (j := 256; j < 382; j++) { 29237 C := append(C,square(j,j+1,color=j+filled)); 29238 } 29239 } 29240\end{verbatim} 29241will show the colors;\\ 29242Input: 29243\begin{center} 29244 \tt 29245 rainbow(); 29246\end{center} 29247Output: 29248\begin{center} 29249 \includegraphics[width=0.75\textwidth]{xcas-rainbow.png} 29250\end{center} 29251The number of a color is its $x$-coordinate. To see just 29252one color, say the color corresponding to $n$ for $256 \le n \le 381$, 29253enter\\ 29254Input: 29255\begin{center} 29256 \tt 29257 rainbow()[$n$-256] 29258\end{center} 29259 29260 \item The names 29261 \texttt{black}, \texttt{white}, \texttt{red}, \texttt{blue}, 29262 \texttt{green}, \texttt{magenta}, \texttt{cyan} or \texttt{yellow}. 29263 \end{itemize} 29264 29265 \item[Fill] The \texttt{filled} value creates a solid object.\\ 29266 Input: 29267 \begin{center} 29268 \tt 29269 triangle(0,1,1+i,display=filled) 29270 \end{center} 29271 Output: 29272 \begin{center} 29273 \includegraphics[width=0.75\textwidth]{xcas-filledtriangle.png} 29274 \end{center} 29275 \item[Point markers] By default, points are drawn 29276 with a small cross. The following (self-explanatory) values 29277 change the marker. 29278 \begin{description} 29279 \item[\texttt{rhombus\_point}] 29280 \item[\texttt{square\_point}] 29281 \item[\texttt{cross\_point}] 29282 \item[\texttt{star\_point}] 29283 \item[\texttt{plus\_point}] 29284 \item[\texttt{point\_point}] 29285 \item[\texttt{triangle\_point}] 29286 \item[\texttt{invisible\_point}] 29287 \end{description} 29288 29289 \item[Point width] The values 29290 \texttt{point\_width\_1},\dots,\texttt{point\_width\_8} 29291 change the thickness of the lines in the point markers. 29292 29293 \item[Line style] The following (self-explanatory) values change 29294 the style of lines. 29295 \begin{description} 29296 \item[\texttt{solid\_line}] 29297 \item[\texttt{dash\_line}] 29298 \item[\texttt{dashdot\_line}] 29299 \item[\texttt{dashdotdot\_line}] 29300 \item[\texttt{cap\_flat\_line}] 29301 \item[\texttt{cap\_round\_line}] 29302 \item[\texttt{cap\_square\_line}] 29303 \end{description} 29304 29305 \item[Line widths] The values 29306 \texttt{line\_width\_1},\dots,\texttt{line\_width\_8} 29307 change the thickness of the lines. 29308 \end{description} 29309 29310 \item[\texttt{thickness}] This controls line thickness, it can be 29311 an integer from 1 to 7. 29312 \item[\texttt{nstep}] This sets the number of sampling points 29313 for three-dimensional objects. 29314 \item[\texttt{tstep}] This sets the step size of the 29315 parameter when drawing a one parameter parametric plot. 29316 \item[\texttt{ustep}] This sets the step size of the 29317 first parameter when drawing a two-parameter parametric plot. 29318 \item[\texttt{vstep}] This sets the step size of the 29319 second parameter when drawing a two-parameter parametric plot. 29320 \item[\texttt{xstep}] This sets the step size of the 29321 $x$ variable. 29322 \item[\texttt{ystep}] This sets the step size of the 29323 $y$ variable. 29324 \item[\texttt{zstep}] This sets the step size of the 29325 $z$ variable. 29326 \item[\texttt{frames}] This sets the number of 29327 graphs computed when an animated graph is created with the 29328 \texttt{animate} or \texttt{animate3d} command. 29329 \item[\texttt{legend}] This adds a legend to a graphic object and 29330 should be a string. It is probably most useful when 29331 that object is a point or a polygon. If the object is a polygon, 29332 the legend will be placed in the middle of the last side. 29333 Other parameters for the graphic object will specify the color or 29334 position of the legend. 29335 % \item[\texttt{style}] This only takes the value \texttt{point}. 29336 % If you set it to point, then lines will be drawn as dotted lines. 29337 \item[\texttt{gl\_texture}] This sets an 29338 image file to be put on the graphic object; it should be the name 29339 of the file. 29340\end{description} 29341 29342\subsubsection{Parameters for global features} 29343\label{sec:globpar} 29344 29345Parameters set at the beginning of a line change features on the 29346entire graphic screen. It only takes effect 29347when the line ends with a graphic command. 29348For example, starting the line with \texttt{title=\textit{title 29349string}} will give the graphic screen a title.\\ 29350Input: 29351\begin{center} 29352 \tt 29353 title = "Some triangles"; triangle(0,1,1+i); triangle(2,3,3+i); 29354\end{center} 29355Output: 29356\begin{center} 29357 \includegraphics[width=0.75\textwidth]{xcas-some-triangles.png} 29358\end{center} 29359 29360The parameters for global features and their possible values are: 29361\begin{description} 29362 \item[\texttt{axes}] 29363 This determines whether axes are shown or hidden; a value of 29364 \texttt{0} or \texttt{false} hides the axes, a value of 29365 \texttt{1} or \texttt{true} shows the axes. 29366 \item[\texttt{labels}] This sets labels for the axes; it should be 29367 a list of two strings \texttt{["\textit{x axis label}","\textit{y 29368 axis label}"]}. 29369 \item[\texttt{label}] 29370 This puts labels on the graphic screen in the following ways. 29371 \begin{itemize} 29372 \item 29373 To set the units on the axes, it can be a list of two or three strings, 29374 \texttt{["\textit{x units}","\textit{y units}"]} or 29375 \texttt{["\textit{x units}","\textit{y units}","\texttt{z 29376 units}"]}. 29377 \item 29378 To place a string at a particular point, it can 29379 be a list of two integers followed by a 29380 string. The integers determine the point, 29381 starting from \texttt{[0,0]} in the top left of the 29382 screen. 29383 \end{itemize} 29384 \item[\texttt{title}] This sets the title for the graphic window, 29385 it should be a string. 29386 \item[\texttt{gl\_texture}] This sets the wallpaper of the graphic 29387 window to be an image file, it should be the name of the file. 29388 \item[\texttt{gl\_x\_axis\_name},\texttt{gl\_x\_axis\_name},\texttt{gl\_x\_axis\_name}] 29389 These set the names of the axes. 29390 \item[\texttt{gl\_x\_axis\_unit},\texttt{gl\_x\_axis\_unit},\texttt{gl\_x\_axis\_unit}] 29391 These set the units of the axes. 29392 \item[\texttt{gl\_x\_axis\_color},\texttt{gl\_x\_axis\_color},\texttt{gl\_x\_axis\_color}] 29393 These set the colors of the axes 29394 labels; they take the same color options as the local parameter 29395 \texttt{color}. 29396 \item[\texttt{gl\_ortho}] This ensures that the graph is 29397 orthonormal when it is set to \texttt{1}. 29398 \item[\texttt{gl\_x},\texttt{gl\_y},\texttt{gl\_z}] 29399 These define the framing of the graph; they should be intervals 29400 \textit{min}\texttt{..}\textit{max}. (They are not compatible with 29401 interactive graphs.) 29402 \item[\texttt{gl\_xtick},\texttt{gl\_ytick},\texttt{gl\_ztick}] 29403 These determine the spacing of the ticks on the axes. 29404 \item[\texttt{gl\_shownames}] This shows or hides object names, it 29405 can be \texttt{true} or \texttt{false}. 29406 \item[\texttt{gl\_rotation}] This sets the axis of rotation for 29407 three-dimensional scene animations; it should be a direction vector 29408 \texttt{[$x$,$y$,$z$]}. 29409 \item[\texttt{gl\_quaternion}] This sets the quaternion for viewing 29410 three-dimensional scenes; it should be a fourtuple 29411 \texttt{[$x$,$y$,$z$,$t$]}. (This is not compatible with 29412 interactive graphs.) 29413\end{description} 29414 29415\subsection{Commands for global display features} 29416 29417\subsubsection{Add a legend: \texttt{legend}} 29418\index{legend} 29419\label{sec:addlegend} 29420 29421The \texttt{legend} command takes two arguments and an optional third. The 29422arguments are: 29423\begin{itemize} 29424 \item A position to put the legend. This can either be a point or a 29425 list of two integers giving the number of pixels from the upper left 29426 hand corner. 29427 \item The legend itself, a string or a variable. 29428 \item An optional third parameter indicating where to put the legend 29429 relative to the point. By default, it will be to the upper right of 29430 the point (\texttt{quadrant1}), but you can specify 29431 \texttt{quadrant1}, \texttt{quadrant2}, \texttt{quadrant3} or 29432 \texttt{quadrant4}. 29433\end{itemize} 29434For example, to put "hello" to the upper left of the point 29435$(1,1)$:\\ 29436Input: 29437\begin{center} 29438 \tt 29439 legend(1+i,"hello",quadrant3) 29440\end{center} 29441or\\ 29442Input: 29443\begin{center} 29444 \tt 29445 legend(1+i,quadrant3,"hello") 29446\end{center} 29447Output: 29448\begin{center} 29449 \tt 29450 \includegraphics[width=0.75\textwidth]{xcas-hello-legend.png} 29451\end{center} 29452 29453%To put a legend at an angle, see section \ref{sec:2dangle}. 29454 29455\subsubsection{Change various features:\texttt{display, color}} 29456\index{display}\index{color} 29457 29458The \texttt{display} command changes the properties of 29459graphics; the same properties that can also be changed with the \texttt{display} and 29460\texttt{color} parameters. (See section \ref{sec:localfeatures}.) 29461The \texttt{color} command is 29462the same as the \texttt{display} command. 29463 29464The \texttt{display} command can draw objects with specified 29465properties. In this case, the first argument will be a command to 29466create the object and the second argument will be a value for the 29467\texttt{display} or \texttt{color} parameter. 29468Both\\ 29469Input: 29470\begin{center} 29471 \tt 29472 display(triangle(0,1,1+i),red) 29473\end{center} 29474and\\ 29475Input: 29476\begin{center} 29477 \tt 29478 triangle(0,1,1+i,display=red) 29479\end{center} 29480draw\\ 29481Output: 29482\begin{center} 29483 \tt 29484 \includegraphics[width=.75\textwidth]{xcas-red-triangle.png} 29485\end{center} 29486Similarly, both\\ 29487Input: 29488\begin{center} 29489 \tt 29490 triangle(0,1,1+i,display=filled) 29491\end{center} 29492and\\ 29493Input: 29494\begin{center} 29495 \tt 29496 display(triangle(0,1,1+i),filled) 29497\end{center} 29498draw\\ 29499Output: 29500\begin{center} 29501 \tt 29502 \includegraphics[width=0.75\textwidth]{xcas-trianglefilled.png} 29503\end{center} 29504The \texttt{display} command can also take a second argument of 29505\texttt{hidden\_name}. By default, if a geometric object is named, 29506the drawing is labeled.\\ 29507Input: 29508\begin{center} 29509 \tt 29510 A := triangle(0,1,1+i) 29511\end{center} 29512Output: 29513\begin{center} 29514 \tt 29515 \includegraphics[width=0.75\textwidth]{xcas-triangleA.png} 29516\end{center} 29517Creating the object with the \texttt{display} command and the 29518\texttt{hidden\_name} argument will draw it without the label.\\ 29519Input: 29520\begin{center} 29521 \tt 29522 display(A := triangle(0,1,1+i),hidden\_name) 29523\end{center} 29524Output: 29525\begin{center} 29526 \tt 29527 \includegraphics[width=0.75\textwidth]{xcas-triangleNoA.png} 29528\end{center} 29529 29530The \texttt{display} command can also be used without drawing an 29531object, such as \texttt{display(hidden\_name)} or 29532\texttt{display(filled)}. In this case it will be a 29533global command; the display effect will apply to all objects 29534afterwards. Entering \texttt{display(0)} will reset the display 29535parameters; afterwards, for example, the colors will be black, the 29536figures won't be filled, and the objects will have labels. 29537 29538\section{Define geometric objects without drawing them: \texttt{nodisp}\index{nodisp}} 29539 29540The \texttt{nodisp} command will define an object without displaying 29541it. Setting a variable to a graphic object draws the object.\\ 29542Input: 29543\begin{center} 29544 \tt 29545 C := point(1+i) 29546\end{center} 29547will define the point \texttt{C} as well as draw it. Setting a 29548variable to a graphic object inside the \texttt{nodisp} command will 29549not draw the object.\\ 29550Input: 29551\begin{center} 29552 \tt 29553 nodisp(C:= point(1+i)) 29554\end{center} 29555will define the point $C$ but not display it. This is 29556equivalent to following the command with \texttt{:;}, 29557\\ 29558Input: 29559\begin{center} 29560 \tt 29561 C := point(1+i):; 29562\end{center} 29563 29564To define a point as above and display it without the 29565label, enter the point's name;\\ 29566Input: 29567\begin{center} 29568 \tt 29569 C 29570\end{center} 29571Alternatively, define the point within an 29572\texttt{eval} statement;\\ 29573Input: 29574\begin{center} 29575 \tt 29576 eval(C := point(1+i)) 29577\end{center} 29578defines $C$ as the point, displays the point, but doesn't display the 29579label. To later display the point with a label, use the 29580\texttt{legend} command.\\ 29581Input: 29582\begin{center} 29583 \tt 29584 legend(C,"C") 29585\end{center} 29586or: 29587\begin{center} 29588 \tt 29589 point(affix(C),legend="C") 29590\end{center} 29591Output: 29592\begin{center} 29593 \tt 29594 \includegraphics[width=0.75\textwidth]{xcas-legendC.png} 29595\end{center} 29596In this case, the string "C" can be replaced with any other string 29597as a label. Alternatively, redefine the 29598variable as itself;\\ 29599Input: 29600\begin{center} 29601 \tt 29602 C := C 29603\end{center} 29604prints $C$ with its label. 29605 29606\section{Geometric demonstrations: \texttt{assume}\index{assume}} 29607 29608Variables should be unspecified to demonstrate a general geometric 29609result, but need to have specific values when drawing. There are a 29610couple of different approaches to deal with this. 29611 29612One approach is to use the \texttt{assume} command. If a variable is 29613\emph{assumed} to have a value, then that value will be used in 29614graphics but the variable will still be unspecified for calculations. 29615For example,\\ 29616Input: 29617\begin{center} 29618 \tt 29619 assume(a = 2.1) 29620\end{center} 29621then 29622\\ 29623Input: 29624\begin{center} 29625 \tt 29626 A := point(a + i) 29627\end{center} 29628will draw a point at the coordinate $(2.1,1)$,\\ 29629Output: 29630\begin{center} 29631 \tt 29632 \includegraphics[width=0.75\textwidth]{xcas-assumea.png} 29633\end{center} 29634but the variable $a$ will still be treated as a variable in 29635calculations;\\ 29636Input: 29637\begin{center} 29638 \tt 29639 distance(0,A) 29640\end{center} 29641Output: 29642\begin{center} 29643 \tt 29644 sqrt((-a)\^{}2 + 1) 29645\end{center} 29646 29647Another approach would be to use \texttt{point} or \texttt{pointer} 29648mode in a geometry screen. If there isn't a geometry screen showing, 29649the command \texttt{Alt-G} or the \texttt{Geo$\blacktriangleright$New 29650figure 2d} menu will open a screen. Clicking on the 29651\texttt{Mode} button right above the graphic screen and choosing 29652\texttt{pointer} or \texttt{point} will put the screen in 29653\texttt{pointer} or \texttt{point} mode. 29654If a point is defined and displayed, such as 29655with \texttt{A := point(2.1 + i)}, then clicking on the name of the 29656point (\texttt{A} in this case) with the right mouse button will bring 29657up a configuration screen. As long as there is a point defined with 29658non-symbolic values, there will be a \texttt{symb} box on the configuration 29659screen. Selecting the \texttt{symb} box and 29660choosing \texttt{OK} will be equivalent to the commands 29661\texttt{assume(Ax=[2.1,-8.16901408451,8.16901408451])} 29662and \texttt{assume(Ay = [1, -5.0, 5.0]}, this will bring up 29663two lines beneath the arrows to the right of the screen which can be 29664used to change the assumed values of \texttt{Ax} and \texttt{Ay}. 29665Also, the point \texttt{A} will be redefined as \texttt{point(Ax,Ay)}. 29666\begin{center} 29667 \tt 29668 \includegraphics[width=0.75\textwidth]{xcas-assumespoint.png} 29669\end{center} 29670 29671\section{Points in the plane} 29672 29673\subsection{Points and complex numbers} 29674 29675The \emph{affix} of a point $(a,b)$ in the plane is the complex number 29676$a + bi$. In this section, when a command take points as arguments, 29677the points can be specified by a pair or by a complex number. 29678 29679\subsection{The point in the plane: \texttt{point}} 29680\index{point} 29681\label{sec:2dpoint} 29682 29683See section \ref{sec:3dpoint} for points in space. 29684 29685In the 2-d geometry screen in point mode, clicking on a point with the 29686left mouse button will choose that point. Points chosen this way are 29687automatically named, first with \texttt{A}, then \texttt{B}, etc. 29688 29689Alternatively, the \texttt{point} command chooses a point, where the 29690point $(a,b)$ is specified by either the two coordinates \text{a,b}, a 29691list \texttt{[a,b]} of the coordinates, or the affix \texttt{a + b*i}.\\ 29692Input: 29693\begin{center} 29694 \tt 29695 A := point(2,1) 29696\end{center} 29697or: 29698\begin{center} 29699 \tt 29700 A := point([2,1]) 29701\end{center} 29702or\\ 29703Input: 29704\begin{center} 29705 \tt 29706 A := point(2 + i) 29707\end{center} 29708Output: 29709\begin{center} 29710 \tt 29711 \includegraphics[width=0.75\textwidth]{xcas-2dpoint.png} 29712\end{center} 29713(The marker used to indicate the point can be changed; see section 29714\ref{sec:localfeatures}.) 29715 29716If the \texttt{point} command has two numbers for arguments, at least 29717one of which is complex but not real, then it will choose two points. 29718Entering\\ 29719Input: 29720\begin{center} 29721 \tt 29722 A := point(1,2*i) 29723\end{center} 29724or: 29725\begin{center} 29726 \tt 29727 A := point([1,2*i]) 29728\end{center} 29729will choose two points named \texttt{A}; one with affix $1$ and 29730one with affix \texttt{2i}. 29731 29732\subsection{The difference and sum of two points in the 29733plane:\texttt{+, -}} 29734\index{+}\index{-} 29735 29736Let \texttt{A} and \texttt{B} be two points in the plane, with affixes 29737$a_1 + i a_2$ and $b_1 + ib_2$ respectively.\\ 29738Input: 29739\begin{center} 29740 \tt 29741 A:= point(1 + 2*i); B := point(3+4*i) 29742\end{center} 29743\begin{itemize} 29744 \item The difference \texttt{B-A} returns the affix 29745 $(b_1-a_1)+i(b_2-a_2)$, which represents the vector $AB$.\\ 29746Input: 29747\begin{center} 29748 \tt 29749 vector(A,B); vector(point(0),point(B-A)) 29750\end{center} 29751Output: 29752\begin{center} 29753 \includegraphics[width=0.75\textwidth]{xcas-subpts.png} 29754\end{center} 29755\item The sum \texttt{B+A} returns the affix 29756 $(b_1+a_1)+i(b_2+a_2)$. If \texttt{D := point(B+A)}, then 29757 \texttt{BD = OA}.\\ 29758Input: 29759\begin{center} 29760 \tt 29761 D := point(B + A);\\ 29762 segment(B,D); segment(point(0),A) 29763\end{center} 29764Output: 29765\begin{center} 29766 \includegraphics[width=0.75\textwidth]{xcas-addpts.png} 29767\end{center} 29768\end{itemize} 29769 29770Note that \texttt{-A} is the point symmetric to \texttt{A} with 29771respect to the origin. 29772 29773The sum of three points \texttt{A + B + C} can 29774be viewed as the translate of \texttt{C} by the vector \texttt{A + B}. 29775So if \texttt{A} or \texttt{B} contains parameters, you should write 29776this as \texttt{C + (A + B)} or \texttt{evalc(A + B) + C}. 29777 29778\subsection{Define random points in the plane: \texttt{point2d}\index{point2d}} 29779 29780The \texttt{point2d} command defines a random point whose 29781coordinates are integers between -5 and 5. This command takes a name 29782as an argument and assigns the point to the name. For example,\\ 29783Input: 29784\begin{center} 29785 \tt 29786 point2d(A) 29787\end{center} 29788assigns \texttt{A} to a random point. Once assigned, the point 29789is fixed. The command can also take a sequence of names, and 29790will assign separate random points to each name. For example, to 29791generate a random triangle, first generate three random points\\ 29792Input: 29793\begin{center} 29794 \tt 29795 point2d(A,B,C) 29796\end{center} 29797and then use them for a triangle\\ 29798Input: 29799\begin{center} 29800 \tt 29801 triangle(A,B,C) 29802\end{center} 29803 29804\subsection{Points in polar coordinates: 29805\texttt{polar\_point}\index{polar\_point}, \texttt{point\_polar}\index{point\_polar}} 29806 29807To specify a point in polar coordinates, enter the polar 29808representation of complex numbers. For example, the command\\ 29809Input: 29810\begin{center} 29811 \tt 29812 point(2*exp(i * pi/4)) 29813\end{center} 29814draws the point with polar coordinates $r=2$, $\theta = \pi/4$. 29815The \texttt{polar\_point} command does this in an easier way, it 29816takes $r$ and $\theta$ as arguments. 29817The command\\ 29818Input: 29819\begin{center} 29820 \tt 29821 polar\_point(2,pi/4) 29822\end{center} 29823creates the same point as before. 29824 29825\subsection{Find a point of intersection of two objects in the plane: \texttt{single\_inter}\index{single\_inter} \texttt{line\_inter}\index{line\_inter}} 29826\label{sec:single2dintersection} 29827 29828See section \ref{sec:3dsingleintersection} for single points of intersection of objects 29829in space. 29830 29831The \texttt{single\_inter} (or the \texttt{line\_inter}) command takes 29832two geometric objects as arguments 29833and returns one of the points of intersection of the two objects. 29834 29835The \texttt{single\_inter} command optionally takes a third 29836argument, which can be a point or a list of points. If the optional 29837argument is 29838a single point, then \texttt{single\_inter} returns the point of 29839intersection \emph{closest} to the optional argument. If the optional 29840argument is a list of points, then \texttt{single\_inter} tries to 29841return a point of intersection not in the list. 29842 29843For example, the unit circle \texttt{circle(0,1)} and line 29844\texttt{line(-1,i)} intersect at the points 29845$(-1,0)$ and $(0,1)$. Entering\\ 29846Input: 29847\begin{center} 29848 \tt 29849 single\_inter(circle(0,1),line(-1,i)) 29850\end{center} 29851draws the point $(-1,0)$. To draw the other 29852point, enter\\ 29853Input: 29854\begin{center} 29855 \tt 29856 single\_inter(circle(0,1),line(-1,i),[-1]) 29857\end{center} 29858and \texttt{Xcas} will draw $(0,1)$. Similarly, since this second 29859point of intersection is closest to $(0,1/2)$, entering\\ 29860Input: 29861\begin{center} 29862 \tt 29863 single\_inter(circle(0,1),line(-1,i),i/2) 29864\end{center} 29865also draws the second point. 29866 29867\subsection{Find the points of intersection of two geometric objects 29868in the plane: \texttt{inter}\index{inter}} 29869\label{sec:2dintersection} 29870 29871See section \ref{sec:3dintersection} for points of intersection of objects 29872in space. 29873 29874The \texttt{inter} command takes 29875two geometric objects as arguments 29876and returns a list of the points of intersection of the two objects. 29877 29878The \texttt{inter} command optionally takes a point as a third argument. 29879In this case, \texttt{inter} returns the point of intersection 29880\emph{closest} to this argument. 29881 29882For example, entering\\ 29883Input: 29884\begin{center} 29885 \tt 29886 inter(circle(0,1),line(1,i)) 29887\end{center} 29888draws the points at $(1,0)$ and $(0,1)$. 29889To get just one of the points, use the usual list indices.\\ 29890Input: 29891\begin{center} 29892 \tt 29893 inter(circle(0,1),line(1,i))[0] 29894\end{center} 29895draws just one of the points. 29896To get the point closest to $(0,1/2)$, enter\\ 29897Input: 29898\begin{center} 29899 \tt 29900 inter(circle(0,1),line(1,i),i/2) 29901\end{center} 29902 29903\subsection{Find the orthocenter of a triangle in the plane: \texttt{orthocenter}\index{orthocenter}} 29904 29905The \texttt{orthocenter} command take a triangle (or three 29906points representing the vertices of a triangle) as argument and returns the 29907orthocenter of the triangle. Entering\\ 29908Input: 29909\begin{center} 29910 \tt 29911 orthocenter(triangle(0,1+i,-1+i)) 29912\end{center} 29913or\\ 29914Input: 29915\begin{center} 29916 \tt 29917 orthocenter(0,1+i,-1+i) 29918\end{center} 29919draws the point $(0,0)$, the orthocenter of the 29920triangle. 29921 29922\subsection{Find the midpoint of a segment in the plane: \texttt{midpoint}\index{midpoint}} 29923\label{sec:2dmidpt} 29924 29925See section \ref{sec:3dmidpt} for midpoints in space. 29926 29927The \texttt{midpoint} command takes two points (or a list of two 29928points) as arguments and draws the midpoint of the segment 29929determined by these points. 29930 29931\subsection{The barycenter in the plane: \texttt{barycenter}\index{barycenter}} 29932\label{sec:2dbarycenter} 29933 29934See section \ref{sec:3dbarycenter} for barycenters of objects in 29935space. 29936 29937The \texttt{barycenter} command draws the barycenter of a set of 29938weighted points. The points and their weights (real numbers) can be 29939given in three different ways. 29940\begin{enumerate} 29941 \item A sequence of lists of length two.\\ 29942 The first element of each list is a point and the second element is 29943 the weight of the point. 29944 \item A matrix with two columns.\\ 29945 The first column of the matrix contains the points and the 29946 second column contains the corresponding weights. 29947 \item A matrix with two rows and more than two columns.\\ 29948 The first row contains the points and the second row the 29949 corresponding weights. 29950\end{enumerate} 29951 29952For example, the following commands will draw the barycenter of the 29953points $(1,1)$ with weight $1$, $(1,-1)$ with weight $1$ and $(1,4)$ 29954with weight $2$.\\ 29955Input: 29956\begin{center} 29957 \tt 29958 barycenter([1 + i,1],[1 - i,1],[1 + 4*i, 2]) 29959\end{center} 29960or: 29961\begin{center} 29962 \tt 29963 barycenter([[1 + i,1],[1 - i,1],[1 + 4*i, 2]]) 29964\end{center} 29965or: 29966\begin{center} 29967 \tt 29968 barycenter([[1 + i, 1 - i, 1 + 4*i],[1,1,2]]) 29969\end{center} 29970Output: 29971\begin{center} 29972 \tt 29973 \includegraphics[width=0.75\textwidth]{xcas-barycenter.png} 29974\end{center} 29975 29976\subsection{The isobarycenter of $n$ points in the plane: \texttt{isobarycenter}\index{isobarycenter}} 29977\label{sec:2disobarycenter} 29978 29979See section \ref{sec:3disobarycenter} for isobarycenters of objects in 29980space. 29981 29982The \texttt{isobarycenter} command takes a list (or sequence) of 29983points and draws the isobarycenter, which is the barycenter when all 29984the points are equally weighted. 29985 29986\subsection{The center of a circle in the plane: \texttt{center}\index{center}} 29987 29988The \texttt{center} command takes as argument a circle and returns and draws the 29989center.\\ 29990Input: 29991\begin{center} 29992 \tt 29993 C := center(circle(point(1+i),1)) 29994\end{center} 29995Output: 29996\begin{center} 29997 \includegraphics[width=0.75\textwidth]{xcas-center.png} 29998\end{center} 29999 30000\subsection{The vertices of a polygon in the plane: 30001\texttt{vertices}\index{vertices}, \texttt{vertices\_abc}\index{vertices\_abc}} 30002 30003The \texttt{vertices} command (or \texttt{vertices\_abc}) takes as argument a 30004polygon. 30005 30006\texttt{vertices} returns a list of the vertices of the polygon and 30007draws them.\\ 30008Input: 30009\begin{center} 30010 \tt 30011 vertices(equilateral\_triangle(0,2)) 30012\end{center} 30013Output: 30014\begin{center} 30015 \includegraphics[width=0.75\textwidth]{xcas-vertices.png} 30016\end{center} 30017Input: 30018\begin{center} 30019 \tt 30020 C := vertices(equilateral\_triangle(0,2))[2] 30021\end{center} 30022Output: 30023\begin{center} 30024 \includegraphics[width=0.75\textwidth]{xcas-vertices2.png} 30025\end{center} 30026 30027\subsection{The vertices of a polygon in the plane, closed: \texttt{vertices\_abca}\index{vertices\_abca}} 30028 30029The \texttt{vertices\_abca} command takes as argument a polygon. 30030 30031\texttt{vertices\_abca} returns the ``closed'' list of vertices (it 30032repeats the beginning vertex) and draws them. 30033% Input: 30034% \begin{center} 30035% \tt 30036% vertices\_abca(equilateral\_triangle(0,2)) 30037% \end{center} 30038% Output: 30039% \begin{center} 30040% \includegraphics[width=0.75\textwidth]{xcas-vertices_abca.png} 30041% \end{center} 30042 30043\subsection{A point on a geometric object in the plane: \texttt{element}\index{element}} 30044\label{sec:element} 30045 30046The \texttt{element} command is most useful in a two-dimensional 30047geometry screen; it creates objects that are 30048restricted to a geometric figure. 30049 30050\texttt{element} takes different types of arguments: 30051\begin{itemize} 30052 \item An interval $a..b$ and an optional initial value (by default 30053 $(a+b)/2$) and step size (by default $(b-a)/100$). 30054 30055 This creates a parameter restricted to the interval, with the 30056 given initial value and whose value can be changed in the given step 30057 sizes. 30058 30059 For example, the command \texttt{t := element(0..pi)} creates a 30060 parameter \texttt{t} which can take on values between $0$ and $\pi$ 30061 and has initial value $\pi/2$. It also creates a slider labeled 30062 \texttt{t} which can be used to change the values. The values of 30063 any later formulas involving \texttt{t} will change with \texttt{t}. 30064 30065 \item A curve and an optional initial value (by default $1/2$). 30066 30067 This creates a point which will be restricted to the curve, 30068 the initial position of the point is determined by setting the 30069 parameter (in the default parameterization of the object) to the 30070 initial value. If the point can be moved by the mouse (as it can 30071 when the geometry screen is in \texttt{Pointer} mode), then the 30072 motion will be restricted to the geometric object. 30073 30074 For example, the command \texttt{A := element(circle(0,2))} 30075 creates a point labeled \texttt{A} whose position is restricted to 30076 the circle of radius $2$ centered at the origin. Since the circle 30077 has default parameterization $2\exp(i t)$, \texttt{A} starts out at 30078 $2\exp(i/2)$. 30079 30080 \item A polygon or polygonal line \texttt{PL} with $n$ sides and 30081 \texttt{[floor($t$),frac($t$)]}, where $t$ is a variable previously 30082 defined by \texttt{$t$ = element(0..$n$)}. 30083 30084 This creates a point restricted to the polygonal line. With the 30085 sides numbered starting at $0$, the value of \texttt{floor($t$)} 30086 determines which side the point is on, and the value of 30087 \texttt{frac($t$)} determines how far along the side the point is. 30088\end{itemize} 30089 30090If a point $A$ (corresponding to the complex number $a$) is defined as 30091an element of a curve $C$ and $B$ is a point (corresponding to the 30092complex number $b$), then $A + B$ will be a point on $C$; it will be 30093the projection onto $C$ of the point corresponding to $a + b$. 30094 30095Note that in this case, if $B'$ is another point, then $A + (B - B')$ 30096isn't the same as $A + B - B'$. The expression $A + (B - B')$ is 30097interpreted as adding the point $A$, defined as a point on $C$, to the 30098point $B - B'$, and so the sum will be on $C$. The expression $A + B 30099- B'$ is interpreted as $(A + B) - B'$, and so the point $B'$ is not 30100being added to a point defined as an element of the curve $C$, and so 30101this sum may not be on $C$. 30102 30103\section{Lines in plane geometry} 30104 30105\subsection{Lines and directed lines in the plane: \texttt{line}\index{line}} 30106\label{sec:2dlines} 30107 30108See section \ref{sec:3dlines} for lines in space. 30109 30110The \texttt{line} command returns and draws a directed line given one of the 30111following types of arguments: 30112\begin{itemize} 30113 \item Two points or a list of two points.\\ 30114 The direction of the line is from the first point 30115 to the second point. 30116 \item A point and a slope given by \texttt{slope=\textit{value}}.\\ 30117 The direction of the line is determined by the slope. 30118 \item A point and direction vector (in the form 30119 \texttt{[$u_1$,$u_2$]}).\\ 30120 The direction of the line is given by the direction vector. 30121 \item An equation of the form \texttt{a*x+b*y+c=0}.\\ 30122 The direction of the line is given by \texttt{[b,-a]}. 30123\end{itemize} 30124Input: 30125\begin{center} 30126 \tt 30127 line(0,1+i) 30128\end{center} 30129or: 30130\begin{center} 30131 \tt 30132 line(1+i,slope=1) 30133\end{center} 30134or: 30135\begin{center} 30136 \tt 30137 line(1+i,[3,3]) 30138\end{center} 30139or: 30140\begin{center} 30141 \tt 30142 line(y - x = 0) 30143\end{center} 30144Output: 30145\begin{center} 30146 \includegraphics[width=0.75\textwidth]{xcas-line.png} 30147\end{center} 30148 30149\textbf{Warning:} To draw a line with an additional argument for color 30150(such as \texttt{color = blue}), this argument must be the third 30151argument. In particular, for a list of two points to 30152specify a line in this command, the list must be turned into a sequence, such as 30153with \texttt{op}. For example, given a list \texttt{L} of two 30154points (possibly the result of a different command) which determines a 30155line, to draw the line \texttt{blue} enter 30156\texttt{line(op(L),color=blue)}; entering 30157\texttt{line(L,color=blue)} results in an error. 30158 30159\subsection{Half-lines in the plane: \texttt{half\_line}} 30160\index{half\_line} 30161\label{sec:2dhalflines} 30162 30163See section \ref{sec:3dhalflines} for half-lines in space. 30164 30165The \texttt{half\_line} command takes as argument two points or a list of two 30166points. 30167 30168\texttt{half\_line} returns and draws the ray from the first point 30169through the second.\\ 30170Input: 30171\begin{center} 30172 \tt 30173 half\_line(0,-1+i) 30174\end{center} 30175Output: 30176\begin{center} 30177 \includegraphics[width=0.75\textwidth]{xcas-halfline.png} 30178\end{center} 30179 30180\subsection{Line segments in the plane: \texttt{segment}\index{segment} \texttt{Line}\index{Line}} 30181\label{sec:2dsegments} 30182 30183See section \ref{sec:3dsegments} for segments in space. 30184 30185The \texttt{segment} command and the \texttt{Line} command draw line 30186segments. (The \texttt{segment} command can also draw vectors, see 30187section \ref{sec:2dvectors}.) 30188 30189\texttt{segment} takes as argument two points or a list of two points. 30190 30191\texttt{segment} returns the corresponding line segment and draws it. 30192 30193\texttt{Line} takes as argument four real numbers, the first two 30194represent the coordinates of the beginning of the segment and the last 30195two represent the end. 30196 30197\texttt{Line} returns the line segment and draws it. 30198 30199\noindent 30200\textbf{Example.}\\ 30201Input: 30202\begin{center} 30203 \tt 30204 segment(-1,1+i) 30205\end{center} 30206or: 30207\begin{center} 30208 \tt 30209 segment(point(-1),point(1,1)) 30210\end{center} 30211or: 30212\begin{center} 30213 \tt 30214 Line(-1,0,1,1) 30215\end{center} 30216Output: 30217\begin{center} 30218 \includegraphics[width=0.75\textwidth]{xcas-segment.png} 30219\end{center} 30220 30221\subsection{Vectors in the plane: \texttt{segment}\index{segment} \texttt{vector}\index{vector}} 30222\label{sec:2dvectors} 30223 30224See section \ref{sec:3dvectors} for vectors in space. 30225 30226The \texttt{segment} and \texttt{vector} commands return and draw vectors. 30227(The \texttt{segment} command can also draw line segments, see section 30228\ref{sec:2dsegments}.) 30229 30230\texttt{segment} takes as arguments a point and a vector. 30231The point indicates the beginning of the result and the vector (given 30232as a list of coordinates) the direction. 30233 30234\texttt{segment} returns and draws the corresponding vector as a line 30235segment. If the arguments are \texttt{P} and \texttt{V}, then command 30236draws the segment from \texttt{P} to \texttt{P+V}.\\ 30237Input: 30238\begin{center} 30239 \tt 30240 segment([-1,0],[1,1]) 30241\end{center} 30242Output: 30243\begin{center} 30244 \includegraphics[width=0.75\textwidth]{xcas-segvec.png} 30245\end{center} 30246 30247\texttt{vector} takes as arguments two points (or a list with two 30248points) or a point and a vector. 30249 30250\texttt{vector} returns and draws the corresponding vector. If the 30251arguments are two points, the vector goes from the first 30252to the second point; if the arguments are a point and a vector, then 30253the vector starts at the given point.\\ 30254Input: 30255\begin{center} 30256 \tt 30257 vector([-1,0],[1,i]) 30258\end{center} 30259or: 30260\begin{center} 30261 \tt 30262 vector(-1,i) 30263\end{center} 30264or: 30265\begin{center} 30266 \tt 30267 V := vector(1,2+i):; vector(-1,V) 30268\end{center} 30269Output: 30270\begin{center} 30271 \includegraphics[width=0.75\textwidth]{xcas-vect.png} 30272\end{center} 30273 30274\subsection{Parallel lines in the plane: \texttt{parallel}\index{parallel}} 30275\label{sec:2dparallel} 30276 30277See section \ref{sec:3dparallel} for parallel lines in space. 30278 30279The \texttt{parallel} command takes as argument a point and a line. 30280 30281\texttt{parallel} returns and draws the line parallel to the given 30282line passing through the given point.\\ 30283Input: 30284\begin{center} 30285 \tt 30286 parallel(0,line(1,i)) 30287\end{center} 30288Output: 30289\begin{center} 30290 \includegraphics[width=0.75\textwidth]{xcas-parallel.png} 30291\end{center} 30292 30293\subsection{Perpendicular lines in the plane: \texttt{perpendicular}\index{perpendicular}} 30294\label{sec:2dperp} 30295 30296See section \ref{sec:3dperp} for perpendicular lines in space. 30297 30298The \texttt{perpendicular} command takes as arguments either a point and a line, 30299or three points (the last two points determining a line). 30300 30301\texttt{perpendicular} returns and draws the line perpendicular to the 30302given line passing through the given point.\\ 30303Input: 30304\begin{center} 30305 \tt 30306 perpendicular(0,line(1,i)) 30307\end{center} 30308or: 30309\begin{center} 30310 \tt 30311 perpendicular(0,1,i) 30312\end{center} 30313Output: 30314\begin{center} 30315 \includegraphics[width=0.75\textwidth]{xcas-perp.png} 30316\end{center} 30317 30318\subsection{Tangents to curves in the plane: \texttt{tangent}\index{tangent}} 30319\label{sec:2dtangents} 30320 30321See section \ref{sec:3dtangents} for tangents in space. 30322 30323The \texttt{tangent} command takes as arguments either a curve and a point, or a 30324point defined with \texttt{element} (see section \ref{sec:element}) using a curve and 30325parameter value. 30326 30327\texttt{tangent} returns and draws the list of lines tangent to the 30328curve passing through the given point.\\ 30329Input: 30330\begin{center} 30331 \tt 30332 tangent(circle(0,1),1+i) 30333\end{center} 30334Output: 30335\begin{center} 30336 \includegraphics[width=0.75\textwidth]{xcas-tangent1.png} 30337\end{center} 30338Input: 30339\begin{center} 30340 \tt 30341 t := element(0..pi,pi/4):; A := element(circle(0,1),t):; tangent(A) 30342\end{center} 30343Output: 30344\begin{center} 30345 \includegraphics[width=0.75\textwidth]{xcas-tangent2.png} 30346\end{center} 30347 30348When \texttt{tangent} is called with an element, 30349the tangent will change along with the point on the element. 30350 30351\subsection{The median of a triangle in the plane: \texttt{median\_line}\index{median\_line}} 30352 30353The \texttt{median\_line} command takes as argument three points representing the 30354vertices of a triangle. 30355 30356\texttt{median\_line} returns and draws the median line, through the 30357point given by the first argument and bisecting the segment determined 30358by the other two arguments.\\ 30359Input: 30360\begin{center} 30361 \tt 30362 median\_line(0,1,i) 30363\end{center} 30364Output: 30365\begin{center} 30366 \includegraphics[width=0.75\textwidth]{xcas-median.png} 30367\end{center} 30368 30369\subsection{The altitude of a triangle: \texttt{altitude}\index{altitude}} 30370 30371The \texttt{altitude} command takes as argument three points representing the 30372vertices of a triangle. 30373 30374\texttt{altitude} returns and draws the altitude line, through the 30375point given by the first argument and perpendicular to the line determined 30376by the other two arguments.\\ 30377Input: 30378\begin{center} 30379 \tt 30380 altitude(0,1,i) 30381\end{center} 30382Output: 30383\begin{center} 30384 \includegraphics[width=0.75\textwidth]{xcas-altitude.png} 30385\end{center} 30386 30387\subsection{The perpendicular bisector of a segment in the plane: \texttt{perpen\_bisector}\index{perpen\_bisector}} 30388\label{sec:2dperpbis} 30389 30390See section \ref{sec:3dperpbis} for perpendicular bisectors in space. 30391 30392The \texttt{perpen\_bisector} command takes as argument a line segment or two 30393points representing the end points of a line segment. 30394 30395\texttt{perpen\_bisector} returns and draws the perpendicular bisector 30396of the segment.\\ 30397Input: 30398\begin{center} 30399 \tt 30400 perpen\_bisector(1,i) 30401\end{center} 30402or: 30403\begin{center} 30404 \tt 30405 perpen\_bisector(segment(1,i)) 30406\end{center} 30407Output: 30408\begin{center} 30409 \includegraphics[width=0.75\textwidth]{xcas-perpbisect.png} 30410\end{center} 30411 30412The \texttt{perpen\_bisector} command can also take two lines as segments, in 30413which case it returns and draws the perpendicular bisector of the 30414segment from the first point defining the first line and the second 30415point defining the second line. 30416 30417\subsection{The angle bisector: \texttt{bisector}\index{bisector}} 30418 30419The \texttt{bisector} command takes as argument three points or a list of three 30420points. The first point represents the vertex of an angle; the 30421remaining two points will be on the two sides of the angle. 30422 30423\texttt{bisector} returns and draws the angle bisector.\\ 30424Input: 30425\begin{center} 30426 \tt 30427 bisector(0,1,i) 30428\end{center} 30429Output: 30430\begin{center} 30431 \includegraphics[width=0.75\textwidth]{xcas-bisector.png} 30432\end{center} 30433 30434\subsection{The exterior angle bisector: \texttt{exbisector}} 30435\index{exbisector} 30436 30437The \texttt{exbisector} command takes as argument three points or a list of three 30438points. 30439 30440\texttt{exbisector} returns the bisector of the exterior angle of the 30441triangle determined by the points; the first point is the vertex of 30442the angle, the opposite of the first and second points determine one 30443side of the angle and the first and third determine the second side.\\ 30444Input: 30445\begin{center} 30446 \tt 30447 exbisector(0,1,i) 30448\end{center} 30449Output: 30450\begin{center} 30451 \includegraphics[width=0.75\textwidth]{xcas-exbisector.png} 30452\end{center} 30453 30454\section{Triangles in the plane} 30455 30456See section \ref{sec:3dtriangles} for triangles in space. 30457 30458\subsection{Arbitrary triangles in the plane: \texttt{triangle}\index{triangle}} 30459\label{sec:2dtricommand} 30460 30461See section \ref{sec:3dtricommand} for the \texttt{triangle} command 30462in space. 30463 30464The \texttt{triangle} command takes as arguments three points or a list of three 30465points. 30466 30467\texttt{triangle} returns and draws the triangle with the given 30468vertices.\\ 30469Input: 30470\begin{center} 30471 \tt 30472 triangle(-1,i,1+i) 30473\end{center} 30474Output: 30475\begin{center} 30476 \includegraphics[width=0.75\textwidth]{xcas-triangle.png} 30477\end{center} 30478 30479\subsection{Isosceles triangles in the plane: \texttt{isosceles\_triangle}\index{isosceles\_triangle}} 30480\label{sec:2disotriangles} 30481 30482See section \ref{sec:3disotriangles} for isosceles triangles in space. 30483 30484The \texttt{isosceles\_triangle} command takes three arguments and an optional 30485fourth. The three mandatory arguments are two points \texttt{A} 30486and \texttt{B} and an angle $\theta$. 30487 30488\texttt{isosceles\_triangle} returns and draws the isosceles triangle 30489\texttt{ABC}, where \texttt{AB} and \texttt{AC} are equal sides and 30490$\theta$ is the angle from \texttt{AB} to \texttt{AC}.\\ 30491Input: 30492\begin{center} 30493 \tt 30494 isosceles\_triangle(i, 1, -3*pi/4) 30495\end{center} 30496Output: 30497\begin{center} 30498 \includegraphics[width=0.75\textwidth]{xcas-isotriangle.png} 30499\end{center} 30500The optional argument needs to be a variable name, 30501which is assigned to vertex \texttt{C}.\\ 30502Input: 30503\begin{center} 30504 \tt 30505 isosceles\_triangle(i, 1, -3*pi/4,C) 30506\end{center} 30507Output: 30508\begin{center} 30509 \tt 30510 \includegraphics[width=0.75\textwidth]{xcas-isotriangleC.png} 30511\end{center} 30512Input: 30513\begin{center} 30514 \tt 30515 normal(affix(C)) 30516\end{center} 30517Output: 30518\begin{center} 30519 \tt 30520 -sqrt(2) + i 30521\end{center} 30522 30523\subsection{Right triangles in the plane: \texttt{right\_triangle}\index{right\_triangle}} 30524\label{sec:2dright} 30525 30526See section \ref{sec:3dright} for right triangles in space. 30527 30528The \texttt{right\_triangle} command takes three arguments and an optional fourth. 30529The three mandatory arguments are two points \texttt{A} and 30530\texttt{B} and a real nonzero number \texttt{k}. 30531 30532\texttt{right\_triangle} returns and draws the right triangle 30533\texttt{ABC}, with the right angle at \texttt{A} and with 30534$\texttt{AB} = |k|\cdot\texttt{AC}$. If $\texttt{k} > 0$, then 30535\texttt{AB} to \texttt{AC} is counterclockwise; if $\texttt{k} < 0$ 30536then \texttt{AB} to \texttt{AC} is clockwise.\\ 30537Input: 30538\begin{center} 30539 \tt 30540 right\_triangle(i,-i,2) 30541\end{center} 30542Output: 30543\begin{center} 30544 \includegraphics[width=0.75\textwidth]{xcas-rttriangle.png} 30545\end{center} 30546Input: 30547\begin{center} 30548 \tt 30549 right\_triangle(i,-i,-2) 30550\end{center} 30551Output: 30552\begin{center} 30553 \includegraphics[width=0.75\textwidth]{xcas-rttriangle2.png} 30554\end{center} 30555The optional argument needs to be a variable name 30556which is assigned to vertex \texttt{C}.\\ 30557Input: 30558\begin{center} 30559 \tt 30560 right\_triangle(i, -i, 2, C) 30561\end{center} 30562Output: 30563\begin{center} 30564 \tt 30565 \includegraphics[width=0.75\textwidth]{xcas-rttriangleC.png} 30566\end{center} 30567Input: 30568\begin{center} 30569 \tt 30570 affix(C) 30571\end{center} 30572Output: 30573\begin{center} 30574 \tt 30575 4 + i 30576\end{center} 30577 30578\subsection{Equilateral triangles in the plane: \texttt{equilateral\_triangle}\index{equilateral\_triangle}} 30579\label{sec:2dequi} 30580 30581See section \ref{sec:3dequi} for equilateral triangles in space. 30582 30583The \texttt{equilateral\_triangle} command takes two arguments and an optional 30584third. The two mandatory arguments are points \texttt{A} and 30585\texttt{B}. 30586 30587\texttt{equilateral\_triangle} returns and draws the equilateral triangle 30588\texttt{ABC}, where \texttt{AB} to \texttt{AC} is counterclockwise.\\ 30589Input: 30590\begin{center} 30591 \tt 30592 equilateral\_triangle(0,2) 30593\end{center} 30594Output: 30595\begin{center} 30596 \includegraphics[width=0.75\textwidth]{xcas-equitriangle.png} 30597\end{center} 30598The optional argument needs to be a variable name 30599which is assigned to vertex \texttt{C}.\\ 30600Input: 30601\begin{center} 30602 \tt 30603 equilateral\_triangle(0, 2, C) 30604\end{center} 30605Output: 30606\begin{center} 30607 \tt 30608 \includegraphics[width=0.75\textwidth]{xcas-equitriangleC.png} 30609\end{center} 30610Input: 30611\begin{center} 30612 \tt 30613 affix(C) 30614\end{center} 30615Output: 30616\begin{center} 30617 \tt 30618 i*sqrt(3) + 1 30619\end{center} 30620 30621\section{Quadrilaterals in the plane} 30622\label{sec:2dquad} 30623 30624See section \ref{sec:3dquad} for quadrilaterals in space. 30625 30626\subsection{Squares in the plane: \texttt{square}\index{square}} 30627\label{sec:2dsquares} 30628 30629See section \ref{sec:3dsquares} for squares in space. 30630 30631The \texttt{square} command takes two mandatory arguments and one or 30632two optional arguments. The mandatory arguments are points \texttt{A} and 30633\texttt{B}. 30634 30635\texttt{square} returns and draws the square \texttt{ABCD}, where the 30636square is traversed counterclockwise.\\ 30637Input: 30638\begin{center} 30639 \tt 30640 square(0,1+i) 30641\end{center} 30642Output: 30643\begin{center} 30644 \includegraphics[width=0.75\textwidth]{xcas-square.png} 30645\end{center} 30646The optional third fourth arguments need to be 30647variable names, which will be assigned to vertex \texttt{C} (and 30648\texttt{D}).\\ 30649Input: 30650\begin{center} 30651 \tt 30652 square(0,1+i,C,D) 30653\end{center} 30654Output: 30655\begin{center} 30656 \tt 30657 \includegraphics[width=0.75\textwidth]{xcas-squareCD.png} 30658\end{center} 30659Input: 30660\begin{center} 30661 \tt 30662 affix(C), affix(D) 30663\end{center} 30664Output: 30665\begin{center} 30666 \tt 30667 2*i, -1 + i 30668\end{center} 30669 30670\subsection{Rhombuses in the plane: \texttt{rhombus}\index{rhombus}} 30671\label{sec:2drhombuses} 30672 30673See section \ref{sec:3drhombuses} for rhombuses in space. 30674 30675The \texttt{rhombus} command takes three mandatory arguments and one 30676or two optional arguments. The three mandatory arguments are two 30677points \texttt{A} and \texttt{B} and a real number \texttt{a}. 30678 30679\texttt{rhombus} returns and draws the rhombus \texttt{ABCD}, where 30680\texttt{a} is the counterclockwise angle from \texttt{AB} to 30681\texttt{AC}.\\ 30682Input: 30683\begin{center} 30684 \tt 30685 rhombus(-2*i, sqrt(3) - i, pi/3) 30686\end{center} 30687Output: 30688\begin{center} 30689 \includegraphics[width=0.75\textwidth]{xcas-rhombus.png} 30690\end{center} 30691The optional fourth and fifth arguments need to be 30692variable names which will be assigned to vertices \texttt{C} and 30693\texttt{D}.\\ 30694Input: 30695\begin{center} 30696 \tt 30697 rhombus(-2*i, sqrt(3) - i, pi/3, C, D) 30698\end{center} 30699Output: 30700\begin{center} 30701 \tt 30702 \includegraphics[width=0.75\textwidth]{xcas-rhombusCD.png} 30703\end{center} 30704Input: 30705\begin{center} 30706 \tt 30707 affix(C), affix(D) 30708\end{center} 30709Output: 30710\begin{center} 30711 \tt 30712 sqrt(3) + i, 0 30713\end{center} 30714 30715\subsection{Rectangles in the plane: \texttt{rectangle}\index{rectangle}} 30716\label{sec:2drect} 30717 30718See section \ref{sec:3drect} for rectangles in space. 30719 30720The \texttt{rectangle} command takes three mandatory arguments and one 30721or two optional arguments. The mandatory arguments are two points 30722\texttt{A} and \texttt{B} and a nonzero real number \texttt{k}. 30723 30724\texttt{rectangle} returns and draws the rectangle \texttt{ABCD}, where 30725$\texttt{AD}=|\texttt{k}|\cdot\texttt{AB}$ and the angle from 30726\texttt{AB} to \texttt{AD} is counterclockwise if $\texttt{k} > 0$, 30727clockwise if $\texttt{k}<0$.\\ 30728Input: 30729\begin{center} 30730 \tt 30731 rectangle(0, 1+i, 1/2) 30732\end{center} 30733Output: 30734\begin{center} 30735 \includegraphics[width=0.75\textwidth]{xcas-rectangle.png} 30736\end{center} 30737Input: 30738\begin{center} 30739 \tt 30740 rectangle(0, 1+i, -1/2) 30741\end{center} 30742Output: 30743\begin{center} 30744 \includegraphics[width=0.75\textwidth]{xcas-rectangle2.png} 30745\end{center} 30746The optional fourth and fifth arguments need to be 30747variable names which will be assigned to vertices \texttt{C} and 30748\texttt{D}.\\ 30749Input: 30750\begin{center} 30751 \tt 30752 rectangle(0, 1+i, -1/2, C, D) 30753\end{center} 30754Output: 30755\begin{center} 30756 \tt 30757 \includegraphics[width=0.75\textwidth]{xcas-rectangleCD.png} 30758\end{center} 30759Input: 30760\begin{center} 30761 \tt 30762 affix(C), affix(D) 30763\end{center} 30764Output: 30765\begin{center} 30766 \tt 30767 (3 + i)/2, (1 - i)/2 30768\end{center} 30769 30770Given \texttt{rectangle(A,B,k)}, \texttt{Xcas} computes \texttt{D} 30771by $\texttt{affix(D}) = \texttt{affix(A)} + 30772\texttt{k}\exp(i\pi/2)(\texttt{affix(B)}-\texttt{affix(A)})$. If 30773\texttt{k} is complex, then \texttt{rectangle} draws a 30774parallelogram.\\ 30775Input: 30776\begin{center} 30777 \tt 30778 rectangle(0,1,1+i) 30779\end{center} 30780Output: 30781\begin{center} 30782 \includegraphics[width=0.75\textwidth]{xcas-rectangle3.png} 30783\end{center} 30784 30785\subsection{Parallelograms in the plane: \texttt{parallelogram}\index{parallelogram}} 30786\label{sec:2dparallelograms} 30787 30788See section \ref{sec:3dparallelograms} for parallelograms in space. 30789 30790The \texttt{parallelogram} command takes three mandatory arguments and 30791one optional argument. The mandatory arguments are three points \texttt{A}, 30792\texttt{B} and \texttt{C}. 30793 30794\texttt{parallelogram} returns and draws the parallelogram 30795\texttt{ABCD} for the appropriate \texttt{D}.\\ 30796Input: 30797\begin{center} 30798 \tt 30799 parallelogram(0, 1, 2 + i) 30800\end{center} 30801Output: 30802\begin{center} 30803 \includegraphics[width=0.75\textwidth]{xcas-parallelogram.png} 30804\end{center} 30805The fourth optional argument will need to be 30806a variable name which will be assigned to vertex \texttt{D}.\\ 30807Input: 30808\begin{center} 30809 \tt 30810 parallelogram(0, 1, 2 + i, D) 30811\end{center} 30812Output: 30813\begin{center} 30814 \tt 30815 \includegraphics[width=0.75\textwidth]{xcas-parallelogramD.png} 30816\end{center} 30817Input: 30818\begin{center} 30819 \tt 30820 affix(D) 30821\end{center} 30822Output: 30823\begin{center} 30824 \tt 30825 1 + i 30826\end{center} 30827 30828\subsection{Arbitrary quadrilaterals in the plane: 30829\texttt{quadrilateral}\index{quadrilateral}} 30830\label{sec:2dquads} 30831 30832%See section \ref{sec:3dquads} for quadrilaterals in space. 30833 30834The \texttt{quadrilateral} command takes four arguments, points 30835\texttt{A}, \texttt{B}, \texttt{C} and \texttt{D}. 30836 30837\texttt{quadrilateral} returns and draws the quadrilateral 30838\texttt{ABCD}.\\ 30839Input: 30840\begin{center} 30841 \tt 30842 quadrilateral(0, 1, 1 + i, -1 + 2*i) 30843\end{center} 30844Output: 30845\begin{center} 30846 \includegraphics[width=0.75\textwidth]{xcas-quadrilateral.png} 30847\end{center} 30848 30849\section{Other polygons in the plane} 30850\label{sec:2dpolygons} 30851 30852See section \ref{sec:3dpolygons} for polygons in space. 30853 30854\subsection{Regular hexagons in the plane: \texttt{hexagon}\index{hexagon}} 30855\label{sec:2dhex} 30856 30857See section \ref{sec:3dhex} for hexagons in space. 30858 30859The \texttt{hexagon} command takes two mandatory arguments and up to 30860four optional arguments. 30861The two mandatory arguments points \texttt{A} and 30862\texttt{B}. 30863 30864\texttt{hexagon} returns and draws the regular hexagon 30865\texttt{ABCDEF}, where the vertices are counterclockwise.\\ 30866Input: 30867\begin{center} 30868 \tt 30869 hexagon(0,1) 30870\end{center} 30871Output: 30872\begin{center} 30873 \includegraphics[width=0.75\textwidth]{xcas-hexagon.png} 30874\end{center} 30875The optional arguments will need to be 30876variable names, which will be assigned in order to vertices \texttt{C} 30877through \texttt{F}.\\ 30878Input: 30879\begin{center} 30880 \tt 30881 hexagon(0, 1, C, D, E, F) 30882\end{center} 30883Output: 30884\begin{center} 30885 \tt 30886 \includegraphics[width=0.75\textwidth]{xcas-hexagonCDEF.png} 30887\end{center} 30888Input: 30889\begin{center} 30890 \tt 30891 affix(C), affix(D), affix(E), affix(F) 30892\end{center} 30893Output: 30894\begin{center} 30895 \tt 30896 3/2 + i*sqrt(3)/2, 1 + i*sqrt(3), i*sqrt(3), -1/2 + i*sqrt(3)/2 30897\end{center} 30898 30899\subsection{Regular polygons in the plane: \texttt{isopolygon}\index{isopolygon}} 30900\label{sec:2dregpoly} 30901 30902See section \ref{sec:3dregpoly} for regular polygons in space. 30903 30904The \texttt{isopolygon} command takes three arguments; two points \texttt{A} and 30905\texttt{B} and a non-zero integer \texttt{k}. 30906 30907\texttt{isopolygon} returns and draws the regular $|\texttt{k}|$-sided 30908polygon with one side \texttt{AB}. If $\texttt{k} > 0$, then the 30909polygon will continue counterclockwise; if $\texttt{k} < 0$, then it 30910will be clockwise.\\ 30911Input: 30912\begin{center} 30913 \tt 30914 isopolygon(0,1,4) 30915\end{center} 30916Output: 30917\begin{center} 30918 \includegraphics[width=0.75\textwidth]{xcas-isopolygon.png} 30919\end{center} 30920Input: 30921\begin{center} 30922 \tt 30923 isopolygon(0,1,-4) 30924\end{center} 30925Output: 30926\begin{center} 30927 \includegraphics[width=0.75\textwidth]{xcas-isopolygon2.png} 30928\end{center} 30929 30930\subsection{General polygons in the plane: \texttt{polygon}\index{polygon}} 30931\label{sec:2dgenpolygon} 30932 30933See section \ref{sec:3dgenpolygon} for general polygons in space. 30934 30935The \texttt{polygon} command takes as argument a sequence or list of points. 30936 30937\texttt{polygon} returns and draws the polygon with the given vertices.\\ 30938Input: 30939\begin{center} 30940 \tt 30941 polygon(-1,-1+i/2,i,1+i,-i) 30942\end{center} 30943Output: 30944\begin{center} 30945 \includegraphics[width=0.75\textwidth]{xcas-polygon.png} 30946\end{center} 30947Input: 30948\begin{center} 30949 \tt 30950 polygon(makelist(x->exp(i*pi*x/3),0,5,1)) 30951\end{center} 30952Output: 30953\begin{center} 30954 \includegraphics[width=0.75\textwidth]{xcas-polygon2.png} 30955\end{center} 30956 30957\subsection{Polygonal lines in the plane: \texttt{open\_polygon}\index{open\_polygon}} 30958\label{sec:2dpolylines} 30959 30960See section \ref{sec:3dpolylines} for polygonal lines in space. 30961 30962The \texttt{open\_polygon} command takes as argument a sequence or list of points. 30963 30964\texttt{open\_polygon} returns and draws the polygon line with the given vertices.\\ 30965Input: 30966\begin{center} 30967 \tt 30968 open\_polygon(-1,-1+i/2,i,1+i,-i) 30969\end{center} 30970Output: 30971\begin{center} 30972 \includegraphics[width=0.75\textwidth]{xcas-opolygon.png} 30973\end{center} 30974Input: 30975\begin{center} 30976 \tt 30977 open\_polygon(makelist(x->exp(i*pi*x/3),0,5,1)) 30978\end{center} 30979Output: 30980\begin{center} 30981 \includegraphics[width=0.75\textwidth]{xcas-opolygon2.png} 30982\end{center} 30983 30984\subsection{Convex hulls: \texttt{convexhull}\index{convexhull}} 30985 30986The \texttt{convexhull} command uses the Graham scanning algorithm to 30987find the convex hull of a set of points. 30988 30989\texttt{convexhull} takes as argument a list of points. 30990 30991\texttt{convexhull} returns the vertices of the convex hull of the 30992points.\\ 30993Input: 30994\begin{center} 30995 \tt 30996 convexhull(0,1,1+i,1+2i,-1-i,1-3i,-2+i) 30997\end{center} 30998Output: 30999\begin{center} 31000 \tt 31001 (1-3*i,1+2*i,-2+i,-1-i) 31002\end{center} 31003To draw the hull, use the \texttt{polygon} command with the output of 31004\texttt{convexhull}.\\ 31005Input: 31006\begin{center} 31007 \tt 31008 polygon(convexhull(0,1,1+i,1+2i,-1-i,1-3i,-2+i)) 31009\end{center} 31010Output: 31011\begin{center} 31012 \includegraphics[width=0.75\textwidth]{xcas-chull.png} 31013\end{center} 31014 31015\section{Circles} 31016 31017\subsection{Circles and arcs in the plane: \texttt{circle}\index{circle}} 31018\label{sec:2dcircles} 31019See also section \ref{sec:2darcs}. 31020 31021See section \ref{sec:3dcircles} for circles in space. 31022 31023The \texttt{circle} command returns and draws a circle or arc of a circle, 31024depending on the arguments. 31025\texttt{circle} can take the following arguments: 31026\begin{itemize} 31027 \item One argument, the equation of a circle with variables $x$ and 31028 $y$ (or an expression assumed to be set to 0).\\ 31029 \texttt{circle} returns and draws the circle. \\ 31030 Input: 31031 \begin{center} 31032 \tt 31033 circle(x\^2 + y\^2 - 2*x - 2*y) 31034 \end{center} 31035 Output: 31036 \begin{center} 31037 \includegraphics[width=0.75\textwidth]{xcas-circle.png} 31038 \end{center} 31039 \item Two arguments, a point and a complex number.\\ 31040 \texttt{circle} returns and draws the circle centered at the 31041 point and whose radius is the modulus of the complex number.\\ 31042 Input: 31043 \begin{center} 31044 \tt 31045 circle(-1,i) 31046 \end{center} 31047 Output: 31048 \begin{center} 31049 \includegraphics[width=0.75\textwidth]{xcas-circle2.png} 31050 \end{center} 31051 \item Two arguments, both points (where the second is the value of 31052 \texttt{point} and not simply the affix).\\ 31053 \texttt{circle} returns and draws the circle with a diameter given 31054 by the two points.\\ 31055 Input: 31056 \begin{center} 31057 \tt 31058 circle(-1,point(i)) 31059 \end{center} 31060 Output: 31061 \begin{center} 31062 \includegraphics[width=0.75\textwidth]{xcas-circle3.png} 31063 \end{center} 31064 \item Four mandatory arguments and two optional arguments. 31065 The mandatory arguments are a point \texttt{C}, a 31066 complex number \texttt{r}, and two real numbers. The 31067 arguments \texttt{C} and \texttt{r} determine a circle, as above. 31068 The two real numbers specify the central angles 31069 that determine an arc, where the angles start on the axis defined by 31070 the points \texttt{C} and \texttt{C + r}.\\ 31071 \texttt{circle} returns and draws the arc.\\ 31072 Input: 31073 \begin{center} 31074 \tt 31075 circle(-1,1,0,pi/4) 31076 \end{center} 31077 Output: 31078 \begin{center} 31079 \includegraphics[width=0.75\textwidth]{xcas-circle4.png} 31080 \end{center} 31081 The optional arguments need to be 31082 variable names which will be assigned to the ends of the arc. 31083 \item Four mandatory arguments and two optional arguments. 31084 The mandatory arguments are two points \texttt{A} 31085 and \texttt{B} and two real numbers. The 31086 points \texttt{A} and \texttt{B} determine a circle, as above. 31087 The two real numbers specify the central angles 31088 that determine an arc, where the angles start on the axis defined by 31089 the diameter \texttt{AB}.\\ 31090 \texttt{circle} returns and draws the arc.\\ 31091 Input: 31092 \begin{center} 31093 \tt 31094 circle(-1,point(i),0,pi/4) 31095 \end{center} 31096 Output: 31097 \begin{center} 31098 \includegraphics[width=0.75\textwidth]{xcas-circle5.png} 31099 \end{center} 31100 The optional arguments need to be 31101 variable names which will be assigned to the ends of the arc. 31102\end{itemize} 31103 31104\subsection{Circular arcs: \texttt{arc}\index{arc}} 31105\label{sec:2darcs} 31106 31107See also section \ref{sec:2dcircles} 31108 31109The \texttt{arc} command takes three mandatory arguments and one or two optional 31110arguments. The mandatory arguments are two points \texttt{A} 31111and \texttt{B} and a real number \texttt{a} between $-2\pi$ and $2\pi$. 31112 31113\texttt{arc} returns and draws the circular arc from \texttt{A} to 31114\texttt{B} that represents and angle of \texttt{a}. 31115(Note that the center of the circle will be 31116\texttt{(A + B)/2 + i*(B - A)/(2*tan(a/2))}.)\\ 31117Input: 31118\begin{center} 31119 \tt 31120 arc(1,i,pi/2) 31121\end{center} 31122Output: 31123\begin{center} 31124 \includegraphics[width=0.75\textwidth]{xcas-arc.png} 31125\end{center} 31126Input: 31127\begin{center} 31128 \tt 31129 arc(1,i,-pi/2) 31130\end{center} 31131Output: 31132\begin{center} 31133 \includegraphics[width=0.75\textwidth]{xcas-arc2.png} 31134\end{center} 31135The optional arguments need to be 31136variable names which will be assigned to the center and 31137the radius of the circle. 31138 31139\subsection{Circles (TI compatibility): \texttt{Circle}\index{Circle}} 31140 31141The \texttt{Circle} command takes three mandatory arguments and an optional argument. The 31142first two arguments are the $x$ and $y$ coordinates of the center and 31143the third is the radius. 31144 31145\texttt{Circle} returns and draws the circle.\\ 31146Input: 31147\begin{center} 31148 \tt 31149 Circle(-1,0,2) 31150\end{center} 31151Output: 31152\begin{center} 31153 \includegraphics[width=0.75\textwidth]{xcas-bigCircle.png} 31154\end{center} 31155The optional fourth argument is either \texttt{0} or \texttt{1}. If 31156it is \texttt{1}, then \texttt{Circle} will draw the circle; this is 31157the default. If it is \texttt{0}, then \texttt{Circle} will erase 31158the circle. 31159 31160\subsection{Inscribed circles: \texttt{incircle}\index{incircle}} 31161 31162The \texttt{incircle} command takes three arguments. The arguments 31163are three points, regarded as the vertices of a triangle. 31164 31165\texttt{incircle} returns and draws the inscribed circle of the 31166triangle.\\ 31167Input: 31168\begin{center} 31169 \tt 31170 incircle(-1,i,1+i) 31171\end{center} 31172Output: 31173\begin{center} 31174 \includegraphics[width=0.75\textwidth]{xcas-incircle.png} 31175\end{center} 31176 31177\subsection{Circumscribed circles: \texttt{circumcircle}\index{circumcircle}} 31178 31179The \texttt{circumcircle} command takes three arguments. The arguments are three 31180points, regarded as the vertices of a triangle. 31181 31182\texttt{circumcircle} returns and draws the circumscribed circle of the 31183triangle.\\ 31184Input: 31185\begin{center} 31186 \tt 31187 circumcircle(-1,i,1+i) 31188\end{center} 31189Output: 31190\begin{center} 31191 \includegraphics[width=0.75\textwidth]{xcas-circumcircle.png} 31192\end{center} 31193 31194\subsection{Excircles: \texttt{excircle}\index{excircle}} 31195 31196The \texttt{excircle} command takes three arguments. The arguments are three 31197points, regarded as the vertices of a triangle. 31198 31199\texttt{excircle} returns and draws the excircle of the 31200triangle in the interior angle of the first vertex.\\ 31201Input: 31202\begin{center} 31203 \tt 31204 excircle(-1,i,1+i) 31205\end{center} 31206Output: 31207\begin{center} 31208 \includegraphics[width=0.75\textwidth]{xcas-excircle.png} 31209\end{center} 31210 31211\subsection{The power of a point relative to a circle: \texttt{powerpc}\index{powerpc}} 31212 31213Given a circle $C$ of radius $r$ and a point $A$ at a distance of $d$ 31214from the center of $C$, the power of $A$ relative to $C$ is $d^2 - r^2$. 31215 31216The \texttt{powerpc} command takes as arguments a circle and a point. 31217 31218\texttt{powerpc} returns the power of the point relative to the circle.\\ 31219Input: 31220\begin{center} 31221 \tt 31222 powerpc(circle(0, 1+i), 3+i) 31223\end{center} 31224Output: 31225\begin{center} 31226 \tt 31227 8 31228\end{center} 31229 31230\subsection{The radical axis of two circles: \texttt{radical\_axis}\index{radical\_axis}} 31231 31232The radical axis of two circles is the set of points which have the 31233same power with respect to each circle. 31234 31235The \texttt{radical\_axis} command takes as arguments two circles. 31236 31237\texttt{radical\_axis} returns and draws the radical axis.\\ 31238Input: 31239\begin{center} 31240 \tt 31241 radical\_axis(circle(0,1+i),circle(1,1+i)) 31242\end{center} 31243Output: 31244\begin{center} 31245 \includegraphics[width=0.75\textwidth]{xcas-radaxis.png} 31246\end{center} 31247 31248\section{Other conic sections} 31249 31250\subsection{The ellipse in the plane: \texttt{ellipse}\index{ellipse}} 31251\label{sec:2dellipse} 31252 31253See section \ref{sec:3dellipse} for ellipses in space. 31254 31255The \texttt{ellipse} command draws ellipses and other conic sections. 31256 31257\texttt{ellipse} can take parameters in different forms. 31258\begin{itemize} 31259 \item \texttt{ellipse} can take one parameter, a second degree equation in the 31260 variables $x$ and $y$ (or an expression which will be set to zero). 31261 31262 \texttt{ellipse} returns and draws the conic section given by the 31263 equation.\\ 31264 Input: 31265 \begin{center} 31266 \tt 31267 ellipse(x\^{}2 + 2*y\^{}2 - 1) 31268 \end{center} 31269 Output: 31270 \begin{center} 31271 \includegraphics[width=0.75\textwidth]{xcas-ellipse.png} 31272 \end{center} 31273 31274 \item \texttt{ellipse} can take three parameters; either three 31275 points or two points and a real number. The first two points will 31276 be the foci of the ellipse and the third argument will be either a 31277 point on the ellipse or the length of the semi-major axis. 31278 31279 \texttt{ellipse} returns and draws the ellipse.\\ 31280 Input: 31281 \begin{center} 31282 \tt 31283 ellipse(-i,i,i+1) 31284 \end{center} 31285 Output: 31286 \begin{center} 31287 \includegraphics[width=0.75\textwidth]{xcas-ellipse2.png} 31288 \end{center} 31289 Input: 31290 \begin{center} 31291 \tt 31292 ellipse(-i,i,sqrt(5) - 1) 31293 \end{center} 31294 Output: 31295 \begin{center} 31296 \includegraphics[width=0.75\textwidth]{xcas-ellipse3.png} 31297 \end{center} 31298 Note that if the third argument is a point on the real axis, the 31299 real affix of the point won't work, it needs to be specified with 31300 the \texttt{point} command. 31301\end{itemize} 31302 31303\subsection{The hyperbola in the plane: \texttt{hyperbola}\index{hyperbola}} 31304\label{sec:2dhyperbola} 31305 31306See section \ref{sec:3dhyperbola} for hyperbolas in space. 31307 31308The \texttt{hyperbola} command draws hyperbolas and other conic sections. 31309 31310\texttt{hyperbola} can take parameters in different forms. 31311\begin{itemize} 31312 \item \texttt{hyperbola} can take one parameter, a second degree equation in the 31313 variables $x$ and $y$ (or an expression which will be set to zero). 31314 31315 \texttt{hyperbola} returns and draws the conic section given by the 31316 equation.\\ 31317 Input: 31318 \begin{center} 31319 \tt 31320 hyperbola(x\^{}2 - 2*y\^{}2 - 1) 31321 \end{center} 31322 Output: 31323 \begin{center} 31324 \includegraphics[width=0.75\textwidth]{xcas-hyperbola1.png} 31325 \end{center} 31326 31327 \item \texttt{hyperbola} can take three parameters; either three 31328 points or two points and a real number. The first two points will 31329 be the foci of the hyperbola and the third argument will be either a 31330 point on the hyperbola or the length of the semi-major axis. 31331 31332 \texttt{hyperbola} returns and draws the hyperbola.\\ 31333 Input: 31334 \begin{center} 31335 \tt 31336 hyperbola(-i,i,i+1) 31337 \end{center} 31338 Output: 31339 \begin{center} 31340 \includegraphics[width=0.75\textwidth]{xcas-hyperbola2.png} 31341 \end{center} 31342 Input: 31343 \begin{center} 31344 \tt 31345 hyperbola(-i,i,1/2) 31346 \end{center} 31347 Output: 31348 \begin{center} 31349 \includegraphics[width=0.75\textwidth]{xcas-hyperbola3.png} 31350 \end{center} 31351 Note that if the third argument is a point on the real axis, the 31352 real affix of the point won't work, it needs to be specified with 31353 the \texttt{point} command. 31354\end{itemize} 31355 31356\subsection{The parabola in the plane: \texttt{parabola}\index{parabola}} 31357\label{sec:2dparabola} 31358 31359See section \ref{sec:3dparabola} for parabolas in space. 31360 31361The \texttt{parabola} command draws parabolas and other conic sections. 31362 31363\texttt{parabola} can take parameters in different forms. 31364\begin{itemize} 31365 \item \texttt{parabola} can take one parameter, a second degree equation in the 31366 variables $x$ and $y$ (or an expression which will be set to zero). 31367 31368 \texttt{parabola} returns and draws the conic section given by the 31369 equation.\\ 31370 Input: 31371 \begin{center} 31372 \tt 31373 parabola(x\^{}2 - y - 1) 31374 \end{center} 31375 Output: 31376 \begin{center} 31377 \includegraphics[width=0.75\textwidth]{xcas-parabola1.png} 31378 \end{center} 31379 31380 \item \texttt{parabola} can take two parameters, both points. 31381 The points will be the focus and vertex of a parabola. 31382 31383 \texttt{parabola} returns and draws the parabola.\\ 31384 Input: 31385 \begin{center} 31386 \tt 31387 parabola(0,i) 31388 \end{center} 31389 Output: 31390 \begin{center} 31391 \includegraphics[width=0.75\textwidth]{xcas-parabola2.png} 31392 \end{center} 31393 31394 Note that if the second argument is a point on the real axis, the 31395 real affix of the point won't work, it needs to be specified with 31396 the \texttt{point} command. 31397 31398 \item \texttt{parabola} can take two parameters, a point $(a,b)$ and 31399 a real number $c$. 31400 31401 \texttt{parabola} returns and draws the parabola 31402 $y = b + c(x - a)^2$.\\ 31403 Input: 31404 \begin{center} 31405 \tt 31406 parabola(-i,1) 31407 \end{center} 31408 Output: 31409 \begin{center} 31410 \includegraphics[width=0.75\textwidth]{xcas-parabola3.png} 31411 \end{center} 31412 Input: 31413 \begin{center} 31414 \tt 31415 parabola(-i,i,1/2) 31416 \end{center} 31417 Output: 31418 \begin{center} 31419 \includegraphics[width=0.75\textwidth]{xcas-parabola4.png} 31420 \end{center} 31421 Note that if the third argument is a point on the real axis, the 31422 real affix of the point won't work, it needs to be specified with 31423 the \texttt{point} command. 31424\end{itemize} 31425 31426\section{Coordinates in the plane} 31427 31428\subsection{The affix of a point or vector: \texttt{affix}\index{affix}} 31429 31430The \texttt{affix} command takes a single argument, a point or a vector. 31431 31432\texttt{affix} returns the affix, the complex number corresponding to 31433the point or vector.\\ 31434Input: 31435\begin{center} 31436 \tt 31437 affix(point(2,3)) 31438\end{center} 31439Output: 31440\begin{center} 31441 \tt 31442 2 + 3*i 31443\end{center} 31444Input: 31445\begin{center} 31446 \tt 31447 affix(vector(-1,i)) 31448\end{center} 31449Output: 31450\begin{center} 31451 \tt 31452 1+i 31453\end{center} 31454 31455\subsection{The abscissa of a point or vector in the plane: \texttt{abscissa}\index{abscissa}} 31456\label{sec:2dabscissa} 31457 31458See section \ref{sec:3dabscissa} for abscissas in three-dimensional geometry. 31459 31460The \texttt{abscissa} command takes a point as argument. 31461 31462\texttt{abscissa} returns the abscissa ($x$-coordinate).\\ 31463Input: 31464\begin{center} 31465 \tt 31466 abscissa(point(1 + 2*i)) 31467\end{center} 31468Output: 31469\begin{center} 31470 \tt 31471 1 31472\end{center} 31473Input: 31474\begin{center} 31475 \tt 31476 abscissa(point(i) - point(1 + 2*i)) 31477\end{center} 31478Output: 31479\begin{center} 31480 \tt 31481 -1 31482\end{center} 31483Input: 31484\begin{center} 31485 \tt 31486 abscissa(1 + 2*i) 31487\end{center} 31488Output: 31489\begin{center} 31490 \tt 31491 1 31492\end{center} 31493Input: 31494\begin{center} 31495 \tt 31496 abscissa([1,2]) 31497\end{center} 31498Output: 31499\begin{center} 31500 \tt 31501 1 31502\end{center} 31503 31504\subsection{The ordinate of a point or vector in the plane: \texttt{ordinate}\index{ordinate}} 31505\label{sec:2dordinate} 31506 31507See section \ref{sec:3dordinate} for ordinates in three-dimensional geometry. 31508 31509The \texttt{ordinate} command takes a point as argument. 31510 31511\texttt{ordinate} returns the ordinate ($y$-coordinate).\\ 31512Input: 31513\begin{center} 31514 \tt 31515 ordinate(point(1 + 2*i)) 31516\end{center} 31517Output: 31518\begin{center} 31519 \tt 31520 2 31521\end{center} 31522Input: 31523\begin{center} 31524 \tt 31525 ordinate(point(i) - point(1 + 2*i)) 31526\end{center} 31527Output: 31528\begin{center} 31529 \tt 31530 -1 31531\end{center} 31532Input: 31533\begin{center} 31534 \tt 31535 ordinate(1 + 2*i) 31536\end{center} 31537Output: 31538\begin{center} 31539 \tt 31540 2 31541\end{center} 31542Input: 31543\begin{center} 31544 \tt 31545 ordinate([1,2]) 31546\end{center} 31547Output: 31548\begin{center} 31549 \tt 31550 2 31551\end{center} 31552 31553\subsection{The coordinates of a point, vector or line in the plane: \texttt{coordinates}\index{coordinates}} 31554\label{sec:2dcoordinates} 31555 31556See section \ref{sec:3dcoordinates} for coordinates in 31557three-dimensional geometry. 31558 31559The \texttt{coordinates} command takes as argument a point 31560or line. 31561 31562If the argument is a point, 31563\texttt{coordinates} returns a list consisting of the abscissa and 31564ordinate. 31565 31566If the argument is a line, \texttt{coordinates} returns a list of 31567two points on the line, in the order determined by the direction of 31568the line.\\ 31569Input: 31570\begin{center} 31571 \tt 31572 coordinates(1+2*i) 31573\end{center} 31574or: 31575\begin{center} 31576 \tt 31577 coordinates(point(1+2*i)) 31578\end{center} 31579or: 31580\begin{center} 31581 \tt 31582 coordinates(vector(1+2*i)) 31583\end{center} 31584Output: 31585\begin{center} 31586 \tt 31587 [1,2] 31588\end{center} 31589Input: 31590\begin{center} 31591 \tt 31592 coordinates(point(1+2*i) - point(i)) 31593\end{center} 31594or: 31595\begin{center} 31596 \tt 31597 coordinates(vector(i,1+2*i)) 31598\end{center} 31599or: 31600\begin{center} 31601 \tt 31602 coordinates(vector(point(i),point(1+2*i))) 31603\end{center} 31604or: 31605\begin{center} 31606 \tt 31607 coordinates(vector([0,1],[1,2])) 31608\end{center} 31609Output: 31610\begin{center} 31611 \tt 31612 [1,1] 31613\end{center} 31614Input: 31615\begin{center} 31616 \tt 31617 d := line(-1+i,1+2*i) 31618\end{center} 31619or: 31620\begin{center} 31621 \tt 31622 d := line(point(-1,1),point(1,2)) 31623\end{center} 31624Input: 31625\begin{center} 31626 \tt 31627 coordinates(d) 31628\end{center} 31629Output: 31630\begin{center} 31631 \tt 31632 [-1+i,1+2*i] 31633\end{center} 31634Input: 31635\begin{center} 31636 \tt 31637 coordinates(line(y = (1/2 * x + 3/2))) 31638\end{center} 31639Output: 31640\begin{center} 31641 \tt 31642 [3*i/2, 1+2*i] 31643\end{center} 31644Input: 31645\begin{center} 31646 \tt 31647 coordinates(line(x - 2*y + 3 = 0)) 31648\end{center} 31649Output: 31650\begin{center} 31651 \tt 31652 [3*i/2, (-4 + i)/2] 31653\end{center} 31654 31655\texttt{coordinates} can also take a sequence or list of points as an 31656argument; it then returns a sequence or list of the coordinates of 31657the points.\\ 31658Input: 31659\begin{center} 31660 \tt 31661 coordinates(i,1+2*i) 31662\end{center} 31663or: 31664\begin{center} 31665 \tt 31666 coordinates(point(i),point(1+2*i)) 31667\end{center} 31668Output: 31669\begin{center} 31670 \tt 31671 [0,1], [1,2] 31672\end{center} 31673Note that if the argument is a list of real numbers, it is 31674interpreted as a list of points on the real axis.\\ 31675Input: 31676\begin{center} 31677 \tt 31678 coordinates([1,2]) 31679\end{center} 31680Output: 31681\begin{center} 31682 \tt 31683 [[1,0],[2,0]] 31684\end{center} 31685 31686\subsection{The rectangular coordinates of a point: \texttt{rectangular\_coordinates}\index{rectangular\_coordinates}} 31687 31688The \texttt{rectangular\_coordinates} command takes as argument a 31689point in polar coordinates. 31690 31691\texttt{rectangular\_coordinates} returns the the rectangular 31692coordinates of the points.\\ 31693Input: 31694\begin{center} 31695 \tt 31696 rectangular\_coordinates(2, pi/4) 31697\end{center} 31698or: 31699\begin{center} 31700 \tt 31701 rectangular\_coordinates(polar\_point(2, pi/4)) 31702\end{center} 31703Output: 31704\begin{center} 31705 \tt 31706 [sqrt(2), sqrt(2)] 31707\end{center} 31708 31709\subsection{The polar coordinates of a point: \texttt{polar\_coordinates}\index{polar\_coordinates}} 31710 31711The \texttt{polar\_coordinates} command takes as argument a 31712point. 31713 31714\texttt{polar\_coordinates} returns the the polar 31715coordinates of the points.\\ 31716Input: 31717\begin{center} 31718 \tt 31719 polar\_coordinates(1 + i) 31720\end{center} 31721or: 31722\begin{center} 31723 \tt 31724 polar\_coordinates(point(1 + i)) 31725\end{center} 31726or: 31727\begin{center} 31728 \tt 31729 polar\_coordinates([1,1]) 31730\end{center} 31731Output: 31732\begin{center} 31733 \tt 31734 [sqrt(2), pi/4] 31735\end{center} 31736 31737\subsection{The Cartesian equation of a geometric object in the plane: \texttt{equation}\index{equation}} 31738\label{sec:2dcarteq} 31739 31740See section \ref{sec:3dcarteq} for Cartesian equations of 31741three-dimensional objects. 31742 31743The \texttt{equation} command takes as argument a geometric object. 31744 31745\texttt{equation} returns the Cartesian equation for the object. 31746(Note that \texttt{x} and \texttt{y} must be formal variables, they 31747might need to be purged with \texttt{purge(x)} and \texttt{purge(y)}).\\ 31748Input: 31749\begin{center} 31750 \tt 31751 equation(line(-1,i)) 31752\end{center} 31753Output: 31754\begin{center} 31755 \tt 31756 y = x + 1 31757\end{center} 31758 31759\subsection{The parametric equation of a geometric object in the plane: \texttt{parameq}\index{parameq}} 31760\label{sec:2dparam} 31761 31762See section \ref{sec:3dparam} for parametric equations in 31763three-dimensional geometry. 31764 31765The \texttt{parameq} command takes as argument a geometric object. 31766 31767\texttt{parameq} returns a parametric equation of the object, in the 31768form \texttt{x(t) + i*y(t)}. (Note that \texttt{t} must be a formal 31769variable, it may be necessary to purge it with \texttt{purge(t)}.)\\ 31770Input: 31771\begin{center} 31772 \tt 31773 parameq(line(-1,i)) 31774\end{center} 31775Output: 31776\begin{center} 31777 \tt 31778 t + (1-t)*i 31779\end{center} 31780Input: 31781\begin{center} 31782 \tt 31783 parameq(circle(-1,i)) 31784\end{center} 31785Output: 31786\begin{center} 31787 \tt 31788 -1 + exp(i*t) 31789\end{center} 31790Input: 31791\begin{center} 31792 \tt 31793 normal(parameq(ellipse(-1,1,i))) 31794\end{center} 31795Output: 31796\begin{center} 31797 \tt 31798 sqrt(2)*cos(t) + i*sin(t) 31799\end{center} 31800 31801\section{Measurements} 31802 31803\subsection{Measurement and display: 31804\texttt{distanceat}\index{distanceat} 31805\texttt{distanceatraw}\index{distanceatraw} 31806\texttt{angleat}\index{angleat} \texttt{angleatraw}\index{angleatraw} 31807\texttt{areaat}\index{areaat} \texttt{areaatraw}\index{areaatraw} 31808\texttt{perimeterat}\index{perimeterat} \texttt{perimeteratraw}\index{perimeteratraw} 31809\texttt{slopeat}\index{slopeat} \texttt{slopeatraw}\index{slopeatraw} 31810\texttt{extract\_measure}\index{extract\_measure}} 31811\label{sec:measdisplay} 31812 31813Many commands to find measures have a version ending in \texttt{at} 31814(or \texttt{atraw}) which are used to interactively find and display 31815the appropriate measure in a two-dimensional geometry screen. To use 31816them, open a geometry screen with \texttt{Alt-G} and then select the 31817appropriate measure from the \texttt{Mode $\blacktriangleright$ 31818Measure} menu. Once the mode is selected, then clicking on the names of 31819the appropriate objects (or, if a point is being selected, a name will 31820be automatically generated if clicking on an open point) with the 31821mouse and then clicking on another point will put the measurement at 31822the point; if the mode is the version ending in \texttt{at}, then the 31823measurement will have a label, if the mode is the version ending in 31824\texttt{atraw}, then the measurement will appear without a label. 31825 31826The commands with \texttt{at} and \texttt{atraw} versions are: 31827\begin{description} 31828 \item[\texttt{distance},\texttt{distanceat},\texttt{distanceatraw}] 31829 This finds the distance between two points or other geometric objects. 31830 (See section \ref{sec:2ddist}.) 31831 31832 \item[\texttt{angle},\texttt{angleat},\texttt{angleatraw}] This finds the measure of an angle $BAC$ given 31833 points $A$, $B$ and $C$. (See section \ref{sec:2dangle}.) 31834 31835 \item[\texttt{area},\texttt{areaat},\texttt{areaatraw}] This finds the area of a circle or a polygon 31836 which is star-shaped with respect to its first vertex. 31837 (See section \ref{sec:area}.) 31838 31839 \item[\texttt{perimeter},\texttt{perimeterat},\texttt{perimeteratraw}] 31840 This finds the perimeter of a circle, circular arc or a polygon. 31841 (See section \ref{sec:perim}.) 31842 31843 \item[\texttt{slope},\texttt{slopeat},\texttt{slopeatraw}] 31844 This finds the slope of a line, segment, or 31845 two points which determine a line. 31846 (See section \ref{sec:slope}.) 31847\end{description} 31848 31849These commands can also be used from the command line. They are like 31850the measurement command but take an extra argument, the point to 31851display the measurement. When using the version ending in 31852\texttt{at}, use names for the objects 31853rather create the objects within the measurement command.\\ 31854Input: 31855\begin{center} 31856 \tt 31857 S1 := square(0,1); areaat(S1,1+i) 31858\end{center} 31859Output: 31860\begin{center} 31861 \includegraphics[width=0.75\textwidth]{xcas-areaat.png} 31862\end{center} 31863Input: 31864\begin{center} 31865 \tt 31866 S2 := square(0,1); areaatraw(S2,1+i) 31867\end{center} 31868Output: 31869\begin{center} 31870 \includegraphics[width=0.75\textwidth]{xcas-areaatraw.png} 31871\end{center} 31872More sophisticated legends are created with the \texttt{legend} command.\\ 31873Input: 31874\begin{center} 31875 \tt 31876 S := square(0,1); a := area(S); legend(1+i,"Area(S) = " + string(a),blue) 31877\end{center} 31878Output: 31879\begin{center} 31880 \includegraphics[width=0.75\textwidth]{xcas-arealegend.png} 31881\end{center} 31882 31883The \texttt{extract\_measure} command takes as argument a command 31884which displays a measurement (one of the \texttt{at} or \texttt{atraw} 31885commands) and returns the measurement.\\ 31886Input: 31887\begin{center} 31888 \tt 31889 A := point(-1); B := point(1+i); C := point(i); 31890\end{center} 31891and then: 31892\begin{center} 31893 \tt 31894 extract\_measure(angleat(A,B,C,0.2i)) 31895\end{center} 31896Output: 31897\begin{center} 31898 \tt 31899 atan(1/3) 31900\end{center} 31901 31902\subsection{The distance between objects in the plane: \texttt{distance}\index{distance}} 31903\label{sec:2ddist} 31904 31905See section \ref{sec:3ddist} for distances in three-dimensional 31906geometry. 31907 31908The \texttt{distance} command takes two 31909arguments; either two points or two geometric objects. 31910 31911\texttt{distance} returns the distance between the two arguments.\\ 31912Input: 31913\begin{center} 31914 \tt 31915 distance(-1, 1+i) 31916\end{center} 31917Output: 31918\begin{center} 31919 \tt 31920 sqrt(5) 31921\end{center} 31922Input: 31923\begin{center} 31924 \tt 31925 distance(0, line(-1,1+i)) 31926\end{center} 31927Output: 31928\begin{center} 31929 \tt 31930 sqrt(5)/5 31931\end{center} 31932Input: 31933\begin{center} 31934 \tt 31935 distance(circle(0,1),line(-2,1+3*i)) 31936\end{center} 31937Output: 31938\begin{center} 31939 \tt 31940 sqrt(2) - 1 31941\end{center} 31942 31943Note that when the distance calculation uses parameters, \texttt{Xcas} 31944must be in real mode. In real mode:\\ 31945Input: 31946\begin{center} 31947 \tt 31948 assumes(a=[4,0,5,0.1]); A := point(0); B := point(a); 31949\end{center} 31950and then: 31951\begin{center} 31952 \tt 31953 simplify(distance(A,B)); simplify(distance(B,A)) 31954\end{center} 31955Output: 31956\begin{center} 31957 \tt 31958 |a|, |a| 31959\end{center} 31960In complex mode:\\ 31961Input: 31962\begin{center} 31963 \tt 31964 assumes(a=[4,0,5,0.1]); A := point(0); B := point(a); 31965\end{center} 31966and then: 31967\begin{center} 31968 \tt 31969 simplify(distance(A,B)); simplify(distance(B,A)) 31970\end{center} 31971Output: 31972\begin{center} 31973 \tt 31974 -a, a 31975\end{center} 31976 31977The \texttt{distance} command has \texttt{distanceat} and 31978\texttt{distanceatraw} versions (see section \ref{sec:measdisplay}). 31979 31980\subsection{The length squared of a segment in the plane: \texttt{distance2}\index{distance2}} 31981\label{sec:2ddist2} 31982 31983See section \ref{sec:3ddist2} for squares of lengths in 31984three-dimensional geometry. 31985 31986The \texttt{distance2} command takes as arguments two points. 31987 31988\texttt{distance2} returns the square of the distance between the 31989points.\\ 31990Input: 31991\begin{center} 31992 \tt 31993 distance2(-1, 1+i) 31994\end{center} 31995Output: 31996\begin{center} 31997 \tt 31998 5 31999\end{center} 32000 32001\subsection{The measure of an angle in the plane: \texttt{angle}\index{angle}} 32002\label{sec:2dangle} 32003 32004See section \ref{sec:3dangle} for angle measures in three-dimensional 32005geometry. 32006 32007The \texttt{angle} command takes three mandatory arguments and one 32008optional argument. The mandatory arguments are points \texttt{A}, 32009\texttt{B} and \texttt{C}, which determine an angle \texttt{BAC}. The 32010optional argument is a string. 32011 32012\texttt{angle} returns the measure of the angle (in the units that 32013\texttt{Xcas} is configured for). If there is an 32014optional fourth argument, the angle will be drawn indicated by a small 32015arc and labeled with the string. If the angle is a right angle, the 32016indicator will be a corner rather than an arc.\\ 32017Input: 32018\begin{center} 32019 \tt 32020 angle(0,1,1+i) 32021\end{center} 32022Output: 32023\begin{center} 32024 \tt 32025 pi/4 32026\end{center} 32027Input: 32028\begin{center} 32029 \tt 32030 angle(0,1,1+i,"a") 32031\end{center} 32032Output: 32033\begin{center} 32034 \includegraphics[width=0.75\textwidth]{xcas-anglea.png} 32035\end{center} 32036Input: 32037\begin{center} 32038 \tt 32039 angle(0,1,1+i,"") 32040\end{center} 32041Output: 32042\begin{center} 32043 \includegraphics[width=0.75\textwidth]{xcas-angleblank.png} 32044\end{center} 32045Input: 32046\begin{center} 32047 \tt 32048 angle(0,1,i,"A") 32049\end{center} 32050Output: 32051\begin{center} 32052 \includegraphics[width=0.75\textwidth]{xcas-anglebigA.png} 32053\end{center} 32054Input: 32055\begin{center} 32056 \tt 32057 angle(0,1,i,"A")[0] 32058\end{center} 32059Output: 32060\begin{center} 32061 \tt 32062 pi/2 32063\end{center} 32064 32065The \texttt{angle} command has \texttt{angleat} and 32066\texttt{angleatraw} versions (see section \ref{sec:measdisplay}). For 32067the command line versions of these commands, the optional fourth 32068argument for \texttt{angle} is replaced by a mandatory fourth argument 32069for the point to put the measurement. 32070 32071\subsection{The graphical representation of the area of a polygon: 32072\texttt{plotarea}\index{plotarea}\texttt{areaplot}\index{areaplot}} 32073 32074The \texttt{plotarea} (or \texttt{areaplot}) command takes as argument 32075a polygon. 32076 32077\texttt{plotarea} draws the filled polygon, with the signed area. 32078(The area is positive if the polygon is counterclockwise, negative if 32079it is clockwise.)\\ 32080Input: 32081\begin{center} 32082 \tt 32083 plotarea(polygon(1,(1+i)/2,1+i) 32084\end{center} 32085Output: 32086\begin{center} 32087 \includegraphics[width=0.75\textwidth]{xcas-plotarea1.png} 32088\end{center} 32089Input: 32090\begin{center} 32091 \tt 32092 plotarea(polygon(1,1+i,(1+i)/2) 32093\end{center} 32094Output: 32095\begin{center} 32096 \includegraphics[width=0.75\textwidth]{xcas-plotarea2.png} 32097\end{center} 32098The fill color can be changed as a local feature (see 32099\ref{sec:localfeatures}) and the position of the legend can be 32100changed (see \ref{sec:addlegend}).\\ 32101Input: 32102\begin{center} 32103 \tt 32104 plotarea(polygon(1,1+i,(1+i)/2),display=red+quadrant2) 32105\end{center} 32106Output: 32107\begin{center} 32108 \includegraphics[width=0.75\textwidth]{xcas-plotarea3.png} 32109\end{center} 32110 32111\subsection{The area of a polygon: \texttt{area}\index{area}} 32112\label{sec:area} 32113 32114The \texttt{area} command takes as argument a circle or polygon which 32115is star-shaped with respect to its first vertex (i.e., the line 32116segment from the first vertex to any point in the polygon lies within 32117the polygon). 32118 32119\texttt{area} returns the area of the object.\\ 32120Input: 32121\begin{center} 32122 \tt 32123 area(triangle(0,1,i)) 32124\end{center} 32125Output: 32126\begin{center} 32127 \tt 32128 1/2 32129\end{center} 32130Input: 32131\begin{center} 32132 \tt 32133 area(square(0,2)) 32134\end{center} 32135Output: 32136\begin{center} 32137 \tt 32138 4 32139\end{center} 32140 32141The \texttt{area} command has \texttt{areaat} and 32142\texttt{areaatraw} versions (see section \ref{sec:measdisplay}). 32143 32144\subsection{The perimeter of a polygon: \texttt{perimeter}\index{perimeter}} 32145\label{sec:perim} 32146 32147See also \texttt{arcLen}, section \ref{sec:arclen}. 32148 32149The \texttt{perimeter} command takes as argument a circle, circular 32150arc or a polygon. 32151 32152\texttt{perimeter} returns the perimeter of the object.\\ 32153Input: 32154\begin{center} 32155 \tt 32156 perimeter(circle(0,1)) 32157\end{center} 32158Output: 32159\begin{center} 32160 \tt 32161 2*pi 32162\end{center} 32163Input: 32164\begin{center} 32165 \tt 32166 perimeter(circle(0,1,pi/4,pi)) 32167\end{center} 32168Output: 32169\begin{center} 32170 \tt 32171 3*pi/4 32172\end{center} 32173Input: 32174\begin{center} 32175 \tt 32176 perimeter(arc(0,pi/4,pi)) 32177\end{center} 32178Output: 32179\begin{center} 32180 \tt 32181 pi\^{}2/8 32182\end{center} 32183Input: 32184\begin{center} 32185 \tt 32186 perimeter(triangle(0,1,i)) 32187\end{center} 32188Output: 32189\begin{center} 32190 \tt 32191 2+sqrt(2) 32192\end{center} 32193Input: 32194\begin{center} 32195 \tt 32196 perimeter(square(0,2)) 32197\end{center} 32198Output: 32199\begin{center} 32200 \tt 32201 8 32202\end{center} 32203 32204The \texttt{perimeter} command has \texttt{perimeterat} and 32205\texttt{perimeteratraw} versions (see section \ref{sec:measdisplay}). 32206 32207\subsection{The slope of a line: \texttt{slope}\index{slope}} 32208\label{sec:slope} 32209 32210The \texttt{slope} command takes as argument either a line, a line 32211segment, or two points which determine a line. 32212 32213\texttt{slope} returns the slope of the line.\\ 32214Input: 32215\begin{center} 32216 \tt 32217 slope(line(1,2i)) 32218\end{center} 32219or: 32220\begin{center} 32221 \tt 32222 slope(segment(1,2i)) 32223\end{center} 32224or: 32225\begin{center} 32226 \tt 32227 slope(1,2i) 32228\end{center} 32229Output: 32230\begin{center} 32231 \tt 32232 -2 32233\end{center} 32234Input: 32235\begin{center} 32236 \tt 32237 slope(line(x - 2y = 3)) 32238\end{center} 32239Output: 32240\begin{center} 32241 \tt 32242 1/2 32243\end{center} 32244Input: 32245\begin{center} 32246 \tt 32247 slope(tangent(plotfunc(sin(x)),pi/4)) 32248\end{center} 32249or: 32250\begin{center} 32251 \tt 32252 slope(LineTan(sin(x),pi/4)) 32253\end{center} 32254Output: 32255\begin{center} 32256 \tt 32257 sqrt(2)/2 32258\end{center} 32259 32260The \texttt{slope} command has \texttt{slopeat} and 32261\texttt{slopeatraw} versions (see section \ref{sec:measdisplay}). 32262 32263\subsection{The radius of a circle: \texttt{radius}\index{radius}} 32264 32265The \texttt{radius} command takes one argument, a circle. 32266 32267\texttt{radius} returns the radius of the circle.\\ 32268Input: 32269\begin{center} 32270 \tt 32271 radius(circle(-1,point(i))) 32272\end{center} 32273Output: 32274\begin{center} 32275 \tt 32276 sqrt(2)/2 32277\end{center} 32278 32279\subsection{The length of a vector: \texttt{abs}\index{abs}} 32280 32281The \texttt{abs} command takes as argument either a complex number or 32282a vector defined by two points. 32283 32284\texttt{abs} returns the absolute value of the complex number or the 32285length of the vector.\\ 32286Input: 32287\begin{center} 32288 \tt 32289 abs(1+i) 32290\end{center} 32291or: 32292\begin{center} 32293 \tt 32294 abs(point(1+2*i) - point(i)) 32295\end{center} 32296Output: 32297\begin{center} 32298 \tt 32299 sqrt(2) 32300\end{center} 32301 32302\subsection{The angle of a vector: \texttt{arg}\index{arg}} 32303 32304The \texttt{abs} command takes as argument a complex number or a 32305vector defined by two points. 32306 32307\texttt{abs} returns the argument of the complex number or the angle 32308between the positive $x$ direction and the vector.\\ 32309Input: 32310\begin{center} 32311 \tt 32312 arg(1+i) 32313\end{center} 32314Output: 32315\begin{center} 32316 \tt 32317 pi/4 32318\end{center} 32319 32320\subsection{Normalize a complex number: \texttt{normalize}\index{normalize}} 32321 32322The \texttt{normalize} command takes as argument a non-zero complex number. 32323 32324\texttt{normalize} returns the normalized version of the complex 32325number; the number divided by its absolute value.\\ 32326Input: 32327\begin{center} 32328 \tt 32329 normalize(3+4*i) 32330\end{center} 32331Output: 32332\begin{center} 32333 \tt 32334 (3 + 4*i)/5 32335\end{center} 32336 32337\section{Transformations} 32338 32339\subsection{General remarks} 32340 32341The transformations in this section operate on any geometric object. 32342As arguments, they can take the parameters which 32343specify the transformation. With those arguments, they will return a 32344new command which performs the transformation. If they are given a 32345geometric object as the final argument, they will return the 32346transformed object. 32347 32348\subsection{Translations in the plane: \texttt{translation}\index{translation}} 32349\label{sec:2dtranslations} 32350 32351See section \ref{sec:3dtranslations} for translations in space. 32352 32353The \texttt{translation} command can take one or two arguments. 32354 32355If \texttt{translation} has a single argument, that argument is the 32356translation vector. The translation vector can be given as a vector, 32357list of coordinates, a difference of points or a complex number. 32358\texttt{translation} 32359returns a new command which performs the translation.\\ 32360Input: 32361\begin{center} 32362 \tt 32363 t := translation(1+i) 32364\end{center} 32365then: 32366\begin{center} 32367 \tt 32368 t(-2) 32369\end{center} 32370Output: 32371\begin{center} 32372 \includegraphics[width=0.75\textwidth]{xcas-translation1.png} 32373\end{center} 32374 32375If \texttt{translation} has two arguments, the first argument is a 32376translation vector as above, and the second argument is a geometric 32377object. \texttt{translation} returns and draws the translated object.\\ 32378Input: 32379\begin{center} 32380 \tt 32381 translation([1,1],line(-2,-i)) 32382\end{center} 32383Output: 32384\begin{center} 32385 \includegraphics[width=0.75\textwidth]{xcas-translation2.png} 32386\end{center} 32387 32388\subsection{Reflections in the plane: \texttt{reflection}\index{reflection}} 32389\label{sec:2dreflections} 32390 32391See section \ref{sec:3dreflections} for reflections in space. 32392 32393The \texttt{reflection} command can take one or two arguments. 32394 32395If \texttt{reflection} has a single argument, that argument is a point 32396or line. \texttt{reflection} returns a new command which performs the reflection 32397with respect to the point or line.\\ 32398Input: 32399\begin{center} 32400 \tt 32401 rf := reflection(-1) 32402\end{center} 32403then: 32404\begin{center} 32405 \tt 32406 rf(1+i) 32407\end{center} 32408Output: 32409\begin{center} 32410 \includegraphics[width=0.75\textwidth]{xcas-reflection1.png} 32411\end{center} 32412 32413If \texttt{reflection} has two arguments, the first argument is a 32414point or line as above, and the second argument is a geometric 32415object. \texttt{reflection} returns and draws the reflection of the object.\\ 32416Input: 32417\begin{center} 32418 \tt 32419 reflection(-1, 1+i) 32420\end{center} 32421Output: 32422\begin{center} 32423 \includegraphics[width=0.75\textwidth]{xcas-reflection2.png} 32424\end{center} 32425Input: 32426\begin{center} 32427 \tt 32428 reflection(line(-1,i),1+i) 32429\end{center} 32430Output: 32431\begin{center} 32432 \includegraphics[width=0.75\textwidth]{xcas-reflection3.png} 32433\end{center} 32434 32435\subsection{Rotation in the plane: \texttt{rotation}\index{rotation}} 32436\label{sec:2drotations} 32437 32438See section \ref{sec:3drotations} for rotations in space. 32439 32440The \texttt{rotation} command can take two or three arguments. 32441 32442If \texttt{rotation} has two arguments, they are a point (the center 32443of rotation) and a real number (the angle of rotation). 32444\texttt{rotation} returns a new command which performs the rotation.\\ 32445Input: 32446\begin{center} 32447 \tt 32448 r := rotation(i, -pi/2) 32449\end{center} 32450then: 32451\begin{center} 32452 \tt 32453 r(1+i) 32454\end{center} 32455Output: 32456\begin{center} 32457 \includegraphics[width=0.75\textwidth]{xcas-rotation1.png} 32458\end{center} 32459 32460If \texttt{rotation} has three arguments, the first two arguments are a 32461point and real number as above, and the third argument is a geometric 32462object. \texttt{rotation} returns and draws the rotated object.\\ 32463Input: 32464\begin{center} 32465 \tt 32466 rotation(i, -pi/2, 1+i) 32467\end{center} 32468Output: 32469\begin{center} 32470 \includegraphics[width=0.75\textwidth]{xcas-rotation2.png} 32471\end{center} 32472Input: 32473\begin{center} 32474 \tt 32475 rotation(i, -pi/2, line(1+i,-1)) 32476\end{center} 32477Output: 32478\begin{center} 32479 \includegraphics[width=0.75\textwidth]{xcas-rotation3.png} 32480\end{center} 32481 32482\subsection{Homothety in the plane: \texttt{homothety}\index{homothety}} 32483\label{sec:2dhomothety} 32484 32485See section \ref{sec:3dhomothety} for homotheties in space. 32486 32487A homothety is a dilation about a given point. 32488The \texttt{homothety} command can take two or three arguments. 32489 32490If \texttt{homothety} has two arguments, they are a point (the center 32491of the homothety) and a real number (the scaling ratio). 32492\texttt{homothery} returns a new command which performs the dilation.\\ 32493Input: 32494\begin{center} 32495 \tt 32496 h := homothety(i, 2) 32497\end{center} 32498then: 32499\begin{center} 32500 \tt 32501 h(1+i) 32502\end{center} 32503Output: 32504\begin{center} 32505 \includegraphics[width=0.75\textwidth]{xcas-homothety1.png} 32506\end{center} 32507 32508If \texttt{homothety} has three arguments, the first two arguments are a 32509point and real number as above, and the third argument is a geometric 32510object. \texttt{homothety} returns and draws the dilated object.\\ 32511Input: 32512\begin{center} 32513 \tt 32514 homothety(i, 2, 1+i) 32515\end{center} 32516Output: 32517\begin{center} 32518 \includegraphics[width=0.75\textwidth]{xcas-homothety2.png} 32519\end{center} 32520Input: 32521\begin{center} 32522 \tt 32523 homothety(i, 2, circle(1+i,1)) 32524\end{center} 32525Output: 32526\begin{center} 32527 \includegraphics[width=0.75\textwidth]{xcas-homothety3.png} 32528\end{center} 32529 32530The \texttt{homothety} command can also take a complex number as the 32531second argument. In that case, the result will be a rotation as well 32532as a dilation. 32533 32534\subsection{Similarity in the plane: \texttt{similarity}\index{similarity}} 32535\label{sec:2dsimilarity} 32536 32537See section \ref{sec:3dsimilarity} for similarities in space. 32538 32539The \texttt{similarity} command rotates and scales about a given 32540point. It takes three or four arguments. 32541 32542If \texttt{similarity} has three arguments, they are a point (the center 32543of rotation), a real number (the scaling ratio) and a real number 32544(the angle of rotation). \texttt{similarity} returns a new 32545command which performs the transformation.\\ 32546Input: 32547\begin{center} 32548 \tt 32549 s := similarity(i, 2, -pi/2) 32550\end{center} 32551then: 32552\begin{center} 32553 \tt 32554 s(1+i) 32555\end{center} 32556Output: 32557\begin{center} 32558 \includegraphics[width=0.75\textwidth]{xcas-similarity1.png} 32559\end{center} 32560then: 32561\begin{center} 32562 \tt 32563 s(circle(1+i,1)) 32564\end{center} 32565Output: 32566\begin{center} 32567 \includegraphics[width=0.75\textwidth]{xcas-similarity2.png} 32568\end{center} 32569 32570If \texttt{similarity} has four arguments, the first three arguments are a 32571point and two numbers as above, and the fourth argument is a geometric 32572object. \texttt{similarity} returns and draws the transformed object.\\ 32573Input: 32574\begin{center} 32575 \tt 32576 similarity(i, 2, -pi/2, 1 + i) 32577\end{center} 32578Output: 32579\begin{center} 32580 \includegraphics[width=0.75\textwidth]{xcas-similarity3.png} 32581\end{center} 32582Input: 32583\begin{center} 32584 \tt 32585 similarity(i, 2, -pi/2, circle(1+i,1)) 32586\end{center} 32587Output: 32588\begin{center} 32589 \includegraphics[width=0.75\textwidth]{xcas-similarity4.png} 32590\end{center} 32591 32592Note that for a point \texttt{A} and numbers \texttt{k} and 32593\texttt{a}, the command \texttt{similarity(A,k,a)} is the same as 32594\texttt{homothety(A,k*exp(i*a))}. 32595 32596\subsection{Inversion in the plane: \texttt{inversion}\index{inversion}} 32597\label{sec:2dinversion} 32598 32599See section \ref{sec:3dinversion} for inversions in space. 32600 32601Given a circle $C$ with center $O$ and radius $r$, the \emph{inversion} 32602of a point $A$ with respect to $C$ is the 32603point $A'$ on the ray $\overrightarrow{OA}$ satisfying 32604$\overline{OA}\cdot\overline{OA'} = r^2$. 32605 32606The \texttt{inversion} command takes two or three arguments. 32607 32608If \texttt{inversion} has two arguments, they are a point (the center 32609of inversion) and a real number (the radius). 32610\texttt{inversion} returns a new command which performs the 32611inversion.\\ 32612Input: 32613\begin{center} 32614 \tt 32615 inver := inversion(i, 2) 32616\end{center} 32617then: 32618\begin{center} 32619 \tt 32620 inver(circle(1+i,1)) 32621\end{center} 32622Output: 32623\begin{center} 32624 \includegraphics[width=0.75\textwidth]{xcas-inversion1.png} 32625\end{center} 32626then: 32627\begin{center} 32628 \tt 32629 inver(circle(1+i,1/2)) 32630\end{center} 32631Output: 32632\begin{center} 32633 \includegraphics[width=0.75\textwidth]{xcas-inversion2.png} 32634\end{center} 32635 32636If \texttt{inversion} has three arguments, the first two arguments are a 32637point and number as above, and the third argument is a geometric 32638object. \texttt{inversion} returns and draws the inverted object.\\ 32639Input: 32640\begin{center} 32641 \tt 32642 inversion(i, 2, circle(1+i,1)) 32643\end{center} 32644Output: 32645\begin{center} 32646 \includegraphics[width=0.75\textwidth]{xcas-inversion3.png} 32647\end{center} 32648Input: 32649\begin{center} 32650 \tt 32651 inversion(i, 2, circle(1+i,1/2)) 32652\end{center} 32653Output: 32654\begin{center} 32655 \includegraphics[width=0.75\textwidth]{xcas-inversion4.png} 32656\end{center} 32657 32658\subsection{Orthogonal projection in the plane: \texttt{projection}\index{projection}} 32659\label{sec:2dprojection} 32660 32661See section \ref{sec:3dprojection} for projections in space. 32662 32663The \texttt{projection} command takes one or two arguments. 32664 32665If \texttt{projection} has one argument, it is a geometric object. 32666\texttt{projection} returns a new command which projects points onto 32667the object.\\ 32668Input: 32669\begin{center} 32670 \tt 32671 p1 := projection(line(-1,i)) 32672\end{center} 32673then: 32674\begin{center} 32675 \tt 32676 p1(i+1) 32677\end{center} 32678Output: 32679\begin{center} 32680 \includegraphics[width=0.75\textwidth]{xcas-projection1.png} 32681\end{center} 32682Input: 32683\begin{center} 32684 \tt 32685 p2 := projection(circle(-1,1)) 32686\end{center} 32687then: 32688\begin{center} 32689 \tt 32690 p2(i) 32691\end{center} 32692Output: 32693\begin{center} 32694 \includegraphics[width=0.75\textwidth]{xcas-projection2.png} 32695\end{center} 32696 32697If \texttt{projection} has two arguments, the first arguments is a 32698geometric object as above, and the second argument is a point. 32699\texttt{projection} returns and draws the projection of the point onto 32700the object.\\ 32701Input: 32702\begin{center} 32703 \tt 32704 projection(line(-1, i), 1+i) 32705\end{center} 32706Output: 32707\begin{center} 32708 \includegraphics[width=0.75\textwidth]{xcas-projection3.png} 32709\end{center} 32710Input: 32711\begin{center} 32712 \tt 32713 projection(circle(-1,1), i) 32714\end{center} 32715Output: 32716\begin{center} 32717 \includegraphics[width=0.75\textwidth]{xcas-projection4.png} 32718\end{center} 32719 32720\section{Properties} 32721 32722\subsection{Check if a point is on an object in the plane: \texttt{is\_element}\index{is\_element}} 32723\label{sec:2diselem} 32724 32725See section \ref{sec:3diselem} for checking elements in 32726three-dimensional geometry. 32727 32728The \texttt{is\_element} command takes two arguments, a point and a 32729geometric object. 32730 32731\texttt{is\_element} returns \texttt{1} if the point is an element of 32732the object and returns \texttt{0} otherwise.\\ 32733Input: 32734\begin{center} 32735 \tt 32736 is\_element(-1-i, line(0,1+i)) 32737\end{center} 32738Output: 32739\begin{center} 32740 \tt 32741 1 32742\end{center} 32743Input: 32744\begin{center} 32745 \tt 32746 is\_element(i, line(0,1+i)) 32747\end{center} 32748Output: 32749\begin{center} 32750 \tt 32751 0 32752\end{center} 32753 32754\subsection{Check if three points are collinear in the plane: \texttt{is\_collinear}\index{is\_collinear}} 32755\label{sec:2dcheckcollinear} 32756 32757See section \ref{sec:3dcheckcollinear} for checking for collinearity in 32758three-dimensional geometry. 32759 32760The \texttt{is\_collinear} command is a Boolean function which takes as argument a list or 32761sequence of points. 32762 32763\texttt{is\_collinear} returns \texttt{1} if the points are collinear 32764and returns \texttt{0} otherwise.\\ 32765Input: 32766\begin{center} 32767 \tt 32768 is\_collinear(0,1+i,-1-i) 32769\end{center} 32770Output: 32771\begin{center} 32772 \tt 32773 1 32774\end{center} 32775Input: 32776\begin{center} 32777 \tt 32778 is\_collinear(i/100, 1+i, -1-i) 32779\end{center} 32780Output: 32781\begin{center} 32782 \tt 32783 0 32784\end{center} 32785 32786\subsection{Check if four points are concyclic in the plane: \texttt{is\_concyclic}\index{is\_concyclic}} 32787\label{sec:2dcheckconcycle} 32788 32789See section \ref{sec:3dcheckconcycle} for checking for concyclicity in 32790three-dimensional geometry. 32791 32792The \texttt{is\_concyclic} command is a Boolean function which takes as argument a list or 32793sequence of points. 32794 32795\texttt{is\_concyclic} returns \texttt{1} if the points are cyclic 32796and returns \texttt{0} otherwise.\\ 32797Input: 32798\begin{center} 32799 \tt 32800 is\_concyclic(1+i, -1+i, -1-i, 1-i) 32801\end{center} 32802Output: 32803\begin{center} 32804 \tt 32805 1 32806\end{center} 32807Input: 32808\begin{center} 32809 \tt 32810 is\_concyclic(i, -1+i, -1-i, 1-i) 32811\end{center} 32812Output: 32813\begin{center} 32814 \tt 32815 0 32816\end{center} 32817 32818\subsection{Check if a point is in a polygon or circle: \texttt{is\_inside}\index{is\_inside}} 32819 32820The \texttt{is\_inside} command is a Boolean function which takes two arguments, a point and a 32821polygon or circle. 32822 32823\texttt{is\_inside} returns \texttt{1} if the point is inside the 32824polygon or circle (including the boundary) and returns \texttt{0} 32825otherwise.\\ 32826Input: 32827\begin{center} 32828 \tt 32829 is\_inside(0,circle(-1,1)) 32830\end{center} 32831Output: 32832\begin{center} 32833 \tt 32834 1 32835\end{center} 32836Input: 32837\begin{center} 32838 \tt 32839 is\_inside(2,polygon([1,2-i,3+i])) 32840\end{center} 32841Output: 32842\begin{center} 32843 \tt 32844 1 32845\end{center} 32846Input: 32847\begin{center} 32848 \tt 32849 is\_inside(1-i, triangle([1,2-i,3+i])) 32850\end{center} 32851Output: 32852\begin{center} 32853 \tt 32854 0 32855\end{center} 32856 32857\subsection{Check if an object is an equilateral triangle in the plane: \texttt{is\_equilateral}\index{is\_equilateral}} 32858\label{sec:2dcheckequilateral} 32859 32860See section \ref{sec:3dcheckequilateral} for checking for equilateral 32861triangles in three-dimensional geometry. 32862 32863The \texttt{is\_equilateral} command is a Boolean function which takes 32864as argument a geometric object or three points. 32865 32866\texttt{is\_equilateral} returns \texttt{1} if the object (or the 32867triangle formed by the three points) is an equilateral triangle and 32868returns \texttt{0} otherwise.\\ 32869Input: 32870\begin{center} 32871 \tt 32872 is\_equilateral(0,2,1+i*sqrt(3)) 32873\end{center} 32874Output: 32875\begin{center} 32876 \tt 32877 1 32878\end{center} 32879Input: 32880\begin{center} 32881 \tt 32882 T := equilateral\_triangle(0,2,C) 32883\end{center} 32884then: 32885\begin{center} 32886 \tt 32887 is\_equilateral(T[0]) 32888\end{center} 32889Output: 32890\begin{center} 32891 \tt 32892 1 32893\end{center} 32894Note that \texttt{T[0]} is a triangle since \texttt{T} is a list made 32895of a triangle and the vertex \texttt{C}. Entering \texttt{affix(C)} 32896returns \texttt{1 + i*sqrt(3)}\\ 32897Input: 32898\begin{center} 32899 \tt 32900 is\_equilateral(1+i, -1+i, -1-i) 32901\end{center} 32902Output: 32903\begin{center} 32904 \tt 32905 0 32906\end{center} 32907 32908\subsection{Check if an object in the plane is an isosceles triangle: \texttt{is\_isosceles}\index{is\_isosceles}} 32909\label{sec:2dcheckisosceles} 32910 32911See section \ref{sec:3dcheckisosceles} for checking for isosceles 32912triangles in three-dimensional geometry. 32913 32914The \texttt{is\_isosceles} command takes 32915as argument a geometric object or three points. 32916 32917\texttt{is\_isosceles} returns \texttt{1}, \texttt{2}, \texttt{3} 32918or \texttt{4} if the object (or triangle formed by the three points) 32919is an isosceles triangle (the number indicates which vertex is on two 32920equal sides) or a value of \texttt{4} means the triangle is equilateral. 32921The command returns \texttt{0} if the object is not an isosceles 32922triangle.\\ 32923Input: 32924\begin{center} 32925 \tt 32926 is\_isosceles(0, 1+i,i) 32927\end{center} 32928Output: 32929\begin{center} 32930 \tt 32931 2 32932\end{center} 32933Input: 32934\begin{center} 32935 \tt 32936 T := isosceles\_triangle(0,1,pi/4) 32937\end{center} 32938then: 32939\begin{center} 32940 \tt 32941 is\_isosceles(T) 32942\end{center} 32943Output: 32944\begin{center} 32945 \tt 32946 1 32947\end{center} 32948Input: 32949\begin{center} 32950 \tt 32951 T := isosceles\_triangle(0,1,pi/4,C) 32952\end{center} 32953then: 32954\begin{center} 32955 \tt 32956 is\_isosceles(T[0]) 32957\end{center} 32958Output: 32959\begin{center} 32960 \tt 32961 1 32962\end{center} 32963Note that \texttt{T[0]} is a triangle since \texttt{T} is a list made 32964of a triangle and the vertex \texttt{C}. Entering \texttt{affix(C)} 32965returns \texttt{sqrt(2)/2 + i*sqrt(2)/2}\\ 32966Input: 32967\begin{center} 32968 \tt 32969 is\_isosceles(1+i, -1+i, -i) 32970\end{center} 32971Output: 32972\begin{center} 32973 \tt 32974 3 32975\end{center} 32976Input: 32977\begin{center} 32978 \tt 32979 is\_isosceles(0,2,1+i*sqrt(3)) 32980\end{center} 32981Output: 32982\begin{center} 32983 \tt 32984 4 32985\end{center} 32986 32987\subsection{Check if an object in the plane is a right triangle or a rectangle: \texttt{is\_rectangle}\index{is\_rectangle}} 32988\label{sec:2dcheckrect} 32989 32990See section \ref{sec:3dcheckrect} for checking for right triangles and rectangles 32991in three-dimensional geometry. 32992 32993The \texttt{is\_rectangle} command takes as argument a geometric object or a 32994sequence of three or four points. 32995 32996When the argument is a triangle or three points, \texttt{is\_rectangle} returns 32997\texttt{1}, \texttt{2} or \texttt{3} if the triangle is a right 32998triangle; the number indicates which vertex has the right angle. 32999When the argument is a quadrilateral or four points, \texttt{is\_rectangle} returns 33000\texttt{1} or \texttt{2} if the quadrilateral is a rectangle; 33001\texttt{1} if it is a rectangle but not a square and \texttt{2} if it 33002is a square. The command returns \texttt{0} if the object is not a 33003right triangle or rectangle.\\ 33004Input: 33005\begin{center} 33006 \tt 33007 is\_rectangle(1,1+i,i) 33008\end{center} 33009Output: 33010\begin{center} 33011 \tt 33012 2 33013\end{center} 33014Input: 33015\begin{center} 33016 \tt 33017 is\_rectangle(1+i, -2+i, -2-i, 1-i) 33018\end{center} 33019Output: 33020\begin{center} 33021 \tt 33022 1 33023\end{center} 33024Input: 33025\begin{center} 33026 \tt 33027 R := rectangle(-2-i,1-i, 3, C, D) 33028\end{center} 33029then: 33030\begin{center} 33031 \tt 33032 is\_rectangle(R[0]) 33033\end{center} 33034Output: 33035\begin{center} 33036 \tt 33037 1 33038\end{center} 33039Note that \texttt{R[0]} is a rectangle since \texttt{R} is a list made 33040of a rectangle and vertices \texttt{C} and \texttt{D}. Entering 33041\texttt{affix(C,D)} returns \texttt{-2+8*i, 1+8*i}. 33042 33043\subsection{Check if an object in the plane is a square: \texttt{is\_square}\index{is\_square}} 33044\label{sec:2dchecksquare} 33045 33046See section \ref{sec:3dchecksquare} for checking for squares 33047in three-dimensional geometry. 33048 33049The \texttt{is\_square} command is a Boolean function which takes as 33050argument a geometric object or four points. 33051 33052\texttt{is\_square} returns \texttt{1} if the object (or quadrilateral 33053determined by the four points) is a square and returns \texttt{0} 33054otherwise.\\ 33055Input: 33056\begin{center} 33057 \tt 33058 is\_square(1+i, -1+i, -1-i, 1-i) 33059\end{center} 33060Output: 33061\begin{center} 33062 \tt 33063 1 33064\end{center} 33065Input: 33066\begin{center} 33067 \tt 33068 K := square(1+i, -1+i) 33069\end{center} 33070then: 33071\begin{center} 33072 \tt 33073 is\_square(K) 33074\end{center} 33075Output: 33076\begin{center} 33077 \tt 33078 1 33079\end{center} 33080Input: 33081\begin{center} 33082 \tt 33083 K := square(1+i, -1+i, C, D); is\_square(K[0]) 33084\end{center} 33085Output: 33086\begin{center} 33087 \tt 33088 1 33089\end{center} 33090Note that \texttt{K[0]} is a square since \texttt{K} is a list made 33091of a square and vertices \texttt{C} and \texttt{D}. Entering 33092\texttt{affix(C,D)} returns \texttt{-1-i,1-i}.\\ 33093Input: 33094\begin{center} 33095 \tt 33096 is\_square(i, -1+i, -1-i, 1-i) 33097\end{center} 33098Output: 33099\begin{center} 33100 \tt 33101 0 33102\end{center} 33103 33104\subsection{Check if an object in the plane is a rhombus: \texttt{is\_rhombus}\index{is\_rhombus}} 33105\label{sec:2dcheckrhombus} 33106 33107See section \ref{sec:3dcheckrhombus} for checking for rhombuses 33108in three-dimensional geometry. 33109 33110The \texttt{is\_rhombus} command takes as 33111argument a geometric object or four points. 33112 33113\texttt{is\_rhombus} returns \texttt{1} or \texttt{2} if the object is 33114a rhombus; it returns \texttt{1} if the object is a rhombus but not a 33115square and \texttt{2} if the object is a square. The command returns 33116\texttt{0} if the object is not a rhombus.\\ 33117Input: 33118\begin{center} 33119 \tt 33120 is\_rhombus(1+i, -1+i, -1-i, 1-i) 33121\end{center} 33122Output: 33123\begin{center} 33124 \tt 33125 1 33126\end{center} 33127Input: 33128\begin{center} 33129 \tt 33130 K := rhombus(1+i, -1+i, pi/4) 33131\end{center} 33132then: 33133\begin{center} 33134 \tt 33135 is\_rhombus(K) 33136\end{center} 33137Input: 33138\begin{center} 33139 \tt 33140 1 33141\end{center} 33142Input: 33143\begin{center} 33144 \tt 33145 K := rhombus(1+i, -1+i, pi/4, C, D) 33146\end{center} 33147then: 33148\begin{center} 33149 \tt 33150 is\_rhombus(K[0]) 33151\end{center} 33152Input: 33153\begin{center} 33154 \tt 33155 1 33156\end{center} 33157Note that \texttt{K[0]} is a rhombus since \texttt{K} is a list made 33158of a rhombus and vertices \texttt{C} and \texttt{D}. Entering 33159\texttt{affix(C,D)} returns \texttt{-sqrt(2)-i,-sqrt(2)+i}.\\ 33160Input: 33161\begin{center} 33162 \tt 33163 is\_rhombus(i, -1+i, -1-i, 1-i) 33164\end{center} 33165Output: 33166\begin{center} 33167 \tt 33168 0 33169\end{center} 33170 33171\subsection{Check if an object in the plane is a parallelogram: \texttt{is\_parallelogram}\index{is\_parallelogram}} 33172\label{sec:2dcheckparallelogram} 33173 33174See section \ref{sec:3dcheckparallelogram} for checking for 33175parallelograms in three-dimensional geometry. 33176 33177The \texttt{is\_parallelogram} command takes as argument a geometric 33178object or four points. 33179 33180\texttt{is\_parallelogram} returns \texttt{1}, \texttt{2}, \texttt{3} 33181or \texttt{4} if the object is a parallelogram. It returns \texttt{4} 33182if the object is a square, \texttt{3} if the object is a rectangle but 33183not a square, \texttt{2} if the object is a rhombus but not a 33184rectangle, and returns \texttt{1} otherwise. The command returns 33185\texttt{0} if the object is not a parallelogram.\\ 33186Input: 33187\begin{center} 33188 \tt 33189 is\_parallelogram(i, -1+i, -1-i, 1-i) 33190\end{center} 33191Output: 33192\begin{center} 33193 \tt 33194 0 33195\end{center} 33196Input: 33197\begin{center} 33198 \tt 33199 is\_parallelogram(1+i, -1+i, -1-i, 1-i) 33200\end{center} 33201Output: 33202\begin{center} 33203 \tt 33204 1 33205\end{center} 33206Input: 33207\begin{center} 33208 \tt 33209 Q := quadrilateral(1+i, -1+i, -1-i, 1-i) 33210\end{center} 33211then: 33212\begin{center} 33213 \tt 33214 is\_parallelogram(Q) 33215\end{center} 33216Output: 33217\begin{center} 33218 \tt 33219 4 33220\end{center} 33221Input: 33222\begin{center} 33223 \tt 33224 P := parallelogram(-1-i, 1-i, i, D) 33225\end{center} 33226then: 33227\begin{center} 33228 \tt 33229 is\_parallelogram(P[0]) 33230\end{center} 33231Output: 33232\begin{center} 33233 \tt 33234 1 33235\end{center} 33236Note that \texttt{P[0]} is a parallelogram since \texttt{P} is a list made 33237of a parallelogram and vertex \texttt{D}. Entering 33238\texttt{affix(D)} returns \texttt{-2+i}.\\ 33239 33240\subsection{Check it two lines in the plane are parallel: \texttt{is\_parallel}\index{is\_parallel}} 33241\label{sec:2dcheckparallel} 33242 33243See section \ref{sec:3dcheckparallel} for checking for parallels in 33244three-dimensional geometry. 33245 33246The \texttt{is\_parallel} command is a Boolean function which takes as 33247argument two lines. 33248 33249\texttt{is\_parallel} returns \texttt{1} if the lines are parallel and 33250returns \texttt{0} otherwise.\\ 33251Input: 33252\begin{center} 33253 \tt 33254 is\_parallel(line(0,1+i),line(i,-1)) 33255\end{center} 33256Output: 33257\begin{center} 33258 \tt 33259 1 33260\end{center} 33261Output: 33262\begin{center} 33263 \tt 33264 is\_parallel(line(0,1+i),line(i,-1-i)) 33265\end{center} 33266Output: 33267\begin{center} 33268 \tt 33269 0 33270\end{center} 33271 33272\subsection{Check if two lines in the plane are perpendicular: \texttt{is\_perpendicular}\index{is\_perpendicular}} 33273\label{sec:2dcheckperp} 33274 33275See section \ref{sec:3dcheckperp} for checking for perpendicularity in 33276three-dimensional geometry. 33277 33278The \texttt{is\_perpendicular} command is a Boolean function which takes as 33279argument two lines. 33280 33281\texttt{is\_perpendicular} returns \texttt{1} if the lines are 33282perpendicular and returns \texttt{0} otherwise.\\ 33283Input: 33284\begin{center} 33285 \tt 33286 is\_perpendicular(line(0,1+i),line(i,1)) 33287\end{center} 33288Output: 33289\begin{center} 33290 \tt 33291 1 33292\end{center} 33293Input: 33294\begin{center} 33295 \tt 33296 is\_parallel(line(0,1+i),line(1+i,1)) 33297\end{center} 33298Output: 33299\begin{center} 33300 \tt 33301 0 33302\end{center} 33303 33304\subsection{Check if two circles in the plane are orthogonal: \texttt{is\_orthogonal}\index{is\_orthogonal}} 33305\label{sec:2dcheckorth} 33306 33307See section \ref{sec:3dcheckorth} for checking for orthogonality in 33308three-dimensional geometry. 33309 33310The \texttt{is\_orthogonal} command is a Boolean function which takes as 33311argument two lines or two circles. 33312 33313\texttt{is\_orthogonal} returns \texttt{1} if the objects are 33314orthogonal and returns \texttt{0} otherwise.\\ 33315Input: 33316\begin{center} 33317 \tt 33318 is\_orthogonal(line(0,1+i),line(i,1)) 33319\end{center} 33320Output: 33321\begin{center} 33322 \tt 33323 1 33324\end{center} 33325Input: 33326\begin{center} 33327 \tt 33328 is\_orthogonal(line(2,i),line(0,1+i)) 33329\end{center} 33330Output: 33331\begin{center} 33332 \tt 33333 0 33334\end{center} 33335Input: 33336\begin{center} 33337 \tt 33338 is\_orthogonal(circle(0,1+i),circle(2,1+i)) 33339\end{center} 33340Output: 33341\begin{center} 33342 \tt 33343 1 33344\end{center} 33345Input: 33346\begin{center} 33347 \tt 33348 is\_orthogonal(circle(0,1),circle(2,1)) 33349\end{center} 33350Output: 33351\begin{center} 33352 \tt 33353 0 33354\end{center} 33355 33356\subsection{Check if elements are conjugates: \texttt{is\_conjugate}\index{is\_conjugate}} 33357 33358The \texttt{is\_conjugate} is a Boolean function which takes as 33359arguments one of the following: 33360\begin{itemize} 33361 \item A circle followed by two more arguments, each of which is a 33362 point or a line.\\ 33363 In this case, \texttt{is\_conjugate} returns \texttt{1} if the 33364 last two arguments are conjugate with respect to the circle, it returns 33365 \texttt{0} otherwise.\\ 33366Input: 33367\begin{center} 33368 \tt 33369 is\_conjugate(circle(0,1+i),point(1-i), point(3+i)) 33370\end{center} 33371Output: 33372\begin{center} 33373 \tt 33374 1 33375\end{center} 33376Input: 33377\begin{center} 33378 \tt 33379 is\_conjugate(circle(0,1),point((1+i)/2), line(1+i,2)) 33380\end{center} 33381Output: 33382\begin{center} 33383 \tt 33384 1 33385\end{center} 33386Input: 33387\begin{center} 33388 \tt 33389 is\_conjugate(circle(0,1), line(1+i,2), line((1+i)/2,0)) 33390\end{center} 33391Output: 33392\begin{center} 33393 \tt 33394 1 33395\end{center} 33396 33397 \item Two lines or two points, followed by two more arguments, each of which is a 33398 point or a line.\\ 33399 In this case, \texttt{is\_conjugate} returns \texttt{1} if the 33400 last two arguments are conjugate with respect to the first two 33401 arguments, it returns \texttt{0} otherwise.\\ 33402Input: 33403\begin{center} 33404 \tt 33405 is\_conjugate(point(1+i),point(3+i),point(i),point(3/2+i)) 33406\end{center} 33407Output: 33408\begin{center} 33409 \tt 33410 1 33411\end{center} 33412Input: 33413\begin{center} 33414 \tt 33415 is\_conjugate(line(0,1+i),line(2,3+i),line(3,4+i),line(3/2,5/2+i)) 33416\end{center} 33417Output: 33418\begin{center} 33419 \tt 33420 1 33421\end{center} 33422\end{itemize} 33423 33424\subsection{Check if four points form a harmonic division: \texttt{is\_harmonic}\index{is\_harmonic}} 33425 33426The \texttt{is\_harmonic} command is a Boolean function which takes as 33427arguments four points. 33428 33429\texttt{is\_harmonic} returns \texttt{1} if the four points form a 33430harmonic range and returns \texttt{0} otherwise.\\ 33431Input: 33432\begin{center} 33433 \tt 33434 is\_harmonic(0,2,3/2,3) 33435\end{center} 33436Output: 33437\begin{center} 33438 \tt 33439 1 33440\end{center} 33441Input: 33442\begin{center} 33443 \tt 33444 is\_harmonic(0,1+i,1,i) 33445\end{center} 33446Output: 33447\begin{center} 33448 \tt 33449 0 33450\end{center} 33451 33452\subsection{Check if lines are in a bundle: \texttt{is\_harmonic\_line\_bundle}\index{is\_harmonic\_line\_bundle}} 33453 33454The \texttt{is\_harmonic\_line\_bundle} command takes as 33455argument a list or sequence of lines. 33456 33457\texttt{is\_harmonic\_line\_bundle} returns 33458\begin{enumerate} 33459 \item \texttt{1} if the lines pass through a common point 33460 \item \texttt{2} if the lines are parallel 33461 \item \texttt{3} if the lines are the same 33462 \item \texttt{0} otherwise 33463\end{enumerate} 33464Input: 33465\begin{center} 33466 \tt 33467 is\_harmonic\_line\_bundle([line(0,1+i),line(0,2+i),line(0,3+i),line(0,1)]) 33468\end{center} 33469Output: 33470\begin{center} 33471 \tt 33472 1 33473\end{center} 33474 33475\subsection{Check if circles are in a bundle: \texttt{is\_harmonic\_circle\_bundle}\index{is\_harmonic\_circle\_bundle}} 33476 33477The \texttt{is\_harmonic\_circle\_bundle} command takes as 33478argument a list or sequence of circles. 33479 33480\texttt{is\_harmonic\_circle\_bundle} returns 33481\begin{enumerate} 33482 \item \texttt{1} if the circles pass through a common point 33483 \item \texttt{2} if the circles are concentric 33484 \item \texttt{3} if the circles are the same 33485 \item \texttt{0} otherwise 33486\end{enumerate} 33487Input: 33488\begin{center} 33489 \tt 33490 is\_harmonic\_circle\_bundle([circle(0,i),circle(4,i),circle(0,1/2)]) 33491\end{center} 33492Output: 33493\begin{center} 33494 \tt 33495 1 33496\end{center} 33497 33498\section{Harmonic division} 33499 33500\subsection{Find a point dividing a segment in the harminic ratio $k$: \texttt{division\_point}\index{division\_point}} 33501 33502The \texttt{division\_point} command takes as argument two points 33503$a$ and $b$ and a complex number $k$. 33504 33505\texttt{division\_point} returns and draws $z$ where 33506$(z-a)/(z-b) = k$.\\ 33507Input: 33508\begin{center} 33509 \tt 33510 division\_point(i,2+i,3+i) 33511\end{center} 33512Output: 33513\begin{center} 33514 \includegraphics[width=0.75\textwidth]{xcas-divpoint.png} 33515\end{center} 33516Input: 33517\begin{center} 33518 \tt 33519 affix(division\_point(i,2+i,3)) 33520\end{center} 33521Output: 33522\begin{center} 33523 \tt 33524 3+i 33525\end{center} 33526 33527\subsection{The cross ratio of four collinear points: \texttt{cross\_ratio}\index{cross\_ratio}} 33528 33529The \texttt{cross\_ratio} command takes as arguments four complex 33530numbers $a,b,c,d$. 33531 33532\texttt{cross\_ratio} returns the cross ratio 33533$[(c-a)/(c-b)]/[(d-a)/(d-b)]$.\\ 33534Input: 33535\begin{center} 33536 \tt 33537 cross\_ratio(0,1,2,3) 33538\end{center} 33539Output: 33540\begin{center} 33541 \tt 33542 4/3 33543\end{center} 33544Input: 33545\begin{center} 33546 \tt 33547 cross\_ratio(i,2+i,3/2 + i, 3+i) 33548\end{center} 33549Output: 33550\begin{center} 33551 \tt 33552 -1 33553\end{center} 33554 33555\subsection{Harmonic division: \texttt{harmonic\_division}\index{harmonic\_division}} 33556 33557Four collinear points $A$,$B$,$C$ and $D$ are in harmonic division if 33558$\overline{CA}/\overline{CB} = \overline{DA}/\overline{DB} $. In 33559this case, $D$ is called the harmonic conjugate of $A$, $B$ and $C$. 33560 33561Four concurrent lines or four parallel lines are in harmonic division 33562if the intersection of any fifth line with these four lines consists 33563of four points in harmonic division. The lines are also said to form a harmonic 33564pencil. The fourth line is called the harmonic conjugate of the first 33565three. 33566 33567The \texttt{harmonic\_division} command takes as argument three 33568collinear points or three concurrent lines and a variable name. 33569 33570\texttt{harmonic\_division} returns and draws the three points or 33571lines along with a fourth so the four objects are in harmonic 33572division, and assigns the fourth point or line to the variable name.\\ 33573Input: 33574\begin{center} 33575 \tt 33576 harmonic\_division(0,2,3/2,D) 33577\end{center} 33578Output: 33579\begin{center} 33580 \includegraphics[width=0.75\textwidth]{xcas-harmdiv.png} 33581\end{center} 33582Input: 33583\begin{center} 33584 \tt 33585 harmonic\_division(point(0),point(2),point(3/2),D) 33586\end{center} 33587Output: 33588\begin{center} 33589 \includegraphics[width=0.75\textwidth]{xcas-harmdiv2.png} 33590\end{center} 33591Input: 33592\begin{center} 33593 \tt 33594 affix(D) 33595\end{center} 33596Output: 33597\begin{center} 33598 \tt 33599 3 33600\end{center} 33601 33602\subsection{The harmonic conjugate: \texttt{harmonic\_conjugate}\index{harmonic\_conjugate}} 33603 33604The \texttt{harmonic\_conjugate} command takes as arguments three 33605collinear points, three concurrent lines or three parallel lines. 33606 33607\texttt{harmonic\_conjugate} returns and draws the harmonic conjugate of the 33608arguments.\\ 33609Input: 33610\begin{center} 33611 \tt 33612 harmonic\_conjugate(0,2,3/2) 33613\end{center} 33614Output: 33615\begin{center} 33616 \includegraphics[width=0.75\textwidth]{xcas-harmconj.png} 33617\end{center} 33618Input: 33619\begin{center} 33620 \tt 33621 affix(harmonic\_conjugate(0,2,3/2)) 33622\end{center} 33623Output: 33624\begin{center} 33625 \tt 33626 3 33627\end{center} 33628Input: 33629\begin{center} 33630 \tt 33631 harmonic\_conjugate(line(0,1+i),line(0,3+i),line(0,i)) 33632\end{center} 33633Output: 33634\begin{center} 33635 \includegraphics[width=0.75\textwidth]{xcas-harmconj2.png} 33636\end{center} 33637 33638\subsection{Pole and polar: \texttt{pole}\index{pole} \texttt{polar}\index{polar}} 33639 33640Given a circle centered at $O$, a point $A$ is a pole and a line $L$ is the corresponding 33641polar if $L$ is the line passing through the inversion of $A$ with 33642respect to the circle (see section \ref{sec:2dinversion}) passing 33643through the line $\overleftrightarrow{OA}$. 33644 33645The \texttt{polar} command takes as argument a circle \texttt{C} and a 33646point \texttt{a}. 33647 33648\texttt{polar} returns and draws the polar of the point \texttt{a} 33649with respect to \texttt{C}.\\ 33650Input: 33651\begin{center} 33652 \tt 33653 polar(circle(0,1),(i+1)/2) 33654\end{center} 33655Output: 33656\begin{center} 33657 \includegraphics[width=0.75\textwidth]{xcas-polar.png} 33658\end{center} 33659 33660The \texttt{pole} command takes as argument a circle \texttt{C} and a 33661line \texttt{L}. 33662 33663\texttt{pole} returns and draws the pole of the line \texttt{L} 33664with respect to \texttt{C}.\\ 33665Input: 33666\begin{center} 33667 \tt 33668 pole(circle(0,1),line(i,1)) 33669\end{center} 33670Output: 33671\begin{center} 33672 \includegraphics[width=0.75\textwidth]{xcas-pole.png} 33673\end{center} 33674Input: 33675\begin{center} 33676 \tt 33677 affix(pole(circle(0,1),line(i,1))) 33678\end{center} 33679Output: 33680\begin{center} 33681 \tt 33682 1+i 33683\end{center} 33684 33685\subsection{The polar reciprocal: \texttt{reciprocation}\index{reciprocation}} 33686 33687The \texttt{reciprocation} command takes as arguments a circle and 33688list of points and lines. 33689 33690\texttt{reciprocation} returns the list formed by replaced each point 33691or line by its polar or pole with respect to the circle.\\ 33692Input: 33693\begin{center} 33694 \tt 33695 reciprocation(circle(0,1),[point((1+i)/2),line(1,-1+i)]) 33696\end{center} 33697Output: 33698\begin{center} 33699 \includegraphics[width=0.75\textwidth]{xcas-reciprocation.png} 33700\end{center} 33701 33702\section{Loci and envelopes} 33703 33704\subsection{Loci: \texttt{locus}\index{locus}} 33705 33706The \texttt{locus} command draws the locus of points determined by 33707geometric objects moving in the plane, where the object depends on a 33708point moving along a curve. It can draw 33709a locus of points which depends on points on a curve, or 33710the envelope of a family of lines depending on points on a curve. 33711 33712\subsubsection{The locus of points depending on points on a curve.} 33713 33714For this, the \texttt{locus} command takes two mandatory arguments and two 33715optional arguments. 33716\begin{itemize} 33717 \item The first mandatory argument is a variable name. This variable 33718 needs to already have been assigned to a point, and that point 33719 should be a function of a second point which moves along a curve. 33720 This second point is the second argument to \texttt{locus}. 33721 \item 33722 The second mandatory argument is another variable name. This 33723 variable needs to already have been assigned to a point, 33724 that point should be the result of the \texttt{element} command; for 33725 example, defined by \texttt{element(C)} for a curve \texttt{C}. 33726 (See section \ref{sec:element}.) 33727 \item The optional third argument is used to set \texttt{t} to 33728 an interval, where \texttt{t} is the parameter 33729 for the curve \texttt{C}. (You can double check the 33730 name of the parameter for a curve \texttt{C} with the command 33731 \texttt{parameq(C)}.) 33732 \item The optional fourth argument is used to set the value of 33733 \texttt{tstep} for the parameter \texttt{t}. 33734\end{itemize} 33735 33736\texttt{locus} will draw the locus of points formed by the first 33737argument, as the second argument traces over the curve \texttt{C}.\\ 33738Input: 33739\begin{center} 33740 \tt 33741 P := element(line(i, i+1)) 33742\end{center} 33743then: 33744\begin{center} 33745 \tt 33746 G := isobarycenter(-1,1,P) 33747\end{center} 33748then: 33749\begin{center} 33750 \tt 33751 locus(G,P) 33752\end{center} 33753This will draw the set of isobarycenters of the triangles with vertices 33754\texttt{-1}, \texttt{1} and \texttt{P}, where \texttt{P} ranges over 33755the line through \texttt{i} and \texttt{i+1}.\\ 33756Output: 33757\begin{center} 33758 \includegraphics[width=0.75\textwidth]{xcas-locus1.png} 33759\end{center} 33760Input: 33761\begin{center} 33762 \tt 33763 parameq(C) 33764\end{center} 33765Output: 33766\begin{center} 33767 \tt 33768 t + i 33769\end{center} 33770Input: 33771\begin{center} 33772 \tt 33773 locus(G,P,t=-3..3,tstep=0.1) 33774\end{center} 33775Output: 33776\begin{center} 33777 \includegraphics[width=0.75\textwidth]{xcas-locus2.png} 33778\end{center} 33779 33780\subsubsection{The envelope of a family of lines which depend on points on a 33781curve.} 33782 33783For this, the \texttt{locus} command takes two mandatory arguments and 33784two optional arguments. 33785\begin{itemize} 33786 \item The first mandatory argument is a variable name. This variable 33787 needs to already have been assigned to a line, and that line 33788 should be a function of a point which moves along a curve. This point is the 33789 second argument to \texttt{locus}. 33790 \item 33791 The second mandatory argument is another variable name. This 33792 variable needs to already have been assigned to a point, 33793 that point should be the result of the \texttt{element} command; for 33794 example, defined by \texttt{element(C)} for a curve \texttt{C}. 33795 (See section \ref{sec:element}.) 33796 \item The optional third argument is used to set \texttt{t} to 33797 an interval, where \texttt{t} is the parameter 33798 for the curve \texttt{C}. (You can double check the 33799 name of the parameter for a curve \texttt{C} with the command 33800 \texttt{parameq(C)}.) 33801 \item The optional fourth argument is used to set the value of 33802 \texttt{tstep} for the parameter \texttt{t}. 33803\end{itemize} 33804 33805\texttt{locus} will draw the envelope of the lines formed by the first 33806argument, as the second argument traces over the curve \texttt{C}.\\ 33807Input: 33808\begin{center} 33809 \tt 33810 F := point(1) 33811\end{center} 33812then: 33813\begin{center} 33814 \tt 33815 H := element(line(x=0)) 33816\end{center} 33817then: 33818\begin{center} 33819 \tt 33820 d := perpen\_bisector(F,H) 33821\end{center} 33822then: 33823\begin{center} 33824 \tt 33825 locus(d,H) 33826\end{center} 33827This will draw the envelope of the family of perpendicular bisectors 33828of the segments from the point \texttt{1} to the points on the line 33829\texttt{x=0}. 33830Output: 33831\begin{center} 33832 \includegraphics[width=0.75\textwidth]{xcas-locus3.png} 33833\end{center} 33834 33835To draw the envelope of a family of lines which depend on a parameter, 33836such as the lines given by the equations 33837\[ 33838 y + x \tan(t) - 2\sin(t)=0 33839\] 33840over the parameter $t$, the parameter can be regarded as the affixes 33841of points on the line $y=0$. \\ 33842Input: 33843\begin{center} 33844 \tt 33845 H := element(line(y=0)) 33846\end{center} 33847then: 33848\begin{center} 33849 \tt 33850 D := line(y + x*tan(affix(H)) - 2*sin(affix(H))) 33851\end{center} 33852then: 33853\begin{center} 33854 \tt 33855 locus(D,H) 33856\end{center} 33857Output: 33858\begin{center} 33859 \includegraphics[width=0.75\textwidth]{xcas-locus4.png} 33860\end{center} 33861Input: 33862\begin{center} 33863 \tt 33864 locus(D,H,t=0..pi) 33865\end{center} 33866Output: 33867\begin{center} 33868 \includegraphics[width=0.75\textwidth]{xcas-locus5.png} 33869\end{center} 33870 33871\subsection{Envelopes: \texttt{envelope}\index{envelope}} 33872 33873The \texttt{envelope} command takes two arguments, the first argument is expression 33874\texttt{expr} in the variables \texttt{x}, \texttt{y} and a parameter 33875such as \texttt{t}, and the second argument is the name of the 33876parameter. 33877 33878\texttt{envelope} draws the envelope of the family of curves given by 33879\texttt{expr=0} over the parameter \texttt{t}.\\ 33880Input: 33881\begin{center} 33882 \tt 33883 envelope(y + x*tan(t) - 2*sin(t),t) 33884\end{center} 33885Output: 33886\begin{center} 33887 \includegraphics[width=0.75\textwidth]{xcas-envelope1.png} 33888\end{center} 33889 33890The \texttt{envelope} command can use variables besides \texttt{x} and \texttt{y}. In 33891this case, the second argument needs to be a list of the two variables 33892and the parameter.\\ 33893Input: 33894\begin{center} 33895 \tt 33896 envelope(v + u*tan(s) - 2*sin(s),[u,v,s]) 33897\end{center} 33898Output: 33899\begin{center} 33900 \includegraphics[width=0.75\textwidth]{xcas-envelope2.png} 33901\end{center} 33902 33903\subsection{The trace of a geometric object: \texttt{trace}} 33904\index{trace} 33905 33906The \texttt{trace} command takes as argument a geometric object which 33907depends on a parameter.\\ 33908\texttt{trace} draws the trace of the object as the parameter is 33909changed or the object is moved in \texttt{Pointer} mode. 33910 33911For example, to find the locus of points equidistant from a line 33912\texttt{D} and a point \texttt{F}, we can create a point \texttt{H} on 33913the line \texttt{D}. Since the point 33914To do this, open a graphic window (\texttt{Alt-G}) and type in the 33915following commands, one per line.\\ 33916First, create a line \texttt{D} (using sample points) and a sample 33917point \texttt{F}.\\ 33918Input 33919\begin{center} 33920 \tt 33921 A := point(-3-i)\\ 33922 B := point(1/2 + 2*i)\\ 33923 D := line(A,B,color=0)\\ 33924 F := point(4/3,1/2,color=0)\\ 33925\end{center} 33926The create a point \texttt{H} on the line \texttt{D} which we can move 33927around.\\ 33928Input: 33929\begin{center} 33930 \tt 33931 assume(a=[0.7,-5,5,0.1])\\ 33932 H := element(D,a) 33933\end{center} 33934To find a point equidistant from \texttt{D} and \texttt{F}, find the 33935point \texttt{M} where the perpendicular to \texttt{D} (at \texttt{H}) intersects 33936the perpendicular bisector to \texttt{HF}, and trace that point.\\ 33937Input: 33938\begin{center} 33939 \tt 33940 T := perpendicular(H,D)\\ 33941 M := single\_inter(perpen\_bisector(H,F),T))\\ 33942 trace(M) 33943\end{center} 33944Then as the point \texttt{H} on the line moves (by changing the value 33945of \texttt{a} with the slider), we will get the trace of \texttt{M}. 33946 33947To erase traces, add traces, activate or deactivate them, use the 33948\texttt{Trace} menu of the \texttt{M} button located on the right side of the geometry screen. 33949 33950 33951 33952\chapter{Three-dimensional Graphics} 33953\label{chap:3dgraphics} 33954 33955\section{Introduction} 33956 33957The \texttt{Alt+H} command brings up a display screen for 33958three-dimensional graphics. This screen has its own menu and command 33959lines. 33960\begin{center} 33961 \includegraphics[width=0.75\textwidth]{xcas-3dscreen.png} 33962\end{center} 33963This screen also automatically appears whenever there is a three-dimensional 33964graphic command. 33965 33966The plane of vision for a three-dimensional graphic screen is 33967perpendicular to the observer's line of vision. The plane of 33968vision is also indicated by dotted lines showing its intersection with 33969the parallelepiped. 33970The axis of vision for a three-dimensional graphic screen is 33971 33972The three-dimensional graphic screen starts with the image of a 33973parallelepiped bounding the graphics and vectors in the $x$, $y$ and 33974$z$ directions. At the top of the screen is the equation of the plane 33975of vision, which is a plane perpendicular to the observer's line of 33976vision. The plane of vision is shown graphically with dotted lines 33977indicating where it intersects the plane of vision. 33978 33979Clicking in the graphic screen outside of the parallelepiped and 33980dragging the mouse moves the $x$, $y$ and $z$ directions relative to 33981the observer; these directions are also changed with the \texttt{x}, 33982\texttt{X}, \texttt{y}, \texttt{Y}, \texttt{z} and \texttt{Z} keys. 33983Scrolling the mouse wheel moves the plane of vision along the line of 33984vision. The \texttt{in} and \texttt{out} buttons on the graphic 33985screen menu zoom in and out of the picture. 33986 33987The graphical features available for two-dimensional graphics (see 33988section \ref{sec:dispfeat}) are also available for three-dimensional 33989graphics, but to see the points the markers must be squares with width 33990(\texttt{point\_width}) at least \texttt{3}. 33991 33992The graphic screen menu has a \texttt{cfg} button which brings up a 33993configuration screen. Among other things, this screen has 33994\begin{itemize} 33995 \item An \texttt{Ortho proj} button, which determines whether the 33996 drawing uses orthogonal projection or perspective 33997 projection. 33998 \item A \texttt{Lights} button, which determines whether the objects 33999 are lit or not; the locations of eight points for lighting are 34000 set using the buttons \texttt{L1}, \ldots, \texttt{L7}, which specify 34001 the points with homogeneous coordinates. 34002 \item A \texttt{Show axis} button, which determines whether or not 34003 the outlining parallelepiped is visible. 34004% \item hidden3d 34005\end{itemize} 34006 34007\section{Change the view} 34008 34009The depictions of three-dimensional objects are made with a coordinate 34010system $Oxyz$, where the $x$ axis is horizontal and directed right, 34011the $y$ axis is vertical and directed up, and the $z$ axis is 34012perpendicular to the screen and directed out of the screen. The 34013depictions can be transformed by changing to a different coordinate 34014system by setting a quaternion. (See section \ref{sec:globpar}.) 34015 34016\section{The axes} 34017 34018\subsection{Draw unit vectors: 34019\texttt{Ox\_3d\_unit\_vector}\index{Ox\_3d\_unit\_vector} 34020\texttt{Oy\_3d\_unit\_vector}\index{Oy\_3d\_unit\_vector} 34021\texttt{Oz\_3d\_unit\_vector}\index{Oz\_3d\_unit\_vector} 34022\texttt{frame\_3d}\index{frame\_3d}} 34023 34024The \texttt{Ox\_3d\_unit\_vector} command draws the unit vector in 34025the $x$-direction on a three-dimensional graphic screen.\\ 34026Input: 34027\begin{center} 34028 \tt 34029 Ox\_3d\_unit\_vector() 34030\end{center} 34031Output: 34032\begin{center} 34033 \includegraphics[width=0.5\textwidth]{xcas-3dunit.png} 34034\end{center} 34035Similarly, the \texttt{Oy\_3d\_unit\_vector} and 34036\texttt{Oz\_3d\_unit\_vector} commands draw the unit vector in the $y$ 34037and $z$ directions, respectively. 34038 34039These commands have no parameters, but can be decorated with the 34040\texttt{legend} command.\\ 34041Input: 34042\begin{center} 34043 \tt 34044 Ox\_3d\_unit\_vector(), legend(point([1,0,0]),"unit x vector",blue) 34045\end{center} 34046Output: 34047\begin{center} 34048 \includegraphics[width=0.5\textwidth]{xcas-3dunitblue.png} 34049\end{center} 34050 34051The \texttt{frame\_3d} command draws all three vectors simultaneously. 34052 34053\section{Points in space} 34054 34055\subsection{Define a point in three-dimensions: \texttt{point}\index{point}} 34056\label{sec:3dpoint} 34057 34058See section \ref{sec:2dpoint} for points in the plane. 34059 34060With the 3-d geometry screen in point mode, a click on a point with the 34061left mouse button will choose that point. Points chosen this way are 34062automatically named, first with \texttt{A}, then \texttt{B}, etc. 34063 34064Alternatively, the \texttt{point} command chooses a point, where the 34065point $(a,b,c)$ is specified by either the three coordinates 34066\text{a,b,c} or a list \texttt{[a,b,c]} of the coordinates. Many 34067commands which takes points as arguments can either take them as 34068\texttt{point(a,b,c)} or the list of coordinates \texttt{[a,b,c]}.\\ 34069Input: 34070\begin{center} 34071 \tt 34072 point(1,2,5) 34073\end{center} 34074or: 34075\begin{center} 34076 \tt 34077 point([1,2,5]) 34078\end{center} 34079Output: 34080\begin{center} 34081 \tt 34082 \includegraphics[width=0.5\textwidth]{xcas-3dpoint.png} 34083\end{center} 34084(The marker used to indicate the point can be changed; see section 34085\ref{sec:localfeatures}.) 34086 34087\subsection{Define a random point in three-dimensions: 34088\texttt{point3d}\index{point3d}} 34089 34090The \texttt{point3d} command takes as argument a sequence of names for 34091points and assigns random points to each name; the random points have 34092coordinates which are integers between -5 and 5. For example,\\ 34093Input: 34094\begin{center} 34095 \tt 34096 point3d(A,B,C) 34097\end{center} 34098then: 34099\begin{center} 34100 \tt 34101 plane(A,B,C) 34102\end{center} 34103Output: 34104\begin{center} 34105 \includegraphics[width=0.5\textwidth]{xcas-3dplane.png} 34106\end{center} 34107 34108\subsection{Find an intersection point of two objects in space: \texttt{single\_inter}\index{single\_inter} \texttt{line\_inter}\index{line\_inter}} 34109\label{sec:3dsingleintersection} 34110 34111See section \ref{sec:single2dintersection} for single points of 34112intersection of objects in the plane. 34113 34114The \texttt{single\_inter} (or the \texttt{line\_inter}) command takes 34115two geometric objects as arguments 34116and returns one of the points of intersection of the two objects. 34117 34118The \texttt{single\_inter} command optionally takes a third 34119argument, which can be a point or a list of points. If the optional 34120argument is 34121a single point, then \texttt{single\_inter} returns the point of 34122intersection \emph{closest} to the optional argument. If the optional 34123argument is a list of points, then \texttt{single\_inter} tries to 34124return a point of intersection not in the list. 34125 34126\noindent 34127Input: 34128\begin{center} 34129 \tt 34130 A := single\_inter(plane(point(0,1,1),point(1,0,1),point(1,1,0)),\\ 34131 line(point(0,0,0),point(1,1,1))) 34132\end{center} 34133then: 34134\begin{center} 34135 \tt 34136 coordinates(A) 34137\end{center} 34138Output: 34139\begin{center} 34140 \tt 34141 [2/3,2/3,2/3] 34142\end{center} 34143 34144\noindent 34145Input: 34146\begin{center} 34147 \tt 34148 B := single\_inter(sphere(point(0,0,0),1),\\ 34149 line(point(0,0,0),point(1,1,1))) 34150\end{center} 34151then: 34152\begin{center} 34153 \tt 34154 coordinates(B) 34155\end{center} 34156Output: 34157\begin{center} 34158 \tt 34159 [1/sqrt(3), 1/sqrt(3), 1/sqrt(3)] 34160\end{center} 34161 34162\noindent 34163Input: 34164\begin{center} 34165 \tt 34166 B1 := single\_inter(sphere(point(0,0,0),1),\\ 34167 line(point(0,0,0),point(1,1,1)), point(-1,0,0)) 34168\end{center} 34169then: 34170\begin{center} 34171 \tt 34172 coordinates(B1) 34173\end{center} 34174Output: 34175\begin{center} 34176 \tt 34177 [-1/sqrt(3), -1/sqrt(3), -1/sqrt(3)] 34178\end{center} 34179 34180\noindent 34181Input: 34182\begin{center} 34183 \tt 34184 C := single\_inter(sphere(point(0,0,0),1),\\ 34185 line(point(1,0,0),point(1,1,1))) 34186\end{center} 34187then: 34188\begin{center} 34189 \tt 34190 coordinates(C) 34191\end{center} 34192Output: 34193\begin{center} 34194 \tt 34195 [1,0,0] 34196\end{center} 34197 34198\noindent 34199Input: 34200\begin{center} 34201 \tt 34202 C1 := single\_inter(sphere(point(0,0,0),1),\\ 34203 line(point(1,0,0),point(1,1,1)),[point(1,0,0)]) 34204\end{center} 34205then: 34206\begin{center} 34207 \tt 34208 coordinates(C1) 34209\end{center} 34210Output: 34211\begin{center} 34212 \tt 34213 [1/3,2/3,2/3] 34214\end{center} 34215 34216\subsection{Find the intersection points of two objects in space: \texttt{inter}\index{inter}} 34217\label{sec:3dintersection} 34218 34219See section \ref{sec:2dintersection} for points of intersection of objects 34220in the plane. 34221 34222The \texttt{inter} command takes 34223two geometric objects as arguments 34224and returns a list of the points of intersection or the curve of 34225intersection of the two objects. 34226 34227The \texttt{inter} command optionally takes a point as a third argument. 34228In this case, \texttt{inter} returns the point of intersection 34229\emph{closest} to this argument. 34230 34231\noindent 34232Input: 34233\begin{center} 34234 \tt 34235 LA:=inter(plane(point(0,1,1),point(1,0,1),point(1,1,0)),\\ 34236 line(point(0,0,0),point(1,1,1))) 34237\end{center} 34238then: 34239\begin{center} 34240 \tt 34241 coordinates(LA) 34242\end{center} 34243Output: 34244\begin{center} 34245 \tt 34246 [[2/3,2/3,2/3]] 34247\end{center} 34248 34249\noindent 34250Input: 34251\begin{center} 34252 \tt 34253 LB:=inter(sphere(point(0,0,0),1),line(point(0,0,0),point(1,1,1))) 34254\end{center} 34255then: 34256\begin{center} 34257 \tt 34258 coordinates(LB) 34259\end{center} 34260Output: 34261\begin{center} 34262 \tt 34263 [[1/sqrt(3),1/sqrt(3),1/sqrt(3)],[-1/sqrt(3),-1/sqrt(3),-1/sqrt(3)]] 34264\end{center} 34265To get just one of the points, use the usual list indices.\\ 34266Input: 34267\begin{center} 34268 \tt 34269 coordinates(LB[0]) 34270\end{center} 34271Output: 34272\begin{center} 34273 \tt 34274 [1/sqrt(3),1/sqrt(3),1/sqrt(3)] 34275\end{center} 34276To get the point closest to $(1/2,1/2,1/2)$, enter\\ 34277Input: 34278\begin{center} 34279 \tt 34280 LB1:=inter(sphere(point(0,0,0),1),\\ 34281 line(point(0,0,0),point(1,1,1)),point(1/2,1/2,1/2)) 34282\end{center} 34283then: 34284\begin{center} 34285 \tt 34286 coordinates(LB1) 34287\end{center} 34288Output: 34289\begin{center} 34290 \tt 34291 [1/sqrt(3),1/sqrt(3),1/sqrt(3)] 34292\end{center} 34293 34294\subsection{Find the midpoint of a segment in space: \texttt{midpoint}\index{midpoint}} 34295\label{sec:3dmidpt} 34296 34297See section \ref{sec:2dmidpt} for midpoints in the plane. 34298 34299The \texttt{midpoint} command takes two points (or a list of two 34300points) as arguments and returns and returns and draws the midpoint 34301of the segment determined by these points. \\ 34302Input: 34303\begin{center} 34304 \tt 34305 MP := midpoint(point(1,4,0),point(1,-2,0)) 34306\end{center} 34307then: 34308\begin{center} 34309 coordinates(MP) 34310\end{center} 34311Output: 34312\begin{center} 34313 \tt 34314 [1,1,0] 34315\end{center} 34316 34317\subsection{Find the isobarycenter of a set of points in space: \texttt{isobarycenter}\index{isobarycenter}} 34318\label{sec:3disobarycenter} 34319 34320See section \ref{sec:2disobarycenter} for isobarycenters of objects in 34321the plane. 34322 34323The \texttt{isobarycenter} command takes a list (or sequence) of 34324points and returns and draws the isobarycenter, which is the barycenter when all 34325the points are equally weighted.\\ 34326Input: 34327\begin{center} 34328 \tt 34329 IB := isobarycenter(point(1,4,0),point(1,-2,0)) 34330\end{center} 34331then: 34332\begin{center} 34333 \tt 34334 coordinates(IB) 34335\end{center} 34336Output: 34337\begin{center} 34338 [1,1,0] 34339\end{center} 34340 34341\subsection{Find the barycenter of a set of points in space: \texttt{barycenter}\index{barycenter}} 34342\label{sec:3dbarycenter} 34343 34344See section \ref{sec:2dbarycenter} for barycenters of objects in 34345the plane. 34346 34347The \texttt{barycenter} command returns and draws the barycenter of a set of 34348weighted points. If the sum of the weights is zero, then this command 34349returns an error. 34350 34351The points and their weights (real numbers) can be 34352given in two different ways. 34353\begin{enumerate} 34354 \item A sequence of lists of length two.\\ 34355 The first element of each list is a point and the second element is 34356 the weight of the point. 34357 \item A matrix with two columns.\\ 34358 The first column of the matrix contains the points and the 34359 second column contains the corresponding weights. 34360 % \item A matrix with two rows and more than two columns.\\ 34361 % The first row contains the points and the second column the 34362 % corresponding weights. 34363\end{enumerate} 34364Input: 34365\begin{center} 34366 \tt 34367 BC := barycenter([point(1,4,0),1],[point(1,-2,0),1]) 34368\end{center} 34369or: 34370\begin{center} 34371 \tt 34372 BC := barycenter([[point(1,4,0),1],[point(1,-2,0),1]]) 34373\end{center} 34374then: 34375\begin{center} 34376 \tt 34377 coordinates(BC) 34378\end{center} 34379Output: 34380\begin{center} 34381 \tt 34382 [1,1,0] 34383\end{center} 34384 34385\section{Lines in space} 34386 34387\subsection{Lines and directed lines in space: \texttt{line}\index{line}} 34388\label{sec:3dlines} 34389 34390See section \ref{sec:2dlines} for lines in the plane. 34391 34392The \texttt{line} command returns and draws a directed line given one of the 34393following types of arguments: 34394\begin{itemize} 34395 \item Two points or a list of two points.\\ 34396 The direction of the line is from the first point 34397 to the second point.\\ 34398 Input: 34399 \begin{center} 34400 \tt 34401 line([0,3,0],point(3,0,3)) 34402 \end{center} 34403 Output: 34404 \begin{center} 34405 \includegraphics[width=0.5\textwidth]{xcas-3dline1.png} 34406 \end{center} 34407 \item A point and direction vector (in the form 34408 \texttt{[$u_1$,$u_2$,$u_3$]}).\\ 34409 The direction of the line is given by the direction vector.\\ 34410 Input: 34411 \begin{center} 34412 \tt 34413 line([0,3,0],[3,0,3]) 34414 \end{center} 34415 Output: 34416 \begin{center} 34417 \includegraphics[width=0.5\textwidth]{xcas-3dline2.png} 34418 \end{center} 34419 \item Two equations for planes.\\ 34420 The direction of the line is given by the cross-product of the 34421 normals for the planes. For example, the intersection of the planes 34422 $x=y$ (normal $(1,-1,0)$) and $y=z$ (normal $(0,1,-1)$) will be 34423 $(1,-1,0)\times (0,1,-1) = (1,1,1)$. 34424 Input: 34425 \begin{center} 34426 \tt 34427 line(x=y, y=z) 34428 \end{center} 34429 Output: 34430 \begin{center} 34431 \includegraphics[width=0.5\textwidth]{xcas-3dline3.png} 34432 \end{center} 34433\end{itemize} 34434 34435\subsection{Half lines in space: \texttt{half\_line}\index{half\_line}} 34436\label{sec:3dhalflines} 34437 34438See section \ref{sec:2dhalflines} for half-lines in the plane. 34439 34440The \texttt{half\_line} command takes as argument two points. 34441 34442\texttt{half\_line} returns and draws the ray from the first point 34443through the second.\\ 34444Input: 34445\begin{center} 34446 \tt 34447 half\_line(point(0,0,0),point(1,1,1)) 34448\end{center} 34449Output: 34450\begin{center} 34451 \includegraphics[width=0.5\textwidth]{xcas-3dhalfline.png} 34452\end{center} 34453 34454\subsection{Segments in space: \texttt{segment}\index{segment}} 34455\label{sec:3dsegments} 34456 34457See section \ref{sec:2dsegments} for segments in the plane. 34458 34459The \texttt{segment} command takes as arguments two points. 34460 34461\texttt{segment} returns and draws the corresponding line segment. 34462 34463\noindent 34464Input: 34465\begin{center} 34466 \tt 34467 segment(point(0,0,0),point(1,1,1)) 34468\end{center} 34469Output: 34470\begin{center} 34471 \includegraphics[width=0.5\textwidth]{xcas-3dsegment.png} 34472\end{center} 34473 34474\subsection{Vectors in space: \texttt{vector}\index{vector}} 34475\label{sec:3dvectors} 34476 34477See section \ref{sec:2dvectors} for vectors in the plane. 34478 34479The \texttt{vector} command returns and draws vectors, given 34480one of the following types of arguments: 34481\begin{itemize} 34482 \item A list of the coordinates of a vector. The vector is drawn 34483 beginning at the origin.\\ 34484 Input: 34485 \begin{center} 34486 \tt 34487 vector([1,2,3]) 34488 \end{center} 34489 Output: 34490 \begin{center} 34491 \includegraphics[width=0.5\textwidth]{xcas-3dvector1.png} 34492 \end{center} 34493 \item Two points or two lists of coordinates for points.\\ 34494 The vector is drawn from the first point to the second.\\ 34495 Input: 34496 \begin{center} 34497 \tt 34498 vector(point(-1,0,0),point(0,1,2)) 34499 \end{center} 34500 or: 34501 \begin{center} 34502 \tt 34503 vector([-1,0,0],[0,1,2]) 34504 \end{center} 34505 Output: 34506 \begin{center} 34507 \includegraphics[width=0.5\textwidth]{xcas-3dvector2.png} 34508 \end{center} 34509 \item A point and a vector.\\ 34510 The vector is drawn beginning at the point.\\ 34511 Input: 34512 \begin{center} 34513 \tt 34514 V := vector([-1,0,0],[0,1,2]) 34515 \end{center} 34516 then: 34517 \begin{center} 34518 \tt 34519 vector(point(-1,2,0),V) 34520 \end{center} 34521 Output: 34522 \begin{center} 34523 \includegraphics[width=0.5\textwidth]{xcas-3dvector3.png} 34524 \end{center} 34525\end{itemize} 34526 34527\subsection{Parallel lines and planes in space: \texttt{parallel}\index{parallel}} 34528\label{sec:3dparallel} 34529 34530See section \ref{sec:2dparallel} for parallel lines in the plane. 34531 34532The \texttt{parallel} command returns and draws a line or plane 34533depending on the arguments. The possible arguments are: 34534\begin{itemize} 34535 \item A point \texttt{A} and a line \texttt{L}.\\ 34536 \texttt{parallel(A,L)} returns and draws the line through \texttt{A} 34537 parallel to \texttt{L}.\\ 34538 Input: 34539 \begin{center} 34540 \tt 34541 parallel(point(1,1,1),line(point(0,0,0),point(0,0,1))) 34542 \end{center} 34543 Output: 34544 \begin{center} 34545 \includegraphics[width=0.5\textwidth]{xcas-3dparallel.png} 34546 \end{center} 34547 \item Two non-parallel lines \texttt{L1} and \texttt{L2}.\\ 34548 \texttt{parallel(L1,L2)} returns and draws the plane containing 34549 \texttt{L2} that is parallel to \texttt{L1}.\\ 34550 Input: 34551 \begin{center} 34552 \tt 34553 parallel(line(point(1,0,0),point(0,1,0)),\\ 34554 line(point(0,0,0),point(0,0,1))) 34555 \end{center} 34556 Output: 34557 \begin{center} 34558 \includegraphics[width=0.5\textwidth]{xcas-3dparallel2.png} 34559 \end{center} 34560 \item A point \texttt{A} and a plane \texttt{P}.\\ 34561 \texttt{parallel(A,P)} returns and draws the plane through 34562 \texttt{A} that is parallel to \texttt{A}.\\ 34563 Input: 34564 \begin{center} 34565 \tt 34566 parallel(point(0,0,0),plane(point(1,0,0),point(0,1,0),point(0,0,1))) 34567 \end{center} 34568 Output: 34569 \begin{center} 34570 \includegraphics[width=0.5\textwidth]{xcas-3dparallel3.png} 34571 \end{center} 34572 \item A point \texttt{A} and two non-parallel lines \texttt{L1} and 34573 \texttt{L2}.\\ 34574 \texttt{parallel(A,L1,L2)} returns and draws the plane through 34575 \texttt{A} that is parallel to \texttt{L1} and \texttt{L2}.\\ 34576 Input: 34577 \begin{center} 34578 \tt 34579 parallel(point(1,1,1),line(point(0,0,0),point(0,0,1)),\\ 34580 line(point(1,0,0),point(0,1,0))) 34581 \end{center} 34582 Output: 34583 \begin{center} 34584 \includegraphics[width=0.5\textwidth]{xcas-3dparallel4.png} 34585 \end{center} 34586\end{itemize} 34587 34588\subsection{Perpendicular lines and planes in space: \texttt{perpendicular}\index{perpendicular}} 34589\label{sec:3dperp} 34590 34591See section \ref{sec:2dperp} for perpendicular lines in the plane. 34592 34593The \texttt{perpendicular} command returns and draws a line or plane, 34594depending on the arguments. The possible arguments are: 34595\begin{itemize} 34596 \item A point \texttt{A} and a line \texttt{L}.\\ 34597 \texttt{perpendicular(A,L)} returns and draws the line through 34598 \texttt{A} that is perpendicular to \texttt{L}.\\ 34599 Input: 34600 \begin{center} 34601 \tt 34602 perpendicular(point(0,0,0),line(point(1,0,0),point(0,1,0))) 34603 \end{center} 34604 Output: 34605 \begin{center} 34606 \includegraphics[width=0.5\textwidth]{xcas-3dperp1.png} 34607 \end{center} 34608 \item A line \texttt{L} and a plane \texttt{P}.\\ 34609 \texttt{perpendicular(L,P)} returns and draws the plane containing 34610 \texttt{L} that is perpendicular to \texttt{P}.\\ 34611 Input: 34612 \begin{center} 34613 \tt 34614 perpendicular(line(point(0,0,0),point(1,1,0)),\\ 34615 plane(point(1,0,0),point(0,1,0),point(0,0,1))) 34616 \end{center} 34617 Output: 34618 \begin{center} 34619 \includegraphics[width=0.5\textwidth]{xcas-3dperp2.png} 34620 \end{center} 34621\end{itemize} 34622 34623\subsection{Planes orthogonal to lines and lines orthogonal to planes in space: \texttt{orthogonal}\index{orthogonal}} 34624\label{sec:3dorthogonal} 34625 34626The \texttt{orthogonal} command returns and draws a line or plane, 34627depending on the arguments. The possible arguments are: 34628\begin{itemize} 34629 \item A point \texttt{A} and a line \texttt{L}.\\ 34630 \texttt{orthogonal(A,L)} returns and draws the plane through 34631 \texttt{A} orthogonal to \texttt{L}.\\ 34632 Input: 34633 \begin{center} 34634 \tt 34635 orthogonal(point(0,0,0),line(point(1,0,0),point(0,1,0))) 34636 \end{center} 34637 Output: 34638 \begin{center} 34639 \includegraphics[width=0.5\textwidth]{xcas-3dorthog1.png} 34640 \end{center} 34641 \item A line \texttt{L} and a plane \texttt{P}.\\ 34642 \texttt{perpendicular(L,P)} returns and draws the plane containing 34643 \texttt{L} that is perpendicular to \texttt{P}.\\ 34644 Input: 34645 \begin{center} 34646 \tt 34647 perpendicular(line(point(0,0,0),point(1,1,0)),\\ 34648 plane(point(1,0,0),point(0,1,0),point(0,0,1))) 34649 \end{center} 34650 Output: 34651 \begin{center} 34652 \includegraphics[width=0.5\textwidth]{xcas-3dorthog2.png} 34653 \end{center} 34654\end{itemize} 34655 34656\subsection{Common perpendiculars to lines in space: \texttt{common\_perpendicular}\index{common\_perpendicular}} 34657 34658The \texttt{common\_perpendicular} command takes as arguments two lines. 34659 34660\texttt{common\_perpendicular} returns and draws the common 34661perpendicular to the two lines. 34662 34663\noindent 34664Input: 34665\begin{center} 34666 \tt 34667 L1 := line(point(1,1,0),point(0,1,1));\\ 34668 L2 := line(point(0,-1,0),point(1,-1,1)) 34669\end{center} 34670then: 34671\begin{center} 34672 \tt 34673 common\_perpendicular(L1,L2) 34674\end{center} 34675Output: 34676\begin{center} 34677 \includegraphics[width=0.5\textwidth]{xcas-3dcomperp.png} 34678\end{center} 34679 34680\section{Planes in space} 34681 34682See also sections \ref{sec:3dperp} and \ref{sec:3dorthogonal} 34683for planes perpendicular and orthogonal to lines and planes. 34684 34685\subsection{Planes in space: \texttt{plane}\index{plane}} 34686 34687The \texttt{plane} command draws and returns a plane. It takes as 34688argument one of the following: 34689\begin{itemize} 34690 \item Three points. 34691 \item A point and a line. 34692 \item The equation of a plane. 34693\end{itemize} 34694Input: 34695\begin{center} 34696 \tt 34697 plane(point(0,0,5),point(0,5,0),point(0,0,5)) 34698\end{center} 34699or: 34700\begin{center} 34701 \tt 34702 plane(point(0,0,5),line(point(0,5,0),point(0,0,5))) 34703\end{center} 34704or: 34705\begin{center} 34706 \tt 34707 plane(x + y + z = 5) 34708\end{center} 34709Output: 34710\begin{center} 34711 \includegraphics[width=0.5\textwidth]{xcas-3dplane1.png} 34712\end{center} 34713 34714\subsection{The bisector plane in space: \texttt{perpen\_bisector}\index{perpen\_bisector}} 34715\label{sec:3dperpbis} 34716 34717See section \ref{sec:2dperpbis} for perpendicular bisectors in the 34718plane. 34719 34720The \texttt{perpen\_bisector} command takes as argument a segment or 34721two points. 34722 34723\texttt{perpen\_bisector} returns and draws the perpendicular bisector 34724plane of the segment. 34725 34726\noindent 34727Input: 34728\begin{center} 34729 \tt 34730 perpen\_bisector(point(0,0,0),point(4,4,4)) 34731\end{center} 34732or: 34733\begin{center} 34734 \tt 34735 perpen\_bisector(segment([0,0,0],[4,4,4]) 34736\end{center} 34737Output: 34738\begin{center} 34739 \includegraphics[width=0.5\textwidth]{xcas-3dplane2.png} 34740\end{center} 34741 34742\subsection{Tangent planes in space: \texttt{tangent}\index{tangent}} 34743\label{sec:3dtangents} 34744 34745See section \ref{sec:2dtangents} for tangents in the plane. 34746 34747The \texttt{tangent} command takes as argument a geometric object and 34748a point on the object. 34749 34750\texttt{tangent} draws and returns the plane through the point tangent 34751to the object. 34752 34753\noindent 34754Input: 34755\begin{center} 34756 \tt 34757 S: = sphere([0,0,0],3) 34758\end{center} 34759then: 34760\begin{center} 34761 \tt 34762 tangent(S,[2,2,1]) 34763\end{center} 34764Output: 34765\begin{center} 34766 \includegraphics[width=0.5\textwidth]{xcas-3dspheretan.png} 34767\end{center} 34768 34769If the geometric object is the graph of a function, then the second 34770argument is a point in the domain of the function; the corresponding point 34771on the graph will be used.\\ 34772Input: 34773\begin{center} 34774 \tt 34775 G:=plotfunc(x\^{}2 + y\^{}2, [x,y]) 34776\end{center} 34777then: 34778\begin{center} 34779 \tt 34780 tangent(G,[2,2]) 34781\end{center} 34782% or: 34783% \begin{center} 34784% \tt 34785% tangent(G,[2,2,8]) 34786% \end{center} 34787Output: 34788\begin{center} 34789 \includegraphics[width=0.5\textwidth]{xcas-3dgraphtan.png} 34790\end{center} 34791 34792\section{Triangles in space} 34793\label{sec:3dtriangles} 34794 34795\subsection{Draw a triangle in space: \texttt{triangle}\index{triangle}} 34796\label{sec:3dtricommand} 34797 34798See section \ref{sec:2dtricommand} for the \texttt{triangle} command 34799in the plane. 34800 34801The \texttt{triangle} takes as arguments three points. 34802 34803\texttt{triangle} returns and draws the triangle determined by these 34804points. 34805 34806\noindent 34807Input: 34808\begin{center} 34809 \tt 34810 A := point(0,0,0); B := point(3,3,3); C := point(0,3,0) 34811\end{center} 34812then: 34813\begin{center} 34814 \tt 34815 triangle(A,B,C) 34816\end{center} 34817Output: 34818\begin{center} 34819 \includegraphics[width=0.5\textwidth]{xcas-3dtriangle.png} 34820\end{center} 34821 34822\subsection{Isosceles triangles in space: \texttt{isosceles\_triangle}\index{isosceles\_triangle}} 34823\label{sec:3disotriangles} 34824 34825See section \ref{sec:2disotriangles} for isosceles triangles in the 34826plane. 34827 34828The \texttt{isosceles\_triangle} command returns and draws an 34829isosceles triangle. It takes as arguments one of the following: 34830\begin{itemize} 34831 \item Three points, \texttt{A}, \texttt{B} and \texttt{P}.\\ 34832 The first two points \texttt{A} and \texttt{B} are vertices of the 34833 triangle, the third point \texttt{P} determines the plane and 34834 orientation of the triangle. The orientation is so that angle 34835 \texttt{BAP} is positive, and the equal interior angles of the 34836 isosceles triangle are determined by angle \texttt{ABP}.\\ 34837 Input: 34838 \begin{center} 34839 \tt 34840 A := point(0,0,0); B := point(3,3,3); P := point(0,0,3) 34841 \end{center} 34842 then: 34843 \begin{center} 34844 \tt 34845 isosceles\_triangle(A,B,P); 34846 \end{center} 34847 Output: 34848 \begin{center} 34849 \includegraphics[width=0.5\textwidth]{xcas-3disotriangle.png} 34850 \end{center} 34851 \item Two points, \texttt{A} and \texttt{B}, and a list consisting 34852 of a point \texttt{P} and a real number \texttt{c}.\\ 34853 The points \texttt{A} and \texttt{B} are vertices of the triangle 34854 and \texttt{P} determines the plane and orientation of the triangle 34855 as above. The number \texttt{c} is the measure of the equal 34856 interior angles.\\ 34857 Input: 34858 \begin{center} 34859 \tt 34860 isosceles\_triangle(A,B,[P,3*pi/4]) 34861 \end{center} 34862 Output: 34863 \begin{center} 34864 \includegraphics[width=0.5\textwidth]{xcas-3disotriangle2.png} 34865 \end{center} 34866\end{itemize} 34867 34868\texttt{isosceles\_triangle} can take an optional fourth argument, 34869which is a variable which will be assigned to the third vertex of the 34870triangle.\\ 34871Input: 34872\begin{center} 34873 \tt 34874 isosceles\_triangle(A,B,[P,3*pi/4],C) 34875\end{center} 34876then: 34877\begin{center} 34878 \tt 34879 coordinates(C) 34880\end{center} 34881Output: 34882\begin{center} 34883 \tt 34884 [(-3*sqrt(2) - 3)/2, (-3*sqrt(2) -3)/2,\\ 34885 (-3*sqrt(2) + 6)/2] 34886\end{center} 34887 34888\subsection{Right triangles in space: \texttt{right\_triangle}\index{right\_triangle}} 34889\label{sec:3dright} 34890 34891See section \ref{sec:2dright} for right triangles in the plane. 34892 34893The \texttt{right\_triangle} command returns and draws a 34894right triangle. It takes as arguments one of the following: 34895\begin{itemize} 34896 \item Three points, \texttt{A}, \texttt{B} and \texttt{P}.\\ 34897 \texttt{right\_triangle(A,B,P)} returns and draws the right 34898 triangle \texttt{BAC} with the right angle at vertex \texttt{A}. 34899 The first two points \texttt{A} and \texttt{B} are vertices of the 34900 triangle, the third point \texttt{P} determines the plane and 34901 orientation of the triangle. The orientation is so that angle 34902 \texttt{BAP} is positive. The length of \texttt{AC} equals the 34903 length of \texttt{AP}.\\ 34904 Input: 34905 \begin{center} 34906 \tt 34907 A := point(0,0,0); B := point(3,3,3);\\ 34908 P := point(0,0,3) 34909 \end{center} 34910 then: 34911 \begin{center} 34912 \tt 34913 right\_triangle(A,B,P); 34914 \end{center} 34915 Output: 34916 \begin{center} 34917 \includegraphics[width=0.5\textwidth]{xcas-3drttriangle.png} 34918 \end{center} 34919 \item Two points, \texttt{A} and \texttt{B}, and a list consisting 34920 of a point \texttt{P} and a real number \texttt{k}.\\ 34921 \texttt{triangle\_rectangle(A,B,[P,k])} returns and draws the right 34922 triangle \texttt{BAC} with the right angle at vertex \texttt{A}. 34923 The first two points \texttt{A} and \texttt{B} are vertices of the 34924 triangle, the third point \texttt{P} determines the plane and 34925 orientation of the triangle as above. The length of \texttt{AC} 34926 is \texttt{|k|} times the length of \texttt{AP}. Angle \texttt{BAC} 34927 has the same orientation as \texttt{BAP} if \texttt{k} is positive, 34928 angle \texttt{BAC} has opposite orientation as \texttt{BAP} if 34929 \texttt{k} is negative. So $\tan(\beta) = \texttt{k}$ if $\beta$ is 34930 the angle \texttt{CAB}.\\ 34931 Input: 34932 \begin{center} 34933 \tt 34934 right\_triangle(A,B,[P,2]) 34935 \end{center} 34936 Output: 34937 \begin{center} 34938 \includegraphics[width=0.5\textwidth]{xcas-3drttriangle2.png} 34939 \end{center} 34940 Input: 34941 \begin{center} 34942 \tt 34943 right\_triangle(A,B,[P,-2]) 34944 \end{center} 34945 Output: 34946 \begin{center} 34947 \includegraphics[width=0.5\textwidth]{xcas-3drttriangle3.png} 34948 \end{center} 34949\end{itemize} 34950 34951\texttt{right\_triangle} can take an optional fourth argument, 34952which is a variable which will be assigned to the third vertex of the 34953triangle.\\ 34954Input: 34955\begin{center} 34956 \tt 34957 right\_triangle(A,B,[P,2],C) 34958\end{center} 34959then: 34960\begin{center} 34961 \tt 34962 coordinates(C) 34963\end{center} 34964Output: 34965\begin{center} 34966 \tt 34967 [-3*sqrt(2), -3*sqrt(2), 6*sqrt(2)] 34968\end{center} 34969 34970\subsection{Equilateral triangles in space: \texttt{equilateral\_triangle}\index{equilateral\_triangle}} 34971\label{sec:3dequi} 34972 34973See section \ref{sec:2dequi} for equilateral triangles in the plane. 34974 34975The \texttt{equilateral\_triangle} command takes as arguments three 34976points, \texttt{A}, \texttt{B} and \texttt{P}. 34977 34978\texttt{equilateral\_triangle} returns and draws the equilateral triangle 34979\texttt{ABC}, where \texttt{C} is in the same half plane as 34980\texttt{P}.\\ 34981Input: 34982\begin{center} 34983 \tt 34984 A := point(0,0,0); B := point(3,3,3);\\ 34985 P := point(0,0,3) 34986\end{center} 34987then: 34988\begin{center} 34989 \tt 34990 equilateral\_triangle(A,B,P) 34991\end{center} 34992Output: 34993\begin{center} 34994 \includegraphics[width=0.5\textwidth]{xcas-3dequitriangle.png} 34995\end{center} 34996 34997\texttt{triangle\_rectangle} can take an optional fourth argument, 34998which is a variable which will be assigned to the third vertex of the 34999triangle.\\ 35000Input: 35001\begin{center} 35002 \tt 35003 triangle\_rectangle(A,B,P,C) 35004\end{center} 35005then: 35006\begin{center} 35007 \tt 35008 simplify(coordinates(C)) 35009\end{center} 35010Output: 35011\begin{center} 35012 \tt 35013 [(-3*sqrt(6)+6)/4,(-3*sqrt(6)+6)/4,(3*sqrt(6)+3)/2] 35014\end{center} 35015 35016\section{Quadrilaterals in space} 35017\label{sec:3dquad} 35018 35019See section \ref{sec:2dquad} for quadrilaterals in the plane. 35020 35021\subsection{Squares in space: \texttt{square}\index{square}} 35022\label{sec:3dsquares} 35023 35024See section \ref{sec:2dsquares} for squares in the plane. 35025 35026The \texttt{square} command takes as arguments three points, 35027\texttt{A}, \texttt{B} and \texttt{P}. 35028 35029\texttt{square} returns and draws and returns the square with one side 35030\texttt{AB} and the remaining sides in the same half-plane as 35031\texttt{P}.\\ 35032Input: 35033\begin{center} 35034 \tt 35035 A := point(0,0,0); B := point(3,3,3); \\ 35036 P := point(0,0,3); 35037\end{center} 35038then: 35039\begin{center} 35040 \tt 35041 square(A,B,P) 35042\end{center} 35043Output: 35044\begin{center} 35045 \includegraphics[width=0.5\textwidth]{xcas-3dsquare.png} 35046\end{center} 35047 35048The \texttt{square} command also optionally takes two more arguments, 35049variable names to assign to the two new vertices.\\ 35050Input: 35051\begin{center} 35052 \tt 35053 square(A,B,P,C,D) 35054\end{center} 35055then: 35056\begin{center} 35057 \tt 35058 cooridinates(C); coordinates(D) 35059\end{center} 35060Output: 35061\begin{center} 35062 \tt 35063 [-3*sqrt(2)/2+3,-3*sqrt(2)/2+3,3*sqrt(2)+3],\\{} 35064 [-3*sqrt(2)/2,-3*sqrt(2)/2,3*sqrt(2)] 35065\end{center} 35066 35067\subsection{Rhombuses in space: \texttt{rhombus}\index{rhombus}} 35068\label{sec:3drhombuses} 35069 35070See section \ref{sec:2drhombuses} for rhombuses in the plane. 35071 35072The \texttt{rhombus} command returns and draws a rhombus. 35073It takes as arguments one of the following: 35074\begin{itemize} 35075 \item Three points, \texttt{A}, \texttt{B} and \texttt{P}.\\ 35076 The first two points \texttt{A} and \texttt{B} are vertices of the 35077 triangle, the third point \texttt{P} determines the plane and 35078 orientation of the rhombus. The orientation is so that angle 35079 \texttt{BAP} is positive. The command returns and draws rhombus 35080 \texttt{ABCD}, where \texttt{D} is on the ray \texttt{AP} (and the 35081 length of \texttt{AD} equals the length of \texttt{AB}).\\ 35082 Input: 35083 \begin{center} 35084 \tt 35085 A := point(0,0,0); B := point(3,3,3);\\ 35086 P := point(0,0,3) 35087 \end{center} 35088 then: 35089 \begin{center} 35090 \tt 35091 rhombus(A,B,P) 35092 \end{center} 35093 Output: 35094 \begin{center} 35095 \includegraphics[width=0.5\textwidth]{xcas-3drhombus.png} 35096 \end{center} 35097 \item Two points, \texttt{A} and \texttt{B}, and a list consisting 35098 of a point \texttt{P} and a real number \texttt{a}.\\ 35099 The points \texttt{A} and \texttt{B} are vertices of the rhombus 35100 and \texttt{P} determines the plane and orientation of the rhombus 35101 as above. The command returns and draws rhombus \texttt{ABCD}, 35102 where angle \texttt{BAD} equals \texttt{a}.\\ 35103 Input: 35104 \begin{center} 35105 \tt 35106 rhombus(A,B,[P,pi/3]) 35107 \end{center} 35108 Output: 35109 \begin{center} 35110 \includegraphics[width=0.5\textwidth]{xcas-3drhombus2.png} 35111 \end{center} 35112\end{itemize} 35113 35114\texttt{rhombus} can take optional fourth and fifth 35115arguments, which are variable names assigned to vertices 35116\texttt{C} and \texttt{D}.\\ 35117Input: 35118\begin{center} 35119 \tt 35120 rhombus(A,B,[P,pi/3],C,D) 35121\end{center} 35122then: 35123\begin{center} 35124 \tt 35125 simplify(coordinates(C)); simplify(coordinates(D)) 35126\end{center} 35127Output: 35128\begin{center} 35129 \tt 35130 [(-3*sqrt(6)+18)/4,(-3*sqrt(6)+18)/4,(3*sqrt(6)+9)/2],\\{} 35131 [(-3*sqrt(6)+6)/4,(-3*sqrt(6)+6)/4,(3*sqrt(6)+3)/2] 35132\end{center} 35133 35134\subsection{Rectangles in space: \texttt{rectangle}\index{rectangle}} 35135\label{sec:3drect} 35136 35137See section \ref{sec:2drect} for rectangles in the plane. 35138 35139The \texttt{rectangle} command returns and draws a 35140rectangle. It takes as arguments one of the following: 35141\begin{itemize} 35142 \item Three points, \texttt{A}, \texttt{B} and \texttt{P}.\\ 35143 \texttt{rectangle(A,B,P)} returns and draws the rectangle 35144 \texttt{ABCD}. 35145 The first two points \texttt{A} and \texttt{B} are vertices of the 35146 rectangle, the third point \texttt{P} determines the plane and 35147 orientation of the rectangle. The orientation is so that angle 35148 \texttt{BAP} is positive. The length of side \texttt{AD} equals the 35149 length of \texttt{AP}.\\ 35150 Input: 35151 \begin{center} 35152 \tt 35153 A := point(0,0,0); B := point(3,3,3);\\ 35154 P := point(0,0,3) 35155 \end{center} 35156 then: 35157 \begin{center} 35158 \tt 35159 rectangle(A,B,P) 35160 \end{center} 35161 Output: 35162 \begin{center} 35163 \includegraphics[width=0.5\textwidth]{xcas-3drect.png} 35164 \end{center} 35165 \item Two points, \texttt{A} and \texttt{B}, and a list consisting 35166 of a point \texttt{P} and a real number \texttt{k}.\\ 35167 \texttt{rectangle(A,B,[P,k])} returns and draws the rectangle 35168 \texttt{ABCD}. The first two points \texttt{A} and \texttt{B} are vertices of the 35169 rectangle, the third point \texttt{P} determines the plane and 35170 orientation of the rectangle as above. The length of \texttt{AD} 35171 is \texttt{|k|} times the length of \texttt{AB}. Angle \texttt{BAD} 35172 has the same orientation as \texttt{BAP} if \texttt{k} is positive, 35173 angle \texttt{BAD} has opposite orientation as \texttt{BAP} if 35174 \texttt{k} is negative.\\ 35175 Input: 35176 \begin{center} 35177 \tt 35178 rectangle(A,B,[P,1/2]) 35179 \end{center} 35180 Output: 35181 \begin{center} 35182 \includegraphics[width=0.5\textwidth]{xcas-3drect2.png} 35183 \end{center} 35184\end{itemize} 35185 35186\texttt{rectangle} takes optional fourth and fifth arguments, 35187which are variables assigned to vertices \texttt{C} and \texttt{D}.\\ 35188Input: 35189\begin{center} 35190 \tt 35191 rectangle(A,B,P,C,D) 35192\end{center} 35193then: 35194\begin{center} 35195 \tt 35196 simplify(coordinates(C)), simplify(coordinates(D)) 35197\end{center} 35198Output: 35199\begin{center} 35200 \tt 35201 [-(sqrt(6))/2,-(sqrt(6))/2,sqrt(6)],\\{} 35202 [(-(sqrt(6))+6)/2,(-(sqrt(6))+6)/2,sqrt(6)+3] 35203\end{center} 35204 35205\subsection{Parallelograms in space: \texttt{parallelogram}\index{parallelogram}} 35206\label{sec:3dparallelograms} 35207 35208See section \ref{sec:2dparallelograms} for parallelograms in the plane. 35209 35210The \texttt{parallelogram} command takes three points as arguments. 35211 35212The \texttt{parallelogram} command returns and draws a parallelogram. 35213If the arguments are \texttt{A}, \texttt{B} and \texttt{C}, then the 35214parallelogram has the form \texttt{ABCD}.\\ 35215Input: 35216\begin{center} 35217 \tt 35218 A := point(0,0,0); B := point(3,3,3);\\ 35219 C := point(0,0,3) 35220\end{center} 35221then: 35222\begin{center} 35223 \tt 35224 parallelogram(A,B,C) 35225\end{center} 35226Output: 35227\begin{center} 35228 \includegraphics[width=0.5\textwidth]{xcas-3dpar.png} 35229\end{center} 35230 35231The \texttt{parallelogram} command takes an optional fourth argument, 35232which is a variable the fourth vertex is assigned to.\\ 35233Input: 35234\begin{center} 35235 \tt 35236 parallelogram(A,B,C,D) 35237\end{center} 35238then: 35239\begin{center} 35240 \tt 35241 coordinates(D) 35242\end{center} 35243Output: 35244\begin{center} 35245 \tt 35246 [-3,-3,0] 35247\end{center} 35248 35249\subsection{Arbitrary quadrilaterals in space: \texttt{quadrilateral}\index{quadrilateral}} 35250\label{sec:3dquads} 35251 35252See section \ref{sec:2dquads} for quadrilaterals in the plane. 35253 35254The \texttt{quadrilateral} command takes as arguments four points.\\ 35255\texttt{quadrilateral} returns and draws the quadrilateral whose 35256vertices are the given points.\\ 35257Input: 35258\begin{center} 35259 \tt 35260 quadrilateral(point(0,0,0),point(0,1,0),point(0,2,2),point(1,0,2)) 35261\end{center} 35262Output: 35263\begin{center} 35264 \includegraphics[width=0.5\textwidth]{xcas-3dquad.png} 35265\end{center} 35266 35267\section{Polygons in space} 35268\label{sec:3dpolygons} 35269 35270See section \ref{sec:2dpolygons} for polygons in the plane. 35271 35272\subsection{Hexagons in space: \texttt{hexagon}\index{hexagon}} 35273\label{sec:3dhex} 35274 35275See section \ref{sec:2dhex} for hexagons in the plane. 35276 35277The \texttt{hexagon} command takes as arguments 35278three points, \texttt{A}, \texttt{B} and \texttt{P}. 35279 35280\texttt{hexagon} returns and draws a regular hexagon. 35281The first two points \texttt{A} and \texttt{B} are vertices of the 35282hexagon, the third point \texttt{P} determines the plane and 35283orientation of the rectangle.\\ 35284Input: 35285\begin{center} 35286 \tt 35287 A := point(0,0,0); B := point(3,3,3);\\ 35288 P := point(0,0,3) 35289\end{center} 35290then: 35291\begin{center} 35292 \tt 35293 hexagon(A,B,P) 35294\end{center} 35295 Output: 35296 \begin{center} 35297 \includegraphics[width=0.5\textwidth]{xcas-3dhex.png} 35298 \end{center} 35299 35300\texttt{hexagon} takes four optional arguments, 35301which are variables assigned to the unnamed vertices.\\ 35302Input: 35303\begin{center} 35304 \tt 35305 hexagon(A,B,P,C,D,E,F) 35306\end{center} 35307then: 35308\begin{center} 35309 \tt 35310 simplify(coordinates(C)) 35311\end{center} 35312Output: 35313\begin{center} 35314 \tt 35315[(-3*sqrt(6)+18)/4,(-3*sqrt(6)+18)/4,(3*sqrt(6)+9)/2] 35316\end{center} 35317 35318\subsection{Regular polygons in space: \texttt{isopolygon}\index{isopolygon}} 35319\label{sec:3dregpoly} 35320 35321See section \ref{sec:2dregpoly} for regular polygons in the plane. 35322 35323The \texttt{isopolygon} command takes as arguments three points and an 35324integer. 35325 35326\texttt{isopolygon} returns and draws a regular polygon. The first 35327two points are adjacent vertices of the polygon, the third determines 35328the plane and orientation of the polygon. The fourth argument, the 35329integer, determines the number of sides of the polygon. If the fourth 35330argument is positive, then it is the number of sides of the polygon, 35331which is positively oriented. If the fourth argument is negative, 35332then the polygon is negatively oriented and the absolute value of the 35333argument is the number of sides.\\ 35334Input: 35335\begin{center} 35336 \tt 35337 A := point(0,0,0); B := point(3,3,3); \\ 35338 P := point(0,0,3) 35339\end{center} 35340then: 35341\begin{center} 35342 \tt 35343 isopolygon(A,B,P,5) 35344\end{center} 35345Output: 35346\begin{center} 35347 \includegraphics[width=0.5\textwidth]{xcas-3disopoly.png} 35348\end{center} 35349Input: 35350\begin{center} 35351 \tt 35352 isopolygon(A,B,P,-5) 35353\end{center} 35354Output: 35355\begin{center} 35356 \includegraphics[width=0.5\textwidth]{xcas-3disopoly2.png} 35357\end{center} 35358 35359\subsection{General polygons in space: \texttt{polygon}\index{polygon}} 35360\label{sec:3dgenpolygon} 35361 35362See section \ref{sec:2dgenpolygon} for general polygons in the plane. 35363 35364The \texttt{polygon} command takes as arguments a sequence of points. 35365 35366\texttt{polygon} returns and draws the polygon whose vertices are the 35367given points. 35368 35369\noindent 35370Input: 35371\begin{center} 35372 \tt 35373 A := point(0,0,0); B := point(3,3,3);\\ 35374 C := point(0,0,3); D := point(-3,-3,0);\\ 35375 E := point(-3,-3,-3) 35376\end{center} 35377then: 35378\begin{center} 35379 \tt 35380 polygon(A,B,C,D,E) 35381\end{center} 35382Output: 35383\begin{center} 35384 \includegraphics[width=0.5\textwidth]{xcas-3dpoly.png} 35385\end{center} 35386 35387\subsection{Polygonal lines in space: \texttt{open\_polygon}\index{open\_polygon}} 35388\label{sec:3dpolylines} 35389 35390See section \ref{sec:2dpolylines} for polygonal lines in the plane. 35391 35392The \texttt{open\_polygon} command takes as arguments a sequence of points. 35393 35394\texttt{open\_polygon} returns and draws the polygon line whose vertices are the 35395given points. 35396 35397\noindent 35398Input: 35399\begin{center} 35400 \tt 35401 open\_polygon(point(0,0,0),point(0,1,0),\\ 35402 point(0,2,2),point(1,0,2)) 35403\end{center} 35404Output: 35405\begin{center} 35406 \includegraphics[width=0.5\textwidth]{xcas-3dopoly.png} 35407\end{center} 35408 35409\section{Circles in space: \texttt{circle}\index{circle}} 35410\label{sec:3dcircles} 35411 35412See section \ref{sec:2dcircles} for circles in the plane. 35413 35414The \texttt{circle} command returns and draws a circle. 35415 35416\texttt{circle} takes as arguments one of the following: 35417\begin{itemize} 35418 \item Three points, \texttt{A}, \texttt{B} and \texttt{C}.\\ 35419 \texttt{circle} will return and draw the circle with a diameter 35420 \texttt{AB}, and the third point \texttt{C} determines the 35421 plane.\\ 35422 Input: 35423 \begin{center} 35424 \tt 35425 circle(point(0,0,1),point(0,1,0),point(0,2,2)) 35426 \end{center} 35427 Output: 35428 \begin{center} 35429 \includegraphics[width=0.5\textwidth]{xcas-3dcircle.png} 35430 \end{center} 35431 \item A point \texttt{A}, a vector \texttt{v} and another point 35432 \texttt{C}.\\ 35433 \texttt{circle} will return and draw the circle with center at 35434 \texttt{A} and with \texttt{A+v} on the circle. The point 35435 \texttt{C} determines the plane.\\ 35436 Input: 35437 \begin{center} 35438 \tt 35439 circle(point(0,0,1),vector(0,1,0),point(0,2,2)) 35440 \end{center} 35441 Output: 35442 \begin{center} 35443 \includegraphics[width=0.5\textwidth]{xcas-3dcircle2.png} 35444 \end{center} 35445\end{itemize} 35446In both cases, the first and third arguments can be the coordinates of 35447the point. 35448 35449\section{Conics in space} 35450 35451\subsection{Ellipses in space: \texttt{ellipse}\index{ellipse}} 35452\label{sec:3dellipse} 35453 35454See section \ref{sec:2dellipse} for ellipses in the plane. 35455 35456The \texttt{ellipse} command takes three non-collinear points as arguments; two 35457points as the foci and a third point to determine the plane. 35458 35459\texttt{ellipse} returns and draws the ellipse with the two given foci 35460and passing through the third argument. 35461 35462\noindent 35463Input: 35464\begin{center} 35465 \tt 35466 ellipse(point(-1,0,0),point(1,0,0),point(1,1,1)) 35467\end{center} 35468Output: 35469\begin{center} 35470 \includegraphics[width=0.5\textwidth]{xcas-3dellipse.png} 35471\end{center} 35472 35473\subsection{Hyperbolas in space: \texttt{hyperbola}\index{hyperbola}} 35474\label{sec:3dhyperbola} 35475 35476See section \ref{sec:2dhyperbola} for hyperbolas in the plane. 35477 35478The \texttt{hyperbola} command takes three non-collinear points as arguments; two 35479points as the foci and a third point to determine the plane. 35480 35481\texttt{hyperbola} returns and draws the hyperbola with the two given foci 35482and passing through the third argument. 35483 35484\noindent 35485Input: 35486\begin{center} 35487 \tt 35488 hyperbola(point(-1,0,0),point(1,0,0),point(1,1,1)) 35489\end{center} 35490Output: 35491\begin{center} 35492 \includegraphics[width=0.5\textwidth]{xcas-3dhyperbola.png} 35493\end{center} 35494 35495\subsection{Parabolas in space: \texttt{parabola}\index{parabola}} 35496\label{sec:3dparabola} 35497 35498See section \ref{sec:2dparabola} for parabolas in the plane. 35499 35500The \texttt{parabola} command takes three non-collinear points as 35501arguments. The first point is the focus, the second point is the 35502vertex, and the third point determines the plane. 35503 35504\texttt{parabola} returns and draws the corresponding parabola. 35505 35506\noindent 35507Input: 35508\begin{center} 35509 \tt 35510 parabola(point(0,0,0),point(-1,0,0),point(1,1,1)) 35511\end{center} 35512Output: 35513\begin{center} 35514 \includegraphics[width=0.5\textwidth]{xcas-3dparabola.png} 35515\end{center} 35516 35517\section{Three-dimensional coordinates} 35518 35519\subsection{The abscissa of a three-dimensional point: \texttt{abscissa}\index{abscissa}} 35520\label{sec:3dabscissa} 35521 35522See section \ref{sec:2dabscissa} for abscissas in two-dimensional geometry. 35523 35524The \texttt{abscissa} command takes as argument a point. 35525 35526\texttt{abscissa} returns the abscissa ($x$-coordinate) of the point. 35527 35528\noindent 35529Input: 35530\begin{center} 35531 \tt 35532 abscissa(point(1,2,3)) 35533\end{center} 35534Output: 35535\begin{center} 35536 \tt 35537 1 35538\end{center} 35539 35540\subsection{The ordinate of a three-dimensional point: \texttt{ordinate}\index{ordinate}} 35541\label{sec:3dordinate} 35542 35543See section \ref{sec:2dordinate} for ordinates in two-dimensional geometry. 35544 35545The \texttt{ordinate} command takes as argument a point. 35546 35547\texttt{ordinate} returns the ordinate ($y$-coordinate) of the point. 35548 35549\noindent 35550Input: 35551\begin{center} 35552 \tt 35553 ordinate(point(1,2,3)) 35554\end{center} 35555Output: 35556\begin{center} 35557 \tt 35558 2 35559\end{center} 35560 35561\subsection{The cote of a three-dimensional point: \texttt{cote}\index{cote}} 35562 35563The \texttt{cote} command takes as argument a point. 35564 35565\texttt{cote} returns the cote ($z$-coordinate) of the point. 35566 35567\noindent 35568Input: 35569\begin{center} 35570 \tt 35571 cote(point(1,2,3)) 35572\end{center} 35573Output: 35574\begin{center} 35575 \tt 35576 3 35577\end{center} 35578 35579\subsection{The coordinates of a point, vector or line in space: \texttt{coordinates}\index{coordinates}} 35580\label{sec:3dcoordinates} 35581 35582See section \ref{sec:2dcoordinates} for coordinates in 35583two-dimensional geometry. 35584 35585The \texttt{coordinates} command takes as argument a point, vector or line. 35586 35587If the argument is a point, 35588\texttt{coordinates} returns a list consisting of the abscissa, 35589ordinate and cote.\\ 35590Input: 35591\begin{center} 35592 \tt 35593 coordinates(point(1,2,3)) 35594\end{center} 35595Output: 35596\begin{center} 35597 \tt 35598 [1,2,3] 35599\end{center} 35600 35601If the argument is a vector, for example from point \texttt{A} to 35602point \texttt{B}, then \texttt{coordinates} returns a list of the 35603coordinates of \texttt{B-A}.\\ 35604Input: 35605\begin{center} 35606 \tt 35607 coordinates(vector(point(1,2,3),point(2,4,7))) 35608\end{center} 35609Output: 35610\begin{center} 35611 \tt 35612 [1,2,4] 35613\end{center} 35614 35615If the argument is a line, \texttt{coordinates} returns a list of 35616two points on the line, in the order determined by the direction of 35617the line.\\ 35618Input: 35619\begin{center} 35620 \tt 35621 coordinates(line(point(-1,1,0),point(1,2,3))) 35622\end{center} 35623Output: 35624\begin{center} 35625 \tt 35626 [[-1,1,0],[1,2,3]] 35627\end{center} 35628Input: 35629\begin{center} 35630 \tt 35631 coordinates(line(x-2*y+3=0, 6*x + 3*y - 5*z + 3 = 0)) 35632\end{center} 35633Output: 35634\begin{center} 35635 \tt 35636 [[-1,1,0],[9,6,15]] 35637\end{center} 35638 35639\texttt{coordinates} can also take a sequence or list of points as an 35640argument; it then returns a sequence or list of the coordinates of 35641the points.\\ 35642Input: 35643\begin{center} 35644 \tt 35645 coordinates(point(0,1,2),point(1,2,4)) 35646\end{center} 35647Output: 35648\begin{center} 35649 \tt 35650 [0,1,2], [1,2,4] 35651\end{center} 35652Note that if the argument is a list of real numbers, it is 35653interpreted as a list of points on the real axis of the plane.\\ 35654Input: 35655\begin{center} 35656 \tt 35657 coordinates([1,2,4]) 35658\end{center} 35659Output: 35660\begin{center} 35661 \tt 35662 [[1,0],[2,0],[4,0]] 35663\end{center} 35664 35665\subsection{The Cartesian equation of an object in space: \texttt{equation}\index{equation}} 35666\label{sec:3dcarteq} 35667 35668See section \ref{sec:2dcarteq} for Cartesian equations of 35669two-dimensional objects. 35670 35671The \texttt{equation} command takes as argument a geometric object. 35672 35673\texttt{equation} returns Cartesian equations which specify the 35674object. The equations will involve \texttt{z}, \texttt{y} and 35675\texttt{z}, so these variables must be unassigned. If they have 35676assignments, they can be unassigned with \texttt{purge(x,y,z)}. 35677 35678\noindent 35679Input: 35680\begin{center} 35681 \tt 35682 equation(line(point(0,1,0),point(1,2,3))) 35683\end{center} 35684Output: 35685\begin{center} 35686 \tt 35687 x-y+1=0,3*x+3*y-2*z-3=0 35688\end{center} 35689 35690\noindent 35691Input: 35692\begin{center} 35693 \tt 35694 equation(sphere(point(0,1,0),2)) 35695\end{center} 35696Output: 35697\begin{center} 35698 \tt 35699 x\^{}2+y\^{}2+z\^{}2-2*y-3=0 35700\end{center} 35701 35702\subsection{The parametric equation of an object in space: \texttt{parameq}\index{parameq}} 35703\label{sec:3dparam} 35704 35705See section \ref{sec:2dparam} for parametric equations in 35706two-dimensional geometry. 35707 35708The \texttt{parameq} command takes as argument a geometric object. 35709 35710\texttt{equation} returns a parameterization for the object. For 35711a curve, the parameter is \texttt{t}, for a surface, the parameters 35712are \texttt{u} and \texttt{v}. These variables must be unassigned. 35713If they have assignments, they can be unassigned with 35714\texttt{purge(t,u,v)}. 35715 35716\noindent 35717Input: 35718\begin{center} 35719 \tt 35720 parameq(line(point(0,1,0),point(1,2,3))) 35721\end{center} 35722Output: 35723\begin{center} 35724 \tt 35725 [t,t+1,3*t] 35726\end{center} 35727 35728\noindent 35729Input: 35730\begin{center} 35731 \tt 35732 parameq(sphere(point(0,1,0),2)) 35733\end{center} 35734Output: 35735\begin{center} 35736 \tt 35737 [2*cos(u)*cos(v),2*cos(u)*sin(v)+1,2*sin(u)] 35738\end{center} 35739 35740\noindent 35741Input: 35742\begin{center} 35743 \tt 35744 normal(parameq(ellipse(point(-1,1,1),point(1,1,1),point(0,1,2)))) 35745\end{center} 35746Output: 35747\begin{center} 35748 \tt 35749 [sqrt(2)*cos(t),1,sin(t)+1] 35750\end{center} 35751 35752\subsection{The length of a segment in space: \texttt{distance}\index{distance}} 35753\label{sec:3ddist} 35754 35755See section \ref{sec:2ddist} for distances in two-dimensional 35756geometry. 35757 35758The \texttt{distance} command takes as arguments two points or two 35759lists with the coordinates of the points. 35760 35761\texttt{distance} returns the distance between these two points. 35762 35763\noindent 35764Input: 35765\begin{center} 35766 \tt 35767 distance(point(-1,1,1),point(1,1,1)) 35768\end{center} 35769or: 35770\begin{center} 35771 \tt 35772 distance([-1,1,1],[1,1,1]) 35773\end{center} 35774Output: 35775\begin{center} 35776 \tt 35777 2 35778\end{center} 35779 35780\subsection{The length squared of a segment in space: \texttt{distance2}\index{distance2}} 35781\label{sec:3ddist2} 35782 35783See section \ref{sec:2ddist2} for squares of lengths in 35784two-dimensional geometry. 35785 35786The \texttt{distance2} command takes as arguments two points or two 35787lists with the coordinates of the points. 35788 35789\texttt{distance2} returns the square of the distance between these two points. 35790 35791\noindent 35792Input: 35793\begin{center} 35794 \tt 35795 distance2(point(-1,1,1),point(1,1,1)) 35796\end{center} 35797or: 35798\begin{center} 35799 \tt 35800 distance2([-1,1,1],[1,1,1]) 35801\end{center} 35802Output: 35803\begin{center} 35804 \tt 35805 4 35806\end{center} 35807 35808\subsection{The measure of an angle in space: \texttt{angle}\index{angle}} 35809\label{sec:3dangle} 35810 35811See section \ref{sec:2dangle} for angle measures in two-dimensional 35812geometry. 35813 35814The \texttt{angle} command takes are arguments one of the following;\\ 35815\begin{itemize} 35816 \item Three points, \texttt{A}, \texttt{B} and \texttt{C}.\\ 35817 \texttt{angle} returns the measure of the undirected angle 35818 \texttt{BAC}.\\ 35819 Input: 35820 \begin{center} 35821 \tt 35822 angle(point(0,0,0),point(1,0,0),point(0,0,1)) 35823 \end{center} 35824 Output: 35825 \begin{center} 35826 \tt 35827 pi/4 35828 \end{center} 35829 \item Two intersecting lines.\\ 35830 \texttt{angle} returns the measure of the angle between the lines.\\ 35831 Input: 35832 \begin{center} 35833 \tt 35834 angle(line([0,0,0],[1,1,0]),line([0,0,0],[1,1,1])) 35835 \end{center} 35836 Output: 35837 \begin{center} 35838 \tt 35839 acos((sqrt(6))/3) 35840 \end{center} 35841 \item A line and a plane.\\ 35842 \texttt{angle} returns the measure of the angle between the line and 35843 the plane.\\ 35844 Input: 35845 \begin{center} 35846 \tt 35847 angle(line([0,0,0],[1,1,0]),plane(x+y+z=0)) 35848 \end{center} 35849 Output: 35850 \begin{center} 35851 \tt 35852 acos((sqrt(6))/3) 35853 \end{center} 35854\end{itemize} 35855 35856\section{Properties} 35857 35858\subsection{Check if an object in space is on another object: \texttt{is\_element}\index{is\_element}} 35859\label{sec:3diselem} 35860 35861See section \ref{sec:2diselem} for checking elements in 35862two-dimensional geometry. 35863 35864The \texttt{is\_element} command takes as arguments two geometric 35865objects. 35866 35867\texttt{is\_element} returns 1 is the first object is contained in the 35868second, it returns 0 otherwise. 35869 35870\noindent 35871Input: 35872\begin{center} 35873 \tt 35874 P := plane([0,0,0],[1,2,-3],[1,1,-2]) 35875\end{center} 35876then: 35877\begin{center} 35878 \tt 35879 is\_element(point(2,3,-5),P) 35880\end{center} 35881Output: 35882\begin{center} 35883 \tt 35884 1 35885\end{center} 35886 35887\noindent 35888Input: 35889\begin{center} 35890 \tt 35891 L := line([2,3,-2],[-1,-1,-1]);\\ 35892 P := plane([-1,-1,-1],[1,2,-3],[1,1,-2]) 35893\end{center} 35894then: 35895\begin{center} 35896 \tt 35897 is\_element(L,P) 35898\end{center} 35899Output: 35900\begin{center} 35901 \tt 35902 0 35903\end{center} 35904 35905\subsection{Check if points and/or lines in space are coplanar: \texttt{is\_coplanar}\index{is\_coplanar}} 35906 35907The \texttt{is\_coplanar} command takes as arguments a list or 35908sequence of points or lines. 35909 35910\texttt{is\_coplanar} returns \texttt{1} is the objects are coplanar; 35911it returns \texttt{0} otherwise. 35912 35913\noindent 35914Input: 35915\begin{center} 35916 \tt 35917 is\_coplanar([0,0,0],[1,2,-3],[1,1,-2],[2,1,-3]) 35918\end{center} 35919Output: 35920\begin{center} 35921 \tt 35922 1 35923\end{center} 35924Input: 35925\begin{center} 35926 \tt 35927 is\_coplanar([-1,2,0],[1,2,-3],[1,1,-2],[2,1,-3]) 35928\end{center} 35929Output: 35930\begin{center} 35931 \tt 35932 0 35933\end{center} 35934 35935\noindent 35936Input: 35937\begin{center} 35938 \tt 35939 is\_coplanar([0,0,0],[1,2,-3],line([1,1,-2],[2,1,-3])) 35940\end{center} 35941Output: 35942\begin{center} 35943 \tt 35944 1 35945\end{center} 35946Input: 35947\begin{center} 35948 \tt 35949 is\_coplanar(line([-1,2,0],[1,2,-3]),line([1,1,-2],[2,1,-3])) 35950\end{center} 35951Output: 35952\begin{center} 35953 \tt 35954 0 35955\end{center} 35956 35957 35958\subsection{Check if lines and/or planes in space are parallel: \texttt{is\_parallel}\index{is\_parallel}} 35959\label{sec:3dcheckparallel} 35960 35961See section \ref{sec:2dcheckparallel} for checking for parallels in 35962two-dimensional geometry. 35963 35964The \texttt{is\_parallel} command takes as arguments two lines, two 35965planes or a line and a plane. 35966 35967\texttt{is\_parallel} returns \texttt{1} is the objects are parallel; 35968it returns \texttt{0} otherwise. 35969 35970\noindent 35971Input: 35972\begin{center} 35973 \tt 35974 L1 := line([0,0,0],[-1,-1,-1])\\ 35975 L2 := line([2,3,-2],[-1,-1,-1]) 35976\end{center} 35977then: 35978\begin{center} 35979 \tt 35980 is\_parallel(L1,L2) 35981\end{center} 35982Output: 35983\begin{center} 35984 \tt 35985 0 35986\end{center} 35987Input: 35988\begin{center} 35989 \tt 35990 P := plane([-1,-1,-1],[1,2,-3],[0,0,0]) 35991\end{center} 35992then: 35993\begin{center} 35994 \tt 35995 is\_parallel(P,L2) 35996\end{center} 35997Output: 35998\begin{center} 35999 \tt 36000 1 36001\end{center} 36002 36003\noindent 36004Input: 36005\begin{center} 36006 \tt 36007 P1 := plane([0,0,0],[1,2,-3],[1,1,-2])\\ 36008 P2 := plane([1,1,0],[2,3,-3],[2,2,-2]) 36009\end{center} 36010then: 36011\begin{center} 36012 \tt 36013 is\_parallel(P1,P2) 36014\end{center} 36015Output: 36016\begin{center} 36017 \tt 36018 1 36019\end{center} 36020 36021\subsection{Check if lines and/or planes in space are perpendicular: \texttt{is\_perpendicular}\index{is\_perpendicular}} 36022\label{sec:3dcheckperp} 36023 36024See section \ref{sec:2dcheckperp} for checking for perpendicularity in 36025two-dimensional geometry. 36026 36027The \texttt{is\_perpendicular} command takes as arguments two lines, two 36028planes or a line and a plane. 36029 36030\texttt{is\_perpendicular} returns \texttt{1} is the objects are 36031perpendicular; it returns \texttt{0} otherwise. (Note that two lines 36032must be coplanar to be perpendicular.) 36033 36034\noindent 36035Input: 36036\begin{center} 36037 \tt 36038 is\_perpendicular(line([2,3,-2],[-1,-1,-1]),line([1,0,0],[1,2,8])) 36039\end{center} 36040Output: 36041\begin{center} 36042 \tt 36043 0 36044\end{center} 36045Input: 36046\begin{center} 36047 \tt 36048 P1 := plane([0,0,0],[1,2,-3],[1,1,-2])\\ 36049 P2 := plane([-1,-1,-1],1,2,-3],[0,0,0]) 36050\end{center} 36051then: 36052\begin{center} 36053 \tt 36054 is\_perpendicular(P1,P2) 36055\end{center} 36056Output: 36057\begin{center} 36058 \tt 36059 1 36060\end{center} 36061 36062\noindent 36063Input: 36064\begin{center} 36065 \tt 36066 L := plane([2,3,-2],[-1,-1,-1])\\ 36067\end{center} 36068then: 36069\begin{center} 36070 \tt 36071 is\_perpendicular(L,P1) 36072\end{center} 36073Output: 36074\begin{center} 36075 \tt 36076 0 36077\end{center} 36078 36079\subsection{Check if two lines or two spheres in space are orthogonal: \texttt{is\_orthogonal}\index{is\_orthogonal}} 36080\label{sec:3dcheckorth} 36081 36082See section \ref{sec:2dcheckorth} for checking for orthogonality in 36083two-dimensional geometry. 36084 36085 36086The \texttt{is\_orthogonal} command takes as arguments two lines, two 36087spheres, two planes or a line and a plane. 36088 36089\texttt{is\_orthogonal} returns \texttt{1} is the objects are 36090orthogonal; it returns \texttt{0} otherwise. 36091 36092\noindent 36093Input: 36094\begin{center} 36095 \tt 36096 is\_orthogonal(line([2,3,-2],[-1,-1,-1]),line([1,0,0],[1,2,8])) 36097\end{center} 36098Output: 36099\begin{center} 36100 \tt 36101 1 36102\end{center} 36103Input: 36104\begin{center} 36105 \tt 36106 is\_orthogonal(line([2,3,-2],[-1,-1,-1]),\\ 36107 plane([-1,-1,-1],[-1,0,3],[-2,0,0])) 36108\end{center} 36109Output: 36110\begin{center} 36111 \tt 36112 1 36113\end{center} 36114Input: 36115\begin{center} 36116 \tt 36117 is\_orthogonal(plane([0,0,0],[1,2,-3],[1,1,-2]),\\ 36118 plane([-1,-1,-1],[1,2,-3],[0,0,0])) 36119\end{center} 36120Output: 36121\begin{center} 36122 \tt 36123 1 36124\end{center} 36125Input: 36126\begin{center} 36127 \tt 36128 is\_orthogonal(sphere([0,0,0],sqrt(2)),sphere([2,0,0],sqrt(2))) 36129\end{center} 36130Output: 36131\begin{center} 36132 \tt 36133 1 36134\end{center} 36135 36136 36137\subsection{Check if three points in space are collinear: \texttt{is\_collinear}\index{is\_collinear}} 36138\label{sec:3dcheckcollinear} 36139 36140See section \ref{sec:2dcheckcollinear} for checking for collinearity in 36141two-dimensional geometry. 36142 36143The \texttt{is\_collinear} command takes as argument a list or 36144sequence of points. 36145 36146\texttt{is\_collinear} returns 1 if the points are collinear, it 36147returns 0 otherwise. 36148 36149\noindent 36150Input: 36151\begin{center} 36152 \tt 36153 is\_collinear([2,0,0],[0,2,0],[1,1,0]) 36154\end{center} 36155Output: 36156\begin{center} 36157 \tt 36158 1 36159\end{center} 36160Input: 36161\begin{center} 36162 \tt 36163 is\_collinear([2,0,0],[0,2,0],[0,1,1]) 36164\end{center} 36165Output: 36166\begin{center} 36167 \tt 36168 0 36169\end{center} 36170 36171\subsection{Check if four points in space are cocyclic: \texttt{is\_concyclic}\index{is\_concyclic}} 36172\label{sec:3dcheckconcycle} 36173 36174See section \ref{sec:2dcheckconcycle} for checking for concyclicity in 36175two-dimensional geometry. 36176 36177The \texttt{is\_concyclic} command takes as argument a list or 36178sequence of points. 36179 36180\texttt{is\_concyclic} returns 1 if the points are cocyclic, it 36181returns 0 otherwise. 36182 36183\noindent 36184Input: 36185\begin{center} 36186 \tt 36187 is\_concyclic([2,0,0],[0,2,0],[sqrt(2),sqrt(2),0],\\{} 36188 [0,0,2],[2/sqrt(3),2/sqrt(3),2/sqrt(3)]) 36189\end{center} 36190Output: 36191\begin{center} 36192 \tt 36193 1 36194\end{center} 36195Input: 36196\begin{center} 36197 \tt 36198 is\_concyclic([2,0,0],[0,2,0],[1,1,0],[0,0,2],[1,1,1]) 36199\end{center} 36200Output: 36201\begin{center} 36202 \tt 36203 0 36204\end{center} 36205 36206\subsection{Check if five points in space are cospherical: 36207\texttt{is\_cospherical}\index{is\_cospherical}} 36208 36209The \texttt{is\_cospherical} command takes as arguments a list or 36210sequence of points. 36211 36212\texttt{is\_cospherical} returns \texttt{1} if the points are 36213cospherical, it returns \texttt{0} if they are not. 36214 36215\noindent 36216Input: 36217\begin{center} 36218 \tt 36219 is\_cospherical([2,0,0],[0,2,0],[sqrt(2),sqrt(2),0],\\{} 36220 [0,0,2],[2/sqrt(3),2/sqrt(3),2/sqrt(3)]) 36221\end{center} 36222Output: 36223\begin{center} 36224 \tt 36225 1 36226\end{center} 36227 36228\noindent 36229Input: 36230\begin{center} 36231 \tt 36232 is\_cospherical([2,0,0],[0,2,0],[1,1,0],[0,0,2],[1,1,1]) 36233\end{center} 36234Output: 36235\begin{center} 36236 \tt 36237 0 36238\end{center} 36239 36240\subsection{Check if an object in space is an equilateral triangle: \texttt{is\_equilateral}\index{is\_equilateral}} 36241\label{sec:3dcheckequilateral} 36242 36243See section \ref{sec:2dcheckequilateral} for checking for equilateral 36244triangles in two-dimensional geometry. 36245 36246The \texttt{is\_equilateral} command takes as argument either three 36247points or a geometric object. 36248 36249\texttt{is\_equilateral} returns 1 if the points are the vertices of 36250an equilateral triangle or if the object is an equilateral triangle. 36251 36252\noindent 36253Input: 36254\begin{center} 36255 \tt 36256 is\_equilateral([2,0,0],[0,0,0],[1,sqrt(3),0]) 36257\end{center} 36258Output: 36259\begin{center} 36260 \tt 36261 1 36262\end{center} 36263 36264\noindent 36265Input: 36266\begin{center} 36267 T := triangle\_equilateral([2,0,0],[0,0,0],[1,sqrt(3),0]) 36268\end{center} 36269then: 36270\begin{center} 36271 \tt 36272 is\_equilateral(T) 36273\end{center} 36274Output: 36275\begin{center} 36276 \tt 36277 1 36278\end{center} 36279 36280\noindent 36281Input: 36282\begin{center} 36283 \tt 36284 is\_equilateral([2,0,0],[0,2,0],[1,1,0]) 36285\end{center} 36286Output: 36287\begin{center} 36288 \tt 36289 0 36290\end{center} 36291 36292\subsection{Check if an object in space is an isosceles triangle: \texttt{is\_isosceles}\index{is\_isosceles}} 36293\label{sec:3dcheckisosceles} 36294 36295See section \ref{sec:2dcheckisosceles} for checking for isosceles 36296triangles in two-dimensional geometry. 36297 36298The \texttt{is\_isosceles} command takes as argument either three 36299points or a geometric object. 36300 36301\texttt{is\_equilateral} returns \texttt{1}, \texttt{2}, \texttt{3} or 36302\texttt{4} is the points are the vertices of an isosceles triangle or 36303if the object is an isosceles triangle, and returns \texttt{0} 36304otherwise. Specifically, 36305\begin{itemize} 36306 \item It returns \texttt{4} if the object is an equilateral triangle 36307 or if the points are the vertices of an equilateral triangle. 36308 \item It returns \texttt{1}, \texttt{2} or \texttt{3}, respectively, 36309 if the object is an isosceles triangle or if the points are the 36310 vertices of an isosceles triangle and the first, second or third 36311 point is the vertex with equal sides. 36312\end{itemize} 36313 36314\noindent 36315Input: 36316\begin{center} 36317 \tt 36318 is\_isosceles([2,0,0],[0,0,0],[0,2,0]) 36319\end{center} 36320Output: 36321\begin{center} 36322 \tt 36323 2 36324\end{center} 36325 36326\noindent 36327Input: 36328\begin{center} 36329 T := triangle\_isosceles([0,0,0],[2,2,0],[2,2,2]) 36330\end{center} 36331then: 36332\begin{center} 36333 \tt 36334 is\_isosceles(T) 36335\end{center} 36336Output: 36337\begin{center} 36338 \tt 36339 1 36340\end{center} 36341 36342\noindent 36343Input: 36344\begin{center} 36345 \tt 36346 is\_isosceles([1,1,0],[-1,1,0],[-1,0,0]) 36347\end{center} 36348Output: 36349\begin{center} 36350 \tt 36351 0 36352\end{center} 36353 36354\subsection{Check if an object in space is a right triangle or a rectangle: \texttt{is\_rectangle}\index{is\_rectangle}} 36355\label{sec:3dcheckrect} 36356 36357See section \ref{sec:2dcheckrect} for checking for right triangles and rectangles 36358in two-dimensional geometry. 36359 36360The \texttt{is\_rectangle} command checks for both right triangles and 36361rectangles. It takes as arguments either three points, four points, 36362or a geometric object. 36363 36364If the arguments are three points or a triangle, then 36365\texttt{is\_rectangle} returns \texttt{1}, \texttt{2} or \texttt{3} if 36366the points form a right triangle which right angle at the first, 36367second or third vertex. It returns \texttt{0} otherwise. 36368 36369If the arguments are four points or a quadrilateral, then 36370\texttt{is\_rectangle} returns \texttt{2} is the points form a square, 36371\texttt{1} is they form a rectangle, and \texttt{0} otherwise. 36372 36373\noindent 36374Input: 36375\begin{center} 36376 \tt 36377 is\_rectangle([2,0,0],[2,2,0],[0,2,0]) 36378\end{center} 36379Output: 36380\begin{center} 36381 \tt 36382 2 36383\end{center} 36384 36385\noindent 36386Input: 36387\begin{center} 36388 \tt 36389 is\_rectangle([2,2,0],[-2,2,0],[-2,-1,0],[2,-1,0]) 36390\end{center} 36391Output: 36392\begin{center} 36393 \tt 36394 1 36395\end{center} 36396 36397\subsection{Check if an object in space is a square: \texttt{is\_square}\index{is\_square}} 36398\label{sec:3dchecksquare} 36399 36400See section \ref{sec:2dchecksquare} for checking for squares 36401in two-dimensional geometry. 36402 36403The \texttt{is\_square} command as arguments either four points 36404or a geometric object. 36405 36406\texttt{is\_rectangle} returns \texttt{1} if the four points are the 36407vertices of a square or if the geometric object is a square, it returns 36408\texttt{0} otherwise. 36409 36410\noindent 36411Input: 36412\begin{center} 36413 \tt 36414 is\_square([2,2,0],[-2,2,0],[-2,-2,0],[2,-2,0]) 36415\end{center} 36416Output: 36417\begin{center} 36418 \tt 36419 1 36420\end{center} 36421 36422\noindent 36423Input: 36424\begin{center} 36425 \tt 36426 S := square([0,0,0],[2,0,0],[0,0,1]) 36427\end{center} 36428then: 36429\begin{center} 36430 \tt 36431 is\_square(S) 36432\end{center} 36433Output: 36434\begin{center} 36435 \tt 36436 1 36437\end{center} 36438 36439\noindent 36440Input: 36441\begin{center} 36442 \tt 36443 is\_square([2,2,0],[-2,2,0],[-2,-1,0],[2,-1,0]) 36444\end{center} 36445Output: 36446\begin{center} 36447 \tt 36448 0 36449\end{center} 36450 36451\subsection{Check if an object in space is a rhombus: \texttt{is\_rhombus}\index{is\_rhombus}} 36452\label{sec:3dcheckrhombus} 36453 36454See section \ref{sec:2dcheckrhombus} for checking for rhombuses 36455in two-dimensional geometry. 36456 36457The \texttt{is\_rhombus} command as arguments either four points 36458or a geometric object. 36459 36460\texttt{is\_rhombus} returns \texttt{1} if the four points are the 36461vertices of a rhombus or if the geometric object is a rhombus, it returns 36462\texttt{0} otherwise. 36463 36464\noindent 36465Input: 36466\begin{center} 36467 \tt 36468 is\_rhombus([2,0,0],[0,1,0],[-2,0,0],[0,-1,0]) 36469\end{center} 36470Output: 36471\begin{center} 36472 \tt 36473 1 36474\end{center} 36475 36476\noindent 36477Input: 36478\begin{center} 36479 \tt 36480 R := rhombus([0,0,0],[2,0,0],[[0,0,1],pi/4]) 36481\end{center} 36482then: 36483\begin{center} 36484 \tt 36485 is\_rhombus(S) 36486\end{center} 36487Output: 36488\begin{center} 36489 \tt 36490 1 36491\end{center} 36492 36493\noindent 36494Input: 36495\begin{center} 36496 \tt 36497 is\_rhombus([2,2,0],[-2,2,0],[-2,-1,0],[2,-1,0]) 36498\end{center} 36499Output: 36500\begin{center} 36501 \tt 36502 0 36503\end{center} 36504 36505\subsection{Check if an object in space is a parallelogram: \texttt{is\_parallelogram}\index{is\_parallelogram}} 36506\label{sec:3dcheckparallelogram} 36507 36508See section \ref{sec:2dcheckparallelogram} for checking for 36509parallelograms in two-dimensional geometry. 36510 36511The \texttt{is\_parallelogram} command as arguments either four points 36512or a geometric object. 36513 36514\texttt{is\_parallelogram} returns \texttt{4}, \texttt{3}, \texttt{2} 36515or \texttt{1} if the four points are the vertices of a square, 36516rectangle, rhombus or parallelogram, respectively, or if the geometric 36517object is a square, rectangle, rhombus or parallelogram. 36518It returns \texttt{0} otherwise. 36519 36520\noindent 36521Input: 36522\begin{center} 36523 \tt 36524 is\_parallelogram([0,0,0],[2,0,0],[3,1,0],[1,1,0]) 36525\end{center} 36526Output: 36527\begin{center} 36528 \tt 36529 1 36530\end{center} 36531 36532\noindent 36533Input: 36534\begin{center} 36535 \tt 36536 is\_parallelogram([-1,0,0],[0,1,0],[2,0,0],[0,-1,0]) 36537\end{center} 36538Output: 36539\begin{center} 36540 \tt 36541 0 36542\end{center} 36543 36544\noindent 36545Input: 36546\begin{center} 36547 \tt 36548 P := parallelogram([0,0,0],[2,0,0],[1,1,0]) 36549\end{center} 36550then: 36551\begin{center} 36552 \tt 36553 is\_parallelogram(P) 36554\end{center} 36555Output: 36556\begin{center} 36557 \tt 36558 1 36559\end{center} 36560 36561Note that\\ 36562Input: 36563\begin{center} 36564 \tt 36565 P := parallelogram([0,0,0],[2,0,0],[1,1,0],D) 36566\end{center} 36567defines \texttt{P} to be a list consisting of the parallelogram and 36568the point \texttt{D}; to test if the object is a parallelogram, the 36569first component of \texttt{P} needs to be tested.\\ 36570Input: 36571\begin{center} 36572 \tt 36573 is\_parallelogram(P[0]) 36574\end{center} 36575Output: 36576\begin{center} 36577 \tt 36578 1 36579\end{center} 36580 36581\noindent 36582Input: 36583\begin{center} 36584 \tt 36585 is\_parallelogram([-1,0,0],[0,1,0],[2,0,0],[0,-1,0]) 36586\end{center} 36587Output: 36588\begin{center} 36589 \tt 36590 0 36591\end{center} 36592 36593\section{Transformations in space} 36594 36595\subsection{General remarks} 36596 36597The transformations in this section operate on any geometric object. 36598They take as arguments parameters to specify the transformation. 36599They can optionally take a geometric object as the last argument, in 36600which case the transformed object is returned. Without the geometric 36601object as an argument, these transformations will return a new command 36602which performs the transformation. For example, to move an object \texttt{P} 366033 units up, either 36604\begin{center} 36605 \tt 36606 translation([0,0,3],P) 36607\end{center} 36608or 36609\begin{center} 36610 \tt 36611 t := translation([0,0,3])\\ 36612 t(P) 36613\end{center} 36614works. 36615 36616\subsection{Translation in space: \texttt{translation}\index{translation}} 36617\label{sec:3dtranslations} 36618 36619See section \ref{sec:2dtranslations} for translations in the plane. 36620 36621The \texttt{translation} command takes one or two arguments. The first 36622argument is the translation vector given by a list of coordinates, the 36623optional second argument is a geometric object. 36624 36625With one argument, \texttt{translation} returns a command which 36626translates objects along the given vector.\\ 36627Input: 36628\begin{center} 36629 \tt 36630 t := translation([1,1,1]) 36631\end{center} 36632then: 36633\begin{center} 36634 \tt 36635 t(point(1,2,3)) 36636\end{center} 36637returns and draws the point at $(1,2,3)+(1,1,1) = (2,3,4)$. 36638% Output: 36639% \begin{center} 36640% % \includegraphics[width=0.5\textwidth]{} 36641% \end{center} 36642 36643With two arguments, a vector and an object, \texttt{transformation} 36644returns and draws the translated object.\\ 36645Input: 36646\begin{center} 36647 \tt 36648 translation([1,1,1],line([0,0,0],[1,2,3])) 36649\end{center} 36650returns and draws the line through $(0,0,0)+(1,1,1)=(1,1,1)$ and 36651$(1,2,3)+(1,1,1)=(2,3,4)$. 36652% Output: 36653% \begin{center} 36654% % \includegraphics[width=0.5\textwidth]{} 36655% \end{center} 36656 36657\subsection{Reflection in space with respect to a plane, line or point: \texttt{reflection}\index{reflection} \texttt{symmetry}\index{symmetry}} 36658\label{sec:3dreflections} 36659 36660See section \ref{sec:2dreflections} for reflections in the plane. 36661 36662The \texttt{reflection} command takes one or two arguments. The first 36663argument is a point, line or plane. The second optional argument is a 36664geometric object. 36665 36666With one argument, \texttt{reflection} returns a command which 36667reflects an object across the point, line or plane.\\ 36668Input: 36669\begin{center} 36670 \tt 36671 r := reflection([1,1,1]) 36672\end{center} 36673then: 36674\begin{center} 36675 \tt 36676 r(point(1,2,4)) 36677\end{center} 36678returns and draws the reflection of the point $(1,2,4)$ across the 36679point $(1,1,1)$; namely $(1,1,1) - [(1,2,4)-(1,1,1)] = (1,0,-2)$. 36680% Output: 36681% \begin{center} 36682% % \includegraphics[width=0.5\textwidth]{} 36683% \end{center} 36684 36685Given a second argument of a geometric object, \texttt{reflection} 36686returns and draws the reflected object.\\ 36687Input: 36688\begin{center} 36689 \tt 36690 reflection(line([1,1,0],[-1,-3,0]),point(-1,2,4)) 36691\end{center} 36692returns and draws the reflection of the point $(-1,2,4)$ across the 36693line through $(1,1,0)$ and $(-1,-3,0)$; namely $(16/5,3/5,-4)$. 36694% Output: 36695% \begin{center} 36696% % \includegraphics[width=0.5\textwidth]{} 36697% \end{center} 36698 36699\subsection{Rotation in space: \texttt{rotation}\index{rotation}} 36700\label{sec:3drotations} 36701 36702See section \ref{sec:2drotations} for rotations in the plane. 36703 36704The \texttt{rotation} command takes two or three arguments. The first 36705argument is a line which is the axis of rotation and the second 36706argument is a real number representing the angle of rotation. 36707The third optional argument is a geometric object. 36708 36709With two arguments, \texttt{rotation} returns a command which 36710rotates an object.\\ 36711Input: 36712\begin{center} 36713 \tt 36714 r := rotation(line(point(0,0,0),point(1,1,1)), 2*pi/3) 36715\end{center} 36716then: 36717\begin{center} 36718 \tt 36719 r(point(0,0,1)) 36720\end{center} 36721returns and draws the result of rotating the point $(0,0,1)$ about the 36722line through $(0,0,0)$ and $(1,1,1)$ through an angle of $2\pi/3$ 36723radians; namely $(1,0,0)$. 36724% Output: 36725% \begin{center} 36726% % \includegraphics[width=0.5\textwidth]{} 36727% \end{center} 36728 36729Given a third argument of a geometric object, \texttt{rotation} 36730returns and draws the rotated object.\\ 36731Input: 36732\begin{center} 36733 \tt 36734 rotation(line(point(0,0,0),point(1,1,1)), 2*pi/3, point(0,0,1)) 36735\end{center} 36736returns and draws the point $(1,0,0)$, as above. 36737% Output: 36738% \begin{center} 36739% % \includegraphics[width=0.5\textwidth]{} 36740% \end{center} 36741 36742\noindent 36743Input: 36744\begin{center} 36745 \tt 36746 rotation(line(point(0,0,0),point(1,1,1)), 2*pi/3,\\ 36747 line(point(1,0,0),point(0,1,0))) 36748\end{center} 36749returns and draws the result of rotating the line through $(1,0,0)$ 36750and $(0,1,0)$ about the line through $(0,0,0)$ and $(1,1,1)$ through 36751an angle of $2\pi/3$ radians; namely the line through $(0,1,0)$ and 36752$(0,0,1)$. 36753% Output: 36754% \begin{center} 36755% % \includegraphics[width=0.5\textwidth]{} 36756% \end{center} 36757 36758\subsection{Homothety in space: \texttt{homothety}\index{homothety}} 36759\label{sec:3dhomothety} 36760 36761See section \ref{sec:2dhomothety} for homotheties in the plane. 36762 36763The \texttt{homothety} command takes two or three arguments. The 36764first argument is a point, the center of the homothety. The second 36765argument is a real number, which is the scaling ratio. The optional 36766third argument is the object which is transformed. 36767 36768With two arguments, \texttt{homothery} returns a new command which 36769performs the dilation.\\ 36770Input: 36771\begin{center} 36772 \tt 36773 h := homothety(point(0,0,0), 2) 36774\end{center} 36775then: 36776\begin{center} 36777 \tt 36778 h(point(0,0,1)) 36779\end{center} 36780returns and draws the point $(0,0,2)$, which is the point $(0,0,1)$ 36781dilated by a factor of 2 away from $(0,0,0)$. 36782% Output: 36783% \begin{center} 36784% % \includegraphics[width=0.5\textwidth]{xcas-homothety1.png} 36785% \end{center} 36786 36787With a third argument of a geometric object, \texttt{homothety} 36788returns and draws the dilated object.\\ 36789Input: 36790\begin{center} 36791 \tt 36792 homothety(point(0,0,0), 2, point(0,0,1)) 36793\end{center} 36794returns and draws $(0,0,2)$, as above. 36795% Output: 36796% \begin{center} 36797% % \includegraphics[width=0.5\textwidth]{xcas-homothety2.png} 36798% \end{center} 36799 36800\noindent 36801Input: 36802\begin{center} 36803 \tt 36804 homothety(point(0,0,0), 2, sphere(point(0,0,0),1)) 36805\end{center} 36806returns and draws the sphere of radius 2 centered at $(0,0,0)$. 36807% Output: 36808% \begin{center} 36809% % \includegraphics[width=0.5\textwidth]{xcas-homothety3.png} 36810% \end{center} 36811 36812\subsection{Similarity in space: \texttt{similarity}\index{similarity}} 36813\label{sec:3dsimilarity} 36814 36815See section \ref{sec:2dsimilarity} for similarities in the plane. 36816 36817The \texttt{similarity} command takes three or four arguments. The 36818first argument is a line, the axis of rotation; the second 36819argument is a real number, which is the scaling ratio, and the third 36820argument is another real number, the angle of rotation. If the 36821scaling ratio is negative, then the direction of rotation is reversed. 36822The optional fourth argument is the object which is transformed. 36823 36824With three arguments, \texttt{similarity} returns a new command which 36825scales and rotated about the given axis.\\ 36826Input: 36827\begin{center} 36828 \tt 36829 s := similarity(line(point(0,0,0),point(1,1,1)), 2, 2*pi/3) 36830\end{center} 36831then: 36832\begin{center} 36833 \tt 36834 s(point(0,0,1)) 36835\end{center} 36836returns and draws the point $(2,0,0)$, which is the point $(0,0,1)$ 36837rotated about the line through $(0,0,0)$ and $(1,1,1)$ through an 36838angle of $2\pi/3$ radians and scaled away from the line by a factor of 368392. 36840% Output: 36841% \begin{center} 36842% % \includegraphics[width=0.5\textwidth]{xcas-homothety1.png} 36843% \end{center} 36844%Input: 36845%\begin{center} 36846% \tt 36847% s(sphere(point(1,0,0),1)) 36848%\end{center} 36849% Output: 36850% \begin{center} 36851% % \includegraphics[width=0.5\textwidth]{} 36852% \end{center} 36853 36854With a fourth argument of a geometric object, \texttt{similarity} 36855returns and draws the transformed object.\\ 36856Input: 36857\begin{center} 36858 \tt 36859 similarity(line(point(0,0,0),point(1,1,1)), 2, 2*pi/3, point(0,0,1)) 36860\end{center} 36861returns and draws the point $(2,0,0)$, as above. 36862% Output: 36863% \begin{center} 36864% % \includegraphics[width=0.5\textwidth]{xcas-homothety2.png} 36865% \end{center} 36866% Input: 36867% \begin{center} 36868% \tt 36869% similarity(line(point(0,0,0),point(1,1,1)), 2, 2*pi/3, sphere(point(1,0,0),1)) 36870% \end{center} 36871% Output: 36872% \begin{center} 36873% % \includegraphics[width=0.5\textwidth]{xcas-homothety3.png} 36874% \end{center} 36875 36876\subsection{Inversion in space: \texttt{inversion}\index{inversion}} 36877\label{sec:3dinversion} 36878 36879See section \ref{sec:2dinversion} for inversions in the plane. 36880 36881Given a point $P$ and a real number $k$, the corresponding \emph{inversion} 36882of a point $A$ is the point $A'$ on the ray $\overrightarrow{PA}$ satisfying 36883$\overline{PA}\cdot\overline{PA'} = k^2$. 36884 36885The \texttt{inversion} command takes two or three arguments. The 36886first argument is a point, the center of inversion; the second 36887argument is a real number, which is the inversion ratio. The optional 36888third argument is the object which is transformed. 36889 36890With two arguments, \texttt{inversion} returns a new command which 36891does the inversion.\\ 36892Input: 36893\begin{center} 36894 \tt 36895 inver := inversion(point(0,0,0), 2) 36896\end{center} 36897then: 36898\begin{center} 36899 \tt 36900 inver(point(1,2,-2)) 36901\end{center} 36902returns and draws the point $(2/9, 4/9, -4/9)$, which is the inversion 36903of $(1,2,-2)$. 36904% Output: 36905% \begin{center} 36906% % \includegraphics[width=0.5\textwidth]{xcas-homothety1.png} 36907% \end{center} 36908% Input: 36909% \begin{center} 36910% \tt 36911% inver(sphere(point(1,0,0),1)) 36912% \end{center} 36913% Output: 36914% \begin{center} 36915% % \includegraphics[width=0.5\textwidth]{} 36916% \end{center} 36917% Input: 36918% \begin{center} 36919% \tt 36920% inver(sphere(point(1,0,0),1/2)) 36921% \end{center} 36922% Output: 36923% \begin{center} 36924% % \includegraphics[width=0.5\textwidth]{} 36925% \end{center} 36926 36927With a third argument of a geometric object, \texttt{inversion} 36928returns and draws the transformed object.\\\\ 36929Input: 36930\begin{center} 36931 \tt 36932 inversion(point(0,0,0), 2, point(1,2,-2)) 36933\end{center} 36934returns and draws $(2/9,4/9,-4/9)$, as above. 36935% Input: 36936% \begin{center} 36937% \tt 36938% inversion(point(0,0,0), 2, sphere(point(1,0,0),1)) 36939% \end{center} 36940% Output: 36941% \begin{center} 36942% % \includegraphics[width=0.5\textwidth]{xcas-homothety2.png} 36943% \end{center} 36944% Input: 36945% \begin{center} 36946% \tt 36947% inversion(point(0,0,0), 2, sphere(point(1,0,0),1/2)) 36948% \end{center} 36949% Output: 36950% \begin{center} 36951% % \includegraphics[width=0.5\textwidth]{xcas-homothety3.png} 36952% \end{center} 36953 36954\subsection{Orthogonal projection in space: \texttt{projection}\index{projection}} 36955\label{sec:3dprojection} 36956 36957See section \ref{sec:2dprojection} for projections in the plane. 36958 36959The \texttt{projection} command takes one or two arguments. The 36960first argument is a geometric object. The second optional 36961argument is a point. The command will project the point onto the 36962object. 36963 36964With one argument, \texttt{projection} returns a new command which 36965projects a point.\\ 36966Input: 36967\begin{center} 36968 \tt 36969 p1 := projection(line(point(0,0,0), point(1,1,1))) 36970\end{center} 36971then: 36972\begin{center} 36973 \tt 36974 p1(point(1,0,0)) 36975\end{center} 36976returns and draws the point $(1/3,1/3,1/3)$, which is the projection 36977of $(1,0,0)$ onto the line. 36978% Output: 36979% \begin{center} 36980% % \includegraphics[width=0.5\textwidth]{xcas-homothety1.png} 36981% \end{center} 36982 36983\noindent 36984Input: 36985\begin{center} 36986 \tt 36987 p2 := projection(plane(point(1,0,0),point(0,0,0),point(1,1,1)) 36988\end{center} 36989then: 36990\begin{center} 36991 \tt 36992 p2(point(0,0,1)) 36993\end{center} 36994returns and draws the point $(0,1/2,1/2)$, which is the projection of 36995the point $(0,0,1)$ onto the plane. 36996% Output: 36997% \begin{center} 36998% % \includegraphics[width=0.5\textwidth]{} 36999% \end{center} 37000% 37001%\noindent 37002%Input: 37003%\begin{center} 37004% \tt 37005% p3 := projection(sphere(point(1,0,0),1)) 37006%\end{center} 37007%then: 37008%\begin{center} 37009% \tt 37010% p3(point(0,0,1)) 37011%\end{center} 37012%returns and draws the point, which is the projection of $(0,0,1)$ onto 37013%the sphere. 37014%Output: 37015% \begin{center} 37016% % \includegraphics[width=0.5\textwidth]{} 37017% \end{center} 37018 37019With a second argument of a point, \texttt{inversion} 37020returns and draws the projection of the point.\\ 37021Input: 37022\begin{center} 37023 \tt 37024 projection(line(point(0,0,0), point(1,1,1)), point(1,0,0)) 37025\end{center} 37026returns and draws the point $(1/3,1/3,1/3)$, as above. 37027% Output: 37028% \begin{center} 37029% % \includegraphics[width=0.5\textwidth]{xcas-homothety2.png} 37030% \end{center} 37031% Input: 37032% \begin{center} 37033% \tt 37034% projection(plane(point(1,0,0), point(0,0,0), point(1,1,1)), point(0,0,1)) 37035% \end{center} 37036% Output: 37037% \begin{center} 37038% % \includegraphics[width=0.5\textwidth]{xcas-homothety3.png} 37039% \end{center} 37040% Input: 37041% \begin{center} 37042% \tt 37043% projection(sphere(point(1,0,0),1),point(0,0,1)) 37044% \end{center} 37045% Output: 37046% \begin{center} 37047% % \includegraphics[width=0.5\textwidth]{} 37048% \end{center} 37049 37050\section{Surfaces} 37051 37052\subsection{Cones: \texttt{cone}\index{cone}} 37053 37054The \texttt{cone} command takes as arguments a point \texttt{A}, a 37055direction vector \texttt{v} and a real number \texttt{t}. 37056 37057\texttt{cone} returns and draws the cone with vertex \texttt{A} 37058opening in the direction \texttt{v} with an aperture of \texttt{2t}.\\ 37059Input: 37060\begin{center} 37061 \tt 37062 cone([0,1,0],[0,0,1],pi/3) 37063\end{center} 37064Output: 37065\begin{center} 37066 \includegraphics[width=0.5\textwidth]{xcas-3dcone.png} 37067\end{center} 37068 37069\subsection{Half-cones: \texttt{half\_cone}\index{half\_cone}} 37070 37071The \texttt{half\_cone} command takes as arguments a point \texttt{A}, a 37072direction vector \texttt{v} and a real number \texttt{t}. 37073 37074\texttt{half\_cone} returns and draws the half cone with vertex \texttt{A} 37075opening in the direction \texttt{v} with an aperture of \texttt{2t}.\\ 37076Input: 37077\begin{center} 37078 \tt 37079 half\_cone([0,1,0],[0,0,1],pi/3) 37080\end{center} 37081Output: 37082\begin{center} 37083 \includegraphics[width=0.5\textwidth]{xcas-3dhalfcone.png} 37084\end{center} 37085 37086\subsection{Cylinders: \texttt{cylinder}\index{cylinder}} 37087 37088The \texttt{cylinder} command takes as arguments a point \texttt{A}, a 37089direction vector \texttt{v} and a real number \texttt{r}. 37090 37091\texttt{cylinder} returns and draws the cylinder with axis through \texttt{A} 37092in the direction \texttt{v} with a radius of \texttt{r}.\\ 37093Input: 37094\begin{center} 37095 \tt 37096 cylinder([0,1,0],[0,0,1],3) 37097\end{center} 37098Output: 37099\begin{center} 37100 \includegraphics[width=0.5\textwidth]{xcas-3dcylinder.png} 37101\end{center} 37102 37103\subsection{Spheres: \texttt{sphere}\index{sphere}} 37104 37105The \texttt{sphere} command takes two arguments; either two points or 37106a point and a real number. 37107 37108With two points as arguments, \texttt{sphere} returns and draws the 37109sphere with a diameter specified by the points.\\ 37110Input: 37111\begin{center} 37112 \tt 37113 sphere([-2,0,0],[2,0,0]) 37114\end{center} 37115Output: 37116\begin{center} 37117 \includegraphics[width=0.5\textwidth]{xcas-3dsphere.png} 37118\end{center} 37119 37120With a point and a real number as arguments, \texttt{sphere} returns 37121and draws the sphere centered at the point with radius given by the 37122number.\\ 37123Input: 37124\begin{center} 37125 \tt 37126 sphere([0,0,0],2) 37127\end{center} 37128returns and draws the same sphere as above. 37129% Output: 37130% \begin{center} 37131% \includegraphics[width=0.5\textwidth]{} 37132% \end{center} 37133 37134\subsection{The graph of a function of two variables: \texttt{funcplot}\index{funcplot}} 37135 37136The \texttt{funcplot} command takes two arguments, an expression with 37137two variables and a list of the two variables. 37138 37139\texttt{funcplot} returns and draws the graph of the expression.\\ 37140Input: 37141\begin{center} 37142 \tt 37143 funcplot(x\^{}2 + y\^{}2, [x,y]) 37144\end{center} 37145Output: 37146\begin{center} 37147 \includegraphics[width=0.5\textwidth]{xcas-3dfuncplot.png} 37148\end{center} 37149 37150\subsection{The graph of parametric equations in space: \texttt{paramplot}\index{paramplot}} 37151 37152The \texttt{paramplot} command takes two arguments, a list of three 37153expressions involving two parameters, and a list of the two parameters. 37154 37155\texttt{paramplot} returns and draws the parameterized surface; the 37156command \texttt{paramplot([f(u,v),g(u,v),h(u,v)],[u,v])} will return 37157and draw the surface given by \texttt{x=f(u,v)}, \texttt{y=g(u,v)}, 37158\texttt{z=h(u,v)}.\\ 37159Input: 37160\begin{center} 37161 \tt 37162 paramplot([u*cos(v),u*sin(v),u],[u,v]) 37163\end{center} 37164Output: 37165\begin{center} 37166 \includegraphics[width=0.5\textwidth]{xcas-3dparamplot.png} 37167\end{center} 37168 37169\section{Solids} 37170 37171\subsection{Cubes: \texttt{cube}\index{cube}} 37172 37173The \texttt{cube} command takes as arguments three points, \texttt{A}, 37174\texttt{B} and \texttt{C}. 37175 37176\texttt{cube} returns and draws the following cube: 37177\begin{itemize} 37178 \item One edge is \texttt{AB}. 37179 \item One face is in the plane \texttt{ABC}, on the same side of 37180 line \texttt{AB} as is \texttt{C}. 37181 \item The cube is on the side of plane \texttt{ABC} that makes the 37182 points \texttt{A}, \texttt{B} and \texttt{C} counterclockwise. 37183\end{itemize} 37184Input: 37185\begin{center} 37186 \tt 37187 C1 := cube([0,0,0],[0,4,0],[0,0,1]) 37188\end{center} 37189Output: 37190\begin{center} 37191 \includegraphics[width=0.5\textwidth]{xcas-3dcube.png} 37192\end{center} 37193Input: 37194\begin{center} 37195 \tt 37196 c1,c2,c3,c4,c5,c6,c7,c8 := vertices(C1) 37197\end{center} 37198Output: 37199\begin{center} 37200 \includegraphics[width=0.5\textwidth]{xcas-3dcube2.png} 37201\end{center} 37202Input: 37203\begin{center} 37204 \tt 37205 faces(C1) 37206\end{center} 37207Output: 37208\begin{center} 37209 \tt 37210 [[[0,0,0],[0,4,0],[0,4,4],[0,0,4]],\\{} 37211 [[4,0,0],[4,4,0],[4,4,4],[4,0,4]],\\{} 37212 [[0,0,0],[4,0,0],[4,0,4],[0,0,4]],\\{} 37213 [[0,0,0],[0,4,0],[4,4,0],[4,0,0]],\\{} 37214 [[0,4,0],[0,4,4],[4,4,4],[4,4,0]],\\{} 37215 [[0,0,4],[4,0,4],[4,4,4],[0,4,4]]] 37216\end{center} 37217 37218\noindent 37219Input: 37220\begin{center} 37221 \tt 37222 C2 := cube([0,0,0],[0,4,0],[0,0,-1]) 37223\end{center} 37224Output: 37225\begin{center} 37226 \includegraphics[width=0.5\textwidth]{xcas-3dcube3.png} 37227\end{center} 37228Input: 37229\begin{center} 37230 \tt 37231 a1,a2,a3,a4,a5,a6,a7,a8 := vertices(C2) 37232\end{center} 37233Output: 37234\begin{center} 37235 \includegraphics[width=0.5\textwidth]{xcas-3dcube4.png} 37236\end{center} 37237 37238\subsection{Tetrahedrons: \texttt{tetrahedron}\index{tetrahedron} \texttt{pyramid}\index{pyramid}} 37239 37240The \texttt{tetrahedron} (or \texttt{pyramid}) command takes as 37241arguments three or four points. 37242 37243When given three points \texttt{A}, \texttt{B} and \texttt{C} as 37244arguments, \texttt{tetrahedron} returns and draws the regular 37245tetrahedron given by:\\ 37246\begin{itemize} 37247 \item One edge is \texttt{AB}. 37248 \item One face is in the plane \texttt{ABC}, on the same side of 37249 line \texttt{AB} as is \texttt{C}. 37250 \item The tetrahedron is on the side of plane \texttt{ABC} that makes the 37251 points \texttt{A}, \texttt{B} and \texttt{C} counterclockwise. 37252\end{itemize} 37253Input: 37254\begin{center} 37255 \tt 37256 tetrahedron([-2,0,0],[2,0,0],[0,2,0]) 37257\end{center} 37258or: 37259\begin{center} 37260 \tt 37261 pyramid([-2,0,0],[2,0,0],[0,2,0]) 37262\end{center} 37263Output: 37264\begin{center} 37265 \includegraphics[width=0.5\textwidth]{xcas-3dtetra.png} 37266\end{center} 37267 37268\noindent 37269Input: 37270\begin{center} 37271 \tt 37272 tetrahedron([-2,0,0],[2,0,0],[0,2,0],[0,0,2]) 37273\end{center} 37274Output: 37275\begin{center} 37276 \includegraphics[width=0.5\textwidth]{xcas-3dtetra2.png} 37277\end{center} 37278 37279\subsection{Parallelepipeds: \texttt{parallelepiped}\index{parallelepiped}} 37280 37281The \texttt{parallelepiped} command takes as arguments four points, 37282\texttt{A}, \texttt{B}, \texttt{C} and \texttt{D}. 37283 37284\texttt{parallelepiped} returns and draws the parallelepiped 37285determined by the edges \texttt{AB}, \texttt{AC} and \texttt{AD}.\\ 37286Input: 37287\begin{center} 37288 \tt 37289 parallelepiped([0,0,0],[5,0,0],[0,5,0],[0,0,5]) 37290\end{center} 37291Output: 37292\begin{center} 37293 \includegraphics[width=0.5\textwidth]{xcas-3dppiped.png} 37294\end{center} 37295 37296\noindent 37297Input: 37298\begin{center} 37299 \tt 37300 p := parallelepiped([0,0,0],[5,0,0],[0,3,0],[0,0,2]):; 37301\end{center} 37302then: 37303\begin{center} 37304 \tt 37305 c1, c2, c3, c4, c5, c6, c7, c8 := vertices(p); 37306\end{center} 37307Output: 37308\begin{center} 37309 \includegraphics[width=0.5\textwidth]{xcas-3dppiped2.png} 37310\end{center} 37311 37312\subsection{Prisms: \texttt{prism}\index{prism}} 37313 37314The \texttt{prism} command takes two arguments, a list of coplanar 37315points \texttt{[A,B,...]} and an additional point \texttt{A1}. 37316 37317\texttt{prism} returns and draws the prism whose base is the polygon 37318determined by the points \texttt{A}, \texttt{B}, \dots, and with edges 37319parallel to \texttt{AA1}.\\ 37320Input: 37321\begin{center} 37322 \tt 37323 prism([[0,0,0],[5,0,0],[0,5,0],[-5,5,0]],[0,0,5]) 37324\end{center} 37325Output: 37326\begin{center} 37327 \includegraphics[width=0.5\textwidth]{xcas-3dprism.png} 37328\end{center} 37329 37330\subsection{Polyhedra: \texttt{polyhedron}\index{polyhedron}} 37331 37332The \texttt{polyhedron} command takes as argument a sequence of points. 37333 37334\texttt{polyhedron} returns and draws the convex polygon whose 37335vertices are from the list of points such that the remaining points 37336are inside or on the surface of the polyhedron.\\ 37337Input: 37338\begin{center} 37339 \tt 37340 polyhedron([0,0,0],[-2,0,0],[2,0,0],[0,2,0],[0,0,2]) 37341\end{center} 37342Output: 37343\begin{center} 37344 \includegraphics[width=0.5\textwidth]{xcas-3dpolyhedron.png} 37345\end{center} 37346 37347\subsection{Vertices: \texttt{vertices}\index{vertices}} 37348 37349The \texttt{vertices} command takes as argument a polyhedron. 37350 37351\texttt{vertices} returns and draws a list of the vertices of the 37352polyhedron.\\ 37353Input: 37354\begin{center} 37355 \tt 37356 V := vertices(polyhedron([0,0,0],[-2,0,0],[2,0,0],[0,2,0],[0,0,2])) 37357\end{center} 37358then: 37359\begin{center} 37360 \tt 37361 coordinates(V) 37362\end{center} 37363Output: 37364\begin{center} 37365 \tt 37366 [[0,0,0],[-2,0,0],[2,0,0],[0,2,0],[0,0,2]] 37367\end{center} 37368% Output: 37369% \begin{center} 37370% % \includegraphics[width=0.5\textwidth]{} 37371% \end{center} 37372 37373\subsection{Faces: \texttt{faces}\index{faces}} 37374 37375The \texttt{faces} command takes as argument a polyhedron. 37376 37377\texttt{faces} returns a list of the faces of the polyhedron.\\ 37378Input: 37379\begin{center} 37380 \tt 37381 faces(polyhedron([1,-1,0],[1,1,0],[0,0,2],[0,0,-2],[-1,1,0],[-1,-1,0])) 37382\end{center} 37383Output: 37384\begin{center} 37385 \tt 37386 [[[1,-1,0],[1,1,0],[0,0,2]],[[1,-1,0],[1,1,0],[0,0,-2]],\\{} 37387 [[1,-1,0],[0,0,2],[-1,-1,0]],[[1,-1,0],[0,0,-2],[-1,-1,0]],\\{} 37388 [[1,1,0],[0,0,2],[-1,1,0]],[[1,1,0],[0,0,-2],[-1,1,0]],\\{} 37389 [[0,0,2],[-1,1,0],[-1,-1,0]],[[0,0,-2],[-1,1,0],[-1,-1,0]]] 37390\end{center} 37391% Output: 37392% \begin{center} 37393% % \includegraphics[width=0.5\textwidth]{} 37394% \end{center} 37395 37396\subsection{Edges: \texttt{line\_segments}\index{line\_segments}} 37397 37398The \texttt{line\_segments} command takes as argument a polyhedron. 37399 37400\texttt{line\_segments} returns and draws a list of the edges of the polyhedron.\\ 37401Input: 37402\begin{center} 37403 \tt 37404 line\_segments(polyhedron([0,0,0],[-2,0,0],[2,0,0],[0,2,0],[0,0,2])) 37405\end{center} 37406Output: 37407\begin{center} 37408 % \includegraphics[width=0.5\textwidth]{} 37409\end{center} 37410Input: 37411\begin{center} 37412 \tt 37413 line\_segments(polyhedron([0,0,0],[-2,0,0],[2,0,0],[0,2,0],[0,0,2]))[1] 37414\end{center} 37415Output: 37416\begin{center} 37417 \includegraphics[width=0.5\textwidth]{xcas-3dlinesegs.png} 37418\end{center} 37419 37420\section{Platonic solids} 37421 37422To specify a Platonic solid, \texttt{Xcas} works with the center, a vertex 37423and a third point to specify a plane of symmetry. 37424To speed up calculations, it may be useful to use approximate 37425calculations, which can be ensured with the \texttt{evalf} command. 37426For example, instead of:\\ 37427Input: 37428\begin{center} 37429 \tt 37430 centered\_cube([0,0,0],[3,2,1],[1,1,0]) 37431\end{center} 37432it would typically be better to use:\\ 37433Input: 37434\begin{center} 37435 \tt 37436 centered\_cube(evalf([0,0,0],[3,2,1],[1,1,0])) 37437\end{center} 37438 37439\subsection{Centered tetrahedra: \texttt{centered\_tetrahedron}\index{centered\_tetrahedron}} 37440 37441The \texttt{centered\_tetrahedron} command takes as arguments three 37442points, \texttt{A}, \texttt{B} and \texttt{C}. 37443 37444\texttt{centered\_tetrahedron} returns and draws the tetrahedron 37445centered at \texttt{A}, with a vertex at \texttt{B} and another vertex 37446on the plane \texttt{ABC}.\\ 37447Input: 37448\begin{center} 37449 \tt 37450 centered\_tetrahedron([0,0,0],[0,0,6],[0,1,0]) 37451\end{center} 37452Output: 37453\begin{center} 37454 \includegraphics[width=0.5\textwidth]{xcas-3dctetra.png} 37455\end{center} 37456 37457\subsection{Centered cubes: \texttt{centered\_cube}\index{centered\_cube}} 37458 37459The \texttt{centered\_cube} command takes as arguments three points, 37460\texttt{A}, \texttt{B} and \texttt{C}. 37461 37462\texttt{centered\_cube} returns and draws the cube centered at 37463\texttt{A} which has \texttt{B} as a vertex and \texttt{ABC} as a 37464plane of symmetry. This plane of symmetry has an edge of the 37465cube containing \texttt{B}, the other endpoint of this edge is on the 37466same side of line \texttt{AB} as \texttt{C} is.\\ 37467Input: 37468\begin{center} 37469 \tt 37470 centered\_cube([0,0,0],[3,3,3],[0,1,0]) 37471\end{center} 37472Output: 37473\begin{center} 37474 \includegraphics[width=0.5\textwidth]{xcas-3dccube.png} 37475\end{center} 37476 37477\noindent 37478Input: 37479\begin{center} 37480 \tt 37481 centered\_cube([0,0,0],[3,3,3],[0,-1,0]) 37482\end{center} 37483Output: 37484\begin{center} 37485 \includegraphics[width=0.5\textwidth]{xcas-3dccube2.png} 37486\end{center} 37487 37488Note that there are two cubes centered at \texttt{A}, with a vertex at 37489\texttt{B} and with a plane of symmetry \texttt{ABC}. 37490Each cube has an edge containing \texttt{B} that's contained in plane 37491of symmetry, these edges are on opposite sides of the line \texttt{AB}. 37492The cube that \texttt{cube} returns is the cube whose edge is on the 37493same side of \texttt{AB} as the point \texttt{C}. 37494 37495\subsection{Octahedra: \texttt{octahedron}\index{octahedron}} 37496 37497The \texttt{octahedron} command takes as arguments three points 37498\texttt{A}, \texttt{B} and \texttt{C}. 37499 37500\texttt{octahedron} returns and draws the octahedron centered at 37501\texttt{A} which has a vertex at \texttt{B} and with four vertices in 37502the plane \texttt{ABC}.\\ 37503Input: 37504\begin{center} 37505 \tt 37506 octahedron([0,0,0],[0,0,5],[0,1,0]) 37507\end{center} 37508or: 37509\begin{center} 37510 \tt 37511 octahedron([0,0,0],[0,5,0],[0,0,1]) 37512\end{center} 37513or: 37514\begin{center} 37515 \tt 37516 octahedron([0,0,0],[5,0,0],[0,0,1]) 37517\end{center} 37518Output: 37519\begin{center} 37520 \includegraphics[width=0.5\textwidth]{xcas-3doct.png} 37521\end{center} 37522 37523\subsection{Dodecahedra: \texttt{dodecahedron}\index{dodecahedron}} 37524 37525The \texttt{dodecahedron} command takes as arguments three points, 37526\texttt{A}, \texttt{B} and \texttt{C}. 37527 37528\texttt{dodecahedron} returns and draws the dodecahedron centered at 37529\texttt{A} with a vertex at \texttt{B} and with an axis of symmetry in 37530the plane \texttt{ABC}. (Note that each face is a pentagon, but will 37531be drawn with one of its diagonals and so will show up as a trapezoid 37532and a triangle.\\ 37533Input: 37534\begin{center} 37535 \tt 37536 dodecahedron([0,0,0],[0,0,5],[0,1,0]) 37537\end{center} 37538Output: 37539\begin{center} 37540 \includegraphics[width=0.5\textwidth]{xcas-3ddod.png} 37541\end{center} 37542 37543\noindent 37544Input: 37545\begin{center} 37546 \tt 37547 dodecahedron([0,0,0],[0,2,sqrt(5)/2 + 3/2], [0,0,1]) 37548\end{center} 37549Output: 37550\begin{center} 37551 \includegraphics[width=0.5\textwidth]{xcas-3ddod2.png} 37552\end{center} 37553 37554\subsection{Icosahedra: \texttt{icosahedron}\index{icosahedron}} 37555 37556The \texttt{icosahedron} command takes as arguments three points, 37557\texttt{A}, \texttt{B} and \texttt{C}. 37558 37559\texttt{icosahedron} returns and draws the icosahedron centered at 37560\texttt{A} with a vertex at \texttt{B} and such that 37561the plane \texttt{ABC} contains one of the vertices closest to 37562\texttt{B}.\\ 37563Input: 37564\begin{center} 37565 \tt 37566 icosahedron([0,0,0],[0,0,5],[0,1,0]) 37567\end{center} 37568Output: 37569\begin{center} 37570 \includegraphics[width=0.5\textwidth]{xcas-3dico.png} 37571\end{center} 37572 37573\noindent 37574Input: 37575\begin{center} 37576 \tt 37577 icosahedron([0,0,0],[0,0,sqrt(5)], [2,1,0]) 37578\end{center} 37579Output: 37580\begin{center} 37581 \includegraphics[width=0.5\textwidth]{xcas-3dico2.png} 37582\end{center} 37583 37584% \section{Figures and proofs of exercises with \texttt{Xcas}} 37585 37586% \subsection{Exercise 1} 37587 37588% \subsection{Exercise 2} 37589 37590% \subsection{Exercise 3, extension of exercise 2} 37591 37592\chapter{Multimedia} 37593 37594\section{Sounds} 37595 37596\subsection{Reading a \texttt{wav} file: \texttt{readwav}\index{readwav}} 37597 37598The \texttt{readwav} command takes as argument a sound file stored in 37599WAV format (file extension: \texttt{.wav}), given as a string. 37600 37601\texttt{readwav} returns a vector consisting of: 37602\begin{itemize} 37603 \item A list consisting of: 37604 37605 \begin{itemize} 37606 \item The number of channels (generally 1 for mono and 2 for 37607 stereo). 37608 \item The number of bits (generally 16). 37609 \item The sampling frequency (44100 for a CD quality sound). 37610 \item The number of bytes (excluding the header); i.e., the number 37611 of seconds times the sampling frequency times the number of 37612 bits/8 times the number of channels. 37613 \end{itemize} 37614 \item A list of digital sound data for each channel. 37615\end{itemize} 37616The result of \texttt{readwav} is typically stored in a variable.\\ 37617For example, if \texttt{sound.wav} is a sound file for a one-second 37618sound in CD quality on a 16-bit channel:\\ 37619Input: 37620\begin{center} 37621 \tt 37622 s := readwav("sound.wav") 37623\end{center} 37624then: 37625\begin{center} 37626 \tt 37627 s[0] 37628\end{center} 37629Output: 37630\begin{center} 37631 \tt 37632 [1,16,44100,88200] 37633\end{center} 37634Input: 37635\begin{center} 37636 \tt 37637 size(s) 37638\end{center} 37639Output: 37640\begin{center} 37641 \tt 37642 2 37643\end{center} 37644which is the number of channels plus 1.\\ 37645Input: 37646\begin{center} 37647 \tt 37648 size(s[1]) 37649\end{center} 37650Output: 37651\begin{center} 37652 \tt 37653 44100 37654\end{center} 37655 37656\subsection{Writing a \texttt{wav} file: \texttt{writewav}\index{writewav}} 37657 37658The \texttt{writewav} command writes sound data to a WAV file. 37659 37660The \texttt{writewav} command takes two arguments; the name of a file 37661and the sound data. The sound data can either be in the same format 37662as that returned by the \texttt{readwav} command or (for a mono sound) 37663a list of the digital data of the sound which will us the default 37664parameters (16 bits, 44100 Hz). 37665 37666\texttt{writewav} writes the sound to the named file. 37667 37668\noindent 37669Input: 37670\begin{center} 37671 \tt 37672 writewav("la.wav",2\^{}14*sin(2*pi*440*soundsec(1))) 37673\end{center} 37674The file \texttt{la.wav} will then contain a sound of frequency 440 Hz 37675sampled 44100 times per second. 37676 37677\subsection{Listening to a digital sound: \texttt{playsnd}\index{playsnd}} 37678 37679The \texttt{playsnd} command takes as argument digitized sound data, 37680which can be read with the \texttt{readwav} command or generated with 37681the help of \texttt{soundsec}. The arguments are either in the format 37682of the output of the \texttt{readwav} command or a list of sampled 37683data for mono sound with the default settings of 1 channel, 16 bits 37684and 44100 Hz. 37685 37686\texttt{playsnd} plays the given sound. 37687 37688\subsection{Preparing digital sound data: \texttt{soundsec}\index{soundsec}} 37689 37690The \texttt{soundsec} prepares sound data in the form of a vector. 37691 37692The \texttt{soundsec} command takes as argument a real number, and an 37693optional second argument of another real number. 37694 37695\texttt{soundsec} returns sound data with duration (in seconds) given 37696by the argument. The optional second argument determines the sampling 37697frequency. The sound data is returned as a vector, whose $i$th 37698element is the time corresponding to index $i$.\\ 37699Input: 37700\begin{center} 37701 \tt 37702 soundsec(2.5) 37703\end{center} 37704returns sound data 2.5 seconds long sampled at the default frequency of 3770544100 Hz.\\ 37706Input: 37707\begin{center} 37708 \tt 37709 soundsec(1,22050) 37710\end{center} 37711returns sound data 1 second long sampled at the frequency of 22050 Hz.\\ 37712Input: 37713\begin{center} 37714 \tt 37715 sin(2*pi*440*soundsec(1.3)) 37716\end{center} 37717returns a sinusoid with frequency 440 Hz sampled 44100 times per 37718second for 1.3 seconds. 37719 37720\section{Images} 37721 37722\subsection{Image structure in \texttt{Xcas}} 37723\label{sec:imagestruct} 37724 37725An image in \texttt{Xcas} is a list with the following elements. 37726\begin{itemize} 37727 \item The first element is itself a list of three integers; the 37728 number of channels (which will be 3 or 4), the number \texttt{n} of 37729 rows and the number \texttt{p} of columns used for the dimension of 37730 the image. Each channel will be an \texttt{n$\times$p} matrix of 37731 integers between 0 and 255. 37732 \item A red channel. 37733 \item A green channel. 37734 \item A transparency channel. 37735 \item A blue channel. 37736\end{itemize} 37737The color of the point at line \texttt{i} and column \texttt{j} is 37738determined by the values of the \texttt{i},\texttt{j}th entry of 37739the matrices. 37740 37741Note that the number of points in the structure isn't necessarily the 37742same as the number of pixels on the screen when it is drawn. It is 37743possible that a single point in the structure is represented by a 37744small rectangle of pixels when it is displayed on the screen. 37745 37746\subsection{Reading images: \texttt{readrgb}\index{readrgb}} 37747 37748The \texttt{readrgb} command takes as argument the name of an image 37749file (it can be \texttt{.jpg}, \texttt{.png} or \texttt{.gif}). 37750 37751\texttt{readrgb} returns an \texttt{Xcas} image structure for the 37752image (see section \ref{sec:imagestruct}). 37753 37754\subsection{Viewing images} 37755 37756\texttt{Xcas} can display images in rectangles in 37757two-dimensions or on surfaces in three-dimensions with the 37758\texttt{gl\_texture} property of the object.\\ 37759Input: 37760\begin{center} 37761 \tt 37762 rectangle(0,200,1/2,gl\_texture="picture.jpg") 37763\end{center} 37764Input: 37765\begin{center} 37766 \tt 37767 sphere([0,0,0],1,gl\_material=[gl\_texture,"picture.jpg"]) 37768\end{center} 37769 37770\subsection{Creating or recreating images: \texttt{writergb}\index{writergb}} 37771 37772The \texttt{writergb} writes images to \texttt{png} files; the image 37773can either be read in with \texttt{readrgb} or created by 37774\texttt{writergb} itself. 37775 37776\subsubsection{Writing images given in \texttt{Xcas} format} 37777 37778To write an existing image, the \texttt{writergb} command takes two 37779arguments, a file name and an image in \texttt{Xcas} format (see 37780section \ref{sec:imagestruct}). 37781 37782\texttt{writergb} writes the image to the given file. 37783 37784As an example, suppose the following image is stored in file 37785\texttt{image1234.jpg}. 37786\begin{center} 37787 \includegraphics[width=0.75\textwidth]{xcas-image1234.png} 37788\end{center} 37789Using \texttt{readrgb},\\ 37790\begin{center} 37791 \tt 37792 a := readrgb("image1234.jpg") 37793\end{center} 37794the variable \texttt{a} will contain a list, 37795\begin{itemize} 37796 \item \texttt{a[0]} will be \texttt{[4,250,500]}, the number of 37797 channels, the height and the width of the image. 37798 \item \texttt{a[1]}, the red channel, 37799 \item \texttt{a[2]}, the green channel, 37800 \item \texttt{a[3]}, the transparency channel, 37801 \item \texttt{a[4]}, the blue channel. 37802\end{itemize} 37803The command\\ 37804Input: 37805\begin{center} 37806 \tt 37807 writergb("image2134.png",[a[0],a[2],a[1],a[3],a[4]]) 37808\end{center} 37809will produce an image file \texttt{image2134.png} which is simply 37810\texttt{image1234.png} with the green and red colors switched. 37811\begin{center} 37812 \includegraphics[width=0.75\textwidth]{xcas-image2134.png} 37813\end{center} 37814 37815% The \texttt{Xcas} image format can be typed in by hand, but that isn't 37816% feasible for large images.\\ 37817% Input: 37818% \begin{center} 37819% \tt 37820% writergb("test.png",[[4,2,2],[[255,0],[0,0]],[[0,255],[0,0]], 37821% [[255,125],[255,255]],[[0,0],[255,0]]]) 37822% \end{center} 37823 37824\subsubsection{Creating images} 37825 37826The \texttt{Xcas} image format can be typed in by hand.\\ 37827Input: 37828\begin{center} 37829 \tt 37830 writergb("image1.png",[[4,2,2],[[255,0],[0,0]],[[0,255],[0,0]], 37831 [[255,125],[255,255]],[[0,0],[255,0]]]) 37832\end{center} 37833creates a file \texttt{image1.png} containing an image 2 points by 2 37834points, the upper left point is red, the upper right point is a muted 37835green, the lower left point is blue, and the lower right point is black. 37836The transparency value of \texttt{125} for the upper right point makes 37837it partially transparent and mutes the color. 37838 37839For larger images, in some cases the matrix operations of 37840\texttt{Xcas} can be used to create the channels.\\ 37841Input: 37842\begin{center} 37843 \tt 37844 writergb("image2.png",[[4,300,300],makemat(0,300,300), 37845 makemat(0,300,300), makemat(255,300,300),makemat(0,300,300) + 37846 idn(300)*255]) 37847\end{center} 37848\begin{center} 37849 \includegraphics[width=0.5\textwidth]{xcas-image2.png} 37850\end{center} 37851 37852The \texttt{writergb} command can also take as input a 37853simplified version of the \texttt{Xcas} image description, which 37854doesn't involve stating the number of channels, the size of the image, 37855or the transparency. There is a full color version of this simplified 37856form and a grayscale version. 37857 37858To create a full color image, the \texttt{writergb} command takes four 37859arguments, the name of the file to store the image, the red channel 37860(matrix), the green channel and the blue channel.\\ 37861Input: 37862\begin{center} 37863 \tt 37864 writergb("image2.png",[[255,250],[0,120]],[[0,255],[0,0]],[[0,0],[255,100]]) 37865\end{center} 37866creates a file \texttt{image2.png} containing an image 2 points by 2 37867points, the upper left point is red (rgb value (255,0,0)), the upper 37868right point is yellow (rgb value (250,255,0)), the lower left point is 37869blue (rgb value (0,0,255) and the lower right point is violet (rgb 37870value (120,0,100)). 37871 37872To create a grayscale image, the \texttt{writergb} command takes two 37873arguments, the name of the file to store the image and a matrix 37874representing how dark each point is (where \texttt{0} is black and 37875\texttt{255} is white).\\ 37876Input: 37877\begin{center} 37878 \tt 37879 writergb("image3.png",[[65,125],[185,200]]) 37880\end{center} 37881creates a file \texttt{image3.png} containing an image 2 points by 2 37882points, the upper left point is dark gray, the upper 37883right point is medium gray, the lower left point is 37884light gray and the lower right point is even lighter gray. 37885 37886\chapter{Using \texttt{giac} inside a program} 37887 37888\section{Using \texttt{giac} inside a \texttt{C++} program} 37889 37890To use \texttt{giac} inside of a \texttt{C++} program, put 37891\begin{verbatim} 37892 #include <giac/giac.h> 37893\end{verbatim} 37894at the beginning of the file. To compile the file, use 37895\begin{verbatim} 37896 c++ -g progname.cc -lgiac -lgmp 37897\end{verbatim} 37898After compiling, there will be a file \texttt{a.out} which can be run 37899with the command 37900\begin{verbatim} 37901 ./a.out 37902\end{verbatim} 37903 37904For example, put the following program in a file named \texttt{pgcd.cc}. 37905\begin{verbatim} 37906// -*- compile-command: "g++ -g pgcd.cc -lgiac -lgmp" -*- 37907#include <giac/config.h> 37908#include <giac/giac.h> 37909 37910using namespace std; 37911using namespace giac; 37912 37913gen pgcd(gen a,gen b){ 37914 gen q,r; 37915 for (;b!=0;){ 37916 r=irem(a,b,q); 37917 a=b; 37918 b=r; 37919 } 37920 return a; 37921} 37922 37923int main(){ 37924 cout << "Enter 2 integers "; 37925 gen a,b; 37926 cin >> a >> b; 37927 cout << pgcd(a,b) << endl; 37928 return 0; 37929} 37930\end{verbatim} 37931After compiling this with 37932\begin{verbatim} 37933 c++ -g pgcd.cc -lgiac -lgmp 37934\end{verbatim} 37935and running it with 37936\begin{verbatim} 37937 ./a.out 37938\end{verbatim} 37939there will be a prompt 37940\begin{verbatim} 37941 Enter 2 integers 37942\end{verbatim} 37943After entering two integers, such as with 37944\begin{verbatim} 37945 Enter 2 integers 30 36 37946\end{verbatim} 37947the result will appear, 37948\begin{verbatim} 37949 6 37950\end{verbatim} 37951 37952\section{Defining new \texttt{giac} functions} 37953 37954New \texttt{giac} functions can be defined with a \texttt{C++} program. 37955All data in the program used in formal calculations needs to be 37956\texttt{gen} type. A variable \texttt{g} can be declared to be 37957\texttt{gen} type with 37958\begin{verbatim} 37959 gen g; 37960\end{verbatim} 37961In this case, \texttt{g.type} can have different values. 37962\begin{itemize} 37963 \item If \texttt{g.val} is an integer type \texttt{int}, then 37964 \texttt{g.type} will be \texttt{\_INT\_}. 37965 \item If \texttt{g.\_DOUBLE\_val} is a real \texttt{double}, 37966 \texttt{g.type} will be \texttt{\_DOUBLE\_}. 37967 \item If \texttt{g.\_SYMBptr} is type \texttt{symbolic}, then 37968 \texttt{g.type} will be \texttt{\_SYMB}. 37969 \item If \texttt{g.\_VECTptr} is a vector, type \texttt{vector}, then 37970 \texttt{g.type} will be \texttt{\_VECT}. 37971 \item If \texttt{g.\_ZINTptr} is an integer type \texttt{zint}, then 37972 \texttt{g.type} will be \texttt{\_ZINT}. 37973 \item If \texttt{g.\_IDNTptr} is an identifier, type \texttt{idnt}, then 37974 \texttt{g.type} will be \texttt{\_IDNT}. 37975 \item If \texttt{g.\_CPLXptr} is a complex type \texttt{complex}, then 37976 \texttt{g.type} will be \texttt{\_CPLX}. 37977\end{itemize} 37978 37979As an example, put the following program in a file called 37980\texttt{pgcd.cpp}. 37981\begin{verbatim} 37982// -*- mode:C++ ; compile-command: "g++ -I.. -fPIC -DPIC -g -c pgcd.cpp -o pgcd.lo && \ 37983// ln -sf pgcd.lo pgcd.o && \ 37984// gcc -shared pgcd.lo -lc -lgiac -Wl,-soname -Wl,libpgcd.so.0 -o \ 37985// libpgcd.so.0.0.0 && ln -sf libpgcd.so.0.0.0 libpgcd.so.0 && \ 37986// ln -sf libpgcd.so.0.0.0 libpgcd.so" -*- 37987using namespace std; 37988#include <stdexcept> 37989#include <cmath> 37990#include <cstdlib> 37991#include <giac/config.h> 37992#include <giac/giac.h> 37993//#include "pgcd.h" 37994 37995#ifndef NO_NAMESPACE_GIAC 37996namespace giac { 37997#endif // ndef NO_NAMESPACE_GIAC 37998 37999 gen monpgcd(const gen & a0,const gen & b0){ 38000 gen q,r,a=a0,b=b0; 38001 for (;b!=0;){ 38002 r=irem(a,b,q); 38003 a=b; 38004 b=r; 38005 } 38006 return a; 38007 } 38008 gen _monpgcd(const gen & args,GIAC_CONTEXT){ 38009 if ( (args.type!=_VECT) || (args._VECTptr->size()!=2)) 38010 setsizeerr(); 38011 vecteur &v=*args._VECTptr; 38012 return monpgcd(v[0],v[1]); 38013 } 38014 const string _monpgcd_s("monpgcd"); 38015 unary_function_eval __monpgcd(0,&_monpgcd,_monpgcd_s); 38016 unary_function_ptr at_monpgcd (&__monpgcd,0,true); 38017 38018 38019#ifndef NO_NAMESPACE_GIAC 38020} // namespace giac 38021#endif // ndef NO_NAMESPACE_GIAC 38022\end{verbatim} 38023After compiling this with the commands after the 38024\texttt{compile-command} in the header, namely 38025\begin{verbatim} 38026 g++ -I.. -fPIC -DPIC -g -c pgcd.cpp -o pgcd.lo && \ 38027 ln -sf pgcd.lo pgcd.o && \ 38028 gcc -shared pgcd.lo -lc -lgiac -Wl,-soname -Wl,libpgcd.so.0 -o \ 38029 libpgcd.so.0.0.0 && ln -sf libpgcd.so.0.0.0 libpgcd.so.0 && \ 38030 ln -sf libpgcd.so.0.0.0 libpgcd.so 38031\end{verbatim} 38032the new command can be inserted with the \texttt{insmod} command in 38033\texttt{giac}, where \texttt{insmod} takes the full absolute path of 38034the \texttt{libpgcd.so} file as argument.\\ 38035Input: 38036\begin{center} 38037 \tt 38038 insmod("/path/to/file/libpgcd.so") 38039\end{center} 38040Afterwords, the \texttt{monpgcd} command will be another \texttt{giac} 38041command.\\ 38042Input: 38043\begin{center} 38044 \tt 38045 monpgcd(30,36) 38046\end{center} 38047Output: 38048\begin{center} 38049 \tt 38050 6 38051\end{center} 38052 38053\end{document} 38054