예제 #1
0
    lcm = SparsePolynomial.lcm([sp1,sp2])
    print("Expected: \t", "x**2*y**3 + x**3*y**2")
    print("Actual: \t", lcm)

    print("--- Division Test ---------------------------------------------------")
    sp1 = SparsePolynomial.from_string("x**2 - 1", ['x'])
    sp2 = SparsePolynomial.from_string("x - 1", ['x'])
    quo = sp1//sp2
    print("Expected: \t", "x + 1")
    print("Actual: \t", quo)

    sp = SparsePolynomial.from_string("1",['x'])
    assert sp.is_unitary()

    sp = SparsePolynomial(['x'])
    assert sp.is_zero()

    rf = RationalFunction.from_string("(PI3KInactive**3*boundEGFReceptor**2*kPI3K + PI3KInactive**3*RasActive**2*kPI3KRas + KmPI3K*PI3KInactive**2*RasActive**2*kPI3KRas + KmPI3KRas*PI3KInactive**2*boundEGFReceptor**2*kPI3K)/(PI3KInactive**2 + KmPI3K*KmPI3KRas + KmPI3K*PI3KInactive + KmPI3KRas*PI3KInactive)",
                                        ['PI3KInactive',
                                        'boundEGFReceptor',
                                        'kPI3K',
                                        'RasActive',
                                        'kPI3KRas',
                                        'KmPI3K',
                                        'KmPI3KRas',
                                        ])
    print(rf)
    rf.simplify()
    print(rf)

예제 #2
0
    def perform_change_of_variables(self, rhs, new_vars_name='y'):
        """
          Restrict a system of ODEs with the rhs given by
          rhs (SparsePolynomial or RationalFunction) to the subspace
          new_vars_name (optional) - the name for variables in the lumped polynomials
        """
        old_vars = rhs[0].gens
        domain = rhs[0].domain
        new_vars = [new_vars_name + str(i) for i in range(self.dim())]
        pivots = set(self.parametrizing_coordinates())
        lpivots = sorted(pivots)
        basis = self.basis()

        logging.debug("Constructing new rhs")
        if isinstance(rhs[0], SparsePolynomial):
            new_rhs = [
                SparsePolynomial(old_vars, domain) for _ in range(self.dim())
            ]
        elif isinstance(rhs[0], RationalFunction):
            new_rhs = [
                RationalFunction(
                    SparsePolynomial(old_vars, domain),
                    SparsePolynomial.from_string("1", old_vars, domain))
                for _ in range(self.dim())
            ]
        for i, vec in enumerate(basis):
            logging.debug(f"    Equation number {i}")
            for j in vec.nonzero_iter():
                # ordering is important due to the implementation of
                # multiplication for SparsePolynomial
                new_rhs[i] += rhs[j] * vec._data[j]

        logging.debug("Plugging zero to nonpivot coordinates")

        if isinstance(rhs[0], SparsePolynomial):
            shrinked_polys = []
            for p in new_rhs:
                filtered_dict = dict()
                for monom, coef in p.dataiter():
                    new_monom = []
                    skip = False
                    for var, exp in monom:
                        if var not in pivots:
                            skip = True
                            break
                        else:
                            new_monom.append((lpivots.index(var), exp))
                    if not skip:
                        new_monom = tuple(new_monom)
                        filtered_dict[new_monom] = coef

                shrinked_polys.append(
                    SparsePolynomial(new_vars, domain, filtered_dict))

            return shrinked_polys

        elif isinstance(rhs[0], RationalFunction):
            # plugging all nonpivot variables with zeros
            shrinked_rfs = []
            for rf in new_rhs:

                num_filtered_dict = dict()
                for monom, coef in rf.num.dataiter():
                    new_monom = []
                    skip = False
                    for var, exp in monom:
                        if var not in pivots:
                            skip = True
                            break
                        else:
                            new_monom.append((lpivots.index(var), exp))
                    if not skip:
                        new_monom = tuple(new_monom)
                        num_filtered_dict[new_monom] = coef
                new_num = SparsePolynomial(new_vars, domain, num_filtered_dict)

                denom_filtered_dict = dict()
                for monom, coef in rf.denom.dataiter():
                    new_monom = []
                    skip = False
                    for var, exp in monom:
                        if var not in pivots:
                            skip = True
                            break
                        else:
                            new_monom.append((lpivots.index(var), exp))
                    if not skip:
                        new_monom = tuple(new_monom)
                        denom_filtered_dict[new_monom] = coef
                new_denom = SparsePolynomial(new_vars, domain,
                                             denom_filtered_dict)

                if new_denom.is_zero() and False:
                    print()
                    print("Before plugging all nonpivot variables with zeros:")
                    print('\t', rf)
                    print()
                    print("After plugging all nonpivot variables with zeros:")
                    print('\t', f"({new_num})/({new_denom})")
                    print()
                    raise ZeroDivisionError

                shrinked_rfs.append(RationalFunction(new_num, new_denom))

            return shrinked_rfs