Skip to content
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
70 changes: 43 additions & 27 deletions mathics/builtin/colors/color_directives.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
Color Directives

There are many different way to specify color; we support all of the color formats below and will convert between the different color formats.
There are many different way to specify color, and we support many of these.

We can convert between the different color formats.
"""

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


def color_to_expression(components, colorspace):
if colorspace == "Grayscale":
converted_color_name = "GrayLevel"
elif colorspace == "HSB":
converted_color_name = "Hue"
else:
converted_color_name = colorspace + "Color"

return to_expression(converted_color_name, *components)


def expression_to_color(color):
try:
return _ColorObject.create(color)
except ColorError:
return None


class _ColorObject(_GraphicsDirective, ImmutableValueMixin):
formats = {
# we are adding ImageSizeMultipliers in the rule below, because we do _not_ want color boxes to
Expand Down Expand Up @@ -306,7 +326,7 @@ class ColorDistance(Builtin):
/ 100,
}

def apply(self, c1, c2, evaluation, options):
def eval(self, c1, c2, evaluation, options):
"ColorDistance[c1_, c2_, OptionsPattern[ColorDistance]]"

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

class GrayLevel(_ColorObject):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/GrayLevel.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/GrayLevel.html</url>

<dl>
<dt>'GrayLevel[$g$]'
Expand All @@ -449,7 +471,9 @@ class GrayLevel(_ColorObject):

class Hue(_ColorObject):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Hue.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Hue.html</url>

<dl>
<dt>'Hue[$h$, $s$, $l$, $a$]'
Expand Down Expand Up @@ -509,7 +533,9 @@ def trans(t):

class LABColor(_ColorObject):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/LABColor.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/LABColor.html</url>

<dl>
<dt>'LABColor[$l$, $a$, $b$]'
Expand All @@ -525,7 +551,9 @@ class LABColor(_ColorObject):

class LCHColor(_ColorObject):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/LCHColor.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/LCHColor.html</url>

<dl>
<dt>'LCHColor[$l$, $c$, $h$]'
Expand Down Expand Up @@ -556,7 +584,9 @@ class LUVColor(_ColorObject):

class Opacity(_GraphicsDirective):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Opacity.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Opacity.html</url>

<dl>
<dt>'Opacity[$level$]'
Expand Down Expand Up @@ -592,7 +622,9 @@ def create_as_style(klass, graphics, item):

class RGBColor(_ColorObject):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/RGBColor.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/RGBColor.html</url>

<dl>
<dt>'RGBColor[$r$, $g$, $b$]'
Expand Down Expand Up @@ -620,7 +652,9 @@ def to_rgba(self):

class XYZColor(_ColorObject):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/XYZColor.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/XYZColor.html</url>

<dl>
<dt>'XYZColor[$x$, $y$, $z$]'
Expand All @@ -631,21 +665,3 @@ class XYZColor(_ColorObject):
color_space = "XYZ"
components_sizes = [3, 4]
default_components = [0, 0, 0, 1]


def expression_to_color(color):
try:
return _ColorObject.create(color)
except ColorError:
return None


def color_to_expression(components, colorspace):
if colorspace == "Grayscale":
converted_color_name = "GrayLevel"
elif colorspace == "HSB":
converted_color_name = "Hue"
else:
converted_color_name = colorspace + "Color"

return to_expression(converted_color_name, *components)
10 changes: 6 additions & 4 deletions mathics/builtin/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

Code compilation allows Mathics functions to be run faster.

When LLVM and Python libraries are available, compilation produces LLVM code.
When LLVM and Python libraries are available, compilation \
produces LLVM code.
"""

# This tells documentation how to sort this module
Expand All @@ -25,6 +26,7 @@
)
from mathics.core.convert.python import from_python
from mathics.core.element import ImmutableValueMixin
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression, SymbolCompiledFunction
from mathics.core.symbols import Atom, Symbol, SymbolFalse, SymbolTrue

Expand Down Expand Up @@ -98,7 +100,7 @@ class Compile(Builtin):
requires = ("llvmlite",)
summary_text = "compile an expression"

def apply(self, vars, expr, evaluation):
def eval(self, vars, expr, evaluation: Evaluation):
"Compile[vars_, expr_]"

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

def atom_to_boxes(self, f, evaluation):
def atom_to_boxes(self, f, evaluation: Evaluation):
return CompiledCodeBox(String(self.__str__()), evaluation=evaluation)


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

def apply(self, argnames, expr, code, args, evaluation):
def eval(self, argnames, expr, code, args, evaluation: Evaluation):
"CompiledFunction[argnames_, expr_, code_CompiledCode][args__]"

argseq = args.get_sequence()
Expand Down
64 changes: 43 additions & 21 deletions mathics/builtin/distance/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from mathics.builtin.base import Builtin
from mathics.core.atoms import Integer1, Integer2
from mathics.core.expression import Expression
from mathics.core.expression import Evaluation, Expression
from mathics.core.symbols import (
SymbolAbs,
SymbolDivide,
Expand All @@ -21,7 +21,7 @@
)


