📄 vector.h
字号:
/**
Get the Real part of a Complex vector. Causes a runtime error if the
underlying base type of the vector is not Complex.
Examples:
vector<complex> vComplex = { 1+2i, 3+4i };
vector vReal1, vReal2;
vComplex.GetReal( vReal1 ); // vReal1 = { 1, 3 };
printf( "vReal1[0]=%g\nvReal1[1]=%g", vReal1[0], vReal1[1] );
vReal1.GetReal( vReal2 ); // Causes runtime error
Parameters:
vReal=Output vector containing the Real part of this Complex vector
Return:
Returns 0 on success and -1 on failure.
SeeAlso:
vectorbase::GetImaginary, vectorbase::GetPhase, vectorbase::GetAmplitude, vectorbase::Conjugate, vectorbase::MakeComplex
*/
int GetReal( vector & vReal ); // Get the Real part of a Complex vector.
/**
Get the Imaginary part of a Complex vector. Causes a runtime error if
the underlying base type of the vector is not Complex.
Examples:
vector<complex> vComplex = { 1+2i, 3+4i };
vector vImag1, vImag2;
vComplex.GetImaginary( vImag1 ); // vImag1 = { 2, 4 };
printf( "vImag1[0]=%g\nmImag1[1]=%g", vImag1[0], vImag1[1] );
vImag1.GetImaginary( vImag2 ); // Causes runtime error
Parameters:
vImag=Output vector containing the Imaginary part of this Complex vector
Return:
Returns 0 on success and -1 on failure.
SeeAlso:
vectorbase::GetReal, vectorbase::GetPhase, vectorbase::GetAmplitude, vectorbase::Conjugate, vectorbase::MakeComplex
*/
int GetImaginary( vector & vImag ); // Get the Imaginary part of a Complex vector.
/**
Get the amplitude(modulus) of the Complex vector. Causes a runtime
error if the underlying base type of the vector is not Complex.
Examples:
vector<complex> vComplex = { 1+2i, 3+4i };
vector vAmplitude1, vAmplitude2, vReal = { 1, 3 };
vComplex.GetAmplitude( vAmplitude1 );
printf( "vAmplitude1[0]=%g\nvAmplitude1[1]=%g", vAmplitude1[0], vAmplitude1[1] );
vReal.GetAmplitude( vAmplitude2 ); // Causes runtime error
Parameters:
vAmplitude=Output vector containing amplitude(modulus) vector for this Complex vector
Return:
Returns 0 on success and -1 on failure.
SeeAlso:
vectorbase::GetReal, vectorbase::GetImaginary, vectorbase::GetPhase, vectorbase::Conjugate, vectorbase::MakeComplex
*/
int GetAmplitude( vector & vAmplitude ); // Get the amplitude(modulus) of the Complex vector.
/**
Get the phase angle vector, in radians, of the Complex vector. Causes
a runtime error if the underlying base type of the vector is not Complex.
Examples:
vector<complex> vComplex = { 1+2i, 3+4i };
vector vPhase1, vPhase2, vReal = { 1, 3 };
vComplex.GetPhase( vPhase1 );
printf( "vPhase1[0]=%g\nvPhase1[1]=%g", vPhase1[0], vPhase1[1] );
vReal.GetPhase( vPhase2 ); // Causes runtime error
Parameters:
vPhase=Output vector containing the phase angle vector for this Complex vector
Return:
Returns 0 on success and -1 on failure.
SeeAlso:
vectorbase::GetReal, vectorbase::GetImaginary, vectorbase::GetAmplitude, vectorbase::Conjugate, vectorbase::MakeComplex
*/
int GetPhase( vector & vPhase ); // Get the phase angle vector, in radians, of the Complex vector.
/**
Replace this vector with the Conjugate of this vector. A runtime error
occurs if the underlying base type of this vector is not double or Complex.
Example:
vector<complex> vA(2);
vA[0] = 1+2i; vA[1] = 3;
vA.Conjugate();
for(int ii = 0; ii < 2; ii++)
printf("%g + %gi, ",vA[ii].m_re, vA[ii].m_im);
printf("\n");
Return:
Returns 0 on success and -1 on failure which occurs if the vector is empty.
SeeAlso:
vectorbase::GetReal, vectorbase::GetImaginary, vectorbase::GetPhase, vectorbase::GetAmplitude, vectorbase::MakeComplex
*/
int Conjugate(); // Replace this vector with the Conjugate of this vector.
/**
Make a Complex vector from two Real vectors (which must have the same
dimensions).
Example:
vector vA(2), vB(2);
vector<complex> vC(2);
vA[0] = 1; vB[0] = -2;
vA[1] = -3; vB[1] = 1;
vC.MakeComplex(vA,vB);
for(int ii = 0; ii < 2; ii++)
printf("%g + %gi\n",vC[ii].m_re,vC[ii].m_im);
Parameters:
vbReal=The vector containing the Real part
vbImag=The vector containing the Imaginary part
Return:
Returns 0 on success and -1 on failure.
SeeAlso:
vectorbase::GetReal, vectorbase::GetImaginary, vectorbase::GetPhase, vectorbase::GetAmplitude, vectorbase::Conjugate
*/
int MakeComplex(vectorbase & vbReal, vectorbase & vbImag); // Make a Complex vector from two Real vectors.
#if _OC_VER > 0x0703
/**#
Get a subset of this vector specified by 0 based element indices.
Example:
vector<double> v1, v2;
v2.SetSize(10);
for(int ii = 0; ii < 10; ii++ )
v2[ii] = 0.01*ii;
v2.GetSubVector(v1, 5, -1); // Copy half part of vector v2 into vector v1
ASSERT(v1.GetSize()==5);
ASSERT(is_equal(round(v1[0],2), 0.05));
ASSERT(is_equal(round(v1[1],2), 0.06));
ASSERT(is_equal(round(v1[2],2), 0.07));
Parameters:
vbTarget=vectorbase derived object containing returned vectorbase subset
c1=Begining element index, default is 0 (0 based offset)
c2=Ending element index, (inclusive) default -1 is GetSize() -1 (0 based offset)
Return:
Returns 0 on success and -1 on failure.
*/
int GetSubVector(vectorbase& vbTarget, int c1 = 0, int c2 = -1); // -1 means to last vector element index
/**#
Set a subset of this vector specified by 0 based element indices. The source and target
vectors can have different types and different dimensions but the target vector is never
resized even if the source vector is larger than the target.
Example:
vector<double> vec1[3];
vec1[1].SetSize(3);
vec1[1][0] = 0.01;
vec1[1][1] = 0.02;
vec1[1][2] = 0.03;
vector<double> vec2;
vec2.SetSize(3);
vec2.SetSubVector(vec1[1], 1);
ASSERT(is_equal(round(vec2[1],2), 0.01));
ASSERT(is_equal(round(vec2[2],2), 0.02));
vector<double> vSrc = { 1.1, 2.1};
vector<int> vDest = { 0, 0, 0, 0 };
vDest.SetSubVector(vSrc, 0 );
ASSERT(vDest[0] == 1);
ASSERT(vDest[1] == 2);
ASSERT(vDest[2] == 0);
ASSERT(vDest[3] == 0);
Parameters:
vbSource = vectorbase oject containing the subset to set
c1 = Begining element index in the target vector, default is 0 (0 based offset)
Return:
Returns 0 on success and -1 on failure.
*/
int SetSubVector(vectorbase& vbSource, int c1 = 0);
/**
do moving average for vector.
Parameters:
nLength: length of average window length, if it is not larger than 0, do nothing.
nMethod: type of moving average window, now, only support RECT_WINDOW, always set it
as RECT_WINDOW now.
Examples:
vector vv1 ={1,3,2,6,4,9,2,5,7};
vector vv2;
vv2.Average(vv1,3);
==> vv2=
1.3333 2.0000 3.6667 4.0000 6.3333 5.0000 5.3333 4.6667 4.0000
*/
void Average(vectorbase &vSource, int nLength, int nMethod = RECT_WINDOW);
/**#
Function to find all instances of a value in a vector (can be then used for dataset as well)
Example:
vector<double> vec = { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 };
vector<uint> vecIndex;
double dLower = 0.3;
double dUpper = _ONAN;
int nPrecision = 5;
int iBegin = 0;
int iEnd = 8;
vec.Find(vecIndex, dLower, dUpper, nPrecision, iBegin, iEnd);
ASSERT(vecIndex.GetSize() == 1);
ASSERT(vecIndex[0] == 2);
dUpper = 0.6;
vec.Find(vecIndex, dLower, dUpper, nPrecision, iBegin, iEnd);
ASSERT(vecIndex.GetSize() == 4);
ASSERT(vecIndex[0] == 2);
ASSERT(vecIndex[1] == 3);
ASSERT(vecIndex[2] == 4);
ASSERT(vecIndex[3] == 5);
iBegin = 4;
vec.Find(vecIndex, dLower, dUpper, nPrecision, iBegin, iEnd);
ASSERT(vecIndex.GetSize() == 2);
ASSERT(vecIndex[0] == 4);
ASSERT(vecIndex[1] == 5);
Parameters:
vecIndex = all indices of vecData where value was found.
dLower = the lower bound for the value to be found. Set this to exact value when you just want to search for a single value and not use bounds
dUpper = the upper bound for the value to be found. Leave it as default (_ONAN) if searching for only a single value
nPrecision = sets the level of precision to be used in the search. This parameter will be ignored if dUpper isn't the default one.
iBegin = the index of vecData from which the search should begin - leave as 0 to start from index 0
iEnd = the index of vecData up to which the search should be done - leave as -1 to seach to end of vecData
Return:
return -1 if no values were found, otherwise return "n" where n is the number of values found.
*/
int Find(vector<uint>& vecIndex, double dLower, double dUpper = _ONAN, int nPrecision = 8, int iBegin = 0, int iEnd = -1);
/**
Remove all missing values from the vectorbase derived object and resize acordingly shifting non-missing
data values up. Trim of Curve objects causes both X and Y Datasets to be trimmed.
Example:
Dataset ds("Data1_A"); // Containing missing values
ds.Trim();
Return:
Returns true on success and false on failure.
SeeAlso:
vectorbase::TrimLeft, vectorbase::TrimRight, vectorbase::GetLowerBound, vector::vector
*/
bool Trim();
/**
TrimLeft removes elements having the value NANUM from the left end or beginning of the
vectorbase derived object by advancing the lower index to the first valid numeric value.
If bShiftLeft is TRUE cells containing NANUM are deleted from the vectorbase object and
a lower index value of 0 is returned shifting numeric values left. bShiftLeft must be TRUE
for vectors or this function will have no effect. When bShiftLeft is TRUE TrimLeft of Curve
objects causes both X and Y Datasets to be trimmed.
Example:
// Worksheet columns Data1_A and data1_B must exist prior to execution
Dataset dsA("Data1",0);
dsA.TrimLeft(TRUE);
Curve crvAxBy("Data1_A","Data1_B");
crvAxBy.TrimLeft(TRUE);
LT_execute("doc -uw");
Parameters:
bShiftLeft=TRUE will delete cells on the left of (before) the first valid numeric value, FASLE will cause the lower bound
or index to be adjusted upward but no cells are deleted.
Return:
Returns -1 on error, 0 on success, and N > 0 for number of cells deleted.
SeeAlso:
vectorbase::Trim, vectorbase::TrimRight, vectorbase::GetLowerBound, vector::vector
*/
int TrimLeft(BOOL bShiftLeft = FALSE); // TrimLeft removes elements having the value NANUM from the left end of a Dataset.
/**
TrimRight removes elements having the value NANUM from the right end or
bottom of vectorbase derived objects by retarding the upper index to
the last valid numeric value. If bDelExtra is set to TRUE cells containing
NANUM are deleted from the vectorbase object. When bDelExtra is TRUE TrimRight
of Curve objects causes both X and Y Datasets to be trimmed.
Example:
// Worksheet columns Data1_A and data1_B must exist prior to execution
Dataset dsA("Data1",0);
dsA.TrimRight(TRUE);
Curve crvAxBy("Data1_A","Data1_B");
crvAxBy.TrimRight(TRUE);
LT_execute("doc -uw");
Parameters:
bDelExtra=TRUE will delete cells on the right of (after) the last valid numeric value, FASLE will cause the upper bound
or index to be adjusted downward but no cells are deleted.
Returns:
Returns -1 on error, 0 on success, and N > 0 for number of cells deleted.
SeeAlso:
vectorbase::Trim, vectorbase::TrimLeft, vectorbase::GetUpperBound, vectorbase::GetSize, vector::vector
*/
int TrimRight(BOOL bDelExtra = FALSE); // TrimRight removes elements having the value NANUM from the right end of a Dataset.
#endif // _OC_VER > 0x0703
/**
Removes all elements in the vector.
Examples:
vector<int> vnData = {1,2,3};
vnData.RemoveAll();
ASSERT(0 == vnData.GetSize());
*/
void RemoveAll();
};
/** >Composite Data Types
An Origin C vector is a dynamically allocated and sized array that is not
tied to an internal Origin data set allowing a greater degree of flexibility.
vector is a template class with a default type of double but a vector of any
basic data type (including char, byte, short, word, int, uint, string) can
be constructed using the syntax vector<type>. The vector class is derived from
the vectorbase class from which it inherits methods and properties.
Example:
int imax = 10;
vector<double> vX(imax); // vector of doubles having imax elements
vector vY(imax); // vector of doubles by default
vector<int> vI; // vector of ints
for(int ii = 0; ii < vX.GetSize(); ii++)
vX[ii] = rnd();
vY = 10 * vX; // vector arithmetic is supported
vI = vY; // Cast double vector into int vector
ASSERT(vI.GetSize() == imax); // Check that vectors have same size
for(ii = 0; ii < vI.GetSize(); ii++) // Output cast elements of vectors
printf("vI[%d]=%d after cast from %f\n",ii,vI[ii],vY[ii]);
*/
class vector : public vectorbase
{
public:
/**
Default constructor for vector class.
Example:
vector vD; // vector of doubles
vector<string> vS; // vector of strings
vector<int> vI; // vector of ints
*/
vector(); // Default constructor for vector class.
/**
Constructor to create a vector having size nSize.
Example:
vector vD(10); // vector of 10 doubles
vector<string> vS(10); // vector of 10 strings
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -