Skip to content

Commit c6e0e3d

Browse files
authored
Merge pull request #712 from Mathics3/apply-reduction
apply->eval reduction and style stuff
2 parents 5a3e692 + 773be64 commit c6e0e3d

File tree

6 files changed

+155
-107
lines changed

6 files changed

+155
-107
lines changed

mathics/builtin/colors/color_directives.py

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""
22
Color Directives
33
4-
There are many different way to specify color; we support all of the color formats below and will convert between the different color formats.
4+
There are many different way to specify color, and we support many of these.
5+
6+
We can convert between the different color formats.
57
"""
68

79
from math import atan2, cos, exp, pi, radians, sin, sqrt
@@ -119,6 +121,24 @@ def _euclidean_distance(a, b):
119121
return sqrt(sum((x1 - x2) * (x1 - x2) for x1, x2 in zip(a, b)))
120122

121123

124+
def color_to_expression(components, colorspace):
125+
if colorspace == "Grayscale":
126+
converted_color_name = "GrayLevel"
127+
elif colorspace == "HSB":
128+
converted_color_name = "Hue"
129+
else:
130+
converted_color_name = colorspace + "Color"
131+
132+
return to_expression(converted_color_name, *components)
133+
134+
135+
def expression_to_color(color):
136+
try:
137+
return _ColorObject.create(color)
138+
except ColorError:
139+
return None
140+
141+
122142
class _ColorObject(_GraphicsDirective, ImmutableValueMixin):
123143
formats = {
124144
# we are adding ImageSizeMultipliers in the rule below, because we do _not_ want color boxes to
@@ -306,7 +326,7 @@ class ColorDistance(Builtin):
306326
/ 100,
307327
}
308328

309-
def apply(self, c1, c2, evaluation, options):
329+
def eval(self, c1, c2, evaluation, options):
310330
"ColorDistance[c1_, c2_, OptionsPattern[ColorDistance]]"
311331

312332
distance_function = options.get("System`DistanceFunction")
@@ -431,7 +451,9 @@ class ColorError(BoxExpressionError):
431451

432452
class GrayLevel(_ColorObject):
433453
"""
434-
<url>:WMA link:https://reference.wolfram.com/language/ref/GrayLevel.html</url>
454+
<url>
455+
:WMA link:
456+
https://reference.wolfram.com/language/ref/GrayLevel.html</url>
435457
436458
<dl>
437459
<dt>'GrayLevel[$g$]'
@@ -449,7 +471,9 @@ class GrayLevel(_ColorObject):
449471

450472
class Hue(_ColorObject):
451473
"""
452-
<url>:WMA link:https://reference.wolfram.com/language/ref/Hue.html</url>
474+
<url>
475+
:WMA link:
476+
https://reference.wolfram.com/language/ref/Hue.html</url>
453477
454478
<dl>
455479
<dt>'Hue[$h$, $s$, $l$, $a$]'
@@ -509,7 +533,9 @@ def trans(t):
509533

510534
class LABColor(_ColorObject):
511535
"""
512-
<url>:WMA link:https://reference.wolfram.com/language/ref/LABColor.html</url>
536+
<url>
537+
:WMA link:
538+
https://reference.wolfram.com/language/ref/LABColor.html</url>
513539
514540
<dl>
515541
<dt>'LABColor[$l$, $a$, $b$]'
@@ -525,7 +551,9 @@ class LABColor(_ColorObject):
525551

526552
class LCHColor(_ColorObject):
527553
"""
528-
<url>:WMA link:https://reference.wolfram.com/language/ref/LCHColor.html</url>
554+
<url>
555+
:WMA link:
556+
https://reference.wolfram.com/language/ref/LCHColor.html</url>
529557
530558
<dl>
531559
<dt>'LCHColor[$l$, $c$, $h$]'
@@ -556,7 +584,9 @@ class LUVColor(_ColorObject):
556584

557585
class Opacity(_GraphicsDirective):
558586
"""
559-
<url>:WMA link:https://reference.wolfram.com/language/ref/Opacity.html</url>
587+
<url>
588+
:WMA link:
589+
https://reference.wolfram.com/language/ref/Opacity.html</url>
560590
561591
<dl>
562592
<dt>'Opacity[$level$]'
@@ -592,7 +622,9 @@ def create_as_style(klass, graphics, item):
592622