def _norm_calc(head, u, v, evaluation):
def _norm_calc(head, u, v, evaluation: Evaluation):
expr = Expression(head, u, v)
old_quiet_all = evaluation.quiet_all
try:
Expand All @@ -38,8 +38,11 @@ def _norm_calc(head, u, v, evaluation):

class BrayCurtisDistance(Builtin):
"""
<url>:Bray-Curtis Dissimilarity:https://en.wikipedia.org/wiki/Bray%E2%80%93Curtis_dissimilarity</url> \
(<url>:WMA link:https://reference.wolfram.com/language/ref/BrayCurtisDistance.html</url>)
<url>
:Bray-Curtis Dissimilarity:
https://en.wikipedia.org/wiki/Bray%E2%80%93Curtis_dissimilarity</url> \
(<url>:WMA:
https://reference.wolfram.com/language/ref/BrayCurtisDistance.html</url>)

<dl>
<dt>'BrayCurtisDistance[$u$, $v$]'
Expand All @@ -56,7 +59,7 @@ class BrayCurtisDistance(Builtin):

summary_text = "Bray-Curtis distance"

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

class CanberraDistance(Builtin):
"""
<url>:Canberra distance:https://en.wikipedia.org/wiki/Canberra_distance</url> \
(<url>:WMA link:https://reference.wolfram.com/language/ref/CanberraDistance.html</url>)
<url>
:Canberra distance:
https://en.wikipedia.org/wiki/Canberra_distance</url> \
(<url>
:WMA:
https://reference.wolfram.com/language/ref/CanberraDistance.html</url>)

<dl>
<dt>'CanberraDistance[$u$, $v$]'
Expand All @@ -88,7 +95,7 @@ class CanberraDistance(Builtin):

summary_text = "Canberra distance"

def apply(self, u, v, evaluation):
def eval(self, u, v, evaluation: Evaluation):
"CanberraDistance[u_, v_]"
t = _norm_calc(SymbolSubtract, u, v, evaluation)
if t is not None:
Expand All @@ -107,7 +114,9 @@ def apply(self, u, v, evaluation):
class ChessboardDistance(Builtin):
"""
<url>:Chebyshev distance:https://en.wikipedia.org/wiki/Chebyshev_distance</url> \
(<url>:WMA link:https://reference.wolfram.com/language/ref/ChessboardDistance.html</url>)
(<url>
:WMA:
https://reference.wolfram.com/language/ref/ChessboardDistance.html</url>)

<dl>
<dt>'ChessboardDistance[$u$, $v$]'
Expand All @@ -123,7 +132,7 @@ class ChessboardDistance(Builtin):

summary_text = "chessboard distance"

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

class CosineDistance(Builtin):
r"""
<url>:Cosine similarity:https://en.wikipedia.org/wiki/Cosine_similarity</url> \
(<url>:WMA link:https://reference.wolfram.com/language/ref/CosineDistance.html</url>)
<url>
:Cosine similarity:
https://en.wikipedia.org/wiki/Cosine_similarity</url> \
(<url>:WMA:
https://reference.wolfram.com/language/ref/CosineDistance.html</url>)

<dl>
<dt>'CosineDistance[$u$, $v$]'
Expand All @@ -152,7 +164,7 @@ class CosineDistance(Builtin):

summary_text = "cosine distance"

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

class EuclideanDistance(Builtin):
"""
<url>:Euclidean similarity:https://en.wikipedia.org/wiki/Euclidean_distance</url> \
(<url>:WMA link:https://reference.wolfram.com/language/ref/EuclideanDistance.html</url>)
<url>
:Euclidean similarity:
https://en.wikipedia.org/wiki/Euclidean_distance</url> \
(<url>
:WMA:
https://reference.wolfram.com/language/ref/EuclideanDistance.html</url>)

<dl>
<dt>'EuclideanDistance[$u$, $v$]'
Expand All @@ -193,7 +209,7 @@ class EuclideanDistance(Builtin):

summary_text = "euclidean distance"

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

class ManhattanDistance(Builtin):
"""
<url>:Manhattan distance:https://en.wikipedia.org/wiki/Taxicab_geometry</url> \
(<url>:WMA link:https://reference.wolfram.com/language/ref/ManhattanDistance.html</url>)
<url>
:Manhattan distance:
https://en.wikipedia.org/wiki/Taxicab_geometry</url> \
(<url>
:WMA:
https://reference.wolfram.com/language/ref/ManhattanDistance.html</url>)

<dl>
<dt>'ManhattanDistance[$u$, $v$]'
Expand All @@ -219,7 +239,7 @@ class ManhattanDistance(Builtin):

summary_text = "Manhattan distance"

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

class SquaredEuclideanDistance(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/SquaredEuclideanDistance.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/SquaredEuclideanDistance.html</url>

<dl>
<dt>'SquaredEuclideanDistance[$u$, $v$]'
Expand All @@ -244,7 +266,7 @@ class SquaredEuclideanDistance(Builtin):

summary_text = "square of the euclidean distance"

def apply(self, u, v, evaluation):
def eval(self, u, v, evaluation: Evaluation):
"SquaredEuclideanDistance[u_, v_]"
t = _norm_calc(SymbolSubtract, u, v, evaluation)
if t is not None:
Expand Down
Loading