Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def filter(equ):
- equ = "".join(equ.split()).lower()
- replace = (('||','|'),('{','('),('[','('),('}',')'),(']',')'),('i','j'))
- for i,j in replace:
- equ = equ.replace(i,j)
- return equ
- def rightscope(equ, start):
- count = 0
- count_change = False
- for i in range(start+1, len(equ), 1):
- if equ[i] == '(':
- count += 1
- count_change = True
- elif equ[i] == ')':
- count -= 1
- count_change = True
- elif equ[i] == '|' and count == 0:
- return i-1
- if count_change:
- if count < 0:
- return i-1
- return len(equ)-1
- def leftscope(equ, start):
- count = 0
- count_change = False
- for i in range(start-1, -1, -1):
- if equ[i] == '(':
- count += 1
- count_change = True
- elif equ[i] == ')':
- count -= 1
- count_change = True
- elif equ[i] == '|' and count == 0:
- return i+1
- if count_change:
- if count > 0:
- return i+1
- return 0
- def parallel(zlist):
- zsum = 0
- for z in zlist:
- zsum += 1/z
- return 1/zsum
- def zcalc(equ):
- equ = filter(equ)
- pos = equ.find('|')
- while pos != -1:
- parallel = []
- temp_pos = pos
- while True: #gather all scopes to the left into list
- lpos = leftscope(equ, temp_pos)
- parallel.insert(0, equ[lpos:temp_pos])
- if (lpos == 0): break
- elif (equ[lpos-1] != "|"): break
- temp_pos = lpos-1
- temp_pos = pos
- while True: #gather all scope to the right into list
- rpos = rightscope(equ, temp_pos)
- parallel.append(equ[temp_pos+1:rpos+1])
- if (rpos == len(equ)-1) or (equ[rpos+1] != "|"): break
- temp_pos = rpos+1
- new_equ = "parallel(["
- for i in parallel: #create string for paralleled function
- new_equ = new_equ + i + ","
- equ = equ[0:lpos] + new_equ[:len(new_equ)-1] + "])" + equ[rpos+1:] #replace parallelized part of string with parallel string
- pos = equ.find("|")
- return equ
- #return eval(equ)
- test = "5j|5+(4+5j)|(4+(5j|-6j))"
- print(eval(zcalc(test)))
- #print(zcalc(test))
Advertisement
Add Comment
Please, Sign In to add comment