最近读过在数值计算中fortran比c/c ++更快的主要原因是因为没有指针别名.
显然,使用restrict
或__restrict__
关键字允许根据具体情况来指示给定存储器元素不存在指针别名.
icc编译器显然有一个选项-fno-alias
,允许全局假设不存在别名.在gcc上-fno-strict-aliasing
,它仅适用于所有别名情况的子集.
gcc中是否存在选项,或者在使用某些优化标志时是否存在未假设别名的情况?
我想-O0
在Rcpp项目的Makevars中设置C++编译器标志.
如果我看一下/etc/R/Makeconf
,我看到编译命令似乎是
$(CXX) $(ALL_CPPFLAGS) $(ALL_CXXFLAGS) -c $< -o $@
Run Code Online (Sandbox Code Playgroud)
以来
ALL_CXXFLAGS = $(R_XTRA_CXXFLAGS) $(PKG_CXXFLAGS) $(CXXPICFLAGS) $(SHLIB_CXXFLAGS) $(CXXFLAGS)
Run Code Online (Sandbox Code Playgroud)
我可以在Makevars中编辑变量$(PKG_CXXFLAGS)
来为特定库添加标题,但我不满意CXXFLAGS = -O3 -pipe -g $(LTO)
.我也希望能够直接在Makevars中做到这一点,以便根据我的需要调整每个项目.
当我CXXFLAGS
在Makevar中编辑时,没有任何反应.有可能调整那个变量吗?另一种方法可行吗?我知道我可以编辑~/.R/Makevars
,并按要求切换.我想知道是否有更强大的方法.
我有一个表单的模板类:
template<typename ContainerType>
class ConfIntParamStat {
public:
typedef typename ContainerType::Type Type;
...
private:
void sample(int iteration) {...}
}
Run Code Online (Sandbox Code Playgroud)
我想为ContainerType是Vector的情况创建一个特定版本的函数示例.Vector本身是一个模板类,但我不知道这个Vector包含哪种类型的值.
我的直觉是在头文件中创建它:
template<typename Type>
ConfIntParamStat<Vector<Type> >::sample(int iteration) {
...
}
Run Code Online (Sandbox Code Playgroud)
但是它没有编译,而clang的错误是:
error: nested name specifier 'ConfIntParamStat<Vector<Type> >::' for declaration does not refer into a class, class template or class template partial specialization
Run Code Online (Sandbox Code Playgroud)
是否可以使用其他语法?
如果我递归地定义斐波那契数列:
fibo_lazy_list = 0 : 1 : zipWith (+) fibo_lazy_list (tail fibo_lazy_list)
Run Code Online (Sandbox Code Playgroud)
然后询问给定值上方的第一个元素,例如:
print $ find (>100) fibo_lazy_list
Run Code Online (Sandbox Code Playgroud)
我知道 Haskell 只评估获得打印结果所需的元素。但是它们是否都保存在内存中直到打印出来?由于只需要列表的两个元素来计算最后一个元素,Haskell 是释放最左边的元素还是列表在内存中不断增长?
我有一个派生自Eigen模板的Matrix类:
template<typename T,
int _Rows = Eigen::Dynamic,
int _Cols = Eigen::Dynamic>
class Matrix : public Eigen::Matrix<T, _Rows, _Cols>
Run Code Online (Sandbox Code Playgroud)
我需要使用这种类型作为std::map
容器的键,因此我需要一个比较器对象.我想专门std::less
为此目的.没有编译的草稿版本看起来像这样,为了让你明白:
template<typename Matrix<typename T,
int _Rows = Eigen::Dynamic,
int _Cols = Eigen::Dynamic> > >
struct less
{
bool operator()(const Matrix<T,
Rows,
Cols>& lhs,
const Matrix<T,
Rows,
Cols>& rhs) const;
{
Matrix<T,
Rows,
Cols>::const_iterator lhsIt = lhs.begin();
Matrix<T,
Rows,
Cols>::const_iterator rhsIt = rhs.begin();
for (;
lhsIt != lhs.end();
++lhsIt, ++rhsIt)
{
if (*lhsIt < *rhsIt)
{
return true;
} …
Run Code Online (Sandbox Code Playgroud) 我想使用一个库(nlopt),它有一个函数set_min_objective,它接受一个指向数值函数myfunc的指针并找到它的最小值.我想创建一个包含适当初始化成员函数的类.然后set_min_objective将在特定实例中找到最佳值(下例中的myP).呼叫顺序是:
opt.set_min_objective(myfunc, NULL);
Run Code Online (Sandbox Code Playgroud)
我想使用类似的东西:
opt.set_min_objective(myP.f, NULL);
Run Code Online (Sandbox Code Playgroud)
编译时得到的错误是:
main.cpp: In function 'int main()':
main.cpp:79:34: error: no matching function for call to 'nlopt::opt::set_min_objective(<unresolved overloaded function type>, NULL)'
../lib/nlopt.hpp:335:10: note: candidates are: void nlopt::opt::set_min_objective(double (*)(unsigned int, const double*, double*, void*), void*)
../lib/nlopt.hpp:342:10: note: void nlopt::opt::set_min_objective(double (*)(const std::vector<double>&, std::vector<double>&, void*), void*)
../lib/nlopt.hpp:368:10: note: void nlopt::opt::set_min_objective(double (*)(unsigned int, const double*, double*, void*), void*, void* (*)(void*), void* (*)(void*))
Run Code Online (Sandbox Code Playgroud)
set_min_objective接受myP.f作为函数的普通指针最简单的解决方案是什么?请注意,myP.f和myfunc具有相同的参数和返回值类型.
谢谢,
JD
在Bartosz Milewski 的《程序员分类理论》一书中,第 4.3 章。
您必须编写一个 Kleisli 范畴,其中态射是偏函数。这是我无法编译的尝试:
data Optional a = Valid a | Invalid deriving (Show)
return :: a -> Optional a
return x = Valid x
(>=>) :: (a -> Optional b) -> (b -> Optional c) -> (a -> Optional c)
f (>=>) g = \x ->
let s = f x
in | s == Valid v = g v
| s == Invalid = Invalid
Run Code Online (Sandbox Code Playgroud)
在>=>
运算符定义中,我想对中间值进行模式匹配s
以测试是否为Valid …
我在编程练习中编写了以下代码:
q = lenP[0]*[lenP[1]*[0.]] # empty 2D array
for i in range(lenP[0]):
for j in range(lenP[1]):
hit = (Z == colors[i][j])
q[i][j] = (hit * sensor_right + (1-hit) * (1.-sensor_right)) * p[i][j]
Run Code Online (Sandbox Code Playgroud)
如果我在循环内测试它,则每个元素q [i] [j]的值都被正确设置.但是,如果我在循环之外打印任何q [i] [j],它已恢复到它的初始值0.我想我已经错过了对对象的python引用的管理,但那会是什么?
在正常设置中,我会使用numpy作为数组,并使用它,但这是一个Udacity课程的代码(IA for Robotics,顺便说一句非常有趣),并且不允许导入任何东西.
如果我定义:
class Contravariant f where
contramap :: (b -> a) -> f a -> f b
type Op r a = (->) a r
Run Code Online (Sandbox Code Playgroud)
并想定义:
instance Contravariant (Op r) where
contramap f g = g . f
Run Code Online (Sandbox Code Playgroud)
我得到实例类型不能是同义词的错误。所以我想我需要类似的东西:
instance Contravariant ((->) _ r) where
contramap f g = g . f
Run Code Online (Sandbox Code Playgroud)
这当然不起作用。我怎样才能让这个 Contravariant 实例工作?
我想使用 hspec 创建一些具有不同值的测试。我写了下面的代码,它不能编译但给出了我的目标:
spec :: Spec
spec = do
describe "productOneLine" $ do
let
inVector = Data.Vector.replicate 0 0
inInteger = 3
outVector = Data.Vector.replicate 1 0
in
it "must manage empty vector" $ productOneLine inVector inInteger `shouldBe` outVector
let
inVector = Data.Vector.fromList [2, 4, 5]
inInteger = 4
outVector = Data.Vector.fromList [9, 6, 1, 2]
in
it "must multiply a vector by an integer" $ productOneLine inVector inInteger `shouldBe` outVector
Run Code Online (Sandbox Code Playgroud)
如何为每个以 开头的 ligne创建不同的inVector
, inInteger
et集?outVector
it
阅读“程序员的范畴论”我试图为 Op 重新创建 Functor 类型类实例。
\n{-# LANGUAGE TypeSynonymInstances #-}\n\nmodule Type.Op where\n\nimport Data.Functor.Contravariant ( Contravariant, contramap )\n\ntype Op r a = a -> r\n-- data Op r a = (->) a r\n\ninstance Contravariant (Op r) where\n contramap f g = g . f\n
Run Code Online (Sandbox Code Playgroud)\n编译产生以下错误:
\n \xe2\x80\xa2 The type synonym \xe2\x80\x98Op\xe2\x80\x99 should have 2 arguments, but has been given 1\n \xe2\x80\xa2 In the instance declaration for \xe2\x80\x98Contravariant (Op r)\xe2\x80\x99\n |\n10 | instance Contravariant (Op r) where\n | ^^^^^^^^^^^^^^^^^^^^\n
Run Code Online (Sandbox Code Playgroud)\n我应该怎么办 …