These are chat archives for symengine/symengine

25th
Dec 2015
Francesco Biscani
@bluescarni
Dec 25 2015 01:00
Merry XMas/Yule/Saturnalia whatever-floats-your-boat, by the way :)
Isuru Fernando
@isuruf
Dec 25 2015 01:16
Wish you the same
piranha::polynomial<Expression,piranha::monomial<short>> would just work?
Francesco Biscani
@bluescarni
Dec 25 2015 01:21
yes it did work, at least for basic stuff... Expression is just a value-semantics wrapper for Basic right?
Isuru Fernando
@isuruf
Dec 25 2015 01:22
yes. It's your implementation
Francesco Biscani
@bluescarni
Dec 25 2015 01:24
right then it should still work, unless I broke something in Piranha in the meantime
Isuru Fernando
@isuruf
Dec 25 2015 01:30
p.pow(n), p.partial("x") doesn't work or am I missing something?
Francesco Biscani
@bluescarni
Dec 25 2015 01:47
yep that sounds about right... so
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
Francesco Biscani
@bluescarni
Dec 25 2015 01:52
Isuru Fernando
@isuruf
Dec 25 2015 01:53
What is the namespace for partial_impl? piranha::detail?
Francesco Biscani
@bluescarni
Dec 25 2015 01:54
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
Isuru Fernando
@isuruf
Dec 25 2015 01:55
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
Francesco Biscani
@bluescarni
Dec 25 2015 01:56
is the capital P in the Piranha namespace a copy typo?
Isuru Fernando
@isuruf
Dec 25 2015 01:56
@bluescarni, yes, integral exponentiation is enough.
Ah no
Francesco Biscani
@bluescarni
Dec 25 2015 01:57
you might need to qualify Expression with symengine::Expression as well
Isuru Fernando
@isuruf
Dec 25 2015 01:57
Now it compiles fine
Francesco Biscani
@bluescarni
Dec 25 2015 01:57
ah ok cool
Isuru Fernando
@isuruf
Dec 25 2015 01:58
For pow(int) what do I need to specialize ?
Francesco Biscani
@bluescarni
Dec 25 2015 01:58
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
Isuru Fernando
@isuruf
Dec 25 2015 02:02
Yes, I'm editing expression.h
Francesco Biscani
@bluescarni
Dec 25 2015 02:02
ok cool
Isuru Fernando
@isuruf
Dec 25 2015 02:05
It compiles now. For printing does it use the std::ostream overloads?
Francesco Biscani
@bluescarni
Dec 25 2015 02:05
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
Isuru Fernando
@isuruf
Dec 25 2015 02:07
:D
Francesco Biscani
@bluescarni
Dec 25 2015 02:11
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
Isuru Fernando
@isuruf
Dec 25 2015 02:11
For subs what do I need to implement?
Francesco Biscani
@bluescarni
Dec 25 2015 02:11
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...
Francesco Biscani
@bluescarni
Dec 25 2015 02:17
depends on what you are interested in
Isuru Fernando
@isuruf
Dec 25 2015 02:17
a Series of the same type.
Francesco Biscani
@bluescarni
Dec 25 2015 02:18
mhm I think we are confusing things... so this subs_impl is needed to "teach" piranha how to substitute in Expressions
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
Isuru Fernando
@isuruf
Dec 25 2015 02:20
My mistake. How to substitute a var in a polynomial with another polynomial
Francesco Biscani
@bluescarni
Dec 25 2015 02:20
var is a symengine symbolic variables or one of the variables in piranha's polynomial?
Isuru Fernando
@isuruf
Dec 25 2015 02:20
variable of the polynomial
Francesco Biscani
@bluescarni
Dec 25 2015 02:21
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?
Isuru Fernando
@isuruf
Dec 25 2015 02:22
I can select that by passing the name of the variable right?
Francesco Biscani
@bluescarni
Dec 25 2015 02:23
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?
Isuru Fernando
@isuruf
Dec 25 2015 02:23
yes
Francesco Biscani
@bluescarni
Dec 25 2015 02:24
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
Isuru Fernando
@isuruf
Dec 25 2015 02:25
Yeah, it says I need to implement subs_term_impl. I'll do that.
Francesco Biscani
@bluescarni
Dec 25 2015 02:26
mhm.. no that is a private method which is not supposed to be specialised
Isuru Fernando
@isuruf
Dec 25 2015 02:27
[  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
Francesco Biscani
@bluescarni
Dec 25 2015 02:28
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
Isuru Fernando
@isuruf
Dec 25 2015 02:29
yes
Francesco Biscani
@bluescarni
Dec 25 2015 02:30
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
Isuru Fernando
@isuruf
Dec 25 2015 02:31
Yes, it's compiling now
Francesco Biscani
@bluescarni
Dec 25 2015 02:33
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
Isuru Fernando
@isuruf
Dec 25 2015 02:33
Nope. long is fine, but long long is not
Francesco Biscani
@bluescarni
Dec 25 2015 02:34
ah right ok
Isuru Fernando
@isuruf
Dec 25 2015 02:34
i.e. limitations of GMP
Francesco Biscani
@bluescarni
Dec 25 2015 02:34
yes it's pretty yucky
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
Isuru Fernando
@isuruf
Dec 25 2015 02:55
Btw, there are custom CMake scripts with pre-compiled header support. Might be useful as including piranha increases the compile time significantly
Francesco Biscani
@bluescarni
Dec 25 2015 02:56
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
Isuru Fernando
@isuruf
Dec 25 2015 03:03

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?

Francesco Biscani
@bluescarni
Dec 25 2015 03:08
what do you mean by that?
Isuru Fernando
@isuruf
Dec 25 2015 03:09
Piranha has a exception handling mechanism right? I just want it disabled, so that the exception is thrown at the right place
Francesco Biscani
@bluescarni
Dec 25 2015 03:10
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?
Isuru Fernando
@isuruf
Dec 25 2015 03:14
Even if it's an Exception it should work. Anyways, that doesn't matter, I'll use something else.
Francesco Biscani
@bluescarni
Dec 25 2015 03:14
can you post the code that trigger the behaviour on gist?
Isuru Fernando
@isuruf
Dec 25 2015 03:18
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
Francesco Biscani
@bluescarni
Dec 25 2015 03:19
ok I need to sleep as well, I'll be able to read about the snippet tomorrow hopefully
cya!
Ralf Stephan
@rwst
Dec 25 2015 06:21
@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.
Ralf Stephan
@rwst
Dec 25 2015 06:27
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.
Ralf Stephan
@rwst
Dec 25 2015 06:35
There are two Sage tickets whose discussion may be interesting:
http://trac.sagemath.org/ticket/3354
http://trac.sagemath.org/ticket/16198
Ralf Stephan
@rwst
Dec 25 2015 08:44
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.
Isuru Fernando
@isuruf
Dec 25 2015 09:11

@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
Isuru Fernando
@isuruf
Dec 25 2015 10:02

@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)
Francesco Biscani
@bluescarni
Dec 25 2015 12:28
@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
Akash Trehan
@CodeMaxx
Dec 25 2015 16:53
how do I start using symengine?
Isuru Fernando
@isuruf
Dec 25 2015 16:53
What do you mean by that?
Akash Trehan
@CodeMaxx
Dec 25 2015 16:57
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.
Isuru Fernando
@isuruf
Dec 25 2015 16:59
Ah. To use it in python, see https://github.com/symengine/symengine.py
Akash Trehan
@CodeMaxx
Dec 25 2015 17:00
what about in c++ programs?
Isuru Fernando
@isuruf
Dec 25 2015 17:00
You can do that as well. See benchmarks/ folder for examples
Akash Trehan
@CodeMaxx
Dec 25 2015 17:01
Ok. Anything else to be done with #678 ?
Isuru Fernando
@isuruf
Dec 25 2015 17:04
@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
Francesco Biscani
@bluescarni
Dec 25 2015 17:04
Ahh ok I see
nice job finding out, stack overflows can be nasty to figure out
Isuru Fernando
@isuruf
Dec 25 2015 17:06
Yeah, thanks to your suggestion, it was easy to find out using valgrind
Francesco Biscani
@bluescarni
Dec 25 2015 17:07
you can also use clang's/gcc's address sanitizer, it works pretty well and it's faster than valgrind usually
Isuru Fernando
@isuruf
Dec 25 2015 17:07
This was the previous implementation
    Expression &operator=(Expression &&other) SYMENGINE_NOEXCEPT
    {
        if (this != &other) {
            *this = std::move(other);
        }
        return *this;
    }
