How do I calculate quantization error of 2 tuples in python - python

I'm currently trying to code Floyd-Steinberg dithering in python
When trying to add colour in Floyd-Steinberg dithering, you have to calculate the "quantization error" of 2 tuples.
How would I go about doing this?
Converting them to grayscale doesn't produce good results, and I'm not sure how I would get it to work.
I don't want help with any other part of my code, just the quantization error.
Here is my code :
from PIL import Image
import sys,math
imag = Image.open(sys.argv[1])
palette = [(0,0,0,255),(255,255,255,255),(255,0,0,255),(0,255,0,255),(0,0,255,255)]
def distance(co1, co2):
return pow(abs(co1[0] - co2[0]), 2) + pow(abs(co1[1] - co2[2]), 2)
def palcol(col,pal):
return min(pal, key=lambda x: distance(x, col))
for y in range(imag.height-1):
for x in range(imag.width-1):
oldpixel = imag.getpixel((x,y))[0] * 299/1000 + imag.getpixel((x,y))[1] * 587/1000 + imag.getpixel((x,y))[2] * 114/1000
newpixel = palcol(imag.getpixel((x,y)),palette)[0] * 299/1000 + palcol(imag.getpixel((x,y)),palette)[1] * 587/1000 + palcol(imag.getpixel((x,y)),palette)[2] * 114/1000
querror = oldpixel - newpixel
newpixel = palcol(imag.getpixel((x,y)),palette)
imag.putpixel((x,y),newpixel)
imag.putpixel((x + 1,y ),tuple([round(imag.getpixel((x + 1,y ))[0] + (querror*7/16)),round(imag.getpixel((x + 1,y ))[1] + (querror*7/16)),round(imag.getpixel((x + 1,y ))[2] + (querror*7/16)),255]))
imag.putpixel((x - 1,y + 1),tuple([round(imag.getpixel((x - 1,y + 1))[0] + (querror*3/16)),round(imag.getpixel((x - 1,y + 1))[1] + (querror*3/16)),round(imag.getpixel((x - 1,y + 1))[2] + (querror*3/16)),255]))
imag.putpixel((x ,y + 1),tuple([round(imag.getpixel((x ,y + 1))[0] + (querror*5/16)),round(imag.getpixel((x ,y + 1))[1] + (querror*5/16)),round(imag.getpixel((x ,y + 1))[2] + (querror*5/16)),255]))
imag.putpixel((x + 1,y + 1),tuple([round(imag.getpixel((x + 1,y + 1))[0] + (querror*1/16)),round(imag.getpixel((x + 1,y + 1))[1] + (querror*1/16)),round(imag.getpixel((x + 1,y + 1))[2] + (querror*1/16)),255]))
imag.show()

Related

How i can shorten code for neural-network?

