當前位置:首頁 » 編程語言 » 用python科學計算

用python科學計算

發布時間: 2022-10-03 11:00:02

『壹』 為什麼用python進行科學計算

不得不說這是一個形勢所趨,
現在高校的很多老師,及學生做科學計算相關的項目都是python。原因大約有以下幾點:
Python的語法簡單,這對很少接觸編程的搞學術老師的福音。
Python相較於其他語言有更豐富的模塊,比如科學計算的numpy。
Python越來越流行。

『貳』 python高級科學計算和python科學計算區別

等級和效果。
1、等級:python高級科學計算指的是高級的科學計算過程方式,而python科學計算指的是普通的計算的方式。
2、效果:python高級科學計算可以達到減少內存佔用,隨時生產,即時消費,不用堆積在內存當中。而python科學計算達不到這個效果。

『叄』 如何用 Python 科學計算中的矩陣替代循環

import time
import math
import numpy as np

x = [i * 0.001 for i in xrange(1000000)] # 初始化數組0.000~999.999
start = time.clock()
for i, t in enumerate(x): # 用循環計算正弦值
x[i] = math.sin(t)
print "math.sin:", time.clock() - start

x = [i * 0.001 for i in xrange(1000000)]
x = np.array(x) # 初始化矩陣(這里是一維)
start = time.clock()
np.sin(x,x) # numpy的廣播計算(代替循環)
print "numpy.sin:", time.clock() - start

# 輸出
# math.sin: 1.15426932753
# numpy.sin: 0.0882399858083

『肆』 如何用 Python 寫一個帶 GUI 的科學計算程序

