Beispiel #1
0
    def map_sum(self, expr, enclosing_prec):
        tgt_dtype = self.infer_type(expr)
        is_complex = tgt_dtype.kind == 'c'

        if not is_complex:
            return CCodeMapperBase.map_sum(self, expr, enclosing_prec)
        else:
            tgt_name = self.complex_type_name(tgt_dtype)

            reals = [
                child for child in expr.children
                if 'c' != self.infer_type(child).kind
            ]
            complexes = [
                child for child in expr.children
                if 'c' == self.infer_type(child).kind
            ]

            from pymbolic.mapper.stringifier import PREC_SUM
            real_sum = self.join_rec(" + ", reals, PREC_SUM)
            complex_sum = self.join_rec(" + ", complexes, PREC_SUM)

            if real_sum:
                result = "%s_fromreal(%s) + %s" % (tgt_name, real_sum,
                                                   complex_sum)
            else:
                result = complex_sum

            return self.parenthesize_if_needed(result, enclosing_prec,
                                               PREC_SUM)
Beispiel #2
0
    def map_sum(self, expr, enclosing_prec):
        tgt_dtype = self.infer_type(expr)
        is_complex = tgt_dtype.kind == "c"

        if not is_complex:
            return CCodeMapperBase.map_sum(self, expr, enclosing_prec)
        else:
            tgt_name = complex_type_name(tgt_dtype)

            reals = [child for child in expr.children if "c" != self.infer_type(child).kind]
            complexes = [child for child in expr.children if "c" == self.infer_type(child).kind]

            from pymbolic.mapper.stringifier import PREC_SUM, PREC_NONE

            real_sum = self.join_rec(" + ", reals, PREC_SUM)

            if len(complexes) == 1:
                myprec = PREC_SUM
            else:
                myprec = PREC_NONE

            complex_sum = self.rec(complexes[0], myprec)
            for child in complexes[1:]:
                complex_sum = "%s_add(%s, %s)" % (tgt_name, complex_sum, self.rec(child, PREC_NONE))

            if real_sum:
                result = "%s_add(%s_fromreal(%s), %s)" % (tgt_name, tgt_name, real_sum, complex_sum)
            else:
                result = complex_sum

            return self.parenthesize_if_needed(result, enclosing_prec, PREC_SUM)
Beispiel #3
0
    def map_sum(self, expr, enclosing_prec):
        tgt_dtype = self.infer_type(expr)
        is_complex = tgt_dtype.kind == "c"

        if not is_complex:
            return CCodeMapperBase.map_sum(self, expr, enclosing_prec)
        else:
            tgt_name = complex_type_name(tgt_dtype)

            reals = [
                child for child in expr.children
                if "c" != self.infer_type(child).kind
            ]
            complexes = [
                child for child in expr.children
                if "c" == self.infer_type(child).kind
            ]

            from pymbolic.mapper.stringifier import PREC_SUM, PREC_NONE
            real_sum = self.join_rec(" + ", reals, PREC_SUM)

            if len(complexes) == 1:
                myprec = PREC_SUM
            else:
                myprec = PREC_NONE

            complex_sum = self.rec(complexes[0], myprec)
            for child in complexes[1:]:
                complex_sum = "{}_add({}, {})".format(
                    tgt_name, complex_sum, self.rec(child, PREC_NONE))

            if real_sum:
                result = "{}_add({}_fromreal({}), {})".format(
                    tgt_name, tgt_name, real_sum, complex_sum)
            else:
                result = complex_sum

            return self.parenthesize_if_needed(result, enclosing_prec,
                                               PREC_SUM)
Beispiel #4
0
    def map_sum(self, expr, enclosing_prec):
        tgt_dtype = self.infer_type(expr)
        is_complex = tgt_dtype.kind == 'c'

        if not is_complex:
            return CCodeMapperBase.map_sum(self, expr, enclosing_prec)
        else:
            tgt_name = self.complex_type_name(tgt_dtype)

            reals = [child for child in expr.children
                    if 'c' != self.infer_type(child).kind]
            complexes = [child for child in expr.children
                    if 'c' == self.infer_type(child).kind]

            from pymbolic.mapper.stringifier import PREC_SUM
            real_sum = self.join_rec(" + ", reals, PREC_SUM)
            complex_sum = self.join_rec(" + ", complexes, PREC_SUM)

            if real_sum:
                result = "%s_fromreal(%s) + %s" % (tgt_name, real_sum, complex_sum)
            else:
                result = complex_sum

            return self.parenthesize_if_needed(result, enclosing_prec, PREC_SUM)