小编jor*_*gen的帖子

球体上的蒙特卡罗模拟:无偏随机步骤

我正在对球体上的粒子进行大都会蒙特卡罗模拟,并且在给定的时间步长中有关于随机运动的问题.

我知道要在球体上获得随机点的均匀分布以开始它是不够的,使用天真最简单的方法(使用具有常数R的球面坐标并选择随机角度theta和phi),但是必须使用例如其中一种方法:http://mathworld.wolfram.com/SpherePointPicking.html

看一下球体上蒙特卡罗的另一个代码,我看到了一种生成随机移动的相当复杂的方法:选择一个随机粒子,计算将其移动到北极的旋转矩阵,找到一个小于一定长度的随机笛卡尔向量并移动它到北极,标准化笛卡尔矢量,然后将其旋转回原始粒子位置附近.

这是为了获得一个无偏见的新随机位置.虽然我怀疑它与详细的平衡有关,但我完全不理解这个理由.但我的感觉是应该有一种更容易(即更快)的方法来做到这一点.实际上,直觉上我觉得在这种情况下可以找到两个小的随机角度theta和phi并将它们添加到粒子的位置 - 或者这是一个错误?

montecarlo

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

替换 ByteString 中的换行符

我想一个函数,它接受一个字节串并替换换行\n,并\n\r用逗号,但想不到的一个很好的方式来做到这一点。

import qualified Data.ByteString as BS
import Data.Char (ord) 
import Data.Word (Word8)

endlWord8 = fromIntegral $ ord '\n' :: Word8

replace :: BS.ByteString -> BS.ByteString
Run Code Online (Sandbox Code Playgroud)

我想过使用BS.map但不知道如何使用,因为我无法在Word8's上进行模式匹配。另一种选择是BS.split然后加入 Word8 逗号,但这听起来又慢又不雅。有任何想法吗?

haskell replace pattern-matching bytestring

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

找不到pip安装的pylint

围绕此有很多问题,因此可能是重复的,但是我找不到解决方案,所以这里是。

我想在原子上使用pylint。如果我使用推荐的方法,pip install --user pylint它似乎可以工作,但是原子找不到,我也不能;which pylintwhereis pylint返回任何结果。如果我使用,也会发生同样的事情pip3

如果我违背智慧并使用sudo pip install pylint发现,但现在我得到原子不同的错误:unable to determine environment

有什么建议么?

macos pip pylint atom-editor

5
推荐指数
2
解决办法
2133
查看次数

c ++浮点减法误差和绝对值

我理解它的方式是:当double在c ++中用双精度减去两个数时,它们首先被转换为有效数,从指数的2次开始.如果减去的数字在有效数字中具有相同的指数和许多相同的数字,则会出现错误,从而导致精度损失.为了测试我的代码,我编写了以下安全添加功能:

double Sadd(double d1, double d2, int& report, double prec) {
    int exp1, exp2;
    double man1=frexp(d1, &exp1), man2=frexp(d2, &exp2);
    if(d1*d2<0) {
        if(exp1==exp2) {
            if(abs(man1+man2)<prec) {
                cout << "Floating point error" << endl;
                report=0;
            }
        }
    }
    return d1+d2;
}
Run Code Online (Sandbox Code Playgroud)

然而,测试这个我注意到一些奇怪的事情:似乎实际错误(不是函数报告错误而是计算产生的实际错误)似乎取决于减去数字的绝对值而不仅仅是相等数字的数字在有意义的...

例如,使用1e-11精度prec并减去以下数字:

1)9.8989898989898-9.8989898989897:该函数报告错误,我得到高度不正确的值9.9475983006414e-14

2)98989898989898-98989898989897:该函数报告错误但我得到正确的值1

显然我误解了一些事情.有任何想法吗?

c++ floating-point rounding-error floating-accuracy subtraction

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

Cabal 安装标准内存不足

我正在一个具有 768MB 内存和 512 MB 交换空间的容器上运行。我不能增加这两个。cabal install criterion总是给

Failed during the building phase.
The exception was: ExitFailure (-9)
This may be due to an out-of-memory condition.
Run Code Online (Sandbox Code Playgroud)

期间Compiling Criterion.Types。有什么办法可以解决这个问题,还是我必须不加标准地凑合?

memory installation haskell cabal haskell-criterion

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

通过子图获得图中的刻度

我有一个复杂的数字在matplotlib制作有三个子图,其中两个有双轴.它运作良好,除了图中没有刻度标记,我无法弄清楚如何获得它们.

这是代码和图:

fig = plt.figure()
gs = gridspec.GridSpec(3, 1)
idx=0
n=len(d_10[idx])
fac=0.2