I wrote a neural network and after that, i want to write neural network bigger than it, and i dont want to write too big code
input[0] = (player_x - Game_logic.block_1x)
input[1] = (player_y - Game_logic.block_1y)
input[2] = (player_x - Game_logic.block_2x)
input[3] = (player_y - Game_logic.block_2y)
input[4] = (player_x - Game_logic.block_3x)
input[5] = (player_y - Game_logic.block_3y)
snk[0] = (((input[0] * weights[0])/10) + ((input[1] * weights[1])/10) + ((input[2] * weights[2])/10) + ((input[3] * weights[3])/10) + ((input[4] * weights[4])/10) + ((input[5] * weights[5])/10) +1)
snk[1] = (((input[0] * weights[6])/10) + ((input[1] * weights[7])/10) + ((input[2] * weights[8])/10) + ((input[3] * weights[9])/10) + ((input[4] * weights[10])/10) + ((input[5] * weights[11])/10) +1)
snk[2] = (((input[0] * weights[12])/10) + ((input[1] * weights[13])/10) + ((input[2] * weights[14])/10) + ((input[3] * weights[15])/10) + ((input[4] * weights[16])/10) + ((input[5] * weights[17])/10) +1)
snk[3] = (((input[0] * weights[18])/10) + ((input[1] * weights[19])/10) + ((input[2] * weights[20])/10) + ((input[3] * weights[21])/10) + ((input[4] * weights[22])/10) + ((input[5] * weights[23])/10) +1)
snk[4] = (((input[0] * weights[24])/10) + ((input[1] * weights[25])/10) + ((input[2] * weights[26])/10) + ((input[3] * weights[27])/10) + ((input[4] * weights[28])/10) + ((input[5] * weights[29])/10) +1)
output[0] = ((snake_layer_1[0] * weights[30])/10) + ((snake_layer_1[1] * weights[31])/10) + ((snake_layer_1[2] * weights[32])/10) + ((snake_layer_1[3] * weights[33])/10) + ((snake_layer_1[4] * weights[34])/10)
output[1] = ((snake_layer_1[0] * weights[35])/10) + ((snake_layer_1[1] * weights[36])/10) + ((snake_layer_1[2] * weights[37])/10) + ((snake_layer_1[3] * weights[38])/10) + ((snake_layer_1[4] * weights[39])/10)
output[3] = ((snake_layer_1[0] * weights[40])/10) + ((snake_layer_1[1] * weights[41])/10) + ((snake_layer_1[2] * weights[42])/10) + ((snake_layer_1[3] * weights[43])/10) + ((snake_layer_1[4] * weights[44])/10)

Unable to retrieve attribute 'objval'

Hoping someone could give me a hand, I am having issues retrieving an optimal value for my code as shown below.
I have found that the status code is 2 which indicates it can be solved and there is an optimal value, I am just unsure why it cannot give me that value.
Please Help, this is a big assignment & I am struggling a lot!.
`mod = gp.Model('Gladstone')
mod.setParam('LogToConsole', 0)
mod.setParam('OutputFlag', 0)
N_investors = 2000
#Add variables
#cost of telephone for each group
t = {1: 15, 2:12, 3:20, 4:18}
#cost of personal for each group
p = {1: 35, 2:30, 3:50, 4:40}
group = [1,2,3,4]
x = mod.addVars(groups, lb=0, vtype=GRB.CONTINUOUS, name = 'personal_group') #number of people contacted in person in group i
y = mod.addVars(groups, lb=0, vtype=GRB.CONTINUOUS, name = 'telephone_group')#number of people contacted in telephone in group i
#Constraints
mod.addConstr((gp.quicksum(x[i] + y[i])for i in groups) == N_investors)
mod.addConstr((x[1] + x[2] + y[1] + y[2]) >= (N_investors))
mod.addConstr((x[1] + x[2] + x[3] + x[4]) >= (0.25 * N_investors))
mod.addConstr((x[1] + x[2] + x[3] + x[4]) >= (0.5* (x[1] + y[1])))
mod.addConstr((x[2] + y[2] + x[4] + y[4]) <=(0.4 * N_investors))
mod.addConstr((x[1] + x[2] + x[3] + x[4] <= (0.25 * (x[2] + y[2] + x[4] + y[4]))))
mod.addConstr((x[1] + y[1]) >= (0.1 * N_investors))
mod.addConstr(((x[2] + y[2])) >= (0.1 * N_investors))
mod.addConstr(((x[3] + y[3])) >= (0.1 * N_investors))
mod.addConstr(((x[4] + y[4])) >= (0.1 * N_investors))
mod.addConstr(((x[1] + y[1])) <= (0.5 * N_investors))
mod.addConstr(((x[2] + y[2])) <= (0.5 * N_investors))
mod.addConstr(((x[3] + y[3])) <= (0.5 * N_investors))
mod.addConstr(((x[4] + y[4])) <= (0.5 * N_investors))
#Objective
costs = (t[1] * y[1]) + (t[2] * y[2]) + (t[3] * y[3]) + (t[4] * y[4]) + (p[1] * x[1]) + (p[2] * x[2]) + (p[3] * x[3]) + (p[4] * x[4])
mod.setObjective(costs, sense = GRB.MINIMIZE)
mod.update()
mod.optimize()
print(mod.objval)`

