Commit 4f0e7cc5 authored by Samuel Jackson's avatar Samuel Jackson
Browse files

Refs #9517 Add binary operation helper functions to DPA.

parent ba4c6b1a
......@@ -3,6 +3,7 @@
#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/IEventWorkspace.h"
#include "MantidKernel/PropertyManager.h"
......@@ -64,7 +65,54 @@ protected:
bool isMainThread();
int getNThreads();
/// Divide a matrix workspace by another matrix workspace
MatrixWorkspace_sptr divide(const MatrixWorkspace_sptr lhs, const MatrixWorkspace_sptr rhs);
/// Divide a matrix workspace by a single value
MatrixWorkspace_sptr divide(const MatrixWorkspace_sptr lhs, const double& rhsValue);
/// Multiply a matrix workspace by another matrix workspace
MatrixWorkspace_sptr multiply(const MatrixWorkspace_sptr lhs, const MatrixWorkspace_sptr rhs);
/// Multiply a matrix workspace by a single value
MatrixWorkspace_sptr multiply(const MatrixWorkspace_sptr lhs, const double& rhsValue);
/// Add a matrix workspace to another matrix workspace
MatrixWorkspace_sptr plus(const MatrixWorkspace_sptr lhs, const MatrixWorkspace_sptr rhs);
/// Add a single value to a matrix workspace
MatrixWorkspace_sptr plus(const MatrixWorkspace_sptr lhs, const double& rhsValue);
/// Subract a matrix workspace by another matrix workspace
MatrixWorkspace_sptr minus(const MatrixWorkspace_sptr lhs, const MatrixWorkspace_sptr rhs);
/// Subract a single value from a matrix workspace
MatrixWorkspace_sptr minus(const MatrixWorkspace_sptr lhs, const double& rhsValue);
private:
template<typename LHSType, typename RHSType, typename ResultType>
ResultType executeBinaryAlgorithm(const std::string & algorithmName, const LHSType lhs, const RHSType rhs)
{
auto alg = createChildAlgorithm(algorithmName);
alg->initialize();
alg->setProperty<LHSType>("LHSWorkspace",lhs);
alg->setProperty<RHSType>("RHSWorkspace",rhs);
alg->setPropertyValue("OutputWorkspace","tmp_binary_op_ws");
alg->execute();
if (alg->isExecuted())
{
// Get the output workspace property
return alg->getProperty("OutputWorkspace");
}
else
{
std::string message = "Error while executing operation: " + algorithmName;
throw std::runtime_error(message);
}
}
/// Create a matrix workspace from a single number
MatrixWorkspace_sptr createWorkspaceSingleValue(const double& rhsValue);
/// The name of the algorithm to invoke when loading data
std::string m_loadAlg;
/// The name of the algorithm to invoke when accumulating data chunks
......
......@@ -280,5 +280,112 @@ namespace API
{
throw std::runtime_error("DataProcessorAlgorithm::forwardProperties is not implemented");
}
//------------------------------------------------------------------------------------------
// Binary opration implementations for DPA so it can record history
//------------------------------------------------------------------------------------------
/**
* Divide a matrix workspace by another matrix workspace
* @param lhs :: the workspace on the left hand side of the divide symbol
* @param rhs :: the workspace on the right hand side of the divide symbol
* @return matrix workspace resulting from the operation
*/
MatrixWorkspace_sptr DataProcessorAlgorithm::divide(const MatrixWorkspace_sptr lhs, const MatrixWorkspace_sptr rhs)
{
return this->executeBinaryAlgorithm<MatrixWorkspace_sptr,MatrixWorkspace_sptr,MatrixWorkspace_sptr>("Divide", lhs, rhs);
}
/**
* Divide a matrix workspace by a single value
* @param lhs :: the workspace on the left hand side of the divide symbol
* @param rhsValue :: the value on the right hand side of the divide symbol
* @return matrix workspace resulting from the operation
*/
MatrixWorkspace_sptr DataProcessorAlgorithm::divide(const MatrixWorkspace_sptr lhs, const double& rhsValue)
{
return this->executeBinaryAlgorithm<MatrixWorkspace_sptr,MatrixWorkspace_sptr,MatrixWorkspace_sptr>("Divide", lhs, createWorkspaceSingleValue(rhsValue));
}
/**
* Multiply a matrix workspace by another matrix workspace
* @param lhs :: the workspace on the left hand side of the multiplication symbol
* @param rhs :: the workspace on the right hand side of the multiplication symbol
* @return matrix workspace resulting from the operation
*/
MatrixWorkspace_sptr DataProcessorAlgorithm::multiply(const MatrixWorkspace_sptr lhs, const MatrixWorkspace_sptr rhs)
{
return this->executeBinaryAlgorithm<MatrixWorkspace_sptr,MatrixWorkspace_sptr,MatrixWorkspace_sptr>("Divide", lhs, rhs);
}
/**
* Multiply a matrix workspace by a single value
* @param lhs :: the workspace on the left hand side of the multiplication symbol
* @param rhsValue :: the value on the right hand side of the multiplication symbol
* @return matrix workspace resulting from the operation
*/
MatrixWorkspace_sptr DataProcessorAlgorithm::multiply(const MatrixWorkspace_sptr lhs, const double& rhsValue)
{
return this->executeBinaryAlgorithm<MatrixWorkspace_sptr,MatrixWorkspace_sptr,MatrixWorkspace_sptr>("Multiply", lhs, createWorkspaceSingleValue(rhsValue));
}
/**
* Add a matrix workspace to another matrix workspace
* @param lhs :: the workspace on the left hand side of the addition symbol
* @param rhs :: the workspace on the right hand side of the addition symbol
* @return matrix workspace resulting from the operation
*/
MatrixWorkspace_sptr DataProcessorAlgorithm::plus(const MatrixWorkspace_sptr lhs, const MatrixWorkspace_sptr rhs)
{
return this->executeBinaryAlgorithm<MatrixWorkspace_sptr,MatrixWorkspace_sptr,MatrixWorkspace_sptr>("Plus", lhs, rhs);
}
/**
* Add a single value to another matrix workspace
* @param lhs :: the workspace on the left hand side of the addition symbol
* @param rhsValue :: the value on the right hand side of the addition symbol
* @return matrix workspace resulting from the operation
*/
MatrixWorkspace_sptr DataProcessorAlgorithm::plus(const MatrixWorkspace_sptr lhs, const double& rhsValue)
{
return this->executeBinaryAlgorithm<MatrixWorkspace_sptr,MatrixWorkspace_sptr,MatrixWorkspace_sptr>("Plus", lhs, createWorkspaceSingleValue(rhsValue));
}
/**
* Subract a matrix workspace from another matrix workspace
* @param lhs :: the workspace on the left hand side of the subtraction symbol
* @param rhs :: the workspace on the right hand side of the subtraction symbol
* @return matrix workspace resulting from the operation
*/
MatrixWorkspace_sptr DataProcessorAlgorithm::minus(const MatrixWorkspace_sptr lhs, const MatrixWorkspace_sptr rhs)
{
return this->executeBinaryAlgorithm<MatrixWorkspace_sptr,MatrixWorkspace_sptr,MatrixWorkspace_sptr>("Minus", lhs, rhs);
}
/**
* Subract a single value from a matrix workspace
* @param lhs :: the workspace on the left hand side of the subtraction symbol
* @param rhsValue :: the workspace on the right hand side of the subtraction symbol
* @return matrix workspace resulting from the operation
*/
MatrixWorkspace_sptr DataProcessorAlgorithm::minus(const MatrixWorkspace_sptr lhs, const double& rhsValue)
{
return this->executeBinaryAlgorithm<MatrixWorkspace_sptr,MatrixWorkspace_sptr,MatrixWorkspace_sptr>("Minus", lhs, createWorkspaceSingleValue(rhsValue));
}
/**
* Create a workspace that contains just a single Y value.
* @param rhsValue :: the value to convert to a single value matrix workspace
* @return matrix workspace resulting from the operation
*/
MatrixWorkspace_sptr DataProcessorAlgorithm::createWorkspaceSingleValue(const double& rhsValue)
{
MatrixWorkspace_sptr retVal = WorkspaceFactory::Instance().create("WorkspaceSingleValue",1,1,1);
retVal->dataY(0)[0]=rhsValue;
return retVal;
}
} // namespace Mantid
} // namespace API
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment