小编Lem*_*ing的帖子

比较Google Test或Google Mock中的Eigen矩阵

我想知道是否有一种很好的方法可以使用Google TestGoogle Mock测试两个Eigen矩阵的近似相等性.

采取以下测试用例作为一个简化的例子:我乘以2点复数值矩阵A,和B,并期望一个确定的结果C_expect.我C_actual = A * B使用Eigen 计算数值结果.现在,我想比较C_expect,和C_actual.现在,相应的代码如下所示:

#include <complex>
#include <Eigen/Dense>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

typedef std::complex<double> Complex;
typedef Eigen::Matrix2cd Matrix;

TEST(Eigen, MatrixMultiplication) {
    Matrix A, B, C_expect, C_actual;

    A << Complex(1, 1), Complex(2, 3),
         Complex(3, 2), Complex(4, 4);
    B << Complex(4, 4), Complex(3, 2),
         Complex(2, 3), Complex(1, 1);
    C_expect << Complex(-5, 20), Complex(0, 10),
                Complex(0, 40), Complex(5, …
Run Code Online (Sandbox Code Playgroud)

c++ unit-testing googletest eigen gmock

7
推荐指数
2
解决办法
4690
查看次数

在nd-array的一个轴上应用1D功能

我想要的是:

我想将1D函数应用于任意形状的ndarray,以便它修改某个轴.类似于中的axis论点numpy.fft.fft.

请看以下示例:

import numpy as np


def transf1d(f, x, y, out):
    """Transform `f(x)` to `g(y)`.

    This function is actually a C-function that is far more complicated
    and should not be modified. It only takes 1D arrays as parameters.    

    """
    out[...] = (f[None,:]*np.exp(-1j*x[None,:]*y[:,None])).sum(-1)


def transf_all(F, x, y, axis=-1, out=None):
    """General N-D transform.

    Perform `transf1d` along the given `axis`.

    Given the following:
      F.shape == (2, 3, 100, 4, 5)
      x.shape == (100,)
      y.shape == (50,)
      axis == …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy cython

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

使用二进制操作``constexpr`减少`std :: array`

我想写一个constexpr函数,它减少std::array了二进制操作的给定.即实现的功能

template <typename T, std::size_t N>
reduce(std::array<T, N>, binary_function);
Run Code Online (Sandbox Code Playgroud)

为了简单起见,我想从补充开始.例如

sum(std::array<int, 5>{{1,2,3,4,5}});  // returns 15.
Run Code Online (Sandbox Code Playgroud)

到目前为止我得到了什么.

我使用通常的索引技巧来索引数组元素.即生成一个int序列,可用于使用参数列表扩展进行索引.

template <int... Is>
struct seq {};
template <int I, int... Is>
struct gen_seq : gen_seq<I - 1, I - 1, Is...> {};
template <int... Is>
struct gen_seq<0, Is...> : seq<Is...> {};  // gen_seq<4> --> seq<0, 1, 2, 3>
Run Code Online (Sandbox Code Playgroud)

sum然后通过可变参数模板递归定义该函数.

// The edge-condition: array of one element.
template <typename T>
constexpr T sum(std::array<T, 1> arr, decltype(gen_seq<0>{})) {
    return std::get<0>(arr); …
Run Code Online (Sandbox Code Playgroud)

c++ arrays c++11

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

如何最好地表示点列表

我在2D空间中有一组点,我将其表示为秩2数组:

points = [0 0; 0 1; 1 0]
Run Code Online (Sandbox Code Playgroud)

它是一种有用的表示,因为它允许轻松访问点的x和y分量.例如

plot(x=points[:,1], y=points[:,2])
Run Code Online (Sandbox Code Playgroud)

但是,有时候最好将其points视为一个列表/一组点而不是一个矩阵.例如,我需要检查某个点(例如[0 1])是否是元素points.直接版本不起作用:

[0 1] in points  # is false
Run Code Online (Sandbox Code Playgroud)

相反,我必须手动扩展points到一个点列表:

[0 1] in [points[i,:] for i in 1:size(points)[1]]  # is true
Run Code Online (Sandbox Code Playgroud)

定义这样一组点,访问组件和检查成员资格的julian方法是什么?


更新:正如@Jubobs建议的那样,我继续并定义了自己的类型.事实证明,我实际上需要一个向量,所以我继续称之为Vec2而不是Point.

immutable Vec2{T<:Real}
    x :: T
    y :: T
end
Vec2{T<:Real}(x::T, y::T) = Vec2{T}(x, y)
Vec2(x::Real, y::Real) = Vec2(promote(x,y)...)

convert{T<:Real}(::Type{Vec2{T}}, p::Vec2) =
    Vec2(convert(T,p.x), convert(T,p.y))
convert{Tp<:Real, T<:Real, S<:Real}(::Type{Vec2{Tp}}, t::(T,S)) =
    Vec2(convert(Tp, t[1]), …
Run Code Online (Sandbox Code Playgroud)

julia

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

Idris中的多参数子类

受到这篇博文这段代码的启发,我想我会在Idris中使用它的接口(类型类)尝试一些类别理论.

我定义Category如下,工作正常:

interface Category (obj : Type) (hom : obj -> obj -> Type) where
  id : {a : obj} -> hom a a
  comp : {a, b, c : obj} -> hom b c -> hom a b -> hom a c
Run Code Online (Sandbox Code Playgroud)

然后,根据Verified模块的精神,我想我会定义一个经过验证的类别:

interface Category obj hom =>
          VerifiedCategory (obj : Type) (hom : obj -> obj -> Type) where
  categoryAssociativity : {a, b, c, d : obj} ->
                          (f : …
Run Code Online (Sandbox Code Playgroud)

idris

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

使用enable_if选择特征 - 与clang一起使用,但不与gcc一起使用

我的工作的通用件的(C++ 11)代码是应该一起工作boost::multi_array,Eigen::Matrix和n维阵列中的,可能是其它类型的.在几个点上,我需要访问给定数组类型的元素类型.boost数组包含一个名为的typedef Element,而Eigen数组包含一个名为的typedef Scalar.

我想要的是一个类型特征,它返回给定数组类型的元素类型.不幸的是,我不能只为所有可能的数组类型专门化trait类,因为Eigen使用表达式模板,因此,有无数种类型的特征矩阵.因此,我使用SFINAE和enable_if来实现我的特性.enable_if应该选择的标准是,类型是否具有被调用的typedef Element,还是被调用的类型Scalar.

为此,我实现了类型特征has_element,并has_scalar确定是否存在相应的typedef.我的实现受到关于类型要求的博客文章的启发.

template <class T>
using ToVoid = void;

template <class T, class Enable = void>
struct has_scalar : public std::false_type {};

template <class T>
struct has_scalar<T, ToVoid<typename T::Scalar>> : public std::true_type {};

template <class T, class Enable = void>
struct has_element : public std::false_type {};

template <class T>
struct has_element<T, ToVoid<typename T::Element>> : public std::true_type {};
Run Code Online (Sandbox Code Playgroud)

我们的想法是,false_type如果没有提供typedef …

c++ gcc templates clang c++11

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

用户定义类型的非类型模板参数

我正在尝试定义一个模板类,它具有用户定义类型的非类型模板参数.不幸的是,迄今没有成功.真正的代码有点过于冗长,但简化示例如下所示:

#include <iostream>

template <class T>
class Maybe {
    bool is_ = false;
    T value_;

  public:
    constexpr Maybe() = default;
    constexpr Maybe(T value) : is_(true), value_(value) {}

    constexpr bool is() const { return is_; }
};

template <Maybe<int> parm>
struct Test {
    void say() const {
        std::cout << "parm is " << (parm.is() ? "set" : "not set") << ".\n";
    }
};

int main() {
    Test<Maybe<int>{}> not_set;
    Test<Maybe<int>(2)> is_set;

    not_set.say();
    is_set.say();
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译此代码(使用Clang 3.4)时,我收到以下错误消息:

test.cc:15:22: error: a non-type template parameter …
Run Code Online (Sandbox Code Playgroud)

c++ templates constexpr c++11

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

标签 统计

c++ ×4

c++11 ×3

arrays ×2

templates ×2

clang ×1

constexpr ×1

cython ×1

eigen ×1

gcc ×1

gmock ×1

googletest ×1

idris ×1

julia ×1

numpy ×1

python ×1

unit-testing ×1