Introduction¶
SimulaMath module is built on top of the scientific Python packages like Numpy, Scipy, Sympy, and Mpmath.
Special Simula Syntax¶
Fractions: on simula, the result of the division of two Integers is a fraction. But on Python, it is a
float
number.simula : 2/3 2/3 simula : 4/10 2/5 simula : 1/2 + 1/5 + 3/5 13/10 simula : int(1)/int(2) 0.5
Multiplication: the multiplication under simula is « * » as in Python.
simula : x = 2; x 2 simula : 3*x 6
There are some special cases of multiplication.
When a number is followed by a variable, it means multiplication.
simula : x = 2; x 2 simula : 3x 6 simula : 5x -2 8
When a number is followed by an open parenthesis « ( », it means multiplication.
simula : x = 2; x 2 simula : 3(x+2) 12 simula : 5(2x-1) 15
When a closed parenthesis « ) » is followed by an open parenthesis « ( », it means multiplication.
simula : x = 3; x 3 simula : (x-1)(x+2) 10 simula : 5(2x-1)(x-1) 50
Power: the power under simula is « ^ » or « ** » as in Python.
simula : 2^3 8 simula : x = 3; 2x^2 18 simula : (x - 1)(2x^3 -10) 88
Remark : The symbol « ^ » means bitwise XOR in Python, but on simula, the equivalent operator is « ^^ ».
EXAMPLE:
simula : bin(0b100101 ^^ 0b001010) '0b101111'
Factorial : A number followed by « ! » symbol means factorial.
simula : 3! 6 simula : 3! == 6 True simula : 3! != 6 False simula : 6!/4! 30
Special Sequences : [a, b, …, n], (a, b, …, n) or {a, b, …, n}.
simula : [1, 3, ..., 11] [1, 3, 5, 7, 9, 11] simula : {1, 3, ..., 11} {1, 3, 5, 7, 9, 11} simula : (1, 3, ..., 11) (1, 3, 5, 7, 9, 11) simula : [10, 20, ..., 100] [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Symbolic variables:
Symbolic Variables
-
simula.symbols.
var
(names, domain=None, parity=None, *, globals=True, **kwargs)¶ Create symbols and inject them into the global namespace.
Valid kwargs:
commutative : True or False
EXAMPLES:
simula : var('x') x simula : x x simula : var('x, y', "RR") # x and y are real numbers (x, y) simula : x.is_real and y.is_real True simula : var('z', "RR*+") # z is a positive real number z simula : z > 0 True simula : z < 0 False simula: z > 4 z > 4 simula : n = var('n', "NN"); n # n is a non-negative integer number n simula : n >= 0 True simula : var('x, y2, ab') (x, y2, ab) simula : y2 y2 simula : var(('a', 'b', 'c')) (a, b, c) simula : var(['a', 'b', 'c']) [a, b, c] simula : var({'a', 'b', 'c'}) {a, b, c} simula : var('x:z') (x, y, z) simula : var('x1:4') (x1, x2, x3) simula : xa, yb = var('x((a:b))') simula : xa x(a)
- Paramètres
globals (bool) –
Functions : You can define a function easily on simula like in mathematics.
simula : x = var('x') simula : f(x) = x^2-2x-2; f Function defined by x |--> x^2 - 2x - 2 simula : f(2) -2 simula : f(2x-1) -4x + (2x - 1)^2 simula : y = var('y') simula : g(x, y) = x - y + 1; g Function defined by (x, y) |--> x - y + 1 simula : g(x, x) 1
Complex numbers: The imaginary unit is represented by I.
simula : 3-5I 3-5I simula : conjugate(3-5I) 3 + 5I simula : real_part(3-5I) 3 simula : im_part(3-5I) -5
Python complex numbers are compatible with Simula complex numbers.
simula : 2-5j 3-5I simula : real_part(2-5j) 3
Polynomial ring : You can define a polynomial ring like in Sage.
simula : R.<x, y, z> = QQ[] simula : R Multivariate Polynomial Ring in x, y, z over QQ with deglex order simula : (x^2-1).factor() (x - 1)*(x + 1) simula : F.<w> = GF(3)[]; F Univariate Polynomial Ring in w over GF(3) with deglex order simula : 5w^4+10w^2-2 2w^4 + w^2 - 2
Finite Fields : You can define a finite field like in Sage.
simula : G.<a> = GF(9); G Finite Field of 9 elements defined by the quotient of F_3[a] by the ideal <a^2 + 2a + 2> simula : a^2 a + 1 simula : 7a^3 2a + 1 simula : 1/a a + 2
Binary, Octal and Hexadecimal:
Python Binary, Octal and Hexadecimal :
simula : 0b1110 14 simula : bin(14) '0b1110' simula : oct(100) '0o144' simula : hex(1000) '0x3e8'
Simula Binary, Octal and Hexadecimal :
simula : Bin(14) 0b1110 simula : A = Bin(111); A 0b1101111 simula : A.to_list() [1, 1, 0, 1, 1, 1, 1] simula : A.to_list(10) [0, 0, 0, 1, 1, 0, 1, 1, 1, 1] simula : Bin(14) + Bin(17) 0b11111 simula : Bin(14) + Bin(17) == Bin(31) True simula : Bin(bin(14)) 0b1110 simula : Oct(1000) 0o1750 simula : Hex(1000) 0x3e8 simula : Hex(100) + Hex(120) == Hex(220) True
Simula Syntaxe as Python¶
Since SimulaMath language is based on Python, 99% of Python valid code work also on SimulaMath.
Float numbers:
simula : 7.8 7.8 simula : 6. 6.0 simula : .5 0.5
Exponents:
simula : 2e3 2000.0 simula : 3e-4 0.0003 simula : 3e+4 30000.0
Lists:
simula : seq = [1,2,3,4,5]; print(seq) [1, 2, 3, 4, 5] simula : seq[0] 1 simula : seq[:2] [1, 2] simula : seq[-2:] [4, 5]
Comprehension of list
simula : [i^2 for i in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] simula : x = var('x'); f(x) = x^2-4x-1 simula : [f(i) for i in range(15)] [-1, -4, -5, -4, -1, 4, 11, 20, 31, 44, 59, 76, 95, 116, 139]
Tuples:
simula : seq2 = (1,2,3,4,5); print(seq2) (1, 2, 3, 4, 5) simula : seq[-1] 5 simula : seq[:2] [1, 2] simula : seq[-2:] [4, 5]
Sets:
simula : A = {1,2,3,4,5, 10, 15}; print(A) {1, 2, 3, 4, 5, 10, 15} simula : len(A) 7 simula : B = {-2, 4}; B {4, -2} simula : A | B {1, 2, 3, 4, 5, 10, 15, -2} simula : A & B {4}
Comprehension of Set
simula : {i^2 for i in range(10)} {0, 1, 64, 4, 36, 9, 16, 49, 81, 25} simula : x = var('x'); f(x) = x^2-4x-1 simula : {f(i) for i in range(15)} {4, 59, 11, 44, 76, 139, 20, 116, 95, -5, -4, -1, 31}
Strings:
simula : word = "SimulaMath"; word 'SimulaMath' simula : word.upper() 'SIMULAMATH' simula : word.isalpha() True simula : word[2:] 'mulaMath' simula : "Simula" "Math" 'SimulaMath' simula : a, b = 2, 8 simula : "We get a = {} and b = {}".format(a, b) 'We get a = 2 and b = 8' simula : f"We get a = {a} and b = {b}" 'We get a = 2 and b = 8' simula : f"We get a = {2a} and b = {b^2}" 'We get a = 4 and b = 64'
Dictionaries:
simula : dico = {'A': 0, "B": 1, 3: (1,2,3)}; print(dico) {'A': 0, 'B': 1, 3: (1, 2, 3)} simula : list(dico.keys()) ['A', 'B', 3] simula : list(dico.values()) [0, 1, (1, 2, 3)] simula : del dico['A']; dico {'B': 1, 3: (1, 2, 3)} simula : dico["S"] = "SimulaMath"; dico {'B': 1, 3: (1, 2, 3), 'S': 'SimulaMath'}
Comprehension of Dictionary
simula : {i : i^2 for i in range(10)} {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81} simula : x = var('x'); g(x) = 3x-1 simula : {2m: g(m) for m in range(15)} {0: -1, 2: 2, 4: 5, 6: 8, 8: 11, 10: 14}
Note that conditions, loops (for loop, while loop) and functions syntax on SimulaMath and Python are the same.
Conditions
simula : N = 194 simula : if N % 7 == 0: . . . . . . : print(f"{N} is a multiple of 7") . . . . . . : else: . . . . . . : print(f"{N} is not a multiple of 7") . . . . . . : 194 is not a multiple of 7
- Loops
simula : for i in range(9): . . . . . . : print(2i) 0 2 4 6 8 10 12 14 16 simula : for elt in [0, 5, ..., 30]: . . . . . . : print(elt) 0 5 10 15 20 25 30
- Functions
simula : def mean(L): . . . . . . : return sum(L)/len(L) . . . . . . : simula : mean([1,2,3,4,5]) 3 simula : mean([3,4]) 7/2
For more details on Python syntax, see the Python Doc
SimulaMath Editor¶
SimulaMath has a basic editor which allow you to save and load files with extension .sim and .py.