Skip to content

Commit

Permalink
Refs #4036 a few more methods on MDHistoWorkspace
Browse files Browse the repository at this point in the history
renamed some others.
  • Loading branch information
Janik Zikovsky committed Nov 7, 2011
1 parent e049713 commit 9db3b9f
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,30 @@ namespace MDEvents

// --------------------------------------------------------------------------------------------
MDHistoWorkspace & operator+=(const MDHistoWorkspace & b);
MDHistoWorkspace & operator+=(const Mantid::DataObjects::WorkspaceSingleValue & b);
void add(const MDHistoWorkspace & b);
void add(const signal_t signal, const signal_t error);

MDHistoWorkspace & operator-=(const MDHistoWorkspace & b);
MDHistoWorkspace & operator-=(const Mantid::DataObjects::WorkspaceSingleValue & b);
void subtract(const MDHistoWorkspace & b);
void subtract(const signal_t signal, const signal_t error);

MDHistoWorkspace & operator*=(const MDHistoWorkspace & b);
MDHistoWorkspace & operator*=(const Mantid::DataObjects::WorkspaceSingleValue & b);
void multiply(const MDHistoWorkspace & b);
void multiply(const signal_t signal, const signal_t error);

MDHistoWorkspace & operator/=(const MDHistoWorkspace & b);
MDHistoWorkspace & operator/=(const Mantid::DataObjects::WorkspaceSingleValue & b);
void divide(const MDHistoWorkspace & b);
void divide(const signal_t signal, const signal_t error);

MDHistoWorkspace & operator&=(const MDHistoWorkspace & b);
MDHistoWorkspace & operator|=(const MDHistoWorkspace & b);
MDHistoWorkspace & operator^=(const MDHistoWorkspace & b);

void operatorNot();
void log();
void log10();
void exp();
void power(double exponent);



Expand Down
179 changes: 147 additions & 32 deletions Code/Mantid/Framework/MDEvents/src/MDHistoWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,65 +372,82 @@ namespace MDEvents
* @return *this after operation */
MDHistoWorkspace & MDHistoWorkspace::operator+=(const MDHistoWorkspace & b)
{
checkWorkspaceSize(b, "+=");
add(b);
return *this;
}

//----------------------------------------------------------------------------------------------
/** Perform the += operation, element-by-element, for two MDHistoWorkspace's
*
* @param b :: workspace on the RHS of the operation
* */
void MDHistoWorkspace::add(const MDHistoWorkspace & b)
{
checkWorkspaceSize(b, "add");
for (size_t i=0; i<m_length; ++i)
{
m_signals[i] += b.m_signals[i];
m_errorsSquared[i] += b.m_errorsSquared[i];
}
return *this;
}

//----------------------------------------------------------------------------------------------
/** Perform the += operation with a scalar as the RHS argument
*
* @param b :: WorkspaceSingleValue (signal and error) as the RHS argument
* @return *this after operation */
MDHistoWorkspace & MDHistoWorkspace::operator+=(const Mantid::DataObjects::WorkspaceSingleValue & b)
* @param signal :: signal to apply
* @param error :: error (not squared) to apply
* */
void MDHistoWorkspace::add(const signal_t signal, const signal_t error)
{
signal_t signal = b.dataY(0)[0];
signal_t errorSquared = b.dataE(0)[0];
errorSquared *= errorSquared;
signal_t errorSquared = error * error;
for (size_t i=0; i<m_length; ++i)
{
m_signals[i] += signal;
m_errorsSquared[i] += errorSquared;
}
return *this;
}


//----------------------------------------------------------------------------------------------
/** Perform the -= operation, element-by-element, for two MDHistoWorkspace's
*
* @param b :: workspace on the RHS of the operation
* @return *this after operation */
MDHistoWorkspace & MDHistoWorkspace::operator-=(const MDHistoWorkspace & b)
{
checkWorkspaceSize(b, "-=");
subtract(b);
return *this;
}

//----------------------------------------------------------------------------------------------
/** Perform the -= operation, element-by-element, for two MDHistoWorkspace's
*
* @param b :: workspace on the RHS of the operation
* */
void MDHistoWorkspace::subtract(const MDHistoWorkspace & b)
{
checkWorkspaceSize(b, "subtract");
for (size_t i=0; i<m_length; ++i)
{
m_signals[i] -= b.m_signals[i];
m_errorsSquared[i] += b.m_errorsSquared[i];
}
return *this;
}

//----------------------------------------------------------------------------------------------
/** Perform the -= operation with a scalar as the RHS argument
*
* @param b :: WorkspaceSingleValue (signal and error) as the RHS argument
* @return *this after operation */
MDHistoWorkspace & MDHistoWorkspace::operator-=(const Mantid::DataObjects::WorkspaceSingleValue & b)
* @param signal :: signal to apply
* @param error :: error (not squared) to apply
* */
void MDHistoWorkspace::subtract(const signal_t signal, const signal_t error)
{
signal_t signal = b.dataY(0)[0];
signal_t errorSquared = b.dataE(0)[0];
errorSquared *= errorSquared;
signal_t errorSquared = error * error;
for (size_t i=0; i<m_length; ++i)
{
m_signals[i] -= signal;
m_errorsSquared[i] += errorSquared;
}
return *this;
}

//----------------------------------------------------------------------------------------------
Expand All @@ -443,7 +460,21 @@ namespace MDEvents
* @return *this after operation */
MDHistoWorkspace & MDHistoWorkspace::operator*=(const MDHistoWorkspace & b_ws)
{
checkWorkspaceSize(b_ws, "*=");
multiply(b_ws);
return *this;
}

//----------------------------------------------------------------------------------------------
/** Perform the *= operation, element-by-element, for two MDHistoWorkspace's
*
* Error propagation of \f$ f = a * b \f$ is given by:
* \f$ df^2 = f^2 * (da^2 / a^2 + db^2 / b^2) \f$
*
* @param b :: workspace on the RHS of the operation
* */
void MDHistoWorkspace::multiply(const MDHistoWorkspace & b_ws)
{
checkWorkspaceSize(b_ws, "multiply");
for (size_t i=0; i<m_length; ++i)
{
signal_t a = m_signals[i];
Expand All @@ -458,7 +489,6 @@ namespace MDEvents
m_signals[i] = f;
m_errorsSquared[i] = df2;
}
return *this;
}

//----------------------------------------------------------------------------------------------
Expand All @@ -467,12 +497,13 @@ namespace MDEvents
* Error propagation of \f$ f = a * b \f$ is given by:
* \f$ df^2 = f^2 * (da^2 / a^2 + db^2 / b^2) \f$
*
* @param b_ws :: WorkspaceSingleValue (signal and error) as the RHS argument
* @param signal :: signal to apply
* @param error :: error (not squared) to apply
* @return *this after operation */
MDHistoWorkspace & MDHistoWorkspace::operator*=(const Mantid::DataObjects::WorkspaceSingleValue & b_ws)
void MDHistoWorkspace:: multiply(const signal_t signal, const signal_t error)
{
signal_t b = b_ws.dataY(0)[0];
signal_t db2 = b_ws.dataE(0)[0] * b_ws.dataE(0)[0];
signal_t b = signal;
signal_t db2 = error * error;
signal_t db2_relative = db2 / (b*b);
for (size_t i=0; i<m_length; ++i)
{
Expand All @@ -485,7 +516,6 @@ namespace MDEvents
m_signals[i] = f;
m_errorsSquared[i] = df2;
}
return *this;
}

//----------------------------------------------------------------------------------------------
Expand All @@ -498,7 +528,22 @@ namespace MDEvents
* @return *this after operation */
MDHistoWorkspace & MDHistoWorkspace::operator/=(const MDHistoWorkspace & b_ws)
{
checkWorkspaceSize(b_ws, "/=");
divide(b_ws);
return *this;
}