593623
class RGBColor(_ColorObject):
594624
"""
595-
<url>:WMA link:https://reference.wolfram.com/language/ref/RGBColor.html</url>
625+
<url>
626+
:WMA link:
627+
https://reference.wolfram.com/language/ref/RGBColor.html</url>
596628
597629
<dl>
598630
<dt>'RGBColor[$r$, $g$, $b$]'
@@ -620,7 +652,9 @@ def to_rgba(self):
620652

621653
class XYZColor(_ColorObject):
622654
"""
623-
<url>:WMA link:https://reference.wolfram.com/language/ref/XYZColor.html</url>
655+
<url>
656+
:WMA link:
657+
https://reference.wolfram.com/language/ref/XYZColor.html</url>
624658
625659
<dl>
626660
<dt>'XYZColor[$x$, $y$, $z$]'
@@ -631,21 +665,3 @@ class XYZColor(_ColorObject):
631665
color_space = "XYZ"
632666
components_sizes = [3, 4]
633667
default_components = [0, 0, 0, 1]
634-
635-
636-
def expression_to_color(color):
637-
try:
638-
return _ColorObject.create(color)
639-
except ColorError:
640-
return None
641-
642-
643-
def color_to_expression(components, colorspace):
644-
if colorspace == "Grayscale":
645-
converted_color_name = "GrayLevel"
646-
elif colorspace == "HSB":
647-
converted_color_name = "Hue"
648-
else:
649-
converted_color_name = colorspace + "Color"
650-
651-
return to_expression(converted_color_name, *components)

