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

making llvmlite to be optional #1101

Merged
merged 1 commit into from
Sep 28, 2024
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
9 changes: 4 additions & 5 deletions mathics/builtin/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ class Compile(Builtin):


>> cf = Compile[{x, y}, x + 2 y]
= CompiledFunction[{x, y}, x + 2 y, -CompiledCode-]
= CompiledFunction[{x, y}, x + 2 y, ...]
>> cf[2.5, 4.3]
= 11.1

>> cf = Compile[{{x, _Real}}, Sin[x]]
= CompiledFunction[{x}, Sin[x], -CompiledCode-]
= CompiledFunction[{x}, Sin[x], ...]
>> cf[1.4]
= 0.98545

Compile supports basic flow control:
>> cf = Compile[{{x, _Real}, {y, _Integer}}, If[x == 0.0 && y <= 0, 0.0, Sin[x ^ y] + 1 / Min[x, 0.5]] + 0.5]
= CompiledFunction[{x, y}, ..., -CompiledCode-]
= CompiledFunction[{x, y}, ...]
>> cf[3.5, 2]
= 2.18888

Expand All @@ -78,7 +78,6 @@ class Compile(Builtin):
"fdup": "Duplicate parameter `1` found in `2`.",
}

requires = ("llvmlite",)
summary_text = "compile an expression"

def eval(self, vars, expr, evaluation: Evaluation):
Expand Down Expand Up @@ -172,7 +171,7 @@ class CompiledFunction(Builtin):
</dl>

>> sqr = Compile[{x}, x x]
= CompiledFunction[{x}, x ^ 2, -CompiledCode-]
= CompiledFunction[{x}, x ^ 2, ...]
>> Head[sqr]
= CompiledFunction
>> sqr[2]
Expand Down
5 changes: 3 additions & 2 deletions mathics/compile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
has_llvmlite = False


from .base import CompileArg, CompileError
from .types import *

if has_llvmlite:
from .base import CompileArg, CompileError
from .compile import _compile
from .ir import IRGenerator
from .types import *
16 changes: 11 additions & 5 deletions mathics/compile/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from llvmlite import ir
try:
from llvmlite import ir

int_type = ir.IntType(64)
real_type = ir.DoubleType()
bool_type = ir.IntType(1)
void_type = ir.VoidType()
int_type = ir.IntType(64)
real_type = ir.DoubleType()
bool_type = ir.IntType(1)
void_type = ir.VoidType()
except:
int_type = int
real_type = float
bool_type = bool
void_type = type(None)
4 changes: 4 additions & 0 deletions mathics/core/convert/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
}
except ImportError:
use_llvm = False
bool_type = bool
int_type = int
real_type = float

permitted_types = {
Expression(SymbolBlank, SymbolInteger): int,
Expression(SymbolBlank, SymbolReal): float,
Expand Down
1 change: 1 addition & 0 deletions mathics/eval/distance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Distance-related evaluation functions and exception classes
"""

from mathics.core.atoms import Integer, Real


Expand Down
3 changes: 3 additions & 0 deletions mathics/eval/drawing/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ def quiet_f(*args):
expr: Optional[Type[BaseElement]] = Expression(SymbolN, expr).evaluate(evaluation)

def quiet_f(*args):
old_quiet_all = evaluation.quiet_all
evaluation.quiet_all = True
vars = {arg_name: Real(arg) for arg_name, arg in zip(arg_names, args)}
value = dynamic_scoping(expr.evaluate, vars, evaluation)
evaluation.quiet_all = old_quiet_all
if list_is_expected:
if value.has_form("List", None):
value = [extract_pyreal(item) for item in value.elements]
Expand Down
12 changes: 7 additions & 5 deletions mathics/eval/files_io/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,14 @@ def read_list_from_types(read_types):
# TODO: look for a better implementation handling "Hold[Expression]".
#
read_types = (
SymbolHoldExpression
if (
typ.get_head_name() == "System`Hold"
and typ.elements[0].get_name() == "System`Expression"
(
SymbolHoldExpression
if (
typ.get_head_name() == "System`Hold"
and typ.elements[0].get_name() == "System`Expression"
)
else typ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reformatting may conflict with another PR, but we'll merge this first and I'll deal with the other later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mmatera!

)
else typ
for typ in read_types
)

Expand Down
1 change: 1 addition & 0 deletions mathics/eval/hyperbolic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Mathics3 builtins from mathics.core.numbers.hyperbolic
"""

from sympy import Symbol as SympySymbol

from mathics.core.convert.sympy import from_sympy
Expand Down
1 change: 1 addition & 0 deletions mathics/eval/strings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
String-related evaluation functions.
"""

from mathics.core.atoms import String
from mathics.core.element import BaseElement
from mathics.core.evaluation import Evaluation
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
description = "A general-purpose computer algebra system."
dependencies = [
"Mathics-Scanner >= 1.3.0",
"llvmlite",
# "llvmlite",
"mpmath>=1.2.0",
"numpy<1.27",
"palettable",
Expand Down
1 change: 1 addition & 0 deletions requirements-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pyocr # Used for TextRecognize
scikit-image >= 0.17 # FindMinimum can use this; used by Image as well
unidecode # Used in Transliterate
wordcloud >= 1.9.3 # Used in builtin/image.py by WordCloud(). Previous versions assume "image.textsize" which no longer exists
llvmlite # Used for llvm compiling
2 changes: 1 addition & 1 deletion test/builtin/atomic/test_strings2.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_string_split():
(
'StringSplit["This is a sentence, which goes on.", Except[WordCharacter] ..]',
"{This, is, a, sentence, which, goes, on}",
)
),
# # FIXME: these forms are not implemented yet:
# ('StringSplit["11a22b3", _?LetterQ]', '{11, 22, 3}'),
# ('StringSplit["a b::c d::e f g", "::" -> "--"]'), '{a, b, --, c d, --, e f g}'),
Expand Down
6 changes: 6 additions & 0 deletions test/builtin/test_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

import pytest

from mathics.compile import has_llvmlite


@pytest.mark.skipif(
not has_llvmlite,
reason="requires llvmlite",
)
@pytest.mark.parametrize(
("str_expr", "msgs", "str_expected", "fail_msg"),
[
Expand Down
8 changes: 8 additions & 0 deletions test/builtin/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
)


@pytest.mark.skipif(
not has_llvmlite,
reason="requires llvmlite",
)
def test_compile_code():
for str_expr, x, res in [
("Sin[x]", 1.5, 0.997495),
Expand All @@ -60,6 +64,10 @@ def test_compile_code():
assert abs(y - res) < 1.0e-6


@pytest.mark.skipif(
not has_llvmlite,
reason="requires llvmlite",
)
def test_builtin_fns_with_symbols_1():
for str_expr, x, res in [
("Sin[x]", 1.5, 0.997495),
Expand Down
1 change: 1 addition & 0 deletions test/consistency-and-style/test_duplicate_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
In the past when reorganizing builtin functions we sometimes
had missing or duplicate built-in functions definitions.
"""

import os

import pytest
Expand Down
1 change: 1 addition & 0 deletions test/doc/test_common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Pytests for the documentation system. Basic functions and classes.
"""

import os.path as osp

from mathics.core.evaluation import Message, Print
Expand Down
1 change: 1 addition & 0 deletions test/doc/test_doctests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Pytests for the documentation system. Basic functions and classes.
"""

import os.path as osp

from mathics.core.evaluation import Message, Print
Expand Down
1 change: 1 addition & 0 deletions test/doc/test_latex.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Pytests for the documentation system. Basic functions and classes.
"""

import os.path as osp

from mathics.core.evaluation import Message, Print
Expand Down