# Ticket #10532: trac_1956_faster_MPowerSeries_mul2.patch

File trac_1956_faster_MPowerSeries_mul2.patch, 4.4 KB (added by pernici, 9 years ago)
• ## sage/rings/multi_power_series_ring.py

```# HG changeset patch
# User Mario Pernici <mario.pernici@gmail.com>
# Date 1295456545 -3600
# Node ID e0d02abc6dca0215ea783fc42404684403e1a3b2
# Parent  bc05e5342eeff321fe1eb0de2199e23c3c689863
Generic multiplication used for multiplication of multivariate series.

diff -r bc05e5342eef -r e0d02abc6dca sage/rings/multi_power_series_ring.py```
 a f = self._poly_ring(f) except TypeError: raise TypeError("Cannot coerce input to polynomial ring.") fg_to_bg_dict = dict((v,v*self._bg_ps_ring().gen()) for v in self._poly_ring().gens()) return self._bg_ps_ring(f.subs(fg_to_bg_dict)) degs = f.degrees() if degs: emax = 0 f1 = [0]*(sum(degs)+1) for x in f: e = sum(x[1].degrees()) if e > emax: emax = e f1[e] += x[0]*x[1] f1 = f1[:emax+1] else: f1 = f return self._bg_ps_ring(f1) def _send_to_fg(self,f): """ 4 + 4*f0 + 4*f2 """ return self._poly_ring(f.subs({self.__bg_indeterminate:1})) return self._poly_ring(sum(f.list()))
• ## sage/rings/multi_power_series_ring_element.py

`diff -r bc05e5342eef -r e0d02abc6dca sage/rings/multi_power_series_ring_element.py`
 a from sage.structure.nonexact import Nonexact from sage.structure.parent_gens import ParentWithGens from sage.structure.category_object import CategoryObject from sage.rings.polynomial.polynomial_element import Polynomial_generic_dense from sage.rings.power_series_poly import PowerSeries_poly def is_MPowerSeries(f): sage: all(S >= g.prec() for S in [sum(e) for e in diff.exponents()]) True """ f = left._bg_value * right._bg_value return MPowerSeries(left.parent(), f, prec=f.prec()) prec = left._bg_value._mul_prec(right._bg_value) if not isinstance(left._bg_value.polynomial(), Polynomial_generic_dense) or  not isinstance(right._bg_value.polynomial(), Polynomial_generic_dense): f = left._bg_value*right._bg_value elif prec is infinity: f = left._bg_value.polynomial()._mul_generic(right._bg_value.polynomial()) f = PowerSeries_poly(left._bg_value.parent(),f,prec,check = False) else: f = left._bg_value.polynomial()._mul_trunc_generic(right._bg_value.polynomial(),prec) f = PowerSeries_poly(left._bg_value.parent(),f,prec,check = True) return MPowerSeries(left.parent(), f, prec) #    def _rmul_(self, c): #        # multivariate power series rings are assumed to be commutative
• ## sage/rings/polynomial/polynomial_element.pyx

`diff -r bc05e5342eef -r e0d02abc6dca sage/rings/polynomial/polynomial_element.pyx`
 a coeffs.append(sum) return self._parent(coeffs) def _mul_trunc_generic(self, right, prec): # TODO optimize case self is right # merge with _mul_generic? x = self.list() y = right.list() cdef Py_ssize_t i, k, start, end cdef Py_ssize_t d1 = len(x)-1, d2 = len(y)-1 if d1 == -1: return self elif d2 == -1: return right elif d1 == 0: c = x[0] return self._parent([c*a for a in y]) elif d2 == 0: c = y[0] return self._parent([a*c for a in x]) coeffs = [] for k from 0 <= k <= min(d1+d2,prec-1): start = 0 if k <= d2 else k-d2 # max(0, k-d2) end =   k if k <= d1 else d1    # min(k, d1) sum = x[start] * y[k-start] for i from start < i <= end: sum += x[i] * y[k-i] coeffs.append(sum) return self._parent(coeffs) def _square_generic(self): x = self.list() cdef Py_ssize_t i, j