Exemplo n.º 1
0
func solvepower(inputEq Equation, solvenum Express) ([]Equation, error) {
	solveside := inputEq.Leftexp
	constantside := inputEq.Rightexp
	root := *solveside.Root
	exponent := *solveside.Exponent
	if !exponent.Contains(solvenum) {
		if exponent.Evaluable(true) && smath.Isint(exponent.Tofloat()) &&
			int(exponent.Tofloat())%2 == 0 {
			returnsolveside := root
			returnconstantside := Power(constantside,
				Fraction(Number("1"), exponent))
			return []Equation{
				inputEq.changeeq(returnsolveside, returnconstantside),
				inputEq.changeeq(returnsolveside, Product(Number("-1"),
					returnconstantside))}, nil

		} else {
			return []Equation{
				inputEq.changeeq(root, Power(constantside, Fraction(Number("1"),
					exponent)))}, nil

		}
	}
	if !root.Contains(solvenum) {
		returnsolveside := exponent
		returnconstantside := Fraction(Natlog(constantside), Natlog(root))
		return []Equation{inputEq.changeeq(returnsolveside,
			returnconstantside)}, nil
	}
	return []Equation{}, errors.New("solvespecfail")
}
Exemplo n.º 2
0
func solveaddition(inputEq Equation, solvenum Express) ([]Equation, error) {
	solveside := inputEq.Leftexp
	constantside := inputEq.Rightexp
	var newaddends []Express
	newconstanadds := []Express{constantside}
	for _, addend := range *solveside.Addends {
		if addend.Contains(solvenum) {
			newaddends = append(newaddends, addend)
		} else {
			newconstanadds = append(newconstanadds, Product(Number("-1"),
				addend))
		}
	}
	returnsolveside := Addition(newaddends...)
	returnconstantside := Addition(newconstanadds...)
	if len(newaddends) != 1 {
		//degbool := false
		ispol := true
		var poladdends []polstruc
		poladdends = []polstruc{{Product(Number("-1"),
			returnconstantside).AlgebraicSimp(solvenum, false), 0}}
		var subvalue Express //the part that contains
		var subvaluebool bool
		for _, addend := range newaddends {
			var coeffadd polstruc
			var coeffaddbool bool
			if addend.Etype == "Product" {
				for index, fact := range *addend.Factors {
					if fact.Contains(solvenum) && !coeffaddbool &&
						fact.Etype == "Power" {
						if fact.Exponent.Evaluable(true) &&
							smath.Isint(fact.Exponent.Tofloat()) && (!subvaluebool) ||
							Expeq(*fact.Root, subvalue) && !coeffaddbool {
							subvalue = *fact.Root
							coeffadd = polstruc{addend.Delfactor(index),
								uint64(fact.Exponent.Tofloat())}
							coeffaddbool = true
							subvaluebool = true
						} else {
							ispol = false
							break
						}
					} else if fact.Contains(solvenum) && !coeffaddbool &&
						fact.Etype == "Product" {
						//Damned negative numbers making nested products
						if (*fact.Factors)[1].Etype == "Power" {
							exponent := *(*fact.Factors)[1].Exponent
							if exponent.Evaluable(true) &&
								smath.Isint(exponent.Tofloat()) && (!subvaluebool ||
								Expeq(*(*fact.Factors)[1].Root, subvalue) &&
									!coeffaddbool) {
								subvalue = *(*fact.Factors)[1].Root
								subvaluebool = true
								coeffadd = polstruc{Product(Number("-1"),
									addend.Delfactor(index)),
									uint64(exponent.Tofloat())}
								coeffaddbool = true
							} else {
								ispol = false
								break
							}

						} else {
							thisval := (*fact.Factors)[1]
							if !coeffaddbool && (!subvaluebool ||
								Expeq(thisval, subvalue)) {
								subvalue = thisval
								subvaluebool = true
								coeffadd = polstruc{Product(Number("-1"),
									addend.Delfactor(index)), 1}
								coeffaddbool = true
							} else {
								ispol = false
								break
							}
						}
					} else if fact.Contains(solvenum) && !coeffaddbool {
						if !coeffaddbool && (!subvaluebool || Expeq(subvalue, fact)) {
							subvalue = fact
							subvaluebool = true
							coeffadd = polstruc{addend.Delfactor(index), 1}
							coeffaddbool = true
						} else {
							ispol = false
							break
						}
					} else if fact.Contains(solvenum) {
						ispol = false
						break
					}

				}
			} else if addend.Etype == "Power" {
				if !coeffaddbool && (addend.Exponent.Evaluable(true) &&
					smath.Isint(addend.Exponent.Tofloat())) && (!subvaluebool ||
					Expeq(*addend.Root, subvalue)) {
					subvalue = *addend.Root
					coeffadd = polstruc{Number("1"), uint64(addend.Exponent.Tofloat())}
					subvaluebool, coeffaddbool = true, true
				} else {
					ispol = false
					break
				}
			} else {
				if !coeffaddbool && (!subvaluebool || Expeq(addend, subvalue)) {
					subvalue = addend
					coeffadd = polstruc{Number("1"), 1}
					subvaluebool, coeffaddbool = true, true
				} else {
					ispol = false
					break
				}
			}
			if coeffaddbool {
				poladdends = append(poladdends, coeffadd)
			}
		}
		if !subvaluebool || !ispol {
			return []Equation{}, errors.New("solvespecfail")
		}
		polsolveresult, err := Polynomialsolve(poladdends)
		if err != nil {
			return []Equation{}, errors.New("polsolvefail")
		}
		var retvar []Equation
		for _, solution := range polsolveresult {
			retvar = append(retvar, inputEq.changeeq(subvalue, solution))
		}
		return retvar, nil
	}
	return []Equation{inputEq.changeeq(returnsolveside,
		returnconstantside)}, nil
}