1\section{dprepro and pyprepro}\label{interfaces:dprepro-and-pyprepro}
2
3Dakota is packaged with two template processing tools that are
4intended for use in the preprocessing phase of analisys drivers.
5
6The first tool, \texttt{pyprepro}, features simple parameter
7substitution, setting of immutable (fixed) variable names, and
8provides full access to all of the Python programming language
9within templates. As such, templates can contain loops,
10conditionals, arrays (lists), dictionaries, and other Python language
11features.
12
13The second tool, \texttt{dprepro}, uses the same template engine
14as \texttt{pyprepro}, and in addition undertands Dakota's parameter
15file formats. In particular, when using \texttt{dprepro}, Dakota variables
16become available for use within templates. \texttt{dprepro} is also integrated
17with the \texttt{dakota.interfacing} module to provide direct access
18to \texttt{Parameters} and \texttt{Results} objects within templates
19(see Section~\ref{interfacing:params-and-results}) and to provide template
20processing capability within Python scripts that import
21\texttt{dakota.interfacing}.
22
23The \texttt{dprepro} described in this section is a replacement for the existing
24\texttt{dprepro} script that shipped with Dakota releases prior to 6.8. The
25new \texttt{dprepro} maintains as much backward compatibility with the old
26\texttt{dprepro} as possible. An important difference between the old
27\texttt{dprepro} and the new \texttt{d/pyprepro} is that the former was written
28in Perl, and the latter are in Python. Although the old \texttt{dprepro} was deprecated
29as of the 6.8 release of Dakota, it is still available in Dakota's \texttt{bin/} folder
30under the name \texttt{dprepro.perl}.
31
32\subsection{Usage}\label{interfaces:dprepro-usage}
33
34Users who are familiar with the existing Perl version of dprepro should
35experience few or no differences with the new Python version. There are,
36however, a number of new options and features, especially in the kinds of
37expressions that are permitted in templates. These are described in
38the following section.
39
40Figure~\ref{advint:dprepro_usage} shows the result of running
41\texttt{dprepro --help}. The three required positional arguments are:
421. \textt{include}: The name of a Dakota parameters file,
432. \textt{infile}: The name of a template file (or a dash if the template
44is provided on \texttt{stdin}), and
453. \textff{outfile}: The name of the output file, which is the result of
46processing the template. This argument is optional, and output is written
47to \texttt{stdout} if it is missing.
48
49The remaining options are used to
50* Set custom delimiters for Python code lines (\texttt{--code}) and blocks
51(\texttt{--code-block}) inline statements that print (\texttt{--inline}).
52The last of these is equivalent to Perl \texttt{dprepro}'s
53\texttt{--left-delimiter} and \texttt{--right-delimter} switches, which also
54are permitted. They default to \texttt{"\{ \}"}.
55* Insert additional parameters for substitution, either from a JSON file
56(\texttt{--json-include}) or directly on the command line (\texttt{--var}).
57* Silence warnings (\texttt{--no-warn})
58* Set the default numerical output format (\texttt{--output-format}).
59
60\begin{figure}
61  \centering
62  \begin{bigbox}
63    \begin{small}
64      \verbatimtabinput[8]{dprepro_usage}
65    \end{small}
66  \end{bigbox}
67  \caption{\texttt{dprepro} usage}
68  \label{advint:dprepro_usage}
69\end{figure}
70
71The \texttt{pyprepro} script accepts largely the same command line options.
72The primary differences are that \texttt{pyprepro} does not require or accept
73Dakota format parameters files, and it has just two positional command line
74arguments, the \texttt{infile} and \texttt{outfile}, both defined as above.
75In addition, \texttt{pyprepro} accepts one or more \texttt{--include} files.
76These may be used to set parameters and execute arbitrary Python scripting
77before template processing occurs.
78
79\subsection{Template Expressions}\label{interfaces:template-expressions}
80
81This section describes the expressions that are permitted in templates. All
82examples, except where otherwise noted, use the default delimiters \texttt{\{\ \ \}}
83for inline printed expressions, \texttt{\%} for single-line
84Python statements, and \texttt{\{\%\ \%\}} for Python code blocks.
85
86Expressions can be of three different forms (with defaults)
87
88\begin{itemize}
89\tightlist
90\item
91  Inline single-line expressions (rendered) {[} \texttt{\{expression\}}
92  {]}
93\item
94  Python code single-line (silent) {[} \texttt{\%\ expression} {]}
95\item
96  Python code multi-line blocks (silent) {[}
97  \texttt{\{\%\ expression\ (that\ can\ be\ over\ many\ line)\ \%\}} {]}
98\end{itemize}
99
100Expressions can contain just about any valid Python code. The only
101important difference is that indentation is ignored and blocks must end
102with \texttt{end}. See the examples below.
103
104\subsubsection{Inline Expressions}\label{interfaces:inline-expressions}
105
106Inline expressions are delineated with \texttt{\{expression\}} and
107\textbf{always display}.
108
109Consider:
110
111\begin{verbatim}
112param1 = {param1 = 10}
113param2 = {param1 + 3}
114param3 = {param3 = param1**2}
115\end{verbatim}
116
117Returns:
118
119\begin{verbatim}
120param1 = 10
121param2 = 13
122param3 = 100
123\end{verbatim}
124
125In this example, the first and third line both display a value
126\emph{and} set the parameter.
127
128\subsubsection{Python Single Line Code}\label{interfaces:python-single-line-code}
129
130A \texttt{\%} at the start of a line is used to begin a single-line code expression.
131These are non-printing.
132Consider the following example.
133
134\begin{verbatim}
135% param1 = pi/4
136The new value is {sin(param1)}
137\end{verbatim}
138
139It returns:
140
141\begin{verbatim}
142The new value is 0.7071067812
143\end{verbatim}
144
145Furthermore, single lines can be used for Python logic and loops. This example
146demonstrates looping over an array, which is explained in further detail below.
147As stated previously, unlike ordinary Python, indentation is not required and is
148ignored. Blocks of Python code are concluded with \texttt{end}.
149
150\begin{verbatim}
151% angles = [0,pi/4,pi/2,3*pi/4,pi]
152% for angle in angles:
153cos({angle}) = { cos(angle)}
154% end
155\end{verbatim}
156
157Returns:
158
159\begin{verbatim}
160cos(0) = 1
161cos(0.7853981634) = 0.7071067812
162cos(1.570796327) = 6.123233996e-17
163cos(2.35619449) = -0.7071067812
164cos(3.141592654) = -1
165\end{verbatim}
166
167\subsubsection{Code Blocks}\label{interfaces:code-blocks}
168
169Finally, multi-line code blocks may be specified without prepending each
170Python statement with \texttt{\%}. Instead, the entire block is enclosed
171in \texttt{\{\% \%\}}. (Indentation is ignored within code blocks.)
172
173\begin{verbatim}
174{%
175# Can have comments too!
176txt = ''
177for ii in range(10):
178    txt += ' {}'.format(ii)
179end
180%}
181txt: {txt}
182\end{verbatim}
183
184returns:
185
186\begin{verbatim}
187txt:  0 1 2 3 4 5 6 7 8 9
188\end{verbatim}
189
190\subsubsection{Changing delimiters}\label{interfaces:changing-delimiters}
191
192As noted in the \texttt{-\/-help} for \texttt{dprepro} and \texttt{pyprepro},
193the delimiters for single-line Python statements, code blocks, and inline
194printed expressions can be changed. This is useful when the defaults are
195reserved characters in the output format.
196
197For code blocks (default \texttt{\{\%\ \%\}}), the innermost characters
198cannot be any of ``\texttt{\{\}{[}{]}()}''.
199
200\subsubsection{Escaping delimiters}\label{interfaces:escaping-delimiters}
201
202All delimiters can be escaped with a leading \texttt{\textbackslash{}}.
203A double \texttt{\textbackslash{}\textbackslash{}} followed by the
204delimiter will return \texttt{\textbackslash{}}. For example:
205
206\begin{verbatim}
207{A=5}
208\{A=5\}
209\\{A=5\\}
210\end{verbatim}
211
212Returns:
213
214\begin{verbatim}
2155
216{A=5}
217\{A=5\}
218\end{verbatim}
219
220Note that escaping the trailing delimiter (e.g.
221\texttt{\textbackslash{}\}}) is optional.
222
223\subsubsection{Immutable Variables}\label{interfaces:immutable-variables}
224
225Variables can be fixed such that they cannot be redefined (without
226explicitly allowing it).
227
228In this example, the attempted reassignemnt of \texttt{param} to 20 is
229ignored,
230
231\begin{verbatim}
232% param = Immutable(10)
233% param = 20
234{param}
235\end{verbatim}
236
237and the output is
238
239\begin{verbatim}
24010
241\end{verbatim}
242
243because \texttt{param} is \texttt{Immutable}. To explicitly make a
244variable mutable again, call it with \texttt{Mutable()}:
245
246\begin{verbatim}
247set             : \{ param = Immutable(10) \} : { param = Immutable(10) }
248try to reset    : \{ param = 20 \}            : { param = 20 }
249make mutable    : \{ param = Mutable(21) \}   : { param = Mutable(21) }
250reset           : \{ param = 20 \}            : { param = 20 }
251\end{verbatim}
252
253Returns:
254
255\begin{verbatim}
256set             : { param = Immutable(10) } : 10
257try to reset    : { param = 20 }            : 10
258make mutable    : { param = Mutable(21) }   : 21
259reset           : { param = 20 }            : 20
260\end{verbatim}
261
262Note that any variable set via an \texttt{-\/-include} file or on the
263command line using \{\textt{-\/-var} will be \textbf{set as Immutable}.
264This is useful for overriding defaults within templates.
265
266\texttt{MyTemplate.inp}:
267
268\begin{verbatim}
269param1 = {param1 = 10}
270param2 = {param2 = pi}
271\end{verbatim}
272
273If called directly:
274
275\begin{verbatim}
276param1 = 10
277param2 = 3.141592654
278\end{verbatim}
279
280However, if called
281\texttt{pyprepro\ -\/-var\ "param1=30"\ \textless{}inputfile\textgreater{}}:
282
283\begin{verbatim}
284param1 = 30
285param2 = 3.141592654
286\end{verbatim}
287
288Or, with an optional \texttt{-\/-include} file:
289
290\texttt{MyInclude.inp}:
291
292\begin{verbatim}
293{param1 = 32}
294\end{verbatim}
295
296And call
297\texttt{pyprepro\ -\/-include\ MyInclude.inp\ MyTemplate.inp}:
298
299\begin{verbatim}
300param1 = 32
301param2 = 3.141592654
302\end{verbatim}
303
304There is one caveat to variable immutability. While the variable name is
305reserved, the value can still be changed if it is a mutable Python object
306(``mutable'' has different meanings for Python objects than is used in
307\texttt{pyprepro} and \texttt{dprepro} templates). For example:
308
309\begin{verbatim}
310% param = Immutable( [1,2,3])
311% param.append(4)   # This will work because it is modifying the object
312% param = ['a','b','c']   # This won't because it is redefining
313{param}
314\end{verbatim}
315
316Will output:
317
318\begin{verbatim}
319[1, 2, 3, 4]
320\end{verbatim}
321
322\\subsubsection{DakotaParams and DakotaResults}\label{interfaces:params-and-results}
323
324If the \texttt{dakota} Python package (see
325Section~\ref{interfaces:dakota.interfacing}) is available for import, (e.g. has
326been added to the \texttt{PYTHONPATH}) then \texttt{Parameters} and
327\texttt{Results} objects generated from the Dakota parameters file provided
328on the comnmand line to \texttt{dprepro} will be available for use in templates.
329The \texttt{Parameters} and \texttt{Results} objects are named \texttt{DakotaParams} and
330\texttt{DakotaResults}, respectively.
331
332This implies that all of these objects' attributes are available within
333templates, such as the evaluation ID (\texttt{DakotaParams.eval\_id} and the
334active set vector entries (\texttt{DakotaResults[0].asv.function}). Dakota
335variables also become available not only directly within the template, but as
336members of \texttt{DakotaParams}. That is, if  \texttt{x1} is a Dakota variable,
337it will be available within a template both by the name \texttt{x1}, and as
338\texttt{DakotaParams["x1"]}. In this way, variables that are disallowed by the
339naming conventions of Python (explained in the following section) can still
340be accessed using their original names.
341
342\subsubsection{Unicode Support}\label{interfaces:unicode}
343
344Variables must obey the naming conventions for the version of Python that is
345used to run \texttt{d/pyprepro}. For Python 2, only ASCII alpanumeric
346characters and the underscore are permitted, and identifiers must not begin
347with a number. In Python 3, this requirement is relaxed considerably, and
348many Unicode characters are permitted in identifiers.
349
350Because Dakota itself has few such restrictions on variable names,
351\texttt{d/pyprepro} "mangles" noncompliant names in the following ways
352before making them available in templates:
353
354* Variables/paramters that begin with a number are prepended by the lowercase
355letter 'i'.
356* Disallowed characters such as # are replaced by underscores (\texttt{\_}).
357* In Python 2, non-ASCII letters are normalized to their rough ASCII
358equivalents (e.g. ñ is replaced by n).
359
360As stated in the previous section, when using \texttt{dprepro} with
361\texttt{dakota.interfacing}, the original variable names are always available
362via the \texttt{DakotaParams} object.
363
364\subsection{General Coding}\label{interfaces:general-coding}
365
366The language of pyprepro is Python with a single, slight modification.
367In normal Python, indentation delineates blocks. However, in pyprepro,
368indentation is ignored and blocks must have an \texttt{end} whether they
369are part of multi-line code (\texttt{\{\%\ \%\}}) or part of single line
370operation (\texttt{\%}).
371
372\subsubsection{Python Coding Tips.}\label{python-coding-tips.}
373
374For the most part, if you are familiar with other interpreted languages
375such as Matlab, coding is Python is very similar.
376
377The key notes are:
378
379\begin{itemize}
380\tightlist
381\item
382  Blocks have \texttt{:} at the end of their statement. For example,
383  \texttt{if\ CONDITION:} \emph{with} the colon
384\item
385  Indentation \textbf{USUALLY} matters but \textbf{DOESN'T} in pyprepo
386
387  \begin{itemize}
388  \tightlist
389  \item
390    As such, you must include a \texttt{end} statement
391  \end{itemize}
392\item
393  Arrays are zero-based
394\item
395  Exponentiation is double \texttt{**}. Example: \texttt{x**y} (``x to
396  the y'')
397\end{itemize}
398
399\subsubsection{Conditionals}\label{conditionals}
400
401Python has the standard set of conditionals. Recall the conditional
402block declaration must end with a \texttt{:} and the entire block must
403have an \texttt{end} statement (again, this is not in normal Python).
404Consider the following example:
405
406\begin{verbatim}
407% param = 10.5
408% if param == 10.0:
409param is 10! See: {param}
410% else:
411param does not equal 10, it is {param}
412% end
413
414% if 10 <= param <= 11:
415param ({param}) is between 10 and 11
416% else:
417param is out of scope
418% end
419\end{verbatim}
420
421results in:
422
423\begin{verbatim}
424param does not equal 10, it is 10.5
425
426param (10.5) is between 10 and 11
427\end{verbatim}
428
429Boolean operations are also possible using simple \texttt{and},
430\texttt{or}, and \texttt{not} syntax
431
432\begin{verbatim}
433% param = 10.5
434% if param >= 10 and param <= 11:
435param is in [10 11]
436% else:
437param is NOT in [10,11]
438% end
439\end{verbatim}
440
441returns:
442
443\begin{verbatim}
444param is in [10 11]
445\end{verbatim}
446
447\subsubsection{Loops}\label{loops}
448
449Python contains \texttt{for} loops that iterate over arbitrary arrays or
450with an index. As with conditionals, the declaration must end with
451\texttt{:} and the block must have an \texttt{end}.
452
453To iterate over an index, from 0 to 4, use the \texttt{range} command
454
455\begin{verbatim}
456% for ii in range(5):
457{ii}
458% end
459\end{verbatim}
460
461will return:
462
463\begin{verbatim}
4640
4651
4662
4673
4684
469\end{verbatim}
470
471You can also iterate over objects in an array (list):
472
473\begin{verbatim}
474% animals = ['cat','mouse','dog','lion']
475% for animal in animals:
476I want a {animal}
477%end
478\end{verbatim}
479
480results in
481
482\begin{verbatim}
483I want a cat
484I want a mouse
485I want a dog
486I want a lion
487\end{verbatim}
488
489\subsubsection{Arrays}\label{arrays}
490
491Arrays are \textbf{zero indexed} and can also have negative indices
492representing the end of the array. They are references by
493\texttt{myarray{[}index{]}}
494
495Consider:
496
497\begin{verbatim}
498% animals = ['cat','mouse','dog','lion']
499{animals[0]}
500{animals[-1]}
501\end{verbatim}
502
503will result in
504
505\begin{verbatim}
506cat
507lion
508\end{verbatim}
509
510Note that pyprepro will \emph{try} to nicely format arrays for printing.
511For certain types, it may not work well.
512
513\begin{verbatim}
514{theta = [0,45,90,135,180,225,270,315]}
515\end{verbatim}
516
517(with \texttt{\{\ \}} to print input) results in
518
519\begin{verbatim}
520[0, 45, 90, 135, 180, 225, 270, 315]
521\end{verbatim}
522
523\subsubsubsection{Math on arrays}\label{math-on-arrays}
524
525Unlike some tools (e.g.~Matlab) you cannot do math on all elements of
526arrays directly. However, there are two possibilities.
527
528The first will \emph{always} work:
529
530\begin{verbatim}
531% theta = [0,45,90,135,180,225,270,315]
532{ [ sin(pi*th/180) for th in theta ] }
533\end{verbatim}
534
535results in
536
537\begin{verbatim}
538[0, 0.7071067812, 1, 0.7071067812, 1.224646799e-16, -0.7071067812, -1, -0.7071067812]
539\end{verbatim}
540
541The alternative is to use NumPy \emph{if} it is installed.
542
543\begin{verbatim}
544% theta = [0,45,90,135,180,225,270,315]
545% import numpy as np
546% theta = np.array(theta) # Redefine as numpy array
547{ np.sin(pi*theta/180) }
548\end{verbatim}
549
550will return:
551
552\begin{verbatim}
553[0, 0.7071067812, 1, 0.7071067812, 1.224646799e-16, -0.7071067812, -1, -0.7071067812]
554\end{verbatim}
555
556\subsubsection{Strings}\label{strings}
557
558Python has extremely powerful and extensive string support. Strings can
559be initialized in any of the following ways:
560
561\begin{verbatim}
562{mystring1="""
563multi-line
564string inline
565"""}
566{mystring1}
567{% mystring2 = '''
568another multi-line example
569but in a block
570''' %}
571mystring2: {mystring2}
572
573Single quotes: {'singe'}
574Double quotes: {'double'}
575\end{verbatim}
576
577and it returns
578
579\begin{verbatim}
580multi-line
581string inline
582
583
584multi-line
585string inline
586
587mystring2:
588another multi-line example
589but in a block
590
591
592Single quotes: singe
593Double quotes: double
594\end{verbatim}
595
596The choice of single \texttt{\textquotesingle{}} or \texttt{"} can be
597driven by convenience.
598
599Strings can be joined by adding them:
600
601\begin{verbatim}
602{%
603a = 'A'
604b = 'B'
605%}
606{a + ' ' + b}
607\end{verbatim}
608
609returns:
610
611\begin{verbatim}
612A B
613\end{verbatim}
614
615\subsubsubsection{Custom Functions}\label{custom-functions}
616
617You can define any arbitrary functions using either \texttt{def} or
618\texttt{lambda}
619
620Consider the following: (note, we use indentation here for readability
621but indentation \emph{is ignored} and the function definition is
622terminated with \texttt{end}):
623
624\begin{verbatim}
625{%
626def myfun1(param):
627    return (param + 1) ** 2 + 3
628end
629
630myfun2 = lambda param: (param + 1) ** 2 + 5
631%}
632{myfun1(1.2)}
633{myfun2(1.2)}
634{ [ myfun1(x) for x in [1,2,3,4] ] }
635\end{verbatim}
636
637returns:
638
639\begin{verbatim}
6407.84
6419.84
642[7, 12, 19, 28]
643\end{verbatim}
644
645\subsection{Auxiliary Functions}\label{interfaces:auxiliary-functions}
646
647There are a series of auxiliary functions to help. The primary one is
648the \texttt{include}
649
650\subsubsection{Include}\label{include}
651
652Using
653
654\begin{verbatim}
655% include('path/to/include.txt')
656\end{verbatim}
657
658Will insert the contents of
659\texttt{\textquotesingle{}path/to/include.txt\textquotesingle{}}. Inside
660\texttt{\textquotesingle{}path/to/include.txt\textquotesingle{}}, there
661can be new variable definitions and/or it can access older ones. Note
662that unlike the command-line \texttt{-\/-include}, there is \emph{no
663(im)mutability assigned} for these unless explicit for each parameter!
664
665The code will search for the include text first in the path of the
666original template file and then in the path where \texttt{pyprepro} is
667executed.
668
669\subsubsection{Immutable and Mutable}\label{immutable-and-mutable}
670
671As explained elsewhere, variables can be defined as
672\texttt{Immutable(value)} or \texttt{Mutable(value)}. If a variable is
673Immutable, the value cannot be reset unless \emph{explicitly} made
674mutable.
675
676Note: files called with the command line \texttt{-\/-include} have all
677of their variables be immutable. But this does not affect those with the
678\texttt{include()} function
679
680\subsubsection{Print all variables}\label{print-all-variables}
681
682\texttt{all\_vars()} and \texttt{all\_var\_names()} print out all
683\emph{defined} variables. Consider the following that also demonstrates
684setting a comment string (two ways)
685
686\begin{verbatim}
687% param1 = 1
688{param2 = 'two'}
689all variables and values: {all_vars()}
690all varables: {all_var_names()}
691
692{all_var_names(comment='//')}
693// {all_var_names()} <--- Don't do this
694\end{verbatim}
695
696returns:
697
698\begin{verbatim}
699two
700all variables and values: {'param1': 1, 'param2': u'two'}
701all varables: ['param2', 'param1']
702
703// ['param2', 'param1']
704// ['param2', 'param1'] <--- Don't do this
705\end{verbatim}
706
707Notice the empty \texttt{()} at the end of \texttt{all\_vars} and
708\texttt{all\_var\_names}. If possible, it is \emph{better} to use
709\texttt{comment=\textquotesingle{}//\textquotesingle{}} syntax since the
710result of these can be multiple lines.
711
712\subsubsection{Set global print format}\label{set-global-print-format}
713
714As discussed elseware, the print format can be set on a per item basis
715by manually converting to a string. Alternatively, it can be (re)set
716globally inside the template (as well as at the command line).
717
718\begin{verbatim}
719{pi}
720% setfmt('%0.3e')
721{pi}
722% setfmt() # resets
723{pi}
724\end{verbatim}
725
726returns:
727
728\begin{verbatim}
7293.141592654
7303.142e+00
7313.141592654
732\end{verbatim}
733
734\subsubsubsection{Aside: Set output format
735individually}\label{aside-set-output-format-individually}
736
737The following demonstrates setting the output for a specific line. The
738key is to use Python to convert the value into a string which is then
739displayed:
740
741\begin{verbatim}
742{pi}
743{ '%0.3f' % pi }
744\end{verbatim}
745
746Will output:
747
748\begin{verbatim}
7493.141592654
7503.142
751\end{verbatim}
752
753\subsubsection{Using Defaults undefined
754parameters}\label{using-defaults-undefined-parameters}
755
756Directly calling undefined parameters will result in an error. There is
757no \emph{universal} default value. However, there are the following
758functions:
759
760\begin{itemize}
761\tightlist
762\item
763  \texttt{get} -- get param with optional default
764\item
765  \texttt{defined} -- determine if the variable is defined
766\end{itemize}
767
768The usage is explained in the following examples:
769
770\begin{verbatim}
771Defined Parameter:
772% param1 = 'one'
773{ get('param1') } <-- one
774{ get('param1','ONE') } <-- one
775
776Undefined Paramater
777{ get('param2') } <-- *blank*
778{ get('param2',0) } <-- 0
779
780Check if defined: { defined('param2') }
781
782% if defined('param2'):
783param2 is defined: {param2}
784% else:
785param2 is undefined
786% end
787\end{verbatim}
788
789returns:
790
791\begin{verbatim}
792Defined Parameter:
793one <-- one
794one <-- one
795
796Undefined Paramater
797 <-- *blank*
7980 <-- 0
799
800Check if defined: False
801
802param2 is undefined
803\end{verbatim}
804
805But notice if you have the following:
806
807\begin{verbatim}
808{param3}
809\end{verbatim}
810
811you will get the following error:
812
813\begin{verbatim}
814Error occurred:
815    NameError: name 'param3' is not defined
816\end{verbatim}
817
818\subsubsection{Mathematical Functions}\label{mathematical-functions}
819
820All of the Python \texttt{math} module in imported with the functions:
821
822\begin{verbatim}
823  acos       degrees     gamma   radians
824  acosh      erf         hypot   sin
825  asin       erfc        isinf   sinh
826  asinh      exp         isnan   sqrt
827  atan       expm1       ldexp   tan
828  atan2      fabs        lgamma  tanh
829  atanh      factorial   log     trunc
830  ceil       floor       log10
831  copysign   fmod        log1p
832  cos        frexp       modf
833  cosh       fsum
834\end{verbatim}
835
836Also included are the following constants
837
838\begin{longtable}[]{@{}ll@{}}
839\toprule
840Name & value\tabularnewline
841\midrule
842\endhead
843\texttt{pi},\texttt{PI} & 3.141592654\tabularnewline
844\texttt{e},\texttt{E} & 2.718281828\tabularnewline
845\texttt{tau} (\texttt{2*pi}) & 6.283185307\tabularnewline
846\texttt{deg} (\texttt{180/pi}) & 57.29577951\tabularnewline
847\texttt{rad} (\texttt{pi/180}) & 0.01745329252\tabularnewline
848\texttt{phi} (\texttt{(sqrt(5)+1\ )/2}) & 1.618033989\tabularnewline
849\bottomrule
850\end{longtable}
851
852Note that all trig functions are assuming radians. See
853\href{https://docs.Python.org/3/library/math.html}{Python's
854\texttt{math} library} for more details. To compute based on degrees,
855convert first:
856
857\begin{verbatim}
858{ tan( radians(45) )}
859{ tan( 45*rad)}
860{ degrees( atan(1) )}
861{ atan(1) * deg }
862\end{verbatim}
863
864returns:
865
866\begin{verbatim}
8671
8681
86945
87045
871\end{verbatim}
872
873\subsubsection{Other Functions}\label{other-functions}
874
875Other functions can be imported. All of Python is available. For
876example, to get a random number, you can do:
877
878\begin{verbatim}
879% from random import random,seed
880% seed(1)
881{A = random()}
882\end{verbatim}
883
884Returns (may depend on the system)
885
886\begin{verbatim}
8870.1343642441
888\end{verbatim}
889