小编kir*_*off的帖子

Python matplotlib - 使用 meshgrid 格式化用户定义的函数

我有一个名为 funcPower3 的函数,其主体如下所示。我想在 MatplotLib 中使用 3D 绘图功能绘制此函数。我在带有网格网格的 scipy 文档中看到了一个示例。然而,在这个例子中,function 不是一个定义的函数,而是一个简单的数学运算:

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,
    linewidth=0, antialiased=False)
Run Code Online (Sandbox Code Playgroud)

我从这个示例中汲取了灵感,但我自己的函数签名与来自网格网格的数据不兼容。我有这个错误:

 ValueError: zero-size array to minimum.reduce without identity
Run Code Online (Sandbox Code Playgroud)

我的函数代码在这里:

def funcPower3(PARAM):
    inputX1 = open("power3X.txt","r")
    inputY = open("power3Y.txt","r")
    X1=[]
    Y=[]
    for line in inputX1:
        X1.append(float(line))
    for line in inputY:
        Y.append(float(line))
    resTmp_ = …
Run Code Online (Sandbox Code Playgroud)

python plot matplotlib scipy

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

googletest doc C字符串和字符串

我正在阅读Googletest doc,我知道有一种比较字符串的语法,另一种用于比较C字符串.我没有看到什么被称为C字符串和字符串.这些有何不同?

c c++ string googletest

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

在构造函数C++中初始化成员指向函数的指针

我有这个成员定义

class X
{
   private:

      int (TMyClass::*pt2Member)(float, char, char);
}
Run Code Online (Sandbox Code Playgroud)

我想在构造函数中将此成员初始化为NULL.有可能这样:

 X::X()
 {
      TMyClass::*pt2Member = NULL;
 }
Run Code Online (Sandbox Code Playgroud)

什么是正确的语法?我的功能是C++非静态.

然后,我应该怎么写setter?

// declaration
void set_pointer_to_function(int(*pt2func)(float f, char c, char c));


// definition
void X::set_pointer_to_function(int(*pt2func)(float f, char c, char c))
{
     pt2Member = pt2func;
} 
Run Code Online (Sandbox Code Playgroud)

这是对的吗?

c++ initialization function-pointers

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

c ++错误的参数类型 - 函数指针

我有一个函数,它将一个指向函数的参数作为参数:

void MySolver::set_pt2func(double(*pt2function)(double*))
{
    pt2Function = pt2function;
}   
Run Code Online (Sandbox Code Playgroud)

在另一个类中,某些方法想要使用以下代码初始化实例my_solver:

double (MyClass::*pt2apply)(double* parameter) = NULL; 
pt2apply = &MyClass::apply_func;

my_solver->set_pt2func(pt2apply);
Run Code Online (Sandbox Code Playgroud)

最后一行显示编译错误:

error: double (MyClass::*pt2apply)(double*)

argument of type  (MyClass::* )(double*) is not compatible 
with argument of type double(*)(double*)
Run Code Online (Sandbox Code Playgroud)

怎么修?

谢谢!

编辑

我用这个小费

http://www.parashift.com/c++-faq/memfnptr-to-const-memfn.html

我设置了typedef:

typedef  double (MyClass::*Pt2apply_func)(double*) const;
Run Code Online (Sandbox Code Playgroud)

在我的方法中,我试试这个:

Pt2apply_func pt2apply = &MyClass::apply_func;

my_solver->set_pt2func(pt2apply);
Run Code Online (Sandbox Code Playgroud)

和编译错误显示&MyClass,再次兼容类型.处理我的问题的其他方法?谢谢!

c++ types pointers compiler-errors function-pointers

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

VS2013中生成MOC文件

我有一个从.pro文件生成的项目.我无法编译它,因为没有生成MOC文件.

  • Q_OBJECT 用于要被MOCed的类的标头中
  • #pragma once 用于要被MOCed的类的标头中

我把冗长的详细信息,我可以看到在正确的类上调用了moc.然后,出现以下错误:standard input(0): Note: No relevant classes found. No output generated.

最后,编译器无法链接moc文件,并且构建失败:

8>  moc_myClass.cpp
8>c1xx : fatal error C1083: Cannot open source file: 
'D:\Things\\L_build\Stuff\moc\moc_myClass.cpp': No such file or directory
Run Code Online (Sandbox Code Playgroud)