ax1=fig.add_subplot(gs[0,0])
ax1.errorbar(range(n),d_10[idx]["abs(Overlap)"][0:n],yerr=d_10[idx]["ErrOverlap"][0:n],ms=4,mew=0.7,marker="o",ls='none',elinewidth=0.7,capsize=3,fillstyle='none',color=qual_cp4[2])
ax1.plot([0],d_10[idx]["abs(Overlap)"][0],marker="o",ls='none',fillstyle='none',color=qual_cp4[2],ms=4,mew=0.4,label='d=0')
ax1.plot(range(n),np.sqrt(hc_10),marker="+",ls='none',color=qual_cp4[0],label='Laughlin')
ax1.axes.get_xaxis().set_ticklabels([])

plt.yscale('log', nonposy="clip") 
plt.axis([-2,101,1e-5,1.2])
plt.legend(loc='upper right')
yl1=plt.ylabel(r'$|\langle\psi_L^d|\phi_n\rangle|$')
plt.axis([-1,101,0.5e-5,2]);

ax2=fig.add_subplot(gs[1,0])
fac2=0.15
ex=corr*(18.5650511599/(2*10)-10/(2*np.sqrt(27.0/2)))
ax2.plot(range(n),energies[idx],color=qual_cp4[1])
yl2=plt.ylabel(r'$E_n$')
deltaE=abs(min(np.array(energies[idx]))-max(np.array(energies[idx])))
Emin=min(np.array(energies[idx]))-fac2*deltaE
Emax=max(np.array(energies[idx]))+fac2*deltaE
plt.axis([-1,n,Emin,Emax]);
plt.yticks(np.arange(Emin, Emax, abs(Emax-Emin)/4.0))
ax2.plot([0,n+1],[ex,ex],ls=':',color='r',lw=1)

ax3 = ax2.twinx()
fac3=0.4
ax3.plot([0],overlaps[idx][0],color=qual_cp4[1],label=r'$E_n$')
ax3.plot(range(n),overlaps[idx],ls="--",color=qual_cp4[3],label=r'$f$')
ax3.plot([0],overlaps[idx][0],ls=':',color='r',lw=1,label=r'$E_L$')
yl3=plt.ylabel(r'$f_n$') 
deltaov=abs(min(overlaps[idx])-max(overlaps[idx]))
ovmin=min(overlaps[idx])-fac3*deltaov
ovmax=max(overlaps[idx])+fac3*deltaov
plt.axis([-1,n,ovmin,ovmax]);
ax3.plot([0,n+1],[1,1],ls='-',color='k',lw=1)

ax2.axes.get_xaxis().set_ticklabels([]) 
ax3.axes.get_xaxis().set_ticklabels([]) 
plt.legend(loc='lower right',ncol=3)

ax4=fig.add_subplot(gs[2,0])
ex=corr*(18.5650511599/(2*10)-10/(2*np.sqrt(27.0/2)))
ax4.plot(range(n),np.absolute(energies[idx]-ex),color=qual_cp4[1],label=r'$E$')
yl2=plt.ylabel(r'$|E_n-E_L|$')
xl=plt.xlabel(r'$n$')
deltaE=abs(min(np.absolute(np.array(energies[idx])-ex))-max(np.absolute(np.array(energies[idx])-ex)))
Emin=min(np.absolute(np.array(energies[idx])-ex))-fac*deltaE
Emax=max(np.absolute(np.array(energies[idx])-ex))+fac*deltaE
#plt.yticks(np.arange(Emin, Emax, abs(Emax-Emin)/4.0))
plt.yscale('log', nonposy="clip") 

currov=np.abs(np.array(overlaps[idx])-1.0)

ax5 = ax4.twinx()
ax5.plot([0],currov[0],color=qual_cp4[1],label=r'$E$')
ax5.plot(range(n),currov,ls="--",color=qual_cp4[3],label=r'$f$')
yl5=plt.ylabel(r'$|f_n-1|$') 
deltaov=abs(min(currov)-max(currov))
ovmin=min(currov)-fac*deltaov
ovmax=max(currov)+fac*deltaov