Francesco Biscani
@bluescarni
Dec 25 2015 17:08
yep sounds about infinite recursion :)
Isuru Fernando
@isuruf
Dec 25 2015 17:09
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?
Francesco Biscani
@bluescarni
Dec 25 2015 17:10
what is m_basic again? a naked pointer?
Isuru Fernando
@isuruf
Dec 25 2015 17:10

class Expression
{
private:
    RCP<const Basic> m_basic;
}
Francesco Biscani
@bluescarni
Dec 25 2015 17:11
ok... and the semantics of copy-assignment of RCP is just to copy the underlying pointer?
Isuru Fernando
@isuruf
Dec 25 2015 17:11
Yes
Francesco Biscani
@bluescarni
Dec 25 2015 17:11
ok then it should be equivalent
wait... unless RCP does reference counting though?
Isuru Fernando
@isuruf
Dec 25 2015 17:12
It does
Francesco Biscani
@bluescarni
Dec 25 2015 17:13
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
Isuru Fernando
@isuruf
Dec 25 2015 17:15
It does have such a move assignment operator. Thanks for the info
Francesco Biscani
@bluescarni
Dec 25 2015 17:15
sure np
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?
Isuru Fernando
@isuruf
Dec 25 2015 17:15
Yes
Francesco Biscani
@bluescarni
Dec 25 2015 17:16
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
Isuru Fernando
@isuruf
Dec 25 2015 17:18
That's not a problem, and as you mentioned noexcept will be there if you can compile Piranha
Francesco Biscani
@bluescarni
Dec 25 2015 17:19
yup
Francesco Biscani
@bluescarni
Dec 25 2015 23:59
Managed to compile and run some perf tests for Piranha on the Raspberry Pi 2 :)