mathics/builtin/compilation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
44
Code compilation allows Mathics functions to be run faster.
55
6-
When LLVM and Python libraries are available, compilation produces LLVM code.
6+
When LLVM and Python libraries are available, compilation \
7+
produces LLVM code.
78
"""
89

910
# This tells documentation how to sort this module
@@ -25,6 +26,7 @@
2526
)
2627
from mathics.core.convert.python import from_python
2728
from mathics.core.element import ImmutableValueMixin
29+
from mathics.core.evaluation import Evaluation
2830
from mathics.core.expression import Expression, SymbolCompiledFunction
2931
from mathics.core.symbols import Atom, Symbol, SymbolFalse, SymbolTrue
3032

@@ -98,7 +100,7 @@ class Compile(Builtin):
98100
requires = ("llvmlite",)
99101
summary_text = "compile an expression"
100102

101-
def apply(self, vars, expr, evaluation):
103+
def eval(self, vars, expr, evaluation: Evaluation):
102104
"Compile[vars_, expr_]"
103105

104106
if not vars.has_form("List", None):
@@ -174,7 +176,7 @@ def to_sympy(self, *args, **kwargs):
174176
def __hash__(self):
175177
return hash(("CompiledCode", ctypes.addressof(self.cfunc))) # XXX hack
176178

177-
def atom_to_boxes(self, f, evaluation):
179+
def atom_to_boxes(self, f, evaluation: Evaluation):
178180
return CompiledCodeBox(String(self.__str__()), evaluation=evaluation)
179181

180182

@@ -199,7 +201,7 @@ class CompiledFunction(Builtin):
199201
messages = {"argerr": "Invalid argument `1` should be Integer, Real or boolean."}
200202
summary_text = "A CompiledFunction object."
201203

202-
def apply(self, argnames, expr, code, args, evaluation):
204+
def eval(self, argnames, expr, code, args, evaluation: Evaluation):
203205
"CompiledFunction[argnames_, expr_, code_CompiledCode][args__]"
204206

205207
argseq = args.get_sequence()

mathics/builtin/distance/numeric.py

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from mathics.builtin.base import Builtin
66
from mathics.core.atoms import Integer1, Integer2
7-
from mathics.core.expression import Expression
7+
from mathics.core.expression import Evaluation, Expression
88
from mathics.core.symbols import (
99
SymbolAbs,
1010
SymbolDivide,
@@ -21,7 +21,7 @@
2121
)
2222

2323

24-
def _norm_calc(head, u, v, evaluation):
24+
def _norm_calc(head, u, v, evaluation: Evaluation):
2525
expr = Expression(head, u, v)
2626
old_quiet_all = evaluation.quiet_all
2727
try:
@@ -38,8 +38,11 @@ def _norm_calc(head, u, v, evaluation):
3838

3939
class BrayCurtisDistance(Builtin):
4040
"""
41-
<url>:Bray-Curtis Dissimilarity:https://en.wikipedia.org/wiki/Bray%E2%80%93Curtis_dissimilarity</url> \
42-
(<url>:WMA link:https://reference.wolfram.com/language/ref/BrayCurtisDistance.html</url>)
41+
<url>
42+
:Bray-Curtis Dissimilarity:
43+
https://en.wikipedia.org/wiki/Bray%E2%80%93Curtis_dissimilarity</url> \
44+
(<url>:WMA:
45+
https://reference.wolfram.com/language/ref/BrayCurtisDistance.html</url>)
4346
4447
<dl>
4548
<dt>'BrayCurtisDistance[$u$, $v$]'
@@ -56,7 +59,7 @@ class BrayCurtisDistance(Builtin):
5659

5760
summary_text = "Bray-Curtis distance"
5861

59-
def apply(self, u, v, evaluation):
62+
def eval(self, u, v, evaluation: Evaluation):
6063
"BrayCurtisDistance[u_, v_]"
6164
t = _norm_calc(SymbolSubtract, u, v, evaluation)
6265
if t is not None:
@@ -71,8 +74,12 @@ def apply(self, u, v, evaluation):
7174

7275
class CanberraDistance(Builtin):
7376
"""
74-
<url>:Canberra distance:https://en.wikipedia.org/wiki/Canberra_distance</url> \
75-
(<url>:WMA link:https://reference.wolfram.com/language/ref/CanberraDistance.html</url>)
77+
<url>
78+
:Canberra distance:
79+
https://en.wikipedia.org/wiki/Canberra_distance</url> \
80+
(<url>
81+
:WMA:
82+
https://reference.wolfram.com/language/ref/CanberraDistance.html</url>)
7683
7784
<dl>
7885
<dt>'CanberraDistance[$u$, $v$]'
@@ -88,7 +95,7 @@ class CanberraDistance(Builtin):
8895

8996
summary_text = "Canberra distance"
9097

91-
def apply(self, u, v, evaluation):
98+
def eval(self, u, v, evaluation: Evaluation):
9299
"CanberraDistance[u_, v_]"
93100
t = _norm_calc(SymbolSubtract, u, v, evaluation)
94101
if t is not None:
@@ -107,7 +114,9 @@ def apply(self, u, v, evaluation):
107114
class ChessboardDistance(Builtin):
108115
"""
109116
<url>:Chebyshev distance:https://en.wikipedia.org/wiki/Chebyshev_distance</url> \
110-
(<url>:WMA link:https://reference.wolfram.com/language/ref/ChessboardDistance.html</url>)
117+
(<url>
118+
:WMA:
119+
https://reference.wolfram.com/language/ref/ChessboardDistance.html</url>)
111120
112121
<dl>
113122
<dt>'ChessboardDistance[$u$, $v$]'
@@ -123,7 +132,7 @@ class ChessboardDistance(Builtin):
123132

124133
summary_text = "chessboard distance"
125134

126-
def apply(self, u, v, evaluation):
135+
def eval(self, u, v, evaluation: Evaluation):
127136
"ChessboardDistance[u_, v_]"
128137
t = _norm_calc(SymbolSubtract, u, v, evaluation)
129138
if t is not None:
@@ -132,8 +141,11 @@ def apply(self, u, v, evaluation):
132141

133142
class CosineDistance(Builtin):
134143
r"""
135-
<url>:Cosine similarity:https://en.wikipedia.org/wiki/Cosine_similarity</url> \
136-
(<url>:WMA link:https://reference.wolfram.com/language/ref/CosineDistance.html</url>)
144+
<url>
145+
:Cosine similarity:
146+
https://en.wikipedia.org/wiki/Cosine_similarity</url> \
147+
(<url>:WMA:
148+
https://reference.wolfram.com/language/ref/CosineDistance.html</url>)
137149
138150
<dl>
139151
<dt>'CosineDistance[$u$, $v$]'
@@ -152,7 +164,7 @@ class CosineDistance(Builtin):
152164

153165
summary_text = "cosine distance"
154166

155-
def apply(self, u, v, evaluation):
167+
def eval(self, u, v, evaluation: Evaluation):
156168
"CosineDistance[u_, v_]"
157169
dot = _norm_calc(SymbolDot, u, v, evaluation)
158170
if dot is not None:
@@ -173,8 +185,12 @@ def apply(self, u, v, evaluation):
173185

174186
class EuclideanDistance(Builtin):
175187
"""
176-
<url>:Euclidean similarity:https://en.wikipedia.org/wiki/Euclidean_distance</url> \
177-
(<url>:WMA link:https://reference.wolfram.com/language/ref/EuclideanDistance.html</url>)
188+
<url>
189+
:Euclidean similarity:
190+
https://en.wikipedia.org/wiki/Euclidean_distance</url> \
191+
(<url>
192+
:WMA:
193+
https://reference.wolfram.com/language/ref/EuclideanDistance.html</url>)
178194
179195
<dl>
180196
<dt>'EuclideanDistance[$u$, $v$]'
@@ -193,7 +209,7 @@ class EuclideanDistance(Builtin):
193209

194210
summary_text = "euclidean distance"
195211

196-
def apply(self, u, v, evaluation):
212+
def eval(self, u, v, evaluation: Evaluation):
197213
"EuclideanDistance[u_, v_]"
198214
t = _norm_calc(SymbolSubtract, u, v, evaluation)
199215
if t is not None:
@@ -202,8 +218,12 @@ def apply(self, u, v, evaluation):
202218

203219
class ManhattanDistance(Builtin):
204220
"""
205-
<url>:Manhattan distance:https://en.wikipedia.org/wiki/Taxicab_geometry</url> \
206-
(<url>:WMA link:https://reference.wolfram.com/language/ref/ManhattanDistance.html</url>)
221+
<url>
222+
:Manhattan distance:
223+
https://en.wikipedia.org/wiki/Taxicab_geometry</url> \
224+
(<url>
225+
:WMA:
226+
https://reference.wolfram.com/language/ref/ManhattanDistance.html</url>)
207227
208228
<dl>
209229
<dt>'ManhattanDistance[$u$, $v$]'
@@ -219,7 +239,7 @@ class ManhattanDistance(Builtin):
219239

220240
summary_text = "Manhattan distance"
221241

222-
def apply(self, u, v, evaluation):
242+
def eval(self, u, v, evaluation: Evaluation):
223243
"ManhattanDistance[u_, v_]"
224244
t = _norm_calc(SymbolSubtract, u, v, evaluation)
225245
if t is not None:
@@ -228,7 +248,9 @@ def apply(self, u, v, evaluation):
228248

229249
class SquaredEuclideanDistance(Builtin):
230250
"""
231-
<url>:WMA link:https://reference.wolfram.com/language/ref/SquaredEuclideanDistance.html</url>
251+
<url>
252+
:WMA link:
253+
https://reference.wolfram.com/language/ref/SquaredEuclideanDistance.html</url>
232254
233255
<dl>
234256
<dt>'SquaredEuclideanDistance[$u$, $v$]'
@@ -244,7 +266,7 @@ class SquaredEuclideanDistance(Builtin):
244266

245267
summary_text = "square of the euclidean distance"
246268

247-
def apply(self, u, v, evaluation):
269+
def eval(self, u, v, evaluation: Evaluation):
248270
"SquaredEuclideanDistance[u_, v_]"
249271
t = _norm_calc(SymbolSubtract, u, v, evaluation)
250272
if t is not None:

0 commit comments

Comments
 (0)