小编Rob*_*ist的帖子

从Matlab到Python的算法均衡

我已经策划了3 d网在Matlab通过以下小m-file:

[x,n] = meshgrid(0:0.1:20, 1:1:100);

mu = 0;
sigma = sqrt(2)./n;

f = normcdf(x,mu,sigma);

mesh(x,n,f);
Run Code Online (Sandbox Code Playgroud)

我将Python通过以下代码片段利用及其相应的模块获得相同的结果:

import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt

sigma = 1

def integrand(x, n):
    return (n/(2*sigma*np.sqrt(np.pi)))*np.exp(-(n**2*x**2)/(4*sigma**2))

tt = np.linspace(0, 20, 2000)
nn = np.linspace(1, 100, 100)  

T = np.zeros([len(tt), len(nn)])

for i,t in enumerate(tt):
    for j,n in enumerate(nn):
        T[i, j], _ = quad(integrand, -np.inf, t, args=(n,))

x, y = np.mgrid[0:20:0.01, 1:101:1]

plt.pcolormesh(x, y, …
Run Code Online (Sandbox Code Playgroud)

python matlab numpy matplotlib matlab-figure

5
推荐指数
1
解决办法
409
查看次数

如何通过at()函数访问2D向量的索引?

我将在成员函数中初始化2D向量,其中输入参数计划被输入到函数参数内的确定索引中.此外,由于安全问题,我不会使用[]运算符来访问成员.如何使用at()函数来访问2D向量的索引,如下所示?

vector<vector<double>> weight;

void Connection::setWeight(const double& value, const double& i, const double& j)
{
    // The other scheme except: weight[i][j] = value;
}
Run Code Online (Sandbox Code Playgroud)

c++ vector c++11

4
推荐指数
2
解决办法
2万
查看次数

用Python绘制微分方程的系统

我刚刚开始使用Python绘制微分方程的数值解。我知道如何用于scipy.odeint求解和绘制单个微分方程,但是我对微分方程组一无所知。如何绘制以下耦合系统?

N' = a * N - (C/(1+C)) * b * N
C' = (C/(1+C)) * N - C + 1

a = 4
b = 7
N(0) = 100
C(0) = 5
Run Code Online (Sandbox Code Playgroud)

python plot matplotlib

4
推荐指数
1
解决办法
4866
查看次数

数学中matlab的subs()的等价函数

我有一个matlab m文件,以便绘制如下的积分.我想在mathematica中重写这段代码,但我不知道subs()的任何等效函数!有人帮我吗?

syms x y w;
fun = (-1/(4.*pi)).*log(x.^2+(y-w).^2);
integral = int(fun, w); 
res_l = subs(integral, w, -0.5);
res_u = subs(integral, w, 0.5);
res = res_u - res_l;
ezsurf(res, [-1,1]);
Run Code Online (Sandbox Code Playgroud)

matlab wolfram-mathematica

2
推荐指数
1
解决办法
1238
查看次数

如何处理"解压缩的值太多"

我已经检查了一些想法和原因,下面针对这个问题进行了调查...... "太多值解包"异常 (Stefano Borini的解释)

但是在这里,我正在迭代列表作为理解列表并将结果移动到列表中......!所以输入的数量读取输出变量的数量,即tempList......

然后,这个过程出了什么问题?!

def DoProcess(self, myList):
    tempList = []
    tempList = [[x,y,False] for [x,y] in myList]
    return tempList
Run Code Online (Sandbox Code Playgroud)

编辑1:myList是列表,就像[[x1, y1], [x2, y2], [x3, y3], [x4 y4]].

class Agent(object):
    def __init__(self, point = None):
        self.locationX = point.x
        self.locationY = point.y

    def __iter__(self):
        return self

    def __next__(self):
        return [self.locationX, self.locationY]

    def __getItem__(self):
        return [self.locationX, self.locationY]

    def GenerateAgents(self, numberOfAgents):
        agentList = []
        while len(agentList) < numberOfAgents:

            point = Point.Point()
            point.x = random.randint(0, 99)
            point.y …
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
883
查看次数

定义具有相同名称但返回类型不同的两个方法是否合法?

我写了一段代码来确定一个典型的回文字符串.我通过返回字符串的reverse()方法的定义来做到这一点.我也渴望拥有相同的方法,但是在虚空形式中,因为未来的某些需求.当我将后者添加到代码中时,有效输出将变为无效.所以,问题是定义两个具有相同名称但返回类型不同的方法是否合法?如果没有,请让我知道如何使用void-type方法编写此代码.

class detector(object):
    def __init__(self,string):
        self.string = string

    forbidden = (' ','!','?','.','-','_','&','%',"#",",")

    def eliminator(self):
        for item in self.forbidden:
            if item in self.string:
                self.string = self.string.replace(item,"")

    def reverse(self):
        return self.string[::-1]            

    #def reverse(self):
    #    self.string = self.string[::-1]    I am prone to add this method

    def check(self):
        reversed = self.reverse()
        if self.string == reversed:
            print("Yes")
        else:
            print("No")

det = detector("rise to vote, sir!")
det.eliminator()
det.check()
Run Code Online (Sandbox Code Playgroud)

当我添加注释行时,有效的"是"变为"否"!

python

1
推荐指数
2
解决办法
4580
查看次数