如何匹配我的数组的数据类型以便在 PYTHON 中操作和绘制我的函数以及如何确保数组匹配长度?

How do I match the data type of my arrays in order to operate and graph my functions in PYTHON and how can I make sure the arrays match length?

大家好!

我想知道是否有人可以在我的代码中帮助我为我的图表创建域和范围?

我一直在两种类型的错误之间来回摇摆:

不能计算方程式,因为它必须采用一致的数据类型。我已经尝试过了,但是将它转换为浮点数是行不通的。也许我必须让一切都漂浮起来?

错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-145-222a5ee3c353> in <module>
    240     d = np.vectorize(np.arange(.5,1000.5,.5))
    241 
--> 242     a_3 = (2/math.sqrt(6))*d
    243     a_4 = (2/math.sqrt(6))*d
    244     a_5 = [4*(1/(math.sqrt(10+2*math.sqrt(5))))*((1/2)*d)]

TypeError: unsupported operand type(s) for *: 'float' and 'vectorize'

相关代码在这里(目的是比较和对比表面积与体积比与直径的关系,并输出用户对不同类型多面体询问的恒定直径信息):

代码:

    #Graph Code
    
    if error == '1':
            print("\n")
    else:
        # overwite past input by generated domain for graphing
        d_vertical_slash = d
        import matplotlib.pyplot as plt
        import numpy as np
        # x value generation from picometer to micrometer
        
     
        d = np.vectorize(np.zeros(2000))
        d = np.vectorize(np.arange(.5,1000.5,.5))
        
        a_3 = (2/math.sqrt(6))*d
        a_4 = (2/math.sqrt(6))*d
        a_5 = [4*(1/(math.sqrt(10+2*math.sqrt(5))))*((1/2)*d)]
        a_6 = [d/math.sqrt(3)]
        a_7 = (2/(math.sqrt(3)*(1+math.sqrt(5)))*d)
        a_8 = ((2/(math.sqrt(10+2*math.sqrt(5))))*d)
        a_9 = ((2/(math.sqrt(50+22*math.sqrt(5))))*d)
        a_10 = ((3/(math.sqrt(3)*(3+math.sqrt(5))))*d)
        a_11 = ((2/(math.sqrt(50+22*math.sqrt(5))))*d)
        SA_vex_tetra = (((a_3)**2)*math.sqrt(3))
        V_vex_tetra = ((((a_3)**3)/12)*math.sqrt(2))
        ratio_vex_tetra = [SA_vex_tetra/V_vex_tetra]
        SA_vex_octa = ((a_4)**2)*math.sqrt(3)
        V_vex_octa = (((a_4)**3)/12)*math.sqrt(2)
        ratio_vex_octa = SA_vex_octa/V_vex_octa
        SA_vex_icosa = 5*((a_5)**2)*math.sqrt(3)
        V_vex_icosa = (5/12)*(a_5)**3*(3+math.sqrt(5))
        ratio_vex_icosa = [SA_vex_icosa/V_vex_icosa]
        SA_vex_cube = SA_vex_cube, [6*(a_6)**2]
        V_vex_cube = [(a_6)**3]
        ratio_vex_cube = SA_vex_cube/V_vex_cube
        SA_vex_dodeca = 3*((a_7)**2)*math.sqrt(25+10*math.sqrt(5))
        V_vex_dodeca = (((a_7)**3)/4)*(15+7*math.sqrt(15))
        ratio_vex_dodeca = SA_vex_dodeca/V_vex_dodeca
        SA_cav_gdodeca = [15*((a_8)**2)*(math.sqrt(5-2*math.sqrt(5)))]
        V_cav_gdodeca = (5/4)*((a_6)**3)*(math.sqrt(5)-1)
        ratio_cav_gdodeca = SA_cav_gdodeca/V_cav_gdodeca
        SA_cav_gicosa = 3*((a_9)**2)*math.sqrt(3)*(5+4*math.sqrt(5))
        V_cav_gicosa = (((a_9)**3)/4)*(25+9*math.sqrt(5))
        ratio_cav_gicosa = SA_cav_gicosa/V_cav_gicosa
        SA_cav_gsdodeca = 15*((a_10)**2)*math.sqrt(5+2*math.sqrt(5))
        V_cav_gsdodeca = (5/4)*((a_10)**3)*(3+math.sqrt(5))
        ratio_cav_gsdodeca = SA_cav_gsdodeca/V_cav_gsdodeca
        SA_cav_ssdodeca = 15*((a_11)**2)*math.sqrt(5+2*math.sqrt(5))
        V_cav_ssdodeca = (5/4)*((a_11)**3)*(7+3*math.sqrt(5))      
        ratio_cav_ssdodeca = SA_cav_ssdodeca/V_cav_ssdodeca
            
    
        # convex shapes
        plt.plot(d,ratio_vex_tetra,label='Tetrahedron', color='teal')
        plt.plot(d,ratio_vex_octa,label='Octahedron', color='paleturquoise')
        plt.plot(d,ratio_vex_icosa,label='Icosahedron', color='aqua')
        plt.plot(d,ratio_vex_cube,label='Cube', color='dodgerblue')
        plt.plot(d,ratio_vex_dodeca,label='Dodecahedron', color='paleturquoise')
    
        # concave shapes
        plt.plot(d,ratio_cav_gdodeca,label='Great Dodecahedron', color='green')
        plt.plot(d,ratio_cav_gicosa,label='Great Icosahedron', color='lime')
        plt.plot(d,ratio_cav_gsdodeca,label='Great-Stellated Dodecahedron', color='palegreen')
        plt.plot(d,ratio_cav_ssdodeca,label='Small-Stellated Dodecahedron', color='honeydew')
    
        # sphere reference and chosen reference of diameter
        plt.plot(d,ratio_sphere,label='Referential Sphere', color='r')
        plt.axvline(x=d_vertical_slash, color='b')
    
        # title, labels, grid (for tracing), legend, and output
        plt.title('Comparison of SA:V amongst Concave/Convex Polyhedra as Diameter is held constant across picometer to micrometer range')
        plt.xlabel('Circumspherical Diameter/Diagonal (nm)')
        plt.ylabel('Ratio Index')
    
        plt.grid(alpha=.4,linestyle='--')
    
        plt.legend()
        plt.show()
    
        #Concluding message
        print('Thanks for taking the time to look through!')
        print('I hope this shed some light on how important considering')
        print('other styles of 3D geometry is in nanoformulation!')
        print("\n")
        print('Programmed by Wesley Allen Williams')