How to convert symbolic expressions to float in Python?

Now i m trying to solve 6th order of nonlinear equations.
For solving this problem, 'fsolve' is the best module for my situation.
But i have a problem for using this 'fsolve'.
My Equations below : eq1, eq2, eq3, eq4, eq5, eq6
U = (E*h/32)*(pi**4*K3+8*pi**2*K4+16*J2-pi**4/b*K1**2-8*pi**2/b*K1*J1)+pi**2/2*D*((K1*K2)**0.5+(1-v)*K5-v*K6)+F/(4*b)*pi**2*K1
eq1 = diff(U,b_1)
eq2 = diff(U,b_2)
eq3 = diff(U,b_3)
eq4 = diff(U,b_4)
eq5 = diff(U,b_5)
eq6 = diff(U,b_6)
Now i m gonna try to define functions:
def functions(v):
b_1 = v[0]
b_2 = v[1]
b_3 = v[2]
b_4 = v[3]
b_5 = v[4]
b_6 = v[5]
return eq1,eq2,eq3,eq4,eq5,eq6
Until now, all of code is perfectly completed.
But next, I got a error code for 'fsolve'
x0 = [0.1,0.1,0.1,0.1,0.1,0.1]
solutions = fsolve(functions,x0)
Traceback (most recent call last):
File "C:\Users\user\AppData\Roaming\Python\Python37\site-packages\sympy\core\expr.py", line 327, in __float__
raise TypeError("can't convert expression to float")
TypeError: can't convert expression to float
Traceback (most recent call last):
File "C:\Users\user\Desktop\-----\trial.py", line 97, in <module>
solutions = fsolve(functions,x0)
File "C:\Users\user\anaconda3\lib\site-packages\scipy\optimize\minpack.py", line 147, in fsolve
res = _root_hybr(func, x0, args, jac=fprime, **options)
File "C:\Users\user\anaconda3\lib\site-packages\scipy\optimize\minpack.py", line 225, in _root_hybr
ml, mu, epsfcn, factor, diag)
error: Result from function call is not a proper array of floats.
Actually i don't know exactly meaning about 'array of floats'.
The eq1~eq6 is really complicated expressions. For example (eq1):
519749583.393768*b_1**3 + 519749583.393768*b_1**2*b_2 + 311849750.036261*b_1**2*b_3 + 222749821.454472*b_1**2*b_4 + 173249861.131256*b_1**2*b_5 + 141749886.380119*b_1**2*b_6 + 589049527.846271*b_1*b_2**2 + 920699262.011818*b_1*b_2*b_3 + 742499404.84824*b_1*b_2*b_4 + 619499503.439037*b_1*b_2*b_5 + 530653420.807624*b_1*b_2*b_6 + 395009683.379264*b_1*b_3**2 + 672299461.117134*b_1*b_3*b_4 + 581053380.409443*b_1*b_3*b_5 + 510299590.968427*b_1*b_3*b_6 + 296183828.527375*b_1*b_4**2 + 524699579.42609*b_1*b_4*b_5 + 469323153.224931*b_1*b_4*b_6 + 236661575.009363*b_1*b_5**2 + 429394392.660243*b_1*b_5*b_6 + 196977114.839905*b_1*b_6**2 + 39642.9110110422*b_1 + 193049845.260542*b_2**3 + 482129613.548124*b_2**2*b_3 + 406949673.808743*b_2**2*b_4 + 350618949.729969*b_2**2*b_5 + 307488215.070719*b_2**2*b_6 + 420149663.228267*b_2*b_3**2 + 732095017.5835*b_2*b_3*b_4 + 645437944.186494*b_2*b_3*b_5 + 575987321.121736*b_2*b_3*b_6 + 326318969.207663*b_2*b_4**2 + 585464236.60242*b_2*b_4*b_5 + 529609601.687166*b_2*b_4*b_6 + 266189415.118358*b_2*b_5**2 + 486862510.0121*b_2*b_5*b_6 + 224616040.694387*b_2*b_6**2 - 29192.3833775011*b_2 + 125272976.510293*b_3**3 + 334349732.001359*b_3**2*b_4 + 299655189.675087*b_3**2*b_5 + 270932940.727941*b_3**2*b_6 + 302393649.018531*b_3*b_4**2 + 549176644.826696*b_3*b_4*b_5 + 501838635.183477*b_3*b_4*b_6 + 252001834.206563*b_3*b_5**2 + 464613445.578031*b_3*b_5*b_6 + 215729827.081362*b_3*b_6**2 - 28558.195613361*b_3 + 92286363.2746599*b_4**3 + 253972873.350655*b_4**2*b_5 + 234063996.922265*b_4**2*b_6 + 234970783.525745*b_4*b_5**2 + 436229928.976872*b_4*b_5*b_6 + 203716223.26551*b_4*b_6**2 - 24903.5955817128*b_4 + 72968765.0411644*b_5**3 + 204424758.743579*b_5**2*b_6 + 191897759.069662*b_5*b_6**2 - 21706.6337724143*b_5 + 60314467.7838601*b_6**3 - 19178.52782655*b_6 + 52.9828871992195*((35.720610813872*b_2**2 + 142.882443255488*b_2*b_3 + 214.323664883232*b_2*b_4 + 285.764886510976*b_2*b_5 + 357.20610813872*b_2*b_6 + 257.188397859878*b_3**2 + 918.529992356708*b_3*b_4 + 1333.56947038455*b_3*b_5 + 1753.55725813553*b_3*b_6 + 893.0152703468*b_4**2 + 2727.7557348775*b_4*b_5 + 3709.44804605594*b_4*b_6 + 2154.22760600582*b_5**2 + 6001.0626167305*b_5*b_6 + 4254.9551116524*b_6**2)*(0.482*b_1**2 + 0.321333333333333*b_1*b_2 + 0.1928*b_1*b_3 + 0.137714285714286*b_1*b_4 + 0.107111111111111*b_1*b_5 + 0.0876363636363637*b_1*b_6 + 0.0964*b_2**2 + 0.137714285714286*b_2*b_3 + 0.107111111111111*b_2*b_4 + 0.0876363636363637*b_2*b_5 + 0.0741538461538462*b_2*b_6 + 0.0535555555555556*b_3**2 + 0.0876363636363637*b_3*b_4 + 0.0741538461538462*b_3*b_5 + 0.0642666666666667*b_3*b_6 + 0.0370769230769231*b_4**2 + 0.0642666666666667*b_4*b_5 + 0.0567058823529412*b_4*b_6 + 0.0283529411764706*b_5**2 + 0.0507368421052632*b_5*b_6 + 0.022952380952381*b_6**2))**0.5*(0.964*b_1 + 0.321333333333333*b_2 + 0.1928*b_3 + 0.137714285714286*b_4 + 0.107111111111111*b_5 + 0.0876363636363637*b_6)/(0.482*b_1**2 + 0.321333333333333*b_1*b_2 + 0.1928*b_1*b_3 + 0.137714285714286*b_1*b_4 + 0.107111111111111*b_1*b_5 + 0.0876363636363637*b_1*b_6 + 0.0964*b_2**2 + 0.137714285714286*b_2*b_3 + 0.107111111111111*b_2*b_4 + 0.0876363636363637*b_2*b_5 + 0.0741538461538462*b_2*b_6 + 0.0535555555555556*b_3**2 + 0.0876363636363637*b_3*b_4 + 0.0741538461538462*b_3*b_5 + 0.0642666666666667*b_3*b_6 + 0.0370769230769231*b_4**2 + 0.0642666666666667*b_4*b_5 + 0.0567058823529412*b_4*b_6 + 0.0283529411764706*b_5**2 + 0.0507368421052632*b_5*b_6 + 0.022952380952381*b_6**2) + (-1.28102213260338e-7*b_2 - 3.20255533150846e-8*b_3 - 3.20255533150846e-8*b_4 - 1.60127766575423e-8*b_6)*(0.482*b_1**2 + 0.321333333333333*b_1*b_2 + 0.1928*b_1*b_3 + 0.137714285714286*b_1*b_4 + 0.107111111111111*b_1*b_5 + 0.0876363636363637*b_1*b_6 + 0.0964*b_2**2 + 0.137714285714286*b_2*b_3 + 0.107111111111111*b_2*b_4 + 0.0876363636363637*b_2*b_5 + 0.0741538461538462*b_2*b_6 + 0.0535555555555556*b_3**2 + 0.0876363636363637*b_3*b_4 + 0.0741538461538462*b_3*b_5 + 0.0642666666666667*b_3*b_6 + 0.0370769230769231*b_4**2 + 0.0642666666666667*b_4*b_5 + 0.0567058823529412*b_4*b_6 + 0.0283529411764706*b_5**2 + 0.0507368421052632*b_5*b_6 + 0.022952380952381*b_6**2) + (1.18477228028269e-9*b_1 + 6.14508444130024e-10*b_2 + 4.60881333097518e-10*b_3 + 1.53627111032506e-10*b_4 + 1.53627111032506e-10*b_5)*(78.9568352087149*b_1**2 + 52.6378901391433*b_1*b_2 + 31.582734083486*b_1*b_3 + 22.5590957739185*b_1*b_4 + 17.5459633797144*b_1*b_5 + 14.3557882197663*b_1*b_6 + 15.791367041743*b_2**2 + 22.5590957739185*b_2*b_3 + 17.5459633797144*b_2*b_4 + 14.3557882197663*b_2*b_5 + 12.1472054167254*b_2*b_6 + 8.77298168985721*b_3**2 + 14.3557882197663*b_3*b_4 + 12.1472054167254*b_3*b_5 + 10.5275780278287*b_3*b_6 + 6.07360270836269*b_4**2 + 10.5275780278287*b_4*b_5 + 9.2890394363194*b_4*b_6 + 4.6445197181597*b_5**2 + 8.31124581144368*b_5*b_6 + 3.75984929565309*b_6**2) + (0.964*b_1 + 0.321333333333333*b_2 + 0.1928*b_3 + 0.137714285714286*b_4 + 0.107111111111111*b_5 + 0.0876363636363637*b_6)*(-1.28102213260338e-7*b_1*b_2 - 3.20255533150846e-8*b_1*b_3 - 3.20255533150846e-8*b_1*b_4 - 1.60127766575423e-8*b_1*b_6 - 1.60127766575423e-8*b_2**2 - 3.20255533150846e-8*b_2*b_3 - 1.60127766575423e-8*b_2*b_5 - 8.00638832877115e-9*b_3**2 + 1.60127766575423e-8*b_3*b_4 - 8.00638832877115e-9*b_3*b_6 - 8.00638832877115e-9*b_4*b_5 + 8.00638832877115e-9*b_4*b_6 + 4.00319416438558e-9*b_5**2 - 8.00638832877115e-9*b_5*b_6 + 4.00319416438558e-9*b_6**2 + 41123.3516712057) - 129937395.848442*(4*b_1 + 1.33333333333333*b_2 + 0.8*b_3 + 0.571428571428572*b_4 + 0.444444444444445*b_5 + 0.363636363636364*b_6)*(b_1**2 + 0.666666666666667*b_1*b_2 + 0.4*b_1*b_3 + 0.285714285714286*b_1*b_4 + 0.222222222222222*b_1*b_5 + 0.181818181818182*b_1*b_6 + 0.2*b_2**2 + 0.285714285714286*b_2*b_3 + 0.222222222222222*b_2*b_4 + 0.181818181818182*b_2*b_5 + 0.153846153846154*b_2*b_6 + 0.111111111111111*b_3**2 + 0.181818181818182*b_3*b_4 + 0.153846153846154*b_3*b_5 + 0.133333333333333*b_3*b_6 + 0.076923076923077*b_4**2 + 0.133333333333333*b_4*b_5 + 0.117647058823529*b_4*b_6 + 0.0588235294117647*b_5**2 + 0.105263157894737*b_5*b_6 + 0.0476190476190477*b_6**2) + (157.91367041743*b_1 + 52.6378901391433*b_2 + 31.582734083486*b_3 + 22.5590957739185*b_4 + 17.5459633797144*b_5 + 14.3557882197663*b_6)*(5.92386140141343e-10*b_1**2 + 6.14508444130024e-10*b_1*b_2 + 4.60881333097518e-10*b_1*b_3 + 1.53627111032506e-10*b_1*b_4 + 1.53627111032506e-10*b_1*b_5 + 2.30440666548759e-10*b_2**2 + 1.53627111032506e-10*b_2*b_3 + 1.53627111032506e-10*b_2*b_4 + 1.53627111032506e-10*b_2*b_6 + 7.6813555516253e-11*b_3**2 - 7.6813555516253e-11*b_3*b_4 + 7.6813555516253e-11*b_3*b_6 + 7.6813555516253e-11*b_4*b_5 + 7.6813555516253e-11*b_4*b_6 + 3.84067777581265e-11*b_5**2 - 251.041666666667)
I must solve such theses 6 equations.
But " Result from function call is not a proper array of floats. " error message comes out.
Is there anyone who help my code?
I'm Python beginning, so Plz understane me

