Example #1
0
    def to_stringio(self,
                    oss,
                    nb_digits,
                    use_space,
                    var_namer=lambda v: v.lp_name):
        # INTERNAL
        # Writes unicode representation of self
        c = 0
        # noinspection PyPep8Naming
        SP = u' '

        for v, coeff in self.iter_sorted_terms():
            if not coeff:
                continue  # pragma: no cover

            # 1 separator
            if use_space and c > 0:
                oss.write(SP)

            # ---
            # sign is printed if  non-first OR negative
            # at the end of this block coeff is positive
            if coeff < 0 or c > 0:
                oss.write(u'-' if coeff < 0 else u'+')
                if coeff < 0:
                    coeff = -coeff
                if use_space and c > 0:
                    oss.write(SP)
            # ---

            if 1 != coeff:
                self._num_to_stringio(oss, coeff, nb_digits)
                if use_space:
                    oss.write(SP)

            varname = var_namer(v)
            oss.write(unitext(varname))
            c += 1

        k = self.constant
        if c == 0:
            self._num_to_stringio(oss, k, nb_digits)
        elif k != 0:
            if k < 0:
                sign = u'-'
                k = -k
            else:
                sign = u'+'
            if use_space:
                oss.write(SP)
            oss.write(sign)
            if use_space:
                oss.write(SP)
            self._num_to_stringio(oss, k, nb_digits)
Example #2
0
 def to_stringio(self, oss, nb_digits, use_space, var_namer=lambda v: v.lp_name):
     self_coef = self._coef
     if self_coef != 1:
         if self_coef < 0:
             oss.write(u'-')
             self_coef = - self_coef
         if self_coef != 1:
             self._num_to_stringio(oss, num=self_coef, ndigits=nb_digits)
         if use_space:
             oss.write(u' ')
     oss.write(unitext(var_namer(self._dvar)))
Example #3
0
    def to_stringio(self,
                    oss,
                    nb_digits,
                    use_space,
                    var_namer=lambda v: v.lp_name):
        q = 0
        # noinspection PyPep8Naming
        SP = u' '
        for qvp, qk in self.iter_sorted_quads():
            if not qk:
                continue
            qv1 = qvp.first
            qv2 = qvp.second
            # ---
            # sign is printed if  non-first OR negative
            # at the end of this block coeff is positive
            if qk < 0 or q > 0:
                oss.write(u'-' if qk < 0 else u'+')
                if qk < 0:
                    qk = -qk
                if use_space and q > 0:
                    oss.write(SP)

            # write coeff if <> 1
            varname1 = var_namer(qv1)
            if 1 != qk:
                self._num_to_stringio(oss, num=qk, ndigits=nb_digits)
                if use_space:
                    oss.write(SP)

            oss.write(unitext(varname1))
            if qv1 is qv2:
                oss.write(u"^2")
            else:
                if use_space:
                    oss.write(SP)
                    oss.write(u'*')
                    oss.write(SP)
                else:
                    oss.write(u'*')
                oss.write(unitext(var_namer(qv2)))
            q += 1
        # problem for linexpr: force '+' ssi c>0
        linexpr = self._linexpr
        lin_constant = linexpr.get_constant()
        if linexpr:
            first_lk = 0
            for lv, lk in linexpr.iter_terms():
                if lk:
                    first_lk = lk
                    break
            if q > 0 and first_lk > 0:
                if use_space:
                    oss.write(u' ')
                oss.write(u"+")

            if first_lk:
                if use_space:
                    oss.write(SP)
                self._linexpr.to_stringio(oss, nb_digits, use_space, var_namer)

            elif lin_constant:
                self._num_to_stringio(oss,
                                      lin_constant,
                                      nb_digits,
                                      print_sign=True,
                                      force_plus=q > 0,
                                      use_space=use_space)
            elif not q:
                oss.write(u'0')