Skip to content

Commit

Permalink
add numerical operators to ProjDataInMemory
Browse files Browse the repository at this point in the history
essentially just expose Array<1,float> methods
  • Loading branch information
KrisThielemans committed May 31, 2024
1 parent bfc7655 commit 301a614
Show file tree
Hide file tree
Showing 4 changed files with 406 additions and 74 deletions.
143 changes: 143 additions & 0 deletions src/buildblock/ProjDataInMemory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "stir/SegmentByView.h"
#include "stir/Bin.h"
#include "stir/is_null_ptr.h"
#include "stir/numerics/norm.h"
#include <iostream>
#include <cstring>
#include <algorithm>
Expand Down Expand Up @@ -361,6 +362,148 @@ ProjDataInMemory::set_bin_value(const Bin& bin)
buffer[this->get_index(bin)] = bin.get_bin_value();
}

float
ProjDataInMemory::sum() const
{
return buffer.sum();
}

float
ProjDataInMemory::find_max() const
{
return buffer.find_max();
}

float
ProjDataInMemory::find_min() const
{
return buffer.find_min();
}

double
ProjDataInMemory::norm() const
{
return stir::norm(this->buffer);
}

double
ProjDataInMemory::norm_squared() const
{
return stir::norm_squared(this->buffer);
}

ProjDataInMemory&
ProjDataInMemory::operator+=(const ProjDataInMemory& v)
{
this->buffer += v.buffer;
return *this;
}

ProjDataInMemory&
ProjDataInMemory::operator-=(const ProjDataInMemory& v)
{
this->buffer -= v.buffer;
return *this;
}

ProjDataInMemory&
ProjDataInMemory::operator*=(const ProjDataInMemory& v)
{
this->buffer *= v.buffer;
return *this;
}

ProjDataInMemory&
ProjDataInMemory::operator/=(const ProjDataInMemory& v)
{
this->buffer /= v.buffer;
return *this;
}

ProjDataInMemory&
ProjDataInMemory::operator+=(const float v)
{
this->buffer += v;
return *this;
}

ProjDataInMemory&
ProjDataInMemory::operator-=(const float v)
{
this->buffer -= v;
return *this;
}

ProjDataInMemory&
ProjDataInMemory::operator*=(const float v)
{
this->buffer *= v;
return *this;
}

ProjDataInMemory&
ProjDataInMemory::operator/=(const float v)
{
this->buffer /= v;
return *this;
}

ProjDataInMemory
ProjDataInMemory::operator+(const ProjDataInMemory& iv) const
{
ProjDataInMemory c(*this);
return c += iv;
}

ProjDataInMemory
ProjDataInMemory::operator-(const ProjDataInMemory& iv) const
{
ProjDataInMemory c(*this);
return c -= iv;
}

ProjDataInMemory
ProjDataInMemory::operator*(const ProjDataInMemory& iv) const
{
ProjDataInMemory c(*this);
return c *= iv;
}

ProjDataInMemory
ProjDataInMemory::operator/(const ProjDataInMemory& iv) const
{
ProjDataInMemory c(*this);
return c /= iv;
}

ProjDataInMemory
ProjDataInMemory::operator+(const float a) const
{
ProjDataInMemory c(*this);
return c += a;
}

ProjDataInMemory
ProjDataInMemory::operator-(const float a) const
{
ProjDataInMemory c(*this);
return c -= a;
}

ProjDataInMemory
ProjDataInMemory::operator*(const float a) const
{
ProjDataInMemory c(*this);
return c *= a;
}

ProjDataInMemory
ProjDataInMemory::operator/(const float a) const
{
ProjDataInMemory c(*this);
return c /= a;
}

