//
//
// Arrays.cpp
//
//
#include <Arrays.h>
#include<algorithm>
#include<numeric>
MJArray::MJArray(unsigned long size)
: Size(size), Capacity(size)
{
if (Size >0)
{
ValuesPtr = new double[size];
EndPtr = ValuesPtr;
EndPtr += size;
}
else
{
ValuesPtr=0;
EndPtr=0;
}
}
MJArray::MJArray(const MJArray& original)
:
Size(original.Size), Capacity(original.Size)
{
if (Size > 0)
{
ValuesPtr = new double[Size];
EndPtr = ValuesPtr;
EndPtr += Size;
std::copy(original.ValuesPtr, original.EndPtr, ValuesPtr);
}
else
{
ValuesPtr = EndPtr =0;
}
}
MJArray::~MJArray()
{
if (ValuesPtr >0)
delete [] ValuesPtr;
}
MJArray& MJArray::operator=(const MJArray& original)
{
if (&original == this)
return *this;
if (original.Size > Capacity)
{
if (Capacity > 0)
delete [] ValuesPtr;
ValuesPtr = new double[original.Size];
Capacity = original.Size;
}
Size=original.Size;
EndPtr = ValuesPtr;
EndPtr += Size;
std::copy(original.ValuesPtr, original.EndPtr, ValuesPtr);
return *this;
}
void MJArray::resize(unsigned long newSize)
{
if (newSize > Capacity)
{
if (Capacity > 0)
delete [] ValuesPtr;
ValuesPtr = new double[newSize];
Capacity = newSize;
}
Size = newSize;
EndPtr = ValuesPtr + Size;
}
MJArray& MJArray::operator+=(const MJArray& operand)
{
#ifdef RANGE_CHECKING
if ( Size != operand.size())
{
throw("to apply += two arrays must be of same size");
}
#endif
for (unsigned long i =0; i < Size; i++)
ValuesPtr[i]+=operand[i];
return *this;
}
MJArray& MJArray::operator-=(const MJArray& operand)
{
#ifdef RANGE_CHECKING
if ( Size != operand.size())
{
throw("to apply -= two arrays must be of same size");
}
#endif
for (unsigned long i =0; i < Size; i++)
ValuesPtr[i]-=operand[i];
return *this;
}
MJArray& MJArray::operator/=(const MJArray& operand)
{
#ifdef RANGE_CHECKING
if ( Size != operand.size())
{
throw("to apply /= two arrays must be of same size");
}
#endif
for (unsigned long i =0; i < Size; i++)
ValuesPtr[i]/=operand[i];
return *this;
}
MJArray& MJArray::operator*=(const MJArray& operand)
{
#ifdef RANGE_CHECKING
if ( Size != operand.size())
{
throw("to apply *= two arrays must be of same size");
}
#endif
for (unsigned long i =0; i < Size; i++)
ValuesPtr[i]*=operand[i];
return *this;
}
/////////////////////////////
MJArray& MJArray::operator+=(const double& operand)
{
for (unsigned long i =0; i < Size; i++)
ValuesPtr[i]+=operand;
return *this;
}
MJArray& MJArray::operator-=(const double& operand)
{
for (unsigned long i =0; i < Size; i++)
ValuesPtr[i]-=operand;
return *this;
}
MJArray& MJArray::operator/=(const double& operand)
{
for (unsigned long i =0; i < Size; i++)
ValuesPtr[i]/=operand;
return *this;
}
MJArray& MJArray::operator*=(const double& operand)
{
for (unsigned long i =0; i < Size; i++)
ValuesPtr[i]*=operand;
return *this;
}
MJArray& MJArray::operator=(const double& val)
{
for (unsigned long i =0; i < Size; i++)
ValuesPtr[i]=val;
return *this;
}
double MJArray::sum() const
{
return std::accumulate(ValuesPtr,EndPtr,0.0);
}
double MJArray::min() const
{
#ifdef RANGE_CHECKING
if ( Size==0)
{
throw("cannot take min of empty array");
}
#endif RANGE_CHECKING
double* tmp = ValuesPtr;
double* endTmp = EndPtr;
return *std::min_element(tmp,endTmp);
}
double MJArray::max() const
{
#ifdef RANGE_CHECKING
if ( Size==0)
{
throw("cannot take max of empty array");
}
#endif RANGE_CHECKING
double* tmp = ValuesPtr;
double* endTmp = EndPtr;
return *std::max_element(tmp,endTmp);
}
MJArray MJArray::apply(double f(double)) const
{
MJArray result(size());
std::transform(ValuesPtr,EndPtr,result.ValuesPtr,f);
return result;
}
/*
*
* Copyright (c) 2002
* Mark Joshi
*
* Permission to use, copy, modify, distribute and sell this
* software for any purpose is hereby
* granted without fee, provided that the above copyright notice
* appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
* Mark Joshi makes no representations about the
* suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
*/
没有合适的资源?快使用搜索试试~ 我知道了~
c++设计模式&&源代码
共105个文件
cpp:60个
h:45个
需积分: 9 30 下载量 15 浏览量
2008-11-26
23:05:39
上传
评论
收藏 1.02MB RAR 举报
温馨提示
Designpatternsarethecutting-edgeparadigmforprogramminginobject-orientedlan guages.Heretheyarediscussedinthecontextofimplementing?nancialmodelsinC AssumingonlyabasicknowledgeofC andmathematical?nance,thereaderistaugh howtoproducewell-designed,structured,reusablecodeviaconcreteexamples.
资源推荐
资源详情
资源评论
收起资源包目录
c++设计模式&&源代码 (105个子文件)
Arrays.cpp 5KB
BlackScholesFormulas.cpp 3KB
TreeMain.cpp 3KB
BinomialTree.cpp 3KB
Normals.cpp 3KB
RandomMain2.cpp 3KB
VanillaMain4.cpp 2KB
RandomMain3.cpp 2KB
ExoticBSEngine.cpp 2KB
Parameters.cpp 2KB
RandomMain1.cpp 2KB
VanillaMain3.cpp 2KB
VanillaMain2.cpp 2KB
ParkMiller.cpp 2KB
StatsMain2.cpp 2KB
EquityFXMain.cpp 2KB
SimpleMCMain1.cpp 2KB
StatsMain1.cpp 2KB
SimpleMCMain2.cpp 2KB
SimpleMCMain3.cpp 2KB
ExoticEngine.cpp 2KB
SolveMain2.cpp 2KB
PathDependentAsian.cpp 2KB
SimpleMCMain4.cpp 2KB
ConvergenceTable.cpp 2KB
SimpleMC8.cpp 2KB
VanillaMain1.cpp 2KB
SolveMain1.cpp 2KB
SimpleMC7.cpp 2KB
SimpleMCMain5.cpp 2KB
AntiThetic.cpp 2KB
SimpleMC3.cpp 2KB
SimpleMC4.cpp 2KB
SimpleMC2.cpp 1KB
SimpleMC.cpp 1KB
Vanilla2.cpp 1KB
SimpleMC5.cpp 1KB
PayOffFactory.cpp 1KB
PayFactoryMain.cpp 1KB
Random1.cpp 1KB
TreeAmerican.cpp 1KB
TreeEuropean.cpp 1KB
Random2.cpp 1KB
SimpleMC6.cpp 1KB
MCStatistics.cpp 1KB
BSCallTwo.cpp 1KB
PayOffBridge.cpp 1KB
DoubleDigital2.cpp 1KB
PayOff3.cpp 1KB
Random3.cpp 1015B
PayOff1.cpp 1014B
DoubleDigital.cpp 989B
BSCallClass.cpp 976B
PayOff2.cpp 940B
Vanilla3.cpp 911B
Vanilla1.cpp 904B
PayOffForward.cpp 846B
PathDependent.cpp 813B
TreeProducts.cpp 761B
PayOffRegistration.cpp 736B
Arrays.h 2KB
Parameters.h 2KB
wrapper.h 2KB
BlackScholesFormulas.h 2KB
PathDependent.h 1KB
ParkMiller.h 1KB
PathDependentAsian.h 1KB
BinomialTree.h 1KB
ExoticBSEngine.h 1KB
PayOff3.h 1KB
Random2.h 1KB
ExoticEngine.h 1KB
PayOffFactory.h 1KB
MCStatistics.h 1KB
PayOffConstructible.h 1KB
AntiThetic.h 1KB
PayOff2.h 1KB
TreeEuropean.h 1KB
TreeAmerican.h 1KB
ConvergenceTable.h 1KB
NewtonRaphson.h 1KB
Bisection.h 1KB
TreeProducts.h 1KB
PayOffBridge.h 1KB
Random3.h 1KB
DoubleDigital2.h 1KB
Vanilla2.h 1KB
SimpleMC8.h 999B
DoubleDigital.h 992B
BSCallTwo.h 955B
SimpleMC7.h 946B
BSCallClass.h 926B
PayOffForward.h 910B
Vanilla3.h 896B
Vanilla1.h 885B
PayOff1.h 882B
SimpleMC6.h 869B
minmax.h 851B
SimpleMC2.h 841B
SimpleMC.h 840B
共 105 条
- 1
- 2
资源评论
yd711
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功