Skip to content

Commit

Permalink
Fix conversion to numpy (#52)
Browse files Browse the repository at this point in the history
* Added tests for to_array functionality

* Stream out operator for TypeInfo

* formatting

* basis function for algebra types

* enhanced framework for converting from algebra to array.

* remove some redundant asserts

* remove cvref on type deductions

* formatting

* always import numpy if available

* lots of little details

* moved some functions around to make them accessible

* formatting

* formatting

* imort numpy function

* moved definition of __array__ to numpy files

* formatting

* Create zeroed array first

* Added missing zeros to expected arrays

* no ownership test - everything copies for now

* Added test for array of rationals

* fixed some issues with scalar allocations

* Updated changelog

* Added test for poly scalars to_array

* Fixed missing break in switch statement.
  • Loading branch information
inakleinbottle authored Nov 24, 2023
1 parent 0960fcd commit 3cdd034
Show file tree
Hide file tree
Showing 19 changed files with 470 additions and 108 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Version 0.1.0:
- Added framework for integrating device support and redesigned scalars module to accommodate the changes.
- Made changes to type deduction in constructors to avoid exceptions when providing lists of python ints/floats.
- Changed the implementation of __array__ for algebra types. A copy is always made, and the size of the array is always
equal to the dimension of the chosen width/depth composition.


Version 0.0.8:
Expand Down
3 changes: 2 additions & 1 deletion algebra/include/roughpy/algebra/algebra_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ class AlgebraBase
return p_impl.get();
}


RPY_NO_DISCARD
basis_type basis() const;
RPY_NO_DISCARD
dimn_t dimension() const;
RPY_NO_DISCARD
Expand Down
9 changes: 9 additions & 0 deletions algebra/include/roughpy/algebra/algebra_base_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ context_pointer AlgebraBase<Interface, DerivedImpl>::context() const noexcept
return (p_impl) ? p_impl->context() : nullptr;
}


template <
typename Interface,
template <typename, template <typename> class> class DerivedImpl>
typename AlgebraBase<Interface, DerivedImpl>::basis_type AlgebraBase<Interface, DerivedImpl>::basis() const
{
return (p_impl) ? p_impl->basis() : basis_type();
}

template <
typename Interface,
template <typename, template <typename> class> class DerivedImpl>
Expand Down
1 change: 1 addition & 0 deletions device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ add_roughpy_component(Device
src/opencl/ocl_version.h
src/buffer.cpp
src/buffer_interface.cpp
src/core.cpp
src/device_handle.cpp
src/device_interface_base.cpp
src/device_provider.cpp
Expand Down
2 changes: 1 addition & 1 deletion device/include/roughpy/device/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ constexpr TypeInfo type_info() noexcept
1U};
}


std::ostream& operator<<(std::ostream& os, const TypeInfo& code);


RPY_SERIAL_SERIALIZE_FN_EXT(TypeInfo)
Expand Down
41 changes: 41 additions & 0 deletions device/src/core.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Created by sam on 11/23/23.
//
#include "core.h"


std::ostream& rpy::devices::operator<<(std::ostream& os, const TypeInfo& info)
{
switch (info.code) {
case TypeCode::Int: os << "int" << info.bytes * CHAR_BIT;
break;
case TypeCode::UInt: os << "uint" << info.bytes * CHAR_BIT;
break;
case TypeCode::Float: os << "float" << info.bytes * CHAR_BIT;
break;
case TypeCode::OpaqueHandle: os << "opaque";
break;
case TypeCode::BFloat: os << "bfloat" << info.bytes * CHAR_BIT;
break;
case TypeCode::Complex: os << "complex" << info.bytes * CHAR_BIT;
break;
case TypeCode::Bool: os << "bool";
break;
case TypeCode::Rational: os << "Rational";
break;
case TypeCode::ArbitraryPrecisionInt: os << "mp_int";
break;
case TypeCode::ArbitraryPrecisionUInt: os << "mp_uint";
break;
case TypeCode::ArbitraryPrecisionFloat: os << "mp_float";
break;
case TypeCode::ArbitraryPrecisionComplex: os << "mp_complex";
break;
case TypeCode::ArbitraryPrecisionRational: os << "Rational";
break;
case TypeCode::APRationalPolynomial: os << "PolyRational";
break;
}

return os;
}
16 changes: 2 additions & 14 deletions roughpy/src/algebra/setup_algebra_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,8 @@ void setup_algebra_type(py::class_<Alg, Args...>& klass)
// setup conversion to numpy array
#ifdef ROUGHPY_WITH_NUMPY
klass.def("__array__", [](const Alg& self) {
// py::dtype dtype = dtype_from(self.coeff_type());
py::dtype dtype = ctype_to_npy_dtype(self.coeff_type());

auto dense_data = self.dense_data();
if (dense_data) {
const auto dense_data_inner = *dense_data;
return py::array(
dtype,
{dense_data_inner.size()},
{},
dense_data_inner.pointer()
);
}
return py::array(dtype);
return algebra_to_array(self);
// return py::array();
});
#endif

Expand Down
Loading

0 comments on commit 3cdd034

Please sign in to comment.