def trig_replace(self, M, angle, name): """Replaces trigonometric expressions cos(x) and sin(x) by CX and SX Parameters ========== M: var or Matrix Object of substitution angle: var symbol that stands for the angle value name: int or string brief name X for the angle Notes ===== The cos(x) and sin(x) will be replaced by CX and SX, where X is the name and x is the angle """ if not isinstance(angle, Expr) or angle.is_number: return M cos_sym, sin_sym = tools.cos_sin_syms(name) sym_list = [(cos_sym, cos(angle)), (sin_sym, sin(angle))] subs_dict = {} for sym, sym_old in sym_list: if sym_old.has(-1): subs_dict[-sym_old] = -sym else: subs_dict[sym_old] = sym self.add_to_dict(sym, sym_old) for i1 in xrange(M.shape[0]): for i2 in xrange(M.shape[1]): M[i1, i2] = M[i1, i2].subs(subs_dict) return M
def trig_replace(self, M, angle, name): """Replaces trigonometric expressions cos(x) and sin(x) by CX and SX Parameters ========== M: var or Matrix Object of substitution angle: var symbol that stands for the angle value name: int or string brief name X for the angle Notes ===== The cos(x) and sin(x) will be replaced by CX and SX, where X is the name and x is the angle """ if not isinstance(angle, Expr) or angle.is_number: return M cos_sym, sin_sym = tools.cos_sin_syms(name) sym_list = [(cos_sym, cos(angle)), (sin_sym, sin(angle))] subs_dict = {} for sym, sym_old in sym_list: if -1 in Mul.make_args(sym_old): sym_old = -sym_old subs_dict[sym_old] = sym self.add_to_dict(sym, sym_old) for i1 in xrange(M.shape[0]): for i2 in xrange(M.shape[1]): M[i1, i2] = M[i1, i2].subs(subs_dict) return M
def CS12_simp(self, sym, silent=False): """ Example ======= >> print SymbolManager().CS12_simp(sympify("C2*C3 - S2*S3")) C23 = C2*C3 - S2*S3 C23 >> print SymbolManager().CS12_simp(sympify("C2*S3*R + S2*C3*R")) S23 = C2*S3 + S2*C3 R*S23 """ if not sym.is_Add: repl_dict = {} for term in sym.atoms(Add): repl_dict[term] = self.CS12_simp(term) sym = sym.xreplace(repl_dict) return sym names, short_form = tools.trignometric_info(sym) names = list(names) if short_form: names.sort() sym2 = sym for n1, n2 in itertools.combinations(names, 2): if short_form: C1, S1 = tools.cos_sin_syms(n1) C2, S2 = tools.cos_sin_syms(n2) np1, nm1 = tools.get_pos_neg(n1) np2, nm2 = tools.get_pos_neg(n2) n12 = tools.ang_sum(np1, np2, nm1, nm2) nm12 = tools.ang_sum(np1, nm2, nm1, np2) C12, S12 = tools.cos_sin_syms(n12) C1m2, S1m2 = tools.cos_sin_syms(nm12) else: C1, S1 = cos(n1), sin(n1) C2, S2 = cos(n2), sin(n2) C12, S12 = cos(n1+n2), sin(n1+n2) C1m2, S1m2 = cos(n1-n2), sin(n1-n2) sym2 = self.try_opt(S12, S1m2, S1*C2, C1*S2, sym2, silent) sym2 = self.try_opt(C12, C1m2, C1*C2, -S1*S2, sym2, silent) if sym2 != sym: return self.CS12_simp(sym2, silent) else: return sym
def CS12_simp(self, sym, silent=False): """ Example ======= >> print SymbolManager().CS12_simp(sympify("C2*C3 - S2*S3")) C23 = C2*C3 - S2*S3 C23 >> print SymbolManager().CS12_simp(sympify("C2*S3*R + S2*C3*R")) S23 = C2*S3 + S2*C3 R*S23 """ if not sym.is_Add: repl_dict = {} for term in sym.atoms(Add): repl_dict[term] = self.CS12_simp(term) sym = sym.xreplace(repl_dict) return sym names, short_form = tools.trignometric_info(sym) names = list(names) if short_form: names.sort() sym2 = sym for n1, n2 in itertools.combinations(names, 2): if short_form: C1, S1 = tools.cos_sin_syms(n1) C2, S2 = tools.cos_sin_syms(n2) np1, nm1 = tools.get_pos_neg(n1) np2, nm2 = tools.get_pos_neg(n2) n12 = tools.ang_sum(np1, np2, nm1, nm2) nm12 = tools.ang_sum(np1, nm2, nm1, np2) C12, S12 = tools.cos_sin_syms(n12) C1m2, S1m2 = tools.cos_sin_syms(nm12) else: C1, S1 = cos(n1), sin(n1) C2, S2 = cos(n2), sin(n2) C12, S12 = cos(n1 + n2), sin(n1 + n2) C1m2, S1m2 = cos(n1 - n2), sin(n1 - n2) sym2 = self.try_opt(S12, S1m2, S1 * C2, C1 * S2, sym2, silent) sym2 = self.try_opt(C12, C1m2, C1 * C2, -S1 * S2, sym2, silent) if sym2 != sym: return self.CS12_simp(sym2, silent) else: return sym
def C2S2_simp(self, sym): """ Example ======= >> print C2S2_simp(sympify("-C**2*RL + S*(D - RL*S)")) D*S - RL """ if not sym.is_Add: repl_dict = {} for term in sym.atoms(Add): repl_dict[term] = self.C2S2_simp(term) sym = sym.xreplace(repl_dict) return sym names, short_form = tools.trignometric_info(sym) for name in names: if short_form: cos_term, sin_term = tools.cos_sin_syms(name) else: cos_term, sin_term = cos(name), sin(name) sym = self.try_opt(tools.ONE, None, sin_term**2, cos_term**2, sym) return sym
def C2S2_simp(self, sym): """ Example ======= >> print C2S2_simp(sympify("-C**2*RL + S*(D - RL*S)")) D*S - RL """ if not sym.is_Add: repl_dict = {} for term in sym.atoms(Add): repl_dict[term] = self.C2S2_simp(term) sym = sym.xreplace(repl_dict) return sym names, short_form = tools.trignometric_info(sym) for name in names: if short_form: cos_term, sin_term = tools.cos_sin_syms(name) else: cos_term, sin_term = cos(name), sin(name) sym = self.try_opt( tools.ONE, None, sin_term**2, cos_term**2, sym ) return sym