Skip to content

Commit

Permalink
Last python update for now
Browse files Browse the repository at this point in the history
  • Loading branch information
Pencilcaseman committed Nov 3, 2023
1 parent 104f740 commit 4270aed
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 8 deletions.
24 changes: 24 additions & 0 deletions librapid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,30 @@ def getSeed():
return _librapid.getSeed()


transpose = _librapid.transpose
dot = _librapid.dot
sin = _librapid.sin
cos = _librapid.cos
tan = _librapid.tan
asin = _librapid.asin
acos = _librapid.acos
atan = _librapid.atan
sinh = _librapid.sinh
cosh = _librapid.cosh
tanh = _librapid.tanh
asinh = _librapid.asinh
acosh = _librapid.acosh
atanh = _librapid.atanh
sqrt = _librapid.sqrt
cbrt = _librapid.cbrt
log = _librapid.log
log2 = _librapid.log2
log10 = _librapid.log10
exp = _librapid.exp
exp2 = _librapid.exp2
exp10 = _librapid.exp10


float32 = DataType("float32", 4)
float64 = DataType("float64", 8)
int32 = DataType("int32", 4)
Expand Down
86 changes: 83 additions & 3 deletions librapid/bindings/generators/arrayGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
("uint64_t", "UInt64"),
("float", "Float"),
("double", "Double"),
("lrc::Complex<float>", "ComplexFloat"),
("lrc::Complex<double>", "ComplexDouble")]:
# ("lrc::Complex<float>", "ComplexFloat"),
# ("lrc::Complex<double>", "ComplexDouble")
]:
for backend in ["CPU"]: # ["CPU", "OpenCL", "CUDA"]:
arrayTypes.append({
"scalar": scalar[0],
Expand Down Expand Up @@ -370,7 +371,86 @@ def generateFunctionsForArray(config):
)
]

return methods, []
functions = [
# Transpose
function.Function(
name="transpose",
args=[
argument.Argument(
name="array",
type=generateCppArrayType(config),
const=True,
ref=True
)
],
op="""
return lrc::transpose(array).eval();
"""
),

# Matmul
function.Function(
name="dot",
args=[
argument.Argument(
name="a",
type=generateCppArrayType(config),
const=True,
ref=True
),
argument.Argument(
name="b",
type=generateCppArrayType(config),
const=True,
ref=True
)
],
op="""
return lrc::dot(a, b).eval();
"""
)
]

for f in [
"sin",
"cos",
"tan",
"asin",
"acos",
"atan",
"sinh",
"cosh",
"tanh",
"asinh",
"acosh",
"atanh",
"sqrt",
"cbrt",
"exp",
# "exp2",
# "exp10",
"log",
"log2",
"log10"
]:
functions.append(
function.Function(
name=f,
args=[
argument.Argument(
name="array",
type=generateCppArrayType(config),
const=True,
ref=True
)
],
op=f"""
return lrc::{f}(array).eval();
"""
)
)

return methods, functions


def generateArrayModule(config):
Expand Down
5 changes: 3 additions & 2 deletions librapid/bindings/generators/generalArrayViewGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
("uint64_t", "UInt64"),
("float", "Float"),
("double", "Double"),
("lrc::Complex<float>", "ComplexFloat"),
("lrc::Complex<double>", "ComplexDouble")]:
# ("lrc::Complex<float>", "ComplexFloat"),
# ("lrc::Complex<double>", "ComplexDouble")
]:
for backend in ["CPU"]: # ["CPU", "OpenCL", "CUDA"]:
arrayTypes.append({
"scalar": scalar[0],
Expand Down
3 changes: 1 addition & 2 deletions librapid/bindings/generators/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def genInterface(self):
ret += "\n"

for func in self.functions:
ret += func.gen(moduleName)
ret += "\n"
ret += func.gen(moduleName) + ";\n"

ret += "}\n"

Expand Down
2 changes: 1 addition & 1 deletion librapid/include/librapid/array/linalg/arrayMultiply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ namespace librapid {
std::forward<ArrayA>(arrA),
alpha * beta,
std::forward<ArrayB>(arrB),
0);
ScalarA(0));
}

namespace typetraits {
Expand Down
126 changes: 126 additions & 0 deletions librapid/include/librapid/array/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,13 @@ namespace librapid {
LIBRAPID_UNARY_FUNCTOR(Sinh, ::librapid::sinh); // sinh(a)
LIBRAPID_UNARY_FUNCTOR(Cosh, ::librapid::cosh); // cosh(a)
LIBRAPID_UNARY_FUNCTOR(Tanh, ::librapid::tanh); // tanh(a)
LIBRAPID_UNARY_FUNCTOR(Asinh, ::librapid::sinh); // asinh(a)
LIBRAPID_UNARY_FUNCTOR(Acosh, ::librapid::cosh); // acosh(a)
LIBRAPID_UNARY_FUNCTOR(Atanh, ::librapid::tanh); // atanh(a)

LIBRAPID_UNARY_FUNCTOR(Exp, ::librapid::exp); // exp(a)
LIBRAPID_UNARY_FUNCTOR(Exp2, ::librapid::exp); // exp2(a)
LIBRAPID_UNARY_FUNCTOR(Exp10, ::librapid::exp); // exp10(a)
LIBRAPID_UNARY_FUNCTOR(Log, ::librapid::log); // log(a)
LIBRAPID_UNARY_FUNCTOR(Log2, ::librapid::log2); // log2(a)
LIBRAPID_UNARY_FUNCTOR(Log10, ::librapid::log10); // log10(a)
Expand Down Expand Up @@ -461,6 +466,33 @@ namespace librapid {
LIBRAPID_UNARY_SHAPE_EXTRACTOR
};

template<>
struct TypeInfo<::librapid::detail::Asinh> {
static constexpr const char *name = "hyperbolic arcsine";
static constexpr const char *filename = "trigonometry";
static constexpr const char *kernelName = "asinhArrays";
LIBRAPID_UNARY_KERNEL_GETTER
LIBRAPID_UNARY_SHAPE_EXTRACTOR
};

template<>
struct TypeInfo<::librapid::detail::Acosh> {
static constexpr const char *name = "hyperbolic arccosine";
static constexpr const char *filename = "trigonometry";
static constexpr const char *kernelName = "acoshArrays";
LIBRAPID_UNARY_KERNEL_GETTER
LIBRAPID_UNARY_SHAPE_EXTRACTOR
};

template<>
struct TypeInfo<::librapid::detail::Atanh> {
static constexpr const char *name = "hyperbolic arctangent";
static constexpr const char *filename = "trigonometry";
static constexpr const char *kernelName = "atanhArrays";
LIBRAPID_UNARY_KERNEL_GETTER
LIBRAPID_UNARY_SHAPE_EXTRACTOR
};

template<>
struct TypeInfo<::librapid::detail::Exp> {
static constexpr const char *name = "exponent";
Expand All @@ -470,6 +502,24 @@ namespace librapid {
LIBRAPID_UNARY_SHAPE_EXTRACTOR
};

template<>
struct TypeInfo<::librapid::detail::Exp2> {
static constexpr const char *name = "exponent base 2";
static constexpr const char *filename = "expLogPow";
static constexpr const char *kernelName = "exp2Arrays";
LIBRAPID_UNARY_KERNEL_GETTER
LIBRAPID_UNARY_SHAPE_EXTRACTOR
};

template<>
struct TypeInfo<::librapid::detail::Exp10> {
static constexpr const char *name = "exponent base 10";
static constexpr const char *filename = "expLogPow";
static constexpr const char *kernelName = "exp10Arrays";
LIBRAPID_UNARY_KERNEL_GETTER
LIBRAPID_UNARY_SHAPE_EXTRACTOR
};

template<>
struct TypeInfo<::librapid::detail::Log> {
static constexpr const char *name = "logarithm";
Expand Down Expand Up @@ -1079,6 +1129,54 @@ namespace librapid {
std::forward<VAL>(val));
}




/// \brief Calculate the hyperbolic arcsine of each element in the array
///
/// \f$R = \{ R_0, R_1, R_2, ... \} \f$ \text{ where } \f$R_i = \sinh^{-1}(A_i)\f$
///
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Hyperbolic sine function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto asinh(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Asinh, VAL> {
return detail::makeFunction<typetraits::DescriptorType_t<VAL>, detail::Asinh>(
std::forward<VAL>(val));
}

/// \brief Calculate the hyperbolic arccosine of each element in the array
///
/// \f$R = \{ R_0, R_1, R_2, ... \} \f$ \text{ where } \f$R_i = \cosh^{-1}(A_i)\f$
///
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Hyperbolic cosine function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto acosh(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Acosh, VAL> {
return detail::makeFunction<typetraits::DescriptorType_t<VAL>, detail::Acosh>(
std::forward<VAL>(val));
}

/// \brief Calculate the hyperbolic arctangent of each element in the array
///
/// \f$R = \{ R_0, R_1, R_2, ... \} \f$ \text{ where } \f$R_i = \tanh^{-1}(A_i)\f$
///
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Hyperbolic tangent function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto atanh(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Atanh, VAL> {
return detail::makeFunction<typetraits::DescriptorType_t<VAL>, detail::Atanh>(
std::forward<VAL>(val));
}




/// \brief Raise e to the power of each element in the array
///
/// \f$R = \{ R_0, R_1, R_2, ... \} \f$ \text{ where } \f$R_i = e^{A_i}\f$
Expand All @@ -1093,6 +1191,34 @@ namespace librapid {
std::forward<VAL>(val));
}

/// \brief Raise 2 to the power of each element in the array
///
/// \f$R = \{ R_0, R_1, R_2, ... \} \f$ \text{ where } \f$R_i = 2^{A_i}\f$
///
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Exponential function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto exp2(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Exp2, VAL> {
return detail::makeFunction<typetraits::DescriptorType_t<VAL>, detail::Exp2>(
std::forward<VAL>(val));
}

/// \brief Raise 10 to the power of each element in the array
///
/// \f$R = \{ R_0, R_1, R_2, ... \} \f$ \text{ where } \f$R_i = 10^{A_i}\f$
///
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Exponential function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto exp10(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Exp10, VAL> {
return detail::makeFunction<typetraits::DescriptorType_t<VAL>, detail::Exp10>(
std::forward<VAL>(val));
}

// \brief Compute the natural logarithm of each element in the array
///
/// \f$R = \{ R_0, R_1, R_2, ... \} \f$ \text{ where } \f$R_i = \ln(A_i)\f$
Expand Down
6 changes: 6 additions & 0 deletions librapid/include/librapid/math/complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,12 @@ namespace librapid {
}
}

template<typename T>
LIBRAPID_NODISCARD Complex<T> cbrt(const Complex<T> &other) {
constexpr T oneThird = T(1) / T(3);
return pow(other, oneThird);
}

/// \brief Calculate the hyperbolic tangent of a complex number
///
/// This function supports propagation of NaNs and Infs.
Expand Down

0 comments on commit 4270aed

Please sign in to comment.