The other error happens when I use numpy and I can't find conditions that will terminate the while loop when I want it to:

Aside: I also tried using np.arange to make a floating point array but it did not work...

ERROR:

Something along the lines of not being able to graph because the array is always mismatched in length or shape.... I always remembered it being something like (2001,) and (1, 2001000)...not sure why but it would always add three zeros to the "y" value... or it was truncated to short with my conditions...I got so many errors I didn't get all of them

Code:
        import math
            
            d = np.array([.001])
            
            a_3 = np.array([(2/math.sqrt(6))*d[0]])
            a_4 = np.array([(2/math.sqrt(6))*d[0]])
            a_5 = np.array([4*(1/(math.sqrt(10+2*math.sqrt(5))))*((1/2)*d[0])])
            a_6 = np.array([d[0]/math.sqrt(3)])
            a_7 = np.array([2/(math.sqrt(3)*(1+math.sqrt(5)))*d[0]])
            a_8 = np.array([(2/(math.sqrt(10+2*math.sqrt(5))))*d[0]])
            a_9 = np.array([(2/(math.sqrt(50+22*math.sqrt(5))))*d[0]])
            a_10 = np.array([(3/(math.sqrt(3)*(3+math.sqrt(5))))*d[0]])
            a_11 = np.array([(2/(math.sqrt(50+22*math.sqrt(5))))*d[0]])
            
            SA_vex_tetra = np.array([((a_3)**2)*math.sqrt(3)])
            V_vex_tetra = np.array([(((a_3)**3)/12)*math.sqrt(2)])
            ratio_vex_tetra = np.array([SA_vex_tetra/V_vex_tetra])
            
            SA_vex_octa = np.array([((a_4)**2)*math.sqrt(3)])
            V_vex_octa = np.array([(((a_4)**3)/12)*math.sqrt(2)])
            ratio_vex_octa = np.array([SA_vex_octa/V_vex_octa])
            
            SA_vex_icosa = np.array([5*((a_5)**2)*math.sqrt(3)])
            V_vex_icosa = np.array([(5/12)*(a_5)**3*(3+math.sqrt(5))])
            ratio_vex_icosa = np.array([SA_vex_icosa/V_vex_icosa])
            
            SA_vex_cube = np.array([6*(a_6)**2])
            V_vex_cube = np.array([(a_6)**3])
            ratio_vex_cube = np.array([SA_vex_cube/V_vex_cube])
            
            SA_vex_dodeca = np.array([3*((a_7)**2)*math.sqrt(25+10*math.sqrt(5))])
            V_vex_dodeca = np.array([(((a_7)**3)/4)*(15+7*math.sqrt(15))])
            ratio_vex_dodeca = np.array([SA_vex_dodeca/V_vex_dodeca])
        
            SA_cav_gdodeca = np.array([15*((a_8)**2)*(math.sqrt(5-2*math.sqrt(5)))])
            V_cav_gdodeca = np.array([(5/4)*((a_8)**3)*(math.sqrt(5)-1)])
            ratio_cav_gdodeca = np.array([SA_cav_gdodeca/V_cav_gdodeca])
        
            SA_cav_gicosa = np.array([3*((a_9)**2)*math.sqrt(3)*(5+4*math.sqrt(5))])
            V_cav_gicosa = np.array([(((a_9)**3)/4)*(25+9*math.sqrt(5))])
            ratio_cav_gicosa = np.array([SA_cav_gicosa/V_cav_gicosa])
        
            SA_cav_gsdodeca = np.array([15*((a_10)**2)*math.sqrt(5+2*math.sqrt(5))])
            V_cav_gsdodeca = np.array([(5/4)*((a_10)**3)*(3+math.sqrt(5))])
            ratio_cav_gsdodeca = np.array([SA_cav_gsdodeca/V_cav_gsdodeca])
        
            SA_cav_ssdodeca = np.array([15*((a_11)**2)*math.sqrt(5+2*math.sqrt(5))])
            V_cav_ssdodeca = np.array([(5/4)*((a_11)**3)*(7+3*math.sqrt(5))])
            ratio_cav_ssdodeca = np.array([SA_cav_ssdodeca/V_cav_ssdodeca])
            
            d_prime = d[0] + .499
            d = np.append(d, d_prime)
            i=1
            while d[i] <= 1000:
                a_3 = np.append(a_3, [(2/math.sqrt(6))*d[i]])
                a_4 = np.append(a_4, [(2/math.sqrt(6))*d[i]])
                a_5 = np.append(a_5, [4*(1/(math.sqrt(10+2*math.sqrt(5))))*((1/2)*d[i])])
                a_6 = np.append(a_6, [d[i]/math.sqrt(3)])
                a_7 = np.append(a_7, [2/(math.sqrt(3)*(1+math.sqrt(5)))*d[i]])
                a_8 = np.append(a_8, [(2/(math.sqrt(10+2*math.sqrt(5))))*d[i]])
                a_9 = np.append(a_9, [(2/(math.sqrt(50+22*math.sqrt(5))))*d[i]])
                a_10 = np.append(a_10, [(3/(math.sqrt(3)*(3+math.sqrt(5))))*d[i]])
                a_11 = np.append(a_11, [(2/(math.sqrt(50+22*math.sqrt(5))))*d[i]])
                SA_vex_tetra = np.append(SA_vex_tetra, [((a_3[i])**2)*math.sqrt(3)])
                V_vex_tetra = np.append(V_vex_tetra, [(((a_3[i])**3)/12)*math.sqrt(2)])
                ratio_vex_tetra = np.append(ratio_vex_tetra, [SA_vex_tetra/V_vex_tetra])
                SA_vex_octa = np.append(SA_vex_octa, [((a_4[i])**2)*math.sqrt(3)])
                V_vex_octa = np.append(V_vex_octa, [(((a_4[i])**3)/12)*math.sqrt(2)])
                ratio_vex_octa = np.append(ratio_vex_octa, [SA_vex_octa/V_vex_octa])
                SA_vex_icosa = np.append(SA_vex_icosa, [5*((a_5[i])**2)*math.sqrt(3)])
                V_vex_icosa = np.append(V_vex_icosa, [(5/12)*(a_5[i])**3*(3+math.sqrt(5))])
                ratio_vex_icosa = np.append(ratio_vex_icosa, [SA_vex_icosa/V_vex_icosa])
                SA_vex_cube = np.append(SA_vex_cube, [6*(a_6[i])**2])
                V_vex_cube = np.append(V_vex_cube, [(a_6[i])**3])
                ratio_vex_cube = np.append(ratio_vex_cube, [SA_vex_cube/V_vex_cube])
                SA_vex_dodeca = np.append(SA_vex_dodeca, [3*((a_7[i])**2)*math.sqrt(25+10*math.sqrt(5))])
                V_vex_dodeca = np.append(V_vex_dodeca, [(((a_7[i])**3)/4)*(15+7*math.sqrt(15))])
                ratio_vex_dodeca = np.append(ratio_vex_dodeca, [SA_vex_dodeca/V_vex_dodeca])
                SA_cav_gdodeca = np.append(SA_cav_gdodeca, [15*((a_8[i])**2)*(math.sqrt(5-2*math.sqrt(5)))])
                V_cav_gdodeca = np.append(V_cav_gdodeca, [(5/4)*((a_8[i])**3)*(math.sqrt(5)-1)])
                ratio_cav_gdodeca = np.append(ratio_cav_gdodeca, [SA_cav_gdodeca/V_cav_gdodeca])
                SA_cav_gicosa = np.append(SA_cav_gicosa, [3*((a_9[i])**2)*math.sqrt(3)*(5+4*math.sqrt(5))])
                V_cav_gicosa = np.append(V_cav_gicosa, [(((a_9[i])**3)/4)*(25+9*math.sqrt(5))])
                ratio_cav_gicosa = np.append(ratio_cav_gicosa, [SA_cav_gicosa/V_cav_gicosa])
                SA_cav_gsdodeca = np.append(SA_cav_gsdodeca, [15*((a_10[i])**2)*math.sqrt(5+2*math.sqrt(5))])
                V_cav_gsdodeca = np.append(V_cav_gsdodeca, [(5/4)*((a_10[i])**3)*(3+math.sqrt(5))])
                ratio_cav_gsdodeca = np.append(ratio_cav_gsdodeca, [SA_cav_gsdodeca/V_cav_gsdodeca])
                SA_cav_ssdodeca = np.append(SA_cav_ssdodeca, [15*((a_11[i])**2)*math.sqrt(5+2*math.sqrt(5))])
                V_cav_ssdodeca = np.append(V_cav_ssdodeca, [(5/4)*((a_11[i])**3)*(7+3*math.sqrt(5))])            
                ratio_cav_ssdodeca = np.append(ratio_cav_ssdodeca, [SA_cav_ssdodeca/V_cav_ssdodeca])
                d_i_plus_one = d[i] + d_prime
                d = np.append(d, d_i_plus_one)
                i+=1
                if d[i] > 1000 or d.size > 2001:
                    break

