I am trying to vectorize the Brown function, which is evaluated over the range (-1,4). S
(vectorized) and sigma
(iterative) should therefore be equal, which they are for most input arrays I have tested, i.e. the commented a
, but not for the uncommented a
, where the difference is quite large (-40960). Since the function implementations are equivalent as far as I know, I cannot figure out why the results are different. Any help would be much appreciated!
import numpy as np
from decimal import Decimal
np.set_printoptions(precision=100)
D = 90
a = np.array([
-0.8195813369795628 , 2.1078160649038598 ,
0.5728485376392076 , 0.19214447077132535 ,
2.01979863827681 , 1.861016566323948 ,
0.03570569401002843 , 1.2179443346030103 ,
-0.2023148152997174 , 2.344402437732328 ,
0.3992427517339374 , -0.580084584056455 ,
2.8553419396152955 , -0.9053166646802753 ,
2.4078276437008532 , 1.769295277018315 ,
0.05839942222762762 , 0.1594092290921887 ,
0.7285414841081187 , -0.62168457722208 ,
1.1889002818073933 , 0.6524303072620927 ,
3.3633879172407637 , 3.0769408543464998 ,
-0.45519320493350635 , 0.43102403540370915 ,
-0.9252201822835948 , 3.12911155641771 ,
2.302579839847708 , 3.16984716563936 ,
1.3127842651843633 , 0.20958492282551844 ,
3.981729076545438 , 1.0909855253127345 ,
3.9449203415891487 , 1.3493337542750858 ,
0.045652220005763855, 3.9876425656088075 ,
2.348089357316039 , 3.556978916005656 ,
1.7058246955468757 , -0.1903975039419017 ,
-0.8527195886839736 , 3.943660607246864 ,
3.6468743314466314 , 1.369093942433826 ,
3.075116242479504 , 2.334122427494658 ,
3.2895527223960297 , 0.14290418230167345 ,
0.4959100263489993 , 2.9460988192449147 ,
1.8885423993409554 , 3.003770472482533 ,
0.8120286831579513 , -0.0938573811789214 ,
3.316954973064636 , 2.776219448028004 ,
3.976149157953796 , 3.836526854565294 ,
2.5311757286425833 , 0.7206181274066051 ,
-0.5537806875062783 , 0.3505280689662853 ,
1.1798421347136443 , -0.8201745245328302 ,
2.1829779508510687 , 1.6780163409792823 ,
1.3730944605318656 , 1.70487098007274 ,
-0.9772480011318625 , 3.8936145630943297 ,
2.208108667511484 , 1.3737024114348877 ,
3.607651563373506 , 2.032977481458486 ,
2.0759850609302424 , -0.39569125550256734 ,
2.1840048431472256 , -0.7307503352781088 ,
-0.2280889610541812 , 0.31422245330372567 ,
1.2536427131615615 , 2.9155338041621053 ,
-0.8296245906825948 , 2.6033094690850427 ,
1.628556602479812 , -0.6583839970597181 ,
3.2178860860566343 , -0.4291117469753949
])
# a = np.array([
# -0.24326079350064667, 1.9398461449036972 , 1.1797471464017137 ,
# 2.785342560109509 , 2.1256127620820333 , 2.17932485196713 ,
# -0.5056361946982487 , -0.6015891193965008 , 1.3604221619244674 ,
# 1.5906269601677465 , -0.5838110012848782 , -0.7794260647387554 ,
# 3.7414065063184694 , 3.908191088860524 , 1.7398922245943935 ,
# 0.2594518014543459 , 0.26853905876986417, 2.6513759153963536 ,
# 3.9300813391410507 , -0.22684113001034145, -0.9510375598056205 ,
# -0.6861883509333768 , -0.18193551587912327, 2.2606226238298963 ,
# 3.8876765949132253 , 0.35301727341430644, 3.2521698770948593 ,
# -0.3758906340542608 , -0.25640107769661824, -0.15220637767735046,
# 2.7460929984988507 , 0.33843737080084413, 1.8371601261136985 ,
# 0.12611175385637474, -0.650561948254508 , -0.4779901630615837 ,
# 2.0010286412998437 , 3.884377947986377 , 3.6300883926415874 ,
# 0.3356095899399807 , 1.2882384696767533 , 1.990009296808449 ,
# 2.5253383884760527 , 1.135461522592501 , 1.9495805928096308 ,
# 0.9083711321253394 , -0.23634761490005896, 1.7484076577378653 ,
# 3.6219200691205877 , 3.078817955667887 , 0.8501396510730472 ,
# 3.359302276017435 , 0.7731124557398252 , 1.8948667444885072 ,
# 2.6958910486918066 , 2.9209091072651847 , 3.4129582373243705 ,
# 1.5821807306506335 , 0.6762606433965022 , 3.8822452600542663 ,
# 3.818937414485327 , -0.20335236087550024, 1.823951066425706 ,
# 1.532365782386333 , 3.3698354058360334 , 3.5832883562938784 ,
# -0.8018961048636466 , 3.6581456199131264 , 3.521788869215107 ,
# -0.39667357751053645, 1.3733773721715767 , -0.07239496582275817,
# 3.905023791644104 , 3.625188362904372 , 3.7790115400299955 ,
# 3.020466900608116 , 3.658497613816361 , 2.9759327122014114 ,
# -0.7121009035990102 , 0.06379796314462038, 0.7256262413218697 ,
# 1.1444416096632541 , 2.281575941092746 , 2.4506770467922987 ,
# 1.5220049951348589 , -0.9653485342086856 , 3.8988630900360306 ,
# 2.17529438453628 , 3.723873897373978 , -0.49841970841454497
# ])
# Iterative calculation
z = a.copy()
sigma = 0
for i in range(D - 1):
base = z[i] ** 2
power = z[i + 1] ** 2 + 1
sigma = sigma + base ** power
base = z[i + 1] ** 2
power = z[i] ** 2 + 1
sigma = sigma + base ** power
print(f'Iterative sum: {Decimal(float(sigma))}')
# Vectorized calculation
z = a[np.newaxis,:]
S = 0
for d in range(D-1):
t1 = z[:,d]**2
t2 = z[:,d+1]**2
t3 = t1**(t2+1)
t4 = t2**(t1+1)
S += t3 + t4
print(f'Vectorized sum: {Decimal(float(S))}')
# Difference
print(f'Difference: {Decimal(float(sigma - S))}')