任何线索?

c++ qt compiler-errors moc visual-studio-2013

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

交换方法适用于任何数据结构

我在swap()使用Python 实现自己的方法时遇到了这个问题

def swap(a,b):
    temp=a
    a=b
    b=temp

list_=[5,4,6,3,7]
swap(list_[4],list_[2])
Run Code Online (Sandbox Code Playgroud)

我expexted list_与更新swap()的呼叫,因为list_[4]list_[2]对函数调用期间被分配新值.但是,list_保持不变:

list_
   [5, 4, 6, 3, 7]
Run Code Online (Sandbox Code Playgroud)

我误解了为什么交换函数调用正在处理副本.我不想在我的交换函数中添加一个列表参数,也不想在swap()调用期间返回列表,因为我希望该方法适用于其他数据结构,例如

 swap(mat[0][1],mat[2,3])
Run Code Online (Sandbox Code Playgroud)

python arguments pass-by-reference pass-by-value data-structures

0
推荐指数
2
解决办法
812
查看次数

Qt QList无法按预期工作

我正在构建QList<QList<double>> *以下方式,在函数中返回randomPoint():

QList<QList<double>> *solverMethod::randomPoint(double* bottom_, double* top_, int items_)
{  
    QList<QList<double>> *lstPt_ = new QList<QList<double>>;

         for(int i=0;i<items_;i++)
        {   
            QList<double>  pt_;
            lstPt_->append(pt_);
            for(int j=0;j<m_ndim;j++)
            {
                pt_.append(TRandom::rand(bottom_[j],top_[j]));
            }
         }
        return lstPt_;
 }
Run Code Online (Sandbox Code Playgroud)

但是在for循环之后有一个停止点,我注意到它pt_是正确填充的(m_ndim元素),而是lstPtitem_空填充QList<double>.怎么了?

c++ qt qlist

0
推荐指数
1
解决办法
971
查看次数

用户给定长度创建C++简单数组

如何在用户给定长度的方法中创建一个数组?我想用随机数填写用户给定长度的向量.

 double* Random::quasiRandomUnif(float a, float b, int NN) 
 {
     int i, errcode;

          N_=NN; // here initialize the member N_

          float r[N_];

     VSLStreamStatePtr stream;
     int i, errcode;

     errcode = vslNewStream( &stream, BRNG,  SEED );
     errcode = vsRngUniform( METHOD, stream, N_, r, a, b );

     double* rd = new double[m_N];
     for(int i=0;i<m_N;i++)
         rd[i]=(double)r[i];

     errcode = vslDeleteStream( &stream );

     return rd;
  }
Run Code Online (Sandbox Code Playgroud)

我认为N_是Random类的成员,要在这个函数的主体中初始化,有一些用户给定的值 - 不可能,因为在数组中分配的空间应该是一个常量.怎么处理?

亲切的问候.

c++ arrays

0
推荐指数
1
解决办法
268
查看次数

需要C++静态方法

我有一个方法run()成员MyClass.编译时,我明白了

    Error   3   error C2662: 'MyClass::run' : 
    cannot convert 'this' pointer from 'const MyClass' to 'MyClass&'
Run Code Online (Sandbox Code Playgroud)

ITOH,如果我把这个方法设为静态,我没有错误.方法调用发生在这里:

Errors MyClass::execute( const AbstractExecutionContext &ctx ) const
{
    Errors errs;

    Watch wat; wat.restart();
    {

        run() ;

    }

    return errs;
}
Run Code Online (Sandbox Code Playgroud)

和这种方法的声明是

Errors execute(const AbstractExecutionContext &ctx) const;
Run Code Online (Sandbox Code Playgroud)

我希望我可以使这个方法不是静态的,因为如果它是静态的,run()调用的方法也必须是静态的,并且无法访问非静态的数据成员(我必须丑陋地将它们作为参数传递给方法) .

编译错误的原因是什么,什么是解决方案?

c++ static static-methods const

0
推荐指数
1
解决办法
174
查看次数

NULL返回值类型为QList <float*>

我想为返回QList的方法返回一个默认的Null值(如果处理失败,我想返回).

如何正确构建Null QList?

问候

c++ null qt default qlist

0
推荐指数
1
解决办法
1508
查看次数