Avoiding value Error into fsolve from scipy.optimize

I'm trying to solve a long block of equations from an EES implementation using the scipy.optimze.fsolve. But in this block of equations there are CoolProp calls that have a range of validation, and sometimes it yields ValueError. I want to know if there is a strategy to avoid ValueError and let fsolve try another guesses.
This is my code:
def block1(x):
def cp_gas(Ti, Tj):
return (1000/(Tj - Ti)*(x[6]*1.25 + x[1]*(0.45 *(((Tj + 273.15)/1000)
-((Ti + 273.15)/1000)) + 1.67*(((Tj + 273.15)/1000)**2 - ((Ti + 273.15)/1000)**2)/2
- 1.27*(((Tj + 273.15)/1000)**3 - ((Ti + 273.15)/1000)**3)/3
+ 0.39*(((Tj + 273.15)/1000)**4 - ((Ti + 273.15)/1000)**4)/4)
+ x[2]*(1.79 *(((Tj + 273.15)/1000) - ((Ti + 273.15)/1000))
+ 0.107*(((Tj + 273.15)/1000)**2 - ((Ti + 273.15)/1000)**2)/2
+ 0.586*(((Tj + 273.15)/1000)**3 - ((Ti + 273.15)/1000)**3)/3
- 0.2*(((Tj + 273.15)/1000)**4 -((Ti + 273.15)/1000)**4)/4)
+ x[3]*(1.11*(((Tj + 273.15)/1000) - ((Ti + 273.15)/1000))
- 0.48*(((Tj + 273.15)/1000)**2 - ((Ti + 273.15)/1000)**2)/2
+ 0.96*(((Tj + 273.15)/1000)**3 - ((Ti + 273.15)/1000)**3)/3
- 0.42*(((Tj + 273.15)/1000)**4 - ((Ti + 273.15)/1000)**4)/4)
+ x[4]*(0.88*(((Tj + 273.15)/1000) - ((Ti + 273.15)/1000))
- 0.0001*(((Tj + 273.15)/1000)**2 - ((Ti + 273.15)/1000)**2)/2
+ 0.54*(((Tj + 273.15)/1000)**3 - ((Ti + 273.15)/1000)**3)/3
- 0.33*(((Tj + 273.15)/1000)**4 - ((Ti + 273.15)/1000)**4)/4)
+ x[5]*(0.37*(((Tj + 273.15)/1000) - ((Ti + 273.15)/1000))
+ 1.05*(((Tj + 273.15)/1000)**2 - ((Ti + 273.15)/1000)**2)/2
- 0.77*(((Tj + 273.15)/1000)**3 - ((Ti + 273.15)/1000)**3)/3
+ 0.21*(((Tj + 273.15)/1000)**4 - ((Ti + 273.15)/1000)**4)/4)))
f = np.zeros(26)
# x[24] = T_out_vent
f[0] = x[0] - cp_gas(T0, Tgas5)
f[1] = m_gas_teoria_conferindo*x[8] - x[9]*0.8 - x[7]
f[2] = x[10] + x[8] - (x[7] + x[9]*0.8)
f[3] = x[9] - x[8]*Z
f[4] = x[12] + x[13] + x[14] + x[15] + x[16] + x[17] - x[11]
f[5] = x[12] - M_CO2*x[8]/x[7]
f[6] = x[13] - M_H2O*x[8]/x[7]
f[7] = x[14] - M_N2*x[8]/x[7]
f[8] = x[15] - M_O2*x[8]/x[7]
f[9] = x[16] - M_SO2*x[8]/x[7]
f[10] = x[17] - (M_Cz*x[8] - 0.8*x[9])/x[7]
f[11] = x[18] - (e*a*((1-omega_ar) + 3.76*(1-omega_ar) + omega_ar)*(MM_ar_CBG)/(MM_CBG)*x[19])
f[12] = x[1] - ((m_gas5-x[7])*FM_g_CO2+x[7]*x[12])/(x[7]*x[11]+(m_gas5-x[7])*FM_g)
f[13] = x[2] - ((m_gas5-x[7])*FM_g_H2O+x[7]*x[13])/(x[7]*x[11]+(m_gas5-x[7])*FM_g)
f[14] = x[3] - ((m_gas5-x[7])*FM_g_N2+x[7]*x[14])/(x[7]*x[11]+(m_gas5-x[7])*FM_g)
f[15] = x[4] - ((m_gas5-x[7])*FM_g_O2+x[7]*x[15])/(x[7]*x[11]+(m_gas5-x[7])*FM_g)
f[16] = x[5] - (x[7]*x[16])/(x[7]*x[11]+(m_gas5-x[7])*FM_g)
f[17] = x[6] - (x[7]*x[17])/(x[7]*x[11]+(m_gas5-x[7])*FM_g)
f[18] = x[20] - x[21]/rho_ar_in
f[19] = (1/3600)*x[21] - (x[10]+x[18])
f[20] = ((x[10]+x[18])*h_in_vent + x[22]) - (x[10] + x[18])*x[23]
f[21] = x[23] - HAPropsSI('H', 'T', x[24] + 273.15, 'P', P_out_vent*1e3, 'W', omega_ar)/1e3
f[22] = x[22] - (0.000012523*x[20] + 0.054570445)
f[23] = x[25] - HAPropsSI('C', 'T', x[24] + 273.15, 'P', P_out_vent*1e3, 'W', omega_ar)/1e3
f[24] = m_gas5 - (x[7]+x[19]+x[18])
f[25] = eta_total - ((m_gas5*x[0]*(Tgas5-T0) - (x[10]+x[18])*x[25]*(x[24]-T0))
/(x[8]*PCI_RSU + x[19]*PCI_CBG))
return f
x = fsolve(block1, np.ones(26))
The code yields ValueError depending on constant values that are previously defined.
ValueError example:
ValueError: The output for key (8) with value (-nan) is outside the range of validity: (0) to (0.94145) :: inputs were:"H","T",1.3025950731911414e+02,"P",2.0132500000000000e+05,"W",1.0890000000000000e-02
If anyone can help me I will be grateful.
Thank in advance
The function you are running doesn't handle NaN values.
You can use try/except blocks to deal with it.
Or change the NaN values to a 0 (or any suitable number of your choice).
Here is a toy example to help you fix your code. You have to decide what should be the correct behavior and use one of the proposed strategies to deal with NaNs.
import bumpy as np
def f(x):
if np.isnan(x):
raise ValueError('NaN is not supported')
return x**x
test_cases = [1, 2, 3, 4, np.nan, 6, 7]
print('skip in case of error')
for x in test_cases:
try:
print(f(x))
except ValueError:
pass
print()
print('fix X in case of NaN')
for x in test_cases:
if np.isnan(x):
x = 0
print(f(x))
Output:
skip in case of error
1
4
27
256
46656
823543
fix X in case of NaN
1
4
27
256
1
46656
823543