plt.yscale('log', nonposy="clip") 
plt.legend(loc='upper …
Run Code Online (Sandbox Code Playgroud)

python axes matplotlib subplot

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

以常见方式作为实例的一组函数

我对haskell很新,我认为我陷入了一些OO陷阱.这是一个结构(简化)的草图,我在实施时遇到了麻烦:

  • Observable的一个概念,它作用于样本列表(Int)以产生结果(Int)

    • 使用某种模式实现结果的SimpleObservable概念(虽然会有Observables以其他方式执行),例如平均值
  • 一个函数实例,例如一个平均值乘以常数的函数实例

我的第一个想法是使用一个子类; 类似的东西(以下是有点人为但有希望得到的想法)

class Observable a where
  estimate :: a -> [Int] -> Int

class (Observable a) => SimpleObservable a where
  compute :: a -> Int -> Int
  simpleEstimate :: a -> [Int] -> Int
  simpleEstimate obs list = sum $ map compute list

data AveConst = AveConst Int

instance Observable AveConst where
  estimate = simpleEstimate

instance SimpleObservable AveConst  where
  compute (AveConst c) x = c * x
Run Code Online (Sandbox Code Playgroud)

然而,即使像上面这样的东西编译它是丑陋的.谷歌搜索告诉我DefaultSignatures可能会有所帮助,因为我没有必要estimate = simpleEstimate为每个实例做些事情,但是从围绕它的讨论来看,这样做似乎是反模式.

另一种选择是没有子类,但是(具有相同的Observable …

haskell default class repeat subclassing

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

Makefile only working with relative paths on MSYS2 (WIndows)

I have a set of programs that compile fine on Ubuntu and I'm working on a setup to compile on Windows as well. I'm using MSYS2.

There is a main Makefile which looks like this:

#### Project main directory

DIR_MAIN=$(shell pwd)

#### Compile all

all:
    @$(MAKE) -C src/Misc DIR_MAIN=$(DIR_MAIN)
    @$(MAKE) -C src/CF_states DIR_MAIN=$(DIR_MAIN)
Run Code Online (Sandbox Code Playgroud)

etcetera. In src/ there are folders each who contain a specific Makefile for that project. They have the following structure:

#### Directories and flags

ifndef $(DIR_MAIN)
  DIR_MAIN=../.. …
Run Code Online (Sandbox Code Playgroud)

windows makefile path relative-path msys2

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

使用this-keyword编译程序

我正在尝试使用makefile编译其他人编写的程序,在使用它之前我没有时间学习程序中的所有内容 - 因此我对一些错误感到困惑.

第一个错误是

(...): In function `_ZN30RealTriDiagonalSymmetricMatrix14ResizeAndCleanEii':
/home/jorgen/diaghamsvn/DiagHam/src/Matrix/RealTriDiagonalSymmetricMatrix.cc:350: 
undefined reference to `GarbageFlag::Used()'
Run Code Online (Sandbox Code Playgroud)

RealTriDiagonalSymmetricMatrix.cc中的违规行是:

if (this->Flag.Used() == true)
Run Code Online (Sandbox Code Playgroud)

我查找GarbageFlag.h文件,它包含函数Used() ; 这是它的一部分:

class GarbageFlag
{

 private:

#ifdef __SMP__
pthread_mutex_t *FlagMutex;
#endif


public:

// default constructor
GarbageFlag();

// copy constructor
// flag = garbage flag to copy
GarbageFlag(const GarbageFlag& flag);

// destructor
~GarbageFlag();

// return value = reference on current garbage flag
GarbageFlag& operator = (const GarbageFlag& flag);

bool Used ();
Run Code Online (Sandbox Code Playgroud)

此外,RealTriDiagonalSymmetricMatrix.cc文件包含一个标题,该标题又包含GarbageFlag.h.所以我很困惑为什么它找不到这个功能.

此后有很多类似的错误.任何人都知道发生了什么事?

c++ compilation this header-files

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

定义ostream运算符的Friend函数

我想定义一个ostream运算符,以方便我输出type的变量alglib::complex。为了提供一个不包含alglib库的有效示例,我将重载complex<double>下面的输出(由于问题的早期版本,因此进行了澄清)。在头文件“ my_class.h”中,我有

using namespace std;
#include <complex>
#include <iostream>

class my_class {

    public:

    ostream& operator << (std::ostream& os, complex<double> a) {
        os << "(" << real(a) << "," << imag(a) << ")";
        return os;
    }

    void output(complex<double>);

    my_class() {}
    ~my_class() {}
};
Run Code Online (Sandbox Code Playgroud)

在源文件“ my_class.cpp”中,

#include "my_class.h"

void my_class::output(complex<double> cd) {
    cout << cd << endl;
}
Run Code Online (Sandbox Code Playgroud)

最后,我有一个主要的方法文件“ run_my_class.cpp”:

#include "my_class.h"

int main(int argc, const char* argv[]) {

    my_class obj;
    complex<double> cd=complex<double>(1.0,-1.0);
    obj.output(cd);

}
Run Code Online (Sandbox Code Playgroud)

我尝试使用编译

g++ -c …
Run Code Online (Sandbox Code Playgroud)

c++ class operators friend ostream

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

有效地将功能应用于所有对

我需要一个二阶函数pairApply,它将二元函数应用于f列表式结构的所有唯一对,然后以某种方式组合它们.示例/草图:

pairApply (+) f [a, b, c] = f a b + f a c + f b c
Run Code Online (Sandbox Code Playgroud)

一些研究让我相信Data.Vector.Unboxed可能会有很好的表现(我还需要快速访问特定元素); 这也是必要的Statistics.Sample,这将更加便利.

考虑到这一点,我有以下几乎编译:

import qualified Data.Vector.Unboxed as U      

pairElement :: (U.Unbox a, U.Unbox b)    
            => (U.Vector a)                    
            -> (a -> a -> b)                   
            -> Int                             
            -> a                               
            -> (U.Vector b)                    
pairElement v f idx el =
  U.map (f el) $ U.drop (idx + 1) v            

pairUp :: (U.Unbox a, U.Unbox b) …
Run Code Online (Sandbox Code Playgroud)

haskell vector fold

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