使用Tkinter圖形庫,如果你是用的linux系統 記得將第一行改為from tkinter import *
這個代碼實現的挺簡單,並不是很復雜的科學計算器界面,你可以以此為基礎,添加自己想要的東西:給你個截圖:
代碼是如下, 我就不給你添注釋了啊:
#!/usr/bin/env python3.4
from Tkinter import *
import parser
root = Tk()
root.title('Calculator')
i = 0
def factorial():
"""Calculates the factorial of the number entered."""
whole_string = display.get()
number = int(whole_string)
fact = 1
counter = number
try:
while counter > 0:
fact = fact*counter
counter -= 1
clear_all()
display.insert(0, fact)
except Exception:
clear_all()
display.insert(0, "Error")
def clear_all():
"""clears all the content in the Entry widget"""
display.delete(0, END)
def get_variables(num):
"""Gets the user input for operands and puts it inside the entry widget"""
global i
display.insert(i, num)
i += 1
def get_operation(operator):
"""Gets the operand the user wants to apply on the functions"""
global i
length = len(operator)
display.insert(i, operator)
i += length
def undo():
"""removes the last entered operator/variable from entry widget"""
whole_string = display.get()
if len(whole_string): ## repeats until
## now just decrement the string by one index
new_string = whole_string[:-1]
print(new_string)
clear_all()
display.insert(0, new_string)
else:
clear_all()
display.insert(0, "Error, press AC")
def calculate():
"""
Evaluates the expression
ref : http://stackoverflow.com/questions/594266/equation-parsing-in-python
"""
whole_string = display.get()
try:
formulae = parser.expr(whole_string).compile()
result = eval(formulae)
clear_all()
display.insert(0, result)
except Exception:
clear_all()
display.insert(0, "Error!")
root.columnconfigure(0,pad=3)
root.columnconfigure(1,pad=3)
root.columnconfigure(2,pad=3)
root.columnconfigure(3,pad=3)
root.columnconfigure(4,pad=3)
root.rowconfigure(0,pad=3)
root.rowconfigure(1,pad=3)
root.rowconfigure(2,pad=3)
root.rowconfigure(3,pad=3)
display = Entry(root, font = ("Calibri", 13))
display.grid(row = 1, columnspan = 6 , sticky = W+E)
one = Button(root, text = "1", command = lambda : get_variables(1), font=("Calibri", 12))
one.grid(row = 2, column = 0)
two = Button(root, text = "2", command = lambda : get_variables(2), font=("Calibri", 12))
two.grid(row = 2, column = 1)
three = Button(root, text = "3", command = lambda : get_variables(3), font=("Calibri", 12))
three.grid(row = 2, column = 2)
four = Button(root, text = "4", command = lambda : get_variables(4), font=("Calibri", 12))
four.grid(row = 3 , column = 0)
five = Button(root, text = "5", command = lambda : get_variables(5), font=("Calibri", 12))
five.grid(row = 3, column = 1)
six = Button(root, text = "6", command = lambda : get_variables(6), font=("Calibri", 12))
six.grid(row = 3, column = 2)
seven = Button(root, text = "7", command = lambda : get_variables(7), font=("Calibri", 12))
seven.grid(row = 4, column = 0)
eight = Button(root, text = "8", command = lambda : get_variables(8), font=("Calibri", 12))
eight.grid(row = 4, column = 1)
nine = Button(root , text = "9", command = lambda : get_variables(9), font=("Calibri", 12))
nine.grid(row = 4, column = 2)
cls = Button(root, text = "AC", command = clear_all, font=("Calibri", 12), foreground = "red")
cls.grid(row = 5, column = 0)
zero = Button(root, text = "0", command = lambda : get_variables(0), font=("Calibri", 12))
zero.grid(row = 5, column = 1)
result = Button(root, text = "=", command = calculate, font=("Calibri", 12), foreground = "red")
result.grid(row = 5, column = 2)
plus = Button(root, text = "+", command = lambda : get_operation("+"), font=("Calibri", 12))
plus.grid(row = 2, column = 3)
minus = Button(root, text = "-", command = lambda : get_operation("-"), font=("Calibri", 12))
minus.grid(row = 3, column = 3)
multiply = Button(root,text = "*", command = lambda : get_operation("*"), font=("Calibri", 12))
multiply.grid(row = 4, column = 3)
divide = Button(root, text = "/", command = lambda : get_operation("/"), font=("Calibri", 12))
divide.grid(row = 5, column = 3)
# adding new operations
pi = Button(root, text = "pi", command = lambda: get_operation("*3.14"), font =("Calibri", 12))
pi.grid(row = 2, column = 4)
molo = Button(root, text = "%", command = lambda : get_operation("%"), font=("Calibri", 12))
molo.grid(row = 3, column = 4)
left_bracket = Button(root, text = "(", command = lambda: get_operation("("), font =("Calibri", 12))
left_bracket.grid(row = 4, column = 4)
exp = Button(root, text = "exp", command = lambda: get_operation("**"), font = ("Calibri", 10))
exp.grid(row = 5, column = 4)
## To be added :
# sin, cos, log, ln
undo_button = Button(root, text = "<-", command = undo, font =("Calibri", 12), foreground = "red")
undo_button.grid(row = 2, column = 5)
fact = Button(root, text = "x!", command = factorial, font=("Calibri", 12))
fact.grid(row = 3, column = 5)
right_bracket = Button(root, text = ")", command = lambda: get_operation(")"), font =("Calibri", 12))
right_bracket.grid(row = 4, column = 5)
square = Button(root, text = "^2", command = lambda: get_operation("**2"), font = ("Calibri", 10))
square.grid(row = 5, column = 5)
root.mainloop()

『伍』 如何用 Python 寫一個帶 GUI 的科學計算程序

強烈推薦使用Tk 庫,非常簡單。 下面是我正在寫的界面,可以運行,後台還沒處理好。不懂的可以問我。

#!python2pymol
#coding:utf-8


fromTkinterimport*
fromttkimport*
frommathimportsin,asin,cos,acos

classPredictGui(Frame):
def__init__(self,parent=None):
Frame.__init__(self,parent)
self.pack(expand=YES,fill=BOTH)

Label(self,text="generatecccc").pack(side=TOP)

