Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RealNumberQ->RealValuedNumberQ #897

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ New Builtins

* `Elements`
* `RealAbs` and `RealSign`
* `RealValuedNumberQ`


Compatibility
-------------
Expand Down
27 changes: 17 additions & 10 deletions mathics/builtin/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,34 +856,41 @@ class Real_(Builtin):
name = "Real"


class RealNumberQ(Test):
class RealValuedNumberQ(Test):
"""
## Not found in WMA
## <url>:WMA link:https://reference.wolfram.com/language/ref/RealNumberQ.html</url>
<url>:WMA link:https://reference.wolfram.com/language/ref/RealValuedNumberQ.html</url>

<dl>
<dt>'RealNumberQ[$expr$]'
<dt>'RealValuedNumberQ[$expr$]'
<dd>returns 'True' if $expr$ is an explicit number with no imaginary component.
</dl>

>> RealNumberQ[10]
>> RealValuedNumberQ[10]
= True
>> RealNumberQ[4.0]
>> RealValuedNumberQ[4.0]
= True
>> RealNumberQ[1+I]
>> RealValuedNumberQ[1+I]
= False
>> RealNumberQ[0 * I]
>> RealValuedNumberQ[0 * I]
= True
>> RealNumberQ[0.0 * I]
>> RealValuedNumberQ[0.0 * I]
= False

"Underflow[]" and "Overflow[]" are considered Real valued numbers:
>> {RealValuedNumberQ[Underflow[]], RealValuedNumberQ[Overflow[]]}
= {True, True}
"""

attributes = A_NO_ATTRIBUTES

summary_text = "test whether an expression is a real number"

def test(self, expr) -> bool:
return isinstance(expr, (Integer, Rational, Real))
return (
isinstance(expr, (Integer, Rational, Real))
or expr.has_form("Underflow", 0)
or expr.has_form("Overflow", 0)
)


class Sum(IterationFunction, SympyFunction):
Expand Down
12 changes: 6 additions & 6 deletions mathics/builtin/image/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ class RandomImage(Builtin):
}
rules = {
"RandomImage[]": "RandomImage[{0, 1}, {150, 150}]",
"RandomImage[max_?RealNumberQ]": "RandomImage[{0, max}, {150, 150}]",
"RandomImage[{minval_?RealNumberQ, maxval_?RealNumberQ}]": "RandomImage[{minval, maxval}, {150, 150}]",
"RandomImage[max_?RealNumberQ, {w_Integer, h_Integer}]": "RandomImage[{0, max}, {w, h}]",
"RandomImage[max_?RealValuedNumberQ]": "RandomImage[{0, max}, {150, 150}]",
"RandomImage[{minval_?RealValuedNumberQ, maxval_?RealValuedNumberQ}]": "RandomImage[{minval, maxval}, {150, 150}]",
"RandomImage[max_?RealValuedNumberQ, {w_Integer, h_Integer}]": "RandomImage[{0, max}, {w, h}]",
}
summary_text = "build an image with random pixels"

def eval(self, minval, maxval, w, h, evaluation, options):
"RandomImage[{minval_?RealNumberQ, maxval_?RealNumberQ}, {w_Integer, h_Integer}, OptionsPattern[RandomImage]]"
"RandomImage[{minval_?RealValuedNumberQ, maxval_?RealValuedNumberQ}, {w_Integer, h_Integer}, OptionsPattern[RandomImage]]"
color_space = self.get_option(options, "ColorSpace", evaluation)
if (
isinstance(color_space, Symbol)
Expand Down Expand Up @@ -203,11 +203,11 @@ class EdgeDetect(Builtin):

rules = {
"EdgeDetect[i_Image]": "EdgeDetect[i, 2, 0.2]",
"EdgeDetect[i_Image, r_?RealNumberQ]": "EdgeDetect[i, r, 0.2]",
"EdgeDetect[i_Image, r_?RealValuedNumberQ]": "EdgeDetect[i, r, 0.2]",
}

def eval(self, image, r, t, evaluation: Evaluation):
"EdgeDetect[image_Image, r_?RealNumberQ, t_?RealNumberQ]"
"EdgeDetect[image_Image, r_?RealValuedNumberQ, t_?RealValuedNumberQ]"
import skimage.feature

pixels = image.grayscale().pixels
Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/image/morph.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class _MorphologyFilter(Builtin):
}

requires = skimage_requires
rules = {"%(name)s[i_Image, r_?RealNumberQ]": "%(name)s[i, BoxMatrix[r]]"}
rules = {"%(name)s[i_Image, r_?RealValuedNumberQ]": "%(name)s[i, BoxMatrix[r]]"}

def eval(self, image, k, evaluation: Evaluation):
"%(name)s[image_Image, k_?MatrixQ]"
Expand Down Expand Up @@ -114,7 +114,7 @@ class MorphologicalComponents(Builtin):
rules = {"MorphologicalComponents[i_Image]": "MorphologicalComponents[i, 0]"}

def eval(self, image, t, evaluation: Evaluation):
"MorphologicalComponents[image_Image, t_?RealNumberQ]"
"MorphologicalComponents[image_Image, t_?RealValuedNumberQ]"
pixels = pixels_as_ubyte(
pixels_as_float(image.grayscale().pixels) > t.round_to_float()
)
Expand Down
6 changes: 3 additions & 3 deletions mathics/builtin/image/pixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PixelValue(Builtin):
summary_text = "get pixel value of image at a given position"

