Represent code in math equations
[comment]: # Represent code in math equations
Introduce
The article shows a way to use math equations to represent code's logical.
Key ideas
logical first
Get readers to see the logical first, so prefer to keep a 'where' section to describe variables separately.
Pure function conception
I am thinking, in a function, whether we can just use
- Only has a 'if/else' statement
- Only has a 'switch/case' statement
- Only has a 'for' statement
- Only has a 'foreach' statement
- Only has a 'while' statement
- Only has statements without 'if/else', 'switch/case', 'for', 'foreach' or 'while'.
Decision Structures
- Decision structure - simple
- Decision structure (if ... then) - if, else
- Decision structure (if ... then) - if, else with multiple lines
- Decision structure(selection) - switch, case, default, goto, break
- Loop structure (conditional) - while, continue
First check the condition to determine if enter the loop.
- Loop structure (conditional) - do, while, continue
First execute the statement, then check the condition to determine if enter the next loop.
- Loop structure (iteration) - for
- Loop structure (iteration) - for with step
- Loop structure (iteration) - foreach
Keywords
- break
\(\bigotimes\) - continue
\(\bigodot\)
function
\[\text{(variable_1 [, ..., variable_n]) function_name(parameter_1 [, ..., parameter_k])} = \\ [\{] \\ \qquad statement_1 \\ \qquad statement_2 \\ \qquad ... \\ \qquad statement_n \\ where \\ \qquad variable_1 = ... \\ \qquad ... \\ \qquad variable_m = ... \\ [\}] \]function in one body
\[\text{(variable_1 [, ..., variable_n]) function_name(parameter_1 [, ..., parameter_l])} = \\ \qquad \begin{cases} statement_1 \\ statement_2 \\ where \\ \qquad variable_1 = ... \\ \qquad ... \\ \qquad variable_m = ... \\ \end{cases} \\ \]class
\[\text{class class_name[(inherited class name)]} = \\ [\{] \\ [where] \\ \qquad field_1 = ... \\ \qquad ... \\ \qquad field_m = ... \\ [functions]\\ \qquad function_1 \\ \qquad function_2 \\ \qquad ... \\ \qquad function_n \\ [\}] \]Comment - single line
\[\text{ # input some comments} \\ \text{ : input some comments} \]Comment - multiple lines
\[''' \\ \text{ this is} \\ \text{ multiple lines comments} \\ ''' \]Comment - multiple lines 2
\[""" \\ \text{ this is} \\ \text{ multiple lines comments} \\ """ \]Sample
\[\text{class firstclass} = \\ \qquad field1 = 1 \\ \qquad field2 = true \\ \qquad \\ \qquad func1(param1, y) \\ \qquad \{ \\ \qquad \qquad var1,\ var2 = func2(1,\ 2) = \\ \qquad \qquad func3(var1) \\ \qquad \qquad y^{(mean)} = f4(y) \\ \qquad where \\ \qquad \qquad var1 \text{ # return value 1}\\ \qquad \qquad var2 \text{ # return value 2}\\ \qquad \qquad y \text{ : result data of training data.} \\ \qquad \qquad y^{(mean)} \text{ : the arithmetic mean along the y.} \\ \qquad \} \\ \qquad \\ \qquad (result1,\ result2)\ func2(param1,\ param2) = \\ \qquad \{ \\ \qquad \qquad result1 = \begin{cases} 1, & \text{param1 > 0} \\ -1 & \text{otherwise} \end{cases} \\ \qquad \qquad var2 = 100 \text{ # it is a variable defined in body. }\\ \qquad \qquad result2 = var1 + var2 + param2 \\ \qquad where \\ \qquad \qquad result1 \text{ # return value 1} \\ \qquad \qquad result2 = 0 \text{ # return value 2} \\ \qquad \qquad var1 = 10 \text{ # it is a variable. } \\ \qquad \} \\ \qquad \\ \qquad func3(param1) = \\ \qquad \begin{cases} \text{# do something ...} \\ \text{# do something ...} \\ \text{# do something ...} \\ \text{# do something ...} \\ \text{# do something ...} \\ \text{# do something ...} \\ \end{cases} \\ \qquad \\ \qquad f4(y) = \frac{sum(y)}{count(y)} \\ \qquad \\ \qquad \]References
- Help:Displaying a formula
- LaTeX Wiki
- List of LaTeX symbols
- LaTeX documentation