用python编写数学公式 [英] Write math formula in python

查看:1999
本文介绍了用python编写数学公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要计算的值. 我写它如下,这里我是虚数.我正在使用python和networkx.有人可以帮忙.

This is the value I want to calculate. I am writing it as follows, here i is the imaginary number.I am using python and networkx.Can someone help.

import math
import matplotlib
matplotlib.use('TkAgg')
from pylab import *
import cmath
import networkx as nx
import random as rd
import numpy as np

g=nx.karate_club_graph()
for i in g.nodes_iter():
    g.node[i]['theta']=1

a=abs((cmath.exp( (g.node[i]['theta']*(1j)) for i in    g.nodes_iter()))/g.number_of_nodes())

我得到的错误输出是:

""文件",第1行,在 运行文件('C:/Users/Bhawesh/Desktop/workingdirectory/bookpractice.py',wdir ='C:/Users/Bhawesh/Desktop/workingdirectory')

The error output that I get is:

"""File "", line 1, in runfile('C:/Users/Bhawesh/Desktop/workingdirectory/bookpractice.py', wdir='C:/Users/Bhawesh/Desktop/workingdirectory')

文件"C:\ Users \ Bhawesh \ Anaconda2 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py",行866,在运行文件中 execfile(文件名,命名空间)

File "C:\Users\Bhawesh\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile execfile(filename, namespace)

execfile中的文件"C:\ Users \ Bhawesh \ Anaconda2 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py",第87行 exec(compile(scripttext,filename,'e​​xec'),glob,loc)

File "C:\Users\Bhawesh\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)

文件"C:/Users/Bhawesh/Desktop/workingdirectory/bookpractice.py",第18行,在 b = abs((cmath.exp((g.node [i] ['theta'] *(1j))for i in g.nodes_iter()))/g.number_of_nodes())

File "C:/Users/Bhawesh/Desktop/workingdirectory/bookpractice.py", line 18, in b=abs((cmath.exp( (g.node[i]['theta']*(1j)) for i in g.nodes_iter()))/g.number_of_nodes())

TypeError:必须为浮点数""

TypeError: a float is required """

推荐答案

Python的sqrt操作不支持负整数.但是,numpy适用于复数.虚数$ i $由Python中的1j给出.因此,您必须将方程式转换为:

Python's sqrt operation doesn't support negative integers. However, numpy works with complex numbers. The imaginary number $i$ is given by 1j in Python. So you will have to convert your equation to:

a=abs((np.exp( (g.node[i]['theta']*(1j)) for i in g.nodes_iter()))/g.number_of_nodes())

这篇关于用python编写数学公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