def eval(self, image: Image, x, y, evaluation: Evaluation):
"PixelValue[image_Image, {x_?RealNumberQ, y_?RealNumberQ}]"
"PixelValue[image_Image, {x_?RealValuedNumberQ, y_?RealValuedNumberQ}]"
x = int(x.round_to_float())
y = int(y.round_to_float())
height = image.pixels.shape[0]
Expand Down Expand Up @@ -86,13 +86,13 @@ class PixelValuePositions(Builtin):
"""

rules = {
"PixelValuePositions[image_Image, val_?RealNumberQ]": "PixelValuePositions[image, val, 0]"
"PixelValuePositions[image_Image, val_?RealValuedNumberQ]": "PixelValuePositions[image, val, 0]"
}

summary_text = "list the position of pixels with a given value"

def eval(self, image: Image, val, d, evaluation: Evaluation):
"PixelValuePositions[image_Image, val_?RealNumberQ, d_?RealNumberQ]"
"PixelValuePositions[image_Image, val_?RealValuedNumberQ, d_?RealValuedNumberQ]"
val = val.round_to_float()
d = d.round_to_float()

Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/manipulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
# "System`Private`ManipulateParameter[{{s_Symbol, d_}, r__}]": "System`Private`ManipulateParameter[{Symbol -> s, Default -> d, Label -> s}, {r}]",
# "System`Private`ManipulateParameter[{{s_Symbol, d_, l_}, r__}]": "System`Private`ManipulateParameter[{Symbol -> s, Default -> d, Label -> l}, {r}]",
# # detect different kinds of widgets. on the use of the duplicate key "Default ->", see _WidgetInstantiator.add()
# "System`Private`ManipulateParameter[var_, {min_?RealNumberQ, max_?RealNumberQ}]": 'Join[{Type -> "Continuous", Minimum -> min, Maximum -> max, Default -> min}, var]',
# "System`Private`ManipulateParameter[var_, {min_?RealNumberQ, max_?RealNumberQ, step_?RealNumberQ}]": 'Join[{Type -> "Discrete", Minimum -> min, Maximum -> max, Step -> step, Default -> min}, var]',
# "System`Private`ManipulateParameter[var_, {min_?RealValuedNumberQ, max_?RealValuedNumberQ}]": 'Join[{Type -> "Continuous", Minimum -> min, Maximum -> max, Default -> min}, var]',
# "System`Private`ManipulateParameter[var_, {min_?RealValuedNumberQ, max_?RealValuedNumberQ, step_?RealValuedNumberQ}]": 'Join[{Type -> "Discrete", Minimum -> min, Maximum -> max, Step -> step, Default -> min}, var]',
# "System`Private`ManipulateParameter[var_, {opt_List}] /; Length[opt] > 0": 'Join[{Type -> "Options", Options -> opt, Default -> Part[opt, 1]}, var]',
# }

Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/matrices/constrmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class DiamondMatrix(Builtin):
summary_text = "create a matrix with 1 in a diamond-shaped region, and 0 outside"

def eval(self, r, evaluation: Evaluation):
"DiamondMatrix[r_?RealNumberQ]"
"DiamondMatrix[r_?RealValuedNumberQ]"
py_r = abs(r.round_to_float())
t = int(math.floor(0.5 + py_r))

Expand Down Expand Up @@ -138,7 +138,7 @@ class DiskMatrix(Builtin):
summary_text = "create a matrix with 1 in a disk-shaped region, and 0 outside"

def eval(self, r, evaluation: Evaluation):
"DiskMatrix[r_?RealNumberQ]"
"DiskMatrix[r_?RealValuedNumberQ]"
py_r = abs(r.round_to_float())
s = int(math.floor(0.5 + py_r))

Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/numbers/calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,7 @@ class Solve(Builtin):
rules = {
"Solve[eqs_, vars_, Complexes]": "Solve[eqs, vars]",
"Solve[eqs_, vars_, Reals]": (
"Cases[Solve[eqs, vars], {Rule[x_,y_?RealNumberQ]}]"
"Cases[Solve[eqs, vars], {Rule[x_,y_?RealValuedNumberQ]}]"
),
"Solve[eqs_, vars_, Integers]": (
"Cases[Solve[eqs, vars], {Rule[x_,y_Integer]}]"
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/numbers/trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ class ArcTan(MPMathFunction):
"ArcTan[Undefined]": "Undefined",
"ArcTan[Undefined, x_]": "Undefined",
"ArcTan[y_, Undefined]": "Undefined",
"ArcTan[x_?RealNumberQ, y_?RealNumberQ]": """If[x == 0, If[y == 0, 0, If[y > 0, Pi/2, -Pi/2]], If[x > 0,
"ArcTan[x_?RealValuedNumberQ, y_?RealValuedNumberQ]": """If[x == 0, If[y == 0, 0, If[y > 0, Pi/2, -Pi/2]], If[x > 0,
ArcTan[y/x], If[y >= 0, ArcTan[y/x] + Pi, ArcTan[y/x] - Pi]]]""",
"Derivative[1][ArcTan]": "1/(1+#^2)&",
}
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ class Round(Builtin):

rules = {
"Round[expr_?NumericQ]": "Round[Re[expr], 1] + I * Round[Im[expr], 1]",
"Round[expr_Complex, k_?RealNumberQ]": (
"Round[expr_Complex, k_?RealValuedNumberQ]": (
"Round[Re[expr], k] + I * Round[Im[expr], k]"
),
}
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def init(
"System`StringQ": self.match_string,
"System`NumericQ": self.match_numericq,
"System`NumberQ": self.match_numberq,
"System`RealNumberQ": self.match_real_numberq,
"System`RealValuedNumberQ": self.match_real_numberq,
"Internal`RealValuedNumberQ": self.match_real_numberq,
"System`Posive": self.match_positive,
"System`Negative": self.match_negative,
Expand Down
Loading