Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
944e3390
Commit
944e3390
authored
Jun 03, 2013
by
Peter Baratta
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for various math functions in calc.py.
parent
ed90ed9a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
124 additions
and
5 deletions
+124
-5
common/lib/calc/calc.py
+21
-1
common/lib/calc/calcfunctions.py
+99
-0
common/lib/calc/tests/test_calc.py
+4
-4
No files found.
common/lib/calc/calc.py
View file @
944e3390
...
...
@@ -12,6 +12,7 @@ import re
import
numpy
import
scipy.constants
import
calcfunctions
# have numpy raise errors on functions outside its domain
# See http://docs.scipy.org/doc/numpy/reference/generated/numpy.seterr.html
...
...
@@ -26,16 +27,35 @@ from pyparsing import (Word, nums, Literal,
DEFAULT_FUNCTIONS
=
{
'sin'
:
numpy
.
sin
,
'cos'
:
numpy
.
cos
,
'tan'
:
numpy
.
tan
,
'sec'
:
calcfunctions
.
sec
,
'csc'
:
calcfunctions
.
csc
,
'cot'
:
calcfunctions
.
cot
,
'sqrt'
:
numpy
.
sqrt
,
'log10'
:
numpy
.
log10
,
'log2'
:
numpy
.
log2
,
'ln'
:
numpy
.
log
,
'exp'
:
numpy
.
exp
,
'arccos'
:
numpy
.
arccos
,
'arcsin'
:
numpy
.
arcsin
,
'arctan'
:
numpy
.
arctan
,
'arcsec'
:
calcfunctions
.
arcsec
,
'arccsc'
:
calcfunctions
.
arccsc
,
'arccot'
:
calcfunctions
.
arccot
,
'abs'
:
numpy
.
abs
,
'fact'
:
math
.
factorial
,
'factorial'
:
math
.
factorial
'factorial'
:
math
.
factorial
,
'sinh'
:
numpy
.
sinh
,
'cosh'
:
numpy
.
cosh
,
'tanh'
:
numpy
.
tanh
,
'sech'
:
calcfunctions
.
sech
,
'csch'
:
calcfunctions
.
csch
,
'coth'
:
calcfunctions
.
coth
,
'arcsinh'
:
numpy
.
arcsinh
,
'arccosh'
:
numpy
.
arccosh
,
'arctanh'
:
numpy
.
arctanh
,
'arcsech'
:
calcfunctions
.
arcsech
,
'arccsch'
:
calcfunctions
.
arccsch
,
'arccoth'
:
calcfunctions
.
arccoth
}
DEFAULT_VARIABLES
=
{
'j'
:
numpy
.
complex
(
0
,
1
),
'e'
:
numpy
.
e
,
...
...
common/lib/calc/calcfunctions.py
0 → 100644
View file @
944e3390
"""
Provide the mathematical functions that numpy doesn't.
Specifically, the secant/cosecant/cotangents and their inverses and
hyperbolic counterparts
"""
import
numpy
# Normal Trig
def
sec
(
arg
):
"""
Secant
"""
return
1
/
numpy
.
cos
(
arg
)
def
csc
(
arg
):
"""
Cosecant
"""
return
1
/
numpy
.
sin
(
arg
)
def
cot
(
arg
):
"""
Cotangent
"""
return
1
/
numpy
.
tan
(
arg
)
# Inverse Trig
# http://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Relationships_among_the_inverse_trigonometric_functions
def
arcsec
(
val
):
"""
Inverse secant
"""
return
numpy
.
arccos
(
1.
/
val
)
def
arccsc
(
val
):
"""
Inverse cosecant
"""
return
numpy
.
arcsin
(
1.
/
val
)
def
arccot
(
val
):
"""
Inverse cotangent
"""
if
numpy
.
real
(
val
)
<
0
:
return
-
numpy
.
pi
/
2
-
numpy
.
arctan
(
val
)
else
:
return
numpy
.
pi
/
2
-
numpy
.
arctan
(
val
)
# Hyperbolic Trig
def
sech
(
arg
):
"""
Hyperbolic secant
"""
return
1
/
numpy
.
cosh
(
arg
)
def
csch
(
arg
):
"""
Hyperbolic cosecant
"""
return
1
/
numpy
.
sinh
(
arg
)
def
coth
(
arg
):
"""
Hyperbolic cotangent
"""
return
1
/
numpy
.
tanh
(
arg
)
# And their inverses
def
arcsech
(
val
):
"""
Inverse hyperbolic secant
"""
return
numpy
.
arccosh
(
1.
/
val
)
def
arccsch
(
val
):
"""
Inverse hyperbolic cosecant
"""
return
numpy
.
arcsinh
(
1.
/
val
)
def
arccoth
(
val
):
"""
Inverse hyperbolic cotangent
"""
return
numpy
.
arctanh
(
1.
/
val
)
common/lib/calc/tests/test_calc.py
View file @
944e3390
...
...
@@ -201,9 +201,9 @@ class EvaluatorTest(unittest.TestCase):
which are: sec, csc, cot, arcsec, arccsc, arccot
"""
angles
=
[
'-pi/4'
,
'pi/6'
,
'pi/5'
,
'5*pi/4'
,
'9*pi/4'
,
'1 + j'
]
sec_values
=
[
1.414
,
1.155
,
1.236
,
-
1.414
,
1.414
,
0.498
+
0.591
j
]
csc_values
=
[
-
1.414
,
2
,
1.701
,
-
1.414
,
1.414
,
0.622
-
0.304
j
]
cot_values
=
[
-
1
,
1.732
,
1.376
,
1
,
1
,
0.218
-
0.868
j
]
sec_values
=
[
1.414
,
1.155
,
1.236
,
-
1.414
,
1.414
,
0.498
+
0.591
j
]
csc_values
=
[
-
1.414
,
2
,
1.701
,
-
1.414
,
1.414
,
0.622
-
0.304
j
]
cot_values
=
[
-
1
,
1.732
,
1.376
,
1
,
1
,
0.218
-
0.868
j
]
self
.
assert_function_values
(
'sec'
,
angles
,
sec_values
)
self
.
assert_function_values
(
'csc'
,
angles
,
csc_values
)
...
...
@@ -272,7 +272,7 @@ class EvaluatorTest(unittest.TestCase):
which are of the form arc[X]h
"""
results
=
[
0
,
0.5
,
1
,
2
,
1
+
1
j
]
results
=
[
0
,
0.5
,
1
,
2
,
1
+
1
j
]
sinh_vals
=
[
'0'
,
'0.5211'
,
'1.1752'
,
'3.6269'
,
'0.635+1.2985*j'
]
self
.
assert_function_values
(
'arcsinh'
,
sinh_vals
,
results
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment