计算范围内所有值的函数?
Calculate function with all values in range?
我正在尝试进行数学模拟以确定何时满足条件。我的方法是定义条件并创建一个增加 x 和 y 值的 while 循环。请看下面的代码:
# Initialize the variables.
x1list = []
y1list = []
x2list = []
y2list = []
x = 0
y = 0
i = 0
while i < 10:
# Update data.
i += 1
x += .1
y += .1
func1 = x + y - 1
func2 = x * y
cond1 = func1 < func2 < 1
if cond1:
x1list.append(x)
y1list.append(y)
这段代码的问题在于它只计算x和y以相同速率增长时的条件。当然,我可以更改费率,但这并不能真正解决问题。
我想做的是计算一个范围内的条件,比如说x(-10,10) 和y(-10,10)。我正在考虑制作一个包含所有 x 值的数组和另一个包含所有 y 值的数组,但是,我不知道如何使用所有这些值计算条件。
我的另一个想法是采用单个 x 值并使用所有 y 值对其进行测试,然后增加 x 并一次又一次地进行测试。
我该如何解决这个问题?
对于这种问题,不要盲目的迭代
首先,您可以寻找明显的解决方案:x = 0
和 y = 0
是一个。
您可以使用 sympy
to try to find general solutions. Wolfram Alpha 也是执行这些任务的好工具,并找到这个区域:
如果这些工具找不到任何代数解,您可以使用 scipy.optimize 找到一些数值结果:
from scipy.optimize import minimize
def f(x):
return (x[0] + x[1] - 1)**2 + (x[0] * x[1])**2
minimize(f, [0,0])
它输出:
status: 0
success: True
njev: 6
nfev: 24
hess_inv: array([[ 0.59518944, -0.40481056],
[-0.40481056, 0.59518944]])
fun: 0.050945906253583216
x: array([ 0.4533971, 0.4533971])
message: 'Optimization terminated successfully.'
jac: array([ -2.88896263e-06, -2.88896263e-06])
这个例子有点矫枉过正,但它显示了更复杂函数的有希望的结果。
方法一:二维'simulation'网格
I was thinking about making an array with all x values and another one with all y values, but, then, I don't know how to calculate the condition with all those values.
import numpy as np
x = np.arange(-10, 10, 0.1)
y = np.arange(-10, 10, 0.1)
# Create 2D simulation meshes for x and y.
# You can read the API entry for meshgrid to learn about options for index
# ordering, mesh sparsity, and memory copying
X, Y = np.meshgrid(x, y)
func1 = X + Y - 1
func2 = X * Y
cond = np.logical_and(func1 < func2, func2 < 1.0) # intrinsic `and` does not work here
# now cond can be used as a 'mask' for any masked-array operations on X and Y
# including for numpy boolean indexing:
print('(X, Y) pairs')
for xy_pair in zip(X[cond], Y[cond]):
print xy_pair
方法二:嵌套循环
Another idea that I had was to take a single value of x and test it with all y values, then increase x and test again and again.
import numpy as np # no slower or memory-intensive than `from numpy import arange`
X = []
Y = []
for y in np.arange(-10, 10, 0.1):
for x in np.arange(-10, 10, 0.1):
if (x+y-1 < x*y) and (x*y < 1.0):
X.append(x)
Y.append(y)
print('(X, Y) pairs')
for xy_pair in zip(X, Y):
print xy_pair
选择哪种方法?
How should I approach this problem?
这完全取决于您要对计算结果为 True
的 (x, y)
对执行的操作。如果您在提供更多指导的情况下编辑您的问题,那么对于您的用例来说哪种解决方案更直接。
例如,方法 1 提供了用于绘制解决方案的二维数组 space 而 方法 2 提供了紧凑的 python list
s 用于数据库。
警告:条件运算符
还必须指出,具有多个条件运算符的数学表达式在 Python 中没有意义。这一行:
cond1 = func1 < func2 < 1
如果使用标准操作顺序进行评估,如 cond1 = (func1 < func2) < 1
将对 cond1 = (True/False) < 1
进行中间评估,这将隐式地将 True
重铸为 1
和 False
为 0
,但不会正确计算数学表达式 func1 < func2 < 1
.
编辑:
@(Eric Duminil) 的答案提供了解决潜在数学问题的替代概念,上面的两种方法假设您的问题需要在离散网格上进行数值求解,并且具有这些离散解点对于无论后面是什么代码。
@Uriel 的回答可能看起来有效,但请参阅我关于条件运算符的说明,了解为什么会产生误导。
此外,我最初键入 and
来组合二维条件语句,但这是不正确的,并导致错误。请改用 np.logical_and
。
我正在尝试进行数学模拟以确定何时满足条件。我的方法是定义条件并创建一个增加 x 和 y 值的 while 循环。请看下面的代码:
# Initialize the variables.
x1list = []
y1list = []
x2list = []
y2list = []
x = 0
y = 0
i = 0
while i < 10:
# Update data.
i += 1
x += .1
y += .1
func1 = x + y - 1
func2 = x * y
cond1 = func1 < func2 < 1
if cond1:
x1list.append(x)
y1list.append(y)
这段代码的问题在于它只计算x和y以相同速率增长时的条件。当然,我可以更改费率,但这并不能真正解决问题。
我想做的是计算一个范围内的条件,比如说x(-10,10) 和y(-10,10)。我正在考虑制作一个包含所有 x 值的数组和另一个包含所有 y 值的数组,但是,我不知道如何使用所有这些值计算条件。
我的另一个想法是采用单个 x 值并使用所有 y 值对其进行测试,然后增加 x 并一次又一次地进行测试。
我该如何解决这个问题?
对于这种问题,不要盲目的迭代
首先,您可以寻找明显的解决方案:x = 0
和 y = 0
是一个。
您可以使用 sympy
to try to find general solutions. Wolfram Alpha 也是执行这些任务的好工具,并找到这个区域:
如果这些工具找不到任何代数解,您可以使用 scipy.optimize 找到一些数值结果:
from scipy.optimize import minimize
def f(x):
return (x[0] + x[1] - 1)**2 + (x[0] * x[1])**2
minimize(f, [0,0])
它输出:
status: 0
success: True
njev: 6
nfev: 24
hess_inv: array([[ 0.59518944, -0.40481056],
[-0.40481056, 0.59518944]])
fun: 0.050945906253583216
x: array([ 0.4533971, 0.4533971])
message: 'Optimization terminated successfully.'
jac: array([ -2.88896263e-06, -2.88896263e-06])
这个例子有点矫枉过正,但它显示了更复杂函数的有希望的结果。
方法一:二维'simulation'网格
I was thinking about making an array with all x values and another one with all y values, but, then, I don't know how to calculate the condition with all those values.
import numpy as np
x = np.arange(-10, 10, 0.1)
y = np.arange(-10, 10, 0.1)
# Create 2D simulation meshes for x and y.
# You can read the API entry for meshgrid to learn about options for index
# ordering, mesh sparsity, and memory copying
X, Y = np.meshgrid(x, y)
func1 = X + Y - 1
func2 = X * Y
cond = np.logical_and(func1 < func2, func2 < 1.0) # intrinsic `and` does not work here
# now cond can be used as a 'mask' for any masked-array operations on X and Y
# including for numpy boolean indexing:
print('(X, Y) pairs')
for xy_pair in zip(X[cond], Y[cond]):
print xy_pair
方法二:嵌套循环
Another idea that I had was to take a single value of x and test it with all y values, then increase x and test again and again.
import numpy as np # no slower or memory-intensive than `from numpy import arange`
X = []
Y = []
for y in np.arange(-10, 10, 0.1):
for x in np.arange(-10, 10, 0.1):
if (x+y-1 < x*y) and (x*y < 1.0):
X.append(x)
Y.append(y)
print('(X, Y) pairs')
for xy_pair in zip(X, Y):
print xy_pair
选择哪种方法?
How should I approach this problem?
这完全取决于您要对计算结果为 True
的 (x, y)
对执行的操作。如果您在提供更多指导的情况下编辑您的问题,那么对于您的用例来说哪种解决方案更直接。
例如,方法 1 提供了用于绘制解决方案的二维数组 space 而 方法 2 提供了紧凑的 python list
s 用于数据库。
警告:条件运算符
还必须指出,具有多个条件运算符的数学表达式在 Python 中没有意义。这一行:
cond1 = func1 < func2 < 1
如果使用标准操作顺序进行评估,如 cond1 = (func1 < func2) < 1
将对 cond1 = (True/False) < 1
进行中间评估,这将隐式地将 True
重铸为 1
和 False
为 0
,但不会正确计算数学表达式 func1 < func2 < 1
.
编辑:
@(Eric Duminil) 的答案提供了解决潜在数学问题的替代概念,上面的两种方法假设您的问题需要在离散网格上进行数值求解,并且具有这些离散解点对于无论后面是什么代码。
@Uriel 的回答可能看起来有效,但请参阅我关于条件运算符的说明,了解为什么会产生误导。
此外,我最初键入 and
来组合二维条件语句,但这是不正确的,并导致错误。请改用 np.logical_and
。