centerF=Frame(self)
centerF.pack(expand=YES,fill=BOTH)
Label(centerF,width=50,text="thecoordofthecenter").pack(side=LEFT)
self.xyz_var=StringVar()
Entry(centerF,text=self.xyz_var,width=15).pack(side=LEFT)
self.xyz_var.set('000')


radiusF=Frame(self)
radiusF.pack(side=TOP,fill=BOTH,expand=YES)
Label(radiusF,text="theradiusofthecc",width=50).pack(side=LEFT)
self.r_var=DoubleVar()
radius_Com=Combobox(radiusF,width=15,textvariable=self.r_var)
radius_Com.pack(side=LEFT)
radius_Com['values']=(5.0,10.0,15.0,20.0,)
radius_Com.set('selectradius')
radius_Com.bind("<<ComboboxSelected>>",self.selectradius)


densityF=Frame(self)
densityF.pack(expand=YES,fill=BOTH)
Label(densityF,text="thedensityis0-1",width=50).pack(side=LEFT)
self.density_var=DoubleVar()
Entry(densityF,text=self.density_var,width=15).pack(side=LEFT)defselectradius(event,self):
'''
'''
temp=asin(1.0/self.r_var.get())
self.density_var.set(temp)if__name__=='__main__':
mainW=PredictGui()
mainW.mainloop()

『陸』 什麼是Python科學計算

本書介紹如何用Python開發科學計算的應用程序,除了介紹數值計算之外,還著重介紹如何製作互動式的2D、3D圖像,如何設計精巧的程序界面,如何與C語言編寫的高速計算程序結合,如何編寫聲音、圖像處理演算法等內容。書中涉及的Python擴展庫包括NumPy、SciPy、SymPy、matplotlib、Traits、TraitsUI、Chaco、TVTK、Mayavi、VPython、OpenCV等,涉及的應用領域包括數值運算、符號運算、二維圖表、三維數據可視化、三維動畫演示、圖像處理以及界面設計等。書中以大量實例引導讀者逐步深入學習,每個實常式序都有詳盡的解釋,並都能在本書推薦的運行環境中正常運行。此外,本書附有大量的圖表和插圖,力求減少長篇的理論介紹和公式推導,以便讀者通過實例和數據學習並掌握理論知識。

『柒』 python能做什麼

python可以做:

1、Web開發;

2、數據科學研究;

3、網路爬蟲;

4、嵌入式應用開發;

5、游戲開發;

6、桌面應用開發。

Python解釋器易於擴展,可以使用C或C++(或者其他可以通過C調用的語言)擴展新的功能和數據類型。Python 也可用於可定製化軟體中的擴展程序語言。Python豐富的標准庫,提供了適用於各個主要系統平台的源碼或機器碼。

(7)用python科學計算擴展閱讀

由於Python語言的簡潔性、易讀性以及可擴展性,在國外用Python做科學計算的研究機構日益增多,一些知名大學已經採用Python來教授程序設計課程。例如卡耐基梅隆大學的編程基礎、麻省理工學院的計算機科學及編程導論就使用Python語言講授。

眾多開源的科學計算軟體包都提供了Python的調用介面,例如著名的計算機視覺庫OpenCV、三維可視化庫VTK、醫學圖像處理庫ITK。

參考資料來源:網路-Python

『捌』 如何使用python計算常微分方程

常用形式
odeint(func, y0, t,args,Dfun)
一般這種形式就夠用了。
下面是官方的例子,求解的是
D(D(y1))-t*y1=0
為了方便,採取D=d/dt。如果我們令初值
y1(0) = 1.0/3**(2.0/3.0)/gamma(2.0/3.0)
D(y1)(0) = -1.0/3**(1.0/3.0)/gamma(1.0/3.0)
這個微分方程的解y1=airy(t)。

令D(y1)=y0,就有這個常微分方程組。
D(y0)=t*y1
D(y1)=y0