SyntaxError when running program

I have to make a calculator with argv.sys. When I run my code I keep getting this error:
>>> "C:\Users\admin\Desktop\uni\Informatik BW\assignment.py" + rect 0 0 10 10
File "<stdin>", line 1
"C:\Users\admin\Desktop\uni\Informatik BW\assignment.py" + rect 0 0 10 10
^
SyntaxError: invalid syntax
>>>
Here is my program:
import sys
import math
def area_rectangle(x,y,widht,height):
return (widht*height)
def xy_centroid_rectangle(x,y):
return (k + l * 0.5)
#def area_circle(x,y,r):
#return (r*r*math.pi)
#def xy_centroid_circle(k,r):
# return ((4 * r / 3 * math.pi) * 2)
#def area_half_circle(x,y,r):
# return (r * r * math.pi / 2)
#def xy_centroid_half_circle(k,r):
# return (4 * r / 3 * math.pi)
#def area_right_triangle(x,y,a,h):
# return (a * h / 2)
#def xy_centroid_right_triangle(k,l):
# return (a + h + math.sqrt((a * a) + (h * h)))
x = 0
y = 0
a = 0
fx = 0
fy = 0
f = 0
i = 1
while i < len(sys.argv):
vz = sys.argv[i]
print i
print vz
if sys.argv[i + 1] == "rect":
f = area_rectangle(float(sys.argv[i + 2]),float(sys.argv[i + 3]),float(sys.argv[i + 4]),float(sys.argv[i + 5]))
fx = xy_centroid_rectangle(float(sys.argv[i + 2]),float(sys.argv[i + 4]))
fy = xy_centroid_rectangle(float(sys.argv[i + 3]),float(sys.argv[i + 5]))
i += 6
#if sys.argv[i + 1] == "circ":
#f = area_circle(float(sys.argv[i + 2]),float(sys.argv[i + 3]),float(sys.argv[i + 4]))
#fx = xy_centroid_circle(foat(sys.argv[i + 2]),float(sys.argv[i + 4]))
#fy = xy_centroid_circle(foat(sys.argv[i + 3]),float(sys.argv[i + 4]))
#i += 5
#if sys.argv[i + 1] == "halfcirc":
#f = area_circle(float(sys.argv[i + 2]),float(sys.argv[i + 3]),float(sys.argv[i + 4]))
#fx = xy_centroid_circle(foat(sys.argv[i + 2]),float(sys.argv[i + 4]))
#fy = xy_centroid_circle(foat(sys.argv[i + 3]),float(sys.argv[i + 4]))
#i += 5
#if sys.argv[i + 1] == "righttri":
#f = area_rectangle(float(sys.argv[i + 2]),float(sys.argv[i + 3]),float(sys.argv[i + 4]),float(sys.argv[i + 5]))
#fx = xy_centroid_rectangle(float(sys.argv[i + 2]),float(sys.argv[i + 4]))
#fy = xy_centroid_rectangle(float(sys.argv[i + 3]),float(sys.argv[i + 5]))
#i += 6
if vz == "+":
x = (x * a + fx * f) / (a + f)
y = (y * a + fy * f) / (a + f)
a = a + f
if vz == "-":
x = (x * a - fx * f) / (a - f)
y = (y * a - fy * f) / (a - f)
a = a - f
print x
print y
print a
Why am I getting this error?
That's not how you run a python program. Open a CMD (Windows) prompt and write your command line there. You'll probably need to add python in front too.

Categories

Resources