结论:

我希望能够成功绘制这些曲线,以比较基于凸性和凹性的不同多面体的 SA:V 比率...我想这对我来说太雄心勃勃了,因为我刚开始编码三天以前……对我放轻松!我知道这看起来非常无能,如果不够彻底,我深表歉意。

还有:

是否可以构建一个从(.001 到 1000,步长为 .001)的循环?我不希望用户输入太有限,但我做的 while 循环最终有太多的迭代。

非常感谢,如果你能解析这个:/...

@fdireito 回答及回复如下:

@WesleyAllenWilliams 这真令人惊讶。抱歉坚持,但你确定你没有打错字吗?您可以使用以下代码逐行创建一个单独的文件 import math , import numpy as np , d = np.arange(.5,1000.5,.5), a_3 = (2/math.sqrt(6))*d, print(d), print(a_3) (从这里逐行复制粘贴)。这对我有用。试试看你是否得到了 d 和 a_3 以及你想要的值。 – fdireito 13 小时前

@WesleyAllenWilliams 那应该给出 d:[5.000e-01 1.000e+00 1.500e+00 ... 9.990e+02 9.995e+02 1.000e+03] 和 a_3 [4.08248290e -01 8.16496581e-01 1.22474487e+00 ... 8.15680084e+02 8.16088333e+02 8.16496581e+02] – fdireito 13 小时前

@fdireito 效果非常好!问题是 Numpy 不喜欢某些方程用括号构建的方式,并且想要简化。接下来,我不得不 运行 通过公式拼写错误,我最终为我的百万个数据点使用了散点图,因为即使匹配的数组长度也不会绘制。然后我不得不简化图例的命令,因为它使我的 CPU 过度工作......然后我不得不将数据集 t运行 分类为 10,000 点并将域绘制为对数函数可视化变化。我的假设成立伙计!谢谢!!! – Wesley Allen Williams 6 分钟前删除