Python求解該微分方程。
>>> from scipy.integrate import odeint
>>> from scipy.special import gamma, airy
>>> y1_0 = 1.0/3**(2.0/3.0)/gamma(2.0/3.0)
>>> y0_0 = -1.0/3**(1.0/3.0)/gamma(1.0/3.0)
>>> y0 = [y0_0, y1_0]
>>> def func(y, t):
... return [t*y[1],y[0]]
>>> def gradient(y,t):
... return [[0,t],[1,0]]
>>> x = arange(0,4.0, 0.01)
>>> t = x
>>> ychk = airy(x)[0]
>>> y = odeint(func, y0, t)
>>> y2 = odeint(func, y0, t, Dfun=gradient)
>>> print ychk[:36:6]
[ 0.355028 0.339511 0.324068 0.308763 0.293658 0.278806]
>>> print y[:36:6,1]
[ 0.355028 0.339511 0.324067 0.308763 0.293658 0.278806]
>>> print y2[:36:6,1]
[ 0.355028 0.339511 0.324067 0.308763 0.293658 0.278806]

得到的解與精確值相比,誤差相當小。
=======================================================================================================

args是額外的參數。
用法請參看下面的例子。這是一個洛侖茲曲線的求解,並且用matplotlib繪出空間曲線圖。(來自《python科學計算》)
from scipy.integrate import odeint
import numpy as np
def lorenz(w, t, p, r, b):
# 給出位置矢量w,和三個參數p, r, b 計算出
# dx/dt, dy/dt, dz/dt 的值
x, y, z = w
# 直接與lorenz 的計算公式對應
return np.array([p*(y-x), x*(r-z)-y, x*y-b*z])
t = np.arange(0, 30, 0.01) # 創建時間點
# 調用ode 對lorenz 進行求解, 用兩個不同的初始值
track1 = odeint(lorenz, (0.0, 1.00, 0.0), t, args=(10.0, 28.0, 3.0))
track2 = odeint(lorenz, (0.0, 1.01, 0.0), t, args=(10.0, 28.0, 3.0))
# 繪圖
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(track1[:,0], track1[:,1], track1[:,2])
ax.plot(track2[:,0], track2[:,1], track2[:,2])
plt.show()
===========================================================================
scipy.integrate.odeint(func, y0, t, args=(), Dfun=None, col_deriv=0, full_output=0, ml=None, mu=None, rtol=None, atol=None, tcrit=None, h0=0.0, hmax=0.0, hmin=0.0, ixpr=0, mxstep=0, mxhnil=0, mxordn=12, mxords=5, printmessg=0)
計算常微分方程(組)
使用 FORTRAN庫odepack中的lsoda解常微分方程。這個函數一般求解初值問題。

參數:

func : callable(y, t0, ...) 計算y在t0 處的導數。
y0 : 數組 y的初值條件(可以是矢量)
t : 數組 為求出y,這是一個時間點的序列。初值點應該是這個序列的第一個元素。
args : 元組 func的額外參數
Dfun : callable(y, t0, ...) 函數的梯度(Jacobian)。即雅可比多項式。
col_deriv : boolean. True,Dfun定義列向導數(更快),否則Dfun會定義橫排導數
full_output : boolean 可選輸出,如果為True 則返回一個字典,作為第二輸出。
printmessg : boolean 是否列印convergence 消息。

返回: y : array, shape (len(y0), len(t))
數組,包含y值,每一個對應於時間序列中的t。初值y0 在第一排。
infodict : 字典,只有full_output == True 時,才會返回。
字典包含額為的輸出信息。
鍵值:

