python數學實驗與建模
數學實驗與數學軟件。In this tutorial, we will learn about Python Math module and its functions. In the previous tutorial, we learned about Python Matrix.
在本教程中,我們將學習Python Math模塊及其功能。 在上一教程中,我們了解了Python Matrix 。
python數學建模,Python Math module provides access to the mathematical functions defined by the C standard. So, we can do many complex mathematical operations with the help of the Python Math functions. The tutorial is designed with some basic functions and examples of math module. Let’s get started.
Python Math模塊提供對C標準定義的數學函數的訪問。 因此,我們可以借助Python Math函數執行許多復雜的數學運算。 本教程設計了一些基本功能和數學模塊示例。 讓我們開始吧。
Python math module is part of the python installation, so we can just import it in our python program and use it.
Python數學模塊是python安裝的一部分,因此我們可以將其導入到python程序中并使用它。
In this section, we will discuss about these three math module functions. The floor()
function is used to get the floor value to the given number. Similarly ceil()
function is used to get the ceiling value of a given number. So, these two functions are used to round the value, either floor value or ceiling value.
在本節中,我們將討論這三個數學模塊功能。 floor()
函數用于將底值設為給定的數字。 同樣, ceil()
函數用于獲取給定數字的上限。 因此,這兩個函數用于舍入底值或上限值。
fabs()
function is used to get the absolute value of the given number. See the example code below.
fabs()
函數用于獲取給定數字的絕對值。 請參見下面的示例代碼。
import mathnumber = -2.34print('The given number is :', number)
print('Floor value is :', math.floor(number))
print('Ceiling value is :', math.ceil(number))
print('Absolute value is :', math.fabs(number))
And the output will be
輸出將是
The given number is : -2.34
Floor value is : -3
Ceiling value is : -2
Absolute value is : 2.34
Math module exp()
function is used to get e^x.
數學模塊exp()
函數用于獲取e ^ x 。
expm1()
function returns (e^x)-1. For small value of x, direct calculation of exp(x)-1
may results in significant loss in precision while the expm1(x)
can produce output in full precision.
expm1()
函數返回(e ^ x)-1 。 對于較小的x值,直接計算exp(x)-1
可能會導致精度顯著降低,而expm1(x)
可能會產生完全精度的輸出。
The log()
function is used to get the log value. See the example code.
log()
函數用于獲取日志值。 請參閱示例代碼。
import mathnumber = 1e-4 # small value of of xprint('The given number (x) is :', number)
print('e^x (using exp() function) is :', math.exp(number)-1)
print('e^x (using expml() function) is :', math.expm1(number))
print('log(fabs(x), base) is :', math.log(math.fabs(number), 10))
And you will get the output like this
這樣您將獲得輸出
The given number (x) is : 0.0001
e^x (using exp() function) is : 0.0001000050001667141
e^x (using expml() function) is : 0.00010000500016667084
log(fabs(x), base) is : -3.999999999999999
All the trigonometric functions are available in python math module, so you can easily calculate them using sin(), cos(), tan(), acos(), asin(), atan() etc functions.
python數學模塊中提供了所有三角函數,因此您可以使用sin() , cos() , tan() , acos() , asin() , atan()等函數輕松地計算它們。
Also you can convert angles from degree to radian and radian to degree. See the example code.
您還可以將角度從度轉換為弧度,并將弧度轉換為度。 請參閱示例代碼。
import mathangleInDegree = 45
angleInRadian = math.radians(angleInDegree)print('The given angle is :', angleInRadian)
print('sin(x) is :', math.sin(angleInRadian))
print('cos(x) is :', math.cos(angleInRadian))
print('tan(x) is :', math.tan(angleInRadian))
So, in output you will get
因此,在輸出中,您將獲得
We can use sqrt(x)
function to get the square root of x. Below is a simple example of python math sqrt function.
我們可以使用sqrt(x)
函數獲得x的平方根。 以下是python math sqrt函數的簡單示例。
import mathx = 16
y = 10
z = 11.2225print('sqrt of 16 is ', math.sqrt(x))
print('sqrt of 10 is ', math.sqrt(y))
print('sqrt of 11.2225 is ', math.sqrt(z))
Output produced by above math sqrt example is:
上面的數學sqrt示例產生的輸出是:
sqrt of 16 is 4.0
sqrt of 10 is 3.1622776601683795
sqrt of 11.2225 is 3.35
Python math module has “pi” as a constant that can be used in mathematical calculations such as area of a circle.
Python數學模塊具有“ pi”作為常量,可用于數學計算(例如圓的面積)。
import mathprint('PI value = ', math.pi)radius = 4print('Area of Circle with Radius 4 =', math.pi * (radius ** 2))
Above python example program will produce following output.
上面的python示例程序將產生以下輸出。
PI value = 3.141592653589793
Area of Circle with Radius 4 = 50.26548245743669
These are some of the basic functions from Python Math module. If you want to know about more functions, then see the official documentation.
這些是Python Math模塊的一些基本功能。 如果您想了解更多功能,請參閱官方文檔 。
翻譯自: https://www.journaldev.com/16049/python-math
python數學實驗與建模
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态