Skip to content

Commit

Permalink
Solve problems using PDF.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jan 19, 2018
1 parent 25c3224 commit ce41686
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 7 deletions.
1 change: 1 addition & 0 deletions expreduce/builtin.go
Expand Up @@ -192,6 +192,7 @@ func GetAllDefinitions() (defs []NamedDefSet) {
defs = append(defs, NamedDefSet{"boolean", GetBooleanDefinitions()})
defs = append(defs, NamedDefSet{"simplify", GetSimplifyDefinitions()})
defs = append(defs, NamedDefSet{"numbertheory", GetNumberTheoryDefinitions()})
defs = append(defs, NamedDefSet{"stats", GetStatsDefinitions()})
defs = append(defs, NamedDefSet{"manip", GetManipDefinitions()})
defs = append(defs, NamedDefSet{"rubi", GetRubiDefinitions()})

Expand Down
8 changes: 8 additions & 0 deletions expreduce/builtin_arithmetic.go
Expand Up @@ -256,6 +256,10 @@ func getArithmeticDefinitions() (defs []Definition) {
return res.Parts[1]
}

// Not exactly right because of "1. + foo[1]", but close enough.
if _, rIsReal := realPart.(*Flt); rIsReal {
return exprToN(es, res)
}
return res
},
})
Expand Down Expand Up @@ -335,6 +339,10 @@ func getArithmeticDefinitions() (defs []Definition) {
}
}

// Not exactly right because of "1. + foo[1]", but close enough.
if _, rIsReal := realPart.(*Flt); rIsReal {
return exprToN(es, res)
}
return res
},
})
Expand Down
8 changes: 8 additions & 0 deletions expreduce/builtin_stats.go
@@ -0,0 +1,8 @@
package expreduce

func GetStatsDefinitions() (defs []Definition) {
defs = append(defs, Definition{Name: "NormalDistribution"})
defs = append(defs, Definition{Name: "LogNormalDistribution"})
defs = append(defs, Definition{Name: "PDF"})
return
}
31 changes: 27 additions & 4 deletions expreduce/resources.go

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion expreduce/resources/arithmetic.m
Expand Up @@ -176,7 +176,10 @@
ESameTest[a^(2-c), a^2/a^c],
ESameTest[m^2, m*m],
ESameTest[1, m/m],
ESameTest[1, m^2/m^2]
ESameTest[1, m^2/m^2],

(*Conversion of exact numeric functions to reals*)
ESameTest[True, MatchQ[Sqrt[2*Pi]*.1, _Real]],
]
};

Expand Down
9 changes: 7 additions & 2 deletions expreduce/resources/solve.m
Expand Up @@ -51,8 +51,11 @@
C[1]\[Element]Integers
]
},
E, {pow -> ConditionalExpression[2 I Pi C[1] + Log[rhs],
C[1] \[Element] Integers]},
E, If[MatchQ[rhs, _Real],
{pow -> Log[rhs]},
{pow -> ConditionalExpression[2 I Pi C[1] + Log[rhs],
C[1] \[Element] Integers]},
],
_, Message[Solve::ifun, Solve];{pow -> Log[rhs]/Log[base]}
]]
];
Expand Down Expand Up @@ -239,6 +242,8 @@
(* Solve combination of Sin and ArcSin *)
ESameTest[{{x->-b+y}}, Solve[Sin[ArcSin[x+b]]==y,x]],
ESameTest[{{x->ConditionalExpression[Pi-ArcSin[Sin[y]]+2 Pi C[1],((Re[y]==-(Pi/2)&&Im[y]>=0)||-(Pi/2)<Re[y]<Pi/2||(Re[y]==Pi/2&&Im[y]<=0))&&C[1]\[Element]Integers]},{x->ConditionalExpression[ArcSin[Sin[y]]+2 Pi C[1],((Re[y]==-(Pi/2)&&Im[y]>=0)||-(Pi/2)<Re[y]<Pi/2||(Re[y]==Pi/2&&Im[y]<=0))&&C[1]\[Element]Integers]}}//normSol, Solve[ArcSin[Sin[x]]==y,x]//normSol],
(* Solve a problem involving a PDF *)
EStringTest["{{x -> -1.66352}, {x -> 1.66352}}", "Solve[PDF[NormalDistribution[0, 1], x] == .1, x]"],

(* POLYNOMIALS *)
ESameTest[{{x->(-b-Sqrt[b^2-4 a c])/(2 a)},{x->(-b+Sqrt[b^2-4 a c])/(2 a)}}, Solve[a*x^2==-b*x-c,x]],
Expand Down
26 changes: 26 additions & 0 deletions expreduce/resources/stats.m
@@ -0,0 +1,26 @@
NormalDistribution::usage = "`NormalDistribution[mu, sigma]` is a normal distribution with a mean `mu` and standard deviation of `sigma`.";
Attributes[NormalDistribution] = {ReadProtected, Protected};
Tests`NormalDistribution = {
ESimpleExamples[
ESameTest[E^(-((x-\[Mu])^2/(2 \[Sigma]^2)))/(Sqrt[2 \[Pi]] \[Sigma]), PDF[NormalDistribution[\[Mu],\[Sigma]],x]]
]
};

LogNormalDistribution::usage = "`LogNormalDistribution[mu, sigma]` is a lognormal distribution with a mean `mu` and standard deviation of `sigma`.";
Attributes[LogNormalDistribution] = {ReadProtected, Protected};
Tests`LogNormalDistribution = {
ESimpleExamples[
ESameTest[Piecewise[{{1/(E^((-\[Mu] + Log[x])^2/(2*\[Sigma]^2))*Sqrt[2*Pi]*x*\[Sigma]), x > 0}}, 0], PDF[LogNormalDistribution[\[Mu],\[Sigma]],x]]
]
};

PDF::usage = "`PDF[dist, var]` calculates the PDF of `dist`.";
PDF[NormalDistribution[mu_, sigma_], x_] := E^(-((-mu+x)^2/(2 sigma^2)))/(Sqrt[2 \[Pi]] sigma);
PDF[LogNormalDistribution[mu_, sigma_], x_] := Piecewise[{{1/(E^((-mu + Log[x])^2/(2*sigma^2))*Sqrt[2*Pi]*sigma*x), x > 0}}, 0]
Attributes[PDF] = {ReadProtected, Protected};
Tests`PDF = {
ESimpleExamples[
ESameTest[E^(-((x-\[Mu])^2/(2 \[Sigma]^2)))/(Sqrt[2 \[Pi]] \[Sigma]), PDF[NormalDistribution[\[Mu],\[Sigma]],x]],
ESameTest[Piecewise[{{1/(E^((-\[Mu] + Log[x])^2/(2*\[Sigma]^2))*Sqrt[2*Pi]*x*\[Sigma]), x > 0}}, 0], PDF[LogNormalDistribution[\[Mu],\[Sigma]],x]]
]
};

0 comments on commit ce41686

Please sign in to comment.