Skip to content

Commit

Permalink
Fix imaginary artifacts appearing in exponent evaluation.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Oct 30, 2018
1 parent d559b80 commit c0c521a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
7 changes: 5 additions & 2 deletions expreduce/builtin_list.go
Expand Up @@ -207,10 +207,13 @@ func getListDefinitions() (defs []Definition) {
if len(this.GetParts()) >= 3 {
mis, isOk := iterspec.MultiSpecFromLists(es, this.GetParts()[2:])
if isOk {
// Simulate evaluation within Block[]
toReturn := atoms.NewExpression([]expreduceapi.Ex{atoms.NewSymbol("System`List")})
for mis.Cont() {
toReturn.AppendEx(matcher.ReplacePD(this.GetParts()[1].DeepCopy(), es, mis.CurrentPDManager()))
thisVal := this.GetParts()[1].DeepCopy()
thisVal = matcher.ReplacePD(thisVal, es, mis.CurrentPDManager())
thisVal = es.Eval(thisVal)
thisVal = matcher.ReplacePD(thisVal, es, mis.CurrentPDManager())
toReturn.AppendEx(thisVal)
mis.Next()
}
return toReturn
Expand Down
1 change: 0 additions & 1 deletion expreduce/builtin_plot.go
@@ -1,7 +1,6 @@
package expreduce

func getPlotDefinitions() (defs []Definition) {
defs = append(defs, Definition{Name: "ExpreducePlotPoints"})
defs = append(defs, Definition{Name: "Plot"})
return
}
7 changes: 7 additions & 0 deletions expreduce/builtin_power.go
Expand Up @@ -218,6 +218,13 @@ func getPowerDefinitions() (defs []Definition) {
inner.DeepCopy(),
),
)).(*atoms.Flt)
// If the exponent has no fractional part, i.e. should be an integer, then we can say there will be no imaginary component to the result.
// Reduce[Sin[b*Arg[a]] == 0, b, Reals] // FullSimplify
// C[1] \[Element] Integers && a < 0 && (b == 2 C[1] || b == 1 + 2 C[1])
if powerFlt.Val.IsInt() {
// TODO: We may want to decide this earlier. Figure this out.
return re
}
im :=

es.Eval(atoms.E(
Expand Down
8 changes: 4 additions & 4 deletions expreduce/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 20 additions & 20 deletions expreduce/resources/plot.m
@@ -1,11 +1,11 @@
Attributes[ExpreducePlotPoints] = {HoldAll};
ExpreducePlotPoints[fn_, range_List] :=
ExpreducePlotPoints[fn_, range_List] :=
Module[{nPoints, stepSize, replacedFn, unfilteredPoints},
nPoints = 500;
stepSize = (range[[3]] - range[[2]])/nPoints // N;
replacedFn = fn /. range[[1]] -> varOfIteration;
unfilteredPoints =
Table[{varOfIteration, replacedFn // N}, {varOfIteration,
unfilteredPoints =
Table[{varOfIteration, replacedFn // N}, {varOfIteration,
range[[2]], range[[3]], stepSize}];
Select[unfilteredPoints, (Im[#[[2]]] == 0) &]
];
Expand All @@ -14,23 +14,23 @@
Attributes[Plot] = {HoldAll, Protected, ReadProtected}
Plot[fn_, range_List] := Module[{plotPoints, fullRange, displayOptions},
plotPoints = ExpreducePlotPoints[fn, range];
fullRange = {MinMax[Join[plotPoints[[All, 1]], range[[2 ;; 3]]]],
fullRange = {MinMax[Join[plotPoints[[All, 1]], range[[2 ;; 3]]]],
MinMax[plotPoints[[All, 2]]]};
displayOptions = {DisplayFunction -> Identity,
AspectRatio -> GoldenRatio^(-1), Axes -> {True, True},
AxesLabel -> {None, None}, AxesOrigin -> {0, 0},
DisplayFunction :> Identity,
Frame -> {{False, False}, {False, False}},
FrameLabel -> {{None, None}, {None, None}},
FrameTicks -> {{Automatic, Automatic}, {Automatic, Automatic}},
GridLines -> {None, None},
GridLinesStyle -> Directive[GrayLevel[0.5, 0.4]],
Method -> {"DefaultBoundaryStyle" -> Automatic,
"DefaultMeshStyle" -> AbsolutePointSize[6],
"ScalingFunctions" -> None}, PlotRange -> fullRange,
PlotRangeClipping -> True,
PlotRangePadding -> {{Scaled[0.02], Scaled[0.02]}, {Scaled[0.05],
displayOptions = {DisplayFunction -> Identity,
AspectRatio -> GoldenRatio^(-1), Axes -> {True, True},
AxesLabel -> {None, None}, AxesOrigin -> {0, 0},
DisplayFunction :> Identity,
Frame -> {{False, False}, {False, False}},
FrameLabel -> {{None, None}, {None, None}},
FrameTicks -> {{Automatic, Automatic}, {Automatic, Automatic}},
GridLines -> {None, None},
GridLinesStyle -> Directive[GrayLevel[0.5, 0.4]],
Method -> {"DefaultBoundaryStyle" -> Automatic,
"DefaultMeshStyle" -> AbsolutePointSize[6],
"ScalingFunctions" -> None}, PlotRange -> fullRange,
PlotRangeClipping -> True,
PlotRangePadding -> {{Scaled[0.02], Scaled[0.02]}, {Scaled[0.05],
Scaled[0.05]}}, Ticks -> {Automatic, Automatic}};
Graphics[{{{}, {}, {Directive[Opacity[1.],
RGBColor[0.37, 0.5, 0.71], AbsoluteThickness[1.6]],
Graphics[{{{}, {}, {Directive[Opacity[1.],
RGBColor[0.37, 0.5, 0.71], AbsoluteThickness[1.6]],
Line[plotPoints]}}}, displayOptions]];
1 change: 1 addition & 0 deletions expreduce/resources/trig.m
Expand Up @@ -61,6 +61,7 @@

ArcSin[p_Plus] := -ArcSin[-p] /; (MatchQ[p[[1]], -_] || p[[1]] < 0);
ArcSin[-x_] := -ArcSin[x];
ArcSin[0] := 0;
Attributes[ArcSin] = {Listable, NumericFunction, Protected, ReadProtected};

Attributes[ArcCos] = {Listable, NumericFunction, Protected, ReadProtected};
Expand Down

0 comments on commit c0c521a

Please sign in to comment.