• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MilesCranmer / SymbolicRegression.jl / 11204590927

06 Oct 2024 07:29PM UTC coverage: 95.808% (+1.2%) from 94.617%
11204590927

Pull #326

github

web-flow
Merge e2b369ea7 into 8f67533b9
Pull Request #326: BREAKING: Change expression types to `DynamicExpressions.Expression` (from `DynamicExpressions.Node`)

466 of 482 new or added lines in 24 files covered. (96.68%)

1 existing line in 1 file now uncovered.

2651 of 2767 relevant lines covered (95.81%)

73863189.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.16
/src/Operators.jl
1
module OperatorsModule
2

3
using DynamicExpressions: DynamicExpressions as DE
4
using SpecialFunctions: SpecialFunctions
5
using DynamicQuantities: UnionAbstractQuantity
6
using SpecialFunctions: erf, erfc
7
using Base: @deprecate
8
using ..ProgramConstantsModule: DATA_TYPE
9
using ...UtilsModule: @ignore
10
#TODO - actually add these operators to the module!
11

12
# TODO: Should this be limited to AbstractFloat instead?
13
function gamma(x::T)::T where {T<:DATA_TYPE}
96,411✔
14
    out = SpecialFunctions.gamma(x)
99,012✔
15
    return isinf(out) ? T(NaN) : out
115,822✔
16
end
17
gamma(x) = SpecialFunctions.gamma(x)
×
18

19
atanh_clip(x) = atanh(mod(x + oneunit(x), oneunit(x) + oneunit(x)) - oneunit(x)) * one(x)
9,999✔
20
# == atanh((x + 1) % 2 - 1)
21

22
# Implicitly defined:
23
#binary: mod
24
#unary: exp, abs, log1p, sin, cos, tan, sinh, cosh, tanh, asin, acos, atan, asinh, acosh, atanh, erf, erfc, gamma, relu, round, floor, ceil, round, sign.
25

26
# Use some fast operators from https://github.com/JuliaLang/julia/blob/81597635c4ad1e8c2e1c5753fda4ec0e7397543f/base/fastmath.jl
27
# Define allowed operators. Any julia operator can also be used.
28
# TODO: Add all of these operators to the precompilation.
29
# TODO: Since simplification is done in DynamicExpressions.jl, are these names correct anymore?
30
function safe_pow(x::T, y::T)::T where {T<:Union{AbstractFloat,UnionAbstractQuantity}}
7,119,060✔
31
    if isinteger(y)
7,923,044✔
32
        y < zero(y) && iszero(x) && return T(NaN)
310,454✔
33
    else
34
        y > zero(y) && x < zero(x) && return T(NaN)
7,668,370✔
35
        y < zero(y) && x <= zero(x) && return T(NaN)
7,000,474✔
36
    end
37
    return x^y
6,629,324✔
38
end
39
function safe_log(x::T)::T where {T<:AbstractFloat}
2,212,829✔
40
    x <= zero(x) && return T(NaN)
2,399,647✔
41
    return log(x)
2,279,371✔
42
end
43
function safe_log2(x::T)::T where {T<:AbstractFloat}
23,156✔
44
    x <= zero(x) && return T(NaN)
26,214✔
45
    return log2(x)
24,966✔
46
end
47
function safe_log10(x::T)::T where {T<:AbstractFloat}
23,156✔
48
    x <= zero(x) && return T(NaN)
26,214✔
49
    return log10(x)
24,966✔
50
end
51
function safe_log1p(x::T)::T where {T<:AbstractFloat}
3,828✔
52
    x <= -oneunit(x) && return T(NaN)
4,584✔
53
    return log1p(x)
3,384✔
54
end
55
function safe_acosh(x::T)::T where {T<:AbstractFloat}
21,833✔
56
    x < oneunit(x) && return T(NaN)
26,190✔
57
    return acosh(x)
22,806✔
58
end
59
function safe_sqrt(x::T)::T where {T<:AbstractFloat}
125,248,225✔
60
    x < zero(x) && return T(NaN)
155,982,835✔
61
    return sqrt(x)
142,018,714✔
62
end
63
# TODO: Should the above be made more generic, for, e.g., compatibility with units?
64

65
# Do not change the names of these operators, as
66
# they have special use in simplifications and printing.
67
square(x) = x * x
9,122✔
68
cube(x) = x * x * x
8,520✔
69
plus(x, y) = x + y
36✔
70
sub(x, y) = x - y
33,690✔
71
mult(x, y) = x * y
36✔
72
# Generics (for SIMD)
73
safe_pow(x, y) = x^y
884,487✔
74
safe_log(x) = log(x)
81,342✔
75
safe_log2(x) = log2(x)
81,342✔
76
safe_log10(x) = log10(x)
81,342✔
77
safe_log1p(x) = log1p(x)
3,942✔
78
safe_acosh(x) = acosh(x)
79,182✔
79
safe_sqrt(x) = sqrt(x)
2,909,014✔
80

81
function neg(x)
7,089✔
82
    return -x
8,502✔
83
end
84
function greater(x, y)
209,904✔
85
    return (x > y) * one(x)
240,107✔
86
end
87
function cond(x, y)
209,911✔
88
    return (x > zero(x)) * y
240,114✔
89
end
90
function relu(x)
682,202✔
91
    return (x > zero(x)) * x
768,992✔
92
end
93
function logical_or(x, y)
209,893✔
94
    return ((x > zero(x)) | (y > zero(y))) * one(x)
240,096✔
95
end
96
function logical_and(x, y)
209,881✔
97
    return ((x > zero(x)) & (y > zero(y))) * one(x)
240,084✔
98
end
99

100
# Strings
101
DE.get_op_name(::typeof(safe_pow)) = "^"
91✔
102
DE.get_op_name(::typeof(safe_log)) = "log"
14✔
103
DE.get_op_name(::typeof(safe_log2)) = "log2"
14✔
104
DE.get_op_name(::typeof(safe_log10)) = "log10"
14✔
105
DE.get_op_name(::typeof(safe_log1p)) = "log1p"
7✔
106
DE.get_op_name(::typeof(safe_acosh)) = "acosh"
14✔
107
DE.get_op_name(::typeof(safe_sqrt)) = "sqrt"
1,665✔
108

109
# Deprecated operations:
110
@deprecate pow(x, y) safe_pow(x, y)
111
@deprecate pow_abs(x, y) safe_pow(x, y)
112

113
# For static analysis tools:
NEW
114
@ignore pow(x, y) = safe_pow(x, y)
×
NEW
115
@ignore pow_abs(x, y) = safe_pow(x, y)
×
116

117
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc