1\documentclass[a4paper,11pt]{book} 2%\textwidth 11,8 cm 3%\textheight 17 cm 4\textheight 23 cm 5\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\\ 49University of Grenoble I\\ 50Corrected by Jay Belanger\\ 51Optimization section written by Luka Marohnić} 52\date{} 53\makeindex 54\usepackage{cutting} 55\begin{document} 56\newcommand{\asinh}{\,\,\mbox{asinh\,}} 57\newcommand{\atanh}{\,\,\mbox{atanh\,}} 58\maketitle 59% {\bf \centerline{Remerciements}} 60 61% \vspace{1cm} 62 63% Je remercie: 64% \begin{itemize} 65% \item Bernard Parisse for ses pr\'ecieux conseils andses remarques sur ce texte, 66 67% \end{itemize} 68 69\vfill 70 71 72\copyright\ 2002, 2007 Ren\'ee De Graeve, Bernard Parisse\\ 73\verb|renee.degraeve@wanadoo.fr|\\ 74\verb|bernard.parisse@ujf-grenoble.fr|\\ 75 76 77 78\newpage 79\tableofcontents 80 81\chapter{Index} 82\printindex 83 84\chapter{Introduction} 85 86\section{Notations used in this manual} 87 88In this manual, the information that you enter will be typeset in 89typewriter font. User input typically takes one of three forms: 90\begin{itemize} 91\item Commands that you enter on the command line.\\ 92For example, to compute the sin of $\pi/4$, you can type 93\begin{center} 94{\tt sin(pi/4)} 95\end{center} 96 97\item Commands requiring a prefix key.\\ 98These will be indicated by separating 99the prefix key and the standard key with a plus \texttt{+}. For 100example, to exit an \texttt{Xcas} session, you can type the control 101key along with the \texttt{q} key, which will be denoted 102\begin{center} 103{\tt Ctrl+Q} 104\end{center} 105 106\item Menu commands.\\ 107When denoting menu items, submenus will be connected using 108$\blacktriangleright$. For example, from within \texttt{Xcas} you 109can choose the \texttt{File} menu, then choose the \texttt{Open} 110submenu, and then choose the \texttt{File} item. This will be 111indicated by 112\begin{center} 113 {\tt File $\blacktriangleright$ Open $\blacktriangleright$ File} 114\end{center} 115\end{itemize} 116 117The index will use different typefaces for different parts of 118the language. The commands themselves will be written with normal 119characters, command options will be written in italics and values of 120commands or options will be written in typewriter font. For example 121(as you will see later), you can draw a blue parabola with the command 122\begin{center} 123 {\tt plotfunc(x\^{}2,color = blue)} 124\end{center} 125In the index, you will see 126\begin{itemize} 127\item 128 plotfunc, the command, written in normal text. 129\item 130 \textit{color}, the command option, written in italics. 131\item 132 \texttt{blue}, the value given to the option, written in typewriter 133 font. 134\end{itemize} 135 136\section{Interfaces for the \texttt{giac} library} 137 138The \texttt{giac} library is a C++ mathematics library. 139It comes with two interfaces for users to use it directly; a graphical 140interface and a command-line interface. 141 142The graphical interface is called \texttt{Xcas}, and is the most 143full-featured interface. As well being able to do symbolic and numeric 144calculations, it has its own programming language, it can draw graphs, 145it has a built-in spreadsheet, it can do dynamic geometry 146and turtle graphics. 147 148The command-line interface can be run inside a terminal. It can also 149do symbolic and numeric calculations and works with the programming 150language. In a graphical environment, the command-line interface can 151also be used to draw graphs. 152 153There is also a web version, which can be run through a browser, 154either over the internet or from local files. Other programs (for 155example, \texttt{TeXmacs}) have interfaces for the command-line 156version. 157 158\subsection{The \texttt{Xcas} interface} 159 160How you run \texttt{Xcas} in a graphical environment depends on which 161operating system you are using. 162\begin{itemize} 163 \item 164If you are using Unix, you can usually find 165an entry for the program in a menu provided by the environment. 166Otherwise, you can start it from a terminal by typing 167\begin{center} 168 {\tt xcas \&} 169\end{center} 170If for some reason \texttt{Xcas} becomes unresponsive, you can open a 171terminal and type 172\begin{center} 173 {\tt killall xcas} 174\end{center} 175That will kill any running \texttt{Xcas} processes. When you restart 176\texttt{Xcas}, you will be asked if you want to resume where you left 177off using an automatic backup file. 178\item 179If you are running Windows, you can use the explorer to go to the 180directory where \texttt{Xcas} is installed. In that directory will be 181a file called \texttt{xcas.bat}. Clicking on that file will start 182\texttt{Xcas}. 183\item 184If you are running Mac OS, you can use the Finder to go to the 185\texttt{xcas\_image.dmg} file and double-click it. Then double-click 186the \texttt{Xcas} disk icon. Finally, to launch \texttt{Xcas}, 187double-click the \texttt{Xcas} program. 188\end{itemize} 189When you start \texttt{Xcas}, a 190window will pop up with menu entries across the top, a bar 191indicating information about the current \texttt{Xcas} configuration, 192and an entry line you can use to enter commands. This interface will 193be described in more detail later, and you can get help 194from within \texttt{Xcas} with the menu item 195\begin{center} 196\texttt{Help$\blacktriangleright$Interface} 197\end{center} 198 199\subsection{The command-line interface} 200 201In Unix and MacOS you can run \texttt{giac} from a terminal with 202the command \texttt{icas} (the command \texttt{giac} also works). 203There are two ways to use the command-line interface. 204 205If you just want to evaluate one expression, you can give 206\texttt{icas} the expression (in quotes) as a command line argument. 207For example, to factor the polynomial $x^2-1$, you can type 208\begin{center} 209 {\tt icas 'factor(x\^{}2-1)'} 210\end{center} 211at a command prompt. The result will be 212\begin{center} 213 {\tt (x-1)*(x+1)} 214\end{center} 215and you will be returned to the operating system command line. 216 217If you want to evaluate several commands, you can enter an interactive 218\texttt{giac} session by entering the command 219\texttt{icas} (or \texttt{giac}) by itself at a command prompt. You 220will then be given a prompt specifically for \texttt{giac} commands, 221which will look like 222\begin{verbatim} 223 0>> 224\end{verbatim} 225You can enter a giac command at this prompt and get the result. 226\begin{verbatim} 227 0>> factor(x^2-1) 228 (x-1)*(x+1) 229 1>> 230\end{verbatim} 231After the result, you will be given another prompt for \texttt{giac} 232commands. You can exit this interactive session by typing 233\texttt{Ctrl+D}. 234 235You can also run \texttt{icas} in batch mode; that is, 236you can have \texttt{icas} run \texttt{giac} commands stored in a 237file. This can be done in Windows as well as Unix and Mac OS. 238To do this, simply enter 239\begin{verbatim} 240 icas filename 241\end{verbatim} 242at a command prompt, where \textit{filename} is the name of the file 243containing the \texttt{giac} commands. 244 245\subsection{The Firefox interface} 246 247You can run \texttt{giac} without installing it by using a 248javascript-enabled web browser. Using Firefox for this is highly 249recommended; Firefox will run \texttt{giac} several times faster than 250Chrome, for example, and Firefox also supports MathML natively. 251 252%This version won't have as much functionality as \texttt{Xcas}, but it 253%doesn't require any software beyond a web browser to be installed. 254 255To run \texttt{giac} through Firefox, you can open the url 256\url{https://www-fourier.ujf-grenoble.fr/~parisse/giac/xcasen.html}. 257At the top of this page is a button which will open a quick tutorial; 258the tutorial will also tell you how to install the necessary files to 259run \texttt{giac} through Firefox without being connected to the 260internet. 261 262\subsection{The TeXmacs interface} 263 264TeXmacs (\url{http://www.texmacs.org}) is a sophisticated word 265processor with special mathematical features. As well as being 266designed to nicely typeset mathematics, it can be used as a frontend 267for various mathematics programs, such as \texttt{giac}. 268 269Once you've started TeXmacs, you can interactively run \texttt{giac} 270within TeXmacs with the menu command 271\texttt{Insert$\blacktriangleright$Session$\blacktriangleright$Giac}. 272Once started, you can enter \texttt{giac} commands as you would in the 273command-line interface. The TeXmacs interface will also have a menu 274specifically for \texttt{giac} commands. 275 276Within TeXmacs, you can combine \texttt{giac} commands and output with 277ordinary text. To enter normal text within a \texttt{giac} session, 278use the menu item \texttt{Focus$\blacktriangleright$Insert Text Field 279Above}. You can reenter a \texttt{giac} entry line by clicking on it 280with a mouse. 281 282% \subsection{The Emacs interface} 283 284% You can run a \texttt{giac} session using \texttt{mupacs} 285% (\url{http://mupacs.sourceforge.net}), an Emacs mode for MuPAD, a 286% computer algebra system no longer independently available. Since this 287% mode was created for MuPAD, you 288 289\chapter{The \texttt{Xcas} interface} 290 291\section{The entry levels} 292 293The \texttt{Xcas} interface can run several independent calculation 294sessions, each session will be contained in a separate tab. Before 295you understand the \texttt{Xcas} interface, it would help to be 296familiar with the components of a session. 297 298Each session can have any number of input levels. Each input level 299will have a number to the left of it; the number is used to identify 300the input level. Each level can have one of the following: 301\begin{itemize} 302 \item A command line.\\ 303 This is the default; you can open a new 304 command line with \texttt{Alt+N}.\\ 305 You can enter a \texttt{giac} command (or a series of commands 306 separated by semicolons) on a command line and send it to be 307 evaluated by hitting enter. You can also scroll through the command 308 history with \texttt{Shift+Up} and \texttt{Shift+Down}. 309 310 If the output is a number or an expression, then it will appear in 311 blue text in a small area below the input region; this area is an 312 expression editor. There will be a scrollbar and a small \texttt{M} 313 to the right of this area; the \texttt{M} is a menu which gives you 314 various options. 315 316 If the output is a graphic, then it will appear in a graphing area 317 below the input region. To the right of the graphic will be a 318 control panel allowing you to manipulate the graphic. 319 320 \item An expression editor.\\ 321 You can open an expression editor with 322 \texttt{Alt+E}. 323 324 \item A two-dimensional geometry screen.\\ 325 You can open up such a 326 screen with \texttt{Alt+G}. 327 This level will have a screen, as well as a control panel, menus and a 328 command line to control the screen. 329 330 \item A three-dimensional geometry screen.\\ 331 You can open up such a 332 screen with \texttt{Alt+H}. 333 This level will have a screen, as well as a control panel, menus and 334 a command line to control the screen. 335 336 \item A turtle graphics screen.\\ 337 You can open up such a screen with 338 \texttt{Alt+D}. 339 This level will have a screen, as well as a program editor and 340 command line. 341 342 \item A spreadsheet.\\ 343 You can open up a spreadsheet with \texttt{Alt+T}. 344 A spreadsheet will be able to open a graphic screen. 345 346 \item A program editor.\\ 347 You can open up a program editor with 348 \texttt{Alt+P}. 349 350 \item A comment line. You can open up a comment line with 351 \texttt{Alt+C}. 352\end{itemize} 353Using commands discussed later, different types of levels can be 354combined to form a single hybrid level. Levels can also be moved up 355or down in a session, or even moved to a different session. 356 357The level containing the cursor is the \emph{current level}. The 358current level can be evaluated or re-evaluated by typing enter. 359 360A level can be selected (for later operations) by clicking on the 361number in the white box to the left of the level. Once selected, the 362box containing the number will turn black. You can select a range of 363levels by clicking on the number for the beginning level, and then 364holding the shift key while you click on the number for the ending level. 365 366\section{The starting window} 367 368When you first start \texttt{Xcas}, you will be given a largely blank window. 369\begin{center} 370 \includeimage{xcas-open.png} 371\end{center} 372The first row will be the main menus; you can 373save and load \texttt{Xcas} sessions, configure \texttt{Xcas} and its 374interface and run various commands with entries from these menus. 375 376The second row will be tabs; one tab for each session that you are 377running in \texttt{Xcas}. The tabs will contain the name of the 378sessions, or \texttt{Unnamed} if a session has no name. The first 379time you start \texttt{Xcas}, there will be only one unnamed session. 380 381The third row will contain various buttons. 382\begin{itemize} 383 \item The first button, \framebox{\tt ?}, will open the help index. 384 (The same as the \texttt{Help$\blacktriangleright$Index} menu entry.) 385 If there is a command on the command line, the help index 386 (see \ahrefloc{helpindex}{help index} 387 \begin{latexonly},p.\pageref{sssec:helpind}\end{latexonly}) 388 will open at this command. 389 390 \item The second button \framebox{\tt Save}, will save the session 391 in a file. The first time you click on it, you will be prompted for 392 a file name ending in \texttt{.xws} to save the session in. The 393 button will be pink if the session is not saved or if it has changed 394 since the last change, it will be green once the session is saved. 395 The name in the title will be the name of the file used to save the 396 session. 397 398\item The third button, which in the picture above is\\ 399 \framebox{\tt Config : exact real RAD 12 xcas 6.2148M}, is a status 400 line indicating the current \texttt{Xcas} configuration. 401 (See section \ref{sec:config}.) 402 If the session is unsaved, it will begin with \texttt{Config :}; if the 403 session is saved in a file \textit{filename.xws}, this button will 404 begin with \texttt{\texttt{Config }\textit{filename.xws}\texttt{ :}}. 405 Other information on this status line: 406 \begin{itemize} 407 \item \texttt{exact} or \texttt{approx}. 408 (See subsection \ref{ssec:approx}.) 409 This tells you whether 410 \texttt{Xcas} will give you exact values, such as 411 $\sqrt{2}$, when possible or to give you decimal approximations. 412 413 \item \texttt{real}, \texttt{cplx} or \texttt{CPLX}. 414 (See subsections \ref{ssec:complex} and \ref{ssec:cvars}.) 415 When this shows \texttt{real}, then (for example) \texttt{Xcas} will by 416 default only find real solutions of equations. When this shows 417 \texttt{cplx}, the \texttt{Xcas} will find complex solutions of 418 equations. When this shows \texttt{CPLX}, then \texttt{Xcas} will 419 regard variables as complex; for example, it won't simplify 420 \texttt{re(z)} (the real part of the variable $z$) to \texttt{z}. 421 422 \item \texttt{RAD} or \texttt{DEG}. 423 (See subsection \ref{ssec:angles}.) 424 This tells you whether 425 angles, as in trigonometric arguments, are measured in radians 426 or degrees. 427 428 \item An integer. (See subsection \ref{ssec:sigdig}, indicating 429 how many significant digits will be used in floating point calculations. 430 431 \item \texttt{xcas}, \texttt{maple}, \texttt{mupad} or \texttt{ti89}. 432 (See subsection \ref{ssec:lang}.) This tells you what syntax 433 \texttt{Xcas} will use. \texttt{Xcas} can be set to emulate the 434 languages of Maple, MuPAD or the TI89 series of calculators. 435 436 \item The last item indicates how much memory \texttt{Xcas} is using. 437 \end{itemize} 438 Clicking on this status line button will open a window where 439 you can configure the settings shown on this line as well as some 440 other settings; you can do the same with the menu item 441 \texttt{Cfg$\blacktriangleright$CAS Configuration}. 442 (See subsection \ref{ssec:confcomp}.) 443 444 \item The fourth button, \framebox{\tt STOP} (in red), can be used 445 to halt a computation which is running on too long. 446 447 \item The fifth button, \framebox{\tt Kbd}, can be used to toggle an 448 on-screen scientific keyboard at the bottom of the window. 449 \begin{center} 450 \includeimage{xcas-scientific-keyboard.png} 451 \end{center} 452 Along the right hand side of the keyboard are some keys that can 453 be used to change the keyboard. 454 \begin{itemize} 455 \item The \texttt{X} key will hide the 456 keyboard, just like pressing the \framebox{\tt Kbd} button again. 457 458 \item The \texttt{cmds} key will toggle a menu bar at the bottom of the 459 screen which can be used as an alternate menu or persistent 460 submenu. This bar will contain buttons, \texttt{home}, 461 \texttt{<{}<}, some menu titles, \texttt{>{}>}, \texttt{var}, 462 \texttt{cust} and \texttt{X}. 463 464 The \texttt{<{}<} and \texttt{>{}>} buttons will scroll through menu 465 items. Clicking on one of the menu buttons will perform the appropriate 466 action or replace the menu items by submenu items. When submenu 467 items appear, there will also be a \texttt{BACK} button to return to 468 the previous menu. Clicking on the \texttt{home} button returns the 469 menu buttons to the main menu. 470 471 After the menu buttons is a \texttt{var} button. This will replace 472 the menu buttons by buttons representing the variables that you have 473 defined. After that is a \texttt{cust} button, which will display 474 commands that you store in a list variable \texttt{CST}. 475 476 The last button, \texttt{X}, will close the menu bar. 477 478 \item The \texttt{msg} key will bring up a message window at the bottom 479 of the window which will give you helpful messages; for example, if 480 you save a graphic, it will tell you the name of the file it is 481 saved in and how to include it in a \LaTeX{} file. 482 483 \item The \texttt{abc} key will toggle the keyboard between the 484 scientific keyboard and an alphabetic keyboard. 485 \end{itemize} 486 487 \item The fifth button, \framebox{\tt X}, will close the current 488 session. 489\end{itemize} 490 491\section{Getting help} 492 493\texttt{Xcas} is an extensive program, but you can get help in several 494different ways. 495 496\subsubsection{Tooltips} 497 498If you hover the mouse cursor over certain parts of the \texttt{Xcas} 499window, a temporary window will appear with information about the 500part. For example, if you move the mouse cursor over the status line, 501you will get a message saying \texttt{Current CAS status. Click to 502modify.} 503 504If you type a function name into the \texttt{Xcas} command line, a 505similar temporary window will appear with information about the 506function. 507 508\subsubsection{HTML help} 509 510If you hit the \texttt{F12} button, you will be given a window in 511which you can use to search the html version of the manual. If you 512type a string in the search area, you will be given a list of help 513topics that contain the string. If you choose a topic and click 514\texttt{View}, your web browser will show the appropriate page of 515the manual. 516 517You can also get HTML help with the menu entry 518\texttt{Help$\blacktriangleright$Find word in HTML help}. 519 520\subsubsection{The help index} 521\label{sssec:helpind}\aname{helpindex}{} 522 523 524If you click on the \framebox{?} button on the status line you will 525get the help index. 526 527The help index is a list of the \texttt{giac} function and variable 528names. Along with the list, the help index window has an area listing 529words related to any chosen word and words synonymous to the chosen word. 530 531You can scroll through the help index items and click on the word that 532you want. There is also a line in the help index window that you can 533use to search the index; you can enter some text and be taken to the 534part of the index with words beginning with that text. The \texttt{?} 535button next to this search line will open the HTML help window. 536 537Below the search line, there is an area which will have a description 538of the chosen command, and below that is an area which will have 539examples of the command being used. If the command is a function, 540then between the description and examples will be some boxes in which 541you can enter arguments for the command. Filling in these boxes and 542hitting enter will put the function on the command line. 543 544At the top of the help index window is a \texttt{Details} button. If 545you click on that, a web page will open up in your browser with the 546relevant portion of the manual. If you click on the 547\framebox{\texttt{?}} next to the search line, you will be taken to 548the HTML help window. 549 550Besides clicking on the \framebox{\texttt{?}} on the status line, 551there are other ways to get to the help index. 552\begin{itemize} 553 \item 554 You can get to the help index by using the menu item 555 \texttt{Help$\blacktriangleright$Index}. 556 557 \item 558 You can press the tab button while at the \texttt{Xcas} 559 command line to get to the help index. If you have 560 entered part of a command name, you will be at the part of the index 561 with words beginning with the text that you entered. 562 563 \item 564 If you select a command from the menu, then as well as putting the 565 command on the command line, you will be taken to the help index 566 window with the command chosen. 567\end{itemize} 568 569\subsubsection{\texttt{findhelp}} 570 571You can get help from \texttt{Xcas} by using the 572\texttt{findhelp} function. If you enter 573\texttt{findhelp(}\textit{function}\texttt{)} (or equivalently 574\texttt{?}\textit{function}) at the command input, where 575\textit{function} is the name of a \texttt{giac} function, then some 576notes on \texttt{function} will appear in the answer portion and the 577appropriate page of the manual will appear in your web browser. 578 579\section{The menus} 580 581\subsection{The \texttt{File} menu} 582 583The \texttt{File} menu contains commands that are used to save 584sessions and parts of sessions and load previously saved sessions. 585This menu contains the following entries: 586\begin{itemize} 587 \item 588 \texttt{New Session}\\ 589 This will create and open a new session. 590 This session will be in a new tab labeled \texttt{Unnamed} until you save it 591 (using the menu item \texttt{File$\blacktriangleright$Save} or the 592 keystroke \texttt{Alt+S}). 593 594 \item 595 \texttt{Open}\\ 596 This will open a previously saved session. 597 There will be a submenu with a list of saved session files in the 598 primary directory that you can open, as well as a \texttt{File} item 599 which will open a directory browser you can use to find a session 600 file. This directory browser can also be opened with \texttt{Alt-O}. 601 602 \item 603 \texttt{Import}\\ 604 This will allow you to open a session that 605 was created with the Maple CAS, a TI89 calculator or a Voyage200 606 calculator. These sessions can then be executed with the 607 \texttt{Edit$\blacktriangleright$Execute Session} menu entry, but it 608 may be better to execute the commands one at a time to see if any 609 modifications need to be done. 610 611 \item 612 \texttt{Clone}\\ 613 This will create a copy of the current session in a Firefox 614 interface; either using the server at 615 \url{http://www-fourier.ujf-grenoble.fr/~parisse/xcasen.html} 616 (\texttt{Online}) or a local copy (\texttt{Offline}). 617 618 \item 619 \texttt{Insert}\\ 620 This allows you to insert a previously saved session, a link to a 621 Firefox session, or a previously saved figure, spreadsheet or program. 622 623 \item 624 \texttt{Save} (\texttt{Alt+S})\\ 625 This will save the current session. 626 627 \item 628 \texttt{Save as}\\ 629 This will save the current session under a 630 different name. 631 632 \item 633 \texttt{Save all}\\ 634 This will save all of the sessions. 635 636 \item 637 \texttt{Export as}\\ 638 This will allow you to save the current session in different 639 formats; either standard \texttt{Xcas} format, Maple format, MuPAD 640 format or TI89 format. 641 642 \item 643 \texttt{Kill}\\ 644 This will kill the current session. 645 646 \item 647 \texttt{Print}\\ 648 This will allow you to save the session in various ways. 649 \texttt{preview} will save an image of the current session in a file 650 that you name. \texttt{print} will send an image of the current 651 session to the printer. \texttt{preview selected levels} will save the images of 652 the commands and outputs of the current session, each in a separate 653 file. 654 655 \item 656 \texttt{LaTeX}\\ 657 This will render the session in \LaTeX{} and give you the 658 result in various ways. \texttt{latex preview} will display a 659 compiled \LaTeX{} version of the current session. \texttt{latex 660 print} will send a copy of the \LaTeX{}ed session to a printer. 661 \texttt{latex print selection} will save a copy. 662 663 \item 664 \texttt{Screen capture}\\ 665 This will create a screenshot that will be saved in various formats. 666 667 \item \texttt{Quit and update Xcas}\\ 668 This will quit \texttt{Xcas} after checking for a newer version. 669 670 \item \texttt{Quit} (\texttt{Ctrl+Q})\\ 671 This will quit \texttt{Xcas}. 672\end{itemize} 673 674\subsection{The \texttt{Edit} menu} 675 676The \texttt{Edit} menu contains commands that are used to execute and 677undo parts of the current session. This menu contains the following 678entries: 679\begin{itemize} 680 \item \texttt{Execute worksheet} (\texttt{Ctrl-F9})\\ 681 This will recalculate each level in the session. 682 683 \item \texttt{Execute worksheet with pauses}\\ 684 This will recalculate each level in the session, pausing between 685 calculations. 686 687 \item \texttt{Execute below}\\ 688 This will recalculate the current level and each level below it. 689 690 \item \texttt{Remove answers below}\\ 691 This will remove the answers to the current level and the levels 692 below it. 693 694 \item \texttt{Undo} (\texttt{Ctrl+Z})\\ 695 This will undo the latest edit done to the levels, including the 696 deletion of levels. It can be repeated to undo more than one edit. 697 698 \item \texttt{Redo} (\texttt{Ctrl+Y})\\ 699 This will redo the undone editing. 700 701 \item \texttt{Paste}\\ 702 This will paste the contents of the system clipboard to the cursor 703 position. 704 705 \item \texttt{Del selected levels}\\ 706 This will delete any entry levels that you have selected. 707 708 \item \texttt{selection -> LaTeX} (\texttt{Ctrl+T})\\ 709 If you select a level, part of a level, or answer with the mouse 710 (click and drag), this menu item will put a \LaTeX{} version of the 711 selection on the system clipboard. 712 713 \item \texttt{New entry} (\texttt{Alt+N})\\ 714 This will insert a new entry level above the current one. 715 716 \item \texttt{New parameter} (\texttt{Ctrl+P})\\ 717 This will bring up a window in which you can enter a name and 718 conditions for a new parameter. 719 720 \item \texttt{Insert newline}. 721 This will insert a newline below the cursor. Note that simply 722 typing return will cause the current entry to be evaluated rather 723 than inserting a newline. 724 725 \item \texttt{Merge selected levels}. 726 This will merge the selected levels into a single level. 727\end{itemize} 728 729\subsection{The \texttt{Cfg} menu} 730 731The \texttt{Cfg} menu contains commands that are used to set the 732behaviour of \texttt{Xcas}. This menu contains the following entries: 733\begin{itemize} 734 \item \texttt{Cas configuration}\\ 735 This will open a window that you can use to configure how 736 \texttt{Xcas} performs calculations. This is the same window you 737 get when you click on the status line. 738 739 \item \texttt{Graph configuration}\\ 740 This will open a window that you can use to configure the default 741 settings for a graph. This includes such things as the initial 742 ranges of the variables. Each graph will also have a \texttt{cfg} 743 button to configure the settings on a per graph basis. 744 745 \item \texttt{General configuration}\\ 746 This will open a window that you can use to configure various 747 non-computational aspects of \texttt{Xcas}, such as the fonts, the 748 default paper size, and the like. 749 750 \item \texttt{Mode (syntax)}\\ 751 This will allow you to change the default syntax. To begin with, it 752 is \texttt{Xcas} syntax, but you can change it to Maple syntax, 753 MuPAD syntax or TI89 syntax. 754 755 \item \texttt{Show}\\ 756 This will allow you to control parts of \texttt{Xcas} to show. 757 \begin{itemize} 758 \item \texttt{DispG}\\ 759 This will show the graphics display screen. 760 This screen will show all graphical commands from the session 761 together. 762 763 \item \texttt{keyboard}\\ 764 This will show the on-screen keyboard; the same as clicking on the 765 \texttt{Kbd} button on the status line. 766 767 \item \texttt{bandeau}\\ 768 This will show the menu buttons at the bottom of the window; 769 the same as clicking on \texttt{cmds} on the on-screen keyboard. 770 771 \item \texttt{msg}\\ 772 This will show the messages window; the same as clicking on 773 \texttt{msg} on the on-screen keyboard. 774 \end{itemize} 775 776 \item \texttt{Hide}\\ 777 This will hide the same items that you can show with \texttt{Show}. 778 779 \item \texttt{Index language}\\ 780 This will let you choose a language in which to display the help index. 781 782 \item \texttt{Colors}\\ 783 This will let you choose colors for various parts of the display. 784 785 \item \texttt{Session font}\\ 786 This will let you choose a font for the sessions. 787 788 \item \texttt{All fonts}\\ 789 This will let you choose a font for the session, the main menu and 790 the keyboard. 791 792 \item \texttt{browser}\\ 793 This will let you choose a browser that \texttt{Xcas} will use when 794 needed. If this is blank, then \texttt{Xcas} will use its own 795 internal browser. 796 797 \item \texttt{Save configuration}\\ 798 This will save the configurations that you chose with the 799 \texttt{Cfg} menu or by clicking on the status line. 800\end{itemize} 801 802\subsection{The \texttt{Help} menu} 803 804The \texttt{Help} menu contains commands that let you get information 805about \texttt{Xcas} from various sources. 806This menu contains the following entries: 807\begin{itemize} 808 \item \texttt{Index}\\ 809 This will bring up the help index. 810 (See \ahrefloc{helpindex}{help index} 811 \begin{latexonly}, p.\pageref{sssec:helpind}\end{latexonly}) 812 813 \item \texttt{Find word in HTML help} (\texttt{F12})\\ 814 This will bring up a page which will help you search for keywords in 815 the html documentation that came with \texttt{Xcas}. The help will 816 be displayed in your browser. 817 818 \item \texttt{Interface}\\ 819 This will bring up a tutorial for the \texttt{Xcas} interface. The 820 tutorial will be displayed in your browser. 821 822 \item \texttt{Reference card, fiches}\\ 823 This will bring up (in your browser) a pdf reference card for 824 \texttt{Xcas}. 825 826 \item \texttt{Manuals}\\ 827 This will let you choose from a variety of manuals for \textsc{Xcas}. 828 They will appear in your browser unless otherwise noted. 829 \begin{itemize} 830 \item \texttt{CAS reference}\\ 831 This will bring up a manual for \texttt{Xcas}. 832 833 \item \texttt{Algorithmes (HTML)}\\ 834 This will bring up a manual for the algorithms used by \texttt{Xcas}. 835 836 \item \texttt{Algorithmes (PDF)}\\ 837 This will bring up a pdf version of the manual for the algorithms 838 used by \texttt{Xcas}. 839 840 \item \texttt{Geometry}\\ 841 This will bring up a manual for two-dimensional geometry in 842 \texttt{Xcas}. 843 844 \item \texttt{Programmation}\\ 845 This will bring up a manual for programming in \texttt{Xcas}. 846 847 \item \texttt{Simulation}\\ 848 This will bring up a manual for statistics and using the 849 \texttt{Xcas} spreadsheet. 850 851 \item \texttt{Turtle}\\ 852 This will bring up a manual for using the \texttt{Turtle} drawing 853 screen in \texttt{Xcas}. 854 855 \item \texttt{Exercices}\\ 856 This will bring up a page of exercises that you can do with 857 \texttt{Xcas}. 858 859 \item \texttt{Amusement}\\ 860 This will bring up a page of mathematical amusements that you can 861 work through with \texttt{Xcas}. 862 863 \item \texttt{PARI-GP}\\ 864 This will bring up documentation for the GP/PARI functions. 865 \end{itemize} 866 867 \item \texttt{Internet}\\ 868 The \texttt{Internet} menu contains commands that take you to 869 various web pages related to \texttt{Xcas}. Among them are the 870 following entries: 871 \begin{itemize} 872 \item \texttt{Forum}\\ 873 This will take you to the \texttt{Xcas} forum. 874 875 % \item \texttt{Aide-memoire lycee}. 876 % This will take you to a paper discussing \texttt{Xcas} and high 877 % school. 878 879 % \item \texttt{Documents pedagogiques lycee}. 880 % This will take you to a page on the \texttt{Xcas} website with a 881 % list of useful links. 882 883 % \item \texttt{Documents algorithmique}. 884 % This will take you to a page on the \texttt{Xcas} website with a 885 % list of links. 886 887 % \item \texttt{Site Lycee de G. Connan}. 888 % This will take you to a page about a free book written by 889 % Guillaume Connan teaching algorithms to high school students. 890 891 % \item \texttt{Site Lycee de L. Briel}. 892 % This will take you to a website about \texttt{Xcas} for high 893 % school students. 894 895 % \item \texttt{Calcul formel au lycee, par D. Chevallair}. 896 % This will take you to a pdf file discussing the use of 897 % \texttt{Xcas} in high school. 898 899 % \item \texttt{Site de F. Han}. 900 % This will take you to a website by Frederic Han about 901 % \texttt{Xcas} and a QT frontent for \texttt{giac}. 902 903 % \item \texttt{Ressources Capes} 904 % This will take you to a website with various external sources. 905 906 % \item \texttt{Ressources Agregation externe}. 907 % This will take you to a collection of external resources. 908 909 % \item \texttt{Ressources Agregation interne}. 910 % This will take you to a page on the \texttt{Xcas} website. 911 912 \item \texttt{Update help}\\ 913 This will install updated help files (retrieved from the 914 \texttt{Xcas} website). 915 \end{itemize} 916 917 \item \texttt{Start with CAS}\\ 918 This menu has the following entries. 919 920 \begin{itemize} 921 \item \texttt{Tutorial}\\ 922 This opens up the tutorial. 923 924 \item \texttt{solutions}\\ 925 This opens up the solutions to the exercises in the tutorial. 926 \end{itemize} 927 928 \item \texttt{Tutoriel algo}\\ 929 This opens up a tutorial on algorithms and programming with 930 \texttt{Xcas}. 931 932 \item \texttt{Rebuild help cache}\\ 933 This will rebuild the help index. 934 935 \item \texttt{About}\\ 936 This will display a message window with information about \texttt{Xcas}. 937 938 \item \texttt{Examples}\\ 939 This will allow you to choose from a variety of example worksheets, 940 which will then be copied to your current directory and opened. 941\end{itemize} 942 943\subsection{The \texttt{Toolbox} menu} 944 945The \texttt{Toolbox} menu contains commands that are used to insert 946operators into the session. This menu includes the following entries: 947\begin{itemize} 948 \item \texttt{New entry} (\texttt{Alt+N})\\ 949 This will insert a new level after the current one. 950 951 \item \texttt{New comment} (\texttt{Alt+C})\\ 952 This will insert a new comment level after the current level. 953\end{itemize} 954The other entries allow you to insert mathematical operations into 955the current level. When you do that, you will also be taken to the 956help index 957(See \ahrefloc{helpindex}{help index} 958\begin{latexonly}, p.\pageref{sssec:helpind}\end{latexonly}) 959with help on the chosen command. 960 961\subsection{The \texttt{Expression} menu} 962 963The \texttt{Expression} menu contains commands that are used to 964transform expressions. The first entry is \texttt{New expression} 965(which is equivalent to \texttt{Alt+E}), 966which will insert a new level above the current level and bring up the 967on-screen keyboard. The rest of the entries can be used to insert a 968transformation. 969 970\subsection{The \texttt{Cmds} menu} 971 972The \texttt{Cmds} menu contains various \texttt{giac} functions and 973constants. 974 975\subsection{The \texttt{Prg} menu} 976 977The \texttt{Prg} menu contains commands that are used to write 978\texttt{giac} programs. The first entry, 979\texttt{Prg$\blacktriangleright$New program} (equivalent to 980\texttt{Alt+P}) , will insert a program level and bring up the program 981editor. The other entries are useful commands for writing 982\texttt{giac} programs. 983 984\subsection{The \texttt{Graphic} menu} 985 986The \texttt{Graphic} menu contains commands that are used to create 987graphs. The first entry, 988\texttt{Graphic$\blacktriangleright$Attributs} (equivalent 989to \texttt{Alt+K}) , will bring up a window 990contains different attributes of the graph (such as line width, color, 991etc.) The other entries are commands for creating and manipulating 992graphs. 993 994\subsection{The \texttt{Geo} menu} 995 996The \texttt{Geo} menu contains commands that are used to work with 997two- and three-dimensional geometric figures. The first two entries, 998\texttt{Geo$\blacktriangleright$New figure 2d} (equivalent to 999\texttt{Alt+G}) and \texttt{Geo$\blacktriangleright$New figure 3d} 1000(equivalent to \texttt{Alt+H}) 1001will create a level for 1002creating two- and three-dimensional figures, respecitively. The other 1003menu items are for working with the figures. 1004 1005\subsection{The \texttt{Spreadsheet} menu} 1006 1007The \texttt{Spreadsheet} menu contains commands that are used to work with 1008spreadsheets. The first menu item, 1009\texttt{Spreadsheet$\blacktriangleright$New spreadsheet} 1010(equivalent to \texttt{Alt+T}), will bring 1011up a window where you can set the size and other attributes of a 1012spreadsheet and then one will be created. The submenus 1013contain commands for working with spreadsheets. Notice that the 1014spreadsheet itself will have menus that are the same as these submenus. 1015 1016\subsection{The \texttt{Phys} menu} 1017 1018The \texttt{Phys} menu contains submenus with various categories of 1019constants, as well as functions for converting units. 1020 1021\subsection{The \texttt{Highschool} menu} 1022 1023The \texttt{Highschool} menu contains computer algebra commands that 1024are useful at different levels of highschool. There is also a 1025\texttt{Program} submenu with some program control functions. 1026 1027\subsection{The \texttt{Turtle} menu} 1028 1029The \texttt{Turtle} menu contains the commands that are used to in a 1030Turtle screen. The first menu item, 1031\texttt{Turtle$\blacktriangleright$New turtle}, will create a Turtle 1032drawing screen, the other menu items contain commands for working with 1033the screen. 1034 1035\section{Configuring \texttt{Xcas}} 1036\label{sec:config} 1037 1038\subsection{The number of significant digits} 1039\label{ssec:sigdig} 1040 1041By default \texttt{Xcas} uses and displays \texttt{12} significant 1042digits, but you can set the number of digits to other positive 1043integers. If you set the number of significant digits to a number 1044less than \texttt{14}, then \texttt{Xcas} will use the computer's 1045floating point hardware, and so calculations will be done to more 1046significant digits than you asked for, but only the number of digits 1047that you asked for will be displayed. If you set the number of 1048significant digits to \texttt{14} or higher, then both the 1049computations and the display will use that number of digits. 1050 1051You can set the number of significant digits for \texttt{Xcas} by 1052using the CAS configuration screen (see subsection \ref{ssec:confcomp}). 1053The number of significant 1054digits is stored in the variable \texttt{DIGITS} or \texttt{Digits}, 1055so you can also set it by giving the variable \texttt{DIGITS} a new 1056value, as in \texttt{DIGITS:= 20}. The value will be stored in the 1057configuration file (see subsection \ref{ssec:conffile}), and so can also be set there. 1058 1059\subsection{The language mode} 1060\label{ssec:lang} 1061 1062\texttt{Xcas} has its own language which it uses by default, but you 1063can have it use the language used by \texttt{Maple}, \texttt{MuPAD} or 1064the \texttt{TI89} calculator. 1065 1066You can set which language \texttt{Xcas} uses in the CAS configuration 1067screen (see subsection \ref{ssec:confcomp}). You can also use the 1068function \texttt{maple\_mode}. If 1069you give it an argument of \texttt{0}, \texttt{maple\_mode(0)}, then 1070\texttt{Xcas} will use its own language. If you give it an argument 1071of \texttt{1}, \texttt{maple\_mode(1)}, then \texttt{Xcas} will use the 1072\texttt{Maple} language. If you give it an argument of \texttt{2}, 1073\texttt{maple\_mode(2)}, then \texttt{Xcas} will use the \texttt{MuPAD} 1074language. Finally, if you give it an argument of \texttt{3}, 1075\texttt{maple\_mode(3)}, then \texttt{Xcas} will use the \texttt{TI89} 1076language. 1077 1078The language you want to use will be stored in the configuration file 1079(see subsection \ref{ssec:conffile}), and so can also be set there. 1080 1081\subsection{The units for angles} 1082\label{ssec:angles} 1083 1084By default, \texttt{Xcas} will assume that any angles you give (for 1085example, as the argument to a trigonometric function) is being 1086measured in radians. If you want, you can have \texttt{Xcas} use 1087degrees. 1088 1089You can set which angle measure \texttt{Xcas} uses in the CAS 1090configuration screen (see subsection \ref{ssec:confcomp}). 1091Your choice will be stored in the variable 1092\texttt{angle\_radian}; this will be \texttt{1} if you measure your 1093angles in radians and \texttt{0} if you measure your angles in 1094degrees. You can also change which angle measure you use by setting 1095the variable \texttt{angle\_radian} to the appropriate value. The 1096angle measure you want to use will be stored in the configuration file 1097(see subsection \ref{ssec:conffile}), and so can also be set there. 1098 1099\subsection{Exact or approximate values} 1100\label{ssec:approx} 1101 1102Some number, such as $\pi$ and $\sqrt{2}$, can't be written down 1103exactly as a decimal number. When computing with such numbers, 1104\texttt{Xcas} will leave them in exact, symbolic form. If you want, 1105you can have \texttt{Xcas} automatically give you decimal 1106approximations for these numbers. 1107 1108You can set whether or not \texttt{Xcas} will give you exact or 1109approximate values from the CAS configuration screen. Your choice will 1110be stored in the variable \texttt{approx\_mode}, where a value of 0 1111means that \texttt{Xcas} should give you exact answers when possible 1112and a value of 1 means that \texttt{Xcas} should give you decimal 1113approximations. Your choice will be stored in the configuration file 1114(see subsection \ref{ssec:conffile}), and so can also be set there. 1115 1116\subsection{Complex numbers} 1117\label{ssec:complex} 1118 1119When factoring polynomials, \texttt{Xcas} won't introduce complex 1120numbers if they aren't already being used. For example, 1121\begin{center} 1122 \texttt{factor(x\^{}2 + 2)} 1123\end{center} 1124will simply return 1125\begin{center} 1126 \texttt{x\^{}2 + 2} 1127\end{center} 1128but if an expression already involves complex numbers then 1129\texttt{Xcas} will use them; 1130\begin{center} 1131 \texttt{factor(i*x\^{}2 + 2*i)} 1132\end{center} 1133will return 1134\begin{center} 1135 \texttt{(x - i*sqrt(2))*(i*x - sqrt(2))} 1136\end{center} 1137\texttt{Xcas} also has ways of finding complex roots even when complex 1138numbers are not present; for example, the command \texttt{cfactor} 1139will factor over the complex numbers 1140\begin{center} 1141 \texttt{cfactor(x\^{}2 + 2)} 1142\end{center} 1143will return 1144\begin{center} 1145 \texttt{(x - i*sqrt(2))*(x + i*sqrt(2))} 1146\end{center} 1147 1148If you want \texttt{Xcas} to use complex numbers by default, you can 1149turn on complex mode. In complex mode, 1150\begin{center} 1151 \texttt{factor(x\^{}2 + 2)} 1152\end{center} 1153will return 1154\begin{center} 1155 \texttt{(x - i*sqrt(2))*(x + i*sqrt(2))} 1156\end{center} 1157 1158You can turn on complex mode from the CAS configuration screen. This 1159mode is determined by the value of \texttt{complex\_mode}; if this is 11601 then complex mode is on, if this variable is 0 then complex mode is 1161off. This option will be stored in the configuration file (see subsection \ref{ssec:conffile}), and 1162so can also be set there. 1163 1164\subsection{Complex variables} 1165\label{ssec:cvars} 1166 1167New variables will be assumed to be real; functions which work with 1168the real and imaginary parts of variables will assume that a variable 1169is real. For example, \texttt{re} returns the real part of its 1170argument and \texttt{im} returns the imaginary part, and so 1171\begin{center} 1172 \texttt{re(z)} 1173\end{center} 1174returns 1175\begin{center} 1176 \texttt{z} 1177\end{center} 1178and 1179\begin{center} 1180 \texttt{im(z)} 1181\end{center} 1182returns 1183\begin{center} 1184 \texttt{0} 1185\end{center} 1186 1187If you want variables to be complex by default, you can have 1188\texttt{Xcas} use complex variable mode. 1189You can set this from the CAS configuration screen. Your choice will 1190be stored in the variable \texttt{complex\_variables}, where a value of 0 1191means that \texttt{Xcas} will assume that variables are real and 1192and a value of 1 means that \texttt{Xcas} will assume that values are 1193complex. Your choice will be stored in the configuration file 1194(see subsection \ref{ssec:conffile}), and so can also be set there. 1195 1196\subsection{Configuring the computations} 1197\label{ssec:confcomp} 1198 1199You can configure how \texttt{Xcas} computes by using the menu item 1200\texttt{Cfg$\blacktriangleright$Cas configuration} or by clicking on 1201the status line. You will then be given a window in which you can 1202change the following options: 1203\begin{itemize} 1204 \item \texttt{Prog style} (default: \texttt{Xcas})\\ 1205 You will have a menu from which you can choose a different language 1206 to program in; you can choose from \texttt{Xcas}, \texttt{Xcas 1207 (Python)}, \texttt{Maple}, \texttt{Mupad} and \texttt{TI89/92}. 1208 1209 \item \texttt{eval} (default: 25)\\ 1210 You can type in a positive integer indicating the maximum number of 1211 recursions allowed when evaluating expressions. 1212 1213 \item \texttt{prog} (default: 1)\\ 1214 You can type in a positive integer indicating the maximum number of 1215 recursions allowed when executing programs. 1216 1217 \item \texttt{recurs} (default: 100)\\ 1218 You can type in a positive integer indicating the maximum number of 1219 recursive calls. 1220 1221 \item \texttt{debug} (default: 0)\\ 1222 You can type in an integer, 0 or 1. If this is 1, then 1223 \texttt{Xcas} will display intermediate information on the 1224 algorithms used by \texttt{giac}. If this number is 0, then no such 1225 information is displayed. 1226 1227 \item \texttt{maxiter} (default: 20)\\ 1228 You can type in an integer indicating the maximum number of 1229 iterations in Newton's method. 1230 1231 \item \texttt{Float format} (default: \texttt{standard})\\ 1232 You will have a menu from which you can choose how to display 1233 decimal numbers. Your choices will be: 1234 \begin{itemize} 1235 \item \texttt{standard} In standard notation, a number will be 1236 written out completely without using exponentials; for example, 1237 \texttt{15000.12} will be displayed as \texttt{15000.12}. 1238 \item \texttt{scientific} In scientific notation, a number will be 1239 written as a number between 1 and 10 times a power of ten; for example, 1240 \texttt{15000.12} will be displayed as \texttt{1.500012000000e+04} 1241 (where the number after \texttt{e} indicates the power of 10). 1242 \item \texttt{engineer} In engineer notation, a number will be 1243 written as a number between 1 and 1000 times a power of ten, where 1244 the power of 10 is a multiple of three. For example, 1245 \texttt{15000.12} will be displayed as \texttt{15.00012e3}. 1246 \end{itemize} 1247 1248 \item \texttt{Digits} (default: 12)\\ 1249 You can enter a positive integer which will indicate the number of 1250 significant digits. 1251 1252 \item \texttt{epsilon} (default: 1e-12)\\ 1253 You can enter a floating point number which will be the value of 1254 epsilon used by \texttt{epsilon2zero}, which is a function which 1255 replaces numbers with absolute value less than epsilon by 0. 1256 1257 \item \texttt{proba} (default: 1e-15)\\ 1258 You can enter a floating point number. If this number is greater 1259 than zero, then in some cases \texttt{giac} can use 1260 probabilistic algorithms and give a result with probability of being 1261 false less than this value. (One such example of a probabilistic 1262 algorithm that \texttt{giac} can use is the algorithm to compute the 1263 determinant of a large matrix with integer coefficients.) 1264 1265 \item \texttt{approx} (default: unchecked)\\ 1266 You will be given a checkbox. If the box is checked, then exact 1267 numbers such as $\sqrt{2}$ will be given a floating point approximation. If 1268 the box in unchecked, then exact values will be used when possible. 1269 1270 \item \texttt{autosimplify} (default: 1)\\ 1271 You can enter a simplification level of 0, 1 or 2. A value of 0 1272 means no automatic simplification will be done, a value of 1 means 1273 grouped simplification will be automatic. A value of 2 means that 1274 all simplification will be automatic. 1275 1276 \item \texttt{threads} (default: 1)\\ 1277 You can enter a positive integer to indicate the number of threads 1278 (for a possible future threaded version). 1279 1280 \item \texttt{Integer basis} (default: 10)\\ 1281 You will be given a menu from which you can choose an integer base 1282 to work in; your choices will be 8, 10 and 16. 1283 1284 \item \texttt{radian} (default: \texttt{checked})\\ 1285 You will be given a checkbox. If the box is checked, then angles 1286 will be measured in radians, otherwise they will be measured in 1287 degrees. 1288 1289 \item \texttt{Complex} (default: \texttt{unchecked})\\ 1290 You will be given a checkbox. If this box is checked, then 1291 \texttt{giac} will work in complex mode, meaning, for example, that 1292 polynomials will be factored with complex numbers if necessary. 1293 1294 \item \texttt{Cmplx\_var} (default: \texttt{unchecked})\\ 1295 You will be given a checkbox. If this box is checked, then 1296 variables will by default be assumed to be complex. For example, 1297 the expression \texttt{re(z)} won't be simplified to simply 1298 \texttt{z}. If this box is unchecked, then \texttt{re(z)} will be 1299 simplified to \texttt{z}. 1300 1301 \item \texttt{increasing power} (default: \texttt{unchecked})\\ 1302 You will be given a checkbox. If this box is checked, then 1303 polynomials will be written out in increasing powers of the 1304 variable; otherwise they will be written in decreasing powers. 1305 1306 \item \texttt{All\_trig\_sol} (default: \texttt{unchecked})\\ 1307 You will be given a checkbox. If this box is unchecked, then only 1308 the primary solutions of trigonometric equations will be given. For 1309 example, the solutions of \texttt{cos(x)=0} will be the pair 1310 \texttt{[-pi/2,pi/2]}. If this box is checked, then the solutions of 1311 \texttt{cos(x)=0} will be \texttt{[(2*n\_0*pi + pi)/2]}, where 1312 \texttt{n\_0} can be any integer. 1313 1314 \item \texttt{Sqrt} (default: \texttt{checked})\\ 1315 You will be given a checkbox. If this box is checked, then the 1316 \texttt{factor} command will factor second degree polynomials, even 1317 when the roots are not in the field determined by the coefficients. 1318 For example, \texttt{factor(x\^{}2 - 3)} will return 1319 \texttt{(x - sqrt(3))*(x + sqrt(3))}. If this box is unchecked, then 1320 \texttt{factor(x\^{}2 - 3)} will return \texttt{x\^{}2 - 3}. 1321\end{itemize} 1322This page will also have buttons for applying the settings, saving the 1323settings for future sessions, canceling any new settings, or restoring 1324the default settings. 1325 1326 1327\subsection{Configuring the graphics} 1328\label{ssec:confgraph} 1329 1330You can configure each graphics screen by clicking on the \texttt{cfg} 1331button on the graphics screen's control panel to the right of the 1332graph. You can also change the default graphical configuration using 1333the the menu item 1334\texttt{Cfg$\blacktriangleright$Graph configuration}. 1335You will then be given a window in which you can 1336change the following options: 1337\begin{itemize} 1338 \item \texttt{X-} and \texttt{X+}\\ 1339 These will determine the $x$ values for which calculations will be 1340 done. 1341 1342 \item \texttt{Y-} and \texttt{Y+}\\ 1343 These will determine the $y$ values for which calculations will be done. 1344 1345 \item \texttt{Z-} and \texttt{Z+}\\ 1346 These will determine the $z$ values for which calculations will be done. 1347 1348 \item \texttt{t-} and \texttt{t+}\\ 1349 These will determine the $t$ values for which calculations will be 1350 done, when plotting parametric curves, for example. 1351 1352 \item \texttt{WX-} and \texttt{WX+}\\ 1353 These will determine the range of $x$ values for the viewing window. 1354 done. 1355 1356 \item \texttt{WY-} and \texttt{WY+}\\ 1357 These will determine the range of $y$ values for the viewing window. 1358 1359% \item \texttt{TX} and \texttt{TY}\\ 1360 1361 \item \texttt{class\_min}\\ 1362 This will determine the minimum size of a statistics class. 1363 1364 \item \texttt{class\_size}\\ 1365 This will determine the default size of a statistics class. 1366 1367 \item \texttt{autoscale}\\ 1368 When checked, the the graphic will be autoscaled. 1369 1370 \item \texttt{ortho}\\ 1371 When checked, all axes of the graphic will be scaled equally. 1372 1373 \item \texttt{>W} and \texttt{W>}\\ 1374 These are convenient shortcuts to copy the \texttt{X-}, \texttt{X+}, 1375 \texttt{Y-} and \texttt{Y+} values to \texttt{WX-}, \texttt{WX+}, 1376 \texttt{WY-} and \texttt{WY+}, or the other way around. 1377\end{itemize} 1378This page will also have buttons for applying the settings, saving the 1379settings for future sessions, or canceling any new settings. 1380 1381\subsection{More configuration} 1382\label{sec:mconf} 1383 1384You can configure other aspects of \texttt{Xcas} (besides the 1385computatioonal aspects and graphics) using the the menu item 1386\texttt{Cfg$\blacktriangleright$General configuration}. 1387You will then be given a window in which you can 1388change the following options: 1389\begin{itemize} 1390 \item \texttt{Font}\\ 1391 This lets you choose a session font, the same as choosing the menu 1392 item \texttt{Cfg$\blacktriangleright$Session font}. 1393 1394 \item \texttt{Level}\\ 1395 This will determine what type of level should be open when you start 1396 a new session. 1397 1398 \item \texttt{browser}\\ 1399 This will determine what browser \texttt{Xcas} should use when it 1400 requires one, for example when displaying help. If this is empty, 1401 \texttt{Xcas} will use its built-in browser. 1402 1403 \item \texttt{Auto HTML help}\\ 1404 If this box is checked, then whenever you choose a function from a menu, 1405 a help page for that function will appear in your browser. 1406 Regardless of whether this box is checked or not, the help page will 1407 also appear in your browser if you type \texttt{?}\textit{function} 1408 in a command box. 1409 1410 \item \texttt{Auto index help} 1411 If this box is checked, then whenever you choose a function from a 1412 menu, the help index page for that function will appear. This is 1413 the same page you would get from choosing the function from the help 1414 index. 1415 1416 \item \texttt{Print format}\\ 1417 This will determine the paper size for printing and saving files. 1418 There is also a button you can use to have the printing done in 1419 landscape mode; if this button is not checked, the printing will be 1420 done in portrait. 1421 1422 \item \texttt{Disable Tool tips}\\ 1423 If this is checked, \texttt{Xcas} will stop displaying tool tips. 1424 1425 \item \texttt{rows} and \texttt{columns}\\ 1426 These will determind the default number of rows and columns for the 1427 matrix editor and spreadsheet. 1428 1429 \item \texttt{PS view}\\ 1430 This determines what program will be used to preview Postscript files. 1431 1432% \item \texttt{Step by step}\\ 1433 1434% \item \texttt{Proxy} 1435\end{itemize} 1436 1437\subsection{The configuration file} 1438\label{ssec:conffile} 1439 1440When you save changes to your configuration, this is stored in a 1441configuration file, which will be \texttt{.xcasrc} in your home 1442directory in Unix and \texttt{xcas.rc} in Windows. This file will 1443have four functions -- \texttt{widget\_size}, \texttt{cas\_setup}, 1444\texttt{maple\_mode} and \texttt{xyztrange} -- which determine the 1445configuration and which are evaluated when \texttt{Xcas} starts. 1446 1447\section{Printing and saving} 1448 1449\subsection{Saving a session} 1450 1451Each tab above the status line represents a session, the active tab 1452will be yellow. The label of each tab will be the name of the file 1453that the session is saved in; if the session hasn't been saved the tab 1454will read \texttt{Unnamed}. 1455 1456You can save your current session by clicking on the \texttt{Save} 1457button on the status line. If the session contains unsaved changes 1458the \texttt{Save} button will be red; the button will be green when 1459nothing needs to be saved. The first time that you save a session you 1460will be prompted for a file name; you should choose a name that ends 1461in \texttt{.xws}. Subsequent times that you save a session it will be 1462saved in the same file; to save a session in a different file you can 1463use the menu item \texttt{File$\blacktriangleright$Save as}. 1464 1465If you have a session saved in a file and you want to load it in a 1466tab, you can use the menu item \texttt{File$\blacktriangleright$Open}. 1467From there you can choose a specific file from a list or open a 1468directory browser that you can use to choose a file. The directory 1469browser can also be opened with \texttt{Alt-O}. 1470 1471\subsection{Saving a spreadsheet} 1472 1473If you have a spreadsheet in one of the levels, you can save it 1474separately from the rest of the session. 1475 1476Once a spreadsheet is inserted, it will have menus right next to the 1477level number. If you select the \texttt{Table$\blacktriangleright$Save 1478sheet as text} menu, you will be prompted for a file name. You should 1479choose a file name that ends in \texttt{.tab}. Once you save the 1480spreadsheet, there will be a button to the right of the menus which 1481you can use to save any changes you make. If you want to save the 1482spreadsheet under a different name, you can use the 1483\texttt{Table$\blacktriangleright$Save as alternate filename} menu entry. 1484You can also use the 1485\texttt{Table$\blacktriangleright$Save as CSV} and 1486\texttt{Table$\blacktriangleright$Save as mathml} menu entries 1487to save the spreadsheet in other formats. 1488 1489You can use the \texttt{Table} menu to insert previously saved 1490spreadsheets; the menu item \texttt{Table$\blacktriangleright$Insert} 1491will bring up a directory browser you can use to select a file to 1492enter. 1493 1494\subsection{Saving a program} 1495 1496You can open up a level in which to write an \texttt{Xcas} program 1497with the menu item \texttt{Prg$\blacktriangleright$New program} (which 1498is equivalent to \texttt{Alt-P}). If you select this item, you will 1499be prompted for information to fill out a template for a program and 1500then be left in the program editor. 1501 1502At the top of the program editor there will be menus and buttons, at 1503the far right will be a \texttt{Save} button that you can press to 1504save the program. The first time you save a program, you will be 1505prompted for a file name, you should choose a name ending in 1506\texttt{.cxx}. Once a program is saved, the file name will appear to 1507the right of the \texttt{Save} button. If you want to save the 1508program under a different name, you can use the 1509\texttt{Prog$\blacktriangleright$Save as} item from the program editor 1510menu. 1511 1512To insert a previously saved program, you can use the 1513\texttt{Prog$\blacktriangleright$Load} item from the program editor menu. 1514 1515\subsection{Printing a session} 1516 1517You can print a session with the 1518\texttt{File$\blacktriangleright$Print$\blacktriangleright$to printer} 1519menu item. 1520 1521If you prefer to save the printed form as a file, you can use the 1522\texttt{File$\blacktriangleright$Print$\blacktriangleright$preview} 1523menu item. You will prompted for a file name to save the printed form 1524in; the file will be a PostScript file, so the name should end in 1525\texttt{.ps}.If you only want to save certain levels in printable 1526form, you can use the 1527\texttt{File$\blacktriangleright$Print$\blacktriangleright$preview 1528selected levels} menu item; this file will be encapsulated PostScript, 1529so the name should end in \texttt{.eps}. 1530 1531\section{Translating to other computer languages} 1532 1533\texttt{Xcas} can translate a session, or parts of a session, to other 1534computer languages; notably \LaTeX{} and MathML. 1535 1536\subsection{Translating an expression to \LaTeX{}\index{LaTeX}} 1537 1538The command \texttt{latex} will translate an expression to a \LaTeX{} 1539expression. If you enter \texttt{latex(\textit{expression})}, then 1540the expression will be evaluated and the result will be given to you 1541in the \LaTeX{} typesetting language. For example, if you enter 1542\begin{center} 1543 {\tt latex(1+1/2)} 1544\end{center} 1545you will get 1546\begin{center} 1547 {\tt $\backslash $frac\{3\}\{2\}} 1548\end{center} 1549 1550\subsection{Translating the entire session to \LaTeX{}} 1551 1552If you want to save your entire document as a complete \LaTeX{} file, 1553you can use the menu item \texttt{File$\blacktriangleright$laTeX 1554preview selection} 1555 1556\subsection{Translating graphical output to \LaTeX{}} 1557 1558You can see all of your graphic output at once on the 1559\texttt{DispG\index{DispG}} screen, which you can bring up with the 1560command \texttt{DispG()}. (This screen can be cleared with the 1561command line command \texttt{erase()}.) On the \texttt{DispG} screen 1562there will be a \texttt{Print} menu; the 1563\texttt{Print$\blacktriangleright$latex print} will give you several 1564in files \texttt{DispG.tex}, \texttt{DispG.ps}, \texttt{DispG.ps} and 1565\texttt{DispG.png} with the graphics in different formats. To save it 1566without using the \texttt{DispG()} command you can use the 1567\texttt{graph2tex} command, which will save all graphic output to a 1568\LaTeX{} file of your choosing. For example, to save your graphs to 1569\texttt{myfile.tex}, you can enter the command 1570\begin{center} 1571 {\tt graph2tex("myfile.tex")} 1572\end{center} 1573to get a \LaTeX{} file \texttt{myfile.tex} with the graphs. To save a 1574three-dimensional graph, you can use the command \texttt{graph3d2tex}. 1575 1576To save a single graph as a \LaTeX{} file, you can use the \texttt{M} 1577menu to the right of the graph. Selecting 1578\texttt{M$\blacktriangleright$Export Print$\blacktriangleright$Print 1579(LaTeX)} will save the current graph. You can also save a single 1580graph by selecting that level, then use the menu item 1581\texttt{File$\blacktriangleright$LaTeX$\blacktriangleright$LaTeX print 1582selection}. This method will save the graph in several formats; 1583\texttt{session0.tex}, \texttt{session0.dvi}, \texttt{session0.ps} 1584and \texttt{session0.png}, or with \texttt{session0} replaced by the 1585session name. 1586 1587\subsection{Translating an expression to MathML\index{MathML}} 1588 1589The \texttt{mathmml\index{mathmml}} command will take an expression 1590and return the result in MathML. For example, if you enter 1591\begin{center} 1592 {\tt mathml(1/4 + 1/4)} 1593\end{center} 1594you will get 1595\begin{verbatim} 1596<?xml version="1.0" encoding="iso-8859-1"?> 1597<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" 1598"http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd" [ 1599<!ENTITY mathml "http://www.w3.org/1998/Math/MathML"> 1600]> 1601<html xmlns="http://www.w3.org/1999/xhtml"> 1602<body> 1603 1604<math mode="display" xmlns="http://www.w3.org/1998/Math/MathML"> 1605 1606<mfrac><mrow><mn>1</mn></mrow><mrow><mn>2</mn></mrow></mfrac> 1607 1608</math><br/> 1609 1610</body> 1611</html> 1612\end{verbatim} 1613which is the number $1/2$ in MathML form, along with enough 1614information to make it a complete HTML document. 1615 1616\subsection{Translating a spreadsheet to MathMML} 1617 1618You can translate an entire spreadsheet to MathML with the spreadsheet 1619menu command \texttt{Table$\blacktriangleright$Save as mathml}. 1620 1621\subsection{Translating a Maple\index{Maple} file to Xcas} 1622 1623You can translate a file of Maple commands to the Xcas language with 1624the \texttt{maple2xcas} command, as in 1625\begin{center} 1626 {\tt maple2xcas("MapleFile","XcasFile")} 1627\end{center} 1628This command takes two arguments, the name of the Maple input file and 1629the name of the file where you want to save the Xcas commands. 1630 1631\chapter{Entry in \texttt{Xcas}} 1632 1633\section{Suppressing output} 1634 1635If you enter a command into \texttt{Xcas}, the result will appear in 1636the output box below the input. If you enter 1637\begin{center} 1638 {\tt a := 2+2} 1639\end{center} 1640then 1641\begin{center} 1642 {\tt 4} 1643\end{center} 1644will appear in the output box. You can evaluate the input and 1645suppress the output with the \texttt{nodisp}\index{nodisp} command. 1646If you enter 1647\begin{center} 1648 {\tt nodisp(a := 2+2)} 1649\end{center} 1650then \texttt{a} will still be set to \texttt{4}, but the result will 1651not appear in the output box. Instead, 1652\begin{center} 1653 {\tt Done} 1654\end{center} 1655will appear. 1656 1657An alternate way of suppressing the output is to end the input with 1658\texttt{:;}\index{:;}, if you enter 1659\begin{center} 1660 {\tt b := 3+3:;} 1661\end{center} 1662then \texttt{b} will be set to \texttt{6} but it won't be displayed. 1663 1664\section{Entering comments\index{comments}} 1665 1666You can annotate an \texttt{Xcas} session by adding comments. 1667You can enter a comment on the current line at any time by typing 1668\texttt{Alt+C}. The line will appear in green text and conclude when 1669you type \texttt{Enter}. Comments are not evaluated and so have no 1670output. If you have begun entering a command when you begin a 1671comment, the command line be pushed down so that you can finish it 1672when you complete the comment. 1673 1674You can open the browser in a comment line by entering the web address 1675beginning with the \texttt{@} sign. If you enter the comment line 1676\begin{center} 1677{\tt The Xcas homepage is at\\ 1678@www-fourier.ujf-grenoble.fr/\~{}parisse/giac.html} 1679\end{center} 1680then the browser will open to the \texttt{Xcas} home page. 1681 1682To add a comment to a program, rather than a session, you can use the 1683\texttt{comment\index{comment}} command, which takes a string as an argument. 1684Alternatively, any part of a program between \texttt{//}\index{//} and the end 1685of the line is a comment. So both 1686\begin{center} 1687{\tt bs() := \{comment("Hello"); return "Hi there!";\}} 1688\end{center} 1689and 1690\begin{center} 1691{\tt bs() := \{ // Hello\\ 1692 return "Hi there!";\}} 1693\end{center} 1694are programs with the comment "Hello". 1695 1696\section{Editing expressions} 1697 1698You can enter expressions on the command line, but \texttt{Xcas} also 1699has a built-in expression editor that you can use to enter expressions 1700in two dimensions, the way they normally look when typeset. When you 1701have an expression in the editor, you can also manipulate 1702subexpressions apart from the entire expression. 1703 1704\subsection{Entering expressions in the editor} 1705 1706The expression 1707\[\frac{x+2}{x^2-4}\] 1708can be entered on the command line with 1709\begin{center} 1710{\tt (x+2)/(x\^{}2-4)} 1711\end{center} 1712You also can use the expression editor\index{expression editor} to 1713enter it visually, as $x+2$ on top of $x^2 - 4$. To do this, you can 1714start the expression editor with the \texttt{Alt+E} keystroke (or the 1715\texttt{Expression $\blacktriangleright$ New Expression} menu 1716command). There will be a small \texttt{M} on the right side of the 1717expression line, which is a menu with some commands you can use on the 1718expressions. There will also be a \texttt{0} selected on the 1719expression line and an on-screen keyboard at the bottom. If you type 1720\texttt{x + 2}, it will overwrite the \texttt{0}. To make this the 1721top of the fraction, you can select it with the mouse (you can also 1722make selections with the keyboard, as will be discussed later) and 1723then type \texttt{/}. This will leave the \texttt{x + 2} on the top 1724and the cursor on the bottom. To enter $x^2 - 4$ on the bottom, begin 1725by typing \texttt{x}. Selecting this \texttt{x} and typing 1726\texttt{\^{}2} will put on the superscript. Finally, selecting the 1727$\texttt{x}^{\texttt{2}}$ and typing \texttt{- 4} will finish the 1728bottom. If you then hit \texttt{Enter}, the expression will be 1729evaluated and will appear on the output line. 1730 1731\subsection{Subexpressions}\index{subexpressions} 1732 1733\texttt{Xcas} can operate on expressions in the expression editor or 1734subexpressions of the expression. To understand subexpressions and 1735how to select them, it helps to know that \texttt{Xcas} stores 1736expressions as \textsl{trees}. 1737 1738A tree, in this sense, consists of objects 1739called nodes. A node can be connected to lower nodes, called the 1740children of the first node. Each node (except one) will be connected 1741to exactly one node above it, called the parent node. One special 1742node, called the root node, won't have a parent node. Two nodes with 1743the same parent nodes are called siblings. Finally, if a node doesn't 1744have any children, it is called a leaf. This terminology comes from a 1745visual representation of a tree, 1746\begin{center} 1747 \includegraphics[width=0.4\textwidth]{xcas-tree.png} 1748\end{center} 1749which looks like an upside-down tree; the root is at the 1750top and the leaves are at the bottom. 1751 1752Given an expression, the nodes of the corresponding 1753tree\index{expression tree} are the functions, operators, variables 1754and constants. The children of a function node are its arguments, the 1755children of an operator node are its operands, and the constants and 1756variables will be the leaves. For example, the tree for $\sin(2*x + 1757y)$ will look like 1758\begin{center} 1759 \includegraphics[width=0.4\textwidth]{xcas-expr-tree.png} 1760\end{center} 1761A subexpression\index{subexpression} of an expression will be a 1762selected node together with the nodes below it. For example, both 1763$2*x$ and $2*x+y$ are subexpressions of $\sin(2*x+y)$, but $x+y$ is 1764not. 1765 1766A subexpression of the contents of the expression editor can be 1767selected with the mouse; the selection will appear white on a black 1768background. A subexpression can also be chosen with the keyboard 1769using the arrow keys. Given a selection: 1770\begin{itemize} 1771 \item 1772 The up arrow will go to the parent node. 1773 \item 1774 The down arrow will go to the leftmost child node. 1775 \item 1776 The right and left arrows will go to the right and left sibling nodes. 1777 \item 1778 The control key with the right and left arrows will switch the 1779 selection with the corresponding sibling. 1780 \item 1781 If a constant or variable is selected, the backspace key will delete 1782 it. For other selections, backspace will delete the function or 1783 operator, and another backspace will delete the arguments or operands. 1784\end{itemize} 1785 1786You can use the arrow keys to navigate the tree structure of an 1787expression, which isn't always evident by looking at the expression 1788itself. For example, suppose you enter \texttt{x*y*z} in the editor. 1789The two multiplications will be a different levels; the tree will look 1790like 1791\begin{center} 1792 \includegraphics[width=0.4\textwidth]{xcas-xyz-tree.png} 1793\end{center} 1794If you select the entire expression with the up arrow and then go to 1795the \texttt{M} menu to the right of the line and choose eval, then the 1796expression will look the same but, as you can check by navigating it 1797with the arrow keys, the tree will look like 1798\begin{center} 1799 \includegraphics[width=0.4\textwidth]{xcas-xyz-tree2.png} 1800\end{center} 1801 1802\subsection{Manipulating subexpressions\index{subexpressions}} 1803 1804If a subexpression is selected in the expression editor, then any menu 1805command will be applied to that subexpression. 1806 1807For example, suppose that you enter the expression 1808\begin{center} 1809 {\tt (x+1)*(x+2)*(x-1)} 1810\end{center} 1811in the expression editor. Note that you can use the abilities of the 1812editor to make this easier. First, enter \texttt{x+1}. Select this 1813with the up arrow, then type \texttt{*} followed by \texttt{x+2}. Select 1814the \texttt{x+2} with the up arrow and then type \texttt{*} followed 1815by \texttt{x-1}. Using the up arrow again will select the \texttt{x-1}. 1816Select the entire expression with the up arrow, and then select 1817\texttt{eval} from the \texttt{M} menu. This will put all factors at 1818the same level. Suppose you want the factors \texttt{(x+1)*(x+2)} to 1819be expanded. You could select \texttt{(x+1)*(x+2)} with the mouse and 1820do one of the following: 1821\begin{itemize} 1822 \item 1823 Select the 1824 \texttt{Expression$\blacktriangleright$Misc$\blacktriangleright$normal} 1825 menu item. You will then have \texttt{normal((x+1)*(x+2))*(x-1)} in 1826 the editor. If you hit enter, the result $(x^2 + 3x + 2)*(x-1)$ will 1827 appear in the output window. 1828 \item 1829 Again, select the 1830 \texttt{Expression$\blacktriangleright$Misc$\blacktriangleright$normal} 1831 menu item, so again you have \texttt{normal((x+1)*(x+2))*(x-1)} in 1832 the editor. Now if you select \texttt{eval} from the \texttt{M} 1833 menu, then the expression in the editor will become the result 1834 $(x^2 + 3x + 2)*(x-1)$, which you can continue editing. 1835 \item 1836 Choose \texttt{normal} from the \texttt{M} menu. This will apply 1837 normal to the selection, and again you will have the result 1838 $(x^2 + 3x + 2)*(x-1)$ in the editor. 1839\end{itemize} 1840 1841There are also keystroke commands that you can use to operate 1842on subexpressions that you've selected. There are the usual 1843\texttt{Ctrl+Z} and \texttt{Ctrl+Y} for undoing and redoing. Some of 1844the others are given in the following table. 1845 1846\begin{center} 1847\begin{tabular}{|p{.20\textwidth}|p{.6\textwidth}|} 1848\hline 1849\textbf{Key} & \textbf{Action on selection}\\ 1850%\multicolumn{2}{|c|}{\textbf{Conversions}}\\ 1851\hline\hline 1852\texttt{Ctrl+D} & differentiate\\ 1853\texttt{Ctrl+F} & factor\\ 1854\texttt{Ctrl+L} & limit\\ 1855\texttt{Ctrl+N} & normalize\\ 1856\texttt{Ctrl+P} & partial fraction\\ 1857\texttt{Ctrl+R} & integrate\\ 1858\texttt{Ctrl+S} & simplify\\ 1859\texttt{Ctrl+T} & copy \LaTeX{} version to clipboard\\ 1860\hline 1861\end{tabular} 1862\end{center} 1863 1864\section{Previous results} 1865 1866The \texttt{ans\index{ans}} command will return the results of previous commands. 1867The input to \texttt{ans} is the number of the command, beginning with 18680. If the first command that you enter is 1869\begin{center} 1870 {\tt 2+5} 1871\end{center} 1872resulting in 1873\begin{center} 1874 {\tt 7} 1875\end{center} 1876then later references to \texttt{ans(0)} will evaluate to \texttt{7}. 1877 1878Note that the argument to \texttt{ans} doesn't correspond to the line 1879number in \texttt{Xcas}. For one thing, the line numbers begin at 1. 1880What's more, if you go back and re-evaluate a previous line, then that 1881will become part of the commands that \texttt{ans} keeps track of. 1882 1883If you give \texttt{ans} a negative number, then it counts backwards 1884from the current input. To get the latest output, for 1885example, you can use \texttt{ans(-1)}. With no argument, 1886\texttt{ans()} wil also return the latest output. 1887 1888Similarly, \texttt{quest\index{quest}} will return the previous inputs. Since 1889these will often be simplified to be the same as the output, 1890\texttt{quest($n$)} sometimes has the same value as \texttt{ans($n$)}. 1891 1892You can also use \texttt{Ctrl} plus the arrow keys to scroll through 1893previous inputs. With the cursor on the command line, 1894\texttt{Ctrl+uparrow} will go backwards in the list of previous 1895commands and \texttt{Ctrl+downarrow} will go forwards. 1896 1897\section{Spreadsheet} 1898\index{spreadsheet} 1899 1900\subsection{Opening a spreadsheet} 1901 1902You can open a spreadsheet (or a matrix editor) with the 1903\texttt{Spreadsheet$\blacktriangleright$New Spreadsheet} menu item or 1904with the key \texttt{Alt+T}. 1905 1906When you open a new spreadsheet, you will be given a configuration screen. 1907The configuration screen allows you to set the following options: 1908\begin{itemize} 1909 \item 1910 \texttt{Variable} 1911 The name of the file where the spreadsheet will be 1912 saved. 1913 \item 1914 \texttt{Rows} and \texttt{Columns} 1915 The number of rows and columns in the spreadsheet. 1916 \item 1917 \texttt{Eval} 1918 Whether or not to automatically re-evaluate the entries in the 1919 spreadsheet after each change. If this is not checked, then you can 1920 re-evaluate the spreadsheet with the \texttt{eval} button on the 1921 spreadsheet menu bar. 1922 \item 1923 \texttt{Distribute} 1924 Whether or not entering a matrix into a cell will keep the entry in 1925 a single cell or distribute it across an appropriate array of cells. 1926 \item 1927 \texttt{Landscape} 1928 Whether the graphical representation of the spreadsheet should be 1929 displayed below the spreadsheet or to the right of the spreadsheet. 1930 If this is checked, it will be displayed below the spreadsheet. 1931 \item 1932 \texttt{Move right} 1933 Whether or not to move to the cell to the right of the current cell 1934 when data is entered. If this is not checked, you will be moved to 1935 the cell below the current cell. 1936 \item 1937 \texttt{Spreadsheet} 1938 Whether to format a spreadsheet or a matrix. 1939 \item 1940 \texttt{Graph} 1941 Whether or not to display the graphical representation of the 1942 spreadsheet. 1943 % \item 1944 % \texttt{Undo history} 1945 % \item 1946 % \texttt{Init sheet} 1947\end{itemize} 1948 1949\subsection{The spreadsheet window} 1950 1951When you open a spreadsheet, the input line will become the 1952spreadsheet. 1953\begin{center} 1954\includeimage{xcas-spreadsheet.png} 1955\end{center} 1956The top will be a menu bar with 1957\texttt{Table}, \texttt{Edit} and \texttt{Maths} menus as well as 1958\texttt{eval}, \texttt{val}, \texttt{init}, \texttt{2-d} and 1959\texttt{3-d} buttons. To the right will be the name of 1960the file the spreadsheet will be saved into. Below the menu bar will 1961be two boxes; a box which displays the active cell (and can be used to 1962choose a cell) and a command line to enter information into the cell. 1963Below that will be a status line, you can click on this to return to 1964the configuration screen. 1965 1966%% \section{Deletions}? 1967 1968\section{Variables} 1969 1970\subsection{Variable names} 1971 1972A variable\index{variable} or function name is a sequence of letters, 1973numbers and underscores that begins with a letter. If you define your 1974own variable or function, you can't use the names of built-in 1975variables or functions, or other keywords reserved by \texttt{Xcas}. 1976 1977%% \subsection{CST}? 1978 1979\subsection{Assigning values} 1980 1981You can assign a value to a variable with the \texttt{:=}\index{:=} 1982operator. For example, to give the variable \texttt{a} the value of 1983\texttt{4}, you can enter 1984\begin{center} 1985 {\tt a := 4} 1986\end{center} 1987Alternatively, you can use the \texttt{=>}\index{=>} operator; when 1988you use this operator, the value comes before the variable; 1989\begin{center} 1990 {\tt 4 => a} 1991\end{center} 1992The function \texttt{sto\index{sto}} or \texttt{Store\index{Store}} 1993can also be used; again, the value comes before the variable 1994\begin{center} 1995 {\tt sto(4,a)} 1996\end{center} 1997After any one of these commands, any time you use the variable 1998\texttt{a} in an expression, it will be replaced by \texttt{4}. 1999 2000You can use sequences or lists to make multiple assignments at the 2001same time. For example, 2002\begin{center} 2003{\tt (a,b,c) := (1,2,3)} 2004\end{center} 2005will assign \texttt{a} the value \texttt{1}, \texttt{b} the value 2006\texttt{2} and \texttt{c} the value \texttt{3}. Note that this can 2007be used to switch the values of two variables; with \texttt{a} and 2008\texttt{b} as above, the command 2009\begin{center} 2010{\tt (a,b) := (b,a)} 2011\end{center} 2012will set \texttt{a} equal to \texttt{b}'s original value, namely 2013\texttt{2}, and will set \texttt{b} equal to \texttt{a}'s original 2014value, namely \texttt{1}. 2015 2016Another way to assign values to variables, useful in Maple mode, is 2017with the \texttt{assign\index{assign}}. If you enter 2018\begin{center} 2019 {\tt assign(a,3)} 2020\end{center} 2021or 2022\begin{center} 2023 {\tt assign(a = 3)} 2024\end{center} 2025then \texttt{a} will have the value \texttt{3}. You can assign 2026multiple values at once; if you enter 2027\begin{center} 2028 {\tt assign([a = 1, b = 2])} 2029\end{center} 2030then \texttt{a} will have the value \texttt{1} and \texttt{b} will 2031have the value \texttt{2}. This command can be useful in Maple mode, 2032where solutions of equations are returned as equations. For example, 2033if you enter (in Maple mode) 2034\begin{center} 2035 {\tt sol := solve([x + y = 1, y = 2])} 2036\end{center} 2037you will get 2038\begin{center} 2039 {\tt [x = -1, y = 2]} 2040\end{center} 2041If you then enter 2042\begin{center} 2043 {\tt assign(sol)} 2044\end{center} 2045the variable \texttt{x} will have value \texttt{-1} and \texttt{y} 2046will have the value \texttt{2}. This same effect can be achieved in 2047standard \texttt{Xcas} mode, where 2048\begin{center} 2049 {\tt sol := solve([x + y = 1, y = 2])} 2050\end{center} 2051will return 2052\begin{center} 2053 {\tt [[x = -1, y = 2]]} 2054\end{center} 2055In this case, the command 2056\begin{center} 2057 {\tt [x,y] := sol[0]} 2058\end{center} 2059will assign \texttt{x} the value \texttt{-1} and \texttt{y} the value 2060\texttt{2}. 2061 2062\subsection{Assignment by reference} 2063\label{subsec:refassign} 2064A list is simply a sequence of values separated by commas and 2065delimited by \texttt{[} and \texttt{]} (see section \ref{sec:seq}). 2066Suppose you give the variable \texttt{a} the value \texttt{[1,1,3,4,5]}, 2067\begin{center} 2068 {\tt a := [1,2,3,4,5]} 2069\end{center} 2070If you later assign to \texttt{a} the value \texttt{[1,2,3,4,5]}, then 2071a new list is created. It may be better to just change the second 2072value in the original list by reference. This can be done with the 2073\texttt{=<} command. Recalling that lists are indexed beginning at 0, 2074the command 2075\begin{center} 2076 {\tt a[1] =< 2} 2077\end{center} 2078will simply change the value of the second element of the list instead 2079of creating a new list, and is a more efficient way to change the 2080value of \texttt{a} to \texttt{[1,2,3,4,5]}. 2081 2082\subsection{Copying values of list} 2083 2084If you enter 2085\begin{center} 2086 {\tt list1 := [1,2,3]} 2087\end{center} 2088and then 2089\begin{center} 2090 {\tt list2 := list1} 2091\end{center} 2092then \texttt{list1} and \texttt{list2} will be equal to the same list, 2093not simply two lists with the same elements. In particular, if you 2094change (by reference) the value of an element of \texttt{list1}, then 2095the change will also be reflected in \texttt{list2}. For example, if 2096you enter 2097\begin{center} 2098 {\tt list1[1] =< 5} 2099\end{center} 2100then both \texttt{list1} and \texttt{list2} will be equal to 2101\texttt{[1,5,3]}. 2102 2103The \texttt{copy\index{copy}} command will create a copy of a list (or 2104vector or matrix) which is equal to the original list, but distinct 2105from it. For example, if you enter 2106\begin{center} 2107 {\tt list1 := [1,2,3]} 2108\end{center} 2109and then 2110\begin{center} 2111 {\tt list2 := copy(list1)} 2112\end{center} 2113then \texttt{list1} and \texttt{list2} will both be \texttt{[1,2,3]}, 2114but now if you enter 2115\begin{center} 2116 {\tt list1[1] =< 5} 2117\end{center} 2118then both \texttt{list1} will be equal to \texttt{[1,5,3]} but 2119\texttt{list2} will still be \texttt{[1,2,3]}. 2120 2121\subsection{Incrementing variables} 2122 2123You can increase the value of a variable \texttt{a} by \texttt{4}, for 2124example, with 2125\begin{center} 2126 {\tt a := a + 4} 2127\end{center} 2128If beforehand \texttt{a} were equal to \texttt{4}, it would now be 2129equal to \texttt{8}. A shorthand way of doing this is with the 2130\texttt{+=}\index{+=} operator; 2131\begin{center} 2132 {\tt a += 4} 2133\end{center} 2134will also increase the value of \texttt{a} by \texttt{4}. 2135 2136Similar shorthands exist for subtraction, multiplication and division. 2137If \texttt{a} is equal to \texttt{8} and you enter 2138\begin{center} 2139 {\tt a -= 2\index{-=}} 2140\end{center} 2141then \texttt{a} will be equal to \texttt{6}. If you follow this with 2142\begin{center} 2143 {\tt a *= 3\index{*=}} 2144\end{center} 2145then \texttt{a} will be equal to \texttt{18}, and finally 2146\begin{center} 2147 {\tt a /= 9\index{/=}} 2148\end{center} 2149will end with \texttt{a} equal to \texttt{2}. 2150 2151 2152\subsection{Storing and recalling variables and their values} 2153 2154You can store variables and their values for later use in a file of 2155your choosing with the \texttt{archive\index{archive}} function. This 2156function takes two arguments, a filename to store the variables in and 2157a variable or list of variables. 2158 2159If you have given the variable \texttt{a} the value \texttt{2} and the 2160variable \texttt{bee} the value \texttt{"letter"} (a string), then 2161entering 2162\begin{center} 2163 {\tt archive("foo",[a,bee])} 2164\end{center} 2165will create a file named ``\texttt{foo}'' which contains the values 2166\texttt{2} and \texttt{"letter"} in a format meant to be efficiently 2167read by \texttt{Xcas}. 2168 2169You can recall the values stored by \texttt{archive} with the 2170\texttt{unarchive\index{unarchive}} command, which takes a file name 2171as argument. If the file ``\texttt{foo}'' is as above, then 2172\begin{center} 2173 {\tt unarchive("foo")} 2174\end{center} 2175will result in 2176\begin{center} 2177 {\tt [2, letter]} 2178\end{center} 2179If you want to reassign these values to \texttt{a} and \texttt{bee}, 2180you can enter 2181\begin{center} 2182 {\tt [a,bee] := unarchive("foo")\index{unarchive}} 2183\end{center} 2184 2185\subsection{Copying variables} 2186\label{subsec:copyvar} 2187 2188If a variable has a value, such as 2189\begin{center} 2190 {\tt a := 1} 2191\end{center} 2192and you set a second variable to the first variable 2193\begin{center} 2194 {\tt b := a} 2195\end{center} 2196the new variable will have the same value as the first; in this case 2197\texttt{b} will be equal to \texttt{1}. If you later give the first 2198variable a new value; 2199\begin{center} 2200 {\tt a := 5} 2201\end{center} 2202the new value will still have the old value, in this case, \texttt{b} 2203will still be equal to \texttt{1}. 2204 2205The \texttt{CopyVar\index{CopyVar}} command will copy one variable to 2206another without evaluating the first variable; the new variable will 2207simply be a copy of the first. With \texttt{a} having the value of 2208\texttt{5}, as above, the command 2209\begin{center} 2210 {\tt CopyVar(a,c)} 2211\end{center} 2212will make \texttt{c} a copy of the variable \texttt{a}, so it will 2213have the value \texttt{5} also. If you now change the value of 2214\texttt{a} 2215\begin{center} 2216 {\tt a := 10} 2217\end{center} 2218then the value of \texttt{c} will also change; here, \texttt{c} will 2219now have the value \texttt{10}. 2220 2221\subsection{Assumptions on variables} 2222 2223If you enter 2224\begin{center} 2225 {\tt abs(var)} 2226\end{center} 2227the \texttt{Xcas} will return it unevaluated, since \texttt{Xcas} 2228doesn't know what type of value the variable is supposed to represent. 2229 2230The \texttt{assume\index{assume}} (or 2231\texttt{supposons\index{supposons}}) command will let you tell 2232\texttt{Xcas} some properties of a variable without giving the 2233variable a specific value. For example, if you enter 2234\begin{center} 2235 {\tt assume(var > 0)} 2236\end{center} 2237then \texttt{Xcas} will assume that \texttt{var} is a positive real 2238number, and so for example 2239\begin{center} 2240 {\tt abs(var)} 2241\end{center} 2242will be evaluated to 2243\begin{center} 2244 {\tt var} 2245\end{center} 2246 2247You can put one or more conditions in the \texttt{assume} command by 2248combining them with \texttt{and\index{and}} and \texttt{or\index{or}}. 2249For example, if you want the variable \texttt{a} to be in $[2,4) \cup 2250(6,\infty)$, you can enter 2251\begin{center} 2252 {\tt assume((a >= 2 and a < 4) or a > 6)} 2253\end{center} 2254 2255If a variable has attached assumptions, then making another assumption 2256with \texttt{assume} will remove the original assumptions. To add 2257extra assumptions, you can either use the 2258\texttt{additionally\index{additionally}} command or give 2259\texttt{assume} a second argument of 2260\texttt{additionally\index{additionally@\textit{additionally}}}. If 2261you assume that $b > 0$ with 2262\begin{center} 2263 {\tt assume(b > 0)} 2264\end{center} 2265and you want to add the condition that $b < 1$, you can either enter 2266\begin{center} 2267 {\tt assume(b < 1, additionally)} 2268\end{center} 2269or 2270\begin{center} 2271 {\tt additionally(b < 1)} 2272\end{center} 2273 2274As well as equalities and inequalities, you can make assumptions about 2275the domain of a variable. If you want \texttt{n} to represent a 2276positive integer\index{integer@\textit{integer}}, for example, you can enter 2277\begin{center} 2278 {\tt assume(n, integer)} 2279\end{center} 2280If you want \texttt{n} to be a positive integer, you can add the 2281condition 2282\begin{center} 2283 {\tt additionally(n > 0)} 2284\end{center} 2285 2286You can use the \texttt{about\index{about}} command to check the assumptions on a 2287variable; for the above positive integer \texttt{n}, if you enter 2288\begin{center} 2289 {\tt about(n)} 2290\end{center} 2291you will get 2292\begin{center} 2293 {\tt assume[integer,[line[0,+infinity]],[0]]} 2294\end{center} 2295The first element tells you that \texttt{n} is an integer, the second 2296element tells you that \texttt{n} is between \texttt{0} and 2297\texttt{+infinity}, and the third element tells you that the value 2298\texttt{0} is excluded. 2299 2300If you assume that a variable is equal to a specific value, such as 2301\begin{center} 2302 {\tt assume(c = 2)} 2303\end{center} 2304then by default the variable \texttt{c} will remain unevaluated in 2305later levels. If you want an expression involving \texttt{c} to be 2306evaluated, you would need to put the expression inside the 2307\texttt{evalf\index{evalf}} command; if you enter 2308\begin{center} 2309 {\tt evalf(c\^{}2 + 3)} 2310\end{center} 2311then you will get 2312\begin{center} 2313 {\tt 7.0} 2314\end{center} 2315Right below the \texttt{assume(c = 2)} command line 2316there will be a slider, namely arrows pointing left and right with the 2317value \texttt{2} between them. These 2318can be used to change the values of \texttt{c}. If you click on the 2319right arrow, the \texttt{assume(c = 2)} command will transform to 2320\begin{center} 2321 {\tt assume(c=[2.2,-10.0,10.0,0.0])} 2322\end{center} 2323and the value between the arrows will be \texttt{2.2}. Also, any 2324later levels where the variable \texttt{c} is evaluated will be 2325re-evaluated with the value of \texttt{c} now \texttt{2.2}. The 2326output to \texttt{evalf(c\^{}2 + 3} will become 2327\begin{center} 2328 {\tt 7.84} 2329\end{center} 2330The \texttt{-10.0} and \texttt{10.0} in the \texttt{assume} line 2331represent the smallest and largest values that \texttt{c} can become 2332using the sliders. You can set them yourself in the \texttt{assume} 2333command, as well as the increment that the value will change; if you 2334want \texttt{c} to start with the value \texttt{5} and vary between 2335\texttt{2} and \texttt{8} in increments of \texttt{0.05}, then you can 2336enter 2337\begin{center} 2338 {\tt assume(c = [5,2,8,0.05])} 2339\end{center} 2340 2341You can remove any assumptions you have made about a variable with the 2342\texttt{purge\index{purge}} command; if you enter 2343\begin{center} 2344 {\tt purge(a)} 2345\end{center} 2346then \texttt{a} will no longer have any assumptions made about it. 2347You can remove assumptions from more than one variable at a time; 2348\begin{center} 2349 {\tt purge(a,b)} 2350\end{center} 2351will remove any assumptions about \texttt{a} and \texttt{b}. 2352 2353\subsection{Unassigning variables} 2354 2355The \texttt{VARS()}\index{VARS} command will list the variables to which you have 2356assigned values or assumptions. If you begin by entering 2357\begin{center} 2358 {\tt a := 1} 2359\end{center} 2360and 2361\begin{center} 2362 {\tt anothervar := 2} 2363\end{center} 2364then 2365\begin{center} 2366 {\tt VARS()} 2367\end{center} 2368will return 2369\begin{center} 2370 {\tt [a, anothervar]} 2371\end{center} 2372 2373The \texttt{purge\index{purge}} command will clear the values and 2374assumptions you make on variables. You can clear the values and 2375assumptions you have made on all variables with 2376\begin{center} 2377 {\tt restart\index{restart}} 2378\end{center} 2379or 2380\begin{center} 2381 {\tt rm\_all\_vars()\index{rm\_all\_vars}} 2382\end{center} 2383command. 2384 2385\section{Functions} 2386 2387\subsection{Defining functions} 2388 2389You can use the \texttt{:=}\index{:=} and \texttt{=>}\index{=>} 2390operators to define functions; both 2391\begin{center} 2392 {\tt f(x) := x\^{}2} 2393\end{center} 2394and 2395\begin{center} 2396 {\tt x\^{}2 => f(x)} 2397\end{center} 2398give the name \texttt{f} to the function which takes a value and 2399returns the square of the value. If you then enter 2400\begin{center} 2401 {\tt f(3)} 2402\end{center} 2403you will get 2404\begin{center} 2405 {\tt 9} 2406\end{center} 2407 2408You can give \texttt{Xcas} a function without a name with the 2409\texttt{->}\index{->} operator; the squaring function can be written 2410without a name as 2411\begin{center} 2412 {\tt x -> x\^{}2} 2413\end{center} 2414You can use this form of the function to assign it to a name; both 2415\begin{center} 2416 {\tt f := x -> x\^{}2} 2417\end{center} 2418and 2419\begin{center} 2420 {\tt x -> x\^{}2 => f} 2421\end{center} 2422are alternate ways to define \texttt{f} as the squaring function. 2423 2424You can similarly define functions of more than one variable. For 2425example, to define a function which takes the lengths of the two legs 2426of a right triangle and returns the hypotenuse, you could enter 2427\begin{center} 2428 {\tt hypot(a,b) := sqrt(a\^{}2 + b\^{}2)} 2429\end{center} 2430or 2431\begin{center} 2432 {\tt hypot := (a,b) -> sqrt(a\^{}2 + b\^{}2)} 2433\end{center} 2434 2435\subsection{Defining piecewise defined functions} 2436\index{piecewise defined functions} 2437\label{subsec:piecewise} 2438 2439You can use \texttt{Xcas}'s control structures to define functions not 2440given by a single simple formula. Notably, you can use the 2441\texttt{ifte\index{ifte}} command or \texttt{? :\index{?:}} operator 2442to define piecewise-defined functions. 2443 2444The \texttt{ifte} command takes three arguments; the first argument is 2445a condition, the second argument tells the command what to return when 2446the condition is true, and the third argument tells the command what 2447to return when the condition is false. For example, you could define 2448your own absolute value function with 2449\begin{center} 2450 {\tt myabs(x) := ifte(x >= 0, x -1*x)} 2451\end{center} 2452Afterwards, for example, entering 2453\begin{center} 2454 {\tt myabs(-4)} 2455\end{center} 2456will return 2457\begin{center} 2458 {\tt 4} 2459\end{center} 2460However, this will return an error if it can't evaluate the 2461conditional. For example, if you enter 2462\begin{center} 2463 {\tt myabs(x)} 2464\end{center} 2465you will get the error 2466\begin{center} 2467 {\tt Ifte: Unable to check test Error: Bad Argument Value} 2468\end{center} 2469 2470The \texttt{?:}\index{? :} construct behaves similarly to \texttt{ifte} but is 2471structured differently. Here, the condition comes first, followed by 2472\texttt{?}, then what to return if the condition is true, followed by 2473the \texttt{:}, and then what to return if the condition is false. 2474You could define your absolute value function with 2475\begin{center} 2476 {\tt myabs(x) := (x >= 0)? x: -1*x} 2477\end{center} 2478If you enter 2479\begin{center} 2480 {\tt myabs(-4)} 2481\end{center} 2482you will again get 2483\begin{center} 2484 {\tt 4} 2485\end{center} 2486but now if the conditional can't be evaluated, you won't get an error. 2487\begin{center} 2488 {\tt myabs(x)} 2489\end{center} 2490will return 2491\begin{center} 2492 {\tt ((x >= 0)? x: -x)} 2493\end{center} 2494 2495The \texttt{when\index{when}} and \texttt{IFTE\index{IFTE}} commands 2496are synonyms for the \texttt{? :} construct; 2497\begin{center} 2498 {\tt (\textit{condition})? \textit{true-result}: \textit{false-result}} 2499\end{center} 2500\begin{center} 2501 {\tt when(\textit{condition}, \textit{true-result}, \textit{false-result})} 2502\end{center} 2503and 2504\begin{center} 2505 {\tt IFTE(\textit{condition}, \textit{true-result}, \textit{false-result})} 2506\end{center} 2507all represent the same expression. 2508 2509If you want to define a function with several pieces, it may be 2510simpler to use the \texttt{piecewise\index{piecewise}} function. The 2511arguments to this function are alternately conditions and results to 2512return if the condition is true, with the last argument being what to 2513return if none of the conditions are true. For example, to define the 2514function given by 2515\[ 2516f(x) = 2517\begin{cases} 2518-2 & \text{if } x < -2\\ 25193x+4 & \text{if } -2 \le x < -1\\ 25201 & \text{if } -1 \le x < 0\\ 2521x + 1 & \text{if } x \ge 0 2522\end{cases} 2523\] 2524you can enter 2525\begin{center} 2526 {\tt f(x) := piecewise(x < -2, -2, x < -1, 3*x+4, x < 0, 1, x + 1)} 2527\end{center} 2528 2529\section{Directories} 2530\index{directories} 2531 2532\subsection{Working directories} 2533 2534\texttt{Xcas} has a working directory that it uses to store files that 2535it creates; typically the user's home directory. You can print the 2536name of the current working directory with the 2537\texttt{pwd()}\index{pwd} command; if you enter 2538\begin{center} 2539 {\tt pwd()} 2540\end{center} 2541you might get something like 2542\begin{center} 2543 {\tt /home/username} 2544\end{center} 2545 2546You can change the working directory with the \texttt{cd\index{cd}} 2547command; if you enter 2548\begin{center} 2549 {\tt cd("foo")} 2550\end{center} 2551or (on a Unix system) 2552\begin{center} 2553 {\tt cd("/home/username/foo")} 2554\end{center} 2555will change to the directory \texttt{foo}, if it exists. 2556Afterwards, any files that you save from \texttt{Xcas} will be in that 2557directory. 2558 2559If you have values saved in a file, then you'll need to be in that 2560working directory to load it. Note that if you have the same file 2561name in different directories, then the result of loading the file 2562name will depend on which directory you are in. 2563 2564\subsection{Reading files} 2565 2566If you have a function or other \texttt{Xcas} information in a file, 2567you can load it with the \texttt{read\index{read}} function. If the 2568file is named \texttt{myfunction.cxx}, then 2569\begin{center} 2570 {\tt read("myfunction.cxx")} 2571\end{center} 2572will load the file, as long as the directory is in the current working 2573directory. If the file is in a different directory, you can still 2574load it by giving the path to the file, 2575\begin{center} 2576 {\tt read("/path/to/file/myfunction.cxx")} 2577\end{center} 2578 2579While \texttt{read} can be used to load files containing \texttt{Xcas} 2580functions, which typically end in \texttt{.cxx}, if you want to load a 2581saved session you should use the \texttt{load\index{load}} function; 2582\begin{center} 2583 {\tt load("mysession.cas")} 2584\end{center} 2585 2586\subsection{Internal directories} 2587\index{internal directories} 2588 2589You can create a directory that isn't actually on your hard drive but 2590is treated like one from \texttt{Xcas}. You can create such an 2591internal directory with the \texttt{NewFold\index{NewFold}} command, 2592which takes a variable name as an argument. If you enter 2593\begin{center} 2594 {\tt NewFold(MyIntDir)} 2595\end{center} 2596then there will be a new internal directory named \texttt{MyIntDir}. 2597Internal directories will also be listed with the 2598\texttt{VARS()}\index{VARS} 2599command. To actually use this directory, you'll have to use the 2600\texttt{SetFold\index{SetFold}} command; 2601\begin{center} 2602 {\tt SetFold(MyIntDir)} 2603\end{center} 2604Finally, we can print out the internal directory that we are in with 2605the \texttt{GetFold\index{GetFold}} command; entering 2606\begin{center} 2607 {\tt GetFold()} 2608\end{center} 2609will result in 2610\begin{center} 2611 {\tt MyIntDir} 2612\end{center} 2613Afterwards, if this directory is empty, you can delete it with the 2614\texttt{DelFold\index{DelFold}} command; 2615\begin{center} 2616 {\tt DelFold(MyIntDir)} 2617\end{center} 2618 2619 2620\chapter{The CAS functions}\label{sec:cas} 2621 2622\section{Symbolic constants : {\tt e pi infinity i}}\index{e}\index{pi} 2623\index{i}\index{+infinity}\index{-infinity}\index{infinity} 2624\noindent {\tt e} is the number $\exp(1)$;\\ 2625{\tt pi} is the number $\pi$.\\ 2626{\tt infinity} is unsigned $\infty$.\\ 2627{\tt +infinity} is $+\infty$.\\ 2628{\tt -infinity} is $-\infty$.\\ 2629{\tt i} is the complex number $i$. 2630 2631\section{Booleans} 2632\label{sec:boolean} 2633\subsection{The values of a boolean : {\tt true false}}\index{true}\index{false}\index{TRUE}\index{FALSE} 2634The value of a boolean is {\tt true} or {\tt false}.\\ 2635The synonyms are :\\ 2636{\tt true} or {\tt TRUE} or {\tt 1},\\ 2637{\tt false} or {\tt FALSE} or {\tt 0}.\\ 2638Tests or conditions are boolean functions. 2639 2640\subsection{Tests : {\tt ==, !=, >, >=, <, =<}}\index{==}\index{>}\index{<}\index{>=}\index{<=}\index{\symbol{33}=} 2641{\tt ==, !=, >, >=, <, =<} are infixed operators.\\ 2642{\tt a==b} tests the equality between {\tt a} and {\tt b} and returns {\tt 1} 2643if {\tt a} is equal to {\tt b} and {\tt 0} otherwise.\\ 2644{\tt a!=b} returns {\tt 1} if {\tt a} and {\tt b} are different and {\tt 0} 2645otherwise.\\ 2646 {\tt a>=b} returns {\tt 1} if {\tt a} is greater than or equal to {\tt b} 2647and {\tt 0} otherwise.\\ 2648{\tt a>b} returns {\tt 1} if {\tt a} is strictly greater than {\tt b} 2649and {\tt 0} otherwise.\\ 2650{\tt a<=b} returns {\tt 1} if {\tt a} is less than or equal to {\tt b} and 2651{\tt 0} otherwise.\\ 2652{\tt a<b} returns {\tt 1} if {\tt a} is strictly less than {\tt b} 2653and {\tt 0} otherwise.\\ 2654To write an algebraic function having the same result as an 2655{\tt if...then...else}, we use the boolean function {\tt ifte}.\\ 2656For example : 2657\begin{center}{\tt f(x):=ifte(x>0,true,false)}\end{center} 2658defines the boolean function $f$ such that {\tt f(x)= true} if 2659$x \in (0;+\infty[$ and {\tt f(x)=false} if $x \in (-\infty;0]$.\\ 2660Input : 2661\begin{center}{\tt f(0)==0}\end{center} 2662Output : 2663\begin{center}{\tt 1}\end{center} 2664{\bf Look out !}\\ 2665{\tt a=b} is not a boolean !!!!\\ 2666{\tt a==b} is a boolean.\\ 2667 2668\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} 2669{\tt or} (or {\tt ||}), {\tt xor}, {\tt and} (or {\tt \&\&}) are infixed 2670operators.\\ 2671{\tt not} is a prefixed operators.\\ 2672If {\tt a} and {\tt b} are two booleans :\\ 2673{\tt (a or b)} {\tt (a || b)} returns {\tt 0} (or {\tt false}) if {\tt a} and 2674{\tt b} are equal to 0 and returns {\tt 1} (or {\tt true}) otherwise.\\ 2675{\tt (a xor b)} returns {\tt 1} if {\tt a} is equal to 1 and {\tt b} is 2676equal to 0 or if {\tt a} is equal to 0 and {\tt b} is equal to 1 and returns 0 2677 if {\tt a} and {\tt b} are equal to 0 2678 or if {\tt a} and {\tt b} are equal to 1 (it is the "exclusive or").\\ 2679{\tt (a and b)} or {\tt (a \&\& b)} returns {\tt 1} (or {\tt true}) if {\tt a} 2680 and {\tt b} are equal to 1 and {\tt 0} (or {\tt false}) otherwise.\\ 2681{\tt not(a)} returns {\tt 1} (or {\tt true}) if {\tt a} is equal to 0 (or 2682{\tt false}), and {\tt 0} (or {\tt false}) if {\tt a} is equal to 1 (or 2683{\tt true}).\\ 2684Input : 2685\begin{center}{\tt 1>=0 or 1<0}\end{center} 2686Output : 2687\begin{center}{\tt 1}\end{center} 2688Input : 2689\begin{center}{\tt 1>=0 xor 1>0}\end{center} 2690Output : 2691\begin{center}{\tt 0}\end{center} 2692Input : 2693\begin{center}{\tt 1>=0 and 1>0}\end{center} 2694Output : 2695\begin{center}{\tt 1}\end{center} 2696Input : 2697\begin{center}{\tt not(0==0)}\end{center} 2698Output : 2699\begin{center}{\tt 0}\end{center} 2700 2701\subsection{Transform a boolean expression to a list : {\tt exp2list}}\index{exp2list} 2702\noindent{\tt exp2list} returns the list {\tt [expr0,expr1]} when the argument 2703is {\tt (var=expr0) or (var=expr1)}.\\ 2704{\tt exp2list} is used in TI mode for easier processing of the answer to a 2705{\tt solve} command.\\ 2706Input : 2707\begin{center}{\tt exp2list((x=2) or (x=0))}\end{center} 2708Output : 2709\begin{center}{\tt [2,0]}\end{center} 2710Input : 2711\begin{center}{\tt exp2list((x>0) or (x<2))}\end{center} 2712Output : 2713\begin{center}{\tt [0,2]}\end{center} 2714In TI mode input : 2715\begin{center}{\tt exp2list(solve((x-1)*(x-2)))}\end{center} 2716Output : 2717\begin{center}{\tt [1,2]}\end{center} 2718 2719\subsection{Evaluate booleans : {\tt evalb}}\index{evalb} 2720\noindent Inside Maple, {\tt evalb} evaluates an boolean expression. 2721Since {\tt Xcas} evaluates booleans automatically, {\tt evalb} is only 2722here for compatibility and is equivalent to {\tt eval}\\ 2723Input : 2724\begin{center}{\tt evalb(sqrt(2)>1.41)}\end{center} 2725or : 2726\begin{center}{\tt sqrt(2)>1.41}\end{center} 2727Output : 2728\begin{center}{\tt 1}\end{center} 2729Input : 2730\begin{center}{\tt evalb(sqrt(2)>1.42)}\end{center} 2731or : 2732\begin{center}{\tt sqrt(2)>1.42}\end{center} 2733Output : 2734\begin{center}{\tt 0}\end{center} 2735 2736\section{Bitwise operators} 2737\subsection{Operators {\tt bitor, bitxor, bitand}}\index{bitor|textbf}\index{bitxor|textbf}\index{bitand|textbf} 2738The integers may be written using hexadecimal notation 0x... 2739for example 0x1f represents 16+15=31 in decimal. 2740Integers may also be output in hexadecimal notation 2741(click on the red CAS status button and select {\tt Base (Integers)}).\\ 2742{\tt bitor} is the logical inclusive {\tt or} (bitwise).\\ 2743Input : 2744\begin{center}{\tt bitor(0x12,0x38)}\end{center} 2745or : 2746\begin{center}{\tt bitor(18,56)}\end{center} 2747Output : 2748\begin{center}{\tt 58}\end{center} 2749because :\\ 2750{\tt 18} is written {\tt 0x12} in base 16 or {\tt 0b010010} in base 2,\\ 2751{\tt 56} is written {\tt 0x38} in base 16 or {\tt 0b111000} in base 2,\\ 2752hence {\tt bitor(18,56)} is {\tt 0b111010} in base 2 and so is equal to 2753{\tt 58}.\\ 2754 2755{\tt bitxor} is the logical exclusive {\tt or} (bitwise).\\ 2756Input : 2757\begin{center}{\tt bitxor(0x12,0x38)}\end{center} 2758or : 2759\begin{center}{\tt bitxor(18,56)}\end{center} 2760Output : 2761\begin{center}{\tt 42}\end{center} 2762because :\\ 2763{\tt 18} is written {\tt 0x12} in base 16 and {\tt 0b010010} in base 2,\\ 2764{\tt 56} is written {\tt 0x38} in base 16 and {\tt 0b111000} in base 2,\\ 2765{\tt bitxor(18,56)} is written {\tt 0b101010} in base 2 and so, is equal to 2766{\tt 42}.\\ 2767 2768{\tt bitand} is the logical {\tt and} (bitwise).\\ 2769Input : 2770\begin{center}{\tt bitand(0x12,0x38)}\end{center} 2771or : 2772\begin{center}{\tt bitand(18,56)}\end{center} 2773Output : 2774\begin{center}{\tt 16}\end{center} 2775because :\\ 2776{\tt 18} is written {\tt 0x12} in base 16 and {\tt 0b010010} in base 2,\\ 2777{\tt 56} is written {\tt 0x38} in base 16 and {\tt 0b111000} in base 2,\\ 2778{\tt bitand(18,56)} is written {\tt 0b010000} in base 2 and so is equal to 2779{\tt 16}. 2780 2781\subsection{Bitwise Hamming distance : {\tt hamdist}}\index{hamdist|textbf} 2782The Hamming distance is the number of differences 2783of the bits of the two arguments.\\ 2784Input : 2785\begin{center}{\tt hamdist(0x12,0x38)}\end{center} 2786or : 2787\begin{center}{\tt hamdist(18,56)}\end{center} 2788Output : 2789\begin{center}{\tt 3}\end{center} 2790because :\\ 2791{\tt 18} is written {\tt 0x12} in base 16 and {\tt 0b010010} in base 2,\\ 2792{\tt 56} is written {\tt 0x38} in base 16 and {\tt 0b111000} in base 2,\\ 2793{\tt hamdist(18,56)} is equal to {\tt 1+0+1+0+1+0} and so is equal to {\tt 3}. 2794 2795\section{Strings} 2796\subsection{Character and string : {\tt "}}\index{\symbol{34}|textbf} 2797\noindent {\tt "} is used to delimit a string. 2798A character is a string of length one.\\ 2799Do not confuse {\tt "} with 2800{\tt '} (or {\tt quote}) which is used to avoid evaluation 2801of an expression \index{quote}. For example, 2802{\tt "a"} returns a string of one character 2803but {\tt 'a'} or {\tt quote(a)} returns 2804the variable {\tt a} unevaluated.\\ 2805 2806When a string is input in a command line, it is evaluated to itself 2807hence the output is the same string. Use {\tt +} 2808to concatenate two strings or a string and another object.\\ 2809Example :\\ 2810Input : 2811\begin{center}{\tt "Hello"}\end{center} 2812{\tt "Hello"} is the input and also the output.\\ 2813Input : 2814\begin{center}{\tt "Hello"+", how are you?"}\end{center} 2815Output : 2816\begin{center}{\tt "Hello, how are you?"}\end{center} 2817Index notation is used to get the n-th character of a string, 2818(as for lists). Indices begin at 0 in Xcas mode, 1 in other modes.\\ 2819Example :\\ 2820Input : 2821\begin{center}{\tt "Hello"[1]}\end{center} 2822Output : 2823\begin{center}{\tt "e"}\end{center} 2824 2825\subsection{First character, middle and end of a string : {\tt head mid tail}}\index{head|textbf} \index{tail|textbf}\index{mid} 2826\begin{itemize} 2827\item {\tt head(s)} returns the first character of the string {\tt s}.\\ 2828 Input : 2829\begin{center}{\tt head("Hello")}\end{center} 2830Output : 2831\begin{center}{\tt "H"}\end{center} 2832\item {\tt mid(s,p,q)} returns the part of the string 2833{\tt s} of size {\tt q} beginning with the character at index {\tt p}.\\ 2834Remember that the first index is 0 in Xcas mode.\\ 2835Input : 2836\begin{center}{\tt mid("Hello",1,3)}\end{center} 2837Output : 2838\begin{center}{\tt "ell"}\end{center} 2839\item {\tt tail(s)} returns the string {\tt s} without its first character.\\ 2840Input : 2841\begin{center}{\tt tail("Hello")}\end{center} 2842Output : 2843\begin{center}{\tt "ello"}\end{center} 2844\end{itemize} 2845 2846\subsection{Concatenation of a sequence of words : {\tt cumSum}}\index{cumSum} 2847\noindent{\tt cumSum} works on strings like it does on expressions by 2848doing partial concatenation.\\ 2849{\tt cumSum} takes as argument a list of strings.\\ 2850{\tt cumSum} returns a list of strings where the element of index $k$ is the 2851concatenation of the strings with indices 0 to $k$ .\\ 2852Input : 2853\begin{center}{\tt cumSum("Hello, ","is ","that ","you?")}\end{center} 2854Output : 2855\begin{center}{\tt "Hello, ","Hello, is ","Hello, is that ","Hello, is that you?}\end{center} 2856 2857\subsection{ASCII code of a character : {\tt ord}}\index{ord|textbf} 2858\noindent {\tt ord} takes as argument a string {\tt s} (resp. 2859a list {\tt l} of 2860strings).\\ 2861{\tt ord} returns the ASCII code of the first character of {\tt s} (resp. the 2862list of the ASCII codes of the first character of the elements of {\tt l}).\\ 2863Input : 2864\begin{center}{\tt ord("a")}\end{center} 2865Output : 2866\begin{center}{\tt 97}\end{center} 2867Input : 2868\begin{center}{\tt ord("abcd")}\end{center} 2869Output : 2870\begin{center}{\tt 97}\end{center} 2871Input : 2872\begin{center}{\tt ord(["abcd","cde"])}\end{center} 2873Output : 2874\begin{center}{\tt [97,99]}\end{center} 2875Input : 2876\begin{center}{\tt ord(["a","b","c","d"])}\end{center} 2877Output : 2878\begin{center}{\tt [97,98,99,100]}\end{center} 2879 2880\subsection{ASCII code of a string : {\tt asc}}\index{asc} 2881\noindent {\tt asc} takes as argument a string {\tt s}.\\ 2882{\tt asc} returns the list of the ASCII codes of the characters of {\tt s}.\\ 2883Input : 2884\begin{center}{\tt asc("abcd")}\end{center} 2885Output : 2886\begin{center}{\tt [97,98,99,100]}\end{center} 2887Input : 2888\begin{center}{\tt asc("a")}\end{center} 2889Output : 2890\begin{center}{\tt [97]}\end{center} 2891 2892\subsection{String defined by the ASCII codes of its characters : {\tt char}}\index{char} 2893\noindent {\tt char} takes as argument a list {\tt l} of ASCII codes.\\ 2894{\tt char} returns the string whose characters have as ASCII codes the 2895elements of the list {\tt l}.\\ 2896Input : 2897\begin{center}{\tt char([97,98,99,100])}\end{center} 2898Output : 2899\begin{center}{\tt "abcd"}\end{center} 2900Input : 2901\begin{center}{\tt char(97)}\end{center} 2902Output : 2903\begin{center}{\tt "a"}\end{center} 2904Input : 2905\begin{center}{\tt char(353)}\end{center} 2906Output : 2907\begin{center}{\tt "a"}\end{center} 2908because:\\ 2909$353-256=97$. 2910 2911\subsection{Find a character in a string : {\tt inString}}\index{inString} 2912\noindent {\tt inString} takes two arguments : a string {\tt S} and a 2913character {\tt c}.\\ 2914{\tt inString} tests if the character {\tt c} is in the string {\tt S}.\\ 2915 {\tt inString} returns the index of its first occurrence 2916or {\tt -1} if {\tt c} is not in {\tt S}.\\ 2917Input : 2918\begin{center}{\tt inString("abcded","d")}\end{center} 2919Output : 2920\begin{center}{\tt 3}\end{center} 2921Input : 2922\begin{center}{\tt inString("abcd","e")}\end{center} 2923Output : 2924\begin{center}{\tt -1}\end{center} 2925 2926\subsection{Concat objects into a string : {\tt cat}}\index{cat|textbf} 2927\noindent {\tt cat} takes as argument a sequence of objects.\\ 2928{\tt cat} concatenates these objects into a string.\\ 2929Input : 2930\begin{center}{\tt cat("abcd",3,"d")}\end{center} 2931Output : 2932\begin{center}{\tt "abcd3d"}\end{center} 2933Input : 2934\begin{center}{\tt c:=5}\end{center} 2935\begin{center}{\tt cat("abcd",c,"e")}\end{center} 2936Output : 2937\begin{center}{\tt "abcd5e"}\end{center} 2938Input : 2939\begin{center}{\tt purge(c)}\end{center} 2940\begin{center}{\tt cat(15,c,3)}\end{center} 2941Output : 2942\begin{center}{\tt "15c3"}\end{center} 2943 2944\subsection{Add an object to a string : {\tt +}}\index{+} 2945\noindent {\tt +} is an infixed operator (resp. {\tt '+'} is a prefixed 2946operator).\\ 2947If {\tt +} (resp. {\tt '+'}) takes as argument a string (resp. 2948a sequence of objects with a string as first or second argument), 2949the result is the concatenation of these objects into a string.\\ 2950{\bf warning}\\ 2951{\tt +} is infixed and {\tt '+'} is prefixed.\\ 2952Input : 2953\begin{center}{\tt '+'("abcd",3,"d")}\end{center} 2954Output : 2955\begin{center}{\tt "abcd"+3+"d"}\end{center} 2956Output : 2957\begin{center}{\tt "abcd3d"}\end{center} 2958Input : 2959\begin{center}{\tt c:=5}\end{center} 2960Then input: 2961\begin{center}{\tt "abcd"+c+"e"}\end{center} 2962or : 2963\begin{center}{\tt '+'("abcd",c,"d")}\end{center} 2964Output : 2965\begin{center}{\tt "abcd5e"}\end{center} 2966 2967\subsection{Transform an integer into a string : {\tt cat +}}\index{+}\index{cat} 2968\noindent Use {\tt cat} with the integer as argument, or add the integer 2969to an empty string\\ 2970Input : 2971\begin{center}{\tt ""+123}\end{center} 2972or : 2973\begin{center}{\tt cat(123)}\end{center} 2974Output : 2975\begin{center}{\tt "123"}\end{center} 2976 2977\subsection{Transform a string into a number : {\tt expr}}\index{expr|textbf}\label{sec:expr1} 2978Use {\tt expr}, the parser with a string representing a number. 2979\begin{itemize} 2980\item For integers, enter the string representing the integer without 2981leading 0 for basis 10, with prefix {\tt 0x} for basis 16, 2982{\tt 0} for basis 8 or {\tt 0b} for basis 2. 2983Input : 2984\begin{center}{\tt expr("123")}\end{center} 2985Output : 2986\begin{center}{\tt 123}\end{center} 2987Input : 2988\begin{center}{\tt expr("0123")}\end{center} 2989Output : 2990\begin{center}{\tt 83}\end{center} 2991because :\\ 2992$1*8^2+2*8+3=83$\\ 2993Input : 2994\begin{center}{\tt expr("0x12f")}\end{center} 2995Output : 2996\begin{center}{\tt 303}\end{center} 2997Because : $1*16^2+2*16+15=303$ 2998\item For decimal numbers, use a string with a {\tt .} or {\tt e} inside.\\ 2999Input : 3000\begin{center}{\tt expr("123.4567")}\end{center} 3001Output : 3002\begin{center}{\tt 123.4567}\end{center} 3003Input : 3004\begin{center}{\tt expr("123e-5")}\end{center} 3005Output : 3006\begin{center}{\tt 0.00123}\end{center} 3007\item Note that {\tt expr} more generally transforms a string 3008into a command if the command exists.\\ 3009Input : 3010\begin{center}{\tt expr("a:=1")}\end{center} 3011Output : 3012\begin{center}{\tt 1}\end{center} 3013Then, input : 3014\begin{center}{\tt a}\end{center} 3015Output : 3016\begin{center}{\tt 1}\end{center} 3017\end{itemize} 3018 3019\section{Write an integer in base $b$: {\tt convert}} 3020\label{sec:convertbase} 3021\index{convert}\index{base@{\sl base}|textbf} 3022\noindent{\tt convert} or {\tt convertir} can do different kind 3023of conversions depending on the option given as the second argument. 3024 3025To convert an integer {\tt n} into the list of its coefficients in 3026base {\tt b}, the option is {\tt base}. The arguments of {\tt convert} or 3027{\tt convertir} are an integer {\tt n}, {\tt base} and {\tt b}, the value of the 3028 basis.\\ 3029{\tt convert} or {\tt convertir} returns the list of coefficients in a {\tt b} 3030basis of the integer {\tt n}.\\ 3031Input : 3032\begin{center}{\tt convert(123,base,8)}\end{center} 3033Output : 3034\begin{center}{\tt [3,7,1]}\end{center} 3035To check the answer, 3036input {\tt expr("0173")} or {\tt horner(revlist([3,7,1]),8)} 3037or {\tt convert([3,7,1],base,8)}, the output is {\tt 123}\\ 3038Input : 3039\begin{center}{\tt convert(142,base,12)}\end{center} 3040Output : 3041\begin{center}{\tt [10,11]}\end{center} 3042 3043To convert the list of coefficients of an integer {\tt n} in base {\tt b}, 3044the option is also {\tt base}. 3045{\tt convert} or {\tt convertir} returns the integer {\tt n}.\\ 3046Input : 3047\begin{center}{\tt convert([3,7,1],base,8)}\end{center} 3048or : 3049\begin{center}{\tt horner(revlist([3,7,1]),8)}\end{center} 3050Output : 3051\begin{center}{\tt 123}\end{center} 3052Input : 3053\begin{center}{\tt convert([10,11],base,12)}\end{center} 3054or : 3055\begin{center}{\tt horner(revlist([10,11]),12)}\end{center} 3056Output : 3057\begin{center}{\tt 142}\end{center} 3058 3059\section{Integers (and Gaussian Integers)} 3060 For all functions in this section, you can use Gaussian integers (numbers of 3061the form $a+ib$, where $a$ and $b$ are in $\mathbb Z$) in place of integers. 3062 3063\subsection{The factorial : {\tt factorial}}\index{factorial} 3064{\tt Xcas} can manage integers with unlimited precision, such as the 3065following:\\ 3066Input : 3067\begin{center}{\tt factorial(100)}\end{center} 3068Output : 3069\begin{verbatim} 3070 9332621544394415268169923885626670049071596826438162 3071 1468592963895217599993229915608941463976156518286253 3072 697920827223758251185210916864000000000000000000000000 3073\end{verbatim} 3074\subsection{GCD : {\tt gcd igcd}}\index{gcd|textbf}\index{igcd|textbf}\label{sec:igcd} 3075\noindent{\tt gcd} or {\tt igcd} denotes the gcd (greatest common divisor) 3076of several integers (for polynomials, see also \ref{sec:gcd}).\\ 3077{\tt gcd} or {\tt igcd} returns the {\tt GCD} of integers.\\ 3078Input : 3079\begin{center}{\tt gcd(18,15)}\end{center} 3080Output : 3081\begin{center}{\tt 3}\end{center} 3082Input : 3083\begin{center}{\tt gcd(18,15,21,36) }\end{center} 3084Output : 3085\begin{center}{\tt 3}\end{center} 3086Input : 3087\begin{center}{\tt gcd([18,15,21,36])}\end{center} 3088Output : 3089\begin{center}{\tt 3}\end{center} 3090We can also put as parameters two lists of same size (or a matrix with 2 3091rows), in this case {\tt gcd} returns the greatest common divisor of 3092the elements with same index (or in the same column).\\ 3093Input : 3094\begin{center}{\tt gcd([6,10,12],[21,5,8])}\end{center} 3095or : 3096\begin{center}{\tt gcd([[6,10,12],[21,5,8]])}\end{center} 3097Output : 3098\begin{center}{\tt [3,5,4]}\end{center} 3099{\bf An example}\\ 3100Find the greatest common divisor of $4n+1$ and $5n+3$ when $n \in \mathbb N$.\\ 3101Input :\\ 3102\begin{center}{\tt f(n):=gcd(4*n+1,5*n+3)}\end{center} 3103Then, input :\\ 3104\begin{verbatim} 3105 essai(n):={ 3106 local j,a,L; 3107 L:=NULL; 3108 for (j:=-n;j<n;j++) { 3109 a:=f(j); 3110 if (a!=1) { 3111 L:=L,[j,a]; 3112 } 3113 } 3114 return L; 3115 } 3116\end{verbatim} 3117Then, input :\\ 3118\begin{center}{\tt essai(20)}\end{center} 3119Output : 3120\begin{center}{\tt [-16,7],[-9,7],[-2,7],[5,7],[12,7],[19,7]}\end{center} 3121So we now have to prove that :\\ 3122If $n\not=5+k*7$ (for $k \in \mathbb Z$), $4n+1$ and $5n+3$ are mutually prime, 3123and $n=5+k*7$ (for $k \in \mathbb Z$), then the greatest common divisor of $4n+1$ 3124and $5n+3$ is 7. 3125\subsection{GCD : {\tt Gcd}}\index{Gcd|textbf} 3126\noindent{\tt Gcd} is the inert form of {\tt gcd}. See the section 3127\ref{sec:gcd} for polynomials with coefficients in $\Z/p\Z$ 3128 for using this instruction.\\ 3129Input : 3130\begin{center}{\tt Gcd(18,15)}\end{center} 3131Output : 3132\begin{center}{\tt gcd(18,15)}\end{center} 3133 3134 3135\subsection{GCD of a list of integers : {\tt lgcd}}\index{lgcd} 3136\noindent{\tt lgcd} has a list of integers (or of a list of polynomials) 3137as argument.\\ 3138{\tt lgcd} returns the {\tt gcd} of all integers of the list (or 3139the {\tt gcd} of all polynomials of the list).\\ 3140Input : 3141\begin{center}{\tt lgcd([18,15,21,36])}\end{center} 3142Output : 3143\begin{center}{\tt 3}\end{center} 3144{\bf Remark}\\ 3145{\tt lgcd} does not accept two lists (even if they have the same size) 3146as arguments. 3147 3148\subsection{The least common multiple : {\tt lcm}}\index{lcm|textbf}\label{sec:ilcm} 3149\noindent {\tt lcm} returns the least common multiple of two integers (or of 3150two polynomials, see also \ref{sec:lcm}).\\ 3151Input : 3152\begin{center}{\tt lcm(18,15) }\end{center} 3153Output : 3154\begin{center}{\tt 90}\end{center} 3155 3156\subsection{Decomposition into prime factors : {\tt ifactor}}\index{ifactor} 3157\noindent{\tt ifactor} has an integer as parameter.\\ 3158{\tt ifactor} decomposes an integer into its prime factors.\\ 3159Input : 3160\begin{center}{\tt ifactor(90) }\end{center} 3161Output : 3162\begin{center}{\tt 2*3\verb|^|2*5}\end{center} 3163Input : 3164\begin{center}{\tt ifactor(-90) }\end{center} 3165Output : 3166\begin{center}{\tt (-1)*2*3\verb|^|2*5}\end{center} 3167 3168\subsection{List of prime factors : {\tt ifactors}}\index{ifactors} 3169\noindent{\tt ifactors} has an integer (or a list of integers) as parameter.\\ 3170{\tt ifactors} decomposes the integer (or the integers of the list) into prime 3171factors, but the result 3172 is given as a list (or a list of lists) in which each prime factor is 3173followed by its multiplicity.\\ 3174Input : 3175\begin{center}{\tt ifactors(90) }\end{center} 3176Output : 3177\begin{center}{\tt [2,1,3,2,5,1] }\end{center} 3178Input : 3179\begin{center}{\tt ifactors(-90) }\end{center} 3180Output : 3181\begin{center}{\tt [-1,1,2,1,3,2,5,1] }\end{center} 3182Input : 3183\begin{center}{\tt ifactor([36,52]) }\end{center} 3184Output : 3185\begin{center}{\tt [[2,2,3,2],[2,2,13,1]]}\end{center} 3186\subsection{Matrix of factors : {\tt maple\_ifactors}}\index{maple\_ifactors} 3187\noindent{\tt maple\_ifactors} has an integer $n$ (or a list of integers) 3188as parameter.\\ 3189{\tt maple\_ifactors} decomposes the integer (or the integers of the list) into 3190prime factors, but the output follows the Maple syntax :\\ 3191it is a list with +1 or -1 (for the sign) and a matrix with 2 columns and 3192 where the lines are the prime factors and their multiplicity (or a list of 3193lists...).\\ 3194Input : 3195\begin{center}{\tt maple\_ifactors(90) }\end{center} 3196Output : 3197\begin{center}{\tt [1,[[2,1],[3,2],[5,1]]]}\end{center} 3198Input : 3199\begin{center}{\tt maple\_ifactor([36,52]) }\end{center} 3200Output : 3201\begin{center}{\tt [[1,[[2,2],[3,2]]],[1,[[2,2],[13,1]]]]}\end{center} 3202\subsection{The divisors of a number : {\tt idivis divisors}} \index{idivis}\index{divisors} 3203\noindent{\tt idivis} or {\tt divisors} gives the list of the divisors of a 3204number (or of a list of numbers).\\ 3205Input : 3206\begin{center}{\tt idivis(36) }\end{center} 3207Output : 3208\begin{center}{\tt [1,2,4,3,6,12,9,18,36] }\end{center} 3209Input : 3210\begin{center}{\tt idivis([36,22]) }\end{center} 3211Output : 3212\begin{center}{\tt [[1,2,4,3,6,12,9,18,36],[1,2,11,22]]}\end{center} 3213 3214\subsection{The integer Euclidean quotient : {\tt iquo intDiv}}\index{iquo}\index{intDiv} 3215\noindent{\tt iquo} (or {\tt intDiv}) returns the integer quotient $q$ of the 3216Euclidean division of two integers $a$ and $b$ given as arguments. 3217($a=b*q+r$ with $0\leq r< b$).\\ 3218For Gaussian integers, we choose $q$ so that $b*q$ is as near by $a$ as 3219possible and it can be proved that $r$ may be chosen so that 3220$|r|^2 \leq |b|^2/2$.\\ 3221Input : 3222\begin{center}{\tt iquo(148,5) }\end{center} 3223Output : 3224\begin{center}{\tt 29}\end{center} 3225{\tt iquo} works with integers or with Gaussian integers.\\ 3226Input : 3227\begin{center}{\tt iquo(factorial(148),factorial(145)+2 )}\end{center} 3228Output : 3229\begin{center}{\tt 3176375}\end{center} 3230Input : 3231\begin{center}{\tt iquo(25+12*i,5+7*i) }\end{center} 3232Output : 3233\begin{center}{\tt 3-2*i}\end{center} 3234Here $a-b*q=-4+i$ and $|-4+i|^2=17<|5+7*i|^2/2=74/2=37$ 3235 3236\subsection{The integer Euclidean remainder : {\tt irem remain smod mods mod \%}}\index{irem}\index{remain} 3237\noindent{\tt irem} (or {\tt remain}) returns the integer remainder $r$ from 3238the Euclidean division of two integers $a$ and $b$ given as arguments 3239($a=b*q+r$ with $0\leq r< b$).\\ 3240For Gaussian integers, we choose $q$ so that $b*q$ is as near to $a$ as 3241possible and it can be proved that $r$ may be chosen so that 3242$|r|^2 \leq |b|^2/2$.\\ 3243Input : 3244\begin{center}{\tt irem(148,5) }\end{center} 3245Output : 3246\begin{center}{\tt 3}\end{center} 3247{\tt irem} works with long integers or with Gaussian integers.\\ 3248Example : 3249\begin{center}{\tt irem(factorial(148),factorial(45)+2 )}\end{center} 3250Output : 3251\begin{center}{\tt 111615339728229933018338917803008301992120942047239639312}\end{center} 3252Another example 3253\begin{center}{\tt irem(25+12*i,5+7*i) }\end{center} 3254Output : 3255\begin{center}{\tt -4+i}\end{center} 3256Here $a-b*q=-4+i$ and $|-4+i|^2=17<|5+7*i|^2/2=74/2=37$ 3257 3258{\tt smod} or {\tt mods}\index{smod|textbf}\index{mods|textbf} is a prefixed 3259function and has two integers $a$ and $b$ as arguments.\\ 3260{\tt smod} or {\tt mods} returns the 3261symmetric remainder $s$ of the Euclidean division of the 3262arguments $a$ and $b$ ($a=b*q+s$ with $-b/2<s \leq b/2$).\\ 3263Input : 3264\begin{center}{\tt smod(148,5) }\end{center} 3265Output : 3266\begin{center}{\tt -2}\end{center} 3267 3268{\tt mod} (or {\tt \%}) is an infixed function 3269and has two integers $a$ and $b$ 3270as arguments.\\ 3271{\tt mod} (or {\tt \%}) returns $r\% b$ of $Z/bZ$ where $r$ is the remainder of 3272the Euclidean division of the arguments $a$ and $b$.\\ 3273Input :\index{mod}\index{\%} 3274\begin{center}{\tt 148\ mod\ 5 }\end{center} 3275or : 3276\begin{center}{\tt 148 \% 5 }\end{center} 3277Output : 3278\begin{center}{\tt 3 \% 5}\end{center} 3279Note that the answer {\tt 3 \% 5} is not an integer (3) but 3280an element of $Z/5Z$ (see \ref{sec:modulaire} to have 3281the possible operations in $Z/5Z$). 3282 3283\subsection{Euclidean quotient and euclidean remainder of two integers : {\tt iquorem}}\index{iquorem}\label{sec:iquorem} 3284\noindent{\tt iquorem} returns the list of the quotient $q$ and the 3285remainder $r$ of the Euclidean division between two integers $a$ and $b$ given 3286as arguments ($a=b*q+r$ with $0\leq r< b$).\\ 3287Input : 3288\begin{center}{\tt iquorem(148,5) }\end{center} 3289Output : 3290\begin{center}{\tt [29,3] }\end{center} 3291 3292\subsection{Test of evenness : {\tt even}}\index{even} 3293\noindent {\tt even} takes as argument an integer {\tt n}.\\ 3294{\tt even} returns {\tt 1} if {\tt n} is even and returns {\tt 0} if {\tt n} 3295is odd.\\ 3296Input : 3297\begin{center}{\tt even(148) }\end{center} 3298Output : 3299\begin{center}{\tt 1 }\end{center} 3300Input : 3301\begin{center}{\tt even(149) }\end{center} 3302Output : 3303\begin{center}{\tt 0}\end{center} 3304 3305 3306\subsection{Test of oddness : {\tt odd}}\index{odd} 3307\noindent {\tt odd} takes as argument an integer {\tt n}.\\ 3308{\tt odd} returns {\tt 1} if {\tt n} is odd and returns {\tt 0} if {\tt n} is 3309even.\\ 3310Input : 3311\begin{center}{\tt odd(148) }\end{center} 3312Output : 3313\begin{center}{\tt 0 }\end{center} 3314Input : 3315\begin{center}{\tt odd(149) }\end{center} 3316Output : 3317\begin{center}{\tt 1}\end{center} 3318 3319\subsection{Test of pseudo-primality : {\tt is\_pseudoprime}}\index{is\_pseudoprime} 3320\noindent If {\tt is\_pseudoprime(n)} returns {\tt 2} (true), then 3321{\tt n} is prime.\\ 3322If it returns 1, then {\tt n} is pseudo-prime (most 3323probably prime).\\ 3324 If it returns 0, then {\tt n} is not prime. \\ 3325{\sc Definition}: For numbers less than $10^{14}$, pseudo-prime and prime 3326are equivalent. But for numbers greater than $10^{14}$, a pseudo-prime 3327 is a number with a large probability of being prime (cf. Rabin's Algorithm and 3328Miller-Rabin's Algorithm in the Algorithmic part (menu 3329{\tt Help->Manuals->Programming})).\\ 3330Input : 3331\begin{center}{\tt is\_pseudoprime(100003) }\end{center} 3332Output : 3333\begin{center}{\tt 2}\end{center} 3334Input : 3335\begin{center}{\tt is\_pseudoprime(9856989898997) }\end{center} 3336Output : 3337\begin{center}{\tt 2}\end{center} 3338Input : 3339\begin{center}{\tt is\_pseudoprime(14) }\end{center} 3340Output : 3341\begin{center}{\tt 0}\end{center} 3342Input : 3343\begin{center}{\tt is\_pseudoprime(9856989898997789789) }\end{center} 3344Output : 3345\begin{center}{\tt 1}\end{center} 3346 3347\subsection{Test of primality : {\tt is\_prime isprime isPrime}}\index{is\_prime} 3348\noindent {\tt is\_prime(n)} returns {\tt 1} (true) if {\tt n} is prime and 3349{\tt 0} (false) if {\tt n} is not prime.\\ 3350{\tt isprime} returns {\tt true} or {\tt false}.\\ 3351Use the command {\tt pari("isprime",n,1)} 3352to have a primality certificate (see the documentation 3353 PARI/GP with the menu {\tt Help->Manuals->PARI-GP}) and 3354{\tt pari("isprime",n,2)} to use the APRCL test. 3355 3356Input : 3357\begin{center}{\tt is\_prime(100003)}\end{center} 3358Output : 3359\begin{center}{\tt 1}\end{center} 3360Input : 3361\begin{center}{\tt isprime(100003)}\end{center} 3362Output : 3363\begin{center}{\tt true}\end{center} 3364Input : 3365\begin{center}{\tt is\_prime(98569898989987)}\end{center} 3366Output : 3367\begin{center}{\tt 1}\end{center} 3368Input : 3369\begin{center}{\tt is\_prime(14)}\end{center} 3370Output : 3371\begin{center}{\tt 0}\end{center} 3372Input : 3373\begin{center}{\tt isprime(14)}\end{center} 3374Output : 3375\begin{center}{\tt false}\end{center} 3376Input : 3377\begin{center}{\tt pari("isprime",9856989898997789789,1)}\end{center} 3378This returns the coefficients giving the proof of primality by the 3379$p-1$ Selfridge-Pocklington-Lehmer test~: 3380\begin{center} 3381{\tt [[2,2,1],[19,2,1],[941,2,1],[1873,2,1],[94907,2,1]]} 3382\end{center} 3383Input : 3384\begin{center}{\tt isprime(9856989898997789789)}\end{center} 3385Output : 3386\begin{center}{\tt true}\end{center} 3387 3388\subsection{The smallest pseudo-prime greater than {\tt n} : {\tt nextprime}}\index{nextprime} 3389\noindent{\tt nextprime(n)} returns the smallest pseudo-prime (or prime) 3390greater than {\tt n}. \\ 3391Input : 3392\begin{center}{\tt nextprime(75) }\end{center} 3393Output : 3394\begin{center}{\tt 79}\end{center} 3395 3396\subsection{The greatest pseudo-prime less than {\tt n} : {\tt prevprime}}\index{prevprime} 3397\noindent{\tt prevprime(n)} returns the greatest pseudo-prime (or prime) less 3398than {\tt n}.\\ 3399Input : 3400\begin{center}{\tt prevprime(75)}\end{center} 3401Output : 3402\begin{center}{\tt 73}\end{center} 3403 3404\subsection{The {\tt n}-th prime number : {\tt ithprime}}\index{ithprime} 3405\noindent{\tt ithprime(n)} returns the {\tt n}-th prime number 3406less than 10000 (current limitation).\\ 3407Input : 3408\begin{center}{\tt ithprime(75)}\end{center} 3409Output : 3410\begin{center}{\tt 379}\end{center} 3411Input : 3412\begin{center}{\tt ithprime(1229)}\end{center} 3413Output : 3414\begin{center}{\tt 9973}\end{center} 3415Input : 3416\begin{center}{\tt ithprime(1230)}\end{center} 3417Output : 3418\begin{center}{\tt ithprime(1230)}\end{center} 3419because {\tt ithprime(1230)} is greater than 10000. 3420 3421\subsection{B\'ezout's Identity : {\tt iegcd igcdex}}\index{iegcd}\index{igcdex} 3422\noindent{\tt iegcd(a,b)} or {\tt igcdex(a,b)} 3423returns the coefficients of the B\'ezout's Identity for two integers given 3424as arguments.\\ 3425{\tt iegcd(a,b)} or {\tt igcdex(a,b)} returns {\tt [u,v,d]} such that 3426{\tt au+bv=d} and {\tt d=gcd(a,b)}.\\ 3427Input : 3428\begin{center}{\tt iegcd(48,30) }\end{center} 3429Output : 3430\begin{center}{\tt [2,-3,6]}\end{center} 3431In other words : 3432$$2 \cdot 48+ (-3) \cdot 30 =6$$ 3433 3434\subsection{Solving au+bv=c in $\Z$: {\tt iabcuv}}\index{iabcuv} 3435\noindent{\tt iabcuv(a,b,c)} returns {\tt [u,v]} so that {\tt au+bv=c}.\\ 3436{\tt c} must be a multiple of {\tt gcd(a,b)} for the existence of 3437a solution.\\ 3438Input : 3439\begin{center}{\tt iabcuv(48,30,18) }\end{center} 3440Output : 3441\begin{center}{\tt [6,-9]}\end{center} 3442 3443\subsection{Chinese remainders : {\tt ichinrem, ichrem}}\index{ichinrem}\index{ichrem} 3444\noindent{\tt ichinrem([a,p],[b,q])} or {\tt ichrem([a,p],[b,q])} returns a 3445list {\tt [c,lcm(p,q)]} of 2 integers.\\ 3446The first number {\tt c} is such that 3447\[ \forall k \in \mathbb Z, \quad d=c+ k \times \mbox{lcm}(p,q) \] 3448has the properties 3449\[ d=a \pmod p, \quad d=b \pmod q \] 3450If {\tt p} and {\tt q} are coprime, a solution {\tt d} always exists 3451and all the solutions are congruent modulo {\tt p*q}.\\ 3452{\bf Examples} : \\ 3453Solve : 3454$${\tt \left \{ \begin{array}{rcl} x&=&3\ (\bmod\ 5)\\ 3455x&=&9\ (\bmod\ 13) \end{array}\right.}$$ 3456Input : 3457\begin{center}{\tt ichinrem([3,5],[9,13])}\end{center} 3458or : 3459\begin{center}{\tt ichrem([3,5],[9,13])}\end{center} 3460Output : 3461\begin{center}{\tt [-17,65] }\end{center} 3462so {\tt x=-17 (mod 65)}\\ 3463We can also input : 3464\begin{center}{\tt ichrem(3\%5,9\%13)}\end{center} 3465Output : 3466\begin{center}{\tt -17\%65 }\end{center} 3467Solve : 3468$${\tt \left \{ \begin{array}{rcl} x&=&3\ (\bmod\ 5)\\ 3469x&=&4\ (\bmod\ 7) \\ 3470x&=&1\ (\bmod\ 9)\end{array}\right.}$$ 3471First input : 3472\begin{center}{\tt tmp:=ichinrem([3,5],[4,7])}\end{center} 3473or : 3474\begin{center}{\tt tmp:=ichrem([3,5],[4,7])}\end{center} 3475Output : 3476\begin{center}{\tt [-17,35] }\end{center} 3477Then input : 3478\begin{center}{\tt ichinrem([1,9],tmp)}\end{center} 3479or : 3480\begin{center}{\tt ichrem([1,9],tmp)}\end{center} 3481Output : 3482\begin{center}{\tt [-17,315] }\end{center} 3483hence {\tt x=-17 (mod 315)}\\ 3484Alternative input:\\ 3485\begin{center}{\tt ichinrem([3\%5,4\%7,1\%9])}\end{center} 3486Output : 3487\begin{center}{\tt -17\%315 }\end{center} 3488 3489{\bf Remark}\\ 3490{\tt ichrem} (or{\tt ichinrem})may be used to find the coefficients of a polynomial 3491whose equivalence classes are known modulo several integers, for example find 3492$ax+b$ modulo $315=5 \times 7 \times 9$ under the assumptions: 3493$${\tt \left \{ \begin{array}{rl} a=&3\ (\bmod\ 5)\\ 3494a=&4\ (\bmod\ 7) \\ 3495a=&1\ (\bmod\ 9) \end{array}\right.}, 3496\quad 3497{\tt \left \{ \begin{array}{rl} b=&1\ (\bmod\ 5)\\ 3498b=&2\ (\bmod\ 7) \\ 3499b=&3\ (\bmod\ 9) \end{array}\right.}$$ 3500Input : 3501\begin{center}{\tt ichrem((3x+1)\%5,(4x+2)\%7,(x+3)\%9)}\end{center} 3502Output : 3503\begin{center}{\tt (-17\%315$\times$ x+156\%315 }\end{center} 3504hence {\tt a=-17 (mod 315)} and {\tt b=156 (mod 315)}. 3505 3506\subsection{Chinese remainders for lists of integers : {\tt chrem}}\index{chrem} 3507\noindent{\tt chrem} takes as argument 2 lists of integers of the same size.\\ 3508{\tt chrem} returns a list of 2 integers.\\ 3509For example, {\tt chrem([a,b,c],[p,q,r])} returns the list 3510{\tt [x,lcm(p,q,r)]} where 3511{\tt x=a mod p} and {\tt x=b mod q} and {\tt x=c mod r}.\\ 3512A solution {\tt x} always exists if {\tt p, q, r} 3513are mutually primes, and all the solutions are equal modulo {\tt p*q*r}. \\ 3514{\sc Be careful} with the order of the parameters, indeed :\\ 3515{\tt chrem([a,b],[p,q])=ichrem([a,p],[b,q])=\\ 3516ichinrem([a,p],[b,q])}\\ 3517{\bf Examples} : \\ 3518Solve : 3519$${\tt \left \{ \begin{array}{rl} x=&3\ (\bmod\ 5)\\ 3520x=&9\ (\bmod\ 13) \end{array}\right.}$$ 3521Input : 3522\begin{center}{\tt chrem([3,9],[5,13])}\end{center} 3523Output : 3524\begin{center}{\tt [-17,65] }\end{center} 3525so, {\tt x=-17 (mod 65)}\\ 3526Solve : 3527$${\tt \left \{ \begin{array}{rl} x=&3\ (\bmod\ 5)\\ 3528x=&4\ (\bmod\ 6) \\ 3529x=&1\ (\bmod\ 9)\end{array}\right.}$$ 3530Input : 3531\begin{center}{\tt chrem([3,4,1],[5,6,9])}\end{center} 3532Output : 3533\begin{center}{\tt [28,90] }\end{center} 3534so {\tt x=28 (mod 90)}\\ 3535{\bf Remark}\\ 3536{\tt chrem} may be used to find the coefficients of a polynomial whose 3537equivalence classes are known modulo several integers, for example find 3538$ax+b$ modulo $315=5 \times 7 \times 9$ under the assumptions: 3539$${\tt \left \{ \begin{array}{rl} a=&3\ (\bmod\ 5)\\ 3540a=&4\ (\bmod\ 7) \\ 3541a=&1\ (\bmod\ 9) \end{array}\right.}, \quad 3542{\tt \left \{ \begin{array}{rl} b=&1\ (\bmod\ 5)\\ 3543b=&2\ (\bmod\ 7) \\ 3544b=&3\ (\bmod\ 9) \end{array}\right.}$$ 3545Input : 3546\begin{center}{\tt chrem([3x+1,4x+2,x+3],[5,7,9])}\end{center} 3547Output : 3548\begin{center}{\tt [-17x+156,315] }\end{center} 3549hence, {\tt a=-17 (mod 315)} and {\tt b=156 (mod 315)}. 3550 3551\subsection{Solving $a^2+b^2=p$ in $\Z$ : {\tt pa2b2}}\index{pa2b2} 3552\noindent{\tt pa2b2} decompose a prime integer $p$ congruent to 1 modulo 4, 3553as a sum of squares : $p= a^2+b^2$. 3554The result is the list {\tt [a,b]}.\\ 3555Input : 3556\begin{center}{\tt pa2b2(17)}\end{center} 3557Output : 3558\begin{center}{\tt [4,1] }\end{center} 3559indeed $17=4^2+1^2$ 3560 3561\subsection{The Euler indicatrix : {\tt euler phi}}\index{euler}\index{phi} 3562\noindent{\tt euler} (or {\tt phi}) returns the Euler indicatrix 3563for a integer. \\ 3564{\tt euler(n)} (or {\tt phi(n)}) is equal to the number of integers less 3565than {\tt n} and prime with {\tt n}. \\ 3566Input : 3567\begin{center}{\tt euler(21)}\end{center} 3568Output : 3569\begin{center}{\tt 12}\end{center} 3570In other words 3571 E=\{2,4,5,7,8,10,11,13,15,16,17,19\} is the set of integers less than 21 3572and coprime with 21. There are 12 members in this set, hence Cardinal(E)=12. 3573 3574Euler has introduced this function to generalize the little Fermat theorem:\\ 3575\centerline{If $a$ and $n$ are mutually prime then $a^{euler(n)}=1\ \bmod \ n$} 3576 3577\subsection{Legendre symbol : {\tt legendre\_symbol}}\index{legendre\_symbol} 3578If $n$ is prime, we define the Legendre symbol of $a$ 3579written $\left(\frac{a}{n}\right)$ by :\\ 3580$$\left(\frac{a}{n}\right)=\left\{\begin{array}{rl} 35810 & \mbox{if }a=0\ \bmod n \\ 35821 & \mbox{if } a \neq 0 \bmod n \mbox{ and if } a=b^2 \bmod n\\ 3583-1 & \mbox{if } a \neq 0 \bmod n \mbox{ and if } a \neq b^2 \bmod n\\ 3584\end{array} 3585\right.$$ 3586Some properties 3587\begin{itemize} 3588\item 3589If $n$ is prime : 3590\[ a^{\frac{n-1}{2}}=\left(\frac{a}{n}\right) \bmod n \] 3591\item 3592\begin{eqnarray*} 3593\left(\frac{p}{q}\right).\left(\frac{q}{p}\right) 3594&=&(-1)^{\frac{p-1}{2}}.(-1)^{\frac{q-1}{2}} 3595\mbox{ if $p$ and $q$ are odd and positive} \\ 3596\left(\frac{2}{p}\right)&=&(-1)^{\frac{p^2-1}{8}} \\ 3597\left(\frac{-1}{p}\right)&=&(-1)^{\frac{p-1}{2}} 3598\end{eqnarray*} 3599\end{itemize} 3600{\tt legendre\_symbol} takes two arguments $a$ and $n$ and returns the Legendre 3601symbol $\left(\frac{a}{n}\right)$.\\ 3602Input : 3603\begin{center}{\tt legendre\_symbol(26,17)}\end{center} 3604Output : 3605\begin{center}{\tt 1}\end{center} 3606Input : 3607\begin{center}{\tt legendre\_symbol(27,17)}\end{center} 3608Output : 3609\begin{center}{\tt -1}\end{center} 3610Input : 3611\begin{center}{\tt legendre\_symbol(34,17)}\end{center} 3612Output : 3613\begin{center}{\tt 0}\end{center} 3614 3615\subsection{Jacobi symbol : {\tt jacobi\_symbol}}\index{jacobi\_symbol} 3616If $n$ is not prime, the Jacobi symbol of $a$, 3617denoted as $\left(\frac{a}{n}\right)$, is defined 3618from the Legendre symbol and from the 3619decomposition of $n$ into prime factors. 3620Let 3621\[ n=p_1^{\alpha _1}..p_k^{\alpha _k} \] 3622where $p_j$ is prime and $\alpha _j$ is an integer for $j=1..k$. 3623The Jacobi symbol of $a$ is defined by : 3624\[ \left(\frac{a}{n}\right)=\left(\frac{a}{p_1}\right)^{\alpha _1}...\left(\frac{a}{p_k}\right)^{\alpha _k} \] 3625{\tt jacobi\_symbol} takes two arguments $a$ and $n$, and it returns the Jacobi 3626symbol $\left(\frac{a}{n}\right)$.\\ 3627Input : 3628\begin{center}{\tt jacobi\_symbol(25,12)}\end{center} 3629Output : 3630\begin{center}{\tt 1}\end{center} 3631Input : 3632\begin{center}{\tt jacobi\_symbol(35,12)}\end{center} 3633Output : 3634\begin{center}{\tt -1}\end{center} 3635Input : 3636\begin{center}{\tt jacobi\_symbol(33,12)}\end{center} 3637Output : 3638\begin{center}{\tt 0}\end{center} 3639 3640\section{Combinatorial analysis} 3641\subsection{Factorial : {\tt factorial \ !}}\index{factorial|textbf}\index{\symbol{33}|textbf} 3642\noindent{\tt factorial} (prefix) or {\tt !} (postfix) 3643takes as argument an integer $n$.\\ 3644{\tt factorial(n)} or {\tt n!} returns $n!$.\\ 3645Input : 3646\begin{center}{\tt factorial(10)}\end{center} 3647or 3648\begin{center}{\tt 10!}\end{center} 3649Output : 3650\begin{center}{\tt 3628800}\end{center} 3651 3652\subsection{Binomial coefficients : {\tt binomial comb nCr}}\index{binomial}\index{comb|textbf}\index{nCr|textbf} 3653\noindent{\tt comb} or {\tt nCr} or {\tt binomial} takes as argument two 3654integers {\tt n} and {\tt p}.\\ 3655{\tt comb(n,p)} or {\tt nCr(n,p)} or {\tt binomial(n,p)} returns 3656$\left(^n_p\right) =C_n^p$.\\ 3657Input : 3658\begin{center}{\tt comb(5,2)}\end{center} 3659Output : 3660\begin{center}{\tt 10}\end{center} 3661{\bf Remark}\\ 3662{\tt binomial} (unlike {\tt comb, nCr}) 3663may have a third real argument, 3664in this case {\tt binomial(n,p,a)} returns 3665$\left(^n_p\right) a^p(1-a)^{n-p}$. 3666 3667\subsection{Permutations : {\tt perm nPr}}\index{perm}\index{nPr} 3668\noindent{\tt perm} or {\tt nPr} takes as arguments two integers $n$ and $p$.\\ 3669{\tt perm(n,p)} or {\tt nPr(n,p)} returns $P_n^p$.\\ 3670Input : 3671\begin{center}{\tt perm(5,2)}\end{center} 3672Output : 3673\begin{center}{\tt 20}\end{center} 3674 3675\subsection{Random integers : {\tt rand}}\index{rand} 3676\index{hasard} 3677\noindent{\tt rand} takes as argument an integer $n$ or no argument. 3678\begin{itemize} 3679\item {\tt rand(n)} returns a random integer $p$ such that $0 \leq p<n$.\\ 3680Input : 3681\begin{center}{\tt rand(10)}\end{center} 3682Output for example : 3683\begin{center}{\tt 8}\end{center} 3684 3685\item {\tt rand()} returns a random integer $p$ such that $0 \leq p<2^{31}$ 3686(or on 64 bits architecture $0 \leq p<2^{63}$).\\ 3687Input : 3688\begin{center}{\tt rand()}\end{center} 3689Output for example : 3690\begin{center}{\tt 846930886}\end{center} 3691\end{itemize} 3692 3693\section{Rationals} 3694\subsection{Transform a floating point number into a rational : {\tt exact 3695float2rational}}\index{float2rational|textbf}\index{exact|textbf}\index{evalf} 3696\noindent {\tt float2rational} or {\tt exact} takes as argument a 3697floating point number {\tt d} and returns 3698a rational number {\tt q} close to 3699{\tt d} such that {\tt abs(d-q)<epsilon}. 3700{\tt epsilon} is defined in the {\tt cas} configuration 3701({\tt Cfg} menu) or with the {\tt cas\_setup} command.\\ 3702Input : 3703\begin{center}{\tt float2rational(0.3670520231)}\end{center} 3704Output when {\tt epsilon=1e-10}: 3705\begin{center}{\tt 127/346}\end{center} 3706% Input : 3707% \begin{center}{\tt 123/12+57/21}\end{center} 3708% Output : 3709% \begin{center}{\tt 363/28}\end{center} 3710% Then 3711Input : 3712\begin{center}{\tt evalf(363/28)}\end{center} 3713Output : 3714\begin{center}{\tt 12.9642857143}\end{center} 3715Input : 3716\begin{center}{\tt float2rational(12.9642857143)}\end{center} 3717Output : 3718\begin{center}{\tt 363/28}\end{center} 3719If two representations are mixed, for example : 3720\begin{center}{\tt 1/2+0.7}\end{center} 3721the rational is converted to a float, output : 3722\begin{center}{\tt 1.2}\end{center} 3723Input : 3724\begin{center}{\tt 1/2+float2rational(0.7)}\end{center} 3725Output : 3726\begin{center}{\tt 6/5}\end{center} 3727 3728\subsection{Integer and fractional part : {\tt propfrac propFrac}}\index{propfrac}\index{propFrac}\label{sec:ipropfrac} 3729\noindent{\tt propfrac(A/B)} or {\tt propFrac(A/B)} returns 3730$$q+\frac{r}{b}\ \mbox{ with } \ 0\leq r<b$$ 3731if $\displaystyle \frac{A}{B}=\frac{a}{b}$ with $\mbox{gcd}(a,b)=1$ 3732and $a=bq+r$.\\ 3733For rational fractions, cf. \ref{sec:propfrac}.\\ 3734Input : 3735\begin{center}{\tt propfrac(42/15)}\end{center} 3736Output : 3737\begin{center}{\tt 2+4/5}\end{center} 3738Input : 3739\begin{center}{\tt propfrac(43/12)}\end{center} 3740Output : 3741\begin{center}{\tt 3+7/12}\end{center} 3742 3743\subsection{Numerator of a fraction after simplification : {\tt numer} 3744{\tt getNum}}\index{numer|textbf}\index{getNum|textbf}\label{sec:inumer} 3745\noindent{\tt numer} or {\tt getNum} takes as argument a fraction and returns 3746the numerator of this fraction after simplification (for rational fractions, 3747see \ref{sec:numer}).\\ 3748Input : 3749\begin{center}{\tt numer(42/12)}\end{center} 3750or : 3751\begin{center}{\tt getNum(42/12)}\end{center} 3752Output : 3753\begin{center}{\tt 7}\end{center} 3754To avoid simplifications, the argument must 3755be quoted (for rational fractions see \ref{sec:getnum}).\\ 3756Input : 3757\begin{center}{\tt numer('42/12')}\end{center} 3758or : 3759\begin{center}{\tt getNum('42/12')}\end{center} 3760Output : 3761\begin{center}{\tt 42}\end{center} 3762 3763 3764\subsection{Denominator of a fraction after simplification : {\tt denom getDenom}}\index{denom|textbf}\index{getDenom|textbf}\label{sec:idenom} 3765\noindent{\tt denom} or {\tt getDenom} takes as argument a fraction and 3766returns the denominator of this fraction after simplification (for rational 3767fractions see \ref{sec:denom}).\\ 3768Input : 3769\begin{center}{\tt denom(42/12)}\end{center} 3770or : 3771\begin{center}{\tt getDenom(42/12)}\end{center} 3772Output : 3773\begin{center}{\tt 2}\end{center} 3774To avoid simplifications, the argument must 3775be quoted (for rational fractions see \ref{sec:getdenom}).\\ 3776Input : 3777\begin{center}{\tt denom('42/12')}\end{center} 3778or : 3779\begin{center}{\tt getDenom('42/12')}\end{center} 3780Output : 3781\begin{center}{\tt 12}\end{center} 3782 3783\subsection{Numerator and denominator of a fraction : {\tt f2nd fxnd}}\index{fxnd}\index{f2nd}\label{sec:ifxnd} 3784\noindent{\tt f2nd} (or {\tt fxnd}) takes as argument a fraction and returns 3785the list of the numerator and denominator of this fraction after simplification 3786(for rational fractions see \ref{sec:fxnd}).\\ 3787Input : 3788\begin{center}{\tt f2nd(42/12)}\end{center} 3789Output : 3790\begin{center}{\tt [7,2]}\end{center} 3791 3792\subsection{Simplification of a pair of integers : {\tt simp2}}\index{simp2|textbf}\label{sec:isimp2} 3793\noindent{\tt simp2} takes as argument two integers or a list of two integers 3794 which represent a fraction (for two polynomials see \ref{sec:simp2}).\\ 3795{\tt simp2} returns the list of the numerator and the denominator of 3796an irreducible representation of this fraction 3797(i.e. after simplification).\\ 3798Input : 3799\begin{center}{\tt simp2(18,15) }\end{center} 3800Output : 3801\begin{center}{\tt [6,5]}\end{center} 3802Input : 3803\begin{center}{\tt simp2([42,12])}\end{center} 3804Output : 3805\begin{center}{\tt [7,2]}\end{center} 3806 3807\subsection{Continued fraction representation of a real : {\tt dfc}}\index{dfc}\label{sec:convertdfc}\index{confrac@{\sl confrac}|textbf} 3808\noindent {\tt dfc} takes as argument a real or a rational or a 3809floating point number {\tt a} and an integer {\tt n} 3810(or a real {\tt epsilon}).\\ 3811{\tt dfc} returns the list of the continued fraction representation 3812of {\tt a} of order {\tt n} (or with precision {\tt epsilon} i.e. 3813the continued fraction representation which 3814approximates {\tt a} or {\tt evalf(a)} with precision 3815{\tt epsilon}, by default {\tt epsilon} is the value of the {\tt epsilon} 3816defined in the {\tt cas} configuration with the menu 3817{\tt Cfg$\blacktriangleright$Cas Configuration}).\\ 3818{\tt convert} with the option {\tt confrac} has a similar 3819functionality: in that case 3820the value of {\tt epsilon} is the value of the {\tt epsilon} 3821defined in the {\tt cas} configuration with the menu 3822{\tt Cfg$\blacktriangleright$Cas Configuration} (see 3823\ref{sec:convert}) 3824and the answer may be stored in an optional third argument. 3825 3826{\bf Remarks} 3827\begin{itemize} 3828\item If the last element of the result is a list, the representation is 3829ultimately periodic, and the last element is the period. It means 3830that the real is a root of an equation of order 2 with integer 3831coefficients. 3832\item if the last element of the result is not an integer, it 3833represents a remainder $r$ ($a=a0+1/....+1/an+1/r$). Be aware 3834that this remainder has lost most of its accuracy. 3835\end{itemize} 3836If {\tt dfc(a)=[a0,a1,a2,[b0,b1]]} that means : 3837\[ 3838a=a0+\frac{1}{a1+\frac{1}{a2+\frac{1}{b0+\frac{1}{b1+\frac{1}{b0+...}}}}} 3839\] 3840If {\tt dfc(a)=[a0,a1,a2,r]} that means : 3841\[ a=a0+\frac{1}{a1+\frac{1}{a2+\frac{1}{r}}} \] 3842Input : 3843\begin{center}{\tt dfc(sqrt(2),5)}\end{center} 3844Output : 3845\begin{center}{\tt [1,2,[2]]}\end{center} 3846Input : 3847\begin{center}{\tt dfc(evalf(sqrt(2)),1e-9)}\end{center} 3848or : 3849\begin{center}{\tt dfc(sqrt(2),1e-9)}\end{center} 3850Output : 3851\begin{center}{\tt [1,2,2,2,2,2,2,2,2,2,2,2,2]}\end{center} 3852Input : 3853\begin{center}{\tt convert(sqrt(2),confrac,'dev')}\end{center} 3854Output (if in the {\tt cas} configuration {\tt epsilon=1e-9}) : 3855\begin{center}{\tt [1,2,2,2,2,2,2,2,2,2,2,2,2]}\end{center} 3856and {\tt [1,2,2,2,2,2,2,2,2,2,2,2,2]} is stored in {\tt dev}.\\ 3857Input : 3858\begin{center}{\tt dfc(9976/6961,5)}\end{center} 3859Output : 3860\begin{center}{\tt [1,2,3,4,5,43/7]}\end{center} 3861Input to verify: 3862\begin{center}{\tt 1+1/(2+1/(3+1/(4+1/(5+7/43))))}\end{center} 3863Output : 3864\begin{center}{\tt 9976/6961}\end{center} 3865Input : 3866\begin{center}{\tt convert(9976/6961,confrac,'l')}\end{center} 3867Output (if in the {\tt cas} configuration {\tt epsilon=1e-9}) : 3868\begin{center}{\tt [1,2,3,4,5,6,7]}\end{center} 3869and {\tt [1,2,3,4,5,6,7]} is stored in {\tt l}\\ 3870Input : 3871\begin{center}{\tt dfc(pi,5)}\end{center} 3872Output : 3873\begin{center}{\tt [3,7,15,1,292,(-113*pi+355)/(33102*pi-103993)]}\end{center} 3874Input : 3875\begin{center}{\tt dfc(evalf(pi),5)}\end{center} 3876Output (if floats are hardware floats, e.g. for Digits=12) : 3877\begin{center}{\tt [3,7,15,1,292,1.57581843574]}\end{center} 3878Input : 3879\begin{center}{\tt dfc(evalf(pi),1e-9)}\end{center} 3880or : 3881\begin{center}{\tt dfc(pi,1e-9)}\end{center} 3882or (if in the {\tt cas} configuration {\tt epsilon=1e-9}) : 3883\begin{center}{\tt convert(pi,confrac,'ll')}\end{center} 3884Output : 3885\begin{center}{\tt [3,7,15,1,292]}\end{center} 3886 3887\subsection{Transform a continued fraction representation into a real : {\tt dfc2f}}\index{dfc2f} 3888\noindent {\tt dfc2f} takes as argument a list representing a continued 3889fraction, namely 3890\begin{itemize} 3891\item a list of integers for a rational number 3892\item a list whose last element is a list for an 3893ultimately periodic representation, i.e. 3894a quadratic number, that is a root of a second order equation with 3895integer coefficients. 3896\item or a list with a remainder $r$ as last element 3897 ($a=a0+1/....+1/an+1/r$). 3898\end{itemize} 3899{\tt dfc2f} returns the rational number or the quadratic number with the 3900argument as continued fraction representation.\\ 3901Input : 3902\begin{center}{\tt dfc2f([1,2,[2]])}\end{center} 3903Output : 3904\begin{center}{\tt 1/(1/(1+sqrt(2))+2)+1}\end{center} 3905After simplification with {\tt normal} : 3906\begin{center}{\tt sqrt(2)}\end{center} 3907Input : 3908\begin{center}{\tt dfc2f([1,2,3])}\end{center} 3909Output : 3910\begin{center}{\tt 10/7}\end{center} 3911Input : 3912\begin{center}{\tt normal(dfc2f([3,3,6,[3,6]]))}\end{center} 3913Output : 3914\begin{center}{\tt sqrt(11)}\end{center} 3915Input : 3916\begin{center}{\tt dfc2f([1,2,3,4,5,6,7])}\end{center} 3917Output : 3918\begin{center}{\tt 9976/6961}\end{center} 3919Input to verify : 3920\begin{center}{\tt 1+1/(2+1/(3+1/(4+1/(5+1/(6+1/7)))))}\end{center} 3921Output : 3922\begin{center}{\tt 9976/6961}\end{center} 3923Input : 3924\begin{center}{\tt dfc2f([1,2,3,4,5,43/7])}\end{center} 3925Output : 3926\begin{center}{\tt 9976/6961}\end{center} 3927Input to verify : 3928\begin{center}{\tt 1+1/(2+1/(3+1/(4+1/(5+7/43))))}\end{center} 3929Output : 3930\begin{center}{\tt 9976/6961}\end{center} 3931 3932\subsection{The $n$-th Bernoulli number : {\tt bernoulli}}\index{bernoulli} 3933\noindent {\tt bernoulli} takes as argument an integer $n$.\\ 3934{\tt bernoulli} returns the $n$-th Bernoulli number $B(n)$.\\ 3935The Bernoulli numbers are defined by : 3936\[ \frac{t}{e^t-1}=\sum_{n=0}^{+\infty} \frac{B(n)}{n!}t^n \] 3937Bernoulli polynomials $B_k$ are defined by : 3938\[ B_0=1, \quad B_k{'}(x)=kB_{k-1}(x), \quad \int_0^1B_k(x)dx=0 \] 3939and the relation $B(n)=B_n(0)$ holds.\\ 3940Input : 3941\begin{center}{\tt bernoulli(6)}\end{center} 3942Output : 3943\begin{center}{\tt 1/42}\end{center} 3944 3945\subsection{Access to PARI/GP commands: {\tt pari}}\index{pari} 3946\begin{itemize} 3947\item 3948{\tt pari} with a string as first argument (the PARI command name) 3949execute the corresponding PARI command with the remaining arguments. 3950For example {\tt pari("weber",1+i)} executes the PARI command 3951{\tt weber} with the argument {\tt 1+i}. 3952\item 3953{\tt pari} without argument exports all PARI/GP functions 3954\begin{itemize} 3955\item with the same command name if they are not already defined inside {\tt 3956 Xcas} 3957\item with their original command name with the prefix {\tt pari\_} 3958\end{itemize} 3959For example, after calling {\tt pari()}, {\tt pari\_weber(1+i)} or 3960{\tt weber(1+i)} will execute the PARI command 3961{\tt weber} with the argument {\tt 1+i}. 3962\end{itemize} 3963 3964The documentation of PARI/GP is available with the menu 3965Help->Manuals. 3966 3967\section{Real numbers} 3968\subsection{Eval a real at a given precision : {\tt evalf} and {\tt 3969 Digits}, {\tt DIGITS}}\index{evalf}\index{Digits}\index{DIGITS} 3970\begin{itemize} 3971\item A real number is an exact number and its numeric evaluation at a given 3972precision is a floating number represented in base 2.\\ 3973The precision of a floating number is the number of bits of its 3974mantissa, which is at least 53 (hardware float numbers, also known as {\tt 3975 double}). Floating numbers are displayed in base 10 with a number 3976of digits controlled by the user either by assigning the {\tt Digits} 3977variable or by modifying the Cas configuration. 3978By default {\tt Digits} is equal to 12. 3979The number of digits displayed controls the number of bits of the 3980mantissa, if Digits is less than 15, 53 bits are used, if Digits is 3981strictly greater than 15, the number of bits is a roundoff of 3982Digits times the log of 10 in base 2. 3983\item 3984An expression is coerced into a floating number with the {\tt evalf} 3985command. {\tt evalf} may have an optional second argument which will 3986be used to evaluate with a given precision. 3987\item 3988Note that if an expression contains a floating number, evaluation will try 3989to convert other arguments to floating point numbers in order 3990to coerce the whole expression to a single floating number. 3991\end{itemize} 3992Input : 3993\begin{center}{\tt 1+1/2}\end{center} 3994Output : 3995\begin{center}{\tt 3/2}\end{center} 3996Input : 3997\begin{center}{\tt 1.0+1/2}\end{center} 3998Output : 3999\begin{center}{\tt 1.5}\end{center} 4000Input: 4001\begin{center}{\tt exp(pi*sqrt(20))}\end{center} 4002Output : 4003\begin{center}{\tt exp(pi*2*sqrt(5)) }\end{center} 4004With {\tt evalf}, input : 4005\begin{center}{\tt evalf(exp(pi*2*sqrt(5)))}\end{center} 4006Output : 4007\begin{center}{\tt 1263794.75367}\end{center} 4008Input : 4009\begin{center}{\tt 1.1\verb|^|{20}}\end{center} 4010Output : 4011\begin{center}{\tt 6.72749994933}\end{center} 4012Input : 4013\begin{center}{\tt sqrt(2)\verb|^|21}\end{center} 4014Output : 4015\begin{center}{\tt sqrt(2)*2\verb|^|10}\end{center} 4016Input for a result with 30 digits : 4017\begin{center}{\tt Digits:=30}\end{center} 4018Input for the numeric value of $e^{\pi\sqrt{163}}$: 4019\begin{center}{\tt evalf(exp(pi*sqrt(163)))}\end{center} 4020Output : 4021\begin{center}{\tt 0.262537412640768743999999999985e18}\end{center} 4022Note that {\tt Digits} is now set to 30. If you don't want to change 4023the value of {\tt Digits} you may input 4024\begin{center}{\tt evalf(exp(pi*sqrt(163)),30)}\end{center} 4025 4026\subsection{Usual infixed functions on reals : {\tt +,-,*,/,\^\ }} 4027\index{+,-,*,/,\^\ } 4028\noindent {\tt +,-,*,/,\^\ } are the usual operators to do 4029additions, subtractions, multiplications, divisions and for raising to a 4030power.\\ 4031Input : 4032\begin{center}{\tt 3+2}\end{center} 4033Output : 4034\begin{center}{\tt 5}\end{center} 4035Input : 4036\begin{center}{\tt 3-2}\end{center} 4037Output : 4038\begin{center}{\tt 1}\end{center} 4039Input : 4040\begin{center}{\tt 3*2}\end{center} 4041Output : 4042\begin{center}{\tt 6}\end{center} 4043Input : 4044\begin{center}{\tt 3/2}\end{center} 4045Output : 4046\begin{center}{\tt 3/2}\end{center} 4047Input : 4048\begin{center}{\tt 3.2/2.1}\end{center} 4049Output : 4050\begin{center}{\tt 1.52380952381}\end{center} 4051Input : 4052\begin{center}{\tt 3\verb|^|2}\end{center} 4053Output : 4054\begin{center}{\tt 9}\end{center} 4055Input : 4056\begin{center}{\tt 3.2\verb|^|2.1}\end{center} 4057Output : 4058\begin{center}{\tt 11.5031015682}\end{center} 4059 4060{\bf Remark}\\ 4061You may use the square key or the cube key if your keyboard has one, 4062for example : ${\tt 3^2}$ returns 9. 4063 4064{\bf Remark on non integral powers } 4065\begin{itemize} 4066\item If $x$ is not an integer, then $a^x=\exp(x \* \ln(a))$, hence 4067$a^x$ is well-defined only for $a>0$ if $x$ is not rational. If $x$ 4068is rational and $a<0$, the principal determination of the logarithm 4069is used, leading to a complex number. 4070\item Hence be aware of the difference between $\sqrt[n]{a}$ and $a^{\frac{1}{n}}$ 4071when $n$ is an odd integer.\\ 4072For example, to draw the graph of $y=\sqrt[3]{x^3-x^2}$, input : 4073\begin{center} 4074{\tt plotfunc(ifte(x>0,(x\verb|^|3-x\verb|^|2)\verb|^|(1/3),\\ 4075-(x\verb|^|2-x\verb|^|3)\verb|^|(1/3)),x,xstep=0.01)} 4076\end{center} 4077You might also input : 4078\begin{center}{\tt plotimplicit(y\verb|^|3=x\verb|^|3-x\verb|^|2)}\end{center} 4079but this is much slower and much less accurate. 4080\end{itemize} 4081 4082\subsection{Usual prefixed functions on reals : {\tt rdiv}}\index{rdiv} 4083{\tt rdiv} is the prefixed form of the division function.\\ 4084Input : 4085\begin{center}{\tt rdiv(3,2)}\end{center} 4086Output : 4087\begin{center}{\tt 3/2}\end{center} 4088Input : 4089\begin{center}{\tt rdiv(3.2,2.1)}\end{center} 4090Output : 4091\begin{center}{\tt 1.52380952381}\end{center} 4092 4093\subsection{$n$-th root : {\tt root}}\index{root} 4094\noindent{\tt root} takes two arguments : an integer $n$ and a number $a$.\\ 4095{\tt root} returns the $n$-th root of $a$ (i.e. $a^{1/n}$). 4096If $a<0$, the $n$-th root is a complex number of argument $2\pi/n$.\\ 4097Input : 4098\begin{center}{\tt root(3,2)}\end{center} 4099Output : 4100\begin{center}{\tt 2\verb|^|(1/3)}\end{center} 4101\noindent Input : 4102\begin{center}{\tt root(3,2.0)}\end{center} 4103Output : 4104\begin{center}{\tt 1.259921049892}\end{center} 4105Input : 4106\begin{center}{\tt root(3,sqrt(2))}\end{center} 4107Output : 4108\begin{center}{\tt 2\verb|^|(1/6)}\end{center} 4109 4110\subsection{Error function : {\tt erf}}\index{erf} 4111\noindent{\tt erf} takes as argument a number $a$.\\ 4112{\tt erf} returns the floating point value of the error function at $x=a$, 4113where the error function is defined by~: 4114\[ \mbox{erf}(x)=\frac{2}{\sqrt{\pi}}\int_0^{x}e^{-t^2}dt \] 4115The normalization is chosen so that: 4116\[ \mbox{erf}(+\infty)=1, \quad \mbox{erf}(-\infty)=-1 \] 4117since : 4118\[ \int_0^{+\infty}e^{-t^2}dt=\frac{\sqrt{\pi}}{2} \] 4119Input : 4120\begin{center}{\tt erf(1)}\end{center} 4121Output : 4122\begin{center}{\tt 0.84270079295}\end{center} 4123Input : 4124\begin{center}{\tt erf(1/(sqrt(2)))*1/2+0.5}\end{center} 4125Output : 4126\begin{center}{\tt 0.841344746069}\end{center} 4127{\bf Remark}\\ 4128The relation between {\tt erf} and {\tt normal\_cdf} is :\\ 4129\[ \mbox{\tt normal\_cdf}(x)=\frac{1}{2}+\frac{1}{2}\*\mbox{\tt 4130 erf}(\frac{x}{\sqrt{2}}) \] 4131Indeed, making the change of variable $t=u*\sqrt{2}$ in 4132\[ \mbox{normal\_cdf}(x)=\frac{1}{2}+\frac{1}{\sqrt{2\pi}}\int_0^{x}e^{-t^2/2}dt\] 4133gives~: 4134\[ \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}})\] 4135Check :\\ 4136{\tt normal\_cdf(1)=0.841344746069} 4137 4138\subsection{Complementary error function: {\tt erfc}}\index{erfc} 4139\noindent{\tt erfc} takes as argument a number $a$.\\ 4140{\tt erfc} returns the value of the complementary error function at 4141$x=a$, this function is defined by~: 4142\[ 4143\mbox{erfc}(x)=\frac{2}{\sqrt{\pi}}\int_x^{+\infty}e^{-t^2}dt=1-\mbox{erf}(x) 4144\] 4145Hence erfc$(0)=1$, since~: 4146\[ \int_0^{+\infty}e^{-t^2}dt=\frac{\sqrt{\pi}}{2} \] 4147Input : 4148\begin{center}{\tt erfc(1)}\end{center} 4149Output : 4150\begin{center}{\tt 0.15729920705}\end{center} 4151Input : 4152\begin{center}{\tt 1- erfc(1/(sqrt(2)))*1/2}\end{center} 4153Output : 4154\begin{center}{\tt 0.841344746069}\end{center} 4155{\bf Remark}\\ 4156The relation between {\tt erfc} and {\tt normal\_cdf} is : 4157\[ \mbox{\tt normal\_cdf}(x)=1-\frac{1}{2}\*\mbox{\tt erfc} 4158(\frac{x}{\sqrt{2}}) \] 4159Check :\\ 4160{\tt normal\_cdf(1)=0.841344746069} 4161 4162\subsection{The $\Gamma$ function : {\tt Gamma}}\index{Gamma} 4163\noindent{\tt Gamma} takes as argument a number $a$.\\ 4164{\tt Gamma} returns the value of the $\Gamma$ function in $a$, defined by~: 4165\[ \Gamma(x)=\int_0^{+\infty}e^{-t}t^{x-1}dt, \mbox{ if } x>0 \] 4166If $x$ is a positive integer, $\Gamma$ is computed by applying 4167the recurrence~: 4168\[ \Gamma(x+1)=x*\Gamma(x), \quad \Gamma(1)=1 \] 4169Hence~: 4170\[ \Gamma(n+1)=n! \] 4171Input : 4172\begin{center}{\tt Gamma(5)}\end{center} 4173Output : 4174\begin{center}{\tt 24}\end{center} 4175% Input : 4176% \begin{center}{\tt Gamma(1/2)}\end{center} 4177% Output : 4178% \begin{center}{\tt sqrt(pi)}\end{center} 4179Input : 4180\begin{center}{\tt Gamma(0.7)}\end{center} 4181Output : 4182\begin{center}{\tt 1.29805533265}\end{center} 4183Input : 4184\begin{center}{\tt Gamma(-0.3)}\end{center} 4185Output : 4186\begin{center}{\tt -4.32685110883}\end{center} 4187Indeed : {\tt Gamma(0.7)=-0.3*Gamma(-0.3)}\\ 4188Input : 4189\begin{center}{\tt Gamma(-1.3)}\end{center} 4190Output : 4191\begin{center}{\tt 3.32834700679}\end{center} 4192Indeed {\tt Gamma(0.7)=-0.3*Gamma(-0.3)=(-0.3)*(-1.3)*Gamma(-1.3)} 4193 4194\subsection{The $\beta$ function : {\tt Beta}}\index{Beta} 4195\noindent{\tt Beta} takes as argument two reals $a,b$.\\ 4196{\tt Beta} returns the value of the $\beta$ function at $a,b \in 4197\mathbb R$, defined by~: 4198\[ \beta(x,y)=\int_0^1 t^{x-1} (1-t)^{y-1} 4199=\frac{\Gamma(x)*\Gamma(y)}{\Gamma(x+y)} \] 4200Remarkable values~: 4201\[ \beta(1,1)=1, \quad \beta(n,1)=\frac{1}{n}, \quad 4202\beta(n,2)=\frac{1}{n(n+1)} \] 4203{\tt Beta(x,y)} is defined for $x$ and $y$ positive reals 4204(to ensure the convergence of the integral) and by 4205prolongation for $x$ and $y$ if they are not negative integers.\\ 4206Input : 4207\begin{center}{\tt Beta(5,2)}\end{center} 4208Output : 4209\begin{center}{\tt 1/30}\end{center} 4210Input : 4211\begin{center}{\tt Beta(x,y)}\end{center} 4212Output : 4213\begin{center}{\tt Gamma(x)*Gamma(y)/Gamma(x+y)}\end{center} 4214Input : 4215\begin{center}{\tt Beta(5.1,2.2)}\end{center} 4216Output : 4217\begin{center}{\tt 0.0242053671402}\end{center} 4218 4219\subsection{Derivatives of the DiGamma function : {\tt Psi}}\index{Psi} 4220\noindent{\tt Psi} takes as arguments a real $a$ and an integer $n$ (by 4221default $n=0$).\\ 4222{\tt Psi} returns the value of the $n$-th derivative of the DiGamma function 4223at $x=a$, where the DiGamma function is the first derivative 4224of $\ln(\Gamma(x))$. This function is used to evaluated sums of 4225rational functions having poles at integers.\\ 4226Input : 4227\begin{center}{\tt Psi(3,1)}\end{center} 4228Output : 4229\begin{center}{\tt pi\verb|^|2/6-5/4}\end{center} 4230 4231If {\tt n=0}, you may use {\tt Psi(a)} instead of {\tt Psi(a,0)} 4232to compute the value of the DiGamma function at $x=a$.\\ 4233Input : 4234\begin{center}{\tt Psi(3)}\end{center} 4235Output : 4236\begin{center}{\tt Psi(1)+3/2}\end{center} 4237Input : 4238\begin{center}{\tt evalf(Psi(3))}\end{center} 4239Output : 4240\begin{center}{\tt .922784335098}\end{center} 4241 4242\subsection{The $\zeta$ function : {\tt Zeta}}\index{zeta} 4243\noindent{\tt Zeta} takes as argument a real $x$.\\ 4244{\tt Zeta} returns for $x>1$ : 4245\[ \zeta(x)= \sum_{n=1}^{+\infty} \frac{1}{n^x} \] 4246and for $x<1$ its meromorphic continuation.\\ 4247Input : 4248\begin{center}{\tt Zeta(2)}\end{center} 4249Output : 4250\begin{center}{\tt pi\verb|^|2/6}\end{center} 4251Input : 4252\begin{center}{\tt Zeta(4)}\end{center} 4253Output : 4254\begin{center}{\tt pi\verb|^|4/90}\end{center} 4255 4256\subsection{Airy functions : {\tt Airy\_Ai} and {\tt Airy\_Bi}}\index{Airy\_Ai}\index{Airy\_Bi} 4257\noindent{\tt Airy\_Ai} and {\tt Airy\_Bi} take as arguments a real $x$.\\ 4258{\tt Airy\_Ai} and {\tt Airy\_Bi} are two independent solutions 4259of the equation 4260\[ y^{\prime\prime}-x*y=0 \] 4261They are defined by~: 4262\begin{eqnarray*} 4263\mbox{Airy\_Ai}(x) &=& (1/\pi) \int_0^\infty \cos(t^3/3 + x*t) dt \\ 4264\mbox{Airy\_Bi}(x) &=& (1/\pi) \int_0^\infty (e^{- t^3/3} + \sin( t^3/3 + 4265x*t)) dt 4266\end{eqnarray*} 4267Properties :\\ 4268\begin{eqnarray*} 4269 \tt \mbox{Airy\_Ai}(x)&=&\mbox{Airy\_Ai}(0)*f(x)+ 4270\mbox{Airy\_Ai}^\prime (0)*g(x) \\ 4271\tt \mbox{Airy\_Bi}(x)&=&\sqrt{3}(\mbox{Airy\_Ai}(0)*f(x) 4272-\mbox{Airy\_Ai}^\prime (0)*g(x) ) 4273\end{eqnarray*} 4274where $f$ and $g$ are two entire series solutions of 4275\[ w^{\prime\prime}-x*w=0 \] 4276more precisely~: 4277\begin{eqnarray*} 4278f(x)&=&\sum_{k=0}^\infty 3^k\left (\frac{\Gamma(k+\frac{1}{3})}{\Gamma(\frac{1}{3})}\right ) \frac{x^{3k}}{(3k)!}\\ 4279g(x)&=&\sum_{k=0}^\infty 3^k\left 4280 (\frac{\Gamma(k+\frac{2}{3})}{\Gamma(\frac{2}{3})}\right ) 4281\frac{x^{3k+1}}{(3k+1)!} 4282\end{eqnarray*} 4283Input : 4284\begin{center}{\tt Airy\_Ai(1)}\end{center} 4285Output : 4286\begin{center}{\tt 0.135292416313}\end{center} 4287Input : 4288\begin{center}{\tt Airy\_Bi(1)}\end{center} 4289Output : 4290\begin{center}{\tt 1.20742359495}\end{center} 4291Input : 4292\begin{center}{\tt Airy\_Ai(0)}\end{center} 4293Output : 4294\begin{center}{\tt 0.355028053888}\end{center} 4295Input : 4296\begin{center}{\tt Airy\_Bi(0)}\end{center} 4297Output : 4298\begin{center}{\tt 0.614926627446}\end{center} 4299 4300\section{Permutations} 4301A permutation $p$ of size $n$ is a bijection from $[0..n-1]$ on 4302$[0..n-1]$ and is represented by the list : 4303 $[p(0),p(1),p(2)...p(n-1)]$.\\ 4304For example, the permutation $p$ represented by $[1,3,2,0]$ is 4305the application from $[0,1,2,3]$ on $[0,1,2,3]$ defined by : 4306\[ p(0)=1,\ p(1)=3,\ p(2)=2,\ p(3)=0 \] 4307A cycle $c$ of size $p$ is represented by the list 4308$[a_0,...,a_{p-1}]$ ($0\leq a_k\leq n-1$) it is the permutation such that 4309\[ c(a_i)=a_{i+1} \mbox{ for }(i=0..p-2), \quad 4310c(a_{p-1})=a_0, \quad 4311 c(k)=k \mbox{ otherwise }\] 4312A cycle $c$ is represented by a list and a cycle decomposition 4313is represented by a list of lists.\\ 4314For example, the cycle $c$ represented by the list $[3,2,1]$ is the 4315permutation $c$ defined by $c(3)=2,\ c(2)=1,\ c(1)=3,\ c(0)=0$ (i.e. the 4316permutation represented by the list $[0,3,1,2]$). 4317 4318\subsection{Random permutation : {\tt randperm}}\index{randperm} 4319\noindent{\tt randperm} takes as argument an integer $n$.\\ 4320{\tt randperm} returns a random permutation of $[0..n-1]$.\\ 4321Input : 4322\begin{center}{\tt randperm(3)}\end{center} 4323Output : 4324\begin{center}{\tt [2,0,1]}\end{center} 4325 4326\subsection{Decomposition as a product of disjoint cycles : 4327{\tt permu2cycles}}\index{permu2cycles} 4328\noindent {\tt permu2cycles} takes as argument a permutation.\\ 4329{\tt permu2cycles} returns its decomposition as a product of 4330disjoint cycles.\\ 4331Input : 4332\begin{center}{\tt permu2cycles([1,3,4,5,2,0])}\end{center} 4333Output : 4334\begin{center}{\tt [[0,1,3,5],[2,4]]}\end{center} 4335In the answer the cycles of size 1 are omitted, except if $n-1$ is a 4336fixed point of the permutation (this is required to find the value of 4337$n$ from the cycle decomposition).\\ 4338Input : 4339\begin{center}{\tt permu2cycles([0,1,2,4,3,5])}\end{center} 4340Output : 4341\begin{center}{\tt [[5],[3,4]]}\end{center} 4342Input : 4343\begin{center}{\tt permu2cycles([0,1,2,3,5,4])}\end{center} 4344Output : 4345\begin{center}{\tt [[4,5]]}\end{center} 4346 4347\subsection{Product of disjoint cycles to permutation: {\tt cycles2permu}}\index{cycles2permu} 4348\noindent{\tt cycles2permu} takes as argument a list of cycles.\\ 4349{\tt cycles2permu} returns the permutation (of size $n$ chosen as small as 4350possible) that is the product of the given cycles 4351(it is the inverse of {\tt permu2cycles}).\\ 4352Input : 4353\begin{center}{\tt cycles2permu([[1,3,5],[2,4]])}\end{center} 4354Output : 4355\begin{center}{\tt [0,3,4,5,2,1]}\end{center} 4356Input : 4357\begin{center}{\tt cycles2permu([[2,4]])}\end{center} 4358Output : 4359\begin{center}{\tt [0,1,4,3,2]}\end{center} 4360Input : 4361\begin{center}{\tt cycles2permu([[5],[2,4]])}\end{center} 4362Output : 4363\begin{center}{\tt [0,1,4,3,2,5]}\end{center} 4364 4365\subsection{Transform a cycle into permutation : {\tt cycle2perm}}\index{cycle2perm} 4366\noindent{\tt cycle2perm} takes on cycle as argument.\\ 4367{\tt cycle2perm} returns the permutation of size $n$ corresponding 4368to the cycle given as argument, where $n$ is chosen as small 4369as possible (see also {\tt permu2cycles} and {\tt cycles2permu}).\\ 4370Input : 4371\begin{center}{\tt cycle2perm([1,3,5])}\end{center} 4372Output : 4373\begin{center}{\tt [0,3,2,5,4,1]}\end{center} 4374 4375\subsection{Transform a permutation into a matrix : {\tt permu2mat}}\index{permu2mat} 4376\noindent{\tt permu2mat} takes as argument a permutation $p$ of size $n$.\\ 4377{\tt permu2mat} returns the matrix of the permutation, that is 4378the matrix obtained by permuting the rows of the identity matrix of size $n$ 4379with the permutation $p$.\\ 4380Input : 4381\begin{center}{\tt permu2mat([2,0,1])}\end{center} 4382Output : 4383\begin{center}{\tt [[0,0,1],[1,0,0],[0,1,0]]}\end{center} 4384 4385\subsection{Checking for a permutation : {\tt is\_permu}}\index{is\_permu} 4386\noindent{\tt is\_permu} is a boolean function.\\ 4387{\tt is\_permu} takes as argument a list.\\ 4388{\tt is\_permu} returns 1 if the argument is a permutation and returns 0 if the 4389argument is not a permutation.\\ 4390Input : 4391\begin{center}{\tt is\_permu([2,1,3]) }\end{center} 4392Output : 4393\begin{center}{\tt 0}\end{center} 4394Input : 4395\begin{center}{\tt is\_permu([2,1,3,0]) }\end{center} 4396Output : 4397\begin{center}{\tt 1}\end{center} 4398 4399\subsection{Checking for a cycle : {\tt is\_cycle}}\index{is\_cycle} 4400\noindent{\tt is\_cycle} is a boolean function.\\ 4401{\tt is\_cycle} takes a list as argument.\\ 4402{\tt is\_cycle} returns 1 if the argument is a cycle and returns 0 if the 4403argument is not a cycle.\\ 4404Input : 4405\begin{center}{\tt is\_cycle([2,1,3]) }\end{center} 4406Output : 4407\begin{center}{\tt 1}\end{center} 4408Input : 4409\begin{center}{\tt is\_cycle([2,1,3,2]) }\end{center} 4410Output : 4411\begin{center}{\tt 0}\end{center} 4412 4413\subsection{Product of two permutations : {\tt p1op2}}\index{p1op2} 4414\noindent{\tt p1op2} takes as arguments two permutations.\\ 4415{\tt p1op2} returns the permutation obtained by composition : 4416\[ 1^{\mbox{st}}\mbox{arg} \circ 2^{\mbox{nd}} \mbox{arg} \] 4417Input : 4418\begin{center}{\tt p1op2([3,4,5,2,0,1],[2,0,1,4,3,5])}\end{center} 4419Output : 4420\begin{center}{\tt [5,3,4,0,2,1]}\end{center} 4421{\bf Warning}\\ 4422Composition is done using the standard mathematical notation, 4423that is the permutation given as the second argument is performed first. 4424 4425\subsection{Composition of a cycle and a permutation : {\tt c1op2}}\index{c1op2} 4426\noindent{\tt c1op2} takes as arguments a cycle and a permutation.\\ 4427{\tt c1op2} returns the permutation obtained by composition : 4428\[ 1^{\mbox{st}}\mbox{arg} \circ 2^{\mbox{nd}} \mbox{arg} \] 4429Input : 4430\begin{center}{\tt c1op2([3,4,5],[2,0,1,4,3,5])}\end{center} 4431Output : 4432\begin{center}{\tt [2,0,1,5,4,3]}\end{center} 4433{\bf Warning}\\ 4434Composition is done using the standard mathematical notation, 4435that is the permutation given as the second argument is performed first. 4436 4437\subsection{Composition of a permutation and a cycle : {\tt p1oc2}}\index{p1oc2} 4438\noindent{\tt p1oc2} takes as arguments a permutation and a cycle.\\ 4439{\tt p1oc2} returns the permutation obtained by composition : 4440\[ 1^{\mbox{st}}\mbox{arg} \circ 2^{\mbox{nd}} \mbox{arg} \] 4441Input : 4442\begin{center}{\tt p1oc2([3,4,5,2,0,1],[2,0,1])}\end{center} 4443Output : 4444\begin{center}{\tt [4,5,3,2,0,1]}\end{center} 4445{\bf Warning}\\ 4446Composition is done using the standard mathematical notation, 4447that is the cycle given as second argument is performed first. 4448 4449\subsection{Product of two cycles : {\tt c1oc2}}\index{c1oc2} 4450\noindent {\tt c1oc2} takes as arguments two cycles.\\ 4451{\tt c1oc2} returns the permutation obtained by composition : 4452\[ 1^{\mbox{st}}\mbox{arg} \circ 2^{\mbox{nd}} \mbox{arg} \] 4453Input : 4454\begin{center}{\tt c1oc2([3,4,5],[2,0,1])}\end{center} 4455Output : 4456\begin{center}{\tt [1,2,0,4,5,3]}\end{center} 4457{\bf Warning}\\ 4458Composition is done using the standard mathematical notation, 4459that is the cycle given as second argument is performed first. 4460 4461\subsection{Signature of a permutation : {\tt signature}}\index{signature} 4462\noindent{\tt signature} takes as argument a permutation.\\ 4463{\tt signature} returns the signature of the permutation given as argument.\\ 4464The signature of a permutation is equal to : 4465\begin{itemize} 4466\item 1 if the permutation is equal to an even product of transpositions, 4467\item -1 if the permutation is equal to an odd product of transpositions. 4468\end{itemize} 4469The signature of a cycle of size $k$ is : $(-1)^{k+1}$.\\ 4470 Input : 4471\begin{center}{\tt signature([3,4,5,2,0,1])}\end{center} 4472Output : 4473\begin{center}{\tt -1}\end{center} 4474Indeed {\tt permu2cycles([3,4,5,2,0,1])=[[0,3,2,5,1,4]]}. 4475 4476\subsection{Inverse of a permutation : {\tt perminv}}\index{perminv} 4477\noindent{\tt perminv} takes as argument a permutation.\\ 4478{\tt perminv} returns the permutation that is the inverse of the permutation 4479given as argument.\\ 4480Input : 4481\begin{center}{\tt perminv([1,2,0])}\end{center} 4482Output 4483\begin{center}{\tt [2,0,1]}\end{center} 4484 4485\subsection{Inverse of a cycle : {\tt cycleinv}}\index{cycleinv} 4486\noindent{\tt cycleinv} takes as argument a cycle.\\ 4487{\tt cycleinv} returns the cycle that is the inverse of the cycle given as 4488argument.\\ 4489Input : 4490\begin{center}{\tt cycleinv([2,0,1])}\end{center} 4491Output 4492\begin{center}{\tt [1,0,2]}\end{center} 4493 4494\subsection{Order of a permutation : {\tt permuorder}}\index{permuorder} 4495\noindent{\tt permuorder} takes as argument a permutation.\\ 4496{\tt permuorder} returns the order $k$ of the permutation $p$ given as 4497argument, that is the smallest integer $m$ such that $p^m$ is the identity.\\ 4498Input : 4499\begin{center}{\tt permuorder([0,2,1])}\end{center} 4500Output 4501\begin{center}{\tt 2}\end{center} 4502Input : 4503\begin{center}{\tt permuorder([3,2,1,4,0])}\end{center} 4504Output 4505\begin{center}{\tt 6}\end{center} 4506 4507\subsection{Group generated by two permutations : {\tt groupermu}}\index{groupermu} 4508\noindent{\tt groupermu} takes as argument two permutations {\tt a} and 4509{\tt b}.\\ 4510{\tt groupermu} returns the group of the permutations generated by {\tt a} and 4511{\tt b}.\\ 4512Input : 4513\begin{center}{\tt groupermu([0,2,1,3],[3,1,2,0])}\end{center} 4514Output 4515\begin{center}{\tt [[0,2,1,3],[3,1,2,0],[0,1,2,3],[3,2,1,0]]}\end{center} 4516 4517\section{Complex numbers} 4518Note that complex numbers are also used to represent a point in the 4519plane or a 1-d function graph. 4520 4521\subsection{Usual complex functions : {\tt +,-,*,/,\^\ }}\index{+}\index{'+'}\index{-}\index{'-'}\index{\^\ } 4522\noindent {\tt +,-,*,/,\^\ } are the usual operators to perform 4523additions, subtractions, multiplications, divisions and for raising to an 4524integer or a fractional power.\\ 4525Input : 4526\begin{center}{\tt (1+2*i)\verb|^|2}\end{center} 4527Output : 4528\begin{center}{\tt -3+4*i}\end{center} 4529 4530\subsection{Real part of a complex number : {\tt re real}}\index{re}\index{real} 4531\noindent{\tt re} (or {\tt real}) takes as argument a complex number (resp. a 4532point $A$).\\ 4533{\tt re} (or {\tt real}) returns the real part of this complex number (resp. 4534the projection on the $x$ axis of $A$).\\ 4535Input : 4536\begin{center}{\tt re(3+4*i)}\end{center} 4537Output : 4538\begin{center}{\tt 3}\end{center} 4539 4540\subsection{Imaginary part of a complex number : {\tt im imag}}\index{im}\index{imag} 4541\noindent{\tt im} (or {\tt imag}) takes as argument a complex number (resp. a 4542point $A$).\\ 4543{\tt im} (or {\tt imag}) returns imaginary part of this complex number (resp. 4544the projection on the $y$ axis of $A$).\\ 4545Input : 4546\begin{center}{\tt im(3+4*i)}\end{center} 4547Output : 4548\begin{center}{\tt 4}\end{center} 4549 4550\subsection{Write a complex as {\tt re(z)+i*im(z)} : {\tt evalc}}\index{evalc} 4551\noindent{\tt evalc} takes as argument a complex number {\tt z}.\\ 4552{\tt evalc} returns this complex number, written as 4553{\tt re(z)+i*im(z)}.\\ 4554Input : 4555\begin{center}{\tt evalc(sqrt(2)*exp(i*pi/4))}\end{center} 4556Output : 4557\begin{center}{\tt 1+i}\end{center} 4558 4559\subsection{Modulus of a complex number : {\tt abs}}\index{abs} 4560\noindent{\tt abs} takes as argument a complex number.\\ 4561{\tt abs} returns the modulus of this complex number.\\ 4562Input : 4563\begin{center}{\tt abs(3+4*i)}\end{center} 4564Output : 4565\begin{center}{\tt 5}\end{center} 4566 4567\subsection{Argument of a complex number : {\tt arg}}\index{arg|textbf} 4568\noindent{\tt arg} takes as argument a complex number.\\ 4569{\tt arg} returns the argument of this complex number.\\ 4570Input : 4571\begin{center}{\tt arg(3+4*i)}\end{center} 4572Output : 4573\begin{center}{\tt atan(4/3)}\end{center} 4574 4575\subsection{The normalized complex number : {\tt normalize unitV}}\index{unitV}\index{normalize} 4576\noindent{\tt normalize} or {\tt unitV} takes as argument a complex number.\\ 4577{\tt normalize} or {\tt unitV} returns the complex number divided by the 4578modulus of this complex number.\\ 4579Input : 4580\begin{center}{\tt normalize(3+4*i)}\end{center} 4581Output : 4582\begin{center}{\tt (3+4*i)/5}\end{center} 4583 4584\subsection{Conjugate of a complex number : {\tt conj}}\index{conj|textbf} 4585\noindent{\tt conj} takes as argument a complex number.\\ 4586{\tt conj} returns the complex conjugate of this complex number.\\ 4587Input : 4588\begin{center}{\tt conj(3+4*i)}\end{center} 4589Output : 4590\begin{center}{\tt 3-4*i}\end{center} 4591 4592\subsection{Multiplication by the complex conjugate : 4593 {\tt mult\_c\_conjugate}}\index{mult\_c\_conjugate} 4594\noindent {\tt mult\_c\_conjugate} takes as argument an complex expression.\\ 4595If this expression has a complex denominator, 4596{\tt mult\_c\_conjugate} multiplies the numerator and the denominator of this 4597 expression by the complex conjugate of the denominator.\\ 4598If this expression does not have a complex denominator, 4599{\tt mult\_c\_conjugate} multiplies the numerator and the denominator of this 4600expression by the complex conjugate of the numerator.\\ 4601Input : 4602\begin{center}{\tt mult\_c\_conjugate((2+i)/(2+3*i))}\end{center} 4603Output : 4604\begin{center}{\tt (2+i)*(2+3*(-i))/((2+3*(i))*(2+3*(-i)))}\end{center} 4605Input : 4606\begin{center}{\tt mult\_c\_conjugate((2+i)/2)}\end{center} 4607Output : 4608\begin{center}{\tt (2+i)*(2+-i)/(2*(2+-i))}\end{center} 4609 4610\subsection{Barycenter of complex numbers : {\tt barycentre}}\index{barycentre}\label{sec:baryc} 4611%{\bf See also :} \ref{sec:barycentre2} and \ref{sec:barycentre3}.\\ 4612\noindent{\tt barycentre} takes as argument two lists of the same size 4613(resp. a matrix with two columns): 4614\begin{itemize} 4615\item the elements of the first list (resp. column) 4616are points $A_j$ or complex numbers $a_j$ (the affixes of the points), 4617\item the elements of the second list (resp. column) are real coefficients 4618$\alpha_j$ such that $\sum \alpha_j \neq 0$. 4619\end{itemize} 4620{\tt barycentre} returns the barycenter point of the points $A_j$ 4621weighted by the real coefficients $\alpha_j$. 4622If $\sum \alpha_j = 0$, {\tt barycentre} returns an 4623error.\\ 4624{\bf Warning} To have a complex number in the output, the input must be :\\ 4625{\tt affix(barycentre(...,...))} 4626because {\tt barycentre(...,...)} returns a point, not a complex number.\\ 4627Input : 4628\begin{center}{\tt affix(barycentre([1+i,1-i],[1,1]))}\end{center} 4629or : 4630\begin{center}{\tt affix(barycentre([[1+i,1],[1-i,1]]))}\end{center} 4631Output : 4632\begin{center}{\tt i}\end{center} 4633 4634\section{Algebraic expressions} 4635\subsection{Evaluate an expression : {\tt eval}}\index{eval} 4636\noindent {\tt eval} is used to evaluate an expression. Since 4637{\tt Xcas} always evaluate expressions entered in the command line, 4638{\tt eval} is mainly used 4639to evaluate a sub-expression in the equation writer.\\ 4640Input : 4641\begin{center}{\tt a:=2}\end{center} 4642Output : 4643\begin{center}{\tt 2}\end{center} 4644Input : 4645\begin{center}{\tt eval(2+3*a)}\end{center} 4646or 4647\begin{center}{\tt 2+3*a}\end{center} 4648Output : 4649\begin{center}{\tt 8}\end{center} 4650 4651\subsection{Evaluate algebraic expressions : {\tt evala}} 4652\index{evala} 4653\noindent In Maple, {\tt evala} is used to evaluate an expression with 4654algebraic extensions. In {\tt Xcas}, {\tt evala} is not necessary, it 4655behaves like {\tt eval}.\\ 4656 4657\subsection{Prevent evaluation : {\tt quote hold '}}\index{quote|textbf}\index{hold|textbf}\index{'|textbf} 4658A quoted subexpression (either with {\tt '} or with the 4659{\tt quote} or {\tt hold}) command will not be evaluated.\\ 4660{\bf Remark} 4661{\tt a:=quote(a)} (or {\tt a:=hold(a)}) is equivalent to {\tt purge(a)} 4662(for the sake of Maple compatibility). It returns 4663the value of this variable (or the hypothesis done on this variable). \\ 4664Input : 4665\begin{center}{\tt a:=2;quote(2+3*a)}\end{center} 4666or 4667\begin{center}{\tt a:=2;'2+3*a'}\end{center} 4668Output : 4669\begin{center}{\tt (2,2+3*a)}\end{center} 4670 4671\subsection{Force evaluation : {\tt unquote}}\index{unquote} 4672{\tt unquote} is used to evaluate inside a quoted expression.\\ 4673For example in an affectation, the variable is automatically quoted 4674(not evaluated) so that the user does not have to quote it explicitly 4675each time he want to modify its value. In some circumstances, you 4676might however want to evaluate it. 4677\\ 4678Input: 4679\begin{center}{\tt purge(b);a:=b;unquote(a):=3}\end{center} 4680Output : 4681\begin{center}{\tt b contains 3, hence a evals to 3}\end{center} 4682 4683\subsection{Distribution : {\tt expand fdistrib}}\index{fdistrib}\index{expand} 4684\noindent {\tt expand} or {\tt fdistrib} takes as argument an expression.\\ 4685{\tt expand} or {\tt fdistrib} returns the expression where 4686multiplication is distributed with respect to the addition.\\ 4687Input : 4688\begin{center}{\tt expand((x+1)*(x-2))}\end{center} 4689or : 4690\begin{center}{\tt fdistrib((x+1)*(x-2))}\end{center} 4691Output : 4692\begin{center}{\tt x\verb|^|2-2*x+x-2}\end{center} 4693 4694\subsection{Canonical form : {\tt canonical\_form}}\index{canonical\_form} 4695\noindent{\tt canonical\_form} takes as argument a trinomial of second 4696degree.\\ 4697{\tt canonical\_form} returns the canonical form of the argument.\\ 4698Example :\\ 4699Find the canonical form of : 4700$$x^2-6x+1$$ 4701Input : 4702\begin{center}{\tt canonical\_form(x\verb|^|2-6*x+1)}\end{center} 4703Output : 4704\begin{center}{\tt (x-3)\verb|^|2-8}\end{center} 4705 4706\subsection{Multiplication by the conjugate quantity : 4707{\tt mult\_conjugate}}\index{mult\_conjugate} 4708\noindent {\tt mult\_conjugate} takes as argument an expression with a 4709denominator or a numerator supposed to contain a square root : 4710\begin{itemize} 4711\item if the denominator contains a square root,\\ 4712{\tt mult\_conjugate} multiplies the numerator and the denominator 4713of the expression by the conjugate quantity of the denominator. 4714\item otherwise, if the numerator contains a square root,\\ 4715{\tt mult\_conjugate} multiplies the numerator and the denominator of this 4716expression by the conjugate quantity of the numerator. 4717\end{itemize} 4718Input : 4719\begin{center}{\tt mult\_conjugate((2+sqrt(2))/(2+sqrt(3)))}\end{center} 4720Output : 4721\begin{center}{\tt (2+sqrt(2))*(2-sqrt(3))/((2+sqrt(3))*(2-sqrt(3)))}\end{center} 4722Input : 4723\begin{center}{\tt mult\_conjugate((2+sqrt(2))/(sqrt(2)+sqrt(3)))}\end{center} 4724Output : 4725\begin{center}{\tt (2+sqrt(2))*(-sqrt(2)+sqrt(3))/}\end{center} 4726\begin{center}{\tt ((sqrt(2)+sqrt(3))*(-sqrt(2)+sqrt(3)))}\end{center} 4727Input : 4728\begin{center}{\tt mult\_conjugate((2+sqrt(2))/2)}\end{center} 4729Output : 4730\begin{center}{\tt (2+sqrt(2))*(2-sqrt(2))/(2*(2-sqrt(2)))}\end{center} 4731 4732\subsection{Separation of variables : {\tt split}}\index{split} 4733\noindent{\tt split} takes two arguments : an expression depending 4734on two variables and the list of these two variables.\\ 4735If the expression may be factorized into two factors 4736where each factor depends 4737only on one variable, {\tt split} returns the list of this two 4738factors, otherwise it returns the list {\tt [0]}.\\ 4739Input : 4740\begin{center}{\tt split((x+1)*(y-2),[x,y])}\end{center} 4741or : 4742\begin{center}{\tt split(x*y-2*x+y-2,[x,y])}\end{center} 4743Output : 4744\begin{center}{\tt [x+1,y-2]}\end{center} 4745Input : 4746\begin{center}{\tt split((x\verb|^|2*y\verb|^|2-1,[x,y])}\end{center} 4747Output : 4748\begin{center}{\tt [0]}\end{center} 4749 4750\subsection{Factorization : {\tt factor}}\index{factor|textbf}\label{sec:factore} 4751\noindent{\tt factor} takes as argument an expression.\\ 4752{\tt factor} factorizes this expression on the field of its coefficients, 4753with the addition of $i$ in complex mode. If {\tt sqrt} is enabled 4754in the Cas configuration, polynomials of order 2 are factorized in 4755complex mode or in real mode if the discriminant is positive.\\ 4756{\bf Examples} 4757\begin{enumerate} 4758\item Factorize $x^4-1$ over $\mathbb Q$.\\ 4759Input : 4760\begin{center}{\tt factor(x\verb|^|4-1)}\end{center} 4761Output : 4762\begin{center}{\tt (x\verb|^|2+1)*(x+1)*(x-1)}\end{center} 4763The coefficients are rationals, hence the factors are polynomials with 4764rationals coefficients.\\ 4765\item Factorize $x^4-1$ over $\mathbb Q[i]$ \\ 4766To have a complex factorization, check {\tt complex} in the {\tt cas} 4767configuration (red button displaying the status line).\\ 4768Input : 4769\begin{center}{\tt factor(x\verb|^|4-1)}\end{center} 4770Output : 4771\begin{center}{\tt -i*(-x+-i)*(i*x+1)*(-x+1)*(x+1)}\end{center} 4772\item Factorize $x^4+1$ over $\mathbb Q$\\ 4773Input : 4774\begin{center}{\tt factor(x\verb|^|4+1)}\end{center} 4775Output : 4776\begin{center}{\tt x\verb|^|4+1}\end{center} 4777Indeed $ x^4+1$ has no factor with rational coefficients.\\ 4778\item Factorize $x^4+1$ over $\mathbb Q[i]$\\ 4779Check {\tt complex} in the {\tt cas} 4780configuration (red button rouge displaying the status line).\\ 4781Input : 4782\begin{center}{\tt factor(x\verb|^|4-1)}\end{center} 4783Output : 4784\begin{center}{\tt (x\verb|^|2+i)*(x\verb|^|2+-i)}\end{center} 4785\item Factorize $x^4+1$ over $\mathbb R$.\\ 4786You have to provide the square root required for extending the 4787rationals. In order to do that with the help of {\tt Xcas}, 4788first check {\tt complex} in the {\tt cas} 4789configuration and input :\\ 4790\begin{center}{\tt solve(x\verb|^|4+1,x)}\end{center}\index{solve}\index{resoudre} 4791Output : 4792\begin{center}{\tt [sqrt(2)/2+(i)*sqrt(2)/2,sqrt(2)/2+(i)*(-(sqrt(2)/2)), 4793 -sqrt(2)/2+(i)*sqrt(2)/2,-sqrt(2)/2+(i)*(-(sqrt(2)/2))]}\end{center} 4794The roots depends on $\sqrt 2$. Uncheck complex mode in the Cas configuration 4795and input : 4796\begin{center}{\tt factor(x\verb|^|4+1,sqrt(2))}\end{center} 4797Output : 4798\begin{center}{\tt (x\verb|^|2+sqrt(2)*x+1)*(x\verb|^|2+(-(sqrt(2)))*x+1)}\end{center} 4799To factorize over $\mathbb C$, check {\tt complex} in the 4800{\tt cas} configuration or input {\tt cFactor(x\verb|^|4+1,sqrt(2))} 4801(cf {\tt cFactor}). 4802\end{enumerate} 4803 4804\subsection{Complex factorization : {\tt cFactor}}\index{cFactor} 4805\noindent{\tt cFactor} takes as argument an expression.\\ 4806{\tt cFactor} factorizes this expression on the field 4807$\mathbb Q[i] \subset \mathbb C$ (or over the complexified field of 4808the coefficients of the argument) even if you are in real mode.\\ 4809{\bf Examples} 4810\begin{enumerate} 4811\item Factorize $x^4-1$ over $\mathbb Z[i]$.\\ 4812Input : 4813\begin{center}{\tt cFactor(x\verb|^|4-1)}\end{center} 4814Output : 4815\begin{center}{\tt -((x+-i)*((-i)*x+1)*((-i)*x+i)*(x+1))}\end{center} 4816\item Factorize $x^4+1$ over $\mathbb Z[i]$.\\ 4817Input : 4818\begin{center}{\tt cFactor(x\verb|^|4+1)}\end{center} 4819Output : 4820\begin{center}{\tt (x\verb|^|2+i)*(x\verb|^|2+-i)}\end{center} 4821\item For a complete factorization of $x^4+1$, 4822check the sqrt box in the Cas configuration or input : 4823\begin{center}{\tt cFactor(x\verb|^|4+1,sqrt(2))}\end{center} 4824Output : 4825\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} 4826\end{enumerate} 4827 4828\subsection{Zeros of an expression : {\tt zeros}}\index{zeros} 4829\noindent{\tt zeros} takes as argument an expression depending on $x$.\\ 4830{\tt zeros} returns a list of values of $x$ where the expression 4831vanishes. The list may be incomplete in exact mode if the expression 4832is not polynomial or if intermediate 4833factorizations have irreducible factors of order strictly 4834greater than 2.\\ 4835In real mode, (complex box unchecked in the Cas configuration 4836or {\tt complex\_mode:=0}), only reals zeros are returned. In 4837({\tt complex\_mode:=1}) reals and complex zeros are returned. See 4838also {\tt cZeros} to get complex zeros in real mode.\\ 4839Input in real mode : 4840\begin{center}{\tt zeros(x\verb|^|2+4)}\end{center} 4841Output : 4842\begin{center}{\tt []}\end{center} 4843Input in complex mode : 4844\begin{center}{\tt zeros(x\verb|^|2+4)}\end{center} 4845Output : 4846\begin{center}{\tt [-2*i,2*i]}\end{center} 4847Input in real mode : 4848\begin{center}{\tt zeros(ln(x)\verb|^|2-2)}\end{center} 4849Output : 4850\begin{center}{\tt [exp(sqrt(2)),exp(-(sqrt(2)))]}\end{center} 4851Input in real mode : 4852\begin{center}{\tt zeros(ln(y)\verb|^|2-2,y)}\end{center} 4853Output : 4854\begin{center}{\tt [exp(sqrt(2)),exp(-(sqrt(2)))]}\end{center} 4855Input in real mode : 4856\begin{center}{\tt zeros(x*(exp(x))\verb|^|2-2*x-2*(exp(x))\verb|^|2+4)}\end{center} 4857Output : 4858 \begin{center}{\tt [[log(sqrt(2)),2]}\end{center} 4859 4860\subsection{Complex zeros of an expression : {\tt cZeros}}\index{cZzeros} 4861\noindent{\tt cZeros} takes as argument an expression depending on $x$.\\ 4862{\tt cZeros} returns a list of complex values of $x$ where the expression 4863vanishes. The list may be incomplete in exact mode if the expression 4864is not polynomial or if intermediate 4865factorizations have irreducible factors of order strictly 4866greater than 2.\\ 4867Input in real or complex mode : 4868\begin{center}{\tt cZeros(x\verb|^|2+4)}\end{center} 4869Output : 4870\begin{center}{\tt [-2*i,2*i]}\end{center} 4871Input : 4872\begin{center}{\tt cZeros(ln(x)\verb|^|2-2)}\end{center} 4873Output : 4874\begin{center}{\tt [exp(sqrt(2)),exp(-(sqrt(2)))]}\end{center} 4875Input : 4876\begin{center}{\tt cZeros(ln(y)\verb|^|2-2,y)}\end{center} 4877Output : 4878\begin{center}{\tt [exp(sqrt(2)),exp(-(sqrt(2)))]}\end{center} 4879Input : 4880\begin{center}{\tt cZeros(x*(exp(x))\verb|^|2-2*x-2*(exp(x))\verb|^|2+4)}\end{center} 4881Output : 4882\begin{center}{\tt [log(sqrt(2)),log(-sqrt(2)),2]}\end{center} 4883 4884\subsection{Normal form : {\tt normal}}\index{normal|textbf} 4885\noindent{\tt normal} takes as argument an expression. 4886The expression is considered as a rational fraction with respect 4887to generalized identifiers 4888(either true identifiers or transcendental functions replaced by 4889a temporary identifiers) with coefficients in $\mathbb Q$ or $\mathbb Q[i]$ 4890or in an algebraic extension (e.g. $\mathbb Q[\sqrt{2}]$). 4891{\tt normal} returns the expanded irreducible representation 4892of this rational fraction. See also {\tt ratnormal} for pure rational 4893fractions or {\tt simplify} if the transcendental functions are 4894not algebraically independent.\\ 4895Input : 4896\begin{center}{\tt normal((x-1)*(x+1))}\end{center} 4897Output : 4898 \begin{center}{\tt x\verb|^|2-1}\end{center} 4899{\bf Remarks} 4900\begin{itemize} 4901\item Unlike {\tt simplify}, 4902{\tt normal} does not try to find algebraic relations between 4903transcendental functions like $\cos(x)^2+\sin(x)^2=1$. 4904\item 4905It is sometimes necessary to run the {\tt normal} command twice to 4906get a fully irreducible representation of an expression 4907containing algebraic extensions. 4908\end{itemize} 4909%% Input : 4910%% \begin{center}{\tt normal(3-54*sqrt(1/162))}\end{center} 4911%% Output : 4912%% \begin{center}{\tt (-9*sqrt(2)+9)/3}\end{center} 4913%% Input : 4914%% \begin{center}{\tt normal((-9*sqrt(2)+9)/3)}\end{center} 4915%% Output : 4916%% \begin{center}{\tt -(3*sqrt(2))+3}\end{center} 4917 4918\subsection{Simplify : {\tt simplify}}\index{simplify|textbf} 4919\noindent{\tt simplify} simplifies an expression. It behaves 4920like {\tt normal} for rational fractions and algebraic extensions. 4921For expressions 4922containing transcendental functions, {\tt simplify} tries first to rewrite 4923them in terms of algebraically independent transcendental functions. 4924For trigonometric expressions, this requires radian mode 4925(check {\tt radian} in the {\tt cas} 4926configuration or input {\tt angle\_radian:=1}).\\ 4927Input : 4928\begin{center}{\tt simplify((x-1)*(x+1))}\end{center} 4929Output : 4930 \begin{center}{\tt x\verb|^|2-1}\end{center} 4931Input : 4932\begin{center}{\tt simplify(3-54*sqrt(1/162))}\end{center} 4933Output : 4934 \begin{center}{\tt -3*sqrt(2)+3}\end{center} 4935Input : 4936\begin{center}{\tt simplify((sin(3*x)+sin(7*x))/sin(5*x))}\end{center} 4937Output : 4938\begin{center}{\tt 4*(cos(x))\verb|^|2-2}\end{center} 4939 4940\subsection{Normal form for rational fractions : {\tt ratnormal}}\index{ratnormal} 4941\noindent{\tt ratnormal} rewrites an expression using 4942its irreducible representation. The expression is viewed 4943as a multivariate rational fraction with 4944coefficients in $\mathbb Q$ (or $\mathbb Q[i]$). The variables are 4945generalized identifiers which are assumed to be algebraically independent. 4946Unlike with {\tt normal}, an algebraic extension 4947is considered as a generalized identifier. Therefore {\tt ratnormal} 4948is faster but might miss some simplifications if 4949the expression contains radicals or algebraically dependent transcendental 4950functions.\\ 4951Input : 4952\begin{center}{\tt ratnormal((x\verb|^|3-1)/(x\verb|^|2-1))}\end{center} 4953Output : 4954 \begin{center}{\tt (x\verb|^|2+x+1)/(x+1)}\end{center} 4955Input : 4956\begin{center}{\tt ratnormal((-2x\verb|^|3+3x\verb|^|2+5x-6)/(x\verb|^|2-2x+1))}\end{center} 4957Output : 4958 \begin{center}{\tt (-2*x\verb|^|2+x+6)/(x-1)}\end{center} 4959 4960\subsection{Substitute a variable by a value : {\tt subst}}\index{subst|textbf}\label{sec:subst} 4961\noindent{\tt subst} takes two or three arguments : 4962\begin{itemize} 4963\item an expression depending on a variable, 4964an equality (variable=value of substitution) or a list of equalities. 4965\item an expression depending on a variable, a variable or a list 4966of variables, a value or a list of values for substitution. 4967\end{itemize} 4968{\tt subst} returns the expression with the substitution done. 4969Note that {\tt subst} does not quote its argument, hence 4970in a normal evaluation process, the substitution variable should 4971be purged otherwise it will be replaced by its assigned value 4972before substitution is done.\\ 4973Input : 4974\begin{center}{\tt subst(a\verb|^|2+1,a=2)}\end{center} 4975or : 4976\begin{center}{\tt subst(a\verb|^|2+1,a,2)}\end{center} 4977Output (if the variable {\tt a} is purged else first input {\tt purge(a)}) : 4978\begin{center}{\tt 5}\end{center} 4979Input : 4980\begin{center}{\tt subst(a\verb|^|2+b,[a,b],[2,1])}\end{center} 4981or : 4982\begin{center}{\tt subst(a\verb|^|2+b,[a=2,b=1])}\end{center} 4983Output (if the variables {\tt a} and {\tt b} are purged else first input 4984{\tt purge(a,b)}) : 4985 \begin{center}{\tt 5}\end{center} 4986{\tt subst} may also be used to make a change of variable in an integral. 4987In this case the {\tt integrate} command should be quoted 4988(otherwise, the integral would be computed before substitution) or 4989the inert form {\tt Int} should be used. 4990In both cases, the name of the integration variable must be given as 4991argument of {\tt Int} or {\tt integrate} even you are integrating 4992with respect to {\tt x}.\\ 4993Input : 4994\begin{center}{\tt subst('integrate(sin(x\verb|^|2)*x,x,0,pi/2)',x=sqrt(t))}\end{center} 4995or : 4996\begin{center}{\tt subst(Int(sin(x\verb|^|2)*x,x,0,pi/2),x=sqrt(t))}\end{center} 4997Output 4998\begin{center}{\tt integrate(sin(t)*sqrt(t)*1/2*1/t*sqrt(t),t,0,(pi/2)\verb|^|2)}\end{center} 4999Input : 5000\begin{center}{\tt subst('integrate(sin(x\verb|^|2)*x,x)',x=sqrt(t))}\end{center} 5001or : 5002\begin{center}{\tt subst(Int(sin(x\verb|^|2)*x,x),x=sqrt(t))}\end{center} 5003Output 5004\begin{center}{\tt integrate(sin(t)*sqrt(t)*1/2*1/t*sqrt(t),t)}\end{center} 5005 5006\subsection{Substitute a variable by a value (Maple and Mupad compatibility) : {\tt subs}}\index{subs}\label{sec:subs} 5007\noindent In {\tt Maple} and in {\tt Mupad}, one would use the {\tt subs} 5008command to substitute a variable 5009by a value in an expression. But the order of the arguments differ 5010between {\tt Maple} and {\tt Mupad}. Therefore, to achieve compatibility, 5011{\tt Xcas} {\tt subs} command arguments order depends on the mode 5012\begin{itemize} 5013\item 5014In {\tt Maple} mode, {\tt subs} takes two arguments : an equality 5015(variable=substitution value) and the expression.\\ 5016To substitute several variables in an expression, use a list of equality 5017(variable names {\tt =} substitution value) as first argument. 5018\item In {\tt Mupad} or {\tt Xcas} or {\tt TI}, {\tt subs} 5019takes two or three arguments : 5020an expression and an equality (variable=substitution value) or 5021an expression, a variable name and the substitution value.\\ 5022To substitute several variables, {\tt subs} takes two or three arguments : 5023\begin{itemize} 5024\item an expression of variables and a list of 5025(variable names {\tt =} substitution value), 5026\item 5027an expression of variables, a list of variables and a list of their 5028substitution values. 5029\end{itemize} 5030\end{itemize} 5031{\tt subs} returns the expression with the substitution done. 5032Note that {\tt subs} does not quote its argument, hence 5033in a normal evaluation process, the substitution variable should 5034be purged otherwise it will be replaced by its assigned value 5035before substitution is done.\\ 5036Input in {\tt Maple} mode (if the variable {\tt a} is purged else input 5037{\tt purge(a)}) : 5038\begin{center}{\tt subs(a=2,a\verb|^|2+1)}\end{center} 5039Output 5040\begin{center}{\tt 2\verb|^|2+1}\end{center} 5041Input in {\tt Maple} mode (if the variables {\tt a} and {\tt b} are purged 5042 else input {\tt purge(a,b)}): 5043\begin{center}{\tt subs([a=2,b=1],a\verb|^|2+b)}\end{center} 5044Output : 5045\begin{center}{\tt 2\verb|^|2+1}\end{center} 5046Input : 5047\begin{center}{\tt subs(a\verb|^|2+1,a=2)}\end{center} 5048or : 5049\begin{center}{\tt subs(a\verb|^|2+1,a,2)}\end{center} 5050Output (if the variable {\tt a} is purged else input {\tt purge(a)}) : 5051\begin{center}{\tt 5}\end{center} 5052Input : 5053\begin{center}{\tt subs(a\verb|^|2+b,[a=2,b=1])}\end{center} 5054or : 5055\begin{center}{\tt subs(a\verb|^|2+b,[a,b],[2,1])}\end{center} 5056Output (if the variables {\tt a} and {\tt b} are purged else input 5057{\tt purge(a,b)}) : 5058\begin{center}{\tt 2\verb|^|2+1}\end{center} 5059 5060\subsection{Evaluate a primitive at boundaries: {\tt preval}}\index{preval} 5061\noindent{\tt preval} takes three arguments : an expression {\tt F} 5062depending on 5063the variable {\tt x}, and two expressions {\tt a} and {\tt b}.\\ 5064{\tt preval} computes $F_{|x=b}-F_{|x=a}$.\\ 5065 {\tt preval} is used to compute a definite integral 5066when the primitive $F$ of the integrand $f$ is known. Assume 5067for example that {\tt F:=int(f,x)}, then {\tt preval(F,a,b)} is equivalent 5068to {\tt int(f,x,a,b)} but does not require to compute again {\tt F} 5069from {\tt f} if you change the values of $a$ or $b$.\\ 5070Input : 5071\begin{center}{\tt preval(x\verb|^|2+x,2,3)}\end{center} 5072Output : 5073\begin{center}{\tt 6}\end{center} 5074 5075\subsection{Sub-expression of an expression : {\tt part}}\index{part} 5076\noindent{\tt part} takes two arguments : an expression and an integer $n$.\\ 5077{\tt part} evaluate the expression and then returns the $n$-th sub-expression 5078of this expression.\\ 5079Input : 5080\begin{center}{\tt part(x\verb|^|2+x+1,2)}\end{center} 5081Output : 5082\begin{center}{\tt x}\end{center} 5083Input : 5084\begin{center}{\tt part(x\verb|^|2+(x+1)*(y-2)+2,2)}\end{center} 5085Output : 5086\begin{center}{\tt (x+1)*(y-2)}\end{center} 5087Input : 5088\begin{center}{\tt part((x+1)*(y-2)/2,2)}\end{center} 5089Output : 5090\begin{center}{\tt y-2}\end{center} 5091 5092\section{Values of $u_n$} 5093\subsection{Array of values of a sequence : {\tt tablefunc}}\index{tablefunc} 5094{\tt tablefunc} is a command that should be used inside a spreadsheet 5095(opened with {\tt Alt+t}), 5096it returns a template to fill two columns, with 5097the table of values of a function. If the step value is 1, 5098{\tt tablefunc(ex,n,n0,1)}, where {\tt ex} is an expression 5099depending on {\tt n}, will fill the spreadsheet with 5100the values of the sequence $u_n=ex$ for $n=n0,\ n0+1,\ n0+2,....$. 5101 5102{\bf Example} : display the values of the sequence $u_n=\sin(n)$\\ 5103Select a cell of a spreadsheet (for example {\tt C0}) 5104and input in the command line : 5105\begin{center}{\tt tablefunc(sin(n),n,0,1)}\end{center} 5106Output : 5107\begin{center}{\tt two columns : {\tt n} and {\tt sin(n)}}\end{center} 5108\begin{itemize} 5109\item in the column C: the variable name {\tt n}, the value of the step 5110(this value should be equal to 1 for a sequence), 5111the value of {\tt n0} (here 0), then a recurrence 5112formula ({\tt C2+C\$1}, ...). 5113\item in the column D: {\tt sin(n)}, {\tt "Tablefunc"}, then a 5114recurrence formula. 5115\item For each row, 5116the values of the sequence ${\tt u_n=\sin(n)}$ correspond to 5117the values of {\tt n} starting from {\tt n=n0} (here 0). 5118\end{itemize} 5119 5120\subsection{Table of values and graph of a recurrent sequence : {\tt tableseq} and {\tt plotseq}}\index{tableseq|textbf}\index{plotseq} 5121{\tt tableseq} is a command that should be used inside a spreadsheet 5122(opened with {\tt Alt+t}), 5123it returns a template to fill one column with 5124${\tt u_0, \ u_{n+1}=f(u_{n})}$ (one-term recurrence) or 5125more generally $u_0,...,u_k, \ \ u_{n+k+1}=f(u_n,u_{n+1},...,u_{n+k})$. 5126The template fills the column starting from the selected cell, or 5127starting from 0 if the whole column was selected.\\ 5128See also {\tt plotseq} (section \ref{sec:plotseq}) for a graphic representation 5129of a one-term recurrence sequence. 5130 5131{\bf Examples} : 5132\begin{itemize} 5133\item display the values of the sequence $u_0=3.5, \ u_n=\sin(u_{n-1})$\\ 5134Select a cell of the spreadsheet (for example {\tt B0}) and 5135 input in the command line : 5136\begin{center}{\tt tableseq(sin(n),n,3.5)}\end{center} 5137Output : 5138\begin{center}{\tt a column with sin(n), n, 3.5 5139and the formula evalf(subst(B\$0,B\$1,B2))} 5140\end{center} 5141You get the values of the sequence 5142${\tt u_0=3.5,\ u_n=sin(u_{n-1})}$ in the column 5143{\tt B}. 5144\item 5145display the values of the Fibonacci sequence 5146$u_0=1, u_1=1 \ u_{n+2}=u_n+u_{n+1}$\\ 5147Select a cell, say {\tt B0}, and input in the command line 5148\begin{center}{\tt tableseq(x+y,[x,y],[1,1])}\end{center} 5149This fills the B column sheet with 5150\begin{center}{\tt \begin{tabular}{|l|l|} 5151\hline 5152row &B\\ 5153\hline 51540 &x+y \\ 5155\hline 51561 & x\\ 5157\hline 51582 & y\\ 5159\hline 51603 & 1\\ 5161\hline 51624 & 1\\ 5163\hline 51645 & 2\\ 5165\hline 5166.. &..\\ 5167\hline 51687 & 5\\ 5169\hline 5170.. &..\\ 5171\hline 5172\end{tabular}}\end{center} 5173\end{itemize} 5174 5175\section{Operators or infixed functions} 5176An operator is an infixed function. 5177 5178\subsection{Usual operators :{\tt +, -, *, /, \^\ }}\index{+}\index{'+'}\index{-}\index{'-'}\index{\^\ } 5179{\tt +, -, *, /, \^\ } are the operators to do 5180additions, subtractions, multiplications, divisions and for raising to a 5181power. 5182 5183\subsection{{\tt Xcas} operators }\index{\@}\index{\@\@}\index{\$}\index{\%} 5184\begin{itemize} 5185\item 5186{\tt \$ } is the infixed version of {\tt seq}, for example :\\ 5187{\tt (2\verb|^|k)\$(k=0..3)= seq(2\verb|^|k,k=0..3)=(1,2,4,8)} (do not forget 5188to put parenthesis around the arguments), 5189\item 5190{\tt mod} or {\tt \%} to define a modular number, 5191\item 5192{\tt @} to compose functions for example : 5193{\tt (f@g)(x)=f(g(x))}, 5194\item 5195{\tt @@ } to compose a function many times (like a power, replacing 5196multiplication by composition), for example : 5197{\tt (f@@3)(x)=f(f(f(x)))}, 5198\item 5199{\tt minus union intersect} to get the difference, the union and the 5200intersection of two sets, 5201\item 5202{\tt ->} to define a function, 5203\item 5204{\tt := =>} to store an expression in a variable (it is the infixed 5205version of {\tt sto} and the argument order is permuted for {\tt :=}), 5206for example : {\tt a:=2} or {\tt 2=>a} or {\tt sto(2,a)}. 5207\item 5208{\tt =<} to store an expression in a variable, but the storage is 5209done by reference if the target is a matrix element or a list element. 5210This is faster if you modify objects inside an existing list or matrix 5211of large size, because no copy is made, the change is done in place. 5212Use with care, all objects pointing to this matrix or list will 5213be modified. 5214\end{itemize} 5215 5216 5217\subsection{Define an operator: {\tt user\_operator}}\index{user\_operator}\index{Binary@{\sl Binary}|textbf}\index{Delete@{\sl Delete}|textbf} 5218\noindent {\tt user\_operator} takes as argument : 5219\begin{itemize} 5220\item a string : the name of the operator, 5221\item a function of two variables with values in $\mathbb R$ or in 5222{\tt true, false}, 5223\item an option {\tt Binary} for the definition or {\tt Delete} to delete 5224this definition. 5225\end{itemize} 5226{\tt user\_operator} returns 1 if the definition is done and else returns 0. 5227 5228{\bf Example 1}\\ 5229Let $R$ be defined on $\mathbb R$ by $x\ R \ y= x*y+x+y$.\\ 5230To define the law $R$, input : 5231\begin{center}{\tt user\_operator("R",(x,y)->x*y+x+y,Binary)}\end{center} 5232Output : 5233\begin{center}{\tt 1}\end{center} 5234Input : 5235\begin{center}{\tt 5 R 7}\end{center} 5236Do not forget to put spaces around {\tt R}.\\ 5237Output : 5238\begin{center}{\tt 47}\end{center} 5239 5240{\bf Example 2}\\ 5241Let $S$ be defined on $\mathbb N$ by :\\ 5242for $x$ and $y$ integers, $x\ S \ y <=> x$ and $y$ are not coprime.\\ 5243To define the law $S$, input : 5244\begin{center}{\tt user\_operator("S",(x,y)->(gcd(x,y))!=1,Binary)}\end{center} 5245Output : 5246\begin{center}{\tt 1}\end{center} 5247Input : 5248\begin{center}{\tt 5 S 7}\end{center} 5249Do not forget to put spaces around {\tt S}.\\ 5250Output : 5251\begin{center}{\tt 0}\end{center} 5252Input : 5253\begin{center}{\tt 8 S 12}\end{center} 5254Do not forget to put spaces around {\tt S}.\\ 5255Output : 5256\begin{center}{\tt 1}\end{center} 5257 5258\section{Functions and expressions with symbolic variables} 5259\subsection{The difference between a function and an expression}\index{->}\index{:=} 5260A function {\tt f} is defined for example by :\\ 5261{\tt f(x):=x\verb|^|2-1} or by {\tt f:=x->x\verb|^|2-1} \\ 5262that is to say, for all $x$, $f(x)$ is equal to the expression 5263$x^2-1$. In that case, to have the value of $f$ for $x=2$, input :{\tt f(2)}.\\ 5264But if the input is 5265{\tt g:=x\verb|^|2-1}, then {\tt g} is a variable where the 5266expression $x^2-1$ is stored. In that case, to have the value of $g$ for $x=2$, 5267input : {\tt subst(g,x=2)} ($g$ is an expression depending on $x$). 5268 5269When a command expects a function as argument, this argument should 5270be either the definition of the function (e.g. {\tt x->x\verb|^|2-1}) 5271or a variable name assigned to a function (e.g. {\tt f} 5272previously defined by e.g. {\tt f(x):=x\verb|^|2-1}).\\ 5273When a command expects an expression as argument, this argument should 5274be either the definition of the expression (for example {\tt x\verb|^|2-1}), 5275or a variable name assigned to an expression (e.g. 5276{\tt g} previously defined, for example, by 5277{\tt g:=x\verb|^|2-1}), or the evaluation of a function. e.g. 5278{\tt f(x)} if {\tt f} is a previously defined function, 5279for example, by {\tt f(x):=x\verb|^|2-1}). 5280 5281\subsection{Transform an expression into a function : {\tt unapply}}\index{unapply} 5282\noindent {\tt unapply} is used to transform an expression into a function.\\ 5283{\tt unapply} takes two arguments an expression and the name of a variable.\\ 5284{\tt unapply} returns the function defined by this expression and 5285this variable. 5286 5287{\bf Warning} when a function is defined, 5288the right member of the assignment is not evaluated, 5289hence \verb|g:=sin(x+1); f(x):=g| does not defined the function 5290$f: x \rightarrow \sin(x+1)$ but defines the function 5291$f: x \rightarrow g$. To defined the former function, {\tt unapply} 5292should be used, like in the following example:\\ 5293Input : 5294\begin{center}{\tt g:= sin(x+1); f:=unapply(g,x)}\end{center} 5295Output : 5296\begin{center}{\tt (sin(x+1), (x)->sin(x+1))}\end{center} 5297hence, the variable {\tt g} is assigned to a symbolic expression 5298and the variable {\tt f} is assigned to a function.\\ 5299Input : 5300\begin{center}{\tt unapply(exp(x+2),x)}\end{center} 5301Output : 5302\begin{center}{\tt (x)->exp(x+2)}\end{center} 5303Input : 5304\begin{center}{\tt f:=unapply(lagrange([1,2,3],[4,8,12]),x)}\end{center} 5305Output : 5306\begin{center}{\tt (x)->4+4*(x-1)}\end{center} 5307Input : 5308\begin{center}{\tt f:=unapply(integrate(log(t),t,1,x),x)}\end{center} 5309Output : 5310\begin{center}{\tt (x)->x*log(x)-x+1}\end{center} 5311Input : 5312\begin{center}{\tt f:=unapply(integrate(log(t),t,1,x),x)}\end{center} 5313\begin{center}{\tt f(x)}\end{center} 5314Output : 5315\begin{center}{\tt x*log(x)-x+1}\end{center} 5316{\bf Remark} 5317Suppose that $f$ is a function of 2 variables $f:(x,w)\rightarrow f(x,w)$, 5318and that $g$ is the function defined by 5319$g: w \rightarrow h_w$ where $h_w$ is the function defined by 5320$h_w(x)=f(x,w)$.\\ 5321{\tt unapply} is also used to define $g$ with {\tt Xcas}.\\ 5322Input : 5323\begin{center}{\tt f(x,w):=2*x+w}\end{center} 5324\begin{center}{\tt g(w):=unapply(f(x,w),x)}\end{center} 5325\begin{center}{\tt g(3)}\end{center} 5326Output : 5327\begin{center}{\tt x->2$\cdot$x+3}\end{center} 5328 5329\subsection{Top and leaves of an expression : {\tt sommet feuille op}}\index{sommet|textbf}\index{feuille|textbf}\index{op|textbf}\label{sec:op} 5330An operator is an infixed function : for example '+' is an 5331operator and 'sin' is a function.\\ 5332An expression can be represented by a tree. The top of the tree is 5333either an operator, or a function and the leaves of the tree are the 5334arguments of the operator or of the function 5335(see also \ref{sec:makesuiteop}).\\ 5336The instruction {\tt sommet} (resp. {\tt feuille} (or {\tt op})) 5337returns the top (resp. the list of the leaves) of an expression.\\ 5338Input : 5339\begin{center}{\tt sommet(sin(x+2))}\end{center} 5340Output : 5341\begin{center}{\tt 'sin'}\end{center} 5342Input : 5343\begin{center}{\tt sommet(x+2*y)}\end{center} 5344Output : 5345\begin{center}{\tt '+'}\end{center} 5346Input : 5347\begin{center}{\tt feuille(sin(x+2))}\end{center} 5348or : 5349\begin{center}{\tt op(sin(x+2))}\end{center} 5350Output : 5351\begin{center}{\tt x+2}\end{center} 5352Input : 5353\begin{center}{\tt feuille(x+2*y)}\end{center} 5354or : 5355\begin{center}{\tt op(x+2*y)}\end{center} 5356Output : 5357\begin{center}{\tt (x,2*y) }\end{center} 5358{\bf Remark}\\ 5359Suppose that a function is defined by a program, for example 5360let us define the {\tt pgcd} function : 5361\begin{center}{\tt pgcd(a,b):=\{local r; while (b!=0) \{r:=irem(a,b);a:=b;b:=r;\} return a;\}}\end{center} 5362Then input : 5363\begin{center}{\tt sommet(pgcd)}\end{center} 5364Output : 5365\begin{center}{\tt 'program'}\end{center} 5366Then input : 5367\begin{center}{\tt feuille(pgcd)[0]}\end{center} 5368Output : 5369\begin{center}{\tt (a,b)}\end{center} 5370Then input : 5371\begin{center}{\tt feuille(pgcd)[1]}\end{center} 5372Output : 5373\begin{center}{\tt (0,0) or (15,25) if the last input was pgcd(15,25)}\end{center} 5374Then input : 5375\begin{center}{\tt feuille(pgcd)[2]}\end{center} 5376Output : 5377\begin{center}{\tt The body of the program : \{local r;....return(a);\}}\end{center} 5378 5379 5380\section{Functions} 5381\subsection{Context-dependent functions.} 5382\subsubsection{Operators {\tt +} and {\tt -}}\index{+}\index{'+'}\index{-}\index{'-'} 5383 \noindent{\tt +} (resp. {\tt -}) is an infixed function and {\tt '+'} (resp. 5384{\tt '-'}) is a prefixed function. The result depends on the nature of its 5385arguments.\\ 5386Examples with {\tt +} (all examples except the last one work also 5387with {\tt -} instead of {\tt +}) : 5388\begin{itemize} 5389\item input (1,2)+(3,4) or (1,2,3)+4 or 1+2+3+4 or '+'(1,2,3,4), output 10, 5390\item input 1+i+2+3*i or '+'(1,i,2,3*i), output 3+4*i, 5391\item input [1,2,3]+[4,1] or [1,2,3]+[4,1,0] or '+'([1,2,3],[4,1]), output 5392[5,3,3], 5393\item input [1,2]+[3,4] or '+'([1,2],[3,4]), output [4,6], 5394\item input [[1,2],[3,4]]+ [[1,2],[3,4]], output [[2,4],[6,8]], 5395\item input [1,2,3]+4 or '+'([1,2,3],4), output poly1[1,2,7], 5396\item input [1,2,3]+(4,1) or '+'([1,2,3],4,1), output poly1[1,2,8], 5397\item input "Hel"+"lo" or '+'("Hel","lo"), output "Hello". 5398\end{itemize} 5399 5400\subsubsection{Operator {\tt *}}\index{*}\index{'*'} 5401\noindent{\tt *} is an infixed function and {\tt '*'} 5402is a prefixed function. The result depends on the nature of its arguments.\\ 5403Examples with {\tt *} : 5404\begin{itemize} 5405\item input (1,2)*(3,4) or (1,2,3)*4 or 1*2*3*4 or '*'(1,2,3,4), output 24, 5406\item input 1*i*2*3*i or '*'(1,i,2,3*i), output -6, 5407\item input [10,2,3]*[4,1] or [10,2,3]*[4,1,0] or '*'([10,2,3],[4,1]), 5408output 42 (scalar product), 5409\item input [1,2]*[3,4] or '*'([1,2],[3,4]), output 11 (scalar product), 5410\item input [[1,2],[3,4]]* [[1,2],[3,4]], output [[7,10],[15,22]], 5411\item input [1,2,3]*4 or '*'([1,2,3],4), output [4,8,12], 5412\item input [1,2,3]*(4,2) or '*'([1,2,3],4,2) or [1,2,3]*8, output [8,16,24], 5413\item input (1,2)+i*(2,3) or 1+2+i*2*3, output 3+6*i. 5414\end{itemize} 5415 5416\subsubsection{Operator {\tt /}}\index{/}\index{'/'} 5417\noindent{\tt /} is an infixed function and {\tt '/'} 5418is a prefixed function. The result depends of the nature of its arguments.\\ 5419Examples with {\tt /} : 5420\begin{itemize} 5421\item input [10,2,3]/[4,1], output invalid dim 5422\item input [1,2]/[3,4] or '/'([1,2],[3,4]), output [1/3,1/2], 5423\item input 1/[[1,2],[3,4]] or '/'(1,[[1,2],[3,4]], output 5424[[-2,1],[3/2,(-1)/2]], 5425\item input [[1,2],[3,4]]*1/ [[1,2],[3,4]], output [[1,0],[0,1]], 5426\item input [[1,2],[3,4]]/ [[1,2],[3,4]], output [[1,1],[1,1]] (division term 5427by term), 5428%\item input [1,2,3]*4 or '*'([1,2,3],4), output [4,8,12], 5429%\item input [1,2,3]/(4,2) or '*'([1,2,3],4,2), output [1,2,3]*8=[8,16,24]. 5430%\item (1,2)+i/(2,3)=1+2+i*2*3=3+6*i 5431\end{itemize} 5432 5433\subsection{Usual functions} 5434\begin{itemize} 5435\item {\tt max}\index{max|textbf} takes as argument two real numbers and 5436returns their maximum, 5437\item 5438{\tt min}\index{min|textbf} takes as argument two real numbers and returns 5439their minimum, 5440\item 5441{\tt abs}\index{abs} takes as argument a complex number and returns the 5442modulus of the complex parameter (the absolute value if the complex is 5443real), 5444\item 5445{\tt sign}\index{sign|textbf} takes as argument a real number and returns its 5446sign (+1 if it is positive, 0 if it is null, and -1 if it is 5447negative), 5448\item 5449{\tt floor}\index{floor|textbf} (or {\tt iPart}\index{iPart|textbf}) 5450takes as argument a real number $r$, 5451and returns the largest integer $\leq r$, 5452\item 5453{\tt round}\index{round|textbf} takes as argument a real number and returns 5454its nearest integer, 5455\item 5456{\tt ceil} or {\tt ceiling}\index{ceil|textbf}\index{ceiling|textbf} takes as 5457argument a real number and returns the smallest integer $\geq r$ 5458\item 5459{\tt frac}\index{frac|textbf} (or {\tt fPart}\index{fPart|textbf}) takes as 5460argument a real number and returns its fractional part, 5461\item 5462{\tt trunc}\index{trunc|textbf} takes as 5463argument a real number and returns the integer equal to the real without its 5464fractional part, 5465\item 5466{\tt id}\index{id|textbf} is the identity function, 5467\item 5468{\tt sq}\index{sq|textbf} is the square function, 5469\item 5470{\tt sqrt}\index{sqrt|textbf} is the squareroot function, 5471\item 5472{\tt exp}\index{exp|textbf} is the exponential function, 5473\item 5474{\tt log}\index{log|textbf} or {\tt ln}\index{ln|textbf} is the 5475natural logarithm function, 5476\item 5477{\tt log10}\index{log10|textbf} is the base-10 logarithm function, 5478\item 5479{\tt logb}\index{logb|textbf} is the logarithm function where the second 5480argument is the base of the logarithm: 5481{\tt logb(7,10)=log10(7)=log(7)/log(10)}, 5482\item 5483{\tt sin}\index{sin|textbf} (resp. {\tt cos}\index{cosh|textbf}, 5484{\tt tan}\index{tan|textbf}) 5485is the sinus function, cosinus function, tangent function, 5486\item {\tt cot, sec, csc} are the cotangent, secant, cosecant function 5487\item {\tt asin} (or {\tt arcsin})\index{asin|textbf}\index{arcsin|textbf},{ 5488\tt acos} (or {\tt arccos})\index{acos|textbf}\index{arccos|textbf}, 5489{\tt atan} (or {\tt arctan})\index{atan|textbf}\index{arctan|textbf}, 5490{\tt acot, asec, acsc} are the inverse trigonometric functions (see 5491section \ref{sec:trigo} for more info on trigonometric functions) 5492\item 5493{\tt sinh}\index{sinh|textbf} (resp. {\tt cosh}\index{cosh|textbf}, 5494{\tt tanh}\index{tanh|textbf}) 5495is the hyperbolic sinus function, cosinus function, tangent function, 5496\item 5497{\tt asinh} or {\tt arcsinh}\index{asinh|textbf}\index{arcsinh|textbf} (resp. 5498{\tt acosh} or {\tt arccosh}\index{acosh|textbf}\index{arccosh|textbf}, 5499{\tt atanh} or {\tt arctanh}\index{atanh|textbf}\index{arctanh|textbf}) 5500is the inverse function of {\tt sinh} (resp. {\tt cosh}, {\tt tanh}) 5501\end{itemize} 5502 5503\subsection{Defining algebraic functions} 5504\subsubsection{Defining a function from $\mathbb{R}^p$ to $\mathbb{R}$} 5505\noindent For $p=1$, e.g. for $f\ :\ (x)\rightarrow x*\sin(x)$, input : 5506\begin{center}{\tt f(x):=x*sin(x)}\end{center} 5507or : 5508\begin{center}{\tt f:=x->x*sin(x)}\end{center} 5509Output : 5510\begin{center}{\tt (x)->x*sin(x)}\end{center} 5511If $p>1$, e.g. for $f\ :\ (x,y)\rightarrow x*\sin(y)$, input : 5512\begin{center}{\tt f(x,y):=x*sin(y)}\end{center} 5513or : 5514\begin{center}{\tt f:=(x,y)->x*sin(y)}\end{center} 5515Output : 5516\begin{center}{\tt (x,y)->x*sin(y)}\end{center} 5517{\bf Warning !!!} the expression after {\tt -> } is not evaluated. 5518You should use {\tt unapply} if you expect the second member to 5519be evaluated before the function is defined. 5520 5521\subsubsection{Defining a function from $\mathbb{R}^p$ to $\mathbb{R}^q$} 5522For example: 5523\begin{itemize} 5524\item To define the function $h\ :\ (x,y)\rightarrow (x*\cos(y),x*\sin(y))$.\\ 5525 Input : 5526\begin{center}{\tt h(x,y):=(x*cos(y),x*sin(y))}\end{center} 5527Output : 5528\begin{center}{\tt (x,y)->\{ \\ 5529 x*cos(y),x*sin(y);\\ 5530\}"}\end{center} 5531\item To define the function $h\ :\ (x,y)\rightarrow [x*\cos(y),x*\sin(y)]$.\\ 5532 Input : 5533\begin{center}{\tt h(x,y):=[x*cos(y),x*sin(y)];}\end{center} 5534or : 5535\begin{center}{\tt h:=(x,y)->[x*cos(y),x*sin(y)];}\end{center} 5536or : 5537\begin{center}{\tt h(x,y):=\{[x*cos(y),x*sin(y)]\};}\end{center} 5538or : 5539\begin{center}{\tt h:=(x,y)->return[x*cos(y),x*sin(y)];}\end{center} 5540or : 5541\begin{center}{\tt h(x,y):=\{return [x*cos(y),x*sin(y)];\}}\end{center} 5542Output : 5543\begin{center}{\tt (x,y)->\{return([x*cos(y),x*sin(y)]);\}}\end{center} 5544\end{itemize} 5545{\bf Warning !!!} The expression after {\tt -> } is not evaluated. 5546 5547\subsubsection{Defining families of function from $\mathbb{R}^{p-1}$ 5548to $\mathbb{R}^q$ using a function from $\mathbb{R}^p$ to $\mathbb{R}^q$} 5549Suppose that the function $f: (x,y) \rightarrow f(x,y)$ is defined, 5550and we want to define a family of functions $g(t)$ such 5551that $g(t)(y):=f(t,y)$ (i.e. $t$ is viewed as a parameter). 5552Since the expression after {\tt -> } (or {\tt :=}) 5553is not evaluated, we should not define $g(t)$ by {\tt g(t):=y->f(t,y)}, 5554we have to use the {\tt unapply} command. 5555 5556For example, assuming that $f:(x,y)\rightarrow x\sin(y)$ and $g(t): y\rightarrow f(t,y)$, input : 5557\begin{center}{\tt f(x,y):=x*sin(y);g(t):=unapply(f(t,y),y)}\end{center} 5558Output : 5559\begin{center}{\tt ((x,y)->x*sin(y), (t)->unapply(f(t,y),y))}\end{center} 5560Input : 5561\begin{center}{\tt g(2)}\end{center} 5562Output : 5563\begin{center}{\tt y->2$\cdot$ sin(y)}\end{center} 5564Input : 5565\begin{center}{\tt g(2)(1)}\end{center} 5566Output : 5567\begin{center}{\tt 2$\cdot$ sin(1)}\end{center} 5568 5569Next example, suppose that the function 5570$h: (x,y) \rightarrow [x*\cos(y),x*\sin(y)]$ is defined, and 5571we want to define the family of functions $k(t)$ having $t$ as 5572parameter such that $k(t)(y):=h(t,y)$. 5573To define the function $h(x,y)$, input : 5574\begin{center}{\tt h(x,y):=(x*cos(y),x*sin(y))}\end{center} 5575To define properly the function $k(t)$, input : 5576\begin{center}{\tt k(t):=unapply(h(x,t),x)}\end{center} 5577Output : 5578\begin{center}{\tt (t)->unapply(h(x,t),x)}\end{center} 5579Input : 5580\begin{center}{\tt k(2)}\end{center} 5581Output : 5582\begin{center}{\tt (x)->(x*cos(2),x*sin(2))}\end{center} 5583Input : 5584\begin{center}{\tt k(2)(1)}\end{center} 5585Output : 5586\begin{center}{\tt (2*cos(1),2*sin(1))}\end{center} 5587 5588\subsection{Composition of two functions: {\tt @}}\index{\@|textbf} 5589With {\tt Xcas}, the composition of functions is done with the infixed operator 5590{\tt @}.\\ 5591Input : 5592\begin{center}{\tt (sq@sin+id)(x)}\end{center} 5593Output : 5594\begin{center}{\tt (sin(x))\verb|^|2+x}\end{center} 5595Input : 5596\begin{center}{\tt (sin@sin)(pi/2)}\end{center} 5597Output : 5598\begin{center}{\tt sin(1)}\end{center} 5599 5600\subsection{Repeated function composition: {\tt @@}}\index{\@\@|textbf} 5601With {\tt Xcas}, the repeated composition of a function with itself 5602 $n \in {\mathbb N}$ times is done with the infixed operator {\tt @@}.\\ 5603Input : 5604\begin{center}{\tt (sin@@3)(x)}\end{center} 5605Output : 5606\begin{center}{\tt sin(sin(sin(x)))}\end{center} 5607Input : 5608\begin{center}{\tt (sin@@2)(pi/2)}\end{center} 5609Output : 5610\begin{center}{\tt sin(1)}\end{center} 5611 5612\subsection{Define a function with the history : {\tt as\_function\_of}}\index{as\_function\_of} 5613\noindent If an entry defines the variable {\tt a} and if a later 5614entry defines the variable {\tt b} (supposed to be dependent on 5615{\tt a}), then 5616{\tt c:=as\_function\_of(b,a)} will define a function {\tt c} such that 5617{\tt c(a)=b}.\\ 5618Input : 5619\begin{center}{\tt a:=sin(x)}\end{center} 5620Output : 5621\begin{center}{\tt sin(x)}\end{center} 5622Input : 5623\begin{center}{\tt b:=sqrt(1+a\verb|^|2)}\end{center} 5624Output : 5625\begin{center}{\tt sqrt(1+sin(x)\verb|^|2)}\end{center} 5626Input : 5627\begin{center}{\tt c:=as\_function\_of(b,a)}\end{center} 5628Output : 5629\begin{flushleft}{\tt (a)-> \\ 5630\{ local NULL;\\ 5631 return(sqrt(1+a\verb|^|2));\\ 5632\}}\end{flushleft} 5633Input : 5634\begin{center}{\tt c(x)}\end{center} 5635Output : 5636\begin{center}{\tt sqrt(1+x\verb|^|2)}\end{center} 5637Input : 5638\begin{center}{\tt a:=2}\end{center} 5639Output : 5640\begin{center}{\tt 2}\end{center} 5641Input : 5642\begin{center}{\tt b:=1+a\verb|^|2}\end{center} 5643Output : 5644\begin{center}{\tt 5}\end{center} 5645Input : 5646\begin{center}{\tt c:=as\_function\_of(b,a)}\end{center} 5647Output : 5648\begin{flushleft}{\tt (a)-> \\ 5649\{ local NULL;\\ 5650 return(sqrt(1+a\verb|^|2));\\ 5651\}}\end{flushleft} 5652Input : 5653\begin{center}{\tt c(x)}\end{center} 5654Output : 5655\begin{center}{\tt 1+x\verb|^|2}\end{center} 5656 5657{\bf Warning !!}\\ 5658If the variable {\tt b} has been assigned 5659several times, the first assignment of {\tt b} following the last 5660assignment of {\tt a} will be used. Moreover, the order used is the 5661order of validation of the commandlines, 5662which may not be reflected by the Xcas interface 5663if you reused previous commandlines.\\ 5664Input for example :\\ 5665{\tt a:=2} then\\ 5666{\tt b:=2*a+1} then\\ 5667{\tt b:=3*a+2} then\\ 5668{\tt c:=as\_function\_of(b,a)}\\ 5669Output : 5670\begin{center}{\tt (a)-> \{local NULL; return(2*a+1);\}}\end{center} 5671i.e. {\tt c(x)} is equal to {\tt 2*x+1}. \\ 5672But, input :\\ 5673{\tt a:=2} then\\ 5674{\tt b:=2*a+1} then\\ 5675{\tt a:=2} then\\ 5676{\tt b:=3*a+2} then\\ 5677{\tt c:=as\_function\_of(b,a)}\\ 5678Output : 5679\begin{center}{\tt (a)-> \{local NULL; return(3*a+2);\}}\end{center} 5680i.e. {\tt c(x)} is equal to {\tt 3*x+2}. \\ 5681Hence the line where {\tt a} is defined must be reevaluated before the good 5682definition of {\tt b}. 5683 5684\section{Derivation and applications.} 5685\subsection{Functional derivative : {\tt function\_diff}}\index{function\_diff} 5686{\tt function\_diff} takes a function as argument.\\ 5687{\tt function\_diff} returns the derivative function of this function.\\ 5688Input : 5689\begin{center}{\tt function\_diff(sin)}\end{center} 5690Output : 5691\begin{center}{\tt (` x`)->cos(` x`)}\end{center} 5692Input : 5693\begin{center}{\tt function\_diff(sin)(x)}\end{center} 5694Output : 5695\begin{center}{\tt cos(x)}\end{center} 5696Input : 5697\begin{center}{\tt f(x):=x\verb|^|2+x*cos(x)}\end{center} 5698\begin{center}{\tt function\_diff(f)}\end{center} 5699Output : 5700\begin{center}{\tt (` x`)->2*` x`+cos(` x`)+` x`*(-(sin(` x`)))}\end{center} 5701Input : 5702\begin{center}{\tt function\_diff(f)(x)}\end{center} 5703Output : 5704\begin{center}{\tt cos(x)+x*(-(sin(x)))+2*x}\end{center} 5705To define the function $g$ as $f'$, input :\\ 5706\begin{center}{\tt g:=function\_diff(f)}\end{center} 5707The {\tt function\_diff} instruction has the same effect as 5708using the expression derivative in conjunction with {\tt unapply} : 5709\begin{center}{\tt g:=unapply(diff(f(x),x),x)}\end{center} 5710\begin{center}{\tt g(x)}\end{center} 5711Output : 5712\begin{center}{\tt cos(x)+x*(-(sin(x)))+2*x}\end{center} 5713{\bf Warning !!!}\\ 5714In {\tt Maple} mode, for compatibility, 5715{\tt D} may be used in place of {\tt function\_diff}. 5716For this reason, it is impossible to assign a variable named 5717{\tt D} in {\tt Maple} mode (hence you can not name a 5718geometric object {\tt D}). 5719 5720\subsection{Length of an arc : {\tt arcLen}}\index{arcLen} 5721\noindent {\tt arcLen} takes four arguments : an expression $ex$ (resp. a list 5722of two expressions $[ex1,ex2]$), the name of a parameter and two values $a$ 5723and $b$ of this parameter.\\ 5724{\tt arcLen} computes the length of the curve define by the equation 5725$y=f(x)=ex$ (resp. by $x=ex1,y=ex2$) when the parameter values varies from $a$ 5726to $b$, using the formula 5727{\tt arcLen(f(x),x,a,b)=}\\ 5728{\tt integrate(sqrt(diff(f(x),x)\verb|^|2+1),x,a,b)}\\ 5729or \\ 5730{\tt integrate(sqrt(diff(x(t),t)\verb|^|2+diff(y(t),t)\verb|^|2),t,a,b)}.\\ 5731 5732{\bf Examples} 5733\begin{itemize} 5734\item Compute the length of the parabola $y=x^2$ from $x=0$ to $x=1$.\\ 5735Input : 5736\begin{center}{\tt arcLen(x\verb|^|2,x,0,1)}\end{center} 5737or 5738\begin{center}{\tt arcLen([t,t\verb|^|2],t,0,1)}\end{center} 5739Output : 5740\begin{center}{\tt -1/4*log(sqrt(5)-2)-(-(sqrt(5)))/2}\end{center} 5741\item Compute the length of the curve $y=\cosh(x)$ from $x=0$ to 5742$x=\ln(2)$.\\ 5743Input : 5744\begin{center}{\tt arcLen(cosh(x),x,0,log(2))}\end{center} 5745Output : 5746\begin{center}{\tt 3/4}\end{center} 5747\item Compute the length of the circle $x=\cos(t),y=\sin(t)$ from $t=0$ to 5748$t=2*\pi$.\\ 5749Input : 5750\begin{center}{\tt arcLen([cos(t),sin(t)],t,0,2*pi)}\end{center} 5751Output : 5752\begin{center}{\tt 2*pi}\end{center} 5753\end{itemize} 5754 5755\subsection{Maximum and minimum of an expression: {\tt fMax fMin}}\index{fMax}\index{fMin} 5756\noindent{\tt fMax} and {\tt fMin} take one or two arguments : an expression 5757of a variable and the name of this variable (by default {\tt x}).\\ 5758{\tt fMax} returns the abscissa 5759of a maximum of the expression.\\ 5760{\tt fMin} returns the abscissa 5761of a minimum of the expression.\\ 5762Input : 5763\begin{center}{\tt fMax(sin(x),x)}\end{center} 5764Or : 5765\begin{center}{\tt fMax(sin(x))}\end{center} 5766Or : 5767\begin{center}{\tt fMax(sin(y),y)}\end{center} 5768Output : 5769\begin{center}{\tt pi/2}\end{center} 5770Input : 5771\begin{center}{\tt fMin(sin(x),x)}\end{center} 5772Or : 5773\begin{center}{\tt fMin(sin(x))}\end{center} 5774Or : 5775\begin{center}{\tt fMin(sin(y),y)}\end{center} 5776Output : 5777\begin{center}{\tt -pi/2}\end{center} 5778Input : 5779\begin{center}{\tt fMin(sin(x)\verb|^|2,x)}\end{center} 5780Output : 5781\begin{center}{\tt 0}\end{center} 5782 5783{\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: 5784\begin{itemize} 5785 \item objective function (an expression) 5786 \item list of constraints (equalities and inequalities) 5787 \item list of problem variables 5788 \item initial guess (must be a list of nonzero reals representing a feasible point) 5789 \item precision (optional), if not given the default epsilon value is used 5790 \item maximum number of iterations (optional) 5791\end{itemize} 5792The objective function does not need to be differentiable. 5793Both {\tt fMin} and {\tt fMax} return the optimal solution as a vector. Note that the actual optimal value of the objective is not returned. 5794 5795Although 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. 5796 5797For example, input : 5798\begin{center} 5799 \tt fMin((x-5)\verb|^|2+y\verb|^|2-25,[y>=x\verb|^|2],[x,y],[1,1]) 5800\end{center} 5801Output : 5802\begin{center} 5803 \tt [1.2347728624961,1.5246640219568] 5804\end{center} 5805Input : 5806\begin{center} 5807 \tt fMax((x-2)\verb|^|2+(y-1)\verb|^|2,[-.25x\verb|^|2-y\verb|^|2+1>=0,x-2y+1=0], 5808 [x,y],[.5,.75]) 5809\end{center} 5810Output : 5811\begin{center} 5812 \tt [-1.82287565553,-0.411437827766] 5813\end{center} 5814 5815\subsection{Table of values and graph : {\tt tablefunc} and {\tt plotfunc}}\index{tablefunc|textbf}\index{plotfunc} 5816{\tt tablefunc} is a special command that should be run from inside 5817the spreadsheet. It returns the evaluation of an expression $ex$ 5818depending on a variable $x$ for $x=x_0,\ x_0+h,....$~: 5819\begin{center} 5820{\tt tablefunc(ex,x,x\_0,h)} or {\tt tablefunc(ex,x)} 5821\end{center} 5822In the latter case, the default value for ${\tt x_0}$ 5823is the default minimum value of $x$ from the graphic configuration 5824and the default value for the step $h$ is 0.1 times the difference 5825between the default maximum and minimum values of $x$ (from the 5826graphic configuration).\\ 5827Example: type {\tt Alt+t} to open a spreadsheet if none are open. 5828Then select a cell of the spreadsheet (for example {\tt C0}) and to get 5829the table of {\tt "sinus"}, input in the command line of the spreadsheet : 5830\begin{center}{\tt tablefunc(sin(x),x)}\end{center} 5831This will fill two columns with the numeric value of {\tt x} and 5832{\tt sin(x)} : 5833\begin{itemize} 5834\item in the first column the variable {\tt x}, 5835the value of the step {\tt h} 5836(1.0), the minimum value of $x$ (-5.0), then a formula, for example 5837{\tt=C2+C\$1}, and the remaining rows 5838of the column is filled by pasting this formula. 5839\item in the next column the function {\tt sin(x)}, the word 5840"Tablefunc", a formula, 5841for example {\tt =evalf(subst(D\$0,C\$0,C2))}, and the remaining rows 5842of the column are filled by pasting this formula. 5843\end{itemize} 5844Hence the values of {\tt sin(x)} are on the same rows as the values 5845of {\tt x}. Note that the step and begin value and the expression 5846may be easily changed by modifying the correspondent cell. 5847 5848The graphic representation may be plotted with the {\tt plotfunc} command 5849(see \ref{sec:plotfunc}). 5850 5851\subsection{Derivative and partial derivative}\index{diff|textbf}\index{derive|textbf}\index{deriver|textbf} 5852\noindent{\tt diff} or {\tt derive} may have one or two arguments 5853to compute a first order derivative (or first order partial 5854derivative) of an expression or of a list of expressions, 5855or several arguments to compute 5856the $n$-th partial derivative of an expression or list of expressions. 5857 5858\subsubsection{Derivative and first order partial derivative : {\tt diff derive deriver}} 5859{\tt diff} (or {\tt derive}) takes two arguments : an expression and a variable 5860(resp. a vector of variable names) (see several variable functions in 5861 \ref{sec:plusvar}). If only one argument is provided, the derivative 5862is taken with respect to {\tt x}\\ 5863{\tt diff} (or {\tt derive}) returns the derivative (resp. a vector of 5864derivatives) of the expression with respect to the variable (resp. with respect 5865to each variable) given as second argument.\\ 5866Examples : 5867\begin{itemize} 5868\item Compute : 5869$$\frac {\partial (x y^2 z^3+x y z)}{\partial z}$$ 5870Input : 5871\begin{center}{\tt diff(x*y\verb|^|2*z\verb|^|3+x*y*z,z)}\end{center} 5872Output : 5873\begin{center}{\tt x*y\verb|^|2*3*z\verb|^|2+x*y}\end{center} 5874\item Compute the 3 first order partial derivatives of $x*y^2*z^3+x*y*z$.\\ 5875Input : 5876\begin{center}{\tt diff(x*y\verb|^|2*z\verb|^|3+x*y,[x,y,z])}\end{center} 5877Output : 5878\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} 5879% \item Compute : 5880% $$\frac {\partial^3 (x.y^2.z^3+x.y.z)}{\partial y\partial^2 z}$$ 5881% Input : 5882% \begin{center}{\tt diff(x*y \verb|^|2*z\verb|^|3+x*y*z,y,z\$2)}\end{center} 5883% Output : 5884% \begin{center}{\tt x*2*y*3*2*z}\end{center} 5885\end{itemize} 5886 5887\subsubsection{Derivative and $n$-th order 5888partial derivative : {\tt diff derive deriver}}\index{\$} 5889\noindent {\tt derive} (or {\tt diff}) may take more than two arguments : an 5890expression and the names of the derivation variables (each variable 5891may be followed by \$$n$ to indicate the number $n$ of derivations).\\ 5892{\tt diff} returns the partial derivative of the expression with respect to 5893the variables given after the first argument. 5894 5895The notation \$ is useful if you want to derive $k$ times with 5896respect to the same variable, instead of entering $k$ times the 5897same variable name, one enters the variable name followed by {\tt \$k}, 5898for example {\tt x\$3} instead of {\tt(x,x,x)}. 5899Each variable may be followed by a \$, for example 5900{\tt diff(exp(x*y),x\$3,y\$2,z)} is the same as 5901{\tt diff(exp(x*y),x,x,x,y,y,z)}\\ 5902{\bf Examples} 5903\begin{itemize} 5904\item Compute : 5905$$\frac {\partial^2 (x y^2 z^3+x y z)}{\partial x\partial z}$$ 5906Input : 5907\begin{center}{\tt diff(x*y\verb|^|2*z\verb|^|3+x*y*z,x,z)}\end{center} 5908Output : 5909\begin{center}{\tt y\verb|^|2*3*z\verb|^|2+y}\end{center} 5910\item Compute : 5911$$\frac {\partial^3 (x y^2 z^3+x y z)}{\partial x\partial^2 z}$$ 5912Input : 5913\begin{center}{\tt diff(x*y\verb|^|2*z\verb|^|3+x*y*z,x,z,z)}\end{center} 5914or : 5915\begin{center}{\tt diff(x*y\verb|^|2*z\verb|^|3+x*y*z,x,z\$2)}\end{center} 5916Output : 5917\begin{center}{\tt y\verb|^|2*3*2*z}\end{center} 5918\item Compute the third derivative of : 5919$$\frac{1}{x^2+2}$$ 5920Input : 5921\begin{center}{\tt normal(diff((1)/(x\verb|^|2+2),x,x,x))}\end{center} 5922or : 5923\begin{center}{\tt normal(diff((1)/(x\verb|^|2+2),x\$3))}\end{center} 5924Output : 5925\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} 5926\end{itemize} 5927{\bf Remark} 5928\begin{itemize} 5929\item 5930Note the difference between {\tt diff(f,x,y)} and {\tt diff(f,[x,y])} :\\ 5931{\tt diff}$(f,x,y)$ returns $\displaystyle \frac{\partial^2(f)}{\partial x\partial y}$ and\\ 5932{\tt diff}$(f,[x,y])$ returns 5933$\displaystyle[\frac{\partial(f)}{\partial x},\frac{\partial 5934 (f)}{\partial y}]$ 5935\item Never define a derivative function with {\tt 5936 f1(x):=diff(f(x),x)}. 5937Indeed, {\tt x} would mean two different things Xcas is unable to 5938deal with: the variable name to 5939define the $f_1$ function and the differentiation variable. 5940The right way to define a derivative is either with {\tt 5941 function\_diff} or: 5942\begin{center} 5943{\tt f1:=unapply(diff(f(x),x),x)} 5944\end{center} 5945\end{itemize} 5946 5947\section{Integration} 5948\subsection{Antiderivative and definite integral : {\tt integrate int Int}}\index{integrate}\index{Int}\index{int} 5949\noindent{\tt integrate} (or {\tt int}) computes a primitive 5950or a definite integral. A difference between the two 5951commands is that if you input {\tt quest()} just after the evaluation of 5952{\tt integrate}, the answer is written with the $\int$ symbol. 5953 5954{\tt integrate} (or {\tt int} or {\tt Int}) takes one, two or four arguments. 5955\begin{itemize} 5956\item with one or two arguments\\ 5957an expression or an expression and 5958the name of a variable (by default {\tt x}),\\ 5959{\tt integrate} (or {\tt int}) returns a primitive of the expression with 5960respect to the variable given as second argument.\\ 5961Input : 5962\begin{center}{\tt integrate(x\verb|^|2)}\end{center} 5963Output : 5964\begin{center}{\tt x\verb|^|3/3}\end{center} 5965Input : 5966\begin{center}{\tt integrate(t\verb|^|2,t)}\end{center} 5967Output : 5968\begin{center}{\tt t\verb|^|3/3}\end{center} 5969\item with four arguments :\\ 5970an expression, a name of a variable and the bounds of the definite integral,\\ 5971{\tt integrate} (or {\tt int}) returns the exact 5972value of the definite integral if the computation was successful or 5973an unevaluated integral otherwise.\\ 5974Input : 5975\begin{center}{\tt integrate(x\verb|^|2,x,1,2)}\end{center} 5976Output : 5977\begin{center}{\tt 7/3}\end{center} 5978Input : 5979\begin{center}{\tt integrate(1/(sin(x)+2),x,0,2*pi)}\end{center} 5980Output after simplification (with the {\tt simplify} command) : 5981\begin{center}{\tt 2*pi*sqrt(3)/3}\end{center} 5982\end{itemize} 5983 5984 5985{\tt Int} is the inert form of {\tt integrate}, it prevents evaluation 5986for example to avoid a symbolic computation that might not be 5987successful if you just want a numeric 5988evaluation.\\ 5989Input : 5990\begin{center}{\tt evalf(Int(exp(x\verb|^|2),x,0,1))}\end{center} 5991or : 5992\begin{center}{\tt evalf(int(exp(x\verb|^|2),x,0,1))}\end{center} 5993Output : 5994\begin{center}{\tt 1.46265174591}\end{center} 5995 5996{\bf Exercise 1}\\ 5997Let $$f(x)=\frac {x}{x^2-1}+\ln(\frac {x+1}{x-1})$$ 5998Find a primitive of $f$.\\ 5999Input : 6000\begin{center}{\tt int(x/(x\verb|^|2-1)+ln((x+1)/(x-1)))}\end{center} 6001Output : 6002\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} 6003Or define the function {\tt f}, input : 6004\begin{center}{\tt f(x):=x/(x\verb|^|2-1)+ln((x+1)/(x-1))}\end{center} 6005then input : 6006\begin{center}{\tt int(f(x))}\end{center} 6007Output of course the same result.\\ 6008{\bf Warning}\\ 6009For {\tt Xcas}, {\tt log} is the natural logarithm (like {\tt ln}), 6010as {\tt log10} is 10-basis logarithm 6011 6012{\bf Exercise 2}\\ 6013Compute : 6014$$\int \frac {2}{x^6+2 \cdot x^4+x^2} \ dx $$ 6015Input : 6016\begin{center}{\tt int(2/(x\verb|^|6+2*x\verb|^|4+x\verb|^|2))}\end{center} 6017Output : 6018\begin{center}{\tt 2*((3*x\verb|^|2+2)/(-(2*(x\verb|^|3+x)))+-3/2*atan(x))}\end{center} 6019 6020{\bf Exercise 3}\\ 6021Compute : 6022$$\int \frac {1}{\sin(x)+\sin(2 \cdot x )} \ dx $$ 6023Input : 6024\begin{center}{\tt integrate(1/(sin(x)+sin(2*x )))}\end{center} 6025Output : 6026\begin{center}{\tt (1/-3*log((tan(x/2))\verb|^|2-3)+1/12*log((tan(x/2))\verb|^|2))*2}\end{center} 6027 6028% \subsection{Primitive and definite integral : {\tt risch}}\index{risch} 6029% \noindent{\tt risch} compute a primitive or a definite integral using the 6030% Risch algorithm.\\ 6031% Input : 6032% \begin{center}{\tt risch(x\verb|^|2)}\end{center} 6033% Output : 6034% \begin{center}{\tt x\verb|^|3/3}\end{center} 6035% Input : 6036% \begin{center}{\tt risch(x\verb|^|2,x,0,1)}\end{center} 6037% Output : 6038% \begin{center}{\tt 1/3}\end{center} 6039% Input : 6040% \begin{center}{\tt risch(exp(-x\verb|^|2))}\end{center} 6041% Output : 6042% \begin{center}{\tt $\int$ exp(x\verb|^|2) dx}\end{center} 6043% that is to say that $\exp(-x^2)$ has no primitive expressing 6044% with usual functions. 6045 6046\subsection{Discrete summation: {\tt sum}}\index{sum|textbf} 6047\noindent{\tt sum} takes two or four arguments : 6048\begin{itemize} 6049\item four arguments\\ 6050an expression, the name of the variable (for 6051example {\tt n}), and the bounds (for example {\tt a} and {\tt b}).\\ 6052{\tt sum} returns the discrete sum of this expression with respect to 6053the variable from $a$ to $b$.\\ 6054Input : 6055\begin{center}{\tt sum(1,k,-2,n) }\end{center} 6056Output : 6057\begin{center}{\tt n+1+2}\end{center} 6058Input : 6059\begin{center}{\tt normal(sum(2*k-1,k,1,n))}\end{center} 6060Output : 6061\begin{center}{\tt n\verb|^|2}\end{center} 6062Input : 6063\begin{center}{\tt sum(1/(n\verb|^|2),n,1,10)}\end{center} 6064Output : 6065\begin{center}{\tt 1968329/1270080}\end{center} 6066Input : 6067\begin{center}{\tt sum(1/(n\verb|^|2),n,1,+(infinity)) }\end{center} 6068Output : 6069\begin{center}{\tt pi\verb|^|2/6}\end{center} 6070Input : 6071\begin{center}{\tt sum(1/(n\verb|^|3-n),n,2,10) }\end{center} 6072Output : 6073\begin{center}{\tt 27/110}\end{center} 6074Input : 6075\begin{center}{\tt sum(1/(n\verb|^|3-n),n,1,+(infinity)) }\end{center} 6076Output : 6077\begin{center}{\tt 1/4}\end{center} 6078This result comes from the decomposition of ${\tt 1/(n\verb|^|3-n)}$.\\ 6079Input : 6080\begin{center}{\tt partfrac(1/(n\verb|^|3-n)) }\end{center} 6081Output : 6082\begin{center}{\tt 1/(2*(n+1))-1/n+1/(2*(n-1))}\end{center} 6083Hence :\\ 6084$\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}$\\ 6085$\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})$\\ 6086$\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})$\\ 6087After simplification by $\sum_{n=2}^{N-2}$, it remains :\\ 6088 $\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)}$\\ 6089Therefore : 6090\begin{itemize} 6091\item for $N=10$ the sum is equal to : $1/4-1/220=27/110$ 6092\item for $N=+\infty$ the sum is equal to : $1/4$ because $\frac{1}{2N(N+1)}$ 6093approaches zero when $N$ approaches infinity. 6094\end{itemize} 6095 6096\item two arguments \\ 6097an expression of one variable (for example $f$) and the name of this 6098 variable (for example $x$).\\ 6099{\tt sum} returns the discrete antiderivative of this expression, i.e. 6100an expression $G$ such that $G_{|x=n+1}-G_{|x=n}=f_{|x=n}$.\\ 6101Input : 6102\begin{center}{\tt sum(1/(x*(x+1)),x)}\end{center} 6103Output : 6104\begin{center}{\tt -1/x}\end{center} 6105\end{itemize} 6106 6107\subsection{Riemann sum : {\tt sum\_riemann}}\index{sum\_riemann} 6108\noindent{\tt sum\_riemann} takes two arguments : an expression depending on 6109two variables and the list of the name of these two variables.\\ 6110{\tt sum\_riemann(expression(n,k),[n,k])} returns in the neighborhood of 6111 $ n=+\infty$ an equivalent of $\sum_{k=1}^n expression(n,k)$ (or of 6112$ \sum_{k=0}^{n-1} expression(n,k)$ or of $ \sum_{k=1}^{n-1} expression(n,k)$) 6113when the sum is looked on as a Riemann sum associated to a continuous 6114function defined on [0,1] or returns 6115{\tt "it is probably not a Riemann sum"} when the no result is found.\\ 6116{\bf Exercise 1}\\ 6117Suppose $\displaystyle S_n=\sum_{k=1}^n \frac{k^2}{n^3}$.\\ 6118Compute $\displaystyle \lim_{n \rightarrow +\infty} S_n$.\\ 6119Input : 6120\begin{center}{\tt sum\_riemann(k\verb|^|2/n\verb|^|3,[n,k])}\end{center} 6121Output : 6122\begin{center}{\tt 1/3}\end{center} 6123{\bf Exercise 2}\\ 6124Suppose $\displaystyle S_n=\sum_{k=1}^n \frac{k^3}{n^4}$.\\ 6125Compute $\displaystyle \lim_{n \rightarrow +\infty} S_n$.\\ 6126Input : 6127\begin{center}{\tt sum\_riemann(k\verb|^|3/n\verb|^|4,[n,k])}\end{center} 6128Output : 6129\begin{center}{\tt 1/4}\end{center} 6130{\bf Exercise 3}\\ 6131Compute 6132$\displaystyle \lim_{n \rightarrow +\infty}(\frac{1}{n+1}+\frac{1}{n+2}+...+\frac{1}{n+n})$.\\ 6133Input : 6134\begin{center}{\tt sum\_riemann(1/(n+k),[n,k])}\end{center} 6135Output : 6136\begin{center}{\tt log(2)}\end{center} 6137{\bf Exercise 4}\\ 6138Suppose $\displaystyle S_n=\sum_{k=1}^n \frac{32n^3}{16n^4-k^4}$.\\ 6139Compute $\displaystyle \lim_{n \rightarrow +\infty} S_n$.\\ 6140Input : 6141\begin{center}{\tt sum\_riemann(32*n\verb|^|3/(16*n\verb|^|4-k\verb|^|4),[n,k])}\end{center} 6142Output : 6143\begin{center}{\tt 2*atan(1/2)+log(3)}\end{center} 6144 6145\subsection{Integration by parts : {\tt ibpdv} and {\tt ibpu}} 6146\subsubsection{\tt ibpdv}\index{ibpdv} 6147\noindent{\tt ibpdv} is used to search the primitive of an expression written 6148as $u(x).v'(x)$.\\ 6149{\tt ibpdv} takes two arguments : 6150\begin{itemize} 6151\item an expression 6152 $u(x) * v'(x)$ and $v(x)$ (or a list of two expressions 6153$[F(x), u(x)*v'(x)]$ and $v(x)$), 6154\item or an expression $g(x)$ and $0$ (or a list of two expressions 6155$[F(x), g(x)]$ and $0$). 6156\end{itemize} 6157{\tt ibpdv} returns : 6158\begin{itemize} 6159\item if $v(x) \neq 0$, the list $[u(x) v(x),-v(x) u'(x)]$ (or 6160$[F(x)+u(x) v(x),-v(x) u'(x)]$), 6161\item if the second argument is zero, a primitive of the first argument 6162$g(x)$ (or $F(x)$+a primitive of $g(x)$) :\\ 6163hence, {\tt ibpdv(g(x),0)} returns a primitive {\tt G(x)} of {\tt g(x)} or \\ 6164{\tt ibpdv([F(x),g(x)],0)} returns {\tt F(x)+G(x)} where {\tt diff(G(x))=g(x)}. 6165\end{itemize} 6166Hence, {\tt ibpdv} returns the terms computed in an integration by parts, 6167with the possibility of doing several {\tt ibpdv}s successively.\\ 6168When the answer of {\tt ibpdv(u(x)*v'(x),v(x))} is computed, to obtain a 6169primitive of $u(x) v'(x)$, it remains to 6170compute the integral of the second term of this answer and then, to sum this 6171integral with the first term of this answer : to do this, just use 6172{\tt ibpdv} command with the answer as first argument and 6173a new $v(x)$ (or $0$ to terminate the integration) as second argument.\\ 6174Input : 6175\begin{center}{\tt ibpdv(ln(x),x) }\end{center} 6176Output : 6177\begin{center}{\tt [x ln(x),-1]}\end{center} 6178then 6179\begin{center}{\tt ibpdv([x ln(x),-1],0) }\end{center} 6180Output : 6181\begin{center}{\tt -x+x ln(x)}\end{center} 6182{\bf Remark}\\ 6183 When the first argument of {\tt ibpdv} is a list of two elements, {\tt ibpdv} 6184works only on the last element of this list and adds the integrated term to 6185the first element of this list. 6186(therefore it is possible to do several {\tt ibpdv}s successively).\\ 6187For example :\\ 6188{\tt ibpdv((log(x))\verb|^|2,x) = [x*(log(x))\verb|^|2,-(2*log(x))]}\\ 6189it remains to integrate {\tt -(2*log(x))}, the input :\\ 6190{\tt ibpdv(ans(),x)} or input :\\ 6191{\tt ibpdv([x*(log(x))\verb|^|2,-(2*log(x))],x)}\\ 6192Output :\\ 6193{\tt [x*(log(x))\verb|^|2+x*(-(2*log(x))),2]}\\ 6194and it remains to integrate {\tt 2}, hence input {\tt ibpdv(ans(),0)} or\\ 6195{\tt ibpdv([x*(log(x))\verb|^|2+x*(-(2*log(x))),2],0)}.\\ 6196Output : 6197{\tt x*(log(x))\verb|^|2+x*(-(2*log(x)))+2*x} 6198\subsubsection{\tt ibpu}\index{ibpu} 6199\noindent{\tt ibpu} is used to search the primitive of an expression written 6200as $u(x).v'(x)$ 6201{\tt ibpu} takes two arguments : 6202\begin{itemize} 6203\item an expression $u(x)*v'(x)$ and $u(x)$ (or a list of two expressions 6204$[F(x), u(x)*v'(x)]$ and $u(x)$), 6205\item an expression $g(x)$ and $0$ (or a list of two expressions $[F(x), g(x)]$ 6206and $0$). 6207\end{itemize} 6208{\tt ibpu} returns : 6209\begin{itemize} 6210\item if $u(x) \neq 0$, the list $[u(x)*v(x),-v(x)*u'(x)]$ 6211(or returns the list $[F(x)+u(x)*v(x),-v(x)*u'(x)]$), 6212\item if the second argument is zero, a primitive of the first argument $g(x)$ 6213(or $F(x)$+a primitive of $g(x)$):\\ 6214{\tt ibpu(g(x),0)} returns {\tt G(x)} where {\tt diff(G(x))=g(x)} or\\ 6215 {\tt ibpu([F(x),g(x)],0)} returns {\tt F(x)+G(x)} where {\tt diff(G(x))=g(x)}. 6216\end{itemize} 6217Hence, {\tt ibpu} returns the terms computed in an integration by parts, 6218with the possibility of doing several {\tt ibpu}s successively.\\ 6219When the answer of {\tt ibpu(u(x)*v'(x),u(x))} is computed, to obtain a 6220primitive of $u(x) v'(x)$, it remains to 6221compute the integral of the second term of this answer and then, to sum this 6222integral with the first term of this answer : to do this, just use 6223{\tt ibpu} command with the answer as first argument and 6224a new $u(x)$ (or $0$ to terminate the integration) as second argument.\\ 6225Input : 6226\begin{center}{\tt ibpu(ln(x),ln(x)) }\end{center} 6227Output : 6228\begin{center}{\tt [x*ln(x),-1]}\end{center} 6229then 6230\begin{center}{\tt ibpu([x*ln(x),-1],0) }\end{center} 6231Output : 6232\begin{center}{\tt -x+x*ln(x)}\end{center} 6233{\bf Remark}\\ 6234When the first argument of {\tt ibpu} is a list of two elements, {\tt ibpu} 6235works only on the last element of this list and adds the integrated term to 6236the first element of this list. 6237(therefore it is possible to do several {\tt ibpu}s successively).\\ 6238For example :\\ 6239{\tt ibpu((log(x))\verb|^|2,log(x)) = [x*(log(x))\verb|^|2,-(2*log(x))]}\\ 6240it remains to integrate {\tt -(2*log(x))}, hence input : \\ 6241{\tt ibpu(ans(),log(x))} 6242 or input :\\ 6243{\tt ibpu([x*(log(x))\verb|^|2,-(2*log(x))],log(x))}\\ 6244Output :\\ 6245{\tt [x*(log(x))\verb|^|2+x*(-(2*log(x))),2]}\\ 6246it remains to integrate {\tt 2}, hence input :\\ 6247{\tt ibpu(ans(),0)} or input :\\ 6248{\tt ibpu([x*(log(x))\verb|^|2+x*(-(2*log(x))),2],0)}.\\ 6249Output : 6250{\tt x*(log(x))\verb|^|2+x*(-(2*log(x)))+2*x} 6251 6252\subsection{Change of variables : {\tt subst}} 6253See the {\tt subst} command in the section \ref{sec:subst}. 6254 6255\section{Limits} 6256\subsection{Limits : {\tt limit}}\index{limit|textbf}\label{sec:limit} 6257 6258{\tt limit} computes the limit of an expression at a finite or infinite point. 6259It is also possible with an optional argument to compute a one-sided 6260limit (1 for the right limit and -1 for the left limit).\\ 6261{\tt limit} takes three or four arguments :\\ 6262an expression, the name of a variable (for example {\tt x}), the limit point 6263(for example {\tt a}) and an optional argument, by default {\tt 0}, to 6264indicate if the limit is unidirectional. 6265This argument is equal to 6266{\tt -1} for a left limit ({\tt x<a}) or is equal to {\tt 1} 6267for a right limit ({\tt x>a}) or is equal to {\tt 0} for a limit.\\ 6268{\tt limit} returns the limit of the expression when the variable (for example 6269{\tt x}) approaches the limit point (for example {\tt a}).\\ 6270{\bf Remark}\\ 6271It is also possible to put {\tt x=a} as argument instead of {\tt x,a}, hence : 6272{\tt limit} takes also as arguments an expression depending of a variable, 6273an equality (variable =value of the limit point) and perhaps 1 or -1 to 6274indicate the direction.\\ 6275Input : 6276\begin{center}{\tt limit(1/x,x,0,-1)}\end{center} 6277or : 6278\begin{center}{\tt limit(1/x,x=0,-1)}\end{center} 6279Output : 6280\begin{center}{\tt -(infinity)}\end{center} 6281Input : 6282\begin{center}{\tt limit(1/x,x,0,1)}\end{center} 6283or : 6284\begin{center}{\tt limit(1/x,x=0,1)}\end{center} 6285Output : 6286\begin{center}{\tt +(infinity)}\end{center} 6287Input : 6288\begin{center}{\tt limit(1/x,x,0,0)}\end{center} 6289or : 6290\begin{center}{\tt limit(1/x,x,0)}\end{center} 6291or : 6292\begin{center}{\tt limit(1/x,x=0)}\end{center} 6293Output : 6294\begin{center}{\tt infinity}\end{center} 6295Hence, {\tt abs(1/x)} approaches $+\infty$ when $x$ approaches $0$. 6296 6297{\bf Exercises} : 6298\begin{itemize} 6299\item Find for $n>2$, the limit when $x$ approaches $0$ of : 6300$$ \frac{n\tan(x)-\tan(nx)}{\sin(nx)-n\sin(x)}$$ 6301Input : 6302\begin{center}{\tt limit((n*tan(x)-tan(n*x))/(sin(n*x)-n*sin(x)),x=0)}\end{center} 6303Output : 6304\begin{center}{\tt 2 }\end{center} 6305\item Find the limit when $x$ approaches $+\infty$ of : 6306$$\sqrt{x+\sqrt{x+\sqrt x}}-\sqrt x$$ 6307Input : 6308\begin{center}{\tt limit(sqrt(x+sqrt(x+sqrt(x)))-sqrt(x),x=+infinity)}\end{center} 6309Output : 6310\begin{center}{\tt 1/2 }\end{center} 6311\item Find the limit when $x$ approaches 0 of : 6312$$\frac{\sqrt{1+x+x^2/2}-\exp(x/2)}{(1-\cos(x))\sin(x)}$$ 6313Input : 6314\begin{center}{\tt limit((sqrt(1+x+x\verb|^|2/2)-exp(x/2))/((1-cos(x))*sin(x)),x,0)}\end{center} 6315Output : 6316\begin{center}{\tt -1/6 }\end{center} 6317\end{itemize} 6318 6319{\bf Remark}\\ 6320To compute limits, it is better sometimes to quote the first argument.\\ 6321Input : 6322\begin{center}{\tt limit('(2*x-1)*exp(1/(x-1))',x=+infinity)}\end{center} 6323Note that the first argument is quoted, because it is better that 6324this argument is not simplified (i.e. not evaluated).\\ 6325Output : 6326\begin{center}{\tt +(infinity)}\end{center} 6327 6328\subsection{Integral and limit}\index{limit} \index{limit} 6329Just two examples :\\ 6330\begin{itemize} 6331\item Find the limit, when $a$ approaches $+\infty$, of : 6332$$ \int _2^a \frac {1}{x^2}\ dx$$ 6333Input : 6334\begin{center}{\tt limit(integrate(1/(x\verb|^|2),x,2,a),a,+(infinity))}\end{center} 6335Output (if {\tt a} is assigned then input {\tt purge(a)}) : 6336\begin{center}{\tt 1/2}\end{center} 6337\item Find the limit, when $a$ approaches $+\infty$, of : 6338$$ \int _2^a (\frac {x}{x^2-1}+\ln(\frac {x+1}{x-1}))\ dx$$ 6339Input : 6340\begin{center}{\tt limit(integrate(x/(x\verb|^|2-1)+log((x+1)/(x-1)),x,2,a),}\end{center} 6341\begin{center}{\tt a,+(infinity))}\end{center} 6342Output (if {\tt a} is assigned then input {\tt purge(a)}) : 6343\begin{center}{\tt +(infinity)}\end{center} 6344\end{itemize} 6345 6346\section{Rewriting transcendental and trigonometric expressions} 6347\subsection{Expand a transcendental and trigonometric expression : {\tt texpand tExpand}}\index{texpand|textbf}\index{tExpand|textbf} 6348\noindent{\tt texpand} or {\tt tExpand} takes as argument an 6349expression containing transcendental or trigonometric functions.\\ 6350{\tt texpand} or {\tt tExpand} expands these functions, like simultaneous 6351calling {\tt expexpand}, {\tt lnexpand} and {\tt trigexpand}, 6352for example, $\ln(x^n)$ becomes $\ n\ln(x)$, $\exp(nx)$ 6353becomes $\ \exp(x)^n$, $\sin(2x)$ becomes $\ 2\sin(x)\cos(x)$...\\ 6354{\bf Examples} :\\ 6355\begin{itemize} 6356\item 6357\begin{enumerate} 6358\item Expand $\cos(x+y)$.\\ 6359Input : 6360\begin{center}{\tt texpand(cos(x+y))}\end{center} 6361Output : 6362\begin{center}{\tt cos(x)*cos(y)-sin(x)*sin(y)}\end{center} 6363\item Expand $\cos(3x)$.\\ 6364Input : 6365\begin{center}{\tt texpand(cos(3*x))}\end{center} 6366Output : 6367\begin{center}{\tt 4*(cos(x))\verb|^| 3-3*cos(x)}\end{center} 6368\item Expand $\displaystyle \frac{\sin(3*x)+\sin(7*x)}{\sin(5*x)}$.\\ 6369Input : 6370\begin{center}{\tt texpand((sin(3*x)+sin(7*x))/sin(5*x))}\end{center} 6371Output 6372\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} 6373Output, after a simplification with {\tt normal(ans())} : 6374\begin{center}{\tt 4*(cos(x))\verb|^|2-2}\end{center} 6375\end{enumerate} 6376\item \begin{enumerate} 6377\item Expand $\exp(x+y)$.\\ 6378Input : 6379\begin{center}{\tt texpand(exp(x+y))}\end{center} 6380Output : 6381\begin{center}{\tt exp(x)*exp(y)}\end{center} 6382\item Expand $\ln(x\times y)$.\\ 6383Input : 6384\begin{center}{\tt texpand(log(x*y))}\end{center} 6385Output : 6386\begin{center}{\tt log(x)+log(y)}\end{center} 6387\item Expand $\ln(x^n)$.\\ 6388Input : 6389\begin{center}{\tt texpand(ln(x\verb|^|n))}\end{center} 6390Output : 6391\begin{center}{\tt n*ln(x)}\end{center} 6392\item Expand $\ln((e^2)+\exp(2*\ln(2))+\exp(\ln(3)+\ln(2)))$.\\ 6393Input : 6394\begin{center}{\tt texpand(log(e\verb|^|2)+exp(2*log(2))+exp(log(3)+log(2)))}\end{center} 6395Output : 6396\begin{center}{\tt 6+3*2}\end{center} 6397Or input : 6398\begin{center}{\tt texpand(log(e\verb|^|2)+exp(2*log(2)))+ lncollect(exp(log(3)+log(2)))}\end{center} 6399Output : 6400\begin{center}{\tt 12}\end{center} 6401\end{enumerate} 6402\item 6403Expand $\exp(x+y)+\cos(x+y)+\ln(3x^2)$.\\ 6404Input : 6405\begin{center}{\tt texpand(exp(x+y)+cos(x+y)+ln(3*x\verb|^|2))}\end{center} 6406Output : 6407\begin{center}{\tt cos(x)*cos(y)-sin(x)*sin(y)+exp(x)*exp(y)+ ln(3)+2*ln(x)}\end{center} 6408\end{itemize} 6409 6410\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} 6411\noindent{\tt combine} takes two arguments : an expression and 6412the name of a function or class of functions 6413{\tt exp,log,ln, sin,cos,trig}.\\ 6414Whenever possible, {\tt combine} put together subexpressions corresponding 6415to the second argument: 6416\begin{itemize} 6417\item {\tt combine(expr,ln)} or {\tt combine(expr,log)} gives the same result 6418as {\tt lncollect(expr)} 6419\item 6420{\tt combine(expr,trig)} or {\tt combine(expr,sin)} or {\tt combine(expr,cos)} 6421gives the same result as {\tt tcollect(expr)}. 6422\end{itemize} 6423Input : 6424\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),exp)}\end{center} 6425Output : 6426\begin{center}{\tt exp(x+y)+sin(x)*cos(x)+ln(x)+ln(y)}\end{center} 6427Input : 6428\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),trig)}\end{center} 6429or 6430\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),sin)}\end{center} 6431or 6432\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),cos)}\end{center} 6433Output : 6434\begin{center}{\tt exp(y)*exp(x)+(sin(2*x))/2+ln(x)+ln(y)}\end{center} 6435Input : 6436\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),ln)}\end{center} 6437or 6438\begin{center}{\tt combine(exp(x)*exp(y)+sin(x)*cos(x)+ln(x)+ln(y),log)}\end{center} 6439Output : 6440\begin{center}{\tt exp(x)*exp(y)+sin(x)*cos(x)+ln(x*y)}\end{center} 6441 6442\section{Trigonometry} 6443\subsection{Trigonometric functions}\label{sec:trigo} 6444\begin{itemize} 6445\item {\tt sin} \index{sin} is the sine function, 6446\item {\tt cos} \index{cos} is the cosine function, 6447\item {\tt tan} \index{tan} is the tangent function ({\tt tan(x)= 6448 sin(x)/cos(x)}), 6449\item 6450{\tt cot} \index{cot|textbf} is the cotangent function ({\tt cot(x)= 6451 cos(x)/sin(x)}), 6452\item 6453{\tt sec} \index{sec|textbf} is the secant function ({\tt sec(x)= 6454 1/cos(x)}), 6455\item 6456{\tt csc} \index{csc|textbf} is the cosecant function ({\tt csc(x) = 6457 1/sin(x)}), 6458\item 6459{\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} 6460are the inverse trigonometric functions. The latter are defined by: 6461\begin{enumerate} 6462\item {\tt asec(x) = acos(1/x)}, 6463\item 6464{\tt acsc(x) = asin(1/x)}, 6465\item 6466{\tt acot(x) = atan(1/x)}. 6467\end{enumerate} 6468\end{itemize} 6469 6470 6471\subsection{Expand a trigonometric expression : {\tt trigexpand}}\index{trigexpand} 6472\noindent{\tt trigexpand} takes as argument an expression 6473containing trigonometric functions.\\ 6474{\tt trigexpand} expands sums, differences and products by an integer 6475inside the trigonometric functions \\ 6476Input : 6477\begin{center}{\tt trigexpand(cos(x+y))}\end{center} 6478Output : 6479\begin{center}{\tt cos(x)*cos(y)-sin(x)*sin(y)}\end{center} 6480 6481 6482\subsection{Linearize a trigonometric expression : {\tt tlin}}\index{tlin} 6483\noindent{\tt tlin} takes as argument an expression 6484containing trigonometric functions.\\ 6485{\tt tlin} linearizes products and integer powers of the trigonometric 6486functions (e.g. in terms of $\sin(n*x)$ and 6487$\cos(n*x)$)\\ 6488{\bf Examples} 6489\begin{itemize} 6490\item Linearize $\cos(x)*\cos(y)$.\\ 6491Input : 6492\begin{center}{\tt tlin(cos(x)*cos(y))}\end{center} 6493Output : 6494\begin{center}{\tt 1/2*cos(x-y)+1/2*cos(x+y)}\end{center} 6495\item Linearize $\cos(x)^3$.\\ 6496Input : 6497\begin{center}{\tt tlin(cos(x)\verb|^|3)}\end{center} 6498Output : 6499\begin{center}{\tt 3/4*cos(x)+1/4*cos(3*x)}\end{center} 6500\item Linearize $4\cos(x)^2-2$.\\ 6501Input : 6502\begin{center}{\tt tlin(4*cos(x)\verb|^|2-2)}\end{center} 6503Output : 6504\begin{center}{\tt 2*cos(2*x)}\end{center} 6505\end{itemize} 6506 6507\subsection{Put together sine and cosine of the same angle : {\tt tcollect tCollect}}\index{tcollect}\index{tCollect} 6508\noindent{\tt tcollect} or {\tt tCollect} takes as argument 6509an expression containing trigonometric functions.\\ 6510{\tt tcollect} first linearizes this expression 6511(e.g. in terms of $\sin(n*x)$ and $\cos(n*x)$), 6512then, puts together sine and cosine of the same angle.\\ 6513Input : 6514\begin{center}{\tt tcollect(sin(x)+cos(x))}\end{center} 6515Output : 6516\begin{center}{\tt sqrt(2)*cos(x-pi/4)}\end{center} 6517Input : 6518\begin{center}{\tt tcollect(2*sin(x)*cos(x)+cos(2*x))}\end{center} 6519Output : 6520\begin{center}{\tt sqrt(2)*cos(2*x-pi/4)}\end{center} 6521 6522\subsection{Simplify : {\tt simplify}}\index{simplify} 6523\noindent{\tt simplify} simplifies the expression.\\ 6524As with all automatic simplifications, do not expect miracles, 6525you will have to use specific rewriting rules if it does not work.\\ 6526Input : 6527\begin{center}{\tt simplify((sin(3*x)+sin(7*x))/sin(5*x))}\end{center} 6528Output : 6529\begin{center}{\tt 4*(cos(x))\verb|^|2-2}\end{center} 6530{\bf Warning} {\tt simplify} is more efficient in {\tt radian} mode (check 6531{\tt radian} in the {\tt cas} configuration 6532 or input {\tt angle\_radian:=1}). 6533 6534 6535\subsection{Simplify trigonometric expressions : {\tt trigsimplify}}\index{trigsimplify} 6536\noindent{\tt trigsimplify} simplifies trigonometric expressions 6537by combining {\tt simplify}, {\tt texpand}, {\tt tlin}, {\tt tcollect}, {\tt trigsin}, {\tt trigcos} and {\tt trigtan} commands in a certain order.\\ 6538Input : 6539\begin{center}{\tt trigsimplify((sin(x+y)-sin(x-y))/(cos(x+y)+cos(x-y)))}\end{center} 6540Output : 6541\begin{center}{\tt tan(4*x/3)}\end{center} 6542Input : 6543\begin{center}{\tt trigsimplify(1-1/4*sin(2a)\verb|^|2-sin(b)\verb|^|2-cos(a)\verb|^|4)}\end{center} 6544Output : 6545\begin{center}{\tt sin(a)\verb|^|2-sin(b)\verb|^|2}\end{center} 6546 6547\subsection{Transform arccos into arcsin : {\tt acos2asin}}\index{acos2asin} 6548\noindent{\tt acos2asin} takes as argument an expression containing 6549inverse trigonometric functions.\\ 6550{\tt acos2asin} replaces $\arccos(x)$ by 6551$\displaystyle \frac{\pi}{2}-\arcsin(x)$ in this expression.\\ 6552Input : 6553\begin{center}{\tt acos2asin(acos(x)+asin(x))}\end{center} 6554Output after simplification : 6555\begin{center}{\tt pi/2}\end{center} 6556 6557\subsection{Transform arccos into arctan : {\tt acos2atan}}\index{acos2atan} 6558\noindent{\tt acos2atan} takes as argument an expression containing 6559inverse trigonometric functions.\\ 6560{\tt acos2atan} replaces $\arccos(x)$ by 6561$\displaystyle \frac{\pi}{2}-\arctan(\frac{x}{\sqrt{1-x^2}})$ in this 6562expression.\\ 6563Input : 6564\begin{center}{\tt acos2atan(acos(x))}\end{center} 6565Output : 6566\begin{center}{\tt pi/2-atan(x/sqrt(1-x\verb|^|2))}\end{center} 6567 6568\subsection{Transform arcsin into arccos : {\tt asin2acos}}\index{asin2acos} 6569\noindent{\tt asin2acos} takes as argument an 6570expression containing inverse trigonometric functions.\\ 6571{\tt asin2acos} replaces $\arcsin(x)$ by 6572$\displaystyle \frac{\pi}{2}-\arccos(x)$ in this expression.\\ 6573Input : 6574\begin{center}{\tt asin2acos(acos(x)+asin(x))}\end{center} 6575Output after simplification : 6576\begin{center}{\tt pi/2}\end{center} 6577 6578\subsection{Transform arcsin into arctan : {\tt asin2atan}}\index{asin2atan} 6579\noindent{\tt asin2atan} takes as argument an expression containing 6580inverse trigonometric functions.\\ 6581{\tt asin2atan} replaces $\arcsin(x)$ by 6582$\displaystyle \arctan(\frac{x}{\sqrt{1-x2}})$ in this expression.\\ 6583Input : 6584\begin{center}{\tt asin2atan(asin(x))}\end{center} 6585Output : 6586\begin{center}{\tt atan(x/sqrt(1-x\verb|^|2))}\end{center} 6587 6588\subsection{Transform arctan into arcsin : {\tt atan2asin}}\index{atan2asin} 6589\noindent{\tt atan2asin} takes as argument an expression 6590containing inverse trigonometric functions. 6591{\tt atan2asin} replaces $\arctan(x)$ by 6592$\displaystyle \arcsin(\frac{x}{\sqrt{1+x2}})$ in this expression.\\ 6593Input : 6594\begin{center}{\tt atan2asin(atan(x))}\end{center} 6595Output : 6596\begin{center}{\tt asin(x/sqrt(1+x\verb|^|2))}\end{center} 6597 6598\subsection{Transform arctan into arccos : {\tt atan2acos}}\index{atan2acos} 6599\noindent{\tt atan2acos} takes as argument an expression containing 6600inverse trigonometric functions.\\ 6601{\tt atan2acos} replaces $\arctan(x)$ by 6602$\displaystyle \frac{\pi}{2}-\arccos(\frac{x}{\sqrt{1+x2}})$ 6603in this expression.\\ 6604Input : 6605\begin{center}{\tt atan2acos(atan(x))}\end{center} 6606Output : 6607\begin{center}{\tt pi/2-acos(x/sqrt(1+x\verb|^|2))}\end{center} 6608 6609\subsection{Transform complex exponentials into sin and cos : {\tt sincos exp2trig}}\index{sincos}\index{exp2trig} 6610\noindent{\tt sincos} or {\tt exp2trig} takes as argument an expression 6611containing complex exponentials.\\ 6612{\tt sincos} or {\tt exp2trig} rewrites this expression in terms of 6613$\sin$ and $\cos$.\\ 6614Input : 6615\begin{center}{\tt sincos(exp(i*x))}\end{center} 6616Output : 6617\begin{center}{\tt cos(x)+(i)*sin(x)}\end{center} 6618Input : 6619\begin{center}{\tt exp2trig(exp(-i*x))}\end{center} 6620Output : 6621\begin{center}{\tt cos(x)+(i)*(-(sin(x)))}\end{center} 6622Input : 6623\begin{center}{\tt simplify(sincos(((i)*(exp((i)*x))\verb|^|2-i)/(2*exp((i)*x))))}\end{center} 6624or : 6625\begin{center}{\tt simplify(exp2trig(((i)*(exp((i)*x))\verb|^|2-i)/(2*exp((i)*x))))}\end{center} 6626Output : 6627\begin{center}{\tt -sin(x)}\end{center} 6628 6629\subsection{Transform tan(x) into sin(x)/cos(x) : {\tt tan2sincos}}\index{tan2sincos} 6630\noindent{\tt tan2sincos} takes as argument an expression containing 6631trigonometric functions.\\ 6632{\tt tan2sincos} replaces $\tan(x)$ by 6633 $\displaystyle \frac{\sin(x)}{\cos(x)}$ in this expression.\\ 6634Input : 6635\begin{center}{\tt tan2sincos(tan(2*x))}\end{center} 6636Output : 6637\begin{center}{\tt sin(2*x)/cos(2*x)}\end{center} 6638 6639\subsection{Rewrite tan(x) with sin(2x) and cos(2x) : {\tt tan2sincos2}}\index{tan2sincos2} 6640\noindent{\tt tan2sincos2} takes as argument an expression containing 6641trigonometric functions.\\ 6642{\tt tan2sincos2} replaces $\tan(x)$ by 6643$\displaystyle \frac{\sin(2x)}{1+\cos(2x)}$ in this expression.\\ 6644Input : 6645\begin{center}{\tt tan2sincos2(tan(x))}\end{center} 6646Output : 6647\begin{center}{\tt sin(2*x)/(1+cos(2*x))}\end{center} 6648 6649\subsection{Rewrite tan(x) with cos(2x) and sin(2x) : {\tt tan2cossin2}}\index{tan2cossin2} 6650\noindent{\tt tan2cossin2} takes as argument an expression 6651containing trigonometric functions.\\ 6652{\tt tan2cossin2} replaces $\tan(x)$ by 6653$\displaystyle \frac{1-\cos(2x)}{\sin(2x)}$ , in this expression.\\ 6654Input : 6655\begin{center}{\tt tan2cossin2(tan(x))}\end{center} 6656Output : 6657\begin{center}{\tt (1-cos(2*x))/sin(2*x)}\end{center} 6658 6659\subsection{Rewrite sin, cos, tan in terms of tan(x/2) : {\tt halftan}}\index{halftan} 6660\noindent{\tt halftan} takes as argument an expression 6661containing trigonometric functions.\\ 6662{\tt halftan} rewrites $\sin(x),\ \cos(x)$ and $ \tan(x)$ 6663 in terms of $\tan(\frac{x}{2})$.\\ 6664Input : 6665\begin{center}{\tt halftan(sin(2*x)/(1+cos(2*x)))}\end{center} 6666Output : 6667\begin{center}{\tt 2*tan(2*x/2)/((tan(2*x/2))\verb|^|2+1)/}\end{center} 6668\begin{center}{\tt (1+(1-(tan(2*x/2))\verb|^|2)/((tan(2*x/2))\verb|^|2+1))}\end{center} 6669Output, after simplification with {\tt normal(ans())} : 6670\begin{center}{\tt tan(x)}\end{center} 6671Input : 6672\begin{center}{\tt halftan(sin(x)\verb|^|2+cos(x)\verb|^|2)}\end{center} 6673Output : 6674\begin{center}{\tt (2*tan(x/2)/((tan(x/2))\verb|^|2+1))\verb|^|2+}\end{center} 6675\begin{center}{\tt ((1-(tan(x/2))\verb|^|2)/((tan(x/2))\verb|^|2+1))\verb|^|2}\end{center} 6676Output, after simplification with {\tt normal(ans())} : 6677\begin{center}{\tt 1}\end{center} 6678 6679\subsection{Rewrite trigonometric functions as function of tan(x/2) 6680and hyperbolic functions as function of exp(x): 6681{\tt halftan\_hyp2exp}}\index{halftan\_hyp2exp} 6682\noindent{\tt halftan\_hyp2exp} takes as argument a trigonometric and 6683hyperbolic expression.\\ 6684{\tt halftan\_hyp2exp} rewrites $\sin(x),\ \cos(x), \tan(x)$ 6685 in terms of $\tan(\frac{x}{2})$ 6686and $\sinh(x),\ \cosh(x), \tanh(x)$ in terms of $\exp(x)$.\\ 6687Input : 6688\begin{center}{\tt halftan\_hyp2exp(tan(x)+tanh(x))}\end{center} 6689Output : 6690\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} 6691Input : 6692\begin{center}{\tt halftan\_hyp2exp(sin(x)\verb|^|2+cos(x)\verb|^|2-sinh(x)\verb|^|2+cosh(x)\verb|^|2)}\end{center} 6693Output, after simplification with {\tt normal(ans())} : 6694\begin{center}{\tt 2}\end{center} 6695 6696\subsection{Transform inverse trigonometric functions into logarithms : {\tt atrig2ln}}\index{atrig2ln} 6697\noindent{\tt atrig2ln} takes as argument an expression containing 6698inverse trigonometric functions.\\ 6699{\tt atrig2ln} rewrites these functions with complex logarithms.\\ 6700Input : 6701\begin{center}{\tt atrig2ln(asin(x))}\end{center} 6702Output : 6703\begin{center}{\tt i*log(x+sqrt(x\verb|^|2-1))+pi/2}\end{center} 6704 6705\subsection{Transform trigonometric functions into complex exponentials : {\tt trig2exp}}\index{trig2exp} 6706\noindent{\tt trig2exp} takes as argument an expression containing 6707trigonometric functions.\\ 6708{\tt trig2exp} rewrites the trigonometric functions with complex exponentials 6709({\sc without} linearization).\\ 6710Input : 6711\begin{center}{\tt trig2exp(tan(x))}\end{center} 6712Output : 6713\begin{center}{\tt ((exp((i)*x))\verb|^|2-1)/((i)*((exp((i)*x))\verb|^|2+1))}\end{center} 6714Input : 6715\begin{center}{\tt trig2exp(sin(x))}\end{center} 6716Output : 6717\begin{center}{\tt (exp((i)*x)-1/(exp((i)*x)))/(2*i)}\end{center} 6718 6719\subsection{Simplify and express preferentially with sine : {\tt trigsin}}\index{trigsin} 6720\noindent{\tt trigsin} takes as argument an expression 6721containing trigonometric functions.\\ 6722{\tt trigsin} simplify this expression with the formula :\\ 6723$\sin(x)^2+\cos(x)^2=1$, $\displaystyle \tan(x)=\frac{\sin(x)}{\cos(x)}$ and 6724tries to rewrite the expression only with sine.\\ 6725Input : 6726\begin{center}{\tt trigsin(sin(x)\verb|^|4+cos(x)\verb|^|2+1)}\end{center} 6727Output : 6728\begin{center}{\tt sin(x)\verb|^|4-sin(x)\verb|^|2+2}\end{center} 6729 6730\subsection{Simplify and express preferentially with cosine : {\tt trigcos}}\index{trigcos} 6731\noindent{\tt trigcos} takes as argument an expression 6732containing trigonometric functions.\\ 6733{\tt trigcos} simplifies this expression with the formula :\\ 6734$\sin(x)^2+\cos(x)^2=1$, $\displaystyle \tan(x)=\frac{\sin(x)}{\cos(x)}$ and 6735tries to rewrite the expression only with cosine.\\ 6736Input : 6737\begin{center}{\tt trigcos(sin(x)\verb|^|4+cos(x)\verb|^|2+1)}\end{center} 6738Output : 6739\begin{center}{\tt cos(x)\verb|^|4-cos(x)\verb|^|2+2}\end{center} 6740 6741\subsection{Simplify and express preferentially with tangents : {\tt trigtan}}\index{trigtan} 6742\noindent{\tt trigtan} takes as argument an expression 6743containing trigonometric functions.\\ 6744{\tt trigtan} simplifies this expression with the formula :\\ 6745$\sin(x)^2+\cos(x)^2=1$, $\displaystyle \tan(x)=\frac{\sin(x)}{\cos(x)}$ and 6746tries to rewrite the expression only with tangents.\\ 6747Input : 6748\begin{center}{\tt trigtan(sin(x)\verb|^|4+cos(x)\verb|^|2+1)}\end{center} 6749Output : 6750\begin{center}{\tt((tan(x))\verb|^|2/(1+(tan(x))\verb|^|2))\verb|^|2+1/(1+(tan(x)\verb|^|2)+1}\end{center} 6751Output, after simplification with {\tt normal} : 6752\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} 6753 6754\subsection{Rewrite an expression with different options : {\tt convert convertir}}\index{convert|textbf}\index{convertir|textbf}\index{sin@{\sl sin}|textbf}\index{cos@{\sl cos}|textbf}\index{sincos@{\sl sincos}|textbf}\index{exp@{\sl exp}|textbf}\index{tan@{\sl tan}|textbf}\index{ln@{\sl ln}|textbf}\index{expln@{\sl expln}|textbf}\index{string@{\sl string}|textbf}\index{matrix@{\sl matrix}|textbf}\index{polynom@{\sl polynom}}\index{parfrac@{\sl parfrac}|textbf}\index{partfrac@{\sl partfrac}|textbf}\index{fullparfrac@{\sl fullparfrac}|textbf}\label{sec:convert} 6755\noindent{\tt convert} takes two arguments an expression and an option.\\ 6756{\tt convert} rewrites this expression applying rules depending 6757on the option. Valid options are : 6758\begin{itemize} 6759\item{\tt sin} converts an expression like {\tt trigsin}. 6760\item{\tt cos} converts an expression like {\tt trigcos}. 6761\item{\tt sincos} converts an expression like {\tt sincos}. 6762\item{\tt trig} converts an expression like {\tt sincos}. 6763\item{\tt tan} converts an expression like {\tt halftan}. 6764\item{\tt exp} converts an expression like {\tt trig2exp}. 6765\item{\tt ln} converts an expression like {\tt trig2exp}. 6766\item{\tt expln} converts an expression like {\tt trig2exp}. 6767\item{\tt string} converts an expression into a string. 6768\item{\tt matrix} converts a list of lists into a matrix. 6769\item{\tt polynom} converts a Taylor series into a polynomial 6770by removing the remainder (cf \ref{sec:convertpoly}). 6771\item{\tt parfrac} or {\tt partfrac} or {\tt fullparfrac} converts a rational 6772fraction into its partial fraction decomposition (\ref{sec:convertparf}). 6773\end{itemize} 6774{\tt convert} can also : 6775\begin{itemize} 6776\item convert units, for example 6777{\tt convert(1000\_g,\_kg)=1.0\_kg} (cf \ref{sec:convertunit}). 6778\item write a real as a continued fraction : 6779{\tt convert(a,confrac,'fc')} writes {\tt a} as a continued fraction 6780stored in {\tt fc}. Do not forget to quote the last argument if it 6781was assigned.\\ 6782For example, {\tt convert(1.2,confrac,'fc')=[1,5]} and {\tt fc} contains the 6783continued fraction equal to 1.2 (cf \ref{sec:convertdfc}). 6784\item transform an integer into the list of its digits in a 6785base, beginning with the units digit (and reciprocally) 6786\begin{itemize} 6787\item 6788{\tt convert(n,base,b)} transforms the integer {\tt n} into the list of its 6789digits in base {\tt b} beginning with the units digit.\\ For example, 6790{\tt convert(123,base,10)=[3,2,1]} and reciprocally 6791\item 6792{\tt convert(l,base,b)} transforms the list {\tt l} into the integer {\tt n} 6793which has {\tt l} as list of its 6794digits in base {\tt b} beginning with the units digit.\\ For example, 6795{\tt convert([3,2,1],base,10)=123} (cf \ref{sec:convertbase}). 6796\end{itemize} 6797\end{itemize} 6798 6799\section{Fourier transformation} 6800\subsection{Fourier coefficients : {\tt fourier\_an} and {\tt fourier\_bn} or {\tt fourier\_cn}} \index{integer} 6801Let $f$ be a $T$-periodic continuous functions on 6802$\mathbb{R}$ except maybe at a finite number of points. 6803One can prove that if $f$ is continuous at $x$, then; 6804\begin{eqnarray*} 6805f(x)&=&\frac{a_0}{2}+\sum _{n=1}^{+\infty} a_n \cos(\frac{2\pi 6806 nx}{T})+b_n \sin(\frac{2\pi nx}{T}) \\ 6807 &=&\sum _{n=-\infty}^{+\infty} c_n e^{\frac{2i\pi nx}{T}} 6808\end{eqnarray*} 6809where the coefficients $a_n,\ b_n$, $n\in N$, (or $c_n$, $n \in Z$) are the 6810Fourier coefficients of $f$. 6811The commands{\tt fourier\_an} and {\tt fourier\_bn} or {\tt fourier\_cn} 6812compute these coefficients. 6813 6814\subsubsection{\tt fourier\_an}\index{fourier\_an}\label{sec:fourier_an} 6815\noindent{\tt fourier\_an} takes four or five arguments : an expression $expr$ 6816depending on a variable, the name of this variable (for example $x$), the 6817period $T$, an integer $n$ and a real $a$ (by default $a=0$).\\ 6818{\tt fourier\_an(expr,x,T,n,a)} returns the Fourier coefficient $a_n$ of a 6819function $f$ of 6820variable $x$ defined on $[a,a+T)$ by $f(x)=expr$ and such that 6821$f$ is periodic of period $T$: 6822$$\displaystyle a_n=\frac{2}{T}\int_a^{a+T}f(x)\cos(\frac{2\pi nx }{T})dx$$ 6823To simplify the computations, one should input {\tt assume(n,integer)} 6824before calling {\tt fourier\_an} to specify 6825that $n$ is an integer.\\ 6826{\bf Example} Let the function $f$, of period $T=2$, defined on $[-1,1)$ by 6827$f(x)=x^2$.\\ 6828Input, to have the coefficient $a_0$ : 6829\begin{center}{\tt fourier\_an(x\verb|^|2,x,2,0,-1)}\end{center} 6830Output : 6831\begin{center}{\tt 1/3}\end{center} 6832Input, to have the coefficient $a_n$ ($n\neq 0$) : 6833 \begin{center}{\tt assume(n,integer);fourier\_an(x\verb|^|2,x,2,n,-1)}\end{center} 6834Output : 6835\begin{center}{\tt 4*(-1)\verb|^|n/(pi\verb|^|2*n\verb|^|2)}\end{center} 6836 6837\subsubsection{\tt fourier\_bn}\index{fourier\_bn}\label{sec:fourier_bn} 6838\noindent{\tt fourier\_bn} takes four or five arguments : an expression $expr$ 6839depending on a variable, the name of this variable (for example $x$), the 6840period $T$, an integer $n$ and a real $a$ (by default $a=0$).\\ 6841{\tt fourier\_bn(expr,x,T,n,a)} returns the Fourier coefficient $b_n$ of a 6842function $f$ of variable $x$ defined on $[a,a+T)$ by $f(x)=expr$ and periodic 6843of period $T$: 6844$$\displaystyle b_n=\frac{2}{T}\int_a^{a+T}f(x)\sin(\frac{2\pi nx}{T})dx$$ 6845To simplify the computations, one should input {\tt assume(n,integer)} 6846before calling {\tt fourier\_bn} to specify that $n$ is an integer.\\ 6847{\bf Examples} 6848\begin{itemize} 6849\item Let the function $f$, of period $T=2$, defined on $[-1,1)$ by 6850$f(x)=x^2$.\\ 6851Input, to have the coefficient $b_n$ ($n\neq 0$) : 6852 \begin{center}{\tt assume(n,integer);fourier\_bn(x\verb|^|2,x,2,n,-1)}\end{center} 6853Output : 6854\begin{center}{\tt 0}\end{center} 6855 6856\item Let the function $f$, of period $T=2$, defined on $[-1,1)$ by 6857$f(x)=x^3$.\\ 6858Input, to have the coefficient $b_1$ : 6859 \begin{center}{\tt fourier\_bn(x\verb|^|3,x,2,1,-1)}\end{center} 6860Output : 6861\begin{center}{\tt (2*pi\verb|^|2-12)/pi\verb|^|3}\end{center} 6862\end{itemize} 6863 6864\subsubsection{\tt fourier\_cn}\index{fourier\_cn}\label{sec:fourier_cn} 6865\noindent{\tt fourier\_cn} takes four or five arguments : an expression $expr$ 6866depending of a variable, the name of this variable (for example $x$), the 6867period $T$, an integer $n$ and a real $a$ (by default $a=0$).\\ 6868{\tt fourier\_cn(expr,x,T,n,a)} returns the Fourier coefficient $c_n$ of a 6869function$f$ of variable $x$ defined on $[a,a+T)$ by $f(x)=expr$ and periodic 6870of period $T$: 6871$$\displaystyle c_n=\frac{1}{T}\int_a^{a+T}f(x)e^{\frac{-2i\pi nx}{T}}dx$$ 6872To simplify the computations, one should 6873input {\tt assume(n,integer)} before calling {\tt fourier\_cn} 6874to specify that $n$ is an integer.\\ 6875{\bf Examples} 6876\begin{itemize} 6877\item Find the Fourier coefficients $c_n$ of the periodic function $f$ of 6878period $2$ and defined on $[-1,1)$ by $ f(x)=x^2$.\\ 6879Input, to have $c_0$ : 6880\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2,0,-1)}\end{center} 6881Output: 6882\begin{center}{\tt 1/3}\end{center} 6883Input, to have $c_n$ : 6884\begin{center}{\tt assume(n,integer)}\end{center} 6885\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2,n,-1)}\end{center} 6886Output: 6887\begin{center}{\tt 2*(-1)\verb|^|n/(pi\verb|^|2*n\verb|^|2)}\end{center} 6888 6889\item Find the Fourier coefficients $c_n$ of the periodic function $f$, of 6890period $2$, and defined on $[0,2)$ by $ f(x)=x^2$.\\ 6891Input, to have $c_0$ : 6892\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2,0)}\end{center} 6893Output: 6894\begin{center}{\tt 4/3}\end{center} 6895Input, to have $c_n$ : 6896\begin{center}{\tt assume(n,integer)}\end{center} 6897\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2,n)}\end{center} 6898Output: 6899\begin{center}{\tt ((2*i)*pi*n+2)/(pi\verb|^|2*n\verb|^|2)}\end{center} 6900 6901\item Find the Fourier coefficients $c_n$ of the periodic function $f$ of 6902period $2\pi$ and defined on $[0,2\pi)$ by $ f(x)=x^2$.\\ 6903Input \index{assume} : 6904\begin{center}{\tt assume(n,integer)}\end{center} 6905\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2*pi,n)}\end{center} 6906Output : 6907\begin{center}{\tt ((2*i)*pi*n+2)/n\verb|^|2}\end{center} 6908If you don't specify {\tt assume(n,integer)}, the output will not be 6909simplified : 6910\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} 6911\begin{center}{\tt (-i)*exp((-i)*n*2*pi)+i)/(pi*n\verb|^|3)}\end{center} 6912You might simplify this expression by replacing 6913{\tt exp((-i)*n*2*pi)} by {\tt 1}, input : 6914\begin{center}{\tt subst(ans(),exp((-i)*n*2*pi)=1)}\end{center} 6915Output : 6916\begin{center}{\tt ((2*i)*pi\verb|^|2*n\verb|^|2+2*pi*n+-i+i)/pi/n\verb|^|3}\end{center} 6917This expression is then simplified with {\tt normal}, the final 6918output is : 6919\begin{center}{\tt ((2*i)*pi*n+2)/n\verb|^|2}\end{center} 6920Hence for $n \neq 0$, $\displaystyle c_n=\frac{2in\pi+2}{n^2}$. 6921As shown in this example, it is better to input {\tt 6922 assume(n,integer)} before calling {\tt fourier\_cn}.\\ 6923We must also compute $c_n$ for $n=0$, input : 6924\begin{center}{\tt fourier\_cn(x\verb|^|2,x,2*pi,0)}\end{center} 6925Output : 6926\begin{center}{\tt 4*pi\verb|^|2/3}\end{center} 6927Hence for $n= 0$, $\displaystyle c_0=\frac{4{\pi}^2}{3}$. 6928\end{itemize} 6929{\bf Remarks} : 6930\begin{itemize} 6931\item Input {\tt purge(n)}\index{purge} to remove the hypothesis done 6932 on $n$. 6933\item 6934Input {\tt about(n)}\index{about} or {\tt assume(n)}\index{assume}, to know 6935the hypothesis done on the variable $n$. 6936\end{itemize} 6937 6938\subsection{Discrete Fourier Transform} 6939Let $N$ be an integer. 6940The Discrete Fourier Transform (DFT) is a transformation $F_N$ defined on 6941the set of periodic sequences of period $N$, it depends on a choice 6942of a primitive $N$-th root of unity $\omega_N$. If the 6943DFT is defined on sequences with complex coefficients, we take: 6944\[ \omega_N=e^{\frac{2 i \pi}{N}}\] 6945If $x$ is a periodic sequence of period 6946$N$, defined by the vector $x=[x_0,x_1,...x_{N-1}]$ then 6947$F_N(x)=y$ is a periodic sequence of period $N$, defined by: 6948\[ {(F_{N,\omega_N}(x))}_k=y_k=\sum_{j=0}^{N-1}x_j\omega_N^{-k\cdot j}, k=0..N-1 \] 6949where $\omega_N$ is a primitive $N$-th root of unity. 6950The discrete Fourier transform may be computed faster than by 6951computing each $y_k$ individually, by the Fast Fourier Transform (FFT). 6952{\tt Xcas} implements the FFT algorithm to compute 6953the discrete Fourier transform only if $N$ is a power of 2. 6954 6955\subsubsection{The properties of the Discrete Fourier Transform} 6956The Discrete Fourier Transform $F_N$ is a bijective transformation 6957on periodic sequences such that 6958\begin{eqnarray*} 6959 F_{N,\omega_N}^{-1}&=&\frac{1}{N} F_{N,\omega_N^{-1}} \\ 6960&=&\frac{1}{N} \overline{F_{N}} \quad \mbox{ on } \mathbb C 6961\end{eqnarray*} 6962i.e. : 6963\[ {(F_N^{-1}(x))}_k=\frac{1}{N}\sum_{j=0}^{N-1}x_j\omega_N^{k\cdot j} \] 6964Inside {\tt Xcas} the discrete Fourier transform and its inverse 6965are denote by {\tt fft} and {\tt ifft}: 6966\begin{center} 6967{\tt fft(x)}=$\displaystyle F_N(x)$, \ {\tt ifft(x)}=$\displaystyle F_N^{-1}(x)$ 6968\end{center} 6969{\bf Definitions}\\ 6970Let $x$ and $y$ be two periodic sequences of period $N$. 6971\begin{itemize} 6972\item The Hadamard product (notation $\cdot$) is defined by: 6973\[ {(x \cdot y)}_k = x_k y_k \] 6974\item the convolution product (notation $*$) is defined by: 6975\[ {(x * y)}_k=\sum_{j=0}^{N-1}x_jy_{k-j} \] 6976\end{itemize} 6977{\bf Properties} : 6978\begin{eqnarray*} 6979N*F_N(x \cdot y)&=&F_N(x) * F_N(y)\\ 6980F_N(x * y)&=&F_N(x) \cdot F_N(y) 6981\end{eqnarray*} 6982 6983\subsubsection{Applications} 6984\begin{enumerate} 6985\item Value of a polynomial\\ 6986Define a polynomial $P(x)=\sum_{j=0}^{N-1}c_jx^j$ by the vector of its 6987coefficients $c:=[c_0,c_1,..c_{N-1}]$, where zeroes may be added so that 6988$N$ is a power of 2. 6989\begin{itemize} 6990\item Compute the values of $P(x)$ at 6991\[ x=a_k=\omega_N^{-k}=\exp(\frac{-2ik\pi}{N}), \quad k=0..N-1 \] 6992This is just the discrete Fourier transform of $c$ since 6993\[ P(a_k)=\sum_{j=0}^{N-1}c_j(\omega_N^{-k})^j=F_N(c)_k \] 6994Input, for example : 6995\begin{center} 6996{\tt P(x):=x+x\verb|^|2; w:=i} 6997\end{center} 6998Here the coefficients of $P$ are [0,1,1,0], 6999$N=4$ and $\omega=\exp(2i\pi/4)=i$.\\ 7000Input :\\ 7001{\tt fft([0,1,1,0])}\\ 7002Output :\\ 7003{\tt [2,-1-i,0,-1+i]}\\ 7004hence 7005\begin{itemize} 7006\item {\tt P(1)=2}, 7007\item {\tt P(-i)=P(w\verb|^|-1)=-1-i}, 7008\item {\tt P(-1)=P(w\verb|^|-2)=0}, 7009\item {\tt P(i)=P(w\verb|^|-3)=-1+i}. 7010\end{itemize} 7011 7012\item Compute the values of $P(x)$ at 7013\[ x=b_k=\omega_N^{k}=\exp(\frac{2ik\pi}{N}), \quad k=0..N-1 \] 7014This is $N$ times the inverse fourier transform of $c$ since 7015\[ P(a_k)=\sum_{j=0}^{N-1}c_j(\omega_N^{k})^j=NF_N^{-1}(c)_k \] 7016Input, for example :\\ 7017{\tt P(x):=x+x\verb|^|2} and {\tt w:=i}\\ 7018Hence, the coefficients of $P$ are [0,1,1,0], 7019$N=4$ and $\omega=\exp(2i\pi/4)=i$.\\ 7020Input :\\ 7021{\tt 4*ifft([0,1,1,0])}\\ 7022Output :\\ 7023{\tt [2,-1+i,0,-1-i]}\\ 7024hence : \begin{itemize} 7025\item {\tt P(1)=2}, 7026\item {\tt P(i)=P(w\verb|^|1)=-1+i}, 7027\item {\tt P(-1)=P(w\verb|^|2)=0}, 7028\item {\tt P(-i)=P(w\verb|^|3)=-1-i}. 7029\end{itemize} 7030We find of course the same values as above... 7031\end{itemize} 7032 7033\item Trigonometric interpolation\\ 7034Let $f$ be periodic function of period $2\pi$, assume that $f(2k\pi/N)=f_k$ 7035for $k=0..(N-1)$. Find a trigonometric polynomial $p$ that interpolates $f$ 7036at $x_k=2k\pi/N$, that is find $p_j, j=0..N-1$ such that 7037\[ p(x)= \sum_{j=0}^{N-1} p_j \exp(ijx), \quad p(x_k)=f_k\] 7038Replacing $x_k$ by its value in $p(x)$ we get: 7039\[ \sum_{j=0}^{N-1} p_j \exp(i\frac{j2k\pi}{N}) = f_k\] 7040In other words, $(f_k)$ is the inverse DFT of $(p_k)$, hence 7041\[ (p_k)= \frac{1}{N} F_N( \ (f_k) \ ) \] 7042If the function $f$ is real, $p_{-k}=\overline p_k$, hence depending 7043whether $N$ is even or odd: 7044\begin{eqnarray*} 7045p(x)&=&p_0+ 70462 \Re(\sum_{k=0}^{\frac{N}{2}-1}p_k\exp(ikx))+\Re(p_{\frac{N}{2}} \exp(i\frac{Nx}{2})) \\ 7047p(x)&=&p_0+ 2 \Re(\sum_{k=0}^{\frac{N-1}{2}}p_k\exp(ikx)) 7048\end{eqnarray*} 7049 7050\item Fourier series\\ 7051Let $f$ be a periodic function of period $2\pi$, such that 7052\[ f(x_k)=y_k, \quad x_k=\frac{2k\pi}{N}, k=0..N-1 \] 7053Suppose that the Fourier series of $f$ converges to $f$ (this will 7054be the case if for example $f$ is continuous). If $N$ is large, 7055a good approximation of $f$ will be given by: 7056\[ \sum_{-\frac{N}{2} \leq n<\frac{N}{2}} c_n \exp(inx) \] 7057Hence we want a numeric approximation of 7058\[ c_n=\frac{1}{2\pi} \int_0^{2\pi}f(t)\exp(-int)dt \] 7059The numeric value of the integral $\int_0^{2\pi}f(t)\exp(-int)dt$ may be 7060computed by the trapezoidal rule 7061(note that the Romberg algorithm would not work here, 7062because the Euler Mac Laurin development 7063has its coefficients equal to zero, since the integrated function is 7064periodic, hence all its derivatives have the same value at $0$ and at $2\pi$). 7065If $\tilde{c_n}$ is the numeric value of $c_n$ obtained by the 7066trapezoidal rule, then 7067\[ 7068\tilde{c_n}=\frac{1}{2\pi}\frac{2\pi}{N}\sum_{k=0}^{N-1}y_k\exp(-2i\frac{nk\pi}{N}), 7069\quad -\frac{N}{2} \leq n<\frac{N}{2} \] 7070Indeed, since $x_k=2k\pi/N$ and $f(x_k)=y_k$: 7071\begin{eqnarray*} 7072f(x_k)\exp(-inx_k)&=&y_k\exp(-2i\frac{nk\pi}{N}), \\ 7073f(0)\exp(0)=f(2\pi)\exp(-2i\frac{nN\pi}{N})&=&y_0=y_N 7074\end{eqnarray*} 7075Hence : 7076\[ 7077[\tilde{c}_0,..\tilde{c}_{\frac{N}{2}-1},\tilde{c}_{\frac{N}{2}+1},..c_{N-1}]= 7078\frac{1}{N}F_N([y_0,y_1...y_{(N-1)}]) \] 7079since 7080\begin{itemize} 7081\item if $n\geq0$, $\tilde{c}_n=y_n$ 7082\item if $n<0$ $\tilde{c}_n=y_{n+N}$ 7083\item $\omega_N=\exp(\frac{2i\pi}{N})$, 7084then $\omega_N^n=\omega_N^{n+N}$ 7085\end{itemize} 7086 7087{\bf Properties} 7088\begin{itemize} 7089\item The coefficients of the trigonometric polynomial that interpolates $f$ 7090at $x=2k\pi/N$ are 7091\[ p_n=\tilde{c}_n, \quad -\frac{N}{2} \leq n<\frac{N}{2} \] 7092\item 7093If $f$ is a trigonometric polynomial $P$ of degree $m\leq \frac{N}{2}$, 7094then 7095\[ f(t)=P(t)=\sum_{k=-m}^{m-1}c_k\exp(2ik\pi t) \] 7096the trigonometric polynomial that interpolate $f=P$ is $P$, the numeric 7097approximation of the coefficients are in fact exact ($\tilde{c}_n=c_n$). 7098\item More generally, we can compute $\tilde{c}_n-c_n$.\\ 7099Suppose that $f$ is equal to its Fourier series, i.e. that :\\ 7100\[ f(t)=\sum_{m=-\infty}^{+\infty}c_m\exp(2i\pi mt), \quad 7101\sum_{m=-\infty}^{+\infty}|c_m|<\infty \] 7102Then : 7103\[ f(x_k)=f(\frac{2k\pi}{N})=y_k=\sum_{m=-\infty}^{+\infty}c_m\omega_N^{km}, 7104\quad 7105\tilde{c_n}=\frac{1}{N}\sum_{k=0}^{N-1}y_k\omega_N^{-kn} \] 7106Replace $y_k$ by its value in $\tilde{c_n}$: 7107\[ 7108\tilde{c_n}=\frac{1}{N}\sum_{k=0}^{N-1}\sum_{m=-\infty}^{+\infty} 7109c_m\omega_N^{km}\omega_N^{-kn} \] 7110If $m\neq n \pmod N$, $\omega_N^{m-n}$ is an $N$-th root of unity different 7111from 1, hence: 7112\[ \omega_N^{(m-n)N}=1, \quad \sum_{k=0}^{N-1}\omega_N^{(m-n)k}=0 \] 7113Therefore, if $m-n$ is a multiple of $N$ ($m=n+l\cdot N$) then 7114$\sum_{k=0}^{N-1}\omega_N^{k(m-n)}=N$, otherwise 7115$\sum_{k=0}^{N-1}\omega_N^{k(m-n)}=0$. 7116By reversing the two sums, we get 7117\begin{eqnarray*} 7118\tilde{c_n}&=&\frac{1}{N}\sum_{m=-\infty}^{+\infty}c_m\sum_{k=0}^{N-1}\omega_N^{k(m-n)} \\ 7119&=&\sum_{l=-\infty}^{+\infty}c_{(n+l\cdot N)} \\ 7120&=&...c_{n-2\cdot N}+c_{n-N}+c_{n}+c_{n+N}+c_{n+2\cdot 7121 N}+..... 7122\end{eqnarray*} 7123Conclusion: if $|n|<N/2$, $\tilde{c_n}-c_n$ is a sum of $c_j$ of large indexes 7124(at least $N/2$ in absolute value), hence is small (depending on the 7125rate of convergence of the Fourier series). 7126\end{itemize} 7127{\bf Example} 7128Input :\\ 7129\begin{center} 7130{\tt f(t):=cos(t)+cos(2*t)}\\ 7131{\tt x:=f(2*k*pi/8)\$(k=0..7)}\\ 7132\end{center} 7133Then : 7134\begin{center} 7135 {\tt x=\{2,sqrt(2)/2,-1,(-sqrt(2)/2,0,(-sqrt(2))/2,-1,sqrt(2)/2\} }\\ 7136{\tt fft(x)=[0.0,4.0,4.0,0.0,0.0,0.0,4.0,4.0]} 7137\end{center} 7138After a division by $N=8$, we get 7139\begin{center} $c_0=0,c_1=4.0/8,c_2=4.0/8,c_3=0.0$,\\ 7140$c_{-4}=0.0,c_{-3}=0.0,c_{-2}=4.0/8,=c_{-1}=4.0/8$ 7141\end{center} 7142Hence $b_k=0$ and $a_k=c_{-k}+c_k$ is equal to 1 if $k=1,2$ and 0 otherwise. 7143 7144\item Convolution Product\\ 7145If $P(x)=\sum_{j=0}^{n-1}a_jx^j$ 7146and $Q(x)=\sum_{j=0}^{m-1}b_jx^j$ 7147are given by the vector of their coefficients 7148$a=[a_0,a_1,..a_{n-1}]$ and $b=[b_0,b_1,..b_{m-1}]$, we may 7149compute the product of these two polynomials using the DFT. 7150The product of polynomials is the convolution product 7151of the periodic sequence of their coefficients 7152if the period is greater or equal to 7153$(n+m)$. Therefore we complete $a$ (resp. $b$) with $m+p$ 7154(resp. $n+p$) zeros, where 7155$p$ is chosen such that $N=n+m+p$ is a power of 2. 7156If $a=[a_0,a_1,..a_{n-1},0..0]$ and $b=[b_0,b_1,..b_{m-1},0..0]$, then: 7157\[ P(x)Q(x)=\sum_{j=0}^{n+m-1}(a*b)_jx^j \] 7158We compute $F_N(a)$, $F_N(b)$, then $ab=F_N^{-1}(F_N(a)\cdot F_N(b))$ 7159using the properties 7160\[ NF_N(x \cdot y)=F_N(x) * F_N(y), \quad 7161F_N(x * y)=F_N(x) \cdot F_N(y) \] 7162\end{enumerate} 7163 7164\subsection{Fast Fourier Transform : {\tt fft}}\index{fft} 7165\noindent{\tt fft} takes as argument a list (or a sequence) 7166${\tt [a_0,..a_{N-1}]}$ where {\tt N} is a power of two.\\ 7167{\tt fft} returns the list ${\tt [b_0,..b_{N-1}]}$ such that, 7168for {\tt k=0..N-1} 7169\[ {\tt 7170 {fft([a_0,..a_{N-1}])}[k]=b_k=\sum_{j=0}^{N-1}x_j\omega_N^{-k\cdot 7171 j}} \] 7172where $\omega_N$ is a primitive $N$-th root of the unity.\\ 7173Input : 7174\begin{center}{\tt fft(0,1,1,0)}\end{center} 7175Output : 7176\begin{center}{\tt [2.0, -1-i, 0.0, -1+i]}\end{center} 7177 7178\subsection{Inverse Fast Fourier Transform : {\tt ifft}}\index{ifft} 7179\noindent{\tt ifft} takes as argument a list ${\tt [b_0,..b_{N-1}]}$ where 7180{\tt N} is a power of two.\\ 7181{\tt ifft} returns the list ${\tt [a_0,..a_{N-1}]}$ such that 7182\[ {\tt fft([a_0,..a_{N-1}])=[b_0,..b_{N-1}]} \] 7183Input : 7184\begin{center}{\tt ifft([2,-1-i,0,-1+i])}\end{center} 7185Output : 7186\begin{center}{\tt [0.0, 1.0, 1.0, 0.0]}\end{center} 7187 7188\subsection{An {\bf exercise} with {\tt fft}} 7189Here are the temperatures $T$, in Celsius degree, at time $t$ : 7190\begin{center} 7191\begin{tabular}{|r|rrrrrrrr|} 7192\hline 7193t & 0 & 3 & 6 & 9 &12 & 15 & 19 & 21\\ 7194\hline 7195T & 11 & 10 & 17 & 24 & 32 & 26 & 23 & 19\\ 7196\hline 7197\end{tabular} 7198\end{center} 7199What was the temperature at 13h45 ? 7200 7201Here $N=8=2*m$. The interpolation polynomial is 7202\[ p(t)=\frac{1}{2} p_{-m}(\exp(-2i\frac{\pi mt}{24})+ 7203\exp(2i\frac{\pi mt}{24}))+ 7204\sum_{k=-m+1}^{m-1}p_k \exp(2i\frac{\pi kt}{24}) \] 7205and 7206\[ p_k=\frac{1}{N} \sum_{k=j}^{N-1}T_k \exp(2i\frac{\pi k}{N}) \] 7207Input :\\ 7208{\tt q:=1/8*fft([11,10,17,24,32,26,23,19])}\\ 7209Output :\\ 7210{\tt q:=[20.25,-4.48115530061+1.72227182413*i,-0.375+0.875*i,\\ 7211-0.768844699385+0.222271824132*i,0.5,\\ 7212-0.768844699385-0.222271824132*i,\\ 7213-0.375-0.875*i,-4.48115530061-1.72227182413*i]}\\ 7214hence: 7215\begin{itemize} 7216\item $p_0=20.25$ 7217\item $p_1=-4.48115530061+1.72227182413*i=\overline{p_{-1}}$, 7218\item $p_2=0.375+0.875*i=\overline{p_{-2}}$, 7219\item $p_3=-0.768844699385+0.222271824132*i=\overline{p_{-3}}$, 7220\item $p_{-4}=0.5$ 7221\end{itemize} 7222Indeed 7223\[ 7224q=[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 7225 \frac{1}{N}fft(y)} \] 7226Input :\\ 7227{\tt pp:=[q[4],q[5],q[6],q[7],q[0],q[1],q[2],q[3]]}\\ 7228Here, $p_k=pp[k+4]$ for $k=-4...3$. 7229It remains to compute the value of the interpolation polynomial at point 7230$t0=13.75=55/4$.\\ 7231Input: 7232\begin{center} 7233{\tt t0(j):=exp(2*i*pi*(13+3/4)/24*j)}\\ 7234{\tt T0:=1/2*pp[0]*(t0(4)+t0(-4))+sum(pp[j+4]*t0(j),j,-3,3)}\\ 7235{\tt evalf(re(T0))} 7236\end{center} 7237Output : 7238\begin{center} 7239{\tt 29.4863181684} 7240\end{center} 7241The temperature is predicted to be equal to 29.49 Celsius degrees.\\ 7242Input : 7243\begin{center} 7244{\tt q1:=[q[4]/2,q[3],q[2],q[1],q[0]/2]}\\ 7245{\tt a:=t0(1)} (or {\tt a:=-exp(i*pi*7/48)})\\ 7246{\tt g(x):=r2e(q1,x)}\\ 7247{\tt evalf(2*re(g(a)))} 7248\end{center} 7249or : 7250\begin{center} 7251{\tt 2.0*re(q[0]/2+q[1]*t0(1)+q[2]*t0(2)+q[3]*t0(3)+q[4]/2*t0(4))} 7252\end{center} 7253Output : 7254\begin{center} 7255{\tt 29.4863181684} 7256\end{center} 7257 7258{\bf Remark}\\ 7259Using the Lagrange interpolation polynomial (the polynomial is not periodic), 7260input : 7261\begin{center} 7262{\tt l1:=[0,3,6,9,12,15,18,21]}\\ 7263{\tt l2:=[11,10,17,24,32,26,23,19]}\\ 7264{\tt subst(lagrange(l1,l2,13+3/4),x=13+3/4)}\\ 7265\end{center} 7266Output : 7267\begin{center} 7268${\tt \displaystyle \frac{8632428959}{286654464}\simeq 30.1144061688}$ 7269\end{center} 7270 7271\section{Signal Processing} 7272\subsection{Cross-correlation of two signals : {\tt cross\_correlation}} 7273\label{sec:crosscorr} 7274{\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 : 7275\[ z_k=\sum_{i=k}^{N-1}\overline{v_{i-k}^\ast}\,w_i^\ast,\quad k=0,1,\dots,N-1, \] 7276where 7277\[ \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}]. \] 7278 7279For example, input : 7280\begin{center} 7281 {\tt cross\_correlation([1,2],[3,4,5])} 7282\end{center} 7283Output : 7284\begin{center} 7285 {\tt [6.0,11.0,14.0,5.0]} 7286\end{center} 7287 7288\subsection{Auto-correlation of a signal : {\tt auto\_correlation}} 7289{\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 : 7290\begin{center} 7291 {\tt auto\_correlation([2,3,4,3,1,4,5,1,3,1])} 7292\end{center} 7293Output : 7294\begin{center} 7295 {\tt [2.0,9.0,15.0,28.0,37.0,44.0,58.0,58.0,68.0,\\ 7296 91.0,68.0,58.0,58.0,44.0,37.0,28.0,15.0,9.0,2.0]} 7297\end{center} 7298 7299\subsection{Convolution of two signals : {\tt convolution}} 7300{\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 : 7301\[ z_k=\sum_{i=0}^{k}v_i\,w_{k-i},\quad k=0,1,\dots,N-1, \] 7302such that $ v_j=0 $ for $ j\geq n $ and $ w_j=0 $ for $ j\geq m $. For example, input : 7303\begin{center} 7304 {\tt convolution([1,2,3],[1,-1,1,-1])} 7305\end{center} 7306Output : 7307\begin{center} 7308 {\tt [1.0,1.0,2.0,-2.0,1.0,-3.0]} 7309\end{center} 7310 7311\subsection{Low-pass filtering : {\tt lowpass}} 7312{\tt lowpass} takes two or three arguments: 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). This implementation is an emulation of a simple first-order lowpass RC filter. 7313 7314For example, input : 7315\begin{center} 7316 {\tt f:=unapply(periodic(sign(x),x,-1/880,1/880),x);\\ 7317 s:=2\verb|^|14*(apply(f,soundsec(1)));\\ 7318 playsnd(lowpass(s,1000))} 7319\end{center} 7320 7321\subsection{High-pass filtering : {\tt highpass}} 7322{\tt highpass} takes two or three arguments: 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). This implementation is an emulation of a simple first-order highpass RC filter. 7323 7324For example, input : 7325\begin{center} 7326 {\tt f:=unapply(periodic(sign(x),x,-1/880,1/880),x);\\ 7327 s:=2\verb|^|14*(apply(f,soundsec(1)));\\ 7328 playsnd(highpass(s,5000))} 7329\end{center} 7330 7331\subsection{Perform thresholding operations on an array : {\tt threshold}} 7332{\tt threshold} changes the data in an array which does not meet some kind of minimality criterion. It takes the following parameters : 7333\begin{itemize} 7334 \item vector $ \mathbf{v} $ of real or complex numbers 7335 \item bound specification {\tt bnd} 7336 \item comparison operator (optional) 7337 \item {\tt abs[=true,false]} (optional) 7338\end{itemize} 7339Bound 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 : 7340\[ w_k=\begin{cases} 7341\text{{\tt uvalue} (defaults to $ u $)},&v_k>u,\\ 7342\text{{\tt lvalue} (defaults to $ l $)},&v_k<l,\\ 7343v_k,&\text{otherwise} 7344\end{cases} \] 7345for $ 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|} $. 7346 7347In the first case where {\tt bnd} is a number or an equation, the return vector $ \mathbf{w} $ is defined by : 7348\[ w_k=\begin{cases} 7349\text{{\tt value} (defaults to $ b $)},&v_k<b,\\ 7350v_k,&\text{otherwise} 7351\end{cases} \] 7352if $ 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 $ : 7353\[ w_k=\begin{cases} 7354\text{{\tt value}},& \text{$ v_k\geq 0 $ and $ |v_k|<b $},\\ 7355-\text{{\tt value}},& \text{$ v_k<0 $ and $ |v_k|<b $},\\ 7356v_k,&\text{otherwise}. 7357\end{cases} \] 7358As 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. 7359 7360For example, input : 7361\begin{center} 7362 {\tt threshold(2,3,1,2,5,4,3,7],3)} 7363\end{center} 7364Output : 7365\begin{center} 7366 {\tt [3,3,3,3,5,4,3,7]} 7367\end{center} 7368Input : 7369\begin{center} 7370 {\tt threshold([2,3,1,2,5,4,3,7],3=a,'>=')} 7371\end{center} 7372Output : 7373\begin{center} 7374 {\tt [2,a,1,2,a,a,a,a]} 7375\end{center} 7376Input : 7377\begin{center} 7378 {\tt threshold([-2,-3,1,2,5,-4,3,-1],3=0,abs=true)} 7379\end{center} 7380Output : 7381\begin{center} 7382 {\tt [0,-3,0,0,5,-4,3,0]} 7383\end{center} 7384Input : 7385\begin{center} 7386 {\tt threshold([-2,-3,1,2,5,-4,3,-1],3=0,'<=',abs=true)} 7387\end{center} 7388Output : 7389\begin{center} 7390 {\tt [0,0,0,0,5,-4,0,0]} 7391\end{center} 7392Input : 7393\begin{center} 7394 {\tt threshold([-120,-11,-3,0,7,27,111,234],[-100,100])} 7395\end{center} 7396Output : 7397\begin{center} 7398 {\tt [-100,-11,-3,0,7,27,100,100]} 7399\end{center} 7400Input : 7401\begin{center} 7402 {\tt threshold([-120,-11,-3,0,7,27,111,234],[-100=-inf,100=inf])} 7403\end{center} 7404Output : 7405\begin{center} 7406 {\tt [-infinity,-11,-3,0,7,27,+infinity,+infinity]} 7407\end{center} 7408 7409\subsection{Bartlett-Hann window function : {\tt bartlett\_hann\_window}} 7410{\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 7411\[ 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) \] 7412for $ k=0,1,\dots,N-1 $, where $ a_0=0.62 $, $ a_1=0.48 $ and $ a_2=0.38 $. For example, input : 7413\begin{center} 7414 {\tt L:=bartlett\_hann\_window(randvector(1000,0..1)):;} 7415\end{center} 7416followed by {\tt scatterplot(L)}. 7417 7418\subsection{Blackman-Harris window function : {\tt blackman\_harris\_window}} 7419{\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 7420\[ 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) \] 7421for $ 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 : 7422\begin{center} 7423 {\tt L:=blackman\_harris\_window(randvector(1000,0..1)):;} 7424\end{center} 7425followed by {\tt scatterplot(L)}. 7426 7427\subsection{Blackman window function : {\tt blackman\_window}} 7428{\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 7429\[ 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) \] 7430for $ k=0,1,\dots,N-1 $. For example, input : 7431\begin{center} 7432 {\tt L:=blackman\_window(randvector(1000,0..1)):;} 7433\end{center} 7434followed by {\tt scatterplot(L)}. 7435 7436\subsection{Bohman window function : {\tt bohman\_window}} 7437{\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 7438\[ w_k=\left(1-x_k\right)\,\cos\left(\pi\,x_k\right)+\frac{1}{\pi}\,\sin\left(\pi\,x_k\right), \] 7439where $ x_k=\left|\frac{2\,k}{N-1}-1\right| $, for $ k=0,1,\dots,N-1 $. For example, input : 7440\begin{center} 7441 {\tt L:=bohman\_window(randvector(1000,0..1)):;} 7442\end{center} 7443followed by {\tt scatterplot(L)}. 7444 7445\subsection{Cosine window function : {\tt cosine\_window}} 7446{\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 7447\[ w_k=\sin^\alpha\left(\frac{k\,\pi}{N-1}\right) \] 7448for $ k=0,1,\dots,N-1 $. For example, input : 7449\begin{center} 7450 {\tt L:=cosine\_window(randvector(1000,0..1),1.5):;} 7451\end{center} 7452followed by {\tt scatterplot(L)}. 7453 7454\subsection{Gaussian window function : {\tt gaussian\_window}} 7455{\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 7456\[ w_k=\exp\left(-\frac{1}{2}\,\left(\frac{k-(N-1)/2}{\alpha\,(N-1)/2}\right)^2\right) \] 7457for $ k=0,1,\dots,N-1 $. For example, input : 7458\begin{center} 7459 {\tt L:=gaussian\_window(randvector(1000,0..1),0.4):;} 7460\end{center} 7461followed by {\tt scatterplot(L)}. 7462 7463\subsection{Hamming window function : {\tt hamming\_window}} 7464{\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 7465\[ w_k=\alpha-\beta\,\cos\left(\frac{2\,k\,\pi}{N-1}\right) \] 7466for $ k=0,1,\dots,N-1 $, where $ \alpha=0.54 $ and $ \beta=1-\alpha=0.46 $. For example, input : 7467\begin{center} 7468 {\tt L:=hamming\_window(randvector(1000,0..1)):;} 7469\end{center} 7470followed by {\tt scatterplot(L)}. 7471 7472\subsection{Hann-Poisson window function : {\tt hann\_poisson\_window}} 7473{\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 7474\[ 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) \] 7475for $ k=0,1,\dots,N-1 $. For example, input : 7476\begin{center} 7477 {\tt L:=hann\_poisson\_window(randvector(1000,0..1),2):;} 7478\end{center} 7479followed by {\tt scatterplot(L)}. 7480 7481\subsection{Hann window function : {\tt hann\_window}} 7482{\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 7483\[ w_k=\sin^2\left(\frac{k\,\pi}{N-1}\right) \] 7484for $ k=0,1,\dots,N-1 $. For example, input : 7485\begin{center} 7486 {\tt L:=hann\_window(randvector(1000,0..1)):;} 7487\end{center} 7488followed by {\tt scatterplot(L)}. 7489 7490\subsection{Parzen window function : {\tt parzen\_window}} 7491{\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 7492\[ w_k=\begin{cases} 7493\left(1-6\,x_k^2\,\left(1-x_k\right)\right),&\left|\frac{N-1}{2}-k\right|\leq\frac{N-1}{4},\\ 74942\,\left(1-x_k\right)^3,&\text{otherwise}, 7495\end{cases} \] 7496where $ x_k=\left|1-\frac{2\,k}{N-1}\right| $, for $ k=0,1,\dots,N-1 $. For example, input : 7497\begin{center} 7498 {\tt L:=parzen\_window(randvector(1000,0..1)):;} 7499\end{center} 7500followed by {\tt scatterplot(L)}. 7501 7502\subsection{Poisson window function : {\tt poisson\_window}} 7503{\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 7504\[ w_k=\exp\left(-\alpha\,\left|\frac{2\,k}{N-1}-1\right|\right) \] 7505for $ k=0,1,\dots,N-1 $. For example, input : 7506\begin{center} 7507 {\tt L:=poisson\_window(randvector(1000,0..1),2):;} 7508\end{center} 7509followed by {\tt scatterplot(L)}. 7510 7511\subsection{Riemann window function : {\tt riemann\_window}} 7512{\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 7513\[ w_k=\begin{cases} 75141,&k=\frac{N-1}{2},\\ 7515\frac{\sin(\pi\,x_k)}{\pi\,x_k},&\text{otherwise}, 7516\end{cases} \] 7517where $ x_k=\frac{2\,k}{N-1}-1 $, for $ k=0,1,\dots,N-1 $. For example, input : 7518\begin{center} 7519 {\tt L:=riemann\_window(randvector(1000,0..1)):;} 7520\end{center} 7521followed by {\tt scatterplot(L)}. 7522 7523\subsection{Triangular window function : {\tt triangle\_window}} 7524{\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 7525\[ w_k=1-\left|\frac{n-\frac{N-1}{2}}{\frac{N+d}{2}}\right| \] 7526for $ k=0,1,\dots,N-1 $ (the case $ d=-1 $ is called the Bartlett window function). For example, input : 7527\begin{center} 7528 {\tt L:=triangle\_window(randvector(1000,0..1),1):;} 7529\end{center} 7530followed by {\tt scatterplot(L)}. 7531 7532\subsection{Tukey window function : {\tt tukey\_window}} 7533{\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 7534\[ w_k=\begin{cases} 7535\frac{1}{2}\,\left(1+\cos\left(\pi\,\left(\frac{k}{\beta}-1\right)\right)\right),&k<\beta,\\ 75361,&\beta\leq k\leq(N-1)\,\left(1-\frac{\alpha}{2}\right),\\ 7537\frac{1}{2}\,\left(1+\cos\left(\pi\,\left(\frac{k}{\beta}-\frac{2}{\alpha}+1\right)\right)\right),&\text{otherwise}, 7538\end{cases} \] 7539where $ \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 : 7540\begin{center} 7541 {\tt L:=tukey\_window(randvector(1000,0..1),0.4):;} 7542\end{center} 7543followed by {\tt scatterplot(L)}. 7544 7545\subsection{Welch window function : {\tt welch\_window}} 7546{\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 7547\[ w_k=1-\left(\frac{k-\frac{N-1}{2}}{\frac{N-1}{2}}\right)^2 \] 7548for $ k=0,1,\dots,N-1 $. For example, input : 7549\begin{center} 7550 {\tt L:=welch\_window(randvector(1000,0..1)):;} 7551\end{center} 7552followed by {\tt scatterplot(L)}. 7553 7554\section{Exponentials and Logarithms} 7555\subsection{Rewrite hyperbolic functions as exponentials : {\tt hyp2exp}}\index{hyp2exp} 7556\noindent{\tt hyp2exp} takes as argument an hyperbolic expression.\\ 7557{\tt hyp2exp} rewrites each hyperbolic functions with exponentials 7558(as a rational fraction of one exponential, 7559i.e. {\sc without} linearization).\\ 7560Input : 7561\begin{center}{\tt hyp2exp(sinh(x))}\end{center} 7562Output : 7563\begin{center}{\tt (exp(x)-1/(exp(x)))/2}\end{center} 7564 7565\subsection{Expand exponentials : {\tt expexpand}}\index{expexpand} 7566\noindent{\tt expexpand} takes as argument an expression with exponentials.\\ 7567{\tt expexpand} expands this expression (rewrites exp of sums as 7568product of exp).\\ 7569Input : 7570\begin{center}{\tt expexpand(exp(3*x)+exp(2*x+2))}\end{center} 7571Output : 7572\begin{center}{\tt exp(x)\verb|^|3+exp(x)\verb|^|2*exp(2)}\end{center} 7573 7574\subsection{Expand logarithms : {\tt lnexpand}}\index{lnexpand} 7575\noindent{\tt lnexpand} takes as argument an expression with logarithms.\\ 7576{\tt lnexpand} expands this expression (rewrites ln of products 7577as sum of ln).\\ 7578Input : 7579\begin{center}{\tt lnexpand(ln(3*x\verb|^|2)+ln(2*x+2))}\end{center} 7580Output : 7581\begin{center}{\tt ln(3)+2*ln(x)+ln(2)+ln(x+1)}\end{center} 7582 7583\subsection{Linearize exponentials : {\tt lin}}\index{lin} 7584\noindent{\tt lin} takes as argument an expression with 7585exponentials.\\ 7586{\tt lin} rewrites hyperbolic functions as exponentials if required, 7587then linearizes this expression (i.e. replace product of 7588exponentials by exponential of sums).\\ 7589{\bf Examples} 7590\begin{itemize} 7591\item Input : 7592\begin{center}{\tt lin(sinh(x)\verb|^|2)}\end{center} 7593Output : 7594\begin{center}{\tt 1/4*exp(2*x)+1/-2+1/4*exp(-(2*x))}\end{center} 7595 7596\item Input : 7597\begin{center}{\tt lin((exp(x)+1)\verb|^|3)}\end{center} 7598Output : 7599\begin{center}{\tt exp(3*x)+3*exp(2*x)+3*exp(x)+1}\end{center} 7600\end{itemize} 7601 7602\subsection{Collect logarithms : {\tt lncollect}}\index{lncollect} 7603\noindent{\tt lncollect} takes as argument an expression with logarithms.\\ 7604{\tt lncollect} collects the logarithms (rewrites sum of ln 7605as ln of products). 7606It may be a good idea to factor the 7607expression with {\tt factor} before collecting by {\tt lncollect}).\\ 7608Input : 7609\begin{center}{\tt lncollect(ln(x+1)+ln(x-1))}\end{center} 7610Output : 7611\begin{center}{\tt log((x+1)*(x-1))}\end{center} 7612Input : 7613\begin{center}{\tt lncollect(exp(ln(x+1)+ln(x-1)))}\end{center} 7614Output : 7615\begin{center}{\tt (x+1)*(x-1)}\end{center} 7616{\bf Warning!!!} For {\tt Xcas}, {\tt log=ln} (use {\tt log10} 7617for 10-base logarithm). 7618 7619\subsection{Expand powers : {\tt powexpand}}\index{powexpand} 7620\noindent{\tt powexpand} rewrites a power of a sum as a product of powers.\\ 7621Input : 7622\begin{center}{\tt powexpand(a\verb|^|(x+y))}\end{center} 7623Output : 7624\begin{center}{\tt a\verb|^|x*a\verb|^|y}\end{center} 7625 7626 7627\subsection{Rewrite a power as an exponential : {\tt pow2exp}}\index{pow2exp} 7628\noindent{\tt pow2exp} rewrites a power as an exponential.\\ 7629Input : 7630\begin{center}{\tt pow2exp(a\verb|^|(x+y))}\end{center} 7631Output : 7632\begin{center}{\tt exp((x+y)*ln(a))}\end{center} 7633 7634\subsection{Rewrite exp(n*ln(x)) as a power : {\tt exp2pow}}\index{exp2pow} 7635\noindent{\tt exp2pow} rewrites expression of the form $\exp(n*\ln(x))$ 7636as a power of $x$.\\ 7637Input : 7638\begin{center}{\tt exp2pow(exp(n*ln(x)))}\end{center} 7639Output : 7640\begin{center}{\tt x\verb|^|n}\end{center} 7641Note the difference with {\tt lncollect} :\\ 7642{\tt lncollect(exp(n*ln(x))) = exp(n*log(x))}\\ 7643{\tt lncollect(exp(2*ln(x))) = exp(2*log(x))}\\ 7644{\tt exp2pow(exp(2*ln(x))) = x\verb|^|2 }\\ 7645But :\\ 7646{\tt lncollect(exp(ln(x)+ln(x))) = x\verb|^|2}\\ 7647{\tt exp2pow(exp(ln(x)+ln(x))) = x\verb|^|(1+1)}\\ 7648 7649\subsection{Simplify complex exponentials : {\tt tsimplify}}\index{tsimplify} 7650\noindent{\tt tsimplify} simplifies transcendental expressions 7651by rewriting the expression with complex exponentials.\\ 7652It is a good idea to try other simplification instructions 7653and call {\tt tsimplify} if they do not work.\\ 7654Input : 7655\begin{center}{\tt tsimplify((sin(7*x)+sin(3*x))/sin(5*x))}\end{center} 7656Output : 7657\begin{center}{\tt ((exp((i)*x))\verb|^|4+1)/(exp((i)*x))\verb|^|2 }\end{center} 7658 7659\section{Polynomials} 7660\label{sec:polynomials} 7661A polynomial of one variable is represented either 7662by a symbolic expression or by the list of its 7663coefficients in decreasing powers order (dense representation). 7664In the latter case, to avoid confusion with other kinds of list 7665\begin{itemize} 7666\item use \verb|poly1[...]| as delimiters in inputs 7667\item check for $\talloblong \ \talloblong$ in {\tt Xcas} output. 7668\end{itemize} 7669Note that polynomials represented as lists of coefficients 7670are always written in decreasing powers order even if 7671{\tt increasing power} is checked in {\tt cas} configuration. 7672 7673A polynomial of several variables is represented 7674\begin{itemize} 7675\item by a symbolic expression 7676\item or by a dense recursive 1-d representation like above 7677\item or by a sum of 7678monomials with non-zero coefficients (distributed sparse 7679representation).\\ 7680A monomial with several variables is represented by a coefficient and a 7681list of integers (interpreted as powers of a variable list). The 7682delimiters for monomials are 7683{\tt \%\%\%\{} and {\tt \%\%\%\}}, for example $3x^2y$ is represented by 7684{\tt \%\%\%\{3,[2,1]\%\%\%\}} with respect to the variable list 7685{\tt [x,y]}). 7686\end{itemize} 7687 7688 7689\subsection{Convert to a symbolic polynomial : {\tt r2e poly2symb}}\index{r2e}\index{poly2symb} 7690\noindent{\tt r2e} or {\tt poly2symb} takes as argument 7691\begin{itemize} 7692\item a list of 7693coefficients of a polynomial (by decreasing order) and a symbolic 7694variable name 7695(by default {\tt x}) 7696\item or a sum of monomials {\tt \%\%\%\{coeff,[n1,....nk] \%\%\%\}} 7697and a vector of symbolic variables {\tt [x1,...,xk]}. 7698\end{itemize} 7699{\tt r2e} or {\tt poly2symb} transforms the argument into a symbolic 7700polynomial.\\ 7701Example with univariate polynomials, input : 7702\begin{center}{\tt r2e([1,0,-1],x)}\end{center} 7703or : 7704\begin{center}{\tt r2e([1,0,-1])}\end{center} 7705or : 7706\begin{center}{\tt poly2symb([1,0,-1],x)}\end{center} 7707Output : 7708\begin{center}{\tt x*x-1}\end{center} 7709Example with sparse multivariate polynomials, input: 7710\begin{center}{\tt poly2symb(\%\%\%\{1,[2]\%\%\%\}+\%\%\%\{-1,[0]\%\%\%\},[x])}\end{center} 7711or : 7712\begin{center}{\tt r2e(\%\%\%\{1,[2]\%\%\%\}+\%\%\%\{-1,[0]\%\%\%\},[x])}\end{center} 7713Output : 7714\begin{center}{\tt x\verb|^2|-1}\end{center} 7715Input : 7716\begin{center}{\tt r2e(\%\%\%\{1,[2,0]\%\%\%\}+\%\%\%\{-1,[1,1]\%\%\%\}+\%\%\%\{2,[0,1]\%\%\%\},[x,y])}\end{center} 7717or : 7718\begin{center}{\tt poly2symb(\%\%\%\{1,[2,0]\%\%\%\}+\%\%\%\{-1,[1,1]\%\%\%\}+\%\%\%\{2,[0,1]\%\%\%\},[x,y])}\end{center} 7719Output : 7720\begin{center}{\tt x\verb|^|2-x*y+2*y}\end{center} 7721 7722\subsection{Convert from a symbolic polynomial : {\tt e2r symb2poly}}\index{e2r}\index{symb2poly} 7723\noindent{\tt e2r} or {\tt symb2poly} takes as argument a symbolic polynomial 7724and either a symbolic variable name (by default {\tt x}) or 7725a list of symbolic variable names.\\ 7726{\tt e2r} or {\tt symb2poly} transforms the polynomial into a list 7727(dense representation of the univariate polynomial, coefficients 7728written by decreasing order) or into a sum of monomials (sparse 7729representation of multivariate polynomials).\\ 7730Input : 7731\begin{center}{\tt e2r(x\verb|^|2-1)}\end{center} 7732or : 7733\begin{center}{\tt symb2poly(x\verb|^|2-1)}\end{center} 7734or : 7735\begin{center}{\tt symb2poly(y\verb|^|2-1,y)}\end{center} 7736or : 7737\begin{center}{\tt e2r(y\verb|^|2-1,y)}\end{center} 7738Output : 7739\begin{center}{\tt $\talloblong$1,0,-1$\talloblong$}\end{center} 7740Input : 7741\begin{center}{\tt e2r(x\verb|^|2-x*y+y, [x,y])}\end{center} 7742or : 7743\begin{center}{\tt symb2poly(x\verb|^|2-x*y+2*y, [x,y])}\end{center} 7744Output : 7745\begin{center}{\tt \%\%\%\{1,[2,0]\%\%\%\}+\%\%\%\{-1,[1,1]\%\%\%\}+\%\%\%\{2,[0,1]\%\%\%\}}\end{center} 7746 7747\subsection{Coefficients of a polynomial: {\tt coeff coeffs}}\index{coeff}\index{coeffs} 7748\noindent{\tt coeff} or {\tt coeffs} takes three arguments : the polynomial, 7749the name of the variable (or the list of the names of variables) and 7750the degree (or the list of the degrees of the variables).\\ 7751{\tt coeff} or {\tt coeffs} returns the coefficient of the polynomial 7752of the degree given as third argument. 7753If no degree was specified, {\tt coeffs} return 7754the list of the coefficients of the polynomial, including 0 in the 7755univariate dense case and excluding 0 in the multivariate sparse case.\\ 7756Input : 7757\begin{center}{\tt coeff(-x\verb|^|4+3*x*y\verb|^|2+x,x,1)}\end{center} 7758Output : 7759\begin{center}{\tt 3*y\verb|^|2+1}\end{center} 7760Input : 7761\begin{center}{\tt coeff(-x\verb|^|4+3x*y\verb|^|2+x,y,2)}\end{center} 7762Output : 7763\begin{center}{\tt 3*x}\end{center} 7764Input : 7765\begin{center}{\tt coeff(-x\verb|^|4+3x*y\verb|^|2+x,[x,y],[1,2])}\end{center} 7766Output : 7767\begin{center}{\tt 3}\end{center} 7768 7769\subsection{Polynomial degree : {\tt degree}}\index{degree} 7770\noindent{\tt degree} takes as argument a polynomial given by its symbolic 7771representation or by the list of its coefficients.\\ 7772{\tt degree} returns the degree of this polynomial (highest 7773degree of its non-zero monomials).\\ 7774Input : 7775\begin{center}{\tt degree(x\verb|^|3+x)}\end{center} 7776Output : 7777\begin{center}{\tt 3}\end{center} 7778Input : 7779\begin{center}{\tt degree([1,0,1,0])}\end{center} 7780Output : 7781\begin{center}{\tt 3}\end{center} 7782 7783\subsection{Polynomial valuation : {\tt valuation ldegree}}\index{valuation}\index{ldegree} 7784\noindent{\tt valuation} or {\tt ldegree} takes as argument a polynomial given 7785by a symbolic expression or by the list of its coefficients.\\ 7786{\tt valuation} or {\tt ldegree} returns the valuation of this 7787polynomial, that is the lowest degree of its non-zero monomials.\\ 7788Input : 7789\begin{center}{\tt valuation(x\verb|^|3+x)}\end{center} 7790Output : 7791\begin{center}{\tt 1}\end{center} 7792Input : 7793\begin{center}{\tt valuation([1,0,1,0])}\end{center} 7794Output : 7795\begin{center}{\tt 1}\end{center} 7796 7797\subsection{Leading coefficient of a polynomial : {\tt lcoeff}}\index{lcoeff} 7798\noindent{\tt lcoeff} takes as argument a polynomial given by a 7799symbolic expression or by the list of its coefficients.\\ 7800{\tt lcoeff} returns the leading coefficient of this polynomial, 7801that is the coefficient of the monomial of highest degree.\\ 7802Input : 7803\begin{center}{\tt lcoeff([2,1,-1,0])}\end{center} 7804Output : 7805\begin{center}{\tt 2}\end{center} 7806Input : 7807\begin{center}{\tt lcoeff(3*x\verb|^|2+5*x,x)}\end{center} 7808Output : 7809\begin{center}{\tt 3}\end{center} 7810Input : 7811\begin{center}{\tt lcoeff(3*x\verb|^|2+5*x*y\verb|^|2,y)}\end{center} 7812Output : 7813\begin{center}{\tt 5*x}\end{center} 7814 7815\subsection{Trailing coefficient degree of a polynomial : {\tt tcoeff}}\index{tcoeff} 7816\noindent{\tt tcoeff} takes as argument a polynomial given by a 7817symbolic expression 7818or by the list of its coefficients.\\ 7819{\tt tcoeff} returns the coefficient of the monomial of lowest degree 7820of this polynomial ({\tt tcoeff}=trailing coefficient).\\ 7821Input : 7822\begin{center}{\tt tcoeff([2,1,-1,0])}\end{center} 7823Output : 7824\begin{center}{\tt -1}\end{center} 7825 Input : 7826\begin{center}{\tt tcoeff(3*x\verb|^|2+5*x,x)}\end{center} 7827Output : 7828\begin{center}{\tt 5}\end{center} 7829 Input : 7830\begin{center}{\tt tcoeff(3*x\verb|^|2+5*x*y\verb|^|2,y)}\end{center} 7831Output : 7832\begin{center}{\tt 3*x\verb|^|2}\end{center} 7833 7834\subsection{Evaluation of a polynomial : {\tt peval polyEval}}\index{peval} 7835\index{polyEval} 7836\noindent{\tt peval} or {\tt polyEval} takes as argument a polynomial 7837{\tt p} given by the list of its coefficients and a real {\tt a} .\\ 7838{\tt peval} or {\tt polyEval} returns the exact or numeric value of 7839{\tt p(a)} using Horner's method.\\ 7840 Input : 7841\begin{center}{\tt peval([1,0,-1],sqrt(2))}\end{center} 7842Output : 7843\begin{center}{\tt sqrt(2)*sqrt(2)-1}\end{center} 7844Then : 7845\begin{center}{\tt normal(sqrt(2)*sqrt(2)-1)}\end{center} 7846Output : 7847\begin{center}{\tt {\tt 1}}\end{center} 7848Input : 7849\begin{center}{\tt peval([1,0,-1],1.4)}\end{center} 7850Output : 7851\begin{center}{\tt 0.96}\end{center} 7852 7853\subsection{Factorize $x^n$ in a polynomial : {\tt factor\_xn}}\index{factor\_xn} 7854\noindent{\tt factor\_xn} takes as argument a polynomial {\tt P}.\\ 7855{\tt factor\_xn} returns the polynomial {\tt P} written 7856as the product of its monomial of largest degree $x^n$ ({\tt n=degree(P)}) 7857with a rational fraction having a non-zero finite limit at infinity.\\ 7858 Input : 7859\begin{center}{\tt factor\_xn(-x\verb|^|4+3)}\end{center} 7860Output : 7861\begin{center}{\tt x\verb|^|4*(-1+3*x\verb|^|-4)}\end{center} 7862 7863\subsection{GCD of the coefficients of a polynomial : {\tt content}}\index{content|textbf} 7864\noindent{\tt content} takes as argument a polynomial {\tt P} given by 7865a symbolic expression or by the list of its coefficients.\\ 7866{\tt content} returns the content of {\tt P}, 7867that is the GCD (greatest common divisor) of the coefficients of 7868{\tt P}.\\ 7869Input : 7870\begin{center}{\tt content(6*x\verb|^|2-3*x+9)}\end{center} 7871or: 7872\begin{center}{\tt content([6,-3,9],x))}\end{center} 7873Output : 7874\begin{center}{\tt 3}\end{center} 7875 7876\subsection{Primitive part of a polynomial : {\tt primpart}}\index{primpart} 7877\noindent{\tt primpart} takes as argument a polynomial {\tt P} given by a 7878symbolic expression or by the list of its coefficients.\\ 7879{\tt primpart} returns the primitive part of {\tt P}, 7880that is {\tt P} divided by the GCD 7881(greatest common divisor) of its coefficients.\\ 7882Input : 7883\begin{center}{\tt primpart(6x\verb|^|2-3x+9)}\end{center} 7884or: 7885\begin{center}{\tt primpart([6,-3,9],x))}\end{center} 7886Output : 7887\begin{center}{\tt 2*x\verb|^|2-x+3}\end{center} 7888 7889\subsection{Factorization : {\tt collect}}\index{collect} 7890\noindent{\tt collect} takes as argument a polynomial or a list of 7891polynomials and optionally an algebraic extension like {\tt sqrt(n)} 7892(for $\sqrt{n}$).\\ 7893{\tt collect} factorizes the polynomial (or the polynomials in the 7894list) on the field of its coefficient (for example $\mathbb Q$) 7895or on the smallest extension containing the optional second argument (e.g. 7896$\mathbb Q[\sqrt{n}]$). In complex mode, the field is complexified.\\ 7897{\bf Examples} : 7898\begin{itemize} 7899\item Factorize $x^2-4$ over the integers, 7900input : 7901\begin{center}{\tt collect(x\verb|^|2-4)}\end{center} 7902Output in real mode : 7903\begin{center}{\tt (x-2)*(x+2)}\end{center} 7904\item Factorize $x^2+4$ over the integers, input : 7905\begin{center}{\tt collect(x\verb|^|2+4)}\end{center} 7906Output in real mode : 7907\begin{center}{\tt x\verb|^|2+4}\end{center} 7908Output in complex mode : 7909\begin{center}{\tt (x+2*i)*(x-2*i)}\end{center} 7910\item Factorize $x^2-2$ over the integers, input : 7911\begin{center}{\tt collect(x\verb|^|2-2)}\end{center} 7912Output in real mode : 7913\begin{center}{\tt x\verb|^|2-2}\end{center} 7914But if you input : 7915\begin{center}{\tt collect(sqrt(2)*(x\verb|^|2-2))}\end{center} 7916Output : 7917\begin{center}{\tt sqrt(2)*(x-sqrt(2))*(x+sqrt(2))}\end{center} 7918\item Factorize over the integers : 7919$$x^3-2x^2+1 \mbox{ and } x^2-x$$ 7920Input : 7921 \begin{center}{\tt collect([x\verb|^|3-2*x\verb|^|2+1,x\verb|^|2-x])}\end{center} 7922Output : 7923\begin{center}{\tt [(x-1)*(x\verb|^|2-x-1),x*(x-1)]}\end{center} 7924But, input : 7925 \begin{center}{\tt collect((x\verb|^|3-2*x\verb|^|2+1)*sqrt(5))}\end{center} 7926Output : 7927\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} 7928Or, input : 7929\begin{center}{\tt collect(x\verb|^|3-2*x\verb|^|2+1,sqrt(5))}\end{center} 7930Output : 7931\begin{center}{\tt ((2*sqrt(5)-19)*((sqrt(5)+25)*x-}\\ 7932 {\tt 13*sqrt(5)-15)*(-x+1)*((sqrt(5)+15)*x+7*sqrt(5)-5))/6820} 7933\end{center} 7934\end{itemize} 7935 7936\subsection{Factorization : {\tt factor factoriser}}\index{factor}\index{factoriser}\label{sec:factor} 7937\noindent {\tt factor} takes as argument a polynomial or a list of 7938polynomials and optionally an algebraic extension, e.g. {\tt sqrt(n)}.\\ 7939{\tt factor} factorizes the polynomial (or the polynomials in the list) on the 7940field of its coefficients (the field is complexified in complex mode) 7941or on the smallest extension containing the optional second argument. 7942Unlike {\tt collect}, 7943{\tt factor} will further factorize each factor of degree 2 7944if {\tt Sqrt} is checked in the {\tt cas} configuration 7945(see also \ref{sec:factore}). 7946You can check the current configuration in the status button under 7947{\tt Xcas} and change the configuration by hitting this status button.\\ 7948Input : 7949 \begin{center}{\tt factor(x\verb|^|2+2*x+1)}\end{center} 7950Output : 7951\begin{center}{\tt (x+1)\verb|^|2}\end{center} 7952Input : 7953\begin{center}{\tt factor(x\verb|^|4-2*x\verb|^|2+1)}\end{center} 7954Output : 7955\begin{center}{\tt (-x+1)\verb|^|2*(x+1)\verb|^|2}\end{center} 7956Input : 7957 \begin{center}{\tt factor(x\verb|^|3-2*x\verb|^|2+1)}\end{center} 7958Output if {\tt Sqrt} is not checked in the {\tt cas} configuration : 7959\begin{center}{\tt (x-1)*(x\verb|^|2-x-1)}\end{center} 7960Output if {\tt Sqrt} is checked in the {\tt cas} configuration : 7961\begin{center}{\tt (x-1)*(x+(sqrt(5)-1)/2)*(x+(-sqrt(5)-1)/2)}\end{center} 7962Input : 7963 \begin{center}{\tt factor(x\verb|^|3-2*x\verb|^|2+1,sqrt(5))}\end{center} 7964Output : 7965\begin{center}{\tt ((2*sqrt(5)-19)*((sqrt(5)+15)*x+}\\ 7966{\tt 7*sqrt(5)-5)*(-x+1)*((sqrt(5)+25)*x-13*sqrt(5)-15))/6820} 7967\end{center} 7968Input : 7969 \begin{center}{\tt factor(x\verb|^|2+1)}\end{center} 7970Output in real mode : 7971\begin{center}{\tt x\verb|^|2+1}\end{center} 7972Output in complex mode : 7973\begin{center}{\tt ((-i)*x+1)*((i)*x+1)}\end{center} 7974 7975\subsection{Square-free factorization : {\tt sqrfree}}\index{sqrfree} 7976\noindent{\tt sqrfree} takes as argument a polynomial.\\ 7977{\tt sqrfree} factorizes this polynomial as a product of 7978powers of coprime factors, where each factor has roots of multiplicity 1 7979(in other words, a factor and its derivative are coprime).\\ 7980Input : 7981\begin{center}{\tt sqrfree((x\verb|^|2-1)*(x-1)*(x+2))}\end{center} 7982Output : 7983 \begin{center}{\tt (x\verb|^|2+3*x+2)*(x-1)\verb|^|2}\end{center} 7984Input : 7985\begin{center}{\tt sqrfree((x\verb|^|2-1)\verb|^|2*(x-1)*(x+2)\verb|^|2)}\end{center} 7986Output : 7987 \begin{center}{\tt (x\verb|^|2+3*x+2)*(x-1)\verb|^|3}\end{center} 7988 7989\subsection{List of factors : {\tt factors}}\index{factors|textbf} 7990\noindent{\tt factors} has either a polynomial or a list of polynomials as 7991argument.\\ 7992{\tt factors} returns a list containing the factors of the polynomial 7993and their exponents.\\ 7994Input : 7995 \begin{center}{\tt factors(x\verb|^|2+2*x+1)}\end{center} 7996Output : 7997\begin{center}{\tt [x+1,2]}\end{center} 7998Input : 7999 \begin{center}{\tt factors(x\verb|^|4-2*x\verb|^|2+1)}\end{center} 8000Output : 8001\begin{center}{\tt [x+1,2,x-1,2]}\end{center} 8002Input : 8003 \begin{center}{\tt factors([x\verb|^|3-2*x\verb|^|2+1,x\verb|^|2-x])}\end{center} 8004Output : 8005\begin{center}{\tt [[x-1,1,x\verb|^|2-x-1,1],[x,1,x-1,1]]}\end{center} 8006Input : 8007 \begin{center}{\tt factors([x\verb|^|2,x\verb|^|2-1])}\end{center} 8008Output : 8009\begin{center}{\tt [[x,2],[x+1,1,x-1,1]]}\end{center} 8010 8011\subsection{Evaluate a polynomial : {\tt horner}}\index{horner} 8012\noindent{\tt horner} takes two arguments : a polynomial {\tt P} given by its 8013symbolic expression or by the list of its coefficients and a number {\tt a}.\\ 8014{\tt horner} returns {\tt P(a)} computed using Horner's method.\\ 8015Input : 8016\begin{center}{\tt horner(x\verb|^|2-2*x+1,2)}\end{center} 8017or : 8018\begin{center}{\tt horner([1,-2,1],2)}\end{center} 8019Output : 8020\begin{center}{\tt 1}\end{center} 8021 8022\subsection{Rewrite in terms of the powers of (x-a) : {\tt ptayl}}\index{ptayl} 8023{\tt ptayl} is used to rewrite a polynomial {\tt P} depending of {\tt x} 8024in terms of the powers of {\tt (x-a)} 8025({\tt ptayl} means polynomial Taylor)\\ 8026{\tt ptayl} takes two arguments: a polynomial {\tt P} given by a 8027symbolic expression or by the list of its coefficients and 8028a number {\tt a}.\\ 8029{\tt ptayl} returns the polynomial {\tt Q} such that {\tt Q(x-a)=P(x)}\\ 8030Input : 8031\begin{center}{\tt ptayl(x\verb|^|2+2*x+1,2)}\end{center} 8032Output, the polynomial Q: 8033\begin{center}{\tt x\verb|^|2+6*x+9}\end{center} 8034Input : 8035\begin{center}{\tt ptayl([1,2,1],2)}\end{center} 8036Output : 8037\begin{center}{\tt [1,6,9]}\end{center} 8038{\bf Remark} 8039\begin{center}{\tt P(x)=Q(x-a)}\end{center} 8040i.e. for the example :\\ 8041$x^2+2x+1=(x-2)^2+6(x-2)+9$ 8042 8043\subsection{Compute with the exact root of a polynomial : {\tt rootof}}\index{rootof} 8044Let $P$ and $Q$ be two polynomials given by the list of their coefficients 8045then {\tt rootof(P,Q)} gives the value $P(\alpha)$ where $\alpha$ is the 8046root of $Q$ with largest real part (and largest imaginary part in 8047case of equality).\\ 8048In exact computations, {\tt Xcas} will rewrite rational evaluations 8049of {\tt rootof} as a unique {\tt rootof} with degree$(P)<$degree$(Q)$. 8050If the resulting rootof is the solution of a second degree equation, 8051it will be simplified. 8052 8053{\bf Example}\\ 8054Let $\alpha$ be the root with largest imaginary 8055part of $Q(x)=x^4+10x^2+1$ (all roots of $Q$ have real part equal to 0). 8056\begin{itemize} 8057\item Compute $\displaystyle \frac{1}{\alpha}$. Input : 8058\begin{center}{\tt normal(1/rootof([1,0],[1,0,10,0,1])) }\end{center} 8059$P(x)=x$ is represented by [1,0] and $\alpha$ 8060by {\tt rootof([1,0],[1,0,10,0,1])}.\\ 8061Output : 8062\begin{center}{\tt rootof([[-1,0,-10,0],[1,0,10,0,1]])}\end{center} 8063i.e. : 8064\[ \frac{1}{\alpha}=-\alpha^3-10\alpha \] 8065\item Compute $\alpha2$. Input : 8066\begin{center}{\tt 8067 normal(rootof([1,0],[1,0,10,0,1])\verb|^|2)}\end{center} 8068or (since $P(x)=x^2$ is represented by [1,0,0]) input 8069\begin{center}{\tt normal(rootof([1,0,0],[1,0,10,0,1]))}\end{center} 8070Output : 8071\begin{center}{\tt -5-2*sqrt(6)}\end{center} 8072\end{itemize} 8073 8074\subsection{Exact roots of a polynomial : {\tt roots}}\index{roots} 8075\noindent{\tt roots} takes as arguments a symbolic 8076polynomial expression and the name of its variable.\\ 8077{\tt roots} returns a 2 columns matrix : each row is 8078the list of a root of the polynomial and its multiplicity.\\ 8079{\bf Examples} 8080\begin{itemize} 8081\item Find the roots of $P(x)=x^5-2x^4+x^3$.\\ 8082Input : 8083\begin{center}{\tt roots(x\verb|^|5-2*x\verb|^|4+x\verb|^|3) }\end{center} 8084Output : 8085\begin{center}{\tt [[8+3*sqrt(7),1],[8-3*sqrt(7),1],[0,3]]}\end{center} 8086\item Find the roots of 8087$x^{10}-15x^8+90x^6-270x^4+405x^2-243=(x^2-3)^5$.\\ 8088 Input : 8089\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} 8090Output : 8091\begin{center}{\tt[[sqrt(3),5],[-(sqrt(3)),5]]}\end{center} 8092\item Find the roots of $t^3-1$.\\ 8093Input : 8094\begin{center}{\tt roots(t\verb|^|3-1,t)}\end{center} 8095Output : 8096\begin{center}{\tt[[(-1+(i)*sqrt(3))/2,1],[(-1-(i)*sqrt(3))/2,1],[1,1]]}\end{center} 8097\end{itemize} 8098 8099\subsection{Coefficients of a polynomial defined by its roots : {\tt pcoeff pcoef}}\index{pcoeff}\index{pcoef} 8100\noindent{\tt pcoeff} (or {\tt pcoef}) takes as argument a list of 8101the roots of a polynomial $P$.\\ 8102{\tt pcoeff} (or {\tt pcoef}) returns a univariate polynomial having 8103these roots, 8104represented as the list of its coefficients by decreasing order.\\ 8105Input : 8106\begin{center}{\tt pcoef([1,2,0,0,3])}\end{center} 8107Output : 8108\begin{center}{\tt [1,-6,11,-6,0,0]}\end{center} 8109i.e. $(x-1)(x-2)(x^2)(x-3)=x^5-6x^4+11x^3-6x^2$. 8110 8111\subsection{Truncate of order $n$ : {\tt truncate}}\index{truncate} 8112\noindent{\tt truncate} takes as argument, a polynomial and an integer 8113{\tt n}.\\ 8114{\tt truncate} truncates this polynomial at order {\tt n} (removing 8115all terms of order greater or equal to {\tt n+1}).\\ 8116{\tt truncate} may be used to transform a series expansion into a 8117polynomial or to compute a series expansion step by step.\\ 8118Input : 8119\begin{center}{\tt truncate((1+x+x\verb|^|2/2)\verb|^|3,4)}\end{center} 8120Output : 8121\begin{center}{\tt (9*x\verb|^|4+16*x\verb|^|3+18*x\verb|^|2+12*x+4)/4}\end{center} 8122Input : 8123\begin{center}{\tt truncate(series(sin(x)),4)}\end{center} 8124Output : 8125\begin{center}{\tt (-x\verb|^|3-(-6)*x)/6}\end{center} 8126Note that the returned polynomial is normalized. 8127 8128\subsection{Convert a series expansion into a polynomial : {\tt convert convertir}}\index{convert}\index{convertir}\index{polynom@{\sl polynom}|textbf}\label{sec:convertpoly} 8129\noindent{\tt convert}, with the option {\tt polynom}, converts a Taylor series 8130into a polynomial. It should be used for operations like drawing 8131the graph of the Taylor series of a function near a point.\\ 8132{\tt convert} takes two arguments : an expression 8133and the option {\tt polynom}.\\ 8134{\tt convert} replaces the {\tt order\_size} functions by 0 inside the 8135expression.\\ 8136Input : 8137\begin{center}{\tt convert(taylor(sin(x)),polynom)}\end{center} 8138Output : 8139\begin{center}{\tt x+1/-6*x\verb|^|3+1/120*\verb|x^|5+x\verb|^|6*0}\end{center} 8140Input : 8141\begin{center}{\tt convert(series(sin(x),x=0,6),polynom)}\end{center} 8142Output : 8143\begin{center}{\tt x+1/-6*x\verb|^|3+1/120*\verb|x^|5+x\verb|^|7*0}\end{center} 8144 8145\subsection{Random polynomial : {\tt randpoly randPoly}}\index{randpoly}\index{randPoly} 8146\noindent{\tt randpoly} (or {\tt randPoly}) takes two arguments: the name of a 8147variable (by default {\tt x}) and an integer {\tt n} (the order of the 8148arguments is not important).\\ 8149{\tt randpoly} returns a polynomial with respect to the variable 8150given argument (or {\tt x} if none was provided), 8151of degree the second argument, having as coefficients 8152random integers evenly distributed on -99..+99.\\ 8153Input : 8154\begin{center}{\tt randpoly(t,4)}\end{center} 8155Output for example: 8156\begin{center}{\tt -8*t\verb|^|4-87*t\verb|^|3-52*t\verb|^|2+94*t+80}\end{center} 8157Input : 8158\begin{center}{\tt randpoly(4)}\end{center} 8159Output for example: 8160\begin{center}{\tt 70*x\verb|^|4-46*x\verb|^|3-7*x\verb|^|2-24*x+52}\end{center} 8161Input : 8162\begin{center}{\tt randpoly(4,u)}\end{center} 8163Output for example: 8164\begin{center}{\tt 2*u\verb|^|4+33*u\verb|^|3-6*u\verb|^|2-92*u-12}\end{center} 8165 8166\subsection{Change the order of variables : {\tt reorder}}\index{reorder} 8167\noindent{\tt reorder} takes two arguments : an expression and a vector 8168of variable names.\\ 8169{\tt reorder} expands the expression according to the order of variables 8170given as second argument.\\ 8171Input : 8172\begin{center}{\tt reorder(x\verb|^|2+2*x*a+a\verb|^|2+z\verb|^|2-x*z,[a,x,z])}\end{center} 8173Output : 8174\begin{center}{\tt a\verb|^|2+2*a*x+x\verb|^|2-x*z+z\verb|^|2}\end{center} 8175{\bf Warning} :\\ 8176The variables must be symbolic (if not, purge them before calling 8177{\tt reorder}) 8178 8179\subsection{Random list : {\tt ranm}}\index{ranm}\label{sec:ranm1} 8180\noindent{\tt ranm} takes as argument an integer {\tt n}.\\ 8181{\tt ranm} returns a list of {\tt n} random integers (between -99 and +99). 8182This list can be seen as the coefficients of an univariate 8183polynomial of degree {\tt n-1} 8184(see also \ref{sec:ranm2}).\\ % and \ref{sec:ranm3}).\\ 8185Input : 8186\begin{center}{\tt ranm(3)}\end{center} 8187Output : 8188\begin{center}{\tt [68,-21,56]}\end{center} 8189 8190\subsection{Lagrange's polynomial : {\tt lagrange interp}}\index{lagrange}\index{interp} 8191\noindent{\tt lagrange} takes as argument two lists of size {\tt n} (resp. a 8192matrix with two rows and {\tt n} columns) and the name of a variable 8193{\tt var} (by default {\tt x}).\\ 8194The first list (resp. row) corresponds to the abscissa values $x_k$ ($k=1..n$), 8195and the second list (resp. row) corresponds to ordinate values $y_k$ 8196($k=1..n$).\\ 8197{\tt lagrange} returns a polynomial expression {\tt P} 8198with respect to {\tt var} of degree 8199{\tt n-1}, such that $P(x_i)=y_i$.\\ 8200Input : 8201\begin{center}{\tt lagrange([[1,3],[0,1]])}\end{center} 8202or : 8203\begin{center}{\tt lagrange([1,3],[0,1])}\end{center} 8204Output : 8205\begin{center}{\tt (x-1)/2}\end{center} 8206since $\frac{x-1}{2}=0$ for $x=1$, and $\frac{x-1}{2}=1$ for $x=3$.\\ 8207Input : 8208\begin{center}{\tt lagrange([1,3],[0,1],y)}\end{center} 8209Output : 8210\begin{center}{\tt (y-1)/2}\end{center} 8211{\bf Warning}\\ 8212{\tt f:=lagrange([1,2],[3,4],y)} does not return a function 8213but an expression with respect to $y$. 8214To define $f$ as a function, input 8215\begin{center} 8216{\tt f:=unapply(lagrange([1,2],[3,4],x),x)} 8217\end{center} 8218Avoid {\tt f(x):=lagrange([1,2],[3,4],x)} since 8219the Lagrange polynomial would be computed each time {\tt f} is called 8220(indeed in a function definition, the second member of the assignment 8221is not evaluated). 8222Note also that \\ 8223{\tt g(x):=lagrange([1,2],[3,4])} would not work 8224since the default argument of {\tt lagrange} 8225would be global, hence not the same as the local 8226variable used for the definition of {\tt g}. 8227 8228\subsection{Trigonometric interpolation : {\tt triginterp}} 8229{\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 $. 8230 8231For 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 8232\[ T=\frac{n\,(b-a)}{n-1}, \] 8233where $ 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: 8234\begin{center} 8235 \begin{tabular}{|r|c|c|c|c|c|c|c|c|} 8236 \hline hour of the day&0&3&6&9&12&15&18&21\\ 8237 \hline temperature (deg~C)&11&10&17&24&32&26&23&19\\\hline 8238 \end{tabular} 8239\end{center} 8240Furthermore, assume that an estimate of the temperature at 13:45 is required. To obtain a trigonometric interpolation of the data, input : 8241\begin{center} 8242 {\tt tp:=triginterp([11,10,17,24,32,26,23,19],x=0..21)} 8243\end{center} 8244Output : 8245\begin{center} 8246 {\tt 81/4+(-21*sqrt(2)-42)/8*cos(pi/12*x)+\\ 8247 (-11*sqrt(2)-12)/8*sin(pi/12*x)+3/4*cos(pi/6*x)\\ 8248 -7/4*sin(pi/6*x)+(21*sqrt(2)-42)/8*cos(pi/4*x)\\ 8249 +(-11*sqrt(2)+12)/8*sin(pi/4*x)+1/2*cos(pi/3*x)} 8250\end{center} 8251Now a temperature at 13:45 hrs can be approximated with the value of {\tt tp} for $ x=13.75 $. Input : 8252\begin{center} 8253 {\tt tp | x=13.75} 8254\end{center} 8255Output : 8256\begin{center} 8257 {\tt 29.4863181684} 8258\end{center} 8259 8260If one of the input parameters is inexact, the result will be inexact too. For example, input : 8261\begin{center} 8262 {\tt Digits:=3;\\ 8263 triginterp([11,10,17,24,32,26,23,19],x=0..21.0)} 8264\end{center} 8265Output : 8266\begin{center} 8267 {\tt 0.5*cos(1.05*x)-1.54*cos(0.785*x)+0.75*cos(0.524*x)\\ 8268 -8.96*cos(0.262*x)-0.445*sin(0.785*x)-1.75*sin(0.524*x)\\ 8269 -3.44*sin(0.262*x)+20.2} 8270\end{center} 8271 8272\subsection{Natural splines: {\tt spline}}\index{spline|textbf} 8273\subsubsection{Definition} 8274Let $\sigma_n$ be a subdivision of a real interval $[a,b]$~: 8275\[ a=x_0,\quad x_1,\quad...,\quad x_n=b \] 8276$s$ is a spline function of degree $l$, if $s$ is a function from $[a,b]$ 8277to $\mathbb R$ such that~: 8278\begin{itemize} 8279\item $s$ has continuous derivatives up to the order $l-1$, 8280\item on each interval of the subdivision, $s$ 8281is a polynomial of degree less or equal than $l$. 8282\end{itemize} 8283 8284\subsubsection{Theorem} 8285The set of spline functions of degree $l$ on $\sigma_n$ is an 8286$\mathbb R$-vector subspace of dimension $n+l$. 8287 8288{\bf Proof}\\ 8289On $[a,x_1]$, $s$ is a polynomial $A$ of degree less or equal to 8290$l$, hence on $[a,x_1]$, $s=A(x)=a_0+a_1x+...a_lx^l$ and $A$ is a linear 8291combination of $1,x,...x^l$.\\ 8292On $[x_1,x_2]$, $s$ is a polynomial $B$ of degree less or equal to 8293$l$, hence on $[x_1,x_2]$, $s=B(x)=b_0+b_1x+...b_lx^l$.\\ 8294$s$ has continuous derivatives up to order $l-1$, hence : 8295\[ \forall 0 \leq j \leq l-1, \quad B^{(j)}(x_1)-A^{(j)}(x_1)=0\] 8296therefore $B(x)-A(x)=\alpha_1(x-x_1)^l$ or $B(x)=A(x)+\alpha_1(x-x_1)^l$.\\ 8297Define the function : 8298\[\mbox{q}_1(x) \mbox{ = } 8299\left\{ 8300\begin{array}{rcl} 83010 & \mbox{on} & [a,x_1] \\ 8302(x-x_1)^l & \mbox{on} & [x_1,b]\\ 8303\end{array} 8304\right. 8305\] 8306Hence : 8307\[ s|_{[a,x_2]}=a_0+a_1x+...a_lx^l+\alpha_1q_1(x) \] 8308On $[x_2,x_3]$, $s$ is a polynomial $C$ of degree less or equal than 8309$l$, hence on $[x_2,x_3]$, $s=C(x)=c_0+c_1x+...c_lx^l$.\\ 8310$s$ has continuous derivatives until $l-1$, hence : 8311\[ \forall 0 \leq j \leq l-1, \quad C^{(j)}(x_2)-B^{(j)}(x_2)=0\] 8312therefore $C(x)-B(x)=\alpha_2(x-x_2)^l$ or $C(x)=B(x)+\alpha_2(x-x_2)^l$.\\ 8313Define the function : 8314\[\mbox{q}_2(x) \mbox{ = } 8315\left\{ 8316\begin{array}{rcl} 83170 & \mbox{on} & [a,x_2] \\ 8318(x-x_2)^l & \mbox{on} & [x_2,b]\\ 8319\end{array} 8320\right. 8321\] 8322Hence : 8323$s|_{[a,x_3]}=a_0+a_1x+...a_lx^l+\alpha_1q_1(x)+\alpha_2q_2(x)$\\ 8324And so on, the functions are defined by : 8325\[\forall 1 \leq j \leq n-1, \mbox{q}_j(x) \mbox{ = } 8326\left\{ 8327\begin{array}{rcl} 83280 & \mbox{on} & [a,x_j] \\ 8329(x-x_j)^l & \mbox{on} & [x_j,b]\\ 8330\end{array} 8331\right. 8332\] 8333hence, 8334\[ s|_{[a,b]}=a_0+a_1x+...a_lx^l+\alpha_1q_1(x)+....+\alpha_{n-1}q_{n-1}(x) \] 8335and $s$ is a linear combination of $n+l$ independent functions 8336$1,x,..x^l,q_1,..q_{n-1}$. 8337 8338\subsubsection{Interpolation with spline functions} 8339If we want to interpolate a function $f$ on $\sigma_n$ by a spline function 8340 $s$ of degree $l$, then $s$ must verify $s(x_k)=y_k=f(x_k)$ for all 8341$0\leq k\leq n$. Hence there are $n+1$ conditions, and $l-1$ degrees of 8342liberty. We can therefore add $l-1$ conditions, these conditions are on the 8343derivatives of $s$ at $a$ and $b$. 8344 8345Hermite interpolation, natural interpolation and periodic interpolation 8346are three kinds of interpolation obtained by specifying three kinds 8347of constraints. The unicity of the 8348solution of the interpolation problem can be proved 8349for each kind of constraints. 8350 8351If $l$ is odd ($l=2m-1$), there are $2m-2$ degrees of 8352freedom. The constraints are defined by : 8353\begin{itemize} 8354\item Hermite interpolation 8355\[ \forall 1\leq j\leq m-1, \quad s^{(j)}(a)=f^{(j)}(a), 8356s^{(j)}(b)=f^{(j)}(b) \] 8357\item Natural interpolation 8358\[ \forall m \leq j \leq 2m-2, \quad s^{(j)}(a)=s^{(j)}(b)=0 \] 8359\item periodic interpolation 8360\[\forall 1\leq j\leq 2m-2, \quad s^{(j)}(a)=s^{(j)}(b) \] 8361\end{itemize} 8362 8363If $l$ is even ($l=2m$), there are $2m-1$ degrees of 8364liberty. The constraints are defined by : 8365\begin{itemize} 8366\item Hermite interpolation 8367\[ \forall 1\leq j\leq m-1, \quad s^{(j)}(a)=f^{(j)}(a), 8368s^{(j)}(b)=f^{(j)}(b) \] 8369and 8370\[s^{(m)}(a)=f^{(m)}(a)\] 8371\item Natural interpolation 8372\[ \forall m \leq j \leq 2m-2, \quad s^{(j)}(a)=s^{(j)}(b)=0 \] 8373and 8374\[s^{(2m-1)}(a)=0\] 8375\item Periodic interpolation 8376\[\forall 1\leq j\leq 2m-1, \quad s^{(j)}(a)=s^{(j)}(b) \] 8377\end{itemize} 8378A natural spline 8379is a spline function which verifies the natural interpolation constraints. 8380 8381{\tt spline} takes as arguments a list of abscissa (by increasing order), 8382a list of ordinates, a variable name, and a degree.\\ 8383{\tt spline} returns the natural spline function (with the specified degree 8384and crossing points) as a list of polynomials, each 8385polynomial being valid on an interval. 8386 8387Examples: 8388\begin{enumerate} 8389\item a natural spline of degree 3, crossing through the points 8390$x_0=0,y_0=1$, $x_1=1,y_1=3$ and $x_2=2, y_2=0$, input~: 8391\begin{center} 8392{\tt spline([0,1,2],[1,3,0],x,3)} 8393\end{center} 8394Output is a list of two polynomial expressions of $x$~: 8395\[[ -5*x^3/4+13*x/4+1, \quad 5*(x-1)^3/4-15*(x-1)^2/4+(x-1)/-2+3 ]\] 8396defined respectively on the intervals $[0,1]$ and $[1,2]$. 8397\item a natural spline of degree 4, crossing through the points 8398$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$, 8399input~: 8400\begin{center} 8401{\tt spline([0,1,2,3],[1,3,0,-1],x,4)} 8402\end{center} 8403Output is a list of three polynomial functions of $x$~: 8404\[ [(-62*x^4+304*x)/121+1,\] 8405\[(201*(x-1)^4-248*(x-1)^3-372*(x-1)^2+56*(x-1))/121+3,\] 8406\[(-139*(x-2)^4+556*(x-2)^3+90*(x-2)^2+-628*(x-2))/121]\] 8407defined respectively on the intervals $[0,1]$, $[1,2]$ and $[2,3]$. 8408\item The natural spline interpolation of $\cos$ on 8409$[0,\pi/2,3\pi/2]$, input~: 8410\begin{center} 8411{\tt spline([0,pi/2,3*pi/2],cos([0,pi/2,3*pi/2]),x,3)} 8412\end{center} 8413Output~: 8414\[ 8415[((3*\pi^3+(-7*\pi^2)*x+4*x^3)*1/3)/(\pi^3),\] 8416\[((15*\pi^3+(-46*\pi^2)*x+36*\pi*x^2-8*x^3)*1/12)/(\pi^3)] 8417\] 8418\end{enumerate} 8419 8420\subsection{Rational interpolation : {\tt thiele}}\index{thiele} 8421\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). 8422 8423This method computes Thiele interpolated continued fraction based on the concept of reciprocal differences. 8424 8425It is not guaranteed that $ R $ is continuous, i.e.~it may have singularities in the shortest segment which contains all components of $ \mathbf{x} $. 8426 8427\subsubsection{Examples} 8428\noindent Input : 8429\begin{center}{\tt thiele([[1,3],[2,4],[4,5],[5,8]],x)}\end{center} 8430Output : 8431\begin{center}{\tt (19*x\verb|^|2-45*x-154)/(18*x-78)}\end{center} 8432Input : 8433\begin{center}{\tt thiele([1,2,a],[3,4,5],3)}\end{center} 8434Output : 8435\begin{center}{\tt (13*a-29)/(3*a-7)}\end{center} 8436 8437In the following example, data is obtained by sampling the function $ f(x)=(1-x^4)\,\mathrm{e}^{1-x^3} $. 8438 8439\noindent Input : 8440\begin{center} 8441{\tt data\_x:=[-1,-0.75,-0.5,-0.25,0,}\\ 8442{\tt 0.25,0.5,0.75,1,1.25,1.5,1.75,2];} 8443{\tt data\_y:=[0.0,2.83341735599,2.88770329586,}\\ 8444{\tt 2.75030303645,2.71828182846,2.66568510781,}\\ 8445{\tt 2.24894558809,1.21863761951,0.0,-0.555711613283,} 8446{\tt -0.377871362418,-0.107135851128,-0.0136782294833];} 8447{\tt thiele(data\_x,data\_y,x)} 8448\end{center} 8449Output : 8450\begin{center} 8451{\tt (-1.55286115659*x\verb|^|6+5.87298387514*x\verb|^|5-5.4439152812*x\verb|^|4}\\ 8452{\tt +1.68655817708*x\verb|^|3-2.40784868317*x\verb|^|2-7.55954205222*x}\\ 8453{\tt +9.40462512097)/(x\verb|^|6-1.24295718965*x\verb|^|5-1.33526268624*x\verb|^|4}\\ 8454{\tt +4.03629272425*x\verb|^|3-0.885419321*x\verb|^|2-2.77913222418*x}\\ 8455{\tt +3.45976823393)} 8456\end{center} 8457 8458\section{Arithmetic and polynomials} 8459Polynomials are represented by expressions or by list of coefficients 8460by decreasing power order. In the first case, for instructions requiring 8461a main variable (like extended gcd computations), the variable 8462used by default is $x$ if not specified. For modular coefficients 8463in $\Z/n\Z$, use {\tt \% n } for each coefficient of the list 8464or apply it to the expression defining the polynomial. 8465 8466\subsection{The divisors of a polynomial : {\tt divis}}\index{divis} 8467\noindent{\tt divis} takes as argument a polynomial (or a list of 8468polynomials) and returns the list 8469 of the divisors of the polynomial(s).\\ 8470Input : 8471\begin{center}{\tt divis(x\verb|^|4-1)}\end{center} 8472Output : 8473\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} 8474\begin{center}{\tt (x+1)*(x-1),(x\verb|^|2+1)*(x+1)*(x-1)]}\end{center} 8475Input : 8476\begin{center}{\tt divis([x\verb|^|2,x\verb|^|2-1])}\end{center} 8477Output : 8478\begin{center}{\tt [[1,x,x\verb|^|2],[1,x+1,x-1,(x+1)*(x-1)]]}\end{center} 8479 8480\subsection{Euclidean quotient : {\tt quo}}\index{quo|textbf} 8481\noindent{\tt quo} returns the euclidean quotient $q$ of the 8482Euclidean division between two polynomials (decreasing power order). 8483If the polynomials are represented as 8484expressions, the variable may be specified as a third 8485argument.\\ 8486Input : 8487\begin{center}{\tt quo(x\verb|^|2+2*x +1,x)}\end{center} 8488Output : 8489\begin{center}{\tt x+2}\end{center} 8490Input : 8491\begin{center}{\tt quo(y\verb|^|2+2*y +1,y,y)}\end{center} 8492Output : 8493\begin{center}{\tt y+2}\end{center} 8494In list representation, the quotient of $x^2+2x+4$ by $x^2+x+2$ 8495one can also input : 8496\begin{center}{\tt quo([1,2,4],[1,1,2])}\end{center} 8497Output : 8498\begin{center}{\tt [1]}\end{center} 8499that is to say the polynomial {\tt 1}. 8500 8501\subsection{Euclidean quotient : {\tt Quo}}\index{Quo|textbf} 8502\noindent {\tt Quo} is the inert form of {\tt quo}.\\ 8503{\tt Quo} returns the euclidean quotient between two polynomials 8504(decreasing power division) without evaluation. 8505It is used when {\tt Xcas} is in Maple mode to compute 8506the euclidean quotient of the division of two 8507polynomials with coefficients in $\Z/p\Z$ using Maple-like syntax.\\ 8508In {\tt Xcas} mode, input : 8509\begin{center}{\tt Quo(x\verb|^|2+2*x+1,x)}\end{center} 8510Output : 8511\begin{center}{\tt quo(x\verb|^|2+2*x+1,x)}\end{center} 8512In {\tt Maple} mode, input : 8513\begin{center}{\tt Quo(x\verb|^|3+3*x,2*x\verb|^|2+6*x+5) mod 5}\end{center} 8514Output : 8515\begin{center}{\tt -(2)*x+1}\end{center} 8516The division was done using modular arithmetic, unlike with 8517\begin{center}{\tt quo(x\verb|^|3+3*x,2*x\verb|^|2+6*x+5) mod 5}\end{center} 8518where the division is done in $\Z[X]$ and reduced after to: 8519\begin{center}{\tt 3*x-9}\end{center} 8520If {\tt Xcas} is not in Maple mode, polynomial division 8521in $\Z/p\Z[X]$ is done e.g. by~: 8522\begin{center} 8523\verb|quo((x^3+3*x)% 5,(2x^2+6x+5)%5)| 8524\end{center} 8525 8526\subsection{Euclidean remainder : {\tt rem}}\index{rem|textbf} 8527\noindent{\tt rem} returns the euclidean remainder between two polynomials 8528(decreasing power division). If the polynomials are represented as 8529expressions, the variable may be specified as a third 8530argument.\\ 8531Input : 8532\begin{center}{\tt rem(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 8533Output : 8534\begin{center}{\tt x-1}\end{center} 8535To have the remainder of $x^2+2x+4$ by $x^2+x+2$ we can also input : 8536\begin{center}{\tt rem([1,2,4],[1,1,2])}\end{center} 8537Output : 8538\begin{center}{\tt [1,2]}\end{center} 8539i.e. the polynomial $x+2$. 8540 8541\subsection{Euclidean remainder: {\tt Rem}}\index{Rem|textbf} 8542\noindent{\tt Rem} is the inert form of {\tt rem}.\\ 8543{\tt Rem} returns the euclidean remainder between two polynomials 8544(decreasing power division) without evaluation. 8545It is used when {\tt Xcas} is in Maple mode to compute 8546the euclidean remainder of the division of two 8547polynomials with coefficients in $\Z/p\Z$ using Maple-like syntax.\\ 8548In {\tt Xcas} mode, input : 8549\begin{center}{\tt Rem(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 8550Output : 8551\begin{center}{\tt rem(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 8552In {\tt Maple} mode, input : 8553\begin{center}{\tt Rem(x\verb|^|3+3*x,2*x\verb|^|2+6*x+5) mod 5}\end{center} 8554Output : 8555\begin{center}{\tt 2*x}\end{center} 8556The division was done using modular arithmetic, unlike with 8557\begin{center}{\tt rem(x\verb|^|3+3*x,2*x\verb|^|2+6*x+5) mod 5}\end{center} 8558where the division is done in $\Z[X]$ and reduced after to: 8559\begin{center}{\tt 12*x}\end{center} 8560If {\tt Xcas} is not in Maple mode, polynomial division 8561in $\Z/p\Z[X]$ is done e.g. by~: 8562\begin{center}\verb|rem((x^3+3*x)% 5,(2x^2+6x+5)%5)| 8563\end{center} 8564 8565\subsection{Quotient and remainder : {\tt quorem divide}}\index{quorem|textbf}\index{divide|textbf}\label{sec:quorem} 8566\noindent{\tt quorem} (or {\tt divide}) returns the list of the quotient and 8567the remainder of the euclidean division (by decreasing power) of two 8568polynomials.\\ 8569Input : 8570\begin{center}{\tt quorem([1,2,4],[1,1,2]) }\end{center} 8571Output : 8572\begin{center}{\tt [poly1[1],poly1[1,2]]}\end{center} 8573Input : 8574\begin{center}{\tt quorem(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 8575Output : 8576\begin{center}{\tt [x,x-1]}\end{center} 8577 8578\subsection{GCD of two polynomials with the Euclidean algorithm: {\tt gcd}}\index{gcd}\label{sec:gcd} 8579\noindent{\tt gcd} denotes the gcd (greatest common divisor) of two polynomials 8580(or of a list of polynomials or of a sequence of polynomials) 8581(see also \ref{sec:igcd} for GCD of integers). 8582 8583{\bf Examples}\\ 8584Input : 8585\begin{center}{\tt gcd(x\verb|^|2+2*x+1,x\verb|^|2-1)}\end{center} 8586Output : 8587\begin{center}{\tt x+1 }\end{center} 8588Input : 8589\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} 8590or 8591\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} 8592Output : 8593\begin{center}{\tt x-1}\end{center} 8594 8595For polynomials with modular coefficients, input e.g. : 8596\begin{center}{\tt gcd((x\verb|^|2+2*x+1) mod 5,(x\verb|^|2-1) mod 5)}\end{center} 8597Output : 8598\begin{center}{\tt x \% 5}\end{center} 8599Note that : 8600\begin{center}{\tt gcd(x\verb|^|2+2*x+1,x\verb|^|2-1) mod 5}\end{center} 8601will output : 8602\begin{center}{\tt 1}\end{center} 8603since the mod operation is done after the GCD is computed in $\Z[X]$. 8604 8605\subsection{GCD of two polynomials with the Euclidean algorithm : {\tt Gcd}}\index{Gcd} 8606\noindent{\tt Gcd} is the inert form of {\tt gcd}. 8607{\tt Gcd} returns the gcd (greatest common divisor) of two polynomials 8608(or of a list of polynomials or of a sequence of polynomials) without 8609evaluation. It is used when {\tt Xcas} is in Maple mode to compute 8610the gcd of polynomials with coefficients in $\Z/p\Z$ using Maple-like syntax.\\ 8611Input in {\tt Xcas} mode : 8612\begin{center}{\tt Gcd(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 8613Output : 8614\begin{center}{\tt gcd(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 8615Input in {\tt Maple} mode : 8616\begin{center}{\tt Gcd(x\verb|^|2+2*x,x\verb|^|2+6*x+5) mod 5}\end{center} 8617Output : 8618\begin{center}{\tt 1}\end{center} 8619 8620\subsection{Choosing the GCD algorithm of two polynomials : 8621{\tt ezgcd heugcd modgcd psrgcd}}\index{ezgcd}\index{psrgcd}\index{modgcd}\index{heugcd} 8622\noindent{\tt ezgcd heugcd modgcd psrgcd} denote the gcd 8623(greatest common divisor) of two univariate or multivariate 8624polynomials with coefficients 8625in $\Z$ or $\Z[i]$ using a specific algorithm~: 8626\begin{itemize} 8627\item {\tt ezgcd} ezgcd algorithm, 8628\item {\tt heugcd} heuristic gcd algorithm, 8629\item {\tt modgcd} modular algorithm, 8630\item {\tt psrgcd} sub-resultant algorithm. 8631\end{itemize} 8632Input : 8633\begin{center}{\tt ezgcd(x\verb|^|2-2*x*y+y\verb|^|2-1,x-y)}\end{center} 8634or : 8635\begin{center}{\tt heugcd(x\verb|^|2-2*x*y+y\verb|^|2-1,x-y)}\end{center} 8636or : 8637\begin{center}{\tt modgcd(x\verb|^|2-2*x*y+y\verb|^|2-1,x-y)}\end{center} 8638or : 8639\begin{center}{\tt psrgcd(x\verb|^|2-2*x*y+y\verb|^|2-1,x-y)}\end{center} 8640Output : 8641\begin{center}{\tt 1 }\end{center} 8642Input : 8643\begin{center}{\tt ezgcd((x+y-1)*(x+y+1),(x+y+1)\verb|^|2)}\end{center} 8644or : 8645\begin{center}{\tt heugcd((x+y-1)*(x+y+1),(x+y+1)\verb|^|2)}\end{center} 8646or : 8647\begin{center}{\tt modgcd((x+y-1)*(x+y+1),(x+y+1)\verb|^|2)}\end{center} 8648Output : 8649\begin{center}{\tt x+y+1}\end{center} 8650Input : 8651\begin{center}{\tt psrgcd((x+y-1)*(x+y+1),(x+y+1)\verb|^|2)}\end{center} 8652Output : 8653\begin{center}{\tt -x-y-1}\end{center} 8654Input : 8655\begin{center}{\tt ezgcd((x+1)\verb|^|4-y\verb|^|4,(x+1-y)\verb|^|2)}\end{center} 8656Output : 8657\begin{center}{\tt "GCD not successful Error: Bad Argument Value"}\end{center} 8658 But input : 8659\begin{center}{\tt heugcd((x+1)\verb|^|4-y\verb|^|4,(x+1-y)\verb|^|2)}\end{center} 8660or : 8661\begin{center}{\tt modgcd((x+1)\verb|^|4-y\verb|^|4,(x+1-y)\verb|^|2)}\end{center} 8662or : 8663\begin{center}{\tt psrgcd((x+1)\verb|^|4-y\verb|^|4,(x+1-y)\verb|^|2)}\end{center} 8664Output : 8665\begin{center}{\tt x-y+1 }\end{center} 8666 8667\subsection{LCM of two polynomials : {\tt lcm}}\index{lcm}\label{sec:lcm} 8668\noindent{\tt lcm} returns the LCM (Least Common Multiple) of two polynomials 8669(or of a list of polynomials or of a sequence of polynomials) 8670(see \ref{sec:ilcm} for LCM of integers).\\ 8671Input : 8672\begin{center}{\tt lcm(x\verb|^|2+2*x+1,x\verb|^|2-1)}\end{center} 8673Output : 8674\begin{center}{\tt (x+1)*(x\verb|^|2-1)}\end{center} 8675Input : 8676\begin{center}{\tt lcm(x,x\verb|^|2+2*x+1,x\verb|^|2-1)}\end{center} 8677or 8678\begin{center}{\tt lcm([x,x\verb|^|2+2*x+1,x\verb|^|2-1])}\end{center} 8679Output : 8680\begin{center}{\tt (x\verb|^|2+x)*(x\verb|^|2-1)}\end{center} 8681 8682\subsection{B\'ezout's Identity : {\tt egcd gcdex}}\index{egcd}\index{gcdex} 8683This function computes the polynomial coefficients of B\'ezout's 8684Identity (also known as Extended Greatest Common Divisor). 8685Given two polynomials $A(x),B(x)$, {\tt egcd} computes 3 polynomials 8686$U(x),V(x)$ and $D(x)$ such that~: 8687\[ U(x)*A(x)+V(x)*B(x)=D(x)=GCD(A(x),B(x)) \] 8688{\tt egcd} takes 2 or 3 arguments: the polynomials $A$ and $B$ as 8689expressions in terms of a variable, if the variable is not specified 8690it will default to $x$. Alternatively, $A$ and $B$ may be given 8691as list-polynomials.\\ 8692Input : 8693\begin{center}{\tt egcd(x\verb|^|2+2*x+1,x\verb|^|2-1)}\end{center} 8694Output : 8695\begin{center}{\tt [1,-1,2*x+2]}\end{center} 8696Input : 8697\begin{center}{\tt egcd([1,2,1],[1,0,-1])}\end{center} 8698Output : 8699\begin{center}{\tt [[1],[-1],[2,2]]}\end{center} 8700Input : 8701\begin{center}{\tt egcd(y\verb|^|2-2*y+1,y\verb|^|2-y+2,y)}\end{center} 8702Output : 8703\begin{center}{\tt [y-2,-y+3,4]}\end{center} 8704Input : 8705\begin{center}{\tt egcd([1,-2,1],[1,-1,2])}\end{center} 8706Output : 8707\begin{center}{\tt [[1,-2],[-1,3],[4]]}\end{center} 8708 8709\subsection{Solving au+bv=c over polynomials: {\tt abcuv}}\index{abcuv} 8710{\tt abcuv} solves the polynomial equation 8711\[ C(x)=U(x)*A(x)+V(x)*B(x) \] 8712where $A,B,C$ are given polynomials and $U$ and $V$ are unknown 8713polynomials. $C$ must be a multiple of the gcd of $A$ and $B$ 8714for a solution to exist. {\tt abcuv} takes 3 expressions as argument, 8715and an optional variable specification (which defaults to $x$) 8716and returns a list of 2 expressions ($U$ and $V$). Alternatively, the 8717polynomials $A,B,C$ may be entered as list-polynomials. 8718 8719Input : 8720\begin{center}{\tt abcuv(x\verb|^|2+2*x+1 ,x\verb|^|2-1,x+1)}\end{center} 8721Output : 8722\begin{center}{\tt [1/2,1/-2]}\end{center} 8723Input : 8724\begin{center}{\tt abcuv(x\verb|^|2+2*x+1 ,x\verb|^|2-1,x\verb|^|3+1)}\end{center} 8725Output : 8726\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} 8727Input : 8728\begin{center}{\tt abcuv([1,2,1],[1,0,-1],[1,0,0,1])}\end{center} 8729Output : 8730\begin{center}{\tt [poly1[1/2,1/-2,1/2],poly1[1/-2,1/2,1/-2]]}\end{center} 8731 8732% 8733 8734\subsection{Chinese remainders : {\tt chinrem}}\index{chinrem} 8735\noindent{\tt chinrem} takes two lists as argument, each list being made of 2 8736polynomials (either expressions or as a list of coefficients in decreasing 8737order). If the polynomials are expressions, an optional third 8738argument may be provided to specify the main variable, by default 8739{\tt x} is used. 8740{\tt chinrem([A,R],[B,Q])} returns the list of two polynomials 8741{\tt P} and {\tt S} such that : 8742\[ S=R Q, \quad P=A \pmod R, \quad P=B \pmod Q \] 8743If {\tt R} and {\tt Q} are coprime, a solution {\tt P} always exists 8744and all the solutions are congruent modulo {\tt S=R*Q}. 8745For example, assume we want to solve : 8746\[ {\tt \left\{ \begin{array}{rlr} P(x)=&x\ &\bmod\ (x^2+1)\\ 8747 P(x)=&x-1\ &\bmod\ (x^2-1) \end{array}\right.} \] 8748Input : 8749\begin{center}{\tt chinrem([[1,0],[1,0,1]],[[1,-1],[1,0,-1]])}\end{center} 8750Output : 8751\begin{center}{\tt [[1/-2,1,1/-2],[1,0,0,0,-1]]}\end{center} 8752or : 8753\begin{center}{\tt chinrem([x,x\verb|^|2+1],[x-1,x\verb|^|2-1])}\end{center} 8754Output : 8755\begin{center}{\tt [1/-2*x\verb|^|2+x+1/-2,x\verb|^|4-1]}\end{center} 8756hence $\displaystyle P(x)=-\frac{x^2-2.x+1}{2} \ (\bmod\ x^4-1)$\\ 8757Another example, input : 8758\begin{center}{\tt chinrem([[1,2],[1,0,1]],[[1,1],[1,1,1]])}\end{center} 8759Output : 8760\begin{center}{\tt [[-1,-1,0,1],[1,1,2,1,1]]}\end{center} 8761or : 8762\begin{center}{\tt chinrem([y+2,y\verb|^|2+1],[y+1,y\verb|^|2+y+1],y)}\end{center} 8763Output : 8764\begin{center}{\tt [-y\verb|^|3-y\verb|^|2+1,y\verb|^|4+y\verb|^|3+2*y\verb|^|2+y+1]}\end{center} 8765 8766\subsection{Cyclotomic polynomial : {\tt cyclotomic}}\index{cyclotomic} 8767\noindent{\tt cyclotomic} takes an integer $n$ as argument and 8768returns the list of the coefficients of the cyclotomic 8769polynomial of index $n$. This 8770is the polynomial having the $n$-th primitive roots of unity 8771as zeros (an $n$-th root of unity is primitive if the set of its 8772powers is the set of all the $n$-th roots of unity). 8773 8774For example, let $n=4$, the fourth roots of unity are: 8775$\{ 1,i,-1,-i\}$ and the primitive roots are: $\{i,-i\}$. 8776Hence, the cyclotomic polynomial of index $4$ is $(x-i).(x+i)=x^2+1$. 8777Verification: 8778\begin{center}{\tt cyclotomic(4)}\end{center} 8779Output : 8780\begin{center}{\tt [1,0,1]}\end{center} 8781Another example, input : 8782\begin{center}{\tt cyclotomic(5)}\end{center} 8783Output : 8784\begin{center}{\tt [1,1,1,1,1]}\end{center} 8785Hence, the cyclotomic polynomial of index $5$ is $x^4+x^3+x^2+x+1$ 8786which divides $x^5-1$ since $(x-1)*(x^4+x^3+x^2+x+1)=x^5-1$.\\ 8787Input : 8788\begin{center}{\tt cyclotomic(10)}\end{center} 8789Output : 8790\begin{center}{\tt [1,-1,1,-1,1]}\end{center} 8791Hence, the cyclotomic polynomial of index $10$ is $x^4-x^3+x^2-x+1$ and 8792\[ (x^5-1)*(x+1)*(x^4-x^3+x^2-x+1)=x^{10}-1 \] 8793Input : 8794\begin{center}{\tt cyclotomic(20)}\end{center} 8795Output : 8796\begin{center}{\tt [1,0,-1,0,1,0,-1,0,1]}\end{center} 8797Hence, the cyclotomic polynomial of index $20$ is $x^8-x^6+x^4-x^2+1$ and 8798\[ (x^{10}-1)*(x^2+1)*(x^8-x^6+x^4-x^2+1)=x^{20}-1 \] 8799 8800\subsection{Sturm sequences and number of sign changes 8801of $P$ on $(a,\ b]$ : {\tt sturm}}\index{sturm} 8802\noindent{\tt sturm} takes two or four arguments : $P$ a polynomial expression 8803or $P/Q$ a rational fraction and a variable name or $P$ a polynomial 8804expression, a variable name and two real or complex numbers $a$ and $b$. 8805 8806If {\tt sturm} takes two arguments, {\tt sturm} returns the list of the Sturm 8807sequences and multiplicities of the square-free factors of $P$ (or 8808$P/Q$) (in this case {\tt sturm} behaves like {\tt sturmseq}). 8809 8810If {\tt sturm} takes four arguments, it behaves like {\tt sturmab}~: 8811\begin{itemize} 8812\item if $a$ and $b$ are reals, 8813{\tt sturm} returns the number of sign changes of $P$ on $(a,\ b]$ 8814\item if $a$ or $b$ are complex, 8815{\tt sturm} returns the number of complex roots of $P$ in the rectangle 8816having $a$ and $b$ as opposite vertices. 8817\end{itemize} 8818Input : 8819\begin{center}{\tt sturm(2*x\verb|^|3+2,x)}\end{center} 8820Output : 8821\begin{center}{\tt [2,[[1,0,0,1],[3,0,0],-9],1]}\end{center} 8822Input : 8823\begin{center}{\tt sturm((2*x\verb|^|3+2)/(x+2),x)}\end{center} 8824Output : 8825\begin{center}{\tt [2,[[1,0,0,1],[3,0,0],-9],1,[[1,2],1]]}\end{center} 8826Input : 8827\begin{center}{\tt sturm(x\verb|^|2*(x\verb|^|3+2),x,-2,0)}\end{center} 8828Output : 8829\begin{center}{\tt 1}\end{center} 8830 8831\subsection{Number of zeros in $[a,b)$ : {\tt sturmab}}\index{sturmab} 8832\noindent{\tt sturmab} takes four arguments: a polynomial expression $P$, a 8833variable name and two real or complex numbers $a$ and $b$ 8834\begin{itemize} 8835\item if $a$ and $b$ are reals, 8836{\tt sturmab} returns the number of sign changes of $P$ on $(a,\ b]$. 8837In other words, it returns the number of zeros in $[a,b)$ of the 8838polynomial $P/G$ where $G=\mbox{gcd}(P,\mbox{diff}(P))$. 8839\item if $a$ or $b$ are complex, 8840{\tt sturmab} returns the number of complex roots of $P$ in the rectangle 8841having $a$ and $b$ as opposite vertices. 8842\end{itemize} 8843Input : 8844\begin{center}{\tt sturmab(x\verb|^|2*(x\verb|^|3+2),x,-2,0)}\end{center} 8845Output : 8846\begin{center}{\tt 1}\end{center} 8847Input : 8848\begin{center}{\tt sturmab(x\verb|^|3-1,x,-2-i,5+3i)}\end{center} 8849Output : 8850\begin{center}{\tt 3}\end{center} 8851Input : 8852\begin{center}{\tt sturmab(x\verb|^|3-1,x,-i,5+3i)}\end{center} 8853Output : 8854\begin{center}{\tt 1}\end{center} 8855{\bf Warning !!!!}\\ 8856 $P$ is defined by its symbolic expression.\\ 8857Input :\\ 8858{\tt sturmab([1,0,0,2,0,0],x,-2,0)},\\ 8859Output :\\ 8860{\tt Bad argument type}. 8861 8862\subsection{Sturm sequences : {\tt sturmseq}}\index{sturmseq} 8863\noindent{\tt sturmseq} takes as argument, a polynomial expression $P$ or a 8864rational fraction $P/Q$ and returns the list of the Sturm sequences 8865of the square-free factors of odd multiplicity of $P$ (or of $P/Q$). 8866For $F$ a square-free factor of odd multiplicity, the Sturm 8867sequence $R_1,R_2,...$ is made from $F$, $F'$ by a recurrence 8868relation~: 8869\begin{itemize} 8870\item 8871$R_1$ is the opposite of the euclidean division remainder of $F$ by 8872$F'$ then, 8873\item 8874$R_2$ is the opposite of the euclidean division remainder of $F'$ by 8875$R_1$, 8876\item ... 8877\item and so on until $R_k=0$. 8878\end{itemize} 8879Input : 8880\begin{center}{\tt sturmseq(2*x\verb|^|3+2)}\end{center} 8881or 8882\begin{center}{\tt sturmseq(2*y\verb|^|3+2,y)}\end{center} 8883Output : 8884\begin{center}{\tt [2,[[1,0,0,1],[3,0,0],-9],1]}\end{center} 8885The first term gives the content of the numerator (here 2), 8886then the Sturm sequence (in list representation) $[x^3+1,3x^2,-9]$.\\ 8887Input : 8888\begin{center}{\tt sturmseq((2*x\verb|^|3+2)/(3*x\verb|^|2+2),x)}\end{center} 8889Output : 8890\begin{center}{\tt [2,[[1,0,0,1],[3,0,0],-9],1,[1,[[3,0,2],[6,0],-72]]}\end{center} 8891The first term gives the content of the numerator (here 2), 8892then the Sturm sequence of the numerator ([[1,0,0,1],[3,0,0],-9]), 8893then the content of the denominator (here 1) and the Sturm 8894sequence of the denominator ([[3,0,2],[6,0],-72]). As expressions, 8895$[x^3+1,3x^2, -9]$ is the Sturm sequence of the numerator and 8896$[3x^2+2,6x,-72]$ is the Sturm sequence of the denominator.\\ 8897Input : 8898\begin{center}{\tt sturmseq((x\verb|^|3+1)\verb|^|2,x)}\end{center} 8899Output : 8900\begin{center}{\tt [1,1]}\end{center} 8901Indeed $F=1$.\\ 8902Input : 8903\begin{center}{\tt sturmseq(3*(3*x\verb|^|3+1)/(2*x+2),x)}\end{center} 8904Output : 8905\begin{center}{\tt[3,[[3,0,0,1],[9,0,0],-81],2,[[1,1],1]]}\end{center} 8906The first term gives the content of the numerator 8907(here {\tt 3}),\\ 8908the second term gives the Sturm sequence of the numerator 8909(here {\tt 3x\verb|^|3+1, 9x\verb|^|2, -81}),\\ 8910the third term gives the content of the denominator (here 8911{\tt 2}),\\ 8912the fourth term gives the Sturm sequence of the denominator 8913({\tt x+1,1}).\\ 8914{\bf Warning !!!!}\\ 8915$P$ is defined by its symbolic expression.\\ 8916Input : 8917\begin{center} 8918{\tt sturmseq([1,0,0,1],x)} 8919\end{center} 8920Output : 8921\begin{center} 8922{\tt Bad argument type}. 8923\end{center} 8924 8925\subsection{Sylvester matrix of two polynomials : {\tt sylvester}}\index{sylvester} 8926\noindent{\tt sylvester} takes two polynomials as arguments.\\ 8927{\tt sylvester} returns the Sylvester matrix $S$ of these polynomials.\\ 8928If $A(x)=\sum_{i=0}^{i=n} a_ix^i$ and 8929$B(x)=\sum_{i=0}^{i=m}b_ix^i$ are 2 polynomials, their Sylvester matrix 8930$S$ is a square matrix of size {\tt m+n} where {\tt m=degree(B(x))} and 8931{\tt n=degree(A(x))}. The {\tt m} first lines are made with the $A(x)$ 8932coefficients, so that : 8933$$\left(\begin{array}{ccccccc} 8934s_{11}=a_n & s_{12}=a_{n-1}& \cdots & s_{1(n+1)}=a_0 & 0 & \cdots & 0\\ 8935s_{21}=0 & s_{22}=a_{n}& \cdots & s_{2(n+1)}=a_1 & s_{2(n+2)}=a_0 & \cdots & 0\\ 8936\vdots &\vdots &\vdots &\ddots &\vdots &\ddots &\vdots\\ 8937s_{m1}=0 & s_{m2}=0& \cdots & s_{m(n+1)}=a_{m-1} & s_{m(n+2)}=a_{m-2} & \cdots&a_0 8938\end{array}\right)$$ 8939and the {\tt n} further lines are made with the $B(x)$ 8940coefficients, so that : 8941$$\left(\begin{array}{ccccccc} 8942s_{(m+1)1}=b_m & s_{(m+1)2}=b_{m-1}& \cdots & s_{(m+1)(m+1)}=b_0 & 0 & \cdots & 0\\ 8943\vdots &\vdots &\vdots &\ddots &\vdots &\ddots &\vdots\\ 8944s_{(m+n)1}=0 & s_{(m+n)2}=0& \cdots & s_{(m+n)(m+1)}=b_{n-1} & b_{n-2} &\cdots&b_0 8945\end{array}\right)$$ 8946Input : 8947\begin{center}{\tt sylvester(x\verb|^|3-p*x+q,3*x\verb|^|2-p,x)}\end{center} 8948Output : 8949\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} 8950Input : 8951\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} 8952Output : 8953\begin{center}{\tt -4*p\verb|^|3--27*q\verb|^|2}\end{center} 8954 8955\subsection{Resultant of two polynomials : {\tt resultant}}\index{resultant} 8956\noindent{\tt resultant} takes as argument two polynomials and 8957returns the resultant of the two polynomials.\\ 8958The resultant of two polynomials is the determinant of their 8959Sylvester matrix $S$. 8960The Sylvester matrix $S$ of two polynomials $A(x)=\sum_{i=0}^{i=n} a_ix^i$ 8961and $B(x)=\sum_{i=0}^{i=m} b_ix^i$ 8962is a square matrix with $m+n$ rows and columns; its first $m$ rows 8963are made from the coefficients of $A(X)$: 8964$$\left(\begin{array}{ccccccc} 8965s_{11}=a_n & s_{12}=a_{n-1}& \cdots & s_{1(n+1)}=a_0 & 0 & \cdots & 0\\ 8966s_{21}=0 & s_{22}=a_{n}& \cdots & s_{2(n+1)}=a_1 & s_{2(n+2)}=a_0 & \cdots & 0\\ 8967\vdots &\vdots &\vdots &\ddots &\vdots &\ddots &\vdots\\ 8968s_{m1}=0 & s_{m2}=0& \cdots & s_{m(n+1)}=a_{m-1} & s_{m(n+2)}=a_{m-2} & \cdots&a_0 8969\end{array}\right)$$ 8970and the following $n$ rows are made in the same way from the 8971coefficients of $B(x)$ : 8972$$\left(\begin{array}{ccccccc} 8973s_{(m+1)1}=b_m & s_{(m+1)2}=b_{m-1}& \cdots & s_{(m+1)(m+1)}=b_0 & 0 & \cdots & 0\\ 8974\vdots &\vdots &\vdots &\ddots &\vdots &\ddots &\vdots\\ 8975s_{(m+n)1}=0 & s_{(m+n)2}=0& \cdots & s_{(m+n)(m+1)}=b_{n-1} & b_{n-2} &\cdots&b_0 8976\end{array}\right)$$ 8977 8978If $A$ and $B$ have integer coefficients with non-zero resultant $r$, 8979then the polynomials equation 8980\[ AU+BV=r\] 8981has a unique solution $U,V$ such that degree$(U)<$degree$(B)$ and 8982degree$(V)<$degree$(A)$, and this solution has integer coefficients. 8983 8984Input : 8985\begin{center}{\tt resultant(x\verb|^|3-p*x+q,3*x\verb|^|2-p,x)}\end{center} 8986Output : 8987\begin{center}{\tt -4*p\verb|^|3--27*q\verb|^|2}\end{center} 8988{\bf Remark}\\ 8989discriminant(P)=resultant(P,P'). 8990 8991{\bf An example using the resultant}\\ 8992Let, $F1$ and $F2$ be 2 fixed points in the plane and 8993$A$, a variable point on the circle of center $F1$ and radius $2a$. 8994Find the cartesian equation of the set of points $M$, intersection of 8995the line $F1A$ and of the perpendicular bisector of $F2A$. 8996 8997Geometric answer~: 8998\[ MF1+MF2=MF1+MA=F1A=2a\] 8999hence $M$ is on an ellipse with focus $F1,F2$ and major axis $2a$. 9000 9001Analytic answer~: 9002In the Cartesian coordinate system with center $F1$ 9003and $x$-axis having the same 9004direction as the vector $F1F2$, the coordinates of $A$ are : 9005\[ A= (2a\cos(\theta),2a\sin(\theta)) \] 9006where $\theta$ is the $(Ox,OA)$ angle. 9007Now choose $t=\tan(\theta/2)$ as parameter, so that the coordinates 9008of $A$ are rational functions with respect to $t$. 9009More precisely~: 9010\[ A=(ax,ay)=(2a\frac{1-t^2}{1+t^2},2a\frac{2t}{1+t^2}) \] 9011If $F1F2=2c$ and if $I$ is the midpoint of $AF2$, 9012since the coordinates of $F2$ are $F2=(2c,0)$, the coordinates 9013of $I$ 9014\[ I=(c+ax/2;ay/2)=(c+a\frac{1-t^2}{1+t^2};a\frac{2t}{1+t^2}) \] 9015$IM$ is orthogonal to $AF2$, hence $M=(x;y)$ satisfies the equation 9016$eq1=0$ where 9017\[ eq1:=(x-ix)*(ax-2*c)+(y-iy)*ay \] 9018But $M=(x,y)$ is also on $F1A$, hence $M$ satisfies the equation $eq2=0$ 9019\[ eq2:=y/x-ay/ax \] 9020The resultant of both equations with respect to $t$ 9021{\tt resultant(eq1,eq2,t)} is a polynomial $eq3$ depending on the 9022variables $x,y$, independent of $t$ which is the cartesian equation 9023of the set of points $M$ when $t$ varies.\\ 9024Input : 9025\begin{center} 9026{\tt ax:=2*a*(1-t\verb|^|2)/(1+t\verb|^|2);ay:=2*a*2*t/(1+t\verb|^|2);}\\ 9027{\tt ix:=(ax+2*c)/2; iy:=(ay/2)}\\ 9028{\tt eq1:=(x-ix)*(ax-2*c)+(y-iy)*ay}\\ 9029{\tt eq2:=y/x-ay/ax}\\ 9030{\tt factor(resultant(eq1,eq2,t))} 9031\end{center} 9032Output gives as resultant :\\ 9033\begin{center} 9034${\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 9035c+2\cdot x\cdot c^3-a^4+2\cdot a^2\cdot c^2+}$\\ 9036${\tt a^2\cdot y^2-c^4))}$ 9037\end{center} 9038The factor ${\tt -64\cdot (x^2+y^2)}$ is always different from zero, 9039hence the locus equation of $M$~: 9040\[ {\tt x^2a^2-x^2c^2+-2xa^2c+2xc^3-a^4+2a^2c^2+a^2y^2-c^4=0} \] 9041If the frame origin is $O$, the middle point of $F1F2$, 9042we find the cartesian equation of an ellipse. 9043To make the change of origin 9044$\overrightarrow{F1M}=\overrightarrow{F1O}+\overrightarrow{OM}$, input : 9045\[ {\tt normal(subst(x^2\cdot a^2-x^2\cdot c^2+-2\cdot x\cdot a^2\cdot 9046c+2\cdot x\cdot c^3-a^4+2\cdot a^2\cdot c^2+} \] 9047\[ {\tt a^2\cdot y^2-c^4,[x,y]=[c+X,Y]))} \] 9048Output : 9049\[ {\tt -c^2*X^2+c^2*a^2+X^2*a^2-a^4+a^2*Y^2} \] 9050or if $b^2=a^2-c^2$, input : 9051\[ {\tt 9052 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))} \] 9053Output : 9054\[ {\tt -a^2*b^2+a^2*Y^2+b^2*X^2} \] 9055that is to say, after division by $a^2*b^2$, $M$ verifies the equation : 9056\[ \frac{X^2}{a^2}+\frac{Y^2}{b^2}=1 \] 9057 9058{\bf Another example using the resultant}\\ 9059Let $F1$ and $F2$ be fixed points and $A$ a variable point on the 9060circle of center $F1$ and radius $2a$. 9061Find the cartesian equation of the hull of $D$, the segment bisector 9062of $F2A$. 9063 9064The segment bisector of $F2A$ is tangent to the ellipse of focus 9065$F1,F2$ and major axis $2a$. 9066 9067In the Cartesian coordinate system of center $F1$ and $x$-axis having the same 9068direction than the vector $F1F2$, the coordinates of $A$ are : 9069\[ A= (2a\cos(\theta);2a\sin(\theta)) \] 9070where $\theta$ is the $(Ox,OA)$ angle. 9071Choose $t=\tan(\theta/2)$ as parameter such that the coordinates of $A$ are 9072rational functions with respect to $t$. 9073More precisely~: 9074\[ A=(ax;ay)=(2a\frac{1-t^2}{1+t^2};2a\frac{2t}{1+t^2}) \] 9075If $F1F2=2c$ and if $I$ is the middle point of $AF2$:\\ 9076\[ F2=(2c,0), \quad 9077I=(c+ax/2;ay/2)=(c+a\frac{1-t^2}{1+t^2};a\frac{2t}{1+t^2}) 9078\] 9079Since $D$ is orthogonal to $AF2$, the equation of $D$ is 9080$eq1=0$ where 9081\[ eq1:=(x-ix)*(ax-2*c)+(y-iy)*ay \] 9082So, the hull of $D$ is the locus of $M$, the intersection point of $D$ 9083and $D'$ where $D'$ has equation $eq2:=diff(eq1,t)=0$. 9084Input : 9085\begin{center} 9086{\tt ax:=2*a*(1-t\verb|^|2)/(1+t\verb|^|2);ay:=2*a*2*t/(1+t\verb|^|2);}\\ 9087{\tt ix:=(ax+2*c)/2; iy:=(ay/2)}\\ 9088{\tt eq1:=normal((x-ix)*(ax-2*c)+(y-iy)*ay)}\\ 9089{\tt eq2:=normal(diff(eq1,t))}\\ 9090{\tt factor(resultant(eq1,eq2,t))} 9091\end{center} 9092Output gives as resultant : 9093\begin{center} 9094${\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+}$\\ 9095${\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)}$\\ 9096\end{center} 9097The factor ${\tt -64\cdot (x^2+y^2)}$ is always different from zero, 9098therefore the locus equation is : 9099\[ {\tt x^2a^2-x^2c^2+-2xa^2c+2xc^3-a^4+2a^2c^2+a^2y^2-c^4=0} \] 9100If $O$, the middle point of $F1F2$, is chosen as origin, 9101we find again the cartesian equation of the ellipse~: 9102\[ \frac{X^2}{a^2}+\frac{Y^2}{b^2}=1 \] 9103 9104\section{Orthogonal polynomials} 9105\subsection{Legendre polynomials: {\tt legendre}}\index{legendre} 9106\noindent{\tt legendre} takes as argument an integer $n$ and 9107optionally a variable name (by default $x$).\\ 9108{\tt legendre} returns the Legendre polynomial of degree $n$ : it is 9109 a polynomial $L(n,x)$, solution of the differential equation: 9110$$(x^2-1) y''-2 x y'-n(n+1) y=0$$ 9111The Legendre polynomials verify the following recurrence relation: 9112\[ L(0,x)=1, \quad 9113L(1,x)=x, \quad 9114L(n,x)=\frac{2n-1}{n}x L(n-1,x)-\frac{n-1}{n}L(n-2,x) 9115\] 9116These polynomials are orthogonal for the scalar product : 9117\[ <f,g>=\int_{-1}^{+1}f(x)g(x)\ dx \] 9118Input : 9119\begin{center}{\tt legendre(4)}\end{center} 9120Output : 9121\begin{center}{\tt (35*x\verb|^|4+-30*x\verb|^|2+3)/8}\end{center} 9122Input : 9123\begin{center}{\tt legendre(4,y)}\end{center} 9124Output : 9125\begin{center}{\tt (35*y\verb|^|4+-30*y\verb|^|2+3)/8}\end{center} 9126 9127\subsection{Hermite polynomial : {\tt hermite}}\index{hermite} 9128\noindent{\tt hermite} takes as argument an integer $n$ and 9129 optionally a variable name (by default $x$).\\ 9130{\tt hermite} returns the Hermite polynomial of degree $n$.\\ 9131If $H(n,x)$ denotes the Hermite polynomial of degree $n$, 9132the following recurrence relation holds: 9133\[ H(0,x)=1, \quad 9134H(1,x)=2x, \quad 9135H(n,x)=2xH(n-1,x)-2(n-1)H(n-2,x) \] 9136These polynomials are orthogonal for the scalar product: 9137\[ <f,g>=\int_{-\infty}^{+\infty}f(x)g(x)e^{-x^2}dx \] 9138Input : 9139\begin{center}{\tt hermite(6)}\end{center} 9140Output : 9141\begin{center}{\tt 64*x\verb|^|6+-480*x\verb|^|4+720*x\verb|^|2-120}\end{center}Input : 9142\begin{center}{\tt hermite(6,y)}\end{center} 9143Output : 9144\begin{center}{\tt 64*y\verb|^|6+-480*y\verb|^|4+720*y\verb|^|2-120}\end{center} 9145 9146\subsection{Laguerre polynomials: {\tt laguerre}}\index{laguerre} 9147\noindent{\tt laguerre} takes as argument an integer $n$ and optionally 9148 a variable name (by default $x$) and a parameter name (by default $a$).\\ 9149{\tt laguerre} returns the Laguerre polynomial of degree $n$ and of 9150parameter $a$.\\ 9151If $L(n,a,x)$ denotes the Laguerre polynomial of degree $n$ and 9152parameter $a$, the following recurrence relation holds: 9153\[ L(0,a,x)=1, \quad 9154L(1,a,x)=1+a-x, \quad 9155L(n,a,x)=\frac{2n+a-1-x}{n}L(n-1,a,x)-\frac{n+a-1}{n}L(n-2,a,x) \] 9156These polynomials are orthogonal for the scalar product 9157\[ <f,g>=\int_{0}^{+\infty}f(x)g(x)x^ae^{-x}dx \] 9158Input : 9159\begin{center}{\tt laguerre(2)}\end{center} 9160Output : 9161\begin{center}{\tt (a\verb|^|2+-2*a*x+3*a+x\verb|^|2+-4*x+2)/2}\end{center} 9162Input : 9163\begin{center}{\tt laguerre(2,y)}\end{center} 9164Output : 9165\begin{center}{\tt (a\verb|^|2+-2*a*y+3*a+y\verb|^|2+-4*y+2)/2}\end{center} 9166Input : 9167\begin{center}{\tt laguerre(2,y,b)}\end{center} 9168Output : 9169\begin{center}{\tt (b\verb|^|2+-2*b*y+3*b+y\verb|^|2+-4*y+2)/2}\end{center} 9170 9171\subsection{Tchebychev polynomials of the first kind: {\tt tchebyshev1}}\index{tchebyshev1} 9172\noindent{\tt tchebyshev1} takes as argument an integer $n$ and optionally a 9173variable name (by default $x$).\\ 9174{\tt tchebyshev1} returns the Tchebychev polynomial of first kind 9175of degree $n$.\\ 9176The Tchebychev polynomial of first kind $T(n,x)$ is defined by 9177\[ T(n,x)= \cos(n \arccos(x)) \] 9178and satisfy the recurrence relation: 9179\[ T(0,x)=1, \quad 9180T(1,x)=x, \quad T(n,x)=2xT(n-1,x)-T(n-2,x) \] 9181The polynomials $T(n,x)$ are orthogonal for the scalar product 9182\[ <f,g>=\int_{-1}^{+1}\frac{f(x)g(x)}{\sqrt{1-x^2}}dx \] 9183Input : 9184\begin{center}{\tt tchebyshev1(4)}\end{center} 9185Output : 9186\begin{center}{\tt 8*x\verb|^|4+-8*x\verb|^|2+1}\end{center} 9187Input : 9188\begin{center}{\tt tchebyshev1(4,y)}\end{center} 9189Output : 9190\begin{center}{\tt 8*y\verb|^|4+-8*y\verb|^|2+1}\end{center} 9191Indeed 9192\begin{eqnarray*} 9193\cos( 4 x)&=&Re((\cos(x)+i \sin(x))^4) \\ 9194 &=&\cos(x)^4-6.\cos(x)^2 (1-\cos(x)^2)+((1-\cos(x)^2)^2 \\ 9195 &=&T(4,\cos(x)) 9196\end{eqnarray*} 9197 9198\subsection{Tchebychev polynomial of the second kind: {\tt tchebyshev2}}\index{tchebyshev2} 9199\noindent{\tt tchebyshev2} takes as argument an integer $n$ and optionally 9200a variable name (by default $x$).\\ 9201{\tt tchebyshev2} returns the Tchebychev polynomial of second kind 9202of degree $n$.\\ 9203The Tchebychev polynomial of second kind $U(n,x)$ is defined by: 9204$$U(n,x)=\frac{\sin((n+1).\arccos(x))}{\sin(\arccos(x))}$$ 9205or equivalently: 9206$$\sin((n+1)x)=\sin(x)*U(n,\cos(x))$$ 9207Then $U(n,x)$ satisfies the recurrence relation: 9208\[ U(0,x)=1, \quad 9209U(1,x)=2x, \quad 9210U(n,x)=2xU(n-1,x)-U(n-2,x) \] 9211The polynomials $U(n,x)$ are orthogonal for the scalar product 9212\[ <f,g>=\int_{-1}^{+1}f(x)g(x)\sqrt{1-x^2}dx \] 9213Input : 9214\begin{center}{\tt tchebyshev2(3)}\end{center} 9215Output : 9216\begin{center}{\tt 8*x\verb|^|3+-4*x}\end{center} 9217Input : 9218\begin{center}{\tt tchebyshev2(3,y)}\end{center} 9219Output : 9220\begin{center}{\tt 8*y\verb|^|3+-4*y}\end{center} 9221Indeed: 9222\[ \sin(4 x)=\sin(x)*(8*\cos(x)^3-4 \cos(x))=\sin(x)*U(3,\cos(x)) \] 9223 9224 9225 9226\section{Gr\"obner basis and Gr\"obner reduction} 9227 \subsection{Gr\"obner basis : {\tt gbasis}}\index{gbasis} 9228\label{sec:gbasis} 9229\noindent{\tt gbasis} takes at least two arguments 9230\begin{itemize} 9231\item a vector of multivariate polynomials 9232\item a vector of variables names, 9233\end{itemize} 9234Optional arguments may be used to specify the ordering and 9235algorithms. By default, the ordering is lexicographic (with respect to the 9236list of variable names ordering) 9237and the polynomials are written in decreasing power orders with 9238respect to this order. 9239For example, the output will be like 9240$...+x^2 y^4 z^3+x^2 y^3 z^4+...$ if the second argument is $[x,y,z]$ because 9241$(2,4,3)>(2,3,4)$ but the output would be like 9242$...+x^2 y^3z^4+x^2 y^4 z^3+...$ if the second argument is $[x,z,y]$.\\ 9243{\tt gbasis} returns a Gr\"obner basis of the polynomial ideal spanned 9244by these polynomials. 9245 9246{\bf Property}\\ 9247If $I$ is an ideal and if $(G_k)_{k \in K}$ is a Gr\"obner basis of this 9248ideal $I$ then, if $F$ is a non-zero polynomial in $I$, the greatest monomial 9249of $F$ is divisible by the greatest monomial of one of the $G_k$. 9250In other words, if you do an euclidean division of $F\neq 0$ 9251by the corresponding $G_k$, take the remainder of this division, do 9252again the same and so on, at some point you get a null remainder. 9253 9254Input : 9255\begin{center}{\tt gbasis([2*x*y-y\verb|^|2,x\verb|^|2-2*x*y],[x,y])} 9256\end{center} 9257Output : 9258\begin{center}{\tt 9259 [4*x\verb|^|2+-4*y\verb|^|2,2*x*y-y\verb|^|2,-(3*y\verb|^|3)]} 9260\end{center} 9261 9262As indicated above, {\tt gbasis} may have more than 2 arguments~: 9263\begin{itemize} 9264\item {\tt plex} (lexicographic only), {\tt tdeg} (total degree then 9265lexicographic order), 9266{\tt revlex} (total degree then inverse lexicographic order), to 9267specify an order on the monomials 9268({\tt plex} is the order by default), 9269\item {\tt with\_cocoa=true} or {\tt with\_cocoa=false}, if you want to use 9270the {\tt CoCoA} library to compute the Gr\"obner basis (recommended, 9271requires that {\tt CoCoA} support compiled in) 9272 9273\item {\tt with\_f5=true} or {\tt with\_f5=false} for using the F5 algorithm 9274of the {\tt CoCoA} library . 9275In this case the specified order is not used (the polynomials are 9276homogenized). 9277\end{itemize} 9278Input~: 9279\begin{center} 9280{\tt gbasis([x1+x2+x3,x1*x2+x1*x3+x2*x3,x1*x2*x3-1], [x1,x2,x3],tdeg,with\_cocoa=false)} 9281\end{center} 9282Output 9283\begin{center} 9284\verb|[x3^3-1,-x2^2-x2*x3-x3^2,x1+x2+x3]| 9285\end{center} 9286 9287\subsection{Gr\"obner reduction : {\tt greduce}}\index{greduce} 9288\noindent{\tt greduce} has three arguments : a multivariate 9289polynomial, 9290a vector made of polynomials which is supposed to be a Gr\"obner 9291basis, and a vector of variable names.\\ 9292{\tt greduce} returns the reduction of 9293the polynomial given as first argument 9294with respect to the Gr\"obner basis given as the second argument. 9295It is 0 if and only if the polynomial belongs to the ideal. 9296 9297Input : 9298\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} 9299Output : 9300\begin{center}{\tt y\verb|^|2-2}\end{center} 9301that is to say $xy-1=\frac{1}{2}(y^2-2)\ \bmod I$ where $I$ is the ideal 9302generated by the Gr\"obner basis $[x^2-y^2,2xy-y^2,y^3]$, because 9303$ y^2-2$ is the euclidean division remainder of $2(xy-1)$ by $G_2=2x y-y^2$.\\ 9304% {\bf Remark}\\ 9305% The multiplcative constant can be fixed by observing how the constant 9306% coefficient is converted. In the example, the constant coefficient 9307% {\tt -1} is converted into the constant coefficient {\tt -2}, so 9308% the multiplcative constant is {\tt 1/2}. 9309 9310Like {\tt gbasis} (cf. \ref{sec:gbasis}), 9311{\tt greduce} may have more than 3 arguments to specify ordering and 9312algorithm if they differ from the default (lexicographic ordering).\\ 9313Input~: 9314\begin{center} 9315{\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)} 9316\end{center} 9317Output 9318\begin{center} 9319\verb|x2| 9320\end{center} 9321 9322\subsection{Build a polynomial from its evaluation : {\tt genpoly}}\index{genpoly} 9323\noindent{\tt genpoly} takes three arguments : a polynomial $P$ with $n-1$ 9324variables, an integer $b$ and the name of a variable {\tt var}.\\ 9325{\tt genpoly} returns the polynomial $Q$ with $n$ variables (the $P$ variables 9326and the variable {\tt var} given as second argument), such that~: 9327\begin{itemize} 9328\item {\tt subst(Q,var=b)==P} 9329\item the coefficients of $Q$ belongs to the interval $(-b/2 \ , \ b/2]$ 9330\end{itemize} 9331In other words, $P$ is written in base $b$ but using the convention 9332that the euclidean remainder belongs to $]-b/2 \ ; \ b/2]$ 9333(this convention is also known as s-mod representation). 9334Input : 9335\begin{center}{\tt genpoly(61,6,x) }\end{center} 9336Output : 9337\begin{center}{\tt 2*x\verb|^|2-2*x+1}\end{center} 9338Indeed 61 divided by 6 is 10 with remainder 1, then 10 divided by 6 is 2 9339with remainder -2 (instead of the usual quotient 1 and remainder 4 out of bounds), 9340\[ 61=2*6^2-2*6+1 \] 9341Input : 9342\begin{center}{\tt genpoly(5,6,x) }\end{center} 9343Output : 9344\begin{center}{\tt x-1}\end{center} 9345Indeed : $5=6-1$\\ 9346Input : 9347\begin{center}{\tt genpoly(7,6,x) }\end{center} 9348Output : 9349\begin{center}{\tt x+1}\end{center} 9350Indeed : $7=6+1$\\ 9351Input : 9352\begin{center}{\tt genpoly(7*y+5,6,x) }\end{center} 9353Output : 9354\begin{center}{\tt x*y+x+y-1}\end{center} 9355Indeed : $x*y+x+y-1=y(x+1)+(x-1)$\\ 9356Input : 9357\begin{center}{\tt genpoly(7*y+5*z\verb|^2|,6,x)}\end{center} 9358Output : 9359\begin{center}{\tt x*y+x*z+y-z}\end{center} 9360Indeed : $x*y+x*z+y-z=y*(x+1)+z*(x-1)$ 9361 9362\section{Rational fractions} 9363\subsection{Numerator : {\tt getNum}}\index{getNum}\label{sec:getnum} 9364\noindent {\tt getNum} takes as argument a rational fraction 9365and returns the numerator of this fraction. Unlike {\tt numer}, 9366{\tt getNum} does not simplify the fraction before extracting 9367the numerator.\\ 9368Input : 9369\begin{center}{\tt getNum((x\verb|^|2-1)/(x-1)) }\end{center} 9370Output : 9371\begin{center}{\tt x\verb|^|2-1}\end{center} 9372Input : 9373\begin{center}{\tt getNum((x\verb|^|2+2*x+1)/(x\verb|^|2-1)) }\end{center} 9374Output : 9375\begin{center}{\tt x\verb|^|2+2*x+1}\end{center} 9376 9377\subsection{Numerator after simplification : {\tt numer}}\index{numer}\label{sec:numer} 9378\noindent{\tt numer} takes as argument a rational fraction 9379and returns the numerator of the irreducible representation of 9380this fraction (see also \ref{sec:inumer}).\\ 9381Input : 9382\begin{center}{\tt numer((x\verb|^|2-1)/(x-1)) }\end{center} 9383Output : 9384\begin{center}{\tt x+1}\end{center} 9385Input : 9386\begin{center}{\tt numer((x\verb|^|2+2*x+1)/(x\verb|^|2-1)) }\end{center} 9387Output : 9388\begin{center}{\tt x+1}\end{center} 9389 9390\subsection{Denominator : {\tt getDenom}}\index{getDenom}\label{sec:getdenom} 9391\noindent{\tt getDenom} takes as argument a rational fraction and returns the 9392denominator of this fraction. Unlike {\tt denom}, 9393{\tt getDenom} does not simplify the fraction before extracting 9394the denominator.\\ 9395Input : 9396\begin{center}{\tt getDenom((x\verb|^|2-1)/(x-1)) }\end{center} 9397Output : 9398\begin{center}{\tt x-1}\end{center} 9399Input : 9400\begin{center}{\tt getDenom((x\verb|^|2+2*x+1)/(x\verb|^|2-1)) }\end{center} 9401Output : 9402\begin{center}{\tt x\verb|^|2-1}\end{center} 9403 9404\subsection{Denominator after simplification : {\tt denom}}\index{denom}\label{sec:denom} 9405\noindent{\tt denom} (or {\tt getDenom}) takes as argument a rational fraction 9406and returns the denominator of an irreducible representation 9407of this fraction (see also \ref{sec:idenom}).\\ 9408Input : 9409\begin{center}{\tt denom((x\verb|^|2-1)/(x-1)) }\end{center} 9410Output : 9411\begin{center}{\tt 1}\end{center} 9412Input : 9413\begin{center}{\tt denom((x\verb|^|2+2*x+1)/(x\verb|^|2-1)) }\end{center} 9414Output : 9415\begin{center}{\tt x-1}\end{center} 9416 9417\subsection{Numerator and denominator : {\tt f2nd fxnd}}\index{fxnd|textbf}\index{f2nd|textbf}\label{sec:fxnd} 9418\noindent{\tt f2nd} (or {\tt fxnd}) takes as argument a rational fraction and 9419returns the list of the numerator and the denominator of the irreducible 9420representation of this fraction (see also \ref{sec:ifxnd}).\\ 9421Input : 9422\begin{center}{\tt f2nd((x\verb|^|2-1)/(x-1)) }\end{center} 9423Output : 9424\begin{center}{\tt [x+1,1]}\end{center} 9425Input : 9426\begin{center}{\tt f2nd((x\verb|^|2+2*x+1)/(x\verb|^|2-1)) }\end{center} 9427Output : 9428\begin{center}{\tt [x+1,x-1]}\end{center} 9429 9430\subsection{Simplify : {\tt simp2}}\index{simp2}\label{sec:simp2} 9431\noindent{\tt simp2} takes as argument two polynomials (or two integers\ see 9432\ref{sec:isimp2}). 9433These two polynomials are seen as the numerator and denominator 9434of a rational fraction.\\ 9435{\tt simp2} returns a list of two polynomials seen as the numerator 9436and denominator of the irreducible representation of 9437this rational fraction.\\ 9438 Input : 9439\begin{center}{\tt simp2(x\verb|^|3-1,x\verb|^|2-1)}\end{center} 9440Output : 9441\begin{center}{\tt [x\verb|^|2+x+1,x+1]}\end{center} 9442 9443\subsection{Common denominator : {\tt comDenom}}\index{comDenom|textbf} 9444\noindent{\tt comDenom} takes as argument a sum of rational fractions.\\ 9445{\tt comDenom} rewrite the sum as a unique rational fraction. 9446The denominator of this rational fraction is the common denominator of the 9447rational fractions given as argument.\\ 9448Input : 9449\begin{center}{\tt comDenom(x-1/(x-1)-1/(x\verb|^|2-1))}\end{center} 9450Output : 9451\begin{center}{\tt (x\verb|^|3+-2*x-2)/(x\verb|^|2-1)}\end{center} 9452 9453\subsection{Integer and fractional part : {\tt propfrac}}\index{propfrac}\label{sec:propfrac} 9454\noindent{\tt propfrac} takes as argument a rational fraction.\\ 9455{\tt propfrac} rewrites this rational fraction as the sum of its 9456integer part and proper fractional part.\\ 9457{\tt propfrac(A(x)/B(x))} writes the fraction $\frac{A(x)}{B(x)}$ (after 9458reduction), as : 9459\[ Q(x)+\frac{R(x)}{B(x)} \quad \mbox{ where } R(x)=0 9460\mbox{ or } 0\leq \mbox{degree}(R(x))< \mbox{degree}(B(x)) \] 9461Input : 9462\begin{center}{\tt propfrac((5*x+3)*(x-1)/(x+2))}\end{center} 9463Output : 9464\begin{center}{\tt 5*x-12+21/(x+2)}\end{center} 9465 9466\subsection{Partial fraction expansion : {\tt partfrac}}\index{partfrac|textbf}\label{sec:convertparf} 9467{\tt partfrac} takes as argument a rational fraction.\\ 9468{\tt partfrac} returns the partial fraction expansion of this rational 9469fraction.\\ 9470The {\tt partfrac} command is equivalent to the {\tt convert} command with 9471{\tt parfrac} (or {\tt partfrac} or {\tt fullparfrac}) as option 9472(see also \ref{sec:convert}).\\ 9473{\bf Example} :\\ 9474Find the partial fraction expansion of : 9475$$\frac{x^5-2x^3+1}{x^4-2x^3+2x^2-2x+1}$$ 9476Input : 9477\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} 9478Output in real mode : 9479\begin{center}{\tt x+2-1/(2*(x-1))+(x-3)/(2*(x\verb|^|2+1)) }\end{center} 9480Output in complex mode: 9481\begin{center}{\tt x+2+(-1+2*i)/((2-2*i)*((i)*x+1))+1/(2*(-x+1))+}\end{center} 9482\begin{center}{\tt (-1-2*i)/((2-2*i)*(x+i))}\end{center} 9483 9484\section{Exact roots of a polynomial} 9485\subsection{Exact bounds for complex roots of a polynomial : 9486{\tt complexroot}}\index{complexroot} 9487\noindent {\tt complexroot} takes 2 or 4 arguments : a polynomial and a real 9488number $\epsilon$ and optionally two complex numbers $\alpha,\beta$.\\ 9489{\tt complexroot} returns a list of vectors. 9490\begin{itemize} 9491\item If {\tt complexroot} has 2 arguments, 9492the elements of each vector are 9493\begin{itemize} 9494\item either an interval (the 9495boundaries of this interval are the opposite vertices of a rectangle with sides 9496parallel to the axis and containing a complex root of the polynomial) and the 9497multiplicity of this root.\\ 9498Let the interval be $[a_1+ib_1,a_2+ib_2]$ then $|a_1-a_2|<\epsilon$, 9499$|b_1-b_2|<\epsilon$ and the root $a+ib$ verifies 9500$a_1\leq a \leq a_2$ and $b_1\leq b \leq b_2$. 9501\item or the value of an exact complex root of 9502the polynomial and the multiplicity of this root 9503\end{itemize} 9504\item If {\tt complexroot} has 4 arguments, {\tt complexroot} returns a list of 9505vectors as above, but only for the roots lying in 9506the rectangle with sides parallel to the axis having $\alpha,\beta$ as 9507opposite vertices.\\ 9508\end{itemize} 9509To find the roots of $x^3+1$, input: 9510\begin{center}{\tt complexroot(x\verb|^|3+1,0.1)}\end{center} 9511Output : 9512\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} 9513Hence, for $x^3+1$ : 9514\begin{itemize} 9515\item -1 is a root of multiplicity 1, 9516\item 1/2+i*$b$ is a root of multiplicity 1 with $-7/8\leq b \leq 9517 -13/16$, 9518\item 1/2+i*$c$ is a root of multiplicity 1 with $13/16\leq c \leq 9519 7/8$. 9520\end{itemize} 9521To find the roots of $x^3+1$ lying inside the rectangle 9522of opposite vertices $-1,1+2*i$, input: 9523\begin{center}{\tt complexroot(x\verb|^|3+1,0.1,-1,1+2*i)}\end{center} 9524Output : 9525\begin{center}{\tt [[-1,1],[[(8+13*i)/16,(4+7*i)/8],1]]}\end{center} 9526 9527\subsection{Exact bounds for real roots of a polynomial : {\tt realroot}}\index{realroot} 9528\noindent {\tt realroot} has 2 or 4 arguments : a polynomial and a real number 9529$\epsilon$ and optionally two reals numbers $\alpha,\beta$.\\ 9530{\tt realroot} returns a list of vectors. 9531\begin{itemize} 9532\item If {\tt realroot} has 2 arguments, the elements of each vector are 9533\begin{itemize} 9534\item 9535either a real interval containing a real root of the polynomial 9536and the multiplicity of this root. 9537Let the interval be $[a_1,a_2]$ then $|a_1-a_2|<\epsilon$ and 9538the root $a$ verifies $a_1\leq a \leq a_2$. 9539\item or the value of an exact real root of the 9540polynomial and the multiplicity of this root. 9541\end{itemize} 9542\item If {\tt realroot} has 4 arguments, {\tt realroot} returns a list of 9543vectors as above, but only for the roots inside 9544the interval $[\alpha,\beta]$. 9545\end{itemize} 9546To find the real roots of $x^3+1$, input: 9547\begin{center}{\tt realroot(x\verb|^|3+1, 0.1)}\end{center} 9548Output : 9549\begin{center}{\tt [[-1,1]] }\end{center} 9550To find the real roots of $x^3-x^2-2x+2$, input: 9551\begin{center}{\tt realroot(x\verb|^|3-x\verb|^|2-2*x+2, 0.1)}\end{center} 9552Output : 9553\begin{center}{\tt [[1,1],[[(-3)/2,(-45)/32],1],[[45/32,3/2],1]]}\end{center} 9554To find the real roots of $x^3-x^2-2x+2$ in the interval $[0;2]$, input: 9555\begin{center}{\tt realroot(x\verb|^|3-x\verb|^|2-2*x+2, 0.1,0,2)}\end{center} 9556Output : 9557\begin{center}{\tt [[1,1],[[11/8,23/16],1]]}\end{center} 9558 9559\subsection{Exact values of rational roots of a polynomial : 9560{\tt rationalroot}}\index{rationalroot} 9561\noindent {\tt rationalroot} takes 1 or 3 arguments : a polynomial and 9562optionally two real numbers $\alpha,\beta$. 9563\begin{itemize} 9564\item If {\tt rationalroot} has 1 argument, {\tt rationalroot} returns the list 9565of the value of the rational roots of the polynomial without multiplicity. 9566\item If {\tt rationalroot} has 3 arguments, {\tt rationalroot} returns only 9567the rational roots of the polynomial which are in the interval 9568$[\alpha,\beta]$. 9569\end{itemize} 9570To find the rational roots of $2*x^3-3*x^2-8*x+12$, input: 9571\begin{center}{\tt rationalroot(2*x\verb|^|3-3*x\verb|^|2-8*x+12)}\end{center} 9572Output : 9573\begin{center}{\tt [2,3/2,-2]}\end{center} 9574To find the rational roots of $2*x^3-3*x^2-8*x+12$ in $[1;2]$, input: 9575\begin{center}{\tt rationalroot(2*x\verb|^|3-3*x\verb|^|2-8*x+12,1,2)}\end{center} 9576Output : 9577\begin{center}{\tt [2,3/2]}\end{center} 9578To find the rational roots of $2*x^3-3*x^2+8*x-12$, input: 9579\begin{center}{\tt rationalroot(2*x\verb|^|3-3*x\verb|^|2+8*x-12)}\end{center} 9580Output : 9581\begin{center}{\tt [3/2]}\end{center} 9582To find the rational roots of $2*x^3-3*x^2+8*x-12$, input: 9583\begin{center}{\tt rationalroot(2*x\verb|^|3-3*x\verb|^|2+8*x-12)}\end{center} 9584Output : 9585\begin{center}{\tt [3/2]}\end{center} 9586To find the rational roots of $(3*x-2)^2*(2x+1)=18*x^3-15*x^2-4*x+4$, input: 9587\begin{center}{\tt rationalroot(18*x\verb|^|3-15*x\verb|^|2-4*x+4)}\end{center} 9588Output : 9589\begin{center}{\tt [(-1)/2,2/3]}\end{center} 9590 9591\subsection{Exact values of the complex rational roots of a polynomial : {\tt crationalroot}}\index{crationalroot} 9592\noindent {\tt crationalroot} takes 1 or 3 arguments : a polynomial and 9593optionally two complex numbers $\alpha,\beta$. 9594\begin{itemize} 9595\item If {\tt crationalroot} has 1 argument, {\tt crationalroot} returns the 9596list of the complex rational roots of the 9597polynomial without multiplicity. 9598\item if {\tt crationalroot} has 3 arguments, {\tt crationalroot} returns only 9599the complex rational roots of the 9600polynomial which are in the rectangle with sides parallel to the 9601axis having $[\alpha,\beta]$ as opposite vertices. 9602\end{itemize} 9603To find the rational complex roots of 9604$(x^2+4)*(2x-3)=2*x^3-3*x^2+8*x-12$, input : 9605\begin{center}{\tt crationalroot(2*x\verb|^|3-3*x\verb|^|2+8*x-12)}\end{center} 9606Output : 9607\begin{center}{\tt [2*i,3/2,-2*i]}\end{center} 9608 9609\section{Exact roots and poles} 9610\subsection{Roots and poles of a rational function : {\tt froot}}\index{froot} 9611\noindent{\tt froot} takes a rational function $F(x)$ as argument.\\ 9612{\tt froot} returns a vector whose components are the roots and the poles 9613of $F[x]$, each one followed by its multiplicity.\\ 9614If {\tt Xcas} can not find the exact values of the roots or poles, 9615it tries to find approximate values if $F(x)$ has numeric coefficients.\\ 9616Input : 9617\begin{center}{\tt froot((x\verb|^|5-2*x\verb|^|4+x\verb|^|3)/(x-2)) }\end{center} 9618Output : 9619\begin{center}{\tt [1,2,0,3,2,-1]}\end{center} 9620Hence, for $\displaystyle F(x)=\frac{x^5-2.x^4+x^3}{x-2}$ : 9621\begin{itemize} 9622\item $1$ is a root of multiplicity 2, 9623\item $0$ is a root of multiplicity 3, 9624\item $2$ is a pole of order 1. 9625\end{itemize} 9626Input : 9627\begin{center}{\tt froot((x\verb|^|3-2*x\verb|^|2+1)/(x-2)) }\end{center} 9628Output : 9629\begin{center}{\tt [1,1,(1+sqrt(5))/2,1,(1-sqrt(5))/2,1,2,-1]}\end{center} 9630{\bf Remark} : to have the complex roots and poles, check {\tt Complex} in 9631the {\tt cas} configuration (red button giving the state line).\\ 9632Input : 9633\begin{center}{\tt froot((x\verb|^|2+1)/(x-2)) }\end{center} 9634Output : 9635\begin{center}{\tt [-i,1,i,1,2,-1]}\end{center} 9636 9637\subsection{Rational function given by roots and poles : {\tt fcoeff}}\index{fcoeff} 9638\noindent{\tt fcoeff} has as argument a vector 9639whose components are the roots and poles of a rational function 9640$F[x]$, each one followed by its multiplicity.\\ 9641{\tt fcoeff} returns the rational function $F(x)$.\\ 9642Input : 9643\begin{center}{\tt fcoeff([1,2,0,3,2,-1]) }\end{center} 9644Output : 9645\begin{center}{\tt (x-1)\verb|^|2*x\verb|^|3/(x-2)}\end{center} 9646 9647 9648 9649\section{Computing in $\Z/p\Z$ or in $\Z/p\Z[x]$}\index{\%|textbf}\label{sec:modulaire} 9650The way to compute over $\Z/p\Z$ or over $\Z/p\Z[x]$ depends 9651on the syntax mode : 9652\begin{itemize} 9653\item In {\tt Xcas} mode, an object $n$ over $\Z/p\Z$ is written 9654$n \% p$. Some examples of input for 9655\begin{itemize} 9656\item an integer {\tt n} in $\Z/13\Z$\\ 9657{\tt n:=12\%13}. 9658\item a vector {\tt V} in $\Z/13\Z$ \\ 9659{\tt V:=[1,2,3]\%13} or 9660{\tt V:=[1\%13,2\%13,3\%13]}. 9661\item a matrix {\tt A} in $\Z/13\Z$ \\ 9662{\tt A:=[[1,2,3],[2,3,4]]\%13} or \\ 9663{\tt A:=[[1\%13,2\%13,3\%13],[[2\%13,3\%13,4\%13]]}. 9664\item 9665a polynomial {\tt A} in $\Z/13\Z[x]$ in symbolic representation\\ 9666{\tt A:=(2*x\verb|^|2+3*x-1)\%13} or \\ 9667{\tt A:=2\%13*x\verb|^|2+3\%13*x-1\%13}. 9668\item 9669a polynomial {\tt A} in $\Z/13\Z[x]$ in list representation\\ 9670{\tt A:=poly1[1,2,3]\%13} or 9671{\tt A:=poly1[1\%13,2\%13,3\%13]}. 9672\end{itemize} 9673To recover an object {\tt o} with integer coefficients instead of modular 9674coefficients, input {\tt o \% 0}. For example, input {\tt o:=4\%7} and 9675 {\tt o\%0},then output is {\tt -3}. 9676\item 9677In {\tt Maple} mode, integers modulo $p$ are represented like 9678usual integers instead of using specific modular integers. 9679To avoid confusion with normal commands, modular 9680commands are written with a capital letter (inert form) and followed 9681by the mod command (see also the next section). 9682\end{itemize} 9683{\bf Remark} 9684\begin{itemize} 9685\item For some commands in $\Z/p\Z$ or in $\Z/p\Z[x]$, {\tt p} must be 9686a prime integer. 9687\item The representation is the symmetric representation :\\ 9688{\tt 11\%13} returns {\tt -2\%13}. 9689\end{itemize} 9690 9691\subsection{Expand and reduce : {\tt normal}}\index{normal} 9692\noindent{\tt normal} takes as argument a polynomial expression.\\ 9693{\tt normal} expands and reduces this expression in $\Z/p\Z[x]$.\\ 9694Input : 9695\begin{center}{\tt normal(((2*x\verb|^|2+12)*( 5*x-4))\%13)}\end{center} 9696Output : 9697\begin{center}{\tt (-3\%13)*x\verb|^|3+(5\%13)*x\verb|^|2+(-5\%13)*x+4\%13}\end{center} 9698 9699\subsection{Addition in $\Z/p\Z$ or in $ \Z/p\Z[x]$ : {\tt +}}\index{+} 9700\noindent{\tt +} adds two integers in $\Z/p\Z$, or 9701two polynomials in $\Z/p\Z[x]$. For polynomial expressions, 9702use the {\tt normal} command to simplify\index{normal}.\\ 9703For integers in $\Z/p\Z$, input : 9704\begin{center}{\tt 3\%13+10\%13}\end{center} 9705Output : 9706\begin{center}{\tt 0\%13}\end{center} 9707For polynomials with coefficients in $\Z/p\Z$, input : 9708\begin{center}{\tt normal((11*x+5 )\% 13+(8*x+6)\%13)}\end{center} 9709or 9710\begin{center}{\tt normal(11\% 13*x+5\%13+8\% 13*x+6\%13)}\end{center} 9711Output : 9712\begin{center}{\tt (6\%13)*x+-2\%13}\end{center} 9713 9714\subsection{Subtraction in $\Z/p\Z$ or in $ \Z/p\Z[x]$ : {\tt -}}\index{-|textbf} 9715\noindent{\tt -} subtracts two integers in $\Z/p\Z$ or 9716two polynomials in $\Z/p\Z[x]$. For polynomial expressions, 9717use the {\tt normal} command to simplify\index{normal}.\\ 9718For integers in $\Z/p\Z$, input : 9719\begin{center}{\tt 31\%13-10\%13}\end{center} 9720Output : 9721\begin{center}{\tt -5\%13}\end{center} 9722For polynomials with coefficients in $\Z/p\Z$, input : 9723\begin{center}{\tt normal((11*x+5)\%13-(8*x+6)\%13)}\end{center} 9724or : 9725\begin{center}{\tt normal(11\% 13*x+5\%13-8\% 13*x+6\%13)}\end{center} 9726Output : 9727\begin{center}{\tt (3\%13)*x+-1\%13}\end{center} 9728 9729\subsection{Multiplication in $\Z/p\Z$ or in $ \Z/p\Z[x]$ : {\tt *}}\index{*} 9730\noindent {\tt *} multiplies two integers in $\Z/p\Z$ or 9731two polynomials in $\Z/p\Z[x]$. For polynomial expressions, 9732use the {\tt normal} command to simplify\index{normal}.\\ 9733For integers in $\Z/p\Z$, input : 9734\begin{center}{\tt 31\%13*10\%13}\end{center} 9735Output : 9736\begin{center}{\tt -2\%13}\end{center} 9737For polynomials with coefficients in $\Z/p\Z$, input : 9738\begin{center}{\tt normal((11*x+5)\%13*(8*x+6 )\% 13)}\end{center} 9739or : 9740\begin{center}{\tt normal((11\% 13*x+5\%13)*(8\% 13*x+6\%13))}\end{center} 9741Output : 9742\begin{center}{\tt (-3\%13)*x\verb|^|2+(2\%13)*x+4\%13}\end{center} 9743 9744\subsection{Euclidean quotient : {\tt quo}}\index{quo} 9745\noindent{\tt quo} takes as arguments 9746two polynomials $A$ and $B$ with coefficients in $\Z/p\Z$, where 9747$A$ and $B$ are list polynomials or symbolic polynomials with 9748respect to $x$ or to an optional third argument.\\ 9749{\tt quo} returns the quotient of the euclidean division 9750of $A$ by $B$ in $\Z/p\Z[x]$.\\ 9751Input : 9752\begin{center}{\tt quo((x\verb|^|3+x\verb|^|2+1)\%13,(2*x\verb|^|2+4)\%13)}\end{center} 9753or : 9754\begin{center}{\tt quo((x\verb|^|3+x\verb|^|2+1,2*x\verb|^|2+4)\%13)}\end{center} 9755Output: 9756\begin{center}{\tt (-6\%13)*x+-6\%13}\end{center} 9757Indeed $\displaystyle x^3+x^2+1=(2x^2+4)(\frac{x+1}{2})+\frac{5x-4}{4}$ 9758and $-3*4=-6*2=1 \ \bmod 13$. 9759 9760\subsection{Euclidean remainder : {\tt rem}}\index{rem} 9761\noindent{\tt rem} takes as arguments 9762two polynomials $A$ and $B$ with coefficients in $\Z/p\Z$, where 9763$A$ and $B$ are list polynomials or symbolic polynomials with 9764respect to $x$ or to an optional third argument.\\ 9765{\tt rem} returns the remainder of the euclidean division 9766of $A$ by $B$ in $\Z/p\Z[x]$.\\ 9767Input : 9768\begin{center}{\tt rem((x\verb|^|3+x\verb|^|2+1)\%13,(2*x\verb|^|2+4)\%13)}\end{center} 9769or : 9770\begin{center}{\tt rem((x\verb|^|3+x\verb|^|2+1,2*x\verb|^|2+4)\%13)}\end{center} 9771Output: 9772\begin{center}{\tt (-2\%13)*x+-1\%13}\end{center} 9773Indeed $\displaystyle x^3+x^2+1=(2x^2+4)(\frac{x+1}{2})+\frac{5x-4}{4}$ 9774and $-3*4=-6*2=1 \ \bmod 13$. 9775 9776\subsection{Euclidean quotient and euclidean remainder : {\tt quorem}}\index{quorem} 9777\noindent{\tt quorem} takes as arguments 9778two polynomials $A$ and $B$ with coefficients in $\Z/p\Z$, where 9779$A$ and $B$ are list polynomials or symbolic polynomials with 9780respect to $x$ or to an optional third argument.\\ 9781{\tt quorem} returns the list of the quotient and remainder of the 9782euclidean division of $A$ by $B$ in $\Z/p\Z[x]$ 9783(see also \ref{sec:iquorem} and \ref{sec:quorem}).\\ 9784Input : 9785\begin{center}{\tt quorem((x\verb|^|3+x\verb|^|2+1)\%13,(2*x\verb|^|2+4)\%13)}\end{center} 9786or : 9787\begin{center}{\tt quorem((x\verb|^|3+x\verb|^|2+1,2*x\verb|^|2+4)\%13)}\end{center} 9788Output: 9789\begin{center}{\tt [(-6\%13)*x+-6\%13,(-2\%13)*x+-1\%13]}\end{center} 9790Indeed 9791$\displaystyle x^3+x^2+1=(2x^2+4)(\frac{x+1}{2})+\frac{5x-4}{4}$\\ 9792and $-3*4=-6*2=1 \ \bmod 13$. 9793 9794\subsection{Division in $\Z/p\Z$ or in $\Z/p\Z[x]$ : {\tt /}}\index{/} 9795\noindent{\tt /} divides two integers in $\Z/p\Z$ or 9796two polynomials $A$ and $B$ in $\Z/p\Z[x]$.\\ 9797For polynomials, the result is the irreducible representation 9798of the fraction $\frac{A}{B}$ in $\Z/p\Z[x]$.\\ 9799For integers in $\Z/p\Z$, input : 9800\begin{center}{\tt 5\%13/2\% 13}\end{center} 9801Since $2$ is invertible in $Z/13\Z$, we get the output : 9802\begin{center}{\tt -4\%13}\end{center} 9803For polynomials with coefficients in $\Z/p\Z$, input : 9804\begin{center}{\tt (2*x\verb|^|2+5)\%13/(5*x\verb|^|2+2*x-3)\%13}\end{center} 9805Output : 9806\begin{center}{\tt ((6\%13)*x+1\%13)/((2\%13)*x+2\%13)}\end{center} 9807 9808\subsection{Power in $\Z/p\Z$ and in $\Z/p\Z[x]$ : {\tt \^\ }}\index{\^\ } 9809To compute {\tt a} to the power {\tt n} in $\Z/p\Z$, we use the operator 9810\verb|^|. {\tt Xcas} implementation is the binary power algorithm.\\ 9811Input : 9812\begin{center}{\tt (5\%13)\verb|^|2}\end{center} 9813Output : 9814\begin{center}{\tt -1\%13}\end{center} 9815To compute {\tt A} to the power {\tt n} in $\Z/p\Z[x]$, we use the operator 9816\verb|^| and the {\tt normal} command \index{normal}.\\ 9817Input : 9818\begin{center}{\tt normal(((2*x+1)\%13)\verb|^|5)}\end{center} 9819Output : 9820\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} 9821because 9822 $10=-3 \ (\bmod\ 13), \ \ 40=1\ (\bmod\ 13),\ \ 80=2 \ (\bmod\ 13),\ \ 32=6\ (\bmod\ 13)$. 9823 9824\subsection{Compute $a^n\ \bmod \ p$ : {\tt powmod powermod}}\index{powmod}\index{powermod} 9825\noindent{\tt powmod} (or {\tt powermod}) takes as argument $a,n,p$.\\ 9826{\tt powmod} (or {\tt powermod}) returns $a^n\ \bmod \ p$ in $[0;p-1]$.\\ 9827Input : 9828\begin{center}{\tt powmod(5,2,13)}\end{center} 9829Output : 9830\begin{center}{\tt 12}\end{center} 9831Input : 9832\begin{center}{\tt powmod(5,2,12)}\end{center} 9833Output : 9834\begin{center}{\tt 1}\end{center} 9835 9836\subsection{Inverse in $\Z/p\Z$ : {\tt inv inverse} or {\tt /}}\index{/}\index{inv} 9837To compute the inverse of an integer {\tt n} in $\Z/p\Z$, input {\tt 1/n\%p} 9838or {\tt inv(n\%p)} or {\tt inverse(n\%p)}.\\ 9839Input : 9840\begin{center}{\tt inv(3\%13) }\end{center} 9841Output : 9842\begin{center}{\tt -4\%13}\end{center} 9843Indeed $3\times-4=-12=1\ (\bmod\ 13)$. 9844 9845\subsection{Rebuild a fraction from its value modulo $p$ : {\tt fracmod}}\index{fracmod} 9846\noindent {\tt fracmod} takes two arguments, an integer $n$ 9847(representing a fraction) and an integer $p$ (the modulus).\\ 9848If possible, {\tt fracmod} returns a fraction $a/b$ such that 9849\[ -\frac{\sqrt{p}}{2} < a \leq \frac{\sqrt{p}}{2}, \quad 9850 0 \leq b < \frac{\sqrt{p}}{2}, \quad 9851 n \times b =a \pmod p \] 9852In other words $n=a/b\pmod p$.\\ 9853Input : 9854\begin{center}{\tt fracmod(3,13) }\end{center} 9855Output : 9856\begin{center}{\tt -1/4}\end{center} 9857Indeed : $3*-4=-12=1\ (\bmod\ 13)$, hence $3=-1/4\%13$.\\ 9858Input : 9859\begin{center}{\tt fracmod(13,121)}\end{center} 9860Output : 9861\begin{center}{\tt -4/9}\end{center} 9862Indeed : $13\times-9=-117=4\ (\bmod\ 121)$ hence $13=-4/9\%13$. 9863 9864\subsection{GCD in $\Z/p\Z[x]$ : {\tt gcd}}\index{gcd}\label{sec:gcdm} 9865\noindent {\tt gcd} takes as arguments two polynomials with 9866coefficients in $\Z/p\Z$ ($p$ must be prime).\\ 9867{\tt gcd} returns the GCD of these polynomials 9868computed in $\Z/p\Z[x]$ (see also 9869\ref{sec:gcd} for polynomials with non modular coefficients).\\ 9870Input : 9871\begin{center}{\tt gcd((2*x\verb|^|2+5)\%13,(5*x\verb|^|2+2*x-3)\%13)}\end{center} 9872Output : 9873\begin{center}{\tt (-4\%13)*x+5\%13}\end{center} 9874Input : 9875\begin{center}{\tt gcd((x\verb|^|2+2*x+1,x\verb|^|2-1)) mod 5)}\end{center} 9876Output : 9877\begin{center}{\tt x\%5}\end{center} 9878Note the difference with a gcd computation in $\Z[X]$ followed 9879by a reduction modulo 5, input: 9880\begin{center}{\tt gcd(x\verb|^|2+2*x+1,x\verb|^|2-1) mod 5}\end{center} 9881Output : 9882\begin{center}{\tt 1}\end{center} 9883 9884\subsection{Factorization over $\Z/p\Z[x]$ : {\tt factor factoriser}}\index{factor}\index{factoriser} 9885\noindent{\tt factor} takes as argument a polynomial 9886with coefficients in $\Z/p\Z[x]$.\\ 9887{\tt factor} factorizes this polynomial in $\Z/p\Z[x]$ ($p$ must 9888be prime).\\ 9889Input : 9890\begin{center}{\tt factor((-3*x\verb|^|3+5*x\verb|^|2-5*x+4)\%13)}\end{center} 9891Output : 9892\begin{center}{\tt ((1\%13)*x+-6\%13)*((-3\%13)*x\verb|^|2+-5\%13)}\end{center} 9893 9894\subsection{Determinant of a matrix in $\Z/p\Z$ : {\tt det}}\index{det} 9895\noindent{\tt det} takes as argument a matrix $A$ with coefficients in 9896$Z/pZ$.\\ 9897{\tt det} returns the determinant of this matrix $A$.\\ 9898Computations are done in $\Z/p\Z$ by Gauss reduction.\\ 9899Input : 9900\begin{center}{\tt det([[1,2,9]\%13,[3,10,0]\%13,[3,11,1]\%13])}\end{center} 9901or : 9902\begin{center}{\tt det([[1,2,9],[3,10,0],[3,11,1]]\%13)}\end{center} 9903Output : 9904\begin{center}{\tt 5\%13}\end{center} 9905hence, in $\Z/13\Z$, the determinant of 9906$A=[[1,2,9],[3,10,0],[3,11,1]]$ is {\tt 5\%13} (in $\Z$, {\tt det(A)=31}). 9907 9908\subsection{Inverse of a matrix with coefficients in $\Z/p\Z$ : {\tt inv inverse}}\index{inv}\index{inverse} 9909\noindent{\tt inverse} (or {\tt inv}) takes as argument a matrix $A$ in 9910$\Z/p\Z$.\\ 9911{\tt inverse} (or {\tt inv}) returns the inverse of the matrix 9912$A$ in $Z/p\Z$.\\ 9913Input : 9914\begin{center}{\tt inverse([[1,2,9]\%13,[3,10,0]\%13,[3,11,1]\%13])}\end{center} 9915or : 9916\begin{center}{\tt inv([[1,2,9]\%13,[3,10,0]\%13,[3,11,1]\%13])}\end{center} 9917or : 9918\begin{center}{\tt inverse([[1,2,9],[3,10,0],[3,11,1]]\%13)}\end{center} 9919or : 9920\begin{center}{\tt inv([[1,2,9],[3,10,0],[3,11,1]]\%13)}\end{center} 9921Output : 9922\begin{center}{\tt [[2\%13,-4\%13,-5\%13],[2\%13,0\%13,-5\%13], [-2\%13,-1\%13,6\%13]]}\end{center} 9923it is the inverse of $A=[[1,2,9],[3,10,0],[3,11,1]]$ in $\Z/13\Z$. 9924 9925\subsection{Row reduction to echelon form in $\Z/p\Z$ : {\tt rref}}\index{rref}\label{sec:rrefm} 9926\noindent{\tt rref} finds the row reduction to echelon form of 9927a matrix with coefficients in $\Z/p\Z$. 9928 9929This may be used 9930to solve a linear system of equations with coefficients in $\Z/p\Z$ 9931by rewriting it in matrix form (see also \ref{sec:rref}) : 9932\begin{center}{\tt A*X=B}\end{center} 9933{\tt rref} takes as argument the augmented matrix 9934of the system (the matrix obtained by augmenting matrix {\tt A} to the 9935right with the column vector {\tt B}).\\ 9936{\tt rref} returns a matrix {\tt [A1,B1]} : {\tt A1} has 1 on 9937its principal diagonal, and zeros outside, and the 9938solutions in $\Z/p\Z$, of : 9939\begin{center}{\tt A1*X=B1}\end{center} 9940are the same as the solutions of: 9941\begin{center}{\tt A*X=B}\end{center} 9942Example, solve in $\Z/13\Z$ 9943$$\left \{\begin{array}{lcr}\ \ x\ +\ \ 2 \cdot y & = &9 \\3 \cdot x +10 \cdot y & =& 0 \end{array}\right.$$ 9944Input : 9945\begin{center}{\tt rref([[1, 2, 9]\%13,[3,10,0]\%13])}\end{center} 9946or : 9947\begin{center}{\tt rref([[1, 2, 9],[3,10,0]])\%13}\end{center} 9948Output : 9949\begin{center}{\tt [[1\%13,0\%13,3\%13],[0\%13,1\%13,3\%13]]}\end{center} 9950hence {\tt x=3\%13} and {\tt y=3\%13}. 9951 9952\subsection{Construction of a Galois field : {\tt GF}}\index{GF} 9953\noindent{\tt GF} takes as arguments a prime integer $p$ 9954and an integer $n>1$.\\ 9955{\tt GF} returns a Galois field of characteristic $p$ having $p^n$ 9956elements.\\ 9957Elements of the field and the field itself 9958are represented by {\tt GF(...)} where {\tt ...} is the following 9959sequence: 9960\begin{itemize} 9961\item the characteristic $p$ ($px=0$), 9962\item an irreducible primitive minimal polynomial generating an 9963ideal $I$ in $\Z/p\Z[X]$, the Galois field being the quotient 9964of $\Z/p\Z[X]$ by $I$, 9965\item the name of the polynomial variable, by default {\tt x}, 9966\item a polynomial (a remainder modulo the minimal polynomial) 9967for an element of the field 9968(field elements are represented with the additive representation) 9969or {\tt undef} for the field itself. 9970\end{itemize} 9971You should give a name to this field (for example {\tt G:=GF(p,n)}), 9972in order to build elements of the field from a polynomial in 9973$\Z/p\Z[X]$, for example {\tt G(x\verb|^|3+x)}. Note that {\tt G(x)} 9974is a generator of the multiplicative group {\tt $G^*$}.\\ 9975Input : 9976\begin{center}{\tt G:=GF(2,8)}\end{center} 9977Output : 9978\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} 9979The field $G$ has $2^8=256$ elements and 9980$x$ generates the multiplicative group 9981of this field ($\{ 1,x,x^2,...x^{254} \}$).\\ 9982Input : 9983\begin{center}{\tt G(x\verb|^|9)}\end{center} 9984Output : 9985\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} 9986indeed $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$.\\ 9987Input : 9988\begin{center}{\tt G(x)\verb|^|255}\end{center} 9989Output should be the unit, indeed: 9990\begin{center} 9991{\tt GF(2,x\verb|^|8-x\verb|^|6-x\verb|^|4-x\verb|^|3-x\verb|^|2-x-1,x,1)}\end{center} 9992As one can see in these examples, the output contains many times the same 9993information that you would prefer not to see 9994if you work many times with the same field. For this reason, 9995the definition of a Galois field may have an optional argument, 9996a variable name which will be used thereafter to represent elements 9997of the field. Since you will also most 9998likely want to modify the name of the indeterminate, the field 9999name is grouped with the variable name in a list 10000passed as third argument to {\tt GF}. 10001Note that these two variable names must be quoted.\\ 10002Example,\\ 10003Input : 10004\begin{center}{\tt G:=GF(2,2,['w','G']):; G(w\verb|^|2)}\end{center} 10005Output : 10006\begin{center}{\tt Done, G(w+1)}\end{center} 10007Input : 10008\begin{center}{\tt G(w\verb|^|3)}\end{center} 10009Output : 10010\begin{center}{\tt G(1)}\end{center} 10011Hence, the elements of {\tt GF(2,2)} are 10012{\tt G(0),G(1),G(w),G(w\verb|^|2)=G(w+1)}. 10013 10014We may also impose the irreducible primitive polynomial that we wish 10015to use, by putting it as second argument (instead of $n$), 10016for example : 10017\begin{center}\verb|G:=GF(2,w^8+w^6+w^3+w^2+1,['w','G'])|\end{center} 10018If the polynomial is not primitive, {\tt Xcas} will replace it 10019automatically by a primitive polynomial, for example :\\ 10020Input : 10021\begin{center}\verb|G:=GF(2,w^8+w^7+w^5+w+1,['w','G'])|\end{center} 10022Output : 10023\begin{center}\verb|G:=GF(2,w^8-w^6-w^3-w^2-1,['w','G'],undef)|\end{center} 10024 10025\subsection{Factorize a polynomial with coefficients in a Galois field : {\tt factor}}\index{factor} 10026\noindent{\tt factor} can also factorize a univariate 10027polynomial with coefficients in a Galois field.\\ 10028Input for example to have {\tt G=}$\mathbb F_4$:\\ 10029\begin{center}{\tt G:=GF(2,2,['w','G'])}\end{center} 10030Output : 10031\begin{center}{\tt GF(2,w\verb|^|2+w+1,[w,G],undef)}\end{center} 10032Input for example : 10033\begin{center}{\tt a:=G(w)}\end{center} 10034\begin{center}{\tt factor(a\verb|^|2*x\verb|^|2+1)}\end{center} 10035Output : 10036\begin{center}{\tt (G(w+1))*(x+G(w+1))\verb|^|2}\end{center} 10037 10038\section{Compute in $\Z/p\Z[x]$ using Maple syntax}\index{mod}\index{\%}\label{sec:modulmap} 10039\subsection{Euclidean quotient : {\tt Quo}}\index{Quo} 10040\noindent {\tt Quo} is the inert form of {\tt quo}.\\ 10041{\tt Quo} returns the euclidean quotient between two polynomials 10042 without evaluation.\\ 10043It is used in conjunction with {\tt mod} in Maple syntax mode to compute 10044the euclidean quotient of the division of two 10045polynomials with coefficients in $\Z/p\Z$.\\ 10046Input in {\tt Xcas} mode: 10047\begin{center}{\tt Quo((x\verb|^|3+x\verb|^|2+1) mod 13,(2*x\verb|^|2+4) mod 13)}\end{center} 10048Output : 10049\begin{center}{\tt quo((x\verb|^|3+x\verb|^|2+1)\%13,(2*x\verb|^|2+4)\%13)}\end{center} 10050you need to {\tt eval(ans())} to get : 10051\begin{center}{\tt (-6\%13)*x+-6\%13}\end{center} 10052Input in {\tt Maple} mode : 10053\begin{center}{\tt Quo(x\verb|^|3+x\verb|^|2+1,2*x\verb|^|2+4) mod 13}\end{center} 10054Output : 10055\begin{center}{\tt (-6)*x-6}\end{center} 10056Input in {\tt Maple} mode : 10057\begin{center}{\tt Quo(x\verb|^|2+2*x,x\verb|^|2+6*x+5) mod 5}\end{center} 10058Output : 10059\begin{center}{\tt 1}\end{center} 10060 10061\subsection{Euclidean remainder: {\tt Rem}}\index{Rem} 10062\noindent{\tt Rem} is the inert form of {\tt rem}.\\ 10063{\tt Rem} returns the euclidean remainder between two polynomials 10064without evaluation. 10065It is used in conjunction with {\tt mod} in Maple syntax mode to compute 10066the euclidean remainder of the division of two 10067polynomials with coefficients in $\Z/p\Z$.\\ 10068Input in {\tt Xcas} mode : 10069\begin{center}{\tt Rem((x\verb|^|3+x\verb|^|2+1) mod 13,(2*x\verb|^|2+4) mod 13)}\end{center} 10070Output : 10071\begin{center}{\tt rem((x\verb|^|3+x\verb|^|2+1)\%13,(2*x\verb|^|2+4)\%13)}\end{center} 10072you need to {\tt eval(ans())} to get : 10073\begin{center}{\tt (-2\%13)*x+-1\%13}\end{center} 10074Input in {\tt Maple} mode : 10075\begin{center}{\tt Rem(x\verb|^|3+x\verb|^|2+1,2*x\verb|^|2+4) mod 13}\end{center} 10076Output : 10077\begin{center}{\tt (-2)*x-1}\end{center} 10078Input in {\tt Maple} mode : 10079\begin{center}{\tt Rem(x\verb|^|2+2*x,x\verb|^|2+6*x+5) mod 5}\end{center} 10080Output : 10081\begin{center}{\tt 1*x}\end{center} 10082 10083\subsection{GCD in $\Z/p\Z[x]$ : {\tt Gcd}}\index{Gcd} 10084\noindent{\tt Gcd} is the inert form of {\tt gcd}.\\ 10085{\tt Gcd} returns the gcd (greatest common divisor) of two polynomials 10086(or of a list of polynomials or of a sequence of polynomials) without 10087evaluation.\\ 10088It is used in conjunction with {\tt mod} in Maple syntax mode to compute 10089the gcd of two polynomials with coefficients in $\Z/p\Z$ with $p$ prime 10090(see also \ref{sec:gcd}).\\ 10091Input in {\tt Xcas} mode : 10092\begin{center}{\tt Gcd((2*x\verb|^|2+5,5*x\verb|^|2+2*x-3)\%13)}\end{center} 10093Output : 10094\begin{center}{\tt gcd((2*x\verb|^|2+5)\%13,(5*x\verb|^|2+2*x-3)\%13)}\end{center} 10095you need to {\tt eval(ans())} to get : 10096\begin{center}{\tt (1\%13)*x+2\%13}\end{center} 10097Input in {\tt Maple} mode : 10098\begin{center}{\tt Gcd(2*x\verb|^|2+5,5*x\verb|^|2+2*x-3) mod 13}\end{center} 10099Output : 10100\begin{center}{\tt 1*x+2}\end{center} 10101Input: 10102\begin{center}{\tt Gcd(x\verb|^|2+2*x,x\verb|^|2+6*x+5) mod 5}\end{center} 10103Output : 10104\begin{center}{\tt 1*x}\end{center} 10105 10106\subsection{Factorization in $\Z/p\Z[x]$ : {\tt Factor}}\index{Factor} 10107\noindent{\tt Factor} is the inert form of {\tt factor}.\\ 10108{\tt Factor} takes as argument a polynomial.\\ 10109{\tt Factor} returns {\tt factor} without evaluation. 10110It is used in conjunction with {\tt mod} in Maple syntax mode to 10111factorize a polynomial with coefficients in $\Z/p\Z$ 10112where $p$ must be prime.\\ 10113Input in {\tt Xcas} mode : 10114\begin{center}{\tt Factor((-3*x\verb|^|3+5*x\verb|^|2-5*x+4)\%13)}\end{center} 10115Output : 10116\begin{center}{\tt factor((-3*x\verb|^|3+5*x\verb|^|2-5*x+4)\%13)}\end{center} 10117you need to {\tt eval(ans())} to get : 10118\begin{center}{\tt ((1\%13)*x+-6\%13)*((-3\%13)*x\verb|^|2+-5\%13)}\end{center} 10119Input in {\tt Maple} mode : 10120\begin{center}{\tt Factor(-3*x\verb|^|3+5*x\verb|^|2-5*x+4) mod 13}\end{center} 10121Output : 10122\begin{center}{\tt -3*(1*x-6)*(1*x\verb|^|2+6)}\end{center} 10123 10124\subsection{Determinant of a matrix with coefficients in $\Z/p\Z$ : {\tt Det}}\index{Det} 10125\noindent{\tt Det} is the inert form of {\tt det}.\\ 10126{\tt Det} takes as argument a matrix with coefficients in $\Z/p\Z$.\\ 10127{\tt Det} returns {\tt det} without evaluation. 10128It is used in conjunction with {\tt mod} in Maple syntax mode to 10129find the determinant of a matrix with coefficients in $\Z/p\Z$.\\ 10130Input in {\tt Xcas} mode : 10131\begin{center}{\tt Det([[1,2,9] mod 13,[3,10,0] mod 13,[3,11,1] mod 13])}\end{center} 10132Output : 10133\begin{center}{\tt det([[1\%13,2\%13,-4\%13],[3\%13,-3\%13,0\%13], [3\%13,-2\%13,1\%13]])}\end{center} 10134you need to {\tt eval(ans())} to get : 10135\begin{center}{\tt 5\%13}\end{center} 10136hence, in $\Z/13\Z$, the determinant of 10137$A=[[1, 2, 9],[3,10,0],[3,11,1]]$ is {\tt 5\%13} (in $\Z$, {\tt det(A)=31}).\\ 10138Input in {\tt Maple} mode : 10139\begin{center}{\tt Det([[1,2,9],[3,10,0],[3,11,1]]) mod 13}\end{center} 10140Output : 10141\begin{center}{\tt 5}\end{center} 10142 10143\subsection{Inverse of a matrix in $\Z/p\Z$ : {\tt Inverse}}\index{Inverse} 10144\noindent{\tt Inverse} is the inert form of {\tt inverse}.\\ 10145{\tt Inverse} takes as argument a matrix with coefficients in $\Z/p\Z$.\\ 10146{\tt Inverse} returns {\tt inverse} without evaluation. 10147It is used in conjunction with {\tt mod} in Maple syntax mode to 10148find the inverse of a matrix with coefficients in $\Z/p\Z$.\\ 10149Input in {\tt Xcas} mode : 10150\begin{center}{\tt Inverse([[1,2,9] mod 13,[3,10,0] mod 13,[3,11,1] mod13])}\end{center} 10151Output : 10152\begin{center}{\tt inverse([[1\%13,2\%13,9\%13],[3\%13,10\%13,0\%13], [3\%13,11\%13,1\%13]])}\end{center} 10153you need to {\tt eval(ans())} to get : 10154\begin{center}{\tt [[2\%13,-4\%13,-5\%13],[2\%13,0\%13,-5\%13], [-2\%13,-1\%13,6\%13]]}\end{center} 10155which is the inverse of $A=[[1,2,9],[3,10,0],[3,11,1]]$ in $\Z/13\Z$.\\ 10156Input in {\tt Maple} mode : 10157\begin{center}{\tt Inverse([[1,2,9],[3,10,0],[3,11,1]]) mod 13}\end{center} 10158Output : 10159\begin{center}{\tt [[2,-4,-5],[2,0,-5],[-2,-1,6]]}\end{center} 10160 10161\subsection{Row reduction to echelon form in $\Z/p\Z$ : {\tt Rref}}\index{Rref} 10162\noindent{\tt Rref} is the inert form of {\tt rref}.\\ 10163{\tt Rref} returns {\tt rref} without 10164evaluation. 10165It is used in conjunction with {\tt mod} in Maple syntax mode to 10166find the row reduction to echelon form 10167of a matrix with coefficients in $\Z/p\Z$ (see 10168also \ref{sec:rref}).\\ 10169Example, solve in $\Z/13\Z$ 10170$$\left \{\begin{array}{lcr}\ \ x\ +\ \ 2 \cdot y & = &9 \\3 \cdot x +10 \cdot y & =& 0 \end{array}\right.$$ 10171Input in {\tt Xcas} mode : 10172\begin{center}{\tt Rref([[1,2,9] mod 13,[3,10,0] mod 13])}\end{center} 10173Output : 10174\begin{center}{\tt rref([[1\%13, 2\%13, 9\%13],[3\%13,10\%13,0\%13]])}\end{center} 10175you need to {\tt eval(ans())} to get : 10176\begin{center}{\tt [[1\%13,0\%13,3\%13],[0\%13,1\%13,3\%13]]}\end{center} 10177and conclude that {\tt x=3\%13} and {\tt y=3\%13}.\\ 10178Input in {\tt Maple} mode : 10179\begin{center}{\tt Rref([[1,2,9],[3,10,0],[3,11,1]]) mod 13}\end{center} 10180Output : 10181\begin{center}{\tt [[1,0,0],[0,1,0],[0,0,1]]}\end{center} 10182 10183 10184\section{Taylor and asymptotic expansions} 10185\subsection{Division by increasing power order : {\tt divpc}}\index{divpc} 10186\noindent{\tt divpc} takes three arguments : two polynomials 10187expressions $A,\ B$ depending on $x$, 10188such that the constant term of $B$ is not 0, and an integer $n$.\\ 10189{\tt divpc} returns the quotient $Q$ of the division of $A$ by $B$ 10190by increasing power order, with {\tt degree}$(Q)\leq n$ or $ Q=0$. 10191In other words, $Q$ is the Taylor expansion of order $n$ of 10192$\displaystyle \frac{A}{B}$ in the vicinity of $x=0$. \\ 10193Input : 10194\begin{center}{\tt divpc(1+x\verb|^|2+x\verb|^|3,1+x\verb|^|2,5)}\end{center} 10195Output : 10196\begin{center}{\tt -x\verb|^|5+x\verb|^|3+1}\end{center} 10197Note that this command does not work on polynomials written 10198as a list of coefficients. 10199 10200\subsection{Taylor expansion : {\tt taylor}}\index{taylor}\index{order\_size|textbf} 10201\noindent{\tt taylor} takes from one to four arguments : 10202\begin{itemize} 10203\item an expression depending of a variable (by default {\tt x}), 10204\item an equality variable=value (e.g. $x=a$) where to compute 10205the Taylor expansion, by default {\tt x=0}, 10206\item an integer $n$, the order of the series expansion, 10207by default {\tt 5} 10208\item a direction {\tt -1, 1} (for unidirectional series expansion) 10209 or {\tt 0} (for bidirectional series expansion) (by default {\tt 10210 0}). 10211\end{itemize} 10212Note that the syntax {\tt ...,x,$n$,$a$,...} 10213(instead of {\tt ...,x=$a$,$n$,...}) is also accepted.\\ 10214{\tt taylor} returns a polynomial in {\tt x-a}, plus a remainder 10215of the form:\\ 10216 {\tt (x-a)\verb|^|n*order\_size(x-a)}\\ 10217where {\tt order\_size} is a function such that, 10218\[ \forall r>0, \quad \lim_{x\rightarrow 0} x^r \mbox{order\_size}(x) = 0 \] 10219For regular series expansion, {\tt order\_size} is a bounded function, 10220but for non regular series expansion, it might tend slowly to 10221infinity, for example like a power of $\ln(x)$.\\ 10222Input : 10223\begin{center}{\tt taylor(sin(x),x=1,2)}\end{center} 10224Or (be careful with the order of the arguments !) : 10225\begin{center}{\tt taylor(sin(x),x,2,1)}\end{center} 10226Output : 10227\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} 10228{\bf Remark}\\ 10229The order returned by {\tt taylor} may be smaller than $n$ if 10230cancellations between numerator and denominator occur, for example 10231\[ \mbox{taylor}(\frac{x^3+\sin(x)^3}{x-\sin(x)}) \] 10232Input : 10233\begin{center}{\tt taylor(x\verb|^|3+sin(x)\verb|^|3/(x-sin(x)))}\end{center} 10234The output is only a 2nd-order series expansion : 10235\begin{center}{\tt 10236 6+-27/10*x\verb|^2|+x\verb|^|3*order\_size(x)}\end{center} 10237Indeed the numerator and denominator valuation is 3, hence we lose 3 10238orders. To get order 4, we should use $n=7$.\\ 10239Input : 10240\begin{center}{\tt taylor(x\verb|^|3+sin(x)\verb|^|3/(x-sin(x)),x=0,7)}\end{center} 10241Output is a 4th-order series expansion : 10242\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} 10243 10244\subsection{Series expansion : {\tt series}}\index{series}\index{order\_size} 10245\noindent{\tt series} takes from one to four arguments : 10246\begin{itemize} 10247\item an expression depending of a variable (by default {\tt x}), 10248\item an equality variable=value (e.g. $x=a$) where to compute 10249the series expansion, by default {\tt x=0}, 10250\item an integer $n$, the order of the series expansion, 10251by default {\tt 5} 10252\item a direction {\tt -1, 1} (for unidirectional series expansion) 10253 or {\tt 0} (for bidirectional series expansion) (by default {\tt 10254 0}). 10255\end{itemize} 10256Note that the syntax {\tt ...,x,$a$,$n$,...} 10257(instead of {\tt ...,x=$a$,$n$,...}) is also accepted.\\ 10258{\tt series} returns a polynomial in {\tt x-a}, plus a remainder 10259of the form: 10260\begin{center} 10261 {\tt (x-a)\verb|^|n*order\_size(x-a)} 10262\end{center} 10263where {\tt order\_size} is a function such that, 10264\[ \forall r>0, \quad \lim_{x\rightarrow 0} x^r \mbox{order\_size}(x) = 0 \] 10265The order returned by {\tt series} may be smaller than $n$ if 10266cancellations between numerator and denominator occur. 10267 10268Examples~: 10269\begin{itemize} 10270\item series expansion in the vicinity of {\tt x=0}\\ 10271 Find an series expansion of 10272$\displaystyle\frac{x^3+\sin(x)^3}{x-\sin(x)}$ 10273in the vicinity of {\tt x=0}.\\ 10274Input : 10275\begin{center}{\tt series(x\verb|^|3+sin(x)\verb|^|3/(x-sin(x)))}\end{center} 10276Output is only a 2nd-order series expansion : 10277\begin{center}{\tt 6+-27/10*x\verb|^2|+x\verb|^|3*order\_size(x)}\end{center} 10278We have lost 3 orders because the valuation of the numerator and 10279denominator is 3. To get a 4-th order expansion, we must therefore 10280take $n=7$.\\ 10281Input : 10282\begin{center}{\tt series(x\verb|^|3+sin(x)\verb|^|3/(x-sin(x)),x=0,7)}\end{center} 10283or : 10284\begin{center}{\tt series(x\verb|^|3+sin(x)\verb|^|3/(x-sin(x)),x,0,7)}\end{center} 10285Output is a 4th-order series expansion : 10286\begin{center}{\tt 6+-27/10*x\verb|^|2+x\verb|^|3+711/1400*x\verb|^|4+ 10287x\verb|^|5*order\_size(x)}\end{center} 10288\item series expansion in the vicinity of {\tt x=a}\\ 10289Find a series 4th-order expansion of $\cos(2x)^2$ in the vicinity of 10290$x=\frac{\pi}{6}$. \\ 10291Input: 10292\begin{center}{\tt series(cos(2*x)\verb|^|2,x=pi/6, 4)}\end{center} 10293Output : 10294\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} 10295\item series expansion in the vicinity of {\tt x=+$\infty$} or {\tt 10296 x=-$\infty$} 10297\begin{enumerate} 10298\item 10299Find a 5th-order series expansion of $\arctan(x)$ in the vicinity of 10300{\tt x=+$\infty$}.\\ 10301 Input : 10302\begin{center}{\tt series(atan(x),x=+infinity,5)}\end{center} 10303Output : 10304\begin{center}{\tt pi/2-1/x+1/3*(1/x)\verb|^|3+1/-5*(1/x)\verb|^|5+ 10305(1/x)\verb|^|6*order\_size(1/x)}\end{center} 10306Note that the expansion variable and the argument of the 10307{\tt order\_size} function is 10308$\displaystyle h=\frac{1}{x} \rightarrow_{x\rightarrow + \infty} 0 $. 10309\item 10310Find a series 2nd-order expansion of $(2x-1)e^{\frac{1}{x-1}}$ in the vicinity of 10311{\tt x=+$\infty$}. \\ 10312Input : 10313\begin{center}{\tt series((2*x-1)*exp(1/(x-1)),x=+infinity,3)}\end{center} 10314Output is only a 1st-order series expansion : 10315\begin{center}{\tt 2*x+1+2/x+(1/x)\verb|^|2*order\_size(1/x)}\end{center} 10316To get a 2nd-order series expansion in $1/x$, input: 10317\begin{center}{\tt series((2*x-1)*exp(1/(x-1)),x=+infinity,4)}\end{center} 10318Output : 10319\begin{center}{\tt 10320 2*x+1+2/x+17/6*(1/x)\verb|^|2+(1/x)\verb|^|3*order\_size(1/x)}\end{center} 10321\item 10322Find a 2nd-order series expansion of $(2x-1)e^{\frac{1}{x-1}}$ in the vicinity 10323of {\tt x=-$\infty$}.\\ 10324Input : 10325\begin{center}{\tt series((2*x-1)*exp(1/(x-1)),x=-infinity,4)}\end{center} 10326Output : 10327\begin{center}{\tt -2*(-x)+1-2*(-1/x)+17/6*(-1/x)\verb|^|2+\\ 10328(-1/x)\verb|^|3*order\_size(-1/x)}\end{center} 10329\end{enumerate} 10330\item unidirectional series expansion.\\ 10331The fourth parameter indicates the direction : 10332\begin{itemize} 10333\item {\tt 1} to do an series expansion in the vicinity of $x=a$ with 10334$ \ x>a$, 10335\item{\tt -1} to do an series expansion in the vicinity of $x=a$ with 10336$ \ x<a$, 10337\item{\tt 0} to do an series expansion in the vicinity of $x=a$ with 10338$ \ x \neq a$. 10339\end{itemize} 10340For example, 10341find a 2nd-order series expansion of $\ \frac{(1+x)^{\frac{1}{x}}}{x^3}\ $ in 10342the vicinity of $x=0^+$.\\ 10343Input : 10344\begin{center}{\tt series((1+x)\verb|^|(1/x)/x\verb|^|3,x=0,2,1)}\end{center} 10345Output : 10346\begin{center}{\tt exp(1)/x\verb|^|3+(-(exp(1)))/2/x\verb|^|2+1/x*order\_size(x)}\end{center} 10347\end{itemize} 10348 10349\subsection{The residue of an expression at a point : {\tt residue}}\index{residue} 10350{\tt residue} takes as argument an expression depending on a variable, 10351the variable name and a complex $a$ or an expression 10352depending on a variable and the equality : variable\_name=$a$. 10353{\tt residue} returns the residue of this expression at the point $a$.\\ 10354Input : 10355\begin{center}{\tt residue(cos(x)/x\verb|^|3,x,0)}\end{center} 10356or : 10357\begin{center}{\tt residue(cos(x)/x\verb|^|3,x=0)}\end{center} 10358Output : 10359\begin{center}{\tt (-1)/2}\end{center} 10360 10361\subsection{Pad\'e expansion: {\tt pade}}\index{pade} 10362{\tt pade} takes 4 arguments 10363\begin{itemize} 10364\item an expression, 10365\item the variable name the expression depends on, 10366\item an integer $n$ or a polynomial $N$, 10367\item an integer $p$. 10368\end{itemize} 10369{\tt pade} returns a rational fraction $P/Q$ such that {\tt 10370 degree(P)}$<p$ and $P/Q=f \pmod{x^{n+1}}$ or $P/Q=f \pmod{N}$. 10371In the first case, it means that $P/Q$ and $f$ have the same 10372Taylor expansion at 0 up to order $n$.\\ 10373Input : 10374\begin{center}{\tt pade(exp(x),x,5,3)}\end{center} 10375or : 10376\begin{center}{\tt pade(exp(x),x,x\verb|^|6,3)}\end{center} 10377Output : 10378\begin{center}{\tt (3*x\verb|^|2+24*x+60)/(-x\verb|^|3+9*x\verb|^|2-36*x+60)}\end{center} 10379To verify input : 10380\begin{center}{\tt taylor((3*x\verb|^|2+24*x+60)/(-x\verb|^|3+9*x\verb|^|2-36*x+60))}\end{center} 10381Output : 10382\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} 10383which is the 5th-order series expansion of {\tt exp(x)} at $x=0$.\\ 10384Input : 10385\begin{center}{\tt pade((x\verb|^|15+x+1)/(x\verb|^|12+1),x,12,3)}\end{center} 10386or : 10387\begin{center}{\tt pade((x\verb|^|15+x+1)/(x\verb|^|12+1),x,x\verb|^|13,3)}\end{center} 10388Output : 10389\begin{center}{\tt x+1}\end{center} 10390Input : 10391\begin{center}{\tt pade((x\verb|^|15+x+1)/(x\verb|^|12+1),x,14,4)}\end{center} 10392or : 10393\begin{center}{\tt pade((x\verb|^|15+x+1)/(x\verb|^|12+1),x,x\verb|^|15,4)}\end{center} 10394Output : 10395\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} 10396To verify, input : 10397\begin{center}{\tt series(ans(),x=0,15)}\end{center} 10398Output : 10399\begin{center}{\tt 1+x-x\verb|^|{12}-x\verb|^|{13}+2x\verb|^|{15}+x\verb|^|{16}*order\_size(x)}\end{center} 10400then input : 10401\begin{center}{\tt series((x\verb|^|15+x+1)/(\verb|x^|12+1),x=0,15)}\end{center} 10402Output : 10403\begin{center}{\tt 1+x-x\verb|^|{12}-x\verb|^|{13}+x\verb|^|{15}+x\verb|^|{16}*order\_size(x)}\end{center} 10404These two expressions have the same 14th-order series expansion at $x=0$. 10405 10406\section{Intervals} 10407\subsection{Definition of an interval : {\tt a1..a2}}\index{..|textbf} 10408An interval is represented by two real numbers 10409separated by {\tt ..}, for example 10410\begin{center}{\tt 1..4}\\ 10411{\tt 1.2..sqrt(2)} 10412\end{center} 10413Input : 10414\begin{center}{\tt A:=1..4}\end{center} 10415\begin{center}{\tt B:=1.2..sqrt(2)}\end{center} 10416{\bf Warning!}\\ 10417The order of the boundaries of the interval is significant. For 10418example, if you input 10419\begin{center} 10420{\tt B:=2..3; C:=3..2}, 10421\end{center} 10422then {\tt B} and {\tt C} are different, {\tt B==C} returns {\tt 0}. 10423 10424\subsection{Boundaries of an interval : {\tt left right}}\index{[]}\index{sommet}\index{feuille}\index{op}\index{left}\index{right} 10425\noindent {\tt left} (resp. {\tt right}) takes as argument an interval.\\ 10426{\tt left} (resp. {\tt right}) returns the left (resp. right) boundary 10427of this interval. \\ 10428Note that {\tt ..} is an infixed operator, therefore: 10429\begin{itemize} 10430\item {\tt sommet(1..5)} is equal to {\tt '..'} and {\tt feuille(1..5)} 10431 is equal to {\tt (1,5)}. 10432\item the name of the interval followed by 10433{\tt [0]} returns the operator {\tt ..} 10434\item 10435the name of the interval followed by {\tt [1]} 10436(or the {\tt left} command) returns the left boundary. 10437\item 10438The name of the interval followed by {\tt [2]} 10439(or the {\tt right} command) 10440returns the right boundary. 10441\end{itemize} 10442Input : 10443\begin{center}{\tt (3..5)[0]}\end{center} 10444or : 10445\begin{center}{\tt sommet(3..5)}\end{center} 10446Output : 10447\begin{center}{\tt '..'}\end{center} 10448Input : 10449\begin{center}{\tt left(3..5)}\end{center} 10450or : 10451\begin{center}{\tt (3..5)[1]}\end{center} 10452or : 10453\begin{center}{\tt feuille(3..5)[0]}\end{center} 10454or : 10455\begin{center}{\tt op(3..5)[0]}\end{center} 10456Output : 10457\begin{center}{\tt 3}\end{center} 10458Input : 10459\begin{center}{\tt right(3..5)}\end{center} 10460or : 10461\begin{center}{\tt (2..5)[2]}\end{center} 10462or : 10463\begin{center}{\tt feuille(3..5)[1]}\end{center} 10464or : 10465\begin{center}{\tt op(3..5)[1]}\end{center} 10466Output : 10467\begin{center}{\tt 5}\end{center} 10468{\bf Remark}\\ 10469{\tt left} (resp. {\tt right}) returns also the left (resp. right) member of an 10470equation (for example {\tt left(2*x+1=x+2)} returns {\tt 2*x+1}). 10471 10472\subsection{Center of an interval : {\tt interval2center}}\index{interval2center} 10473\noindent {\tt interval2center} takes as argument an interval or a list of 10474intervals.\\ 10475{\tt interval2center} returns the center of this interval or the list of 10476centers of these intervals.\\ 10477Input : 10478\begin{center}{\tt interval2center(3..5)}\end{center} 10479Output : 10480\begin{center}{\tt 4}\end{center} 10481Input : 10482\begin{center}{\tt interval2center([2..4,4..6,6..10])}\end{center} 10483Output : 10484\begin{center}{\tt [3,5,8]}\end{center} 10485 10486\subsection{Intervals defined by their center : {\tt center2interval}}\index{center2interval} 10487\noindent {\tt center2interval} takes as argument a vector {\tt V} of reals 10488and optionally a real as second argument 10489(by default {\tt V[0]-(V[1]-V[0])/2}).\\ 10490{\tt center2interval} returns a vector of intervals having 10491the real values of the first argument as centers, where 10492the value of the second argument is 10493the left boundary of the first interval.\\ 10494Input : 10495\begin{center}{\tt center2interval([3,5,8])}\end{center} 10496Or (since the default value is 3-(5-3)/2=2) : 10497\begin{center}{\tt center2interval([3,5,8],2)}\end{center} 10498Output : 10499\begin{center}{\tt [2..4,4..6,6..10]}\end{center} 10500Input : 10501\begin{center}{\tt center2interval([3,5,8],2.5)}\end{center} 10502Output : 10503\begin{center}{\tt [2.5..3.5,3.5..6.5,6.5..9.5]}\end{center} 10504 10505\section{Sequences} 10506\label{sec:seq} 10507\subsection{Definition : {\tt seq[] ()}}\index{seq[]}\index{()} 10508A sequence is represented by 10509a sequence of elements separated by commas, without 10510delimiters or with either {\tt ( )} or {\tt seq[...]} 10511as delimiters, for example 10512\begin{center} 10513{\tt (1,2,3,4)}\\ 10514{\tt seq[1,2,3,4]} 10515\end{center} 10516Input : 10517\begin{center}{\tt A:=(1,2,3,4)} or {\tt A:=seq[1,2,3,4]}\end{center} 10518\begin{center}{\tt B:=(5,6,3,4)} or {\tt B:=seq[5,6,3,4]}\end{center} 10519{\bf Remarks} 10520\begin{itemize} 10521\item The order of the elements of the sequence is significant. 10522For example, if {\tt B:=(5,6,3,4)} and {\tt C:=(3,4,5,6)}, then 10523{\tt B==C} returns {\tt 0}. 10524\item 10525(see also \ref{sec:seq})\\ 10526{\tt seq([0,2])=(0,0)} and {\tt seq([0,1,1,5])=[0,0,0,0,0]} but\\ 10527{\tt seq[0,2]=(0,2)} and {\tt seq[0,1,1,5]=(0,1,1,5)} 10528\end{itemize} 10529 10530\subsection{Concat two sequences : {\tt ,}}\index{,} 10531The infix operator {\tt ,} concatenates two sequences.\\ 10532Input : 10533\begin{center}{\tt A:=(1,2,3,4)}\end{center} 10534\begin{center}{\tt B:=(5,6,3,4)}\end{center} 10535\begin{center}{\tt A,B}\end{center} 10536Output : 10537\begin{center}{\tt (1,2,3,4,5,6,3,4)}\end{center} 10538 10539\subsection{Get an element of a sequence : {\tt []}}\index{[]} 10540The elements of a sequence have indexes beginning at 0 in {\tt Xcas} 10541mode or 1 in other modes.\\ 10542A sequence or a variable name assigned to a sequence 10543followed by {\tt [n]} returns the element of index {\tt n} of the sequence.\\ 10544Input : 10545\begin{center}{\tt (0,3,2)[1]}\end{center} 10546Output : 10547\begin{center}{\tt 3}\end{center} 10548 10549\subsection{Sub-sequence of a sequence : {\tt []}}\index{[]}\index{..} 10550A sequence or a variable name assigned to a sequence followed by 10551{\tt [n1..n2]} returns the sub-sequence of this sequence 10552starting at index {\tt n1} and ending at index {\tt n2}.\\ 10553Input : 10554\begin{center}{\tt (0,1,2,3,4)[1..3]}\end{center} 10555Output : 10556\begin{center}{\tt (1,2,3)}\end{center} 10557 10558\subsection{Make a sequence or a list : {\tt seq \$}}\index{seq|textbf}\index{\$|textbf} 10559\noindent{\tt seq} takes two, three, four or five arguments : the first 10560argument is an expression depending of a parameter (for example $j$) and 10561the remaining argument(s) describe which values of $j$ will be used to 10562generate the sequence. More precisely $j$ is assumed to move 10563from $a$ to $b$: 10564\begin{itemize} 10565\item with a default step of 1 or -1: {\tt j=a..b} or 10566{\tt j,a..b} (Maple-like syntax), {\tt j,a,b} (TI-like syntax) 10567\item or with a specific step: 10568{\tt j=a..b,p} (Maple-like syntax), {\tt j,a,b,p} (TI-like syntax). 10569\end{itemize} 10570If the Maple-like syntax is used, {\tt seq} returns a sequence, 10571if the TI-like syntax is used, {\tt seq} returns a list. 10572 10573{\tt \$} is the infixed version of {\tt seq} when {\tt seq} has only two 10574arguments and always returns a sequence.\\ 10575{\bf Remark:} 10576\begin{itemize} 10577\item In {\tt Xcas} mode, the precedence of {\tt \$} is not the same as 10578for example in {\tt Maple}, in case of doubt 10579put the arguments of {\tt \$} in parenthesis. 10580For example, the equivalent of {\tt seq(j\verb|^|2,j=-1..3)} is 10581{\tt (j\verb|^|2)\$(j=-1..3)} and 10582returns {\tt (1,0,1,4,9)}. 10583The equivalent of {\tt seq(4,3)} is {\tt 4\$3} and returns 10584{\tt (4,4,4)}. 10585\item 10586With {\tt Maple} syntax, {\tt j,a..b,p} is not valid. 10587To specify a step $p$ for the variation of 10588$j$ from $a$ to $b$, use {\tt j=a..b,p} or use the {\tt TI} syntax 10589{\tt j,a,b,p} and get the sequence from the list with {\tt op(...)}. 10590\end{itemize} 10591In summary, the different way to build a sequence are : 10592\begin{itemize} 10593\item with {\tt Maple}-like {\bf syntax} 10594\begin{enumerate} 10595\item {\tt seq} has two arguments, 10596either an expression depending on a parameter 10597(for example $j$) and $j=a..b$ where $a$ and $b$ are reals, 10598or a constant expression and an integer $n$.\\ 10599{\tt seq} returns the sequence where $j$ is replaced in the 10600expression by $a$, $a+1$,...,$b$ if $b>a$ and by $a$, $a-1$,...,$b$ if $b<a$, 10601or {\tt seq} returns the sequence made by copying the constant $n$ times. 10602\item {\tt seq} has three arguments, an expression depending on a parameter 10603(for example $j$) and $j=a..b,p$ where $a$, $b$ are reals and $p$ is a 10604real number.\\ 10605{\tt seq} returns the sequence where $j$ is replaced in the 10606expression by $a$, $a+p$,...,$b$ if $b>a$ and by $a$, $a-p$,...,$b$ 10607if $b<a$.\\ 10608Note that $j,a..b$ is also valid but $j,a..b,p$ is not valid. 10609\end{enumerate} 10610\item {\tt TI} {\bf syntax} 10611\begin{enumerate} 10612\item {\tt seq} has four arguments, an expression depending on a parameter (for 10613example $j$), the name of the parameter (for example $j$), $a$ and $b$ where 10614$a$ and $b$ are reals.\\ 10615{\tt seq} returns the list where $j$ is replaced in the 10616expression by $a$, $a+1$,...,$b$ if $b>a$ and by $a$, $a-1$,...,$b$ if $b<a$. 10617\item {\tt seq} has five arguments, an expression depending on a parameter (for 10618example $j$), the name of the parameter (for example $j$), $a$, $b$ and $p$ 10619where $a$, $b$ and $p$ are reals.\\ 10620{\tt seq} returns the list where 10621$j$ is substituted in the 10622expression by $a$, $a+p$,...,$a+k*p$ ($a+k*p \leq b <a+(k+1)*p$ or 10623$a+k*p \geq b> a+(k+1)*p$). 10624By default, $p$=1 if $b>a$ and $p$=-1 if $b<a$. 10625%If the sign of $p$ is not correct, {\tt Xcas} corrects it ! 10626\end{enumerate} 10627\end{itemize} 10628{\bf Note} that 10629in {\tt Maple} syntax, {\tt seq} takes no more than 3 arguments and 10630returns a sequence, 10631while in {\tt TI} syntax, {\tt seq} takes at least 4 arguments 10632and returns a list.\\ 10633Input to have a sequence with same elements : 10634\begin{center}{\tt seq(t,4)}\end{center} 10635or : 10636\begin{center}{\tt seq(t,k=1..4)}\end{center} 10637or : 10638\begin{center}{\tt t\$4}\end{center} 10639Output : 10640\begin{center}{\tt (t,t,t,t)}\end{center} 10641Input to have a sequence : 10642\begin{center}{\tt seq(j\verb|^|3,j=1..4)}\end{center} 10643or : 10644\begin{center}{\tt (j\verb|^|3)\$(j=1..4)}\end{center} 10645or : 10646\begin{center}{\tt seq(j\verb|^|3,j,1..4)}\end{center} 10647Output : 10648\begin{center}{\tt (1,8,27,64)}\end{center} 10649Input to have a sequence : 10650\begin{center}{\tt seq(j\verb|^|3,j=-1..4,2)}\end{center} 10651Output : 10652\begin{center}{\tt (-1,1,27)}\end{center} 10653Or to have a list,\\ 10654Input : 10655\begin{center}{\tt seq(j\verb|^|3,j,1,4)}\end{center} 10656Output : 10657\begin{center}{\tt [1,8,27,64]}\end{center} 10658Input : 10659\begin{center}{\tt seq(j\verb|^|3,j,0,5,2)}\end{center} 10660Output : 10661\begin{center}{\tt [0,8,64]}\end{center} 10662Input : 10663\begin{center}{\tt seq(j\verb|^|3,j,5,0,-2)}\end{center} 10664or 10665\begin{center}{\tt seq(j\verb|^|3,j,5,0,2)}\end{center} 10666Output : 10667\begin{center}{\tt [125,27,1]}\end{center} 10668Input : 10669\begin{center}{\tt seq(j\verb|^|3,j,1,3,0.5)}\end{center} 10670Output : 10671\begin{center}{\tt [1,3.375,8,15.625,27]}\end{center} 10672Input : 10673\begin{center}{\tt seq(j\verb|^|3,j,1,3,1/2)}\end{center} 10674Output : 10675\begin{center}{\tt [1,27/8,8,125/8,27]}\end{center} 10676{\bf Examples} 10677\begin{itemize} 10678\item Find the third derivative of $\ \ln(t)$, input: 10679\begin{center}{\tt diff(log(t),t\$3)}\end{center} 10680Output : 10681\begin{center}{\tt -((-(2*t))/t\verb|^|4)}\end{center} 10682\item Input : 10683\begin{center}{\tt l:=[[2,3],[5,1],[7,2]]}\end{center} 10684\begin{center}{\tt seq((l[k][0])\$(l[k][1]),k=0 .. size(l)-1)}\end{center} 10685Output : 10686\begin{center}{\tt 2,2,2,seq[5],7,7}\end{center} 10687then {\tt eval(ans())} returns: 10688\begin{center}{\tt 2,2,2,5,7,7}\end{center} 10689\item Input to transform a string into the list of its characters : 10690\begin{verbatim} 10691f(chn):={ 10692 local l; 10693 l:=size(chn); 10694 return seq(chn[j],j,0,l-1); 10695} 10696\end{verbatim} 10697then input: 10698\begin{center}{\tt f("abracadabra")}\end{center} 10699Output : 10700\begin{center}{\tt 10701 ["a","b","r","a","c","a","d","a","b","r","a"]}\end{center} 10702\end{itemize} 10703 10704\subsection{Transform a sequence into a list : {\tt [] nop}}\index{[]}\index{nop} 10705To transform a sequence into list, just put square brackets ({\tt []}) around 10706the sequence or use the command {\tt nop}.\\ 10707Input : 10708\begin{center}{\tt [seq(j\verb|^|3,j=1..4)]}\end{center} 10709or : 10710\begin{center}{\tt seq(j\verb|^|3,j,1,4)}\end{center} 10711or : 10712\begin{center}{\tt [(j\verb|^|3)\$(j=1..4)]}\end{center} 10713Output : 10714\begin{center}{\tt [1,4,9,16]}\end{center} 10715Input : 10716\begin{center}{\tt nop(1,4,9,16)}\end{center} 10717Output : 10718\begin{center}{\tt [1,4,9,16]}\end{center} 10719 10720\subsection{The {\tt +} operator applied on sequences}\index{+} 10721The infixed operator {\tt +}, with two sequences as argument, 10722returns the total sum of the elements of the two sequences.\\ 10723Note the difference with the lists, where 10724the term by term sums of the elements of the two lists would 10725be returned.\\ 10726Input : 10727\begin{center}{\tt (1,2,3,4,5,6)+(4,3,5)}\end{center} 10728or : 10729\begin{center}{\tt '+'((1,2,3,4,5,6),(4,3,5))}\end{center} 10730Output : 10731\begin{center}{\tt 33}\end{center} 10732But input : 10733\begin{center}{\tt [1,2,3,4,5,6]+[4,3,5]}\end{center} 10734Output : 10735\begin{center}{\tt [5,5,8,4,5,6]}\end{center} 10736{\bf Warning}\\ 10737When the operator {\tt +} is prefixed, it has to be quoted ({\tt '+'}). 10738 10739\section{Sets} 10740\subsection{Definition : {\tt set[]}}\index{\%\{ \%\}}\index{set[]} 10741To define a set of elements, put the elements separated by a comma, with 10742{\tt \%\{ ... \%\}} or {\tt set[ ... ]} as delimiters.\\ 10743Input : 10744\begin{center} 10745{\tt \%\{1,2,3,4\%\}}\\ 10746{\tt set[1,2,3,4]} 10747\end{center} 10748In the {\tt Xcas} answers, the set delimiters are displayed 10749as $\llbracket$ and $\rrbracket$ in order 10750not to confuse sets with lists. 10751For example, $\llbracket$1,2,3$\rrbracket$ is the set {\tt \%\{1,2,3\%\}}, 10752unlike [1,2,3] (normal brackets) which is the list {\tt [1,2,3]}.\\ 10753Input : 10754\begin{center}{\tt A:=\%\{1,2,3,4\%\}} or {\tt A:=set[1,2,3,4]}\end{center} 10755Output : 10756\begin{center}{\tt $\llbracket$1,2,3,4$\rrbracket$ }\end{center} 10757Input : 10758\begin{center}{\tt B:=\%\{5,5,6,3,4\%\}} or {\tt B:=set[5,5,6,3,4]}\end{center} 10759Output : 10760\begin{center}{\tt $\llbracket$5,6,3,4$\rrbracket$ }\end{center} 10761{\bf Remark}\\ 10762The order in a set is not significant and 10763the elements in a set are all distinct. If you input 10764{\tt B:=\%\{5,5,6,3,4\%\}} and {\tt C:=\%\{3,4,5,3,6\%\}}, then 10765{\tt B==C} will return {\tt 1}. 10766 10767\subsection{Union of two sets or of two lists : {\tt union}}\index{union} 10768\noindent{\tt union} is an infixed operator.\\ 10769{\tt union} takes as argument two sets or two lists, 10770{\tt union} returns the union set of the arguments.\\ 10771Input : 10772\begin{center}{\tt set[1,2,3,4] union set[5,6,3,4]}\end{center} 10773or : 10774\begin{center}{\tt \%\{1,2,3,4\%\} union \%\{5,6,3,4\%\}}\end{center} 10775Output : 10776\begin{center}{\tt $\llbracket$1,2,3,4,5,6$\rrbracket$}\end{center} 10777Input : 10778\begin{center}{\tt [1,2,3] union [2,5,6]}\end{center} 10779Output : 10780\begin{center}{\tt $\llbracket$1,2,3,5,6$\rrbracket$}\end{center} 10781 10782\subsection{Intersection of two sets or of two lists : {\tt intersect}}\index{intersect} 10783\noindent {\tt intersect} is an infixed operator.\\ 10784{\tt intersect} takes as argument two sets or two lists.\\ 10785{\tt intersect} returns the intersection set of the arguments.\\ 10786Input : 10787\begin{center}{\tt set[1,2,3,4] intersect set[5,6,3,4]}\end{center} 10788or : 10789\begin{center}{\tt \%\{1,2,3,4\%\} intersect \%\{5,6,3,4\%\}}\end{center} 10790Output : 10791\begin{center}{\tt $\llbracket$3,4$\rrbracket$}\end{center} 10792Input : 10793\begin{center}{\tt [1,2,3,4] intersect [5,6,3,4]}\end{center} 10794Output : 10795\begin{center}{\tt $\llbracket$3,4$\rrbracket$}\end{center} 10796 10797\subsection{Difference of two sets or of two lists : {\tt minus}}\index{minus} 10798\noindent{\tt minus} is an infixed operator.\\ 10799{\tt minus} takes as argument two sets or two lists.\\ 10800{\tt minus} returns the difference set of the arguments.\\ 10801Input : 10802\begin{center}{\tt set[1,2,3,4] minus set[5,6,3,4]}\end{center} 10803or : 10804\begin{center}{\tt \%\{1,2,3,4\%\} minus \%\{5,6,3,4\%\}}\end{center} 10805Output : 10806\begin{center}{\tt $\llbracket$1,2$\rrbracket$}\end{center} 10807Input : 10808\begin{center}{\tt [1,2,3,4] minus [5,6,3,4]}\end{center} 10809Output : 10810\begin{center}{\tt $\llbracket$1,2$\rrbracket$}\end{center} 10811 10812\section{Lists\index{lists} and vectors\index{vectors}}\index{[]|textbf} 10813\subsection{Definition} 10814A list (or a vector) is delimited by {\tt [ ]}, 10815its elements must be separated by commas. 10816For example, {\tt [1,2,5]} is a list of three integers. 10817 10818Lists can contain lists (for example, a matrix is a list of lists 10819of the same size). Lists may be used to represent vectors 10820(list of coordinates), matrices, univariate polynomials 10821(list of coefficients by decreasing order). 10822 10823Lists are different from sequences, because sequences are flat : an element 10824of a sequence cannot be a sequence. 10825Lists are different from sets, because for a list, the order is 10826important and the same element can be repeated in a list (unlike in 10827a set where each element is unique). 10828 10829In {\tt Xcas} output : 10830\begin{itemize} 10831\item vector (or list) delimiters are displayed as {\tt []}, 10832\item matrix delimiters are displayed as {\bf []}, 10833\item polynomial delimiters are displayed as $\talloblong \ \talloblong$, 10834\item set delimiters are displayed as $\llbracket \ \rrbracket$. 10835\end{itemize} 10836 10837The list elements are indexed starting from 0 in Xcas syntax mode 10838and from 1 in all other syntax modes. 10839 10840 10841\subsection{Get an element or a sub-list of a list : {\tt at []}}\index{at|textbf}\label{sec:at} 10842\subsubsection{Get an element} 10843\noindent The $n$-th element of a list {\tt l} of size $s$ 10844is addressed by {\tt l[n]} where $n$ is in $[0..s-1]$ or $[1..s]$. 10845The equivalent prefixed function is 10846{\tt at}, which takes as argument a list and an integer {\tt n}.\\ 10847{\tt at} returns the element of the list at index {\tt n}.\\ 10848Input : 10849\begin{center}{\tt [0,1,2][1]}\end{center} 10850or : 10851\begin{center}{\tt at([0,1,2],1)}\end{center} 10852Output : 10853\begin{center}{\tt 1}\end{center} 10854 10855\subsubsection{Extract a sub-list} 10856If $l$ is a list of size $s$, {\tt l[n1..n2]} returns the list 10857extracted from {\tt l} containing the elements of indexes $n_1$ to $n_2$ 10858where $0 \leq n_1\leq n_2 < s$ (in Xcas syntax mode) or 10859$0 < n_1\leq n_2 \leq s$ in other syntax modes. 10860The equivalent prefixed function is 10861{\tt at} with a list and an interval of integers ({\tt n1..n2}) 10862as arguments.\\ 10863{\bf See also} : {\tt mid}, section \ref{sec:mid}.\\ 10864Input : 10865\begin{center}{\tt [0,1,2,3,4][1..3]}\end{center} 10866or : 10867\begin{center}{\tt at([0,1,2,3,4],1..3)}\end{center} 10868Output : 10869\begin{center}{\tt [1,2,3]}\end{center} 10870{\bf Warning}\\ 10871{\tt at} can not be used for sequences, index notation 10872must be used, as in {\tt (0,1,2,3,4,5)[2..3]}. 10873 10874 10875\subsection{Extract a sub-list : {\tt mid}}\index{mid}\label{sec:mid} 10876{\bf See also :} {\tt at} section \ref{sec:at}.\\ 10877\noindent{\tt mid} is used to extract a sub-list of a list\index{mid}.\\ 10878{\tt mid} takes as argument a list, the index of the beginning of the 10879sub-list and the length of the sub-list.\\ 10880{\tt mid} returns the sub-list.\\ 10881Input : 10882\begin{center}{\tt mid([0,1,2,3,4,5],2,3)}\end{center} 10883Output : 10884\begin{center}{\tt [1,2,3]}\end{center} 10885{\bf Warning}\\ 10886{\tt mid} can not be used to extract a subsequence of a sequence, 10887because the arguments of {\tt mid} would be merged with the sequence. 10888Index notation must be used, like e.g.{\tt (0,1,2,3,4,5)[2..3]}. 10889 10890\subsection{Get the first element of a list : {\tt head}}\index{head} 10891\noindent{\tt head} takes as argument a list.\\ 10892{\tt head} returns the first element of this list.\\ 10893Input : 10894\begin{center}{\tt head([0,1,2,3])}\end{center} 10895Output : 10896\begin{center}{\tt 0}\end{center} 10897{\tt a:=head([0,1,2,3])} does the same thing as {\tt a:=[0,1,2,3][0]} 10898 10899\subsection{Remove an element in a list : {\tt suppress}}\index{suppress} 10900\noindent{\tt suppress} takes as argument a list and an integer {\tt n}.\\ 10901{\tt suppress} returns the list where the element of index {\tt n} is 10902removed.\\ 10903Input : 10904\begin{center}{\tt suppress([3,4,2],1)}\end{center} 10905Output : 10906\begin{center}{\tt [3,2]}\end{center} 10907 10908\subsection{Remove the first element : {\tt tail}}\index{tail} 10909\noindent{\tt tail} takes as argument a list. 10910{\tt tail} returns the list without its first element.\\ 10911Input : 10912\begin{center}{\tt tail([0,1,2,3])}\end{center} 10913Output : 10914\begin{center}{\tt [1,2,3]}\end{center} 10915{\tt l:=tail([0,1,2,3])} does the same thing as 10916{\tt l:=suppress([0,1,2,3],0)}\\ 10917 10918\subsection{Reverse order in a list : {\tt revlist}}\index{revlist} 10919\noindent{\tt revlist} takes as argument a list (resp. sequence).\\ 10920{\tt revlist} returns the list (resp. sequence) in the reverse order.\\ 10921Input : 10922\begin{center}{\tt revlist([0,1,2,3,4])}\end{center} 10923Output : 10924\begin{center}{\tt [4,3,2,1,0]}\end{center} 10925Input : 10926\begin{center}{\tt revlist([0,1,2,3,4],3)}\end{center} 10927Output : 10928\begin{center}{\tt 3,[0,1,2,3,4]}\end{center} 10929 10930\subsection{Reverse a list starting from its n-th element : {\tt rotate}}\index{rotate} 10931\noindent{\tt rotate} takes as argument a list and an integer {\tt n} (by 10932default {\tt n=-1}).\\ 10933{\tt rotate} rotates the list by {\tt n} places to the left if {\tt n>0} 10934or to the right if {\tt n<0}. Elements leaving the list from one 10935side come back on the other side. 10936By default {\tt n=-1} and the last element becomes first.\\ 10937Input : 10938\begin{center}{\tt rotate([0,1,2,3,4])}\end{center} 10939Output : 10940\begin{center}{\tt [4,0,1,2,3]}\end{center} 10941Input : 10942\begin{center}{\tt rotate([0,1,2,3,4],2)}\end{center} 10943Output : 10944\begin{center}{\tt [2,3,4,0,1]}\end{center} 10945Input : 10946\begin{center}{\tt rotate([0,1,2,3,4],-2)}\end{center} 10947Output : 10948\begin{center}{\tt [3,4,0,1,2]}\end{center} 10949 10950\subsection{Permuted list from its n-th element : {\tt shift}}\index{shift} 10951\noindent{\tt shift} takes as argument a list {\tt l} and an integer {\tt n} 10952(by default {\tt n}=-1).\\ 10953{\tt shift} rotates the list to the left if {\tt n>0} or to 10954the right if {\tt n<0}. Elements leaving the list from one side 10955are replaced by {\tt undef} on the other side.\\ 10956Input : 10957\begin{center}{\tt shift([0,1,2,3,4])}\end{center} 10958Output : 10959\begin{center}{\tt [undef,0,1,2,3]}\end{center} 10960Input : 10961\begin{center}{\tt shift([0,1,2,3,4],2)}\end{center} 10962Output : 10963\begin{center}{\tt [2,3,4,undef,undef]}\end{center} 10964Input : 10965\begin{center}{\tt shift([0,1,2,3,4],-2)}\end{center} 10966Output : 10967\begin{center}{\tt [undef,undef,0,1,2]}\end{center} 10968 10969\subsection{Modify an element in a list : {\tt subsop}}\index{subsop} 10970\noindent {\tt subsop} modifies an element in a list. 10971%? directly (it is not necessary to store this element in a variable).\\ 10972{\tt subsop} takes as argument a list and an equality (an index=a new 10973value) in all syntax modes, but in {\tt Maple} syntax mode 10974the order of the arguments is reversed. \\ 10975{\bf Remark} If the second argument is {\tt 'k=NULL'}, the element of index 10976{\tt k} is removed of the list.\\ 10977Input in {\tt Xcas} mode (the index of the first element is 0) : 10978\begin{center}{\tt subsop([0,1,2],1=5)}\end{center} 10979or : 10980\begin{center}{\tt L:=[0,1,2];L[1]:=5}\end{center} 10981Output : 10982\begin{center}{\tt [0,5,2]}\end{center} 10983Input in {\tt Xcas} mode (the index of the first element is 0) : 10984\begin{center}{\tt subsop([0,1,2],'1=NULL')}\end{center} 10985Output : 10986\begin{center}{\tt [0,2]}\end{center} 10987 10988Input in {\tt Mupad TI} mode (the index of the first element is 1) : 10989\begin{center}{\tt subsop([0,1,2],2=5)}\end{center} 10990or : 10991\begin{center}{\tt L:=[0,1,2];L[2]:=5}\end{center} 10992Output : 10993\begin{center}{\tt [0,5,2]}\end{center} 10994In {\tt Maple} mode the arguments are permuted and the index of 10995the first element is 1.\\ 10996Input : 10997\begin{center}{\tt subsop(2=5,[0,1,2])}\end{center} 10998or : 10999\begin{center}{\tt L:=[0,1,2];L[2]:=5}\end{center} 11000Output : 11001\begin{center}{\tt [0,5,2]}\end{center} 11002 11003\subsection{Transform a list into a sequence : {\tt op makesuite}}\index{op}\index{makesuite} 11004\noindent{\tt op} or {\tt makesuite} takes as argument a list.\\ 11005{\tt op} or {\tt makesuite} transforms this list into a sequence. 11006\label{sec:makesuiteop}\\ 11007See \ref{sec:op} for other usages of {\tt op}.\\ 11008Input : 11009\begin{center}{\tt op([0,1,2])}\end{center} 11010or : 11011\begin{center}{\tt makesuite([0,1,2])}\end{center} 11012Output : 11013\begin{center}{\tt (0,1,2)}\end{center} 11014 11015\subsection{Transform a sequence into a list : {\tt makevector []}}\index{makevector}\index{[]} 11016Square brackets put around a sequence transform this sequence into a list 11017or vector. The equivalent prefixed function is 11018{\tt makevector} which takes a sequence as argument.\\ 11019{\tt makevector} transforms this sequence into a list or vector.\\ 11020Input : 11021\begin{center}{\tt makevector(0,1,2)}\end{center} 11022Output : 11023\begin{center}{\tt [0,1,2]}\end{center} 11024Input : 11025\begin{center}{\tt a:=(0,1,2)}\end{center} 11026Input : 11027\begin{center}{\tt [a]}\end{center} 11028or : 11029\begin{center}{\tt makevector(a)}\end{center} 11030Output : 11031\begin{center}{\tt [0,1,2]}\end{center} 11032 11033\subsection{Length of a list : {\tt size nops length}}\index{size}\index{nops}\index{length} 11034\noindent{\tt size} or {\tt nops} or {\tt length} takes as argument a list 11035(resp. sequence).\\ 11036{\tt size} or {\tt nops} or {\tt length} returns the length of this list (resp. 11037 sequence).\\ 11038Input : 11039\begin{center}{\tt nops([3,4,2])}\end{center} 11040or : 11041\begin{center}{\tt size([3,4,2])}\end{center} 11042or : 11043\begin{center}{\tt length([3,4,2])}\end{center} 11044Output : 11045\begin{center}{\tt 3}\end{center} 11046 11047\subsection{Sizes of a list of lists : {\tt sizes}}\index{sizes} 11048\noindent {\tt sizes} takes as argument a list of lists.\\ 11049{\tt sizes} returns the list of the lengths of these lists.\\ 11050Input : 11051\begin{center}{\tt sizes([[3,4],[2]])}\end{center} 11052Output : 11053\begin{center}{\tt [2,1]}\end{center} 11054 11055\subsection{Concatenate two lists or a list and an element : {\tt concat augment}}\index{concat|textbf}\index{augment|textbf} 11056\noindent{\tt concat} (or {\tt augment}) takes as argument a list and an 11057element or two lists.\\ 11058{\tt concat} (or {\tt augment}) concats this list and this element, or concats 11059these two lists.\\ 11060Input : 11061\begin{center}{\tt concat([3,4,2],[1,2,4])}\end{center} 11062or : 11063\begin{center}{\tt augment([3,4,2],[1,2,4])}\end{center} 11064Output : 11065\begin{center}{\tt [3,4,2,1,2,4]}\end{center} 11066Input : 11067\begin{center}{\tt concat([3,4,2],5)}\end{center} 11068or : 11069\begin{center}{\tt augment([3,4,2],5)}\end{center} 11070Output : 11071\begin{center}{\tt [3,4,2,5]}\end{center} 11072{\bf Warning} 11073If you input : 11074\begin{center}{\tt concat([[3,4,2]],[[1,2,4]])}\end{center} 11075or 11076\begin{center}{\tt augment([[3,4,2]],[[1,2,4]])}\end{center} 11077the output will be: 11078\begin{center}{\tt [[3,4,2,1,2,4]]}\end{center} 11079 11080\subsection{Append an element at the end of a list : {\tt append}}\index{append} 11081\noindent{\tt append} takes as argument a list and an element.\\ 11082{\tt append} puts this element at the end of this list.\\ 11083Input : 11084\begin{center}{\tt append([3,4,2],1)}\end{center} 11085Output : 11086\begin{center}{\tt [3,4,2,1]}\end{center} 11087Input : 11088\begin{center}{\tt append([1,2],[3,4])}\end{center} 11089Output : 11090\begin{center}{\tt [1,2,[3,4]]}\end{center} 11091 11092\subsection{Prepend an element at the beginning of a list : {\tt prepend}}\index{prepend} 11093\noindent{\tt prepend} takes as argument a list and an element.\\ 11094{\tt prepend} puts this element at the beginning of this list.\\ 11095Input : 11096\begin{center}{\tt prepend([3,4,2],1)}\end{center} 11097Output : 11098\begin{center}{\tt [1,3,4,2]}\end{center} 11099Input : 11100\begin{center}{\tt prepend([1,2],[3,4])}\end{center} 11101Output : 11102\begin{center}{\tt [[3,4],1,2]}\end{center} 11103 11104\subsection{Sort : {\tt sort}}\index{sort} 11105\noindent{\tt sort} takes as argument a list or an expression. 11106\begin{itemize} 11107\item For a list,\\ 11108{\tt sort} returns the list sorted in increasing order.\\ 11109Input : 11110\begin{center}{\tt sort([3,4,2])}\end{center} 11111Output : 11112\begin{center}{\tt [2,3,4]}\end{center} 11113 11114\item For an expression,\\ 11115 {\tt sort} sorts and collects terms in sums and products.\\ 11116Input : 11117\begin{center}{\tt sort(exp(2*ln(x))+x*y-x+y*x+2*x)}\end{center} 11118Output : 11119\begin{center}{\tt 2*x*y+exp(2*ln(x))+x}\end{center} 11120Input : 11121\begin{center}{\tt simplify(exp(2*ln(x))+x*y-x+y*x+2*x)}\end{center} 11122Output : 11123\begin{center}{\tt x\verb|^|2+2*x*y+x}\end{center} 11124\end{itemize} 11125{\tt sort} accepts an optional second argument, which is a bivariate 11126function returning 0 or 1. If provided, this function 11127will be used to sort the list, for example 11128{\tt (x,y)->x>=y} may be used as second argument 11129to sort the list in decreasing order. 11130This may also be used to sort list of lists 11131(that {\tt sort} with one argument does not know how to sort).\\ 11132Input : 11133\begin{center}{\tt sort([3,4,2],(x,y)->x>=y)}\end{center} 11134Output : 11135\begin{center}{\tt [4,3,2]}\end{center} 11136 11137\subsection{Sort a list by increasing order : {\tt SortA}}\index{SortA} 11138\noindent{\tt SortA} takes as argument a list.\\ 11139{\tt SortA} returns this list sorted by increasing order.\\ 11140Input : 11141\begin{center}{\tt SortA([3,4,2])}\end{center} 11142Output : 11143\begin{center}{\tt [2,3,4]}\end{center} 11144{\tt SortA} may have a matrix as argument and in this case, 11145{\tt SortA} modifies the order of columns by sorting the first matrix 11146row by increasing order.\\ 11147Input : 11148\begin{center}{\tt SortA([[3,4,2],[6,4,5]])}\end{center} 11149Output : 11150\begin{center}{\tt [[2,3,4],[5,6,4]]}\end{center} 11151 11152\subsection{Sort a list by decreasing order : {\tt SortD}}\index{SortD} 11153\noindent{\tt SortD} takes a list as argument.\\ 11154{\tt SortD} returns this list sorted by decreasing order.\\ 11155Input : 11156\begin{center}{\tt SortD([3,4,2])}\end{center} 11157Output : 11158\begin{center}{\tt [2,3,4]}\end{center} 11159{\tt SortD} may have a matrix as argument and in this case, 11160{\tt SortD} modifies the order of columns by sorting the first matrix 11161row by decreasing order.\\ 11162Input : 11163\begin{center}{\tt SortD([[3,4,2],[6,4,5]])}\end{center} 11164Output : 11165\begin{center}{\tt [[4,3,2],[4,6,5]]}\end{center} 11166 11167\subsection{Select the elements of a list : {\tt select}}\index{select} 11168\noindent{\tt select} takes as arguments : a boolean function {\tt f} 11169 and a list {\tt L}.\\ 11170{\tt select} selects in the list {\tt L}, the elements {\tt c} such that 11171{\tt f(c)==true}.\\ 11172Input : 11173\begin{center}{\tt select(x->(x>=2),[0,1,2,3,1,5])}\end{center} 11174Output : 11175\begin{center}{\tt [2,3,5]}\end{center} 11176 11177\subsection{Remove elements of a list : {\tt remove}}\index{remove} 11178\noindent{\tt remove} takes as argument : a boolean function {\tt f} and 11179a list {\tt L}.\\ 11180{\tt remove} removes in the list {\tt L}, the elements {\tt c} such that 11181{\tt f(c)==true}.\\ 11182Input : 11183\begin{center}{\tt remove(x->(x>=2),[0,1,2,3,1,5])}\end{center} 11184Output : 11185\begin{center}{\tt [0,1,1]}\end{center} 11186{\bf Remark} The same applies on strings, for example, 11187to remove all the "a" of a string:\\ 11188Input : 11189\begin{center}{\tt ord("a")}\end{center} 11190Output : 11191\begin{center}{\tt 97}\end{center} 11192Input : 11193\begin{verbatim} 11194f(chn):={ 11195 local l:=length(chn)-1; 11196 return remove(x->(ord(x)==97),seq(chn[k],k,0,l)); 11197} 11198\end{verbatim} 11199Then, input : 11200\begin{center}{\tt f("abracadabra")}\end{center} 11201Output : 11202\begin{center}{\tt ["b","r","c","d","b","r"]}\end{center} 11203To get a string, input : 11204\begin{center}{\tt char(ord(["b","r","c","d","b","r"])}\end{center} 11205Output : 11206\begin{center}{\tt "brcdbr"}\end{center} 11207 11208\subsection{Test if a value is in a list : {\tt member}}\index{member|textbf} 11209\noindent{\tt member} takes as argument a value {\tt c} and a list 11210(or a set) {\tt L}.\\ 11211{\tt member} is a function that tests if {\tt c} is an element of the 11212list {\tt L}.\\ 11213{\tt member} returns {\tt 0} if {\tt c} is not in {\tt L}, or 11214a strictly positive integer which is 112151 plus the index of the first occurrence of {\tt c} in {\tt L}.\\ 11216Note the order of the arguments (required for compatibility reasons)\\ 11217Input : 11218\begin{center}{\tt member(2,[0,1,2,3,4,2])}\end{center} 11219Output : 11220\begin{center}{\tt 3}\end{center} 11221Input : 11222\begin{center}{\tt member(2,\%\{0,1,2,3,4,2\%\})}\end{center} 11223Output : 11224\begin{center}{\tt 3}\end{center} 11225 11226\subsection{Test if a value is in a list : {\tt contains}}\index{contains|textbf} 11227\noindent{\tt contains} takes as argument a list (or a set) 11228{\tt L} and a value {\tt c}.\\ 11229{\tt contains} tests if {\tt c} is an element of the list {\tt L}.\\ 11230 {\tt contains} returns {\tt 0} if {\tt c} is not in {\tt L}, 11231or a strictly positive integer which is 112321+the index of the first occurrence of {\tt c} in {\tt L}.\\ 11233Input : 11234\begin{center}{\tt contains([0,1,2,3,4,2],2)}\end{center} 11235Output : 11236\begin{center}{\tt 3}\end{center} 11237Input : 11238\begin{center}{\tt contains(\%\{0,1,2,3,4,2\%\},2)}\end{center} 11239Output : 11240\begin{center}{\tt 3}\end{center} 11241 11242\subsection{Sum of list (or matrix) elements 11243transformed by a function : {\tt count}}\index{count|textbf} 11244\noindent{\tt count} takes as argument : a real function {\tt f} and a list 11245{\tt l} of length {\tt n} (or a matrix {\tt A} of dimension {\tt p*q}).\\ 11246{\tt count} applies the function to the list (or matrix) elements and returns 11247their sum, i.e. :\\ 11248{\tt count(f,l)} returns {\tt f(l[0])+f(l[1])+...+f(l[n-1])} or\\ 11249{\tt count(f,A)} returns {\tt f(A[0,0])+....+f(A[p-1,q-1])}.\\ 11250If {\tt f} is a boolean function {\tt count} returns the number of elements 11251of the list (or of the matrix) for which the boolean function is true.\\ 11252Input : 11253\begin{center}{\tt count((x)->x,[2,12,45,3,7,78])}\end{center} 11254Output : 11255\begin{center}{\tt 147}\end{center} 11256because : 2+12+45+3+7+78=147.\\ 11257Input : 11258\begin{center}{\tt count((x)->x<12,[2,12,45,3,7,78])}\end{center} 11259Output : 11260\begin{center}{\tt 3}\end{center} 11261Input : 11262\begin{center}{\tt count((x)->x==12,[2,12,45,3,7,78])}\end{center} 11263Output : 11264\begin{center}{\tt 1}\end{center} 11265Input : 11266\begin{center}{\tt count((x)->x>12,[2,12,45,3,7,78])}\end{center} 11267Output : 11268\begin{center}{\tt 2}\end{center} 11269Input : 11270\begin{center}{\tt count(x->x\verb|^|2,[3,5,1])}\end{center} 11271Output : 11272\begin{center}{\tt 35}\end{center} 11273Indeed $3^2+5^2+1^1=35$.\\ 11274Input : 11275\begin{center}{\tt count(id,[3,5,1])}\end{center} 11276Output : 11277\begin{center}{\tt 9}\end{center} 11278Indeed, {\tt id} is the identity functions and 3+5+1=9.\\ 11279Input : 11280\begin{center}{\tt count(1,[3,5,1])}\end{center} 11281Output : 11282\begin{center}{\tt 3}\end{center} 11283Indeed, {\tt 1} is the constant function equal to 1 and 1+1+1=3. 11284 11285\subsection{Number of elements equal to a given value : {\tt count\_eq}}\index{count\_eq|textbf} 11286\noindent{\tt count\_eq} takes as argument : a real and a real list 11287(or matrix).\\ 11288{\tt count\_eq} returns the number of elements of the list (or matrix) 11289which are equal to the first argument.\\ 11290Input : 11291\begin{center}{\tt count\_eq(12,[2,12,45,3,7,78])}\end{center} 11292Output : 11293\begin{center}{\tt 1}\end{center} 11294 11295\subsection{Number of elements smaller than a given value : {\tt count\_inf}}\index{count\_inf|textbf} 11296\noindent{\tt count\_inf} takes as argument : a real and a real list 11297(or matrix).\\ 11298{\tt count\_inf} returns the number of elements of the list (or matrix) which 11299are strictly less than the first argument.\\ 11300Input : 11301\begin{center}{\tt count\_inf(12,[2,12,45,3,7,78])}\end{center} 11302Output : 11303\begin{center}{\tt 3}\end{center} 11304 11305\subsection{Number of elements greater than a given value : {\tt count\_sup}}\index{count\_sup|textbf} 11306\noindent{\tt count\_sup} takes as argument : a real and a real list 11307(or matrix).\\ 11308{\tt count\_sup} returns the number of elements of the list 11309(or matrix) which are strictly greater than the first argument.\\ 11310Input : 11311\begin{center}{\tt count\_sup(12,[2,12,45,3,7,78])}\end{center} 11312Output : 11313\begin{center}{\tt 2}\end{center} 11314 11315\subsection{Sum of elements of a list : {\tt sum add}}\index{sum}\index{add} 11316\noindent{\tt sum} or {\tt add} takes as argument a list {\tt l} (resp. 11317sequence) of reals.\\ 11318{\tt sum} or {\tt add} returns the sum of the elements of {\tt l}.\\ 11319Input : 11320\begin{center}{\tt sum(2,3,4,5,6)}\end{center} 11321Output : 11322\begin{center}{\tt 20}\end{center} 11323 11324\subsection{Cumulated sum of the elements of a list : {\tt cumSum}}\index{cumSum|textbf} 11325\noindent{\tt cumSum} takes as argument a list {\tt l} (resp. sequence) 11326of numbers or of strings.\\ 11327{\tt cumSum} returns the list (resp. sequence) with same length as {\tt l} and 11328 with $k$-th element the sum (or concatenation) of the 11329elements ${\tt l[0],..,l[k]}$.\\ 11330Input : 11331\begin{center}{\tt cumSum(sqrt(2),3,4,5,6)}\end{center} 11332Output : 11333\begin{center}{\tt sqrt(2),3+sqrt(2),3+sqrt(2)+4,3+sqrt(2)+4+5,}\end{center} 11334\begin{center}{\tt 3+sqrt(2)+4+5+6}\end{center} 11335Input : 11336\begin{center}{\tt normal(cumSum(sqrt(2),3,4,5,6))}\end{center} 11337Output : 11338\begin{center}{\tt sqrt(2),sqrt(2)+3,sqrt(2)+7,sqrt(2)+12,sqrt(2)+18}\end{center}Input : 11339\begin{center}{\tt cumSum(1.2,3,4.5,6)}\end{center} 11340Output : 11341\begin{center}{\tt 1.2,4.2,8.7,14.7}\end{center} 11342Input : 11343\begin{center}{\tt cumSum([0,1,2,3,4])}\end{center} 11344Output : 11345\begin{center}{\tt [0,1,3,6,10]}\end{center} 11346Input : 11347\begin{center}{\tt cumSum("a","b","c","d")}\end{center} 11348Output : 11349\begin{center}{\tt "a","ab","abc","abcd"}\end{center} 11350Input : 11351\begin{center}{\tt cumSum("a","ab","abc","abcd")}\end{center} 11352Output : 11353\begin{center}{\tt "a","aab","aababc","aababcabcd"}\end{center} 11354 11355\subsection{Product : {\tt product mul}}\index{product|textbf}\index{mul|textbf} 11356See also \ref{sec:product}, \ref{sec:product1} and 11357\ref{sec:product2}). 11358 11359\subsubsection{Product of values of an expression : {\tt product}}\label{sec:product0} 11360\noindent{\tt product(expr,var,a,b,p)} or {\tt mul(expr,var,a,b,p)} returns the 11361product of values of an expression {\tt ex} when the variable {\tt var} goes 11362from {\tt a} to {\tt b} with a step {\tt p} (by default p=1) : this syntax is 11363for compatibility with Maple.\\ 11364Input : 11365\begin{center}{\tt product(x\verb|^|2+1,x,1,4)}\end{center} 11366or: 11367\begin{center}{\tt mul(x\verb|^|2+1,x,1,4)}\end{center} 11368Output : 11369\begin{center}{\tt 1700}\end{center} 11370Indeed $2*5*10*17=1700$\\ 11371Input : 11372\begin{center}{\tt product(x\verb|^|2+1,x,1,5,2)}\end{center} 11373or: 11374\begin{center}{\tt mul(x\verb|^|2+1,x,1,5,2)}\end{center} 11375Output : 11376\begin{center}{\tt 520}\end{center} 11377Indeed $2*10*26=520$ 11378 11379\subsubsection{Product of elements of a list : {\tt product}} 11380\noindent{\tt product} or {\tt mul} takes as argument a list {\tt l} 11381of reals (or floating numbers) or two lists of the same size (see 11382also \ref{sec:product0}, \ref{sec:product1} and \ref{sec:product2}). 11383\begin{itemize} 11384\item if {\tt product} or {\tt mul} has a list {\tt l} 11385as argument, {\tt product} or 11386{\tt mul} returns the product of the elements of {\tt l}\label{sec:product}.\\ 11387Input : 11388\begin{center}{\tt product([2,3,4])}\end{center} 11389or : 11390\begin{center}{\tt mul([2,3,4])}\end{center} 11391Output : 11392\begin{center}{\tt 24}\end{center} 11393Input : 11394\begin{center}{\tt product([[2,3,4],[5,6,7]])}\end{center} 11395Output : 11396\begin{center}{\tt [10,18,28]}\end{center} 11397\item if {\tt product} or {\tt mul} takes as arguments 11398{\tt l1} and {\tt l2} 11399(two lists or two matrices), {\tt product} or {\tt mul} returns 11400the term by term product of the elements of {\tt l1} and 11401{\tt l2}.\\ 11402Input : 11403\begin{center}{\tt product([2,3,4],[5,6,7])}\end{center} 11404or : 11405\begin{center}{\tt mul([2,3,4],[5,6,7])}\end{center} 11406Output : 11407\begin{center}{\tt [10,18,28]}\end{center} 11408Input : 11409\begin{center}{\tt product([[2,3,4],[5,6,7]],[[2,3,4],[5,6,7]])}\end{center} 11410or : 11411\begin{center}{\tt mul([[2,3,4],[5,6,7]],[[2,3,4],[5,6,7]])}\end{center} 11412Output : 11413\begin{center}{\tt [[4,9,16],[25,36,49]]}\end{center} 11414\end{itemize} 11415 11416\subsection{Apply a function of one variable to the elements of a list : {\tt map apply of}}\index{map}\index{apply}\index{of} 11417\noindent{\tt map} or {\tt apply} or {\tt of} applies a function to a list 11418of elements.\\ 11419{\tt of} is the prefixed function equivalent to the parenthesis : 11420{\tt Xcas} translates {\tt f(x)} internally to {\tt of(f,x)}. 11421It is more natural to call {\tt map} 11422or {\tt apply} than {\tt of}. Be careful with the order of arguments 11423(that is required for compatibility reasons).\\ 11424Note that {\tt apply} returns a list ({\tt []}) 11425even if the second argument is not a list.\\ 11426Input : 11427\begin{center}{\tt apply(x->x\verb|^|2,[3,5,1])}\end{center} 11428or : 11429\begin{center}{\tt of(x->x\verb|^|2,[3,5,1])}\end{center} 11430or : 11431\begin{center}{\tt map([3,5,1],x->x\verb|^|2)}\end{center} 11432or first define the function $h(x)=x^2$, input : 11433\begin{center}{\tt h(x):=x\verb|^|2}\end{center} 11434then : 11435\begin{center}{\tt apply(h,[3,5,1])}\end{center} 11436or : 11437\begin{center}{\tt of(h,[3,5,1])}\end{center} 11438or : 11439\begin{center}{\tt map([3,5,1],h)}\end{center} 11440Output : 11441\begin{center}{\tt [9,25,1]}\end{center} 11442Next example, define the function $g(x)=[x,x^2,x^3]$, input : 11443\begin{center}{\tt g:=(x)->[x,x\verb|^|2,x\verb|^|3]}\end{center} 11444then : 11445\begin{center}{\tt apply(g,[3,5,1])}\end{center} 11446or : 11447\begin{center}{\tt of(g,[3,5,1])}\end{center} 11448or : 11449\begin{center}{\tt map([3,5,1],g)}\end{center} 11450Output : 11451\begin{center}{\tt [[3,9,27],[5,25,125],[1,1,1]]}\end{center} 11452{\bf Warning!!!} first purge {\tt x} if {\tt x} is not symbolic.\\ 11453Note that if {\tt l1,l2,l3} are lists 11454{\tt sizes([l1,l2,l3])} is equivalent to {\tt map(size,[l1,l2,l3])}. 11455 11456\subsection{Apply a bivariate function to the elements of two lists : {\tt zip}}\index{zip} 11457\noindent{\tt zip} applies a bivariate function to the elements of 2 lists.\\ 11458Input : 11459\begin{center}{\tt zip('sum',[a,b,c,d],[1,2,3,4])}\end{center} 11460Output : 11461\begin{center}{\tt [a+1,b+2,c+3,d+4]}\end{center} 11462Input : 11463\begin{center}{\tt zip((x,y)->x\verb|^|2+y\verb|^|2,[4,2,1],[3,5,1])}\end{center} 11464or : 11465\begin{center}{\tt f:=(x,y)->x\verb|^|2+y\verb|^|2}\end{center} 11466then, 11467\begin{center}{\tt zip(f,[4,2,1],[3,5,1])}\end{center} 11468Output : 11469\begin{center}{\tt [25,29,2]}\end{center} 11470Input : 11471\begin{center}{\tt f:=(x,y)->[x\verb|^|2+y\verb|^|2,x+y]}\end{center} 11472then : 11473\begin{center}{\tt zip(f,[4,2,1],[3,5,1])}\end{center} 11474Output : 11475\begin{center}{\tt [[25,7],[29,7],[2,2]]}\end{center} 11476 11477\subsection{Make a list with zeros : {\tt newList}}\index{newList} 11478\noindent{\tt newList(n)} makes a list of {\tt n} zeros.\\ 11479Input : 11480\begin{center}{\tt newList(3)}\end{center} 11481Output : 11482\begin{center}{\tt [0,0,0]}\end{center} 11483 11484\subsection{Make a list with a function : {\tt makelist}}\index{makelist} 11485\noindent{\tt makelist} takes as argument a function {\tt f}, 11486the bounds {\tt a,b} of an index variable and a step {\tt p} 11487(by default 1 or -1 depending on the bounds order).\\ 11488{\tt makelist} makes the list {\tt [f(a),f(a+p)...f(a+k*p)]} with $k$ such 11489that~: $a<a+k*p \leq b <a+(k+1)*p$ or $a>a+k*p \geq b >a+(k+1)*p$.\\ 11490Input : 11491\begin{center}{\tt makelist(x->x\verb|^|2,3,5)}\end{center} 11492or 11493\begin{center}{\tt makelist(x->x\verb|^|2,3,5,1)}\end{center} 11494or first define the function $h(x)=x^2$ by {\tt h(x):=x\verb|^|2} 11495then input 11496\begin{center}{\tt makelist(h,3,5,1)}\end{center} 11497Output : 11498\begin{center}{\tt [9,16,25]}\end{center} 11499Input : 11500\begin{center}{\tt makelist(x->x\verb|^|2,3,6,2)}\end{center} 11501Output : 11502\begin{center}{\tt [9,25]}\end{center} 11503{\bf Warning!!!} purge {\tt x} if {\tt x} is not symbolic. 11504 11505\subsection{Make a random vector or list : {\tt randvector}}\index{randvector} 11506\label{sec:ranm4} 11507\noindent{\tt randvector} takes as argument an integer $n$ and optionally a 11508second argument, either an integer $k$ or the quoted name of 11509a random distribution law 11510(see also \ref{sec:ranm1}).\\ %, \ref{sec:ranm4} and \ref{sec:ranm3}).\\ 11511{\tt randvector} returns a vector of size $n$ containing random integers 11512uniformly distributed between -99 and +99 (default), or between 0 and $k-1$ 11513or containing random 11514integers according to the law put between quotes.\\ 11515Input : 11516\begin{center}{\tt randvector(3)}\end{center} 11517Output : 11518\begin{center}{\tt [-54,78,-29]}\end{center} 11519Input : 11520\begin{center}{\tt randvector(3,5)}\end{center} 11521or : 11522\begin{center}{\tt randvector(3,'rand(5)')}\end{center} 11523Output : 11524\begin{center}{\tt [1,2,4]}\end{center} 11525Input : 11526\begin{center}{\tt randvector(3,'randnorm(0,1)')}\end{center} 11527Output : 11528\begin{center}{\tt [1.39091705476,-0.136794772167,0.187312440336]}\end{center} 11529Input : 11530\begin{center}{\tt randvector(3,2..4)}\end{center} 11531Output : 11532\begin{center}{\tt [3.92450003885,3.50059241243,2.7322040787]}\end{center} 11533 11534\subsection{List of differences of consecutive terms : {\tt deltalist}}\index{deltalist} 11535\noindent{\tt deltalist} takes as argument a list.\\ 11536{\tt deltalist} returns the list of the difference of all 11537pairs of consecutive terms of this list.\\ 11538Input : 11539\begin{center}{\tt deltalist([5,8,1,9])}\end{center} 11540Output : 11541\begin{center}{\tt [3,-7,8]}\end{center} 11542 11543\subsection{Make a matrix with a list : {\tt list2mat}}\index{list2mat} 11544\noindent{\tt list2mat} takes as argument a list {\tt l} and an integer 11545{\tt p}.\\ 11546{\tt list2mat} returns a matrix having {\tt p} columns 11547by cutting the list {\tt l} in rows of length {\tt p}. 11548The matrix is filled with {\tt 0}s if the size of {\tt l} is not 11549a multiple of {\tt p}.\\ 11550Input : 11551\begin{center}{\tt list2mat([5,8,1,9,5,6],2)}\end{center} 11552Output : 11553\begin{center}{\tt [[5,8],[1,9],[5,6]]}\end{center} 11554Input : 11555\begin{center}{\tt list2mat([5,8,1,9],3)}\end{center} 11556Output : 11557\begin{center}{\tt [[5,8,1],[9,0,0]]}\end{center} 11558{\bf Remark} \\ 11559{\tt Xcas} displays matrix with {\bf[} and {\bf]} and lists with $[$ and $]$ 11560as delimiters (the vertical bar of the brackets are thicker for matrices). 11561 11562\subsection{Make a list with a matrix : {\tt mat2list}}\index{mat2list} 11563\noindent{\tt mat2list}\index{mat2list} takes as argument a matrix.\\ 11564{\tt mat2list} returns the list of the coefficients of this matrix.\\ 11565Input : 11566\begin{center}{\tt mat2list([[5,8],[1,9]])}\end{center} 11567Output : 11568\begin{center}{\tt [5,8,1,9]}\end{center} 11569 11570\section{Functions for vectors} 11571\subsection{Norms of a vector : {\tt maxnorm l1norm l2norm 11572norm}}\index{norm|textbf} 11573The instructions to compute the different norm of a vector are : 11574\begin{itemize} 11575\item{\tt maxnorm} returns the ${\mathnormal{l}}^\infty$ norm of a 11576vector, 11577defined as the maximum of the absolute values of its 11578coordinates\index{maxnorm|textbf}\label{sec:maxnormv}.\\ 11579Input : 11580\begin{center}{\tt maxnorm([3,-4,2])}\end{center} 11581Output : 11582\begin{center}{\tt 4}\end{center} 11583Indeed : {\tt x=3, y=-4, z=2} and {\tt 4=max(|x|,|y|,|z|)}. 11584\item{\tt l1norm} returns the ${\tt {\mathnormal{l}}^1}$ norm of a 11585vector defined as the sum of the absolute values of its 11586coordinates\index{l1norm}\label{sec:l1normv}.\\ 11587Input : 11588\begin{center}{\tt l1norm([3,-4,2])}\end{center} 11589Output : 11590\begin{center}{\tt 9}\end{center} 11591Indeed : {\tt x=3, y=-4, z=2} and {\tt 9=|x|+|y|+|z|}. 11592\item{\tt norm} or {\tt l2norm} returns the 11593 ${\mathnormal{l}}^2$ norm of a vector defined as the square root 11594of the sum of the squares of its 11595coordinates\index{l2norm}\label{sec:l2normv}.\\ 11596Input : 11597\begin{center}{\tt norm([3,-4,2])}\end{center} 11598Output : 11599\begin{center}{\tt sqrt(29)}\end{center} 11600Indeed : {\tt x=3, y=-4, z=2} and $ 29=|x|^2+|y|^2+|z|^2$. 11601\end{itemize} 11602 11603\subsection{Normalize a vector : {\tt normalize 11604unitV}}\index{normalize|textbf}\index{unitV|textbf} 11605\noindent {\tt normalize} or {\tt unitV} takes as argument a vector.\\ 11606 {\tt normalize} or {\tt unitV} normalizes this vector for the 11607${\mathnormal{l}}^2$ norm 11608(the square root of the sum of the squares of its coordinates).\\ 11609Input : 11610\begin{center}{\tt normalize([3,4,5])}\end{center} 11611Output : 11612\begin{center}{\tt 11613[3/(5*sqrt(2)),4/(5*sqrt(2)),5/(5*sqrt(2))]}\end{center} 11614Indeed : {\tt x=3, y=4, z=5} and $ 50=|x|^2+|y|^2+|z|^2$. 11615 11616\subsection{Term by term sum of two lists : {\tt + 11617.+}}\index{+|textbf} 11618\index{.+|textbf} 11619The infixed operator {\tt +} or {\tt .+} and the prefixed operator 11620 {\tt '+'} returns the term by term sum of two lists.\\ 11621If the two lists do not have the same size, the smaller list is 11622completed with 11623zeros.\\ 11624Note the difference with sequences : if the infixed operator {\tt +} 11625or the 11626prefixed operator {\tt '+'} takes as arguments two sequences, it 11627merges the sequences, hence return the 11628sum of all the terms of the two sequences.\\ 11629Input : 11630\begin{center}{\tt [1,2,3]+[4,3,5]}\end{center} 11631or : 11632\begin{center}{\tt [1,2,3] .+[4,3,5]}\end{center} 11633or : 11634\begin{center}{\tt '+'([1,2,3],[4,3,5])}\end{center} 11635or : 11636\begin{center}{\tt '+'([[1,2,3],[4,3,5]])}\end{center} 11637Output : 11638\begin{center}{\tt [5,5,8]}\end{center} 11639Input : 11640\begin{center}{\tt [1,2,3,4,5,6]+[4,3,5]}\end{center} 11641or : 11642\begin{center}{\tt '+'([1,2,3,4,5,6],[4,3,5])}\end{center} 11643or : 11644\begin{center}{\tt '+'([[1,2,3,4,5,6],[4,3,5]])}\end{center} 11645Output : 11646\begin{center}{\tt [5,5,8,4,5,6]}\end{center} 11647{\bf Warning !}\\ 11648When the operator {\tt +} is prefixed, it should be quoted ({\tt 11649'+'}). 11650 11651\subsection{Term by term difference of two lists : {\tt - 11652.-}}\index{-|textbf} 11653\index{.-|textbf} 11654The infixed operator {\tt -} or {\tt .-} and the prefixed operator 11655{\tt '-'} returns the term by term difference of two lists.\\ 11656If the two lists do not have the same size, the smaller list is 11657completed with 11658zeros.\\ 11659Input : 11660\begin{center}{\tt [1,2,3]-[4,3,5]}\end{center} 11661or : 11662\begin{center}{\tt [1,2,3] .+ [4,3,5]}\end{center} 11663or : 11664\begin{center}{\tt '-'([1,2,3],[4,3,5])}\end{center} 11665or : 11666\begin{center}{\tt '-'([[1,2,3],[4,3,5]])}\end{center} 11667Output : 11668\begin{center}{\tt [-3,-1,-2]}\end{center} 11669{\bf Warning !}\\ 11670When the operator {\tt -} is prefixed, it should be quoted ({\tt 11671'-'}). 11672 11673\subsection{Term by term product of two lists : {\tt 11674.*}}\index{.*|textbf} 11675The infixed operator {\tt .*} returns the term by term product of two 11676lists of 11677the same size.\\ 11678Input : 11679\begin{center}{\tt [1,2,3] .* [4,3,5]}\end{center} 11680Output : 11681\begin{center}{\tt [4,6,15]}\end{center} 11682 11683\subsection{Term by term quotient of two lists : {\tt 11684./}}\index{./|textbf} 11685The infixed operator {\tt ./} returns the term by term quotient of two 11686lists 11687of the same size.\\ 11688Input : 11689\begin{center}{\tt [1,2,3] ./ [4,3,5]}\end{center} 11690Output : 11691\begin{center}{\tt [1/4,2/3,3/5]}\end{center} 11692 11693\subsection{Scalar product : {\tt scalar\_product * dotprod dot dotP 11694scalar\_Product}}\index{dot}\index{dotP}\index{dotprod}\index{scalar\_product}\index{*|textbf}\index{scalarProduct} 11695{\tt dot} or {\tt dotP} or {\tt dotprod} or {\tt scalar\_product} or 11696{\tt scalarProduct} or the infixed operator {\tt *} takes as argument 11697two vectors.\\ 11698{\tt dot} or {\tt dotP} or {\tt dotprod} or {\tt scalar\_product} or 11699{\tt scalarProduct} or {\tt *} returns the scalar product of these 11700two 11701vectors.\\ 11702Input : 11703\begin{center}{\tt dot([1,2,3],[4,3,5])}\end{center} 11704or : 11705\begin{center}{\tt scalar\_product([1,2,3],[4,3,5])}\end{center} 11706or : 11707\begin{center}{\tt [1,2,3]*[4,3,5]}\end{center} 11708or : 11709\begin{center}{\tt '*'([1,2,3],[4,3,5])}\end{center} 11710Output : 11711\begin{center}{\tt 25}\end{center} 11712Indeed {\tt 25=1*4+2*3+3*5}. 11713 11714Note that {\tt *} may be used to find the product of two polynomials 11715represented as list of their coefficients, but to avoid ambiguity, 11716the polynomial lists must be {\tt poly1[...]}. 11717 11718\subsection{Cross product : {\tt cross crossP 11719crossproduct}}\index{cross}\index{crossP}\index{crossproduct} 11720{\tt cross} or {\tt crossP} or {\tt crossproduct} takes as argument 11721two vectors.\\ 11722{\tt cross} or {\tt crossP} or {\tt crossproduct} returns the cross 11723product 11724of these two vectors.\\ 11725Input : 11726\begin{center}{\tt cross([1,2,3],[4,3,2])}\end{center} 11727Output : 11728\begin{center}{\tt [-5,10,-5]}\end{center} 11729Indeed : 11730$-5=2*2-3*3$, $ 10=-1*2+4*3$, $ -5=1*3-2*4$. 11731\section{Statistics functions : {\tt mean,variance,stddev, stddevp,median,quantile,quartiles,boxwhisker}}\index{mean} \index{stddev}\index{variance}\index{median}\index{stddevp}\index{quantile}\index{boxwhisker}\index{quartiles}\label{sec:statlist} 11732The functions described here may be used if the statistics series 11733is contained in a list. See also section \ref{sec:statmat} for matrices. 11734%and chapter \ref{sec:stat} for weighted lists. 11735\begin{itemize} 11736\item{\tt mean} computes the arithmetic mean of a list\\ 11737Input : 11738\begin{center}{\tt mean([3,4,2])}\end{center} 11739Output : 11740\begin{center}{\tt 3}\end{center} 11741Input : 11742\begin{center}{\tt mean([1,0,1])}\end{center} 11743Output 11744\begin{center}{\tt 2/3}\end{center} 11745\item{\tt stddev} computes the standard deviation of a population, 11746if the argument is the population.\\ 11747Input : 11748\begin{center}{\tt stddev([3,4,2])}\end{center} 11749Output : 11750\begin{center}{\tt sqrt(2/3)}\end{center} 11751\item{\tt stddevp} computes an unbiased estimate of 11752the standard deviation of the population, 11753if the argument is a sample. The following 11754relation holds: 11755\begin{center} 11756 {\tt stddevp(l)\verb|^|2=size(l)*stddev(l)\verb|^|2/(size(l)-1)}. 11757\end{center} 11758Input : 11759\begin{center}{\tt stddevp([3,4,2])}\end{center} 11760Output : 11761\begin{center}{\tt 1}\end{center} 11762\item{\tt variance} computes the variance of a list, that is 11763the square of {\tt stddevp}\\ 11764Input : 11765\begin{center}{\tt variance([3,4,2])}\end{center} 11766Output : 11767\begin{center}{\tt 2/3}\end{center} 11768\item{\tt median} computes the median of a list.\\ 11769Input : 11770\begin{center}{\tt median([0,1,3,4,2,5,6])}\end{center} 11771Output : 11772\begin{center}{\tt 3.0}\end{center} 11773\item{\tt quantile} computes the deciles of a list given as first 11774argument, where the decile is the second argument.\\ 11775Input : 11776\begin{center}{\tt quantile([0,1,3,4,2,5,6],0.25)}\end{center} 11777Output the first quartile : 11778\begin{center}{\tt [1.0]}\end{center} 11779Input : 11780\begin{center}{\tt quantile([0,1,3,4,2,5,6],0.5)}\end{center} 11781Output the median : 11782\begin{center}{\tt [3.0]}\end{center} 11783Input : 11784\begin{center}{\tt quantile([0,1,3,4,2,5,6],0.75)}\end{center} 11785Output the third quartile : 11786\begin{center}{\tt [4.0]}\end{center} 11787\item{\tt quartiles} computes the minimum, the first quartile, the 11788median, the third quartile and the maximum of a list.\\ 11789Input : 11790\begin{center}{\tt quartiles([0,1,3,4,2,5,6])}\end{center} 11791Output : 11792\begin{center}{\tt [[0.0],[1.0],[3.0],[4.0],[6.0]]}\end{center} 11793\item{\tt boxwhisker} draws the whisker box of a statistics series 11794stored in a list.\\ 11795Input : 11796\begin{center}{\tt boxwhisker([0,1,3,4,2,5,6])}\end{center} 11797Output 11798\begin{center}{\tt the graph of the whisker box of this statistic list}\end{center} 11799\end{itemize} 11800{\bf Example}\\ 11801Define the list {\tt A} by: 11802\begin{center} 11803{\tt A:=[0,1,2,3,4,5,6,7,8,9,10,11]} 11804\end{center} 11805Outputs : 11806\begin{enumerate} 11807\item {\tt 11/2} for {\tt mean(A)} 11808\item 11809{\tt sqrt(143/12)} for {\tt stddev(A)} 11810\item 11811{\tt 0} for {\tt min(A)} 11812\item 11813{\tt [1.0]} for {\tt quantile(A,0.1)} 11814\item 11815{\tt [2.0]} for {\tt quantile(A,0.25)} 11816\item 11817{\tt [5.0]} for {\tt median(A)} or for {\tt quantile(A,0.5)} 11818\item 11819{\tt [8.0]} for {\tt quantile(A,0.75)} 11820\item 11821{\tt [9.0]} for {\tt quantile(A,0.9)} 11822\item 11823{\tt 11} for {\tt max(A)} 11824\item 11825{\tt [[0.0],[2.0],[5.0],[8.0],[11.0]]} for {\tt quartiles(A)} 11826\end{enumerate} 11827 11828\section{Table with strings as indexes : {\tt table}}\index{table} 11829A table is an associative container (or map), it is used to store information 11830associated to indexes which are much more general than integers, 11831like strings or sequences. It may be used for example to store 11832a table of phone numbers indexed by names.\\ 11833In {\tt Xcas}, the indexes in a table may be any kind of {\tt Xcas} 11834objects. Access is done by a binary search algorithm, where the 11835sorting function first sorts by {\tt type} then uses an order for 11836each type (e.g. $<$ for numeric types, lexicographic order for 11837strings, etc.)\\ 11838{\tt table} takes as argument a list or a sequence of equalities 11839{\tt index\_name=element\_value}.\\ 11840{\tt table} returns this table.\\ 11841Input : 11842\begin{center}{\tt T:=table(3=-10,"a"=10,"b"=20,"c"=30,"d"=40)}\end{center} 11843Input : 11844\begin{center}{\tt T["b"]}\end{center} 11845Output : 11846\begin{center}{\tt 20}\end{center} 11847Input : 11848\begin{center}{\tt T[3]}\end{center} 11849Output : 11850\begin{center}{\tt -10}\end{center} 11851{\bf Remark}\\ 11852If you assign {\tt T[n]:= ...} where {\tt T} is a variable name 11853and {\tt n} an integer 11854\begin{itemize} 11855\item if the variable name was assigned to a list or a sequence, then the 11856$n$-th element of {\tt T} is modified, 11857\item if the variable name was not assigned, a table {\tt T} 11858is created with one entry (corresponding to the index $n$). Note 11859that after the assignation {\tt T} is not a list, despite the fact that $n$ 11860was an integer. 11861\end{itemize} 11862 11863\section{Usual matrix} 11864A matrix is represented by a list of lists, all having the same size. 11865In the {\tt Xcas} answers, the matrix delimiters are {\bf []} (bold brackets). 11866For example, {\bf [}1,2,3{\bf ]} is the matrix [[1,2,3]] with only one row, 11867unlike [1,2,3] (normal brackets) which is the list [1,2,3].\\ 11868In this document, the input notation ([[1,2,3]]) will be used for input 11869and output. 11870 11871\subsection{Identity matrix : {\tt idn identity}}\index{idn}\index{identity} 11872\noindent{\tt idn} takes as argument an integer $n$ or a square matrix.\\ 11873{\tt idn} returns the identity matrix of size $n$ or of the same size 11874as the matrix argument.\\ 11875Input : 11876\begin{center}{\tt idn(2)}\end{center} 11877Output : 11878\begin{center}{\tt [[1,0],[0,1]]}\end{center} 11879Input : 11880\begin{center}{\tt idn(3)}\end{center} 11881Output : 11882\begin{center}{\tt [[1,0,0],[0,1,0],[0,0,1]]}\end{center} 11883 11884\subsection{Zero matrix : {\tt newMat matrix}}\index{newMat} 11885\noindent{\tt newMat(n,p)} or {\tt matrix(n,p)} 11886takes as argument two integers.\\ 11887{\tt newMat(n,p)} returns the zero matrix with {\tt n} rows and 11888{\tt p} columns.\\ 11889Input : 11890\begin{center}{\tt newMat(4,3)}\end{center} 11891Output : 11892\begin{center}{\tt[[0,0,0],[0,0,0],[0,0,0],[0,0,0]]}\end{center} 11893 11894\subsection{Random matrix : {\tt ranm randMat randmatrix}}\index{ranm}\index{randMat}\index{randmatrix}\label{sec:ranm2} 11895\noindent{\tt ranm} or {\tt randMat} or {\tt randmatrix} takes as argument an 11896integer $n$ or two integers $n,m$ and optionally a third argument, either an 11897integer $k$ or the quoted name of a random distribution law 11898(see also \ref{sec:ranm1} and \ref{sec:ranm4}.\\ % and \ref{sec:ranm3}).\\ 11899{\tt ranm} returns a vector of size $n$ or a matrix of size $n\times m$ 11900 containing random integers uniformly distributed between -99 and +99 11901(default), or between 0 and $k-1$ or a matrix of size $n\times m$ 11902containing random integers according to the law put between quotes.\\ 11903Input : 11904\begin{center}{\tt ranm(3)}\end{center} 11905Output : 11906\begin{center}{\tt [-54,78,-29]}\end{center} 11907Input : 11908\begin{center}{\tt ranm(2,4)}\end{center} 11909Output : 11910\begin{center}{\tt [[27,-29,37,-66],[-11,76,65,-33]]}\end{center} 11911Input : 11912\begin{center}{\tt ranm(2,4,3)}\end{center} 11913or : 11914\begin{center}{\tt ranm(2,4,'rand(3)')}\end{center} 11915Output : 11916\begin{center}{\tt [[0,1,1,0],[0,1,2,0]]}\end{center} 11917Input : 11918\begin{center}{\tt ranm(2,4,'randnorm(0,1)')}\end{center} 11919Output : 11920\begin{center}{\tt [[1.83785427742,0.793007112053,-0.978388964902,-1.88602023857], [-1.50900874199,-0.241173369698,0.311373795585,-0.532752431454]]}\end{center} 11921Input : 11922\begin{center}{\tt ranm(2,4,2..4)}\end{center} 11923Output : 11924\begin{center}{\tt [[2.00549363438,3.03381264955,2.06539073586,2.04844321217], 11925 [3.88383254968,3.28664474655,3.76909781061,2.39113253355]]}\end{center} 11926 11927 11928\subsection{Diagonal of a matrix or matrix of a diagonal : {\tt BlockDiagonal diag}}\index{diag}\index{BlockDiagonal} 11929\noindent{\tt diag} or {\tt BlockDiagonal} takes as argument a matrix $A$ or 11930a list $l$.\\ 11931{\tt diag} returns the diagonal of $A$ or the diagonal matrix with the list 11932$l$ on the diagonal (and 0 elsewhere).\\ 11933Input : 11934\begin{center}{\tt diag([[1,2],[3,4]])}\end{center} 11935Output : 11936\begin{center}{\tt [1,4]}\end{center} 11937Input : 11938\begin{center}{\tt diag([1,4])}\end{center} 11939Output : 11940\begin{center}{\tt [[1,0],[0,4]]}\end{center} 11941 11942\subsection{Jordan block : {\tt JordanBlock}}\index{JordanBlock} 11943\noindent {\tt JordanBlock} takes as argument an expression $a$ and an integer 11944$n$.\\ 11945{\tt JordanBlock} returns a square matrix of size $n$ with $a$ 11946on the principal diagonal, 1 above this diagonal and 0 elsewhere.\\ 11947Input : 11948\begin{center}{\tt JordanBlock(7,3)}\end{center} 11949Output : 11950\begin{center}{\tt [[7,1,0],[0,7,1],[0,0,7]]}\end{center} 11951 11952\subsection{Hilbert matrix : {\tt hilbert}}\index{hilbert} 11953\noindent{\tt hilbert} takes as argument an integer $n$.\\ 11954{\tt hilbert} returns the Hilbert matrix.\\ 11955 A Hilbert matrix is a square matrix of size $n$ whose elements 11956$a_{j,k}$ are : 11957\[ a_{j,k}=\frac{1}{j+k+1}, \quad 0\leq j, 0 \leq k \] 11958Input : 11959\begin{center}{\tt hilbert(4)}\end{center} 11960Output : 11961\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} 11962 11963\subsection{Vandermonde matrix : {\tt vandermonde}}\index{vandermonde} 11964\noindent{\tt vandermonde} takes as argument a vector whose components are 11965denoted by $x_j$ for $j=0..n-1$.\\ 11966{\tt vandermonde} returns the corresponding Vandermonde matrix 11967(the $k$-th row of the matrix is the vector whose components are 11968$x_i^{k}$ for $i=0..n-1$ and $k=0..n-1$).\\ 11969{\bf Warning !}\\ 11970The indices of the rows and columns begin at 0 with {\tt Xcas}.\\ 11971Input : 11972\begin{center}{\tt vandermonde([a,2,3])}\end{center} 11973Output (if {\tt a} is symbolic else purge(a)) : 11974\begin{center}{\tt [[1,1,1],[a,2,3],[a*a,4,9]]}\end{center} 11975 11976\section{Arithmetic and matrix} 11977\subsection{Evaluate a matrix : {\tt evalm}}\index{evalm} 11978\noindent {\tt evalm} is used in {\tt Maple} to evaluate a matrix. 11979In {\tt Xcas}, matrices are evaluated by default, the command 11980{\tt evalm} is only available for compatibility, it is equivalent 11981to {\tt eval}. 11982 11983\subsection{Addition and subtraction of two matrices : {\tt + - .+ .-}}\index{+}\index{-}\index{.+}\index{.-} 11984\noindent The infixed operator {\tt +} or {\tt .+} (resp. {\tt -} or {\tt .-}) 11985are used for the addition (resp. subtraction) of two matrices.\\ 11986Input : 11987\begin{center}{\tt [[1,2],[3,4]] + [[5,6],[7,8]]}\end{center} 11988Output : 11989\begin{center}{\tt [[6,8],[10,12]]}\end{center} 11990Input : 11991\begin{center}{\tt [[1,2],[3,4]] - [[5,6],[7,8]]}\end{center} 11992Output : 11993\begin{center}{\tt [[-4,-4],[-4,-4]]}\end{center} 11994{\bf Remark}\\ 11995{\tt +} can be used as a prefixed operator, in that case 11996{\tt +} must be quoted ({\tt '+'}).\\ 11997Input : 11998\begin{center}{\tt '+'([[1,2],[3,4]],[[5,6],[7,8]],[[2,2],[3,3]])}\end{center} 11999Output : 12000\begin{center}{\tt [[8,10],[13,15]]}\end{center} 12001 12002\subsection{Multiplication of two matrices : {\tt * \&*}}\index{*}\index{\&*} 12003\noindent The infixed operator {\tt *} (or {\tt \&*}) is used for the 12004multiplication of two matrices.\\ 12005Input : 12006\begin{center}{\tt [[1,2],[3,4]] * [[5,6],[7,8]]}\end{center} 12007or : 12008\begin{center}{\tt [[1,2],[3,4]] \&* [[5,6],[7,8]]}\end{center} 12009Output : 12010\begin{center}{\tt [[19,22],[43,50]]}\end{center} 12011 12012\subsection{Addition of elements of a column of a matrix : {\tt sum}}\index{sum} 12013\noindent {\tt sum} takes as argument a matrix $A$.\\ 12014{\tt sum} returns the list whose elements are the sum of the elements of each 12015column of the matrix $A$.\\ 12016Input : 12017\begin{center}{\tt sum([[1,2],[3,4]])}\end{center} 12018Output : 12019\begin{center}{\tt [4,6]}\end{center} 12020 12021\subsection{Cumulated sum of elements of each column of a matrix : {\tt cumSum}}\index{cumSum} 12022\noindent {\tt cumSum} takes as argument a matrix $A$.\\ 12023{\tt cumSum} returns the matrix whose columns are the cumulated sum of the 12024elements of the corresponding column of the matrix $A$.\\ 12025Input : 12026\begin{center}{\tt cumSum([[1,2],[3,4],[5,6]])}\end{center} 12027Output : 12028\begin{center}{\tt [[1,2],[4,6],[9,12]]}\end{center} 12029since the cumulated sums are : 1, 1+3=4, 1+3+5=9 and 2, 2+4=6, 2+4+6=12. 12030 12031\subsection{Multiplication of elements of each column of a matrix : {\tt product}}\index{product}\label{sec:product1} 12032\noindent {\tt product} takes as argument a matrix $A$.\\ 12033{\tt product} returns the list whose elements are the product of the elements 12034of each column of the matrix $A$ (see also \ref{sec:product} and 12035\ref{sec:product2}).\\ 12036Input : 12037\begin{center}{\tt product([[1,2],[3,4]])}\end{center} 12038Output : 12039\begin{center}{\tt [3,8]}\end{center} 12040 12041\subsection{Power of a matrix :\ \^\ \ \&\^\ }\index{\^\ |textbf}\index{\&\^\ } 12042The infixed operator {\tt \verb|^|} (or {\tt \&\verb|^|}) is used to 12043raise a matrix to an integral power.\\ 12044Input : 12045\begin{center}{\tt [[1,2],[3,4]] \verb|^| 5}\end{center} 12046or : 12047\begin{center}{\tt [[1,2],[3,4]] \&\verb|^| 5}\end{center} 12048Output : 12049\begin{center}{\tt [[1069,1558],[2337,3406]]}\end{center} 12050 12051\subsection{Hadamard product : {\tt hadamard product}}\index{hadamard}\index{product}\label{sec:product2} 12052\noindent{\tt hadamard} (or {\tt product}) takes as arguments two matrices $A$ 12053and $B$ of the same size.\\ 12054{\tt hadamard} (or {\tt product}) returns the matrix where each term is the 12055term by term product of $A$ and $B$.\\ 12056Input : 12057\begin{center}{\tt hadamard([[1, 2],[3,4]],[[5, 6],[7, 8]])}\end{center} 12058Output : 12059\begin{center}{\tt [[5,12],[21,32]]}\end{center} 12060See also \ref{sec:product} and \ref{sec:product1} for {\tt product}. 12061 12062\subsection{Hadamard product (infixed version): {\tt .*}}\index{.*} 12063\noindent{\tt .*} takes as arguments two matrices or two lists $A$ and $B$ 12064of the same size.\\ 12065{\tt .*} is an infixed operator that returns the matrix or the list 12066where each term is the term by term product of the corresponding 12067terms of $A$ and $B$.\\ 12068Input : 12069\begin{center}{\tt [[1, 2],[3,4]] .* [[5, 6],[7, 8]]}\end{center} 12070Output : 12071\begin{center}{\tt [[5,12],[21,32]]}\end{center} 12072Input : 12073\begin{center}{\tt [1,2,3,4] .* [5,6,7,8]}\end{center} 12074Output : 12075\begin{center}{\tt [5,12,21,32]}\end{center} 12076 12077\subsection{Hadamard division (infixed version): {\tt ./}}\index{./} 12078\noindent{\tt ./} takes as arguments two matrices or two lists $A$ and 12079$B$ of the same size.\\ 12080{\tt ./} is an infixed operator that returns the matrix or the list 12081where each term is the term by term division of the corresponding 12082terms of $A$ and $B$.\\ 12083Input : 12084\begin{center}{\tt [[1, 2],[3,4]] ./ [[5, 6],[7, 8]]}\end{center} 12085Output : 12086\begin{center}{\tt [[1/5,1/3],[3/7,1/2]]}\end{center} 12087 12088\subsection{Hadamard power (infixed version): {\tt .\^\ }}\index{.\^\ } 12089\noindent{\tt .\verb|^|} takes as arguments a matrix or a list 12090$A$ and a real $b$.\\ 12091{\tt .\verb|^|} is an infixed operator that returns the matrix 12092or the list where each term is the corresponding 12093term of $A$ raised to the power $b$.\\ 12094Input : 12095\begin{center}{\tt [[1, 2],[3,4]] .\verb|^| 2}\end{center} 12096Output : 12097\begin{center}{\tt [[1,4],[9,16]]}\end{center} 12098 12099\subsection{Extracting element(s) of a matrix : {\tt [] at}}\index{at} 12100Recall that a matrix is a list of lists with the same size.\\ 12101Input : 12102\begin{center}{\tt A:=[[3,4,5],[1,2,6]]}\end{center} 12103Output : 12104\begin{center}{\tt [[3,4,5],[1,2,6]]}\end{center} 12105The prefixed function {\tt at} or the 12106index notation {\tt [...]} is used to access 12107to an element or a row or a column of a matrix: 12108\begin{itemize} 12109\item To extract an element, put the matrix and then, between square 12110brackets put its row index, a comma, and its column index. 12111In {\tt Xcas} mode the first index is 0, in other modes the first 12112index is 1.\\ 12113Input : 12114\begin{center}{\tt [[3,4,5],[1,2,6]][0,1]}\end{center} 12115or : 12116\begin{center}{\tt A[0,1]}\end{center} 12117or : 12118\begin{center}{\tt A[0][1]}\end{center} 12119or : 12120\begin{center}{\tt at(A,[0,1])}\end{center} 12121Output : 12122\begin{center}{\tt 4}\end{center} 12123 12124\item To extract a row of the matrix {\tt A}, 12125put the matrix and then, between 12126square brackets put the row index, input : 12127\begin{center}{\tt [[3,4,5],[1,2,6]][0]}\end{center} 12128or : 12129\begin{center}{\tt A[0]}\end{center} 12130or : 12131\begin{center}{\tt at(A,0)}\end{center} 12132Output : 12133\begin{center}{\tt [3,4,2]}\end{center} 12134 12135\item To extract a part of a row, put two arguments 12136between the square brackets : 12137the row index and an interval to designate the selected columns.\\ 12138Input : 12139\begin{center}{\tt A[1,0..2]}\end{center} 12140Output : 12141\begin{center}{\tt [1,2,6]}\end{center} 12142Input : 12143\begin{center}{\tt A[1,1..2]}\end{center} 12144Output : 12145\begin{center}{\tt [2,6]}\end{center} 12146 12147\item To extract a column of the matrix {\tt A}, first transpose 12148{\tt A} ({\tt transpose(A)}) then extract the row like above.\\ 12149Input : 12150\begin{center}{\tt tran(A)[1]}\end{center} 12151or : 12152\begin{center}{\tt at(tran(A),1)}\end{center} 12153Output : 12154\begin{center}{\tt [4,2]}\end{center} 12155 12156\item To extract a part of a column of the matrix {\tt A} 12157as a list, put two arguments 12158between the square brackets : an index interval to 12159designate the selected rows and the column index.\\ 12160Input : 12161\begin{center}{\tt A[0..0,1]}\end{center} 12162Output : 12163\begin{center}{\tt [4]}\end{center} 12164 12165This may be used to extract a full column, by specifying all the rows 12166as an index interval.\\ 12167Input : 12168\begin{center}{\tt A[0..1,1]}\end{center} 12169Output : 12170\begin{center}{\tt [4,2]}\end{center} 12171 12172\item 12173To extract a sub-matrix of a matrix, put between the square brackets two 12174intervals : one interval for the selected rows and one interval for the 12175selected columns.\\ 12176To define the matrix {\tt A}, input : 12177\begin{center}{\tt A:=[[3,4,5],[1,2,6]]}\end{center} 12178Input : 12179\begin{center}{\tt A[0..1,1..2]}\end{center} 12180Output : 12181\begin{center}{\tt [[4,5],[2,6]]}\end{center} 12182Input : 12183\begin{center}{\tt A[0..1,1..1]}\end{center} 12184Output : 12185\begin{center}{\tt [[4],[2]]}\end{center} 12186{\bf Remark} 12187If the second interval is omitted, the sub-matrix is made with the consecutive 12188rows given by the first interval.\\ 12189Input : 12190\begin{center}{\tt A[1..1]}\end{center} 12191Output : 12192\begin{center}{\tt [[1,2,6]]}\end{center} 12193\end{itemize} 12194 12195You may also assign an element of a matrix using index notation, 12196if you assign with {\tt :=} a new copy of the matrix is created 12197and the element is modified, if you assign with {\tt =<}, 12198the matrix is modified in place. 12199 12200\subsection{Modify an element or a row of a matrix : {\tt subsop}}\index{subsop|textbf} 12201\noindent {\tt subsop} modifies an element or a row of a matrix. 12202It is used mainly for {\tt Maple} and {\tt MuPAD} compatibility. 12203Unlike {\tt :=} or {\tt =<}, 12204it does not require the matrix to be stored in a variable.\\ 12205{\tt subsop} takes two or three arguments, 12206{\bf these arguments are permuted} in {\tt Maple} mode. 12207\begin{enumerate} 12208\item Modify an element 12209\begin{itemize} 12210\item In {\tt Xcas} mode, the first index is 0\\ 12211{\tt subsop} has two (resp. three) arguments: a matrix {\tt A} and an 12212equality {\tt [r,c]=v} (resp. a matrix {\tt A}, a list of indexes {\tt [r,c]}, 12213a value {\tt v}).\\ 12214{\tt subsop} replaces the element {\tt A[r,c]} by {\tt v}.\\ 12215Input in {\tt Xcas} mode : 12216\begin{center}{\tt subsop([[4,5],[2,6]],[1,0]=3)}\end{center} 12217or : 12218\begin{center}{\tt subsop([[4,5],[2,6]],[1,0],3)}\end{center} 12219Output : 12220\begin{center}{\tt [[4,5],[3,6]]}\end{center} 12221{\bf Remark}\\ 12222If the matrix is stored in a variable, for example 12223{\tt A:=[[4,5],[2,6]]}, it is easier to input {\tt A[1,0]:=3} 12224which modifies {\tt A} into the matrix\\ {\tt [[4,5],[3,6]]}. 12225 12226\item In {\tt Mupad, TI} mode, the first index is 1\\ 12227{\tt subsop} has two (resp. three) arguments: a matrix {\tt A} and an 12228equality {\tt [r,c]=v} (resp. a matrix {\tt A}, a list of index {\tt [r,c]}, 12229a value {\tt v}).\\ 12230{\tt subsop} replaces the element {\tt A[r,c]} by {\tt v}.\\ 12231Input in {\tt Mupad, TI} mode : 12232\begin{center}{\tt subsop([[4,5],[2,6]],[2,1]=3)}\end{center} 12233or : 12234\begin{center}{\tt subsop([[4,5],[2,6]],[2,1],3)}\end{center} 12235Output : 12236\begin{center}{\tt [[4,5],[3,6]]}\end{center} 12237{\bf Remark}\\ 12238If the matrix is stored in a variable, for example 12239{\tt A:=[[4,5],[2,6]]}, it is easier to input {\tt A[2,1]:=3} which 12240modifies {\tt A} into the matrix \\{\tt [[4,5],[3,6]]}. 12241 12242\item In {\tt Maple} mode, 12243the arguments are permuted and the first index is 1\\ 12244{\tt subsop} has two arguments: an equality {\tt [r,c]=v} and a matrix 12245{\tt A}.\\ 12246{\tt subsop} replaces the element {\tt A[r,c]} by {\tt v}.\\ 12247Input in {\tt Maple} mode 12248\begin{center}{\tt subsop([2,1]=3,[[4,5],[2,6]])}\end{center} 12249Output : 12250\begin{center}{\tt [[4,5],[3,6]]}\end{center} 12251{\bf Remark}\\ 12252If the matrix is stored in a variable, for example 12253{\tt A:=[[4,5],[2,6]]}, it is easier to input {\tt A[2,1]:=3} which 12254modifies {\tt A} into the matrix\\ {\tt [[4,5],[3,6]]}. 12255\end{itemize} 12256 12257\item Modify a row 12258\begin{itemize} 12259\item in {\tt Xcas} mode, the first index is 0\\ 12260{\tt subsop} takes two arguments : a matrix and an 12261equality (the index of the row to be modified, the {\tt =} sign and the new 12262row value).\\ 12263Input in {\tt Xcas} mode : 12264\begin{center}{\tt subsop([[4,5],[2,6]],1=[3,3])}\end{center} 12265Output : 12266\begin{center}{\tt [[4,5],[3,3]]}\end{center} 12267{\bf Remark}\\ 12268If the matrix is stored in a variable, for example 12269{\tt A:=[[4,5],[2,6]]}, is is easier to input {\tt A[1]:=[3,3]} 12270which modifies {\tt A} into the matrix\\ {\tt [[4,5],[3,3]]}. 12271 12272\item In {\tt Mupad, TI} mode, the first index is 1 \\ 12273{\tt subsop} takes two arguments : a matrix and an 12274equality (the index of the row to be modified, the {\tt =} sign and the new 12275row value).\\ 12276Input in {\tt Mupad, TI} mode : 12277\begin{center}{\tt subsop([[4,5],[2,6]],2=[3,3])}\end{center} 12278Output : 12279\begin{center}{\tt [[4,5],[3,3]]}\end{center} 12280{\bf Remark}\\ 12281If the matrix is stored in a variable, for example 12282{\tt A:=[[4,5],[2,6]]}, it is easier to input {\tt A[2]:=[3,3]} which modifies 12283{\tt A} into the matrix\\ {\tt [[4,5],[3,3]]}. 12284 12285\item in {\tt Maple} mode, the arguments are permuted and the first index 12286is 1 :\\ 12287{\tt subsop} takes two arguments : an 12288equality (the index of the row to be modified, the {\tt =} sign and the new 12289row value) and a matrix.\\ 12290Input in {\tt Maple} mode : 12291\begin{center}{\tt subsop(2=[3,3],[[4,5],[2,6]])}\end{center} 12292Output : 12293\begin{center}{\tt [[4,5],[3,3]]}\end{center} 12294{\bf Remark}\\ 12295If the matrix is stored in a variable, for example 12296{\tt A:=[[4,5],[2,6]]}, it is easier to input {\tt A[2]:=[3,3]} which modifies 12297{\tt A} into the matrix\\ {\tt [[4,5],[3,3]]}. 12298\end{itemize} 12299\end{enumerate} 12300{\bf Remark}\\ 12301Note also that {\tt subsop} with a {\tt 'n=NULL'} argument 12302deletes row number {\tt n}. 12303In {\tt Xcas} mode input : 12304\begin{center}{\tt subsop([[4,5],[2,6]],'1=NULL')}\end{center} 12305Output : 12306\begin{center}{\tt [[4,5]]}\end{center} 12307 12308\subsection{Extract rows or columns of a matrix (Maple compatibility) : {\tt row col}}\index{row}\index{col} 12309\noindent{\tt row} (resp. {\tt col}) extracts one or several rows (resp. columns) 12310of a matrix.\\ 12311{\tt row} (resp. {\tt col}) takes 2 arguments : a matrix $A$, and an integer $n$ 12312or an interval $n_1..n_2$.\\ 12313{\tt row} (resp. {\tt col}) returns the row (resp. column) of index $n$ of $A$, 12314or the sequence of rows (resp. columns) of index from $n_1$ to $n_2$ of $A$.\\ 12315Input : 12316\begin{center}{\tt row([[1,2,3],[4,5,6],[7,8,9]],1)}\end{center} 12317Output : 12318\begin{center}{\tt [4,5,6]}\end{center} 12319Input : 12320\begin{center}{\tt row([[1,2,3],[4,5,6],[7,8,9]],0..1)}\end{center} 12321Output : 12322\begin{center}{\tt ([1,2,3],[4,5,6])}\end{center} 12323Input : 12324\begin{center}{\tt col([[1,2,3],[4,5,6],[7,8,9]],1)}\end{center} 12325Output : 12326\begin{center}{\tt [2,5,8]}\end{center} 12327Input : 12328\begin{center}{\tt col([[1,2,3],[4,5,6],[7,8,9]],0..1)}\end{center} 12329Output : 12330\begin{center}{\tt ([1,4,7,[2,5,8])}\end{center} 12331 12332\subsection{Remove rows or columns of a matrix : {\tt delrows delcols}}\index{delrows}\index{delcols} 12333\noindent{\tt delrows} (resp. {\tt delcols}) removes one or several rows (resp. 12334columns) of a matrix.\\ 12335{\tt delrows} (resp. {\tt delcols}) takes 2 arguments : a matrix $A$, and 12336an interval $n_1..n_2$.\\ 12337{\tt delrows} (resp. {\tt delcols}) returns the matrix where the rows 12338(resp. columns) of index from $n_1$ to $n_2$ of $A$ are removed.\\ 12339Input : 12340\begin{center}{\tt delrows([[1,2,3],[4,5,6],[7,8,9]],1..1)}\end{center} 12341Output : 12342\begin{center}{\tt [[1,2,3],[7,8,9]]}\end{center} 12343Input : 12344\begin{center}{\tt delrows([[1,2,3],[4,5,6],[7,8,9]],0..1)}\end{center} 12345Output : 12346\begin{center}{\tt [[7,8,9]]}\end{center} 12347Input : 12348\begin{center}{\tt delcols([[1,2,3],[4,5,6],[7,8,9]],1..1)}\end{center} 12349Output : 12350\begin{center}{\tt [[1,3],[4,6],[7,9]]}\end{center} 12351Input : 12352\begin{center}{\tt delcols([[1,2,3],[4,5,6],[7,8,9]],0..1)}\end{center} 12353Output : 12354\begin{center}{\tt [[3],[6],[9]]}\end{center} 12355 12356\subsection{Extract a sub-matrix of a matrix (TI compatibility) : {\tt subMat}}\index{subMat} 12357\noindent{\tt subMat} takes 5 arguments : a matrix $A$, and 4 integers 12358$nl1,nc1,nl2,nc2$, where 12359$nl1$ is the index of the first row, $nc1$ is 12360the index of the first column, $nl2$ is the index of 12361the last row and $nc2$ is the index of the last column.\\ 12362{\tt subMat(A,nl1,nc1,nl2,nc2)} extracts the sub-matrix of the matrix {\tt A} 12363with first element {\tt A[nl1,nc1]} and last element 12364{\tt A[nl2,nc2]}.\\ 12365Define the matrix {\tt A} : 12366\begin{center}{\tt A:=[[3,4,5],[1,2,6]]}\end{center} 12367Input : 12368\begin{center}{\tt subMat(A,0,1,1,2)}\end{center} 12369Output : 12370\begin{center}{\tt [[4,5],[2,6]]}\end{center} 12371Input : 12372\begin{center}{\tt subMat(A,0,1,1,1]}\end{center} 12373Output : 12374\begin{center}{\tt [[4],[2]]}\end{center} 12375By default $nl1=0$, $nc1=0$, $nl2$={\tt nrows(A)}-1 and 12376$nc2$={\tt ncols(A)}-1\\ 12377Input : 12378\begin{center}{\tt subMat(A,1)}\end{center} 12379or : 12380\begin{center}{\tt subMat(A,1,0)}\end{center} 12381or : 12382\begin{center}{\tt subMat(A,1,0,1)}\end{center} 12383or : 12384\begin{center}{\tt subMat(A,1,0,1,2)}\end{center} 12385Output : 12386\begin{center}{\tt [[1,2,6]]}\end{center} 12387 12388\subsection{Add a row to another row : {\tt rowAdd}}\index{rowAdd} 12389\noindent{\tt rowAdd} takes three arguments : a matrix $A$ and two integers 12390$n1$ and $n2$.\\ 12391{\tt rowAdd} returns the matrix obtained by replacing in $A$, the row of index 12392$n2$ by the sum of the rows of index $n1$ and $n2$.\\ 12393Input : 12394\begin{center}{\tt rowAdd([[1,2],[3,4]],0,1)}\end{center} 12395Output : 12396\begin{center}{\tt [[1,2],[4,6]]}\end{center} 12397 12398\subsection{Multiply a row by an expression : {\tt mRow}}\index{mRow} 12399\noindent{\tt mRow} takes three arguments : an expression, a matrix $A$ and an 12400integer $n$.\\ 12401{\tt mRow} returns the matrix obtained by replacing in $A$, the row of index 12402$n$ by the product of the row of index $n$ by the expression.\\ 12403Input : 12404\begin{center}{\tt mRow(12,[[1,2],[3,4]],1)}\end{center} 12405Output : 12406\begin{center}{\tt [[1,2],[36,48]]}\end{center} 12407 12408\subsection{Add $k$ times a row to an another row : {\tt mRowAdd}}\index{mRowAdd} 12409\noindent{\tt mRowAdd} takes four arguments : a real $k$, a matrix $A$ and two 12410integers $n1$ and $n2$.\\ 12411{\tt mRowAdd} returns the matrix obtained by replacing in $A$, the 12412row of index $n2$ by the sum of the row of index $n2$ and $k$ times the row of 12413index $n1$.\\ 12414Input : 12415\begin{center}{\tt mRowAdd(1.1,[[5,7],[3,4],[1,2]],1,2)}\end{center} 12416Output : 12417\begin{center}{\tt [[5,7],[3,4],[4.3,6.4]]}\end{center} 12418 12419\subsection{Exchange two rows : {\tt rowSwap}}\index{rowSwap} 12420\noindent{\tt rowSwap} takes three arguments : a matrix $A$ and two integers 12421$n1$ and $n2$.\\ 12422{\tt rowSwap} returns the matrix obtained by exchanging in $A$, the row of 12423index $n1$ with the row of index $n2$.\\ 12424Input : 12425\begin{center}{\tt rowSwap([[1,2],[3,4]],0,1)}\end{center} 12426Output : 12427\begin{center}{\tt [[3,4],[1,2]]}\end{center} 12428 12429\subsection{Make a matrix with a list of matrices : {\tt blockmatrix}}\index{blockmatrix} 12430\noindent{\tt blockmatrix} takes as arguments two integers $n,m$ and a 12431list of size $n*m$ of matrices of the same dimension $p \times q$ 12432(or more generally such that the $m$ first matrices 12433have the same number of rows and $c$ columns, the 12434$m$ next rows have the same number of rows and $c$ columns, and so on ...). 12435In both cases, we have $n$ blocks of $c$ columns.\\ 12436{\tt blockmatrix} returns a matrix having $c$ columns 12437by putting these $n$ blocks one under another (vertical gluing). 12438If the matrix arguments 12439have the same dimension $p \times q$, the answer is a matrix of 12440dimension $p*n \times q*m$.\\ 12441Input : 12442\begin{center}{\tt blockmatrix(2,3,[idn(2),idn(2),idn(2), idn(2),idn(2),idn(2)])}\end{center} 12443Output : 12444\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} 12445Input : 12446\begin{center}{\tt blockmatrix(3,2,[idn(2),idn(2), idn(2),idn(2),idn(2),idn(2)])}\end{center} 12447Output : 12448\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} 12449Input : 12450\begin{center}{\tt blockmatrix(2,2,[idn(2),newMat(2,3), newMat(3,2),idn(3)])}\end{center} 12451Output : 12452\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} 12453Input : 12454\begin{center}{\tt blockmatrix(3,2,[idn(1),newMat(1,4), newMat(2,3),idn(2),newMat(1,2),[[1,1,1]]])}\end{center} 12455Output : 12456\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} 12457Input : 12458\begin{center}{\tt A:=[[1,1],[1,1]];B:=[[1],[1]]}\end{center} 12459then : 12460\begin{center}{\tt blockmatrix(2,3,[2*A,3*A,4*A,5*B,newMat(2,4),6*B])}\end{center} 12461Output : 12462\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} 12463 12464\subsection{Make a matrix from two matrices : {\tt semi\_augment}}\index{semi\_augment|textbf} 12465\noindent {\tt semi\_augment} concat two matrices with the same number 12466of columns.\\ 12467Input : 12468\begin{center}{\tt semi\_augment([[3,4],[2,1],[0,1]],[[1,2],[4,5]])}\end{center} 12469Output : 12470\begin{center}{\tt [[3,4],[2,1],[0,1],[1,2],[4,5]]}\end{center} 12471Input : 12472\begin{center}{\tt semi\_augment([[3,4,2]],[[1,2,4]])}\end{center} 12473Output : 12474\begin{center}{\tt [[3,4,2],[1,2,4]]}\end{center} 12475Note the difference with {\tt concat}.\\ 12476Input : 12477\begin{center}{\tt concat([[3,4,2]],[[1,2,4]]}\end{center} 12478Output : 12479\begin{center}{\tt [[3,4,2,1,2,4]]}\end{center} 12480Indeed, when the two matrices $A$ and $B$ have the same dimension, {\tt concat} 12481makes a matrix with the same number of rows as $A$ and $B$ by 12482gluing them side by side.\\ 12483Input : 12484\begin{center}{\tt concat([[3,4],[2,1],[0,1]],[[1,2],[4,5]]}\end{center} 12485Output : 12486\begin{center}{\tt [[3,4],[2,1],[0,1],[1,2],[4,5]]}\end{center} 12487but input : 12488\begin{center}{\tt concat([[3,4],[2,1]],[[1,2],[4,5]]}\end{center} 12489Output : 12490\begin{center}{\tt [[3,4,1,2],[2,1,4,5]]}\end{center} 12491 12492\subsection{Make a matrix from two matrices : {\tt augment concat}}\index{augment}\index{concat} 12493\noindent {\tt augment} or {\tt concat} concats two matrices $A$ and $B$ 12494having the same number of rows, or having the same number of columns. 12495In the first case, it returns a matrix having the same number of rows 12496as $A$ and $B$ by horizontal gluing, in the second case 12497it returns a matrix having the same number of columns by 12498vertical gluing.\\ 12499Input : 12500\begin{center}{\tt augment([[3,4,5],[2,1,0]],[[1,2],[4,5]])}\end{center} 12501Output : 12502\begin{center}{\tt [[3,4,5,1,2],[2,1,0,4,5]]}\end{center} 12503Input : 12504\begin{center}{\tt augment([[3,4],[2,1],[0,1]],[[1,2],[4,5]])}\end{center} 12505Output : 12506\begin{center}{\tt [[3,4],[2,1],[0,1],[1,2],[4,5]]}\end{center} 12507Input : 12508\begin{center}{\tt augment([[3,4,2]],[[1,2,4]]}\end{center} 12509Output : 12510\begin{center}{\tt [[3,4,2,1,2,4]]}\end{center} 12511Note that if $A$ and $B$ have the same dimension, {\tt augment} 12512makes a matrix with the same number of rows as $A$ and $B$ 12513by horizontal gluing, in that case 12514you must use {\tt semi\_augment} for vertical gluing.\\ 12515Input : 12516\begin{center}{\tt augment([[3,4],[2,1]],[[1,2],[4,5]])}\end{center} 12517Output : 12518\begin{center}{\tt [[3,4,1,2],[2,1,4,5]]]}\end{center} 12519 12520\subsection{Build a matrix with a function : {\tt makemat}}\index{makemat} 12521\noindent{\tt makemat} takes three arguments : 12522\begin{itemize} 12523\item a function of two variables {\tt j} and {\tt k} which 12524should return the value of $a_{j,k}$, the element of 12525row index {\tt j} and column index {\tt k} of the matrix to be built. 12526\item two integers $n$ and $p$. 12527\end{itemize} 12528{\tt makemat} returns the matrix $A=(a_{j,k})$ 12529($j=0..n-1$ and $k=0..p-1$) of dimension $n \times p$.\\ 12530Input : 12531\begin{center}{\tt makemat((j,k)->j+k,4,3)}\end{center} 12532or first define the $h$ function: 12533\begin{center}{\tt h(j,k):=j+k}\end{center} 12534then, input: 12535\begin{center}{\tt makemat(h,4,3)}\end{center} 12536Output : 12537\begin{center}{\tt [[0,1,2],[1,2,3],[2,3,4],[3,4,5]]}\end{center} 12538Note that the indices are counted starting from 0. 12539 12540\subsection{Define a matrix : {\tt matrix}}\index{matrix} 12541\noindent{\tt matrix} takes three arguments : 12542\begin{itemize} 12543\item two integers $n$ and $p$. 12544\item a function of two variables {\tt j} and {\tt k} which 12545should return the value of $a_{j,k}$, the element of 12546row index {\tt j} and column index {\tt k} of the matrix to be build. 12547\end{itemize} 12548{\tt matrix} returns the matrix $A=(a_{j,k})$ ($j=1..n$ and $k=1..p$) of 12549dimension $n \times p$.\\ 12550Input : 12551\begin{center}{\tt matrix(4,3,(j,k)->j+k)}\end{center} 12552or first define the $h$ function: 12553\begin{center} {\tt h(j,k):=j+k}\end{center} 12554then, input: 12555\begin{center}{\tt matrix(4,3,h)}\end{center} 12556Output : 12557\begin{center}{\tt [[2,3,4],[3,4,5],[4,5,6],[5,6,7]]}\end{center} 12558Note the argument order and the fact that the indices are counted 12559starting from 1. If the last argument is not provided, it defaults to 0. 12560 12561\subsection{Append a column to a matrix : {\tt border}}\index{border} 12562\noindent{\tt border} takes as argument a matrix {\tt A} of dimension $p*q$ 12563 and a list {\tt b} of size $p$ (i.e. {\tt nrows(A)=size(b)}).\\ 12564{\tt border} returns the matrix obtained by appending 12565{\tt tran(b)} as last column to the matrix {\tt A}, therefore: 12566\begin{center} 12567{\tt border(A,b)=tran([op(tran(A)),b])=tran(append(tran(A),b))} 12568\end{center} 12569Input : 12570\begin{center}{\tt border([[1,2,4],[3,4,5]],[6,7])}\end{center} 12571Output : 12572\begin{center}{\tt [[1,2,4,6],[3,4,5,7]]}\end{center} 12573Input : 12574\begin{center}{\tt border([[1,2,3,4],[4,5,6,8],[7,8,9,10]],[1,3,5])}\end{center} 12575Output : 12576\begin{center}{\tt [[1,2,3,4,1],[4,5,6,8,3],[7,8,9,10,5]]}\end{center} 12577 12578\subsection{Count the elements of a matrix verifying a property : {\tt count}}\index{count} 12579\noindent{\tt count} takes as arguments : a real function {\tt f} and 12580a real matrix {\tt A} of dimension {\tt p*q} (resp. a list {\tt l} of size 12581{\tt n}).\\ 12582{\tt count} returns {\tt f(A[0,0])+..f(A[p-1,q-1])} (resp. 12583{\tt f(l[0])+..f(l[n-1])})\\ 12584Hence, if {\tt f} is a boolean function, {\tt count} returns the number 12585of elements of the matrix {\tt A} (resp. the list {\tt l}) verifying 12586the property {\tt f}.\\ 12587Input : 12588\begin{center}{\tt count(x->x,[[2,12],[45,3],[7,78]])}\end{center} 12589Output : 12590\begin{center}{\tt 147}\end{center} 12591indeed: 2+12+45+3+7+78=147.\\ 12592Input : 12593\begin{center}{\tt count(x->x<10,[[2,12],[45,3],[7,78]])}\end{center} 12594Output : 12595\begin{center}{\tt 3}\end{center} 12596 12597\subsection{Count the elements equal to a given value : {\tt count\_eq}}\index{count\_eq} 12598\noindent{\tt count\_eq} takes as arguments: a real and a real list or 12599a real matrix.\\ 12600{\tt count\_eq} returns the number of elements of the list or matrix 12601equal to the first argument.\\ 12602Input : 12603\begin{center}{\tt count\_eq(12,[[2,12,45],[3,7,78]])}\end{center} 12604Output : 12605\begin{center}{\tt 1}\end{center} 12606 12607\subsection{Count the elements smaller than a given value : {\tt count\_inf}}\index{count\_inf} 12608\noindent{\tt count\_inf} takes as arguments: a real and a real list or a real 12609matrix.\\ 12610{\tt count\_inf} returns the number of elements of the list or 12611matrix which are strictly less than the first argument.\\ 12612Input : 12613\begin{center}{\tt count\_inf(12,[2,12,45,3,7,78])}\end{center} 12614Output : 12615\begin{center}{\tt 3}\end{center} 12616 12617\subsection{Count the elements greater than a given value : {\tt count\_sup}}\index{count\_sup} 12618\noindent{\tt count\_sup} takes as arguments: a real and a real list or a real 12619 matrix.\\ 12620{\tt count\_sup} returns the number of elements of the list or matrix 12621which are strictly greater to the first argument.\\ 12622Input : 12623\begin{center}{\tt count\_sup(12,[[2,12,45],[3,7,78]])}\end{center} 12624Output : 12625\begin{center}{\tt 2}\end{center} 12626 12627\subsection{Statistics functions acting on column matrices : {\tt mean}, {\tt stddev}, {\tt variance}, {\tt median}, {\tt quantile}, {\tt quartiles}, {\tt boxwhisker}}\label{sec:statmat} 12628\index{mean} \index{stddev}\index{variance}\index{median}\index{quartiles}\index{quantile}\index{boxwhisker} 12629The following functions work on matrices, acting column by column: 12630\begin{itemize} 12631\item{\tt mean} computes the arithmetic means of the statistical series 12632stored in the columns of a matrix.\\ 12633Input : 12634\begin{center}{\tt mean([[3,4,2],[1,2,6]])}\end{center} 12635Output is the vector of the means of each column : 12636\begin{center}{\tt [2,3,4]}\end{center} 12637Input : 12638\begin{center}{\tt mean([[1,0,0],[0,1,0],[0,0,1]])}\end{center} 12639Output 12640\begin{center}{\tt [1/3,1/3,1/3]}\end{center} 12641 12642\item{\tt stddev} computes the standard deviations of the population 12643statistical series stored in the columns of a matrix.\\ 12644Input : 12645\begin{center}{\tt stddev([[3,4,2],[1,2,6]])}\end{center} 12646Output is the vector of the standard deviations of each column : 12647\begin{center}{\tt [1,1,2]}\end{center} 12648\item{\tt variance} computes the variances of the statistical series 12649stored in the columns of a matrix.\\ 12650Input : 12651\begin{center}{\tt variance([[3,4,2],[1,2,6]])}\end{center} 12652Output is the vector of the variance of each column : 12653\begin{center}{\tt [1,1,4]}\end{center} 12654 12655\item{\tt median} computes the medians of the statistical series 12656stored in the columns of a matrix.\\ 12657Input : 12658\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} 12659Output is the vector of the median of each column : 12660\begin{center}{\tt [3,3,4,4,4,3,4]}\end{center} 12661 12662\item{\tt quantile} computes the deciles as specified by the second 12663argument of the statistical series stored in the columns of a matrix.\\ 12664Input :\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} 12665Output is the vector of the first quartile of each column : 12666\begin{center}{\tt [1,1,2,2,1,1,1]}\end{center} 12667Input : 12668\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} 12669Output is the vector of the third quartile of each column : 12670\begin{center}{\tt [3,3,4,4,4,3,4]}\end{center} 12671 12672\item{\tt quartiles} computes the minima, the first quartiles, the 12673medians, the third quartiles and the maxima of the statistical series 12674stored in the columns of a matrix.\\ 12675Input : 12676\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} 12677Output is a matrix, its first row is the minima of each column, 12678its second row is the fist quartiles of each column, 12679its third row the medians 12680of each column, its fourth row the third 12681quartiles of each column and its last row the maxima of each column: 12682\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} 12683\begin{center}{\tt [3,3,4,4,4,3,4],[6,5,6,6,6,6,6]]}\end{center} 12684 12685\item{\tt boxwhisker} draws the whisker boxes of the statistical series 12686stored in the columns of a matrix .\\ 12687Input : 12688\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} 12689Output : 12690\begin{center}{\tt the drawing of the whisker boxes of the statistical 12691series of each column of the matrix argument}\end{center} 12692\end{itemize} 12693 12694\subsection{Dimension of a matrix : {\tt dim}}\index{dim} 12695\noindent{\tt dim} takes as argument a matrix $A$.\\ 12696{\tt dim} returns the list of the number of rows and columns 12697of the matrix $A$.\\ 12698Input : 12699\begin{center}{\tt dim([[1,2,3],[3,4,5]])}\end{center} 12700Output : 12701\begin{center}{\tt [2,3]}\end{center} 12702 12703\subsection{Number of rows : {\tt rowdim rowDim nrows}}\index{rowdim}\index{rowDim}\index{nrows} 12704\noindent{\tt rowdim} (or {\tt rowDim} or {\tt nrows}) takes as argument a 12705matrix $A$.\\ 12706{\tt rowdim} (or {\tt rowDim} or {\tt nrows}) returns the number of rows of the 12707matrix $A$.\\ 12708Input : 12709\begin{center}{\tt rowdim([[1,2,3],[3,4,5]])}\end{center} 12710or : 12711\begin{center}{\tt nrows([[1,2,3],[3,4,5]])}\end{center} 12712Output : 12713\begin{center}{\tt 2}\end{center} 12714 12715\subsection{Number of columns : {\tt coldim colDim ncols}}\index{coldim}\index{colDim}\index{ncols} 12716\noindent{\tt coldim} (or {\tt colDim} or {\tt ncols}) takes as argument a 12717matrix $A$.\\ 12718{\tt coldim} (or {\tt colDim} or {\tt ncols}) returns the number of columns of 12719the matrix $A$.\\ 12720Input : 12721\begin{center}{\tt coldim([[1,2,3],[3,4,5]])}\end{center} 12722or : 12723\begin{center}{\tt ncols([[1,2,3],[3,4,5]])}\end{center} 12724Output : 12725\begin{center}{\tt 3}\end{center} 12726 12727 12728\section{Linear algebra } 12729\subsection{Transpose of a matrix : {\tt tran transpose}}\index{tran}\index{transpose} 12730\noindent{\tt tran} or {\tt transpose} takes as argument a matrix $A$.\\ 12731{\tt tran} or {\tt transpose} returns the transpose matrix of $A$.\\ 12732Input : 12733\begin{center}{\tt tran([[1,2],[3,4]])}\end{center} 12734Output : 12735\begin{center}{\tt [[1,3],[2,4]]}\end{center} 12736 12737\subsection{Inverse of a matrix : {\tt inv /}}\index{inv|textbf}\index{/|textbf} 12738\noindent{\tt inv} takes as argument a square matrix $A$.\\ 12739{\tt inv} returns the inverse matrix of $A$.\\ 12740Input : 12741\begin{center}{\tt inv([[1,2],[3,4]])}\end{center} 12742or : 12743\begin{center}{\tt 1/[[1,2],[3,4]])}\end{center} 12744or : 12745\begin{center}{\tt A:=[[1,2],[3,4]];1/A}\end{center} 12746Output : 12747\begin{center}{\tt [[-2,1],[3/2,1/-2]]}\end{center} 12748 12749\subsection{Trace of a matrix : {\tt trace}}\index{trace} 12750\noindent{\tt trace} takes as argument a matrix $A$.\\ 12751{\tt trace} returns the trace of the matrix $A$, that is 12752the sum of the diagonal elements.\\ 12753Input : 12754\begin{center}{\tt trace([[1,2],[3,4]])}\end{center} 12755Output : 12756\begin{center}{\tt 5}\end{center} 12757 12758\subsection{Determinant of a matrix : {\tt det}}\index{det|textbf} 12759\noindent{\tt det} takes as argument a matrix $A$.\\ 12760{\tt det} returns the determinant of the matrix $A$.\\ 12761Input : 12762\begin{center}{\tt det([[1,2],[3,4]])}\end{center} 12763Output : 12764\begin{center}{\tt -2}\end{center} 12765Input : 12766\begin{center}{\tt det(idn(3))}\end{center} 12767Output : 12768\begin{center}{\tt 1}\end{center} 12769 12770\subsection{Determinant of a sparse matrix : {\tt det\_minor}}\index{det\_minor} 12771\noindent{\tt det\_minor} takes as argument a matrix $A$.\\ 12772{\tt det\_minor} returns the determinant of the matrix $A$ computed by 12773expanding the determinant using Laplace's algorithm.\\ 12774Input : 12775\begin{center}{\tt det\_minor([[1,2],[3,4]])}\end{center} 12776Output : 12777\begin{center}{\tt -2}\end{center} 12778Input : 12779\begin{center}{\tt det\_minor(idn(3))}\end{center} 12780Output : 12781\begin{center}{\tt 1}\end{center} 12782 12783\subsection{Rank of a matrix : {\tt rank}}\index{rank} 12784\noindent{\tt rank} takes as argument a matrix $A$.\\ 12785{\tt rank} returns the rank of the matrix $A$.\\ 12786Input : 12787\begin{center}{\tt rank([[1,2],[3,4]])}\end{center} 12788Output : 12789\begin{center}{\tt 2}\end{center} 12790Input : 12791\begin{center}{\tt rank([[1,2],[2,4]])}\end{center} 12792Output : 12793\begin{center}{\tt 1}\end{center} 12794 12795\subsection{Transconjugate of a matrix : {\tt trn}}\index{trn} 12796\noindent{\tt trn} takes as argument a matrix $A$.\\ 12797{\tt trn} returns the transconjugate of $A$ (i.e. the conjugate of the 12798transpose matrix of $A$).\\ 12799Input : 12800\begin{center}{\tt trn([[i, 1+i],[1, 1-i]])}\end{center} 12801Output after simplification: 12802\begin{center}{\tt [[-i,1],[1-i,1+i]]}\end{center} 12803 12804 12805\subsection{Equivalent matrix : {\tt changebase}}\index{changebase} 12806\noindent{\tt changebase} takes as argument a matrix $A$ and a 12807change-of-basis matrix $P$.\\ 12808{\tt changebase} returns the matrix $B$ such that $B=P^{-1}AP$.\\ 12809Input : 12810\begin{center}{\tt changebase([[1,2],[3,4]],[[1,0],[0,1]])}\end{center} 12811Output : 12812\begin{center}{\tt [[1,2],[3,4]]}\end{center} 12813Input : 12814\begin{center}{\tt changebase([[1,1],[0,1]],[[1,2],[3,4]])}\end{center} 12815Output : 12816\begin{center}{\tt [[-5,-8],[9/2,7]]}\end{center} 12817Indeed : 12818 $${\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]$$. 12819 12820\subsection{Basis of a linear subspace : {\tt basis}}\index{basis} 12821\noindent{\tt basis} takes as argument a list of vectors generating 12822a linear subspace of $\mathbb R^n$.\\ 12823{\tt basis} returns a list of vectors, that is a basis of this 12824linear subspace.\\ 12825Input : 12826\begin{center}{\tt basis([[1,2,3],[1,1,1],[2,3,4]])}\end{center} 12827Output : 12828\begin{center}{\tt [[1,0,-1],\ [0,1,2]]}\end{center} 12829 12830\subsection{Basis of the intersection of two subspaces : {\tt ibasis}}\index{ibasis} 12831\noindent{\tt ibasis} takes as argument two lists of vectors generating 12832two subspaces of $\mathbb R^n$.\\ 12833{\tt ibasis} returns a list of vectors, that is a basis of the 12834intersection of these two subspaces.\\ 12835Input : 12836\begin{center}{\tt ibasis([[1,2]],[[2,4]])}\end{center} 12837Output : 12838\begin{center}{\tt [[1,2]]}\end{center} 12839 12840\subsection{Image of a linear function : {\tt image}}\index{image} 12841\noindent{\tt image} takes as argument the matrix of a linear 12842function $f$ with respect to the canonical basis.\\ 12843{\tt image} returns a list of vectors that is a basis of the image 12844of $f$.\\ 12845Input : 12846\begin{center}{\tt image([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 12847Output : 12848\begin{center}{\tt [[-1,0,1],[0,-1,-2]]}\end{center} 12849 12850\subsection{Kernel of a linear function : {\tt kernel nullspace ker}}\index{ker}\index{kernel}\index{nullspace} 12851\noindent{\tt ker} (or {\tt kernel} or {\tt nullspace}) takes as argument the 12852matrix of an linear function $f$ with respect to the canonical basis.\\ 12853{\tt ker} (or {\tt kernel} or {\tt nullspace}) returns a list of 12854vectors that is a basis of the kernel of $f$.\\ 12855Input : 12856\begin{center}{\tt ker([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 12857Output : 12858\begin{center}{\tt [[1,1,-1]]}\end{center} 12859The kernel is generated by the vector {\tt [1,1,-1]}. 12860 12861\subsection{Kernel of a linear function : {\tt Nullspace}}\index{Nullspace} 12862\noindent {\bf Warning} This function is useful in Maple mode only 12863(hit the state line red button then {\tt Prog style}, 12864then choose Maple and Apply).\\ 12865{\tt Nullspace} is the inert form of {\tt nullspace}.\\ 12866{\tt Nullspace} takes as argument an integer matrix of a linear 12867function $f$ with respect to the canonical basis.\\ 12868{\tt Nullspace}) followed by {\tt mod p} returns a list of vectors 12869that is a basis of the kernel of $f$ computed in $\mathbb Z/p\mathbb Z[X]$.\\ 12870Input : 12871\begin{center}{\tt Nullspace([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 12872Output : 12873\begin{center}{\tt nullspace([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 12874Input (in Maple mode): 12875\begin{center}{\tt Nullspace([[1,2],[3,1]]) mod 5}\end{center} 12876Output : 12877\begin{center}{\tt [2,-1]}\end{center} 12878In Xcas mode, the equivalent input is : 12879\begin{center}{\tt nullspace([[1,2],[3,1]] \% 5)}\end{center} 12880Output : 12881\begin{center}{\tt [2\% 5,-1]}\end{center} 12882 12883\subsection{Subspace generated by the columns of a matrix : {\tt colspace}}\index{colspace} 12884\noindent{\tt colspace} takes as argument the matrix $A$ of a linear 12885function $f$ with respect to the canonical basis.\\ 12886{\tt colspace} returns a matrix. The columns of this matrix are a basis of the 12887subspace generated by the columns of $A$.\\ 12888{\tt colspace} may have a variable name as second argument, where 12889{\tt Xcas} 12890will store the dimension of the subspace generated by the columns of $A$.\\ 12891Input : 12892\begin{center}{\tt colspace([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 12893Output : 12894\begin{center}{\tt [[-1,0],[0,-1],[1,-2]]}\end{center} 12895Input : 12896\begin{center}{\tt colspace([[1,1,2],[2,1,3],[3,1,4]],dimension)}\end{center} 12897Output : 12898\begin{center}{\tt [[-1,0],[0,-1],[1,-2]]}\end{center} 12899Then input: 12900\begin{center}{\tt dimension}\end{center} 12901Output : 12902\begin{center}{\tt 2}\end{center} 12903 12904\subsection{Subspace generated by the rows of a matrix : {\tt rowspace}}\index{rowspace} 12905\noindent{\tt rowspace} takes as argument the matrix $A$ of a linear 12906function $f$ with respect to the canonical basis.\\ 12907{\tt rowspace} returns a list of vectors that is a basis of the 12908subspace generated by the rows of $A$.\\ 12909{\tt rowspace} may have a variable name as second argument where {\tt Xcas} 12910will store the dimension of the subspace generated by the rows of $A$.\\ 12911Input : 12912\begin{center}{\tt rowspace([[1,1,2],[2,1,3],[3,1,4]])}\end{center} 12913Output : 12914\begin{center}{\tt [[-1,0,-1],[0,-1,-1]]}\end{center} 12915Input : 12916\begin{center}{\tt rowspace([[1,1,2],[2,1,3],[3,1,4]],dimension)}\end{center} 12917Output : 12918\begin{center}{\tt [[-1,0,-1],[0,-1,-1]]}\end{center} 12919Then input: 12920\begin{center}{\tt dimension}\end{center} 12921Output : 12922\begin{center}{\tt 2}\end{center} 12923 12924\section{Linear Programmation}\index{simplex\_reduce|textbf} 12925Linear programming problems are maximization problem of a linear 12926functionals under linear equality or inequality constraints. 12927The most simple case can be solved directly by the so-called simplex 12928algorithm. Most cases require to solve an auxiliary linear 12929programming problem to find an initial vertex for the simplex 12930algorithm. 12931 12932\subsection{Simplex algorithm: {\tt simplex\_reduce}} 12933{\bf The simple case}\\ 12934The function {\tt simplex\_reduce} makes the reduction 12935by the simplex algorithm to find : 12936\[ \mbox{max}(c.x), \quad A.x \leq b,\ x \geq 0,\ b\geq 0 \] 12937where $c,x$ are vectors of $\mathbb R^n$, $b\geq 0$ is a vector in 12938$\mathbb R^p$ and $A$ is a matrix of $p$ rows and $n$ columns.\\ 12939{\tt simplex\_reduce} takes as argument {\tt A,b,c} and 12940returns {\tt max(c.x)}, the augmented solution of {\tt x} 12941(augmented since the algorithm works by adding rows($A$) auxiliary 12942variables) and the reduced matrix.\\ 12943{\bf Example}\\ 12944Find \[ \mbox{max}(X+2Y) \mbox{ where } 12945\left\{ 12946\begin{array}{rcl} 12947(X,Y) & \geq & 0 \\ 12948-3X +2Y & \leq & 3\\ 12949X +Y & \leq & 4 12950\end{array} 12951\right. 12952\] 12953Input : 12954\begin{center}{\tt simplex\_reduce([[-3,2],[1,1]],[3,4],[1,2])}\end{center} 12955Output : 12956\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} 12957Which means that the maximum of {\tt X+2Y} under these conditions 12958is {\tt 7}, it is obtained for {\tt X=1,Y=3} 12959because {\tt [1,3,0,0]} is the augmented solution and the reduced matrix is :\\ 12960{\tt [[0,1,1/5,3/5,3],[1,0,(-1)/5,2/5,1], [0,0,1/5,8/5,7]]}. 12961 12962{\bf A more complicated case that reduces to the simple case}\\ 12963With the former call of {\tt simplex\_reduce}, we have to : 12964\begin{itemize} 12965\item rewrite constraints to the form $x_k \geq 0$, 12966\item remove variables without constraints, 12967\item add variables such that all the constraints have positive components. 12968\end{itemize} 12969For example, find : 12970\begin{equation}\label{eq:lpexample} 12971\mbox{min}(2x+y-z+4) \quad \mbox{ where } 12972\left\{ 12973\begin{array}{rcl} 12974x & \leq & 1 \\ 12975y & \geq & 2 \\ 12976x+3y-z & = & 2 \\ 129772x-y+z & \leq & 8\\ 12978-x+y & \leq & 5 12979\end{array} 12980\right. 12981\end{equation} 12982Let $x=1-X$, $y=Y+2$, $z=5-X+3Y$ 12983the problem is equivalent to finding the minimum of 12984$(-2X+Y-(5-X+3Y)+8)$ 12985where : 12986\[ 12987\left\{ 12988\begin{array}{rcl} 12989X & \geq & 0 \\ 12990Y & \geq & 0 \\ 129912(1-X)-(Y+2)+ 5-X+3Y & \leq & 8\\ 12992-(1-X) +(Y+2) & \leq & 5 12993\end{array} 12994\right. 12995\] 12996or to find the minimum of~: 12997\[ (-X-2Y+3) \quad \mbox{ where } 12998\left\{ 12999\begin{array}{rcl} 13000X & \geq & 0 \\ 13001Y & \geq & 0 \\ 13002-3X+2Y & \leq & 3\\ 13003X +Y & \leq & 4 13004\end{array} 13005\right. 13006\] 13007i.e. to find the maximum of $-(-X-2Y+3)=X+2Y-3$ 13008under the same conditions, hence it is the same problem as 13009to find the maximum of $X+2Y$ seen before. We found {\tt 7}, 13010hence, the result here is {\tt 7-3=4}. 13011 13012{\bf The general case}\\ 13013A linear programming problem may not in general be directly 13014reduced like above to the simple case. The reason is that 13015a starting vertex must be found before applying the simplex 13016algorithm. Therefore, 13017{\tt simplex\_reduce} may be called by specifying this starting 13018vertex, in that case, all the arguments including the starting 13019vertex are grouped in a single matrix. 13020 13021We first illustrate this kind 13022of call in the simple case where the starting point does not 13023require solving an auxiliary problem. 13024If {\tt A} has $p$ rows and $n$ columns and if we define : 13025\begin{center} 13026{\tt B:=augment(A,idn(p));} {\tt C:=border(B,b);} \\ 13027{\tt d:=append(-c,0\$(p+1));} {\tt D:=augment(C,[d]);} 13028\end{center} 13029{\tt simplex\_reduce} may be called with {\tt D} as single argument.\\ 13030For the previous example, input : 13031\begin{center}{\tt A:=[[-3,2],[1,1]];B:=augment(A,idn(2)); C:=border(B,[3,4]); 13032D:=augment(C,[[-1,-2,0,0,0]])}\end{center} 13033Here 13034{\tt C=[[-3,2,1,0,3],[1,1,0,1,4]]}\\ 13035and {\tt D=[[-3,2,1,0,3],[1,1,0,1,4],[-1,-2,0,0,0]]}\\ 13036Input : 13037\begin{center}{\tt simplex\_reduce(D)}\end{center} 13038Output is the same result as before. 13039 13040{\bf Back to the general case.}\\ 13041The standard form of a linear programming problem is similar 13042to the simplest case above, but with $Ax=b$ (instead of $Ax\leq b$) 13043under the conditions $x\geq 0$. We may further assume that $b\geq 0$ 13044(if not, one can change the sign of the corresponding line). 13045\begin{itemize} 13046\item The first problem is to find an $x$ in the $Ax=b, x\geq 0$ domain. 13047Let $m$ be the number of lines of $A$. Add artificial variables 13048$y_1,...,y_m$ and maximize 13049$-\sum y_i$ under the conditions $Ax=b, x \geq 0, y \geq 0$ 13050starting with initial value $0$ for $x$ variables 13051and $y=b$ 13052(to solve this with {\tt Xcas}, call \verb|simplex_reduce| with 13053a single matrix argument obtained by augmenting $A$ by the 13054identity, $b$ unchanged and an artificial 13055$c$ with 0 under $A$ and 1 under the identity). 13056If the maximum exists and is 0, the identity submatrix above the last 13057column corresponds to an $x$ solution, we may forget the artificial 13058variables (they are 0 if the maximum is 0). 13059\item Now we make a second call to \verb|simplex_reduce| 13060with the original $c$ and the value of $x$ we found in the domain. 13061\item 13062Example~: find the minimum of $2x+3y-z+t$ with 13063$x,y,z,t\geq 0$ and~: 13064\[ \left\{ \begin{array}{rcl} 13065-x-y+t&=&1\\ 13066y-z+t&=&3 13067\end{array} 13068\right. \] 13069This is equivalent to find the opposite of the maximum of $-(2x+3y-z+t)$. 13070Let us add two artificial variables $y_1$ and $y_2$, 13071\begin{verbatim} 13072simplex_reduce([[-1,-1,0,1,1,0,1], 13073[0,1,-1,1,0,1,3], 13074[0,0,0,0,1,1,0]]) 13075\end{verbatim} 13076Output: optimum=0, artificial variables=0, and the matrix 13077\[ 13078\left(\begin{array}{ccccccc} 13079-1/2 & 0 & -1/2 & 1 & 1/2 & 1/2 & 2 \\ 130801/2 & 1 & -1/2 & 0 & -1/2 & 1/2 & 1 \\ 130810 & 0 & 0 & 0 & 1 & 1 & 0 13082\end{array}\right) 13083\] 13084Columns 2 and 4 are the columns of the identity (in lines 1 and 2). 13085Hence $x=(0,1,0,2)$ is an initial point in the domain. 13086We are reduced to solve the initial problem, after replacing the 13087lines of $Ax=b$ by the two first lines of the answer above, 13088removing the last columns corresponding to the artificial variables. 13089We add $c.x$ as last line 13090\begin{verbatim} 13091simplex_reduce([[-1/2,0,-1/2,1,2], 13092[1/2,1,-1/2,0,1],[2,3,-1,1,0]]) 13093\end{verbatim} 13094Output: maximum=-5, hence the minimum of the opposite is 5, 13095obtained for $(0,1,0,2)$, after replacement 13096$x=0$, $y=1$, $z=0$ and $t=2$. 13097\end{itemize} 13098 13099For more details, search google for \verb|simplex algorithm|. 13100 13101\subsection{Solving general linear programming problems: {\tt lpsolve}}\index{lpsolve|textbf} 13102Linear 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. 13103 13104{\tt lpsolve} accepts four arguments : 13105\begin{enumerate} 13106\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) 13107\item {\tt constr} (optional) : list of linear constraints which may be equalities or inequalities or bounded expressions entered as {\tt expr=a..b} 13108\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 13109\item {\tt opts} (optional) : sequence of solver settings in form {\tt option=value}, where {\tt option} may be one of : 13110\begin{description} 13111 \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} 13112 \item [\tt lp\_integervariables] -- list of identifiers or indices (of integer variables), default : \emph{empty} 13113 \item [\tt lp\_binaryvariables] -- list of identifiers or indices (of binary variables), default : \emph{empty} 13114 \item [\tt lp\_maximize] (or {\tt maximize}) -- \emph{true} or \emph{false} (objective direction), default : \emph{false} 13115 \item [\tt lp\_method] -- one of {\tt exact}, {\tt float}, {\tt lp\_simplex} or {\tt lp\_interiorpoint}\\(solver type), default {\tt lp\_simplex} 13116 \item [\tt lp\_depthlimit] -- positive integer (max.~depth of branch\&bound tree), default : \emph{unlimited} 13117 \item [\tt lp\_nodelimit] -- positive integer (max.~nodes in branch\&bound tree), default : \emph{unlimited} 13118 \item [\tt lp\_iterationlimit] -- positive integer (max.~iterations of simplex algorithm), default : \emph{unlimited} 13119 \item [\tt lp\_timelimit] -- positive number (max.~solving time in milliseconds), default : \emph{unlimited} 13120 \item[\tt lp\_maxcuts] -- nonnegative integer (max.~GMI cuts per node), default : 5 13121 \item [\tt lp\_gaptolerance] -- positive number (relative integrality gap threshold), default : 0 13122 \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} 13123 \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} 13124 \item [\tt lp\_verbose] -- \emph{true} or \emph{false}, default : \emph{false} 13125\end{description} 13126\end{enumerate} 13127 13128The 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. 13129 13130The 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. 13131 13132\subsubsection{Solving LP problems} 13133By default, {\tt lpsolve} uses primal simplex method implementation to solve LP problems. For example, to solve the problem specified in~\eqref{eq:lpexample}, input : 13134\begin{center} 13135{\tt constr:=[x<=1,y>=2,x+3y-z=2,3x-y+z<=8,-x+y<=5];}\\ 13136{\tt lpsolve(2x+y-z+4,constr)} 13137\end{center} 13138Output : 13139\begin{center} 13140\tt [-4,[x=0,y=5,z=13]] 13141\end{center} 13142Therefore, 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) $. 13143 13144Constraints may also take the form {\tt expr=a..b} for bounded linear expressions. 13145 13146\noindent Input : 13147\begin{center} 13148{\tt lpsolve(x+2y+3z,[x+y=1..5,y+z+1=2..4,x>=0,y>=0])} 13149\end{center} 13150Output : 13151\begin{center} 13152{\tt [-2,[x=0,y=5,z=-4]]} 13153\end{center} 13154 13155Use the {\tt assume=lp\_nonnegative} option to specify that all variables are nonnegative. It is easier than entering the nonnegativity constraints explicitly. 13156 13157\noindent Input: 13158\begin{center} 13159{\tt lpsolve(-x-y,[y<=3x+1/2,y<=-5x+2],}\\ 13160{\tt assume=lp\_nonnegative)} 13161\end{center} 13162Output: 13163\begin{center} 13164{\tt [-5/4,[x=3/16,y=17/16]]} 13165\end{center} 13166 13167Bounds can be added separately for some variables. They should be entered after constraints. 13168 13169\noindent Input : 13170\begin{center} 13171{\tt constr:=[5x-10y<=20,2z-3y=6,-x+3y<=3];}\\ 13172{\tt lpsolve(-6x+4y+z,constr,x=1..20,y=0..inf)} 13173\end{center} 13174Output : 13175\begin{center} 13176{\tt [-133/2,[x=18,y=7,z=27/2]]} 13177\end{center} 13178 13179Number 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. 13180 13181\subsubsection{Entering problems in matrix form} 13182{\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} $. 13183 13184The 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}. 13185 13186\noindent Input : 13187\begin{center} 13188 {\tt c:=[-2,1];A:=[[-1,1],[1,1],[-1,0],[0,-1]];}\\ 13189 {\tt b:=[3,5,0,0];lpsolve(c,[A,b])} 13190\end{center} 13191Output : 13192\begin{center} 13193 {\tt [-10,[5,0]]} 13194\end{center} 13195Input : 13196\begin{center} 13197 {\tt c:=[-2,5,-3];bl:=[2,3,1];bu:=[6,10,7/2];}\\ 13198 {\tt lpsolve(c,[],[bl,bu])} 13199\end{center} 13200Output : 13201\begin{center} 13202 {\tt [-15/2,[6,3,7/2]]} 13203\end{center} 13204Input : 13205\begin{center} 13206 {\tt c:=[4,5];Aeq:=[[-1,3/2],[-3,2]];beq:=[2,3];}\\ 13207 {\tt lpsolve(c,[[],[],Aeq,beq])} 13208\end{center} 13209Output : 13210\begin{center} 13211 {\tt [26/5,[-1/5,6/5]]} 13212\end{center} 13213 13214\subsubsection{Solving MIP (Mixed Integer Programming) problems} 13215{\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. 13216 13217To solve pure integer programming problems, in which all variables are integers, use option {\tt assume=integer} or {\tt assume=lp\_integer}. 13218 13219\noindent Input : 13220\begin{center} 13221{\tt lpsolve(-5x-7y,[7x+y<=35,-x+3y<=6],assume=integer)} 13222\end{center} 13223Output : 13224\begin{center} 13225{\tt [-41,[x=4,y=3]]} 13226\end{center} 13227 13228Use 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. 13229 13230\noindent Input : 13231\begin{center} 13232{\tt lpsolve(8x1+11x2+6x3+4x4,[5x1+7x2+4x3+3x4<=14],}\\ 13233{\tt assume=lp\_binary,maximize)} 13234\end{center} 13235Output : 13236\begin{center} 13237{\tt [21,[x1=0,x2=1,x3=1,x4=1]]} 13238\end{center} 13239 13240To 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. 13241 13242\noindent Input : 13243\begin{center} 13244{\tt lpsolve(x+3y+3z,[x+3y+2z<=7,2x+2y+z<=11],}\\ 13245{\tt assume=lp\_nonnegative,lp\_maximize,} 13246{\tt lp\_integervariables=[x,z])} 13247\end{center} 13248Output : 13249\begin{center} 13250 \tt [10,[x=1,y=0,z=3]] 13251\end{center} 13252 13253Use the {\tt assume=lp\_nonnegint} or {\tt assume=nonnegint} option to get nonnegative integer values. 13254 13255\noindent Input : 13256\begin{center} 13257{\tt lpsolve(2x+5y,[3x-y=1,x-y<=5],assume=nonnegint)} 13258\end{center} 13259Output : 13260\begin{center} 13261{\tt [12,[x=1,y=2]]} 13262\end{center} 13263 13264When 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. 13265 13266\noindent Input : 13267\begin{center} 13268 {\tt c:=[2,-3,-5];A:=[[-5,4,-5],[2,5,7],[2,-3,4]];}\\ 13269 {\tt b:=[3,1,-2];lpsolve(c,[A,b],lp\_integervariables=[0,2])} 13270\end{center} 13271Output : 13272\begin{center} 13273 {\tt [19,[1,3/4,-1]]} 13274\end{center} 13275One 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. 13276 13277\paragraph{Implementation details.} 13278Branch\&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. 13279 13280\paragraph{Stopping criteria.} 13281There 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. 13282 13283If 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. 13284 13285\paragraph{Branching strategies.} 13286At 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. 13287 13288Node selection strategy can be set by using the {\tt lp\_nodeselect} option. Possible values are : 13289\begin{description} 13290 \item[\tt lp\_breadthfirst] -- choose the active node which provides the best bound for the objective value, 13291 \item[\tt lp\_depthfirst] -- choose the deepest active node and break ties by selecting the node providing the best bound, 13292 \item[\tt lp\_hybrid] -- combine the above two strategies, 13293 \item[\tt lp\_bestprojection] -- choose the node with best simple projection. 13294\end{description} 13295By 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. 13296 13297Variable selection strategy can be set by using the {\tt lp\_varselect} option. Possible values are : 13298\begin{description} 13299 \item[\tt lp\_firstfractional] -- choose the first fractional variable, 13300 \item[\tt lp\_lastfractional] -- choose the last fractional variable, 13301 \item[\tt lp\_mostfractional] -- choose the variable with fractional part closest to 0.5, 13302 \item[\tt lp\_pseudocost] -- choose the variable which had the greatest impact on the objective value in previous branchings. 13303\end{description} 13304By 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. 13305 13306Using 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 : 13307\begin{center} 13308 Minimize $ \mathbf{c}^T\,\mathbf{x} $ subject to $ \mathbf{A}\,\mathbf{x}=\mathbf{b} $, where $ \mathbf{x}\in\mathbb{Z}_+^8 $ and 13309\end{center} 13310\[ \mathbf{A}=\begin{bmatrix} 1331122 & 13 & 26 & 33 & 21 & 3 & 14 & 26 \\ 1331239 & 16 & 22 & 28 & 26 & 30 & 23 & 24 \\ 1331318 & 14 & 29 & 27 & 30 & 38 & 26 & 26 \\ 1331441 & 26 & 28 & 36 & 18 & 38 & 16 & 26 13315\end{bmatrix},\quad\mathbf{b}=\begin{bmatrix} 7872 \\ 10466 \\ 11322 \\ 12058 \end{bmatrix},\quad\mathbf{c}=\begin{bmatrix} 133162 \\10 \\13 \\17 \\7 \\5 \\7 \\3 13317\end{bmatrix}. \] 13318When 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. 13319 13320\paragraph{Cutting planes.} 13321Strong 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. 13322 13323\paragraph{Displaying detailed output.} 13324By 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 13325\begin{verbatim} 13326<n>: <m> nodes active, lower bound: <lb>[, integrality gap: <g>] 13327\end{verbatim} 13328is 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. 13329 13330In 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. 13331 13332\noindent Input : 13333\begin{center} 13334 \tt lpsolve(x1+x2,[1867x1+1913x2=3618894],\\ 13335 assume=nonnegint,lp\_verbose=true) 13336\end{center} 13337Output : 13338\begin{verbatim} 13339Optimizing... 13340Applying branch&bound method to find integer feasible solutions... 13341 3937: Incumbent solution found 13342Summary: 13343 * 3938 subproblem(s) examined 13344 * max. tree size: 1 nodes 13345 * 0 Gomory cut(s) applied 13346\end{verbatim} 13347\begin{center} 13348 \tt [1916,[x1=1009,x2=907]] 13349\end{center} 13350 13351\subsubsection{Solving problems in floating-point arithmetic} 13352{\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. 13353 13354By 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). 13355 13356Setting {\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). 13357 13358Specifying {\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. 13359 13360Setting {\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. 13361 13362For example, try to solve the following LP problem using the default settings. 13363\begin{center} 13364 Minimize $ 1.06\,x_1+0.56\,x_2+3.0\,x_3 $ 13365\end{center} 13366subject to 13367\begin{align*} 133681.06\,x1+0.015\,x3 &\geq 729824.87\\ 133690.56\,x_2+0.649\,x_3 &\geq 1522188.03\\ 13370x_3 &\geq 1680.05\\ 13371x_k &\geq 0\quad\text{for}\ k=1,2,3 13372\end{align*} 13373\noindent Input : 13374\begin{center} 13375 {\tt lpsolve(1.06x1+0.56x2+3x3,[1.06x1+0.015x3>=729824.87,}\\ 13376 {\tt 0.56x2+0.649x3>=1522188.03,x3>=1680.05],}\\ 13377 {\tt assume=lp\_nonnegative)} 13378\end{center} 13379Output : 13380\begin{center} 13381\tt [2255937.4968,[x1=688490.254009,x2=2716245.85277,x3=1680.05]] 13382\end{center} 13383If {\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 : 13384\begin{center} 13385 \tt [2255940.66,[x1=688491.0,x2=2716245.0,x3=1681.0]] 13386\end{center} 13387The 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}~: 13388\begin{center} 13389 \tt [2255937.50731,[x1=688490.256652,x2=2716245.85608,\\ 13390 x3=1680.05195065]] 13391\end{center} 13392 13393\subsubsection{Loading problem from a file} 13394Linear (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. 13395 13396For example, assume that {\tt somefile.lp} file is stored in directory {\tt /path/to/file} contains the following lines of text : 13397\begin{verbatim} 13398Maximize 13399obj: x1 + 2 x2 + 3 x3 + x4 13400Subject To 13401c1: - x1 + x2 + x3 + 10 x4 <= 20 13402c2: x1 - 3 x2 + x3 <= 30 13403c3: x2 - 3.5 x4 = 0 13404Bounds 134050 <= x1 <= 40 134062 <= x4 <= 3 13407End 13408\end{verbatim} 13409To find an optimal solution to linear program specified in the file, one just needs to input : 13410\begin{center} 13411 \tt lpsolve("/path/to/file/somefile.lp") 13412\end{center} 13413Output : 13414\begin{verbatim} 13415Reading problem data from '/path/to/file/somefile.lp'... 134163 rows, 4 columns, 9 non-zeros 1341710 lines were read 13418\end{verbatim} 13419\begin{center} 13420 \tt [116,[x1=38,x2=9,x3=19,x4=3]] 13421\end{center} 13422 13423Additional 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. 13424 13425\noindent Input : 13426\begin{center} 13427 \tt lpsolve("/path/to/file/somefile.lp",x2=1..8,x3=-10..10,\\ 13428 lp\_integervariables=[x4]) 13429\end{center} 13430Output : 13431\begin{center} 13432 \tt [82,[x1=38,x2=6,x3=10,x4=2]] 13433\end{center} 13434 13435It 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. 13436 13437\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.}. 13438 13439\subsection{Nonlinear optimization: {\tt nlpsolve}}\index{nlpsolve} 13440{\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: 13441\begin{itemize} 13442 \item {\tt obj} : objective expression 13443 \item {\tt constr} : list of equality and inequality constraints (optional) 13444 \item {\tt bd} : sequence of variable boundaries (optional) : {\tt x=a..b}, {\tt y=c..d}, \ldots 13445 \item {\tt opt} : sequence of options (optional), which may be one of: 13446 \begin{itemize} 13447 \item {\tt maximize=true} or {\tt false} (or just {\tt maximize}) 13448 \item {\tt nlp\_initialpoint=[x=x0,y=y0,...]} 13449 \item {\tt nlp\_iterationlimit=n} 13450 \item {\tt assume=nlp\_nonnegative} 13451 \item {\tt nlp\_precision=eps} 13452 \end{itemize} 13453\end{itemize} 13454 13455{\tt nlpsolve} returns a list containing the optimal value of the objective and a vector of optimal values of the decision variables. 13456 13457The objective is minimized by default, unless {\tt maximize} or {\tt maximize=true} is specified as an option. 13458 13459Initial 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. 13460 13461Input syntax for {\tt nlpsolve} resembles that of Maple's {\tt NLPSolve} (entering the objective as a function (univariate case) is not supported, however). 13462 13463\subsubsection{Examples} 13464 13465Input : 13466\begin{center} 13467 \tt nlpsolve(ln(1+x1\^{}2)-x2,[(1+x1\^{}2)\^{}2+x2\^{}2=4]) 13468\end{center} 13469Output : 13470\begin{center} 13471 \tt [-1.73205080757,[x1=-4.77142305945e-08,x2=1.73205080757]] 13472\end{center} 13473Input : 13474\begin{center} 13475 \tt nlpsolve(-x1*x2*x3,[72-x1-2x2-2x3>=0], 13476 x1=0..20,x2=0..11,x3=0..42) 13477\end{center} 13478Output : 13479\begin{center} 13480 \tt [-3300.0,[x1=20.0,x2=11.0,x3=15.0]] 13481\end{center} 13482Input : 13483\begin{center} 13484 \tt nlpsolve(x\^{}3+2x*y-2y\^{}2,x=-10..10,y=-10..10, 13485 nlp\_initialpoint=[x=3,y=4],maximize) 13486\end{center} 13487Output : 13488\begin{center} 13489 \tt [1050.0,[x=10.0,y=4.99999985519]] 13490\end{center} 13491Input : 13492\begin{center} 13493 \tt nlpsolve(sin(x)/x,x=1..30) 13494\end{center} 13495Output : 13496\begin{center} 13497 \tt [-0.217233628211,[x=4.49340942383]] 13498\end{center} 13499Input : 13500\begin{center} 13501 \tt nlpsolve(2-1/120*x1*x2*x3*x4*x5, 13502 [x1<=1,x2<=2,x3<=3,x4<=4,x5<=5],assume=nlp\_nonnegative) 13503\end{center} 13504Output : 13505\begin{center} 13506 \tt [1.0,[x1=1.0,x2=2.0,x3=3.0,x4=4.0,x5=5.0]] 13507\end{center} 13508 13509\subsection{Solving transportation problems: {\tt tpsolve}}\index{tpsolve|textbf} 13510The 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 : 13511\begin{itemize} 13512\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 $, 13513\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 $, 13514\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 $. 13515\end{itemize} 13516The 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 $. 13517 13518Function {\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}^* $. 13519 13520\noindent Input : 13521\begin{center} 13522{\tt s:=[12,17,11];d:=[10,10,10,10];}\\ 13523{\tt C:=[[50,75,30,45],[65,80,40,60],[40,70,50,55]];}\\ 13524{\tt tpsolve(s,d,C)} 13525\end{center} 13526Output : 13527\begin{center} 13528{\tt 2020,[[0,0,2,10],[0,9,8,0],[10,1,0,0]]} 13529\end{center} 13530 13531If 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. 13532 13533\noindent Input : 13534\begin{center} 13535{\tt s:=[7,10,8,8,9,6];d:=[9,6,12,8,10];}\\ 13536{\tt C:=[[36,40,32,43,29],[28,27,29,40,38],[34,35,41,29,31],}\\ 13537{\tt [41,42,35,27,36],[25,28,40,34,38],[31,30,43,38,40]];}\\ 13538{\tt tpsolve(s,d,C)} 13539\end{center} 13540Output : 13541\begin{center} 13542{\tt 1275,[[0,0,2,0,5],[0,0,10,0,0],[0,0,0,0,5],}\\ 13543{\tt [0,0,0,8,0],[9,0,0,0,0],[0,6,0,0,0]]} 13544\end{center} 13545 13546Sometimes 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. 13547 13548\noindent Input : 13549\begin{center} 13550{\tt s:=[95,70,165,165];d:=[195,150,30,45,75];}\\ 13551{\tt C:=[[15,M,45,M,0],[12,40,M,M,0],}\\ 13552{\tt [0,15,25,25,0],[M,0,M,12,0]]}\\ 13553{\tt tpsolve(s,d,C)} 13554\end{center} 13555Output : 13556\begin{center} 13557{\tt 2820,[[20,0,0,0,75],[70,0,0,0,0],}\\ 13558{\tt [105,0,30,30,0],[0,150,0,15,0]]} 13559\end{center} 13560 13561\section{Nonlinear optimization} 13562 13563\subsection{Global extrema: {\tt minimize maximize}}\index{minimize|textbf}\index{maximize|textbf} 13564 13565The function {\tt minimize} takes four arguments : 13566\begin{itemize} 13567\item {\tt obj} : univariate or multivariate expression 13568\item {\tt constr} (optional) : list of equality and inequality constraints 13569\item {\tt vars} : list of variables 13570\item {\tt location} (optional) : option keyword which may be {\tt coordinates}, {\tt locus} or {\tt point} 13571\end{itemize} 13572The 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. 13573 13574Constraints 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. 13575 13576{\tt minimize} returns 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 sequence. Keywords {\tt locus}, {\tt coordinates} and {\tt point} all have the same effect. 13577 13578The 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. 13579 13580\subsubsection{Examples} 13581Input : 13582\begin{center} 13583{\tt minimize(sin(x),[x=0..4])} 13584\end{center} 13585Output : 13586\begin{center} 13587{\tt sin(4)} 13588\end{center} 13589Input : 13590\begin{center} 13591{\tt minimize(asin(x),x=-1..1)} 13592\end{center} 13593Output : 13594\begin{center} 13595{\tt -pi/2} 13596\end{center} 13597Input : 13598\begin{center} 13599{\tt minimize(x\verb|^|4-x\verb|^|2,x=-3..3,locus)} 13600\end{center} 13601Output : 13602\begin{center} 13603{\tt -1/4,[-sqrt(2)/2]} 13604\end{center} 13605Input : 13606\begin{center} 13607{\tt minimize(x-abs(x),x=-1..1)} 13608\end{center} 13609Output : 13610\begin{center} 13611{\tt -2} 13612\end{center} 13613Input : 13614\begin{center} 13615{\tt minimize(when(x==0,0,exp(-1/x\verb|^|2)),x=-1..1)} 13616\end{center} 13617Output : 13618\begin{center} 13619{\tt 0} 13620\end{center} 13621Input : 13622\begin{center} 13623{\tt minimize(sin(x)+cos(x),x=0..20,coordinates)} 13624\end{center} 13625Output : 13626\begin{center} 13627{\tt -sqrt(2),[5*pi/4,13*pi/4,21*pi/4]} 13628\end{center} 13629Input : 13630\begin{center} 13631{\tt minimize(x\verb|^|2-3x+y\verb|^|2+3y+3,[x=2..4,y=-4..-2],point)} 13632\end{center} 13633Output : 13634\begin{center} 13635{\tt -1,[[2,-2]]} 13636\end{center} 13637Input : 13638\begin{center} 13639{\tt obj:=sqrt(x\verb|^|2+y\verb|^|2)-z;}\\ 13640{\tt constr:=[x\verb|^|2+y\verb|^|2<=16,x+y+z=10];}\\ 13641{\tt minimize(obj,constr,[x,y,z])} 13642\end{center} 13643Output : 13644\begin{center} 13645{\tt -4*sqrt(2)-6} 13646\end{center} 13647Input : 13648\begin{center} 13649{\tt minimize(x\verb|^|2*(y+1)-2y,[y<=2,sqrt(1+x\verb|^|2)<=y],[x,y])} 13650\end{center} 13651Output : 13652\begin{center} 13653{\tt -4} 13654\end{center} 13655Input : 13656\begin{center} 13657{\tt maximize(cos(x),x=1..3)} 13658\end{center} 13659Output : 13660\begin{center} 13661{\tt cos(1)} 13662\end{center} 13663Input : 13664\begin{center} 13665{\tt obj:=piecewise(x<=-2,x+6,x<=1,x\verb|^|2,3/2-x/2);} 13666{\tt maximize(obj,x=-3..2)} 13667\end{center} 13668Output : 13669\begin{center} 13670{\tt 4} 13671\end{center} 13672Input : 13673\begin{center} 13674{\tt maximize(x*y*z,x\verb|^|2+2*y\verb|^|2+3*z\verb|^|2<=1,[x,y,z])} 13675\end{center} 13676Output : 13677\begin{center} 13678{\tt sqrt(2)/18} 13679\end{center} 13680Input : 13681\begin{center} 13682{\tt maximize(x*y,[x+y\verb|^|2<=2,x>=0,y>=0],[x,y],locus)} 13683\end{center} 13684Output : 13685\begin{center} 13686{\tt 4*sqrt(6)/9,[[4/3,sqrt(6)/3]]} 13687\end{center} 13688Input : 13689\begin{center} 13690{\tt maximize(y\verb|^|2-x\verb|^|2*y,y<=x,[x=0..2,y=0..2])} 13691\end{center} 13692Output : 13693\begin{center} 13694{\tt 4/27} 13695\end{center} 13696Input : 13697\begin{center} 13698{\tt assume(a>0);}\\ 13699{\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])} 13700\end{center} 13701Output : 13702\begin{center} 13703{\tt a\verb|^|6/27} 13704\end{center} 13705 13706\subsection{Local extrema: {\tt extrema}}\index{extrema|textbf} 13707 13708Local extrema of a univariate or multivariate differentiable function under equality constraints can be obtained by using function {\tt extrema} which takes four arguments : 13709\begin{itemize} 13710\item {\tt expr} : differentiable expression 13711\item {\tt constr} (optional) : list of equality constraints 13712\item {\tt vars} : list of variables 13713\item {\tt order\_size=<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 13714\end{itemize} 13715Function returns sequence of two lists of points: local minima and maxima, 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. 13716 13717A 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. 13718 13719Number 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 $). 13720 13721Variables 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} $. 13722 13723If {\tt order\_size=<n>} is specified as the fourth argument, derivatives up to order $ n $ are inspected to find critical points and classify them. For {\tt order\_size=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. 13724 13725 13726\subsubsection{Examples} 13727Input : 13728\begin{center} 13729{\tt extrema(-2*cos(x)-cos(x)\verb|^|2,x)} 13730\end{center} 13731Output : 13732\begin{center} 13733{\tt [0],[pi]} 13734\end{center} 13735Input : 13736\begin{center} 13737{\tt extrema(x/2-2*sin(x/2),x=-12..12)} 13738\end{center} 13739Output : 13740\begin{center} 13741{\tt [2*pi/3,-10*pi/3],[10*pi/3,-2*pi/3]} 13742\end{center} 13743Input : 13744\begin{center} 13745{\tt assume(a>=0);extrema(x\verb|^|2+a*x,x)} 13746\end{center} 13747Output : 13748\begin{center} 13749{\tt [-a/2],[]} 13750\end{center} 13751Input : 13752\begin{center} 13753{\tt extrema(exp(x\verb|^|2-2x)*ln(x)*ln(1-x),x=0.5)} 13754\end{center} 13755Output : 13756\begin{center} 13757{\tt [],[0.277769149124]} 13758\end{center} 13759Input : 13760\begin{center} 13761{\tt extrema(x\verb|^|3-2x*y+3y\verb|^|4,[x,y])} 13762\end{center} 13763Output : 13764\begin{center} 13765{\tt [[12\verb|^|(1/5)/3,(12\verb|^|(1/5))\verb|^|2/6]],[]} 13766\end{center} 13767Input : 13768\begin{center} 13769{\tt assume(a>0);extrema(x/a\verb|^|2+a*y\verb|^|2,x+y=a,[x,y])} 13770\end{center} 13771Output : 13772\begin{center} 13773{\tt [[(2*a\verb|^|4-1)/(2*a\verb|^|3),1/(2*a\verb|^|3)]],[]} 13774\end{center} 13775Input : 13776\begin{center} 13777{\tt extrema(x\verb|^|2+y\verb|^|2,x*y=1,[x=0..inf,y=0..inf])} 13778\end{center} 13779Output : 13780\begin{center} 13781{\tt [[1,1]],[]} 13782\end{center} 13783Input : 13784\begin{center} 13785{\tt extrema(x2\verb|^|4-x1\verb|^|4-x2\verb|^|8+x1\verb|^|10,[x1,x2])} 13786\end{center} 13787Output : 13788\begin{center} 13789{\tt [[6250\verb|^|(1/6)/5,0],[-6250\verb|^|(1/6)/5,0]],[]} 13790\end{center} 13791Input : 13792\begin{center} 13793{\tt extrema(x*y*z,x+y+z=1,[x,y,z],order\_size=1)} 13794\end{center} 13795Output : 13796\begin{center} 13797{\tt [[1,0,0],[0,1,0],[0,0,1],[1/3,1/3,1/3]]} 13798\end{center} 13799 13800\subsection{Minimax polynomial approximation: {\tt minimax}}\index{minimax|textbf} 13801 13802The function {\tt minimax} is called by entering : 13803\begin{center} 13804{\tt minimax(expr,var=a..b,n,[limit=m])} 13805\end{center} 13806where {\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. 13807 13808If the fourth argument is specified, $ m $ is used to limit the number of iterations of the algorithm. It is unlimited by default. 13809 13810The 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. 13811 13812Since 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. 13813 13814\noindent Input : 13815\begin{center} 13816{\tt minimax(sin(x),x=0..2*pi,10)} 13817\end{center} 13818Output : 13819\begin{center} 13820{\tt 5.8514210172e-06+0.999777263385*x+0.00140015265723*x\verb|^|2}\\ 13821{\tt -0.170089663733*x\verb|^|3+0.0042684304696*x\verb|^|4+}\\ 13822{\tt 0.00525794766407*x\verb|^|5+0.00135760214958*x\verb|^|6}\\ 13823{\tt -0.000570502074548*x\verb|^|7+6.07297119422e-05*x\verb|^|8}\\ 13824{\tt -2.14787414001e-06*x\verb|^|9-2.97767481643e-15*x\verb|^|10} 13825\end{center} 13826The largest absolute error of this approximation is $ 5.85234008632\times 10^{-6} $. 13827 13828 13829\section{Different matrix norm} 13830\subsection{$l^2$ matrix norm : {\tt norm l2norm}}\index{norm}\index{l2norm|textbf}\label{sec:l2normm} 13831\noindent{\tt norm} (or {\tt l2norm}) takes as argument a matrix $A=a_{j,k}$ 13832(see also \ref{sec:l2normv}).\\ 13833{\tt norm} (or {\tt l2norm}) returns 13834$\displaystyle \sqrt{\sum_{j,k} a_{j,k}^2}$.\\ 13835Input : 13836\begin{center}{\tt norm([[1,2],[3,-4]])}\end{center} 13837or : 13838\begin{center}{\tt l2norm([[1,2],[3,-4]])}\end{center} 13839Output : 13840\begin{center}{\tt sqrt(30)}\end{center} 13841 13842\subsection{$l^\infty$ matrix norm : {\tt maxnorm}}\index{maxnorm}\label{sec:maxnormm} 13843\noindent{\tt maxnorm} takes as argument a matrix $A=a_{j,k}$ (see also \ref{sec:maxnormv}).\\ 13844{\tt maxnorm} returns $ \max(|a_{j,k}|)$.\\ 13845Input : 13846\begin{center}{\tt maxnorm([[1,2],[3,-4]])}\end{center} 13847Output : 13848\begin{center}{\tt 4}\end{center} 13849 13850\subsection{Matrix row norm : {\tt rownorm rowNorm}}\index{rowNorm}\index{rownorm} 13851\noindent{\tt rownorm} (or {\tt rowNorm}) takes as argument a matrix $A=a_{j,k}$.\\ 13852{\tt rownorm} (or {\tt rowNorm}) returns $\max_k(\sum_j |a_{j,k}|)$.\\ 13853Input : 13854\begin{center}{\tt rownorm([[1,2],[3,-4]])}\end{center} 13855or : 13856\begin{center}{\tt rowNorm([[1,2],[3,-4]])}\end{center} 13857Output : 13858\begin{center}{\tt 7}\end{center} 13859Indeed : $\max(1+2,3+4)=7$ 13860 13861\subsection{Matrix column norm : {\tt colnorm colNorm}}\index{colNorm}\index{colnorm} 13862\noindent{\tt colnorm} (or {\tt colNorm}) takes as argument a matrix 13863$A=a_{j,k}$.\\ 13864{\tt colnorm} (or {\tt colNorm}) returns $\max_j(\sum_k(|a_{j,k}|))$.\\ 13865Input : 13866\begin{center}{\tt colnorm([[1,2],[3,-4]])}\end{center} 13867or : 13868\begin{center}{\tt colNorm([[1,2],[3,-4]])}\end{center} 13869Output : 13870\begin{center}{\tt 6}\end{center} 13871Indeed : $\max(1+3,2+4)=6$ 13872 13873\section{Matrix reduction} 13874\subsection{Eigenvalues : {\tt eigenvals}}\index{eigenvals} 13875\noindent{\tt eigenvals} takes as argument a square 13876matrix $A$ of size $n$.\\ 13877{\tt eigenvals} returns the sequence of the $n$ eigenvalues of $A$.\\ 13878{\bf Remark} : If $A$ is exact, {\tt Xcas} may not be able 13879to find the exact roots of the characteristic polynomial, 13880{\tt eigenvals} will return approximate eigenvalues of $A$ if the 13881coefficients are numeric or a subset of the eigenvalues if the 13882coefficients are symbolic.\\ 13883Input : 13884\begin{center}{\tt eigenvals([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 13885Output : 13886\begin{center}{\tt (2,2,2) }\end{center} 13887Input : 13888\begin{center}{\tt eigenvals([[4,1,0],[1,2,-1],[2,1,0]])}\end{center} 13889Output : 13890\begin{center}{\tt (0.324869129433,4.21431974338,1.46081112719)}\end{center} 13891 13892\subsection{Eigenvalues : {\tt egvl eigenvalues eigVl}}\index{egvl}\index{eigVl}\index{eigenvalues} 13893\noindent{\tt egvl} (or {\tt eigenvalues eigVl}) takes as argument a 13894square matrix $A$ of size $n$.\\ 13895{\tt egvl} (or {\tt eigenvalues eigVl}) returns the Jordan normal 13896form of $A$.\\ 13897{\bf Remark} : If $A$ is exact, {\tt Xcas} may not be able 13898to find the exact roots of the characteristic polynomial, 13899{\tt eigenvalues} will return an approximate diagonalization of $A$ if the 13900coefficients are numeric.\\ 13901Input : 13902\begin{center}{\tt egvl([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 13903Output : 13904\begin{center}{\tt [[2,1,0],[0,2,1],[0,0,2]] }\end{center} 13905Input : 13906\begin{center}{\tt egvl([[4,1,0],[1,2,-1],[2,1,0]])}\end{center} 13907Output : 13908\begin{center}{\tt [[0.324869129433,0,0],[0,4.21431974338,0],[0,0,1.46081112719]]}\end{center} 13909 13910\subsection{Eigenvectors : {\tt egv eigenvectors eigenvects 13911eigVc}}\index{egv}\index{eigenvectors}\index{eigenvects}\index{eigVc} 13912\noindent{\tt egv} (or {\tt eigenvectors eigenvects eigVc}) takes as argument 13913a square matrix $A$ of size $n$.\\ 13914If $A$ is a diagonalizable matrix, {\tt egv} (or 13915{\tt eigenvectors eigenvects eigVc}) returns a matrix whose columns are the 13916eigenvectors of the matrix $A$. Otherwise, it will fail (see also 13917{\tt jordan} for characteristic vectors).\\ 13918Input : 13919\begin{center}{\tt egv([[1,1,3],[1,3,1],[3,1,1]])}\end{center} 13920Output : 13921\begin{center}{\tt [[-1,1,1],[2,1,0],[-1,1,-1]] }\end{center} 13922Input : 13923\begin{center}{\tt egv([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 13924Output : 13925\begin{center}{\tt "Not diagonalizable at eigenvalue 2"}\end{center} 13926In complex mode, input : 13927\begin{center}{\tt egv([[2,0,0],[0,2,-1],[2,1,2]])}\end{center} 13928Output : 13929\begin{center}{\tt [0,1,0],[-1,-2,-1],[i,0,-i]]}\end{center} 13930 13931\subsection{Rational Jordan matrix : {\tt rat\_jordan}}\index{rat\_jordan} 13932\noindent {\tt rat\_jordan} takes as argument a square 13933matrix $A$ of size $n$ with exact coefficients.\\ 13934{\tt rat\_jordan} returns : 13935\begin{itemize} 13936\item in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode\\ 13937a sequence of two matrices : a matrix $P$ (the columns of $P$ are 13938the eigenvectors if $A$ is diagonalizable in the field of its coefficients) 13939and the rational Jordan matrix $J$ of $A$, that is the most reduced 13940matrix in the field of the coefficients of $A$ (or the complexified 13941field in complex mode), where 13942\[ J=P^{-1}AP \] 13943\item in {\tt Maple} mode\\ 13944the Jordan matrix $J$ of $A$. We can also have the matrix $P$ verifying 13945$J=P^{-1}AP$ in a variable 13946by passing this variable as second argument, for example 13947\begin{center} {\tt rat\_jordan([[1,0,0],[1,2,-1],[0,0,1]],'P')} 13948\end{center} 13949\end{itemize} 13950{\bf Remarks} 13951\begin{itemize} 13952\item the syntax {\tt Maple} is also valid in the other modes, for example, in 13953{\tt Xcas} mode input 13954\begin{center} {\tt rat\_jordan([[4,1,1],[1,4,1],[1,1,4]],'P')} 13955\end{center} 13956Output : 13957\begin{center} {\tt [[1,-1,1/2],[1,0,-1],[1,1,1/2]]} 13958\end{center} 13959then {\tt P} returns 13960\begin{center} {\tt [[6,0,0],[0,3,0],[0,0,3]]} 13961\end{center} 13962\item the coefficients of $P$ and $J$ belongs to the same field as the 13963coefficients of $A$.\\ 13964For example, in {\tt Xcas} mode, input : 13965\begin{center} {\tt rat\_jordan([[1,0,1],[0,2,-1],[1,-1,1]])} 13966\end{center} 13967Output : 13968\begin{center} {\tt [[1,1,2],[0,0,-1],[0,1,2]],[[0,0,-1],[1,0,-3],[0,1,4]]}\end{center} 13969Input (put {\tt -pcar(...)} because the argument of {\tt companion} is a unit 13970polynomial (see \ref{sec:compagne}) 13971\begin{center} {\tt companion(-pcar([[1,0,1],[0,2,-1],[1,-1,1]],x),x)}\end{center} 13972Output : 13973\begin{center} {\tt [[0,0,-1],[1,0,-3],[0,1,4]]}\end{center} 13974Input : 13975\begin{center} {\tt rat\_jordan([[1,0,0],[0,1,1],[1,1,-1]])}\end{center} 13976Output : 13977\begin{center} {\tt [[-1,0,0],[1,1,1],[0,0,1]],[[1,0,0],[0,0,2],[0,1,0]]}\end{center} 13978Input : 13979\begin{center} {\tt factor(pcar([[1,0,0],[0,1,1],[1,1,-1]],x))}\end{center} 13980Output : 13981\begin{center} {\tt -(x-1)*(x\verb|^|2-2)}\end{center} 13982Input : 13983\begin{center} {\tt companion((x\verb|^|2-2),x)}\end{center} 13984Output : 13985\begin{center} {\tt [[0,2],[1,0]]}\end{center} 13986 13987 13988 13989\item When $A$ is symmetric and has eigenvalues with an multiple order, 13990{\tt Xcas} returns orthogonal eigenvectors (not always of norm equal to 1) 13991i.e. {\tt tran(P)*P} is a diagonal matrix where the diagonal is the square norm 13992of the eigenvectors, for example : 13993\begin{center} {\tt rat\_jordan([[4,1,1],[1,4,1],[1,1,4]])} 13994\end{center} 13995returns : 13996\begin{center} {\tt [[1,-1,1/2],[1,0,-1],[1,1,1/2]],[[6,0,0],[0,3,0],[0,0,3]]} 13997\end{center} 13998\end{itemize} 13999Input in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode : 14000\begin{center}{\tt rat\_jordan([[1,0,0],[1,2,-1],[0,0,1]])}\end{center} 14001Output : 14002\begin{center}{\tt [[0,1,0],[1,0,1],[0,1,1]],[[2,0,0],[0,1,0],[0,0,1]]}\end{center} 14003Input in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode : 14004\begin{center}{\tt rat\_jordan([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 14005Output : 14006\begin{center}{\tt [[[1,2,1],[0,1,0],[1,2,0]],[[2,1,0],[0,2,1],[0,0,2]]]}\end{center} 14007In complex mode and in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode , input : 14008\begin{center}{\tt rat\_jordan([[2,0,0],[0,2,-1],[2,1,2]])}\end{center} 14009Output : 14010\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} 14011Input in {\tt Maple} mode : 14012\begin{center}{\tt rat\_jordan([[1,0,0],[1,2,-1],[0,0,1]],'P')}\end{center} 14013Output : 14014\begin{center}{\tt [[2,0,0],[0,1,0],[0,0,1]]}\end{center} 14015then input : 14016\begin{center}{\tt P}\end{center} 14017Output : 14018\begin{center}{\tt [[0,1,0],[1,0,1],[0,1,1]]]}\end{center} 14019 14020\subsection{Jordan normal form : {\tt jordan}}\index{jordan} 14021\noindent {\tt jordan} takes as argument a square 14022matrix $A$ of size $n$.\\ 14023{\tt jordan} returns : 14024\begin{itemize} 14025\item in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode\\ 14026a sequence of two matrices : a matrix $P$ whose columns are 14027the eigenvectors and characteristic vectors 14028of the matrix $A$ and the Jordan matrix $J$ of $A$ verifying $J=P^{-1}AP$, 14029\item in {\tt Maple} mode\\ 14030the Jordan matrix $J$ of $A$. We can also have the matrix $P$ verifying 14031$J=P^{-1}AP$ in a variable 14032by passing this variable as second argument, for example 14033\begin{center} {\tt jordan([[1,0,0],[0,1,1],[1,1,-1]],'P')} 14034\end{center} 14035\end{itemize} 14036{\bf Remarks} 14037\begin{itemize} 14038\item the {\tt Maple} syntax is also valid in the other modes, for example, in 14039{\tt Xcas} mode input : 14040\begin{center} {\tt jordan([[4,1,1],[1,4,1],[1,1,4]],'P')} 14041\end{center} 14042Output : 14043\begin{center} {\tt [[1,-1,1/2],[1,0,-1],[1,1,1/2]]} 14044\end{center} 14045then {\tt P} returns 14046\begin{center} {\tt [[6,0,0],[0,3,0],[0,0,3]]} 14047\end{center} 14048\item When $A$ is symmetric and has eigenvalues with multiple orders, 14049{\tt Xcas} returns orthogonal eigenvectors (not always of norm equal to 1) 14050i.e. {\tt tran(P)*P} is a diagonal matrix where the diagonal is the square norm 14051of the eigenvectors, for example : 14052\begin{center} {\tt jordan([[4,1,1],[1,4,1],[1,1,4]])} 14053\end{center} 14054returns : 14055\begin{center} {\tt [[1,-1,1/2],[1,0,-1],[1,1,1/2]],[[6,0,0],[0,3,0],[0,0,3]]} 14056\end{center} 14057\end{itemize} 14058 Input in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode : 14059\begin{center}{\tt jordan([[1,0,0],[0,1,1],[1,1,-1]])}\end{center} 14060Output : 14061\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} 14062Input in {\tt Maple} mode : 14063\begin{center}{\tt jordan([[1,0,0],[0,1,1],[1,1,-1]])}\end{center} 14064Output : 14065\begin{center}{\tt [[1,0,0],[0,-(sqrt(2)),0],[0,0,sqrt(2)]]}\end{center} 14066then input : 14067\begin{center}{\tt P}\end{center} 14068Output : 14069\begin{center}{\tt [[-1,0,0],[1,1,1],[0,-sqrt(2)-1,sqrt(2)-1]]}\end{center} 14070Input in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode : 14071\begin{center}{\tt jordan([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 14072Output : 14073\begin{center}{\tt [[[1,2,1],[0,1,0],[1,2,0]],[[2,1,0],[0,2,1],[0,0,2]]]}\end{center} 14074In complex mode and in {\tt Xcas}, {\tt Mupad} or {\tt TI} mode , input : 14075\begin{center}{\tt jordan([[2,0,0],[0,2,-1],[2,1,2]])}\end{center} 14076Output : 14077\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} 14078 14079\subsection{Characteristic polynomial : {\tt charpoly}}\index{pcar}\index{charpoly} 14080\noindent{\tt charpoly} (or {\tt pcar}) takes one or two argument(s), 14081 a square matrix $A$ of size $n$ and optionally 14082the name of a symbolic variable.\\ 14083{\tt charpoly} returns the characteristic polynomial 14084$P$ of $A$ written as the 14085list of its coefficients if no variable name was provided 14086or written as an expression with respect to 14087the variable name provided as second argument.\\ 14088The characteristic polynomial $P$ of $A$ is defined as 14089\[ P(x)=\det(x I-A) \] 14090Input : 14091\begin{center}{\tt charpoly([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 14092Output : 14093\begin{center}{\tt[1,-6,12,-8]}\end{center} 14094Hence, the characteristic polynomial of this matrix is 14095$x^3-6x^2+12x-8$ (input {\tt normal(poly2symb([1,-6,12,-8],x))} to get 14096its symbolic representation).\\ 14097Input : 14098\begin{center}{\tt purge(X):; 14099charpoly([[4,1,-2],[1,2,-1],[2,1,0]],X)}\end{center} 14100Output : 14101\begin{center}{\tt X\verb|^|3-6*X\verb|^|2+12*X-8}\end{center} 14102 14103\subsection{Characteristic polynomial using Hessenberg algorithm : 14104 {\tt pcar\_hessenberg}}\index{pcar\_hessenberg} 14105\noindent{\tt pcar\_hessenberg} takes as argument a square 14106matrix $A$ of size $n$ and optionally the name of a symbolic variable.\\ 14107{\tt pcar\_hessenberg} returns the characteristic polynomial $P$ of $A$ written 14108as the list of its coefficients if no variable was provided 14109or written in its symbolic form with respect to the variable name given 14110as second argument, where 14111\[ P(x)=\det(xI-A) \] 14112The characteristic polynomial is computed using the Hessenberg algorithm 14113(see e.g. Cohen) which is more efficient ($O(n^3)$ deterministic) if 14114the coefficients of $A$ are in a finite field or use a finite 14115representation like approximate numeric coefficients. Note however that 14116this algorithm behaves badly if the coefficients are e.g. in $\mathbb Q$.\\ 14117Input : 14118\begin{center}{\tt pcar\_hessenberg([[4,1,-2],[1,2,-1],[2,1,0]] \% 37)}\end{center} 14119Output : 14120\begin{center}{\tt[1 \% 37 ,-6\% 37,12 \% 37,-8 \% 37]}\end{center} 14121Input : 14122\begin{center}{\tt pcar\_hessenberg([[4,1,-2],[1,2,-1],[2,1,0]] \% 37,x)}\end{center} 14123Output : 14124\begin{center}{\tt x\verb|^|3-6 \%37 *x\verb|^|2+12 \% 37 *x-8 \% 37}\end{center} 14125Hence, the characteristic polynomial of [[4,1,-2],[1,2,-1],[2,1,0]] in 14126$\mathbb Z/37 \mathbb Z$ is 14127\[ x^3-6x^2+12x-8 \] 14128 14129\subsection{Minimal polynomial : {\tt pmin}}\index{pmin} 14130\noindent{\tt pmin} takes one (resp. two) argument(s): 14131a square matrix $A$ of size $n$ and optionally 14132the name of a symbolic variable.\\ 14133{\tt pmin} returns the minimal polynomial of $A$ written as a 14134list of its coefficients if no variable was provided, or 14135written in symbolic form with respect to the 14136variable name given as second argument. 14137The minimal polynomial of $A$ is the polynomial $P$ 14138having minimal degree such that $P(A)=0$.\\ 14139Input : 14140\begin{center}{\tt pmin([[1,0],[0,1]])}\end{center} 14141Output : 14142\begin{center}{\tt [1,-1]}\end{center} 14143Input : 14144\begin{center}{\tt pmin([[1,0],[0,1]],x)}\end{center} 14145Output : 14146\begin{center}{\tt x-1}\end{center} 14147Hence the minimal polynomial of [[1,0],[0,1]] is {\tt x-1}.\\ 14148Input : 14149\begin{center}{\tt pmin([[2,1,0],[0,2,0],[0,0,2]])}\end{center} 14150Output : 14151\begin{center}{\tt [1,-4,4]}\end{center} 14152 Input : 14153\begin{center}{\tt pmin([[2,1,0],[0,2,0],[0,0,2]],x)}\end{center} 14154Output : 14155\begin{center}{\tt x\verb|^|2-4*x+4}\end{center} 14156Hence, the minimal polynomial of [[2,1,0],[0,2,0],[0,0,2]] is $x^2-4x+4$. 14157 14158\subsection{Adjoint matrix : {\tt adjoint\_matrix}}\index{adjoint\_matrix} 14159\noindent{\tt adjoint\_matrix } takes as argument a square matrix 14160$A$ of size $n$.\\ 14161{\tt adjoint\_matrix } returns the list of the coefficients of $P$ 14162(the characteristic polynomial of $A$), and 14163the list of the matrix coefficients of $Q$ (the adjoint matrix 14164of $A$). 14165 14166The comatrix of a square matrix $A$ of size $n$ is the matrix $B$ 14167defined by $A\times B=\det(A)\times I$. The adjoint matrix of $A$ 14168is the comatrix of $xI-A$. It is a polynomial of degree $n-1$ in $x$ 14169having matrix coefficients. 14170The following relation holds: 14171\[ P(x)\times I=\det(xI-A)I=(xI-A)Q(x)\] 14172Since the polynomial $P(x)\times I-P(A)$ (with matrix coefficients) 14173is also divisible by $x\times I-A$ (by algebraic identities), 14174this proves that $P(A)=0$. 14175We also have $Q(x)\ =\ I\times x^{n-1}+...+B_0 $ 14176where $B_0=$ is the comatrix of $A$ (up to the sign if $n$ is odd).\\ 14177Input : 14178\begin{center}{\tt adjoint\_matrix([[4,1,-2],[1,2,-1],[2,1,0]])}\end{center} 14179Output : 14180\begin{center} 14181{\tt [ 14182 {\bf [}1,-6,12,-8{\bf ]},\\ 14183{\bf [} [[1,0,0],[0,1,0],[0,0,1]], 14184 [[-2,1,-2], [1,-4,-1],[2,1,-6]], 14185 [[1,-2,3],[-2,4,2],[-3,-2,7]] {\bf ]} 14186] }\end{center} 14187Hence the characteristic polynomial is : 14188\[ P(x)=x^3-6*x^2+12*x-8 \] 14189The determinant of $A$ is equal to $-P(0)=8$. 14190The comatrix of $A$ is equal to : 14191\[ B=Q(0)=[[1,-2,3],[-2,4,2],[-3,-2,7]] \] 14192Hence the inverse of $A$ is equal to : 14193\[ 1/8*[[1,-2,3],[-2,4,2],[-3,-2,7]] \] 14194The adjoint matrix of $A$ is : 14195\[ [[x^2-2x+1,x-2,-2x+3],[x-2,x^2-4x+4,-x+2],[2x-3,x-2,x^2-6x+7]] \] 14196Input : 14197\begin{center}{\tt adjoint\_matrix([[4,1],[1,2]])}\end{center} 14198Output : 14199\begin{center}{\tt[[1,-6,7],[[[1,0],[0,1]],[[-2,1],[1,-4]]]]}\end{center} 14200Hence the characteristic polynomial $P$ is : 14201\[ P(x)=x^2-6*x+7 \] 14202The determinant of $A$ is equal to $+P(0)=7$. 14203The comatrix of $A$ is equal to 14204\[ Q(0)= -[[-2,1],[1,-4]] \] 14205Hence the inverse of $A$ is equal to : 14206\[ -1/7*[[-2,1],[1,-4]] \] 14207The adjoint matrix of $A$ is : 14208\[ -[[x-2,1],[1,x-4]] \] 14209 14210\subsection{Companion matrix of a polynomial : {\tt companion}}\index{companion|textbf}\label{sec:compagne} 14211\noindent{\tt companion} takes as argument an unitary polynomial $P$ and the 14212name of its variable.\\ 14213{\tt companion} returns the matrix whose characteristic polynomial is $P$.\\ 14214If $P(x)=x^n+a_{n-1}x^{n-1}+...+a_{-1}x+a_0$, 14215this matrix is equal to the unit matrix of size $n-1$ bordered with 14216$[0,0..,0,-a_0]$ as first row, and with 14217$[-a_0,-a_1,....,-a_{n-1}]$ as last column.\\ 14218Input : 14219\begin{center}{\tt companion(x\verb|^|2+5x-7,x)}\end{center} 14220Output : 14221\begin{center}{\tt [[0,7],[1,-5]]}\end{center} 14222Input : 14223\begin{center}{\tt companion(x\verb|^|4+3x\verb|^|3+2x\verb|^|2+4x-1,x)}\end{center} 14224Output : 14225\begin{center}{\tt [[0,0,0,1],[1,0,0,-4],[0,1,0,-2],[0,0,1,-3]]}\end{center} 14226 14227\subsection{Hessenberg matrix reduction : {\tt hessenberg}}\index{hessenberg} 14228\noindent{\tt hessenberg} takes as argument a matrix $A$.\\ 14229{\tt hessenberg} returns a matrix $B$ equivalent to $A$ where the 14230coefficients below the sub-principal diagonal are zero. 14231$B$ is a Hessenberg matrix.\\ 14232Input : 14233\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} 14234Output : 14235\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} 14236Input 14237\begin{center} 14238{\tt A:=[[3,2,2,2,2],[2,1,2,-1,-1],[2,2,1,-1,1],}\\ 14239{\tt \ [2,-1,-1,3,1],[2,-1,1,1,2]] :;}\\ 14240{\tt B:= hessenberg(A):; pcar(A); pcar(B) 14241} 14242\end{center} 14243Output: {\tt [1,-7,-66,-24]}. 14244 14245\subsection{Hermite normal form : {\tt ihermite}}\index{ihermite} 14246\noindent {\tt ihermite} takes as argument a matrix {\tt A} with coefficients 14247in $\mathbb Z$.\\ 14248{\tt ihermite} returns two matrices {\tt U} and {\tt B} such that 14249{\tt B=U*A}, {\tt U} is invertible in $\mathbb Z$ (det$(U) = \pm 1$) 14250and {\tt B} is upper-triangular. Moreover, 14251the absolute value of the coefficients above the diagonal of {\tt B} are 14252smaller than the pivot of the column divided by 2. 14253 14254The answer is obtained by a Gauss-like reduction algorithm 14255using only operations of rows with integer coefficients 14256and invertible in $\mathbb Z$.\\ 14257Input : 14258\begin{center}{\tt A:=[[9,-36,30],[-36,192,-180],[30,-180,180]]; U,B:=ihermite(A)}\end{center} 14259Output : 14260\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} 14261 14262{\bf Application: Compute a $\mathbb Z$-basis of the kernel of a 14263matrix having integer coefficients}\\ 14264Let {\tt M} be a matrix with integer coefficients.\\ 14265Input : 14266\begin{center} 14267{\tt (U,A):=ihermite(transpose(M))}. 14268\end{center} 14269This returns $U$ and $A$ such that {\tt A=U*transpose(M)} hence \\ 14270{\tt transpose(A)=M*transpose(U)}.\\ 14271The columns of {\tt transpose(A)} which are identically 0 (at the right, 14272coming from the rows of $A$ which are identically 0 at the bottom) 14273correspond to columns of {\tt transpose(U)} which form a basis 14274of {\tt Ker(M)}. In other words, the rows of {\tt A} 14275which are identically 0 correspond to rows of {\tt U} 14276which form a basis of {\tt Ker(M)}.\\ 14277{\bf Example}\\ 14278Let {\tt M:=[[1,4,7],[2,5,8],[3,6,9]]}. Input 14279\begin{center}{\tt U,A:=ihermite(tran(M))}\end{center} 14280Output 14281\begin{center} 14282{\tt U:=[[-3,1,0],[4,-1,0],[-1,2,-1]] and A:=[[1,-1,-3],[0,3,6],[0,0,0]]} 14283\end{center} 14284Since {\tt A[2]=[0,0,0]}, a $\mathbb Z$-basis of {\tt Ker(M)} is 14285{\tt U[2]=[-1,2,-1]}.\\ 14286Verification {\tt M*U[2]=[0,0,0]}. 14287 14288\subsection{Smith normal form : {\tt ismith}}\index{ismith} 14289\noindent {\tt ismith} takes as argument a matrix with coefficients in 14290$\mathbb Z$.\\ 14291{\tt ismith} returns three matrices {\tt U,B} and {\tt V} such 14292that {\tt B=U*A*V}, {\tt U} and {\tt V} are invertible in $\mathbb Z$, 14293{\tt B} is diagonal, and {\tt B[i,i]} divides {\tt B[i+1,i+1]}. 14294The coefficients {\tt B[i,i]} are called 14295invariant factors, they are used to describe 14296the structure of finite abelian groups.\\ 14297Input : 14298\begin{center} 14299{\tt A:=[[9,-36,30],[-36,192,-180],[30,-180,180]]; 14300U,B,V:=ismith(A)} 14301\end{center} 14302Output : 14303\begin{center}{\tt 14304[[-3,0,1],[6,4,3],[20,15,12]], 14305[[3,0,0],[0,12,0],[0,0,60]], 14306[[1,24,-30],[0,1,0],[0,0,1]] } 14307\end{center} 14308The invariant factors are 3, 12 and 60. 14309 14310\section{Isometries} 14311\subsection{Recognize an isometry : {\tt isom}}\index{isom} 14312\noindent{\tt isom} takes as argument the matrix of a linear 14313function in dimension 2 or 3.\\ 14314{\tt isom} returns : 14315\begin{itemize} 14316\item 14317if the linear function is a direct isometry,\\ 14318the list of the characteristic elements of this isometry and {\tt +1}, 14319\item if the linear function is an indirect isometry,\\ 14320the list of the characteristic elements of this isometry and {\tt -1} 14321\item if the linear function is not an isometry,\\ 14322{\tt [0]}. 14323\end{itemize} 14324Input : 14325\begin{center}{\tt isom([[0,0,1],[0,1,0],[1,0,0]])}\end{center} 14326Output : 14327\begin{center}{\tt [[1,0,-1],-1]}\end{center} 14328which means that this isometry is a 3-d symmetry with respect to the plane 14329$x\ -\ z\ =\ 0$.\\ 14330Input : 14331\begin{center}{\tt isom(sqrt(2)/2*[[1,-1],[1,1]])}\end{center} 14332Output : 14333\begin{center}{\tt [pi/4,1]}\end{center} 14334Hence, this isometry is a 2-d rotation of angle 14335$\displaystyle \frac{\pi}{4}$.\\ 14336Input : 14337\begin{center}{\tt isom([[0,0,1],[0,1,0],[0,0,1]])}\end{center} 14338Output : 14339\begin{center}{\tt [0]}\end{center} 14340therefore this transformation is not an isometry. 14341 14342\subsection{Find the matrix of an isometry : {\tt mkisom}}\index{mkisom} 14343{\tt mkisom} takes as argument : 14344\begin{itemize} 14345\item In dimension 3, the list of characteristic elements 14346(axis direction, angle for a rotation or normal to the plane for 14347a symmetry) and {\tt +1} for a direct isometry or 14348{\tt -1} an indirect isometry. 14349\item In dimension 2, a characteristic element (an angle or a vector) and 14350{\tt +1} for a direct isometry (rotation) or {\tt -1} for an 14351indirect isometry (symmetry). 14352\end{itemize} 14353{\tt mkisom} returns the matrix of the corresponding isometry.\\ 14354Input : 14355\begin{center}{\tt mkisom([[-1,2,-1],pi],1)}\end{center} 14356Output the matrix of the rotation of axis $[-1,2,-1]$ and angle $\pi$: 14357\begin{center}{\tt [[-2/3,-2/3,1/3],[-2/3,1/3,-2/3],[1/3,-2/3,-2/3]]}\end{center} 14358Input : 14359\begin{center}{\tt mkisom([pi],-1)}\end{center} 14360Output the matrix of the symmetry with respect to $O$ : 14361\begin{center}{\tt [[-1,0,0],[0,-1,0],[0,0,-1]]}\end{center} 14362Input : 14363\begin{center}{\tt mkisom([1,1,1],-1)}\end{center} 14364Output the matrix of the symmetry with respect to the plane $x+y+z=0$ : 14365\begin{center}{\tt [[1/3,-2/3,-2/3],[-2/3,1/3,-2/3],[-2/3,-2/3,1/3]]}\end{center} 14366Input : 14367\begin{center}{\tt mkisom([[1,1,1],pi/3],-1)}\end{center} 14368Output the matrix of the product of a rotation of axis $[1,1,1]$ and angle 14369$\frac{\pi}{3}$ and of a symmetry with respect to the plane $x+y+z=0$: 14370\begin{center}{\tt [[0,-1,0],[0,0,-1],[-1,0,0]]}\end{center} 14371Input : 14372\begin{center}{\tt mkisom(pi/2,1)}\end{center} 14373Output the matrix of the plane rotation of angle $\frac{\pi}{2}$ : 14374\begin{center}{\tt [[0,-1],[1,0]]}\end{center} 14375Input : 14376\begin{center}{\tt mkisom([1,2],-1)}\end{center} 14377Output matrix of the plane symmetry with respect to the line 14378of equation $x+2y=0$: 14379\begin{center}{\tt [[3/5,-4/5],[-4/5,-3/5]]}\end{center} 14380 14381\section{Matrix factorizations}\label{sec:factormatrice} 14382Note that most matrix factorization algorithms are implemented numerically, 14383only a few of them will work symbolically. 14384 14385\subsection{Cholesky decomposition : {\tt cholesky}}\index{cholesky} 14386\noindent{\tt cholesky} takes as argument a square symmetric 14387positive definite matrix {\tt M} of size $n$.\\ 14388{\tt cholesky} returns a symbolic or numeric matrix {\tt P}. {\tt P} is a 14389lower triangular matrix such that : 14390\begin{center} 14391{\tt tran(P)*P=M} 14392\end{center} 14393Input : 14394\begin{center}{\tt cholesky([[1,1],[1,5]])}\end{center} 14395Output : 14396\begin{center}{\tt [[1,0],[1,2]]}\end{center} 14397Input : 14398\begin{center}{\tt cholesky([[3,1],[1,4]])}\end{center} 14399Output : 14400\begin{center}{\tt [[sqrt(3),0],[(sqrt(3))/3,(sqrt(33))/3]]}\end{center} 14401Input : 14402\begin{center}{\tt cholesky([[1,1],[1,4]])}\end{center} 14403Output : 14404\begin{center}{\tt [[1,0],[1,sqrt(3)]]}\end{center} 14405{\bf Warning} If the matrix argument $A$ is not a symmetric matrix, 14406{\tt cholesky} does not return an error, instead {\tt cholesky} will 14407use the symmetric matrix $B$ of the the quadratic form $q$ 14408corresponding to the (non symmetric) bilinear form of the matrix $A$.\\ 14409Input : 14410\begin{center}{\tt cholesky([[1,-1],[-1,4]])}\end{center} 14411or : 14412\begin{center}{\tt cholesky([[1,-3],[1,4]])}\end{center} 14413Output : 14414\begin{center}{\tt [[1,0],[-1,sqrt(3)]]}\end{center} 14415 14416\subsection{QR decomposition : {\tt qr}}\index{qr} 14417\noindent{\tt qr} takes as argument a numeric 14418square matrix $A$ of size $n$.\\ 14419{\tt qr} factorizes numerically 14420%(symbolically when it is possible) 14421this matrix as $Q*R$ where 14422$Q$ is an orthogonal matrix (${}^tQ*Q=I$) and $R$ is an upper triangular 14423matrix. 14424{\tt qr(A)} returns only {\tt R}, run {\tt Q=A*inv(R)} to get {\tt Q}.\\ 14425Input : 14426\begin{center}{\tt qr([[3,5],[4,5]])}\end{center} 14427Output is the matrix {\tt R} : 14428\begin{center}{\tt [[-5,-7],[0,-1]]}\end{center} 14429Input : 14430\begin{center}{\tt qr([[1,2],[3,4]])}\end{center} 14431Output is the matrix {\tt R} : 14432\begin{center}{\tt [[-3.16227766017,-4.42718872424],[0,-0.632455532034]] }\end{center} 14433 14434\subsection{QR decomposition (for TI compatibility) : {\tt QR}}\index{QR} 14435\noindent{\tt QR} takes as argument a numeric square matrix $A$ of size 14436$n$ and two variable names, {\tt var1} and {\tt var2}.\\ 14437{\tt QR} factorizes this matrix numerically as $Q*R$ where 14438$Q$ is an orthogonal matrix (${}^tQ*Q=I$) and $R$ is an upper triangular 14439matrix. {\tt QR(A,var1,var2)} returns {\tt R}, stores {\tt Q=A*inv(R)} in {\tt 14440 var1} and {\tt R} in {\tt var2}.\\ 14441Input : 14442\begin{center}{\tt QR([[3,5],[4,5]],Q,R)}\end{center} 14443Output the matrix {\tt R} : 14444\begin{center}{\tt [[-5,-7],[0,-1]]}\end{center} 14445Then input : 14446\begin{center}{\tt Q}\end{center} 14447Output the matrix {\tt Q} : 14448\begin{center}{\tt [[-0.6,-0.8],[-0.8,0.6]]}\end{center} 14449 14450\subsection{LU decomposition : {\tt lu}}\index{lu} 14451\noindent{\tt lu} takes as argument a square matrix $A$ of size $n$ (numeric or 14452symbolic).\\ 14453{\tt lu(A)} returns a permutation $p$ of 0..$n-1$, 14454a lower triangular matrix $L$, with $1$s on the diagonal, 14455and an upper triangular matrix $U$, such that : 14456\begin{itemize} 14457\item $P*A=L*U$ where $P$ is the permutation matrix 14458associated to $p$ (that may be computed by {\tt P:=permu2mat(p)}), 14459\item the equation $A*x=B$ is equivalent to : 14460\[ L*U*x=P*B=p(B) \mbox{ where } p(B)=[b_{p(0)},b_{p(1)}..b_{p(n-1)}], 14461\quad B=[b_0,b_1..b_{n-1}] \] 14462\end{itemize} 14463The permutation matrix $P$ is defined from $p$ by : 14464\[ P[i, p(i)]=1, \quad P[i, j]=0 \mbox{ if } j \ \neq\ p(i) \] 14465In other words, it is the identity matrix where the rows are permuted 14466according to the permutation $p$. 14467The function {\tt permu2mat}\index{permu2mat} may be used to compute $P$ 14468({\tt permu2mat(p)} returns ${\tt P}$).\\ 14469Input : 14470\begin{center}{\tt (p,L,U):=lu([[3.,5.],[4.,5.]])}\end{center} 14471Output : 14472\begin{center}{\tt [1,0],[[1,0],[0.75,1]],[[4,5],[0,1.25]]}\end{center} 14473Here $n=2$, hence : 14474\[ P[0,p(0)]=P_2[0,1]=1, \quad P[1,p(1)]=P_2[1,0]=1, \quad 14475P=[[0,1],[1,0]] \] 14476Verification :\\ 14477Input : 14478\begin{center}{\tt permu2mat(p)*A; L*U}\end{center} 14479Output: 14480\begin{center}{\tt [[4.0,5.0],[3.0,5.0]],[[4.0,5.0],[3.0,5.0]]}\end{center} 14481Note that the permutation is different for exact input (the choice of 14482pivot is the simplest instead of the largest in absolute value).\\ 14483Input : 14484\begin{center}{\tt lu([[1,2],[3,4]])}\end{center} 14485Output : 14486\begin{center}{\tt [1,0],[[1,0],[3,1]],[[1,2],[0,-2]]}\end{center} 14487Input : 14488\begin{center}{\tt lu([[1.0,2],[3,4]])}\end{center} 14489Output : 14490\begin{center}{\tt [1,0],[[1,0],[0.333333333333,1]],[[3,4], [0,0.666666666667]]}\end{center} 14491 14492\subsection{LU decomposition (for TI compatibility) : {\tt LU}}\index{LU} 14493\noindent{\tt LU} takes as argument a numeric 14494square matrix $A$ of size $n$ and three variable names, {\tt var1}, 14495{\tt var2} and {\tt var3}.\\ 14496{\tt LU(A,var1,var2,var3)} returns $P$, a permutation matrix, and stores : 14497\begin{itemize} 14498\item a lower triangular matrix $L$, with $1$ on the diagonal, in 14499{\tt var1}, 14500\item an upper triangular matrix $U$ in {\tt var2}, 14501\item the permutation matrix $P$, result of the command {\tt LU}, in 14502{\tt var3}. 14503\end{itemize} 14504These matrices are such that 14505\begin{center} 14506the equation $A*x=B$ is equivalent to $L*U*x=P*B$. 14507\end{center} 14508Input : 14509\begin{center}{\tt LU([[3,5],[4,5]],L,U,P)}\end{center} 14510Output : 14511\begin{center}{\tt [[0,1],[1,0]]}\end{center} 14512Input : 14513\begin{center}{\tt L}\end{center} 14514Output : 14515\begin{center}{\tt [[1,0],[0.75,1]]}\end{center} 14516Input : 14517\begin{center}{\tt U}\end{center} 14518Output : 14519\begin{center}{\tt [[4,5],[0,1.25]]}\end{center} 14520Input : 14521\begin{center}{\tt P}\end{center} 14522Output : 14523\begin{center}{\tt [[0,1],[1,0]]}\end{center} 14524 14525\subsection{Singular value decomposition : {\tt svd}}\index{svd} 14526\noindent{\tt svd} (singular value decomposition) takes as argument a numeric 14527square matrix of size $n$.\\ 14528{\tt svd(A)} returns an orthogonal matrix $U$, the diagonal $s$ of a diagonal 14529matrix $S$ and an orthogonal matrix $Q$ (${}^tQ*Q=I$) such that : 14530\[ A=U S{}^t Q \] 14531Input : 14532\begin{center}{\tt svd([[1,2],[3,4]])}\end{center} 14533Output : 14534\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} 14535Input : 14536\begin{center}{\tt (U,s,Q):=svd([[3,5],[4,5]])}\end{center} 14537Output : 14538\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} 14539Verification :\\ 14540Input : 14541\begin{center}{\tt U*diag(s)*tran(Q)}\end{center} 14542Output : 14543\begin{center}{\tt [[3.0,5.0],[4.0,5.0]]}\end{center} 14544 14545\subsection{Short basis of a lattice : {\tt lll}}\index{lll} 14546\noindent{\tt lll} takes as argument an invertible matrix $M$ with 14547integer coefficients.\\ 14548{\tt lll} returns $(S,A,L,O)$ such that: 14549\begin{itemize} 14550\item the rows of $S$ is a short basis of the $\mathbb Z$-module 14551generated by the rows of $M$, 14552\item $A$ is the change-of-basis matrix from the short basis to the basis 14553defined by the rows of $M$ ($A*M=S$), 14554\item $L$ is a lower triangular matrix, the modulus of its non diagonal 14555coefficients are less than 1/2, 14556\item $O$ is a matrix with orthogonal rows such that $L*O=S$. 14557\end{itemize} 14558% If in 2 dimension, $[a,b]$ are coordinates of a vector system in the basis 14559% defined by $M$ and if $[a1,b1]$ are its coordinates in the short basis 14560% defined by $S$ i.e. if $[a,b]*M=[a1,b1]*S$, then :\\ 14561% $[a,b]=[a1,b1]*A$\\ 14562% $[a1,b1]*S=[a1,b1]*A*M=[a,b]*M$ et\\ 14563% $[a,b]*M=[a,b]*A^{-1}*S=[a1,b1]*S$\\ 14564Input : 14565\begin{center}{\tt (S,A,L,O):=lll(M:=[[2,1],[1,2]])}\end{center} 14566Output : 14567\begin{center}{\tt [[-1,1],[2,1]], [[-1,1],[1,0]], [[1,0],[1/-2,1]], [[-1,1],[3/2,3/2]]}\end{center} 14568Hence :\\ 14569{\tt S=[[-1,1],[2,1]]}\\ 14570{\tt A=[[-1,1],[1,0]]}\\ 14571{\tt L=[[1,0],[1/-2,1]]}\\ 14572{\tt O=[[-1,1],[3/2,3/2]]}\\ 14573Hence the original basis is {\tt v1=[2,1], v2=[1,2]}\\ 14574and the short basis is {\tt w1=[-1,1], w2=[2,1]}.\\ 14575Since {\tt w1=-v1+v2} and {\tt w2=v1} then :\\ 14576{\tt A:=[[-1,1],[1,0]]}, {\tt A*M==S} and {\tt L*O==S}.\\ 14577Input : 14578\begin{center}{\tt (S,A,L,O):=lll([[3,2,1],[1,2,3],[2,3,1]])}\end{center} 14579Output : 14580\begin{center}{\tt S=[[-1,1,0],[-1,-1,2],[3,2,1]] }\end{center} 14581\begin{center}{\tt A= [[-1,0,1],[0,1,-1],[1,0,0]]}\end{center} 14582\begin{center}{\tt L= [[1,0,0],[0,1,0],[(-1)/2,(-1)/2,1]]}\end{center} 14583\begin{center}{\tt O= [[-1,1,0],[-1,-1,2],[2,2,2]]}\end{center} 14584Input :\\ 14585{\tt M:=[[3,2,1],[1,2,3],[2,3,1]]}\\ 14586Properties :\\ 14587{\tt A*M==S} and {\tt L*O==S} 14588 14589\section{Quadratic forms} 14590\subsection{Matrix of a quadratic form : {\tt q2a}}\index{q2a} 14591\noindent{\tt q2a} takes two arguments : the symbolic expression 14592of a quadratic form $q$ and a 14593vector of variable names.\\ 14594{\tt q2a} returns the matrix $A$ of $q$.\\ 14595Input : 14596\begin{center}{\tt q2a(2*x*y,[x,y])}\end{center} 14597Output : 14598\begin{center}{\tt [[0,1],[1,0]]}\end{center} 14599 14600\subsection{Transform a matrix into a quadratic form : {\tt a2q}}\index{a2q} 14601\noindent{\tt a2q} takes two arguments : the symmetric matrix $A$ 14602of a quadratic 14603form $q$ and a vector of variable names of the same size.\\ 14604{\tt a2q} returns the symbolic expression of the quadratic form $q$.\\ 14605Input : 14606\begin{center}{\tt a2q([[0,1],[1,0]],[x,y])}\end{center} 14607Output : 14608\begin{center}{\tt 2*x*y}\end{center} 14609Input : 14610\begin{center}{\tt a2q([[1,2],[2,4]],[x,y]) }\end{center} 14611Output : 14612\begin{center}{\tt x\verb|^|2+4*x*y+4*y\verb|^|2}\end{center} 14613 14614\subsection{Reduction of a quadratic form : {\tt gauss}}\index{gauss} 14615\noindent{\tt gauss} takes two arguments : a symbolic expression 14616representing a quadratic form $q$ and a 14617vector of variable names.\\ 14618{\tt gauss} returns $q$ written as sum or difference of squares 14619using Gauss algorithm.\\ 14620Input : 14621\begin{center}{\tt gauss(2*x*y,[x,y])}\end{center} 14622Output : 14623\begin{center}{\tt (y+x)\verb|^|2/2+(-(y-x)\verb|^|2)/2}\end{center} 14624 14625\subsection{Gram-Schmidt orthonormalization : {\tt gramschmidt}}\index{gramschmidt} 14626\noindent{\tt gramschmidt} takes one or two arguments : 14627\begin{itemize} 14628\item a matrix viewed as a list of row vectors, 14629the scalar product being the canonical 14630 scalar product, or 14631\item a list of elements 14632that is a basis of a vector subspace, and a function that defines a scalar 14633product on this vector space. 14634\end{itemize} 14635{\tt gramschmidt} returns an orthonormal basis for this scalar product.\\ 14636Input : 14637\begin{center}{\tt normal(gramschmidt([[1,1,1],[0,0,1],[0,1,0]]))}\end{center} 14638Or input : 14639\begin{center}{\tt normal(gramschmidt([[1,1,1],[0,0,1],[0,1,0]],dot))}\end{center} 14640Output : 14641\begin{center}{\tt 14642[[(sqrt(3))/3,(sqrt(3))/3,(sqrt(3))/3],[(-(sqrt(6)))/6,}\\ 14643 {\tt (-(sqrt(6)))/6,(sqrt(6))/3],[(-(sqrt(2)))/2,(sqrt(2))/2,0]]}\end{center} 14644 {\bf Example}\\ 14645We define a scalar product on the vector space of 14646polynomials by: 14647$$P\cdot Q=\int_{-1}^1P(x) Q(x)dx $$ 14648Input : 14649 \begin{center}{\tt gramschmidt([1,1+x],(p,q)->integrate(p*q,x,-1,1))}\end{center} 14650Or define the function {\tt p\_scal}, input :\\ 14651{\tt p\_scal(p,q):=integrate(p*q,x,-1,1)}\\ 14652then input : 14653\begin{center}{\tt gramschmidt([1,1+x],p\_scal)}\end{center} 14654Output : 14655\begin{center}{\tt [1/(sqrt(2)),(1+x-1)/sqrt(2/3)]}\end{center} 14656 14657\subsection{Graph of a conic : {\tt conique}}\index{conique} 14658\noindent{\tt conique} takes as argument the equation of a conic with 14659respect to $x,y$. You may also specify the names of the variables as 14660second and third arguments or as a vector as second argument.\\ 14661{\tt conique} draws this conic.\\ 14662Input : 14663\begin{center}{\tt conique(2*x\verb|^|2+2*x*y+2*y\verb|^|2+6*x)}\end{center} 14664Output : 14665\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} 14666{\bf Remark} :\\ 14667See also {\tt conique\_reduite} for the parametric equation of the conic. 14668 14669\subsection{Conic reduction : {\tt conique\_reduite}}\index{conique\_reduite} 14670\noindent{\tt conique\_reduite} takes two arguments : the equation of a conic 14671and a vector of variable names.\\ 14672{\tt conique\_reduite} returns a list whose elements are: 14673\begin{itemize} 14674\item the origin of the conic, 14675\item the matrix of a basis in which the conic is reduced, 14676\item 0 or 1 (0 if the conic is degenerate), 14677\item the reduced equation of the conic 14678\item a vector of its parametric equations. 14679\end{itemize} 14680Input : 14681\begin{center}{\tt conique\_reduite(2*x\verb|^|2+2*x*y+2*y\verb|^|2+5*x+3,[x,y])}\end{center} 14682Output : 14683\begin{center}{\tt [[-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]]]}\end{center} 14684Which means that the conic is not degenerate, its reduced equation is 14685\[3x^2+y^2-7/6=0 \] 14686its origin is $-5/3+5*i/6$, its axes are 14687parallel to the vectors $(-1,1)$ and $(-1,-1)$. 14688Its parametric equation is 14689\[ \displaystyle \frac{-10+5*i}{6}+ 14690\frac{(1+i)}{\sqrt 2}*\frac{(\sqrt{14}*cos(t)+i*\sqrt{42}*sin(t))}{6} 14691\] 14692where the suggested parameter values for drawing are 14693$t$ from 0 to $2\pi$ with {\tt tstep}= $2\pi/60$. 14694 14695{\bf Remark} :\\ 14696Note that if the conic is degenerate and is made of 1 or 2 line(s), 14697the lines are not given by 14698their parametric equation but by the list of two points of the line.\\ 14699Input : 14700\begin{center}{\tt conique\_reduite(x\verb|^|2-y\verb|^|2+3*x+y+2)}\end{center} 14701Output : 14702\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} 14703 14704\subsection{Graph of a quadric : {\tt quadrique}}\index{quadrique} 14705\noindent{\tt quadrique} takes as arguments the expression of a 14706quadric with respect to $x,y,z$. You may also specify the variables 14707as a vector (second argument) or as second, third and fourth arguments.\\ 14708{\tt quadrique} draws this quadric.\\ 14709Input : 14710\begin{center}{\tt quadrique(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} 14711Output : 14712\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} 14713See also {\tt quadrique\_reduite} for 14714the parametric equation of the quadric. 14715 14716\subsection{Quadric reduction : {\tt quadrique\_reduite}}\index{quadrique\_reduite} 14717\noindent{\tt quadrique\_reduite} takes two arguments : the equation of a 14718quadric and a vector of variable names.\\ 14719{\tt quadrique\_reduite} returns a list whose elements are: 14720\begin{itemize} 14721\item the origin, 14722\item the matrix of a basis where the quadric is reduced, 14723\item 0 or 1 (0 if the quadric is degenerate), 14724\item the reduced equation of the quadric 14725\item a vector with its parametric equations. 14726\end{itemize} 14727{\bf Warning !} 14728{\tt u,v} will be used as parameters of the parametric equations : 14729these variables should not be assigned ({\tt purge} them before 14730calling {\tt quadrique\_reduite}).\\ 14731Input : 14732\begin{center}{\tt quadrique\_reduite(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} 14733Output is a list containing : 14734\begin{itemize} 14735\item The origin (center of symmetry) of the quadric 14736\begin{center}{\tt [11/27,(-26)/27,(-29)/54],}\end{center} 14737\item The matrix of the basis change: 14738\begin{center}{\tt [[(sqrt(6))/3,(sqrt(5))/5,(-(sqrt(30)))/15], 14739 [(sqrt(6))/6,0,(sqrt(30))/6], 14740 [(-(sqrt(6)))/6,(2*sqrt(5))/5,(sqrt(30))/30]],}\end{center} 14741\item 1 hence the quadric is not degenerated 14742\item the reduced equation of the quadric : 14743\begin{center}{\tt 14744 0,9*x\verb|^|2+3*y\verb|^|2+3*z\verb|^|2+(-602)/27,}\end{center} 14745\item 14746The parametric equations (in the original frame) are : 14747\begin{center}{\tt [[(sqrt(6)*sqrt(602/243)*sin(u)*cos(v))/3+ 14748 (sqrt(5)*sqrt(602/81)*sin(u)*sin(v))/5+ 14749 ((-(sqrt(30)))*sqrt(602/81)*cos(u))/15+11/27, 14750 (sqrt(6)*sqrt(602/243)*sin(u)*cos(v))/6+ 14751 (sqrt(30)*sqrt(602/81)*cos(u))/6+(-26)/27, 14752 ((-(sqrt(6)))*sqrt(602/243)*sin(u)*cos(v))/6+ 14753 (2*sqrt(5)*sqrt(602/81)*sin(u)*sin(v))/5+ 14754 (sqrt(30)*sqrt(602/81)*cos(u))/30+(-29)/54], 14755 u=(0 .. pi),v=(0.. (2*pi)),ustep=(pi/20), 14756 vstep=((2*pi)/20)]]}\end{center} 14757\end{itemize} 14758Hence the quadric is an ellipsoid and its reduced equation is : 14759\[ 9*x^2+3*y^2+3*z^2+(-602)/27 = 0\] 14760after the change of origin $[11/27,(-26)/27,(-29)/54]$, 14761the matrix of basis change {\tt P} is : 14762\[ \left[ 14763\begin{array}{ccc} 14764\displaystyle \frac{\sqrt 6}{3} & \displaystyle\frac{\sqrt 5}{5} & \displaystyle-\frac{\sqrt{30}}{15}\\ 14765\displaystyle \frac{\sqrt 6}{6} & 0 & \displaystyle \frac{\sqrt{30}}{6}\\ 14766\displaystyle -\frac{\sqrt 6}{6} & \displaystyle \frac{2\sqrt{5}}{5} & \displaystyle \frac{\sqrt{30}}{30}\\ 14767\end{array} 14768\right] \] 14769Its parametric equation is : 14770\[ \left\{ 14771\begin{array}{l} 14772x =\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}\\ 14773y =\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}\\ 14774z =\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} 14775\end{array} 14776\right. 14777\] 14778{\bf Remark} :\\ 14779Note that if the quadric is degenerate and made of 1 or 2 plane(s), 14780each plane is not given by 14781its parametric equation but by the list of a point of the plane 14782and of a normal vector to the plane.\\ 14783Input : 14784\begin{center}{\tt quadrique\_reduite(x\verb|^|2-y\verb|^|2+3*x+y+2)}\end{center} 14785Output : 14786\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} 14787 14788 \section{Multivariate calculus}\label{sec:plusvar} 14789\subsection{Gradient : {\tt derive deriver diff grad}}\index{derive}\index{diff}\index{grad}\index{deriver}\label{sec:derive}\index{solve}\index{resoudre} 14790\noindent{\tt derive} (or {\tt diff} or {\tt grad}) takes two arguments : an 14791expression $F$ of $n$ real variables and a vector of these variable names.\\ 14792{\tt derive} returns the gradient of $F$, 14793where the gradient is the vector of all partial derivatives, 14794for example in dimension $n=3$ 14795\[ \overrightarrow{\mbox{grad}}(F)= [\frac{\partial F}{\partial x},\frac{\partial F}{\partial y},\frac{\partial F}{\partial z}] \] 14796{\bf Example} \\ 14797Find the gradient of $F(x,y,z)=2x^2y-xz^3$.\\ 14798Input : 14799\begin{center}{\tt derive(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z])}\end{center} 14800or : 14801\begin{center}{\tt diff(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z])}\end{center} 14802or : 14803\begin{center}{\tt grad(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z])}\end{center} 14804Output : 14805\begin{center}{\tt [2*2*x*y-z\verb|^|3,2*x\verb|^|2,-(x*3*z\verb|^|2)]}\end{center} 14806Output after simplification with {\tt normal(ans())} : 14807\begin{center}{\tt [4*x*y-z\verb|^|3,2*x\verb|^|2,-(3*x*z\verb|^|2)]}\end{center} 14808To find the critical points of 14809$F(x,y,z)=2x^2y-xz^3$, input : 14810\begin{center}{\tt solve(derive(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z]),[x,y,z])}\end{center} 14811Output : 14812\begin{center}{\tt [[0,y,0]]}\end{center} 14813 14814\subsection{Laplacian : {\tt laplacian}}\index{laplacian} 14815\noindent{\tt laplacian} takes two arguments : an 14816expression $F$ of $n$ real variables and a vector of these variable names.\\ 14817{\tt laplacian} returns the Laplacian of$F$, that is the sum of all second 14818partial derivatives, for example in dimension $n=3$: 14819\[ \nabla^2(F)=\frac{\partial^2 F}{\partial x^2}+\frac{\partial^2 F}{\partial y^2}+\frac{\partial^2 F}{\partial z^2} \] 14820{\bf Example}\\ 14821Find the Laplacian of $F(x,y,z)=2x^2y-xz^3$.\\ 14822Input : 14823\begin{center}{\tt laplacian(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z])}\end{center} 14824Output : 14825\begin{center}{\tt 4*y+-6*x*z}\end{center} 14826 14827\subsection{Hessian matrix : {\tt hessian}}\index{hessian} 14828\noindent{\tt hessian} takes two arguments : an 14829expression $F$ of $n$ real variables and a vector of these variable names.\\ 14830{\tt hessian} returns the hessian matrix of $F$, that is the matrix of the 14831derivatives of order 2.\\ 14832{\bf Example}\\ 14833Find the hessian matrix of $F(x,y,z)=2x^2y-xz^3$.\\ 14834Input : 14835\begin{center}{\tt hessian(2*x\verb|^|2*y-x*z\verb|^|3 , [x,y,z])}\end{center} 14836Output : 14837\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} 14838To have the hessian matrix at the critical points, first input : 14839\begin{center}{\tt solve(derive(2*x\verb|^|2*y-x*z\verb|^|3,[x,y,z]),[x,y,z])}\end{center} 14840Output is the critical points : 14841\begin{center}{\tt [[0,y,0]]}\end{center} 14842Then, to have the hessian matrix at this points, input : 14843\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} 14844Output : 14845\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} 14846and after simplification : 14847\begin{center}{\tt [[4*y,0,0],[0,0,0],[0,0,0]]}\end{center} 14848 14849\subsection{Divergence : {\tt divergence}}\index{divergence} 14850\noindent{\tt divergence} takes two arguments : a vector 14851field of dimension $n$ depending on $n$ real variables.\\ 14852{\tt divergence} returns the divergence of $F$ that is the sum 14853of the derivative of the $k$-th component with respect 14854to the $k$-th variable. For example in dimension $n=3$: 14855\begin{center} 14856 {\tt divergence([A,B,C],[x,y,z])}=$\displaystyle\frac{\partial A}{\partial x}+\frac{\partial B}{\partial y}+\frac{\partial C}{\partial z}$ 14857\end{center} 14858Input : 14859\begin{center}{\tt divergence([x*z,-y\verb|^|2,2*x\verb|^|y],[x,y,z])}\end{center} 14860Output : 14861\begin{center}{\tt z+-2*y}\end{center} 14862 14863\subsection{Rotational : {\tt curl}}\index{curl} 14864\noindent{\tt curl} takes two arguments : a 3-d vector field 14865depending on 3 variables.\\ 14866{\tt curl} returns the rotational of the vector, defined by: 14867\begin{center} 14868{\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}]$ 14869\end{center} 14870Note that $n$ {\bf must be equal to 3}.\\ 14871Input : 14872\begin{center}{\tt curl([x*z,-y\verb|^|2,2*x\verb|^|y],[x,y,z])}\end{center} 14873Output : 14874\begin{center}{\tt [2*x\verb|^|y*log(x),x-2*y*x\verb|^|(y-1),0]}\end{center} 14875 14876\subsection{Potential : {\tt potential}}\index{potential} 14877\noindent{\tt potential} takes two arguments : a vector field 14878$\overrightarrow V$ in $R^n$ with respect to $n$ real variables 14879and the vector of these variable names.\\ 14880{\tt potential} returns, if it is possible, a function $U$ such that 14881$\overrightarrow{\mbox{grad}}(U)=\overrightarrow V$. When it is possible, we 14882say that $\overrightarrow V$ derives the potential $U$, and 14883$U$ is defined up to a constant.\\ 14884{\tt potential} is the reciprocal function of {\tt derive}.\\ 14885Input : 14886\begin{center}{\tt potential([2*x*y+3,x\verb|^|2-4*z,-4*y],[x,y,z])}\end{center} 14887Output : 14888\begin{center}{\tt 2*y*x\verb|^|2/ 148892+3*x+(x\verb|^|2-4*z-2*x\verb|^|2/2)*y}\end{center} 14890Note that in $\R^3$ 14891a vector $\overrightarrow V$ is a gradient if and only if its 14892rotational is zero i.e. if {\tt curl(V)=0}. 14893In time-independent electro-magnetism, 14894$\overrightarrow V$=$\overrightarrow E$ is the 14895electric field and $U$ is the electric potential. 14896 14897\subsection{Conservative flux field : {\tt vpotential}}\index{vpotential} 14898\noindent{\tt vpotential} takes two arguments : a vector field 14899$\overrightarrow V$ 14900in $R^n$ with respect to $n$ real variables 14901and the vector of these variable names.\\ 14902{\tt vpotential} returns, if it is possible, a vector $\overrightarrow U$ such 14903that $\overrightarrow{\mbox{curl}}(\overrightarrow U)=\overrightarrow V$. 14904When it is possible we say that $\overrightarrow V$ is a conservative flux 14905field or a solenoidal field. 14906The general solution is the sum of a particular solution and of the 14907gradient of an arbitrary function, {\tt Xcas} returns a particular 14908solution with zero as first component.\\ 14909{\tt vpotential} is the reciprocal function of {\tt curl}.\\ 14910Input : 14911\begin{center}{\tt vpotential([2*x*y+3,x\verb|^|2-4*z,-2*y*z],[x,y,z]) }\end{center} 14912Output :~ 14913\begin{center}{\tt [0,(-(2*y))*z*x,-x\verb|^|3/3-(-(4*z))*x+3*y]}\end{center} 14914In $\R^3$, a vector field $\overrightarrow V$ is a rotational 14915if and only if its 14916divergence is zero \\({\tt divergence(V,[x,y,z])=0}). 14917In time-independent electro-magnetism, 14918$\overrightarrow V$= $\overrightarrow B$ is the magnetic field and 14919$\overrightarrow U$= $\overrightarrow A$ is the potential vector. 14920 14921\section{Equations} 14922\subsection{Define an equation : {\tt equal}}\index{equal} 14923\noindent{\tt equal} takes as argument the two members of an equation.\\ 14924{\tt equal} returns this equation. It is the prefixed version of {\tt =}\\ 14925Input : 14926\begin{center}{\tt equal(2x-1,3)}\end{center} 14927Output : 14928\begin{center}{\tt (2*x-1)=3}\end{center} 14929We can also directly write {\tt (2*x-1)=3}. 14930 14931\subsection{Transform an equation into a difference : {\tt equal2diff}}\index{equal2diff} 14932\noindent{\tt equal2diff} takes as argument an equation.\\ 14933{\tt equal2diff} returns the difference of the two members of this equation.\\ 14934Input : 14935\begin{center}{\tt equal2diff(2x-1=3)}\end{center} 14936Output : 14937\begin{center}{\tt 2*x-1-3}\end{center} 14938 14939\subsection{Transform an equation into a list : {\tt equal2list}}\index{equal2list} 14940\noindent{\tt equal2list} takes as argument an equation.\\ 14941{\tt equal2list} returns the list of the two members of this equation.\\ 14942Input : 14943\begin{center}{\tt equal2list(2x-1=3)}\end{center} 14944Output : 14945\begin{center}{\tt [2*x-1,3]}\end{center} 14946 14947\subsection{The left member of an equation : {\tt left gauche lhs}}\index{left|textbf}\index{lhs|textbf}\index{gauche|textbf} 14948\noindent{\tt left} or {\tt lhs} takes as argument an equation or an 14949interval.\\ 14950{\tt left} or {\tt lhs} returns the left member of this equation or the left 14951bound of this interval.\\ 14952Input : 14953\begin{center}{\tt left(2x-1=3)}\end{center} 14954Or input: 14955\begin{center}{\tt lhs(2x-1=3)}\end{center} 14956Output : 14957\begin{center}{\tt 2*x-1}\end{center} 14958Input : 14959\begin{center}{\tt left(1..3)}\end{center} 14960Or input: 14961\begin{center}{\tt lhs(1..3)}\end{center} 14962Output : 14963\begin{center}{\tt 1}\end{center} 14964 14965\subsection{The right member of an equation : {\tt right droit rhs}}\index{right|textbf}\index{rhs|textbf} \index{droit|textbf} 14966\noindent{\tt right} or {\tt rhs} takes as argument an equation or an 14967interval.\\ 14968{\tt right} or {\tt rhs} returns the right member of this equation or the 14969right bound of this interval.\\ 14970Input : 14971\begin{center}{\tt right(2x-1=3)}\end{center} 14972or : 14973\begin{center}{\tt rhs(2x-1=3)}\end{center} 14974Output : 14975\begin{center}{\tt 3}\end{center} 14976Input : 14977\begin{center}{\tt right(1..3)}\end{center} 14978or : 14979\begin{center}{\tt rhs(1..3)}\end{center} 14980Output : 14981\begin{center}{\tt 3}\end{center} 14982 14983\subsection{Solving equation(s): {\tt solve}}\index{solve|textbf} 14984\noindent{\tt solve} solves an equation or a system of polynomial 14985equations. It takes 2 arguments: 14986\begin{itemize} 14987\item Solving an equation\\ 14988{\tt solve} takes as arguments an equation between two expressions or an 14989expression ({\tt =0} is omitted), and a variable name (by default {\tt x}).\\ 14990{\tt solve} solves this equation. 14991\item Solving a system of polynomial equations\\ 14992{\tt solve} takes as arguments two vectors : 14993a vector of polynomial equations and a 14994vector of variable names. \\ 14995{\tt solve} solves this polynomial equation system. 14996\end{itemize} 14997{\bf Remarks}: 14998\begin{itemize} 14999\item In real mode, {\tt solve} returns only real solutions. To have 15000the complex solutions, switch to complex mode, e.g. by checking 15001{\tt Complex} in the cas configuration, or use the {\tt cSolve} 15002command. 15003\item 15004For trigonometric equations, {\tt solve} returns by default the principal 15005solutions. To have all the solutions check {\tt All\_trig\_sol} in the cas 15006configuration. 15007\end{itemize} 15008{\bf Examples} : 15009\begin{itemize} 15010\item Solve $x^4-1=3$\\ 15011 Input : 15012\begin{center}{\tt solve(x\verb|^|4-1=3)}\end{center} 15013Output in real mode : 15014\begin{center}{\tt [sqrt(2),-(sqrt(2))]}\end{center} 15015Output in complex mode : 15016\begin{center}{\tt [sqrt(2),-(sqrt(2)),(i)*sqrt(2),-((i)*sqrt(2))]}\end{center} 15017\item Solve $\exp(x)=2$ \\ 15018Input : 15019\begin{center}{\tt solve(exp(x)=2)}\end{center} 15020Output in real mode : 15021\begin{center}{\tt [log(2)]}\end{center} 15022\item Find $x,y$ such that $x+y=1,x-y=0$\\ 15023 Input : 15024\begin{center}{\tt solve([x+y=1,x-y],[x,y])}\end{center} 15025Output : 15026\begin{center}{\tt [[1/2,1/2]] }\end{center} 15027\item Find $x,y$ such that $x^2+y=2,x+y^2=2$\\ 15028Input : 15029\begin{center}{\tt solve([x\verb|^|2+y=2,x+y\verb|^|2=2],[x,y])}\end{center} 15030Output : 15031\begin{center}{\tt [[-2,-2],[1,1],[(-sqrt(5)+1)/2,(1+sqrt(5))/2],}\end{center} 15032\begin{center}{\tt [(sqrt(5)+1)/2,(1-sqrt(5))/2]] }\end{center} 15033\item Find $x,y,z$ such that $x^2-y^2=0,x^2-z^2=0$\\ 15034Input : 15035\begin{center}{\tt solve([x\verb|^|2-y\verb|^|2=0,x\verb|^|2-z\verb|^|2=0],[x,y,z])}\end{center} 15036Output : 15037\begin{center}{\tt [[x,x,x],[x,-x,-x],[x,-x,x],[x,x,-x]]}\end{center} 15038\item Solve $\cos(2*x)=1/2$\\ 15039Input : 15040\begin{center}{\tt solve(cos(2*x)=1/2)}\end{center} 15041Output : 15042\begin{center}{\tt [pi/6,(-pi)/6]}\end{center} 15043Output with {\tt All\_trig\_sol} checked : 15044\begin{center}{\tt [(6*pi*n\_0+pi)/6,(6*pi*n\_0-pi)/6]}\end{center} 15045\item 15046Find the intersection of a straight line 15047(given by a list of equations) and a plane.\\ For example, 15048let $D$ be the straight line of cartesian equations 15049$[y-z=0,z-x=0]$ and let $P$ the plane of equation $x-1+y+z=0$. 15050Find the intersection of $D$ and $P$.\\ 15051Input : 15052\begin{center}{\tt solve([[y-z=0,z-x=0],x-1+y+z=0],[x,y,z])}\end{center} 15053Output : 15054\begin{center}{\tt [[1/3,1/3,1/3]]}\end{center} 15055\end{itemize} 15056 15057\subsection{Equation solving in $\mathbb C$ : {\tt cSolve}}\index{cSolve} 15058\noindent{\tt cSolve} takes two arguments and solves an equation or a system 15059of polynomial equations. 15060\begin{itemize} 15061\item solving an equation\\ 15062{\tt cSolve} takes as arguments an equation between two expressions or an 15063expression ({\tt =0} is omitted), and a variable name (by default {\tt x}).\\ 15064{\tt cSolve} solves this equation in $\mathbb C$ even if you are in 15065real mode. 15066\item solving a system of polynomial equations\\ 15067{\tt cSolve} takes as arguments two vectors : a vector of polynomial equations 15068and a vector of variable names. \\ 15069{\tt cSolve} solves this equation system in $\mathbb C$ even if you are in 15070real mode. 15071\end{itemize} 15072Input : 15073\begin{center}{\tt cSolve(x\verb|^|4-1=3)}\end{center} 15074Output : 15075\begin{center}{\tt [sqrt(2),-(sqrt(2)),(i)*sqrt(2),-((i)*sqrt(2))]}\end{center} 15076Input : 15077\begin{center}{\tt cSolve([-x\verb|^|2+y=2,x\verb|^|2+y],[x,y])}\end{center} 15078Output : 15079\begin{center}{\tt [[i,1],[-i,1]]}\end{center} 15080 15081 15082\section{Linear systems} 15083In this paragraph, we call the "augmented matrix" of the system 15084$A \cdot X=B$ (or matrix "representing" the system $A \cdot X=B$), 15085the matrix obtained by gluing the column vector $B$ or $-B$ 15086to the right of the matrix $A$, as with {\tt border(A,tran(B))}. 15087 15088\subsection{Matrix of a system : {\tt syst2mat}}\index{syst2mat} 15089\noindent{\tt syst2mat} takes two vectors as arguments. The components of the 15090first vector are the equations of a linear system and the components of the 15091second vector are the variable names.\\ 15092{\tt syst2mat} returns the augmented matrix of the system $AX=B$, 15093obtained by gluing the column vector $-B$ 15094to the right of the matrix $A$.\\ 15095Input : 15096\begin{center}{\tt syst2mat([x+y,x-y-2],[x,y])}\end{center} 15097Output : 15098\begin{center}{\tt [[1,1,0],[1,-1,-2]]}\end{center} 15099Input : 15100\begin{center}{\tt syst2mat([x+y=0,x-y=2],[x,y])}\end{center} 15101Output : 15102\begin{center}{\tt [[1,1,0],[1,-1,-2]]}\end{center} 15103{\bf Warning !!!}\\ 15104The variables (here {\tt x} and {\tt y}) must be purged. 15105 15106\subsection{Gauss reduction of a matrix : {\tt ref}}\index{ref}\label{ref} \label{sec:ref} 15107\noindent{\tt ref} is used to solve a linear system of equations written in 15108matrix form: 15109 \begin{center}{\tt A*X=B}\end{center} 15110The argument of {\tt ref} is the augmented matrix of the system 15111(the matrix obtained by augmenting the matrix {\tt A} to the right with 15112the column vector {\tt B}).\\ 15113The result is a matrix {\tt [A1,B1]} where {\tt A1} has zeros 15114under its principal diagonal, and the solutions of: 15115\begin{center}{\tt A1*X=B1}\end{center} 15116are the same as the solutions of: 15117\begin{center}{\tt A*X=B}\end{center} 15118 15119For example, solve the system : 15120\[ \left \{ 15121\begin{array}{lcr} 3x + y & = &-2 \\3x +2y & =& 2 \end{array}\right. 15122\] 15123Input : 15124\begin{center}{\tt ref([[3,1,-2],[3,2,2]])}\end{center} 15125Output : 15126\begin{center}{\tt [[1,1/3,-2/3],[0,1,4]]}\end{center} 15127Hence the solution is $y=4$ (last row) and $x=-2$ (substitute $y$ 15128in the first row). 15129 15130\subsection{Gauss-Jordan reduction: {\tt rref gaussjord}}\index{rref|textbf}\index{gaussjord|textbf}\label{sec:rref} 15131\noindent{\tt rref} solves a linear system of equations written in 15132matrix form (see also \ref{sec:rrefm}) : 15133 \begin{center}{\tt A*X=B}\end{center} 15134{\tt rref} takes one or two arguments. 15135\begin{itemize} 15136\item 15137If {\tt rref} has only one argument, this argument is the augmented matrix 15138of the system (the matrix obtained by augmenting matrix {\tt A} to the 15139right with the column vector {\tt B}).\\ 15140The result is a matrix {\tt [A1,B1]} : {\tt A1} has zeros both above and under 15141its principal diagonal and has 1 on its principal diagonal, and the solutions 15142of: 15143\begin{center}{\tt A1*X=B1}\end{center} 15144are the same as : 15145\begin{center}{\tt A*X=B}\end{center} 15146For example, to solve the system: 15147\[ 15148\left \{ 15149\begin{array}{lcr} 3x + y & = &-2 \\3x +2y & =& 2 \end{array}\right. 15150\] 15151Input : 15152\begin{center}{\tt rref([[3,1,-2],[3,2,2]])}\end{center} 15153Output : 15154\begin{center}{\tt [[1,0,-2],[0,1,4]]}\end{center} 15155Hence $x=-2$ and $y=4$ is the solution of this system. 15156 15157\noindent{\tt rref} can also solve several linear systems 15158of equations having the same first member. 15159We write the second members as a column matrix.\\ 15160Input : 15161\begin{center}{\tt rref([[3,1,-2,1],[3,2,2,2]])}\end{center} 15162Output : 15163\begin{center}{\tt [[1,0,-2,0],[0,1,4,1]]}\end{center} 15164Which means that ($x=-2$ and $y=4$) is the solution of the system 15165$$\left \{ 15166\begin{array}{lcr} 3x + y & = &-2 \\3x +2y & =& 2 \end{array}\right.$$ 15167and ($x=0$ and $y=1$) is the solution of the system 15168$$\left \{ 15169\begin{array}{lcr} 3x + y & = &1 \\3x +2y & =& 2 \end{array}\right.$$ 15170\item 15171If {\tt rref} has two parameters, the second parameter must be an integer 15172$k$, and the Gauss-Jordan reduction will be performed on (at most) 15173the first $k$ columns.\\ 15174Input : 15175\begin{center}{\tt rref([[3,1,-2,1],[3,2,2,2]],1)}\end{center} 15176Output : 15177\begin{center}{\tt [[3,1,-2,1],[0,1,4,1]]}\end{center} 15178\end{itemize} 15179 15180\subsection{Solving A*X=B : {\tt simult}}\index{simult} 15181\noindent{\tt simult} is used to solve a linear system of equations (resp. 15182several linear systems of equations with the same matrix {\tt A}) written 15183in matrix form (see also \ref{sec:rrefm}) : 15184\begin{center}{\tt A*X=b (resp. A*X=B)}\end{center} 15185{\tt simult} takes as arguments the matrix {\tt A} of the system and the 15186column vector (i.e. a one column matrix) {\tt b} of the second 15187member of the system (resp. 15188the matrix {\tt B} whose columns are the 15189vectors {\tt b} of the second members of the different systems).\\ 15190The result is a column vector solution of the system (resp. a matrix 15191whose columns are the solutions of the different systems).\\ 15192For example, to solve the system : 15193$$\left \{ 15194\begin{array}{lcr} 3x + y & = &-2 \\3x +2y & =& 2 \end{array}\right.$$ 15195Input : 15196\begin{center}{\tt simult([[3,1],[3,2]],[[-2],[2]])}\end{center} 15197Output : 15198\begin{center}{\tt [[-2],[4]]}\end{center} 15199Hence $x=-2$ and $y=4$ is the solution.\\ 15200Input : 15201\begin{center}{\tt simult([[3,1],[3,2]],[[-2,1],[2,2]])}\end{center} 15202Output : 15203\begin{center}{\tt [[-2,0],[4,1]]}\end{center} 15204Hence $x=-2$ and $y=4$ is the solution of 15205$$\left \{ 15206\begin{array}{lcr} 3x + y & = &-2 \\3x +2y & =& 2 \end{array}\right.$$ 15207whereas $x=0$ and $y=1$ is the solution of 15208$$\left \{ 15209\begin{array}{lcr} 3x + y & = &1 \\3x +2y & =& 2 \end{array}\right.$$ 15210 15211\subsection{Step by step Gauss-Jordan reduction of a matrix : {\tt pivot}}\index{pivot}\label{sec:pivot} 15212\noindent{\tt pivot} takes three arguments : a matrix with $n$ rows and $p$ 15213columns and two integers $l$ and $c$ such that $0\leq l<n$, $0\leq c<p$ 15214and $A_{l,c}\neq 0$.\\ 15215{\tt pivot(A,l,c)} performs one step of the Gauss-Jordan method 15216using {\tt A[l,c]} as pivot and returns an equivalent matrix 15217with zeros in the column {\tt c} of {\tt A} (except at row $l$).\\ 15218Input : 15219\begin{center}{\tt pivot([[1,2],[3,4],[5,6]],1,1)}\end{center} 15220Output : 15221\begin{center}{\tt [[-2,0],[3,4],[2,0]]}\end{center} 15222Input : 15223\begin{center}{\tt pivot([[1,2],[3,4],[5,6]],0,1)}\end{center} 15224Output : 15225\begin{center}{\tt [[1,2],[2,0],[4,0]]}\end{center} 15226 15227\subsection{Linear system solving: {\tt linsolve}}\index{linsolve} 15228\noindent{\tt linsolve} is used to solve a system of linear equations.\\ 15229{\tt linsolve} has two arguments: a list of equations or 15230expressions (in that case the convention is that the equation 15231is $expression = 0$), and a list of variable names.\\ 15232{\tt linsolve} returns the solution of the system in a list.\\ 15233Input : 15234\begin{center}{\tt linsolve([2*x+y+z=1,x+y+2*z=1,x+2*y+z=4],[x,y,z])}\end{center} 15235Output : 15236\begin{center}{\tt [1/-2,5/2,1/-2]}\end{center} 15237Which means that 15238\[ x=-\frac{1}{2}, y=\frac{5}{2}, z=-\frac{1}{2} \] 15239is the solution of the system : 15240$$\left\{ 15241\begin{array}{rl} 152422x+y+z &=1\\ 15243x+y+2z &=1\\ 15244x+2y+z &=4 15245\end{array} 15246\right.$$ 15247 15248\subsection{Finding linear recurrences : {\tt reverse\_rsolve}}\index{reverse\_rsolve} 15249\noindent{\tt reverse\_rsolve} takes as argument a vector 15250$v=[v_0...v_{2n-1}]$ made of the first $2n$ terms of a sequence $(v_n)$ 15251which is supposed to verify a linear recurrence relation of 15252degree smaller than $n$ 15253\[ x_n*v_{n+k}+...+x_0*v_k=0 \] 15254where the $x_j$ are $n+1$ unknowns.\\ 15255{\tt reverse\_rsolve} returns the list $x=[x_n,...,x_0]$ 15256of the $x_j$ coefficients (if $x_n\neq 0$ it is reduced to 1). 15257 15258In other words {\tt reverse\_rsolve} solves the linear system of 15259 $n$ equations : 15260\begin{eqnarray*} 15261x_n*v_{n}+...+x_0*v_0 &=&0 \\ 15262...\\ 15263x_n*v_{n+k}+...+x_0*v_k &=&0 \\ 15264...\\ 15265x_n*v_{2*n-1}+...+x_0*v_{n-1}&=&0 15266\end{eqnarray*} 15267The matrix $A$ of the system has $n$ rows and $n+1$ columns : 15268\[ A=[[v_0,v_1...v_n],[v_1,v_2,...v_{n-1}],...,[v_{n-1},v_n...v_{2n-1}]] \] 15269{\tt reverse\_rsolve} returns the list $x=[x_n,...x_1,x_0]$ with $x_n=1$ 15270and $x$ is the solution of the system $A*{\tt revlist}(x)$. 15271 15272{\bf Examples} 15273\begin{itemize} 15274\item Find a sequence satisfying a linear recurrence of degree at 15275most 2 whose first elements 1, -1, 3, 3.\\ 15276Input : 15277\begin{center}{\tt reverse\_rsolve([1,-1,3,3])}\end{center} 15278Output : 15279\begin{center}{\tt [1,-3,-6]}\end{center} 15280Hence $x_0=-6$, $x_1=-3$, $x_2=1$ and the recurrence relation is 15281 \[ v_{k+2} -3v_{k+1} -6 v_k =0\] 15282Without {\tt reverse\_rsolve}, we would write the matrix of the system :\\ 15283{\tt [[1,-1,3],[-1,3,3]]} and use the {\tt rref} command :\\ 15284{\tt rref([[1,-1,3],[-1,3,3]])}\\ 15285Output is {\tt [[1,0,6],[0,1,3]]} hence $x_0=-6$ and $x_1=-3$ 15286(because $x_2=1$). 15287 15288\item Find a sequence satisfying a linear recurrence of degree at 15289most 3 whose first elements are 1, -1, 3, 3,-1, 1.\\ 15290Input : 15291\begin{center}{\tt reverse\_rsolve([1,-1,3,3,-1,1])}\end{center} 15292Output : 15293\begin{center}{\tt [1,(-1)/2,1/2,-1]}\end{center} 15294Hence so, $x_0=-1$, $x_1=1/2$, $x_2=-1/2$, $x_3=1$, the recurrence 15295relation is 15296\[ v_{k+3} -\frac{1}{2} v_{k+2} +\frac{1}{2} v_{k+1} -v_k =0 \] 15297Without {\tt reverse\_rsolve}, we would write the matrix of the system :\\ 15298{\tt [[1,-1,3,3],[-1,3,3,-1],[3,3,-1,1]]}.\\ 15299Using {\tt rref} command, we would input :\\ 15300{\tt rref([[1,-1,3,3],[-1,3,3,-1],[3,3,-1,1]])}\\ 15301Output is {\tt [1,0,0,1],[0,1,0,1/-2],[0,0,1,1/2]]} 15302hence $x_0=-1$, $x_1=1/2$ and $x_2=-1/2$ because $x_3=1$), 15303\end{itemize} 15304 15305\section{Differential equations} 15306This section is limited to symbolic (or exact) solutions of 15307differential equations. 15308For numeric solutions of differential equations, see {\tt odesolve}. 15309For graphic representation of solutions of differential equations, 15310see {\tt plotfield}, {\tt plotode} and {\tt interactive\_plotode}. 15311 15312\subsection{Solving differential equations : {\tt desolve deSolve 15313dsolve}}\index{desolve}\index{deSolve}\index{dsolve} 15314{\tt desolve} (or {\tt deSolve}) can solve : 15315\begin{itemize} 15316\item linear differential equations with constant coefficients, 15317\item first order linear differential equations, 15318\item first order differential equations without $y$, 15319\item first order differential equations without $x$, 15320\item first order differential equations with separable variables, 15321\item first order homogeneous differential equations ($y'=F(y/x)$), 15322\item first order differential equations with integrating factor, 15323\item first order Bernoulli differential equations ($a(x)y'+b(x)y=c(x)y^n$), 15324\item first order Clairaut differential equations ($y=x*y'+f(y')$). 15325\end{itemize} 15326{\tt desolve} takes as arguments : 15327\begin{itemize} 15328\item if the independent variable is the current variable (here supposed 15329to be $x$), 15330\begin{itemize} 15331\item the differential equation (or the list of 15332the differential equation and of the initial conditions) 15333\item the unknown (usually {\tt y}). 15334\end{itemize} 15335In the differential equation, the function $y$ is denoted by $y$, 15336its first derivative $y \prime$ is denoted by 15337${\tt y'}$, and its second derivative $y'{'}$ is written 15338${\tt y''}$.\\ 15339For example {\tt desolve(y''+2*y'+y,y)} or \\ 15340{\tt desolve([y''+2*y'+y,y(0)=1,y'(0)=0],y)}. 15341\item if the independent variable is not the current variable, 15342for example $t$ instead of $x$, 15343\begin{itemize} 15344\item the differential equation (or the list of 15345the differential equation and of the initial conditions), 15346\item the variable, e.g. {\tt t} 15347\item the unknown as a variable {\tt y} or as a function {\tt y(t)}. 15348\end{itemize} 15349In the differential equation, the function $y$ is denoted by $y(t)$, 15350its derivative $y \prime$ is denoted by 15351{\tt diff(y(t),t)}, and its second derivative 15352$y'{'}$ is denoted by {\tt diff(y(t),t\$2)}.\\ 15353For example : \\ 15354{\tt desolve(diff(y(t),t\$2)+2*diff(y(t),t)+y(t),y(t))}; or\\ 15355{\tt desolve(diff(y(t),t\$2)+2*diff(y(t),t)+y(t),t,y)}; 15356and \\ 15357\begin{verbatim} 15358desolve([diff(y(t),t$2)+2*diff(y(t),t)+y(t), 15359 y(0)=1,y'(0)=0],y(t)); or 15360desolve([diff(y(t),t$2)+2*diff(y(t),t)+y(t), 15361 y(0)=1,y'(0)=0],t,y); 15362\end{verbatim} 15363\end{itemize} 15364If there is no initial conditions (or one initial condition for a second 15365order equation), 15366{\tt desolve} returns the general solution in terms of 15367constants of integration 15368{\tt c\_0, c\_1}, where {\tt y(0)=c\_0} and {\tt y'(0)=c\_1}, 15369or a list of solutions.\\ 15370{\bf Examples} 15371\begin{itemize} 15372\item Examples of second linear differential equations with constant 15373coefficients. 15374\begin{enumerate} 15375\item 15376Solve : 15377$$y''+y=\cos (x) $$ 15378Input (typing twice prime for {\tt y''}): 15379\begin{center}{\tt desolve(y''+y=cos(x),y)}\end{center} 15380or input : 15381\begin{center}{\tt desolve((diff(diff(y))+y)=(cos(x)),y)}\end{center} 15382Output : 15383\begin{center}{\tt c\_0*cos(x)+(x+2*c\_1)*sin(x)/2}\end{center} 15384{\tt c\_0, c\_1} are the constants of integration : {\tt y(0)=c\_0} and 15385{\tt y'(0)=c\_1}.\\ 15386If the variable is not {\tt x} but {\tt t}, input : 15387\begin{center} 15388{\tt desolve(derive(derive(y(t),t),t)+y(t)=cos(t),t,y)} 15389\end{center} 15390Output : 15391\begin{center}{\tt c\_0*cos(t)+(t+2*c\_1)/2*sin(t)}\end{center} 15392{\tt c\_0, c\_1} are the constants of integration : {\tt y(0)=c\_0} and 15393{\tt y'(0)=c\_1}. 15394\item 15395Solve : 15396$$y''+y=\cos (x), \; \; y(0)=1 $$ 15397Input : 15398\begin{center}{\tt desolve([y''+y=cos(x),y(0)=1],y)}\end{center} 15399Output : 15400\begin{center}{\tt [cos(x)+(x+2*c\_1)/2*sin(x)]}\end{center} 15401the components of this vector are solutions (here there is just one component, 15402so we have just one solution depending of the constant {\tt c\_1}). 15403\item 15404Solve : 15405$$y''+y=\cos (x) \; \; (y(0))^2=1 $$ 15406Input : 15407\begin{center}{\tt desolve([y''+y=cos(x),y(0)\verb|^|2=1],y)}\end{center} 15408Output : 15409\begin{center}{\tt [-cos(x)+(x+2*c\_1)/2*sin(x),cos(x)+(x+2*c\_1)/2*sin(x)]}\end{center} 15410each component of this list is a solution, 15411we have two solutions depending 15412on the constant {\tt c\_1} ($y'(0)=c_1$) 15413and corresponding to $y(0)=1$ and to $y(0)=-1$. 15414\item 15415Solve : 15416$$y''+y=\cos (x), \; \; (y(0))^2=1 \; \; y'(0)=1$$ 15417Input : 15418\begin{center}{\tt desolve([y''+y=cos(x),y(0)\verb|^|2=1,y'(0)=1],y)} 15419\end{center} 15420Output : 15421\begin{center}{\tt [-cos(x)+(x+2)/2*sin(x),cos(x)+(x+2)/2*sin(x)]}\end{center} 15422each component of this list is a solution (we have two solutions). 15423\item 15424Solve : 15425$$y''+2y'+y=0$$ 15426Input : 15427\begin{center}{\tt desolve(y''+2*y'+y=0,y)}\end{center} 15428Output : 15429\begin{center}{\tt (x*c\_0+x*c\_1+c\_0)*exp(-x)}\end{center} 15430the solution depends of 2 constants of integration : 15431{\tt c\_0, c\_1} ({\tt y(0)=c\_0} and {\tt y'(0)=c\_1}). 15432\item 15433Solve : 15434$$y''-6y'+9y=xe^{3x}$$ 15435Input: 15436\begin{center}{\tt desolve(y''-6*y'+9*y=(x*exp(3*x),y)}\end{center} 15437Output : 15438\begin{center}{\tt (x\verb|^|3+(-(18*x))*c\_0+6*x*c\_1+6*c\_0)*1/6*exp(3*x)}\end{center} 15439the solution depends on 2 constants of integration : 15440{\tt c\_0, c\_1} ({\tt y(0)=c\_0} and {\tt y'(0)=c\_1}). 15441\end{enumerate} 15442\item Examples of first order linear differential equations. 15443\begin{enumerate} 15444\item 15445Solve : 15446$$xy'+y-3x^2=0$$ 15447Input : 15448\begin{center}{\tt desolve(x*y'+y-3*x\verb|^|2,y)}\end{center} 15449Output : 15450\begin{center}{\tt(3*1/3*x\verb|^|3+c\_0)/x }\end{center} 15451\item 15452Solve : 15453$$y'+x*y=0, y(0)=1$$ 15454Input : 15455\begin{center}{\tt desolve([y'+x*y=0, y(0)=1]),y)}\end{center} 15456or : 15457\begin{center}{\tt desolve((y'+x*y=0) \&\& (y(0)=1),y)}\end{center} 15458Output : 15459\begin{center}{\tt [1/(exp(1/2*x\verb|^|2))]}\end{center} 15460\item 15461Solve : 15462$$x(x^2-1)y'+2y=0$$ 15463Input : 15464\begin{center}{\tt desolve(x*(x\verb|^|2-1)*y'+2*y=0,y)}\end{center} 15465Output : 15466\begin{center}{\tt (c\_0)/((x\verb|^|2-1)/(x\verb|^|2))}\end{center} 15467\item 15468Solve : 15469$$x(x^2-1)y'+2y=x^2$$ 15470Input : 15471\begin{center}{\tt desolve(x*(x\verb|^|2-1)*y'+2*y=x\verb|^|2,y)}\end{center} 15472Output : 15473\begin{center}{\tt (ln(x)+c\_0)/((x\verb|^|2-1)/(x\verb|^|2))}\end{center} 15474\item 15475If the variable is $t$ instead of $x$, for example : 15476$$t(t^2-1)y'(t)+2y(t)=t^2$$ 15477Input : 15478\begin{center}{\tt desolve(t*(t\verb|^|2-1)*diff(y(t),t)+2*y(t)=(t\verb|^|2),y(t))}\end{center} 15479Output : 15480\begin{center}{\tt (ln(t)+c\_0)/((t\verb|^|2-1)/(t\verb|^|2))}\end{center} 15481\item 15482Solve : 15483$$x(x^2-1)y'+2y=x^2,y(2)=0$$ 15484Input : 15485\begin{center}{\tt desolve([x*(x\verb|^|2-1)*y'+2*y=x\verb|^|2,y(0)=1],y)}\end{center} 15486Output : 15487\begin{center}{\tt [(ln(x)-ln(2))*1/(x\verb|^|2-1)*x\verb|^|2]}\end{center} 15488\item 15489Solve : 15490$$\sqrt{1+x^2}y'-x-y=\sqrt{1+x^2}$$ 15491Input : 15492\begin{center}{\tt desolve(y'*sqrt(1+x\verb|^|2)-x-y-sqrt(1+x\verb|^|2),y)}\end{center} 15493Output : 15494\begin{center}{\tt (-c\_0+ln(sqrt(x\verb|^|2+1)-x))/(x-sqrt(x\verb|^|2+1))}\end{center} 15495\end{enumerate} 15496 15497\item Examples of first differential equations with separable variables. 15498\begin{enumerate} 15499\item Solve : 15500$$y'=2\sqrt{y}$$ 15501Input : 15502\begin{center}{\tt desolve(y'=2*sqrt(y),y)}\end{center} 15503Output : 15504\begin{center}{\tt [x\verb|^|2+-2*x*c\_0+c\_0\verb|^|2]}\end{center} 15505\item 15506Solve : 15507$$xy'\ln(x)-y(3\ln(x)+1)=0$$ 15508Input : 15509\begin{center}{\tt desolve(x*y'*ln(x)-(3*ln(x)+1)*y,y)}\end{center} 15510Output : 15511\begin{center}{\tt c\_0*x\verb|^|3*ln(x)}\end{center} 15512\end{enumerate} 15513 15514\item Examples of Bernoulli differential equations 15515$a(x)y'+b(x)y=c(x)y^n$ where $n$ is a real constant.\\ 15516The method used is to divide the equation by $y^n$, 15517so that it becomes a first order linear differential equation 15518in $u=1/y^{n-1}$. 15519\begin{enumerate} 15520\item 15521Solve : 15522$$xy'+2y+xy^2=0$$ 15523Input : 15524\begin{center}{\tt desolve(x*y'+2*y+x*y\verb|^|2,y)}\end{center} 15525Output : 15526\begin{center}{\tt [1/(exp(2*ln(x))*(-1/x+c\_0))]}\end{center} 15527\item 15528Solve : 15529$$xy'-2y=xy^3$$ 15530Input : 15531\begin{center}{\tt desolve(x*y'-2*y-x*y\verb|^|3,y)}\end{center} 15532Output : 15533\begin{center}{\tt [((-2*1/5*x\verb|^|5+c\_0)*exp(-(4*log(x))))\verb|^|(1/-2),}\end{center} 15534\begin{center}{\tt -((-2*1/5*x\verb|^|5+c\_0)*exp(-(4*log(x))))\verb|^|(1/-2)]}\end{center} 15535\item 15536Solve : 15537$$x^2y'-2y=xe^{(4/x)}y^3$$ 15538Input : 15539\begin{center}{\tt desolve(x*y'-2*y-x*exp(4/x)*y\verb|^|3,y)}\end{center} 15540Output : 15541\begin{center}{\tt [((-2*ln(x)+c\_0)*exp(-(4*(-(1/x)))))\verb|^|(1/-2),}\end{center} 15542\begin{center}{\tt -(((-2*ln(x)+c\_0)*exp(-(4*(-(1/x)))))\verb|^|(1/-2))]}\end{center} 15543\end{enumerate} 15544 15545\item Examples of first order homogeneous differential equations ($y'=F(y/x)$, 15546the method of integration is to search $t=y/x$ instead of $y$). 15547\begin{enumerate} 15548\item 15549Solve : 15550$$3x^3y'=y(3x^2-y^2)$$ 15551Input : 15552\begin{center}{\tt desolve(3*x\verb|^|3*diff(y)=((3*x\verb|^|2-y\verb|^|2)*y),y)}\end{center} 15553Output : 15554\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} 15555hence the solutions are $y=0$ and the familiy of curves of parametric 15556equation $x=c_0\exp(3/(2t^2)), y=t*c_0\exp(3/(2t^2))$ 15557(the parameter is denoted by {\tt ` t`} in the answer). 15558\item 15559Solve : 15560$$xy'=y+\sqrt{x^2+y^2}$$ 15561Input : 15562\begin{center}{\tt desolve(x*y'=y+sqrt(x\verb|^|2+y\verb|^|2),y)}\end{center} 15563Output : 15564\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} 15565hence the solutions are : 15566$$y=ix,y=-ix$$ 15567 and the family of curves of parametric equations 15568$$x=c_0/(\sqrt{t^2+1}-t), y=t*c_0/(\sqrt{t^2+1}-t)$$ 15569(the parameter is denoted by {\tt ` t`} in the answer). 15570\end{enumerate} 15571 15572 15573\item Examples of first order differential equations with an 15574integrating factor. By multiplying the equation by a function of $x,y$, 15575it becomes a closed differential form. 15576\begin{enumerate} 15577\item 15578Solve : 15579$$yy'+x$$ 15580Input : 15581\begin{center}{\tt desolve(y*y'+x,y)}\end{center} 15582Output : 15583\begin{center}{\tt [sqrt(-2*c\_0-x\verb|^|2),-(sqrt(-2*c\_0-x\verb|^|2))]}\end{center} 15584In this example, $xdx+ydy$ is closed, the integrating factor was 1. 15585\item 15586Solve : 15587$$2xyy'+x^2-y^2+a^2=0$$ 15588Input : 15589\begin{center}{\tt desolve(2*x*y*y'+x\verb|^|2-y\verb|^|2+a\verb|^|2,y)}\end{center} 15590Output : 15591\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} 15592In this example, the integrating factor was $1/x^2$. 15593\end{enumerate} 15594 15595\item Example of first order differential equations without $x$.\\ 15596Solve : 15597$$(y+y')^4+y'+3y=0$$ 15598This kind of equation cannot be solved directly by {\tt Xcas}, we explain 15599how to solve them with its help. 15600The idea is to find a parametric representation of 15601$F(u,v)=0$ where the equation is $F(y,y')=0$, 15602Let $u=f(t),v=g(t)$ be such a parametrization of $F=0$, then 15603$y=f(t)$ and $dy/dx=y'=g(t)$. Hence 15604\[ dy/dt=f'(t)=y'*dx/dt=g(t)*dx/dt \] 15605The solution is the curve of parametric equations 15606$x(t), y(t)=f(t)$, where $x(t)$ is solution of the differential equation 15607 $g(t)dx=f'(t)dt$.\\ 15608Back to the example, we put $y+y'=t$, hence: 15609\[ y=-t-8*t^4, \quad y'=dy/dx=3*t+8*t^4 \quad dy/dt=-1-32*t^3 15610\] 15611therefore 15612\[ (3*t+8*t^4)*dx=(-1-32*t^3)dt \] 15613Input : 15614\begin{center}{\tt desolve((3*t+8*t\verb|^|4)*diff(x(t),t)=(-1-32*t\verb|^|3),x(t))}\end{center} 15615Output : 15616\begin{center}{\tt -11*1/9*ln(8*t\verb|^|3+3)+1/-9*ln(t\verb|^|3)+c\_0}\end{center} 15617eventually the solution is the curve of parametric equation : 15618\[ x(t)=-11*1/9*\ln(8*t^3+3)+1/-9*\ln(t^3)+c_0, 15619\quad y(t)=-t-8*t^4 \] 15620 15621\item Examples of first order 15622Clairaut differential equations ($y=x*y'+f(y')$).\\ 15623The solutions are the lines $D_m$ of equation $y=mx+f(m)$ where 15624 $m$ is a real constant. 15625\begin{enumerate} 15626\item Solve : 15627$$xy'+y'^3-y=0$$ 15628Input : 15629\begin{center}{\tt desolve(x*y'+y'\verb|^|3-y),y)}\end{center} 15630Output : 15631\begin{center}{\tt c\_0*x+c\_0\verb|^|3}\end{center} 15632\item 15633Solve : 15634$$y-xy' - \sqrt{a^2+b^2*y'^2}=0$$ 15635Input : 15636\begin{center}{\tt desolve((y-x*y'-sqrt(a\verb|^|2+b\verb|^|2*y'\verb|^|2),y)}\end{center} 15637Output : 15638\begin{center}{\tt c\_0*x+sqrt(a\verb|^|2+b\verb|^|2*c\_0\verb|^|2)}\end{center} 15639\end{enumerate} 15640\end{itemize} 15641 15642\subsection{Laplace transform and inverse Laplace transform : {\tt laplace ilaplace}}\index{laplace}\index{ilaplace}\label{sec:lap} 15643{\tt laplace} and {\tt ilaplace} take one, two or three arguments : 15644 an expression and optionally the name(s) of the variable(s).\\ 15645The expression is an expression of the current variable (here $x$) or an 15646expression of the variable given as second argument.\\ 15647{\tt laplace} returns the Laplace transform of the expression given as argument 15648and {\tt ilaplace} the inverse Laplace transform of the expression given 15649as argument. The result of {\tt laplace} or {\tt ilaplace} is expressed 15650in terms of the variable given as third argument if supplied 15651or second argument if supplied or $x$ otherwise. 15652 15653The Laplace transform ({\tt laplace}) and inverse Laplace transform 15654({\tt ilaplace}) are useful to solve linear differential equations 15655with constant coefficients. For example : 15656$$y \prime \prime +p. y \prime+q. y \ =\ f(x)$$ $$ y(0)=a, \ y\prime(0)=b$$ 15657Denoting by ${\mathcal{L}}$ the Laplace transform, 15658the following relations hold : 15659\begin{eqnarray*} 15660{\mathcal{L}}(y)(x)&=&\int_0^{+\infty}e^{-x u}y(u)du \\ 15661{\mathcal{L}}^{-1}(g)(x)&=&\frac{1}{2i\pi}\int_C e^{z x}g(z)dz 15662\end{eqnarray*} 15663where $C$ is a closed contour enclosing the poles of {\tt g}.\\ 15664Input : 15665\begin{center}{\tt laplace(sin(x))}\end{center} 15666The expression (here $\sin(x)$) is an expression of the current variable 15667(here $x$) and the answer will also be an expression of the current variable 15668$x$.\\ 15669Output : 15670\begin{center}{\tt 1/((-x)\verb|^|2+1)}\end{center} 15671or : 15672\begin{center}{\tt laplace(sin(t),t)}\end{center} 15673here the variable name is $t$ and this name is also used in the answer.\\ 15674Output : 15675\begin{center}{\tt 1/((-t)\verb|^|2+1)}\end{center} 15676Or input : 15677\begin{center}{\tt laplace(sin(t),t,s)}\end{center} 15678here the variable name is $t$ and the variable name of the answer is $s$.\\ 15679Output: 15680\begin{center}{\tt 1/((-s)\verb|^|2+1)}\end{center} 15681The following properties hold : 15682\begin{eqnarray*} 15683{\mathcal{L}}(y')(x) &=&-y(0)+x.{\mathcal{L}}(y)(x) \\ 15684{\mathcal{L}}(y'')(x) &=&-y'(0)+x.{\mathcal{L}}(y')(x) \\ 15685 &=& -y'(0)-x.y(0)+x^2.{\mathcal{L}}(y)(x) 15686\end{eqnarray*} 15687If $y \prime \prime(x) +p y \prime(x)+q y(x) \ =\ f(x)$, then : 15688\begin{eqnarray*} 15689{\mathcal{L}}(f)(x) &=&{\mathcal{L}}(y''+p.y'+q.y)(x) \\ 15690&=& -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) \\ 15691&=& (x^2+p x+q) {\mathcal{L}}(y)(x)-y'(0)-(x+p) y(0) 15692\end{eqnarray*} 15693Therefore, if $a=y(0)$ and $b=y'(0)$, we have 15694$${\mathcal{L}}(f)(x)=(x^2+p x+q).{\mathcal{L}}(y)(x)-(x+p) a-b$$ 15695and the solution of the differential equation is : 15696\[ y(x)= 15697{\mathcal{L}}^{-1}(({\mathcal{L}}(f)(x)+(x+p) a +b)/(x^2+p x+q)) 15698\] 15699Example :\\ 15700Solve : 15701\[ y\prime \prime -6 y\prime+9 y \ =\ x e^{3. x}, 15702\quad y(0)=c\_0, \quad y\prime(0)=c\_1 15703\] 15704Here, $p=-6,\ q=9$.\\ 15705Input : 15706\begin{center}{\tt laplace(x*exp(3*x))}\end{center} 15707Output : 15708\begin{center}{\tt 1/(x\verb|^| 2-6*x+9)}\end{center} 15709Input : 15710\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} 15711Output : 15712\begin{center}{\tt (216*x\verb|^|3-3888*x*c\_0+1296*x*c\_1+1296*c\_0)*exp(3*x)/1296}\end{center} 15713After simplification and factorization ({\tt factor} command) 15714the solution $y$ is : 15715\begin{center}{\tt (-18*c\_0*x+6*c\_0+x\verb|^|3+6*x*c\_1)*exp(3*x)/6}\end{center} 15716Note that this equation could be solved directly. 15717Input : 15718\begin{center}{\tt desolve(y''-6*y'+9*y=x*exp(3*x),y)}\end{center} 15719Output : 15720\begin{center}{\tt exp(3*x)*(-18*c\_0*x+6*c\_0+x\verb|^|3+6*x*c\_1)/6}\end{center} 15721 15722\section{Other functions} 15723\subsection{Replace small values by 0: {\tt epsilon2zero}} 15724\index{epsilon2zero} \label{sec:epsilon2zero} 15725\noindent{\tt epsilon2zero} takes as argument an expression of {\tt x}.\\ 15726{\tt epsilon2zero} returns the expression where the values of modulus 15727less than {\tt epsilon} are replaced by zero. The expression 15728is not evaluated.\\ 15729The {\tt epsilon}\index{epsilon} value is defined in the {\tt cas} 15730configuration (by default {\tt epsilon=1e-10}).\\ 15731Input : 15732\begin{center}{\tt epsilon2zero(1e-13+x) }\end{center} 15733Output (with {\tt epsilon=1e-10}) : 15734\begin{center}{\tt 0+x}\end{center} 15735Input : 15736\begin{center}{\tt epsilon2zero((1e-13+x)*100000) }\end{center} 15737Output (with {\tt epsilon=1e-10}) : 15738\begin{center}{\tt (0+x)*100000}\end{center} 15739Input : 15740\begin{center}{\tt epsilon2zero(0.001+x) }\end{center} 15741Output (with {\tt epsilon=0.0001}) : 15742\begin{center}{\tt 0.001+x}\end{center} 15743 15744\subsection{List of variables : {\tt lname indets}}\index{lname}\index{indets} 15745\noindent{\tt lname} (or {\tt indets}) takes as argument an expression.\\ 15746{\tt lname} (or {\tt indets}) returns the list of the symbolic 15747variable names used in this expression.\\ 15748Input : 15749\begin{center}{\tt lname(x*y*sin(x))}\end{center} 15750Output : 15751\begin{center}{\tt [x,y]}\end{center} 15752Input : 15753\begin{center}{\tt a:=2;assume(b>0);assume(c=3);}\end{center} 15754\begin{center}{\tt lname(a*x\verb|^|2+b*x+c)}\end{center} 15755Output : 15756\begin{center}{\tt [x,b,c]}\end{center} 15757 15758\subsection{List of variables and of expressions : {\tt lvar}}\index{lvar}\label{sec:lvar} 15759\noindent{\tt lvar} takes as argument an expression.\\ 15760{\tt lvar} returns a list of variable names and non-rational 15761expressions such that its argument is a rational fraction 15762with respect to the variables and expressions of the list.\\ 15763Input : 15764\begin{center}{\tt lvar(x*y*sin(x)\verb|^|2)}\end{center} 15765Output : 15766\begin{center}{\tt [x,y,sin(x)]}\end{center} 15767Input : 15768\begin{center}{\tt lvar(x*y*sin(x)\verb|^|2+ln(x)*cos(y))}\end{center} 15769Output : 15770\begin{center}{\tt [x,y,sin(x),ln(x),cos(y)]}\end{center} 15771Input : 15772\begin{center}{\tt lvar(y+x*sqrt(z)+y*sin(x))}\end{center} 15773Output : 15774\begin{center}{\tt [x,y,sqrt(z),sin(x)]}\end{center} 15775 15776\subsection{List of variables of an algebraic expressions: {\tt algvar}}\index{algvar} 15777\noindent{\tt algvar} takes as argument an expression.\\ 15778{\tt algvar} returns the list of the symbolic 15779variable names used in this expression. The list is ordered 15780by the algebraic extensions required to build the original expression.\\ 15781Input : 15782\begin{center}{\tt algvar(y+x*sqrt(z))}\end{center} 15783Output : 15784\begin{center}{\tt [[y,x],[z]]}\end{center} 15785Input : 15786\begin{center}{\tt algvar(y*sqrt(x)*sqrt(z))}\end{center} 15787Output : 15788\begin{center}{\tt [[y],[z],[x]]}\end{center} 15789Input : 15790\begin{center}{\tt algvar(y*sqrt(x*z))}\end{center} 15791Output : 15792\begin{center}{\tt [[y],[x,z]]}\end{center} 15793Input : 15794\begin{center}{\tt algvar(y+x*sqrt(z)+y*sin(x))}\end{center} 15795Output : 15796\begin{center}{\tt [[x,y,sin(x)],[z]]}\end{center} 15797 15798\subsection{Test if a variable is in an expression : {\tt has}}\index{has|textbf} 15799\noindent{\tt has} takes as argument an expression and the name of a 15800variable.\\ 15801{\tt has} returns {\tt 1} if this variable is in this expression, and else 15802returns {\tt 0}.\\ 15803Input : 15804\begin{center}{\tt has(x*y*sin(x),y)}\end{center} 15805Output : 15806\begin{center}{\tt 1}\end{center} 15807Input : 15808\begin{center}{\tt has(x*y*sin(x),z)}\end{center} 15809Output : 15810\begin{center}{\tt 0}\end{center} 15811 15812\subsection{Numeric evaluation : {\tt evalf}}\index{evalf} 15813\noindent{\tt evalf} takes as argument an expression or a matrix.\\ 15814{\tt evalf} returns the numeric value of this expression or of this matrix.\\ 15815Input : 15816\begin{center}{\tt evalf(sqrt(2))}\end{center} 15817Output : 15818\begin{center}{\tt 1.41421356237}\end{center} 15819Input : 15820\begin{center}{\tt evalf([[1,sqrt(2)],[0,1]])}\end{center} 15821Output : 15822\begin{center}{\tt [[1.0,1.41421356237],[0.0,1.0]]}\end{center} 15823 15824\subsection{Rational approximation : {\tt float2rational exact}}\index{float2rational}\index{exact} 15825\noindent{\tt float2rational} (or {\tt exact}) 15826takes as argument an expression.\\ 15827{\tt float2rational} returns a rational approximation of 15828all the floating point numbers $r$ contained in this expression, such 15829that $|r-\mbox{\tt float2rational}(r)|<\epsilon$, where 15830$\epsilon$ is defined by {\tt epsilon} in the {\tt cas} configuration 15831({\tt Cfg} menu, or {\tt cas\_setup} command).\\ 15832Input : 15833\begin{center}{\tt float2rational(1.5)}\end{center} 15834Output : 15835\begin{center}{\tt 3/2}\end{center} 15836Input : 15837\begin{center}{\tt float2rational(1.414)}\end{center} 15838Output : 15839\begin{center}{\tt 707/500}\end{center} 15840Input : 15841\begin{center}{\tt float2rational(0.156381102937*2)}\end{center} 15842Output : 15843\begin{center}{\tt 5144/16447}\end{center} 15844Input : 15845\begin{center}{\tt float2rational(1.41421356237)}\end{center} 15846Output : 15847\begin{center}{\tt 114243/80782}\end{center} 15848Input : 15849\begin{center}{\tt float2rational(1.41421356237\verb|^|2)}\end{center} 15850Output : 15851\begin{center}{\tt 2}\end{center} 15852 15853\chapter{Graphs}\label{sec:plot} 15854Most graph instructions take expressions as arguments. A few 15855exceptions (mostly Maple-compatibility instructions) also accept 15856functions. 15857Some optional arguments, like {\tt color, thickness}, can be used as optional 15858attributes in all graphic instructions. They are described below. 15859 15860\section{Graph and geometric objects attributes} 15861There are two kinds of attributes: global attributes of a graphic 15862scene and individual attributes. 15863 15864\subsection{Individual attributes}\index{color}\index{display} 15865\index{red@{\it red}|textbf}\index{blue@{\it blue}|textbf}\index{yellow@{\it yellow}|textbf}\index{magenta@{\it magenta}|textbf}\index{green@{\it green}|textbf}\index{cyan@{\it cyan}|textbf}\index{white@{\it white}|textbf}\index{black@{\it black}|textbf}\index{filled@{\it filled}} 15866Graphic attributes are optional arguments of the 15867form {\tt display=value}, they must be given 15868as the last argument of a graphic instruction. Attributes 15869are ordered in several categories: color, point shape, point width, 15870line style, line thickness, legend value, position and presence. 15871In addition, surfaces may be filled or not, 3-d surfaces 15872may be filled with a texture, 3-d objects may also have properties 15873with respect to the light. 15874Attributes of different categories 15875may be added, e.g. \\ 15876{\tt plotfunc($x^2+y^2$,[x,y],display=red+line\_width\_3+filled)} 15877\begin{itemize} 15878\item Colors {\tt display=} or {\tt color=} 15879\begin{itemize} 15880\item {\tt black}, {\tt white}, {\tt red}, {\tt blue}, {\tt green}, 15881{\tt magenta}, {\tt cyan}, {\tt yellow}, 15882\item a numeric value between 0 and 255, 15883\item a numeric value between 256 and 256+7*16+14 for a color of the 15884rainbow, 15885\item any other numeric value smaller than 65535, the rendering 15886is not guaranteed to be portable. 15887\end{itemize} 15888\item Point shapes {\tt display=} one of the following value 15889{\tt rhombus\_point plus\_point square\_point cross\_point 15890triangle\_point star\_point point\_point invisible\_point} 15891\item Point width: {\tt display=} one of the following value 15892{\tt point\_width\_n} where {\tt n} is an 15893integer between 1 and 7 15894\item Line thickness: {\tt thickness=n} 15895or {\tt display=line\_width\_n} where {\tt n} is an 15896integer between 1 and 7 or 15897\item Line shape: {\tt display=} one of the following values 15898{\tt dash\_line solid\_line dashdot\_line dashdotdot\_line 15899 cap\_flat\_line cap\_square\_line cap\_round\_line } 15900\item Legend, value: {\tt legend="legendname"}; 15901 position: {\tt display=} one of 15902{\tt quandrant1 quadrant2 quadrant3 quadrant4} 15903corresponding to the position of the legend of the object 15904(using the trigonometric plane conventions). 15905The legend is not displayed if the attribute 15906{\tt display=hidden\_name} is added 15907\item {\tt display=filled} specifies that surfaces will be filled, 15908\item {\tt gl\_texture="picture\_filename"} is used to fill 15909a surface with a texture. 15910Cf. the interface manual for a more complete 15911description and for {\tt gl\_material=} options. 15912\end{itemize} 15913{\bf Examples}\\ 15914Input : 15915\begin{center}{\tt polygon(-1,-i,1,2*i,legend="P")}\end{center} 15916Input : 15917\begin{center}{\tt point(1+i,legend="hello")}\end{center} 15918Input : 15919\begin{center}{\tt A:=point(1+i);B:=point(-1);display(D:=droite(A,B),hidden\_name)}\end{center} 15920Input : 15921\begin{center}{\tt color(segment(0,1+i),red)}\end{center} 15922Input : 15923\begin{center}{\tt segment(0,1+i,color=red)}\end{center} 15924 15925\subsection{Global attributes} 15926These attributes are shared by all objects of the same scene 15927\begin{itemize} 15928\item {\tt title="titlename"} defines the title 15929\item {\tt labels=["xname","yname","zname"]}: names of the $x,y,z$ 15930axis 15931\item {\tt gl\_x\_axis\_name="xname"}, {\tt gl\_y\_axis\_name="yname"}, 15932{\tt gl\_z\_axis\_name=""}: individual definitions 15933of the names of the $x,y,z$ axis 15934\item {\tt legend=["xunit","yunit","zunit"]}: units for the 15935$x,y,z$ axis 15936\item {\tt gl\_x\_axis\_unit="xunit"}, {\tt gl\_y\_axis\_unit="yunit"}, 15937{\tt gl\_z\_axis\_unit=""}: individual definition 15938of the units of the $x,y,z$ axis 15939\item {\tt axes=true or false} show or hide axis 15940\item {\tt gl\_texture="filename"}: background image 15941\item {\tt gl\_x=xmin..xmax}, {\tt gl\_y=ymin..ymax}, 15942{\tt gl\_z=zmin..zmax}: set the graphic configuration 15943(do not use for interactive scenes) 15944\item {\tt gl\_xtick=}, {\tt gl\_ytick=}, {\tt gl\_ztick=}: 15945set the tick mark for the axis 15946\item {\tt gl\_shownames=true or false}: show or hide objects names 15947\item {\tt gl\_rotation=[x,y,z]}: defines the rotation axis 15948for the animation rotation of 3-d scenes. 15949\item {\tt gl\_quaternion=[x,y,z,t]}: defines the quaternion 15950for the visualization in 3-d scenes (do not use for interactive 15951scenes) 15952\item a few other OpenGL light configuration options are 15953available but not described here. 15954\end{itemize} 15955{\bf Examples}\\ 15956Input : 15957\begin{center}{\tt legend=["mn","kg"]}\end{center} 15958Input : 15959\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} 15960Input : 15961\begin{center}{\tt labels=["u","v"];plotfunc(u+1,u)}\end{center} 15962 15963\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}} 15964 15965\subsection{2-d graph}\label{sec:plotfunc} 15966\noindent{\tt plotfunc(f(x),x)} draws the graph of $y=f(x)$ for $x$ in 15967the default interval, 15968{\tt plotfunc(f(x),x=a..b)} draws the graph of $y=f(x)$ for $a\leq x\leq b$. 15969{\tt plotfunc} accepts an optional \verb|xstep=...| argument to 15970specify the discretization step in $x$.\\ 15971Input : 15972\begin{center}{\tt plotfunc(x\verb|^|2-2)}\end{center} 15973or : 15974\begin{center}{\tt plotfunc(a\verb|^|2-2,a=-1..2)}\end{center} 15975Output : 15976\begin{center}{\tt the graph of y=x\verb|^|2-2}\end{center} 15977Input : 15978\begin{center}{\tt plotfunc(x\verb|^|2-2,x,xstep=1)}\end{center} 15979Output : 15980\begin{center}{\tt a polygonal line which is a bad representation of y=x\verb|^|2-2 }\end{center} 15981It is also possible to specify the number of points used for the 15982representation of the function with \verb|nstep=| instead of \verb|xstep=|. 15983For example, input~: 15984\begin{center}{\tt plotfunc(x\verb|^|2-2,x=-2..3,nstep=30)}\end{center} 15985 15986\subsection{3-d graph}\label{sec:plotfunc3} 15987\noindent{\tt plotfunc} takes two main arguments : an expression of two 15988variables or a list of several expressions of two variables and the list of 15989these two variables, where each variable may be replaced by 15990an equality variable=interval to specify the range for this variable 15991(if not specified, default values are taken from the graph configuration). 15992{\tt plotfunc} accepts two optional arguments to specify 15993the discretization step in $x$ and in $y$ by 15994{\tt xstep=...} and {\tt ystep=...}. 15995Alternatively one can specify the number of points used for the 15996representation of the function with \verb|nstep=| (instead of \verb|xstep| and 15997{\tt ystep}).\\ 15998{\tt plotfunc} draws the surface(s) defined by $z=$ the first argument.\\ 15999Input : 16000\begin{center}{\tt plotfunc( x\verb|^|2+y\verb|^|2,[x,y])}\end{center} 16001Output : 16002\begin{center}{\tt A 3D graph of z=x\verb|^|2+y\verb|^|2}\end{center} 16003Input : 16004\begin{center}{\tt plotfunc(x*y,[x,y]) }\end{center} 16005Output : 16006\begin{center}{\tt The surface z=x*y, default ranges}\end{center} 16007Input : 16008\begin{center}{\tt plotfunc([x*y-10,x*y,x*y+10],[x,y]) }\end{center} 16009Output : 16010\begin{center}{\tt The surfaces z=x*y-10, z=x*y and z=x*y+10}\end{center} 16011Input : 16012\begin{center}{\tt plotfunc(x*sin(y),[x=0..2,y=-pi..pi]) }\end{center} 16013Output : 16014\begin{center}{\tt The surface $z=x*y$ for the specified ranges}\end{center} 16015Now an example where we specify the $x$ and $y$ discretization step 16016with \verb|xstep| and \verb|ystep|.\\ 16017Input : 16018\begin{center} 16019{\tt plotfunc(x*sin(y),[x=0..2,y=-pi..pi],xstep=1,ystep=0.5) }\end{center} 16020Output : 16021\begin{center}{\tt A portion of surface $z=x*y$}\end{center} 16022Alternatively we may specify 16023the number of points used for the representation of the 16024function with \verb|nstep| instead of \verb|xstep| and \verb|ystep|.\\ 16025Input~: 16026\begin{center}{\tt plotfunc(x*sin(y),[x=0..2,y=-pi..pi],nstep=300)}\end{center} 16027Output : 16028\begin{center}{\tt A portion of surface $z=x*y$}\end{center} 16029{\bf Remarks} 16030\begin{itemize} 16031\item 16032Like any 3-d scene, the viewpoint may be modified by rotation 16033around the {\tt x} axis, the {\tt y} axis or the 16034{\tt z} axis, either by dragging the mouse inside the graphic 16035window (push the mouse outside the parallelepiped used for 16036the representation), or with the shortcuts 16037{\tt x}, {\tt X}, {\tt y}, {\tt Y}, {\tt z} and {\tt Z}. 16038\item 16039If you want to print a graph or get a \LaTeX\ translation, use the graph 16040menu\\ 16041{\tt Menu$\blacktriangleright$print$\blacktriangleright$Print(with 16042 Latex)} 16043\end{itemize} 16044 16045\subsection{3-d graph with rainbow colors}\label{sec:plotfunc3d} 16046\noindent{\tt plotfunc} represents a pure imaginary expression {\tt i*E} 16047of two variables with a rainbow color depending 16048on the value of {\tt z=E}. This gives an easy way to 16049find points having the same third coordinate.\\ 16050The first arguments of {\tt plotfunc} must be {\tt i*E} instead of {\tt E}, 16051the remaining arguments are the same 16052as for a real 3-d graph (cf \ref{sec:plotfunc3}) 16053Input : 16054\begin{center}{\tt plotfunc(i*x*sin(y),[x=0..2,y=-pi..pi]) }\end{center} 16055Output : 16056\begin{center}{\tt A piece of the surface $z=x*\sin(y)$ with rainbow colors}\end{center} 16057{\bf Remark}\\ 16058 If you want the graphic in LaTeX, you have to use :\\ 16059{\tt Menu$\blacktriangleright$print$\blacktriangleright$Print(with Latex)}. 16060 16061\subsection{4-d graph.}\label{sec:plotfunc4} 16062\noindent{\tt plotfunc} represents a complex expression {\tt E} 16063(such that {\tt re(E)} is not identically 0 on the discretization mesh) 16064by the surface {\tt z=abs(E)} where {\tt arg(E)} defines the color 16065from the rainbow. This gives an easy way to 16066see the points having the same argument. 16067Note that if {\tt re(E)==0} on the discretization mesh, 16068it is the surface {\tt z=E/i} that is represented with rainbow colors 16069(cf \ref{sec:plotfunc3d}).\\ 16070The first argument of {\tt plotfunc} is {\tt E}, 16071the remaining arguments are the same 16072as for a real 3-d graph (cf \ref{sec:plotfunc3}).\\ 16073Input : 16074\begin{center}{\tt plotfunc((x+i*y)\verb|^|2,[x,y])}\end{center} 16075Output : 16076\begin{center}{\tt A graph 3D of z=abs((x+i*y)\verb|^|2 with the same color for 16077points having the same argument}\end{center} 16078Input : 16079\begin{center}{\tt plotfunc((x+i*y)\verb|^|2x,[x,y], display=filled)}\end{center} 16080Output : 16081\begin{center}{\tt The same surface but filled}\end{center} 16082We may specify the range of variation of $x$ and $y$ and the number of 16083discretization points.\\ 16084Input : 16085\begin{center}{\tt plotfunc((x+i*y)\verb|^|2,[x=-1..1,y=-2..2], nstep=900,display=filled)}\end{center} 16086Output : 16087\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} 16088 16089\section{2d graph for Maple compatibility : {\tt plot}} 16090\index{plot} \label{sec:plot2d} 16091\noindent{\tt plot(f(x),x)} draws the graph of $y=f(x)$. 16092The second argument may specify the range of values {\tt 16093 x=xmin..xmax}. One can also plot a function instead of an 16094expression using the syntax {\tt plot(f,xmin..xmax)}. 16095{\tt plot} accepts an optional argument to specify 16096the step used in $x$ for the discretization with 16097\verb|xstep=| or the number of points of the discretization 16098with \verb|nstep=|.\\ 16099Input : 16100\begin{center}{\tt plot(x\verb|^|2-2,x)}\end{center} 16101Output : 16102\begin{center}{\tt the graph of y=x\verb|^|2-2}\end{center} 16103Input : 16104\begin{center}{\tt plot(x\verb|^|2-2,xstep=1)}\end{center} 16105or : 16106\begin{center}{\tt plot(x\verb|^|2-2,x,xstep=1)}\end{center} 16107Output : 16108\begin{center}{\tt a polygonal line which is a bad representation of 16109 y=x\verb|^|2-2 }\end{center} 16110Input! 16111\begin{center}{\tt plot(x\verb|^|2-2,x=-2..3,nstep=30)}\end{center} 16112 16113 16114\section{3d surfaces for Maple compatibility {\tt plot3d}}\index{plot3d} 16115\noindent{\tt plot3d} takes three arguments : a function of two variables or 16116an expression of two variables or a list of three functions of two variables 16117or a list of three expressions of two variables and the names of these two 16118variables with an optional range (for expressions) or the ranges 16119(for functions).\\ 16120{\tt plot3d(f(x,y),x,y)} (resp. {\tt plot3d([f(u,v),g(u,v),h(u,v)],u,v)}) draws 16121the surface $z=f(x,y)$ (resp. $x=f(u,v),y=g(u,v),z=h(u,v)$). 16122The syntax {\tt plot3d(f(x,y),x=x0..x1,y=y0..y1)} or 16123{\tt plot3d(f,x0..x1,y0..y1)} specifies which part of surface 16124will be computed (otherwise default values are taken from the graph 16125configuration).\\ 16126Input : 16127\begin{center}{\tt plot3d(x*y,x,y)}\end{center} 16128Output : 16129\begin{center}{\tt The surface $z=x*y$}\end{center} 16130Input : 16131\begin{center}{\tt plot3d([v*cos(u),v*sin(u),v],u,v) }\end{center} 16132Output : 16133\begin{center}{\tt The cone $x=v*\cos(u),y=v*\sin(u),z=v$}\end{center} 16134Input : 16135\begin{center}{\tt plot3d([v*cos(u),v*sin(u),v],u=0..pi,v=0..3)}\end{center} 16136Output : 16137\begin{center}{\tt A portion of the cone $x=v*\cos(u),y=v*\sin(u),z=v$}\end{center} 16138 16139\section{Graph of a line and tangent to a graph} 16140\subsection{Draw a line : {\tt line}}\index{line}\label{sec:doite} 16141%{\bf See also :} \ref{sec:droite2} and \ref{sec:droite3} for line usage in 16142%geometry and see \ref{sec:axe2} and \ref{sec:axe3} for axis.\\ 16143\noindent {\tt line} takes as argument cartesian equation(s) : 16144\begin{itemize} 16145\item in 2D: one line equation, 16146\item in 3D: two plane equations. 16147\end{itemize} 16148{\tt line} defines and draws the corresponding line.\\ 16149Input : 16150\begin{center}{\tt line(2*y+x-1=0)}\end{center} 16151Output : 16152\begin{center}{\tt the line 2*y+x-1=0}\end{center} 16153Input : 16154\begin{center}{\tt line(y=1)}\end{center} 16155Output : 16156\begin{center}{\tt the horizontal line y=1}\end{center} 16157Input : 16158\begin{center}{\tt line(x=1)}\end{center} 16159Output : 16160\begin{center}{\tt the vertical line x=1}\end{center} 16161Input : 16162\begin{center}{\tt line(x+2*y+z-1=0,z=2)}\end{center} 16163Output : 16164\begin{center}{\tt the line x+2*y+1=0 in the plane z=2}\end{center} 16165Input : 16166\begin{center}{\tt line(y=1,x=1)}\end{center} 16167Output : 16168\begin{center}{\tt the vertical line crossing through (1,1,0)}\end{center} 16169{\bf Remark}\\ 16170{\tt line} defines an oriented line : 16171\begin{itemize} 16172\item when the 2D line is given by an equation, it is rewritten 16173as "left\_member-right\_member={\tt ax+by+c=0}", this determines 16174its normal vector {\tt [a,b]} and the orientation is given by the vector 16175{\tt [b,-a]}) (or its orientation is defined by the 3D cross product of its 16176normal vectors (with third coordinate 0) and the vector [0,0,1]).\\ 16177For example {\tt line(y=2*x)} defines the line {\tt -2x+y=0} with as direction 16178the vector {\tt [1,2]} (or {\tt cross([-2,1,0],[0,0,1])}={\tt [1,2,0]}). 16179\item when the 3D line is given by two plane equations, its 16180direction is defined by the cross product of the normals to the planes 16181(where the plane equation is rewritten as 16182"left\_member-right\_member={\tt ax+by+cz+d=0}", so that 16183the normal is {\tt [a,b,c]}).\\ 16184For example the {\tt line(x=y,y=z)} is the line {\tt x-y=0,y-z=0} and its 16185direction is :\\ 16186{\tt cross([1,-1,0],[0,1,-1])}={\tt [1,1,1]}. 16187\end{itemize} 16188 16189\subsection{Draw an 2D horizontal line : {\tt LineHorz}}\index{LineHorz} 16190\noindent {\tt LineHorz} takes as argument an expression $a$.\\ 16191 {\tt LineHorz} draws the horizontal line $y=a$.\\ 16192Input : 16193\begin{center}{\tt LineHorz(1)}\end{center} 16194Output : 16195\begin{center}{\tt the line y=1}\end{center} 16196 16197\subsection{Draw a 2D vertical line : {\tt LineVert}}\index{LineVert} 16198\noindent {\tt LineVert} takes as argument an expression $a$.\\ 16199 {\tt LineVert} draws the vertical line $x=a$.\\ 16200Input : 16201\begin{center}{\tt LineVert(1)}\end{center} 16202Output : 16203\begin{center}{\tt the line x=1}\end{center} 16204 16205\subsection{Tangent to a 2D graph : {\tt LineTan}}\index{LineTan} 16206\noindent {\tt LineTan} takes two arguments : an expression $E_x$ of the 16207variable $x$ and a value $x0$ of $x$.\\ 16208 {\tt LineTan} draws the tangent at $x=x0$ to the graph of $y=E_x$.\\ 16209Input : 16210\begin{center}{\tt LineTan(ln(x),1)}\end{center} 16211Output : 16212\begin{center}{\tt the line y=x-1}\end{center} 16213Input : 16214\begin{center}{\tt equation(LineTan(ln(x),1))}\end{center} 16215Output : 16216\begin{center}{\tt y=(x-1)}\end{center} 16217 16218\subsection{Tangent to a 2D graph : {\tt tangent}}\index{tangent|textbf}\label{sec:tangente} 16219%{\bf See also :} \ref{sec:tangent} for plane geometry and 16220%\ref{sec:tangent3} for 3D geometry.\\ 16221\noindent {\tt tangent} takes two arguments : a geometric object and a point 16222{\tt A}.\\ 16223{\tt tangent} draws tangent(s) to this geometric object crossing through 16224{\tt A}. If the geometric object is the graph {\tt G} of a 2D function, 16225the second argument is either, a real number {\tt x0}, or a 16226point {\tt A} on {\tt G}. In that case {\tt tangent} draws a tangent to this 16227graph {\tt G} crossing through the point {\tt A} or through the 16228point of abscissa {\tt x0}.\\ 16229For example, define the function {\tt g} 16230\begin{center} \verb|g(x):=x^2|\end{center} 16231then the graph {\tt G=\{(x,y)$\in \R^2$, y=g(x)\}} 16232of $g$ and a point $A$ on the graph $G$: 16233\begin{center} 16234{\tt G:=plotfunc(g(x),x);}\\ 16235{\tt A:=point(1.2,g(1.2));} 16236\end{center} 16237If we want to draw the tangent at the point {\tt A} to the graph {\tt 16238 G}, we will input: 16239\begin{center} 16240{\tt T:=tangent(G, A)} 16241\end{center} 16242or : 16243\begin{center} 16244{\tt T:=tangent(G, 1.2)} 16245\end{center} 16246For the equation of the tangent line, input : 16247\begin{center}{\tt equation(T)}\end{center} 16248 16249\subsection{Intersection of a 2D graph with the axis}\index{solve}\index{resoudre} 16250\begin{itemize} 16251\item The ordinate of the intersection of the graph of $f$ with the 16252$y$-axis is returned by : 16253\begin{center}{\tt f(0)}\end{center} 16254indeed the point of coordinates $(0,f(0))$ is the intersection point of the 16255graph of $f$ with the $y$-axis, 16256\item Finding the intersection of the graph of $f$ with the $x$-axis 16257requires solving the equation $f(x)=0$. \\ 16258If the equation is polynomial-like, {\tt solve} will find 16259the exact values of the abscissa of these points. Input: 16260\begin{center}{\tt solve(f(x),x)}\end{center} 16261Otherwise, we can find numeric approximations of these 16262abscissa. First look at the graph for an initial guess or a 16263range with an intersection and refine with {\tt fsolve}. 16264\end{itemize} 16265 16266\section{Graph of inequalities with 2 variables : {\tt plotinequation inequationplot}}\index{plotinequation|textbf}\index{inequationplot|textbf} 16267\noindent{\tt plotinequation([f1(x,y)<a1,...fk(x,y)<ak],[x=x1..x2,y=y1..y2])} 16268draws the points of the plane whose coordinates 16269satisfy the inequalities of 2 variables : 16270\[ \left\{ \begin{array}{ccc} 16271f1(x,y) &<&a1 \\ 16272& ... & \\ 16273fk(x,y)&<&ak 16274\end{array}\right., \quad 16275x1\leq x \leq x2, y1 \leq y \leq y2 \] 16276Input : 16277\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} 16278Output : 16279\begin{center}{\tt the filled portion enclosing the origin and limited by the hyperbola x\verb|^|2-y\verb|^|2=3}\end{center} 16280Input : 16281\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} 16282Output : 16283\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} 16284Note that if the ranges for $x$ and $y$ are not specified, 16285{\tt Xcas} takes the default values of 16286{\tt X-,X+,Y-,Y+} defined in the general graphic configuration 16287({\tt Cfg$\blacktriangleright$Graphic configuration}). 16288 16289\section{Graph of the area below a curve : {\tt plotarea areaplot}}\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} 16290\begin{itemize} 16291\item With two arguments, {\tt plotarea} shades the area below a curve.\\ 16292{\tt plotarea(f(x),x=a..b)} draws the area below the curve $y=f(x)$ for 16293$a<x<b$, i.e. the portion of the plane defined by the inequalities $a<x<b$ and 16294$0<y<f(x)$ or $0>y>f(x)$ according to the sign of $f(x)$ .\\ 16295Input : 16296\begin{center}{\tt plotarea(sin(x),x=0..2*pi)}\end{center} 16297Output : 16298\begin{center}{\tt the portion of plane locates in the two arches of sin(x)}\end{center} 16299\item With four arguments, {\tt plotarea} represents a numeric approximation 16300of the area below a curve, according to a quadrature method from the 16301following list:\\ 16302{\tt trapezoid,rectangle\_left,rectangle\_right,middle\_point}.\\ 16303For example {\tt plotarea(f(x),x=a..b,n,trapezoid)} 16304draws the area of $n$ trapezoids : the 16305third argument is an integer $n$, and the fourth argument is the name of the 16306numeric method of integration when $[a,b]$ is cut into $n$ equal parts.\\ 16307Input : 16308\begin{center}{\tt plotarea((x\verb|^|2,x=0..1,5,trapezoid)}\end{center} 16309If you want to display the graph of the curve in contrast 16310(e.g. in bold red), input : 16311\begin{center}{\tt plotarea(x\verb|^|2,x=0..1,5,trapezoid); 16312plot(x\verb|^|2,x=0..1,display=red+line\_width\_3)}\end{center} 16313Output : 16314\begin{center}{\tt the 5 trapezoids used in the trapezoid method to approach the integral}\end{center} 16315Input : 16316\begin{center}{\tt plotarea((x\verb|^|2,x=0..1,5,middle\_point)}\end{center} 16317Or with the graph of the curve in bold red, input : 16318\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} 16319Output : 16320\begin{center}{\tt the 5 rectangles used in the middle\_point method 16321 to approach the integral}\end{center} 16322\end{itemize} 16323 16324\section{Contour lines: {\tt plotcontour contourplot DrwCtour}}\index{plotcontour|textbf}\index{contourplot|textbf}\index{DrwCtour|textbf}\label{sec:plotcontour} 16325\noindent{\tt plotcontour(f(x,y),[x,y])} (or {\tt DrwCtour(f(x,y),[x,y])} or \\ 16326 {\tt contourplot(f(x,y),[x,y])}) 16327draws the contour lines of the surface defined by $z=f(x,y)$ for $z=-10$, 16328$z=-8$, .., $z=0$, $z=2$, .., $z=10$. You may specify the desired contour 16329lines by a list of values of $z$ given as third argument.\\ 16330Input : 16331\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} 16332Output : 16333\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} 16334Input : 16335\begin{center}{\tt plotcontour(x\verb|^|2-y\verb|^|2,[x,y])}\end{center} 16336Output : 16337\begin{center}{\tt the graph of 11 hyperbolas x\verb|^|2-y\verb|^|2=n for n=-10,-8,..10}\end{center} 16338 16339If you want to draw the surface in 3-d representation, 16340input {\tt plotfunc(f(x,y),[x,y])}, see \ref{sec:plotfunc3}): 16341\begin{center}{\tt plotfunc( x\verb|^|2-y\verb|^|2,[x,y])}\end{center} 16342Output : 16343\begin{center}{\tt A 3D representation of z=x\verb|^|2+y\verb|^|2}\end{center} 16344 16345\section{2-d graph of a 2-d function with colors : 16346{\tt plotdensity densityplot}} 16347\index{plotdensity|textbf}\index{densityplot|textbf} 16348\noindent{\tt plotdensity(f(x,y),[x,y])} or {\tt densityplot(f(x,y),[x,y])} 16349draws the graph of $z=f(x,y)$ in the plane where the values of 16350$z$ are represented by the rainbow colors. The optional argument 16351{\tt z=zmin..zmax} specifies the range of $z$ corresponding to the 16352full rainbow, if it is not specified, it is deduced from the minimum 16353and maximum value of $f$ on the discretization. The discretization 16354may be specified by optional {\tt xstep=...} and {\tt ystep=...} 16355or {\tt nstep=...} arguments.\\ 16356Input : 16357\begin{center}{\tt plotdensity(x\verb|^|2-y\verb|^|2,[x=-2..2,y=-2..2], xstep=0.1,ystep=0.1)}\end{center} 16358Output : 16359\begin{center}{\tt A 2D graph where each hyperbola defined by 16360 x\verb|^|2-y\verb|^|2=z has a color from the rainbow}\end{center} 16361{\bf Remark} : A rectangle representing the scale of colors is 16362displayed below the graph. 16363 16364\section{Implicit graph: {\tt plotimplicit implicitplot}}\index{plotimplicit}\index{implicitplot}\index{unfactored} 16365\noindent{\tt plotimplicit} or {\tt implicitplot} draws curves or 16366surfaces defined by an implicit expression or equation. 16367If the option {\tt unfactored} is given as last argument, the 16368original expression is taken unmodified. Otherwise, 16369the expression is normalized, then replaced by the 16370factorization of the numerator of its normalization. 16371 16372Each factor of the expression corresponds to a component 16373of the implicit curve or surface. For each factor, 16374Xcas tests if it is of total 16375degree less or equal to 2, in that case {\tt conic} or 16376{\tt quadric} is called. Otherwise the numeric implicit solver 16377is called. 16378 16379Optional step and ranges arguments may be passed to the numeric 16380implicit solver, note that they are dismissed for each component 16381that is a conic or a quadric. 16382 16383\subsection{2D implicit curve}\label{sec:implicitplot} 16384\begin{itemize} 16385\item {\tt plotimplicit(f(x,y),x,y)} draws the graphic representation of the 16386curve defined by the implicit equation $f(x,y)=0$ when $x$ (resp. $y$) 16387is in {\tt WX-, WX+} (resp. in {\tt WY-, WY+}) defined by {\tt cfg}, 16388 16389\item {\tt plotimplicit(f(x,y),x=0..1,y=-1..1)} draws the graphic 16390representation of the curve defined by the implicit equation $f(x,y)=0$ 16391when $0\leq x \leq 1$ and $-1\leq y \leq 1$ 16392\end{itemize} 16393It is possible to add two arguments to specify the discretization 16394steps for $x$ 16395and $y$ with {\tt xstep=...} and {\tt ystep=...}.\\ 16396Input : 16397\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,x,y)}\end{center} 16398or : 16399\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,x,y,unfactored)}\end{center} 16400Output : 16401\begin{center}{\tt The unit circle}\end{center} 16402Input : 16403\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,x,y,xstep=0.2,ystep=0.3)}\end{center} 16404or : 16405\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,[x,y],xstep=0.2,ystep=0.3)}\end{center} 16406or : 16407\begin{center}{\tt plotimplicit(x\verb|^|2+y\verb|^|2-1,[x,y], xstep=0.2,ystep=0.3,unfactored)}\end{center} 16408Output : 16409\begin{center}{\tt The unit circle}\end{center} 16410Input : 16411\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} 16412Output : 16413\begin{center}{\tt The unit circle}\end{center} 16414 16415\subsection{3D implicit surface}\label{sec:implicitplot3} 16416\begin{itemize} 16417\item {\tt plotimplicit(f(x,y,z),x,y,z)} draws the graphic 16418representation of the surface defined by the implicit equation $f(x,y,z)=0$, 16419\item {\tt plotimplicit(f(x,y,z),x=0..1,y=-1..1,z=-1..1)} draws the surface 16420defined by the implicit equation $f(x,y,z)=0$, 16421where $0\leq x \leq 1$, $-1\leq y \leq 1$ and $-1\leq z \leq 1$. 16422\end{itemize} 16423It is possible to add three arguments to specify the discretization 16424steps used for $x$, $y$ and $z$ with {\tt xstep=...}, {\tt ystep=...} and 16425{\tt zstep=...}.\\ 16426Input : 16427\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} 16428Input : 16429\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} 16430Output : 16431\begin{center}{\tt The unit sphere}\end{center} 16432Input : 16433\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} 16434Output : 16435\begin{center}{\tt The unit sphere}\end{center} 16436 16437\subsection{Implicit differentiation : {\tt implicitdiff}} 16438{\tt implicitdiff} is called with one of the following three sets of parameters : 16439\begin{enumerate} 16440\item {\tt expr}, {\tt constr}, {\tt depvars}, {\tt diffvars} 16441\item {\tt constr}, {\tt [depvars]}, {\tt y}, {\tt diffvars} 16442\item {\tt expr}, {\tt constr}, {\tt vars}, {\tt order\_size=k}, {\tt [pt]} 16443\end{enumerate} 16444Details on parameters : 16445\begin{itemize} 16446\item {\tt expr} : differentiable expression $ f(x_1,x_2,\dots,x_n,y_1,y_2,\dots,y_m) $ 16447\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 $ 16448\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)} 16449\item {\tt diffvars} : sequence of variables $ x_{i_1},x_{i_2},\dots,x_{i_k} $ with respect to which is {\tt expr} differentiated 16450\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]} 16451\item {\tt y} : (list of) dependent variable(s) $ y_{j_1},y_{j_2},\dots,y_{j_l} $ that need to be differentiated 16452\end{itemize} 16453Dependent 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. 16454 16455When 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}. 16456 16457\noindent Input : 16458\begin{center} 16459{\tt implicitdiff(x*y,-2x\verb|^|3+15x\verb|^|2*y+11y\verb|^|3-24y=0,y(x),x)} 16460\end{center} 16461Output : 16462\begin{center} 16463{\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)} 16464\end{center} 16465In 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} $.\\ 16466Input : 16467\begin{center} 16468{\tt implicitdiff(x\verb|^|2*y+y\verb|^|2=1,y,x)} 16469\end{center} 16470Output : 16471\begin{center} 16472{\tt -2*x*y/(x\verb|^|2+2*y)} 16473\end{center} 16474Input : 16475\begin{center} 16476{\tt implicitdiff([x\verb|^|2+y=z,x+y*z=1],[y(x),z(x)],y,x)} 16477\end{center} 16478Output : 16479\begin{center} 16480{\tt (-2*x*y-1)/(y+z)} 16481\end{center} 16482In the next example, $ \frac{\mathrm{d}\,y}{\mathrm{d}\,x} $ and $ \frac{\mathrm{d}\,z}{\mathrm{d}\,x} $ are computed.\\ 16483Input : 16484\begin{center} 16485{\tt implicitdiff([-2x*z+y\verb|^|2=1,x\verb|^|2-exp(x*z)=y],}\\ 16486{\tt [y(x),z(x)],[y,z],x)} 16487\end{center} 16488Output : 16489\begin{center} 16490{\tt [2*x/(y*exp(x*z)+1),}\\ 16491{\tt (2*x*y-y*z*exp(x*z)-z)/(x*y*exp(x*z)+x)]} 16492\end{center} 16493 16494For the third case of input syntax, all partial derivatives of order equal to {\tt order\_size}, 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}}. \] 16495Input : 16496\begin{center} 16497{\tt f:=x*y*z; g:=-2x\verb|^|3+15x\verb|^|2*y+11y\verb|^|3-24y=0;}\\ 16498{\tt implicitdiff(f,g,[x,z,y],order\_size=1)} 16499\end{center} 16500Output : 16501\begin{center} 16502{\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),}\\ 16503{\tt x*y]} 16504\end{center} 16505Input : 16506\begin{center} 16507{\tt implicitdiff(f,g,order\_size=2,[1,-1,0])} 16508\end{center} 16509Output : 16510\begin{center} 16511{\tt [[64/9,-2/3],[-2/3,0]]} 16512\end{center} 16513In the next example, the value of $ \frac{\partial^4 f}{\partial x^4} $ is computed at point $ (x=0,y=0,z) $.\\ 16514Input : 16515\begin{center} 16516{\tt pd:=implicitdiff(f,g,[x,z,y],order\_size=4,[0,z,0]);}\\ 16517{\tt pd[4,0]} 16518\end{center} 16519Output : 16520\begin{center} 16521{\tt -2*z} 16522\end{center} 16523 16524 16525\section{Parametric curves and surfaces : {\tt plotparam paramplot DrawParm}}\index{plotparam|textbf}\index{paramplot|textbf}\index{DrawParm|textbf} 16526\subsection{2D parametric curve } 16527\noindent {\tt plotparam([f(t),g(t)],t)} 16528or {\tt plotparam(f(t)+i*g(t),t)} (resp. 16529{\tt plotparam(f(t)+i*g(t),t=t1..t2)}) 16530draws the parametric representation of the curve 16531defined by $x=f(t),y=g(t)$ 16532with the default range of values of $t$ (resp. for $t1 \leq t\leq t2$).\\ 16533The default range of values is taken as specified 16534in the graphic configuration ({\tt t-} and {\tt t+}, 16535cf. \ref{ssec:confgraph}). 16536{\tt plotparam} accepts an optional argument to specify the discretization 16537step for $t$ with {\tt tstep=}.\\ 16538Input : 16539\begin{center}{\tt plotparam(cos(x)+i*sin(x),x) }\end{center} 16540or : 16541\begin{center}{\tt plotparam([cos(x),sin(x)],x) }\end{center} 16542Output : 16543\begin{center}{\tt The unit circle}\end{center} 16544If in the graphic configuration {\tt t} goes from -4 to 1, input : 16545\begin{center}{\tt plotparam(sin(t)+i*cos(t))}\end{center} 16546or : 16547\begin{center}{\tt plotparam(sin(t)+i*cos(t),t=-4..1) }\end{center} 16548or : 16549\begin{center}{\tt plotparam(sin(x)+i*cos(x),x=-4..1) }\end{center} 16550Output : 16551\begin{center}{\tt the arc (sin(-4)+i*cos(-4),sin(1)+i*cos(1)) of the unit circle}\end{center} 16552If in the graphic configuration {\tt t} goes from -4 to 1, input : 16553\begin{center}{\tt plotparam(sin(t)+i*cos(t),t,tstep=0.5)}\end{center} 16554or : 16555\begin{center}{\tt plotparam(sin(t)+i*cos(t),t=-4..1,tstep=0.5)}\end{center} 16556Output : 16557\begin{center}{\tt A polygon approaching the arc (sin(-4)+i*cos(-4),sin(1)+i*cos(1)) of the unit circle}\end{center} 16558 16559\subsection{3D parametric surface : {\tt plotparam paramplot DrawParm}}\index{plotparam}\index{paramplot}\index{DrawParm} 16560\noindent{\tt plotparam} takes two main arguments, 16561a list of three 16562expressions of two variables and the list of these variable names 16563where each variable name may be replaced by variable=interval 16564to specify the range of the parameters. 16565It accepts an optional argument to specify 16566the discretization steps of the parameters $u$ and $v$ with 16567{\tt ustep=...} and {\tt vstep=...}.\\ 16568{\tt plotparam([f(u,v),g(u,v),h(u,v)],[u,v])} draws the surface defined by the 16569first argument : $x=f(u,v),y=g(u,v),z=h(u,v)$, where $u$ and $v$ 16570ranges default to the graphic configuration.\\ 16571Input : 16572\begin{center}{\tt plotparam([v*cos(u),v*sin(u),v],[u,v])}\end{center} 16573Output : 16574\begin{center}{\tt The cone $x=v*\cos(u),y=v*\sin(u),z=v$}\end{center} 16575To specify the range of each parameters, replace each variable 16576by an equation variable=range, like this: 16577\begin{center}{\tt plotparam([v*cos(u),v*sin(u),v],[u=0..pi,v=0..3]) }\end{center} 16578Output : 16579\begin{center}{\tt A portion of the cone $x=v*\cos(u),y=v*\sin(u),z=v$}\end{center} 16580Input : 16581\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} 16582Output : 16583\begin{center}{\tt A portion of the cone $x=v*\cos(u),y=v*\sin(u),z=v$}\end{center} 16584 16585\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} 16586\noindent Let $E_t$ be an expression depending on the variable $t$.\\ 16587{\tt plotpolar($E_t$,t)} draws the polar representation of the 16588curve defined by $\rho=E_t$ for $\theta=t$, that is 16589in cartesian coordinates the curve $(E_t \cos(t),E_t \sin(t))$. 16590The range of the parameter may be specified by replacing the second argument 16591by {\tt t=tmin..tmax}. The discretization parameter may be specified 16592by an optional {\tt tstep=...} argument.\\ 16593Input 16594\begin{center}{\tt plotpolar(t,t)}\end{center} 16595Output : 16596\begin{center}{\tt The spiral $\rho$=t is plotted}\end{center} 16597Input 16598\begin{center}{\tt plotpolar(t,t,tstep=1)}\end{center} 16599or : 16600\begin{center}{\tt plotpolar(t,t=0..10,tstep=1)}\end{center} 16601Output : 16602\begin{center}{\tt A polygon line approaching the spiral $\rho$=t is plotted}\end{center} 16603 16604\section{Graph of a recurrent sequence : {\tt plotseq seqplot graphe\_suite}}\index{plotseq}\index{seqplot}\index{graphe\_suite}\label{sec:plotseq} 16605\noindent Let $f(x)$ be an expression depending on the variable $x$ 16606(resp. $f(t)$ an expression depending on the variable $t$).\\ 16607{\tt plotseq($f(x)$,a,n)} (resp. {\tt plotseq($f(t)$,t=a,n)}) draws the line 16608$y=x$, the graph of $y=f(x)$ (resp. $y=f(t)$) and the $n$ first terms of the 16609recurrent sequence defined by : $u_0=a,\ \ u_n=f(u_{n-1})$. 16610The $a$ value may be replaced by a list of 3 elements, $[a,x_-,x_+]$ 16611where $x_-..x_+$ will be passed as $x$ range for the graph computation.\\ 16612Input : 16613\begin{center}{\tt plotseq(sqrt(1+x),x=[3,0,5],5)}\end{center} 16614Output : 16615\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} 16616 16617\section{Tangent field : {\tt plotfield fieldplot}}\index{plotfield}\index{fieldplot} 16618\begin{itemize} 16619\item Let $f(t,y)$ be an expression depending on two variables $t$ and $y$, 16620then : 16621\begin{center} 16622{\tt plotfield(f(t,y),[t,y])} 16623\end{center} 16624draws the tangent field of the 16625differential equation $y'=f(t,y)$ where $y$ is a real variable and 16626where $t$ is the abscissa, 16627\item Let $V$ be 16628a vector of two expressions depending on 2 variables $x,y$ but 16629independent of the time $t$, then 16630\begin{center} 16631{\tt plotfield(V,[x,y])} 16632\end{center} 16633draws the vector field $V$, 16634\item The range of values of $t,y$ or of $x,y$ can be specified with\\ 16635{\tt t=tmin..tmax}, {\tt x=xmin..xmax}, {\tt y=ymin..ymax}\\ 16636in place of the variable name. 16637\item The discretization may be specified with optional 16638arguments {\tt xstep=...}, {\tt ystep=...}. 16639\end{itemize} 16640Input : 16641\begin{center}{\tt plotfield(4*sin(t*y),[t=0..2,y=-3..7]) }\end{center} 16642Output : 16643\begin{center}{\tt Segments with slope 4*sin(t*y), representing tangents, are plotting in different points}\end{center} 16644With two variables $x,y$, input : 16645\begin{center} 16646{\tt plotfield(5*[-y,x],[x=-1..1,y=-1..1]) } 16647\end{center} 16648 16649\section{Plotting a solution of a differential equation : {\tt plotode odeplot}}\index{plotode}\index{odeplot} 16650\noindent Let $f(t,y)$ be an expression depending on two variables 16651$t$ and $y$. 16652\begin{itemize} 16653\item {\tt plotode($f(t,y)$,[t,y],[t0,y0])} draws the solution of 16654the differential equation $y'=f(t,y)$ crossing through 16655the point {\tt (t0,y0)} (i.e. such that $y(t_0)=y_0$) 16656\item 16657By default, $t$ goes in both directions. The range of value of $t$ 16658may be specified by the optional argument 16659{\tt t=tmin..tmax}. 16660\item 16661We can also represent, in the space or in the plane, 16662the solution of a differential equation 16663$y'=f(t,y)$ where $y=(X,Y)$ is a vector of size 2. 16664Just replace $y$ by the variable names $X,Y$ 16665and the initial value $y_0$ by the two initial values of the 16666variables at time $t_0$. 16667\end{itemize} 16668Input : 16669\begin{center}{\tt plotode(sin(t*y),[t,y],[0,1]) }\end{center} 16670Output : 16671\begin{center}{The graph of the solution of y'=sin(t,y) crossing through the point (0,1)}\end{center} 16672Input~: 16673\begin{center} 16674{\tt S:=odeplot([h-0.3*h*p, 0.3*h*p-p], [t,h,p],[0,0.3,0.7])} 16675\end{center} 16676Output, the graph in the space of the solution of : 16677\[ [h,p]'=[h-0.3 h*p, 0.3 h*p-p] \quad [h,p](0)=[0.3,0.7] \] 16678To have a 2-d graph (in the plane), use the option 16679{\tt plane} 16680\begin{center} 16681{\tt S:=odeplot([h-0.3*h*p, 0.3*h*p-p], [t,h,p],[0,0.3,0.7],plane)} 16682\end{center} 16683To compute the values of the solution, see 16684the subsection \ref{ssec:odesolve}. 16685 16686\section{Interactive plotting of solutions of a differential equation : {\tt interactive\_plotode interactive\_odeplot}}\index{interactive\_plotode}\index{interactive\_odeplot} 16687\noindent Let $f(t,y)$ be an expression depending on two 16688variables $t$ and $y$.\\ 16689{\tt interactive\_plotode(f(t,y),[t,y])} draws the tangent field 16690of the differential equation $y'=f(t,y)$ in a new window. 16691In this window, one can click on a point to get the 16692plot of the solution of $y'=f(t,y)$ crossing through this point.\\ 16693You can further click to display 16694several solutions. To stop press 16695the {\tt Esc} key.\\ 16696Input : 16697\begin{center}{\tt interactive\_plotode(sin(t*y),[t,y]) }\end{center} 16698Output : 16699\begin{center}{\tt The tangent field is plotted with the 16700 solutions of y'=sin(t,y) crossing through the points defined by 16701 mouse clicks}\end{center} 16702 16703\section{Animated graphs (2D, 3D or "4D")} 16704{\tt Xcas} can display animated 2D, 3D or "4D" graphs. 16705This is done first by computing 16706a sequence of graphic objects, then after completion, 16707by displaying the sequence in a loop. 16708\begin{itemize} 16709\item To stop or start again the animation, click on the button 16710$\blacktriangleright \mid$ (at the left of {\tt Menu}). 16711\item 16712The display time of each graphic object is specified in {\tt animate} of the 16713graph configuration ({\tt cfg} button). Put a small time, 16714to have a fast animation. 16715\item 16716If {\tt animate} is {\tt 0}, the animation is frozen, 16717you can move in the sequence of objects one by one by clicking 16718on the mouse in the graphic scene. 16719\end{itemize} 16720 16721\subsection{Animation of a 2D graph~: {\tt animate}}\index{animate} 16722\noindent{\tt animate} can create a 2-d animation with graphs of functions 16723depending on a parameter. The parameter is specified as the 16724third argument of 16725{\tt animate}, the number of pictures as fourth argument with 16726{\tt frames=}\index{frames@{\sl frames}|textbf}number, 16727the remaining arguments are the same as those of the {\tt plot} command, 16728see section \ref{sec:plot2d}, p. \pageref{sec:plot2d}.\\ 16729Input : 16730\begin{center} 16731{\tt animate(sin(a*x),x=-pi..pi,a=-2..2,frames=10,color=red)} 16732\end{center} 16733Output : 16734\begin{center}{\tt a sequence of graphic representations of y=sin($a$x) for 1673511 values of $a$ between -2 and 2}\end{center} 16736 16737\subsection{Animation of a 3D graph~: {\tt animate3d}}\index{animate3d} 16738\noindent{\tt animate3d} can create a 3-d animation with 16739function graphs depending on a parameter. The parameter is specified as 16740the third argument of {\tt animate3d}, the number of pictures 16741as fourth argument with 16742{\tt frames=}\index{frames@{\sl frames}}number, the remaining arguments 16743are the same as those of the {\tt plotfunc} command, see 16744section \ref{sec:plotfunc3}, p. \pageref{sec:plotfunc3}.\\ 16745Input : 16746\begin{center} 16747{\tt animate3d(x\verb|^|2+a*y\verb|^|2,[x=-2..2,y=-2..2],a=-2..2, frames=10,display=red+filled)} 16748\end{center} 16749Output : 16750\begin{center}{\tt a sequence of graphic representations 16751 of z=x\verb|^|2+$a$*y\verb|^|2 for 11 values of $a$ between -2 and 2} 16752\end{center} 16753 16754\subsection{Animation of a sequence of graphic objects~: {\tt animation}}\index{animation} 16755\noindent{\tt animation} animates the representation of a 16756sequence of graphic objects 16757with a given display time. The sequence of objects depends most of 16758the time on a parameter and is defined using the {\tt seq} command 16759but it is not mandatory.\\ 16760{\tt animation} takes as argument the sequence of graphic objects.\\ 16761To define a sequence of graphic objects with {\tt seq}, 16762enter the definition of the graphic object (depending on 16763the parameter), the parameter name, its minimum value, its 16764 maximum value maximum and optionally a step value.\\ 16765Input : 16766\begin{center}{\tt animation(seq(plotfunc(cos(a*x),x),a,0,10))}\end{center} 16767Output : 16768\begin{center}{\tt The sequence of the curves defined by $y=\cos(ax)$, for $a=0,1,2..10$}\end{center} 16769Input : 16770\begin{center} 16771{\tt animation(seq(plotfunc(cos(a*x),x),a,0,10,0.5))} 16772\end{center} 16773or : 16774\begin{center} 16775{\tt animation(seq(plotfunc(cos(a*x),x),a=0..10,0.5))} 16776\end{center} 16777Output : 16778\begin{center}{\tt The sequence of the curves defined by $y=\cos(ax)$, for $a=0,0.5,1,1.5..10$ }\end{center} 16779Input : 16780\begin{center}{\tt animation(seq(plotfunc([cos(a*x),sin(a*x)],x=0..2*pi/a), a,1,10))}\end{center} 16781Output : 16782\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} 16783Input : 16784\begin{center}{\tt animation(seq(plotparam([cos(a*t),sin(a*t)], t=0..2*pi),a,1,10))}\end{center} 16785Output : 16786\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} 16787Input : 16788\begin{center}{\tt animation(seq(plotparam([sin(t),sin(a*t)], t,0,2*pi,tstep=0.01),a,1,10))}\end{center} 16789Output : 16790\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} 16791Input : 16792\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} 16793Output : 16794\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} 16795Input : 16796\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} 16797Output : 16798\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} 16799Input : 16800\begin{center}{\tt animation(seq(display(square(0,1+i*a),filled),a,-5,5))}\end{center} 16801Output : 16802\begin{center}{\tt The sequence of the squares defined by the points 0 and 1+i*$a$ for $a=-5..5$}\end{center} 16803Input : 16804\begin{center}{\tt animation(seq(droite([0,0,0],[1,1,a]),a,-5,5))}\end{center} 16805Output : 16806\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} 16807Input : 16808\begin{center}{\tt animation(seq(plotfunc(x\verb|^|2-y\verb|^|a,[x,y]),a=1..3))}\end{center} 16809Output : 16810\begin{center}{\tt The sequence of the "3D" surface defined by $x^2-y^a$, for $a=1..3$ with rainbow colors}\end{center} 16811Input : 16812\begin{center}{\tt animation(seq(plotfunc((x+i*y)\verb|^|a,[x,y], display=filled),a=1..10)}\end{center} 16813Output : 16814\begin{center}{\tt The sequence of the "4D" surfaces defined by $(x+i*y)^a$, for $a=0..10$ with rainbow colors}\end{center} 16815 16816{\bf Remark} 16817We may also define the sequence with a program, 16818for example if we want to draw the 16819segments of length $1,\sqrt 2...\sqrt 20$ constructed with a 16820right triangle of side 1 and the previous segment 16821(note that there is a {\tt c:=evalf(..)} statement 16822to force approx. evaluation otherwise the computing time 16823would be too long) : 16824\begin{verbatim} 16825seg(n):={ 16826 local a,b,c,j,aa,bb,L; 16827 a:=1; 16828 b:=1; 16829 L:=[point(1)]; 16830 for(j:=1;j<=n;j++){ 16831 L:=append(L,point(a+i*b)); 16832 c:=evalf(sqrt(a^2+b^2)); 16833 aa:=a; 16834 bb:=b; 16835 a:=aa-bb/c; 16836 b:=bb+aa/c; 16837 } 16838 L; 16839} 16840\end{verbatim} 16841Then input : 16842\begin{center}{\tt animation(seg(20))}\end{center} 16843We see, each point, one to one with a display time that 16844depends of the {\tt animate} value in {\tt cfg}.\\ 16845or : 16846\begin{center}{\tt L:=seg(20); s:=segment(0,L[k])\$(k=0..20)}\end{center} 16847We see 21 segments. \\ 16848Then, input : 16849\begin{center}{\tt animation(s)}\end{center} 16850We see, each segment, one to one with a display time that 16851depends of the {\tt animate} value in {\tt cfg}. 16852 16853\chapter{Statistics}\label{sec:stat} 16854 16855\section{One variable statistics} 16856 16857\texttt{Xcas} has several functions to perform statistics; the data is 16858typically given as a list of numbers, such as \texttt{A := 16859[0,1,2,3,4,5,6,7,8,9,10,11]}. We will use this particular list in 16860several examples. Section \ref{sec:statmat} will discuss statistics on 16861matrices. 16862 16863\subsection{The mean: \texttt{mean}\index{mean}} 16864 16865Recall that the mean of a list $x_1,\dots,x_n$ is simply their numeric 16866average $(x_1 + \cdots + x_n)/n$. \texttt{Xcas} can calculate the 16867mean of a list of numbers with the \texttt{mean} command. 16868If you enter 16869\begin{center} 16870 \tt 16871 mean([1,2,3,4]) 16872\end{center} 16873then you will get 16874\begin{center} 16875 \tt 16876 5/2 16877\end{center} 16878since $(1+2+3+4)/4 = 5/2$. 16879If you give \texttt{mean} a matrix as an argument, then it will return a 16880list with the numeric average of each column; 16881\begin{center} 16882 \tt 16883 mean([[1,2,3],[5,6,7]]) 16884\end{center} 16885will return 16886\begin{center} 16887 \tt 16888 [3,4,5] 16889\end{center} 16890since $(1+5)/2 = 3$, $(2+6)/2 = 4$ and $(3 + 7)/2 = 5$. 16891 16892To get the weighted average of a list of numbers you can give 16893\texttt{mean} a second argument, which should be a list of the 16894weights. For example, 16895\begin{center} 16896 \tt 16897 mean([2,4,6,8],[2,2,3,3]) 16898\end{center} 16899will return 16900\begin{center} 16901 \tt 16902 27/5 16903\end{center} 16904since $(2\cdot 2 + 4\cdot 2 + 6\cdot 3 + 8\cdot 3)/(2 + 2 + 3 + 3) = 1690527/5$. 16906Similarly, you can find the weighted average of the columns of a 16907matrix by giving \texttt{mean} a second argument of a matrix of 16908weights. If you enter 16909\begin{center} 16910 \tt 16911 mean([[1,2],[3,4]],[[1,2],[2,1]]) 16912\end{center} 16913then you will get 16914\begin{center} 16915 \tt 16916 [7/3,8/3] 16917\end{center} 16918since $(1\cdot 1 + 3\cdot 2)/(1+2) = 7/3$ and $(2\cdot 2 + 4\cdot 1)/(2 + 169191) = 8/3$. 16920 16921\subsection{Variance and standard deviation: \texttt{variance}\index{variance} \texttt{stdev}\index{stdev}} 16922 16923The variance of a list of numbers measures how close the numbers are 16924to their mean by finding the average of the squares of the 16925differences between the numbers and the mean; specifically, 16926given a list of numbers $[x_1,\dots,x_n]$ with mean $\mu = (x_1 + 16927\cdots + x_n)/n$, the variance is 16928\[ 16929\frac{(x_1 - \mu)^2 + \dots + (x_n - \mu)^2}{n}. 16930\] 16931The squares help ensure that the numbers above the mean and those 16932below the mean don't cancel out. The variance can be computed with 16933the command \texttt{variance}, 16934 16935A potentially better way to measure how close numbers are to their 16936mean is the standard deviation, which is the square root of the 16937variance;. Note that if the list of numbers have units, then the 16938standard deviation will have the same unit. 16939The \texttt{stddev} function will compute the standard 16940deviation of a list of numbers. For example, 16941the list $[1,2,3,4]$ has mean $5/2$, and so 16942\texttt{stddev([1,2,3,4])} 16943will return 16944\begin{center} 16945 \tt 16946 2*sqrt(5)/4 16947\end{center} 16948since 16949\[ \sqrt{\frac{(1-5/2)^2 + (2-5/2)^2 + (3-5/2)^2 + (4-5/2)^2}{4}} = 16950\frac{2\sqrt{5}}{4} 16951\] 16952Like the mean, given a matrix, \texttt{stddev} will compute the 16953standard deviation of each column separately; 16954\begin{center} 16955 \tt 16956 stddev([[1,2],[3,6]]) 16957\end{center} 16958will compute 16959\begin{center} 16960 \tt 16961 [1,2] 16962\end{center} 16963Also, a second list (or matrix) as an argument will provide weights 16964when finding the standard deviation; 16965\begin{center} 16966 \tt 16967 stddev([1,2,3],[2,1,1]) 16968\end{center} 16969will return 16970\begin{center} 16971 \tt 16972 4*sqrt(11)/16 16973\end{center} 16974 16975\subsection{The population standard deviation: \texttt{stddevp}\index{stddevp} \texttt{stdDev}\index{stdDev}} 16976 16977Given a large population, rather than collecting all of the numbers it 16978might be more feasible to get a smaller collection of numbers and try 16979to extrapolate from that. For example, to get information about the 16980ages of a large population, you might get the ages of a sample of 100 16981of the people and work with that. 16982 16983If a list of numbers is a sample of data from a larger population, 16984then the \texttt{mean} function will find the mean of the sample, 16985which can be used to estimate the mean of the population. The 16986standard deviation uses the mean to find the standard 16987deviation of the sample, but since the mean of the sample is only an 16988approximation to the mean of the entire population, the standard 16989deviation of the sample doesn't provide an optimal estimate of 16990the standard deviation of the population. An unbiased estimate of the 16991standard deviation of the entire population is given by the population 16992standard deviation \texttt{stddevp} function; 16993given a list $L = [x_1,\dots,x_n]$ with mean $\mu$, the population 16994standard deviation is 16995\[ 16996\sigma = \sqrt{\frac{(x_1 - \mu)^2 + \dots + (x_n - \mu)^2}{n-1}}. 16997\] 16998Note that 16999\[ \text{stddevp}(L)^2 = \frac{n}{n-1} \text{stddev}(L)^2.\] 17000 17001For example, 17002\begin{center} 17003 \tt 17004 stddev([1,2,3,4]) 17005\end{center} 17006will return 17007\begin{center} 17008 \tt 17009 sqrt(5)/2 17010\end{center} 17011while 17012\begin{center} 17013 \tt 17014 stddevp([1,2,3,4]) 17015\end{center} 17016will return 17017\begin{center} 17018 \tt 17019 sqrt(15)/3 17020\end{center} 17021 17022Like \texttt{stddev}, the \texttt{stddevp} command can take a second 17023argument for weights. If you enter 17024\begin{center} 17025 \tt 17026 A := [0,1,2,3,4,5,6,7,8,9,10,11]\\ 17027 stddevp(A,A) 17028\end{center} 17029then you will get 17030\begin{center} 17031 \tt 17032 sqrt(66)/3 17033\end{center} 17034 17035The \texttt{stdDev} function is equivalent to \texttt{stddevp}, for TI 17036compatibility. There is no population variance function; if needed, 17037it can be computed by squaring the \texttt{stddevp} function. 17038 17039\subsection{The median: \texttt{median}\index{median}} 17040 17041Although the average of a list of numbers typically means the mean, 17042there are other notions of ``average''. Another one is the median; 17043the median of a list of numbers is the middle number when they are 17044listed in numeric order. For example, the median of the list 17045$[1,2,5,7,20]$ is simply $5$. If the length of a list of numbers is 17046even, so there isn't a middle number, the median is then the mean of 17047the two middle numbers; for example, the median of $[1,2,5,7,20,21]$ is 17048$(5+7)/2 = 6$. 17049 17050The \texttt{median} function finds the median of a list. The command 17051\begin{center} 17052 \tt 17053 median([1,2,5,7,20]) 17054\end{center} 17055will return 17056\begin{center} 17057 \tt 17058 5 17059\end{center} 17060The \texttt{median} function can take weights with a second argument, 17061where the weight of number represents how many times it is counted in 17062a list. For example, 17063\begin{center} 17064 \tt 17065 median([1,2,5,7,20],[5,3,2,1,2]) 17066\end{center} 17067will return 17068\begin{center} 17069 \tt 17070 2 17071\end{center} 17072since the median of $1,1,1,1,1,2,2,2,5,5,7,20,20$ is $2$. 17073 17074\subsection{Quartiles: \texttt{quartiles}\index{quartiles} \texttt{quartile1}\index{quartile1} \texttt{quartile3}\index{quartile3}} 17075 17076Recall that the quartiles of a list of numbers divide it into four 17077equal parts; the first quartile is the number $q_1$ such that 17078one-fourth of the list numbers fall below $q_1$; i.e., the median of 17079that part of the list which fall at or below the list median. The 17080second quartiles is the number $q_2$ such that half of the list 17081numbers fall at or below $q_2$; more specifically, the median of the 17082list. And of course the third quartile is the number $q_3$ such that 17083three-fourths of the list numbers fall at or below $q_3$. 17084 17085The function \texttt{quartiles} takes a list and returns a column 17086vector consisting of the minimum of the list, the first quartile, the second 17087quartile, the third quartile and the maximum. If you enter 17088\begin{center} 17089 \tt 17090 A := [0,1,2,3,4,5,6,7,8,9,10,11];\\ 17091 quartiles(A) 17092\end{center} 17093you will get 17094\begin{center} 17095 \tt 17096 [[0.0],[2.0],[5.0],[8.0],[11.0]] 17097\end{center} 17098 17099You can get the individual entries of this vector with the commands 17100\texttt{min}, \texttt{quartile1}, \texttt{median}, \texttt{quartile2} 17101and \texttt{max}. 17102 17103Just as with median, the \texttt{quartiles} function can take a second 17104argument consisting of weights for the first argument; for example, 17105\begin{center} 17106 \tt 17107 quartiles(A,A) 17108\end{center} 17109would return 17110\begin{center} 17111 \tt 17112 [0,6,8,10,11] 17113\end{center} 17114 17115\subsection{Quantiles: \texttt{quantile}\index{quantile}} 17116 17117Similar to quartiles, a quantile of a list is the number $q$ such that 17118a given fraction of the list numbers fall at or below $q$. The first 17119quartile, for example, is the quantile with the fraction 0.25. 17120 17121The command \texttt{quantile} takes a list of numbers and a value $p$ 17122between 0 and 1 as arguments and returns the $p$th quantile. For 17123example, 17124\begin{center} 17125 \tt 17126 A := [0,1,2,3,4,5,6,7,8,9,10,11]\\ 17127 quantile(A,0.1) 17128\end{center} 17129returns the quantile with $p = 0.1$ (the first decile): 17130\begin{center} 17131 \tt 17132 1.0 17133\end{center} 17134 17135Like \texttt{quartile}, the \texttt{quantile} command can take an 17136argument representing weights of the list; the weights can be given as 17137a second argument and then the value $p$ will be the third. The command 17138\begin{center} 17139 \tt 17140 quantile(A,A,0.25) 17141\end{center} 17142will return 17143\begin{center} 17144 \tt 17145 6 17146\end{center} 17147 17148\subsection{The boxwhisker: \texttt{boxwhisker}\index{boxwhisker} \texttt{mustache}\index{mustache}} 17149 17150A boxwhisker is a graphical view of the quartiles of a list of numbers. 17151The boxwhisker consists of a line segment from the the minimum of the 17152list to the first quartile, leading to a rectangle from the first 17153quartile to the third quartile, followed by a line segment from the 17154third quartile to the maximum of the list. The rectangle will contain 17155a vertical segment indicating the median, and the two line segments 17156will contain vertical lines indicating the first and ninth decile. 17157 17158The \texttt{boxwhisker} (or \texttt{mustache}) command will create a 17159boxwhisker for a list. For example, if you enter 17160\begin{center} 17161 \tt 17162 boxwhisker([-1,1,2,2.2,3,4,-2,5]) 17163\end{center} 17164a graphic window will appear showing the boxwhisker, 17165\begin{center} 17166 \includeimage{xcas-boxwhisker.png} 17167\end{center} 17168 17169\subsection{Classes: \texttt{classes}\index{classes}} 17170 17171The \texttt{classes} command can be used to groups a collection of 17172numbers into intervals; the result will be a list where each element 17173is an interval \texttt{a..b} followed by how many of the numbers are 17174in the interval $[a,b)$. The collection of numbers can be given as a 17175list or matrix. 17176 17177If $L$ is a collection of numbers, $a$ and $b$ are numbers, then 17178\texttt{classes($L$,$a$,$b$)} will return the list 17179\texttt{[[$a$..$a+b$,$n_1$],[$a+b$..$a+2b$,$n_2$],...]} where each number in $L$ is 17180in one of the intervals $[a+kb, a+(k+1)b)$ and $n_k$ is 17181how many numbers from $L$ are in the corresponding interval. For 17182example, 17183\begin{center} 17184 \tt 17185 classes([0,0.5,1,1.5,2,2.5,3,3.5,4],0,2) 17186\end{center} 17187will return 17188\begin{center} 17189 \tt 17190 [[0.0 .. 2.0,4],[2.0 .. 4.0,4],[4.0 .. 6.0,1]] 17191\end{center} 17192while 17193\begin{center} 17194 \tt 17195 classes([0,0.5,1,1.5,2,2.5,3,3.5,4],-1,2) 17196\end{center} 17197will return 17198\begin{center} 17199 \tt 17200 [[(-1.0) .. 1.0,2],[1.0 .. 3.0,4],[3.0 .. 5.0,3]] 17201\end{center} 17202If the numbers $a$ and $b$ are omitted, they will default to the 17203configurable values of \texttt{class\_min} and \texttt{class\_size}, 17204which default to $0$ and $1$. 17205 17206Another way to split the list $L$ into intervals is by making the 17207third argument the midpoints of the desired intervals. For example, 17208if you enter 17209\begin{center} 17210 \tt 17211 classes([0,0.5,1,1.5,2,2.5,3,3.5,4],1,[1,3,5]) 17212\end{center} 17213you will get 17214\begin{center} 17215 \tt 17216 [[0.0..2.0,4],[2.0..4.0,4],[4.0..6.0,1]] 17217\end{center} 17218 17219Finally, you can simply state the intervals that you want to use by 17220giving them as a list for the second argument. In this case, not 17221every number in the list is necessarily in one of the intervals. If 17222you enter 17223\begin{center} 17224 \tt 17225 classes([0,0.5,1,1.5,2,2.5,3,3.5,4],[1..3,3..6]) 17226\end{center} 17227you will get 17228\begin{center} 17229 \tt 17230 [[1 .. 3,4],[3 .. 6,3]] 17231\end{center} 17232 17233\subsection{Histograms: \texttt{histogram}\index{histogram} \texttt{histogramme}\index{histogramme}} 17234 17235Given a list of intervals and a number of points in each interval, 17236such as is given by the output of the \texttt{classes} command, the 17237\texttt{histogram} (or \texttt{histogramme}) command will draw a box 17238over each interval so that the height of each box is proportional to 17239the number of points and the total area of the boxes is 1. For 17240example, if you enter 17241\begin{center} 17242 \tt 17243 histogram([[1.5..1.65,50],[1.65..1.7,20],[1.7..1.8,30]]) 17244\end{center} 17245you will get 17246\begin{center} 17247 \includeimage{xcas-histogram1.png} 17248\end{center} 17249 17250If you just give the \texttt{histogram} a list of numbers, or a list 17251with values $a$ and $b$, then you will get the histogram of the result 17252of applying \texttt{classes} to the list. 17253% will start at 0 and draw boxes over intervals of width 1, such that 17254% the height of the boxes are proportional to the number of list 17255% elements in the interval and the total area will be 1. For example, 17256% if you enter 17257% \begin{center} 17258% \tt 17259% histogram([1,2,2.5,2.5,3]) 17260% \end{center} 17261% you will get 17262% PIC HISTOGRAM1 17263% You can change the starting point and interval width (from 0 and 1, 17264% respectively) by giving \texttt{histogram} second and third arguments; 17265For example, if you enter 17266\begin{center} 17267 \tt 17268 histogram([1,2,2.5,2.5,3],0.5,0.75) 17269\end{center} 17270you will get 17271\begin{center} 17272 \includeimage{xcas-histogram2.png} 17273\end{center} 17274 17275\subsection{Accumulating terms: 17276\texttt{accumulate\_head\_tail}\index{accumulate\_head\_tail}} 17277 17278The first terms and last terms of a list can be accumulated by 17279replacing them with their sum using the \texttt{accumulate\_head\_tail} 17280command. This command takes the list, the number of initial terms to 17281sum, and the number of end terms to add, and returns the list with the 17282initial terms and end terms replaced by their sums. For example, the 17283command 17284\begin{center} 17285 \tt 17286 accumulate\_head\_tail([1,2,3,4,5,6,7,8,9,10],3,4) 17287\end{center} 17288will return 17289\begin{center} 17290 \tt 17291 [6,4,5,6,34] 17292\end{center} 17293 17294\subsection{Frequencies: \texttt{frequencies}\index{frequencies} \texttt{frequences}\index{frequences}} 17295 17296Given a list of numbers, the \texttt{frequencies} (or 17297\texttt{frequences}) command will return 17298the numbers in the list with their frequencies; i.e., the fraction of 17299list items equal to the number. For example, 17300\begin{center} 17301 \tt 17302 frequencies([1,2,1,1,2,1,2,4,3,3]) 17303\end{center} 17304will return 17305\begin{center} 17306 \tt 17307 [[1,0.4],[2,0.3],[3,0.2],[4,0.1]] 17308\end{center} 17309 17310You can use this, for example, to simulate flipping a fair coin and 17311seeing how many times each side appears; to flip a coin 1000 times, 17312for example, you can enter 17313\begin{center} 17314 \tt 17315 frequencies([rand(2) \$ (k=1..1000)]) 17316\end{center} 17317and you might get 17318\begin{center} 17319 \tt 17320 [[0,0.513],[1,0.487]] 17321\end{center} 17322 17323\subsection{Cumulative frequencies: \texttt{cumulated\_frequencies}\index{cumulated\_frequencies} \texttt{frequences\_cumulees}\index{frequences\_cumulees}} 17324 17325Given a list, the \texttt{cumulated\_frequencies} command will plot 17326the cumulated frequency of the numbers in the list; i.e., the area 17327under the resulting graph at a value $x$ will be the fraction of 17328numbers less than $x$. For example, if you enter 17329\begin{center} 17330 \tt 17331 cumulated\_frequencies([1,2,1,1,2,1,2,4,3,3]) 17332\end{center} 17333then you will get 17334\begin{center} 17335 \includeimage{xcas-cumfreq1.png} 17336\end{center} 17337 17338The \texttt{cumulated\_frequencies} command can also take a matrix 17339with two columns as an argument. In this case, the first column will 17340represent values while the second column will represent the number of 17341times the values occur. For example, the above graph can be drawn 17342with the command 17343\begin{center} 17344 \tt 17345 cumulated\_frequencies([[1,4],[2,3], [3,2], [4,1]]) 17346\end{center} 17347 17348If the first column of the input matrix contains intervals 17349\texttt{$a$..$b$} instead of numbers, then the second column values will 17350be normalized to add up to one, and will represent the frequencies of 17351the intervals. If the matrix has the form 17352\[ [[a_0..a_1,f_1],...,[a_{n-1}..a_n,f_n]] \] 17353then the plot will consist of the polygonal path starting at $(a_0,0)$ 17354and moving to $(a_1,f_1)$ to $(a_2,f_1+f_2)$ and so on until 17355$(a_n,f_1+\cdots + f_n)$. For example, both 17356\begin{center} 17357 \tt 17358 cumulated\_frequencies([[1..2,30],[2..4,40],[4..5,30]]) 17359\end{center} 17360and 17361\begin{center} 17362 \tt 17363 cumulated\_frequencies([[1..2,03],[2..4,0.4],[4..5,0.3]]) 17364\end{center} 17365will give you 17366\begin{center} 17367 \includeimage{xcas-cumfreqint.png} 17368\end{center} 17369 17370If the matrix given to \texttt{cumulated\_frequencies} has more than 17371two columns, then each additional column will represent a different 17372distribution of the numbers in the first column, and each distribution 17373will be graphed. For example, if you enter 17374\begin{center} 17375 \tt 17376 cumulated\_frequencies([[1,4,1],[2,3,4], [3,2,1], [4,1,2]]) 17377\end{center} 17378then both the distributions given by 17379\texttt{[[1,4],[2,3], [3,2], [4,1]]} 17380and 17381\texttt{[[1,1],[2,4], [3,1], [4,2]]} 17382will be drawn on the same axes; the result will be 17383\begin{center} 17384 \includeimage{xcas-cumfreq2.png} 17385\end{center} 17386 17387\subsection{Bar graphs: \texttt{bar\_plot}\index{bar\_plot}} 17388 17389You can draw bar graphs with the \texttt{bar\_plot} command. You give 17390it a list, whose elements are pairs of labels and values, and the 17391result will be a bar graph with a bar for each label, whose height is 17392given by the corresponding value. For example, if you enter 17393\begin{center} 17394 \tt 17395 bar\_plot([["France", 6],["Germany", 12], ["Switzerland", 5]]) 17396\end{center} 17397you will get 17398\begin{center} 17399 \includeimage{xcas-barplot1.png} 17400\end{center} 17401 17402If you have more than one set of values for each label, you can use 17403\texttt{bar\_plot} to draw several bar graphs at the same time by 17404including all values for each label, with a list of identifiers for 17405the bar graphs given by the first argument. If you enter 17406\begin{center} 17407 \tt 17408 bar\_plot([[2,"xyz","abc"],["A",2,5],["B",5,6],["C",6,6]]) 17409\end{center} 17410you will get 17411\begin{center} 17412 \includeimage{xcas-barplot2.png} 17413\end{center} 17414 17415\subsection{Pie charts: \texttt{camembert}\index{camembert}} 17416 17417You can draw pie charts using the same structure as bar graphs, but 17418with the command \texttt{camembert}. If you enter 17419\begin{center} 17420 \tt 17421 camembert([["France", 6],["Germany", 12], ["Switzerland", 5]]) 17422\end{center} 17423you will get 17424\begin{center} 17425 \includeimage{xcas-piechart1.png} 17426\end{center} 17427and if you enter 17428\begin{center} 17429 \tt 17430 camembert([[2,"xyz","abc"],["A",2,5],["B",5,6],["C",6,6]]) 17431\end{center} 17432you will get 17433\begin{center} 17434 \includeimage{xcas-piechart2.png} 17435\end{center} 17436 17437\section{Two variable statistics} 17438 17439\subsection{Covariance and correlation: \texttt{covariance}\index{covariance} \texttt{correlation}\index{correlation} \texttt{covariance\_correlation}\index{covariance\_correlation}} 17440 17441The covariance of two random variables measures their connectedness; 17442i.e., whether they tend to change with each other. 17443If $X$ and $Y$ are two random variables, then the covariance is the 17444expected value of $(X-\bar{X})(Y-\bar{Y})$, where $\bar{X}$ and 17445$\bar{Y}$ are the means of $X$ and $Y$, respectively. You can 17446calculate covariances with the \texttt{covariance} command. 17447 17448If $X$ and $Y$ are given by lists of the same size, then 17449\texttt{covariance($X$,$Y$)} will return their covariance. For example, 17450if you enter 17451\begin{center} 17452 \tt 17453 covariance([1,2,3,4],[1,4,9,16]) 17454\end{center} 17455then you will get 17456\begin{center} 17457 \tt 17458 25/4 17459\end{center} 17460Alternatively, you could use a matrix with two columns instead of two 17461lists to enter $X$ and $Y$; the command 17462\begin{center} 17463 \tt 17464 covariance([[1,1],[2,4],[3,9],[4,16]]) 17465\end{center} 17466is another way to enter the above calculation. 17467 17468If the entries in the lists $X=[a_0,\dots,a_{n-1}]$ and 17469$Y=[b_0,\dots,b_{n-1}]$ have different weights, say $a_j$ and $b_j$ have 17470weight $w_j$, then \texttt{covariance} can be given a third list 17471$W=[w_0,\dots,w_{n-1}]$ (or alternatively, you could use a matrix with 17472three columns). For example, if you enter 17473\begin{center} 17474 \tt 17475 covariance([1,2,3,4],[1,4,9,16],[3,1,5,2]) 17476\end{center} 17477then you will get 17478\begin{center} 17479 \tt 17480 662/121 17481\end{center} 17482 17483If each pair of entries in the lists $X=[a_0,\dots,a_{m-1}]$ and 17484$Y=[b_0,\dots,b_{0}]$ have different weights, say $a_j$ and $b_k$ have 17485weight $w_{jk}$, then \texttt{covariance} can be given a third 17486argument of an $m\times n$ matrix $W=(w_{jk})$. (Note that in this 17487case the lists $X$ and $Y$ don't have to be the same length.) For 17488example, the covariance computed above could also have been computed 17489by entering 17490\begin{center} 17491 \tt 17492 covariance([1,2,3,4],[1,4,9,16], 17493 [[3,0,0,0],[0,1,0,0],[0,0,5,0],[0,0,0,2]]) 17494\end{center} 17495which would of course return 17496\begin{center} 17497 \tt 17498 662/121 17499\end{center} 17500In this case, to make it simpler to enter the data in a spreadsheet, 17501the lists $X$ and $Y$ and the matrix $W$ can be combined into a single 17502matrix, by augmenting $W$ with the list $Y$ on the top and the 17503transpose of the list $X$ on the left, with a filler in the upper left 17504hand corner; 17505\[ 17506\begin{pmatrix} 17507"XY" & Y\\ 17508X^T & W 17509\end{pmatrix} 17510\] 17511When you use this method, you need to give \texttt{covariance} a 17512second argument of -1. The above covariance can then be computed with 17513the command 17514\begin{center} 17515 \tt 17516 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) 17517\end{center} 17518 17519The linear correlation coefficient of two random variables is another 17520way to measure their connectedness. Given random variables $X$ and 17521$Y$, their correlation is defined as 17522$cov(X,Y)/(\sigma(X)\sigma(Y))$, where $\sigma(X)$ and $\sigma(Y)$ are 17523the standard deviations of $X$ and $Y$, respectively. The correlation 17524can be computed with the \texttt{correlation} command, which takes the 17525same types of arguments as the \texttt{covariance} command. If you 17526enter 17527\begin{center} 17528 \tt 17529 correlation([1,2,3,4],[1,4,9,16]) 17530\end{center} 17531you will get 17532\begin{center} 17533 \tt 17534 100/(4*sqrt(645)) 17535\end{center} 17536 17537The \texttt{covariance\_correlation} command will compute both the 17538covariance and correlation simultaneously, and return a list with both 17539values. This command takes the same type of arguments as the 17540\texttt{covariance} and \texttt{correlation} commands. For example, 17541if you enter 17542\begin{center} 17543 \tt 17544 covariance\_correlation([1,2,3,4],[1,4,9,16]) 17545\end{center} 17546you will get 17547\begin{center} 17548 \tt 17549 [25/4,100/(4*sqrt(645))] 17550\end{center} 17551 17552\subsection{Scatterplots: \texttt{scatterplot}\index{scatterplot} 17553\texttt{nuaged\_points}\index{nuage\_points} \texttt{batons}\index{batons}} 17554 17555A scatter plot is simply a set of points plotted on axes. You can 17556draw a scatter plot with the \texttt{scatterplot} or 17557\texttt{nuage\_points} command. 17558 17559You can call \texttt{scatterplot} with a matrix with two columns 17560(essentially, a list of points) or a list of 17561$x$-coordinates followed by a list of $y$-coordinates. If you enter 17562\begin{center} 17563 \tt 17564 scatterplot([[0,0],[1,1],[2,4],[3,9],[4,16]]) 17565\end{center} 17566or 17567\begin{center} 17568 \tt 17569 scatterplot([0,1,2,3,4],[0,1,4,9,16]) 17570\end{center} 17571you will get 17572\begin{center} 17573 \includeimage{xcas-scatterplot.png} 17574\end{center} 17575 17576If you want the points connected to the $x$-axis, the \texttt{batons} 17577command will take the same arguments at \texttt{scatterplot} and plot 17578the points with a vertical line segment connecting them to the 17579$x$-axis. If you enter 17580\begin{center} 17581 \tt 17582 batons([[0,0],[1,1],[2,4],[3,9],[4,16]]) 17583\end{center} 17584you will get 17585\begin{center} 17586 \includeimage{xcas-batons.png} 17587\end{center} 17588 17589\subsection{Polygonal paths: \texttt{polygonplot}\index{polygonplot} 17590\texttt{ligne\_polygonale}\index{ligne\_polygonale} 17591\texttt{linear\_interpolate}\index{linear\_interpolate} 17592\texttt{listplot}\index{listplot} \texttt{plotlist}\index{plotlist}} 17593 17594You can draw a polygonal path with either \texttt{polygonplot} or 17595\texttt{listplot}. 17596 17597Given a list of points (a two-column matrix) or two lists (the $x$ 17598coordinates and the $y$-coordinates), the \texttt{polygonplot} (or 17599\texttt{polygonscatterplot}) command 17600will draw the polygonal path through the points, from left to right 17601(so the points are automatically ordered by increasing 17602$x$-coordinates). If you enter 17603\begin{center} 17604 \tt 17605 polygonplot([0,1,2,3,4],[0,1,4,9,16]) 17606\end{center} 17607or 17608\begin{center} 17609 \tt 17610 polygonplot([[0,0],[1,1],[2,4],[3,9],[4,16]]) 17611\end{center} 17612or even 17613\begin{center} 17614 \tt 17615 polygonplot([[2,4],[0,0],[3,9],[1,1],[4,16]]) 17616\end{center} 17617you will get 17618\begin{center} 17619 \includeimage{xcas-polygonplot.png} 17620\end{center} 17621 17622If you give \texttt{polygonplot} a single list of numbers, then they will 17623be taken to be the $y$-coordinates and the $x$-coordinates will be 17624assumed to be integers starting at $0$. If you enter 17625\begin{center} 17626 \tt 17627 polygonplot([0,1,4,9,16]) 17628\end{center} 17629 17630If you want to get coordinates on the polygonal path, you can use the 17631\texttt{linear\_interpolate} command. This command takes four 17632arguments; a two-row matrix consisting of the $x$-coordinates and the 17633$y$-coordinates, $x_{min}$, the minimum value of $x$ that you are 17634interested in, $x_{max}$, the maximum value of $x$, and 17635$x_{step}$, the step size you want. (The values of $x_{min}$ 17636and $x_{max}$ must be between the smallest and largest 17637$x$-coordinates of the points.) You will get a matrix with 17638two rows, the first row will be 17639$[x_{min}, x_{min}+x_{step}, x_{min}+2x_{step},...,x_{max}]$ 17640and the second row will be the corresponding $y$-coordinates of the 17641points on the polygonal path. For example, if you enter 17642\begin{center} 17643 \tt 17644 linear\_interpolate([[1,2,6,9],[3,4,6,12]],2,7,1) 17645\end{center} 17646you will get 17647\begin{center} 17648 \tt 17649 [[2.0,3.0,4.0,5.0,6.0,7.0],[4.0,4.5,5.0,5.5,6.0,8.0]] 17650\end{center} 17651 17652If you want to draw a polygonal path through points in an order 17653determined by you, you can use the \texttt{listplot} (or 17654\texttt{plotlist}) command. If you give \texttt{listplot} a list of 17655points, then you will get a polygonal path through the points in the 17656order given by the list. If you enter 17657\begin{center} 17658 \tt 17659 listplot([[2,4],[0,0],[3,9],[1,1],[4,16]]) 17660\end{center} 17661you will get 17662\begin{center} 17663 \includeimage{xcas-listplot.png} 17664\end{center} 17665 17666As with \texttt{polygonplot}, if you give \texttt{listplot} a single 17667list of numbers, then they will be taken to be the $y$-coordinates and 17668the $x$-coordinates will be assumed to be integers starting at $0$. 17669If you enter 17670\begin{center} 17671 \tt 17672 listplot([0,1,4,9,16]) 17673\end{center} 17674you will get the same graph that you got with \texttt{polygonplot}. 17675However, unlike \texttt{polygonplot}, the \texttt{listplot} command 17676can't be given two lists of numbers as arguments. 17677 17678\subsection{Linear regression: \texttt{linear\_regression}\index{linear\_regression} \texttt{linear\_regression\_plot}\index{linear\_regression\_plot}} 17679 17680Given a set of points $(x_0,y_0),\dots,(x_{n-1},y_{n-1})$, linear 17681regression finds the line $y=mx+b$ that comes 17682closest to passing through all of the points; i.e., that makes 17683$\sqrt{(y_0 - (m x_0 + b))^2 + \dots + (y_{n-1} - (m x_{n-1} + b))^2}$ 17684as small as possible. Given a set of points (a two-column matrix) or 17685two lists of numbers (the $x$- and $y$-coordinates), the 17686\texttt{linear\_regression} command will find the values of $m$ and 17687$b$ which determine the line. For example, if you enter 17688\begin{center} 17689 \tt 17690 linear\_regression([[0,0],[1,1],[2,4],[3,9],[4,16]]) 17691\end{center} 17692or 17693\begin{center} 17694 \tt 17695 linear\_regression([0,1,2,3,4],[0,1,4,9,16]) 17696\end{center} 17697you will get 17698\begin{center} 17699 \tt 17700 4, -2 17701\end{center} 17702which means that the line $y = 4x - 2$ is the best fit line. 17703 17704The best fit line can be drawn with the 17705\texttt{linear\_regression\_plot} command; if you enter 17706\begin{center} 17707 \tt 17708 linear\_regression\_plot([0,1,2,3,4],[0,1,4,9,16]) 17709\end{center} 17710you will get 17711\begin{center} 17712 \includeimage{xcas-linregplot.png} 17713\end{center} 17714This will draw the line (in this case $y=4x-2$) and give you the 17715equation at the top, as well as the $R^2$ value, which is 17716\[ 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}\] 17717(The $R^2$ value will be between 0 and 1 and is one measure of how 17718good the line fits the data; a value close to 1 indicates a good fit, 17719a value close to 0 indicates a bad fit.) 17720 17721\subsection{Exponential regression: \texttt{exponential\_regression}\index{exponential\_regression} \texttt{exponential\_regression\_plot}\index{exponential\_regression\_plot}} 17722 17723A set of points might be expected to lie on an exponential curve 17724$y=b a^x$. Given a set of points, either as a list of $x$-coordinates 17725followed by a list of $y$-coordinates, or simply by a list of points, 17726the \texttt{exponential\_regression} command will find the values of 17727$a$ and $b$ which give the best fit exponential. For example, if you 17728enter 17729\begin{center} 17730 \tt 17731 evalf(exponential\_regression([[1,1],[2,4],[3,9],[4,16]])) 17732\end{center} 17733or 17734\begin{center} 17735 \tt 17736 evalf(exponential\_regression([1,2,3,4],[1,4,9,16])) 17737\end{center} 17738(where the \texttt{evalf} is used to get a numeric approximation to an 17739exact expression) you will get 17740\begin{center} 17741 \tt 17742 2.49146187923,0.5 17743\end{center} 17744so the best fit exponential curve will be $y = 0.5*(2.49146187923)^x$. 17745 17746To plot the curve, you can use the command 17747\texttt{exponential\_regression\_plot}; if you enter 17748\begin{center} 17749 \tt 17750 exponential\_regression\_plot([1,2,3,4],[1,4,9,16]) 17751\end{center} 17752you will get 17753\begin{center} 17754 \includeimage{xcas-expregplot.png} 17755\end{center} 17756which plots the graph, and has the equation and $R^2$ value above the 17757graph. 17758 17759\subsection{Logarithmic regression: \texttt{logarithmic\_regression}\index{logarithmic\_regression} \texttt{logarithmic\_regression\_plot}\index{logarithmic\_regression\_plot}} 17760 17761A set of points might be expected to lie on a logarithmic curve 17762$y=m \ln(x) + b$. Given a set of points, either as a list of $x$-coordinates 17763followed by a list of $y$-coordinates, or simply by a list of points, 17764the \texttt{logarithmic\_regression} command will find the values of 17765$m$ and $b$ which give the best fit exponential. For example, if you 17766enter 17767\begin{center} 17768 \tt 17769 evalf(logarithmic\_regression([[1,1],[2,4],[3,9],[4,16]])) 17770\end{center} 17771or 17772\begin{center} 17773 \tt 17774 evalf(logarithmic\_regression([1,2,3,4],[1,4,9,16])) 17775\end{center} 17776(where the \texttt{evalf} is used to get a numeric approximation to an 17777exact expression) you will get 17778\begin{center} 17779 \tt 17780 10.1506450002,-0.564824055818 17781\end{center} 17782so the best fit exponential curve will be $y = 10.1506450002 \ln(x) 17783-0.564824055818$. 17784 17785To plot the curve, you can use the command 17786\texttt{exponential\_regression\_plot}; if you enter 17787\begin{center} 17788 \tt 17789 logarithmic\_regression\_plot([1,2,3,4],[1,4,9,16]) 17790\end{center} 17791you will get 17792\begin{center} 17793 \includeimage{xcas-logregplot.png} 17794\end{center} 17795which plots the graph, and has the equation and $R^2$ value above the 17796graph. 17797 17798\subsection{Power regression: \texttt{power\_regression}\index{power\_regression} \texttt{power\_regression\_plot}\index{power\_regression\_plot}} 17799 17800To find the graph $y=bx^m$ which best fits a set of data points, you 17801can use the \texttt{power\_regression} command. 17802Given a set of points, either as a list of $x$-coordinates 17803followed by a list of $y$-coordinates, or simply by a list of points, 17804the \texttt{power\_regression} command will find the values of 17805$m$ and $b$ which give the best fit curve. For example, if you 17806enter 17807\begin{center} 17808 \tt 17809 power\_regression([[1,1],[2,4],[3,9],[4,16]]) 17810\end{center} 17811or 17812\begin{center} 17813 \tt 17814 power\_regression([1,2,3,4],[1,4,9,16]) 17815\end{center} 17816you will get 17817\begin{center} 17818 \tt 17819 2.0, 1.0 17820\end{center} 17821so the best fit (in this case, exact fit) power curve will be $y = 1.0 x^2$. 17822 17823To plot the curve, you can use the command 17824\texttt{power\_regression\_plot}; if you enter 17825\begin{center} 17826 \tt 17827 power\_regression\_plot([1,2,3,4],[1,4,9,16]) 17828\end{center} 17829you will get 17830\begin{center} 17831 \includeimage{xcas-powregplot.png} 17832\end{center} 17833which plots the graph, and has the equation and $R^2$ value above the 17834graph. Note that in this case the $R^2$ value is 1, indicating that 17835the data points fall directly on the curve. 17836 17837\subsection{Polynomial regression: \texttt{polynomial\_regression}\index{polynomial\_regression} \texttt{polynomial\_regression\_plot}\index{polynomial\_regression\_plot}} 17838 17839If you want to find a more general polynomial $y=a_0x^n + \dots + a_n$ 17840which best fits a set of data points, you can use the 17841\texttt{polynomial\_regression} command. 17842Given a set of points, either as a list of $x$-coordinates 17843followed by a list of $y$-coordinates, or simply by a list of points, 17844as well as a power $n$, the \texttt{polynomial\_regression} command 17845will return the list $[a_n,\dots,a_0]$ of coefficients of the 17846polynomial. For example, if you enter 17847\begin{center} 17848 \tt 17849 polynomial\_regression([[1,1],[2,2],[3,10],[4,20]],3) 17850\end{center} 17851or 17852\begin{center} 17853 \tt 17854 polynomial\_regression([1,2,3,4],[1,2,10,20],3) 17855\end{center} 17856you will get 17857\begin{center} 17858 \tt 17859 [-5/6,17/2,-56/3,12] 17860\end{center} 17861so the best fit polynomial will be $y = (-5/6)x^3 + (17/2)x^2 - 17862(56/3)x + 12$. 17863 17864To plot the curve, you can use the command 17865\texttt{polynomial\_regression\_plot}; if you enter 17866\begin{center} 17867 \tt 17868 polynomial\_regression\_plot([1,2,3,4],[1,2,10,20],3) 17869\end{center} 17870you will get 17871\begin{center} 17872 \includeimage{xcas-polyregplot.png} 17873\end{center} 17874 17875\subsection{Logistic regression: \texttt{logistic\_regression}\index{logistic\_regression} \texttt{logistic\_regression\_plot}\index{logistic\_regression\_plot}} 17876 17877Differential equations of the form $y' = y(a*y + b)$ come up often, 17878particularly when studying bounded population growth. With the 17879initial condition $y(x0) = y0$, the solution is the logistic equation 17880\[ 17881y = \frac{-b*y0}{a*y0 - (a*y0+b)\exp(b(x0-x))} 17882\] 17883However, you often don't know the values of $a$ and $b$. You can 17884still get a ``best fit'' logisitic equation with the following 17885information: The initial value of $x$, the initial value of $y$, and 17886several values of $y'$; namely, $y'(x0),y'(x0+1),\dots,y'(x0+n-1)$ where 17887$x0$ is the initial value of $x$. \texttt{Xcas} will then take the 17888initial value $y(x0) = y0$ and the approximation $y(t+1) \approx y(t) 17889+ y'(t)$ to get the approximations $y(x0+1) \approx y0 + y'(x0)$, 17890$y(x0+2) \approx y0 + y'(x0) + y'(x0+1)$, \ldots 17891$y(x0+n) \approx y0 + y'(x0) +\dots y'(x0+n-1)$, \ldots 17892Since $y'/y = a + by$, \texttt{Xcas} will take the 17893approximate values of $y'(x0+j)/y(x0+j)$ and use linear interpolation 17894to get the best fit values of $a$ and $b$, and then solve the 17895differential equation. 17896 17897The \texttt{logistic\_regression} command will take as input a list 17898and two numbers; the list will be $[y_{10},y_{11},\dots,y_{1(n-1)}]$, 17899where $y_{1j}$ represents the value of $y'(x_0 + j)$, 17900the first number is $x_0$ and the last number is 17901$y_0=y(x_0)$. The command will return the function $y(x)$, the 17902derivative $y'(x)$, the number $C=-b/a$, $y'(x_M)$ which is the maximum value 17903of $y'$, $x_M$ which is where $y'$ has its maximum, and the linear 17904correlation coefficient $R$ of $Y=y'/y$ as a function of $y$ with $Y=a*y 17905+ b$. For example, if you enter 17906\begin{center} 17907 \tt 17908 logistic\_regression([0.0,1.0,2.5],0,1) 17909\end{center} 17910you will get 17911\begin{verbatim} 17912 Pinstant=0.132478632479*Pcumul+0.0206552706553 17913 Correlation 0.780548607383, Estimated total P=-0.155913978495 17914 Returning estimated Pcumul, Pinstant, Ptotal, Pinstantmax, tmax, R 17915 [-0.155913978495/(1+exp(-0.0554152581707*x+0.140088513344+3.14159265359*i)), 17916 -0.00161022271237/(1+cos((-i)*(-0.0554152581707*x+0.140088513344+3.14159265359*i))), 17917 -0.155913978495,-0.000805111356186,2.52797727501+56.6918346552*i,0.780548607383] 17918\end{verbatim} 17919 17920You can plot the logistic equation with the command 17921\texttt{logistic\_regression\_plot}; if you enter 17922\begin{center} 17923 \tt 17924 logistic\_regression\_plot([1,2,4,6,8,7,5],0,2.0) 17925\end{center} 17926you will get 17927\begin{center} 17928 \includeimage{xcas-logisticregplot.png} 17929\end{center} 17930 17931\section{Random numbers} 17932 17933\subsection{Producing uniformly distributed random numbers: \texttt{rand}\index{rand} \texttt{random}\index{random} \texttt{alea}\index{alea} \texttt{hasard}\index{hasard}} 17934 17935The \texttt{rand} (or \texttt{random}) command will produce a number in 17936$[0,1)$ randomly and with equal probability. If you enter 17937\begin{center} 17938 \tt 17939 rand() 17940\end{center} 17941you might get, for example, 17942\begin{center} 17943 \tt 17944 0.93233498279 17945\end{center} 17946 17947If you want a random number in a different interval, you can give 17948\texttt{rand} two real arguments; \texttt{rand($a$,$b$)} will return a 17949random number from the interval $[a,b)$. If you enter 17950\begin{center} 17951 \tt 17952 rand(1,1.5) 17953\end{center} 17954for example, you might get 17955\begin{center} 17956 \tt 17957 1.27419309644 17958\end{center} 17959If you give \texttt{rand} an interval, then you will get function 17960which will generate a random number in the interval. If you enter 17961\begin{center} 17962 \tt 17963 r:=rand(1.0..2.5) 17964\end{center} 17965you will get 17966\begin{center} 17967 \tt 17968 // Success 17969 (NULL)->rand(1.0,2.5) 17970\end{center} 17971and you can get a random number in the interval by calling the function; 17972\begin{center} 17973 \tt 17974 r() 17975\end{center} 17976might return 17977\begin{center} 17978 \tt 17979 1.76314622024 17980\end{center} 17981 17982If you want to generate a random integer, then \texttt{rand($n$)} (for 17983integer $n$) will return a random integer in $[0,n)$ (or $(n,0]$ if 17984$n$ is negative). If you enter 17985\begin{center} 17986 \tt 17987 rand(5) 17988\end{center} 17989for example, you might get 17990\begin{center} 17991 \tt 17992 2 17993\end{center} 17994 17995You can then use \texttt{rand} to find a random integer in a specified interval; if 17996you want an random integer between 6 and 10, inclusive, for example, 17997you can enter 17998\begin{center} 17999 \tt 18000 6 + rand(11-6) 18001\end{center} 18002You might get 18003\begin{center} 18004 \tt 18005 7 18006\end{center} 18007Alternatively, the \texttt{randint} will give you a random integer in 18008a given interval; \texttt{randint($n_1$,$n_2$)} will return a random 18009integer between $n_1$ and $n_2$, inclusive; to get a random integer 18010from 6 to 10, you could enter 18011\begin{center} 18012 \tt 18013 randint(6,10) 18014\end{center} 18015 18016The \texttt{rand} command can also choose elements without 18017replacement. If you give \texttt{rand} three integer arguments, 18018\texttt{rand($p$,$n_1$,$n_2$)} then it will return $p$ distinct random 18019integers from $n_1$ to $n_2$. If you enter 18020\begin{center} 18021 \tt 18022 rand(2,1,10) 18023\end{center} 18024for example, you will get 2 distinct random numbers from 1 to 10; 18025perhaps 18026\begin{center} 18027 \tt 18028 [2,9] 18029\end{center} 18030 18031You can also choose (without replacement) random elements of a given 18032list. For this, you give \texttt{rand}, a postive integer $n$ 18033and a list $L$; \texttt{rand($n$,$L$)} will then return $n$ random 18034elements from the list. If you enter 18035\begin{center} 18036 \tt 18037 rand(3,["a","b","c","d","e","f","g","h"]) 18038\end{center} 18039you might get 18040\begin{center} 18041 \tt 18042 ["c","b","e"] 18043\end{center} 18044The list can have repeated elements; if you enter 18045\begin{center} 18046 \tt 18047 rand(4,["r","r","r","r","v","v","v"]) 18048\end{center} 18049you might get 18050\begin{center} 18051 \tt 18052 ["v","v","r","v"] 18053\end{center} 18054The \texttt{sample} command will also randomly select items from a 18055list without replacement. With the \texttt{sample} command, the list 18056comes first and then the integer. If you enter 18057\begin{center} 18058 \tt 18059 sample(["r","r","r","r","v","v","v"],4) 18060\end{center} 18061you might get 18062\begin{center} 18063 \tt 18064 ["v","r","r","r"] 18065\end{center} 18066 18067\subsection{Initializing the random number generator: \texttt{srand}\index{srand} \texttt{randseed}\index{randseed} \texttt{RandSeed}\index{RandSeed}} 18068 18069The \texttt{srand} (or \texttt{randseed}) and \texttt{RandSeed} 18070commands will initialize (or re-initialize) the random numbers given 18071by \texttt{rand}. The \texttt{RandSeed} requires an integer argument, 18072and \texttt{srand} can either take an integer argument or no argument. 18073If you don't give \texttt{srand} an argument, then it will use the 18074system clock to initialize the random numbers. 18075 18076\subsection{Producing random numbers with the binomial distribution: \texttt{randbinomial}\index{randbinomial}} 18077 18078The command \texttt{randbinomial} will take parameters an integer $n$ 18079and a number $p$ between 0 and 1, and return an integer from 0 to $n$ 18080chosen according to the binomial distribution; i.e., the number of 18081successes you might get if you did an experiment $n$ times, where the 18082probability of success each time is $p$. If you enter 18083\begin{center} 18084 \tt 18085 randbinomial(100,0.4) 18086\end{center} 18087for example, you might get 18088\begin{center} 18089 \tt 18090 34 18091\end{center} 18092 18093\subsection{Producing random numbers with a multinomial distribution: \texttt{randmultinomial}\index{randmultinomial}} 18094 18095Given a list \texttt{P=[$p_0$,...,$p_{n-1}$]} of $n$ probabilities which 18096add to 1 (representing the probability that one of several mutually 18097exclusive events occurs), the \texttt{randmultinomial} command will 18098return an index whose probability is determined by the corresponding 18099multinomial distribution. If you enter 18100\begin{center} 18101 \tt 18102 randmultinomial([1/2, 1/3, 1/6]) 18103\end{center} 18104you might get 18105\begin{center} 18106 \tt 18107 0 18108\end{center} 18109If $K$ is a list of length $n$, then \texttt{randmultinomial($P$,$K$)} 18110will return an element of the list, whose index is chosen according to 18111the multinomial distribution. If you enter 18112\begin{center} 18113 \tt 18114 randmultinomial([1/2, 1/3, 1/6],["R","V","B"]) 18115\end{center} 18116you might get 18117\begin{center} 18118 \tt 18119 "R" 18120\end{center} 18121 18122\subsection{Producing random numbers with a Poisson distribution: \texttt{randpoisson}\index{randpoisson}} 18123 18124Recall that given a number $\lambda$, the corresponding Poisson 18125distribution $P(\lambda)$ satisfies 18126\[ \text{Prob}(X \le k) = \exp(-\lambda)\lambda^k/k!\] 18127It will have mean $\lambda$ and standard deviation $\sqrt{\lambda}$. 18128 18129The \texttt{randpoisson} command will take a parameter $\lambda$ and 18130return an integer chosen at random using the Poisson distribution. 18131If you enter 18132\begin{center} 18133 \tt 18134 randpoisson(10.6) 18135\end{center} 18136you might get 18137\begin{center} 18138 \tt 18139 16 18140\end{center} 18141 18142\subsection{Producing random numbers with a normal distribution: \texttt{randnorm}\index{randnorm} \texttt{randNorm}\index{randNorm}} 18143 18144The \texttt{randnorm} (or \texttt{randNorm}) command will choose a 18145random number according to a normal distribution. Given the mean 18146$\mu$ and standard deviation $\sigma$, 18147\texttt{randnorm($\mu$,$\sigma$)} will return a number chosen 18148according the normal distribution. If you enter 18149\begin{center} 18150 \tt 18151 randnorm(2,1) 18152\end{center} 18153you might get 18154\begin{center} 18155 \tt 18156 2.45598713143 18157\end{center} 18158 18159\subsection{Producing random numbers with an exponential distribution: \texttt{randexp}\index{randexp}} 18160 18161Recall that given a positive number $a$, the corresponding exponential 18162distribution satisfies 18163\[ \text{Prob}(X \le t) = a \int_0^t exp(-a * u) du\] 18164Given a parameter $a$, the command \texttt{randexp($a$)} will return a 18165number chosen randomly according to the corresponding exponential 18166distribution. For example, if you enter 18167\begin{center} 18168 \tt 18169 randexp(1) 18170\end{center} 18171you might get 18172\begin{center} 18173 \tt 18174 0.193354391918 18175\end{center} 18176 18177\subsection{Producing random matrices: \texttt{randmatrix}\index{randmatrix} \texttt{ranm}\index{ranm} \texttt{randMat}\index{randMat}} 18178 18179You can produce a random vector or matrix with the \texttt{randmatrix} 18180(or \texttt{ranm} or \texttt{randMat}) command. (See also sections 18181\ref{sec:ranm1} and \ref{sec:ranm2}.) The \texttt{randmatrix} command 18182has the following possible arguments. 18183\begin{description} 18184\item[An integer $n$] 18185With an integer $n$, \texttt{randmatrix($n$)} will return a vector of 18186length $n$ whose elements are integers chosen randomly from 18187$[-99,-98,\dots,98,99]$ with equal probability. If you enter 18188\begin{center} 18189 \tt 18190 randmatrix(5) 18191\end{center} 18192you might get 18193\begin{center} 18194 \tt 18195 [86,-97,-82,7,-27] 18196\end{center} 18197\item[Two integers $n$ and $p$] 18198Given two integers $n$ and $p$, \texttt{randmatrix($n$,$p$)} will 18199return an $n\times p$ matrix whose elements are integers chosen randomly from 18200$[-99,99]$ with equal probability. If you enter 18201\begin{center} 18202 \tt 18203 randmatrix(2,3) 18204\end{center} 18205you might get 18206\begin{center} 18207 \tt 18208 [[26,-89,63],[-49,-86,-64]] 18209\end{center} 18210\item[Three integers $n$, $p$ and $a$] 18211Given three integers $n$, $p$ and $a$, \texttt{randmatrix($n$,$p$,$a$)} will 18212return an $n\times p$ matrix whose elements are integers chosen randomly from 18213$[0,a)$ (or $(a,0]$ is $a$ is negative) with equal probability. If you enter 18214\begin{center} 18215 \tt 18216 randmatrix(2,3,10) 18217\end{center} 18218you might get 18219\begin{center} 18220 \tt 18221 [[4,7,6],[7,4,5]] 18222\end{center} 18223\item[Two integers $n$ and $p$, and an interval $a..b$.] 18224Given two integers $n$, $p$ and an $a..b$, 18225\texttt{randmatrix($n$,$p$,$a..b$)} will 18226return an $n\times p$ matrix whose elements are real numbers chosen randomly from 18227$[a,b)$ with equal probability. If you enter 18228\begin{center} 18229 \tt 18230 randmatrix(2,3,0..1) 18231\end{center} 18232you might get 18233\begin{center} 18234 \tt 18235 [[0.90923402831,0.594602484722,0.250897713937],[0.332611694932,0.145975249354,0.543010003399]] 18236\end{center} 18237\item[Two integers $n$ and $p$ and a function (which must be quoted) 18238to produce random numbers] 18239In this case, the third argument must be 18240one of \texttt{'rand($n$)'}, \texttt{'binomial($n$,$p$)'}, 18241\texttt{'binomial,$n$,$p$'}, \texttt{'randbinomial($n$,$p$)'}, 18242\texttt{'multinomial($P$,$K$)'}, \texttt{'multinomial,$P$,$K$'}, 18243\texttt{'randmultinomial($P$,$K$)'}, \texttt{'poisson($\lambda$)'}, 18244\texttt{'poisson, $\lambda$'}, \texttt{'randpoisson($\lambda$)'}, 18245\texttt{'normald($\mu$,$\sigma$)'}, \texttt{'normald,$\mu$,$\sigma$'}, 18246\texttt{'randnorm($\mu$,$\sigma$)'}, \texttt{'exp($a$)'}, 18247\texttt{'exp,$a$'}, \texttt{'randexp($a$)'}, 18248\texttt{'fisher($n$,$m$)'}, \texttt{'fisher,$n$,$m$'}, or 18249\textrm{'randfisher($n$,$m$)'}. 18250\end{description} 18251Given such an $R$, the command \texttt{randmatrix($n$,$p$,$R$)} will 18252return an $n\times p$ matrix whose elements are numbers chosen 18253randomly according to the rule determined by $R$. 18254If you enter 18255\begin{center} 18256 \tt 18257 randmatrix(2,3,'randnorm(2,1)') 18258\end{center} 18259you might get 18260\begin{center} 18261 \tt 18262 [[2.6324726358,0.539273367446,0.793750476229],[2.24729803442,1.28189228187,2.25750809791]] 18263\end{center} 18264 18265\section{Density and distribution functions} 18266 18267\subsection{The binomial distribution} 18268 18269\subsubsection{The probability density function for the binomial 18270distribution : \texttt{binomial}\index{binomial}} 18271 18272If you perform an experiment $n$ times, where the probability of 18273success each time is $p$, then the probability of exactly $k$ 18274successes is 18275\[ \texttt{binomial($n$,$k$,$p$)} = \left(^n_k\right) p^k (1-p)^{n-k}\] 18276This determines the binomial distribution, and so this is called the 18277\texttt{binomial} command. If you enter 18278\begin{center} 18279 \tt 18280 binomial(10,2,0.4) 18281\end{center} 18282you will get 18283\begin{center} 18284 \tt 18285 0.120932352 18286\end{center} 18287If no third argument $p$ is given, then \texttt{binomial} will just 18288compute $\left(^n_k\right)$, which recall is called the binomial 18289coefficient and is also computed by \texttt{comb}. If you enter 18290\begin{center} 18291 \tt 18292 binomial(10,2) 18293\end{center} 18294or 18295\begin{center} 18296 \tt 18297 comb(10,2) 18298\end{center} 18299then you will get 18300\begin{center} 18301 \tt 18302 45 18303\end{center} 18304 18305\subsubsection{The cumulative distribution function for the binomial 18306distribution: \texttt{binomial\_cdf}\index{binomial\_cdf}} 18307 18308Recall that the cumulative distribution function (cdf) for a 18309distribution is $cdf(x) = \text{Prob}(X \le x)$. For the binomial 18310distribution, this is given by the \texttt{binomial\_cdf} command; 18311\texttt{binomial\_cdf($n$,$p$,$x$)}, which in this case will equal 18312\texttt{binomial($n$,$0$,$p$) + \ldots + binomial($n$,floor($x$,$p$)}. 18313If you enter 18314\begin{center} 18315 \tt 18316 binomial\_cdf(4,0.5,2) 18317\end{center} 18318you will get 18319\begin{center} 18320 \tt 18321 0.6875 18322\end{center} 18323You can give \texttt{binomial\_cdf} an additional argument; 18324\texttt{binomial\_cdf($n$,$p$,$x$,$y$) = Prob($x \le X \le y$)}, which 18325in this case would be 18326\texttt{binomial($n$,ceil($x$),$p$) + $\cdots$ + binomial($n$,floor($y$),$p$)}. 18327If you enter 18328\begin{center} 18329 \tt 18330 binomial\_cdf(2,0.3,1,2) 18331\end{center} 18332you will get 18333\begin{center} 18334 \tt 18335 0.51 18336\end{center} 18337 18338\subsubsection{The inverse distribution function for the binomial 18339distribution: \texttt{binomial\_icdf}\index{binomial\_icdf}} 18340 18341Given a value $h$, the inverse distribution function gives the value 18342of $x$ so that Prob($X \le x$) = $h$; or for discrete distributions, 18343the smallest $x$ so that Prob($X \le x$) $\ge h$. For the binomial 18344distribution with $n$ and $p$, the \texttt{binomial\_icdf} gives the 18345inverse distribution function. If you enter 18346\begin{center} 18347 \tt 18348 binomial\_icdf(4,0.5,0.9) 18349\end{center} 18350you will get 18351\begin{center} 18352 \tt 18353 3 18354\end{center} 18355Note that \texttt{binomial\_cdf(4,0.5,3)} 18356is $0.9375$, bigger than $0.9$, while 18357\texttt{binomial\_cdf(4,0.5,2)} is $0.6875$, smaller than $0.9$. 18358 18359\subsection{The negative binomial distribution} 18360 18361\subsubsection{The probability density function for the negative 18362binomial distribution: \texttt{negbinomial}\index{negbinomial}} 18363If you repeatedly perform an experiment with probability of success 18364$p$, then, given an integer $n$, the probability of $k$ failures that occur 18365before you have $n$ successes is given by the negative binomial 18366distribution, and can be computed with 18367\texttt{negbinomial($n$,$k$,$p$)}. It is given by the formula 18368$\left(^{n+k-1}_{k}\right)p^n(1-p)^k$. If you enter 18369\begin{center} 18370 \tt 18371 negbinomial(4,2,0.5) 18372\end{center} 18373you will get 18374\begin{center} 18375 \tt 18376 0.15625 18377\end{center} 18378 18379Note that 18380\[ 18381\left(^{n}_{k}\right) = \frac{n!}{k! (n-k)!} = \frac{n (n-1 ) \dots ( 18382n-k+1)}{k!} 18383\] 18384The second formula makes sense even if $n$ is negative, and you can 18385write 18386\texttt{negbinomial}$(n,k,p) = \left(^{-n}_{k}\right)p^n (p-1)^k$, from 18387which the name negative binomial distribution comes from. This also 18388makes it simple to determine the mean ($n(1-p)/p$) and variance 18389($n(1-p)/p^2$). The negative binomial is also called the Pascal 18390distribution (after Blaise Pascal) or the P\'{o}lya distribution 18391(after George P\'{o}lya). 18392 18393\subsubsection{The cumulative distribution function for the negative 18394binomial distribution: \texttt{negbinomial\_cdf}\index{negbinomial\_cdf}} 18395 18396The cumulative distribution function for the negative binomial 18397distribution is given by the \texttt{negbinomial\_cdf} command. Given 18398parameters $n$ and $p$, as above, then 18399\texttt{negbinomial\_cdf($n$,$p$,$x$) = Prob($X \le x$) = 18400negbinomial($n$,$0$,$p$) + \ldots + negbinomial($n$,floor($x$),$p$)}, 18401and 18402\texttt{negbinomial\_cdf($n$,$p$,$x$,$y$) = Prob($x \le X \le y$) = 18403negbinomial($n$,ceil($x$),$p$) + $\cdots$ + negbinomial($n$,floor($y$),$p$)}. 18404If you enter 18405\begin{center} 18406 \tt 18407 negbinomial\_cdf(4,0.5,2) 18408\end{center} 18409for example, you will get 18410\begin{center} 18411 \tt 18412 0.34375 18413\end{center} 18414 18415\subsubsection{The inverse distribution function for the negative 18416binomial distribution: \texttt{negbinomial\_icdf}\index{negbinomial\_icdf}} 18417 18418Given a value $h$, the inverse distribution function gives 18419the smallest value 18420of $x$ so that Prob($X \le x$) $\ge h$. 18421The \texttt{negbinomial\_icdf} gives the inverse 18422distribution function for the negative binomial distribution. 18423If you enter 18424\begin{center} 18425 \tt 18426 negbinomial\_icdf(4,0.5,0.9) 18427\end{center} 18428for example, you will get 18429\begin{center} 18430 \tt 18431 8 18432\end{center} 18433 18434\subsection{The multinomial probability function: \texttt{multinomial}\index{multinomial}} 18435 18436If $X$ follows a multinomial probability distribution with 18437$P = [p_0,p_1,\dots,p_j]$ (where $p_0 + \dots + p_j = 1$), 18438then for $K=[k_0,\dots,k_j]$ with $k_0 + \dots + k_j = n$, the 18439probability that $X=K$ is given by the \texttt{multinomial} command; 18440\[\texttt{multinomial}(n,P,K)= \frac{n!}{k_0!k_1!\dots 18441k_j!}(p_0^{k_0}p_1^{k_1}\dots p_j^{k_j}.\] 18442You will get an error if $k_0 + \dots + k_j$ is not equal to $n$, 18443although you won't get one if $p_0 + \dots + p_j$ is not equal to $1$. 18444 18445For example, if you make $10$ choices, where each choice is one of 18446three items; the first has a $0.2$ probability of being chosen, the 18447second a $0.3$ probability and the third a $0.5$ probability, the 18448probability that you end up with $3$ of the first item, $2$ of the 18449second and $5$ of the third will be 18450\begin{center} 18451 \tt 18452 multinomial(10,[0.2,0.3,0.5],[3,2,5]) 18453\end{center} 18454or 18455\begin{center} 18456 \tt 18457 0.0567 18458\end{center} 18459 18460\subsection{The Poisson distribution} 18461\subsubsection{The probability density function for the Poisson 18462distribution: \texttt{poisson}\index{poisson}} 18463 18464Recall that for the Poisson distribution with parameter $\mu$, the 18465probability of a non-negative integer $k$ is $e^{-\mu}\mu^k/k!$. It 18466will mean $\mu$ and variance $\mu$. The \texttt{poisson} command will 18467find this value, given $\mu$ and $k$. For example, 18468\begin{center} 18469 \tt 18470 poisson(10.0,9) 18471\end{center} 18472is 18473\begin{center} 18474 \tt 18475 0.125110035721 18476\end{center} 18477 18478\subsubsection{The cumulative distribution function for the Poisson 18479distribution: \texttt{poisson\_cdf}\index{poisson\_cdf}} 18480 18481The cumulative distribution function for the Poisson distribution is 18482given by the \texttt{poisson\_cdf} command with arguments $\mu$ and 18483$x$; \texttt{poisson\_cdf($\mu$,$x$) = Prob($X \le x$)}. If you enter 18484\begin{center} 18485 \tt 18486 poisson\_cdf(10.0,3) 18487\end{center} 18488you will get 18489\begin{center} 18490 \tt 18491 0.0103360506759 18492\end{center} 18493 18494With another argument, \texttt{poisson\_cdf} will find the probability 18495of falling between two values; 18496\texttt{poisson\_cdf($\mu$,$x$,$y$) = Prob($x \le X \le y$)}. 18497If you enter 18498\begin{center} 18499 \tt 18500 poisson\_cdf(10.0,3,10) 18501\end{center} 18502you will get 18503\begin{center} 18504 \tt 18505 0.580270354477 18506\end{center} 18507 18508\subsubsection{The inverse distribution function for the Poisson 18509distribution: \texttt{poisson\_icdf}\index{poisson\_icdf}} 18510Given a value $h$, the inverse distribution function gives 18511the smallest value 18512of $x$ so that Prob($X \le x$) $\ge h$. 18513Given arguments of a parameter $\mu$ and a value $x$, the 18514\texttt{poisson\_icdf} gives the inverse 18515distribution function for the poisson distribution. If you enter 18516\begin{center} 18517 \tt 18518 poisson\_icdf(10.0,0.975) 18519\end{center} 18520you will get 18521\begin{center} 18522 \tt 18523 17 18524\end{center} 18525 18526\subsection{Normal distributions} 18527\subsubsection{The probability density function for a normal 18528distribution: \texttt{normald}\index{normald} \texttt{loi\_normal}\index{loi\_normal}} 18529 18530The \texttt{normald} (or \texttt{loi\_normal}) command returns the 18531value of the normal probability density function. You can give it 18532arguments of the mean $\mu$, standard deviation $\sigma$ and a value 18533$x$ then 18534\[ 18535\texttt{normald}(\mu,\sigma,x) = \frac{1}{\sqrt{2\pi} \sigma} 18536e^{(x-\mu)^2/2} 18537\] 18538If you enter 18539\begin{center} 18540 \tt 18541 normald(2,1,3) 18542\end{center} 18543you will get 18544\begin{center} 18545 \tt 18546 exp(-1/2)/sqrt(2*pi) 18547\end{center} 18548 18549If you don't give the command values for $\mu$ and $\sigma$, then 18550\texttt{normald} will use the values $\mu=0$ and $\sigma = 1$, and so 18551compute the standard normal density function. If you enter 18552\begin{center} 18553 \tt 18554 normald(2) 18555\end{center} 18556you will get 18557\begin{center} 18558 \tt 18559 1/(sqrt(2*pi)*exp(2)) 18560\end{center} 18561 18562\subsubsection{The cumulative distribution function for normal 18563distributions: \texttt{normal\_cdf}\index{normal\_cdf} \texttt{normald\_cdf}\index{normald\_cdf}} 18564 18565The command \texttt{normal\_cdf} (or \texttt{normald\_cdf}) computes 18566the cumulative distribution function for the normal distribution. 18567Like \texttt{normald}, you can give it the mean and standard deviation 18568of the distribution; if you enter 18569\begin{center} 18570 \tt 18571 normal\_cdf(1,2,1.96) 18572\end{center} 18573you will get 18574\begin{center} 18575 \tt 18576 0.684386303484 18577\end{center} 18578You can also leave off the mean and standard deviation, in which case 18579\texttt{normal\_cdf} will compute the cumulative distribution function 18580for the standard normal distribution; 18581\begin{center} 18582 \tt 18583 normal\_cdf(1,2.1,1.2) 18584\end{center} 18585you will get 18586\begin{center} 18587 \tt 18588 0.537937144066 18589\end{center} 18590 18591If you give \texttt{normal\_cdf} an extra argument (with or without 18592the mean and standard deviation), you will get the probability that 18593the random variable lies between two values; 18594$\texttt{normal\_cdf($x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 18595enter 18596\begin{center} 18597 \tt 18598 normal\_cdf(1,2.1,1.2,9) 18599\end{center} 18600you will get 18601\begin{center} 18602 \tt 18603 0.461993238584 18604\end{center} 18605 18606\subsubsection{The inverse distribution function for normal 18607distributions: \texttt{normal\_icdf}\index{normal\_icdf} \texttt{normald\_icdf}\index{normald\_icdf}} 18608Given a value $h$, the inverse distribution function gives 18609the value of $x$ with Prob($X \le x$) $\le h$. 18610The \texttt{normal\_icdf} (or \texttt{normald\_icdf}) will compute the 18611inverse distribution for the normal distribution. If no 18612mean or standard deviation are given, the standard normal distribution 18613will be used. If you enter 18614\begin{center} 18615 \tt 18616 normal\_icdf(0.975) 18617\end{center} 18618you will get 18619\begin{center} 18620 \tt 18621 1.95996398454 18622\end{center} 18623You can, of course, also give the mean and standard deviation. If you 18624enter 18625\begin{center} 18626 \tt 18627 normal\_icdf(1,2,0.495) 18628\end{center} 18629you will get 18630\begin{center} 18631 \tt 18632 0.974933060984 18633\end{center} 18634 18635\subsubsection{The upper tail cumulative function for normal 18636distributions: \texttt{UTPN}\index{UTPN}} 18637 18638The \texttt{UTPN} (the Upper Tail Probability - Normal distribution) will compute 18639$\text{Prob}(X > x)$. If you don't give it a mean and variance, then 18640it will compute the probability for the standard normal distribution. 18641If you enter 18642\begin{center} 18643 \tt 18644 UTPN(1.96) 18645\end{center} 18646you will get 18647\begin{center} 18648 \tt 18649 0.0249978951482 18650\end{center} 18651You can also specify a mean and a variance, but note that unlike 18652\texttt{normald} and \texttt{normal\_cdf}, the \texttt{UTPN} requires 18653the variance and not the standard deviation. If you enter 18654\begin{center} 18655 \tt 18656 UTPN(1,4,1.96) 18657\end{center} 18658you will get 18659\begin{center} 18660 \tt 18661 0.315613696516 18662\end{center} 18663 18664\subsection{Student's distribution} 18665 18666\subsubsection{The probability density function for Student's 18667distribution: \texttt{student}\index{student} \texttt{studentd}\index{studentd}} 18668 18669Student's distribution (also called Student's $t$-distribution or just 18670the $t$-distribution) with 18671$n$ degrees of freedom has density function given by 18672\[ 18673\texttt{student}(n,x) = 18674\frac{\Gamma((n+1)/2)}{\Gamma(n/2)\sqrt{n\pi}}\left(1 + 18675\frac{x^2}{n}\right)^{-n-1/2} 18676\] 18677where recall the Gamma function is defined for $x>0$ by $\Gamma(x) = 18678\int_0^\infty e^{-t}t^{x-1}dx$. If you enter 18679\begin{center} 18680 \tt 18681 student(2,3) 18682\end{center} 18683you will get 18684\begin{center} 18685 \tt 18686 sqrt(pi)/(11*sqrt(2*pi)*sqrt(11/2)) 18687\end{center} 18688which can be numerically approximated by 18689\begin{center} 18690 \tt 18691 evalf(student(2,3)) 18692\end{center} 18693which is 18694\begin{center} 18695 \tt 18696 0.0274101222343 18697\end{center} 18698 18699\subsubsection{The cumulative distribution function for Student's 18700distribution: \texttt{student\_cdf}\index{student\_cdf}} 18701 18702The cumulative distribution function for Student's distribution with 18703$n$ degrees of freedom at a value $x$ is 18704$\texttt{student\_cdf}(n,x) = \texttt{Prob}(X \le x)$; if you 18705enter 18706\begin{center} 18707 \tt 18708 student\_cdf(5,2) 18709\end{center} 18710you will get 18711\begin{center} 18712 \tt 18713 0.949030260585 18714\end{center} 18715 18716If you give \texttt{student\_cdf} an extra argument, 18717you will get the probability that 18718the random variable lies between two values; 18719$\texttt{student\_cdf($n$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 18720enter 18721\begin{center} 18722 \tt 18723 student\_cdf(5,-2,2) 18724\end{center} 18725you will get 18726\begin{center} 18727 \tt 18728 0.89806052117 18729\end{center} 18730 18731\subsubsection{The inverse distribution function for Student's 18732distribution: \texttt{student\_icdf}\index{student\_icdf}} 18733 18734The inverse distribution function for Student's 18735distribution with $n$ degrees of freedom is computed with 18736\texttt{student\_icdf($n$,$h$)}; recall that this will return the 18737value $x$ with $\texttt{student\_cdf}(n,x) = h$. If you enter 18738\begin{center} 18739 \tt 18740 student\_icdf(5,0.95) 18741\end{center} 18742you will get 18743\begin{center} 18744 \tt 18745 2.01504837333 18746\end{center} 18747 18748 18749\subsubsection{The upper tail cumulative function for Student's 18750distribution: \texttt{UTPT}\index{UTPT}} 18751 18752The \texttt{UTPT} (the Upper Tail Probability - T distribution) will compute 18753$\text{Prob}(X > x)$. 18754If you enter 18755\begin{center} 18756 \tt 18757 UTPT(5,2) 18758\end{center} 18759you will get 18760\begin{center} 18761 \tt 18762 0.0509697394149 18763\end{center} 18764 18765\subsection{The $\chi^2$ distribution} 18766 18767\subsubsection{The probability density function for the $\chi^2$ 18768distribution: \texttt{chisquare}\index{chisquare}} 18769 18770The $\chi^2$ distribution with 18771$n$ degrees of freedom has density function given by 18772\[ 18773\texttt{chisquare}(n,x) = 18774\frac{x^{n/2-1}e^{-x/2}}{2^{n/2}\Gamma(n/2)} 18775\] 18776If you enter 18777\begin{center} 18778 \tt 18779 chisquare(5,2) 18780\end{center} 18781you will get 18782\begin{center} 18783 \tt 18784 2*sqrt(2)/(exp(1)*sqrt(2)*3*sqrt(pi)) 18785\end{center} 18786which can be numerically approximated by 18787\begin{center} 18788 \tt 18789 evalf(chisquare(5,2)) 18790\end{center} 18791which is 18792\begin{center} 18793 \tt 18794 0.138369165807 18795\end{center} 18796 18797\subsubsection{The cumulative distribution function for the $\chi^2$ 18798distribution: \texttt{chisquare\_cdf}\index{chisquare\_cdf}} 18799 18800The cumulative distribution function for the $\chi^2$ distribution with 18801$n$ degrees of freedom at a value $x$ is 18802$\texttt{chisquare\_cdf}(n,x) = \texttt{Prob}(X \le x)$; if you 18803enter 18804\begin{center} 18805 \tt 18806 chisquare\_cdf(5,11) 18807\end{center} 18808you will get 18809\begin{center} 18810 \tt 18811 0.948620016517 18812\end{center} 18813 18814If you give \texttt{chisquare\_cdf} an extra argument, 18815you will get the probability that 18816the random variable lies between two values; 18817$\texttt{chisquare\_cdf($n$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 18818enter 18819\begin{center} 18820 \tt 18821 chisquare\_cdf(3,1,2) 18822\end{center} 18823you will get 18824\begin{center} 18825 \tt 18826 0.22884525243 18827\end{center} 18828 18829\subsubsection{The inverse distribution function for the $\chi^2$ 18830distribution: \texttt{chisquare\_icdf}\index{chisquare\_icdf}} 18831 18832The inverse distribution function for the $\chi^2$ 18833distribution with $n$ degrees of freedom is computed with 18834\texttt{chisquare\_icdf($n$,$h$)}; recall that this will return the 18835value $x$ with $\texttt{chisquare\_cdf}(n,x) = h$. If you enter 18836\begin{center} 18837 \tt 18838 chisquare\_icdf(5,0.95) 18839\end{center} 18840you will get 18841\begin{center} 18842 \tt 18843 11.0704976935 18844\end{center} 18845 18846\subsubsection{The upper tail cumulative function for the $\chi^2$ 18847distribution: \texttt{UTPC}\index{UTPC}} 18848 18849The \texttt{UTPC} (the Upper Tail Probability - Chi-square distribution) will compute 18850$\text{Prob}(X > x)$. 18851If you enter 18852\begin{center} 18853 \tt 18854 UTPC(5,11) 18855\end{center} 18856you will get 18857\begin{center} 18858 \tt 18859 0.0513799834831 18860\end{center} 18861 18862\subsection{The Fisher-Sn\'{e}d\'{e}cor distribution} 18863 18864\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}} 18865 18866The Fisher-Sn\'{e}d\'{e}cor distribution (also called the F-distribution) 18867with $n_1$ and $n_2$ degrees of freedom has density function given by 18868for $x \ge 0$, 18869\[ 18870\texttt{fisher}(n_1,n_2,x) = 18871\frac{(n_1/n_2)^{n_1/2}\Gamma((n_1+n_2)/2)}{\Gamma(n_1/2)\Gamma(n_2/2)} 18872\frac{x^{(n_1-2)/2}}{(1+(n_1/n_2)x)^{(n_1+n_2)/2}} 18873\] 18874(The \texttt{snecedor} command is the same as the \texttt{fisher} 18875command.) 18876If you enter 18877\begin{center} 18878 \tt 18879 fisher(5,3,2.5) 18880\end{center} 18881you will get 18882\begin{center} 18883 \tt 18884 0.10131184472 18885\end{center} 18886 18887\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}} 18888 18889The cumulative distribution function for the Fisher-Sn\'{e}d\'{e}cor 18890distribution with $n_1$ and $n_2$ degrees of freedom at a value $x$ is 18891$\texttt{fisher\_cdf}(n_1,n_2,x) = \texttt{snedecor}(n_1,n_2,x) = 18892\texttt{Prob}(X \le x)$; if you enter 18893\begin{center} 18894 \tt 18895 fisher\_cdf(5,3,9) 18896\end{center} 18897you will get 18898\begin{center} 18899 \tt 18900 Beta(5/2,3/2,15/16,1) 18901\end{center} 18902which can be numerically approximated with 18903\begin{center} 18904 \tt 18905 evalf(fisher\_cdf(5,3,9,10)) 18906\end{center} 18907which is 18908\begin{center} 18909 \tt 18910 0.949898927032 18911\end{center} 18912 18913\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}} 18914 18915The inverse distribution function for the 18916Fisher-Sn\'{e}d\'{e}cor 18917distribution with $n_1$ and $n_2$ degrees of freedom is computed with 18918\texttt{fisher\_icdf($n_1$,$n_2$,$h$)}; recall that this will return the 18919value $x$ with $\texttt{fisher\_cdf}(n_1,n_2,x) = h$. If you enter 18920\begin{center} 18921 \tt 18922 fisher\_icdf(5,3,0.95) 18923\end{center} 18924you will get 18925\begin{center} 18926 \tt 18927 9.01345516752 18928\end{center} 18929 18930\subsubsection{The upper tail cumulative function for the Fisher-Sn\'{e}d\'{e}cor distribution: \texttt{UTPF}\index{UTPF}} 18931 18932The \texttt{UTPF} (the Upper Tail Probability - 18933Fisher-Sn\'{e}d\'{e}cor distribution) will compute 18934$\text{Prob}(X > x)$. 18935If you enter 18936\begin{center} 18937 \tt 18938 UTPF(5,3,9) 18939\end{center} 18940you will get 18941\begin{center} 18942 \tt 18943 0.050101072968 18944\end{center} 18945 18946\subsection{The gamma distribution} 18947\subsubsection{The probability density function for the gamma distribution: \texttt{gammad}\index{gammad}} 18948 18949The gamma distribution depends on two parameters, $a>0$ and $b>0$; the 18950value of the density function at $x \ge 0$ is 18951$\texttt{gammad}(a,b,x) = x^{a-1}e^{-bx}b^a/\Gamma(a)$. If you enter 18952\begin{center} 18953 \tt 18954 gammad(2,1,3) 18955\end{center} 18956for example, you will get 18957\begin{center} 18958 \tt 18959 3/exp(3) 18960\end{center} 18961 18962\subsubsection{The cumulative distribution function for the gamma distribution: \texttt{gammad\_cdf}\index{gammad\_cdf}} 18963 18964The cumulative distribution function for the gamma distribution with 18965parameters $a$ and $b$ at a value $x$ is 18966$\texttt{gammad\_cdf}(n,x) = \texttt{Prob}(X \le x)$. It turns out that 18967$\texttt{gammad\_cdf}(n,x) = \texttt{igamma}(a, bx, 1)$ where 18968\texttt{igamma} is the incomplete gamma function; 18969$\texttt{igamma}(a,x,1) = \int_0^x e^{-t}t^{a-1}dt/\Gamma(a)$. 18970If you 18971enter 18972\begin{center} 18973 \tt 18974 gammad\_cdf(2,1,0.5) 18975\end{center} 18976you will get 18977\begin{center} 18978 \tt 18979 0.090204010431 18980\end{center} 18981 18982If you give \texttt{gammad\_cdf} an extra argument, 18983you will get the probability that 18984the random variable lies between two values; 18985$\texttt{gammad\_cdf($a$,$b$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 18986enter 18987\begin{center} 18988 \tt 18989 gammad\_cdf(2,1,0.5,1.5) 18990\end{center} 18991you will get 18992\begin{center} 18993 \tt 18994 0.351970589198 18995\end{center} 18996 18997\subsubsection{The inverse distribution function for the gamma distribution: \texttt{gammad\_icdf}\index{gammad\_icdf}} 18998 18999The inverse distribution function for the gamma 19000distribution with parameters $a$ and $b$ is computed with 19001\texttt{gammad\_icdf($a$,$b$,$h$)}; recall that this will return the 19002value $x$ with $\texttt{gammad\_cdf}(a,b,x) = h$. If you enter 19003\begin{center} 19004 \tt 19005 gammad\_icdf(2,1,0.5) 19006\end{center} 19007you will get 19008\begin{center} 19009 \tt 19010 1.67834699002 19011\end{center} 19012 19013\subsection{The beta distribution} 19014 19015\subsubsection{The probability density function for the beta distribution: \texttt{betad}\index{betad}} 19016 19017The beta distribution depends on two parameters, $a>0$ and $b>0$; the 19018value of the density function at $x$ in $[0,1]$ is 19019$\texttt{betad}(a,b,x) = 19020\Gamma(a+b)x^{a-1}(1-x)^{b-1}/(\Gamma(a)\Gamma(b))$. If you enter 19021\begin{center} 19022 \tt 19023 betad(2,1,0.3) 19024\end{center} 19025for example, you will get 19026\begin{center} 19027 \tt 19028 0.6 19029\end{center} 19030 19031\subsubsection{The cumulative distribution function for the beta distribution: \texttt{betad\_cdf}\index{betad\_cdf}} 19032 19033The cumulative distribution function for the beta distribution with 19034parameters $a$ and $b$ at a value $x$ in $[0,1]$ is 19035$\texttt{betad\_cdf}(a,b,x) = \texttt{Prob}(X \le x)$. It turns out 19036that $\texttt{betad\_cdf}(a,b,x) = 19037\beta(a,b,x)\Gamma(a+b)/(\Gamma(a)\Gamma(b))$ where 19038$\beta(a,b,x) = \int_0^x t^{a-1}(1-t)^{b-1} dt$. 19039If you 19040enter 19041\begin{center} 19042 \tt 19043 betad\_cdf(2,3,0.2) 19044\end{center} 19045for example, you will get 19046\begin{center} 19047 \tt 19048 0.1808 19049\end{center} 19050 19051If you give \texttt{betad\_cdf} an extra argument $y$, also in $[0,1]$, 19052you will get the probability that 19053the random variable lies between the two values; 19054$\texttt{betad\_cdf($a$,$b$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. 19055If you enter 19056\begin{center} 19057 \tt 19058 betad\_cdf(2,3,0.25,.5) 19059\end{center} 19060you will get 19061\begin{center} 19062 \tt 19063 0.42578125 19064\end{center} 19065 19066\subsubsection{The inverse distribution function for the beta distribution: \texttt{betad\_icdf}\index{betad\_icdf}} 19067 19068The inverse distribution function for the beta 19069distribution with parameters $a$ and $b$ is computed with 19070\texttt{betad\_icdf($a$,$b$,$h$)}; recall that this will return the 19071value $x$ with $\texttt{betad\_cdf}(a,b,x) = h$. If you enter 19072\begin{center} 19073 \tt 19074 betad\_icdf(2,3,0.2) 19075\end{center} 19076you will get 19077\begin{center} 19078 \tt 19079 0.212317128278 19080\end{center} 19081 19082\subsection{The geometric distribution} 19083 19084\subsubsection{The probability density function for the geometric 19085distribution: \texttt{geometric}\index{geometric}} 19086 19087If an experiment with probability of success $p$ is iterated, the 19088probability that the first success occurs on the $k$th trial is 19089$(1-p)^{k-1}p$. This gives the geometric distribution (with parameter 19090$p$) on the natural numbers. Given such a $p$, the geometric density 19091function at $n$ is given by $\texttt{geometric}(p,n) = (1-p)^{n-1}p$. 19092If you enter 19093\begin{center} 19094 \tt 19095 geometric(0.2,3) 19096\end{center} 19097for example, you will get 19098\begin{center} 19099 \tt 19100 0.128 19101\end{center} 19102 19103\subsubsection{The cumulative distribution function of the geometric 19104distribution: \texttt{geometric\_cdf}\index{geometric\_cdf}} 19105 19106The cumulative distribution function for the geometric distribution with 19107parameter $p$ at a natural number $n$ is 19108$\texttt{geometric\_cdf}(p,n) = \texttt{Prob}(X \le n)$, which in this 19109case turns out to be $\texttt{geometric\_cdf}(p,n) = 191101 - (1-p)^n$. 19111If you enter 19112\begin{center} 19113 \tt 19114 geometric\_cdf(0.2,3) 19115\end{center} 19116for example, you will get 19117\begin{center} 19118 \tt 19119 0.488 19120\end{center} 19121 19122If you give \texttt{geometric\_cdf} an extra argument $k$, also a 19123natural number, you will get the probability that 19124the random variable lies between the two values; 19125$\texttt{geometric\_cdf($p$,$n$,$k$)} = \text{Prob}(n \le X \le k)$. 19126If you enter 19127\begin{center} 19128 \tt 19129 geometric\_cdf(0.2,3,5) 19130\end{center} 19131you will get 19132\begin{center} 19133 \tt 19134 0.31232 19135\end{center} 19136 19137\subsubsection{The inverse distribution function for the 19138geometric distribution: \texttt{geometric\_icdf}\index{geometric\_icdf}} 19139 19140The inverse distribution function for the geometric 19141distribution with parameter $p$ is computed with 19142\texttt{geometric\_icdf($p$,$h$)}; recall that this will return the 19143smallest natural number $n$ with $\texttt{geometric\_cdf}(p,n) \ge h$. If you enter 19144\begin{center} 19145 \tt 19146 geometric\_icdf(0.2,0.5) 19147\end{center} 19148you will get 19149\begin{center} 19150 \tt 19151 4 19152\end{center} 19153 19154\subsection{The Cauchy distribution} 19155 19156\subsubsection{The probability density function for the Cauchy 19157distribution: \texttt{cauchy}\index{cauchy} \texttt{cauchyd}\index{cauchyd}} 19158 19159The probability density function of the Cauchy distribution (sometimes 19160called the Lorentz distribution) is given by the 19161\texttt{cauchy} (or \texttt{cauchyd}) command. The Cauchy 19162distribution depends on two parameters $a$ and $b$, and the value of 19163the density function at $x$ is 19164$\texttt{cauchy}(a,b,x) = b/(\pi ((x-a)^2 + b^2))$. 19165If you enter 19166\begin{center} 19167 \tt 19168 cauchy(2.2,1.5,0.8) 19169\end{center} 19170you will get 19171\begin{center} 19172 \tt 19173 0.113412073462 19174\end{center} 19175 19176If you leave out the parameters $a$ and $b$, they will default to $0$ 19177and $1$, respectively; $\texttt{cauchy}(x) = 1/(\pi (x^2 + 1))$. If 19178you enter 19179\begin{center} 19180 \tt 19181 cauchy(0.3) 19182\end{center} 19183you will get 19184\begin{center} 19185 \tt 19186 0.292027418517 19187\end{center} 19188 19189\subsubsection{The cumulative distribution function for the Cauchy 19190distribution: \texttt{cauchy\_cdf}\index{cauchy\_cdf} \texttt{cauchyd\_cdf}\index{cauchyd\_cdf}} 19191 19192The command \texttt{cauchy\_cdf} (or \texttt{cauchyd\_cdf}) computes 19193the cumulative distribution function for the Cauchy distribution. 19194Like \texttt{cauchy}, you can give it the parameters $a$ and $b$, or 19195let them default to $0$ and $1$. The Cauchy cumulative distribution 19196function is given by the formula 19197$\texttt{cauchy\_cdf}(a,b,x) = 1/2 + \arctan((x-a)/b)/\pi$. 19198If you enter 19199\begin{center} 19200 \tt 19201 cauchy\_cdf(2,3,1.4) 19202\end{center} 19203you will get 19204\begin{center} 19205 \tt 19206 0.437167041811 19207\end{center} 19208and if you enter 19209\begin{center} 19210 \tt 19211 cauchy\_cdf(1.4) 19212\end{center} 19213you will get 19214\begin{center} 19215 \tt 19216 0.802568456711 19217\end{center} 19218 19219If you give \texttt{cauchy\_cdf} an extra argument (with or without 19220the parameters), you will get the probability that 19221the random variable lies between two values; 19222$\texttt{cauchy\_cdf($a$,$b$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 19223enter 19224\begin{center} 19225 \tt 19226 cauchy\_cdf(2,3,-1.9,1.4) 19227\end{center} 19228you will get 19229\begin{center} 19230 \tt 19231 0.228452641651 19232\end{center} 19233 19234\subsubsection{The inverse distribution function for the 19235Cauchy distribution: \texttt{cauchy\_icdf}\index{cauchy\_icdf} \texttt{cauchyd\_icdf}\index{cauchyd\_icdf}} 19236 19237Given a value $h$, the inverse distribution function gives 19238the value of $x$ with $\texttt{Prob}(X \le x) = h$. 19239The \texttt{cauchy\_icdf} will compute the 19240inverse distribution for the Cauchy distribution. (If no 19241parameters are given, they will be assumed to be 0 and 1.) 19242If you enter 19243\begin{center} 19244 \tt 19245 cauchy\_icdf(2,3,0.23) 19246\end{center} 19247you will get 19248\begin{center} 19249 \tt 19250 -1.40283204777 19251\end{center} 19252 19253\subsection{The uniform distribution} 19254\subsubsection{The probability density function for the uniform 19255distribution: \texttt{uniform}\index{uniform} \texttt{uniformd}\index{uniformd}} 19256 19257Given two values $a$ and $b$ with $a < b$, the uniform distribution on 19258$[a,b]$ has density function $1/(b-a)$ for $x$ in $[a,b]$. The 19259\texttt{uniform} (or \texttt{uniformd}) command will compute this; 19260$\texttt{uniform}(a,b,x) = 1/(b-a)$. If you enter 19261\begin{center} 19262 \tt 19263 uniform(2.2,3.5,2.8) 19264\end{center} 19265you will get 19266\begin{center} 19267 \tt 19268 0.769230769231 19269\end{center} 19270 19271\subsubsection{The cumulative distribution function for the uniform 19272distribution: \texttt{uniform\_cdf}\index{uniform\_cdf} \texttt{uniformd\_cdf}\index{uniformd\_cdf}} 19273 19274Given two values $a$ and $b$ with $a <b$, the cumulative distribution 19275function for the uniform distribution on $[a,b]$ is (for $x$ in $[a,b]$) 19276$\texttt{uniform\_cdf}(a,b,x) = \texttt{Prob}(X \le x) = (x-a)/(b-a)$. 19277If you enter 19278\begin{center} 19279 \tt 19280 uniform\_cdf(2,4,3.2) 19281\end{center} 19282you will get 19283\begin{center} 19284 \tt 19285 0.6 19286\end{center} 19287 19288With an extra argument $y$ in $[a,b]$, \texttt{uniform\_cdf} will 19289compute $\texttt{uniform\_cdf}(a,b,x,y) = \texttt{Prob}(x \le X \le y) 19290= (y-x)/(b-a)$. If you enter 19291\begin{center} 19292 \tt 19293 uniform\_cdf(2,4,3,3.2) 19294\end{center} 19295you will get 19296\begin{center} 19297 \tt 19298 0.1 19299\end{center} 19300 19301\subsubsection{The inverse distribution function for the 19302uniform distribution: \texttt{uniform\_icdf}\index{uniform\_icdf} \texttt{uniformd\_icdf}\index{uniformd\_icdf}} 19303 19304Given a value $h$, the inverse distribution function for a uniform 19305distribution is the value of $x$ with $\texttt{Prob}(X \le x) = 19306\texttt{uniform\_cdf}(a,b,x) = h$. This value is computed with the 19307\texttt{uniform\_icdf} command. If you enter 19308\begin{center} 19309 \tt 19310 uniform\_icdf(2,3,.6) 19311\end{center} 19312you will get 19313\begin{center} 19314 \tt 19315 2.6 19316\end{center} 19317 19318\subsection{The exponential distribution} 19319 19320\subsubsection{The probability density function for the exponential distribution: \texttt{exponential}\index{exponential} \texttt{exponentiald}\index{exponentiald}} 19321 19322The exponential distribution depends on one parameters, $\lambda>0$; the 19323value of the density function at $x \ge 0$ is 19324$\texttt{exponential}(\lambda,x) = 19325\lambda e^{-\lambda x}$. If you enter 19326\begin{center} 19327 \tt 19328 exponential(2.1,3.5) 19329\end{center} 19330for example, you will get 19331\begin{center} 19332 \tt 19333 0.00134944395675 19334\end{center} 19335 19336\subsubsection{The cumulative distribution function for the exponential distribution: \texttt{exponential\_cdf}\index{exponential\_cdf} \texttt{exponentiald\_cdf}\index{exponentiald\_cdf}} 19337 19338The cumulative distribution function for the exponential distribution with 19339parameter $\lambda > 0$ at a value $x \ge 0$ is 19340$\texttt{exponential\_cdf}(\lambda,x) = \texttt{Prob}(X \le x)$. 19341If you enter 19342\begin{center} 19343 \tt 19344 exponential\_cdf(2.3,3.2) 19345\end{center} 19346for example, you will get 19347\begin{center} 19348 \tt 19349 0.99936380154 19350\end{center} 19351 19352If you give \texttt{exponential\_cdf} an extra argument $y > x$, 19353you will get the probability that 19354the random variable lies between the two values; 19355$\texttt{exponential\_cdf($\lambda$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. 19356If you enter 19357\begin{center} 19358 \tt 19359 exponential\_cdf(2.3,0.9,3.2) 19360\end{center} 19361you will get 19362\begin{center} 19363 \tt 19364 0.125549583246 19365\end{center} 19366 19367\subsubsection{The inverse distribution function for the exponential distribution: \texttt{exponential\_icdf}\index{exponential\_icdf} \texttt{exponentiald\_icdf}\index{exponentiald\_icdf}} 19368 19369The inverse distribution function for the exponential 19370distribution with parameter $\lambda > 0$ is computed with 19371\texttt{exponential\_icdf($\lambda$,$h$)}; recall that this will return the 19372value $x$ with $\texttt{exponential\_cdf}(\lambda,x) = h$. If you enter 19373\begin{center} 19374 \tt 19375 exponential\_icdf(2.3,0.87) 19376\end{center} 19377you will get 19378\begin{center} 19379 \tt 19380 0.887052534142 19381\end{center} 19382 19383\subsection{The Weibull distribution} 19384\subsubsection{The probability density function for the Weibull 19385distribution: \texttt{weibull}\index{weibull} 19386\texttt{weibulld}\index{weibulld}} 19387 19388The Weibull distribution depends on three parameters; $k>0$, $\lambda 19389> 0$ and a real number $\theta$. The probability density at $x$ is 19390given by $\frac{k}{\lambda}(\frac{x - \theta}{\lambda})^2 19391e^{-((x-\theta)\lambda)^2}$. The \texttt{weibull} (or 19392\texttt{weibulld}) command computes this, where it can take arguments 19393$k$,$\lambda$,$\theta$ and $x$, where the $\theta$ can be left out and 19394will default to 0. If you enter 19395\begin{center} 19396 \tt 19397 weibull(2,1,3) 19398\end{center} 19399or 19400\begin{center} 19401 \tt 19402 weibull(2,1,0,3) 19403\end{center} 19404you will get 19405\begin{center} 19406 \tt 19407 6/exp(9) 19408\end{center} 19409 19410\subsubsection{The cumulative distribution function for the Weibull distribution: \texttt{weibull\_cdf}\index{weibull\_cdf} \texttt{weibulld\_cdf}\index{weibulld\_cdf}} 19411 19412The command \texttt{weibull\_cdf} computes 19413the cumulative distribution function for the Weibull distribution. 19414Like \texttt{weibull}, it takes parameters $k$, $\lambda$ and 19415$\theta$, where $\theta$ will default to 1 if it is omitted. 19416The Weibull cumulative distribution 19417function is given by the formula 19418$\texttt{weibull\_cdf}(k,\lambda,\theta,x) = 1 - 19419e^{-((x-\theta)/\lambda)^2}$. 19420If you enter 19421\begin{center} 19422 \tt 19423 weibull\_cdf(2,3,5) 19424\end{center} 19425or 19426\begin{center} 19427 \tt 19428 weibull\_cdf(2,3,0,5) 19429\end{center} 19430you will get 19431\begin{center} 19432 \tt 19433 1-exp(-25/9) 19434\end{center} 19435and if you enter 19436\begin{center} 19437 \tt 19438 weibull\_cdf(2.2,1.5,0.4,1.9) 19439\end{center} 19440you will get 19441\begin{center} 19442 \tt 19443 0.632120558829 19444\end{center} 19445 19446If you give \texttt{weibull\_cdf} an extra argument (which will 19447require that $\theta$ be explicitly included), you will get the probability that 19448the random variable lies between two values; 19449$\texttt{weibull\_cdf($k$,$\lambda$,$\theta$,$x$,$y$)} = \text{Prob}(x \le X \le y)$. If you 19450enter 19451\begin{center} 19452 \tt 19453 weibull\_cdf(2.2,1.5,0.4,1.2,1.9) 19454\end{center} 19455for example you will get 19456\begin{center} 19457 \tt 19458 0.410267239944 19459\end{center} 19460 19461\subsubsection{The inverse distribution function for the Weibull distribution: \texttt{weibull\_icdf}\index{weibull\_icdf} \texttt{weibulld\_icdf}\index{weibulld\_icdf}} 19462 19463Given a value $h$, the inverse distribution function gives 19464the value of $x$ with $\texttt{Prob}(X \le x) = h$. 19465The \texttt{weibull\_icdf} command will compute the 19466inverse distribution for the Weibull distribution. This uses the 19467arguments $k$, $\lambda$ and $\theta$ as well as $h$, although 19468$\theta$ can be omitted and will default to 0. 19469If you enter 19470\begin{center} 19471 \tt 19472 weibull\_icdf(2.2,1.5,0.4,0.632) 19473\end{center} 19474you will get 19475\begin{center} 19476 \tt 19477 1.89977657604 19478\end{center} 19479 19480\subsection{The Kolmogorov-Smirnov distribution: \texttt{kolmogorovd}\index{kolmogorovd}} 19481 19482For real $x$, the \texttt{kolmogorovd} command computes the density 19483function for the Kolmogorov-Smirnov distribution. 19484\[ 19485\texttt{kolmogorovd}(x) = 1 - 2\sum_{k=1}^{\infty} (-1)^{k-1} e^{-k^2 x^2} 19486\] 19487If you enter 19488\begin{center} 19489 \tt 19490 kolmogorovd(1.36) 19491\end{center} 19492for example, you will get 19493\begin{center} 19494 \tt 19495 0.950514123245 19496\end{center} 19497 19498\subsection{The Wilconon or Mann-Whitney distribution} 19499\subsection{The Wilconon test polynomial: \texttt{wilcoxonp}\index{wilcoxonp}} 19500 19501The \texttt{wilcoxonp} command will compute the polynomial for the 19502Wilcoxon or Mann-Whitney test; it can take one or two parameters. If 19503you enter 19504\begin{center} 19505 \tt 19506 wilcoxonp(4) 19507\end{center} 19508you will get 19509\begin{center} 19510 \tt 19511 poly1[1/16,1/16,1/16,1/8,1/8,1/8,1/8,1/8,1/16,1/16,1/16] 19512\end{center} 19513and if you enter 19514\begin{center} 19515 \tt 19516 wilcoxonp(4,3) 19517\end{center} 19518you will get 19519\begin{center} 19520 \tt 19521 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] 19522\end{center} 19523 19524\subsubsection{The Wilcoxon/Mann-Whitney statistic: \texttt{wilcoxons}\index{wilcoxons}} 19525 19526Given two lists, or one list and a real number (a median), the 19527\texttt{wilcoxons} command will return the Wilcoxon or Mann-Whitney statistic. 19528If you enter 19529\begin{center} 19530 \tt 19531 wilcoxons([1,3,4,5,7,8,8,12,15,17],10) 19532\end{center} 19533you will get 19534\begin{center} 19535 \tt 19536 18 19537\end{center} 19538and if you enter 19539\begin{center} 19540 \tt 19541 wilcoxons([1,3,4,5,7,8,8,12,15,17],[2,6,10,11,13,14,15,18,19,20]) 19542\end{center} 19543you will get 19544\begin{center} 19545 \tt 19546 128.5 19547\end{center} 19548 19549\subsubsection{The Wilcoxon or Mann-Whitney test} 19550 19551The \texttt{wilcoxont} command will perform the Wilcoxon or 19552Mann-Whitney test, given two samples or one sample and a number (a 19553median). It can additionally take an optional third argument of a 19554function and an optional fourth argument of a real number. 19555If you enter 19556\begin{center} 19557 \tt 19558 wilcoxont([1,2,3,4,5,7,8,8,12,15,17],[2,6,10,11,13,14,15,18,19,20]) 19559\end{center} 19560you will get 19561\begin{verbatim} 19562 Mann-Whitney 2-sample test, H0 same Median, H1 <> 19563 ranksum 93.0, shifted ranksum 27.0 19564 u1=83 ,u2=27, u=min(u1,u2)=27 19565 Limit value to reject H0 26 19566 P-value 9055/176358 (0.0513444244094), alpha=0.05 H0 not rejected 19567 1 19568\end{center} 19569If you enter 19570\begin{center} 19571 \tt 19572 wilcoxont([1,3,4,5,7,8,8,12,15,17],[2,6,10,11,13,14,15,18,19,20],0.3) 19573\end{center} 19574you will get 19575\begin{verbatim} 19576 Mann-Whitney 2-sample test, H0 same Median, H1 <> 19577 ranksum 81.5, shifted ranksum 26.5 19578 u1=73.5 ,u2=26.5, u=min(u1,u2)=26.5 19579 Limit value to reject H0 35 19580 P-value 316/4199 (0.0752560133365), alpha=0.3 H0 rejected 19581 0 19582\end{verbatim} 19583and if you enter 19584\begin{center} 19585 \tt 19586 wilcoxont([1,3,4,5,7,8,8,12,15,17] ,10,`>`,0.05) 19587\end{center} 19588you will get 19589\begin{verbatim} 19590 Wilcoxon 1-sample test, H0 Median=10, H1 M<>10 19591 Wilcoxon statistic: 18, p-value: 0.375, confidence level: 0.05 19592 1 19593\end{verbatim} 19594 19595\subsection{Moment generating functions for probability distributions: \texttt{mgf}\index{mgf}} 19596 19597The \texttt{mgf} command will compute the moment generating function 19598for a probability distribution (such as normal, binomial, poisson, 19599beta, gamma). It takes as arguments the name of the distribution and 19600any necessary parameters. To find the moment generating function 19601for the standard normal distribution, you can enter 19602\begin{center} 19603 \tt 19604 mgf(normald,1,0) 19605\end{center} 19606and get 19607\begin{center} 19608 \tt 19609 exp(t) 19610\end{center} 19611If you enter 19612\begin{center} 19613 \tt 19614 mgf(binomial,n,p) 19615\end{center} 19616you will get 19617\begin{center} 19618 \tt 19619 (1-p+p*exp(t))\^{}n 19620\end{center} 19621 19622\subsection{Cumulative distribution functions: \texttt{cdf}\index{cdf}} 19623 19624The \texttt{cdf} command will take as arguments the name of a 19625probability distribution, along with any needed parameters, and return 19626an expression for the cumulative distribution function. If you enter 19627\begin{center} 19628 \tt 19629 cdf(normald,0,1) 19630\end{center} 19631you will get 19632\begin{center} 19633 \tt 19634 (erf(x*sqrt(2)/2)+1)/2 19635\end{center} 19636 19637You can evaluate the cumulative distribution function at a value by 19638adding the value as an argument; if you enter 19639\begin{center} 19640 \tt 19641 cdf(binomial,10,0.5,4) 19642\end{center} 19643you will get 19644\begin{center} 19645 \tt 19646 0.376953125 19647\end{center} 19648 19649\subsection{Inverse distribution functions: \texttt{icdf}\index{icdf}} 19650 19651The \texttt{icdf} command will take as arguments the name of a 19652probability distribution, along with any needed parameters, and return 19653an expression for the inverse cumulative distribution function. This 19654is typically most useful if you evaluate the inverse cumulative 19655function at a specific value by adding it as an argument. If you enter 19656\begin{center} 19657 \tt 19658 icdf(normald,0,0.5,0.975) 19659\end{center} 19660you will get 19661\begin{center} 19662 \tt 19663 0.97998199227 19664\end{center} 19665 19666\subsection{Markov chains: \texttt{markov}\index{markov}} 19667 19668Given the transition matrix of a Markov chain, the \texttt{markov} 19669command will compute characteristic features of the chain. If $M$ is 19670a transition matrix, then \texttt{markov($M$)} will return the list of 19671the positive recurrent states, the list of corresponding 19672invariant probabilities, the list of other strong connected 19673components, the list of probabilities of ending up in the sequence of 19674recurrent states. 19675For example, if you enter 19676\begin{center} 19677 \tt 19678 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]]) 19679\end{center} 19680you will get 19681\begin{center} 19682 \tt 19683 [[4]],[[0,0,0,0,1]],[[3,1,2,0]],[[1],[1],[1],[1],[1]] 19684\end{center} 19685 19686\subsection{Generating a random walks: \texttt{randmarkov}\index{randmarkov}} 19687 19688Given the transition matrix $M$ for a Markov chain and an initial 19689state $i_0$, the command \texttt{randmarkov($M$,$i_0$,$n$)} will 19690generate a random walk (given as a vector) starting at $i_0$ and 19691taking $n$ random steps, where each step is a transition with 19692probabilities given by $M$. For example, if you enter 19693\begin{center} 19694 \tt 19695 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) 19696\end{center} 19697you might get 19698\begin{center} 19699 \tt 19700 [2,3,2,0,3,2,2,0,3,2,0] 19701\end{center} 19702 19703Alternatively, given a vector $v = [n_1,\dots,n_p]$, the command 19704\texttt{randmatrix($v$,$i_0$)} will create a stochastic matrix with 19705$p$ recurrent loops (given by $v$) and $i_0$ transient states. If you 19706enter 19707\begin{center} 19708 \tt 19709 randmarkov([1,2],2) 19710\end{center} 19711you might get 19712\begin{center} 19713 \tt 19714 [[1.0,0.0,0.0,0.0,0.0], 19715 [0.0,0.289031975209,0.710968024791,0.0,0.0], 19716 [0.0,0.46230383289,0.53769616711,0.0,0.0], 19717 [0.259262238137,0.149948861946,0.143448150524,0.242132758802,0.205207990592], 19718 [0.231568633749,0.145429586345,0.155664673778,0.282556511895,0.184780594232]] 19719\end{center} 19720 19721\section{Hypothesis testing} 19722 19723\subsection{General} 19724 19725Given a random variable $X$, you may want to know whether some 19726effective parameter $p$ is the same as some expected value $p_0$. You 19727will then want to test the hypothesis $p = p_0$, which will be the 19728null hypothesis $H_0$. The alternative hypothesis will be $H_1$. 19729The tests are: 19730\begin{description} 19731 \item[Two-tailed test] This test will reject the hypothesis $H_0$ 19732 if the relevant statistic is outside of a determined interval. 19733 This can be denoted '!='. 19734 \item[Left-tailed test] This test will reject the hypothesis $H_0$ 19735 if the relevant statistic is less than a specific value. 19736 This can be denoted '<'. 19737 \item[Right-tailed test] This test will reject the hypothesis $H_0$ 19738 if the relevant statistic is greater than a specific value. 19739 This can be denoted '>'. 19740\end{description} 19741 19742\subsection{Testing the mean with the Z test: \texttt{normalt}\index{normalt}} 19743 19744The \texttt{normalt} command will use the Z test to test the mean of 19745data. 19746You need to provide the command with the following arguments: 19747\begin{enumerate} 19748 \item 19749 The sample data information can be given as a list $[n_s,n_e]$ 19750 consisting of the number of successes $n_s$ and the number of trials 19751 $n_e$, or a list $[m,t]$ consisting of the mean $m$ and the sample 19752 size $t$, or a data list of the sample. 19753 \item 19754 The mean of the population to or a data list from a control sample. 19755 \item 19756 The standard deviation of the population. If the data list from a 19757 control sample is provided, then this item is unnecessary. 19758 \item 19759 The type of test; "!=","<" or ">". 19760 \item 19761 The confidence level. This is optional; the default 19762 value is $0.05$. 19763\end{enumerate} 19764The \texttt{normalt} command will return the result of a Z test. It 19765will return 0 if the test fails, 1 if the test succeeds, and it will 19766display a summary of the test. 19767 19768If you enter 19769\begin{center} 19770 \tt 19771 normalt([10,30], 0.5, 0.02, '!=', 0.1) 19772\end{center} 19773you will get 19774\begin{verbatim} 19775 *** TEST RESULT 0 *** 19776 Summary Z-Test null hypothesis H0 mu1=mu2, alt. hyp. H1 mu1!=mu2. 19777 Test returns 0 if probability to observe data is less than 0.1 19778 (null hyp. mu1=mu2 rejected with less than alpha probability error) 19779 Test returns 1 otherwise (can not reject null hypothesis) 19780 Data mean mu1=10, population mean mu2=0.5 19781 alpha level 0.1, multiplier*stddev/sqrt(sample size)= 1.64485*0.02/5.47723 19782 0 19783\end{verbatim} 19784If you enter 19785\begin{center} 19786 \tt 19787 normalt([0.48,50],0.5,0.1,'<') 19788\end{center} 19789you will get 19790\begin{verbatim} 19791 *** TEST RESULT 1 *** 19792 Summary Z-Test null hypothesis H0 mu1=mu2, alt. hyp. H1 mu1<mu2. 19793 Test returns 0 if probability to observe data is less than 0.05 19794 (null hyp. mu1=mu2 rejected with less than alpha probability error) 19795 Test returns 1 otherwise (can not reject null hypothesis) 19796 Data mean mu1=0.48, population mean mu2=0.5 19797 alpha level 0.05, multiplier*stddev/sqrt(sample size)= 1.64485*0.1/7.07107 19798 1 19799\end{verbatim} 19800 19801\subsection{Testing the mean with the T test: \texttt{studentt}\index{studentt}} 19802 19803The \texttt{studentt} command will examine whether data conforms to 19804Student's distribution. For small sample sizes, the \texttt{studentt} 19805test is preferable to \texttt{normalt}. 19806You need to provide the \texttt{studentt} command with the following 19807arguments: 19808\begin{enumerate} 19809 \item 19810 The sample data information can be given as a list $[n_s,n_e]$ 19811 consisting of the number of successes $n_s$ and the number of trials 19812 $n_e$, or a list $[m,t]$ consisting of the mean $m$ and the sample 19813 size $t$, or a data list of the sample. 19814 \item 19815 The mean of the population to or a data list from a control sample. 19816 \item 19817 The standard deviation of the population. If the data list from a 19818 control sample is provided, then this item is unnecessary. 19819 \item 19820 The type of test; "!=","<" or ">". 19821 \item 19822 The confidence level. This is optional; the default 19823 value is $0.05$. 19824\end{enumerate} 19825The \texttt{studentt} command will return the result of a T test. It 19826will return 0 if the test fails, 1 if the test succeeds, and it will 19827display a summary of the test. 19828 19829If you enter 19830\begin{center} 19831 \tt 19832 studentt([10,20], 0.5, 0.02, '!=', 0.1) 19833\end{center} 19834you will get 19835\begin{verbatim} 19836 *** TEST RESULT 0 *** 19837 Summary T-Test null hypothesis H0 mu1=mu2, alt. hyp. H1 mu1!=mu2. 19838 Test returns 0 if probability to observe data is less than 0.1 19839 (null hyp. mu1=mu2 rejected with less than alpha probability error) 19840 Test returns 1 otherwise (can not reject null hypothesis) 19841 Data mean mu1=10, population mean mu2=0.5, degrees of freedom 20 19842 alpha level 0.1, multiplier*stddev/sqrt(sample size)= 1.32534*0.02/4.47214 19843 0 19844\end{verbatim} 19845If you enter 19846\begin{center} 19847 \tt 19848 studentt([0.48,20],0.5,0.1,'<') 19849\end{center} 19850you will get 19851\begin{verbatim} 19852 *** TEST RESULT 1 *** 19853 Summary T-Test null hypothesis H0 mu1=mu2, alt. hyp. H1 mu1<mu2. 19854 Test returns 0 if probability to observe data is less than 0.05 19855 (null hyp. mu1=mu2 rejected with less than alpha probability error) 19856 Test returns 1 otherwise (can not reject null hypothesis) 19857 Data mean mu1=0.48, population mean mu2=0.5, degrees of freedom 20 19858 alpha level 0.05, multiplier*stddev/sqrt(sample size)= 1.72472*0.1/4.47214 19859 1 19860\end{verbatim} 19861 19862\subsection{Testing a distribution with the $\chi^2$ distribution: \texttt{chisquaret}\index{chisquaret}} 19863 19864The \texttt{chisquaret} command will use the $\chi^2$ test to compare 19865sample data to a specified distribution. 19866You need to provide \texttt{chisquaret} with the following arguments: 19867\begin{enumerate} 19868 \item A list of sample data. 19869 \item The name of a distribution, or another list of sample data. 19870 If this is omitted, a uniform distribution will be used. 19871 \item The parameters of the distribution, if a name is given as the 19872 previous argument, or the parameter \texttt{class} followed by 19873 \texttt{class\_min} and \texttt{class\_dim} (or the default values 19874 will be used). 19875\end{enumerate} 19876The \texttt{chisquaret} command will return the result of the $\chi^2$ 19877test between the sample data and the named distribution or the two 19878sample data. 19879 19880For example, if you enter 19881\begin{center} 19882 \tt 19883 chisquaret([57,54]) 19884\end{center} 19885you will get 19886\begin{verbatim} 19887 Guessing data is the list of number of elements in each class, 19888 adequation to uniform distribution 19889 Sample adequation to a finite discrete probability distribution 19890 Chi2 test result 0.0810810810811, 19891 reject adequation if superior to chisquare_icdf(1,0.95)=3.84145882069 or chisquare_icdf(1,1-alpha) if alpha!=5% 19892 0.0810810810811 19893\end{verbatim} 19894If you enter 19895\begin{center} 19896 \tt 19897 chisquaret([1,1,1,1,1,0,0,1,0,1,1],[.4,.6]) 19898\end{center} 19899you will get 19900\begin{verbatim} 19901 Sample adequation to a finite discrete probability distribution 19902 Chi2 test result 0.742424242424, 19903 reject adequation if superior to chisquare_icdf(1,0.95)=3.84145882069 19904 or chisquare_icdf(1,1-alpha) if alpha!=5% 19905 0.742424242424 19906\end{verbatim} 19907If you enter 19908\begin{center} 19909 \tt 19910 chisquaret(ranv(1000,binomial,10,.5),binomial) 19911\end{center} 19912you will get 19913\begin{verbatim} 19914 Binomial: estimating n and p from data 10 0.5055 19915 Sample adequation to binomial(10,0.5055,.), Chi2 test result 7.77825189838, 19916 reject adequation if superior to chisquare_icdf(7,0.95)=14.0671404493 19917 or chisquare_icdf(7,1-alpha) if alpha!=5% 19918 7.77825189838 19919\end{verbatim} 19920and if you enter 19921\begin{center} 19922 \tt 19923 chisquaret(ranv(1000,binomial,10,.5),binomial,11,.5) 19924\end{center} 19925you will get 19926\begin{verbatim} 19927 Sample adequation to binomial(11,0.5,.), Chi2 test result 125.617374161, 19928 reject adequation if superior to chisquare_icdf(10,0.95)=18.3070380533 19929 or chisquare_icdf(10,1-alpha) if alpha!=5% 19930 125.617374161 19931\end{verbatim} 19932For an example using \texttt{class\_min} and \texttt{class\_dim}, let 19933\begin{center} 19934 \tt 19935 L := ranv(1000,normald,0,.2) 19936\end{center} 19937If you then enter 19938\begin{center} 19939 \tt 19940 chisquaret(L,normald,classes,-2,.25) 19941\end{center} 19942or equivalently set \texttt{class\_min} to $-2$ and 19943\texttt{class\_dim} to $-0.25$ in the graphical configuration and enter 19944\begin{center} 19945 \tt 19946 chisquaret(L,normald,classes) 19947\end{center} 19948you will get 19949\begin{verbatim} 19950 Normal density, 19951 estimating mean and stddev from data -0.00345919752912 0.201708100832 19952 Sample adequation to normald_cdf(-0.00345919752912,0.201708100832,.), 19953 Chi2 test result 2.11405080381, 19954 reject adequation if superior to chisquare_icdf(4,0.95)=9.48772903678 19955 or chisquare_icdf(4,1-alpha) if alpha!=5% 19956 2.11405080381 19957\end{verbatim} 19958In this last case, you are given the value of $d^2$ of the statistic 19959$D^2 = \sum_{j=1}^{k} (n_j - e_j)/e_j$, where $k$ is the number of 19960sample classes for \texttt{classes(L,-2,0.25)} (or 19961\texttt{classes(L)}), $n_j$ is the size of the $j$th class, and $e_j = 19962n p_j$ where $n$ is the size of \texttt{L} and $p_j$ is the 19963probability of the $j$th class interval assuming a normal distribution 19964with the mean and population standard deviation of \texttt{L}. 19965 19966\subsection{Testing a distribution with the Kolmogorov-Smirnov 19967distribution: \texttt{kolmogorovt}\index{kolmogorovt}} 19968 19969The \texttt{kolmogorovt} command will use the Kolmogorov test to compare 19970sample data to a specified continuous distribution. 19971You need to provide \texttt{kolmogorovt} with either two lists of data 19972or a list of data followed by the name of a distribution with the 19973parameters. 19974The \texttt{kolmogorovt} command will return three values: 19975\begin{itemize} 19976 \item The $D$ statistic, which is the maximum distance between the 19977 cumulative distribution functions of the samples or the sample and 19978 the given distribution. 19979 \item The $K$ value, where $K = D\sqrt{n}$ (for a single data set, 19980 where $n$ is the size of the data set) or $K=D\sqrt{n_1 n_2 /(n_1 + 19981 n_2)}$ (when there are two data sets, with sizes $n_1$ and $n_2$). 19982 The $K$ value will tend towards the Kolmogorov-Smirnov distribution 19983 as the size of the data set goes to infinity. 19984 \item \texttt{1 - kolmogorovd(K)}, which will be close to 1 when the 19985 distributions look like they match. 19986\end{itemize} 19987For example, if you enter 19988\begin{center} 19989 \tt 19990 kolmogorovt(randvector(100,normald,0,1),normald(0,1)) 19991\end{center} 19992you might get 19993\begin{center} 19994 \tt 19995 ["D=",0.112592987625,"K=",1.12592987625,"1-kolmogorovd(K)=",0.158375510292] 19996\end{center} 19997and if you enter 19998\begin{center} 19999 \tt 20000 kolmogorovt(randvector(100,normald,0,1),student(2)) 20001\end{center} 20002you might get 20003\begin{center} 20004 \tt 20005 ["D=",0.0996114067923,"K=",0.996114067923,"1-kolmogorovd(K)=",0.27418851907] 20006\end{center} 20007 20008\chapter{Numerical computations}\label{sec:numeric} 20009Real numbers may have an exact representation 20010(e.g. rationals, symbolic expressions 20011involving square roots or constants like $\pi$, ...) 20012or approximate representation, which means that the real 20013is represented by a rational (with a denominator that is a power 20014of the basis of the representation) close to the real. 20015Inside {\tt Xcas}, the standard scientific notation is used 20016for approximate representation, that is a mantissa (with a point 20017as decimal separator) optionally followed by the letter {\tt e} 20018and an integer exponent. 20019 20020Note that the real number $10^{-4}$ is an exact number but 20021$1e-4$ is an approximate representation of this number. 20022 20023\section{Floating point representation.} 20024In this section, we explain how real numbers are represented. 20025 20026\subsection{Digits} 20027The {\tt Digits} variable is used to control how real numbers 20028are represented and also how they are displayed. 20029When the specified 20030number of digits is less or equal to 14 (for example {\tt 20031 Digits:=14}), then hardware floating point 20032numbers are used and they are displayed using the specified 20033number of digits. 20034When {\tt Digits} is larger than 14, Xcas uses the MPFR 20035library, the representation is similar to hardware floats 20036(cf. infra) but the number of bits of 20037the mantissa is not fixed and the range of exponents is much larger. 20038More precisely, the number of bits of the mantissa of a created MPFR float 20039is {\tt ceil(Digits*log(10)/log(2))}. 20040 20041Note that if you change the value of {\tt Digits}, this will affect 20042the creation of new real numbers compiled from command lines 20043or programs or by instructions like {\tt approx}, but it will 20044not affect existing real numbers. Hence hardware floats may coexist 20045with MPFR floats, and even in MPFR floats, some may have 100 bits 20046of mantissa and some may have 150 bits of mantissa. If operations 20047mix different kinds of floats, the most precise kind of floats 20048are coerced to the less precise kind of floats. 20049 20050\subsection{Representation by hardware floats} 20051A real is represented by a floating number $d$, that is 20052\[ d=2^\alpha*(1+m), \quad 0<m<1, -2^{10} < \alpha < 2^{10} \] 20053If $\alpha>1-2^{10}$, then $m \geq 1/2$, and $d$ is 20054a normalized floating point number, otherwise 20055$d$ is denormalized ($\alpha=1-2^{10}$). The special exponent $2^{10}$ 20056is used to represent plus or minus infinity and NaN (Not a Number). 20057A hardware float is made of 64 bits: 20058\begin{itemize} 20059\item the first bit is for the sign of $d$ (0 for '+' and 1 for '-') 20060\item the 11 following bits represents the exponent, more precisely 20061if $\alpha$ denotes the integer from the 11 bits, 20062the exponent is $\alpha+2^{10}-1$, 20063\item the 52 last bits codes the mantissa $m$, more precisely if 20064$M$ denotes the integer from the 52 bits, then 20065$m=1/2+M/2^{53}$ for normalized floats and $m=M/2^{53}$ for 20066denormalized floats. 20067\end{itemize} 20068Examples of representations of the exponent: 20069\begin{itemize} 20070\item $\alpha=0$ is coded by 011 1111 1111 20071\item $\alpha=1$ is coded by 100 0000 0000 20072\item $\alpha=4$ is coded by 100 0000 0011 20073\item $\alpha=5$ is coded by 100 0000 0100 20074\item $\alpha=-1$ is coded by 011 1111 1110 20075\item $\alpha=-4$ is coded by 011 1111 1011 20076\item $\alpha=-5$ is coded by 011 1111 1010 20077\item $\alpha=2^{10}$ is coded by 111 1111 1111 20078\item $\alpha=2^{-10}-1$ is coded by 000 0000 000 20079\end{itemize} 20080{\bf Remark}: $2^{-52}=0.2220446049250313e-15$ 20081 20082\subsection{Examples of representations of normalized floats} 20083\begin{itemize} 20084\item 3.1 :\\ 20085We have : 20086\begin{eqnarray*} 200873.1&=&2*(1+\frac{1}{2}+\frac{1}{2^5}+\frac{1}{2^6}+ 20088\frac{1}{2^9}+\frac{1}{2^{10}}+....)\\ 20089&=&2*(1+\frac{1}{2}+\sum_{k=1}^\infty(\frac{1}{2^{4*k+1}}+\frac{1}{2^{4*k+2}}) ) 20090\end{eqnarray*} 20091hence $\alpha=1$ and 20092$m=\frac{1}{2}+\sum_{k=1}^\infty(\frac{1}{2^{4*k+1}}+\frac{1}{2^{4*k+2}})$. 20093Hence the hexadecimal and binary representation of 3.1 is: 20094\begin{verbatim} 2009540 (01000000), 8 (00001000), cc (11001100), cc (11001100), 20096cc (11001100), cc (11001100), cc (11001100), cd (11001101), 20097\end{verbatim} 20098the last octet is 1101, the last bit is 1, because the 20099following digit is 1 (upper rounding). 20100\item 3. :\\ 20101We have $3=2*(1+1/2)$. 20102Hence the hexadecimal and binary representation of 3 is: 20103\begin{verbatim} 2010440 (01000000), 8 (00001000), 0 (00000000), 0 (00000000), 201050 (00000000), 0 (00000000), 0 (00000000), 0 (00000000) 20106\end{verbatim} 20107\end{itemize} 20108 20109\subsection{Difference between the representation of (3.1-3) and of 0.1} 20110\begin{itemize} 20111\item representation of 0.1 :\\ 20112We have : 20113\[ 0.1=2^{-4}*(1+\frac{1}{2}+\frac{1}{2^4}+\frac{1}{2^5}+ 20114\frac{1}{2^8}+\frac{1}{2^9}+...)= 201152^{-4}*\sum_{k=0}^\infty (\frac{1}{2^{4*k}}+\frac{1}{2^{4*k+1}}) \] 20116hence $\alpha=1$ and $m=\frac{1}{2}+ 20117\sum_{k=1}^\infty (\frac{1}{2^{4*k}}+\frac{1}{2^{4*k+1}})$, 20118therefore the representation of 0.1 is 20119\begin{verbatim} 201203f (00111111), b9 (10111001), 99 (10011001), 99 (10011001), 2012199 (10011001), 99 (10011001), 99 (10011001), 9a (10011010), 20122\end{verbatim} 20123the last octet is 1010, indeed the 2 last bits 2012401 became 10 because the following digit is 1 (upper rounding). 20125 20126\item representation of a:=3.1-3 :\\ 20127Computing a is done by adjusting exponents (here nothing 20128to do), then subtract the mantissa, and adjust the 20129exponent of the result to have a normalized float. 20130The exponent is $\alpha=-4$ (that corresponds at $2*2^{-5}$) 20131and the bits 20132corresponding to the mantissa begin at $1/2=2*2^{-6}$ : 20133the bits of the mantissa are shifted to the left of 5 positions 20134and we have : 20135\begin{verbatim} 201363f (00111111), b9 (10111001), 99 (10011001), 99 (10011001), 2013799 (10011001), 99 (10011001), 99 (10011001), 9a (10100000), 20138\end{verbatim} 20139Therefore 20140$a>0.1$ and $a-0.1=1/2^{50}+1/2^{51}$ 20141(since 100000-11010=110) 20142\end{itemize} 20143{\bf Remark}\\ 20144This is the reason why 20145\begin{center} 20146{\tt floor(1/(3.1-3))} 20147\end{center} 20148returns {\tt 9} and not {\tt 10} when {\tt Digits:=14}. 20149 20150\section{Approx. evaluation : {\tt evalf approx} and {\tt Digits}}\index{evalf|textbf}\index{approx|textbf}\index{DIGITS}\index{Digits} 20151\noindent {\tt evalf} or {\tt approx} evaluates to a numeric 20152approximation (if possible).\\ 20153Input : 20154\begin{center}{\tt evalf(sqrt(2))}\end{center} 20155Output, if in the {\tt cas} configuration ({\tt Cfg} menu) {\tt Digits=7} 20156(that is hardware floats are used, and 7 digits are displayed) : 20157\begin{center}{\tt 1.414214}\end{center} 20158You can change the number of digits in a command line by assigning 20159the variable {\tt DIGITS} or {\tt Digits}. 20160Input : 20161\begin{center}{\tt DIGITS:=20}\end{center} 20162\begin{center}{\tt evalf(sqrt(2))}\end{center} 20163Output : 20164\begin{center}{\tt 1.4142135623730950488}\end{center} 20165Input : 20166\begin{center}{\tt evalf(10\verb|^|-5)}\end{center} 20167Output : 20168\begin{center}{\tt 1e-05}\end{center} 20169Input : 20170\begin{center}{\tt evalf(10\verb|^|15)}\end{center} 20171Output : 20172\begin{center}{\tt 1e+15}\end{center} 20173Input : 20174\begin{center}{\tt evalf(sqrt(2))*10\verb|^|-5}\end{center} 20175Output : 20176\begin{center}{\tt 1.41421356237e-05}\end{center} 20177 20178\section{Numerical algorithms} 20179\subsection{Approximate solution of an equation : {\tt newton}}\index{newton} 20180\noindent{\tt newton} takes as arguments : an expression {\tt ex}, 20181the variable 20182name of this expression (by default {\tt x}), and three values {\tt a} (by 20183default {\tt a=0}), {\tt eps} (by default {\tt eps=1e-8}) and {\tt nbiter} 20184(by default {\tt nbiter=12}).\\ 20185{\tt newton(ex,x,a,eps,nbiter)} computes an approximate 20186solution {\tt x} of the equation {\tt ex=0} 20187using the Newton algorithm with starting point 20188{\tt x=a}. The maximum number of iterations is {\tt nbiter} 20189and the precision is {\tt eps}.\\ 20190Input : 20191\begin{center}{\tt newton(x\verb|^|2-2,x,1) }\end{center} 20192Output : 20193\begin{center}{\tt 1.41421356237}\end{center} 20194Input : 20195\begin{center}{\tt newton(x\verb|^|2-2,x,-1) }\end{center} 20196Output : 20197\begin{center}{\tt -1.41421356237}\end{center} 20198Input : 20199\begin{center}{\tt newton(cos(x)-x,x,0)}\end{center} 20200Output : 20201\begin{center}{\tt0.739085133215 }\end{center} 20202 20203\subsection{Approximate computation of the derivative number : {\tt nDeriv}}\index{nDeriv} 20204\noindent{\tt nDeriv} takes as arguments : an expression {\tt ex}, the variable 20205name of this expression (by default {\tt x}), and {\tt h} (by default 20206{\tt h=0.001}).\\ 20207{\tt nDeriv(ex,x,h)} computes an approximated value of the derivative of the 20208expression {\tt ex} at the point {\tt x} and returns : 20209\begin{center}{\tt (f(x+h)-f(x+h))/2*h}\end{center} 20210Input : 20211\begin{center}{\tt nDeriv(x\verb|^|2,x)}\end{center} 20212Output : 20213\begin{center}{\tt ((x+0.001)\verb|^|2-(x+-0.001)\verb|^|2)*500.0}\end{center} 20214Input : 20215\begin{center}{\tt subst(nDeriv(x\verb|^|2,x),x=1)}\end{center} 20216Output : 20217\begin{center}{\tt 2}\end{center} 20218Input : 20219\begin{center}{\tt nDeriv(exp(x\verb|^| 2),x,0.00001)}\end{center} 20220Output : 20221\begin{center}{\tt (exp((x+1e-05)\verb|^|2)-exp((x+-1e-05)\verb|^|2))*50000}\end{center} 20222Input : 20223\begin{center}{\tt subst(exp(nDeriv(x\verb|^| 2),x,0.00001),x=1)}\end{center} 20224Output : 20225\begin{center}{\tt 5.43656365783}\end{center} 20226which is an approximate value of {\tt 2e=5.43656365692}. 20227 20228\subsection{Approximate computation of integrals : {\tt romberg nInt}}\index{romberg}\index{nInt} 20229\noindent{\tt romberg} or {\tt nInt} takes as arguments : an expression 20230{\tt ex}, the variable name of this expression (by default {\tt x}), and 20231two real values {\tt a,b}.\\ 20232{\tt romberg(ex,x,a,b)} or {\tt nInt(ex,x,a,b)} computes an approximated 20233value of the integral $\int_a^b ex\ dx$ using the Romberg method. The 20234integrand must be sufficiently regular for the approximation to 20235be accurate. Otherwise, {\tt romberg} returns a list of real values, 20236that comes from the application of the 20237Romberg algorithm (the first list element is 20238the trapezoid rule approximation, the next ones come from the application 20239of the Euler-MacLaurin formula to remove successive even powers of 20240the step of the trapezoid rule).\\ 20241Input : 20242\begin{center}{\tt romberg(exp(x\verb|^|2),x,0,1)}\end{center} 20243Output : 20244\begin{center}{\tt 1.46265174591}\end{center} 20245 20246\subsection{Approximate solution of y'=f(t,y) : {\tt odesolve}}\index{odesolve|textbf} 20247\label{ssec:odesolve} 20248\begin{itemize} 20249\item Let $f$ be a function from $\mathbb R^2$ to $\mathbb R$.\\ 20250 {\tt odesolve(f(t,y),[t,y],[t0,y0],t1)} or\\ 20251{\tt odesolve(f(t,y),t=t0..t1,y,y0)} or\\ 20252{\tt odesolve(t0..t1,f,y0)} or\\ 20253{\tt odesolve(t0..t1,(t,y)->f(t,y),y0)}\\ 20254returns an approximate value of $y(t1)$ where $y(t)$ is the 20255solution of: 20256\[ y'(t)=f(t,y(t)), \quad y(t0)=y0 \] 20257\item {\tt odesolve} accepts an optional argument for the 20258discretization of {\tt t} ({\tt tstep=value}). 20259This value is passed as initial tstep value to the numeric solver 20260from the GSL (Gnu Scientific Library), it may be modified 20261by the solver. It is also used to control the number of iterations 20262of the solver by {\tt 2*(t1-t0)/tstep} (if the number 20263of iterations exceeds this value, the solver will stops at a time $t<t1$). 20264\item {\tt odesolve} accepts {\tt curve} as an optional argument. 20265In that case, 20266{\tt odesolve} returns the list of all the [$t,[y(t)]$] values 20267that were computed. 20268\end{itemize} 20269Input : 20270\begin{center}{\tt odesolve(sin(t*y),[t,y],[0,1],2)}\end{center} 20271or : 20272\begin{center}{\tt odesolve(sin(t*y),t=0..2,y,1)}\end{center} 20273or : 20274\begin{center}{\tt odesolve(0..2,(t,y)->sin(t*y),1)}\end{center} 20275or define the function : 20276\begin{center}{\tt f(t,y):=sin(t*y)} \end{center} 20277and input : 20278\begin{center}{\tt odesolve(0..2,f,1)}\end{center} 20279Output : 20280\begin{center}{\tt [1.82241255675]}\end{center} 20281Input : 20282\begin{center}{\tt odesolve(0..2,f,1,tstep=0.3)}\end{center} 20283Output : 20284\begin{center}{\tt [1.82241255675]}\end{center} 20285Input : 20286\begin{center}{\tt odesolve(sin(t*y),t=0..2,y,1,tstep=0.5)}\end{center} 20287Output : 20288\begin{center}{\tt [1.82241255675]}\end{center} 20289Input : 20290\begin{center}{\tt odesolve(sin(t*y),t=0..2,y,1,tstep=0.5,curve)}\end{center} 20291Output : 20292\begin{center}{\tt [[0.760963063136,[1.30972370515]],[1.39334557388,[1.86417104853]]]}\end{center} 20293 20294 20295\subsection{Approximate solution of the system v'=f(t,v) : {\tt odesolve}}\index{odesolve} 20296\begin{itemize} 20297\item If $v$ is a vector 20298of variables $[x1,..,xn]$ and if $f$ is given by a vector of expressions 20299{\tt [e1,...,en]} depending on $t$ and of $[x1,..,xn]$, 20300if the initial value of $v$ at {\tt t0} 20301is the vector $[x10,...,xn0]$ then the instruction 20302\begin{center} 20303{\tt odesolve([e1,..,en],t=t0..t1,[x1,...,xn], 20304[x10,...,xn0])} 20305\end{center} 20306returns an approximated value of $v$ at $t=t1$. 20307With the optional argument {\tt curve}, {\tt odesolve} returns the list of 20308the intermediate values of [$t,v(t)$] computed by the solver. 20309 20310Example, to solve the system 20311\begin{eqnarray*} 20312x'(t) &=&-y(t)\\ 20313y'(t)&=&x(t) 20314\end{eqnarray*} 20315Input : 20316\begin{center} 20317{\tt odesolve([-y,x],t=0..pi,[x,y],[0,1])}\end{center} 20318Output : 20319\begin{center}{\tt [-1.79045146764e-15,-1]}\end{center} 20320 20321\item If $f$ is a function from $\mathbb R \times \mathbb R^n$ to 20322$\mathbb R^n$.\\ 20323{\tt odesolve(t0..t1,(t,v)->f(t,v),v0)} or\\ 20324{\tt odesolve(t0..t1,f,v0)}\\ 20325computes an approximate value of $v(t1)$ where the vector $v(t)$ 20326in $\mathbb R^n$ is the solution of 20327\[ v'(t)=f(t,v(t)), v(t0)=v0 \] 20328With the optional argument {\tt curve}, {\tt odesolve} returns the list of 20329the intermediate value [$t,v(t)$] computed by the solver. 20330 20331Example, to solve the system :\\ 20332\begin{eqnarray*} 20333x'(t) &=&-y(t)\\ 20334y'(t)&=&x(t) 20335\end{eqnarray*} 20336Input : 20337\begin{center}{\tt odesolve(0..pi,(t,v)->[-v[1],v[0]],[0,1])}\end{center} 20338Or define the function: 20339\begin{center}{\tt f(t,v):=[-v[1],v[0]]}\end{center} 20340then input : 20341\begin{center}{\tt odesolve(0..pi,f,[0,1])}\end{center} 20342Output : 20343\begin{center}{\tt [-1.79045146764e-15,-1]}\end{center} 20344Alternative input : 20345\begin{center}{\tt odesolve(0..pi/4,f,[0,1],curve)}\end{center} 20346Output : 20347\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} 20348\end{itemize} 20349 20350 20351\section{Solve equations with {\tt fsolve nSolve}}\index{fsolve}\index{nSolve} 20352\noindent{\tt fsolve} or {\tt nSolve} solves numeric equations 20353(unlike {\tt solve} or {\tt proot}, it is not limited to polynomial 20354equations) of the form: 20355\[ f(x)=0, \quad x \in ]a,b[ \] 20356{\tt fsolve} or {\tt nSolve} accepts a last optional argument, 20357the name of an iterative algorithm to be used by the GSL solver. 20358The different methods are explained in the following section. 20359 20360\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt bisection\_solver}}\index{bisection\_solver@{\sl bisection\_solver}|textbf} 20361This algorithm of dichotomy is the simplest but also generically 20362the slowest. 20363It encloses the zero of a function on an interval. 20364Each iteration, cuts the interval into two parts. We compute the middle point 20365value. The function sign at this point, gives us the half-interval 20366on which the next iteration will be performed.\\ 20367Input : 20368\begin{center}{\tt fsolve((cos(x))=x,x,-1..1,bisection\_solver)}\end{center} 20369Output : 20370\begin{center}{\tt [0.739085078239,0.739085137844]}\end{center} 20371 20372\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt brent\_solver}}\index{brent\_solver{\sl brent\_solver}|textbf}\index{color@{\sl }|textbf} 20373The Brent method interpolates of $f$ at three points, finds 20374the intersection of the interpolation with the $x$ axis, computes 20375the sign of $f$ at this point and chooses the interval where the sign changes. 20376%: on prend l'intersection de la courbe d'interpolaton passant par 3 points with l'axe des $x$ 20377It is generically faster than bisection.\\ 20378Input : 20379\begin{center}{\tt fsolve((cos(x))=x,x,-1..1,brent\_solver)}\end{center} 20380Output : 20381\begin{center}{\tt [0.73908513321 5,0.739085133215]}\end{center} 20382 20383\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt falsepos\_solver}}\index{falsepos\_solver{\sl falsepos\_solver}|textbf} 20384The "false position" algorithm is an iterative algorithm based on linear 20385interpolation : we compute the value of $f$ at the intersection of the line 20386$(a,f(a))$, $(b,f(b))$ with the $x$ axis. This value gives us the part of the 20387interval containing the root, and on which a new iteration is performed.\\ 20388The convergence is linear but generically faster than bisection.\\ 20389Input : 20390\begin{center}{\tt fsolve((cos(x))=x,x,-1..1,falsepos\_solver)}\end{center} 20391Output : 20392\begin{center}{\tt [0.739085133215,0.739085133215]}\end{center} 20393 20394\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt newton\_solver}}\index{newton\_solver{\sl newton\_solver}|textbf} 20395{\tt newton\_solver} is the standard Newton method. 20396The algorithm starts at an initial value $x_0$, then we search the 20397intersection $x_1$ of the tangent at $x_0$ to the graph of $f$, with the $x$ 20398axis, the next iteration is done with $x_1$ instead of $x_0$. 20399The $x_i$ sequence is defined by 20400\[ x_0=x_0, \quad x_{n+1}=x_n-\frac{f(x_n)}{f'(x_n)} \] 20401If the Newton method converges, it is a quadratic convergence for 20402roots of multiplicity 1.\\ 20403Input : 20404\begin{center}{\tt fsolve((cos(x))=x,x,0,newton\_solver)}\end{center} 20405Output : 20406\begin{center}{\tt 0.739085133215}\end{center} 20407 20408\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt secant\_solver}}\index{secant\_solver{\sl secant\_solver}|textbf} 20409The secant method is a simplified version of the Newton method. 20410The computation of $x_1$ is done using the Newton method. 20411The computation of $f'(x_n), n>1$ is done approximately. 20412This method is used when the 20413computation of the derivative is expensive: 20414\[ x_{i+1} = x_i-\frac{ f(x_i)}{f'_{est}}, \quad 20415f'_{est} = \frac{f(x_i) - f(x_{i-1})}{(x_i - x_{i-1})} 20416\] 20417The convergence for roots of multiplicity 1 20418is of order $(1 + \sqrt5)/2 \approx 1.62... $.\\ 20419Input : 20420\begin{center}{\tt fsolve((cos(x))=x,x,-1..1,secant\_solver)}\end{center} 20421Output : 20422\begin{center}{\tt [0.739085078239,0.739085137844]}\end{center} 20423Input : 20424\begin{center}{\tt fsolve((cos(x))=x,x,0,secant\_solver)}\end{center} 20425Output : 20426\begin{center}{\tt 0.739085133215}\end{center} 20427 20428\subsection{{\tt fsolve} or {\tt nSolve} with the option {\tt steffenson\_solver}}\index{steffenson\_solver{\sl steffenson\_solver}|textbf} 20429The Steffenson method is generically the fastest method.\\ 20430It combines the Newton method with a "delta-two" Aitken acceleration : 20431with the Newton method, we obtain the sequence $x_i$ and the convergence 20432acceleration gives the Steffenson sequence 20433\[ R_i =x_i - \frac{(x_{i+1} - x_i)^2}{ (x_{i+2} - 2 x_{i+1} + x_{i})} \] 20434Input : 20435\begin{center}{\tt fsolve(cos(x)=x,x,0,steffenson\_solver)}\end{center} 20436Output : 20437\begin{center}{\tt 0.739085133215}\end{center} 20438 20439\section{Solve systems with {\tt fsolve}}\index{fsolve} 20440{\tt Xcas} provides six methods (inherited from the GSL) 20441to solve numeric systems of equations 20442of the form $f(x)=0$: 20443\begin{itemize} 20444\item Three methods use the jacobian matrix $f'(x)$ and their names are 20445terminated with {\tt j\_solver}. 20446\item 20447The three other methods use approximation for $f'(x)$ and use only 20448$f$. 20449\end{itemize} 20450All methods use an iteration of Newton kind 20451\[ x_{n+1}=x_n-{f'(x_n)}^{-1}*f(x_n) \] 20452The four methods {\tt hybrid*\_solver} use also a method of 20453gradient descent when the Newton iteration would make a too large step. 20454The length of the step is computed without scaling 20455for {\tt hybrid\_solver} and {\tt hybridj\_solver} 20456or with scaling (computed from $f'(x_n)$) for 20457{\tt hybrids\_solver} and {\tt hybridsj\_solver}. 20458 20459\subsection{{\tt fsolve} with the option {\tt dnewton\_solver}}\index{dnewton\_solver{\sl dnewton\_solver}|textbf} 20460\noindent Input : 20461\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[2,2],dnewton\_solver)}\end{center} 20462Output : 20463\begin{center}{\tt [1.0,1.0]}\end{center} 20464 20465\subsection{{\tt fsolve} with the option {\tt hybrid\_solver}}\index{hybrid\_solver{\sl hybrid\_solver}|textbf} 20466\noindent Input : 20467\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[2,2],}\end{center} 20468\begin{center}{\tt cos(x)=x,x,0,hybrid\_solver)}\end{center} 20469Output : 20470\begin{center}{\tt [1.0,1.0]}\end{center} 20471 20472\subsection{{\tt fsolve} with the option {\tt hybrids\_solver}}\index{hybrids\_solver{\sl hybrids\_solver}|textbf} 20473\noindent Input : 20474\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[2,2],hybrids\_solver)}\end{center} 20475Output : 20476\begin{center}{\tt [1.0,1.0]}\end{center} 20477 20478\subsection{{\tt fsolve} with the option {\tt newtonj\_solver}}\index{newtonj\_solver{\sl newtonj\_solver}|textbf} 20479\noindent Input : 20480\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[0,0],newtonj\_solver)}\end{center} 20481Output : 20482\begin{center}{\tt [1.0,1.0]}\end{center} 20483 20484\subsection{{\tt fsolve} with the option {\tt hybridj\_solver}}\index{hybridj\_solver{\sl hybridj\_solver}|textbf} 20485\noindent Input : 20486\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[2,2],hybridj\_solver)}\end{center} 20487Output : 20488\begin{center}{\tt [1.0,1.0]}\end{center} 20489 20490\subsection{{\tt fsolve} with the option {\tt hybridsj\_solver}}\index{hybridsj\_solver{\sl hybridsj\_solver}|textbf} 20491\noindent Input : 20492\begin{center}{\tt fsolve([x\verb|^|2+y-2,x+y\verb|^|2-2],[x,y],[2,2],hybridsj\_solver)}\end{center} 20493Output : 20494\begin{center}{\tt [1.0,1.0]}\end{center} 20495 20496\section{Numeric roots of a polynomial : {\tt proot}}\index{proot} 20497\noindent{\tt proot} takes as argument a squarefree polynomial, 20498either in symbolic form or as a list of 20499polynomial coefficients (written by decreasing order).\\ 20500{\tt proot} returns a list of the numeric roots of this polynomial.\\ 20501To find the numeric roots of $P(x)=x^3+1$, input : 20502\begin{center}{\tt proot([1,0,0,1]) }\end{center} 20503or : 20504\begin{center}{\tt proot(x\verb|^|3+1) }\end{center} 20505Output : 20506\begin{center}{\tt [0.5+0.866025403784*i,0.5-0.866025403784*i,-1.0]}\end{center} 20507To find the numeric roots of $x^2-3$, input : 20508\begin{center}{\tt proot([1,0,-3])}\end{center} 20509or : 20510\begin{center}{\tt proot(x\verb|^|2-3)}\end{center} 20511Output : 20512\begin{center}{\tt [1.73205080757,-1.73205080757]}\end{center} 20513%proot([1,0,-15,0,90,0,-270,0,405,0,-243]) 20514 20515 20516\section{Numeric factorization of a matrix : {\tt cholesky qr lu svd}} 20517Matrix numeric factorizations of 20518\begin{itemize} 20519\item Cholesky, 20520\item QR, 20521\item LU, 20522\item svd, 20523\end{itemize} 20524are described in section \ref{sec:factormatrice}. 20525 20526\chapter{Unit objects and physical constants}\label{sec:unit} 20527The {\tt Phys} menu contains: 20528\begin{itemize} 20529\item the physical constants ({\tt Constant} sub-menu), 20530\item the unit conversion functions 20531({\tt Unit\_convert} sub-menu), 20532\item the unit prefixes ({\tt Unit\_prefix} sub-menu) 20533\item the unit objects organized by subject 20534\end{itemize} 20535 20536\section{Unit objects} 20537\subsection{Notation of unit objects}\index{\_|textbf} 20538A unit object has two parts : a real number and a unit expression (a single 20539unit or a multiplicative combination of units). The two parts are linked by the 20540character {\tt \_} ("underscore"). For example {\tt 2\_m} for 2 meters. 20541For composite units, parenthesis must be used, e.g. {\tt 1\_(m*s)}.\\ 20542If a prefix is put before the unit then the unit is multiplied by a power of 2054310. For example {\tt k} or {\tt K} for kilo (indicate a multiplication by 20544$10^3$), {\tt D} for deca (indicate a multiplication by $10$), {\tt d} for 20545deci (indicate a multiplication by $10^{-1}$) etc...\\ 20546Input : 20547\begin{center}{\tt 10.5\_m}\end{center} 20548Output : 20549\begin{center}{\tt a unit object of value 10.5 meters}\end{center} 20550Input : 20551\begin{center}{\tt 10.5\_km}\end{center} 20552Output : 20553\begin{center}{\tt a unit object of value 10.5 kilometers}\end{center} 20554 20555\subsection{Computing with units} 20556{\tt Xcas} performs usual arithmetic operations (+, -, *, /, \verb|^|) on 20557unit objects. Different units may be used, but they must be 20558compatible for + and -. The result is an unit object 20559\begin{itemize} 20560\item for the 20561multiplication and the division of two unit objects 20562{\tt \_u1} and {\tt \_u2} the unit of the result is written 20563{\tt \_(u1*u2)} or {\tt \_(u1/u2)}. 20564\item for an addition or a subtraction of compatible unit objects, 20565the result is expressed with the same unit as the first term of the operation. 20566\end{itemize} 20567Input : 20568\begin{center}{\tt 1\_m+100\_cm}\end{center} 20569Output : 20570\begin{center}{\tt 2\_m}\end{center} 20571Input : 20572\begin{center}{\tt 100\_cm+1\_m}\end{center} 20573Output : 20574\begin{center}{\tt 200\_cm}\end{center} 20575Input : 20576\begin{center}{\tt 1\_m*100\_cm}\end{center} 20577Output : 20578\begin{center}{\tt 1\_m\verb|^|2}\end{center} 20579 20580\subsection{Convert units into MKSA units : {\tt mksa}}\index{mksa} 20581\noindent{\tt mksa} converts a unit object into a unit object 20582written with the compatible {\tt MKSA} base unit.\\ 20583\noindent Input : 20584\begin{center}{\tt mksa(15\_C)}\end{center} 20585Output : 20586\begin{center}{\tt 15\_(s*A)}\end{center} 20587 20588\subsection{Convert units : {\tt convert}}\index{convert}\label{sec:convertunit} 20589\noindent {\tt convert} convert units : the first argument is an unit object 20590and the second argument is the new unit (which must be compatible).\\ 20591Input : 20592\begin{center}{\tt convert(1\_h,\_s) }\end{center} 20593Output : 20594\begin{center}{\tt 3600\_s}\end{center} 20595Input : 20596\begin{center}{\tt convert(3600\_s,\_h) }\end{center} 20597Output : 20598\begin{center}{\tt 1\_h}\end{center} 20599 20600\subsection{Factorize a unit : {\tt ufactor}}\index{ufactor|textbf} 20601\noindent {\tt ufactor} factorizes a unit in a unit object : the first 20602argument is a unit object and the second argument is the unit to factorize.\\ 20603The result is an unit object multiplied by the remaining {\tt MKSA} units.\\ 20604Input : 20605\begin{center}{\tt ufactor(3\_J,\_W) }\end{center} 20606Output : 20607\begin{center}{\tt 3\_(W*s)}\end{center} 20608Input : 20609\begin{center}{\tt ufactor(3\_W,\_J) }\end{center} 20610Output : 20611\begin{center}{\tt 3\_(J/s)}\end{center} 20612 20613\subsection{Simplify a unit : {\tt usimplify}}\index{usimplify} 20614\noindent {\tt usimplify} simplifies a unit in an unit object.\\ 20615Input : 20616\begin{center}{\tt usimplify(3\_(W*s))}\end{center} 20617Output : 20618\begin{center}{\tt 3\_J}\end{center} 20619 20620\subsection{Unit prefixes} 20621You can insert a unit prefix in front of a unit to indicate a power of ten.\\ 20622The following table gives the available prefixes: 20623\begin{center} 20624\begin{tabular}{|l|c|r||l|c|r|} 20625\hline 20626Prefix & Name & (*10\verb|^|) n & Prefix & Name & (*10\verb|^|) n \\ 20627\hline 20628Y & yota & 24 & d & deci & -1\\ 20629Z & zeta & 21 & c & cent & -2\\ 20630E & exa & 18 & m & mili & -3\\ 20631P & peta & 15 & mu & micro &-6\\ 20632T & tera & 12 & n & nano & -9\\ 20633G & giga & 9 & p & pico & -12\\ 20634M & mega & 6 & f & femto & -15\\ 20635k or K & kilo & 3 & a & atto & -18\\ 20636h or H & hecto & 2 & z & zepto & -21\\ 20637D & deca & 1 & y & yocto &-24\\ 20638\hline 20639\end{tabular} 20640\end{center} 20641{\bf Remark}\\ 20642You cannot use a prefix with a built-in unit if the result gives another 20643built-in unit.\\ 20644For example, 20645{\tt 1\_a} is one are, but {\tt 1\_Pa} is one pascal and not 20646{\tt 10\verb|^|15\_a}. 20647 20648\section{Constants} 20649\subsection{Notation of physical constants}\index{\_} 20650If you want to use a physical constants inside Xcas, put 20651its name between two characters {\tt \_} 20652("underscore"). Don't confuse physical constants with symbolic constants, 20653for example, $e,\pi$ are symbolic constants as {\tt \_c\_,\_NA\_} are physical 20654constants.\\ 20655Input : 20656\begin{center}{\tt \_c\_ }\end{center} 20657Output speed of light in vacuum : 20658\begin{center}{\tt 299792458\_m*s\verb|^|-1}\end{center} 20659Input : 20660\begin{center}{\tt \_NA\_ }\end{center} 20661Output Avogadro's number : 20662\begin{center}{\tt 6.0221367e23\_gmol\verb|^|-1}\end{center} 20663 20664\subsection{Constants Library} 20665The physical constants are in the {\tt Phys} menu, {\tt Constant} 20666sub-menu. 20667% or also in the {\tt Help} menu.\\ 20668The following table gives the Constants Library : 20669\begin{center} 20670\begin{tabular}{|l|l|} 20671\hline 20672Name & Description\\ 20673\hline 20674{\tt \_NA\_} &Avogadro's number\\ 20675{\tt \_k\_} &Boltzmann constant\\ 20676{\tt \_Vm\_} &Molar volume\\ 20677{\tt \_R\_} &Universal gas constant\\ 20678{\tt \_StdT\_} &Standard temperature \\ 20679{\tt \_StdP\_} &Standard pressure\\ 20680{\tt \_sigma\_} &Stefan-Boltzmann constant\\ 20681{\tt \_c\_} &Speed of light in vacuum\\ 20682{\tt \_epsilon0\_} &Permitivity of vacuum\\ 20683{\tt \_mu0\_} &Permeability of vacuum\\ 20684{\tt \_g\_} &Acceleration of gravity\\ 20685{\tt \_G\_} &Gravitational constant\\ 20686{\tt \_h\_} &Planck's constant\\ 20687{\tt \_hbar\_} &Dirac's constant\\ 20688{\tt \_q\_} &Electron charge\\ 20689{\tt \_me\_} &Electron rest mass\\ 20690{\tt \_qme\_} &q/me (Electron charge/mass)\\ 20691{\tt \_mp\_} &Proton rest mass\\ 20692{\tt \_mpme\_} &mp/me (proton mass/electron mass)\\ 20693{\tt \_alpha\_} &Fine structure constant\\ 20694{\tt \_phi\_} &Magnetic flux quantum\\ 20695{\tt \_F\_} & Faraday constant\\ 20696{\tt \_Rinfinity\_} &Rydberg constant\\ 20697{\tt \_a0\_} &Bohr radius\\ 20698{\tt \_muB\_} &Bohr magneton\\ 20699{\tt \_muN\_} &Nuclear magneton\\ 20700{\tt \_lambda0\_} &Photon wavelength (ch/e)\\ 20701{\tt \_f0\_} &Photon frequency (e/h)\\ 20702{\tt \_lambdac\_} &Compton wavelength\\ 20703{\tt \_rad\_} &1 radian\\ 20704{\tt \_twopi\_} &2*pi radians\\ 20705{\tt \_angl\_} &180 degrees angle\\ 20706{\tt \_c3\_} &Wien displacement constant\\ 20707{\tt \_kq\_} & k/q (Boltzmann/electron charge)\\ 20708{\tt \_epsilon0q\_} &epsilon0/q (permitivity /electron charge)\\ 20709{\tt \_qepsilon0\_} &q*epsilon0 (electron charge *permitivity)\\ 20710{\tt \_epsilonsi\_} &Silicium dielectric constant\\ 20711{\tt \_epsilonox\_} &Bioxyd of silicium dielectric constant\\ 20712{\tt \_I0\_} &Reference intensity\\ 20713\hline 20714\end{tabular} 20715\end{center} 20716To have the value of a constant, input the constant name in the command line 20717of {\tt Xcas} and evaluate with {\tt enter} (don't forget to put 20718{\tt \_} at the beginning and at the end of the constant name). 20719 20720\chapter{Programming} 20721 20722\section{Functions, programs and scripts} 20723 20724\subsection{The program editor} 20725 20726\texttt{Xcas} provides a program editor, which you can open with 20727\texttt{Alt+P}. This can be useful for writing small programs, but 20728for writing larger programs you may want to use your usual editor. 20729(Note that this requires an editor, such as \texttt{emacs}, and not a 20730word processor.) If you use your own editor, then you will need to 20731save the program to a file, such as \texttt{myprog.cxx}, and then load 20732it into \texttt{Xcas} with the command line command \texttt{load}, as 20733in \texttt{load("myprog.cxx")}. 20734 20735\subsection{Functions\index{functions}} 20736 20737You have already seen functions defined with \texttt{:=}. For 20738example, to define a function \texttt{sumprod} which takes two inputs 20739and returns a list with the sum and the product of the inputs, you can 20740enter 20741\begin{center} 20742{\tt sumprod(a,b) := [a+b,a*b]} 20743\end{center} 20744Afterwards, entering 20745\begin{center} 20746{\tt sumprod(3,5)} 20747\end{center} 20748will return 20749\begin{center} 20750{\tt [8,15]} 20751\end{center} 20752 20753You can define functions that are computed with a sequence of 20754instructions by putting the instructions between braces, where each 20755command ends with a semicolon. If any local variables will be used, 20756they can be declared with the \texttt{local\index{local}} keyword, 20757followed by the variable names. The value returned by the function 20758will be indicated with the \texttt{return\index{return}} keyword. For 20759example, the above function \texttt{sumprod} could also be defined by 20760\begin{verbatim} 20761sumprod(a,b) := { 20762local s, p; 20763s := a + b; 20764p := a*b; 20765return [s,p]; 20766} 20767\end{verbatim} 20768 20769Another way to use a sequence of instructions to define a function is 20770with the \texttt{function\index{function}} \ldots 20771\texttt{endfunction\index{endfunction}} construction. With this 20772approach, the function name and parameters follow the 20773\texttt{function} keyword. This is otherwise like the previous 20774approach. The \texttt{sumprod} function could be defined by 20775\begin{verbatim} 20776function sumprod(a,b) 20777local s, p; 20778s := a + b; 20779p := a*b; 20780return [s,p]; 20781endfunction 20782\end{verbatim} 20783 20784\subsection{Local variables} 20785 20786Local variables in a function definition can be given initial values 20787in the line they are declared in if you put their initialization in 20788parentheses; for example, 20789\begin{verbatim} 20790local a,b; 20791a := 1; 20792\end{verbatim} 20793is the same as 20794\begin{verbatim} 20795local (a := 1), b; 20796\end{verbatim} 20797 20798Local variables should be given values within the function definition. 20799If you want to use a local variable as a symbolic variable, then you 20800can indicate that with the \texttt{assume\index{assume}} command. For 20801example, if you define a function \texttt{myroots} by 20802\begin{verbatim} 20803myroots (a) := { 20804local x; 20805return solve(x^2=a,x); 20806} 20807\end{verbatim} 20808then calling 20809\begin{center} 20810{\tt myroots(4)} 20811\end{center} 20812will simply return the empty list. You could leave \texttt{x} 20813undeclared, but that would make \texttt{x} a global variable and could 20814interact with other functions in unexpected ways. You can get the 20815behavior you probably expected by explicitly assuming \texttt{x} to be a 20816symbol\index{symbol@\textit{symbol}}; 20817\begin{verbatim} 20818myroots (a) := { 20819local x; 20820assume(x,symbol); 20821return solve(x^2=a,x); 20822} 20823\end{verbatim} 20824(Alternatively, you could use \texttt{purge(x)\index{purge}} instead 20825of \texttt{assume(x,symbol)}.) Now if you enter 20826\begin{center} 20827{\tt myroots(4)} 20828\end{center} 20829you will get 20830\begin{center} 20831 \[ [-2,2]\] 20832\end{center} 20833 20834\subsection{Default values of the parameters} 20835 20836You can give the parameters of a function default values by putting 20837\textit{parameter}\texttt{=}\textit{value} in the parameter list of 20838the function. For example, if you define a function 20839\begin{verbatim} 20840f(x,y=5,z) := { 20841return x*y*z; 20842} 20843\end{verbatim} 20844then 20845\begin{center} 20846{\tt f(1,2,3)} 20847\end{center} 20848will return the product $1*2*3=6$. If you give \texttt{f} only two 20849values as input, 20850\begin{center} 20851{\tt f(3,4)} 20852\end{center} 20853then these values will be given to the parameters which don't have default 20854values; in this case, \texttt{y} will get its default value \texttt{5} while 20855\texttt{3} and \texttt{4} will be assigned to \texttt{x} and 20856\texttt{z}, respectively. The result will be $x*y*z=3*5*4=60$. 20857 20858\subsection{Programs} 20859 20860A program\index{program} is similar to a function, and written like a 20861function without a return value. Programs are used to display results 20862or to create drawings. It is a good idea to turn a program into a 20863function by putting \texttt{return 0} at the end; this way you will 20864get a response of \texttt{0} when the program executes. 20865 20866\subsection{Scripts} 20867 20868A script is a file containing a sequence of instructions, each ending 20869with a semicolon. 20870 20871\subsection{Code blocks} 20872 20873A code block, such as used in defining functions, is a sequence of 20874statements delimited by braces or by \texttt{begin\index{begin}} and 20875\texttt{end\index{end}}. Each statement must end with a semicolon. 20876(If the block makes up a function, you can step through it one 20877statement at a time by using the debugger\index{debugger}; see section 20878\ref{sec:debug}.) 20879 20880\section{Basic instructions} 20881 20882\subsection{Comments\index{comments}} 20883 20884The characters \texttt{//} indicate that you are writing a comment; 20885any text between \texttt{//} and the end of the line will be ignored 20886by \texttt{Xcas}. 20887 20888\subsection{Input} 20889 20890You can prompt the user to enter a value for a variable with the 20891\texttt{input\index{input}} (or \texttt{Input} command). If you enter 20892\begin{center} 20893{\tt input(a)} 20894\end{center} 20895the the user will be given a box where they can enter a value for the 20896variable \texttt{a}. There will be a prompt 20897indicating the name of the variable; if you want a more descriptive 20898prompt, you can give \texttt{input} a string argument before the 20899variable name. 20900\begin{center} 20901{\tt input("Set a to the value: ",a)} 20902\end{center} 20903will prompt the user with "Set a to the value: " before the input box. 20904 20905If the value that you enter for \texttt{input} is a string, it should 20906be between quotes. If you want the user to enter a string without 20907having to use the quotes, you can use 20908\texttt{InputStr\index{InputStr}} or 20909\texttt{textinput\index{textinput}}, which will assume the input will 20910be a string and so the user won't need to use quotes. 20911 20912The \texttt{output\index{output}} (or \texttt{Output\index{Output}}) 20913command can take strings (or variables representing strings) as 20914arguments and can be used to add information to the input window. For 20915example, if you enter 20916\begin{center} 20917{\tt input(output("Calculate p(a)"),"polynomial",p,"value",a)} 20918\end{center} 20919then you will get a window with a box containing 20920\begin{center} 20921{\tt Calculate p(a)} 20922\end{center} 20923followed by the prompts for \texttt{p} and \texttt{a}. 20924 20925\subsection{Reading a single keystroke} 20926 20927If you want the user to enter a single key, you can use the 20928\texttt{getKey\index{getKey}} command, which doesn't take any 20929arguments, to get the ASCII code of the next keystroke. For example, 20930if you enter 20931\begin{center} 20932{\tt asciicode := getKey()} 20933\end{center} 20934and then hit the \texttt{A} key, then the variable \texttt{asciicode} 20935will have the value $65$, which is the ASCII code of capital A. 20936 20937\subsection{Checking conditions with \texttt{assert\index{assert}}} 20938 20939You can break out of a function with an error by using the 20940\texttt{assert} command, which takes a boolean as an argument. If the 20941boolean is false, then the function will return with an error. For 20942example, if you define a function 20943\begin{verbatim} 20944 sqofpos(x) := {assert(x > 0); return x^2;} 20945\end{verbatim} 20946then if you enter 20947\begin{center} 20948{\tt sqofpos(4)} 20949\end{center} 20950you will get $16$, but 20951\begin{center} 20952{\tt sqofpos(-4)} 20953\end{center} 20954will return an error, since \texttt{-4 > 0} is false. 20955 20956\subsection{Checking the type of the argument} 20957 20958You can check the type of the argument of a function (or anything 20959else, for that matter) with the \texttt{type\index{type}} command. 20960For example, entering 20961\begin{center} 20962{\tt type(4)} 20963\end{center} 20964will return 20965\begin{center} 20966{\tt integer} 20967\end{center} 20968 20969The output of a \texttt{type} command is actually an integer from 1 to 2097012. The output of \texttt{type(4)} is \texttt{integer}, which is a 20971constant with the value 1. Another way to represent this type is with 20972\texttt{DOM\_INT}; entering 20973\begin{center} 20974{\tt type(4) == DOM\_INT} 20975\end{center} 20976will return 20977\begin{center} 20978{\tt true} 20979\end{center} 20980Possible types (followed by the integer they represent) include: 20981\begin{itemize} 20982 \item 20983 \texttt{real}, \texttt{double} or \texttt{DOM\_FLOAT} (1). 20984 \item 20985 \texttt{integer} or \texttt{DOM\_INT} (2). 20986 \item 20987 \texttt{complex} or \texttt{DOM\_COMPLEX} (4). 20988 \item 20989 \texttt{identifier} or \texttt{DOM\_IDENT} (6). 20990 \item 20991 \texttt{vector} or \texttt{DOM\_LIST} (7). 20992 \item 20993 \texttt{func} or \texttt{DOM\_FUNC} (13). 20994 \item 20995 \texttt{expression} or \texttt{DOM\_SYMBOLIC} (8). 20996 \item 20997 \texttt{rational} or \texttt{DOM\_RAT} (10). 20998 \item 20999 \texttt{string} or \texttt{DOM\_STRING} (12). 21000\end{itemize} 21001 21002If the item being tested is a list (in \texttt{DOM\_LIST}), then the 21003\texttt{subtype\index{subtype}} command can determine what type of 21004list it is. If the object is a sequence, then \texttt{subtype} returns $1$; 21005\begin{center} 21006{\tt subtype(1,2,3)} 21007\end{center} 21008returns 21009\begin{center} 21010{\tt 1} 21011\end{center} 21012If the object is a set, then \texttt{subtype} returns 2. If the 21013object is a polynomial represented as a list (see section 21014\ref{sec:polynomials}), then \texttt{subtype} will return 10. If the 21015object isn't one of these types of list, then \texttt{subtype} returns 0. 21016 21017The \texttt{compare\index{compare}} function will compare two objects 21018taking their type into account; in other words, \texttt{compare(a,b)} 21019returns \texttt{1} (true) if \texttt{a} and \texttt{b} have the same 21020type with \texttt{a} less than \texttt{b}, or if \texttt{a} and 21021\texttt{b} have different types and the integer \texttt{type(a)} is 21022less than \texttt{type(b)}. For example, 21023\begin{center} 21024{\tt compare("a","b")} 21025\end{center} 21026returns 21027\begin{center} 21028{\tt 1} 21029\end{center} 21030since \texttt{"a"} and \texttt{"b"} have the same type 21031(\texttt{string}) and \texttt{"a"} is less than \texttt{"b"} in the 21032string ordering. Also, if \texttt{b} is a formal variable, then 21033\begin{center} 21034{\tt compare("a",b)} 21035\end{center} 21036returns 21037\begin{center} 21038{\tt 0} 21039\end{center} 21040since the type of \texttt{"a"} is \texttt{string} (the integer 12) 21041while the type of \texttt{b} is \texttt{identifier} (the integer 6) 21042and \texttt{12} is not less than \texttt{6}. 21043 21044The \texttt{getType\index{getType}} command is similar to \texttt{type} in that it 21045takes an object and returns the type, but it has different possible 21046return values. It is included for compatibility reasons. 21047For example, 21048\begin{center} 21049{\tt getType(3.14)} 21050\end{center} 21051returns 21052\begin{center} 21053{\tt NUM} 21054\end{center} 21055and 21056\begin{center} 21057{\tt getType(x)} 21058\end{center} 21059returns 21060\begin{center} 21061{\tt VAR} 21062\end{center} 21063Other possible return values include \texttt{STR}, \texttt{EXPR}, 21064\texttt{NONE}, \texttt{PIC}, \texttt{MAT} and \texttt{FUNC}. 21065 21066\subsection{Printing} 21067 21068The \texttt{print\index{print}} (or \texttt{Disp\index{Disp}}) command 21069will print its arguments in a special pane and return the number 21070\texttt{1}. For example, 21071\begin{center} 21072{\tt print("Hello")} 21073\end{center} 21074will result in 21075\begin{center} 21076{\tt Hello} 21077\end{center} 21078If you enter 21079\begin{center} 21080{\tt a := 12} 21081\end{center} 21082then 21083\begin{center} 21084{\tt print("a =",a)} 21085\end{center} 21086will print 21087\begin{center} 21088{\tt "a =", 12} 21089\end{center} 21090 21091The \texttt{ClrIO\index{ClrIO}} (no argument) will erase the printing 21092that was done in the level it was typed. For example, 21093\begin{center} 21094{\tt print("Hello"); ClrIO()} 21095\end{center} 21096will simply return the result \texttt{(1,1)}. 21097 21098\subsection{Displaying exponents} 21099 21100The \texttt{printpow} command determines how the \texttt{print} 21101command will print exponents. By default, 21102\begin{center} 21103{\tt print(x\^{}3)} 21104\end{center} 21105will print 21106\begin{center} 21107{\tt x\^{}3} 21108\end{center} 21109If you use the command 21110\begin{center} 21111{\tt printpow(1)} 21112\end{center} 21113then 21114\begin{center} 21115{\tt print(x\^{}3)} 21116\end{center} 21117will print as 21118\begin{center} 21119{\tt pow(x,3)} 21120\end{center} 21121If you use the command 21122\begin{center} 21123{\tt printpow(-1)} 21124\end{center} 21125then 21126\begin{center} 21127{\tt print(x\^{}3)} 21128\end{center} 21129will print as 21130\begin{center} 21131{\tt x**3} 21132\end{center} 21133Finally, 21134\begin{center} 21135{\tt printpow(0)} 21136\end{center} 21137will restore the default form. 21138 21139\section{Control structures} 21140 21141\subsection{\texttt{if\index{if}} statements} 21142 21143The \texttt{Xcas} language has different ways of writing 21144\texttt{if\ldots then} statements (see section \ref{subsec:piecewise}). The 21145standard version of the \texttt{if\ldots then} statement consists of 21146the \texttt{if} keyword, followed by a boolean expression (see section 21147\ref{sec:boolean} in parentheses, followed by a statement block which 21148will be executed if the boolean is true. 21149 21150As an example, if the variables \texttt{a} and \texttt{b} have the 21151values \texttt{3} and \texttt{2}, respectively, and you enter 21152\begin{center} 21153 {\tt if (a > b) \{ a := a + 5; b := a - b;\}} 21154\end{center} 21155then since \texttt{a > b} will evaluate to true, the variable 21156\texttt{a} will be reset to \texttt{8} and \texttt{b} will be reset 21157to the value \texttt{6}. 21158 21159An \texttt{if} statement can include a block of statements to execute 21160when the boolean is false by putting it at the end following the 21161\texttt{else\index{else}} keyword. For example, if the variable 21162\texttt{val} has a real value, then the statement 21163\begin{center} 21164 {\tt if (val > 0) \{abs := val;\} else \{abs := -1*val; \} } 21165\end{center} 21166will set \texttt{abs} to the same value as \texttt{val} if 21167\texttt{val} is positive and it will set \texttt{abs} to negative the 21168value of \texttt{val} otherwise. 21169 21170An alternate way to write an \texttt{if} statement is to 21171enclose the code block in \texttt{then\index{then}} and 21172\texttt{end\index{end}} instead of braces; if the variable \texttt{a} 21173is equal to \texttt{3}, then 21174\begin{center} 21175 {\tt if (a > 1) then a := a + 5; end} 21176\end{center} 21177will reset \texttt{a} to \texttt{8}. 21178An \texttt{else} block can be included by putting the else statements 21179after \texttt{else} and before the \texttt{end}. For example, with 21180\texttt{a} having the value \texttt{8} as above, 21181\begin{center} 21182 {\tt if (a > 10) then a := a + 10; else a := a - 5; end} 21183\end{center} 21184will reset \texttt{a} to the value \texttt{3}. 21185This 21186can also be written: 21187\begin{center} 21188 {\tt si (a > 10) alors a := a + 10; sinon a := a - 5; fsi} 21189\end{center} 21190 21191Several \texttt{if} statements can be nested; for example, the statement 21192\begin{center} 21193 {\tt if (a > 1) then a := 1; else if (a < 0) then a := 0; else a := 21194 0.5; end; end} 21195\end{center} 21196A simpler way is to replace the \texttt{else if} by \texttt{elif\index{elif}}; the 21197above statement can be written 21198\begin{center} 21199 {\tt if (a > 1) then a := 1; elif (a < 0) then a := 0; else a := 21200 0.5; end} 21201\end{center} 21202In general, such a combination can be written 21203\begin{verbatim} 21204 if (boolean 1) then 21205 block 1; 21206 elif (boolean 2) then 21207 block 2; 21208 ... 21209 elif (boolean n) then 21210 block n; 21211 else 21212 last block; 21213 end 21214\end{verbatim} 21215(where the last \texttt{else} is optional.) 21216For example, if you want to define a function $f$ by 21217\[ 21218f(x) = 21219 \begin{cases} 21220 8 & \text{if } x > 8\\ 21221 4 & \text{if } 4 < x \le 8\\ 21222 2 & \text{if } 2 < x \le 4\\ 21223 1 & \text{if } 0 < x \le 2\\ 21224 0 & \text{if } x \le 0 21225 \end{cases} 21226\] 21227you can enter 21228\begin{verbatim} 21229 f(x) := { 21230 if (x > 8) then 21231 return 8; 21232 elif (x > 4) then 21233 return 4; 21234 elif (x > 2) then 21235 return 2; 21236 elif (x > 0) then 21237 return 1; 21238 else 21239 return 0; 21240 end; 21241 } 21242\end{verbatim} 21243 21244\subsection{The \texttt{switch\index{switch}} statement} 21245 21246The \texttt{switch} statement can be used when you want the value of 21247a block to depend on an integer. It takes one argument, an 21248expression which evaluates to an integer. It should be followed by 21249a sequence of \texttt{case\index{case}} statements, which takes the form 21250\texttt{case} followed by an integer and then a colon, which is 21251followed by a code block to be executed if the expression equals the 21252integer. At the end is an optional \texttt{default\index{default}:} 21253statement, which is followed by a code block to be executed if the 21254expression doesn't equal any of the given integers. For example, if 21255you wanted to define a function of three variables which performed an 21256operation on the first two variables depending on the third, you could 21257enter 21258\begin{verbatim} 21259 oper(a,b,c) := { 21260 switch (c) { 21261 case 1: {a := a + b; break;} 21262 case 2: {a := a - b; break;} 21263 case 3: {a := a * b; break;} 21264 default: {a := a ^ b;} 21265 } 21266 return a; 21267 } 21268\end{verbatim} 21269Then 21270\begin{center} 21271 oper(2,3,1) 21272\end{center} 21273will return $2+3=5$, since the third argument is \texttt{1}, and 21274\begin{center} 21275 oper(2,3,2) 21276\end{center} 21277will return $2-3=-1$, since the third argument is \texttt{2}. 21278 21279\subsection{The \texttt{for\index{for}} loop} 21280 21281The \texttt{for} loop has three different forms, each of which uses an 21282index variable. If the \texttt{for} loop is used in a program, the 21283index variable should be declared as a local variable. (Recall that 21284\texttt{i} represents the imaginary unit, and so cannot be used as the 21285index.) 21286 21287\paragraph{The first form} 21288For the first form, the \texttt{for} is followed by the starting value 21289for the index, the end condition, and the increment step, separated by 21290semicolons and in parentheses. Afterwards is a block of code to be 21291executed for each iteration. For example, to add the even numbers 21292less than 100, you can set the running total to 0, 21293\begin{center} 21294 {\tt S := 0} 21295\end{center} 21296and the use an \texttt{for} loop to do the summing, 21297\begin{center} 21298 {\tt for (j := 0; j < 100; j := j + 2) \{S := S + j\}} 21299\end{center} 21300 21301\paragraph{The second form} 21302The second form of a \texttt{for} loop has a fixed increment for the 21303index. It is 21304written out with \texttt{for} followed by the index, followed by 21305\texttt{from}, the initial value, \texttt{to}, the ending value, 21306\texttt{step}, the size of the increment, and finally the statements 21307to be executed between \texttt{do} and \texttt{end\_for}. For 21308example, having set the variable \texttt{S} equal to 0, you can again 21309add the even numbers less than 100 with 21310\begin{center} 21311 {\tt for j from 2 to 98 step 2 do S := S + j; end\_for} 21312\end{center} 21313There is also a French version of this syntax; 21314\begin{center} 21315 {\tt pour j de 2 jusque 98 pas 2 faire S := S + j; fpour} 21316\end{center} 21317 21318\paragraph{The third form} 21319The third form of the \texttt{for} loop lets you iterate over the 21320values in a list (or a set or a range). In this form, the 21321\texttt{for} is followed by the index, then \texttt{in}, the list, and 21322then the instructions between \texttt{do} and \texttt{end\_for}. For 21323example, to add all integers from 1 to 100, you can again set the 21324running total \texttt{S} to 0, then 21325\begin{center} 21326 {\tt for j in 1..100 do S:= S + j; end\_for} 21327\end{center} 21328or 21329\begin{center} 21330 {\tt pour j in 1..100 faire S:= S + j; fpour} 21331\end{center} 21332 21333\subsection{The \texttt{repeat\index{repeat}} loop} 21334 21335The \texttt{repeat} loop allows you to repeat statements until a given 21336condition is met. To use it, enter \texttt{repeat}, the statements, 21337the keyword \texttt{until\index{until}} followed by the condition, a 21338boolean. For example, if you want the user to enter a value for a 21339variable \texttt{x} which is greater than 4, you could have 21340\begin{verbatim} 21341 repeat 21342 input("Enter a value for x (greater than 4)",x); 21343 until (x > 4); 21344\end{verbatim} 21345This can also be written\index{repeter}\index{jusqua} 21346\begin{verbatim} 21347 repeter 21348 input("Enter a value for x (greater than 4)",x); 21349 jusqua (x > 4); 21350\end{verbatim} 21351 21352\subsection{The \texttt{while\index{while}} loop} 21353 21354The \texttt{while} loop is used to repeat a code block as long as a 21355given condition holds. To use it, enter \texttt{while}, the 21356condition, and then a code block. For example, to add the terms of 21357the harmonic series $1 + 1/2 + 1/3 + 1/4 + \dots$ until a term is less 21358than 0.05, you could initialize the sum \texttt{S} to 0 and let 21359\texttt{j} be the first term 1. Then 21360\begin{center} 21361{\tt while (1/j >= 0.05) \{S := S + 1/j; j := j+1;\}} 21362\end{center} 21363will find the sum. This line is the same as 21364\begin{center} 21365{\tt tantque (1/j >= 0.05) faire S := S + 1/j; j := j+1; ftantque} 21366\end{center} 21367 21368Note that a \texttt{while} loop can also be written as a \texttt{for} 21369loop. For example, as long as \texttt{S} is set to 0 and \texttt{j} 21370is set to 1 , the above loop 21371can be written as 21372\begin{center} 21373{\tt for (;1/j >= 0.05;) \{S := S + 1/j; j := j+1;\}} 21374\end{center} 21375or, with only \texttt{S} set to 0, 21376\begin{center} 21377{\tt for (j := 1; 1/j >= 0.05; j++) \{S := S + 1/j;\}} 21378\end{center} 21379 21380\subsection{Breaking out a loop} 21381 21382If your program is running a loop and you want it to exit the loop 21383without finishing it, you can use the \texttt{break\index{break}} 21384command. For example, you can define a program 21385\begin{verbatim} 21386 testbreak(a,b) := { 21387 local r; 21388 while (true) { 21389 if (b == 0) {break;} 21390 r := irem(a,b); 21391 a := b; 21392 b := r; 21393 } 21394 return a; 21395 } 21396\end{verbatim} 21397If you then enter 21398\begin{center} 21399 {\tt testbreak(4,0)} 21400\end{center} 21401it will return 21402\begin{center} 21403 {\tt 4} 21404\end{center} 21405since the \texttt{while} loop is interrupted when \texttt{b} is 0 and 21406\texttt{a} is 4. 21407 21408\subsection{Going to the next iteration of a loop} 21409 21410The \texttt{continue} command will skip the rest of the current 21411iteration of a loop and go to the next iteration. For example, 21412if you enter 21413\begin{verbatim} 21414 S := 0 21415 for (j := 1, j <= 10; j++) { 21416 if (j == 5) {continue;} 21417 S := S + j; 21418 } 21419\end{verbatim} 21420then \texttt{S} will be 50, which is the sum of the integers from 1 to 2142110 except for 5, since the loop never gets to \texttt{S := S + j} when 21422\texttt{j} is equal to 5. 21423 21424\subsection{Changing the order of execution} 21425 21426The \texttt{goto} command will tell a program to jump to a different 21427spot in a program, where the spot needs to have been marked with 21428\texttt{label}. They both must have the same argument, which is 21429simply a sequence of characters. For example, the following program 21430will add the terms of the harmonic series until the term is less than 21431some specified value \texttt{eps} and print the result. 21432\begin{verbatim} 21433 harmsum(eps) := { 21434 local S, j; 21435 S := 0; 21436 j := 0; 21437 label(spot); 21438 j := j + 1; 21439 S := S + 1/j; 21440 if (1/j >= eps) goto (spot); 21441 print(S); 21442 return 0; 21443 } 21444\end{verbatim} 21445 21446\section{Other useful instructions} 21447 21448\subsection{Assignments in a program} 21449 21450Recall that the \texttt{=<} operator will change the value of a single 21451entry in a list or matrix by reference (see subsection \ref{subsec:refassign}). 21452This make it efficient when changing many values, one at a time, in a 21453list, as might be done by a program. 21454 21455Care must be taken, since your intent might be changed when a program 21456is compiled. For example, if a program contains 21457\begin{verbatim} 21458 local a; 21459 a := [0,1,2,3,4]; 21460 ... 21461 a[3] =< 33; 21462\end{verbatim} 21463then in the compiled program, \texttt{a := [0,1,2,3,4]} will be 21464replaced by \texttt{a := [0,1,2,33,4]}. To avoid this, you can assign 21465a copy of the list to \texttt{a}; you could write 21466\begin{verbatim} 21467 local a; 21468 a := copy([0,1,2,3,4]); 21469 ... 21470 a[3] =< 33; 21471\end{verbatim} 21472Alternately, you could use a command which recreates a list every time 21473the program is run, such as \texttt{makelist\index{makelist}} or 21474\texttt{\$}, instead of copying a list; \texttt{a := 21475makelist(n,n,0,4)} or \texttt{a := [n\$(n=0..4)]} can also be used in 21476place of \texttt{a := [0,1,2,3,4]}. 21477 21478\subsection{Writing variable values to a file} 21479 21480You can save variable values to a file, to be read later, with the 21481\texttt{write\index{write}} command. This command takes a string for a file name 21482and a list of variables to save. For example, if \texttt{a} has the 21483value \texttt{3.14} and \texttt{b} has the value \texttt{7}, then 21484\begin{center} 21485 {\tt write("foo",a,b)} 21486\end{center} 21487will create a file named ``foo'' with the contents 21488\begin{verbatim} 21489 a:=(3.14); 21490 b:=7; 21491\end{verbatim} 21492If you wanted to store the first million digits of $\pi$ to a file, 21493you could set it equal to a variable 21494\begin{center} 21495 {\tt pidec := evalf(pi,10\^{}6):;} 21496\end{center} 21497and then store it in a file 21498\begin{center} 21499 {\tt write("pi1million",pidec)} 21500\end{center} 21501 21502If you want to restore the values of variables saved this way, for 21503example in a different session or if you have purged the 21504variables, then you can use the \texttt{read} command, which simply 21505takes a file name as a string. If, in a different session, you want 21506to use the values of \texttt{a} and \texttt{b} above, the command 21507\begin{center} 21508 {\tt read("foo")} 21509\end{center} 21510will give them the values \texttt{3.14} and \texttt{7} again. Note 21511that this will silently overwrite any values that \texttt{a} and 21512\texttt{b} might have had. 21513 21514\subsection{Writing output to a file} 21515 21516You can use the \texttt{fopen\index{fopen}}, 21517\texttt{fprint\index{fprint}} and \texttt{fclose\index{fclose}} 21518commands to write output to a file instead of the screen. 21519 21520To begin, you need to open the file and associate it with a variable. 21521You use \texttt{fopen} for this, which takes a file name as argument. 21522For example, 21523\begin{center} 21524 {\tt f := fopen("bar")} 21525\end{center} 21526will create a file named ``bar'' (and so erase it if it already 21527exists). You can use the \texttt{fprint} command to write to the 21528file; it takes the variable representing the file as the first 21529argument, followed by what you want to write to the file. For 21530example, if \texttt{x} is equal to 9, then 21531\begin{center} 21532 {\tt fprint(f,"x + 1 is ", x+1)} 21533\end{center} 21534will put 21535\begin{verbatim} 21536 "x + 1 is "10 21537\end{verbatim} 21538in the file. Note that the quotation marks are inserted with the 21539string. If you want to insert strings without quotes, then you can 21540give \texttt{fprint} a second argument of 21541\texttt{Unquoted\index{Unquoted@\texttt{Unquoted}}}. If instead of 21542the above \texttt{printf} you entered 21543\begin{center} 21544 {\tt fprint(f,Unquoted,"x + 1 is ", x+1)} 21545\end{center} 21546then the file would contain 21547\begin{verbatim} 21548 x + 1 is 10 21549\end{verbatim} 21550Finally, after you have finished writing what you want into the file, 21551you close the file with the \texttt{fclose} command, 21552\begin{center} 21553 {\tt fclose(f)} 21554\end{center} 21555 21556\subsection{Using strings as names} 21557 21558Variable and function names are symbols, namely sequences of characters, which are 21559different from strings. For example, you can have a variable named \texttt{abc}, but 21560not \texttt{"abc"}. The \texttt{\#\index{\#}} operator will turn a 21561string into a symbol; for example \texttt{(\#"abc")} is the symbol 21562\texttt{abc}. 21563 21564If you enter 21565\begin{center} 21566 {\tt a := "abc"; (\#a) := 3} 21567\end{center} 21568or 21569\begin{center} 21570 {\tt (\#"abc") := 3} 21571\end{center} 21572then the variable \texttt{abc} will have the value \texttt{3}. 21573Entering \texttt{\#a} will still give you \texttt{abc}; you can get 21574\texttt{3} with \texttt{eval(\#a)}. 21575 21576Similarly for functions. If you enter 21577\begin{center} 21578 {\tt b := "sin"; (\#b)(pi/4)} 21579\end{center} 21580or 21581\begin{center} 21582 {\tt (\#"sin")(pi/4)} 21583\end{center} 21584you will get 21585\begin{center} 21586 {\tt 1/sqrt(2)} 21587\end{center} 21588which is {\tt sin(pi/4)}. 21589 21590\subsection{Using strings as commands} 21591 21592The \texttt{expr\index{expr}} command will let you use a string as a 21593command. Given a string that expresses a valid command, \texttt{expr} 21594will convert the string to the command and evaluate it. 21595For example, if you enter 21596\begin{center} 21597 {\tt expr("c := 1")} 21598\end{center} 21599then the variable \texttt{c} will be set to 1. 21600Similarly, if you enter 21601\begin{center} 21602 {\tt a := "ifactor(54)"} 21603\end{center} 21604then 21605\begin{center} 21606 {\tt expr(a)} 21607\end{center} 21608will return 21609\begin{center} 21610 {\tt 2 * 3\^{}3} 21611\end{center} 21612which is the same thing as entering \texttt{ifactor(54)} directly. 21613 21614You can also use \texttt{expr} to convert a string to a number. 21615If a string is simply a number enclosed by quotation marks, then 21616\texttt{expr} will return the number. For example, 21617\begin{center} 21618 {\tt expr("123")} 21619\end{center} 21620will return 21621\begin{center} 21622 {\tt 123} 21623\end{center} 21624In particular, the following strings will be converted to the 21625appropriate number. 21626\begin{itemize} 21627 \item 21628 A string consisting of the digits 0 through 9 which doesn't start 21629 with 0 will be converted to an integer. For example, 21630 \begin{center} 21631 {\tt expr("2133")} 21632 \end{center} 21633 will return 21634 \begin{center} 21635 {\tt 2133} 21636 \end{center} 21637 21638 \item 21639 A string consisting of the digits 0 through 9 which contains a 21640 single decimal point will be converted to a decimal. 21641 For example, 21642 \begin{center} 21643 {\tt expr("123.4")} 21644 \end{center} 21645 will return 21646 \begin{center} 21647 {\tt 123.4} 21648 \end{center} 21649 21650 \item 21651 A string consisting of the digits 0 through 9, possibly containing a 21652 single decimal point, followed by \texttt{e} and then more digits 0 21653 through 9, will be read as a decimal in exponential notation. 21654 For example, 21655 \begin{center} 21656 {\tt expr("1.23e4")} 21657 \end{center} 21658 will return 21659 \begin{center} 21660 {\tt 12300.0} 21661 \end{center} 21662 21663 \item 21664 A string consisting of the digits 0 through 7 which starts 21665 with 0 will be read as an integer base 8. For example, 21666 \begin{center} 21667 {\tt expr("0176")} 21668 \end{center} 21669 will return 21670 \begin{center} 21671 {\tt 126} 21672 \end{center} 21673 since 176 base 8 equals 126 base 10. 21674 21675 \item 21676 A string starting with \texttt{0x} followed by digits 0 through 9 21677 and letters \texttt{a} through \texttt{f} will be read as an integer 21678 base 16. For example, 21679 \begin{center} 21680 {\tt expr("0x2a3f")} 21681 \end{center} 21682 will return 21683 \begin{center} 21684 {\tt 10815} 21685 \end{center} 21686 since \texttt{2a3f} base 16 equals 10815 base 10. 21687 21688 \item 21689 A string starting with \texttt{0b} followed by digits 0 and 1 will 21690 be read as a binary integer. For example, 21691 \begin{center} 21692 {\tt expr("0b1101")} 21693 \end{center} 21694 will return 21695 \begin{center} 21696 {\tt 13} 21697 \end{center} 21698 since 1101 base 2 equals 13 base 10. 21699\end{itemize} 21700 21701\subsection{Converting an expression to a string} 21702 21703The \texttt{string\index{string}} can be used to convert an expression 21704to a string. If you give it an expression as an argument, the 21705expression will be evaluated and then converted to a string. For 21706example, if you enter 21707\begin{center} 21708 {\tt string(ifactor(6))} 21709\end{center} 21710the result will be the string 21711\begin{center} 21712 {\tt "2 * 3"} 21713\end{center} 21714This is the same thing as adding the empty string to the expression; 21715\begin{center} 21716 {\tt ifactor(6) + ""} 21717\end{center} 21718 21719If you want to convert an unevaluated expression to a string, you can 21720quote the expression. If you enter 21721\begin{center} 21722 {\tt string(quote(ifactor(6)))} 21723\end{center} 21724then you will get the string 21725\begin{center} 21726 {\tt "ifactor(6)"} 21727\end{center} 21728 21729\subsection{Working with the graphics screen} 21730 21731Recall that the \texttt{DispG\index{DispG}} screen contains the 21732graphical output of \texttt{Xcas}. You can use the \texttt{DispG} 21733command (without parentheses) to bring it up; entering 21734\begin{center} 21735 {\tt DispG;} 21736\end{center} 21737will open the graphics screen. 21738 21739To clear the graphics screen, you can use the 21740\texttt{ClrGraph\index{ClrGraph}} command. If you enter 21741\begin{center} 21742 {\tt ClrGraph} 21743\end{center} 21744or 21745\begin{center} 21746 {\tt ClrGraph()} 21747\end{center} 21748then the \texttt{DispG} screen will be erased. 21749 21750You can close the \texttt{DispG} screen with the \texttt{DispHome} 21751command; entering 21752\begin{center} 21753 {\tt DispHome;} 21754\end{center} 21755will make the graphics screen go away. 21756 21757\subsection{Pausing a program} 21758 21759The \texttt{Pause\index{Pause}} command (written without parentheses) 21760will bring up a Pause informational window and pause \texttt{Xcas} 21761until you click \texttt{Close} in the Pause window. If you enter 21762\texttt{Pause} followed by a number, then \texttt{Xcas} will pause for 21763that number of seconds; entering 21764\begin{center} 21765 {\tt Pause 10} 21766\end{center} 21767will pause \texttt{Xcas} for 10 seconds. 21768 21769The \texttt{WAIT\index{WAIT}} command will also pause \texttt{Xcas}; 21770it requires an argument in parentheses. To pause \texttt{Xcas} for 10 21771seconds, you can enter 21772\begin{center} 21773 {\tt WAIT(10)} 21774\end{center} 21775 21776\subsection{Dealing with errors} 21777 21778Some commands produce errors, and if your program tries to run such a 21779command it will halt with an error. To avoid this, you can use the 21780\texttt{try\index{try}} and \texttt{catch\index{catch}} commands. To 21781use these, you put any potentially problematic statements in a block 21782following \texttt{try}, and immediately after the block put 21783\texttt{catch} with an argument of an unused symbol. If the 21784\texttt{try} block doesn't produce an error, then \texttt{catch} and 21785the block following \texttt{catch} will be ignored. If the 21786\texttt{try} block does produce an error, then a string describing the 21787error is assigned to the argument to \texttt{catch}, and the block 21788following catch is evaluated. For example, the command 21789\begin{center} 21790 {\tt [[1,1]]*[[2,2]]} 21791\end{center} 21792will produce an error saying \textit{Error: Invalid dimension}. 21793However, 21794\begin{verbatim} 21795 try {[[1,1]]*[[2,2]]} 21796 catch (err) { 21797 print("The error is " + err) 21798 } 21799\end{verbatim} 21800will not produce an error; instead it will print 21801\begin{center} 21802 {\tt The error is Error: Invalid dimension} 21803\end{center} 21804 21805With the following program 21806\begin{verbatim} 21807 test(x) := { 21808 local y, str, err; 21809 try { y := [[1,1]]*x; str := "This produced a product.";} 21810 catch (err) 21811 {y := x; 21812 str := "This produced an error " + err + " The input is returned.";} 21813 print(str); 21814 return y; 21815 } 21816\end{verbatim} 21817if you enter 21818\begin{center} 21819 {\tt test([[2],[2]])} 21820\end{center} 21821then 21822\begin{center} 21823 {\tt This produced a product.} 21824\end{center} 21825will be printed and the result will be 21826\begin{center} 21827 {\tt [4]} 21828\end{center}. 21829If you enter 21830\begin{center} 21831 {\tt test([[2,2]])} 21832\end{center} 21833then 21834\begin{center} 21835 {\tt This produced an error Error: Invalid dimension The input is 21836 returned.} 21837\end{center} 21838will be printed and the result will be 21839\begin{center} 21840 {\tt [[2,2]]} 21841\end{center} 21842 21843You can use the \texttt{throw\index{throw}} command (or equivalently, 21844the \texttt{error\index{error}} or \texttt{ERROR\index{ERROR}} 21845command) to generate an error and error string, possibly to be caught 21846by \texttt{catch}. The \texttt{throw} command takes as argument a 21847string, which will be used as the error message. For example, suppose 21848you have the program 21849\begin{verbatim} 21850 f(x) := { 21851 if (type(x) != DOM_INT) 21852 throw("Not an integer"); 21853 else 21854 return x; 21855 } 21856\end{verbatim} 21857Then 21858\begin{center} 21859 {\tt f(12)} 21860\end{center} 21861will simply return 21862\begin{center} 21863 {\tt 12} 21864\end{center} 21865since 12 is an integer, but 21866\begin{center} 21867 {\tt f(1.2)} 21868\end{center} 21869will signal an error 21870\begin{center} 21871 {\tt Not an integer Error: Bad Argument Value} 21872\end{center} 21873since 1.2 in not an integer. You can catch this error in other 21874programs; the program 21875\begin{verbatim} 21876 g(x) := { 21877 try(f(x)) catch(err) {x := 0;} 21878 return x; 21879 } 21880\end{verbatim} 21881will return \texttt{x} is \texttt{x} is an integer, but if \texttt{x} 21882is not an integer, \texttt{f(x)} will give an error and so 21883\texttt{g(x)} will return 0. 21884 21885\section{Debugging} 21886\label{sec:debug} 21887 21888\subsection{Starting the debugger} 21889 21890To start the debugger, you give the \texttt{debug\index{debug}} command an argument 21891of a function and its argument. That will bring up a debug window 21892which contains a pane with the program with the current line highlighted, an 21893\texttt{eval} entry box, a pane with the program including the 21894breakpoints, a row of buttons, and a pane keeping track of 21895the values of variables. By default, the value of all variables in 21896the program are in this pane. The buttons are shortcuts 21897for entering commands in the \texttt{eval} box, but you can enter 21898other commands in the \texttt{eval} box to change the values of 21899variables or to run a command in the context of the program. 21900 21901\paragraph{The \texttt{sst\index{sst}} button} 21902This button will run the \texttt{sst} command, which takes no 21903arguments and runs the highlighted line in the program before moving 21904to the next line. 21905 21906\paragraph{The \texttt{in\index{in}} button} 21907This button will run the \texttt{sst\_in} command, which takes no 21908argument and runs one step in the program or a user defined 21909function used in the program. 21910 21911\paragraph{The \texttt{cont\index{cont}} button} 21912This button will run the \texttt{cont} command, which takes no 21913arguments and runs the commands from the highlighted line to a breakpoint. 21914 21915\paragraph{The \texttt{kill\index{kill}} button} 21916This button will run the \texttt{kill} command, which exits the debugger. 21917 21918\paragraph{The \texttt{break\index{break}} button} 21919This button will put the command \texttt{breakpoint\index{breakpoint}} 21920in the \texttt{eval} box, with default 21921arguments of the current program and the current line. It sets a 21922breakpoint at the given line of the given program. 21923Alternatively, if you click on a line in the program in the top pane, 21924you will get the \texttt{breakpoint} command with that program and the 21925line you clicked on. 21926 21927You can set a breakpoint when you write a program with the 21928\texttt{halt\index{halt}()} command. When a program has a 21929\texttt{halt} command, then running the program will bring up the 21930debugger. If you want to debug the program, though, it is still 21931better to use the debug command. Also, you should remove any 21932\texttt{halt} commands when you are done debugging. 21933 21934\paragraph{The \texttt{rmbrk\index{rmbrk}} button} 21935This button will put the command 21936\texttt{rmbreakpoint\index{rmbreakpoint}} in the \texttt{eval} box , with default 21937arguments of the current program and the current line. It removes a 21938breakpoint at the given line of the given program. Alternatively, you 21939can click on the line in the program in the top pane with the bookmark 21940you want to remove. 21941 21942\paragraph{The \texttt{watch\index{watch}} button} 21943This button will put the command \texttt{watch} in the 21944\texttt{eval} box, without the arguments 21945filled in. It takes a list of variables as arguments, and will keep 21946track of the values of these variables in the variable pane. 21947 21948\paragraph{The \texttt{rmwtch\index{rmwtch}} button} 21949This button will put the command \texttt{rmwatch\index{rmwatch}} in 21950the \texttt{eval} box without the arguments filled in. The arguments 21951are the variables you want to remove from the watch list. 21952 21953\end{document} 21954