## Calculations by SmartCnsl and Python Sf:1/??

November 13, 2010 Leave a comment

We need to make expressions more similar to usual mathematical ones. Python numpy/scipy, etc packages/modules are very useful for mathematical calculations. But they are limited by Python syntax. You must write multiply operator all the time. Although mathematical expressions omit multiply operator almost every time. You can’t write Greek characters until python 3000. Although mathematical expressions include Greek characters many times. There is no symbolic representation that notate a matrix/vector in Python. You must call a function as numpy.array([…])

So we introduced a preprocessor and extend Python syntax upper compatibly and extend name space to avoid conflicts between Python and mathematics

Optimally customizing Python sf for each application field, it become possible to calculate by a one-liner for more than 90 percent. In mathematics one-liners are adequate for a units of consideration.

You can download free portable Python sf from hear

------------------------------------------------------- ----------- Python sf Preprocessor ------------------- -------------------------------------------------------

We have extended Python syntax upper compatibly and make enable to calculate expressions similar to usual mathematical ones as below.

**Multiply operator should be omissible.**

```
a,b = 2,3; 4 a b # 4 * a * b == 24
===============================
24
```

```
```

"a,b = 2,3; 4 a b # 4 * a * b == 24" string is pass to a pre-processor module sfPP.py. For this string, Actually **python -u -m sfPP.py "a,b = 2,3; 4 a b # 4 * a * b == 24"** command must be executed.

Probably you want to now the value of the last expression in a series of Python sf expressions. So, Python sf preprocessor automatically print out the last value to a console. You can omit the print instruction.

If you use SmartCnsl plugin menu:pysf -->C then the plugin add **python -u -m sfPP.py** with double quotations and execute the python command. It is very convenient for calculations.

Hereafter we write only Python sf expressions and omit python -u -m sfPP.py.

Because Python sf is upper compatible for Python, you can write a multply operator and other Python expressions if you want them.

```
a,b = 2,3; 4 a b == 4*a*b == 24
===============================
True
```

```
```

**Basic numeric functions should be in a global name space.**

Basic numeric functions: exp, sin, cos, tan, sinh, cosh, tanh, arcsin, arccos, arctan, log, log10, sqrt are in global name space in Python sf. They could be used without import.

```
a,b = pi/6, pi/3; sin(a+b)
===============================
1.0
sin(3+4j)
===============================
(3.85373803792-27.016813258j)
a,b = pi/6, pi/3; sin([a,b])
===============================
[ 0.5 0.8660254]
---- ClTensor ----
a,b = pi/6, pi/3; sin([[a,b, a+b],[4,5,6]])
===============================
[[ 0.5 0.8660254 1. ]
[-0.7568025 -0.95892427 -0.2794155 ]]
---- ClTensor ----
```

```
```

These basic numeric functions can calculate for complex numbers. They can calculate a sequence of numbers. The result is a vector or matrix.

**^ operator should be usable for power operations**

At Python syntax, ^ operator is bit exclusive or. But it is quietly confined to use bit exclusive or. On the other side, ^ is used commonly as a power operator in mathematics. So in Python sf, ^ operator is a power operator. If you want to use bit exclusive or in Python sf, you must use \^. Of course, you can use ** operator as another power operator.

```
2^4
===============================
16
2**4
===============================
16
2\^4
===============================
6
```

```
```

**Matrix/vector should be describes easily as far as possible and be operated similarly as usual four operations**

So we introduce ~[...] representation for matrix/vector and extended numpy.ndarray to ClTensor class.

```
~[1,2,3] # a vector representation in Python sf
===============================
[ 1. 2. 3.]
---- ClTensor ----
vct=~[1,2,3]; 2 vct # scalar * default float vector
===============================
[ 2. 4. 6.]
---- ClTensor ----
v1,v2=~[1,2,3],~[4,5,6]; v1+v2 # add vectors
===============================
[ 5. 7. 9.]
---- ClTensor ----
v1,v2=~[1,2,3],~[4,5,6]; v1 v2 # inner product
===============================
32.0
v1,v2=~[1,2,3],~[4,5,6]; v1^v2 # diadic: outer product
===============================
[[ 4. 5. 6.]
[ 8. 10. 12.]
[ 12. 15. 18.]]
---- ClTensor ----
vct=~[1,2,3,int]; 2 vct # scalar * vector designeted as integer type
===============================
[2 4 6]
---- ClTensor ----
```

```
```

Usually numerical calculation is performed as float/complex numbers. If integer type vectors or matrices are used, there might be many error calculations eliminating numbers after the decimal point.

