Source code for fluxpyt.utility
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 19 10:44:54 2016
@author: Trunil
"""
from math import factorial
import numpy as np
[docs]def space():
for i in range(5):
print('')
[docs]def size(array):
'''array should be numpy array or list of lists'''
assert len(array) != 0, 'input matrix is empty.'
n_row = 0
n_col = 0
l = len(array[0])
for r in array:
assert len(r) == l,'length of all rows does not match'
n_row += 1
for c in r:
n_col += 1
return n_row,n_col
#a = list(np.array([1,2,3]))
#print(size([a]))
[docs]def split_rxn(rxn):
rxn_split = rxn.split()
if len(find(rxn_split,'->')) > 0:
rxn_split.remove('->')
if len(find(rxn_split,'+')) > 0:
while '+' in rxn_split:
rxn_split.remove('+')
return rxn_split
#print(split_rxn('a + v + f + f -> r + r + e'))
[docs]def nCr(n,r):
'''http://stackoverflow.com/questions/4941753/is-there-a-math-ncr-function-in-python'''
f = factorial
return f(n)/(f(r)*f(n-r))
[docs]def find(a,b,exclude=False):
''' a = list
b = item to be found
'''
ind = []
for i in range(len(a)):
item = a[i]
if exclude == False:
if b == item:
ind.append(i)
elif exclude == True:
if b != item:
ind.append(i)
return ind
[docs]def prod(array):
'''array = list of lists'''
ans = []
for r in array:
p = 1
for i in r:
p = p*i
ans.append(p)
return ans
[docs]def pause():
input('\n\nProgram paused. Put cursor in kernel and press enter to continue. ') #for python 3k
[docs]def sum_matrix(matrix):
'''gives sum of elements in a matrix.
matrix = list of list of numbers'''
ans = 0
for rows in matrix:
for el in rows:
ans += el
return ans