Esempio n. 1
0
 def apply(self, f, xs, evaluation):
     'SageIntegrate[f_, xs__]'
     
     xs = xs.get_sequence()
     vars = []
     for x in xs:
         if x.has_form('List', 3):
             x, a, b = x.leaves
         else:
             a = b = None
         if not x.get_name():
             return evaluation.message('Integrate', 'ilim')
         if a is None or b is None:
             vars.append(x)
         else:
             vars.append((x, a, b))
     (f, vars), subs = to_sage((f, vars), evaluation)
     
     try:
         result = sage.integrate(f, *vars)
     except TypeError:
         # SageIntegrate[f[x], x] raises TypeError because maxima can't handle unknown functions, obviously
         return
     if a is not None and b is not None:
         prec_a = a.get_precision()
         prec_b = b.get_precision()
         if prec_a is not None and prec_b is not None:
             #prec = min(prec_a, prec_b)
             result = sage.n(result)
             
     return from_sage(result, subs)
Esempio n. 2
0
    def apply(self, f, xs, evaluation):
        'SageIntegrate[f_, xs__]'

        xs = xs.get_sequence()
        vars = []
        for x in xs:
            if x.has_form('List', 3):
                x, a, b = x.leaves
            else:
                a = b = None
            if not x.get_name():
                return evaluation.message('Integrate', 'ilim')
            if a is None or b is None:
                vars.append(x)
            else:
                vars.append((x, a, b))
        (f, vars), subs = to_sage((f, vars), evaluation)

        try:
            result = sage.integrate(f, *vars)
        except TypeError:
            # SageIntegrate[f[x], x] raises TypeError because maxima can't handle unknown functions, obviously
            return
        if a is not None and b is not None:
            prec_a = a.get_precision()
            prec_b = b.get_precision()
            if prec_a is not None and prec_b is not None:
                #prec = min(prec_a, prec_b)
                result = sage.n(result)

        return from_sage(result, subs)
    def apply(self, c, G, h, evaluation):
        "LinearProgramming[c_, G_, h_]"

        (c, G, h), subs = to_sage((c, G, h), evaluation)
        n = len(c)
        c = vector(c)
        G = matrix([[-item for item in row] for row in G] + [unit_vector(k, n, -1) for k in range(n)])
        h = vector([-item for item in h] + [0] * n)
        result = linear_program(c, G, h)
        status = result["status"]
        if status == "dual infeasible":
            evaluation.message("LinearProgramming", "lpsub")
            return Expression("List", *([Symbol("Indeterminate")] * n))
        elif status == "primal infeasible":
            return evaluation.message("LinearProgramming", "lpsnf")
        # print result
        x = result["x"]
        x = [round(value, mpf("0.000001")) for value in x]  # round result to 6 digits after comma
        return from_sage(x, subs)
    def apply(self, c, G, h, evaluation):
        "LinearProgramming[c_, G_, h_]"

        (c, G, h), subs = to_sage((c, G, h), evaluation)
        n = len(c)
        c = vector(c)
        G = matrix([[-item for item in row]
                    for row in G] + [unit_vector(k, n, -1) for k in range(n)])
        h = vector([-item for item in h] + [0] * n)
        result = linear_program(c, G, h)
        status = result['status']
        if status == 'dual infeasible':
            evaluation.message('LinearProgramming', 'lpsub')
            return Expression('List', *([Symbol('Indeterminate')] * n))
        elif status == 'primal infeasible':
            return evaluation.message('LinearProgramming', 'lpsnf')
        #print result
        x = result['x']
        x = [round(value, 6)
             for value in x]  # round result to 6 digits after comma
        return from_sage(x, subs)