//----------------------------------------------------------------------------------------------
/** Perform the /= operation, element-by-element, for two MDHistoWorkspace's
*
* Error propagation of \f$ f = a / b \f$ is given by:
* \f$ df^2 = f^2 * (da^2 / a^2 + db^2 / b^2) \f$
*
* @param b_ws :: workspace on the RHS of the operation
**/
void MDHistoWorkspace::divide(const MDHistoWorkspace & b_ws)
{
checkWorkspaceSize(b_ws, "divide");
for (size_t i=0; i<m_length; ++i)
{
signal_t a = m_signals[i];
Expand All @@ -513,21 +558,22 @@ namespace MDEvents
m_signals[i] = f;
m_errorsSquared[i] = df2;
}
return *this;
}



//----------------------------------------------------------------------------------------------
/** Perform the /= operation with a scalar as the RHS argument
*
* Error propagation of \f$ f = a / b \f$ is given by:
* \f$ df^2 = f^2 * (da^2 / a^2 + db^2 / b^2) \f$
*
* @param b_ws :: WorkspaceSingleValue (signal and error) as the RHS argument
* @return *this after operation */
MDHistoWorkspace & MDHistoWorkspace::operator/=(const Mantid::DataObjects::WorkspaceSingleValue & b_ws)
**/
void MDHistoWorkspace::divide(const signal_t signal, const signal_t error)
{
signal_t b = b_ws.dataY(0)[0];
signal_t db2 = b_ws.dataE(0)[0] * b_ws.dataE(0)[0];
signal_t b = signal;
signal_t db2 = error * error;
signal_t db2_relative = db2 / (b*b);
for (size_t i=0; i<m_length; ++i)
{
Expand All @@ -540,7 +586,6 @@ namespace MDEvents
m_signals[i] = f;
m_errorsSquared[i] = df2;
}
return *this;
}


Expand Down Expand Up @@ -613,6 +658,76 @@ namespace MDEvents
}
}

//----------------------------------------------------------------------------------------------
/** Perform the natural logarithm on each signal in the workspace.
*
* Error propagation of \f$ f = ln(a) \f$ is given by:
* \f$ df^2 = a^2 / da^2 \f$
*/
void MDHistoWorkspace::log()
{
for (size_t i=0; i<m_length; ++i)
{
signal_t a = m_signals[i];
signal_t da2 = m_errorsSquared[i];
m_signals[i] = std::log(a);
m_errorsSquared[i] = da2 / (a*a);
}
}

//----------------------------------------------------------------------------------------------
/** Perform the base-10 logarithm on each signal in the workspace.
*
* Error propagation of \f$ f = ln(a) \f$ is given by:
* \f$ df^2 = a^2 / da^2 \f$
*/
void MDHistoWorkspace::log10()
{
for (size_t i=0; i<m_length; ++i)
{
signal_t a = m_signals[i];
signal_t da2 = m_errorsSquared[i];
m_signals[i] = std::log10(a);
m_errorsSquared[i] = da2 / (a*a);
}
}

//----------------------------------------------------------------------------------------------
/** Perform the exp() function on each signal in the workspace.
*
* Error propagation of \f$ f = exp(a) \f$ is given by:
* \f$ df^2 = f^2 * da^2 \f$
*/
void MDHistoWorkspace::exp()
{
for (size_t i=0; i<m_length; ++i)
{
signal_t f = std::exp(m_signals[i]);
signal_t da2 = m_errorsSquared[i];
m_signals[i] = f;
m_errorsSquared[i] = f*f * da2;
}
}

//----------------------------------------------------------------------------------------------
/** Perform the power function (signal^exponent) on each signal S in the workspace.
*
* Error propagation of \f$ f = a^b \f$ is given by:
* \f$ df^2 = f^2 * b^2 * (da^2 / a^2) \f$
*/
void MDHistoWorkspace::power(double exponent)
{
double exponent_squared = exponent * exponent;
for (size_t i=0; i<m_length; ++i)
{
signal_t a = m_signals[i];
signal_t f = std::pow(a, exponent);
signal_t da2 = m_errorsSquared[i];
m_signals[i] = f;
m_errorsSquared[i] = f*f * exponent_squared * da2 / (a*a);
}
}


} // namespace Mantid
} // namespace MDEvents
Expand Down

0 comments on commit 9db3b9f

Please sign in to comment.