Merry XMas/Yule/Saturnalia whatever-floats-your-boat, by the way :)

`piranha::polynomial<Expression,piranha::monomial<short>>`

would just work?
yes it did work, at least for basic stuff...

`Expression`

is just a value-semantics wrapper for `Basic`

right?
right then it should still work, unless I broke something in Piranha in the meantime

`p.pow(n)`

, `p.partial("x")`

doesn't work or am I missing something?
for

`p.pow()`

to work you need to implement a specialisation of `piranha::pow()`

for `Expression`

for

`p.partial("x")`

to work you need to implement a specialisation of `piranha::partial()`

for `Expression`

lemme fetch an example

https://github.com/bluescarni/piranha/blob/master/src/real.hpp#L1821 so this is how it is done for

`piranha::real`

the scheme is identical for

`Expression`

: you need to write a specialisation of a functor
you will need to include

`piranha/math.hpp`

before implementing the specialisation
because that file contains the declaration of the default, unspecialised functor for the implementation of

`partial`

- which does not provide a call operator, so it's not functional
`pow()`

is a tad more complex, because it's a function with two parameters - base and exponent
and you can specialise the behaviour for each base/expo pair if you like

`pow()`

is defined in its own header `pow.hpp`

https://github.com/bluescarni/piranha/blob/master/src/real.hpp#L1720 here is the specialisation for

`real`

if for an initial implementation you are just interested in integral exponentiation, you can write a specialisation only for

`Expression`

base and `int`

exponent, then you can make it more complicated if you want
should be

`piranha::math`

but let me check
yeah looks like it

```
namespace Piranha {
namespace math {
template<typename T>
struct partial_impl<T, typename std::enable_if<std::is_same<T, Expression>::value>::type> {
/// Call operator.
/**
* @return an instance of Expression constructed from zero.
*/
Expression operator()(const Expression &, const std::string &) const {
return Expression(0);
}
};
}
}
```

fails with `partial_impl`

is not a class template
is the capital

`P`

in the `Piranha`

namespace a copy typo?
Ah no

you might need to qualify

`Expression`

with `symengine::Expression`

as well
`piranha::math::pow_impl`

, and you need to include `piranha/pow.hpp`

... lemme see
```
namespace piranha { namespace math {
template <typename T, typename U>
struct pow_impl<T,U, typename std::enable_if<std::is_same<T, Expression>::value && std::is_same<U,int>::value>::type>
{
Expression operator()(const Expression &, const T &) const {
return Expression(0);
}
};
}}
```

something like this (untested)

(was missing an angle bracket, check the syntax :p )

note that all this needs to go in the header file of

`Expression`

... it's really important
so each time you include

`expression.hpp`

the compiler sees these specialisations
It compiles now. For printing does it use the std::ostream overloads?

yes the default implementation of the printing functor is enabled if

`std::ostream`

overloads are available
but you can customise with a functor also that

it's a bit like python's

`__str__()`

really :)
with the horrible syntax of templates of course

in case you are interested the implementation of these functors and the idea behind it are explained here: http://bluescarni.github.io/overloading-overloaded.html

`piranha::math::subs_impl`

, it's a 2-arguments functor like `pow_impl`

defined in

`math.hpp`

so same deal

wait

a tad different, lemme fetch an example

you need also the name of the variable you want to subs in the signature of the

`operator()`

```
template <typename T, typename U>
struct subs_impl<T,U, typename std::enable_if<std::is_same<T, Expression>::value && std::is_same<U,int>::value>::type>
{
Expression operator()(const Expression &s, const std::string &name, const T &x) const
{
return Expression(0);
}
};
```

something like this I think

the signature of the operator means: substitute, in expression

`s`

, the symbol called `name`

with the object `x`

here

`x`

is an int but it can be an expression of course...
mhm I think we are confusing things... so this

`subs_impl`

is needed to "teach" piranha how to substitute in `Expression`

s
what kind of operation would you like to do on the

`Series`

object?
because without the

`subs_impl`

specialisation piranha can still do substitution in the monomial part
My mistake. How to substitute a

`var`

in a `polynomial`

with another `polynomial`

`var`

is a symengine symbolic variables or one of the variables in piranha's polynomial?
so for instance if you have the polynomial

`e*x**2*y**3`

, where `e`

is a symengine expression, you want to substitute either `x`

or `y`

?
I can select that by passing the name of the variable right?

yes sorry I mispelled the question... my point is that you want some substitution to happen for either

`x`

or `y`

but you don't want to do any subs in the expression, is that correct?
ok then it should work out of the box I think... is it giving compilation errors?

the substitution in this case is handled by the

`monomial`

class itself
Yeah, it says I need to implement

`subs_term_impl`

. I'll do that.
mhm.. no that is a private method which is not supposed to be specialised

```
[ 9%] Building CXX object symengine/CMakeFiles/symengine.dir/series_piranha.cpp.o
/home/isuru/projects/symengine/symengine/series_piranha.cpp:160:14: error: no matching member function for call to 'subs'
return s.subs(var.get_symbol_set()[0].get_name(), r);
~~^~~~
/usr/local/include/piranha/substitutable_series.hpp:193:16: note: candidate template ignored: substitution failure [with T =
piranha::polynomial<SymEngine::Expression, piranha::monomial<short, std::integral_constant<unsigned long, 0> > >]: no
matching function for call to 'subs_term_impl'
subs_type<T> subs(const std::string &name, const T &x) const
^
1 error generated
```

I get this error

ok.. so the substitution method has been disabled because piranha thinks that the classes involved in the substitution do not support the necessary arithmetic operations

so here might be the problem

in order to do the subs, we need to be able to raise

`Expression`

to the power of `short`

, right?
as exponents in the monomial are represented as

`short`

but I think we only implemented exponentiation to

`int`

right?
for

`Expression`

I mean
so I think you should change the

`std::is_same<U,int>::value`

to `std::is_integral<U>::value`

for now
in the

`pow_impl`

machinery
ok nice hopefully it works :) you should probably decide how strict that condition needs to be, as with

`std::is_integral`

it would accept even `long`

which would then be truncated to `int`

by `Expression`

I imagine?
but that's stuff for the second iteration

`GMP`

is fantastic but it's showing its age with respect to software engineering
the other thing that really bothers me it that it just crashes when running our of memory

Btw, there are custom CMake scripts with pre-compiled header support. Might be useful as including piranha increases the compile time significantly

I gave them a spin some time ago, I cannot remember the name of the one I tried... it's on github though

didn't manage to make it work unfortunately

ah yeah, cotire

I get this,

```
terminate called after throwing an instance of 'std::invalid_argument'
what():
function: monomial
where: /usr/local/include/piranha/monomial.hpp, 283
what: invalid monomial
```

Is there a way to disable the exception handling by piranha, so that I get a nice stacktrace via symengine?

Piranha has a exception handling mechanism right? I just want it disabled, so that the exception is thrown at the right place

not sure I understand... that error is thrown on the indicated line by a regular

`throw`

statement and it is not caught anywhere
would you like to have the

`throw`

removed?
or maybe you would like to put a segfault there to trigger symengine stacktrace functionality?

Even if it's an Exception it should work. Anyways, that doesn't matter, I'll use something else.

can you post the code that trigger the behaviour on gist?

It's here, https://github.com/isuruf/symengine/tree/series. I have to go now for a bit, I'll send a small example once I figure out what is going now, and after checking if I haven't done anything stupid

ok I need to sleep as well, I'll be able to read about the snippet tomorrow hopefully

cya!

@isuruf You can get gdb stacktrace even if an exception was caught if you give the command

`catch throw`

on the gdb prompt. Otherwise, nice work.
There is no precedence in Sage how to handle series with expressions as coefficients in a polynomial ring, and Pynac has expressions but no specific polynomial code.

There are two Sage tickets whose discussion may be interesting:

http://trac.sagemath.org/ticket/3354

http://trac.sagemath.org/ticket/16198

http://trac.sagemath.org/ticket/3354

http://trac.sagemath.org/ticket/16198

These two tickets show that it is easy to implement more general series than with rational coefficients by first implementing reals and for the

`sqrt`

function by implementi8ng quadratic field element coefficients (if you have them already like in Sage). For neither are symbolic expressions needed.
Pari switches automatically to reals with

`log(x+2)`

and `sqrt(x+2)`

but we already know that Pari is special.
@rwst, you can try my fork's series branch. It has a working implementation for symbolic expressions and it is very easy to implement another with quadratic field element coefficients if it is implemented, but switching from one to another is not implemented.

@bluescarni, things like `1 - Poly`

are not implemented right? I spent lots of time trying to figure out the segfaults.

@bluescarni, about the exception, it was because of Catch catching the exception. @rwst, thanks for the suggestion

@bluescarni, I changed the printing of the coefficient, but it still looks ugly. Is there a way to add spaces on either side of `+`

?

Compare

`sin(1)*cos(2)+(-sin(1)*sin(2) + cos(1)*cos(2))*x+(-sin(1)*cos(2) - sin(2)*cos(1))*x**2+((2/3)*sin(1)*sin(2) + (-2/3)*cos(1)*cos(2))*x**3+((1/3)*sin(1)*cos(2) + (1/3)*sin(2)*cos(1))*x**4+((-2/15)*sin(1)*sin(2) + (2/15)*cos(1)*cos(2))*x**5+((-2/45)*sin(1)*cos(2) + (-2/45)*sin(2)*cos(1))*x**6+((4/315)*sin(1)*sin(2) + (-4/315)*cos(1)*cos(2))*x**7+((1/315)*sin(1)*cos(2) + (1/315)*sin(2)*cos(1))*x**8+((-2/2835)*sin(1)*sin(2) + (2/2835)*cos(1)*cos(2))*x**9+((-2/14175)*sin(1)*cos(2) + (-2/14175)*sin(2)*cos(1))*x**10 + O(x**10)`

with SymPy's output

`sin(1)*cos(2) + x*(-sin(1)*sin(2) + cos(1)*cos(2)) + x**2*(-sin(2)*cos(1) - sin(1)*cos(2)) + x**3*(-2*cos(1)*cos(2)/3 + 2*sin(1)*sin(2)/3) + x**4*(sin(1)*cos(2)/3 + sin(2)*cos(1)/3) + x**5*(-2*sin(1)*sin(2)/15 + 2*cos(1)*cos(2)/15) + x**6*(-2*sin(2)*cos(1)/45 - 2*sin(1)*cos(2)/45) + x**7*(-4*cos(1)*cos(2)/315 + 4*sin(1)*sin(2)/315) + x**8*(sin(1)*cos(2)/315 + sin(2)*cos(1)/315) + x**9*(-2*sin(1)*sin(2)/2835 + 2*cos(1)*cos(2)/2835) + O(x**10)`

@isuruf No at the moment there is no way to customise the term printing, only coefficient printing

regarding the segfaults, have you tried to run it through valgrind?

`1 - poly`

should be implemented
using it in my python programs so I can better understand its functionality which would help me understand what the issues posted on github mean. Presently I am unaware how input and output works in symengine.

You can do that as well. See

`benchmarks/`

folder for examples
@bluescarni,

`1 - poly`

is implemented. Sorry for the noise. It was a bug in Expression, the implementation of `Expression &operator=(Expression &&other)`

called itself and the stack overflows
nice job finding out, stack overflows can be nasty to figure out

Yeah, thanks to your suggestion, it was easy to find out using valgrind

you can also use clang's/gcc's address sanitizer, it works pretty well and it's faster than valgrind usually

This was the previous implementation

```
Expression &operator=(Expression &&other) SYMENGINE_NOEXCEPT
{
if (this != &other) {
*this = std::move(other);
}
return *this;
}
```

Changed the 3rd line to

`this->m_basic = other.m_basic;`

. It is the same as `this->m_basic = std::move(other.m_basic);`

right?
ok... and the semantics of copy-assignment of RCP is just to copy the underlying pointer?

wait... unless RCP does reference counting though?

because if it does need to increase the reference, then a copy will result also in an atomic operation on the reference counter

so, in the hypothesis that RCP has a "true" move assignment operator (that is, that just transfers ownership without changing counts), then the move will be more efficient

It does have such a move assignment operator. Thanks for the info

just FYI, so there's one other thing now that I see the

`SYMENGINE_NOEXCEPT`

macro.. I imagine it's for supporting compilers that do not have `noexcept`

?
ok because by default Piranha requires all types (including coefficients) to have

`noexcept`

semantics for move operations and for the destructors
it should not be a problem because probably Piranha could not be compiled anyway by any existing C++11 compiler that does not have

`noexcept`

just a bit of info

but if you need to use C++03 classes as coefficient types, you can disable the

`noexcept`

checks by specialising a type trait
you would need to do that for instance if one wanted to use GINAC expressions as coefficient type

That's not a problem, and as you mentioned

`noexcept`

will be there if you can compile Piranha
Managed to compile and run some perf tests for Piranha on the Raspberry Pi 2 :)