『hu』 vector of step sizes successfully used for each time step.
『tcur』 vector with the value of t reached for each time step. (will always be at least as large as the input times).
『tolsf』 vector of tolerance scale factors, greater than 1.0, computed when a request for too much accuracy was detected.
『tsw』 value of t at the time of the last method switch (given for each time step)
『nst』 cumulative number of time steps
『nfe』 cumulative number of function evaluations for each time step
『nje』 cumulative number of jacobian evaluations for each time step
『nqu』 a vector of method orders for each successful step.
『imxer』index of the component of largest magnitude in the weighted local error vector (e / ewt) on an error return, -1 otherwise.
『lenrw』 the length of the double work array required.
『leniw』 the length of integer work array required.
『mused』a vector of method indicators for each successful time step: 1: adams (nonstiff), 2: bdf (stiff)
其他參數,官方網站和文檔都沒有明確說明。相關的資料,暫時也找不到。

『玖』 python的科學計算庫有哪些

1、Numpy庫簡介
在Python中很多高級庫都是基本Numpy科學庫去做的。之前如果用Python對數據進行操作,需要一行一行或者一個一個數據的去進行操作。而在Numpy中,則是封裝了一系列矩陣的操作:首先把數據轉換成一系列矩陣的格式,然後再對矩陣進行操作。這樣既高效,也省時。Numpy封裝了一系列的函數函數,方便我們去操作矩陣。Numpy中一行代碼就頂Python中十幾行的代碼。
2、Pandas庫簡介
在Pandas 是基於Numpy的一種工具,該工具是為了解決數據分析任務而創建的。Pandas
納入了大量庫和一些標準的數據模型,提供了高效地操作大型數據集所需的工具。Pandas提供了大量能使我們快速便捷地處理數據的函數和方法。你很快就會發現,它是使Python成為強大而高效的數據分析環境的重要因素之一。
3、Matplotlib庫簡介
Matplotlib 是一個 Python 的 2D繪圖庫,它以各種硬拷貝格式和跨平台的互動式環境生成出版質量級別的圖形,通過
Matplotlib,開發者可以僅需要幾行代碼,便可以生成繪圖,直方圖,功率譜,條形圖,錯誤圖,散點圖等。

『拾』 用python計算圓的面積

首先定義一個findArea()方法,參數r為圓的半徑,圓周率π取3.142,函數的返回值為PI * (r*r),然後調用該方法即可求出圓的面積。

Python解釋器易於擴展,可以使用C或C++(或者其他可以通過C調用的語言)擴展新的功能和數據類型。[4]Python 也可用於可定製化軟體中的擴展程序語言。Python豐富的標准庫,提供了適用於各個主要系統平台的源碼或機器碼。

2021年10月,語言流行指數的編譯器Tiobe將Python加冕為最受歡迎的編程語言,20年來首次將其置於Java、C和JavaScript之上。

由於Python語言的簡潔性、易讀性以及可擴展性,在國外用Python做科學計算的研究機構日益增多,一些知名大學已經採用Python來教授程序設計課程。例如卡耐基梅隆大學的編程基礎、麻省理工學院的計算機科學及編程導論就使用Python語言講授。

眾多開源的科學計算軟體包都提供了Python的調用介面,例如著名的計算機視覺庫OpenCV、三維可視化庫VTK、醫學圖像處理庫ITK。而Python專用的科學計算擴展庫就更多了。

熱點內容
五班資料庫 發布:2024-04-23 13:59:57 瀏覽:797
在c語言中數字029是一個 發布:2024-04-23 13:52:13 瀏覽:740
我的世界電腦版伺服器怎麼禁足 發布:2024-04-23 13:24:49 瀏覽:547
y壓縮包 發布:2024-04-23 12:41:20 瀏覽:166
內網互相訪問 發布:2024-04-23 12:36:23 瀏覽:319
安卓國際服在哪裡看賬號 發布:2024-04-23 12:30:29 瀏覽:299
android開發伺服器 發布:2024-04-23 12:24:42 瀏覽:237
騰訊雲個人伺服器 發布:2024-04-23 12:24:40 瀏覽:803
debian下載源碼 發布:2024-04-23 12:20:43 瀏覽:479
curl本地IP伺服器 發布:2024-04-23 12:19:22 瀏覽:509