void
ProjDataInMemory::axpby(const float a, const ProjData& x, const float b, const ProjData& y)
{
Expand Down
173 changes: 101 additions & 72 deletions src/include/stir/ProjDataInMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class Succeeded;
*/
class ProjDataInMemory : public ProjData
{

typedef ProjDataInMemory self_type;

public:
//! constructor with only info, but no data
/*!
Expand All @@ -57,39 +60,21 @@ class ProjDataInMemory : public ProjData

Viewgram<float> get_viewgram(const int view_num,
const int segment_num,
const bool make_num_tangential_poss_odd = false
#ifdef STIR_TOF
,
const int timing_pos = 0
#endif
) const override;
const bool make_num_tangential_poss_odd = false,
const int timing_pos = 0) const override;
Succeeded set_viewgram(const Viewgram<float>& v) override;

Sinogram<float> get_sinogram(const int ax_pos_num,
const int segment_num,
const bool make_num_tangential_poss_odd = false
#ifdef STIR_TOF
,
const int timing_pos = 0
#endif
) const override;
const bool make_num_tangential_poss_odd = false,
const int timing_pos = 0) const override;

Succeeded set_sinogram(const Sinogram<float>& s) override;

//! Get all sinograms for the given segment
SegmentBySinogram<float> get_segment_by_sinogram(const int segment_num
#ifdef STIR_TOF
,
const int timing_pos = 0
#endif
) const override;
SegmentBySinogram<float> get_segment_by_sinogram(const int segment_num, const int timing_pos = 0) const override;
//! Get all viewgrams for the given segment
SegmentByView<float> get_segment_by_view(const int segment_num
#ifdef STIR_TOF
,
const int timing_pos = 0
#endif
) const override;
SegmentByView<float> get_segment_by_view(const int segment_num, const int timing_pos = 0) const override;

//! Set all sinograms for the given segment
Succeeded set_segment(const SegmentBySinogram<float>&) override;
Expand All @@ -115,6 +100,73 @@ class ProjDataInMemory : public ProjData

void set_bin_value(const Bin& bin);

//! @name arithmetic operations
///@{
//! return sum of all elements
float sum() const;

//! return maximum value of all elements
float find_max() const;

//! return minimum value of all elements
float find_min() const;

//! return L2-norm (sqrt of sum of squares)
double norm() const;

//! return L2-norm squared (sum of squares)
double norm_squared() const;

//! elem by elem addition
self_type operator+(const self_type& iv) const;

//! elem by elem subtraction
self_type operator-(const self_type& iv) const;

//! elem by elem multiplication
self_type operator*(const self_type& iv) const;

//! elem by elem division
self_type operator/(const self_type& iv) const;

//! addition with a 'float'
self_type operator+(const float a) const;

//! subtraction with a 'float'
self_type operator-(const float a) const;

//! multiplication with a 'float'
self_type operator*(const float a) const;

//! division with a 'float'
self_type operator/(const float a) const;

// corresponding assignment operators

//! adding elements of \c v to the current vector
self_type& operator+=(const self_type& v);

//! subtracting elements of \c v from the current vector
self_type& operator-=(const self_type& v);

//! multiplying elements of the current vector with elements of \c v
self_type& operator*=(const self_type& v);

//! dividing all elements of the current vector by elements of \c v
self_type& operator/=(const self_type& v);

//! adding an \c float to the elements of the current vector
self_type& operator+=(const float v);

//! subtracting an \c float from the elements of the current vector
self_type& operator-=(const float v);

//! multiplying the elements of the current vector with an \c float
self_type& operator*=(const float v);

//! dividing the elements of the current vector by an \c float
self_type& operator/=(const float v);

//! \deprecated a*x+b*y (use xapyb)
STIR_DEPRECATED void axpby(const float a, const ProjData& x, const float b, const ProjData& y) override;

Expand All @@ -137,6 +189,7 @@ class ProjDataInMemory : public ProjData
/// This implementation requires that a, b and y are ProjDataInMemory
/// (else falls back on general method)
void sapyb(const ProjData& a, const ProjData& y, const ProjData& b) override;
///@}

/** @name iterator typedefs
* iterator typedefs
Expand All @@ -149,71 +202,35 @@ class ProjDataInMemory : public ProjData
///@}

//! start value for iterating through all elements in the array, see iterator
inline iterator begin()
{
return buffer.begin();
}
iterator begin() { return buffer.begin(); }
//! start value for iterating through all elements in the (const) array, see iterator
inline const_iterator begin() const
{
return buffer.begin();
}
const_iterator begin() const { return buffer.begin(); }
//! end value for iterating through all elements in the array, see iterator
inline iterator end()
{
return buffer.end();
}
iterator end() { return buffer.end(); }
//! end value for iterating through all elements in the (const) array, see iterator
inline const_iterator end() const
{
return buffer.end();
}
const_iterator end() const { return buffer.end(); }
//! start value for iterating through all elements in the array, see iterator
inline iterator begin_all()
{
return buffer.begin_all();
}
iterator begin_all() { return buffer.begin_all(); }
//! start value for iterating through all elements in the (const) array, see iterator
inline const_iterator begin_all() const
{
return buffer.begin_all();
}
const_iterator begin_all() const { return buffer.begin_all(); }
//! end value for iterating through all elements in the array, see iterator
inline iterator end_all()
{
return buffer.end_all();
}
iterator end_all() { return buffer.end_all(); }
//! end value for iterating through all elements in the (const) array, see iterator
inline const_iterator end_all() const
{
return buffer.end_all();
}
const_iterator end_all() const { return buffer.end_all(); }

//! \name access to the data via a pointer
//@{
//! member function for access to the data via a float*
inline float* get_data_ptr()
{
return buffer.get_data_ptr();
}
float* get_data_ptr() { return buffer.get_data_ptr(); }

//! member function for access to the data via a const float*
inline const float* get_const_data_ptr() const
{
return buffer.get_const_data_ptr();
}
const float* get_const_data_ptr() const { return buffer.get_const_data_ptr(); }

//! signal end of access to float*
inline void release_data_ptr()
{
buffer.release_data_ptr();
}
void release_data_ptr() { buffer.release_data_ptr(); }

//! signal end of access to const float*
inline void release_const_data_ptr() const
{
buffer.release_const_data_ptr();
}
void release_const_data_ptr() const { buffer.release_const_data_ptr(); }
//@}

private:
Expand All @@ -236,6 +253,18 @@ class ProjDataInMemory : public ProjData
std::streamoff get_index(const Bin&) const;
};

inline double
norm(const ProjDataInMemory& p)
{
return p.norm();
}

inline double
norm_squared(const ProjDataInMemory& p)
{
return p.norm_squared();
}

END_NAMESPACE_STIR

#endif
Loading

0 comments on commit 301a614

Please sign in to comment.