```
~[[1,2,3],[4,5,6]] # a matrix representation in Python sf
===============================
[[ 1. 2. 3.]
[ 4. 5. 6.]]
---- ClTensor ----
mt=~[[1,2,3],[4,5,6]]; 2 mt # scalr * matrix
===============================
[[ 2. 4. 6.]
[ 8. 10. 12.]]
---- ClTensor ----
mt,vct=~[[1,2,3],[4,5,6]], ~[7,8,9]; mt vct # matrx * vector
===============================
[ 50. 122.]
---- ClTensor ----
mt,vct=~[[1,2,3],[4,5,6]], ~[7,8]; vct mt # vector * matrix
===============================
[ 39. 54. 69.]
---- ClTensor ----
mt,lst=~[[1,2,3],[4,5,6]], [7,8,9]; mt lst # matrx * list
===============================
[ 50. 122.]
---- ClTensor ----
mt,tpl=~[[1,2,3],[4,5,6]], (7,8,9); mt tpl # matrx * tuple
===============================
[ 50. 122.]
---- ClTensor ----
mt =~[[1,2],[3,4]]; mt^2
===============================
[[ 7. 10.]
[ 15. 22.]]
---- ClTensor ----
mt =~[[1,2],[3,4]]; mt^0
===============================
[[ 1. 0.]
[ 0. 1.]]
---- ClTensor ----
mt =~[[1,2],[3,4]]; mt^-1 # matrix inverse
===============================
[[-2. 1. ]
[ 1.5 -0.5]]
---- ClTensor ----
mt =~[[1,2],[3,4]]; 1/mt # other matrix inverse
===============================
[[-2. 1. ]
[ 1.5 -0.5]]
---- ClTensor ----
mt =~[[1,2],[3,4]]; mt.inv()# other matrix inverse
===============================
[[-2. 1. ]
[ 1.5 -0.5]]
---- ClTensor ----
mt =~[[1,2],[3,4]]; mt^2 + 3mt+100mt^0 # a polynomial of a matrix
===============================
[[ 110. 16.]
[ 24. 134.]]
---- ClTensor ----
mt =~[[1,2],[3,4]]; (mt^2+ mt^0)/(mt^2 + 3mt+100mt^0) # a ratianal expresion of a matrix
===============================
[[ 0.05795486 0.06770688]
[ 0.10156032 0.15951519]]
---- ClTensor ----
mt=~[[1,2,3],[4,5,6]]; mt.t # transpose matrx
===============================
[[ 1. 4.]
[ 2. 5.]
[ 3. 6.]]
---- ClTensor ----
mt=~[[1,2,3],[4,5,6+7j]]; mt.conj() # transpose matrx
===============================
[[ 1.-0.j 2.-0.j 3.-0.j]
[ 4.-0.j 5.-0.j 6.-7.j]]
---- ClTensor ----
mt =~[[1,2],[3,4]]; mt.m_dtrm # determinant
===============================
-2.0
mt=~[[1,2,3],[4,5,6]]; mt.r # vector from matrx
===============================
[ 1. 2. 3. 4. 5. 6.]
---- ClTensor ----
```

```
```

You can use slice operator : for a matrix or vector.

```
vc=~[1,2,3,4]; vc[:2]
===============================
[ 1. 2.]
---- ClTensor ----
vc=~[1,2,3,4]; vc[2:4]
===============================
[ 3. 4.]
---- ClTensor ----
vc=~[1,2,3,4]; vc[-2:]
===============================
[ 3. 4.]
---- ClTensor ----
vc=~[1,2,3,4]; vc[-2]
===============================
3.0
# assign ment for elements
vc=~[1,2,3,4]; vc[2:4]=[10,11];vc
===============================
[ 1. 2. 10. 11.]
---- ClTensor ----
mt=~[[1,2,3],[4,5,6]]; mt[:,0]
===============================
[ 1. 4.]
---- ClTensor ----
mt=~[[1,2,3],[4,5,6]]; mt[1,:]
===============================
[ 4. 5. 6.]
---- ClTensor ----
mt=~[[1,2,3],[4,5,6]]; mt[1,1:]
===============================
[ 5. 6.]
---- ClTensor ----
# assignment for elements
mt=~[[1,2,3],[4,5,6]]; mt[1,1:]=[10,11];mt
===============================
[[ 1. 2. 3.]
[ 4. 10. 11.]]
---- ClTensor ----
```

```
```

You can calculate eigen values/vectors, exponential of a matrix, logarithm of a matrix,

```
mt =~[[1,2],[3,4]]; eig(mt) # eigen values and eigen vectors
===============================
(ClTensor([-0.37228132, 5.37228132]),
ClTensor([[-0.82456484, -0.41597356],
[ 0.56576746, -0.90937671]]))
mt =~[[1,2],[3,4]]; expm(mt) # exponential for a matrix, nor for elements
===============================
[[ 51.9689562 74.73656457]
[ 112.10484685 164.07380305]]
---- ClTensor ----
mt =~[[1,2],[3,4]]; exp(mt) # exponential for elements
===============================
[[ 2.71828183 7.3890561 ]
[ 20.08553692 54.59815003]]
---- ClTensor ----
mt =~[[1,2],[3,4]]; logm(mt)
===============================
[[-0.35043981+2.39111795j 0.92935121-1.09376217j]
[ 1.39402681-1.64064326j 1.04358699+0.7504747j ]]
---- ClTensor ----
H =~[[1,2],[3,4]]; [expm(t H) [1,2] for t in (0, 0.1, 0.2, 0.3) ]
===============================
[ClTensor([ 1., 2.]), ClTensor([ 1.66279504, 3.45576546]), ClTensor([ 2.79877861, 5.94573902]), ClTensor([ 4.7444446 , 10.20555274])]
```

```
```