26#include "ndarray/eigen.h"
46 if (factors.size() < 1u) {
48 pex::exceptions::LengthError,
49 "ProductBoundedField requires at least one BoundedField factor."
52 auto iter = factors.begin();
53 auto bbox = (**iter).getBBox();
55 for (;
iter != factors.end(); ++
iter) {
56 if ((**iter).getBBox() !=
bbox) {
58 pex::exceptions::InvalidParameterError,
59 (boost::format(
"Inconsistency in ProductBoundedField bboxes: %s != %s") %
60 bbox % (**iter).getBBox()).str()
70 BoundedField(checkAndExtractBBox(factors)), _factors(factors)
79 for (
auto const &
field : _factors) {
86 ndarray::Array<double const, 1>
const&
x,
87 ndarray::Array<double const, 1>
const&
y
89 if (
x.getShape() !=
y.getShape()) {
92 (boost::format(
"Inconsistent shapes: %s != %s") %
x.getShape() %
y.getShape()).str()
95 ndarray::Array<double, 1, 1>
z = ndarray::allocate(
x.getShape());
97 for (
auto const &
field : _factors) {
98 ndarray::asEigenArray(
z) *= ndarray::asEigenArray(
field->evaluate(
x,
y));
107struct PersistenceHelper {
111 static PersistenceHelper
const & get() {
112 static PersistenceHelper
const instance;
118 PersistenceHelper() :
120 id(
schema.addField<int>(
"id",
"Archive ID of a BoundedField factor."))
123 PersistenceHelper(PersistenceHelper
const &) =
delete;
124 PersistenceHelper(PersistenceHelper &&) =
delete;
125 PersistenceHelper & operator=(PersistenceHelper
const &) =
delete;
126 PersistenceHelper & operator=(PersistenceHelper &&) =
delete;
128 ~PersistenceHelper() noexcept = default;
132class ProductBoundedFieldFactory : public table::io::PersistableFactory {
135 : afw::table::io::PersistableFactory(
name) {}
138 CatalogVector
const& catalogs)
const override {
140 auto const &
keys = PersistenceHelper::get();
141 auto const & cat = catalogs.front();
144 for (
auto const & record : cat) {
145 factors.
push_back(archive.get<BoundedField>(record.get(
keys.id)));
147 return std::make_shared<ProductBoundedField>(factors);
151std::string getProductBoundedFieldPersistenceName() {
return "ProductBoundedField"; }
153ProductBoundedFieldFactory registration(getProductBoundedFieldPersistenceName());
158 return std::all_of(_factors.begin(), _factors.end(),
159 [](
auto const &
field) { return field->isPersistable(); });
163 return getProductBoundedFieldPersistenceName();
169 auto const & keys = PersistenceHelper::get();
171 catalog.
reserve(_factors.size());
172 for (
auto const &
field : _factors) {
182 bool multiplied =
false;
183 for (
auto &
field : factors) {
185 field = (*field) * scale;
191 ndarray::Array<double, 2, 2>
coefficients = ndarray::allocate(1, 1);
195 return std::make_shared<ProductBoundedField>(factors);
200 if (!rhsCasted)
return false;
202 return (
getBBox() == rhsCasted->getBBox()) &&
204 rhsCasted->_factors.begin(), rhsCasted->_factors.end(),
205 [](
auto const &
a,
auto const &
b) {
return *
a == *
b; });
210 os <<
"ProductBoundedField([";
211 for (
auto const &
field : _factors) {
212 os << (*field) <<
", ";
table::Key< std::string > name
ndarray::Array< double const, 2, 2 > coefficients
#define LSST_EXCEPT(type,...)
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
An abstract base class for 2-d functions defined on an integer bounding boxes.
lsst::geom::Box2I getBBox() const
Return the bounding box that defines the region where the field is valid.
virtual double evaluate(lsst::geom::Point2D const &position) const =0
Evaluate the field at the given point.
A BoundedField that lazily multiplies a sequence of other BoundedFields.
ProductBoundedField(std::vector< std::shared_ptr< BoundedField const > > const &factors)
Construct from a sequence of BoundedField factors.
std::string toString() const override
bool operator==(BoundedField const &rhs) const override
BoundedFields (of the same sublcass) are equal if their bounding boxes and parameters are equal.
bool isPersistable() const noexcept override
ProductBoundedField is persistable if and only if all of its factors are.
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
std::shared_ptr< BoundedField > operator*(double const scale) const override
Return a scaled BoundedField.
double evaluate(lsst::geom::Point2D const &position) const override
Evaluate the field at the given point.
~ProductBoundedField() override
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
void reserve(size_type n)
Increase the capacity of the catalog to the given size.
An object passed to Persistable::write to allow it to persist itself.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
int put(Persistable const *obj, bool permissive=false)
Save an object to the archive and return a unique ID that can be used to retrieve it from an InputArc...
std::shared_ptr< table::io::Persistable > read(table::io::InputArchive const &archive, table::io::CatalogVector const &catalogs) const override