Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/assumed_shape/precision.f90 +4 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/accesstype.f90 +13 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/data_common.f +8 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/data_multiplier.f +5 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh15035.f +16 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh17859.f +12 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh22648.pyf +7 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23533.f +5 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23598.f90 +4 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23879.f90 +20 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh2848.f90 +13 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/operators.f90 +49 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/privatemod.f90 +11 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/publicmod.f90 +10 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/unicode_comment.f90 +4 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/regression/gh25337/data.f90 +8 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/regression/gh25337/use_data.f90 +6 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/regression/inout.f90 +9 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/return_integer/foo77.f +56 -0
- llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/return_integer/foo90.f90 +59 -0
- parrot/lib/python3.10/site-packages/sympy/functions/elementary/complexes.py +1473 -0
- parrot/lib/python3.10/site-packages/sympy/functions/elementary/hyperbolic.py +2285 -0
- parrot/lib/python3.10/site-packages/sympy/functions/elementary/piecewise.py +1516 -0
- parrot/lib/python3.10/site-packages/sympy/functions/elementary/tests/test_hyperbolic.py +1542 -0
- parrot/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/bench_matrix.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/funcmatrix.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/slice.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/testing/__pycache__/quality_unicode.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/testing/pytest.py +401 -0
- parrot/lib/python3.10/site-packages/sympy/testing/randtest.py +19 -0
- parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/diagnose_imports.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/test_code_quality.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/test_deprecated.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/test_pytest.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/test_runtests_pytest.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/testing/tests/test_module_imports.py +42 -0
- parrot/lib/python3.10/site-packages/sympy/testing/tests/test_pytest.py +211 -0
- parrot/lib/python3.10/site-packages/sympy/testing/tests/test_runtests_pytest.py +178 -0
- parrot/lib/python3.10/site-packages/sympy/unify/__init__.py +15 -0
- parrot/lib/python3.10/site-packages/sympy/unify/__pycache__/core.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/unify/core.py +234 -0
- parrot/lib/python3.10/site-packages/sympy/unify/rewrite.py +55 -0
- parrot/lib/python3.10/site-packages/sympy/unify/tests/__init__.py +0 -0
- parrot/lib/python3.10/site-packages/sympy/unify/tests/test_rewrite.py +74 -0
- parrot/lib/python3.10/site-packages/sympy/unify/tests/test_unify.py +88 -0
- parrot/lib/python3.10/site-packages/sympy/unify/usympy.py +124 -0
- parrot/lib/python3.10/site-packages/sympy/vector/__pycache__/__init__.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/vector/__pycache__/basisdependent.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/sympy/vector/__pycache__/coordsysrect.cpython-310.pyc +0 -0
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/assumed_shape/precision.f90
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module precision
|
| 2 |
+
integer, parameter :: rk = selected_real_kind(8)
|
| 3 |
+
integer, parameter :: ik = selected_real_kind(4)
|
| 4 |
+
end module
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/accesstype.f90
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module foo
|
| 2 |
+
public
|
| 3 |
+
type, private, bind(c) :: a
|
| 4 |
+
integer :: i
|
| 5 |
+
end type a
|
| 6 |
+
type, bind(c) :: b_
|
| 7 |
+
integer :: j
|
| 8 |
+
end type b_
|
| 9 |
+
public :: b_
|
| 10 |
+
type :: c
|
| 11 |
+
integer :: k
|
| 12 |
+
end type c
|
| 13 |
+
end module foo
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/data_common.f
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BLOCK DATA PARAM_INI
|
| 2 |
+
COMMON /MYCOM/ MYDATA
|
| 3 |
+
DATA MYDATA /0/
|
| 4 |
+
END
|
| 5 |
+
SUBROUTINE SUB1
|
| 6 |
+
COMMON /MYCOM/ MYDATA
|
| 7 |
+
MYDATA = MYDATA + 1
|
| 8 |
+
END
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/data_multiplier.f
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BLOCK DATA MYBLK
|
| 2 |
+
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
|
| 3 |
+
COMMON /MYCOM/ IVAR1, IVAR2, IVAR3, IVAR4, EVAR5
|
| 4 |
+
DATA IVAR1, IVAR2, IVAR3, IVAR4, EVAR5 /2*3,2*2,0.0D0/
|
| 5 |
+
END
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh15035.f
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
subroutine subb(k)
|
| 2 |
+
real(8), intent(inout) :: k(:)
|
| 3 |
+
k=k+1
|
| 4 |
+
endsubroutine
|
| 5 |
+
|
| 6 |
+
subroutine subc(w,k)
|
| 7 |
+
real(8), intent(in) :: w(:)
|
| 8 |
+
real(8), intent(out) :: k(size(w))
|
| 9 |
+
k=w+1
|
| 10 |
+
endsubroutine
|
| 11 |
+
|
| 12 |
+
function t0(value)
|
| 13 |
+
character value
|
| 14 |
+
character t0
|
| 15 |
+
t0 = value
|
| 16 |
+
endfunction
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh17859.f
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
integer(8) function external_as_statement(fcn)
|
| 2 |
+
implicit none
|
| 3 |
+
external fcn
|
| 4 |
+
integer(8) :: fcn
|
| 5 |
+
external_as_statement = fcn(0)
|
| 6 |
+
end
|
| 7 |
+
|
| 8 |
+
integer(8) function external_as_attribute(fcn)
|
| 9 |
+
implicit none
|
| 10 |
+
integer(8), external :: fcn
|
| 11 |
+
external_as_attribute = fcn(0)
|
| 12 |
+
end
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh22648.pyf
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python module iri16py ! in
|
| 2 |
+
interface ! in :iri16py
|
| 3 |
+
block data ! in :iri16py:iridreg_modified.for
|
| 4 |
+
COMMON /fircom/ eden,tabhe,tabla,tabmo,tabza,tabfl
|
| 5 |
+
end block data
|
| 6 |
+
end interface
|
| 7 |
+
end python module iri16py
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23533.f
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SUBROUTINE EXAMPLE( )
|
| 2 |
+
IF( .TRUE. ) THEN
|
| 3 |
+
CALL DO_SOMETHING()
|
| 4 |
+
END IF ! ** .TRUE. **
|
| 5 |
+
END
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23598.f90
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
integer function intproduct(a, b) result(res)
|
| 2 |
+
integer, intent(in) :: a, b
|
| 3 |
+
res = a*b
|
| 4 |
+
end function
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh23879.f90
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module gh23879
|
| 2 |
+
implicit none
|
| 3 |
+
private
|
| 4 |
+
public :: foo
|
| 5 |
+
|
| 6 |
+
contains
|
| 7 |
+
|
| 8 |
+
subroutine foo(a, b)
|
| 9 |
+
integer, intent(in) :: a
|
| 10 |
+
integer, intent(out) :: b
|
| 11 |
+
b = a
|
| 12 |
+
call bar(b)
|
| 13 |
+
end subroutine
|
| 14 |
+
|
| 15 |
+
subroutine bar(x)
|
| 16 |
+
integer, intent(inout) :: x
|
| 17 |
+
x = 2*x
|
| 18 |
+
end subroutine
|
| 19 |
+
|
| 20 |
+
end module gh23879
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/gh2848.f90
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
subroutine gh2848( &
|
| 2 |
+
! first 2 parameters
|
| 3 |
+
par1, par2,&
|
| 4 |
+
! last 2 parameters
|
| 5 |
+
par3, par4)
|
| 6 |
+
|
| 7 |
+
integer, intent(in) :: par1, par2
|
| 8 |
+
integer, intent(out) :: par3, par4
|
| 9 |
+
|
| 10 |
+
par3 = par1
|
| 11 |
+
par4 = par2
|
| 12 |
+
|
| 13 |
+
end subroutine gh2848
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/operators.f90
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module foo
|
| 2 |
+
type bar
|
| 3 |
+
character(len = 32) :: item
|
| 4 |
+
end type bar
|
| 5 |
+
interface operator(.item.)
|
| 6 |
+
module procedure item_int, item_real
|
| 7 |
+
end interface operator(.item.)
|
| 8 |
+
interface operator(==)
|
| 9 |
+
module procedure items_are_equal
|
| 10 |
+
end interface operator(==)
|
| 11 |
+
interface assignment(=)
|
| 12 |
+
module procedure get_int, get_real
|
| 13 |
+
end interface assignment(=)
|
| 14 |
+
contains
|
| 15 |
+
function item_int(val) result(elem)
|
| 16 |
+
integer, intent(in) :: val
|
| 17 |
+
type(bar) :: elem
|
| 18 |
+
|
| 19 |
+
write(elem%item, "(I32)") val
|
| 20 |
+
end function item_int
|
| 21 |
+
|
| 22 |
+
function item_real(val) result(elem)
|
| 23 |
+
real, intent(in) :: val
|
| 24 |
+
type(bar) :: elem
|
| 25 |
+
|
| 26 |
+
write(elem%item, "(1PE32.12)") val
|
| 27 |
+
end function item_real
|
| 28 |
+
|
| 29 |
+
function items_are_equal(val1, val2) result(equal)
|
| 30 |
+
type(bar), intent(in) :: val1, val2
|
| 31 |
+
logical :: equal
|
| 32 |
+
|
| 33 |
+
equal = (val1%item == val2%item)
|
| 34 |
+
end function items_are_equal
|
| 35 |
+
|
| 36 |
+
subroutine get_real(rval, item)
|
| 37 |
+
real, intent(out) :: rval
|
| 38 |
+
type(bar), intent(in) :: item
|
| 39 |
+
|
| 40 |
+
read(item%item, *) rval
|
| 41 |
+
end subroutine get_real
|
| 42 |
+
|
| 43 |
+
subroutine get_int(rval, item)
|
| 44 |
+
integer, intent(out) :: rval
|
| 45 |
+
type(bar), intent(in) :: item
|
| 46 |
+
|
| 47 |
+
read(item%item, *) rval
|
| 48 |
+
end subroutine get_int
|
| 49 |
+
end module foo
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/privatemod.f90
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module foo
|
| 2 |
+
private
|
| 3 |
+
integer :: a
|
| 4 |
+
public :: setA
|
| 5 |
+
integer :: b
|
| 6 |
+
contains
|
| 7 |
+
subroutine setA(v)
|
| 8 |
+
integer, intent(in) :: v
|
| 9 |
+
a = v
|
| 10 |
+
end subroutine setA
|
| 11 |
+
end module foo
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/publicmod.f90
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module foo
|
| 2 |
+
public
|
| 3 |
+
integer, private :: a
|
| 4 |
+
public :: setA
|
| 5 |
+
contains
|
| 6 |
+
subroutine setA(v)
|
| 7 |
+
integer, intent(in) :: v
|
| 8 |
+
a = v
|
| 9 |
+
end subroutine setA
|
| 10 |
+
end module foo
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/crackfortran/unicode_comment.f90
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
subroutine foo(x)
|
| 2 |
+
real(8), intent(in) :: x
|
| 3 |
+
! Écrit à l'écran la valeur de x
|
| 4 |
+
end subroutine
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/regression/gh25337/data.f90
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module data
|
| 2 |
+
real(8) :: shift
|
| 3 |
+
contains
|
| 4 |
+
subroutine set_shift(in_shift)
|
| 5 |
+
real(8), intent(in) :: in_shift
|
| 6 |
+
shift = in_shift
|
| 7 |
+
end subroutine set_shift
|
| 8 |
+
end module data
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/regression/gh25337/use_data.f90
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
subroutine shift_a(dim_a, a)
|
| 2 |
+
use data, only: shift
|
| 3 |
+
integer, intent(in) :: dim_a
|
| 4 |
+
real(8), intent(inout), dimension(dim_a) :: a
|
| 5 |
+
a = a + shift
|
| 6 |
+
end subroutine shift_a
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/regression/inout.f90
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
! Check that intent(in out) translates as intent(inout).
|
| 2 |
+
! The separation seems to be a common usage.
|
| 3 |
+
subroutine foo(x)
|
| 4 |
+
implicit none
|
| 5 |
+
real(4), intent(in out) :: x
|
| 6 |
+
dimension x(3)
|
| 7 |
+
x(1) = x(1) + x(2) + x(3)
|
| 8 |
+
return
|
| 9 |
+
end
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/return_integer/foo77.f
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
function t0(value)
|
| 2 |
+
integer value
|
| 3 |
+
integer t0
|
| 4 |
+
t0 = value
|
| 5 |
+
end
|
| 6 |
+
function t1(value)
|
| 7 |
+
integer*1 value
|
| 8 |
+
integer*1 t1
|
| 9 |
+
t1 = value
|
| 10 |
+
end
|
| 11 |
+
function t2(value)
|
| 12 |
+
integer*2 value
|
| 13 |
+
integer*2 t2
|
| 14 |
+
t2 = value
|
| 15 |
+
end
|
| 16 |
+
function t4(value)
|
| 17 |
+
integer*4 value
|
| 18 |
+
integer*4 t4
|
| 19 |
+
t4 = value
|
| 20 |
+
end
|
| 21 |
+
function t8(value)
|
| 22 |
+
integer*8 value
|
| 23 |
+
integer*8 t8
|
| 24 |
+
t8 = value
|
| 25 |
+
end
|
| 26 |
+
|
| 27 |
+
subroutine s0(t0,value)
|
| 28 |
+
integer value
|
| 29 |
+
integer t0
|
| 30 |
+
cf2py intent(out) t0
|
| 31 |
+
t0 = value
|
| 32 |
+
end
|
| 33 |
+
subroutine s1(t1,value)
|
| 34 |
+
integer*1 value
|
| 35 |
+
integer*1 t1
|
| 36 |
+
cf2py intent(out) t1
|
| 37 |
+
t1 = value
|
| 38 |
+
end
|
| 39 |
+
subroutine s2(t2,value)
|
| 40 |
+
integer*2 value
|
| 41 |
+
integer*2 t2
|
| 42 |
+
cf2py intent(out) t2
|
| 43 |
+
t2 = value
|
| 44 |
+
end
|
| 45 |
+
subroutine s4(t4,value)
|
| 46 |
+
integer*4 value
|
| 47 |
+
integer*4 t4
|
| 48 |
+
cf2py intent(out) t4
|
| 49 |
+
t4 = value
|
| 50 |
+
end
|
| 51 |
+
subroutine s8(t8,value)
|
| 52 |
+
integer*8 value
|
| 53 |
+
integer*8 t8
|
| 54 |
+
cf2py intent(out) t8
|
| 55 |
+
t8 = value
|
| 56 |
+
end
|
llava_next/lib/python3.10/site-packages/numpy/f2py/tests/src/return_integer/foo90.f90
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module f90_return_integer
|
| 2 |
+
contains
|
| 3 |
+
function t0(value)
|
| 4 |
+
integer :: value
|
| 5 |
+
integer :: t0
|
| 6 |
+
t0 = value
|
| 7 |
+
end function t0
|
| 8 |
+
function t1(value)
|
| 9 |
+
integer(kind=1) :: value
|
| 10 |
+
integer(kind=1) :: t1
|
| 11 |
+
t1 = value
|
| 12 |
+
end function t1
|
| 13 |
+
function t2(value)
|
| 14 |
+
integer(kind=2) :: value
|
| 15 |
+
integer(kind=2) :: t2
|
| 16 |
+
t2 = value
|
| 17 |
+
end function t2
|
| 18 |
+
function t4(value)
|
| 19 |
+
integer(kind=4) :: value
|
| 20 |
+
integer(kind=4) :: t4
|
| 21 |
+
t4 = value
|
| 22 |
+
end function t4
|
| 23 |
+
function t8(value)
|
| 24 |
+
integer(kind=8) :: value
|
| 25 |
+
integer(kind=8) :: t8
|
| 26 |
+
t8 = value
|
| 27 |
+
end function t8
|
| 28 |
+
|
| 29 |
+
subroutine s0(t0,value)
|
| 30 |
+
integer :: value
|
| 31 |
+
integer :: t0
|
| 32 |
+
!f2py intent(out) t0
|
| 33 |
+
t0 = value
|
| 34 |
+
end subroutine s0
|
| 35 |
+
subroutine s1(t1,value)
|
| 36 |
+
integer(kind=1) :: value
|
| 37 |
+
integer(kind=1) :: t1
|
| 38 |
+
!f2py intent(out) t1
|
| 39 |
+
t1 = value
|
| 40 |
+
end subroutine s1
|
| 41 |
+
subroutine s2(t2,value)
|
| 42 |
+
integer(kind=2) :: value
|
| 43 |
+
integer(kind=2) :: t2
|
| 44 |
+
!f2py intent(out) t2
|
| 45 |
+
t2 = value
|
| 46 |
+
end subroutine s2
|
| 47 |
+
subroutine s4(t4,value)
|
| 48 |
+
integer(kind=4) :: value
|
| 49 |
+
integer(kind=4) :: t4
|
| 50 |
+
!f2py intent(out) t4
|
| 51 |
+
t4 = value
|
| 52 |
+
end subroutine s4
|
| 53 |
+
subroutine s8(t8,value)
|
| 54 |
+
integer(kind=8) :: value
|
| 55 |
+
integer(kind=8) :: t8
|
| 56 |
+
!f2py intent(out) t8
|
| 57 |
+
t8 = value
|
| 58 |
+
end subroutine s8
|
| 59 |
+
end module f90_return_integer
|
parrot/lib/python3.10/site-packages/sympy/functions/elementary/complexes.py
ADDED
|
@@ -0,0 +1,1473 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Tuple as tTuple
|
| 2 |
+
|
| 3 |
+
from sympy.core import S, Add, Mul, sympify, Symbol, Dummy, Basic
|
| 4 |
+
from sympy.core.expr import Expr
|
| 5 |
+
from sympy.core.exprtools import factor_terms
|
| 6 |
+
from sympy.core.function import (Function, Derivative, ArgumentIndexError,
|
| 7 |
+
AppliedUndef, expand_mul)
|
| 8 |
+
from sympy.core.logic import fuzzy_not, fuzzy_or
|
| 9 |
+
from sympy.core.numbers import pi, I, oo
|
| 10 |
+
from sympy.core.power import Pow
|
| 11 |
+
from sympy.core.relational import Eq
|
| 12 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 13 |
+
from sympy.functions.elementary.piecewise import Piecewise
|
| 14 |
+
|
| 15 |
+
###############################################################################
|
| 16 |
+
######################### REAL and IMAGINARY PARTS ############################
|
| 17 |
+
###############################################################################
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class re(Function):
|
| 21 |
+
"""
|
| 22 |
+
Returns real part of expression. This function performs only
|
| 23 |
+
elementary analysis and so it will fail to decompose properly
|
| 24 |
+
more complicated expressions. If completely simplified result
|
| 25 |
+
is needed then use ``Basic.as_real_imag()`` or perform complex
|
| 26 |
+
expansion on instance of this function.
|
| 27 |
+
|
| 28 |
+
Examples
|
| 29 |
+
========
|
| 30 |
+
|
| 31 |
+
>>> from sympy import re, im, I, E, symbols
|
| 32 |
+
>>> x, y = symbols('x y', real=True)
|
| 33 |
+
>>> re(2*E)
|
| 34 |
+
2*E
|
| 35 |
+
>>> re(2*I + 17)
|
| 36 |
+
17
|
| 37 |
+
>>> re(2*I)
|
| 38 |
+
0
|
| 39 |
+
>>> re(im(x) + x*I + 2)
|
| 40 |
+
2
|
| 41 |
+
>>> re(5 + I + 2)
|
| 42 |
+
7
|
| 43 |
+
|
| 44 |
+
Parameters
|
| 45 |
+
==========
|
| 46 |
+
|
| 47 |
+
arg : Expr
|
| 48 |
+
Real or complex expression.
|
| 49 |
+
|
| 50 |
+
Returns
|
| 51 |
+
=======
|
| 52 |
+
|
| 53 |
+
expr : Expr
|
| 54 |
+
Real part of expression.
|
| 55 |
+
|
| 56 |
+
See Also
|
| 57 |
+
========
|
| 58 |
+
|
| 59 |
+
im
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
args: tTuple[Expr]
|
| 63 |
+
|
| 64 |
+
is_extended_real = True
|
| 65 |
+
unbranched = True # implicitly works on the projection to C
|
| 66 |
+
_singularities = True # non-holomorphic
|
| 67 |
+
|
| 68 |
+
@classmethod
|
| 69 |
+
def eval(cls, arg):
|
| 70 |
+
if arg is S.NaN:
|
| 71 |
+
return S.NaN
|
| 72 |
+
elif arg is S.ComplexInfinity:
|
| 73 |
+
return S.NaN
|
| 74 |
+
elif arg.is_extended_real:
|
| 75 |
+
return arg
|
| 76 |
+
elif arg.is_imaginary or (I*arg).is_extended_real:
|
| 77 |
+
return S.Zero
|
| 78 |
+
elif arg.is_Matrix:
|
| 79 |
+
return arg.as_real_imag()[0]
|
| 80 |
+
elif arg.is_Function and isinstance(arg, conjugate):
|
| 81 |
+
return re(arg.args[0])
|
| 82 |
+
else:
|
| 83 |
+
|
| 84 |
+
included, reverted, excluded = [], [], []
|
| 85 |
+
args = Add.make_args(arg)
|
| 86 |
+
for term in args:
|
| 87 |
+
coeff = term.as_coefficient(I)
|
| 88 |
+
|
| 89 |
+
if coeff is not None:
|
| 90 |
+
if not coeff.is_extended_real:
|
| 91 |
+
reverted.append(coeff)
|
| 92 |
+
elif not term.has(I) and term.is_extended_real:
|
| 93 |
+
excluded.append(term)
|
| 94 |
+
else:
|
| 95 |
+
# Try to do some advanced expansion. If
|
| 96 |
+
# impossible, don't try to do re(arg) again
|
| 97 |
+
# (because this is what we are trying to do now).
|
| 98 |
+
real_imag = term.as_real_imag(ignore=arg)
|
| 99 |
+
if real_imag:
|
| 100 |
+
excluded.append(real_imag[0])
|
| 101 |
+
else:
|
| 102 |
+
included.append(term)
|
| 103 |
+
|
| 104 |
+
if len(args) != len(included):
|
| 105 |
+
a, b, c = (Add(*xs) for xs in [included, reverted, excluded])
|
| 106 |
+
|
| 107 |
+
return cls(a) - im(b) + c
|
| 108 |
+
|
| 109 |
+
def as_real_imag(self, deep=True, **hints):
|
| 110 |
+
"""
|
| 111 |
+
Returns the real number with a zero imaginary part.
|
| 112 |
+
|
| 113 |
+
"""
|
| 114 |
+
return (self, S.Zero)
|
| 115 |
+
|
| 116 |
+
def _eval_derivative(self, x):
|
| 117 |
+
if x.is_extended_real or self.args[0].is_extended_real:
|
| 118 |
+
return re(Derivative(self.args[0], x, evaluate=True))
|
| 119 |
+
if x.is_imaginary or self.args[0].is_imaginary:
|
| 120 |
+
return -I \
|
| 121 |
+
* im(Derivative(self.args[0], x, evaluate=True))
|
| 122 |
+
|
| 123 |
+
def _eval_rewrite_as_im(self, arg, **kwargs):
|
| 124 |
+
return self.args[0] - I*im(self.args[0])
|
| 125 |
+
|
| 126 |
+
def _eval_is_algebraic(self):
|
| 127 |
+
return self.args[0].is_algebraic
|
| 128 |
+
|
| 129 |
+
def _eval_is_zero(self):
|
| 130 |
+
# is_imaginary implies nonzero
|
| 131 |
+
return fuzzy_or([self.args[0].is_imaginary, self.args[0].is_zero])
|
| 132 |
+
|
| 133 |
+
def _eval_is_finite(self):
|
| 134 |
+
if self.args[0].is_finite:
|
| 135 |
+
return True
|
| 136 |
+
|
| 137 |
+
def _eval_is_complex(self):
|
| 138 |
+
if self.args[0].is_finite:
|
| 139 |
+
return True
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
class im(Function):
|
| 143 |
+
"""
|
| 144 |
+
Returns imaginary part of expression. This function performs only
|
| 145 |
+
elementary analysis and so it will fail to decompose properly more
|
| 146 |
+
complicated expressions. If completely simplified result is needed then
|
| 147 |
+
use ``Basic.as_real_imag()`` or perform complex expansion on instance of
|
| 148 |
+
this function.
|
| 149 |
+
|
| 150 |
+
Examples
|
| 151 |
+
========
|
| 152 |
+
|
| 153 |
+
>>> from sympy import re, im, E, I
|
| 154 |
+
>>> from sympy.abc import x, y
|
| 155 |
+
>>> im(2*E)
|
| 156 |
+
0
|
| 157 |
+
>>> im(2*I + 17)
|
| 158 |
+
2
|
| 159 |
+
>>> im(x*I)
|
| 160 |
+
re(x)
|
| 161 |
+
>>> im(re(x) + y)
|
| 162 |
+
im(y)
|
| 163 |
+
>>> im(2 + 3*I)
|
| 164 |
+
3
|
| 165 |
+
|
| 166 |
+
Parameters
|
| 167 |
+
==========
|
| 168 |
+
|
| 169 |
+
arg : Expr
|
| 170 |
+
Real or complex expression.
|
| 171 |
+
|
| 172 |
+
Returns
|
| 173 |
+
=======
|
| 174 |
+
|
| 175 |
+
expr : Expr
|
| 176 |
+
Imaginary part of expression.
|
| 177 |
+
|
| 178 |
+
See Also
|
| 179 |
+
========
|
| 180 |
+
|
| 181 |
+
re
|
| 182 |
+
"""
|
| 183 |
+
|
| 184 |
+
args: tTuple[Expr]
|
| 185 |
+
|
| 186 |
+
is_extended_real = True
|
| 187 |
+
unbranched = True # implicitly works on the projection to C
|
| 188 |
+
_singularities = True # non-holomorphic
|
| 189 |
+
|
| 190 |
+
@classmethod
|
| 191 |
+
def eval(cls, arg):
|
| 192 |
+
if arg is S.NaN:
|
| 193 |
+
return S.NaN
|
| 194 |
+
elif arg is S.ComplexInfinity:
|
| 195 |
+
return S.NaN
|
| 196 |
+
elif arg.is_extended_real:
|
| 197 |
+
return S.Zero
|
| 198 |
+
elif arg.is_imaginary or (I*arg).is_extended_real:
|
| 199 |
+
return -I * arg
|
| 200 |
+
elif arg.is_Matrix:
|
| 201 |
+
return arg.as_real_imag()[1]
|
| 202 |
+
elif arg.is_Function and isinstance(arg, conjugate):
|
| 203 |
+
return -im(arg.args[0])
|
| 204 |
+
else:
|
| 205 |
+
included, reverted, excluded = [], [], []
|
| 206 |
+
args = Add.make_args(arg)
|
| 207 |
+
for term in args:
|
| 208 |
+
coeff = term.as_coefficient(I)
|
| 209 |
+
|
| 210 |
+
if coeff is not None:
|
| 211 |
+
if not coeff.is_extended_real:
|
| 212 |
+
reverted.append(coeff)
|
| 213 |
+
else:
|
| 214 |
+
excluded.append(coeff)
|
| 215 |
+
elif term.has(I) or not term.is_extended_real:
|
| 216 |
+
# Try to do some advanced expansion. If
|
| 217 |
+
# impossible, don't try to do im(arg) again
|
| 218 |
+
# (because this is what we are trying to do now).
|
| 219 |
+
real_imag = term.as_real_imag(ignore=arg)
|
| 220 |
+
if real_imag:
|
| 221 |
+
excluded.append(real_imag[1])
|
| 222 |
+
else:
|
| 223 |
+
included.append(term)
|
| 224 |
+
|
| 225 |
+
if len(args) != len(included):
|
| 226 |
+
a, b, c = (Add(*xs) for xs in [included, reverted, excluded])
|
| 227 |
+
|
| 228 |
+
return cls(a) + re(b) + c
|
| 229 |
+
|
| 230 |
+
def as_real_imag(self, deep=True, **hints):
|
| 231 |
+
"""
|
| 232 |
+
Return the imaginary part with a zero real part.
|
| 233 |
+
|
| 234 |
+
"""
|
| 235 |
+
return (self, S.Zero)
|
| 236 |
+
|
| 237 |
+
def _eval_derivative(self, x):
|
| 238 |
+
if x.is_extended_real or self.args[0].is_extended_real:
|
| 239 |
+
return im(Derivative(self.args[0], x, evaluate=True))
|
| 240 |
+
if x.is_imaginary or self.args[0].is_imaginary:
|
| 241 |
+
return -I \
|
| 242 |
+
* re(Derivative(self.args[0], x, evaluate=True))
|
| 243 |
+
|
| 244 |
+
def _eval_rewrite_as_re(self, arg, **kwargs):
|
| 245 |
+
return -I*(self.args[0] - re(self.args[0]))
|
| 246 |
+
|
| 247 |
+
def _eval_is_algebraic(self):
|
| 248 |
+
return self.args[0].is_algebraic
|
| 249 |
+
|
| 250 |
+
def _eval_is_zero(self):
|
| 251 |
+
return self.args[0].is_extended_real
|
| 252 |
+
|
| 253 |
+
def _eval_is_finite(self):
|
| 254 |
+
if self.args[0].is_finite:
|
| 255 |
+
return True
|
| 256 |
+
|
| 257 |
+
def _eval_is_complex(self):
|
| 258 |
+
if self.args[0].is_finite:
|
| 259 |
+
return True
|
| 260 |
+
|
| 261 |
+
###############################################################################
|
| 262 |
+
############### SIGN, ABSOLUTE VALUE, ARGUMENT and CONJUGATION ################
|
| 263 |
+
###############################################################################
|
| 264 |
+
|
| 265 |
+
class sign(Function):
|
| 266 |
+
"""
|
| 267 |
+
Returns the complex sign of an expression:
|
| 268 |
+
|
| 269 |
+
Explanation
|
| 270 |
+
===========
|
| 271 |
+
|
| 272 |
+
If the expression is real the sign will be:
|
| 273 |
+
|
| 274 |
+
* $1$ if expression is positive
|
| 275 |
+
* $0$ if expression is equal to zero
|
| 276 |
+
* $-1$ if expression is negative
|
| 277 |
+
|
| 278 |
+
If the expression is imaginary the sign will be:
|
| 279 |
+
|
| 280 |
+
* $I$ if im(expression) is positive
|
| 281 |
+
* $-I$ if im(expression) is negative
|
| 282 |
+
|
| 283 |
+
Otherwise an unevaluated expression will be returned. When evaluated, the
|
| 284 |
+
result (in general) will be ``cos(arg(expr)) + I*sin(arg(expr))``.
|
| 285 |
+
|
| 286 |
+
Examples
|
| 287 |
+
========
|
| 288 |
+
|
| 289 |
+
>>> from sympy import sign, I
|
| 290 |
+
|
| 291 |
+
>>> sign(-1)
|
| 292 |
+
-1
|
| 293 |
+
>>> sign(0)
|
| 294 |
+
0
|
| 295 |
+
>>> sign(-3*I)
|
| 296 |
+
-I
|
| 297 |
+
>>> sign(1 + I)
|
| 298 |
+
sign(1 + I)
|
| 299 |
+
>>> _.evalf()
|
| 300 |
+
0.707106781186548 + 0.707106781186548*I
|
| 301 |
+
|
| 302 |
+
Parameters
|
| 303 |
+
==========
|
| 304 |
+
|
| 305 |
+
arg : Expr
|
| 306 |
+
Real or imaginary expression.
|
| 307 |
+
|
| 308 |
+
Returns
|
| 309 |
+
=======
|
| 310 |
+
|
| 311 |
+
expr : Expr
|
| 312 |
+
Complex sign of expression.
|
| 313 |
+
|
| 314 |
+
See Also
|
| 315 |
+
========
|
| 316 |
+
|
| 317 |
+
Abs, conjugate
|
| 318 |
+
"""
|
| 319 |
+
|
| 320 |
+
is_complex = True
|
| 321 |
+
_singularities = True
|
| 322 |
+
|
| 323 |
+
def doit(self, **hints):
|
| 324 |
+
s = super().doit()
|
| 325 |
+
if s == self and self.args[0].is_zero is False:
|
| 326 |
+
return self.args[0] / Abs(self.args[0])
|
| 327 |
+
return s
|
| 328 |
+
|
| 329 |
+
@classmethod
|
| 330 |
+
def eval(cls, arg):
|
| 331 |
+
# handle what we can
|
| 332 |
+
if arg.is_Mul:
|
| 333 |
+
c, args = arg.as_coeff_mul()
|
| 334 |
+
unk = []
|
| 335 |
+
s = sign(c)
|
| 336 |
+
for a in args:
|
| 337 |
+
if a.is_extended_negative:
|
| 338 |
+
s = -s
|
| 339 |
+
elif a.is_extended_positive:
|
| 340 |
+
pass
|
| 341 |
+
else:
|
| 342 |
+
if a.is_imaginary:
|
| 343 |
+
ai = im(a)
|
| 344 |
+
if ai.is_comparable: # i.e. a = I*real
|
| 345 |
+
s *= I
|
| 346 |
+
if ai.is_extended_negative:
|
| 347 |
+
# can't use sign(ai) here since ai might not be
|
| 348 |
+
# a Number
|
| 349 |
+
s = -s
|
| 350 |
+
else:
|
| 351 |
+
unk.append(a)
|
| 352 |
+
else:
|
| 353 |
+
unk.append(a)
|
| 354 |
+
if c is S.One and len(unk) == len(args):
|
| 355 |
+
return None
|
| 356 |
+
return s * cls(arg._new_rawargs(*unk))
|
| 357 |
+
if arg is S.NaN:
|
| 358 |
+
return S.NaN
|
| 359 |
+
if arg.is_zero: # it may be an Expr that is zero
|
| 360 |
+
return S.Zero
|
| 361 |
+
if arg.is_extended_positive:
|
| 362 |
+
return S.One
|
| 363 |
+
if arg.is_extended_negative:
|
| 364 |
+
return S.NegativeOne
|
| 365 |
+
if arg.is_Function:
|
| 366 |
+
if isinstance(arg, sign):
|
| 367 |
+
return arg
|
| 368 |
+
if arg.is_imaginary:
|
| 369 |
+
if arg.is_Pow and arg.exp is S.Half:
|
| 370 |
+
# we catch this because non-trivial sqrt args are not expanded
|
| 371 |
+
# e.g. sqrt(1-sqrt(2)) --x--> to I*sqrt(sqrt(2) - 1)
|
| 372 |
+
return I
|
| 373 |
+
arg2 = -I * arg
|
| 374 |
+
if arg2.is_extended_positive:
|
| 375 |
+
return I
|
| 376 |
+
if arg2.is_extended_negative:
|
| 377 |
+
return -I
|
| 378 |
+
|
| 379 |
+
def _eval_Abs(self):
|
| 380 |
+
if fuzzy_not(self.args[0].is_zero):
|
| 381 |
+
return S.One
|
| 382 |
+
|
| 383 |
+
def _eval_conjugate(self):
|
| 384 |
+
return sign(conjugate(self.args[0]))
|
| 385 |
+
|
| 386 |
+
def _eval_derivative(self, x):
|
| 387 |
+
if self.args[0].is_extended_real:
|
| 388 |
+
from sympy.functions.special.delta_functions import DiracDelta
|
| 389 |
+
return 2 * Derivative(self.args[0], x, evaluate=True) \
|
| 390 |
+
* DiracDelta(self.args[0])
|
| 391 |
+
elif self.args[0].is_imaginary:
|
| 392 |
+
from sympy.functions.special.delta_functions import DiracDelta
|
| 393 |
+
return 2 * Derivative(self.args[0], x, evaluate=True) \
|
| 394 |
+
* DiracDelta(-I * self.args[0])
|
| 395 |
+
|
| 396 |
+
def _eval_is_nonnegative(self):
|
| 397 |
+
if self.args[0].is_nonnegative:
|
| 398 |
+
return True
|
| 399 |
+
|
| 400 |
+
def _eval_is_nonpositive(self):
|
| 401 |
+
if self.args[0].is_nonpositive:
|
| 402 |
+
return True
|
| 403 |
+
|
| 404 |
+
def _eval_is_imaginary(self):
|
| 405 |
+
return self.args[0].is_imaginary
|
| 406 |
+
|
| 407 |
+
def _eval_is_integer(self):
|
| 408 |
+
return self.args[0].is_extended_real
|
| 409 |
+
|
| 410 |
+
def _eval_is_zero(self):
|
| 411 |
+
return self.args[0].is_zero
|
| 412 |
+
|
| 413 |
+
def _eval_power(self, other):
|
| 414 |
+
if (
|
| 415 |
+
fuzzy_not(self.args[0].is_zero) and
|
| 416 |
+
other.is_integer and
|
| 417 |
+
other.is_even
|
| 418 |
+
):
|
| 419 |
+
return S.One
|
| 420 |
+
|
| 421 |
+
def _eval_nseries(self, x, n, logx, cdir=0):
|
| 422 |
+
arg0 = self.args[0]
|
| 423 |
+
x0 = arg0.subs(x, 0)
|
| 424 |
+
if x0 != 0:
|
| 425 |
+
return self.func(x0)
|
| 426 |
+
if cdir != 0:
|
| 427 |
+
cdir = arg0.dir(x, cdir)
|
| 428 |
+
return -S.One if re(cdir) < 0 else S.One
|
| 429 |
+
|
| 430 |
+
def _eval_rewrite_as_Piecewise(self, arg, **kwargs):
|
| 431 |
+
if arg.is_extended_real:
|
| 432 |
+
return Piecewise((1, arg > 0), (-1, arg < 0), (0, True))
|
| 433 |
+
|
| 434 |
+
def _eval_rewrite_as_Heaviside(self, arg, **kwargs):
|
| 435 |
+
from sympy.functions.special.delta_functions import Heaviside
|
| 436 |
+
if arg.is_extended_real:
|
| 437 |
+
return Heaviside(arg) * 2 - 1
|
| 438 |
+
|
| 439 |
+
def _eval_rewrite_as_Abs(self, arg, **kwargs):
|
| 440 |
+
return Piecewise((0, Eq(arg, 0)), (arg / Abs(arg), True))
|
| 441 |
+
|
| 442 |
+
def _eval_simplify(self, **kwargs):
|
| 443 |
+
return self.func(factor_terms(self.args[0])) # XXX include doit?
|
| 444 |
+
|
| 445 |
+
|
| 446 |
+
class Abs(Function):
|
| 447 |
+
"""
|
| 448 |
+
Return the absolute value of the argument.
|
| 449 |
+
|
| 450 |
+
Explanation
|
| 451 |
+
===========
|
| 452 |
+
|
| 453 |
+
This is an extension of the built-in function ``abs()`` to accept symbolic
|
| 454 |
+
values. If you pass a SymPy expression to the built-in ``abs()``, it will
|
| 455 |
+
pass it automatically to ``Abs()``.
|
| 456 |
+
|
| 457 |
+
Examples
|
| 458 |
+
========
|
| 459 |
+
|
| 460 |
+
>>> from sympy import Abs, Symbol, S, I
|
| 461 |
+
>>> Abs(-1)
|
| 462 |
+
1
|
| 463 |
+
>>> x = Symbol('x', real=True)
|
| 464 |
+
>>> Abs(-x)
|
| 465 |
+
Abs(x)
|
| 466 |
+
>>> Abs(x**2)
|
| 467 |
+
x**2
|
| 468 |
+
>>> abs(-x) # The Python built-in
|
| 469 |
+
Abs(x)
|
| 470 |
+
>>> Abs(3*x + 2*I)
|
| 471 |
+
sqrt(9*x**2 + 4)
|
| 472 |
+
>>> Abs(8*I)
|
| 473 |
+
8
|
| 474 |
+
|
| 475 |
+
Note that the Python built-in will return either an Expr or int depending on
|
| 476 |
+
the argument::
|
| 477 |
+
|
| 478 |
+
>>> type(abs(-1))
|
| 479 |
+
<... 'int'>
|
| 480 |
+
>>> type(abs(S.NegativeOne))
|
| 481 |
+
<class 'sympy.core.numbers.One'>
|
| 482 |
+
|
| 483 |
+
Abs will always return a SymPy object.
|
| 484 |
+
|
| 485 |
+
Parameters
|
| 486 |
+
==========
|
| 487 |
+
|
| 488 |
+
arg : Expr
|
| 489 |
+
Real or complex expression.
|
| 490 |
+
|
| 491 |
+
Returns
|
| 492 |
+
=======
|
| 493 |
+
|
| 494 |
+
expr : Expr
|
| 495 |
+
Absolute value returned can be an expression or integer depending on
|
| 496 |
+
input arg.
|
| 497 |
+
|
| 498 |
+
See Also
|
| 499 |
+
========
|
| 500 |
+
|
| 501 |
+
sign, conjugate
|
| 502 |
+
"""
|
| 503 |
+
|
| 504 |
+
args: tTuple[Expr]
|
| 505 |
+
|
| 506 |
+
is_extended_real = True
|
| 507 |
+
is_extended_negative = False
|
| 508 |
+
is_extended_nonnegative = True
|
| 509 |
+
unbranched = True
|
| 510 |
+
_singularities = True # non-holomorphic
|
| 511 |
+
|
| 512 |
+
def fdiff(self, argindex=1):
|
| 513 |
+
"""
|
| 514 |
+
Get the first derivative of the argument to Abs().
|
| 515 |
+
|
| 516 |
+
"""
|
| 517 |
+
if argindex == 1:
|
| 518 |
+
return sign(self.args[0])
|
| 519 |
+
else:
|
| 520 |
+
raise ArgumentIndexError(self, argindex)
|
| 521 |
+
|
| 522 |
+
@classmethod
|
| 523 |
+
def eval(cls, arg):
|
| 524 |
+
from sympy.simplify.simplify import signsimp
|
| 525 |
+
|
| 526 |
+
if hasattr(arg, '_eval_Abs'):
|
| 527 |
+
obj = arg._eval_Abs()
|
| 528 |
+
if obj is not None:
|
| 529 |
+
return obj
|
| 530 |
+
if not isinstance(arg, Expr):
|
| 531 |
+
raise TypeError("Bad argument type for Abs(): %s" % type(arg))
|
| 532 |
+
|
| 533 |
+
# handle what we can
|
| 534 |
+
arg = signsimp(arg, evaluate=False)
|
| 535 |
+
n, d = arg.as_numer_denom()
|
| 536 |
+
if d.free_symbols and not n.free_symbols:
|
| 537 |
+
return cls(n)/cls(d)
|
| 538 |
+
|
| 539 |
+
if arg.is_Mul:
|
| 540 |
+
known = []
|
| 541 |
+
unk = []
|
| 542 |
+
for t in arg.args:
|
| 543 |
+
if t.is_Pow and t.exp.is_integer and t.exp.is_negative:
|
| 544 |
+
bnew = cls(t.base)
|
| 545 |
+
if isinstance(bnew, cls):
|
| 546 |
+
unk.append(t)
|
| 547 |
+
else:
|
| 548 |
+
known.append(Pow(bnew, t.exp))
|
| 549 |
+
else:
|
| 550 |
+
tnew = cls(t)
|
| 551 |
+
if isinstance(tnew, cls):
|
| 552 |
+
unk.append(t)
|
| 553 |
+
else:
|
| 554 |
+
known.append(tnew)
|
| 555 |
+
known = Mul(*known)
|
| 556 |
+
unk = cls(Mul(*unk), evaluate=False) if unk else S.One
|
| 557 |
+
return known*unk
|
| 558 |
+
if arg is S.NaN:
|
| 559 |
+
return S.NaN
|
| 560 |
+
if arg is S.ComplexInfinity:
|
| 561 |
+
return oo
|
| 562 |
+
from sympy.functions.elementary.exponential import exp, log
|
| 563 |
+
|
| 564 |
+
if arg.is_Pow:
|
| 565 |
+
base, exponent = arg.as_base_exp()
|
| 566 |
+
if base.is_extended_real:
|
| 567 |
+
if exponent.is_integer:
|
| 568 |
+
if exponent.is_even:
|
| 569 |
+
return arg
|
| 570 |
+
if base is S.NegativeOne:
|
| 571 |
+
return S.One
|
| 572 |
+
return Abs(base)**exponent
|
| 573 |
+
if base.is_extended_nonnegative:
|
| 574 |
+
return base**re(exponent)
|
| 575 |
+
if base.is_extended_negative:
|
| 576 |
+
return (-base)**re(exponent)*exp(-pi*im(exponent))
|
| 577 |
+
return
|
| 578 |
+
elif not base.has(Symbol): # complex base
|
| 579 |
+
# express base**exponent as exp(exponent*log(base))
|
| 580 |
+
a, b = log(base).as_real_imag()
|
| 581 |
+
z = a + I*b
|
| 582 |
+
return exp(re(exponent*z))
|
| 583 |
+
if isinstance(arg, exp):
|
| 584 |
+
return exp(re(arg.args[0]))
|
| 585 |
+
if isinstance(arg, AppliedUndef):
|
| 586 |
+
if arg.is_positive:
|
| 587 |
+
return arg
|
| 588 |
+
elif arg.is_negative:
|
| 589 |
+
return -arg
|
| 590 |
+
return
|
| 591 |
+
if arg.is_Add and arg.has(oo, S.NegativeInfinity):
|
| 592 |
+
if any(a.is_infinite for a in arg.as_real_imag()):
|
| 593 |
+
return oo
|
| 594 |
+
if arg.is_zero:
|
| 595 |
+
return S.Zero
|
| 596 |
+
if arg.is_extended_nonnegative:
|
| 597 |
+
return arg
|
| 598 |
+
if arg.is_extended_nonpositive:
|
| 599 |
+
return -arg
|
| 600 |
+
if arg.is_imaginary:
|
| 601 |
+
arg2 = -I * arg
|
| 602 |
+
if arg2.is_extended_nonnegative:
|
| 603 |
+
return arg2
|
| 604 |
+
if arg.is_extended_real:
|
| 605 |
+
return
|
| 606 |
+
# reject result if all new conjugates are just wrappers around
|
| 607 |
+
# an expression that was already in the arg
|
| 608 |
+
conj = signsimp(arg.conjugate(), evaluate=False)
|
| 609 |
+
new_conj = conj.atoms(conjugate) - arg.atoms(conjugate)
|
| 610 |
+
if new_conj and all(arg.has(i.args[0]) for i in new_conj):
|
| 611 |
+
return
|
| 612 |
+
if arg != conj and arg != -conj:
|
| 613 |
+
ignore = arg.atoms(Abs)
|
| 614 |
+
abs_free_arg = arg.xreplace({i: Dummy(real=True) for i in ignore})
|
| 615 |
+
unk = [a for a in abs_free_arg.free_symbols if a.is_extended_real is None]
|
| 616 |
+
if not unk or not all(conj.has(conjugate(u)) for u in unk):
|
| 617 |
+
return sqrt(expand_mul(arg*conj))
|
| 618 |
+
|
| 619 |
+
def _eval_is_real(self):
|
| 620 |
+
if self.args[0].is_finite:
|
| 621 |
+
return True
|
| 622 |
+
|
| 623 |
+
def _eval_is_integer(self):
|
| 624 |
+
if self.args[0].is_extended_real:
|
| 625 |
+
return self.args[0].is_integer
|
| 626 |
+
|
| 627 |
+
def _eval_is_extended_nonzero(self):
|
| 628 |
+
return fuzzy_not(self._args[0].is_zero)
|
| 629 |
+
|
| 630 |
+
def _eval_is_zero(self):
|
| 631 |
+
return self._args[0].is_zero
|
| 632 |
+
|
| 633 |
+
def _eval_is_extended_positive(self):
|
| 634 |
+
return fuzzy_not(self._args[0].is_zero)
|
| 635 |
+
|
| 636 |
+
def _eval_is_rational(self):
|
| 637 |
+
if self.args[0].is_extended_real:
|
| 638 |
+
return self.args[0].is_rational
|
| 639 |
+
|
| 640 |
+
def _eval_is_even(self):
|
| 641 |
+
if self.args[0].is_extended_real:
|
| 642 |
+
return self.args[0].is_even
|
| 643 |
+
|
| 644 |
+
def _eval_is_odd(self):
|
| 645 |
+
if self.args[0].is_extended_real:
|
| 646 |
+
return self.args[0].is_odd
|
| 647 |
+
|
| 648 |
+
def _eval_is_algebraic(self):
|
| 649 |
+
return self.args[0].is_algebraic
|
| 650 |
+
|
| 651 |
+
def _eval_power(self, exponent):
|
| 652 |
+
if self.args[0].is_extended_real and exponent.is_integer:
|
| 653 |
+
if exponent.is_even:
|
| 654 |
+
return self.args[0]**exponent
|
| 655 |
+
elif exponent is not S.NegativeOne and exponent.is_Integer:
|
| 656 |
+
return self.args[0]**(exponent - 1)*self
|
| 657 |
+
return
|
| 658 |
+
|
| 659 |
+
def _eval_nseries(self, x, n, logx, cdir=0):
|
| 660 |
+
from sympy.functions.elementary.exponential import log
|
| 661 |
+
direction = self.args[0].leadterm(x)[0]
|
| 662 |
+
if direction.has(log(x)):
|
| 663 |
+
direction = direction.subs(log(x), logx)
|
| 664 |
+
s = self.args[0]._eval_nseries(x, n=n, logx=logx)
|
| 665 |
+
return (sign(direction)*s).expand()
|
| 666 |
+
|
| 667 |
+
def _eval_derivative(self, x):
|
| 668 |
+
if self.args[0].is_extended_real or self.args[0].is_imaginary:
|
| 669 |
+
return Derivative(self.args[0], x, evaluate=True) \
|
| 670 |
+
* sign(conjugate(self.args[0]))
|
| 671 |
+
rv = (re(self.args[0]) * Derivative(re(self.args[0]), x,
|
| 672 |
+
evaluate=True) + im(self.args[0]) * Derivative(im(self.args[0]),
|
| 673 |
+
x, evaluate=True)) / Abs(self.args[0])
|
| 674 |
+
return rv.rewrite(sign)
|
| 675 |
+
|
| 676 |
+
def _eval_rewrite_as_Heaviside(self, arg, **kwargs):
|
| 677 |
+
# Note this only holds for real arg (since Heaviside is not defined
|
| 678 |
+
# for complex arguments).
|
| 679 |
+
from sympy.functions.special.delta_functions import Heaviside
|
| 680 |
+
if arg.is_extended_real:
|
| 681 |
+
return arg*(Heaviside(arg) - Heaviside(-arg))
|
| 682 |
+
|
| 683 |
+
def _eval_rewrite_as_Piecewise(self, arg, **kwargs):
|
| 684 |
+
if arg.is_extended_real:
|
| 685 |
+
return Piecewise((arg, arg >= 0), (-arg, True))
|
| 686 |
+
elif arg.is_imaginary:
|
| 687 |
+
return Piecewise((I*arg, I*arg >= 0), (-I*arg, True))
|
| 688 |
+
|
| 689 |
+
def _eval_rewrite_as_sign(self, arg, **kwargs):
|
| 690 |
+
return arg/sign(arg)
|
| 691 |
+
|
| 692 |
+
def _eval_rewrite_as_conjugate(self, arg, **kwargs):
|
| 693 |
+
return sqrt(arg*conjugate(arg))
|
| 694 |
+
|
| 695 |
+
|
| 696 |
+
class arg(Function):
|
| 697 |
+
r"""
|
| 698 |
+
Returns the argument (in radians) of a complex number. The argument is
|
| 699 |
+
evaluated in consistent convention with ``atan2`` where the branch-cut is
|
| 700 |
+
taken along the negative real axis and ``arg(z)`` is in the interval
|
| 701 |
+
$(-\pi,\pi]$. For a positive number, the argument is always 0; the
|
| 702 |
+
argument of a negative number is $\pi$; and the argument of 0
|
| 703 |
+
is undefined and returns ``nan``. So the ``arg`` function will never nest
|
| 704 |
+
greater than 3 levels since at the 4th application, the result must be
|
| 705 |
+
nan; for a real number, nan is returned on the 3rd application.
|
| 706 |
+
|
| 707 |
+
Examples
|
| 708 |
+
========
|
| 709 |
+
|
| 710 |
+
>>> from sympy import arg, I, sqrt, Dummy
|
| 711 |
+
>>> from sympy.abc import x
|
| 712 |
+
>>> arg(2.0)
|
| 713 |
+
0
|
| 714 |
+
>>> arg(I)
|
| 715 |
+
pi/2
|
| 716 |
+
>>> arg(sqrt(2) + I*sqrt(2))
|
| 717 |
+
pi/4
|
| 718 |
+
>>> arg(sqrt(3)/2 + I/2)
|
| 719 |
+
pi/6
|
| 720 |
+
>>> arg(4 + 3*I)
|
| 721 |
+
atan(3/4)
|
| 722 |
+
>>> arg(0.8 + 0.6*I)
|
| 723 |
+
0.643501108793284
|
| 724 |
+
>>> arg(arg(arg(arg(x))))
|
| 725 |
+
nan
|
| 726 |
+
>>> real = Dummy(real=True)
|
| 727 |
+
>>> arg(arg(arg(real)))
|
| 728 |
+
nan
|
| 729 |
+
|
| 730 |
+
Parameters
|
| 731 |
+
==========
|
| 732 |
+
|
| 733 |
+
arg : Expr
|
| 734 |
+
Real or complex expression.
|
| 735 |
+
|
| 736 |
+
Returns
|
| 737 |
+
=======
|
| 738 |
+
|
| 739 |
+
value : Expr
|
| 740 |
+
Returns arc tangent of arg measured in radians.
|
| 741 |
+
|
| 742 |
+
"""
|
| 743 |
+
|
| 744 |
+
is_extended_real = True
|
| 745 |
+
is_real = True
|
| 746 |
+
is_finite = True
|
| 747 |
+
_singularities = True # non-holomorphic
|
| 748 |
+
|
| 749 |
+
@classmethod
|
| 750 |
+
def eval(cls, arg):
|
| 751 |
+
a = arg
|
| 752 |
+
for i in range(3):
|
| 753 |
+
if isinstance(a, cls):
|
| 754 |
+
a = a.args[0]
|
| 755 |
+
else:
|
| 756 |
+
if i == 2 and a.is_extended_real:
|
| 757 |
+
return S.NaN
|
| 758 |
+
break
|
| 759 |
+
else:
|
| 760 |
+
return S.NaN
|
| 761 |
+
from sympy.functions.elementary.exponential import exp, exp_polar
|
| 762 |
+
if isinstance(arg, exp_polar):
|
| 763 |
+
return periodic_argument(arg, oo)
|
| 764 |
+
elif isinstance(arg, exp):
|
| 765 |
+
i_ = im(arg.args[0])
|
| 766 |
+
if i_.is_comparable:
|
| 767 |
+
i_ %= 2*S.Pi
|
| 768 |
+
if i_ > S.Pi:
|
| 769 |
+
i_ -= 2*S.Pi
|
| 770 |
+
return i_
|
| 771 |
+
|
| 772 |
+
if not arg.is_Atom:
|
| 773 |
+
c, arg_ = factor_terms(arg).as_coeff_Mul()
|
| 774 |
+
if arg_.is_Mul:
|
| 775 |
+
arg_ = Mul(*[a if (sign(a) not in (-1, 1)) else
|
| 776 |
+
sign(a) for a in arg_.args])
|
| 777 |
+
arg_ = sign(c)*arg_
|
| 778 |
+
else:
|
| 779 |
+
arg_ = arg
|
| 780 |
+
if any(i.is_extended_positive is None for i in arg_.atoms(AppliedUndef)):
|
| 781 |
+
return
|
| 782 |
+
from sympy.functions.elementary.trigonometric import atan2
|
| 783 |
+
x, y = arg_.as_real_imag()
|
| 784 |
+
rv = atan2(y, x)
|
| 785 |
+
if rv.is_number:
|
| 786 |
+
return rv
|
| 787 |
+
if arg_ != arg:
|
| 788 |
+
return cls(arg_, evaluate=False)
|
| 789 |
+
|
| 790 |
+
def _eval_derivative(self, t):
|
| 791 |
+
x, y = self.args[0].as_real_imag()
|
| 792 |
+
return (x * Derivative(y, t, evaluate=True) - y *
|
| 793 |
+
Derivative(x, t, evaluate=True)) / (x**2 + y**2)
|
| 794 |
+
|
| 795 |
+
def _eval_rewrite_as_atan2(self, arg, **kwargs):
|
| 796 |
+
from sympy.functions.elementary.trigonometric import atan2
|
| 797 |
+
x, y = self.args[0].as_real_imag()
|
| 798 |
+
return atan2(y, x)
|
| 799 |
+
|
| 800 |
+
|
| 801 |
+
class conjugate(Function):
|
| 802 |
+
"""
|
| 803 |
+
Returns the *complex conjugate* [1]_ of an argument.
|
| 804 |
+
In mathematics, the complex conjugate of a complex number
|
| 805 |
+
is given by changing the sign of the imaginary part.
|
| 806 |
+
|
| 807 |
+
Thus, the conjugate of the complex number
|
| 808 |
+
:math:`a + ib` (where $a$ and $b$ are real numbers) is :math:`a - ib`
|
| 809 |
+
|
| 810 |
+
Examples
|
| 811 |
+
========
|
| 812 |
+
|
| 813 |
+
>>> from sympy import conjugate, I
|
| 814 |
+
>>> conjugate(2)
|
| 815 |
+
2
|
| 816 |
+
>>> conjugate(I)
|
| 817 |
+
-I
|
| 818 |
+
>>> conjugate(3 + 2*I)
|
| 819 |
+
3 - 2*I
|
| 820 |
+
>>> conjugate(5 - I)
|
| 821 |
+
5 + I
|
| 822 |
+
|
| 823 |
+
Parameters
|
| 824 |
+
==========
|
| 825 |
+
|
| 826 |
+
arg : Expr
|
| 827 |
+
Real or complex expression.
|
| 828 |
+
|
| 829 |
+
Returns
|
| 830 |
+
=======
|
| 831 |
+
|
| 832 |
+
arg : Expr
|
| 833 |
+
Complex conjugate of arg as real, imaginary or mixed expression.
|
| 834 |
+
|
| 835 |
+
See Also
|
| 836 |
+
========
|
| 837 |
+
|
| 838 |
+
sign, Abs
|
| 839 |
+
|
| 840 |
+
References
|
| 841 |
+
==========
|
| 842 |
+
|
| 843 |
+
.. [1] https://en.wikipedia.org/wiki/Complex_conjugation
|
| 844 |
+
"""
|
| 845 |
+
_singularities = True # non-holomorphic
|
| 846 |
+
|
| 847 |
+
@classmethod
|
| 848 |
+
def eval(cls, arg):
|
| 849 |
+
obj = arg._eval_conjugate()
|
| 850 |
+
if obj is not None:
|
| 851 |
+
return obj
|
| 852 |
+
|
| 853 |
+
def inverse(self):
|
| 854 |
+
return conjugate
|
| 855 |
+
|
| 856 |
+
def _eval_Abs(self):
|
| 857 |
+
return Abs(self.args[0], evaluate=True)
|
| 858 |
+
|
| 859 |
+
def _eval_adjoint(self):
|
| 860 |
+
return transpose(self.args[0])
|
| 861 |
+
|
| 862 |
+
def _eval_conjugate(self):
|
| 863 |
+
return self.args[0]
|
| 864 |
+
|
| 865 |
+
def _eval_derivative(self, x):
|
| 866 |
+
if x.is_real:
|
| 867 |
+
return conjugate(Derivative(self.args[0], x, evaluate=True))
|
| 868 |
+
elif x.is_imaginary:
|
| 869 |
+
return -conjugate(Derivative(self.args[0], x, evaluate=True))
|
| 870 |
+
|
| 871 |
+
def _eval_transpose(self):
|
| 872 |
+
return adjoint(self.args[0])
|
| 873 |
+
|
| 874 |
+
def _eval_is_algebraic(self):
|
| 875 |
+
return self.args[0].is_algebraic
|
| 876 |
+
|
| 877 |
+
|
| 878 |
+
class transpose(Function):
|
| 879 |
+
"""
|
| 880 |
+
Linear map transposition.
|
| 881 |
+
|
| 882 |
+
Examples
|
| 883 |
+
========
|
| 884 |
+
|
| 885 |
+
>>> from sympy import transpose, Matrix, MatrixSymbol
|
| 886 |
+
>>> A = MatrixSymbol('A', 25, 9)
|
| 887 |
+
>>> transpose(A)
|
| 888 |
+
A.T
|
| 889 |
+
>>> B = MatrixSymbol('B', 9, 22)
|
| 890 |
+
>>> transpose(B)
|
| 891 |
+
B.T
|
| 892 |
+
>>> transpose(A*B)
|
| 893 |
+
B.T*A.T
|
| 894 |
+
>>> M = Matrix([[4, 5], [2, 1], [90, 12]])
|
| 895 |
+
>>> M
|
| 896 |
+
Matrix([
|
| 897 |
+
[ 4, 5],
|
| 898 |
+
[ 2, 1],
|
| 899 |
+
[90, 12]])
|
| 900 |
+
>>> transpose(M)
|
| 901 |
+
Matrix([
|
| 902 |
+
[4, 2, 90],
|
| 903 |
+
[5, 1, 12]])
|
| 904 |
+
|
| 905 |
+
Parameters
|
| 906 |
+
==========
|
| 907 |
+
|
| 908 |
+
arg : Matrix
|
| 909 |
+
Matrix or matrix expression to take the transpose of.
|
| 910 |
+
|
| 911 |
+
Returns
|
| 912 |
+
=======
|
| 913 |
+
|
| 914 |
+
value : Matrix
|
| 915 |
+
Transpose of arg.
|
| 916 |
+
|
| 917 |
+
"""
|
| 918 |
+
|
| 919 |
+
@classmethod
|
| 920 |
+
def eval(cls, arg):
|
| 921 |
+
obj = arg._eval_transpose()
|
| 922 |
+
if obj is not None:
|
| 923 |
+
return obj
|
| 924 |
+
|
| 925 |
+
def _eval_adjoint(self):
|
| 926 |
+
return conjugate(self.args[0])
|
| 927 |
+
|
| 928 |
+
def _eval_conjugate(self):
|
| 929 |
+
return adjoint(self.args[0])
|
| 930 |
+
|
| 931 |
+
def _eval_transpose(self):
|
| 932 |
+
return self.args[0]
|
| 933 |
+
|
| 934 |
+
|
| 935 |
+
class adjoint(Function):
|
| 936 |
+
"""
|
| 937 |
+
Conjugate transpose or Hermite conjugation.
|
| 938 |
+
|
| 939 |
+
Examples
|
| 940 |
+
========
|
| 941 |
+
|
| 942 |
+
>>> from sympy import adjoint, MatrixSymbol
|
| 943 |
+
>>> A = MatrixSymbol('A', 10, 5)
|
| 944 |
+
>>> adjoint(A)
|
| 945 |
+
Adjoint(A)
|
| 946 |
+
|
| 947 |
+
Parameters
|
| 948 |
+
==========
|
| 949 |
+
|
| 950 |
+
arg : Matrix
|
| 951 |
+
Matrix or matrix expression to take the adjoint of.
|
| 952 |
+
|
| 953 |
+
Returns
|
| 954 |
+
=======
|
| 955 |
+
|
| 956 |
+
value : Matrix
|
| 957 |
+
Represents the conjugate transpose or Hermite
|
| 958 |
+
conjugation of arg.
|
| 959 |
+
|
| 960 |
+
"""
|
| 961 |
+
|
| 962 |
+
@classmethod
|
| 963 |
+
def eval(cls, arg):
|
| 964 |
+
obj = arg._eval_adjoint()
|
| 965 |
+
if obj is not None:
|
| 966 |
+
return obj
|
| 967 |
+
obj = arg._eval_transpose()
|
| 968 |
+
if obj is not None:
|
| 969 |
+
return conjugate(obj)
|
| 970 |
+
|
| 971 |
+
def _eval_adjoint(self):
|
| 972 |
+
return self.args[0]
|
| 973 |
+
|
| 974 |
+
def _eval_conjugate(self):
|
| 975 |
+
return transpose(self.args[0])
|
| 976 |
+
|
| 977 |
+
def _eval_transpose(self):
|
| 978 |
+
return conjugate(self.args[0])
|
| 979 |
+
|
| 980 |
+
def _latex(self, printer, exp=None, *args):
|
| 981 |
+
arg = printer._print(self.args[0])
|
| 982 |
+
tex = r'%s^{\dagger}' % arg
|
| 983 |
+
if exp:
|
| 984 |
+
tex = r'\left(%s\right)^{%s}' % (tex, exp)
|
| 985 |
+
return tex
|
| 986 |
+
|
| 987 |
+
def _pretty(self, printer, *args):
|
| 988 |
+
from sympy.printing.pretty.stringpict import prettyForm
|
| 989 |
+
pform = printer._print(self.args[0], *args)
|
| 990 |
+
if printer._use_unicode:
|
| 991 |
+
pform = pform**prettyForm('\N{DAGGER}')
|
| 992 |
+
else:
|
| 993 |
+
pform = pform**prettyForm('+')
|
| 994 |
+
return pform
|
| 995 |
+
|
| 996 |
+
###############################################################################
|
| 997 |
+
############### HANDLING OF POLAR NUMBERS #####################################
|
| 998 |
+
###############################################################################
|
| 999 |
+
|
| 1000 |
+
|
| 1001 |
+
class polar_lift(Function):
|
| 1002 |
+
"""
|
| 1003 |
+
Lift argument to the Riemann surface of the logarithm, using the
|
| 1004 |
+
standard branch.
|
| 1005 |
+
|
| 1006 |
+
Examples
|
| 1007 |
+
========
|
| 1008 |
+
|
| 1009 |
+
>>> from sympy import Symbol, polar_lift, I
|
| 1010 |
+
>>> p = Symbol('p', polar=True)
|
| 1011 |
+
>>> x = Symbol('x')
|
| 1012 |
+
>>> polar_lift(4)
|
| 1013 |
+
4*exp_polar(0)
|
| 1014 |
+
>>> polar_lift(-4)
|
| 1015 |
+
4*exp_polar(I*pi)
|
| 1016 |
+
>>> polar_lift(-I)
|
| 1017 |
+
exp_polar(-I*pi/2)
|
| 1018 |
+
>>> polar_lift(I + 2)
|
| 1019 |
+
polar_lift(2 + I)
|
| 1020 |
+
|
| 1021 |
+
>>> polar_lift(4*x)
|
| 1022 |
+
4*polar_lift(x)
|
| 1023 |
+
>>> polar_lift(4*p)
|
| 1024 |
+
4*p
|
| 1025 |
+
|
| 1026 |
+
Parameters
|
| 1027 |
+
==========
|
| 1028 |
+
|
| 1029 |
+
arg : Expr
|
| 1030 |
+
Real or complex expression.
|
| 1031 |
+
|
| 1032 |
+
See Also
|
| 1033 |
+
========
|
| 1034 |
+
|
| 1035 |
+
sympy.functions.elementary.exponential.exp_polar
|
| 1036 |
+
periodic_argument
|
| 1037 |
+
"""
|
| 1038 |
+
|
| 1039 |
+
is_polar = True
|
| 1040 |
+
is_comparable = False # Cannot be evalf'd.
|
| 1041 |
+
|
| 1042 |
+
@classmethod
|
| 1043 |
+
def eval(cls, arg):
|
| 1044 |
+
from sympy.functions.elementary.complexes import arg as argument
|
| 1045 |
+
if arg.is_number:
|
| 1046 |
+
ar = argument(arg)
|
| 1047 |
+
# In general we want to affirm that something is known,
|
| 1048 |
+
# e.g. `not ar.has(argument) and not ar.has(atan)`
|
| 1049 |
+
# but for now we will just be more restrictive and
|
| 1050 |
+
# see that it has evaluated to one of the known values.
|
| 1051 |
+
if ar in (0, pi/2, -pi/2, pi):
|
| 1052 |
+
from sympy.functions.elementary.exponential import exp_polar
|
| 1053 |
+
return exp_polar(I*ar)*abs(arg)
|
| 1054 |
+
|
| 1055 |
+
if arg.is_Mul:
|
| 1056 |
+
args = arg.args
|
| 1057 |
+
else:
|
| 1058 |
+
args = [arg]
|
| 1059 |
+
included = []
|
| 1060 |
+
excluded = []
|
| 1061 |
+
positive = []
|
| 1062 |
+
for arg in args:
|
| 1063 |
+
if arg.is_polar:
|
| 1064 |
+
included += [arg]
|
| 1065 |
+
elif arg.is_positive:
|
| 1066 |
+
positive += [arg]
|
| 1067 |
+
else:
|
| 1068 |
+
excluded += [arg]
|
| 1069 |
+
if len(excluded) < len(args):
|
| 1070 |
+
if excluded:
|
| 1071 |
+
return Mul(*(included + positive))*polar_lift(Mul(*excluded))
|
| 1072 |
+
elif included:
|
| 1073 |
+
return Mul(*(included + positive))
|
| 1074 |
+
else:
|
| 1075 |
+
from sympy.functions.elementary.exponential import exp_polar
|
| 1076 |
+
return Mul(*positive)*exp_polar(0)
|
| 1077 |
+
|
| 1078 |
+
def _eval_evalf(self, prec):
|
| 1079 |
+
""" Careful! any evalf of polar numbers is flaky """
|
| 1080 |
+
return self.args[0]._eval_evalf(prec)
|
| 1081 |
+
|
| 1082 |
+
def _eval_Abs(self):
|
| 1083 |
+
return Abs(self.args[0], evaluate=True)
|
| 1084 |
+
|
| 1085 |
+
|
| 1086 |
+
class periodic_argument(Function):
|
| 1087 |
+
r"""
|
| 1088 |
+
Represent the argument on a quotient of the Riemann surface of the
|
| 1089 |
+
logarithm. That is, given a period $P$, always return a value in
|
| 1090 |
+
$(-P/2, P/2]$, by using $\exp(PI) = 1$.
|
| 1091 |
+
|
| 1092 |
+
Examples
|
| 1093 |
+
========
|
| 1094 |
+
|
| 1095 |
+
>>> from sympy import exp_polar, periodic_argument
|
| 1096 |
+
>>> from sympy import I, pi
|
| 1097 |
+
>>> periodic_argument(exp_polar(10*I*pi), 2*pi)
|
| 1098 |
+
0
|
| 1099 |
+
>>> periodic_argument(exp_polar(5*I*pi), 4*pi)
|
| 1100 |
+
pi
|
| 1101 |
+
>>> from sympy import exp_polar, periodic_argument
|
| 1102 |
+
>>> from sympy import I, pi
|
| 1103 |
+
>>> periodic_argument(exp_polar(5*I*pi), 2*pi)
|
| 1104 |
+
pi
|
| 1105 |
+
>>> periodic_argument(exp_polar(5*I*pi), 3*pi)
|
| 1106 |
+
-pi
|
| 1107 |
+
>>> periodic_argument(exp_polar(5*I*pi), pi)
|
| 1108 |
+
0
|
| 1109 |
+
|
| 1110 |
+
Parameters
|
| 1111 |
+
==========
|
| 1112 |
+
|
| 1113 |
+
ar : Expr
|
| 1114 |
+
A polar number.
|
| 1115 |
+
|
| 1116 |
+
period : Expr
|
| 1117 |
+
The period $P$.
|
| 1118 |
+
|
| 1119 |
+
See Also
|
| 1120 |
+
========
|
| 1121 |
+
|
| 1122 |
+
sympy.functions.elementary.exponential.exp_polar
|
| 1123 |
+
polar_lift : Lift argument to the Riemann surface of the logarithm
|
| 1124 |
+
principal_branch
|
| 1125 |
+
"""
|
| 1126 |
+
|
| 1127 |
+
@classmethod
|
| 1128 |
+
def _getunbranched(cls, ar):
|
| 1129 |
+
from sympy.functions.elementary.exponential import exp_polar, log
|
| 1130 |
+
if ar.is_Mul:
|
| 1131 |
+
args = ar.args
|
| 1132 |
+
else:
|
| 1133 |
+
args = [ar]
|
| 1134 |
+
unbranched = 0
|
| 1135 |
+
for a in args:
|
| 1136 |
+
if not a.is_polar:
|
| 1137 |
+
unbranched += arg(a)
|
| 1138 |
+
elif isinstance(a, exp_polar):
|
| 1139 |
+
unbranched += a.exp.as_real_imag()[1]
|
| 1140 |
+
elif a.is_Pow:
|
| 1141 |
+
re, im = a.exp.as_real_imag()
|
| 1142 |
+
unbranched += re*unbranched_argument(
|
| 1143 |
+
a.base) + im*log(abs(a.base))
|
| 1144 |
+
elif isinstance(a, polar_lift):
|
| 1145 |
+
unbranched += arg(a.args[0])
|
| 1146 |
+
else:
|
| 1147 |
+
return None
|
| 1148 |
+
return unbranched
|
| 1149 |
+
|
| 1150 |
+
@classmethod
|
| 1151 |
+
def eval(cls, ar, period):
|
| 1152 |
+
# Our strategy is to evaluate the argument on the Riemann surface of the
|
| 1153 |
+
# logarithm, and then reduce.
|
| 1154 |
+
# NOTE evidently this means it is a rather bad idea to use this with
|
| 1155 |
+
# period != 2*pi and non-polar numbers.
|
| 1156 |
+
if not period.is_extended_positive:
|
| 1157 |
+
return None
|
| 1158 |
+
if period == oo and isinstance(ar, principal_branch):
|
| 1159 |
+
return periodic_argument(*ar.args)
|
| 1160 |
+
if isinstance(ar, polar_lift) and period >= 2*pi:
|
| 1161 |
+
return periodic_argument(ar.args[0], period)
|
| 1162 |
+
if ar.is_Mul:
|
| 1163 |
+
newargs = [x for x in ar.args if not x.is_positive]
|
| 1164 |
+
if len(newargs) != len(ar.args):
|
| 1165 |
+
return periodic_argument(Mul(*newargs), period)
|
| 1166 |
+
unbranched = cls._getunbranched(ar)
|
| 1167 |
+
if unbranched is None:
|
| 1168 |
+
return None
|
| 1169 |
+
from sympy.functions.elementary.trigonometric import atan, atan2
|
| 1170 |
+
if unbranched.has(periodic_argument, atan2, atan):
|
| 1171 |
+
return None
|
| 1172 |
+
if period == oo:
|
| 1173 |
+
return unbranched
|
| 1174 |
+
if period != oo:
|
| 1175 |
+
from sympy.functions.elementary.integers import ceiling
|
| 1176 |
+
n = ceiling(unbranched/period - S.Half)*period
|
| 1177 |
+
if not n.has(ceiling):
|
| 1178 |
+
return unbranched - n
|
| 1179 |
+
|
| 1180 |
+
def _eval_evalf(self, prec):
|
| 1181 |
+
z, period = self.args
|
| 1182 |
+
if period == oo:
|
| 1183 |
+
unbranched = periodic_argument._getunbranched(z)
|
| 1184 |
+
if unbranched is None:
|
| 1185 |
+
return self
|
| 1186 |
+
return unbranched._eval_evalf(prec)
|
| 1187 |
+
ub = periodic_argument(z, oo)._eval_evalf(prec)
|
| 1188 |
+
from sympy.functions.elementary.integers import ceiling
|
| 1189 |
+
return (ub - ceiling(ub/period - S.Half)*period)._eval_evalf(prec)
|
| 1190 |
+
|
| 1191 |
+
|
| 1192 |
+
def unbranched_argument(arg):
|
| 1193 |
+
'''
|
| 1194 |
+
Returns periodic argument of arg with period as infinity.
|
| 1195 |
+
|
| 1196 |
+
Examples
|
| 1197 |
+
========
|
| 1198 |
+
|
| 1199 |
+
>>> from sympy import exp_polar, unbranched_argument
|
| 1200 |
+
>>> from sympy import I, pi
|
| 1201 |
+
>>> unbranched_argument(exp_polar(15*I*pi))
|
| 1202 |
+
15*pi
|
| 1203 |
+
>>> unbranched_argument(exp_polar(7*I*pi))
|
| 1204 |
+
7*pi
|
| 1205 |
+
|
| 1206 |
+
See also
|
| 1207 |
+
========
|
| 1208 |
+
|
| 1209 |
+
periodic_argument
|
| 1210 |
+
'''
|
| 1211 |
+
return periodic_argument(arg, oo)
|
| 1212 |
+
|
| 1213 |
+
|
| 1214 |
+
class principal_branch(Function):
|
| 1215 |
+
"""
|
| 1216 |
+
Represent a polar number reduced to its principal branch on a quotient
|
| 1217 |
+
of the Riemann surface of the logarithm.
|
| 1218 |
+
|
| 1219 |
+
Explanation
|
| 1220 |
+
===========
|
| 1221 |
+
|
| 1222 |
+
This is a function of two arguments. The first argument is a polar
|
| 1223 |
+
number `z`, and the second one a positive real number or infinity, `p`.
|
| 1224 |
+
The result is ``z mod exp_polar(I*p)``.
|
| 1225 |
+
|
| 1226 |
+
Examples
|
| 1227 |
+
========
|
| 1228 |
+
|
| 1229 |
+
>>> from sympy import exp_polar, principal_branch, oo, I, pi
|
| 1230 |
+
>>> from sympy.abc import z
|
| 1231 |
+
>>> principal_branch(z, oo)
|
| 1232 |
+
z
|
| 1233 |
+
>>> principal_branch(exp_polar(2*pi*I)*3, 2*pi)
|
| 1234 |
+
3*exp_polar(0)
|
| 1235 |
+
>>> principal_branch(exp_polar(2*pi*I)*3*z, 2*pi)
|
| 1236 |
+
3*principal_branch(z, 2*pi)
|
| 1237 |
+
|
| 1238 |
+
Parameters
|
| 1239 |
+
==========
|
| 1240 |
+
|
| 1241 |
+
x : Expr
|
| 1242 |
+
A polar number.
|
| 1243 |
+
|
| 1244 |
+
period : Expr
|
| 1245 |
+
Positive real number or infinity.
|
| 1246 |
+
|
| 1247 |
+
See Also
|
| 1248 |
+
========
|
| 1249 |
+
|
| 1250 |
+
sympy.functions.elementary.exponential.exp_polar
|
| 1251 |
+
polar_lift : Lift argument to the Riemann surface of the logarithm
|
| 1252 |
+
periodic_argument
|
| 1253 |
+
"""
|
| 1254 |
+
|
| 1255 |
+
is_polar = True
|
| 1256 |
+
is_comparable = False # cannot always be evalf'd
|
| 1257 |
+
|
| 1258 |
+
@classmethod
|
| 1259 |
+
def eval(self, x, period):
|
| 1260 |
+
from sympy.functions.elementary.exponential import exp_polar
|
| 1261 |
+
if isinstance(x, polar_lift):
|
| 1262 |
+
return principal_branch(x.args[0], period)
|
| 1263 |
+
if period == oo:
|
| 1264 |
+
return x
|
| 1265 |
+
ub = periodic_argument(x, oo)
|
| 1266 |
+
barg = periodic_argument(x, period)
|
| 1267 |
+
if ub != barg and not ub.has(periodic_argument) \
|
| 1268 |
+
and not barg.has(periodic_argument):
|
| 1269 |
+
pl = polar_lift(x)
|
| 1270 |
+
|
| 1271 |
+
def mr(expr):
|
| 1272 |
+
if not isinstance(expr, Symbol):
|
| 1273 |
+
return polar_lift(expr)
|
| 1274 |
+
return expr
|
| 1275 |
+
pl = pl.replace(polar_lift, mr)
|
| 1276 |
+
# Recompute unbranched argument
|
| 1277 |
+
ub = periodic_argument(pl, oo)
|
| 1278 |
+
if not pl.has(polar_lift):
|
| 1279 |
+
if ub != barg:
|
| 1280 |
+
res = exp_polar(I*(barg - ub))*pl
|
| 1281 |
+
else:
|
| 1282 |
+
res = pl
|
| 1283 |
+
if not res.is_polar and not res.has(exp_polar):
|
| 1284 |
+
res *= exp_polar(0)
|
| 1285 |
+
return res
|
| 1286 |
+
|
| 1287 |
+
if not x.free_symbols:
|
| 1288 |
+
c, m = x, ()
|
| 1289 |
+
else:
|
| 1290 |
+
c, m = x.as_coeff_mul(*x.free_symbols)
|
| 1291 |
+
others = []
|
| 1292 |
+
for y in m:
|
| 1293 |
+
if y.is_positive:
|
| 1294 |
+
c *= y
|
| 1295 |
+
else:
|
| 1296 |
+
others += [y]
|
| 1297 |
+
m = tuple(others)
|
| 1298 |
+
arg = periodic_argument(c, period)
|
| 1299 |
+
if arg.has(periodic_argument):
|
| 1300 |
+
return None
|
| 1301 |
+
if arg.is_number and (unbranched_argument(c) != arg or
|
| 1302 |
+
(arg == 0 and m != () and c != 1)):
|
| 1303 |
+
if arg == 0:
|
| 1304 |
+
return abs(c)*principal_branch(Mul(*m), period)
|
| 1305 |
+
return principal_branch(exp_polar(I*arg)*Mul(*m), period)*abs(c)
|
| 1306 |
+
if arg.is_number and ((abs(arg) < period/2) == True or arg == period/2) \
|
| 1307 |
+
and m == ():
|
| 1308 |
+
return exp_polar(arg*I)*abs(c)
|
| 1309 |
+
|
| 1310 |
+
def _eval_evalf(self, prec):
|
| 1311 |
+
z, period = self.args
|
| 1312 |
+
p = periodic_argument(z, period)._eval_evalf(prec)
|
| 1313 |
+
if abs(p) > pi or p == -pi:
|
| 1314 |
+
return self # Cannot evalf for this argument.
|
| 1315 |
+
from sympy.functions.elementary.exponential import exp
|
| 1316 |
+
return (abs(z)*exp(I*p))._eval_evalf(prec)
|
| 1317 |
+
|
| 1318 |
+
|
| 1319 |
+
def _polarify(eq, lift, pause=False):
|
| 1320 |
+
from sympy.integrals.integrals import Integral
|
| 1321 |
+
if eq.is_polar:
|
| 1322 |
+
return eq
|
| 1323 |
+
if eq.is_number and not pause:
|
| 1324 |
+
return polar_lift(eq)
|
| 1325 |
+
if isinstance(eq, Symbol) and not pause and lift:
|
| 1326 |
+
return polar_lift(eq)
|
| 1327 |
+
elif eq.is_Atom:
|
| 1328 |
+
return eq
|
| 1329 |
+
elif eq.is_Add:
|
| 1330 |
+
r = eq.func(*[_polarify(arg, lift, pause=True) for arg in eq.args])
|
| 1331 |
+
if lift:
|
| 1332 |
+
return polar_lift(r)
|
| 1333 |
+
return r
|
| 1334 |
+
elif eq.is_Pow and eq.base == S.Exp1:
|
| 1335 |
+
return eq.func(S.Exp1, _polarify(eq.exp, lift, pause=False))
|
| 1336 |
+
elif eq.is_Function:
|
| 1337 |
+
return eq.func(*[_polarify(arg, lift, pause=False) for arg in eq.args])
|
| 1338 |
+
elif isinstance(eq, Integral):
|
| 1339 |
+
# Don't lift the integration variable
|
| 1340 |
+
func = _polarify(eq.function, lift, pause=pause)
|
| 1341 |
+
limits = []
|
| 1342 |
+
for limit in eq.args[1:]:
|
| 1343 |
+
var = _polarify(limit[0], lift=False, pause=pause)
|
| 1344 |
+
rest = _polarify(limit[1:], lift=lift, pause=pause)
|
| 1345 |
+
limits.append((var,) + rest)
|
| 1346 |
+
return Integral(*((func,) + tuple(limits)))
|
| 1347 |
+
else:
|
| 1348 |
+
return eq.func(*[_polarify(arg, lift, pause=pause)
|
| 1349 |
+
if isinstance(arg, Expr) else arg for arg in eq.args])
|
| 1350 |
+
|
| 1351 |
+
|
| 1352 |
+
def polarify(eq, subs=True, lift=False):
|
| 1353 |
+
"""
|
| 1354 |
+
Turn all numbers in eq into their polar equivalents (under the standard
|
| 1355 |
+
choice of argument).
|
| 1356 |
+
|
| 1357 |
+
Note that no attempt is made to guess a formal convention of adding
|
| 1358 |
+
polar numbers, expressions like $1 + x$ will generally not be altered.
|
| 1359 |
+
|
| 1360 |
+
Note also that this function does not promote ``exp(x)`` to ``exp_polar(x)``.
|
| 1361 |
+
|
| 1362 |
+
If ``subs`` is ``True``, all symbols which are not already polar will be
|
| 1363 |
+
substituted for polar dummies; in this case the function behaves much
|
| 1364 |
+
like :func:`~.posify`.
|
| 1365 |
+
|
| 1366 |
+
If ``lift`` is ``True``, both addition statements and non-polar symbols are
|
| 1367 |
+
changed to their ``polar_lift()``ed versions.
|
| 1368 |
+
Note that ``lift=True`` implies ``subs=False``.
|
| 1369 |
+
|
| 1370 |
+
Examples
|
| 1371 |
+
========
|
| 1372 |
+
|
| 1373 |
+
>>> from sympy import polarify, sin, I
|
| 1374 |
+
>>> from sympy.abc import x, y
|
| 1375 |
+
>>> expr = (-x)**y
|
| 1376 |
+
>>> expr.expand()
|
| 1377 |
+
(-x)**y
|
| 1378 |
+
>>> polarify(expr)
|
| 1379 |
+
((_x*exp_polar(I*pi))**_y, {_x: x, _y: y})
|
| 1380 |
+
>>> polarify(expr)[0].expand()
|
| 1381 |
+
_x**_y*exp_polar(_y*I*pi)
|
| 1382 |
+
>>> polarify(x, lift=True)
|
| 1383 |
+
polar_lift(x)
|
| 1384 |
+
>>> polarify(x*(1+y), lift=True)
|
| 1385 |
+
polar_lift(x)*polar_lift(y + 1)
|
| 1386 |
+
|
| 1387 |
+
Adds are treated carefully:
|
| 1388 |
+
|
| 1389 |
+
>>> polarify(1 + sin((1 + I)*x))
|
| 1390 |
+
(sin(_x*polar_lift(1 + I)) + 1, {_x: x})
|
| 1391 |
+
"""
|
| 1392 |
+
if lift:
|
| 1393 |
+
subs = False
|
| 1394 |
+
eq = _polarify(sympify(eq), lift)
|
| 1395 |
+
if not subs:
|
| 1396 |
+
return eq
|
| 1397 |
+
reps = {s: Dummy(s.name, polar=True) for s in eq.free_symbols}
|
| 1398 |
+
eq = eq.subs(reps)
|
| 1399 |
+
return eq, {r: s for s, r in reps.items()}
|
| 1400 |
+
|
| 1401 |
+
|
| 1402 |
+
def _unpolarify(eq, exponents_only, pause=False):
|
| 1403 |
+
if not isinstance(eq, Basic) or eq.is_Atom:
|
| 1404 |
+
return eq
|
| 1405 |
+
|
| 1406 |
+
if not pause:
|
| 1407 |
+
from sympy.functions.elementary.exponential import exp, exp_polar
|
| 1408 |
+
if isinstance(eq, exp_polar):
|
| 1409 |
+
return exp(_unpolarify(eq.exp, exponents_only))
|
| 1410 |
+
if isinstance(eq, principal_branch) and eq.args[1] == 2*pi:
|
| 1411 |
+
return _unpolarify(eq.args[0], exponents_only)
|
| 1412 |
+
if (
|
| 1413 |
+
eq.is_Add or eq.is_Mul or eq.is_Boolean or
|
| 1414 |
+
eq.is_Relational and (
|
| 1415 |
+
eq.rel_op in ('==', '!=') and 0 in eq.args or
|
| 1416 |
+
eq.rel_op not in ('==', '!='))
|
| 1417 |
+
):
|
| 1418 |
+
return eq.func(*[_unpolarify(x, exponents_only) for x in eq.args])
|
| 1419 |
+
if isinstance(eq, polar_lift):
|
| 1420 |
+
return _unpolarify(eq.args[0], exponents_only)
|
| 1421 |
+
|
| 1422 |
+
if eq.is_Pow:
|
| 1423 |
+
expo = _unpolarify(eq.exp, exponents_only)
|
| 1424 |
+
base = _unpolarify(eq.base, exponents_only,
|
| 1425 |
+
not (expo.is_integer and not pause))
|
| 1426 |
+
return base**expo
|
| 1427 |
+
|
| 1428 |
+
if eq.is_Function and getattr(eq.func, 'unbranched', False):
|
| 1429 |
+
return eq.func(*[_unpolarify(x, exponents_only, exponents_only)
|
| 1430 |
+
for x in eq.args])
|
| 1431 |
+
|
| 1432 |
+
return eq.func(*[_unpolarify(x, exponents_only, True) for x in eq.args])
|
| 1433 |
+
|
| 1434 |
+
|
| 1435 |
+
def unpolarify(eq, subs=None, exponents_only=False):
|
| 1436 |
+
"""
|
| 1437 |
+
If `p` denotes the projection from the Riemann surface of the logarithm to
|
| 1438 |
+
the complex line, return a simplified version `eq'` of `eq` such that
|
| 1439 |
+
`p(eq') = p(eq)`.
|
| 1440 |
+
Also apply the substitution subs in the end. (This is a convenience, since
|
| 1441 |
+
``unpolarify``, in a certain sense, undoes :func:`polarify`.)
|
| 1442 |
+
|
| 1443 |
+
Examples
|
| 1444 |
+
========
|
| 1445 |
+
|
| 1446 |
+
>>> from sympy import unpolarify, polar_lift, sin, I
|
| 1447 |
+
>>> unpolarify(polar_lift(I + 2))
|
| 1448 |
+
2 + I
|
| 1449 |
+
>>> unpolarify(sin(polar_lift(I + 7)))
|
| 1450 |
+
sin(7 + I)
|
| 1451 |
+
"""
|
| 1452 |
+
if isinstance(eq, bool):
|
| 1453 |
+
return eq
|
| 1454 |
+
|
| 1455 |
+
eq = sympify(eq)
|
| 1456 |
+
if subs is not None:
|
| 1457 |
+
return unpolarify(eq.subs(subs))
|
| 1458 |
+
changed = True
|
| 1459 |
+
pause = False
|
| 1460 |
+
if exponents_only:
|
| 1461 |
+
pause = True
|
| 1462 |
+
while changed:
|
| 1463 |
+
changed = False
|
| 1464 |
+
res = _unpolarify(eq, exponents_only, pause)
|
| 1465 |
+
if res != eq:
|
| 1466 |
+
changed = True
|
| 1467 |
+
eq = res
|
| 1468 |
+
if isinstance(res, bool):
|
| 1469 |
+
return res
|
| 1470 |
+
# Finally, replacing Exp(0) by 1 is always correct.
|
| 1471 |
+
# So is polar_lift(0) -> 0.
|
| 1472 |
+
from sympy.functions.elementary.exponential import exp_polar
|
| 1473 |
+
return res.subs({exp_polar(0): 1, polar_lift(0): 0})
|
parrot/lib/python3.10/site-packages/sympy/functions/elementary/hyperbolic.py
ADDED
|
@@ -0,0 +1,2285 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import S, sympify, cacheit
|
| 2 |
+
from sympy.core.add import Add
|
| 3 |
+
from sympy.core.function import Function, ArgumentIndexError
|
| 4 |
+
from sympy.core.logic import fuzzy_or, fuzzy_and, fuzzy_not, FuzzyBool
|
| 5 |
+
from sympy.core.numbers import I, pi, Rational
|
| 6 |
+
from sympy.core.symbol import Dummy
|
| 7 |
+
from sympy.functions.combinatorial.factorials import (binomial, factorial,
|
| 8 |
+
RisingFactorial)
|
| 9 |
+
from sympy.functions.combinatorial.numbers import bernoulli, euler, nC
|
| 10 |
+
from sympy.functions.elementary.complexes import Abs, im, re
|
| 11 |
+
from sympy.functions.elementary.exponential import exp, log, match_real_imag
|
| 12 |
+
from sympy.functions.elementary.integers import floor
|
| 13 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 14 |
+
from sympy.functions.elementary.trigonometric import (
|
| 15 |
+
acos, acot, asin, atan, cos, cot, csc, sec, sin, tan,
|
| 16 |
+
_imaginary_unit_as_coefficient)
|
| 17 |
+
from sympy.polys.specialpolys import symmetric_poly
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def _rewrite_hyperbolics_as_exp(expr):
|
| 21 |
+
return expr.xreplace({h: h.rewrite(exp)
|
| 22 |
+
for h in expr.atoms(HyperbolicFunction)})
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@cacheit
|
| 26 |
+
def _acosh_table():
|
| 27 |
+
return {
|
| 28 |
+
I: log(I*(1 + sqrt(2))),
|
| 29 |
+
-I: log(-I*(1 + sqrt(2))),
|
| 30 |
+
S.Half: pi/3,
|
| 31 |
+
Rational(-1, 2): pi*Rational(2, 3),
|
| 32 |
+
sqrt(2)/2: pi/4,
|
| 33 |
+
-sqrt(2)/2: pi*Rational(3, 4),
|
| 34 |
+
1/sqrt(2): pi/4,
|
| 35 |
+
-1/sqrt(2): pi*Rational(3, 4),
|
| 36 |
+
sqrt(3)/2: pi/6,
|
| 37 |
+
-sqrt(3)/2: pi*Rational(5, 6),
|
| 38 |
+
(sqrt(3) - 1)/sqrt(2**3): pi*Rational(5, 12),
|
| 39 |
+
-(sqrt(3) - 1)/sqrt(2**3): pi*Rational(7, 12),
|
| 40 |
+
sqrt(2 + sqrt(2))/2: pi/8,
|
| 41 |
+
-sqrt(2 + sqrt(2))/2: pi*Rational(7, 8),
|
| 42 |
+
sqrt(2 - sqrt(2))/2: pi*Rational(3, 8),
|
| 43 |
+
-sqrt(2 - sqrt(2))/2: pi*Rational(5, 8),
|
| 44 |
+
(1 + sqrt(3))/(2*sqrt(2)): pi/12,
|
| 45 |
+
-(1 + sqrt(3))/(2*sqrt(2)): pi*Rational(11, 12),
|
| 46 |
+
(sqrt(5) + 1)/4: pi/5,
|
| 47 |
+
-(sqrt(5) + 1)/4: pi*Rational(4, 5)
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@cacheit
|
| 52 |
+
def _acsch_table():
|
| 53 |
+
return {
|
| 54 |
+
I: -pi / 2,
|
| 55 |
+
I*(sqrt(2) + sqrt(6)): -pi / 12,
|
| 56 |
+
I*(1 + sqrt(5)): -pi / 10,
|
| 57 |
+
I*2 / sqrt(2 - sqrt(2)): -pi / 8,
|
| 58 |
+
I*2: -pi / 6,
|
| 59 |
+
I*sqrt(2 + 2/sqrt(5)): -pi / 5,
|
| 60 |
+
I*sqrt(2): -pi / 4,
|
| 61 |
+
I*(sqrt(5)-1): -3*pi / 10,
|
| 62 |
+
I*2 / sqrt(3): -pi / 3,
|
| 63 |
+
I*2 / sqrt(2 + sqrt(2)): -3*pi / 8,
|
| 64 |
+
I*sqrt(2 - 2/sqrt(5)): -2*pi / 5,
|
| 65 |
+
I*(sqrt(6) - sqrt(2)): -5*pi / 12,
|
| 66 |
+
S(2): -I*log((1+sqrt(5))/2),
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
@cacheit
|
| 71 |
+
def _asech_table():
|
| 72 |
+
return {
|
| 73 |
+
I: - (pi*I / 2) + log(1 + sqrt(2)),
|
| 74 |
+
-I: (pi*I / 2) + log(1 + sqrt(2)),
|
| 75 |
+
(sqrt(6) - sqrt(2)): pi / 12,
|
| 76 |
+
(sqrt(2) - sqrt(6)): 11*pi / 12,
|
| 77 |
+
sqrt(2 - 2/sqrt(5)): pi / 10,
|
| 78 |
+
-sqrt(2 - 2/sqrt(5)): 9*pi / 10,
|
| 79 |
+
2 / sqrt(2 + sqrt(2)): pi / 8,
|
| 80 |
+
-2 / sqrt(2 + sqrt(2)): 7*pi / 8,
|
| 81 |
+
2 / sqrt(3): pi / 6,
|
| 82 |
+
-2 / sqrt(3): 5*pi / 6,
|
| 83 |
+
(sqrt(5) - 1): pi / 5,
|
| 84 |
+
(1 - sqrt(5)): 4*pi / 5,
|
| 85 |
+
sqrt(2): pi / 4,
|
| 86 |
+
-sqrt(2): 3*pi / 4,
|
| 87 |
+
sqrt(2 + 2/sqrt(5)): 3*pi / 10,
|
| 88 |
+
-sqrt(2 + 2/sqrt(5)): 7*pi / 10,
|
| 89 |
+
S(2): pi / 3,
|
| 90 |
+
-S(2): 2*pi / 3,
|
| 91 |
+
sqrt(2*(2 + sqrt(2))): 3*pi / 8,
|
| 92 |
+
-sqrt(2*(2 + sqrt(2))): 5*pi / 8,
|
| 93 |
+
(1 + sqrt(5)): 2*pi / 5,
|
| 94 |
+
(-1 - sqrt(5)): 3*pi / 5,
|
| 95 |
+
(sqrt(6) + sqrt(2)): 5*pi / 12,
|
| 96 |
+
(-sqrt(6) - sqrt(2)): 7*pi / 12,
|
| 97 |
+
I*S.Infinity: -pi*I / 2,
|
| 98 |
+
I*S.NegativeInfinity: pi*I / 2,
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
###############################################################################
|
| 102 |
+
########################### HYPERBOLIC FUNCTIONS ##############################
|
| 103 |
+
###############################################################################
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
class HyperbolicFunction(Function):
|
| 107 |
+
"""
|
| 108 |
+
Base class for hyperbolic functions.
|
| 109 |
+
|
| 110 |
+
See Also
|
| 111 |
+
========
|
| 112 |
+
|
| 113 |
+
sinh, cosh, tanh, coth
|
| 114 |
+
"""
|
| 115 |
+
|
| 116 |
+
unbranched = True
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def _peeloff_ipi(arg):
|
| 120 |
+
r"""
|
| 121 |
+
Split ARG into two parts, a "rest" and a multiple of $I\pi$.
|
| 122 |
+
This assumes ARG to be an ``Add``.
|
| 123 |
+
The multiple of $I\pi$ returned in the second position is always a ``Rational``.
|
| 124 |
+
|
| 125 |
+
Examples
|
| 126 |
+
========
|
| 127 |
+
|
| 128 |
+
>>> from sympy.functions.elementary.hyperbolic import _peeloff_ipi as peel
|
| 129 |
+
>>> from sympy import pi, I
|
| 130 |
+
>>> from sympy.abc import x, y
|
| 131 |
+
>>> peel(x + I*pi/2)
|
| 132 |
+
(x, 1/2)
|
| 133 |
+
>>> peel(x + I*2*pi/3 + I*pi*y)
|
| 134 |
+
(x + I*pi*y + I*pi/6, 1/2)
|
| 135 |
+
"""
|
| 136 |
+
ipi = pi*I
|
| 137 |
+
for a in Add.make_args(arg):
|
| 138 |
+
if a == ipi:
|
| 139 |
+
K = S.One
|
| 140 |
+
break
|
| 141 |
+
elif a.is_Mul:
|
| 142 |
+
K, p = a.as_two_terms()
|
| 143 |
+
if p == ipi and K.is_Rational:
|
| 144 |
+
break
|
| 145 |
+
else:
|
| 146 |
+
return arg, S.Zero
|
| 147 |
+
|
| 148 |
+
m1 = (K % S.Half)
|
| 149 |
+
m2 = K - m1
|
| 150 |
+
return arg - m2*ipi, m2
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
class sinh(HyperbolicFunction):
|
| 154 |
+
r"""
|
| 155 |
+
``sinh(x)`` is the hyperbolic sine of ``x``.
|
| 156 |
+
|
| 157 |
+
The hyperbolic sine function is $\frac{e^x - e^{-x}}{2}$.
|
| 158 |
+
|
| 159 |
+
Examples
|
| 160 |
+
========
|
| 161 |
+
|
| 162 |
+
>>> from sympy import sinh
|
| 163 |
+
>>> from sympy.abc import x
|
| 164 |
+
>>> sinh(x)
|
| 165 |
+
sinh(x)
|
| 166 |
+
|
| 167 |
+
See Also
|
| 168 |
+
========
|
| 169 |
+
|
| 170 |
+
cosh, tanh, asinh
|
| 171 |
+
"""
|
| 172 |
+
|
| 173 |
+
def fdiff(self, argindex=1):
|
| 174 |
+
"""
|
| 175 |
+
Returns the first derivative of this function.
|
| 176 |
+
"""
|
| 177 |
+
if argindex == 1:
|
| 178 |
+
return cosh(self.args[0])
|
| 179 |
+
else:
|
| 180 |
+
raise ArgumentIndexError(self, argindex)
|
| 181 |
+
|
| 182 |
+
def inverse(self, argindex=1):
|
| 183 |
+
"""
|
| 184 |
+
Returns the inverse of this function.
|
| 185 |
+
"""
|
| 186 |
+
return asinh
|
| 187 |
+
|
| 188 |
+
@classmethod
|
| 189 |
+
def eval(cls, arg):
|
| 190 |
+
if arg.is_Number:
|
| 191 |
+
if arg is S.NaN:
|
| 192 |
+
return S.NaN
|
| 193 |
+
elif arg is S.Infinity:
|
| 194 |
+
return S.Infinity
|
| 195 |
+
elif arg is S.NegativeInfinity:
|
| 196 |
+
return S.NegativeInfinity
|
| 197 |
+
elif arg.is_zero:
|
| 198 |
+
return S.Zero
|
| 199 |
+
elif arg.is_negative:
|
| 200 |
+
return -cls(-arg)
|
| 201 |
+
else:
|
| 202 |
+
if arg is S.ComplexInfinity:
|
| 203 |
+
return S.NaN
|
| 204 |
+
|
| 205 |
+
i_coeff = _imaginary_unit_as_coefficient(arg)
|
| 206 |
+
|
| 207 |
+
if i_coeff is not None:
|
| 208 |
+
return I * sin(i_coeff)
|
| 209 |
+
else:
|
| 210 |
+
if arg.could_extract_minus_sign():
|
| 211 |
+
return -cls(-arg)
|
| 212 |
+
|
| 213 |
+
if arg.is_Add:
|
| 214 |
+
x, m = _peeloff_ipi(arg)
|
| 215 |
+
if m:
|
| 216 |
+
m = m*pi*I
|
| 217 |
+
return sinh(m)*cosh(x) + cosh(m)*sinh(x)
|
| 218 |
+
|
| 219 |
+
if arg.is_zero:
|
| 220 |
+
return S.Zero
|
| 221 |
+
|
| 222 |
+
if arg.func == asinh:
|
| 223 |
+
return arg.args[0]
|
| 224 |
+
|
| 225 |
+
if arg.func == acosh:
|
| 226 |
+
x = arg.args[0]
|
| 227 |
+
return sqrt(x - 1) * sqrt(x + 1)
|
| 228 |
+
|
| 229 |
+
if arg.func == atanh:
|
| 230 |
+
x = arg.args[0]
|
| 231 |
+
return x/sqrt(1 - x**2)
|
| 232 |
+
|
| 233 |
+
if arg.func == acoth:
|
| 234 |
+
x = arg.args[0]
|
| 235 |
+
return 1/(sqrt(x - 1) * sqrt(x + 1))
|
| 236 |
+
|
| 237 |
+
@staticmethod
|
| 238 |
+
@cacheit
|
| 239 |
+
def taylor_term(n, x, *previous_terms):
|
| 240 |
+
"""
|
| 241 |
+
Returns the next term in the Taylor series expansion.
|
| 242 |
+
"""
|
| 243 |
+
if n < 0 or n % 2 == 0:
|
| 244 |
+
return S.Zero
|
| 245 |
+
else:
|
| 246 |
+
x = sympify(x)
|
| 247 |
+
|
| 248 |
+
if len(previous_terms) > 2:
|
| 249 |
+
p = previous_terms[-2]
|
| 250 |
+
return p * x**2 / (n*(n - 1))
|
| 251 |
+
else:
|
| 252 |
+
return x**(n) / factorial(n)
|
| 253 |
+
|
| 254 |
+
def _eval_conjugate(self):
|
| 255 |
+
return self.func(self.args[0].conjugate())
|
| 256 |
+
|
| 257 |
+
def as_real_imag(self, deep=True, **hints):
|
| 258 |
+
"""
|
| 259 |
+
Returns this function as a complex coordinate.
|
| 260 |
+
"""
|
| 261 |
+
if self.args[0].is_extended_real:
|
| 262 |
+
if deep:
|
| 263 |
+
hints['complex'] = False
|
| 264 |
+
return (self.expand(deep, **hints), S.Zero)
|
| 265 |
+
else:
|
| 266 |
+
return (self, S.Zero)
|
| 267 |
+
if deep:
|
| 268 |
+
re, im = self.args[0].expand(deep, **hints).as_real_imag()
|
| 269 |
+
else:
|
| 270 |
+
re, im = self.args[0].as_real_imag()
|
| 271 |
+
return (sinh(re)*cos(im), cosh(re)*sin(im))
|
| 272 |
+
|
| 273 |
+
def _eval_expand_complex(self, deep=True, **hints):
|
| 274 |
+
re_part, im_part = self.as_real_imag(deep=deep, **hints)
|
| 275 |
+
return re_part + im_part*I
|
| 276 |
+
|
| 277 |
+
def _eval_expand_trig(self, deep=True, **hints):
|
| 278 |
+
if deep:
|
| 279 |
+
arg = self.args[0].expand(deep, **hints)
|
| 280 |
+
else:
|
| 281 |
+
arg = self.args[0]
|
| 282 |
+
x = None
|
| 283 |
+
if arg.is_Add: # TODO, implement more if deep stuff here
|
| 284 |
+
x, y = arg.as_two_terms()
|
| 285 |
+
else:
|
| 286 |
+
coeff, terms = arg.as_coeff_Mul(rational=True)
|
| 287 |
+
if coeff is not S.One and coeff.is_Integer and terms is not S.One:
|
| 288 |
+
x = terms
|
| 289 |
+
y = (coeff - 1)*x
|
| 290 |
+
if x is not None:
|
| 291 |
+
return (sinh(x)*cosh(y) + sinh(y)*cosh(x)).expand(trig=True)
|
| 292 |
+
return sinh(arg)
|
| 293 |
+
|
| 294 |
+
def _eval_rewrite_as_tractable(self, arg, limitvar=None, **kwargs):
|
| 295 |
+
return (exp(arg) - exp(-arg)) / 2
|
| 296 |
+
|
| 297 |
+
def _eval_rewrite_as_exp(self, arg, **kwargs):
|
| 298 |
+
return (exp(arg) - exp(-arg)) / 2
|
| 299 |
+
|
| 300 |
+
def _eval_rewrite_as_sin(self, arg, **kwargs):
|
| 301 |
+
return -I * sin(I * arg)
|
| 302 |
+
|
| 303 |
+
def _eval_rewrite_as_csc(self, arg, **kwargs):
|
| 304 |
+
return -I / csc(I * arg)
|
| 305 |
+
|
| 306 |
+
def _eval_rewrite_as_cosh(self, arg, **kwargs):
|
| 307 |
+
return -I*cosh(arg + pi*I/2)
|
| 308 |
+
|
| 309 |
+
def _eval_rewrite_as_tanh(self, arg, **kwargs):
|
| 310 |
+
tanh_half = tanh(S.Half*arg)
|
| 311 |
+
return 2*tanh_half/(1 - tanh_half**2)
|
| 312 |
+
|
| 313 |
+
def _eval_rewrite_as_coth(self, arg, **kwargs):
|
| 314 |
+
coth_half = coth(S.Half*arg)
|
| 315 |
+
return 2*coth_half/(coth_half**2 - 1)
|
| 316 |
+
|
| 317 |
+
def _eval_rewrite_as_csch(self, arg, **kwargs):
|
| 318 |
+
return 1 / csch(arg)
|
| 319 |
+
|
| 320 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0):
|
| 321 |
+
arg = self.args[0].as_leading_term(x, logx=logx, cdir=cdir)
|
| 322 |
+
arg0 = arg.subs(x, 0)
|
| 323 |
+
|
| 324 |
+
if arg0 is S.NaN:
|
| 325 |
+
arg0 = arg.limit(x, 0, dir='-' if cdir.is_negative else '+')
|
| 326 |
+
if arg0.is_zero:
|
| 327 |
+
return arg
|
| 328 |
+
elif arg0.is_finite:
|
| 329 |
+
return self.func(arg0)
|
| 330 |
+
else:
|
| 331 |
+
return self
|
| 332 |
+
|
| 333 |
+
def _eval_is_real(self):
|
| 334 |
+
arg = self.args[0]
|
| 335 |
+
if arg.is_real:
|
| 336 |
+
return True
|
| 337 |
+
|
| 338 |
+
# if `im` is of the form n*pi
|
| 339 |
+
# else, check if it is a number
|
| 340 |
+
re, im = arg.as_real_imag()
|
| 341 |
+
return (im%pi).is_zero
|
| 342 |
+
|
| 343 |
+
def _eval_is_extended_real(self):
|
| 344 |
+
if self.args[0].is_extended_real:
|
| 345 |
+
return True
|
| 346 |
+
|
| 347 |
+
def _eval_is_positive(self):
|
| 348 |
+
if self.args[0].is_extended_real:
|
| 349 |
+
return self.args[0].is_positive
|
| 350 |
+
|
| 351 |
+
def _eval_is_negative(self):
|
| 352 |
+
if self.args[0].is_extended_real:
|
| 353 |
+
return self.args[0].is_negative
|
| 354 |
+
|
| 355 |
+
def _eval_is_finite(self):
|
| 356 |
+
arg = self.args[0]
|
| 357 |
+
return arg.is_finite
|
| 358 |
+
|
| 359 |
+
def _eval_is_zero(self):
|
| 360 |
+
rest, ipi_mult = _peeloff_ipi(self.args[0])
|
| 361 |
+
if rest.is_zero:
|
| 362 |
+
return ipi_mult.is_integer
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
class cosh(HyperbolicFunction):
|
| 366 |
+
r"""
|
| 367 |
+
``cosh(x)`` is the hyperbolic cosine of ``x``.
|
| 368 |
+
|
| 369 |
+
The hyperbolic cosine function is $\frac{e^x + e^{-x}}{2}$.
|
| 370 |
+
|
| 371 |
+
Examples
|
| 372 |
+
========
|
| 373 |
+
|
| 374 |
+
>>> from sympy import cosh
|
| 375 |
+
>>> from sympy.abc import x
|
| 376 |
+
>>> cosh(x)
|
| 377 |
+
cosh(x)
|
| 378 |
+
|
| 379 |
+
See Also
|
| 380 |
+
========
|
| 381 |
+
|
| 382 |
+
sinh, tanh, acosh
|
| 383 |
+
"""
|
| 384 |
+
|
| 385 |
+
def fdiff(self, argindex=1):
|
| 386 |
+
if argindex == 1:
|
| 387 |
+
return sinh(self.args[0])
|
| 388 |
+
else:
|
| 389 |
+
raise ArgumentIndexError(self, argindex)
|
| 390 |
+
|
| 391 |
+
@classmethod
|
| 392 |
+
def eval(cls, arg):
|
| 393 |
+
from sympy.functions.elementary.trigonometric import cos
|
| 394 |
+
if arg.is_Number:
|
| 395 |
+
if arg is S.NaN:
|
| 396 |
+
return S.NaN
|
| 397 |
+
elif arg is S.Infinity:
|
| 398 |
+
return S.Infinity
|
| 399 |
+
elif arg is S.NegativeInfinity:
|
| 400 |
+
return S.Infinity
|
| 401 |
+
elif arg.is_zero:
|
| 402 |
+
return S.One
|
| 403 |
+
elif arg.is_negative:
|
| 404 |
+
return cls(-arg)
|
| 405 |
+
else:
|
| 406 |
+
if arg is S.ComplexInfinity:
|
| 407 |
+
return S.NaN
|
| 408 |
+
|
| 409 |
+
i_coeff = _imaginary_unit_as_coefficient(arg)
|
| 410 |
+
|
| 411 |
+
if i_coeff is not None:
|
| 412 |
+
return cos(i_coeff)
|
| 413 |
+
else:
|
| 414 |
+
if arg.could_extract_minus_sign():
|
| 415 |
+
return cls(-arg)
|
| 416 |
+
|
| 417 |
+
if arg.is_Add:
|
| 418 |
+
x, m = _peeloff_ipi(arg)
|
| 419 |
+
if m:
|
| 420 |
+
m = m*pi*I
|
| 421 |
+
return cosh(m)*cosh(x) + sinh(m)*sinh(x)
|
| 422 |
+
|
| 423 |
+
if arg.is_zero:
|
| 424 |
+
return S.One
|
| 425 |
+
|
| 426 |
+
if arg.func == asinh:
|
| 427 |
+
return sqrt(1 + arg.args[0]**2)
|
| 428 |
+
|
| 429 |
+
if arg.func == acosh:
|
| 430 |
+
return arg.args[0]
|
| 431 |
+
|
| 432 |
+
if arg.func == atanh:
|
| 433 |
+
return 1/sqrt(1 - arg.args[0]**2)
|
| 434 |
+
|
| 435 |
+
if arg.func == acoth:
|
| 436 |
+
x = arg.args[0]
|
| 437 |
+
return x/(sqrt(x - 1) * sqrt(x + 1))
|
| 438 |
+
|
| 439 |
+
@staticmethod
|
| 440 |
+
@cacheit
|
| 441 |
+
def taylor_term(n, x, *previous_terms):
|
| 442 |
+
if n < 0 or n % 2 == 1:
|
| 443 |
+
return S.Zero
|
| 444 |
+
else:
|
| 445 |
+
x = sympify(x)
|
| 446 |
+
|
| 447 |
+
if len(previous_terms) > 2:
|
| 448 |
+
p = previous_terms[-2]
|
| 449 |
+
return p * x**2 / (n*(n - 1))
|
| 450 |
+
else:
|
| 451 |
+
return x**(n)/factorial(n)
|
| 452 |
+
|
| 453 |
+
def _eval_conjugate(self):
|
| 454 |
+
return self.func(self.args[0].conjugate())
|
| 455 |
+
|
| 456 |
+
def as_real_imag(self, deep=True, **hints):
|
| 457 |
+
if self.args[0].is_extended_real:
|
| 458 |
+
if deep:
|
| 459 |
+
hints['complex'] = False
|
| 460 |
+
return (self.expand(deep, **hints), S.Zero)
|
| 461 |
+
else:
|
| 462 |
+
return (self, S.Zero)
|
| 463 |
+
if deep:
|
| 464 |
+
re, im = self.args[0].expand(deep, **hints).as_real_imag()
|
| 465 |
+
else:
|
| 466 |
+
re, im = self.args[0].as_real_imag()
|
| 467 |
+
|
| 468 |
+
return (cosh(re)*cos(im), sinh(re)*sin(im))
|
| 469 |
+
|
| 470 |
+
def _eval_expand_complex(self, deep=True, **hints):
|
| 471 |
+
re_part, im_part = self.as_real_imag(deep=deep, **hints)
|
| 472 |
+
return re_part + im_part*I
|
| 473 |
+
|
| 474 |
+
def _eval_expand_trig(self, deep=True, **hints):
|
| 475 |
+
if deep:
|
| 476 |
+
arg = self.args[0].expand(deep, **hints)
|
| 477 |
+
else:
|
| 478 |
+
arg = self.args[0]
|
| 479 |
+
x = None
|
| 480 |
+
if arg.is_Add: # TODO, implement more if deep stuff here
|
| 481 |
+
x, y = arg.as_two_terms()
|
| 482 |
+
else:
|
| 483 |
+
coeff, terms = arg.as_coeff_Mul(rational=True)
|
| 484 |
+
if coeff is not S.One and coeff.is_Integer and terms is not S.One:
|
| 485 |
+
x = terms
|
| 486 |
+
y = (coeff - 1)*x
|
| 487 |
+
if x is not None:
|
| 488 |
+
return (cosh(x)*cosh(y) + sinh(x)*sinh(y)).expand(trig=True)
|
| 489 |
+
return cosh(arg)
|
| 490 |
+
|
| 491 |
+
def _eval_rewrite_as_tractable(self, arg, limitvar=None, **kwargs):
|
| 492 |
+
return (exp(arg) + exp(-arg)) / 2
|
| 493 |
+
|
| 494 |
+
def _eval_rewrite_as_exp(self, arg, **kwargs):
|
| 495 |
+
return (exp(arg) + exp(-arg)) / 2
|
| 496 |
+
|
| 497 |
+
def _eval_rewrite_as_cos(self, arg, **kwargs):
|
| 498 |
+
return cos(I * arg, evaluate=False)
|
| 499 |
+
|
| 500 |
+
def _eval_rewrite_as_sec(self, arg, **kwargs):
|
| 501 |
+
return 1 / sec(I * arg, evaluate=False)
|
| 502 |
+
|
| 503 |
+
def _eval_rewrite_as_sinh(self, arg, **kwargs):
|
| 504 |
+
return -I*sinh(arg + pi*I/2, evaluate=False)
|
| 505 |
+
|
| 506 |
+
def _eval_rewrite_as_tanh(self, arg, **kwargs):
|
| 507 |
+
tanh_half = tanh(S.Half*arg)**2
|
| 508 |
+
return (1 + tanh_half)/(1 - tanh_half)
|
| 509 |
+
|
| 510 |
+
def _eval_rewrite_as_coth(self, arg, **kwargs):
|
| 511 |
+
coth_half = coth(S.Half*arg)**2
|
| 512 |
+
return (coth_half + 1)/(coth_half - 1)
|
| 513 |
+
|
| 514 |
+
def _eval_rewrite_as_sech(self, arg, **kwargs):
|
| 515 |
+
return 1 / sech(arg)
|
| 516 |
+
|
| 517 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0):
|
| 518 |
+
arg = self.args[0].as_leading_term(x, logx=logx, cdir=cdir)
|
| 519 |
+
arg0 = arg.subs(x, 0)
|
| 520 |
+
|
| 521 |
+
if arg0 is S.NaN:
|
| 522 |
+
arg0 = arg.limit(x, 0, dir='-' if cdir.is_negative else '+')
|
| 523 |
+
if arg0.is_zero:
|
| 524 |
+
return S.One
|
| 525 |
+
elif arg0.is_finite:
|
| 526 |
+
return self.func(arg0)
|
| 527 |
+
else:
|
| 528 |
+
return self
|
| 529 |
+
|
| 530 |
+
def _eval_is_real(self):
|
| 531 |
+
arg = self.args[0]
|
| 532 |
+
|
| 533 |
+
# `cosh(x)` is real for real OR purely imaginary `x`
|
| 534 |
+
if arg.is_real or arg.is_imaginary:
|
| 535 |
+
return True
|
| 536 |
+
|
| 537 |
+
# cosh(a+ib) = cos(b)*cosh(a) + i*sin(b)*sinh(a)
|
| 538 |
+
# the imaginary part can be an expression like n*pi
|
| 539 |
+
# if not, check if the imaginary part is a number
|
| 540 |
+
re, im = arg.as_real_imag()
|
| 541 |
+
return (im%pi).is_zero
|
| 542 |
+
|
| 543 |
+
def _eval_is_positive(self):
|
| 544 |
+
# cosh(x+I*y) = cos(y)*cosh(x) + I*sin(y)*sinh(x)
|
| 545 |
+
# cosh(z) is positive iff it is real and the real part is positive.
|
| 546 |
+
# So we need sin(y)*sinh(x) = 0 which gives x=0 or y=n*pi
|
| 547 |
+
# Case 1 (y=n*pi): cosh(z) = (-1)**n * cosh(x) -> positive for n even
|
| 548 |
+
# Case 2 (x=0): cosh(z) = cos(y) -> positive when cos(y) is positive
|
| 549 |
+
z = self.args[0]
|
| 550 |
+
|
| 551 |
+
x, y = z.as_real_imag()
|
| 552 |
+
ymod = y % (2*pi)
|
| 553 |
+
|
| 554 |
+
yzero = ymod.is_zero
|
| 555 |
+
# shortcut if ymod is zero
|
| 556 |
+
if yzero:
|
| 557 |
+
return True
|
| 558 |
+
|
| 559 |
+
xzero = x.is_zero
|
| 560 |
+
# shortcut x is not zero
|
| 561 |
+
if xzero is False:
|
| 562 |
+
return yzero
|
| 563 |
+
|
| 564 |
+
return fuzzy_or([
|
| 565 |
+
# Case 1:
|
| 566 |
+
yzero,
|
| 567 |
+
# Case 2:
|
| 568 |
+
fuzzy_and([
|
| 569 |
+
xzero,
|
| 570 |
+
fuzzy_or([ymod < pi/2, ymod > 3*pi/2])
|
| 571 |
+
])
|
| 572 |
+
])
|
| 573 |
+
|
| 574 |
+
|
| 575 |
+
def _eval_is_nonnegative(self):
|
| 576 |
+
z = self.args[0]
|
| 577 |
+
|
| 578 |
+
x, y = z.as_real_imag()
|
| 579 |
+
ymod = y % (2*pi)
|
| 580 |
+
|
| 581 |
+
yzero = ymod.is_zero
|
| 582 |
+
# shortcut if ymod is zero
|
| 583 |
+
if yzero:
|
| 584 |
+
return True
|
| 585 |
+
|
| 586 |
+
xzero = x.is_zero
|
| 587 |
+
# shortcut x is not zero
|
| 588 |
+
if xzero is False:
|
| 589 |
+
return yzero
|
| 590 |
+
|
| 591 |
+
return fuzzy_or([
|
| 592 |
+
# Case 1:
|
| 593 |
+
yzero,
|
| 594 |
+
# Case 2:
|
| 595 |
+
fuzzy_and([
|
| 596 |
+
xzero,
|
| 597 |
+
fuzzy_or([ymod <= pi/2, ymod >= 3*pi/2])
|
| 598 |
+
])
|
| 599 |
+
])
|
| 600 |
+
|
| 601 |
+
def _eval_is_finite(self):
|
| 602 |
+
arg = self.args[0]
|
| 603 |
+
return arg.is_finite
|
| 604 |
+
|
| 605 |
+
def _eval_is_zero(self):
|
| 606 |
+
rest, ipi_mult = _peeloff_ipi(self.args[0])
|
| 607 |
+
if ipi_mult and rest.is_zero:
|
| 608 |
+
return (ipi_mult - S.Half).is_integer
|
| 609 |
+
|
| 610 |
+
|
| 611 |
+
class tanh(HyperbolicFunction):
|
| 612 |
+
r"""
|
| 613 |
+
``tanh(x)`` is the hyperbolic tangent of ``x``.
|
| 614 |
+
|
| 615 |
+
The hyperbolic tangent function is $\frac{\sinh(x)}{\cosh(x)}$.
|
| 616 |
+
|
| 617 |
+
Examples
|
| 618 |
+
========
|
| 619 |
+
|
| 620 |
+
>>> from sympy import tanh
|
| 621 |
+
>>> from sympy.abc import x
|
| 622 |
+
>>> tanh(x)
|
| 623 |
+
tanh(x)
|
| 624 |
+
|
| 625 |
+
See Also
|
| 626 |
+
========
|
| 627 |
+
|
| 628 |
+
sinh, cosh, atanh
|
| 629 |
+
"""
|
| 630 |
+
|
| 631 |
+
def fdiff(self, argindex=1):
|
| 632 |
+
if argindex == 1:
|
| 633 |
+
return S.One - tanh(self.args[0])**2
|
| 634 |
+
else:
|
| 635 |
+
raise ArgumentIndexError(self, argindex)
|
| 636 |
+
|
| 637 |
+
def inverse(self, argindex=1):
|
| 638 |
+
"""
|
| 639 |
+
Returns the inverse of this function.
|
| 640 |
+
"""
|
| 641 |
+
return atanh
|
| 642 |
+
|
| 643 |
+
@classmethod
|
| 644 |
+
def eval(cls, arg):
|
| 645 |
+
if arg.is_Number:
|
| 646 |
+
if arg is S.NaN:
|
| 647 |
+
return S.NaN
|
| 648 |
+
elif arg is S.Infinity:
|
| 649 |
+
return S.One
|
| 650 |
+
elif arg is S.NegativeInfinity:
|
| 651 |
+
return S.NegativeOne
|
| 652 |
+
elif arg.is_zero:
|
| 653 |
+
return S.Zero
|
| 654 |
+
elif arg.is_negative:
|
| 655 |
+
return -cls(-arg)
|
| 656 |
+
else:
|
| 657 |
+
if arg is S.ComplexInfinity:
|
| 658 |
+
return S.NaN
|
| 659 |
+
|
| 660 |
+
i_coeff = _imaginary_unit_as_coefficient(arg)
|
| 661 |
+
|
| 662 |
+
if i_coeff is not None:
|
| 663 |
+
if i_coeff.could_extract_minus_sign():
|
| 664 |
+
return -I * tan(-i_coeff)
|
| 665 |
+
return I * tan(i_coeff)
|
| 666 |
+
else:
|
| 667 |
+
if arg.could_extract_minus_sign():
|
| 668 |
+
return -cls(-arg)
|
| 669 |
+
|
| 670 |
+
if arg.is_Add:
|
| 671 |
+
x, m = _peeloff_ipi(arg)
|
| 672 |
+
if m:
|
| 673 |
+
tanhm = tanh(m*pi*I)
|
| 674 |
+
if tanhm is S.ComplexInfinity:
|
| 675 |
+
return coth(x)
|
| 676 |
+
else: # tanhm == 0
|
| 677 |
+
return tanh(x)
|
| 678 |
+
|
| 679 |
+
if arg.is_zero:
|
| 680 |
+
return S.Zero
|
| 681 |
+
|
| 682 |
+
if arg.func == asinh:
|
| 683 |
+
x = arg.args[0]
|
| 684 |
+
return x/sqrt(1 + x**2)
|
| 685 |
+
|
| 686 |
+
if arg.func == acosh:
|
| 687 |
+
x = arg.args[0]
|
| 688 |
+
return sqrt(x - 1) * sqrt(x + 1) / x
|
| 689 |
+
|
| 690 |
+
if arg.func == atanh:
|
| 691 |
+
return arg.args[0]
|
| 692 |
+
|
| 693 |
+
if arg.func == acoth:
|
| 694 |
+
return 1/arg.args[0]
|
| 695 |
+
|
| 696 |
+
@staticmethod
|
| 697 |
+
@cacheit
|
| 698 |
+
def taylor_term(n, x, *previous_terms):
|
| 699 |
+
if n < 0 or n % 2 == 0:
|
| 700 |
+
return S.Zero
|
| 701 |
+
else:
|
| 702 |
+
x = sympify(x)
|
| 703 |
+
|
| 704 |
+
a = 2**(n + 1)
|
| 705 |
+
|
| 706 |
+
B = bernoulli(n + 1)
|
| 707 |
+
F = factorial(n + 1)
|
| 708 |
+
|
| 709 |
+
return a*(a - 1) * B/F * x**n
|
| 710 |
+
|
| 711 |
+
def _eval_conjugate(self):
|
| 712 |
+
return self.func(self.args[0].conjugate())
|
| 713 |
+
|
| 714 |
+
def as_real_imag(self, deep=True, **hints):
|
| 715 |
+
if self.args[0].is_extended_real:
|
| 716 |
+
if deep:
|
| 717 |
+
hints['complex'] = False
|
| 718 |
+
return (self.expand(deep, **hints), S.Zero)
|
| 719 |
+
else:
|
| 720 |
+
return (self, S.Zero)
|
| 721 |
+
if deep:
|
| 722 |
+
re, im = self.args[0].expand(deep, **hints).as_real_imag()
|
| 723 |
+
else:
|
| 724 |
+
re, im = self.args[0].as_real_imag()
|
| 725 |
+
denom = sinh(re)**2 + cos(im)**2
|
| 726 |
+
return (sinh(re)*cosh(re)/denom, sin(im)*cos(im)/denom)
|
| 727 |
+
|
| 728 |
+
def _eval_expand_trig(self, **hints):
|
| 729 |
+
arg = self.args[0]
|
| 730 |
+
if arg.is_Add:
|
| 731 |
+
n = len(arg.args)
|
| 732 |
+
TX = [tanh(x, evaluate=False)._eval_expand_trig()
|
| 733 |
+
for x in arg.args]
|
| 734 |
+
p = [0, 0] # [den, num]
|
| 735 |
+
for i in range(n + 1):
|
| 736 |
+
p[i % 2] += symmetric_poly(i, TX)
|
| 737 |
+
return p[1]/p[0]
|
| 738 |
+
elif arg.is_Mul:
|
| 739 |
+
coeff, terms = arg.as_coeff_Mul()
|
| 740 |
+
if coeff.is_Integer and coeff > 1:
|
| 741 |
+
T = tanh(terms)
|
| 742 |
+
n = [nC(range(coeff), k)*T**k for k in range(1, coeff + 1, 2)]
|
| 743 |
+
d = [nC(range(coeff), k)*T**k for k in range(0, coeff + 1, 2)]
|
| 744 |
+
return Add(*n)/Add(*d)
|
| 745 |
+
return tanh(arg)
|
| 746 |
+
|
| 747 |
+
def _eval_rewrite_as_tractable(self, arg, limitvar=None, **kwargs):
|
| 748 |
+
neg_exp, pos_exp = exp(-arg), exp(arg)
|
| 749 |
+
return (pos_exp - neg_exp)/(pos_exp + neg_exp)
|
| 750 |
+
|
| 751 |
+
def _eval_rewrite_as_exp(self, arg, **kwargs):
|
| 752 |
+
neg_exp, pos_exp = exp(-arg), exp(arg)
|
| 753 |
+
return (pos_exp - neg_exp)/(pos_exp + neg_exp)
|
| 754 |
+
|
| 755 |
+
def _eval_rewrite_as_tan(self, arg, **kwargs):
|
| 756 |
+
return -I * tan(I * arg, evaluate=False)
|
| 757 |
+
|
| 758 |
+
def _eval_rewrite_as_cot(self, arg, **kwargs):
|
| 759 |
+
return -I / cot(I * arg, evaluate=False)
|
| 760 |
+
|
| 761 |
+
def _eval_rewrite_as_sinh(self, arg, **kwargs):
|
| 762 |
+
return I*sinh(arg)/sinh(pi*I/2 - arg, evaluate=False)
|
| 763 |
+
|
| 764 |
+
def _eval_rewrite_as_cosh(self, arg, **kwargs):
|
| 765 |
+
return I*cosh(pi*I/2 - arg, evaluate=False)/cosh(arg)
|
| 766 |
+
|
| 767 |
+
def _eval_rewrite_as_coth(self, arg, **kwargs):
|
| 768 |
+
return 1/coth(arg)
|
| 769 |
+
|
| 770 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0):
|
| 771 |
+
from sympy.series.order import Order
|
| 772 |
+
arg = self.args[0].as_leading_term(x)
|
| 773 |
+
|
| 774 |
+
if x in arg.free_symbols and Order(1, x).contains(arg):
|
| 775 |
+
return arg
|
| 776 |
+
else:
|
| 777 |
+
return self.func(arg)
|
| 778 |
+
|
| 779 |
+
def _eval_is_real(self):
|
| 780 |
+
arg = self.args[0]
|
| 781 |
+
if arg.is_real:
|
| 782 |
+
return True
|
| 783 |
+
|
| 784 |
+
re, im = arg.as_real_imag()
|
| 785 |
+
|
| 786 |
+
# if denom = 0, tanh(arg) = zoo
|
| 787 |
+
if re == 0 and im % pi == pi/2:
|
| 788 |
+
return None
|
| 789 |
+
|
| 790 |
+
# check if im is of the form n*pi/2 to make sin(2*im) = 0
|
| 791 |
+
# if not, im could be a number, return False in that case
|
| 792 |
+
return (im % (pi/2)).is_zero
|
| 793 |
+
|
| 794 |
+
def _eval_is_extended_real(self):
|
| 795 |
+
if self.args[0].is_extended_real:
|
| 796 |
+
return True
|
| 797 |
+
|
| 798 |
+
def _eval_is_positive(self):
|
| 799 |
+
if self.args[0].is_extended_real:
|
| 800 |
+
return self.args[0].is_positive
|
| 801 |
+
|
| 802 |
+
def _eval_is_negative(self):
|
| 803 |
+
if self.args[0].is_extended_real:
|
| 804 |
+
return self.args[0].is_negative
|
| 805 |
+
|
| 806 |
+
def _eval_is_finite(self):
|
| 807 |
+
arg = self.args[0]
|
| 808 |
+
|
| 809 |
+
re, im = arg.as_real_imag()
|
| 810 |
+
denom = cos(im)**2 + sinh(re)**2
|
| 811 |
+
if denom == 0:
|
| 812 |
+
return False
|
| 813 |
+
elif denom.is_number:
|
| 814 |
+
return True
|
| 815 |
+
if arg.is_extended_real:
|
| 816 |
+
return True
|
| 817 |
+
|
| 818 |
+
def _eval_is_zero(self):
|
| 819 |
+
arg = self.args[0]
|
| 820 |
+
if arg.is_zero:
|
| 821 |
+
return True
|
| 822 |
+
|
| 823 |
+
|
| 824 |
+
class coth(HyperbolicFunction):
|
| 825 |
+
r"""
|
| 826 |
+
``coth(x)`` is the hyperbolic cotangent of ``x``.
|
| 827 |
+
|
| 828 |
+
The hyperbolic cotangent function is $\frac{\cosh(x)}{\sinh(x)}$.
|
| 829 |
+
|
| 830 |
+
Examples
|
| 831 |
+
========
|
| 832 |
+
|
| 833 |
+
>>> from sympy import coth
|
| 834 |
+
>>> from sympy.abc import x
|
| 835 |
+
>>> coth(x)
|
| 836 |
+
coth(x)
|
| 837 |
+
|
| 838 |
+
See Also
|
| 839 |
+
========
|
| 840 |
+
|
| 841 |
+
sinh, cosh, acoth
|
| 842 |
+
"""
|
| 843 |
+
|
| 844 |
+
def fdiff(self, argindex=1):
|
| 845 |
+
if argindex == 1:
|
| 846 |
+
return -1/sinh(self.args[0])**2
|
| 847 |
+
else:
|
| 848 |
+
raise ArgumentIndexError(self, argindex)
|
| 849 |
+
|
| 850 |
+
def inverse(self, argindex=1):
|
| 851 |
+
"""
|
| 852 |
+
Returns the inverse of this function.
|
| 853 |
+
"""
|
| 854 |
+
return acoth
|
| 855 |
+
|
| 856 |
+
@classmethod
|
| 857 |
+
def eval(cls, arg):
|
| 858 |
+
if arg.is_Number:
|
| 859 |
+
if arg is S.NaN:
|
| 860 |
+
return S.NaN
|
| 861 |
+
elif arg is S.Infinity:
|
| 862 |
+
return S.One
|
| 863 |
+
elif arg is S.NegativeInfinity:
|
| 864 |
+
return S.NegativeOne
|
| 865 |
+
elif arg.is_zero:
|
| 866 |
+
return S.ComplexInfinity
|
| 867 |
+
elif arg.is_negative:
|
| 868 |
+
return -cls(-arg)
|
| 869 |
+
else:
|
| 870 |
+
if arg is S.ComplexInfinity:
|
| 871 |
+
return S.NaN
|
| 872 |
+
|
| 873 |
+
i_coeff = _imaginary_unit_as_coefficient(arg)
|
| 874 |
+
|
| 875 |
+
if i_coeff is not None:
|
| 876 |
+
if i_coeff.could_extract_minus_sign():
|
| 877 |
+
return I * cot(-i_coeff)
|
| 878 |
+
return -I * cot(i_coeff)
|
| 879 |
+
else:
|
| 880 |
+
if arg.could_extract_minus_sign():
|
| 881 |
+
return -cls(-arg)
|
| 882 |
+
|
| 883 |
+
if arg.is_Add:
|
| 884 |
+
x, m = _peeloff_ipi(arg)
|
| 885 |
+
if m:
|
| 886 |
+
cothm = coth(m*pi*I)
|
| 887 |
+
if cothm is S.ComplexInfinity:
|
| 888 |
+
return coth(x)
|
| 889 |
+
else: # cothm == 0
|
| 890 |
+
return tanh(x)
|
| 891 |
+
|
| 892 |
+
if arg.is_zero:
|
| 893 |
+
return S.ComplexInfinity
|
| 894 |
+
|
| 895 |
+
if arg.func == asinh:
|
| 896 |
+
x = arg.args[0]
|
| 897 |
+
return sqrt(1 + x**2)/x
|
| 898 |
+
|
| 899 |
+
if arg.func == acosh:
|
| 900 |
+
x = arg.args[0]
|
| 901 |
+
return x/(sqrt(x - 1) * sqrt(x + 1))
|
| 902 |
+
|
| 903 |
+
if arg.func == atanh:
|
| 904 |
+
return 1/arg.args[0]
|
| 905 |
+
|
| 906 |
+
if arg.func == acoth:
|
| 907 |
+
return arg.args[0]
|
| 908 |
+
|
| 909 |
+
@staticmethod
|
| 910 |
+
@cacheit
|
| 911 |
+
def taylor_term(n, x, *previous_terms):
|
| 912 |
+
if n == 0:
|
| 913 |
+
return 1 / sympify(x)
|
| 914 |
+
elif n < 0 or n % 2 == 0:
|
| 915 |
+
return S.Zero
|
| 916 |
+
else:
|
| 917 |
+
x = sympify(x)
|
| 918 |
+
|
| 919 |
+
B = bernoulli(n + 1)
|
| 920 |
+
F = factorial(n + 1)
|
| 921 |
+
|
| 922 |
+
return 2**(n + 1) * B/F * x**n
|
| 923 |
+
|
| 924 |
+
def _eval_conjugate(self):
|
| 925 |
+
return self.func(self.args[0].conjugate())
|
| 926 |
+
|
| 927 |
+
def as_real_imag(self, deep=True, **hints):
|
| 928 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 929 |
+
if self.args[0].is_extended_real:
|
| 930 |
+
if deep:
|
| 931 |
+
hints['complex'] = False
|
| 932 |
+
return (self.expand(deep, **hints), S.Zero)
|
| 933 |
+
else:
|
| 934 |
+
return (self, S.Zero)
|
| 935 |
+
if deep:
|
| 936 |
+
re, im = self.args[0].expand(deep, **hints).as_real_imag()
|
| 937 |
+
else:
|
| 938 |
+
re, im = self.args[0].as_real_imag()
|
| 939 |
+
denom = sinh(re)**2 + sin(im)**2
|
| 940 |
+
return (sinh(re)*cosh(re)/denom, -sin(im)*cos(im)/denom)
|
| 941 |
+
|
| 942 |
+
def _eval_rewrite_as_tractable(self, arg, limitvar=None, **kwargs):
|
| 943 |
+
neg_exp, pos_exp = exp(-arg), exp(arg)
|
| 944 |
+
return (pos_exp + neg_exp)/(pos_exp - neg_exp)
|
| 945 |
+
|
| 946 |
+
def _eval_rewrite_as_exp(self, arg, **kwargs):
|
| 947 |
+
neg_exp, pos_exp = exp(-arg), exp(arg)
|
| 948 |
+
return (pos_exp + neg_exp)/(pos_exp - neg_exp)
|
| 949 |
+
|
| 950 |
+
def _eval_rewrite_as_sinh(self, arg, **kwargs):
|
| 951 |
+
return -I*sinh(pi*I/2 - arg, evaluate=False)/sinh(arg)
|
| 952 |
+
|
| 953 |
+
def _eval_rewrite_as_cosh(self, arg, **kwargs):
|
| 954 |
+
return -I*cosh(arg)/cosh(pi*I/2 - arg, evaluate=False)
|
| 955 |
+
|
| 956 |
+
def _eval_rewrite_as_tanh(self, arg, **kwargs):
|
| 957 |
+
return 1/tanh(arg)
|
| 958 |
+
|
| 959 |
+
def _eval_is_positive(self):
|
| 960 |
+
if self.args[0].is_extended_real:
|
| 961 |
+
return self.args[0].is_positive
|
| 962 |
+
|
| 963 |
+
def _eval_is_negative(self):
|
| 964 |
+
if self.args[0].is_extended_real:
|
| 965 |
+
return self.args[0].is_negative
|
| 966 |
+
|
| 967 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0):
|
| 968 |
+
from sympy.series.order import Order
|
| 969 |
+
arg = self.args[0].as_leading_term(x)
|
| 970 |
+
|
| 971 |
+
if x in arg.free_symbols and Order(1, x).contains(arg):
|
| 972 |
+
return 1/arg
|
| 973 |
+
else:
|
| 974 |
+
return self.func(arg)
|
| 975 |
+
|
| 976 |
+
def _eval_expand_trig(self, **hints):
|
| 977 |
+
arg = self.args[0]
|
| 978 |
+
if arg.is_Add:
|
| 979 |
+
CX = [coth(x, evaluate=False)._eval_expand_trig() for x in arg.args]
|
| 980 |
+
p = [[], []]
|
| 981 |
+
n = len(arg.args)
|
| 982 |
+
for i in range(n, -1, -1):
|
| 983 |
+
p[(n - i) % 2].append(symmetric_poly(i, CX))
|
| 984 |
+
return Add(*p[0])/Add(*p[1])
|
| 985 |
+
elif arg.is_Mul:
|
| 986 |
+
coeff, x = arg.as_coeff_Mul(rational=True)
|
| 987 |
+
if coeff.is_Integer and coeff > 1:
|
| 988 |
+
c = coth(x, evaluate=False)
|
| 989 |
+
p = [[], []]
|
| 990 |
+
for i in range(coeff, -1, -1):
|
| 991 |
+
p[(coeff - i) % 2].append(binomial(coeff, i)*c**i)
|
| 992 |
+
return Add(*p[0])/Add(*p[1])
|
| 993 |
+
return coth(arg)
|
| 994 |
+
|
| 995 |
+
|
| 996 |
+
class ReciprocalHyperbolicFunction(HyperbolicFunction):
|
| 997 |
+
"""Base class for reciprocal functions of hyperbolic functions. """
|
| 998 |
+
|
| 999 |
+
#To be defined in class
|
| 1000 |
+
_reciprocal_of = None
|
| 1001 |
+
_is_even: FuzzyBool = None
|
| 1002 |
+
_is_odd: FuzzyBool = None
|
| 1003 |
+
|
| 1004 |
+
@classmethod
|
| 1005 |
+
def eval(cls, arg):
|
| 1006 |
+
if arg.could_extract_minus_sign():
|
| 1007 |
+
if cls._is_even:
|
| 1008 |
+
return cls(-arg)
|
| 1009 |
+
if cls._is_odd:
|
| 1010 |
+
return -cls(-arg)
|
| 1011 |
+
|
| 1012 |
+
t = cls._reciprocal_of.eval(arg)
|
| 1013 |
+
if hasattr(arg, 'inverse') and arg.inverse() == cls:
|
| 1014 |
+
return arg.args[0]
|
| 1015 |
+
return 1/t if t is not None else t
|
| 1016 |
+
|
| 1017 |
+
def _call_reciprocal(self, method_name, *args, **kwargs):
|
| 1018 |
+
# Calls method_name on _reciprocal_of
|
| 1019 |
+
o = self._reciprocal_of(self.args[0])
|
| 1020 |
+
return getattr(o, method_name)(*args, **kwargs)
|
| 1021 |
+
|
| 1022 |
+
def _calculate_reciprocal(self, method_name, *args, **kwargs):
|
| 1023 |
+
# If calling method_name on _reciprocal_of returns a value != None
|
| 1024 |
+
# then return the reciprocal of that value
|
| 1025 |
+
t = self._call_reciprocal(method_name, *args, **kwargs)
|
| 1026 |
+
return 1/t if t is not None else t
|
| 1027 |
+
|
| 1028 |
+
def _rewrite_reciprocal(self, method_name, arg):
|
| 1029 |
+
# Special handling for rewrite functions. If reciprocal rewrite returns
|
| 1030 |
+
# unmodified expression, then return None
|
| 1031 |
+
t = self._call_reciprocal(method_name, arg)
|
| 1032 |
+
if t is not None and t != self._reciprocal_of(arg):
|
| 1033 |
+
return 1/t
|
| 1034 |
+
|
| 1035 |
+
def _eval_rewrite_as_exp(self, arg, **kwargs):
|
| 1036 |
+
return self._rewrite_reciprocal("_eval_rewrite_as_exp", arg)
|
| 1037 |
+
|
| 1038 |
+
def _eval_rewrite_as_tractable(self, arg, limitvar=None, **kwargs):
|
| 1039 |
+
return self._rewrite_reciprocal("_eval_rewrite_as_tractable", arg)
|
| 1040 |
+
|
| 1041 |
+
def _eval_rewrite_as_tanh(self, arg, **kwargs):
|
| 1042 |
+
return self._rewrite_reciprocal("_eval_rewrite_as_tanh", arg)
|
| 1043 |
+
|
| 1044 |
+
def _eval_rewrite_as_coth(self, arg, **kwargs):
|
| 1045 |
+
return self._rewrite_reciprocal("_eval_rewrite_as_coth", arg)
|
| 1046 |
+
|
| 1047 |
+
def as_real_imag(self, deep = True, **hints):
|
| 1048 |
+
return (1 / self._reciprocal_of(self.args[0])).as_real_imag(deep, **hints)
|
| 1049 |
+
|
| 1050 |
+
def _eval_conjugate(self):
|
| 1051 |
+
return self.func(self.args[0].conjugate())
|
| 1052 |
+
|
| 1053 |
+
def _eval_expand_complex(self, deep=True, **hints):
|
| 1054 |
+
re_part, im_part = self.as_real_imag(deep=True, **hints)
|
| 1055 |
+
return re_part + I*im_part
|
| 1056 |
+
|
| 1057 |
+
def _eval_expand_trig(self, **hints):
|
| 1058 |
+
return self._calculate_reciprocal("_eval_expand_trig", **hints)
|
| 1059 |
+
|
| 1060 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0):
|
| 1061 |
+
return (1/self._reciprocal_of(self.args[0]))._eval_as_leading_term(x)
|
| 1062 |
+
|
| 1063 |
+
def _eval_is_extended_real(self):
|
| 1064 |
+
return self._reciprocal_of(self.args[0]).is_extended_real
|
| 1065 |
+
|
| 1066 |
+
def _eval_is_finite(self):
|
| 1067 |
+
return (1/self._reciprocal_of(self.args[0])).is_finite
|
| 1068 |
+
|
| 1069 |
+
|
| 1070 |
+
class csch(ReciprocalHyperbolicFunction):
|
| 1071 |
+
r"""
|
| 1072 |
+
``csch(x)`` is the hyperbolic cosecant of ``x``.
|
| 1073 |
+
|
| 1074 |
+
The hyperbolic cosecant function is $\frac{2}{e^x - e^{-x}}$
|
| 1075 |
+
|
| 1076 |
+
Examples
|
| 1077 |
+
========
|
| 1078 |
+
|
| 1079 |
+
>>> from sympy import csch
|
| 1080 |
+
>>> from sympy.abc import x
|
| 1081 |
+
>>> csch(x)
|
| 1082 |
+
csch(x)
|
| 1083 |
+
|
| 1084 |
+
See Also
|
| 1085 |
+
========
|
| 1086 |
+
|
| 1087 |
+
sinh, cosh, tanh, sech, asinh, acosh
|
| 1088 |
+
"""
|
| 1089 |
+
|
| 1090 |
+
_reciprocal_of = sinh
|
| 1091 |
+
_is_odd = True
|
| 1092 |
+
|
| 1093 |
+
def fdiff(self, argindex=1):
|
| 1094 |
+
"""
|
| 1095 |
+
Returns the first derivative of this function
|
| 1096 |
+
"""
|
| 1097 |
+
if argindex == 1:
|
| 1098 |
+
return -coth(self.args[0]) * csch(self.args[0])
|
| 1099 |
+
else:
|
| 1100 |
+
raise ArgumentIndexError(self, argindex)
|
| 1101 |
+
|
| 1102 |
+
@staticmethod
|
| 1103 |
+
@cacheit
|
| 1104 |
+
def taylor_term(n, x, *previous_terms):
|
| 1105 |
+
"""
|
| 1106 |
+
Returns the next term in the Taylor series expansion
|
| 1107 |
+
"""
|
| 1108 |
+
if n == 0:
|
| 1109 |
+
return 1/sympify(x)
|
| 1110 |
+
elif n < 0 or n % 2 == 0:
|
| 1111 |
+
return S.Zero
|
| 1112 |
+
else:
|
| 1113 |
+
x = sympify(x)
|
| 1114 |
+
|
| 1115 |
+
B = bernoulli(n + 1)
|
| 1116 |
+
F = factorial(n + 1)
|
| 1117 |
+
|
| 1118 |
+
return 2 * (1 - 2**n) * B/F * x**n
|
| 1119 |
+
|
| 1120 |
+
def _eval_rewrite_as_sin(self, arg, **kwargs):
|
| 1121 |
+
return I / sin(I * arg, evaluate=False)
|
| 1122 |
+
|
| 1123 |
+
def _eval_rewrite_as_csc(self, arg, **kwargs):
|
| 1124 |
+
return I * csc(I * arg, evaluate=False)
|
| 1125 |
+
|
| 1126 |
+
def _eval_rewrite_as_cosh(self, arg, **kwargs):
|
| 1127 |
+
return I / cosh(arg + I * pi / 2, evaluate=False)
|
| 1128 |
+
|
| 1129 |
+
def _eval_rewrite_as_sinh(self, arg, **kwargs):
|
| 1130 |
+
return 1 / sinh(arg)
|
| 1131 |
+
|
| 1132 |
+
def _eval_is_positive(self):
|
| 1133 |
+
if self.args[0].is_extended_real:
|
| 1134 |
+
return self.args[0].is_positive
|
| 1135 |
+
|
| 1136 |
+
def _eval_is_negative(self):
|
| 1137 |
+
if self.args[0].is_extended_real:
|
| 1138 |
+
return self.args[0].is_negative
|
| 1139 |
+
|
| 1140 |
+
|
| 1141 |
+
class sech(ReciprocalHyperbolicFunction):
|
| 1142 |
+
r"""
|
| 1143 |
+
``sech(x)`` is the hyperbolic secant of ``x``.
|
| 1144 |
+
|
| 1145 |
+
The hyperbolic secant function is $\frac{2}{e^x + e^{-x}}$
|
| 1146 |
+
|
| 1147 |
+
Examples
|
| 1148 |
+
========
|
| 1149 |
+
|
| 1150 |
+
>>> from sympy import sech
|
| 1151 |
+
>>> from sympy.abc import x
|
| 1152 |
+
>>> sech(x)
|
| 1153 |
+
sech(x)
|
| 1154 |
+
|
| 1155 |
+
See Also
|
| 1156 |
+
========
|
| 1157 |
+
|
| 1158 |
+
sinh, cosh, tanh, coth, csch, asinh, acosh
|
| 1159 |
+
"""
|
| 1160 |
+
|
| 1161 |
+
_reciprocal_of = cosh
|
| 1162 |
+
_is_even = True
|
| 1163 |
+
|
| 1164 |
+
def fdiff(self, argindex=1):
|
| 1165 |
+
if argindex == 1:
|
| 1166 |
+
return - tanh(self.args[0])*sech(self.args[0])
|
| 1167 |
+
else:
|
| 1168 |
+
raise ArgumentIndexError(self, argindex)
|
| 1169 |
+
|
| 1170 |
+
@staticmethod
|
| 1171 |
+
@cacheit
|
| 1172 |
+
def taylor_term(n, x, *previous_terms):
|
| 1173 |
+
if n < 0 or n % 2 == 1:
|
| 1174 |
+
return S.Zero
|
| 1175 |
+
else:
|
| 1176 |
+
x = sympify(x)
|
| 1177 |
+
return euler(n) / factorial(n) * x**(n)
|
| 1178 |
+
|
| 1179 |
+
def _eval_rewrite_as_cos(self, arg, **kwargs):
|
| 1180 |
+
return 1 / cos(I * arg, evaluate=False)
|
| 1181 |
+
|
| 1182 |
+
def _eval_rewrite_as_sec(self, arg, **kwargs):
|
| 1183 |
+
return sec(I * arg, evaluate=False)
|
| 1184 |
+
|
| 1185 |
+
def _eval_rewrite_as_sinh(self, arg, **kwargs):
|
| 1186 |
+
return I / sinh(arg + I * pi /2, evaluate=False)
|
| 1187 |
+
|
| 1188 |
+
def _eval_rewrite_as_cosh(self, arg, **kwargs):
|
| 1189 |
+
return 1 / cosh(arg)
|
| 1190 |
+
|
| 1191 |
+
def _eval_is_positive(self):
|
| 1192 |
+
if self.args[0].is_extended_real:
|
| 1193 |
+
return True
|
| 1194 |
+
|
| 1195 |
+
|
| 1196 |
+
###############################################################################
|
| 1197 |
+
############################# HYPERBOLIC INVERSES #############################
|
| 1198 |
+
###############################################################################
|
| 1199 |
+
|
| 1200 |
+
class InverseHyperbolicFunction(Function):
|
| 1201 |
+
"""Base class for inverse hyperbolic functions."""
|
| 1202 |
+
|
| 1203 |
+
pass
|
| 1204 |
+
|
| 1205 |
+
|
| 1206 |
+
class asinh(InverseHyperbolicFunction):
|
| 1207 |
+
"""
|
| 1208 |
+
``asinh(x)`` is the inverse hyperbolic sine of ``x``.
|
| 1209 |
+
|
| 1210 |
+
The inverse hyperbolic sine function.
|
| 1211 |
+
|
| 1212 |
+
Examples
|
| 1213 |
+
========
|
| 1214 |
+
|
| 1215 |
+
>>> from sympy import asinh
|
| 1216 |
+
>>> from sympy.abc import x
|
| 1217 |
+
>>> asinh(x).diff(x)
|
| 1218 |
+
1/sqrt(x**2 + 1)
|
| 1219 |
+
>>> asinh(1)
|
| 1220 |
+
log(1 + sqrt(2))
|
| 1221 |
+
|
| 1222 |
+
See Also
|
| 1223 |
+
========
|
| 1224 |
+
|
| 1225 |
+
acosh, atanh, sinh
|
| 1226 |
+
"""
|
| 1227 |
+
|
| 1228 |
+
def fdiff(self, argindex=1):
|
| 1229 |
+
if argindex == 1:
|
| 1230 |
+
return 1/sqrt(self.args[0]**2 + 1)
|
| 1231 |
+
else:
|
| 1232 |
+
raise ArgumentIndexError(self, argindex)
|
| 1233 |
+
|
| 1234 |
+
@classmethod
|
| 1235 |
+
def eval(cls, arg):
|
| 1236 |
+
if arg.is_Number:
|
| 1237 |
+
if arg is S.NaN:
|
| 1238 |
+
return S.NaN
|
| 1239 |
+
elif arg is S.Infinity:
|
| 1240 |
+
return S.Infinity
|
| 1241 |
+
elif arg is S.NegativeInfinity:
|
| 1242 |
+
return S.NegativeInfinity
|
| 1243 |
+
elif arg.is_zero:
|
| 1244 |
+
return S.Zero
|
| 1245 |
+
elif arg is S.One:
|
| 1246 |
+
return log(sqrt(2) + 1)
|
| 1247 |
+
elif arg is S.NegativeOne:
|
| 1248 |
+
return log(sqrt(2) - 1)
|
| 1249 |
+
elif arg.is_negative:
|
| 1250 |
+
return -cls(-arg)
|
| 1251 |
+
else:
|
| 1252 |
+
if arg is S.ComplexInfinity:
|
| 1253 |
+
return S.ComplexInfinity
|
| 1254 |
+
|
| 1255 |
+
if arg.is_zero:
|
| 1256 |
+
return S.Zero
|
| 1257 |
+
|
| 1258 |
+
i_coeff = _imaginary_unit_as_coefficient(arg)
|
| 1259 |
+
|
| 1260 |
+
if i_coeff is not None:
|
| 1261 |
+
return I * asin(i_coeff)
|
| 1262 |
+
else:
|
| 1263 |
+
if arg.could_extract_minus_sign():
|
| 1264 |
+
return -cls(-arg)
|
| 1265 |
+
|
| 1266 |
+
if isinstance(arg, sinh) and arg.args[0].is_number:
|
| 1267 |
+
z = arg.args[0]
|
| 1268 |
+
if z.is_real:
|
| 1269 |
+
return z
|
| 1270 |
+
r, i = match_real_imag(z)
|
| 1271 |
+
if r is not None and i is not None:
|
| 1272 |
+
f = floor((i + pi/2)/pi)
|
| 1273 |
+
m = z - I*pi*f
|
| 1274 |
+
even = f.is_even
|
| 1275 |
+
if even is True:
|
| 1276 |
+
return m
|
| 1277 |
+
elif even is False:
|
| 1278 |
+
return -m
|
| 1279 |
+
|
| 1280 |
+
@staticmethod
|
| 1281 |
+
@cacheit
|
| 1282 |
+
def taylor_term(n, x, *previous_terms):
|
| 1283 |
+
if n < 0 or n % 2 == 0:
|
| 1284 |
+
return S.Zero
|
| 1285 |
+
else:
|
| 1286 |
+
x = sympify(x)
|
| 1287 |
+
if len(previous_terms) >= 2 and n > 2:
|
| 1288 |
+
p = previous_terms[-2]
|
| 1289 |
+
return -p * (n - 2)**2/(n*(n - 1)) * x**2
|
| 1290 |
+
else:
|
| 1291 |
+
k = (n - 1) // 2
|
| 1292 |
+
R = RisingFactorial(S.Half, k)
|
| 1293 |
+
F = factorial(k)
|
| 1294 |
+
return S.NegativeOne**k * R / F * x**n / n
|
| 1295 |
+
|
| 1296 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0): # asinh
|
| 1297 |
+
arg = self.args[0]
|
| 1298 |
+
x0 = arg.subs(x, 0).cancel()
|
| 1299 |
+
if x0.is_zero:
|
| 1300 |
+
return arg.as_leading_term(x)
|
| 1301 |
+
|
| 1302 |
+
if x0 is S.NaN:
|
| 1303 |
+
expr = self.func(arg.as_leading_term(x))
|
| 1304 |
+
if expr.is_finite:
|
| 1305 |
+
return expr
|
| 1306 |
+
else:
|
| 1307 |
+
return self
|
| 1308 |
+
|
| 1309 |
+
# Handling branch points
|
| 1310 |
+
if x0 in (-I, I, S.ComplexInfinity):
|
| 1311 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 1312 |
+
# Handling points lying on branch cuts (-I*oo, -I) U (I, I*oo)
|
| 1313 |
+
if (1 + x0**2).is_negative:
|
| 1314 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 1315 |
+
if re(ndir).is_positive:
|
| 1316 |
+
if im(x0).is_negative:
|
| 1317 |
+
return -self.func(x0) - I*pi
|
| 1318 |
+
elif re(ndir).is_negative:
|
| 1319 |
+
if im(x0).is_positive:
|
| 1320 |
+
return -self.func(x0) + I*pi
|
| 1321 |
+
else:
|
| 1322 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 1323 |
+
return self.func(x0)
|
| 1324 |
+
|
| 1325 |
+
def _eval_nseries(self, x, n, logx, cdir=0): # asinh
|
| 1326 |
+
arg = self.args[0]
|
| 1327 |
+
arg0 = arg.subs(x, 0)
|
| 1328 |
+
|
| 1329 |
+
# Handling branch points
|
| 1330 |
+
if arg0 in (I, -I):
|
| 1331 |
+
return self.rewrite(log)._eval_nseries(x, n, logx=logx, cdir=cdir)
|
| 1332 |
+
|
| 1333 |
+
res = Function._eval_nseries(self, x, n=n, logx=logx)
|
| 1334 |
+
if arg0 is S.ComplexInfinity:
|
| 1335 |
+
return res
|
| 1336 |
+
|
| 1337 |
+
# Handling points lying on branch cuts (-I*oo, -I) U (I, I*oo)
|
| 1338 |
+
if (1 + arg0**2).is_negative:
|
| 1339 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 1340 |
+
if re(ndir).is_positive:
|
| 1341 |
+
if im(arg0).is_negative:
|
| 1342 |
+
return -res - I*pi
|
| 1343 |
+
elif re(ndir).is_negative:
|
| 1344 |
+
if im(arg0).is_positive:
|
| 1345 |
+
return -res + I*pi
|
| 1346 |
+
else:
|
| 1347 |
+
return self.rewrite(log)._eval_nseries(x, n, logx=logx, cdir=cdir)
|
| 1348 |
+
return res
|
| 1349 |
+
|
| 1350 |
+
def _eval_rewrite_as_log(self, x, **kwargs):
|
| 1351 |
+
return log(x + sqrt(x**2 + 1))
|
| 1352 |
+
|
| 1353 |
+
_eval_rewrite_as_tractable = _eval_rewrite_as_log
|
| 1354 |
+
|
| 1355 |
+
def _eval_rewrite_as_atanh(self, x, **kwargs):
|
| 1356 |
+
return atanh(x/sqrt(1 + x**2))
|
| 1357 |
+
|
| 1358 |
+
def _eval_rewrite_as_acosh(self, x, **kwargs):
|
| 1359 |
+
ix = I*x
|
| 1360 |
+
return I*(sqrt(1 - ix)/sqrt(ix - 1) * acosh(ix) - pi/2)
|
| 1361 |
+
|
| 1362 |
+
def _eval_rewrite_as_asin(self, x, **kwargs):
|
| 1363 |
+
return -I * asin(I * x, evaluate=False)
|
| 1364 |
+
|
| 1365 |
+
def _eval_rewrite_as_acos(self, x, **kwargs):
|
| 1366 |
+
return I * acos(I * x, evaluate=False) - I*pi/2
|
| 1367 |
+
|
| 1368 |
+
def inverse(self, argindex=1):
|
| 1369 |
+
"""
|
| 1370 |
+
Returns the inverse of this function.
|
| 1371 |
+
"""
|
| 1372 |
+
return sinh
|
| 1373 |
+
|
| 1374 |
+
def _eval_is_zero(self):
|
| 1375 |
+
return self.args[0].is_zero
|
| 1376 |
+
|
| 1377 |
+
def _eval_is_extended_real(self):
|
| 1378 |
+
return self.args[0].is_extended_real
|
| 1379 |
+
|
| 1380 |
+
def _eval_is_finite(self):
|
| 1381 |
+
return self.args[0].is_finite
|
| 1382 |
+
|
| 1383 |
+
|
| 1384 |
+
class acosh(InverseHyperbolicFunction):
|
| 1385 |
+
"""
|
| 1386 |
+
``acosh(x)`` is the inverse hyperbolic cosine of ``x``.
|
| 1387 |
+
|
| 1388 |
+
The inverse hyperbolic cosine function.
|
| 1389 |
+
|
| 1390 |
+
Examples
|
| 1391 |
+
========
|
| 1392 |
+
|
| 1393 |
+
>>> from sympy import acosh
|
| 1394 |
+
>>> from sympy.abc import x
|
| 1395 |
+
>>> acosh(x).diff(x)
|
| 1396 |
+
1/(sqrt(x - 1)*sqrt(x + 1))
|
| 1397 |
+
>>> acosh(1)
|
| 1398 |
+
0
|
| 1399 |
+
|
| 1400 |
+
See Also
|
| 1401 |
+
========
|
| 1402 |
+
|
| 1403 |
+
asinh, atanh, cosh
|
| 1404 |
+
"""
|
| 1405 |
+
|
| 1406 |
+
def fdiff(self, argindex=1):
|
| 1407 |
+
if argindex == 1:
|
| 1408 |
+
arg = self.args[0]
|
| 1409 |
+
return 1/(sqrt(arg - 1)*sqrt(arg + 1))
|
| 1410 |
+
else:
|
| 1411 |
+
raise ArgumentIndexError(self, argindex)
|
| 1412 |
+
|
| 1413 |
+
@classmethod
|
| 1414 |
+
def eval(cls, arg):
|
| 1415 |
+
if arg.is_Number:
|
| 1416 |
+
if arg is S.NaN:
|
| 1417 |
+
return S.NaN
|
| 1418 |
+
elif arg is S.Infinity:
|
| 1419 |
+
return S.Infinity
|
| 1420 |
+
elif arg is S.NegativeInfinity:
|
| 1421 |
+
return S.Infinity
|
| 1422 |
+
elif arg.is_zero:
|
| 1423 |
+
return pi*I / 2
|
| 1424 |
+
elif arg is S.One:
|
| 1425 |
+
return S.Zero
|
| 1426 |
+
elif arg is S.NegativeOne:
|
| 1427 |
+
return pi*I
|
| 1428 |
+
|
| 1429 |
+
if arg.is_number:
|
| 1430 |
+
cst_table = _acosh_table()
|
| 1431 |
+
|
| 1432 |
+
if arg in cst_table:
|
| 1433 |
+
if arg.is_extended_real:
|
| 1434 |
+
return cst_table[arg]*I
|
| 1435 |
+
return cst_table[arg]
|
| 1436 |
+
|
| 1437 |
+
if arg is S.ComplexInfinity:
|
| 1438 |
+
return S.ComplexInfinity
|
| 1439 |
+
if arg == I*S.Infinity:
|
| 1440 |
+
return S.Infinity + I*pi/2
|
| 1441 |
+
if arg == -I*S.Infinity:
|
| 1442 |
+
return S.Infinity - I*pi/2
|
| 1443 |
+
|
| 1444 |
+
if arg.is_zero:
|
| 1445 |
+
return pi*I*S.Half
|
| 1446 |
+
|
| 1447 |
+
if isinstance(arg, cosh) and arg.args[0].is_number:
|
| 1448 |
+
z = arg.args[0]
|
| 1449 |
+
if z.is_real:
|
| 1450 |
+
return Abs(z)
|
| 1451 |
+
r, i = match_real_imag(z)
|
| 1452 |
+
if r is not None and i is not None:
|
| 1453 |
+
f = floor(i/pi)
|
| 1454 |
+
m = z - I*pi*f
|
| 1455 |
+
even = f.is_even
|
| 1456 |
+
if even is True:
|
| 1457 |
+
if r.is_nonnegative:
|
| 1458 |
+
return m
|
| 1459 |
+
elif r.is_negative:
|
| 1460 |
+
return -m
|
| 1461 |
+
elif even is False:
|
| 1462 |
+
m -= I*pi
|
| 1463 |
+
if r.is_nonpositive:
|
| 1464 |
+
return -m
|
| 1465 |
+
elif r.is_positive:
|
| 1466 |
+
return m
|
| 1467 |
+
|
| 1468 |
+
@staticmethod
|
| 1469 |
+
@cacheit
|
| 1470 |
+
def taylor_term(n, x, *previous_terms):
|
| 1471 |
+
if n == 0:
|
| 1472 |
+
return I*pi/2
|
| 1473 |
+
elif n < 0 or n % 2 == 0:
|
| 1474 |
+
return S.Zero
|
| 1475 |
+
else:
|
| 1476 |
+
x = sympify(x)
|
| 1477 |
+
if len(previous_terms) >= 2 and n > 2:
|
| 1478 |
+
p = previous_terms[-2]
|
| 1479 |
+
return p * (n - 2)**2/(n*(n - 1)) * x**2
|
| 1480 |
+
else:
|
| 1481 |
+
k = (n - 1) // 2
|
| 1482 |
+
R = RisingFactorial(S.Half, k)
|
| 1483 |
+
F = factorial(k)
|
| 1484 |
+
return -R / F * I * x**n / n
|
| 1485 |
+
|
| 1486 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0): # acosh
|
| 1487 |
+
arg = self.args[0]
|
| 1488 |
+
x0 = arg.subs(x, 0).cancel()
|
| 1489 |
+
# Handling branch points
|
| 1490 |
+
if x0 in (-S.One, S.Zero, S.One, S.ComplexInfinity):
|
| 1491 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 1492 |
+
|
| 1493 |
+
if x0 is S.NaN:
|
| 1494 |
+
expr = self.func(arg.as_leading_term(x))
|
| 1495 |
+
if expr.is_finite:
|
| 1496 |
+
return expr
|
| 1497 |
+
else:
|
| 1498 |
+
return self
|
| 1499 |
+
|
| 1500 |
+
# Handling points lying on branch cuts (-oo, 1)
|
| 1501 |
+
if (x0 - 1).is_negative:
|
| 1502 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 1503 |
+
if im(ndir).is_negative:
|
| 1504 |
+
if (x0 + 1).is_negative:
|
| 1505 |
+
return self.func(x0) - 2*I*pi
|
| 1506 |
+
return -self.func(x0)
|
| 1507 |
+
elif not im(ndir).is_positive:
|
| 1508 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 1509 |
+
return self.func(x0)
|
| 1510 |
+
|
| 1511 |
+
def _eval_nseries(self, x, n, logx, cdir=0): # acosh
|
| 1512 |
+
arg = self.args[0]
|
| 1513 |
+
arg0 = arg.subs(x, 0)
|
| 1514 |
+
|
| 1515 |
+
# Handling branch points
|
| 1516 |
+
if arg0 in (S.One, S.NegativeOne):
|
| 1517 |
+
return self.rewrite(log)._eval_nseries(x, n, logx=logx, cdir=cdir)
|
| 1518 |
+
|
| 1519 |
+
res = Function._eval_nseries(self, x, n=n, logx=logx)
|
| 1520 |
+
if arg0 is S.ComplexInfinity:
|
| 1521 |
+
return res
|
| 1522 |
+
|
| 1523 |
+
# Handling points lying on branch cuts (-oo, 1)
|
| 1524 |
+
if (arg0 - 1).is_negative:
|
| 1525 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 1526 |
+
if im(ndir).is_negative:
|
| 1527 |
+
if (arg0 + 1).is_negative:
|
| 1528 |
+
return res - 2*I*pi
|
| 1529 |
+
return -res
|
| 1530 |
+
elif not im(ndir).is_positive:
|
| 1531 |
+
return self.rewrite(log)._eval_nseries(x, n, logx=logx, cdir=cdir)
|
| 1532 |
+
return res
|
| 1533 |
+
|
| 1534 |
+
def _eval_rewrite_as_log(self, x, **kwargs):
|
| 1535 |
+
return log(x + sqrt(x + 1) * sqrt(x - 1))
|
| 1536 |
+
|
| 1537 |
+
_eval_rewrite_as_tractable = _eval_rewrite_as_log
|
| 1538 |
+
|
| 1539 |
+
def _eval_rewrite_as_acos(self, x, **kwargs):
|
| 1540 |
+
return sqrt(x - 1)/sqrt(1 - x) * acos(x)
|
| 1541 |
+
|
| 1542 |
+
def _eval_rewrite_as_asin(self, x, **kwargs):
|
| 1543 |
+
return sqrt(x - 1)/sqrt(1 - x) * (pi/2 - asin(x))
|
| 1544 |
+
|
| 1545 |
+
def _eval_rewrite_as_asinh(self, x, **kwargs):
|
| 1546 |
+
return sqrt(x - 1)/sqrt(1 - x) * (pi/2 + I*asinh(I*x, evaluate=False))
|
| 1547 |
+
|
| 1548 |
+
def _eval_rewrite_as_atanh(self, x, **kwargs):
|
| 1549 |
+
sxm1 = sqrt(x - 1)
|
| 1550 |
+
s1mx = sqrt(1 - x)
|
| 1551 |
+
sx2m1 = sqrt(x**2 - 1)
|
| 1552 |
+
return (pi/2*sxm1/s1mx*(1 - x * sqrt(1/x**2)) +
|
| 1553 |
+
sxm1*sqrt(x + 1)/sx2m1 * atanh(sx2m1/x))
|
| 1554 |
+
|
| 1555 |
+
def inverse(self, argindex=1):
|
| 1556 |
+
"""
|
| 1557 |
+
Returns the inverse of this function.
|
| 1558 |
+
"""
|
| 1559 |
+
return cosh
|
| 1560 |
+
|
| 1561 |
+
def _eval_is_zero(self):
|
| 1562 |
+
if (self.args[0] - 1).is_zero:
|
| 1563 |
+
return True
|
| 1564 |
+
|
| 1565 |
+
def _eval_is_extended_real(self):
|
| 1566 |
+
return fuzzy_and([self.args[0].is_extended_real, (self.args[0] - 1).is_extended_nonnegative])
|
| 1567 |
+
|
| 1568 |
+
def _eval_is_finite(self):
|
| 1569 |
+
return self.args[0].is_finite
|
| 1570 |
+
|
| 1571 |
+
|
| 1572 |
+
class atanh(InverseHyperbolicFunction):
|
| 1573 |
+
"""
|
| 1574 |
+
``atanh(x)`` is the inverse hyperbolic tangent of ``x``.
|
| 1575 |
+
|
| 1576 |
+
The inverse hyperbolic tangent function.
|
| 1577 |
+
|
| 1578 |
+
Examples
|
| 1579 |
+
========
|
| 1580 |
+
|
| 1581 |
+
>>> from sympy import atanh
|
| 1582 |
+
>>> from sympy.abc import x
|
| 1583 |
+
>>> atanh(x).diff(x)
|
| 1584 |
+
1/(1 - x**2)
|
| 1585 |
+
|
| 1586 |
+
See Also
|
| 1587 |
+
========
|
| 1588 |
+
|
| 1589 |
+
asinh, acosh, tanh
|
| 1590 |
+
"""
|
| 1591 |
+
|
| 1592 |
+
def fdiff(self, argindex=1):
|
| 1593 |
+
if argindex == 1:
|
| 1594 |
+
return 1/(1 - self.args[0]**2)
|
| 1595 |
+
else:
|
| 1596 |
+
raise ArgumentIndexError(self, argindex)
|
| 1597 |
+
|
| 1598 |
+
@classmethod
|
| 1599 |
+
def eval(cls, arg):
|
| 1600 |
+
if arg.is_Number:
|
| 1601 |
+
if arg is S.NaN:
|
| 1602 |
+
return S.NaN
|
| 1603 |
+
elif arg.is_zero:
|
| 1604 |
+
return S.Zero
|
| 1605 |
+
elif arg is S.One:
|
| 1606 |
+
return S.Infinity
|
| 1607 |
+
elif arg is S.NegativeOne:
|
| 1608 |
+
return S.NegativeInfinity
|
| 1609 |
+
elif arg is S.Infinity:
|
| 1610 |
+
return -I * atan(arg)
|
| 1611 |
+
elif arg is S.NegativeInfinity:
|
| 1612 |
+
return I * atan(-arg)
|
| 1613 |
+
elif arg.is_negative:
|
| 1614 |
+
return -cls(-arg)
|
| 1615 |
+
else:
|
| 1616 |
+
if arg is S.ComplexInfinity:
|
| 1617 |
+
from sympy.calculus.accumulationbounds import AccumBounds
|
| 1618 |
+
return I*AccumBounds(-pi/2, pi/2)
|
| 1619 |
+
|
| 1620 |
+
i_coeff = _imaginary_unit_as_coefficient(arg)
|
| 1621 |
+
|
| 1622 |
+
if i_coeff is not None:
|
| 1623 |
+
return I * atan(i_coeff)
|
| 1624 |
+
else:
|
| 1625 |
+
if arg.could_extract_minus_sign():
|
| 1626 |
+
return -cls(-arg)
|
| 1627 |
+
|
| 1628 |
+
if arg.is_zero:
|
| 1629 |
+
return S.Zero
|
| 1630 |
+
|
| 1631 |
+
if isinstance(arg, tanh) and arg.args[0].is_number:
|
| 1632 |
+
z = arg.args[0]
|
| 1633 |
+
if z.is_real:
|
| 1634 |
+
return z
|
| 1635 |
+
r, i = match_real_imag(z)
|
| 1636 |
+
if r is not None and i is not None:
|
| 1637 |
+
f = floor(2*i/pi)
|
| 1638 |
+
even = f.is_even
|
| 1639 |
+
m = z - I*f*pi/2
|
| 1640 |
+
if even is True:
|
| 1641 |
+
return m
|
| 1642 |
+
elif even is False:
|
| 1643 |
+
return m - I*pi/2
|
| 1644 |
+
|
| 1645 |
+
@staticmethod
|
| 1646 |
+
@cacheit
|
| 1647 |
+
def taylor_term(n, x, *previous_terms):
|
| 1648 |
+
if n < 0 or n % 2 == 0:
|
| 1649 |
+
return S.Zero
|
| 1650 |
+
else:
|
| 1651 |
+
x = sympify(x)
|
| 1652 |
+
return x**n / n
|
| 1653 |
+
|
| 1654 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0): # atanh
|
| 1655 |
+
arg = self.args[0]
|
| 1656 |
+
x0 = arg.subs(x, 0).cancel()
|
| 1657 |
+
if x0.is_zero:
|
| 1658 |
+
return arg.as_leading_term(x)
|
| 1659 |
+
if x0 is S.NaN:
|
| 1660 |
+
expr = self.func(arg.as_leading_term(x))
|
| 1661 |
+
if expr.is_finite:
|
| 1662 |
+
return expr
|
| 1663 |
+
else:
|
| 1664 |
+
return self
|
| 1665 |
+
|
| 1666 |
+
# Handling branch points
|
| 1667 |
+
if x0 in (-S.One, S.One, S.ComplexInfinity):
|
| 1668 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 1669 |
+
# Handling points lying on branch cuts (-oo, -1] U [1, oo)
|
| 1670 |
+
if (1 - x0**2).is_negative:
|
| 1671 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 1672 |
+
if im(ndir).is_negative:
|
| 1673 |
+
if x0.is_negative:
|
| 1674 |
+
return self.func(x0) - I*pi
|
| 1675 |
+
elif im(ndir).is_positive:
|
| 1676 |
+
if x0.is_positive:
|
| 1677 |
+
return self.func(x0) + I*pi
|
| 1678 |
+
else:
|
| 1679 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 1680 |
+
return self.func(x0)
|
| 1681 |
+
|
| 1682 |
+
def _eval_nseries(self, x, n, logx, cdir=0): # atanh
|
| 1683 |
+
arg = self.args[0]
|
| 1684 |
+
arg0 = arg.subs(x, 0)
|
| 1685 |
+
|
| 1686 |
+
# Handling branch points
|
| 1687 |
+
if arg0 in (S.One, S.NegativeOne):
|
| 1688 |
+
return self.rewrite(log)._eval_nseries(x, n, logx=logx, cdir=cdir)
|
| 1689 |
+
|
| 1690 |
+
res = Function._eval_nseries(self, x, n=n, logx=logx)
|
| 1691 |
+
if arg0 is S.ComplexInfinity:
|
| 1692 |
+
return res
|
| 1693 |
+
|
| 1694 |
+
# Handling points lying on branch cuts (-oo, -1] U [1, oo)
|
| 1695 |
+
if (1 - arg0**2).is_negative:
|
| 1696 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 1697 |
+
if im(ndir).is_negative:
|
| 1698 |
+
if arg0.is_negative:
|
| 1699 |
+
return res - I*pi
|
| 1700 |
+
elif im(ndir).is_positive:
|
| 1701 |
+
if arg0.is_positive:
|
| 1702 |
+
return res + I*pi
|
| 1703 |
+
else:
|
| 1704 |
+
return self.rewrite(log)._eval_nseries(x, n, logx=logx, cdir=cdir)
|
| 1705 |
+
return res
|
| 1706 |
+
|
| 1707 |
+
def _eval_rewrite_as_log(self, x, **kwargs):
|
| 1708 |
+
return (log(1 + x) - log(1 - x)) / 2
|
| 1709 |
+
|
| 1710 |
+
_eval_rewrite_as_tractable = _eval_rewrite_as_log
|
| 1711 |
+
|
| 1712 |
+
def _eval_rewrite_as_asinh(self, x, **kwargs):
|
| 1713 |
+
f = sqrt(1/(x**2 - 1))
|
| 1714 |
+
return (pi*x/(2*sqrt(-x**2)) -
|
| 1715 |
+
sqrt(-x)*sqrt(1 - x**2)/sqrt(x)*f*asinh(f))
|
| 1716 |
+
|
| 1717 |
+
def _eval_is_zero(self):
|
| 1718 |
+
if self.args[0].is_zero:
|
| 1719 |
+
return True
|
| 1720 |
+
|
| 1721 |
+
def _eval_is_extended_real(self):
|
| 1722 |
+
return fuzzy_and([self.args[0].is_extended_real, (1 - self.args[0]).is_nonnegative, (self.args[0] + 1).is_nonnegative])
|
| 1723 |
+
|
| 1724 |
+
def _eval_is_finite(self):
|
| 1725 |
+
return fuzzy_not(fuzzy_or([(self.args[0] - 1).is_zero, (self.args[0] + 1).is_zero]))
|
| 1726 |
+
|
| 1727 |
+
def _eval_is_imaginary(self):
|
| 1728 |
+
return self.args[0].is_imaginary
|
| 1729 |
+
|
| 1730 |
+
def inverse(self, argindex=1):
|
| 1731 |
+
"""
|
| 1732 |
+
Returns the inverse of this function.
|
| 1733 |
+
"""
|
| 1734 |
+
return tanh
|
| 1735 |
+
|
| 1736 |
+
|
| 1737 |
+
class acoth(InverseHyperbolicFunction):
|
| 1738 |
+
"""
|
| 1739 |
+
``acoth(x)`` is the inverse hyperbolic cotangent of ``x``.
|
| 1740 |
+
|
| 1741 |
+
The inverse hyperbolic cotangent function.
|
| 1742 |
+
|
| 1743 |
+
Examples
|
| 1744 |
+
========
|
| 1745 |
+
|
| 1746 |
+
>>> from sympy import acoth
|
| 1747 |
+
>>> from sympy.abc import x
|
| 1748 |
+
>>> acoth(x).diff(x)
|
| 1749 |
+
1/(1 - x**2)
|
| 1750 |
+
|
| 1751 |
+
See Also
|
| 1752 |
+
========
|
| 1753 |
+
|
| 1754 |
+
asinh, acosh, coth
|
| 1755 |
+
"""
|
| 1756 |
+
|
| 1757 |
+
def fdiff(self, argindex=1):
|
| 1758 |
+
if argindex == 1:
|
| 1759 |
+
return 1/(1 - self.args[0]**2)
|
| 1760 |
+
else:
|
| 1761 |
+
raise ArgumentIndexError(self, argindex)
|
| 1762 |
+
|
| 1763 |
+
@classmethod
|
| 1764 |
+
def eval(cls, arg):
|
| 1765 |
+
if arg.is_Number:
|
| 1766 |
+
if arg is S.NaN:
|
| 1767 |
+
return S.NaN
|
| 1768 |
+
elif arg is S.Infinity:
|
| 1769 |
+
return S.Zero
|
| 1770 |
+
elif arg is S.NegativeInfinity:
|
| 1771 |
+
return S.Zero
|
| 1772 |
+
elif arg.is_zero:
|
| 1773 |
+
return pi*I / 2
|
| 1774 |
+
elif arg is S.One:
|
| 1775 |
+
return S.Infinity
|
| 1776 |
+
elif arg is S.NegativeOne:
|
| 1777 |
+
return S.NegativeInfinity
|
| 1778 |
+
elif arg.is_negative:
|
| 1779 |
+
return -cls(-arg)
|
| 1780 |
+
else:
|
| 1781 |
+
if arg is S.ComplexInfinity:
|
| 1782 |
+
return S.Zero
|
| 1783 |
+
|
| 1784 |
+
i_coeff = _imaginary_unit_as_coefficient(arg)
|
| 1785 |
+
|
| 1786 |
+
if i_coeff is not None:
|
| 1787 |
+
return -I * acot(i_coeff)
|
| 1788 |
+
else:
|
| 1789 |
+
if arg.could_extract_minus_sign():
|
| 1790 |
+
return -cls(-arg)
|
| 1791 |
+
|
| 1792 |
+
if arg.is_zero:
|
| 1793 |
+
return pi*I*S.Half
|
| 1794 |
+
|
| 1795 |
+
@staticmethod
|
| 1796 |
+
@cacheit
|
| 1797 |
+
def taylor_term(n, x, *previous_terms):
|
| 1798 |
+
if n == 0:
|
| 1799 |
+
return -I*pi/2
|
| 1800 |
+
elif n < 0 or n % 2 == 0:
|
| 1801 |
+
return S.Zero
|
| 1802 |
+
else:
|
| 1803 |
+
x = sympify(x)
|
| 1804 |
+
return x**n / n
|
| 1805 |
+
|
| 1806 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0): # acoth
|
| 1807 |
+
arg = self.args[0]
|
| 1808 |
+
x0 = arg.subs(x, 0).cancel()
|
| 1809 |
+
if x0 is S.ComplexInfinity:
|
| 1810 |
+
return (1/arg).as_leading_term(x)
|
| 1811 |
+
if x0 is S.NaN:
|
| 1812 |
+
expr = self.func(arg.as_leading_term(x))
|
| 1813 |
+
if expr.is_finite:
|
| 1814 |
+
return expr
|
| 1815 |
+
else:
|
| 1816 |
+
return self
|
| 1817 |
+
|
| 1818 |
+
# Handling branch points
|
| 1819 |
+
if x0 in (-S.One, S.One, S.Zero):
|
| 1820 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 1821 |
+
# Handling points lying on branch cuts [-1, 1]
|
| 1822 |
+
if x0.is_real and (1 - x0**2).is_positive:
|
| 1823 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 1824 |
+
if im(ndir).is_negative:
|
| 1825 |
+
if x0.is_positive:
|
| 1826 |
+
return self.func(x0) + I*pi
|
| 1827 |
+
elif im(ndir).is_positive:
|
| 1828 |
+
if x0.is_negative:
|
| 1829 |
+
return self.func(x0) - I*pi
|
| 1830 |
+
else:
|
| 1831 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 1832 |
+
return self.func(x0)
|
| 1833 |
+
|
| 1834 |
+
def _eval_nseries(self, x, n, logx, cdir=0): # acoth
|
| 1835 |
+
arg = self.args[0]
|
| 1836 |
+
arg0 = arg.subs(x, 0)
|
| 1837 |
+
|
| 1838 |
+
# Handling branch points
|
| 1839 |
+
if arg0 in (S.One, S.NegativeOne):
|
| 1840 |
+
return self.rewrite(log)._eval_nseries(x, n, logx=logx, cdir=cdir)
|
| 1841 |
+
|
| 1842 |
+
res = Function._eval_nseries(self, x, n=n, logx=logx)
|
| 1843 |
+
if arg0 is S.ComplexInfinity:
|
| 1844 |
+
return res
|
| 1845 |
+
|
| 1846 |
+
# Handling points lying on branch cuts [-1, 1]
|
| 1847 |
+
if arg0.is_real and (1 - arg0**2).is_positive:
|
| 1848 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 1849 |
+
if im(ndir).is_negative:
|
| 1850 |
+
if arg0.is_positive:
|
| 1851 |
+
return res + I*pi
|
| 1852 |
+
elif im(ndir).is_positive:
|
| 1853 |
+
if arg0.is_negative:
|
| 1854 |
+
return res - I*pi
|
| 1855 |
+
else:
|
| 1856 |
+
return self.rewrite(log)._eval_nseries(x, n, logx=logx, cdir=cdir)
|
| 1857 |
+
return res
|
| 1858 |
+
|
| 1859 |
+
def _eval_rewrite_as_log(self, x, **kwargs):
|
| 1860 |
+
return (log(1 + 1/x) - log(1 - 1/x)) / 2
|
| 1861 |
+
|
| 1862 |
+
_eval_rewrite_as_tractable = _eval_rewrite_as_log
|
| 1863 |
+
|
| 1864 |
+
def _eval_rewrite_as_atanh(self, x, **kwargs):
|
| 1865 |
+
return atanh(1/x)
|
| 1866 |
+
|
| 1867 |
+
def _eval_rewrite_as_asinh(self, x, **kwargs):
|
| 1868 |
+
return (pi*I/2*(sqrt((x - 1)/x)*sqrt(x/(x - 1)) - sqrt(1 + 1/x)*sqrt(x/(x + 1))) +
|
| 1869 |
+
x*sqrt(1/x**2)*asinh(sqrt(1/(x**2 - 1))))
|
| 1870 |
+
|
| 1871 |
+
def inverse(self, argindex=1):
|
| 1872 |
+
"""
|
| 1873 |
+
Returns the inverse of this function.
|
| 1874 |
+
"""
|
| 1875 |
+
return coth
|
| 1876 |
+
|
| 1877 |
+
def _eval_is_extended_real(self):
|
| 1878 |
+
return fuzzy_and([self.args[0].is_extended_real, fuzzy_or([(self.args[0] - 1).is_extended_nonnegative, (self.args[0] + 1).is_extended_nonpositive])])
|
| 1879 |
+
|
| 1880 |
+
def _eval_is_finite(self):
|
| 1881 |
+
return fuzzy_not(fuzzy_or([(self.args[0] - 1).is_zero, (self.args[0] + 1).is_zero]))
|
| 1882 |
+
|
| 1883 |
+
|
| 1884 |
+
class asech(InverseHyperbolicFunction):
|
| 1885 |
+
"""
|
| 1886 |
+
``asech(x)`` is the inverse hyperbolic secant of ``x``.
|
| 1887 |
+
|
| 1888 |
+
The inverse hyperbolic secant function.
|
| 1889 |
+
|
| 1890 |
+
Examples
|
| 1891 |
+
========
|
| 1892 |
+
|
| 1893 |
+
>>> from sympy import asech, sqrt, S
|
| 1894 |
+
>>> from sympy.abc import x
|
| 1895 |
+
>>> asech(x).diff(x)
|
| 1896 |
+
-1/(x*sqrt(1 - x**2))
|
| 1897 |
+
>>> asech(1).diff(x)
|
| 1898 |
+
0
|
| 1899 |
+
>>> asech(1)
|
| 1900 |
+
0
|
| 1901 |
+
>>> asech(S(2))
|
| 1902 |
+
I*pi/3
|
| 1903 |
+
>>> asech(-sqrt(2))
|
| 1904 |
+
3*I*pi/4
|
| 1905 |
+
>>> asech((sqrt(6) - sqrt(2)))
|
| 1906 |
+
I*pi/12
|
| 1907 |
+
|
| 1908 |
+
See Also
|
| 1909 |
+
========
|
| 1910 |
+
|
| 1911 |
+
asinh, atanh, cosh, acoth
|
| 1912 |
+
|
| 1913 |
+
References
|
| 1914 |
+
==========
|
| 1915 |
+
|
| 1916 |
+
.. [1] https://en.wikipedia.org/wiki/Hyperbolic_function
|
| 1917 |
+
.. [2] https://dlmf.nist.gov/4.37
|
| 1918 |
+
.. [3] https://functions.wolfram.com/ElementaryFunctions/ArcSech/
|
| 1919 |
+
|
| 1920 |
+
"""
|
| 1921 |
+
|
| 1922 |
+
def fdiff(self, argindex=1):
|
| 1923 |
+
if argindex == 1:
|
| 1924 |
+
z = self.args[0]
|
| 1925 |
+
return -1/(z*sqrt(1 - z**2))
|
| 1926 |
+
else:
|
| 1927 |
+
raise ArgumentIndexError(self, argindex)
|
| 1928 |
+
|
| 1929 |
+
@classmethod
|
| 1930 |
+
def eval(cls, arg):
|
| 1931 |
+
if arg.is_Number:
|
| 1932 |
+
if arg is S.NaN:
|
| 1933 |
+
return S.NaN
|
| 1934 |
+
elif arg is S.Infinity:
|
| 1935 |
+
return pi*I / 2
|
| 1936 |
+
elif arg is S.NegativeInfinity:
|
| 1937 |
+
return pi*I / 2
|
| 1938 |
+
elif arg.is_zero:
|
| 1939 |
+
return S.Infinity
|
| 1940 |
+
elif arg is S.One:
|
| 1941 |
+
return S.Zero
|
| 1942 |
+
elif arg is S.NegativeOne:
|
| 1943 |
+
return pi*I
|
| 1944 |
+
|
| 1945 |
+
if arg.is_number:
|
| 1946 |
+
cst_table = _asech_table()
|
| 1947 |
+
|
| 1948 |
+
if arg in cst_table:
|
| 1949 |
+
if arg.is_extended_real:
|
| 1950 |
+
return cst_table[arg]*I
|
| 1951 |
+
return cst_table[arg]
|
| 1952 |
+
|
| 1953 |
+
if arg is S.ComplexInfinity:
|
| 1954 |
+
from sympy.calculus.accumulationbounds import AccumBounds
|
| 1955 |
+
return I*AccumBounds(-pi/2, pi/2)
|
| 1956 |
+
|
| 1957 |
+
if arg.is_zero:
|
| 1958 |
+
return S.Infinity
|
| 1959 |
+
|
| 1960 |
+
@staticmethod
|
| 1961 |
+
@cacheit
|
| 1962 |
+
def taylor_term(n, x, *previous_terms):
|
| 1963 |
+
if n == 0:
|
| 1964 |
+
return log(2 / x)
|
| 1965 |
+
elif n < 0 or n % 2 == 1:
|
| 1966 |
+
return S.Zero
|
| 1967 |
+
else:
|
| 1968 |
+
x = sympify(x)
|
| 1969 |
+
if len(previous_terms) > 2 and n > 2:
|
| 1970 |
+
p = previous_terms[-2]
|
| 1971 |
+
return p * ((n - 1)*(n-2)) * x**2/(4 * (n//2)**2)
|
| 1972 |
+
else:
|
| 1973 |
+
k = n // 2
|
| 1974 |
+
R = RisingFactorial(S.Half, k) * n
|
| 1975 |
+
F = factorial(k) * n // 2 * n // 2
|
| 1976 |
+
return -1 * R / F * x**n / 4
|
| 1977 |
+
|
| 1978 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0): # asech
|
| 1979 |
+
arg = self.args[0]
|
| 1980 |
+
x0 = arg.subs(x, 0).cancel()
|
| 1981 |
+
# Handling branch points
|
| 1982 |
+
if x0 in (-S.One, S.Zero, S.One, S.ComplexInfinity):
|
| 1983 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 1984 |
+
|
| 1985 |
+
if x0 is S.NaN:
|
| 1986 |
+
expr = self.func(arg.as_leading_term(x))
|
| 1987 |
+
if expr.is_finite:
|
| 1988 |
+
return expr
|
| 1989 |
+
else:
|
| 1990 |
+
return self
|
| 1991 |
+
|
| 1992 |
+
# Handling points lying on branch cuts (-oo, 0] U (1, oo)
|
| 1993 |
+
if x0.is_negative or (1 - x0).is_negative:
|
| 1994 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 1995 |
+
if im(ndir).is_positive:
|
| 1996 |
+
if x0.is_positive or (x0 + 1).is_negative:
|
| 1997 |
+
return -self.func(x0)
|
| 1998 |
+
return self.func(x0) - 2*I*pi
|
| 1999 |
+
elif not im(ndir).is_negative:
|
| 2000 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 2001 |
+
return self.func(x0)
|
| 2002 |
+
|
| 2003 |
+
def _eval_nseries(self, x, n, logx, cdir=0): # asech
|
| 2004 |
+
from sympy.series.order import O
|
| 2005 |
+
arg = self.args[0]
|
| 2006 |
+
arg0 = arg.subs(x, 0)
|
| 2007 |
+
|
| 2008 |
+
# Handling branch points
|
| 2009 |
+
if arg0 is S.One:
|
| 2010 |
+
t = Dummy('t', positive=True)
|
| 2011 |
+
ser = asech(S.One - t**2).rewrite(log).nseries(t, 0, 2*n)
|
| 2012 |
+
arg1 = S.One - self.args[0]
|
| 2013 |
+
f = arg1.as_leading_term(x)
|
| 2014 |
+
g = (arg1 - f)/ f
|
| 2015 |
+
if not g.is_meromorphic(x, 0): # cannot be expanded
|
| 2016 |
+
return O(1) if n == 0 else O(sqrt(x))
|
| 2017 |
+
res1 = sqrt(S.One + g)._eval_nseries(x, n=n, logx=logx)
|
| 2018 |
+
res = (res1.removeO()*sqrt(f)).expand()
|
| 2019 |
+
return ser.removeO().subs(t, res).expand().powsimp() + O(x**n, x)
|
| 2020 |
+
|
| 2021 |
+
if arg0 is S.NegativeOne:
|
| 2022 |
+
t = Dummy('t', positive=True)
|
| 2023 |
+
ser = asech(S.NegativeOne + t**2).rewrite(log).nseries(t, 0, 2*n)
|
| 2024 |
+
arg1 = S.One + self.args[0]
|
| 2025 |
+
f = arg1.as_leading_term(x)
|
| 2026 |
+
g = (arg1 - f)/ f
|
| 2027 |
+
if not g.is_meromorphic(x, 0): # cannot be expanded
|
| 2028 |
+
return O(1) if n == 0 else I*pi + O(sqrt(x))
|
| 2029 |
+
res1 = sqrt(S.One + g)._eval_nseries(x, n=n, logx=logx)
|
| 2030 |
+
res = (res1.removeO()*sqrt(f)).expand()
|
| 2031 |
+
return ser.removeO().subs(t, res).expand().powsimp() + O(x**n, x)
|
| 2032 |
+
|
| 2033 |
+
res = Function._eval_nseries(self, x, n=n, logx=logx)
|
| 2034 |
+
if arg0 is S.ComplexInfinity:
|
| 2035 |
+
return res
|
| 2036 |
+
|
| 2037 |
+
# Handling points lying on branch cuts (-oo, 0] U (1, oo)
|
| 2038 |
+
if arg0.is_negative or (1 - arg0).is_negative:
|
| 2039 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 2040 |
+
if im(ndir).is_positive:
|
| 2041 |
+
if arg0.is_positive or (arg0 + 1).is_negative:
|
| 2042 |
+
return -res
|
| 2043 |
+
return res - 2*I*pi
|
| 2044 |
+
elif not im(ndir).is_negative:
|
| 2045 |
+
return self.rewrite(log)._eval_nseries(x, n, logx=logx, cdir=cdir)
|
| 2046 |
+
return res
|
| 2047 |
+
|
| 2048 |
+
def inverse(self, argindex=1):
|
| 2049 |
+
"""
|
| 2050 |
+
Returns the inverse of this function.
|
| 2051 |
+
"""
|
| 2052 |
+
return sech
|
| 2053 |
+
|
| 2054 |
+
def _eval_rewrite_as_log(self, arg, **kwargs):
|
| 2055 |
+
return log(1/arg + sqrt(1/arg - 1) * sqrt(1/arg + 1))
|
| 2056 |
+
|
| 2057 |
+
_eval_rewrite_as_tractable = _eval_rewrite_as_log
|
| 2058 |
+
|
| 2059 |
+
def _eval_rewrite_as_acosh(self, arg, **kwargs):
|
| 2060 |
+
return acosh(1/arg)
|
| 2061 |
+
|
| 2062 |
+
def _eval_rewrite_as_asinh(self, arg, **kwargs):
|
| 2063 |
+
return sqrt(1/arg - 1)/sqrt(1 - 1/arg)*(I*asinh(I/arg, evaluate=False)
|
| 2064 |
+
+ pi*S.Half)
|
| 2065 |
+
|
| 2066 |
+
def _eval_rewrite_as_atanh(self, x, **kwargs):
|
| 2067 |
+
return (I*pi*(1 - sqrt(x)*sqrt(1/x) - I/2*sqrt(-x)/sqrt(x) - I/2*sqrt(x**2)/sqrt(-x**2))
|
| 2068 |
+
+ sqrt(1/(x + 1))*sqrt(x + 1)*atanh(sqrt(1 - x**2)))
|
| 2069 |
+
|
| 2070 |
+
def _eval_rewrite_as_acsch(self, x, **kwargs):
|
| 2071 |
+
return sqrt(1/x - 1)/sqrt(1 - 1/x)*(pi/2 - I*acsch(I*x, evaluate=False))
|
| 2072 |
+
|
| 2073 |
+
def _eval_is_extended_real(self):
|
| 2074 |
+
return fuzzy_and([self.args[0].is_extended_real, self.args[0].is_nonnegative, (1 - self.args[0]).is_nonnegative])
|
| 2075 |
+
|
| 2076 |
+
def _eval_is_finite(self):
|
| 2077 |
+
return fuzzy_not(self.args[0].is_zero)
|
| 2078 |
+
|
| 2079 |
+
|
| 2080 |
+
class acsch(InverseHyperbolicFunction):
|
| 2081 |
+
"""
|
| 2082 |
+
``acsch(x)`` is the inverse hyperbolic cosecant of ``x``.
|
| 2083 |
+
|
| 2084 |
+
The inverse hyperbolic cosecant function.
|
| 2085 |
+
|
| 2086 |
+
Examples
|
| 2087 |
+
========
|
| 2088 |
+
|
| 2089 |
+
>>> from sympy import acsch, sqrt, I
|
| 2090 |
+
>>> from sympy.abc import x
|
| 2091 |
+
>>> acsch(x).diff(x)
|
| 2092 |
+
-1/(x**2*sqrt(1 + x**(-2)))
|
| 2093 |
+
>>> acsch(1).diff(x)
|
| 2094 |
+
0
|
| 2095 |
+
>>> acsch(1)
|
| 2096 |
+
log(1 + sqrt(2))
|
| 2097 |
+
>>> acsch(I)
|
| 2098 |
+
-I*pi/2
|
| 2099 |
+
>>> acsch(-2*I)
|
| 2100 |
+
I*pi/6
|
| 2101 |
+
>>> acsch(I*(sqrt(6) - sqrt(2)))
|
| 2102 |
+
-5*I*pi/12
|
| 2103 |
+
|
| 2104 |
+
See Also
|
| 2105 |
+
========
|
| 2106 |
+
|
| 2107 |
+
asinh
|
| 2108 |
+
|
| 2109 |
+
References
|
| 2110 |
+
==========
|
| 2111 |
+
|
| 2112 |
+
.. [1] https://en.wikipedia.org/wiki/Hyperbolic_function
|
| 2113 |
+
.. [2] https://dlmf.nist.gov/4.37
|
| 2114 |
+
.. [3] https://functions.wolfram.com/ElementaryFunctions/ArcCsch/
|
| 2115 |
+
|
| 2116 |
+
"""
|
| 2117 |
+
|
| 2118 |
+
def fdiff(self, argindex=1):
|
| 2119 |
+
if argindex == 1:
|
| 2120 |
+
z = self.args[0]
|
| 2121 |
+
return -1/(z**2*sqrt(1 + 1/z**2))
|
| 2122 |
+
else:
|
| 2123 |
+
raise ArgumentIndexError(self, argindex)
|
| 2124 |
+
|
| 2125 |
+
@classmethod
|
| 2126 |
+
def eval(cls, arg):
|
| 2127 |
+
if arg.is_Number:
|
| 2128 |
+
if arg is S.NaN:
|
| 2129 |
+
return S.NaN
|
| 2130 |
+
elif arg is S.Infinity:
|
| 2131 |
+
return S.Zero
|
| 2132 |
+
elif arg is S.NegativeInfinity:
|
| 2133 |
+
return S.Zero
|
| 2134 |
+
elif arg.is_zero:
|
| 2135 |
+
return S.ComplexInfinity
|
| 2136 |
+
elif arg is S.One:
|
| 2137 |
+
return log(1 + sqrt(2))
|
| 2138 |
+
elif arg is S.NegativeOne:
|
| 2139 |
+
return - log(1 + sqrt(2))
|
| 2140 |
+
|
| 2141 |
+
if arg.is_number:
|
| 2142 |
+
cst_table = _acsch_table()
|
| 2143 |
+
|
| 2144 |
+
if arg in cst_table:
|
| 2145 |
+
return cst_table[arg]*I
|
| 2146 |
+
|
| 2147 |
+
if arg is S.ComplexInfinity:
|
| 2148 |
+
return S.Zero
|
| 2149 |
+
|
| 2150 |
+
if arg.is_infinite:
|
| 2151 |
+
return S.Zero
|
| 2152 |
+
|
| 2153 |
+
if arg.is_zero:
|
| 2154 |
+
return S.ComplexInfinity
|
| 2155 |
+
|
| 2156 |
+
if arg.could_extract_minus_sign():
|
| 2157 |
+
return -cls(-arg)
|
| 2158 |
+
|
| 2159 |
+
@staticmethod
|
| 2160 |
+
@cacheit
|
| 2161 |
+
def taylor_term(n, x, *previous_terms):
|
| 2162 |
+
if n == 0:
|
| 2163 |
+
return log(2 / x)
|
| 2164 |
+
elif n < 0 or n % 2 == 1:
|
| 2165 |
+
return S.Zero
|
| 2166 |
+
else:
|
| 2167 |
+
x = sympify(x)
|
| 2168 |
+
if len(previous_terms) > 2 and n > 2:
|
| 2169 |
+
p = previous_terms[-2]
|
| 2170 |
+
return -p * ((n - 1)*(n-2)) * x**2/(4 * (n//2)**2)
|
| 2171 |
+
else:
|
| 2172 |
+
k = n // 2
|
| 2173 |
+
R = RisingFactorial(S.Half, k) * n
|
| 2174 |
+
F = factorial(k) * n // 2 * n // 2
|
| 2175 |
+
return S.NegativeOne**(k +1) * R / F * x**n / 4
|
| 2176 |
+
|
| 2177 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0): # acsch
|
| 2178 |
+
arg = self.args[0]
|
| 2179 |
+
x0 = arg.subs(x, 0).cancel()
|
| 2180 |
+
# Handling branch points
|
| 2181 |
+
if x0 in (-I, I, S.Zero):
|
| 2182 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 2183 |
+
|
| 2184 |
+
if x0 is S.NaN:
|
| 2185 |
+
expr = self.func(arg.as_leading_term(x))
|
| 2186 |
+
if expr.is_finite:
|
| 2187 |
+
return expr
|
| 2188 |
+
else:
|
| 2189 |
+
return self
|
| 2190 |
+
|
| 2191 |
+
if x0 is S.ComplexInfinity:
|
| 2192 |
+
return (1/arg).as_leading_term(x)
|
| 2193 |
+
# Handling points lying on branch cuts (-I, I)
|
| 2194 |
+
if x0.is_imaginary and (1 + x0**2).is_positive:
|
| 2195 |
+
ndir = arg.dir(x, cdir if cdir else 1)
|
| 2196 |
+
if re(ndir).is_positive:
|
| 2197 |
+
if im(x0).is_positive:
|
| 2198 |
+
return -self.func(x0) - I*pi
|
| 2199 |
+
elif re(ndir).is_negative:
|
| 2200 |
+
if im(x0).is_negative:
|
| 2201 |
+
return -self.func(x0) + I*pi
|
| 2202 |
+
else:
|
| 2203 |
+
return self.rewrite(log)._eval_as_leading_term(x, logx=logx, cdir=cdir)
|
| 2204 |
+
return self.func(x0)
|
| 2205 |
+
|
| 2206 |
+
def _eval_nseries(self, x, n, logx, cdir=0): # acsch
|
| 2207 |
+
from sympy.series.order import O
|
| 2208 |
+
arg = self.args[0]
|
| 2209 |
+
arg0 = arg.subs(x, 0)
|
| 2210 |
+
|
| 2211 |
+
# Handling branch points
|
| 2212 |
+
if arg0 is I:
|
| 2213 |
+
t = Dummy('t', positive=True)
|
| 2214 |
+
ser = acsch(I + t**2).rewrite(log).nseries(t, 0, 2*n)
|
| 2215 |
+
arg1 = -I + self.args[0]
|
| 2216 |
+
f = arg1.as_leading_term(x)
|
| 2217 |
+
g = (arg1 - f)/ f
|
| 2218 |
+
if not g.is_meromorphic(x, 0): # cannot be expanded
|
| 2219 |
+
return O(1) if n == 0 else -I*pi/2 + O(sqrt(x))
|
| 2220 |
+
res1 = sqrt(S.One + g)._eval_nseries(x, n=n, logx=logx)
|
| 2221 |
+
res = (res1.removeO()*sqrt(f)).expand()
|
| 2222 |
+
res = ser.removeO().subs(t, res).expand().powsimp() + O(x**n, x)
|
| 2223 |
+
return res
|
| 2224 |
+
|
| 2225 |
+
if arg0 == S.NegativeOne*I:
|
| 2226 |
+
t = Dummy('t', positive=True)
|
| 2227 |
+
ser = acsch(-I + t**2).rewrite(log).nseries(t, 0, 2*n)
|
| 2228 |
+
arg1 = I + self.args[0]
|
| 2229 |
+
f = arg1.as_leading_term(x)
|
| 2230 |
+
g = (arg1 - f)/ f
|
| 2231 |
+
if not g.is_meromorphic(x, 0): # cannot be expanded
|
| 2232 |
+
return O(1) if n == 0 else I*pi/2 + O(sqrt(x))
|
| 2233 |
+
res1 = sqrt(S.One + g)._eval_nseries(x, n=n, logx=logx)
|
| 2234 |
+
res = (res1.removeO()*sqrt(f)).expand()
|
| 2235 |
+
return ser.removeO().subs(t, res).expand().powsimp() + O(x**n, x)
|
| 2236 |
+
|
| 2237 |
+
res = Function._eval_nseries(self, x, n=n, logx=logx)
|
| 2238 |
+
if arg0 is S.ComplexInfinity:
|
| 2239 |
+
return res
|
| 2240 |
+
|
| 2241 |
+
# Handling points lying on branch cuts (-I, I)
|
| 2242 |
+
if arg0.is_imaginary and (1 + arg0**2).is_positive:
|
| 2243 |
+
ndir = self.args[0].dir(x, cdir if cdir else 1)
|
| 2244 |
+
if re(ndir).is_positive:
|
| 2245 |
+
if im(arg0).is_positive:
|
| 2246 |
+
return -res - I*pi
|
| 2247 |
+
elif re(ndir).is_negative:
|
| 2248 |
+
if im(arg0).is_negative:
|
| 2249 |
+
return -res + I*pi
|
| 2250 |
+
else:
|
| 2251 |
+
return self.rewrite(log)._eval_nseries(x, n, logx=logx, cdir=cdir)
|
| 2252 |
+
return res
|
| 2253 |
+
|
| 2254 |
+
def inverse(self, argindex=1):
|
| 2255 |
+
"""
|
| 2256 |
+
Returns the inverse of this function.
|
| 2257 |
+
"""
|
| 2258 |
+
return csch
|
| 2259 |
+
|
| 2260 |
+
def _eval_rewrite_as_log(self, arg, **kwargs):
|
| 2261 |
+
return log(1/arg + sqrt(1/arg**2 + 1))
|
| 2262 |
+
|
| 2263 |
+
_eval_rewrite_as_tractable = _eval_rewrite_as_log
|
| 2264 |
+
|
| 2265 |
+
def _eval_rewrite_as_asinh(self, arg, **kwargs):
|
| 2266 |
+
return asinh(1/arg)
|
| 2267 |
+
|
| 2268 |
+
def _eval_rewrite_as_acosh(self, arg, **kwargs):
|
| 2269 |
+
return I*(sqrt(1 - I/arg)/sqrt(I/arg - 1)*
|
| 2270 |
+
acosh(I/arg, evaluate=False) - pi*S.Half)
|
| 2271 |
+
|
| 2272 |
+
def _eval_rewrite_as_atanh(self, arg, **kwargs):
|
| 2273 |
+
arg2 = arg**2
|
| 2274 |
+
arg2p1 = arg2 + 1
|
| 2275 |
+
return sqrt(-arg2)/arg*(pi*S.Half -
|
| 2276 |
+
sqrt(-arg2p1**2)/arg2p1*atanh(sqrt(arg2p1)))
|
| 2277 |
+
|
| 2278 |
+
def _eval_is_zero(self):
|
| 2279 |
+
return self.args[0].is_infinite
|
| 2280 |
+
|
| 2281 |
+
def _eval_is_extended_real(self):
|
| 2282 |
+
return self.args[0].is_extended_real
|
| 2283 |
+
|
| 2284 |
+
def _eval_is_finite(self):
|
| 2285 |
+
return fuzzy_not(self.args[0].is_zero)
|
parrot/lib/python3.10/site-packages/sympy/functions/elementary/piecewise.py
ADDED
|
@@ -0,0 +1,1516 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import S, Function, diff, Tuple, Dummy, Mul
|
| 2 |
+
from sympy.core.basic import Basic, as_Basic
|
| 3 |
+
from sympy.core.numbers import Rational, NumberSymbol, _illegal
|
| 4 |
+
from sympy.core.parameters import global_parameters
|
| 5 |
+
from sympy.core.relational import (Lt, Gt, Eq, Ne, Relational,
|
| 6 |
+
_canonical, _canonical_coeff)
|
| 7 |
+
from sympy.core.sorting import ordered
|
| 8 |
+
from sympy.functions.elementary.miscellaneous import Max, Min
|
| 9 |
+
from sympy.logic.boolalg import (And, Boolean, distribute_and_over_or, Not,
|
| 10 |
+
true, false, Or, ITE, simplify_logic, to_cnf, distribute_or_over_and)
|
| 11 |
+
from sympy.utilities.iterables import uniq, sift, common_prefix
|
| 12 |
+
from sympy.utilities.misc import filldedent, func_name
|
| 13 |
+
|
| 14 |
+
from itertools import product
|
| 15 |
+
|
| 16 |
+
Undefined = S.NaN # Piecewise()
|
| 17 |
+
|
| 18 |
+
class ExprCondPair(Tuple):
|
| 19 |
+
"""Represents an expression, condition pair."""
|
| 20 |
+
|
| 21 |
+
def __new__(cls, expr, cond):
|
| 22 |
+
expr = as_Basic(expr)
|
| 23 |
+
if cond == True:
|
| 24 |
+
return Tuple.__new__(cls, expr, true)
|
| 25 |
+
elif cond == False:
|
| 26 |
+
return Tuple.__new__(cls, expr, false)
|
| 27 |
+
elif isinstance(cond, Basic) and cond.has(Piecewise):
|
| 28 |
+
cond = piecewise_fold(cond)
|
| 29 |
+
if isinstance(cond, Piecewise):
|
| 30 |
+
cond = cond.rewrite(ITE)
|
| 31 |
+
|
| 32 |
+
if not isinstance(cond, Boolean):
|
| 33 |
+
raise TypeError(filldedent('''
|
| 34 |
+
Second argument must be a Boolean,
|
| 35 |
+
not `%s`''' % func_name(cond)))
|
| 36 |
+
return Tuple.__new__(cls, expr, cond)
|
| 37 |
+
|
| 38 |
+
@property
|
| 39 |
+
def expr(self):
|
| 40 |
+
"""
|
| 41 |
+
Returns the expression of this pair.
|
| 42 |
+
"""
|
| 43 |
+
return self.args[0]
|
| 44 |
+
|
| 45 |
+
@property
|
| 46 |
+
def cond(self):
|
| 47 |
+
"""
|
| 48 |
+
Returns the condition of this pair.
|
| 49 |
+
"""
|
| 50 |
+
return self.args[1]
|
| 51 |
+
|
| 52 |
+
@property
|
| 53 |
+
def is_commutative(self):
|
| 54 |
+
return self.expr.is_commutative
|
| 55 |
+
|
| 56 |
+
def __iter__(self):
|
| 57 |
+
yield self.expr
|
| 58 |
+
yield self.cond
|
| 59 |
+
|
| 60 |
+
def _eval_simplify(self, **kwargs):
|
| 61 |
+
return self.func(*[a.simplify(**kwargs) for a in self.args])
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class Piecewise(Function):
|
| 65 |
+
"""
|
| 66 |
+
Represents a piecewise function.
|
| 67 |
+
|
| 68 |
+
Usage:
|
| 69 |
+
|
| 70 |
+
Piecewise( (expr,cond), (expr,cond), ... )
|
| 71 |
+
- Each argument is a 2-tuple defining an expression and condition
|
| 72 |
+
- The conds are evaluated in turn returning the first that is True.
|
| 73 |
+
If any of the evaluated conds are not explicitly False,
|
| 74 |
+
e.g. ``x < 1``, the function is returned in symbolic form.
|
| 75 |
+
- If the function is evaluated at a place where all conditions are False,
|
| 76 |
+
nan will be returned.
|
| 77 |
+
- Pairs where the cond is explicitly False, will be removed and no pair
|
| 78 |
+
appearing after a True condition will ever be retained. If a single
|
| 79 |
+
pair with a True condition remains, it will be returned, even when
|
| 80 |
+
evaluation is False.
|
| 81 |
+
|
| 82 |
+
Examples
|
| 83 |
+
========
|
| 84 |
+
|
| 85 |
+
>>> from sympy import Piecewise, log, piecewise_fold
|
| 86 |
+
>>> from sympy.abc import x, y
|
| 87 |
+
>>> f = x**2
|
| 88 |
+
>>> g = log(x)
|
| 89 |
+
>>> p = Piecewise((0, x < -1), (f, x <= 1), (g, True))
|
| 90 |
+
>>> p.subs(x,1)
|
| 91 |
+
1
|
| 92 |
+
>>> p.subs(x,5)
|
| 93 |
+
log(5)
|
| 94 |
+
|
| 95 |
+
Booleans can contain Piecewise elements:
|
| 96 |
+
|
| 97 |
+
>>> cond = (x < y).subs(x, Piecewise((2, x < 0), (3, True))); cond
|
| 98 |
+
Piecewise((2, x < 0), (3, True)) < y
|
| 99 |
+
|
| 100 |
+
The folded version of this results in a Piecewise whose
|
| 101 |
+
expressions are Booleans:
|
| 102 |
+
|
| 103 |
+
>>> folded_cond = piecewise_fold(cond); folded_cond
|
| 104 |
+
Piecewise((2 < y, x < 0), (3 < y, True))
|
| 105 |
+
|
| 106 |
+
When a Boolean containing Piecewise (like cond) or a Piecewise
|
| 107 |
+
with Boolean expressions (like folded_cond) is used as a condition,
|
| 108 |
+
it is converted to an equivalent :class:`~.ITE` object:
|
| 109 |
+
|
| 110 |
+
>>> Piecewise((1, folded_cond))
|
| 111 |
+
Piecewise((1, ITE(x < 0, y > 2, y > 3)))
|
| 112 |
+
|
| 113 |
+
When a condition is an ``ITE``, it will be converted to a simplified
|
| 114 |
+
Boolean expression:
|
| 115 |
+
|
| 116 |
+
>>> piecewise_fold(_)
|
| 117 |
+
Piecewise((1, ((x >= 0) | (y > 2)) & ((y > 3) | (x < 0))))
|
| 118 |
+
|
| 119 |
+
See Also
|
| 120 |
+
========
|
| 121 |
+
|
| 122 |
+
piecewise_fold
|
| 123 |
+
piecewise_exclusive
|
| 124 |
+
ITE
|
| 125 |
+
"""
|
| 126 |
+
|
| 127 |
+
nargs = None
|
| 128 |
+
is_Piecewise = True
|
| 129 |
+
|
| 130 |
+
def __new__(cls, *args, **options):
|
| 131 |
+
if len(args) == 0:
|
| 132 |
+
raise TypeError("At least one (expr, cond) pair expected.")
|
| 133 |
+
# (Try to) sympify args first
|
| 134 |
+
newargs = []
|
| 135 |
+
for ec in args:
|
| 136 |
+
# ec could be a ExprCondPair or a tuple
|
| 137 |
+
pair = ExprCondPair(*getattr(ec, 'args', ec))
|
| 138 |
+
cond = pair.cond
|
| 139 |
+
if cond is false:
|
| 140 |
+
continue
|
| 141 |
+
newargs.append(pair)
|
| 142 |
+
if cond is true:
|
| 143 |
+
break
|
| 144 |
+
|
| 145 |
+
eval = options.pop('evaluate', global_parameters.evaluate)
|
| 146 |
+
if eval:
|
| 147 |
+
r = cls.eval(*newargs)
|
| 148 |
+
if r is not None:
|
| 149 |
+
return r
|
| 150 |
+
elif len(newargs) == 1 and newargs[0].cond == True:
|
| 151 |
+
return newargs[0].expr
|
| 152 |
+
|
| 153 |
+
return Basic.__new__(cls, *newargs, **options)
|
| 154 |
+
|
| 155 |
+
@classmethod
|
| 156 |
+
def eval(cls, *_args):
|
| 157 |
+
"""Either return a modified version of the args or, if no
|
| 158 |
+
modifications were made, return None.
|
| 159 |
+
|
| 160 |
+
Modifications that are made here:
|
| 161 |
+
|
| 162 |
+
1. relationals are made canonical
|
| 163 |
+
2. any False conditions are dropped
|
| 164 |
+
3. any repeat of a previous condition is ignored
|
| 165 |
+
4. any args past one with a true condition are dropped
|
| 166 |
+
|
| 167 |
+
If there are no args left, nan will be returned.
|
| 168 |
+
If there is a single arg with a True condition, its
|
| 169 |
+
corresponding expression will be returned.
|
| 170 |
+
|
| 171 |
+
EXAMPLES
|
| 172 |
+
========
|
| 173 |
+
|
| 174 |
+
>>> from sympy import Piecewise
|
| 175 |
+
>>> from sympy.abc import x
|
| 176 |
+
>>> cond = -x < -1
|
| 177 |
+
>>> args = [(1, cond), (4, cond), (3, False), (2, True), (5, x < 1)]
|
| 178 |
+
>>> Piecewise(*args, evaluate=False)
|
| 179 |
+
Piecewise((1, -x < -1), (4, -x < -1), (2, True))
|
| 180 |
+
>>> Piecewise(*args)
|
| 181 |
+
Piecewise((1, x > 1), (2, True))
|
| 182 |
+
"""
|
| 183 |
+
if not _args:
|
| 184 |
+
return Undefined
|
| 185 |
+
|
| 186 |
+
if len(_args) == 1 and _args[0][-1] == True:
|
| 187 |
+
return _args[0][0]
|
| 188 |
+
|
| 189 |
+
newargs = _piecewise_collapse_arguments(_args)
|
| 190 |
+
|
| 191 |
+
# some conditions may have been redundant
|
| 192 |
+
missing = len(newargs) != len(_args)
|
| 193 |
+
# some conditions may have changed
|
| 194 |
+
same = all(a == b for a, b in zip(newargs, _args))
|
| 195 |
+
# if either change happened we return the expr with the
|
| 196 |
+
# updated args
|
| 197 |
+
if not newargs:
|
| 198 |
+
raise ValueError(filldedent('''
|
| 199 |
+
There are no conditions (or none that
|
| 200 |
+
are not trivially false) to define an
|
| 201 |
+
expression.'''))
|
| 202 |
+
if missing or not same:
|
| 203 |
+
return cls(*newargs)
|
| 204 |
+
|
| 205 |
+
def doit(self, **hints):
|
| 206 |
+
"""
|
| 207 |
+
Evaluate this piecewise function.
|
| 208 |
+
"""
|
| 209 |
+
newargs = []
|
| 210 |
+
for e, c in self.args:
|
| 211 |
+
if hints.get('deep', True):
|
| 212 |
+
if isinstance(e, Basic):
|
| 213 |
+
newe = e.doit(**hints)
|
| 214 |
+
if newe != self:
|
| 215 |
+
e = newe
|
| 216 |
+
if isinstance(c, Basic):
|
| 217 |
+
c = c.doit(**hints)
|
| 218 |
+
newargs.append((e, c))
|
| 219 |
+
return self.func(*newargs)
|
| 220 |
+
|
| 221 |
+
def _eval_simplify(self, **kwargs):
|
| 222 |
+
return piecewise_simplify(self, **kwargs)
|
| 223 |
+
|
| 224 |
+
def _eval_as_leading_term(self, x, logx=None, cdir=0):
|
| 225 |
+
for e, c in self.args:
|
| 226 |
+
if c == True or c.subs(x, 0) == True:
|
| 227 |
+
return e.as_leading_term(x)
|
| 228 |
+
|
| 229 |
+
def _eval_adjoint(self):
|
| 230 |
+
return self.func(*[(e.adjoint(), c) for e, c in self.args])
|
| 231 |
+
|
| 232 |
+
def _eval_conjugate(self):
|
| 233 |
+
return self.func(*[(e.conjugate(), c) for e, c in self.args])
|
| 234 |
+
|
| 235 |
+
def _eval_derivative(self, x):
|
| 236 |
+
return self.func(*[(diff(e, x), c) for e, c in self.args])
|
| 237 |
+
|
| 238 |
+
def _eval_evalf(self, prec):
|
| 239 |
+
return self.func(*[(e._evalf(prec), c) for e, c in self.args])
|
| 240 |
+
|
| 241 |
+
def _eval_is_meromorphic(self, x, a):
|
| 242 |
+
# Conditions often implicitly assume that the argument is real.
|
| 243 |
+
# Hence, there needs to be some check for as_set.
|
| 244 |
+
if not a.is_real:
|
| 245 |
+
return None
|
| 246 |
+
|
| 247 |
+
# Then, scan ExprCondPairs in the given order to find a piece that would contain a,
|
| 248 |
+
# possibly as a boundary point.
|
| 249 |
+
for e, c in self.args:
|
| 250 |
+
cond = c.subs(x, a)
|
| 251 |
+
|
| 252 |
+
if cond.is_Relational:
|
| 253 |
+
return None
|
| 254 |
+
if a in c.as_set().boundary:
|
| 255 |
+
return None
|
| 256 |
+
# Apply expression if a is an interior point of the domain of e.
|
| 257 |
+
if cond:
|
| 258 |
+
return e._eval_is_meromorphic(x, a)
|
| 259 |
+
|
| 260 |
+
def piecewise_integrate(self, x, **kwargs):
|
| 261 |
+
"""Return the Piecewise with each expression being
|
| 262 |
+
replaced with its antiderivative. To obtain a continuous
|
| 263 |
+
antiderivative, use the :func:`~.integrate` function or method.
|
| 264 |
+
|
| 265 |
+
Examples
|
| 266 |
+
========
|
| 267 |
+
|
| 268 |
+
>>> from sympy import Piecewise
|
| 269 |
+
>>> from sympy.abc import x
|
| 270 |
+
>>> p = Piecewise((0, x < 0), (1, x < 1), (2, True))
|
| 271 |
+
>>> p.piecewise_integrate(x)
|
| 272 |
+
Piecewise((0, x < 0), (x, x < 1), (2*x, True))
|
| 273 |
+
|
| 274 |
+
Note that this does not give a continuous function, e.g.
|
| 275 |
+
at x = 1 the 3rd condition applies and the antiderivative
|
| 276 |
+
there is 2*x so the value of the antiderivative is 2:
|
| 277 |
+
|
| 278 |
+
>>> anti = _
|
| 279 |
+
>>> anti.subs(x, 1)
|
| 280 |
+
2
|
| 281 |
+
|
| 282 |
+
The continuous derivative accounts for the integral *up to*
|
| 283 |
+
the point of interest, however:
|
| 284 |
+
|
| 285 |
+
>>> p.integrate(x)
|
| 286 |
+
Piecewise((0, x < 0), (x, x < 1), (2*x - 1, True))
|
| 287 |
+
>>> _.subs(x, 1)
|
| 288 |
+
1
|
| 289 |
+
|
| 290 |
+
See Also
|
| 291 |
+
========
|
| 292 |
+
Piecewise._eval_integral
|
| 293 |
+
"""
|
| 294 |
+
from sympy.integrals import integrate
|
| 295 |
+
return self.func(*[(integrate(e, x, **kwargs), c) for e, c in self.args])
|
| 296 |
+
|
| 297 |
+
def _handle_irel(self, x, handler):
|
| 298 |
+
"""Return either None (if the conditions of self depend only on x) else
|
| 299 |
+
a Piecewise expression whose expressions (handled by the handler that
|
| 300 |
+
was passed) are paired with the governing x-independent relationals,
|
| 301 |
+
e.g. Piecewise((A, a(x) & b(y)), (B, c(x) | c(y)) ->
|
| 302 |
+
Piecewise(
|
| 303 |
+
(handler(Piecewise((A, a(x) & True), (B, c(x) | True)), b(y) & c(y)),
|
| 304 |
+
(handler(Piecewise((A, a(x) & True), (B, c(x) | False)), b(y)),
|
| 305 |
+
(handler(Piecewise((A, a(x) & False), (B, c(x) | True)), c(y)),
|
| 306 |
+
(handler(Piecewise((A, a(x) & False), (B, c(x) | False)), True))
|
| 307 |
+
"""
|
| 308 |
+
# identify governing relationals
|
| 309 |
+
rel = self.atoms(Relational)
|
| 310 |
+
irel = list(ordered([r for r in rel if x not in r.free_symbols
|
| 311 |
+
and r not in (S.true, S.false)]))
|
| 312 |
+
if irel:
|
| 313 |
+
args = {}
|
| 314 |
+
exprinorder = []
|
| 315 |
+
for truth in product((1, 0), repeat=len(irel)):
|
| 316 |
+
reps = dict(zip(irel, truth))
|
| 317 |
+
# only store the true conditions since the false are implied
|
| 318 |
+
# when they appear lower in the Piecewise args
|
| 319 |
+
if 1 not in truth:
|
| 320 |
+
cond = None # flag this one so it doesn't get combined
|
| 321 |
+
else:
|
| 322 |
+
andargs = Tuple(*[i for i in reps if reps[i]])
|
| 323 |
+
free = list(andargs.free_symbols)
|
| 324 |
+
if len(free) == 1:
|
| 325 |
+
from sympy.solvers.inequalities import (
|
| 326 |
+
reduce_inequalities, _solve_inequality)
|
| 327 |
+
try:
|
| 328 |
+
t = reduce_inequalities(andargs, free[0])
|
| 329 |
+
# ValueError when there are potentially
|
| 330 |
+
# nonvanishing imaginary parts
|
| 331 |
+
except (ValueError, NotImplementedError):
|
| 332 |
+
# at least isolate free symbol on left
|
| 333 |
+
t = And(*[_solve_inequality(
|
| 334 |
+
a, free[0], linear=True)
|
| 335 |
+
for a in andargs])
|
| 336 |
+
else:
|
| 337 |
+
t = And(*andargs)
|
| 338 |
+
if t is S.false:
|
| 339 |
+
continue # an impossible combination
|
| 340 |
+
cond = t
|
| 341 |
+
expr = handler(self.xreplace(reps))
|
| 342 |
+
if isinstance(expr, self.func) and len(expr.args) == 1:
|
| 343 |
+
expr, econd = expr.args[0]
|
| 344 |
+
cond = And(econd, True if cond is None else cond)
|
| 345 |
+
# the ec pairs are being collected since all possibilities
|
| 346 |
+
# are being enumerated, but don't put the last one in since
|
| 347 |
+
# its expr might match a previous expression and it
|
| 348 |
+
# must appear last in the args
|
| 349 |
+
if cond is not None:
|
| 350 |
+
args.setdefault(expr, []).append(cond)
|
| 351 |
+
# but since we only store the true conditions we must maintain
|
| 352 |
+
# the order so that the expression with the most true values
|
| 353 |
+
# comes first
|
| 354 |
+
exprinorder.append(expr)
|
| 355 |
+
# convert collected conditions as args of Or
|
| 356 |
+
for k in args:
|
| 357 |
+
args[k] = Or(*args[k])
|
| 358 |
+
# take them in the order obtained
|
| 359 |
+
args = [(e, args[e]) for e in uniq(exprinorder)]
|
| 360 |
+
# add in the last arg
|
| 361 |
+
args.append((expr, True))
|
| 362 |
+
return Piecewise(*args)
|
| 363 |
+
|
| 364 |
+
def _eval_integral(self, x, _first=True, **kwargs):
|
| 365 |
+
"""Return the indefinite integral of the
|
| 366 |
+
Piecewise such that subsequent substitution of x with a
|
| 367 |
+
value will give the value of the integral (not including
|
| 368 |
+
the constant of integration) up to that point. To only
|
| 369 |
+
integrate the individual parts of Piecewise, use the
|
| 370 |
+
``piecewise_integrate`` method.
|
| 371 |
+
|
| 372 |
+
Examples
|
| 373 |
+
========
|
| 374 |
+
|
| 375 |
+
>>> from sympy import Piecewise
|
| 376 |
+
>>> from sympy.abc import x
|
| 377 |
+
>>> p = Piecewise((0, x < 0), (1, x < 1), (2, True))
|
| 378 |
+
>>> p.integrate(x)
|
| 379 |
+
Piecewise((0, x < 0), (x, x < 1), (2*x - 1, True))
|
| 380 |
+
>>> p.piecewise_integrate(x)
|
| 381 |
+
Piecewise((0, x < 0), (x, x < 1), (2*x, True))
|
| 382 |
+
|
| 383 |
+
See Also
|
| 384 |
+
========
|
| 385 |
+
Piecewise.piecewise_integrate
|
| 386 |
+
"""
|
| 387 |
+
from sympy.integrals.integrals import integrate
|
| 388 |
+
|
| 389 |
+
if _first:
|
| 390 |
+
def handler(ipw):
|
| 391 |
+
if isinstance(ipw, self.func):
|
| 392 |
+
return ipw._eval_integral(x, _first=False, **kwargs)
|
| 393 |
+
else:
|
| 394 |
+
return ipw.integrate(x, **kwargs)
|
| 395 |
+
irv = self._handle_irel(x, handler)
|
| 396 |
+
if irv is not None:
|
| 397 |
+
return irv
|
| 398 |
+
|
| 399 |
+
# handle a Piecewise from -oo to oo with and no x-independent relationals
|
| 400 |
+
# -----------------------------------------------------------------------
|
| 401 |
+
ok, abei = self._intervals(x)
|
| 402 |
+
if not ok:
|
| 403 |
+
from sympy.integrals.integrals import Integral
|
| 404 |
+
return Integral(self, x) # unevaluated
|
| 405 |
+
|
| 406 |
+
pieces = [(a, b) for a, b, _, _ in abei]
|
| 407 |
+
oo = S.Infinity
|
| 408 |
+
done = [(-oo, oo, -1)]
|
| 409 |
+
for k, p in enumerate(pieces):
|
| 410 |
+
if p == (-oo, oo):
|
| 411 |
+
# all undone intervals will get this key
|
| 412 |
+
for j, (a, b, i) in enumerate(done):
|
| 413 |
+
if i == -1:
|
| 414 |
+
done[j] = a, b, k
|
| 415 |
+
break # nothing else to consider
|
| 416 |
+
N = len(done) - 1
|
| 417 |
+
for j, (a, b, i) in enumerate(reversed(done)):
|
| 418 |
+
if i == -1:
|
| 419 |
+
j = N - j
|
| 420 |
+
done[j: j + 1] = _clip(p, (a, b), k)
|
| 421 |
+
done = [(a, b, i) for a, b, i in done if a != b]
|
| 422 |
+
|
| 423 |
+
# append an arg if there is a hole so a reference to
|
| 424 |
+
# argument -1 will give Undefined
|
| 425 |
+
if any(i == -1 for (a, b, i) in done):
|
| 426 |
+
abei.append((-oo, oo, Undefined, -1))
|
| 427 |
+
|
| 428 |
+
# return the sum of the intervals
|
| 429 |
+
args = []
|
| 430 |
+
sum = None
|
| 431 |
+
for a, b, i in done:
|
| 432 |
+
anti = integrate(abei[i][-2], x, **kwargs)
|
| 433 |
+
if sum is None:
|
| 434 |
+
sum = anti
|
| 435 |
+
else:
|
| 436 |
+
sum = sum.subs(x, a)
|
| 437 |
+
e = anti._eval_interval(x, a, x)
|
| 438 |
+
if sum.has(*_illegal) or e.has(*_illegal):
|
| 439 |
+
sum = anti
|
| 440 |
+
else:
|
| 441 |
+
sum += e
|
| 442 |
+
# see if we know whether b is contained in original
|
| 443 |
+
# condition
|
| 444 |
+
if b is S.Infinity:
|
| 445 |
+
cond = True
|
| 446 |
+
elif self.args[abei[i][-1]].cond.subs(x, b) == False:
|
| 447 |
+
cond = (x < b)
|
| 448 |
+
else:
|
| 449 |
+
cond = (x <= b)
|
| 450 |
+
args.append((sum, cond))
|
| 451 |
+
return Piecewise(*args)
|
| 452 |
+
|
| 453 |
+
def _eval_interval(self, sym, a, b, _first=True):
|
| 454 |
+
"""Evaluates the function along the sym in a given interval [a, b]"""
|
| 455 |
+
# FIXME: Currently complex intervals are not supported. A possible
|
| 456 |
+
# replacement algorithm, discussed in issue 5227, can be found in the
|
| 457 |
+
# following papers;
|
| 458 |
+
# http://portal.acm.org/citation.cfm?id=281649
|
| 459 |
+
# http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.70.4127&rep=rep1&type=pdf
|
| 460 |
+
|
| 461 |
+
if a is None or b is None:
|
| 462 |
+
# In this case, it is just simple substitution
|
| 463 |
+
return super()._eval_interval(sym, a, b)
|
| 464 |
+
else:
|
| 465 |
+
x, lo, hi = map(as_Basic, (sym, a, b))
|
| 466 |
+
|
| 467 |
+
if _first: # get only x-dependent relationals
|
| 468 |
+
def handler(ipw):
|
| 469 |
+
if isinstance(ipw, self.func):
|
| 470 |
+
return ipw._eval_interval(x, lo, hi, _first=None)
|
| 471 |
+
else:
|
| 472 |
+
return ipw._eval_interval(x, lo, hi)
|
| 473 |
+
irv = self._handle_irel(x, handler)
|
| 474 |
+
if irv is not None:
|
| 475 |
+
return irv
|
| 476 |
+
|
| 477 |
+
if (lo < hi) is S.false or (
|
| 478 |
+
lo is S.Infinity or hi is S.NegativeInfinity):
|
| 479 |
+
rv = self._eval_interval(x, hi, lo, _first=False)
|
| 480 |
+
if isinstance(rv, Piecewise):
|
| 481 |
+
rv = Piecewise(*[(-e, c) for e, c in rv.args])
|
| 482 |
+
else:
|
| 483 |
+
rv = -rv
|
| 484 |
+
return rv
|
| 485 |
+
|
| 486 |
+
if (lo < hi) is S.true or (
|
| 487 |
+
hi is S.Infinity or lo is S.NegativeInfinity):
|
| 488 |
+
pass
|
| 489 |
+
else:
|
| 490 |
+
_a = Dummy('lo')
|
| 491 |
+
_b = Dummy('hi')
|
| 492 |
+
a = lo if lo.is_comparable else _a
|
| 493 |
+
b = hi if hi.is_comparable else _b
|
| 494 |
+
pos = self._eval_interval(x, a, b, _first=False)
|
| 495 |
+
if a == _a and b == _b:
|
| 496 |
+
# it's purely symbolic so just swap lo and hi and
|
| 497 |
+
# change the sign to get the value for when lo > hi
|
| 498 |
+
neg, pos = (-pos.xreplace({_a: hi, _b: lo}),
|
| 499 |
+
pos.xreplace({_a: lo, _b: hi}))
|
| 500 |
+
else:
|
| 501 |
+
# at least one of the bounds was comparable, so allow
|
| 502 |
+
# _eval_interval to use that information when computing
|
| 503 |
+
# the interval with lo and hi reversed
|
| 504 |
+
neg, pos = (-self._eval_interval(x, hi, lo, _first=False),
|
| 505 |
+
pos.xreplace({_a: lo, _b: hi}))
|
| 506 |
+
|
| 507 |
+
# allow simplification based on ordering of lo and hi
|
| 508 |
+
p = Dummy('', positive=True)
|
| 509 |
+
if lo.is_Symbol:
|
| 510 |
+
pos = pos.xreplace({lo: hi - p}).xreplace({p: hi - lo})
|
| 511 |
+
neg = neg.xreplace({lo: hi + p}).xreplace({p: lo - hi})
|
| 512 |
+
elif hi.is_Symbol:
|
| 513 |
+
pos = pos.xreplace({hi: lo + p}).xreplace({p: hi - lo})
|
| 514 |
+
neg = neg.xreplace({hi: lo - p}).xreplace({p: lo - hi})
|
| 515 |
+
# evaluate limits that may have unevaluate Min/Max
|
| 516 |
+
touch = lambda _: _.replace(
|
| 517 |
+
lambda x: isinstance(x, (Min, Max)),
|
| 518 |
+
lambda x: x.func(*x.args))
|
| 519 |
+
neg = touch(neg)
|
| 520 |
+
pos = touch(pos)
|
| 521 |
+
# assemble return expression; make the first condition be Lt
|
| 522 |
+
# b/c then the first expression will look the same whether
|
| 523 |
+
# the lo or hi limit is symbolic
|
| 524 |
+
if a == _a: # the lower limit was symbolic
|
| 525 |
+
rv = Piecewise(
|
| 526 |
+
(pos,
|
| 527 |
+
lo < hi),
|
| 528 |
+
(neg,
|
| 529 |
+
True))
|
| 530 |
+
else:
|
| 531 |
+
rv = Piecewise(
|
| 532 |
+
(neg,
|
| 533 |
+
hi < lo),
|
| 534 |
+
(pos,
|
| 535 |
+
True))
|
| 536 |
+
|
| 537 |
+
if rv == Undefined:
|
| 538 |
+
raise ValueError("Can't integrate across undefined region.")
|
| 539 |
+
if any(isinstance(i, Piecewise) for i in (pos, neg)):
|
| 540 |
+
rv = piecewise_fold(rv)
|
| 541 |
+
return rv
|
| 542 |
+
|
| 543 |
+
# handle a Piecewise with lo <= hi and no x-independent relationals
|
| 544 |
+
# -----------------------------------------------------------------
|
| 545 |
+
ok, abei = self._intervals(x)
|
| 546 |
+
if not ok:
|
| 547 |
+
from sympy.integrals.integrals import Integral
|
| 548 |
+
# not being able to do the interval of f(x) can
|
| 549 |
+
# be stated as not being able to do the integral
|
| 550 |
+
# of f'(x) over the same range
|
| 551 |
+
return Integral(self.diff(x), (x, lo, hi)) # unevaluated
|
| 552 |
+
|
| 553 |
+
pieces = [(a, b) for a, b, _, _ in abei]
|
| 554 |
+
done = [(lo, hi, -1)]
|
| 555 |
+
oo = S.Infinity
|
| 556 |
+
for k, p in enumerate(pieces):
|
| 557 |
+
if p[:2] == (-oo, oo):
|
| 558 |
+
# all undone intervals will get this key
|
| 559 |
+
for j, (a, b, i) in enumerate(done):
|
| 560 |
+
if i == -1:
|
| 561 |
+
done[j] = a, b, k
|
| 562 |
+
break # nothing else to consider
|
| 563 |
+
N = len(done) - 1
|
| 564 |
+
for j, (a, b, i) in enumerate(reversed(done)):
|
| 565 |
+
if i == -1:
|
| 566 |
+
j = N - j
|
| 567 |
+
done[j: j + 1] = _clip(p, (a, b), k)
|
| 568 |
+
done = [(a, b, i) for a, b, i in done if a != b]
|
| 569 |
+
|
| 570 |
+
# return the sum of the intervals
|
| 571 |
+
sum = S.Zero
|
| 572 |
+
upto = None
|
| 573 |
+
for a, b, i in done:
|
| 574 |
+
if i == -1:
|
| 575 |
+
if upto is None:
|
| 576 |
+
return Undefined
|
| 577 |
+
# TODO simplify hi <= upto
|
| 578 |
+
return Piecewise((sum, hi <= upto), (Undefined, True))
|
| 579 |
+
sum += abei[i][-2]._eval_interval(x, a, b)
|
| 580 |
+
upto = b
|
| 581 |
+
return sum
|
| 582 |
+
|
| 583 |
+
def _intervals(self, sym, err_on_Eq=False):
|
| 584 |
+
r"""Return a bool and a message (when bool is False), else a
|
| 585 |
+
list of unique tuples, (a, b, e, i), where a and b
|
| 586 |
+
are the lower and upper bounds in which the expression e of
|
| 587 |
+
argument i in self is defined and $a < b$ (when involving
|
| 588 |
+
numbers) or $a \le b$ when involving symbols.
|
| 589 |
+
|
| 590 |
+
If there are any relationals not involving sym, or any
|
| 591 |
+
relational cannot be solved for sym, the bool will be False
|
| 592 |
+
a message be given as the second return value. The calling
|
| 593 |
+
routine should have removed such relationals before calling
|
| 594 |
+
this routine.
|
| 595 |
+
|
| 596 |
+
The evaluated conditions will be returned as ranges.
|
| 597 |
+
Discontinuous ranges will be returned separately with
|
| 598 |
+
identical expressions. The first condition that evaluates to
|
| 599 |
+
True will be returned as the last tuple with a, b = -oo, oo.
|
| 600 |
+
"""
|
| 601 |
+
from sympy.solvers.inequalities import _solve_inequality
|
| 602 |
+
|
| 603 |
+
assert isinstance(self, Piecewise)
|
| 604 |
+
|
| 605 |
+
def nonsymfail(cond):
|
| 606 |
+
return False, filldedent('''
|
| 607 |
+
A condition not involving
|
| 608 |
+
%s appeared: %s''' % (sym, cond))
|
| 609 |
+
|
| 610 |
+
def _solve_relational(r):
|
| 611 |
+
if sym not in r.free_symbols:
|
| 612 |
+
return nonsymfail(r)
|
| 613 |
+
try:
|
| 614 |
+
rv = _solve_inequality(r, sym)
|
| 615 |
+
except NotImplementedError:
|
| 616 |
+
return False, 'Unable to solve relational %s for %s.' % (r, sym)
|
| 617 |
+
if isinstance(rv, Relational):
|
| 618 |
+
free = rv.args[1].free_symbols
|
| 619 |
+
if rv.args[0] != sym or sym in free:
|
| 620 |
+
return False, 'Unable to solve relational %s for %s.' % (r, sym)
|
| 621 |
+
if rv.rel_op == '==':
|
| 622 |
+
# this equality has been affirmed to have the form
|
| 623 |
+
# Eq(sym, rhs) where rhs is sym-free; it represents
|
| 624 |
+
# a zero-width interval which will be ignored
|
| 625 |
+
# whether it is an isolated condition or contained
|
| 626 |
+
# within an And or an Or
|
| 627 |
+
rv = S.false
|
| 628 |
+
elif rv.rel_op == '!=':
|
| 629 |
+
try:
|
| 630 |
+
rv = Or(sym < rv.rhs, sym > rv.rhs)
|
| 631 |
+
except TypeError:
|
| 632 |
+
# e.g. x != I ==> all real x satisfy
|
| 633 |
+
rv = S.true
|
| 634 |
+
elif rv == (S.NegativeInfinity < sym) & (sym < S.Infinity):
|
| 635 |
+
rv = S.true
|
| 636 |
+
return True, rv
|
| 637 |
+
|
| 638 |
+
args = list(self.args)
|
| 639 |
+
# make self canonical wrt Relationals
|
| 640 |
+
keys = self.atoms(Relational)
|
| 641 |
+
reps = {}
|
| 642 |
+
for r in keys:
|
| 643 |
+
ok, s = _solve_relational(r)
|
| 644 |
+
if ok != True:
|
| 645 |
+
return False, ok
|
| 646 |
+
reps[r] = s
|
| 647 |
+
# process args individually so if any evaluate, their position
|
| 648 |
+
# in the original Piecewise will be known
|
| 649 |
+
args = [i.xreplace(reps) for i in self.args]
|
| 650 |
+
|
| 651 |
+
# precondition args
|
| 652 |
+
expr_cond = []
|
| 653 |
+
default = idefault = None
|
| 654 |
+
for i, (expr, cond) in enumerate(args):
|
| 655 |
+
if cond is S.false:
|
| 656 |
+
continue
|
| 657 |
+
if cond is S.true:
|
| 658 |
+
default = expr
|
| 659 |
+
idefault = i
|
| 660 |
+
break
|
| 661 |
+
if isinstance(cond, Eq):
|
| 662 |
+
# unanticipated condition, but it is here in case a
|
| 663 |
+
# replacement caused an Eq to appear
|
| 664 |
+
if err_on_Eq:
|
| 665 |
+
return False, 'encountered Eq condition: %s' % cond
|
| 666 |
+
continue # zero width interval
|
| 667 |
+
|
| 668 |
+
cond = to_cnf(cond)
|
| 669 |
+
if isinstance(cond, And):
|
| 670 |
+
cond = distribute_or_over_and(cond)
|
| 671 |
+
|
| 672 |
+
if isinstance(cond, Or):
|
| 673 |
+
expr_cond.extend(
|
| 674 |
+
[(i, expr, o) for o in cond.args
|
| 675 |
+
if not isinstance(o, Eq)])
|
| 676 |
+
elif cond is not S.false:
|
| 677 |
+
expr_cond.append((i, expr, cond))
|
| 678 |
+
elif cond is S.true:
|
| 679 |
+
default = expr
|
| 680 |
+
idefault = i
|
| 681 |
+
break
|
| 682 |
+
|
| 683 |
+
# determine intervals represented by conditions
|
| 684 |
+
int_expr = []
|
| 685 |
+
for iarg, expr, cond in expr_cond:
|
| 686 |
+
if isinstance(cond, And):
|
| 687 |
+
lower = S.NegativeInfinity
|
| 688 |
+
upper = S.Infinity
|
| 689 |
+
exclude = []
|
| 690 |
+
for cond2 in cond.args:
|
| 691 |
+
if not isinstance(cond2, Relational):
|
| 692 |
+
return False, 'expecting only Relationals'
|
| 693 |
+
if isinstance(cond2, Eq):
|
| 694 |
+
lower = upper # ignore
|
| 695 |
+
if err_on_Eq:
|
| 696 |
+
return False, 'encountered secondary Eq condition'
|
| 697 |
+
break
|
| 698 |
+
elif isinstance(cond2, Ne):
|
| 699 |
+
l, r = cond2.args
|
| 700 |
+
if l == sym:
|
| 701 |
+
exclude.append(r)
|
| 702 |
+
elif r == sym:
|
| 703 |
+
exclude.append(l)
|
| 704 |
+
else:
|
| 705 |
+
return nonsymfail(cond2)
|
| 706 |
+
continue
|
| 707 |
+
elif cond2.lts == sym:
|
| 708 |
+
upper = Min(cond2.gts, upper)
|
| 709 |
+
elif cond2.gts == sym:
|
| 710 |
+
lower = Max(cond2.lts, lower)
|
| 711 |
+
else:
|
| 712 |
+
return nonsymfail(cond2) # should never get here
|
| 713 |
+
if exclude:
|
| 714 |
+
exclude = list(ordered(exclude))
|
| 715 |
+
newcond = []
|
| 716 |
+
for i, e in enumerate(exclude):
|
| 717 |
+
if e < lower == True or e > upper == True:
|
| 718 |
+
continue
|
| 719 |
+
if not newcond:
|
| 720 |
+
newcond.append((None, lower)) # add a primer
|
| 721 |
+
newcond.append((newcond[-1][1], e))
|
| 722 |
+
newcond.append((newcond[-1][1], upper))
|
| 723 |
+
newcond.pop(0) # remove the primer
|
| 724 |
+
expr_cond.extend([(iarg, expr, And(i[0] < sym, sym < i[1])) for i in newcond])
|
| 725 |
+
continue
|
| 726 |
+
elif isinstance(cond, Relational) and cond.rel_op != '!=':
|
| 727 |
+
lower, upper = cond.lts, cond.gts # part 1: initialize with givens
|
| 728 |
+
if cond.lts == sym: # part 1a: expand the side ...
|
| 729 |
+
lower = S.NegativeInfinity # e.g. x <= 0 ---> -oo <= 0
|
| 730 |
+
elif cond.gts == sym: # part 1a: ... that can be expanded
|
| 731 |
+
upper = S.Infinity # e.g. x >= 0 ---> oo >= 0
|
| 732 |
+
else:
|
| 733 |
+
return nonsymfail(cond)
|
| 734 |
+
else:
|
| 735 |
+
return False, 'unrecognized condition: %s' % cond
|
| 736 |
+
|
| 737 |
+
lower, upper = lower, Max(lower, upper)
|
| 738 |
+
if err_on_Eq and lower == upper:
|
| 739 |
+
return False, 'encountered Eq condition'
|
| 740 |
+
if (lower >= upper) is not S.true:
|
| 741 |
+
int_expr.append((lower, upper, expr, iarg))
|
| 742 |
+
|
| 743 |
+
if default is not None:
|
| 744 |
+
int_expr.append(
|
| 745 |
+
(S.NegativeInfinity, S.Infinity, default, idefault))
|
| 746 |
+
|
| 747 |
+
return True, list(uniq(int_expr))
|
| 748 |
+
|
| 749 |
+
def _eval_nseries(self, x, n, logx, cdir=0):
|
| 750 |
+
args = [(ec.expr._eval_nseries(x, n, logx), ec.cond) for ec in self.args]
|
| 751 |
+
return self.func(*args)
|
| 752 |
+
|
| 753 |
+
def _eval_power(self, s):
|
| 754 |
+
return self.func(*[(e**s, c) for e, c in self.args])
|
| 755 |
+
|
| 756 |
+
def _eval_subs(self, old, new):
|
| 757 |
+
# this is strictly not necessary, but we can keep track
|
| 758 |
+
# of whether True or False conditions arise and be
|
| 759 |
+
# somewhat more efficient by avoiding other substitutions
|
| 760 |
+
# and avoiding invalid conditions that appear after a
|
| 761 |
+
# True condition
|
| 762 |
+
args = list(self.args)
|
| 763 |
+
args_exist = False
|
| 764 |
+
for i, (e, c) in enumerate(args):
|
| 765 |
+
c = c._subs(old, new)
|
| 766 |
+
if c != False:
|
| 767 |
+
args_exist = True
|
| 768 |
+
e = e._subs(old, new)
|
| 769 |
+
args[i] = (e, c)
|
| 770 |
+
if c == True:
|
| 771 |
+
break
|
| 772 |
+
if not args_exist:
|
| 773 |
+
args = ((Undefined, True),)
|
| 774 |
+
return self.func(*args)
|
| 775 |
+
|
| 776 |
+
def _eval_transpose(self):
|
| 777 |
+
return self.func(*[(e.transpose(), c) for e, c in self.args])
|
| 778 |
+
|
| 779 |
+
def _eval_template_is_attr(self, is_attr):
|
| 780 |
+
b = None
|
| 781 |
+
for expr, _ in self.args:
|
| 782 |
+
a = getattr(expr, is_attr)
|
| 783 |
+
if a is None:
|
| 784 |
+
return
|
| 785 |
+
if b is None:
|
| 786 |
+
b = a
|
| 787 |
+
elif b is not a:
|
| 788 |
+
return
|
| 789 |
+
return b
|
| 790 |
+
|
| 791 |
+
_eval_is_finite = lambda self: self._eval_template_is_attr(
|
| 792 |
+
'is_finite')
|
| 793 |
+
_eval_is_complex = lambda self: self._eval_template_is_attr('is_complex')
|
| 794 |
+
_eval_is_even = lambda self: self._eval_template_is_attr('is_even')
|
| 795 |
+
_eval_is_imaginary = lambda self: self._eval_template_is_attr(
|
| 796 |
+
'is_imaginary')
|
| 797 |
+
_eval_is_integer = lambda self: self._eval_template_is_attr('is_integer')
|
| 798 |
+
_eval_is_irrational = lambda self: self._eval_template_is_attr(
|
| 799 |
+
'is_irrational')
|
| 800 |
+
_eval_is_negative = lambda self: self._eval_template_is_attr('is_negative')
|
| 801 |
+
_eval_is_nonnegative = lambda self: self._eval_template_is_attr(
|
| 802 |
+
'is_nonnegative')
|
| 803 |
+
_eval_is_nonpositive = lambda self: self._eval_template_is_attr(
|
| 804 |
+
'is_nonpositive')
|
| 805 |
+
_eval_is_nonzero = lambda self: self._eval_template_is_attr(
|
| 806 |
+
'is_nonzero')
|
| 807 |
+
_eval_is_odd = lambda self: self._eval_template_is_attr('is_odd')
|
| 808 |
+
_eval_is_polar = lambda self: self._eval_template_is_attr('is_polar')
|
| 809 |
+
_eval_is_positive = lambda self: self._eval_template_is_attr('is_positive')
|
| 810 |
+
_eval_is_extended_real = lambda self: self._eval_template_is_attr(
|
| 811 |
+
'is_extended_real')
|
| 812 |
+
_eval_is_extended_positive = lambda self: self._eval_template_is_attr(
|
| 813 |
+
'is_extended_positive')
|
| 814 |
+
_eval_is_extended_negative = lambda self: self._eval_template_is_attr(
|
| 815 |
+
'is_extended_negative')
|
| 816 |
+
_eval_is_extended_nonzero = lambda self: self._eval_template_is_attr(
|
| 817 |
+
'is_extended_nonzero')
|
| 818 |
+
_eval_is_extended_nonpositive = lambda self: self._eval_template_is_attr(
|
| 819 |
+
'is_extended_nonpositive')
|
| 820 |
+
_eval_is_extended_nonnegative = lambda self: self._eval_template_is_attr(
|
| 821 |
+
'is_extended_nonnegative')
|
| 822 |
+
_eval_is_real = lambda self: self._eval_template_is_attr('is_real')
|
| 823 |
+
_eval_is_zero = lambda self: self._eval_template_is_attr(
|
| 824 |
+
'is_zero')
|
| 825 |
+
|
| 826 |
+
@classmethod
|
| 827 |
+
def __eval_cond(cls, cond):
|
| 828 |
+
"""Return the truth value of the condition."""
|
| 829 |
+
if cond == True:
|
| 830 |
+
return True
|
| 831 |
+
if isinstance(cond, Eq):
|
| 832 |
+
try:
|
| 833 |
+
diff = cond.lhs - cond.rhs
|
| 834 |
+
if diff.is_commutative:
|
| 835 |
+
return diff.is_zero
|
| 836 |
+
except TypeError:
|
| 837 |
+
pass
|
| 838 |
+
|
| 839 |
+
def as_expr_set_pairs(self, domain=None):
|
| 840 |
+
"""Return tuples for each argument of self that give
|
| 841 |
+
the expression and the interval in which it is valid
|
| 842 |
+
which is contained within the given domain.
|
| 843 |
+
If a condition cannot be converted to a set, an error
|
| 844 |
+
will be raised. The variable of the conditions is
|
| 845 |
+
assumed to be real; sets of real values are returned.
|
| 846 |
+
|
| 847 |
+
Examples
|
| 848 |
+
========
|
| 849 |
+
|
| 850 |
+
>>> from sympy import Piecewise, Interval
|
| 851 |
+
>>> from sympy.abc import x
|
| 852 |
+
>>> p = Piecewise(
|
| 853 |
+
... (1, x < 2),
|
| 854 |
+
... (2,(x > 0) & (x < 4)),
|
| 855 |
+
... (3, True))
|
| 856 |
+
>>> p.as_expr_set_pairs()
|
| 857 |
+
[(1, Interval.open(-oo, 2)),
|
| 858 |
+
(2, Interval.Ropen(2, 4)),
|
| 859 |
+
(3, Interval(4, oo))]
|
| 860 |
+
>>> p.as_expr_set_pairs(Interval(0, 3))
|
| 861 |
+
[(1, Interval.Ropen(0, 2)),
|
| 862 |
+
(2, Interval(2, 3))]
|
| 863 |
+
"""
|
| 864 |
+
if domain is None:
|
| 865 |
+
domain = S.Reals
|
| 866 |
+
exp_sets = []
|
| 867 |
+
U = domain
|
| 868 |
+
complex = not domain.is_subset(S.Reals)
|
| 869 |
+
cond_free = set()
|
| 870 |
+
for expr, cond in self.args:
|
| 871 |
+
cond_free |= cond.free_symbols
|
| 872 |
+
if len(cond_free) > 1:
|
| 873 |
+
raise NotImplementedError(filldedent('''
|
| 874 |
+
multivariate conditions are not handled.'''))
|
| 875 |
+
if complex:
|
| 876 |
+
for i in cond.atoms(Relational):
|
| 877 |
+
if not isinstance(i, (Eq, Ne)):
|
| 878 |
+
raise ValueError(filldedent('''
|
| 879 |
+
Inequalities in the complex domain are
|
| 880 |
+
not supported. Try the real domain by
|
| 881 |
+
setting domain=S.Reals'''))
|
| 882 |
+
cond_int = U.intersect(cond.as_set())
|
| 883 |
+
U = U - cond_int
|
| 884 |
+
if cond_int != S.EmptySet:
|
| 885 |
+
exp_sets.append((expr, cond_int))
|
| 886 |
+
return exp_sets
|
| 887 |
+
|
| 888 |
+
def _eval_rewrite_as_ITE(self, *args, **kwargs):
|
| 889 |
+
byfree = {}
|
| 890 |
+
args = list(args)
|
| 891 |
+
default = any(c == True for b, c in args)
|
| 892 |
+
for i, (b, c) in enumerate(args):
|
| 893 |
+
if not isinstance(b, Boolean) and b != True:
|
| 894 |
+
raise TypeError(filldedent('''
|
| 895 |
+
Expecting Boolean or bool but got `%s`
|
| 896 |
+
''' % func_name(b)))
|
| 897 |
+
if c == True:
|
| 898 |
+
break
|
| 899 |
+
# loop over independent conditions for this b
|
| 900 |
+
for c in c.args if isinstance(c, Or) else [c]:
|
| 901 |
+
free = c.free_symbols
|
| 902 |
+
x = free.pop()
|
| 903 |
+
try:
|
| 904 |
+
byfree[x] = byfree.setdefault(
|
| 905 |
+
x, S.EmptySet).union(c.as_set())
|
| 906 |
+
except NotImplementedError:
|
| 907 |
+
if not default:
|
| 908 |
+
raise NotImplementedError(filldedent('''
|
| 909 |
+
A method to determine whether a multivariate
|
| 910 |
+
conditional is consistent with a complete coverage
|
| 911 |
+
of all variables has not been implemented so the
|
| 912 |
+
rewrite is being stopped after encountering `%s`.
|
| 913 |
+
This error would not occur if a default expression
|
| 914 |
+
like `(foo, True)` were given.
|
| 915 |
+
''' % c))
|
| 916 |
+
if byfree[x] in (S.UniversalSet, S.Reals):
|
| 917 |
+
# collapse the ith condition to True and break
|
| 918 |
+
args[i] = list(args[i])
|
| 919 |
+
c = args[i][1] = True
|
| 920 |
+
break
|
| 921 |
+
if c == True:
|
| 922 |
+
break
|
| 923 |
+
if c != True:
|
| 924 |
+
raise ValueError(filldedent('''
|
| 925 |
+
Conditions must cover all reals or a final default
|
| 926 |
+
condition `(foo, True)` must be given.
|
| 927 |
+
'''))
|
| 928 |
+
last, _ = args[i] # ignore all past ith arg
|
| 929 |
+
for a, c in reversed(args[:i]):
|
| 930 |
+
last = ITE(c, a, last)
|
| 931 |
+
return _canonical(last)
|
| 932 |
+
|
| 933 |
+
def _eval_rewrite_as_KroneckerDelta(self, *args, **kwargs):
|
| 934 |
+
from sympy.functions.special.tensor_functions import KroneckerDelta
|
| 935 |
+
|
| 936 |
+
rules = {
|
| 937 |
+
And: [False, False],
|
| 938 |
+
Or: [True, True],
|
| 939 |
+
Not: [True, False],
|
| 940 |
+
Eq: [None, None],
|
| 941 |
+
Ne: [None, None]
|
| 942 |
+
}
|
| 943 |
+
|
| 944 |
+
class UnrecognizedCondition(Exception):
|
| 945 |
+
pass
|
| 946 |
+
|
| 947 |
+
def rewrite(cond):
|
| 948 |
+
if isinstance(cond, Eq):
|
| 949 |
+
return KroneckerDelta(*cond.args)
|
| 950 |
+
if isinstance(cond, Ne):
|
| 951 |
+
return 1 - KroneckerDelta(*cond.args)
|
| 952 |
+
|
| 953 |
+
cls, args = type(cond), cond.args
|
| 954 |
+
if cls not in rules:
|
| 955 |
+
raise UnrecognizedCondition(cls)
|
| 956 |
+
|
| 957 |
+
b1, b2 = rules[cls]
|
| 958 |
+
k = Mul(*[1 - rewrite(c) for c in args]) if b1 else Mul(*[rewrite(c) for c in args])
|
| 959 |
+
|
| 960 |
+
if b2:
|
| 961 |
+
return 1 - k
|
| 962 |
+
return k
|
| 963 |
+
|
| 964 |
+
conditions = []
|
| 965 |
+
true_value = None
|
| 966 |
+
for value, cond in args:
|
| 967 |
+
if type(cond) in rules:
|
| 968 |
+
conditions.append((value, cond))
|
| 969 |
+
elif cond is S.true:
|
| 970 |
+
if true_value is None:
|
| 971 |
+
true_value = value
|
| 972 |
+
else:
|
| 973 |
+
return
|
| 974 |
+
|
| 975 |
+
if true_value is not None:
|
| 976 |
+
result = true_value
|
| 977 |
+
|
| 978 |
+
for value, cond in conditions[::-1]:
|
| 979 |
+
try:
|
| 980 |
+
k = rewrite(cond)
|
| 981 |
+
result = k * value + (1 - k) * result
|
| 982 |
+
except UnrecognizedCondition:
|
| 983 |
+
return
|
| 984 |
+
|
| 985 |
+
return result
|
| 986 |
+
|
| 987 |
+
|
| 988 |
+
def piecewise_fold(expr, evaluate=True):
|
| 989 |
+
"""
|
| 990 |
+
Takes an expression containing a piecewise function and returns the
|
| 991 |
+
expression in piecewise form. In addition, any ITE conditions are
|
| 992 |
+
rewritten in negation normal form and simplified.
|
| 993 |
+
|
| 994 |
+
The final Piecewise is evaluated (default) but if the raw form
|
| 995 |
+
is desired, send ``evaluate=False``; if trivial evaluation is
|
| 996 |
+
desired, send ``evaluate=None`` and duplicate conditions and
|
| 997 |
+
processing of True and False will be handled.
|
| 998 |
+
|
| 999 |
+
Examples
|
| 1000 |
+
========
|
| 1001 |
+
|
| 1002 |
+
>>> from sympy import Piecewise, piecewise_fold, S
|
| 1003 |
+
>>> from sympy.abc import x
|
| 1004 |
+
>>> p = Piecewise((x, x < 1), (1, S(1) <= x))
|
| 1005 |
+
>>> piecewise_fold(x*p)
|
| 1006 |
+
Piecewise((x**2, x < 1), (x, True))
|
| 1007 |
+
|
| 1008 |
+
See Also
|
| 1009 |
+
========
|
| 1010 |
+
|
| 1011 |
+
Piecewise
|
| 1012 |
+
piecewise_exclusive
|
| 1013 |
+
"""
|
| 1014 |
+
if not isinstance(expr, Basic) or not expr.has(Piecewise):
|
| 1015 |
+
return expr
|
| 1016 |
+
|
| 1017 |
+
new_args = []
|
| 1018 |
+
if isinstance(expr, (ExprCondPair, Piecewise)):
|
| 1019 |
+
for e, c in expr.args:
|
| 1020 |
+
if not isinstance(e, Piecewise):
|
| 1021 |
+
e = piecewise_fold(e)
|
| 1022 |
+
# we don't keep Piecewise in condition because
|
| 1023 |
+
# it has to be checked to see that it's complete
|
| 1024 |
+
# and we convert it to ITE at that time
|
| 1025 |
+
assert not c.has(Piecewise) # pragma: no cover
|
| 1026 |
+
if isinstance(c, ITE):
|
| 1027 |
+
c = c.to_nnf()
|
| 1028 |
+
c = simplify_logic(c, form='cnf')
|
| 1029 |
+
if isinstance(e, Piecewise):
|
| 1030 |
+
new_args.extend([(piecewise_fold(ei), And(ci, c))
|
| 1031 |
+
for ei, ci in e.args])
|
| 1032 |
+
else:
|
| 1033 |
+
new_args.append((e, c))
|
| 1034 |
+
else:
|
| 1035 |
+
# Given
|
| 1036 |
+
# P1 = Piecewise((e11, c1), (e12, c2), A)
|
| 1037 |
+
# P2 = Piecewise((e21, c1), (e22, c2), B)
|
| 1038 |
+
# ...
|
| 1039 |
+
# the folding of f(P1, P2) is trivially
|
| 1040 |
+
# Piecewise(
|
| 1041 |
+
# (f(e11, e21), c1),
|
| 1042 |
+
# (f(e12, e22), c2),
|
| 1043 |
+
# (f(Piecewise(A), Piecewise(B)), True))
|
| 1044 |
+
# Certain objects end up rewriting themselves as thus, so
|
| 1045 |
+
# we do that grouping before the more generic folding.
|
| 1046 |
+
# The following applies this idea when f = Add or f = Mul
|
| 1047 |
+
# (and the expression is commutative).
|
| 1048 |
+
if expr.is_Add or expr.is_Mul and expr.is_commutative:
|
| 1049 |
+
p, args = sift(expr.args, lambda x: x.is_Piecewise, binary=True)
|
| 1050 |
+
pc = sift(p, lambda x: tuple([c for e,c in x.args]))
|
| 1051 |
+
for c in list(ordered(pc)):
|
| 1052 |
+
if len(pc[c]) > 1:
|
| 1053 |
+
pargs = [list(i.args) for i in pc[c]]
|
| 1054 |
+
# the first one is the same; there may be more
|
| 1055 |
+
com = common_prefix(*[
|
| 1056 |
+
[i.cond for i in j] for j in pargs])
|
| 1057 |
+
n = len(com)
|
| 1058 |
+
collected = []
|
| 1059 |
+
for i in range(n):
|
| 1060 |
+
collected.append((
|
| 1061 |
+
expr.func(*[ai[i].expr for ai in pargs]),
|
| 1062 |
+
com[i]))
|
| 1063 |
+
remains = []
|
| 1064 |
+
for a in pargs:
|
| 1065 |
+
if n == len(a): # no more args
|
| 1066 |
+
continue
|
| 1067 |
+
if a[n].cond == True: # no longer Piecewise
|
| 1068 |
+
remains.append(a[n].expr)
|
| 1069 |
+
else: # restore the remaining Piecewise
|
| 1070 |
+
remains.append(
|
| 1071 |
+
Piecewise(*a[n:], evaluate=False))
|
| 1072 |
+
if remains:
|
| 1073 |
+
collected.append((expr.func(*remains), True))
|
| 1074 |
+
args.append(Piecewise(*collected, evaluate=False))
|
| 1075 |
+
continue
|
| 1076 |
+
args.extend(pc[c])
|
| 1077 |
+
else:
|
| 1078 |
+
args = expr.args
|
| 1079 |
+
# fold
|
| 1080 |
+
folded = list(map(piecewise_fold, args))
|
| 1081 |
+
for ec in product(*[
|
| 1082 |
+
(i.args if isinstance(i, Piecewise) else
|
| 1083 |
+
[(i, true)]) for i in folded]):
|
| 1084 |
+
e, c = zip(*ec)
|
| 1085 |
+
new_args.append((expr.func(*e), And(*c)))
|
| 1086 |
+
|
| 1087 |
+
if evaluate is None:
|
| 1088 |
+
# don't return duplicate conditions, otherwise don't evaluate
|
| 1089 |
+
new_args = list(reversed([(e, c) for c, e in {
|
| 1090 |
+
c: e for e, c in reversed(new_args)}.items()]))
|
| 1091 |
+
rv = Piecewise(*new_args, evaluate=evaluate)
|
| 1092 |
+
if evaluate is None and len(rv.args) == 1 and rv.args[0].cond == True:
|
| 1093 |
+
return rv.args[0].expr
|
| 1094 |
+
if any(s.expr.has(Piecewise) for p in rv.atoms(Piecewise) for s in p.args):
|
| 1095 |
+
return piecewise_fold(rv)
|
| 1096 |
+
return rv
|
| 1097 |
+
|
| 1098 |
+
|
| 1099 |
+
def _clip(A, B, k):
|
| 1100 |
+
"""Return interval B as intervals that are covered by A (keyed
|
| 1101 |
+
to k) and all other intervals of B not covered by A keyed to -1.
|
| 1102 |
+
|
| 1103 |
+
The reference point of each interval is the rhs; if the lhs is
|
| 1104 |
+
greater than the rhs then an interval of zero width interval will
|
| 1105 |
+
result, e.g. (4, 1) is treated like (1, 1).
|
| 1106 |
+
|
| 1107 |
+
Examples
|
| 1108 |
+
========
|
| 1109 |
+
|
| 1110 |
+
>>> from sympy.functions.elementary.piecewise import _clip
|
| 1111 |
+
>>> from sympy import Tuple
|
| 1112 |
+
>>> A = Tuple(1, 3)
|
| 1113 |
+
>>> B = Tuple(2, 4)
|
| 1114 |
+
>>> _clip(A, B, 0)
|
| 1115 |
+
[(2, 3, 0), (3, 4, -1)]
|
| 1116 |
+
|
| 1117 |
+
Interpretation: interval portion (2, 3) of interval (2, 4) is
|
| 1118 |
+
covered by interval (1, 3) and is keyed to 0 as requested;
|
| 1119 |
+
interval (3, 4) was not covered by (1, 3) and is keyed to -1.
|
| 1120 |
+
"""
|
| 1121 |
+
a, b = B
|
| 1122 |
+
c, d = A
|
| 1123 |
+
c, d = Min(Max(c, a), b), Min(Max(d, a), b)
|
| 1124 |
+
a, b = Min(a, b), b
|
| 1125 |
+
p = []
|
| 1126 |
+
if a != c:
|
| 1127 |
+
p.append((a, c, -1))
|
| 1128 |
+
else:
|
| 1129 |
+
pass
|
| 1130 |
+
if c != d:
|
| 1131 |
+
p.append((c, d, k))
|
| 1132 |
+
else:
|
| 1133 |
+
pass
|
| 1134 |
+
if b != d:
|
| 1135 |
+
if d == c and p and p[-1][-1] == -1:
|
| 1136 |
+
p[-1] = p[-1][0], b, -1
|
| 1137 |
+
else:
|
| 1138 |
+
p.append((d, b, -1))
|
| 1139 |
+
else:
|
| 1140 |
+
pass
|
| 1141 |
+
|
| 1142 |
+
return p
|
| 1143 |
+
|
| 1144 |
+
|
| 1145 |
+
def piecewise_simplify_arguments(expr, **kwargs):
|
| 1146 |
+
from sympy.simplify.simplify import simplify
|
| 1147 |
+
|
| 1148 |
+
# simplify conditions
|
| 1149 |
+
f1 = expr.args[0].cond.free_symbols
|
| 1150 |
+
args = None
|
| 1151 |
+
if len(f1) == 1 and not expr.atoms(Eq):
|
| 1152 |
+
x = f1.pop()
|
| 1153 |
+
# this won't return intervals involving Eq
|
| 1154 |
+
# and it won't handle symbols treated as
|
| 1155 |
+
# booleans
|
| 1156 |
+
ok, abe_ = expr._intervals(x, err_on_Eq=True)
|
| 1157 |
+
def include(c, x, a):
|
| 1158 |
+
"return True if c.subs(x, a) is True, else False"
|
| 1159 |
+
try:
|
| 1160 |
+
return c.subs(x, a) == True
|
| 1161 |
+
except TypeError:
|
| 1162 |
+
return False
|
| 1163 |
+
if ok:
|
| 1164 |
+
args = []
|
| 1165 |
+
covered = S.EmptySet
|
| 1166 |
+
from sympy.sets.sets import Interval
|
| 1167 |
+
for a, b, e, i in abe_:
|
| 1168 |
+
c = expr.args[i].cond
|
| 1169 |
+
incl_a = include(c, x, a)
|
| 1170 |
+
incl_b = include(c, x, b)
|
| 1171 |
+
iv = Interval(a, b, not incl_a, not incl_b)
|
| 1172 |
+
cset = iv - covered
|
| 1173 |
+
if not cset:
|
| 1174 |
+
continue
|
| 1175 |
+
try:
|
| 1176 |
+
a = cset.inf
|
| 1177 |
+
except NotImplementedError:
|
| 1178 |
+
pass # continue with the given `a`
|
| 1179 |
+
else:
|
| 1180 |
+
incl_a = include(c, x, a)
|
| 1181 |
+
if incl_a and incl_b:
|
| 1182 |
+
if a.is_infinite and b.is_infinite:
|
| 1183 |
+
c = S.true
|
| 1184 |
+
elif b.is_infinite:
|
| 1185 |
+
c = (x > a) if a in covered else (x >= a)
|
| 1186 |
+
elif a.is_infinite:
|
| 1187 |
+
c = (x <= b)
|
| 1188 |
+
elif a in covered:
|
| 1189 |
+
c = And(a < x, x <= b)
|
| 1190 |
+
else:
|
| 1191 |
+
c = And(a <= x, x <= b)
|
| 1192 |
+
elif incl_a:
|
| 1193 |
+
if a.is_infinite:
|
| 1194 |
+
c = (x < b)
|
| 1195 |
+
elif a in covered:
|
| 1196 |
+
c = And(a < x, x < b)
|
| 1197 |
+
else:
|
| 1198 |
+
c = And(a <= x, x < b)
|
| 1199 |
+
elif incl_b:
|
| 1200 |
+
if b.is_infinite:
|
| 1201 |
+
c = (x > a)
|
| 1202 |
+
else:
|
| 1203 |
+
c = And(a < x, x <= b)
|
| 1204 |
+
else:
|
| 1205 |
+
if a in covered:
|
| 1206 |
+
c = (x < b)
|
| 1207 |
+
else:
|
| 1208 |
+
c = And(a < x, x < b)
|
| 1209 |
+
covered |= iv
|
| 1210 |
+
if a is S.NegativeInfinity and incl_a:
|
| 1211 |
+
covered |= {S.NegativeInfinity}
|
| 1212 |
+
if b is S.Infinity and incl_b:
|
| 1213 |
+
covered |= {S.Infinity}
|
| 1214 |
+
args.append((e, c))
|
| 1215 |
+
if not S.Reals.is_subset(covered):
|
| 1216 |
+
args.append((Undefined, True))
|
| 1217 |
+
if args is None:
|
| 1218 |
+
args = list(expr.args)
|
| 1219 |
+
for i in range(len(args)):
|
| 1220 |
+
e, c = args[i]
|
| 1221 |
+
if isinstance(c, Basic):
|
| 1222 |
+
c = simplify(c, **kwargs)
|
| 1223 |
+
args[i] = (e, c)
|
| 1224 |
+
|
| 1225 |
+
# simplify expressions
|
| 1226 |
+
doit = kwargs.pop('doit', None)
|
| 1227 |
+
for i in range(len(args)):
|
| 1228 |
+
e, c = args[i]
|
| 1229 |
+
if isinstance(e, Basic):
|
| 1230 |
+
# Skip doit to avoid growth at every call for some integrals
|
| 1231 |
+
# and sums, see sympy/sympy#17165
|
| 1232 |
+
newe = simplify(e, doit=False, **kwargs)
|
| 1233 |
+
if newe != e:
|
| 1234 |
+
e = newe
|
| 1235 |
+
args[i] = (e, c)
|
| 1236 |
+
|
| 1237 |
+
# restore kwargs flag
|
| 1238 |
+
if doit is not None:
|
| 1239 |
+
kwargs['doit'] = doit
|
| 1240 |
+
|
| 1241 |
+
return Piecewise(*args)
|
| 1242 |
+
|
| 1243 |
+
|
| 1244 |
+
def _piecewise_collapse_arguments(_args):
|
| 1245 |
+
newargs = [] # the unevaluated conditions
|
| 1246 |
+
current_cond = set() # the conditions up to a given e, c pair
|
| 1247 |
+
for expr, cond in _args:
|
| 1248 |
+
cond = cond.replace(
|
| 1249 |
+
lambda _: _.is_Relational, _canonical_coeff)
|
| 1250 |
+
# Check here if expr is a Piecewise and collapse if one of
|
| 1251 |
+
# the conds in expr matches cond. This allows the collapsing
|
| 1252 |
+
# of Piecewise((Piecewise((x,x<0)),x<0)) to Piecewise((x,x<0)).
|
| 1253 |
+
# This is important when using piecewise_fold to simplify
|
| 1254 |
+
# multiple Piecewise instances having the same conds.
|
| 1255 |
+
# Eventually, this code should be able to collapse Piecewise's
|
| 1256 |
+
# having different intervals, but this will probably require
|
| 1257 |
+
# using the new assumptions.
|
| 1258 |
+
if isinstance(expr, Piecewise):
|
| 1259 |
+
unmatching = []
|
| 1260 |
+
for i, (e, c) in enumerate(expr.args):
|
| 1261 |
+
if c in current_cond:
|
| 1262 |
+
# this would already have triggered
|
| 1263 |
+
continue
|
| 1264 |
+
if c == cond:
|
| 1265 |
+
if c != True:
|
| 1266 |
+
# nothing past this condition will ever
|
| 1267 |
+
# trigger and only those args before this
|
| 1268 |
+
# that didn't match a previous condition
|
| 1269 |
+
# could possibly trigger
|
| 1270 |
+
if unmatching:
|
| 1271 |
+
expr = Piecewise(*(
|
| 1272 |
+
unmatching + [(e, c)]))
|
| 1273 |
+
else:
|
| 1274 |
+
expr = e
|
| 1275 |
+
break
|
| 1276 |
+
else:
|
| 1277 |
+
unmatching.append((e, c))
|
| 1278 |
+
|
| 1279 |
+
# check for condition repeats
|
| 1280 |
+
got = False
|
| 1281 |
+
# -- if an And contains a condition that was
|
| 1282 |
+
# already encountered, then the And will be
|
| 1283 |
+
# False: if the previous condition was False
|
| 1284 |
+
# then the And will be False and if the previous
|
| 1285 |
+
# condition is True then then we wouldn't get to
|
| 1286 |
+
# this point. In either case, we can skip this condition.
|
| 1287 |
+
for i in ([cond] +
|
| 1288 |
+
(list(cond.args) if isinstance(cond, And) else
|
| 1289 |
+
[])):
|
| 1290 |
+
if i in current_cond:
|
| 1291 |
+
got = True
|
| 1292 |
+
break
|
| 1293 |
+
if got:
|
| 1294 |
+
continue
|
| 1295 |
+
|
| 1296 |
+
# -- if not(c) is already in current_cond then c is
|
| 1297 |
+
# a redundant condition in an And. This does not
|
| 1298 |
+
# apply to Or, however: (e1, c), (e2, Or(~c, d))
|
| 1299 |
+
# is not (e1, c), (e2, d) because if c and d are
|
| 1300 |
+
# both False this would give no results when the
|
| 1301 |
+
# true answer should be (e2, True)
|
| 1302 |
+
if isinstance(cond, And):
|
| 1303 |
+
nonredundant = []
|
| 1304 |
+
for c in cond.args:
|
| 1305 |
+
if isinstance(c, Relational):
|
| 1306 |
+
if c.negated.canonical in current_cond:
|
| 1307 |
+
continue
|
| 1308 |
+
# if a strict inequality appears after
|
| 1309 |
+
# a non-strict one, then the condition is
|
| 1310 |
+
# redundant
|
| 1311 |
+
if isinstance(c, (Lt, Gt)) and (
|
| 1312 |
+
c.weak in current_cond):
|
| 1313 |
+
cond = False
|
| 1314 |
+
break
|
| 1315 |
+
nonredundant.append(c)
|
| 1316 |
+
else:
|
| 1317 |
+
cond = cond.func(*nonredundant)
|
| 1318 |
+
elif isinstance(cond, Relational):
|
| 1319 |
+
if cond.negated.canonical in current_cond:
|
| 1320 |
+
cond = S.true
|
| 1321 |
+
|
| 1322 |
+
current_cond.add(cond)
|
| 1323 |
+
|
| 1324 |
+
# collect successive e,c pairs when exprs or cond match
|
| 1325 |
+
if newargs:
|
| 1326 |
+
if newargs[-1].expr == expr:
|
| 1327 |
+
orcond = Or(cond, newargs[-1].cond)
|
| 1328 |
+
if isinstance(orcond, (And, Or)):
|
| 1329 |
+
orcond = distribute_and_over_or(orcond)
|
| 1330 |
+
newargs[-1] = ExprCondPair(expr, orcond)
|
| 1331 |
+
continue
|
| 1332 |
+
elif newargs[-1].cond == cond:
|
| 1333 |
+
continue
|
| 1334 |
+
newargs.append(ExprCondPair(expr, cond))
|
| 1335 |
+
return newargs
|
| 1336 |
+
|
| 1337 |
+
|
| 1338 |
+
_blessed = lambda e: getattr(e.lhs, '_diff_wrt', False) and (
|
| 1339 |
+
getattr(e.rhs, '_diff_wrt', None) or
|
| 1340 |
+
isinstance(e.rhs, (Rational, NumberSymbol)))
|
| 1341 |
+
|
| 1342 |
+
|
| 1343 |
+
def piecewise_simplify(expr, **kwargs):
|
| 1344 |
+
expr = piecewise_simplify_arguments(expr, **kwargs)
|
| 1345 |
+
if not isinstance(expr, Piecewise):
|
| 1346 |
+
return expr
|
| 1347 |
+
args = list(expr.args)
|
| 1348 |
+
|
| 1349 |
+
args = _piecewise_simplify_eq_and(args)
|
| 1350 |
+
args = _piecewise_simplify_equal_to_next_segment(args)
|
| 1351 |
+
return Piecewise(*args)
|
| 1352 |
+
|
| 1353 |
+
|
| 1354 |
+
def _piecewise_simplify_equal_to_next_segment(args):
|
| 1355 |
+
"""
|
| 1356 |
+
See if expressions valid for an Equal expression happens to evaluate
|
| 1357 |
+
to the same function as in the next piecewise segment, see:
|
| 1358 |
+
https://github.com/sympy/sympy/issues/8458
|
| 1359 |
+
"""
|
| 1360 |
+
prevexpr = None
|
| 1361 |
+
for i, (expr, cond) in reversed(list(enumerate(args))):
|
| 1362 |
+
if prevexpr is not None:
|
| 1363 |
+
if isinstance(cond, And):
|
| 1364 |
+
eqs, other = sift(cond.args,
|
| 1365 |
+
lambda i: isinstance(i, Eq), binary=True)
|
| 1366 |
+
elif isinstance(cond, Eq):
|
| 1367 |
+
eqs, other = [cond], []
|
| 1368 |
+
else:
|
| 1369 |
+
eqs = other = []
|
| 1370 |
+
_prevexpr = prevexpr
|
| 1371 |
+
_expr = expr
|
| 1372 |
+
if eqs and not other:
|
| 1373 |
+
eqs = list(ordered(eqs))
|
| 1374 |
+
for e in eqs:
|
| 1375 |
+
# allow 2 args to collapse into 1 for any e
|
| 1376 |
+
# otherwise limit simplification to only simple-arg
|
| 1377 |
+
# Eq instances
|
| 1378 |
+
if len(args) == 2 or _blessed(e):
|
| 1379 |
+
_prevexpr = _prevexpr.subs(*e.args)
|
| 1380 |
+
_expr = _expr.subs(*e.args)
|
| 1381 |
+
# Did it evaluate to the same?
|
| 1382 |
+
if _prevexpr == _expr:
|
| 1383 |
+
# Set the expression for the Not equal section to the same
|
| 1384 |
+
# as the next. These will be merged when creating the new
|
| 1385 |
+
# Piecewise
|
| 1386 |
+
args[i] = args[i].func(args[i + 1][0], cond)
|
| 1387 |
+
else:
|
| 1388 |
+
# Update the expression that we compare against
|
| 1389 |
+
prevexpr = expr
|
| 1390 |
+
else:
|
| 1391 |
+
prevexpr = expr
|
| 1392 |
+
return args
|
| 1393 |
+
|
| 1394 |
+
|
| 1395 |
+
def _piecewise_simplify_eq_and(args):
|
| 1396 |
+
"""
|
| 1397 |
+
Try to simplify conditions and the expression for
|
| 1398 |
+
equalities that are part of the condition, e.g.
|
| 1399 |
+
Piecewise((n, And(Eq(n,0), Eq(n + m, 0))), (1, True))
|
| 1400 |
+
-> Piecewise((0, And(Eq(n, 0), Eq(m, 0))), (1, True))
|
| 1401 |
+
"""
|
| 1402 |
+
for i, (expr, cond) in enumerate(args):
|
| 1403 |
+
if isinstance(cond, And):
|
| 1404 |
+
eqs, other = sift(cond.args,
|
| 1405 |
+
lambda i: isinstance(i, Eq), binary=True)
|
| 1406 |
+
elif isinstance(cond, Eq):
|
| 1407 |
+
eqs, other = [cond], []
|
| 1408 |
+
else:
|
| 1409 |
+
eqs = other = []
|
| 1410 |
+
if eqs:
|
| 1411 |
+
eqs = list(ordered(eqs))
|
| 1412 |
+
for j, e in enumerate(eqs):
|
| 1413 |
+
# these blessed lhs objects behave like Symbols
|
| 1414 |
+
# and the rhs are simple replacements for the "symbols"
|
| 1415 |
+
if _blessed(e):
|
| 1416 |
+
expr = expr.subs(*e.args)
|
| 1417 |
+
eqs[j + 1:] = [ei.subs(*e.args) for ei in eqs[j + 1:]]
|
| 1418 |
+
other = [ei.subs(*e.args) for ei in other]
|
| 1419 |
+
cond = And(*(eqs + other))
|
| 1420 |
+
args[i] = args[i].func(expr, cond)
|
| 1421 |
+
return args
|
| 1422 |
+
|
| 1423 |
+
|
| 1424 |
+
def piecewise_exclusive(expr, *, skip_nan=False, deep=True):
|
| 1425 |
+
"""
|
| 1426 |
+
Rewrite :class:`Piecewise` with mutually exclusive conditions.
|
| 1427 |
+
|
| 1428 |
+
Explanation
|
| 1429 |
+
===========
|
| 1430 |
+
|
| 1431 |
+
SymPy represents the conditions of a :class:`Piecewise` in an
|
| 1432 |
+
"if-elif"-fashion, allowing more than one condition to be simultaneously
|
| 1433 |
+
True. The interpretation is that the first condition that is True is the
|
| 1434 |
+
case that holds. While this is a useful representation computationally it
|
| 1435 |
+
is not how a piecewise formula is typically shown in a mathematical text.
|
| 1436 |
+
The :func:`piecewise_exclusive` function can be used to rewrite any
|
| 1437 |
+
:class:`Piecewise` with more typical mutually exclusive conditions.
|
| 1438 |
+
|
| 1439 |
+
Note that further manipulation of the resulting :class:`Piecewise`, e.g.
|
| 1440 |
+
simplifying it, will most likely make it non-exclusive. Hence, this is
|
| 1441 |
+
primarily a function to be used in conjunction with printing the Piecewise
|
| 1442 |
+
or if one would like to reorder the expression-condition pairs.
|
| 1443 |
+
|
| 1444 |
+
If it is not possible to determine that all possibilities are covered by
|
| 1445 |
+
the different cases of the :class:`Piecewise` then a final
|
| 1446 |
+
:class:`~sympy.core.numbers.NaN` case will be included explicitly. This
|
| 1447 |
+
can be prevented by passing ``skip_nan=True``.
|
| 1448 |
+
|
| 1449 |
+
Examples
|
| 1450 |
+
========
|
| 1451 |
+
|
| 1452 |
+
>>> from sympy import piecewise_exclusive, Symbol, Piecewise, S
|
| 1453 |
+
>>> x = Symbol('x', real=True)
|
| 1454 |
+
>>> p = Piecewise((0, x < 0), (S.Half, x <= 0), (1, True))
|
| 1455 |
+
>>> piecewise_exclusive(p)
|
| 1456 |
+
Piecewise((0, x < 0), (1/2, Eq(x, 0)), (1, x > 0))
|
| 1457 |
+
>>> piecewise_exclusive(Piecewise((2, x > 1)))
|
| 1458 |
+
Piecewise((2, x > 1), (nan, x <= 1))
|
| 1459 |
+
>>> piecewise_exclusive(Piecewise((2, x > 1)), skip_nan=True)
|
| 1460 |
+
Piecewise((2, x > 1))
|
| 1461 |
+
|
| 1462 |
+
Parameters
|
| 1463 |
+
==========
|
| 1464 |
+
|
| 1465 |
+
expr: a SymPy expression.
|
| 1466 |
+
Any :class:`Piecewise` in the expression will be rewritten.
|
| 1467 |
+
skip_nan: ``bool`` (default ``False``)
|
| 1468 |
+
If ``skip_nan`` is set to ``True`` then a final
|
| 1469 |
+
:class:`~sympy.core.numbers.NaN` case will not be included.
|
| 1470 |
+
deep: ``bool`` (default ``True``)
|
| 1471 |
+
If ``deep`` is ``True`` then :func:`piecewise_exclusive` will rewrite
|
| 1472 |
+
any :class:`Piecewise` subexpressions in ``expr`` rather than just
|
| 1473 |
+
rewriting ``expr`` itself.
|
| 1474 |
+
|
| 1475 |
+
Returns
|
| 1476 |
+
=======
|
| 1477 |
+
|
| 1478 |
+
An expression equivalent to ``expr`` but where all :class:`Piecewise` have
|
| 1479 |
+
been rewritten with mutually exclusive conditions.
|
| 1480 |
+
|
| 1481 |
+
See Also
|
| 1482 |
+
========
|
| 1483 |
+
|
| 1484 |
+
Piecewise
|
| 1485 |
+
piecewise_fold
|
| 1486 |
+
"""
|
| 1487 |
+
|
| 1488 |
+
def make_exclusive(*pwargs):
|
| 1489 |
+
|
| 1490 |
+
cumcond = false
|
| 1491 |
+
newargs = []
|
| 1492 |
+
|
| 1493 |
+
# Handle the first n-1 cases
|
| 1494 |
+
for expr_i, cond_i in pwargs[:-1]:
|
| 1495 |
+
cancond = And(cond_i, Not(cumcond)).simplify()
|
| 1496 |
+
cumcond = Or(cond_i, cumcond).simplify()
|
| 1497 |
+
newargs.append((expr_i, cancond))
|
| 1498 |
+
|
| 1499 |
+
# For the nth case defer simplification of cumcond
|
| 1500 |
+
expr_n, cond_n = pwargs[-1]
|
| 1501 |
+
cancond_n = And(cond_n, Not(cumcond)).simplify()
|
| 1502 |
+
newargs.append((expr_n, cancond_n))
|
| 1503 |
+
|
| 1504 |
+
if not skip_nan:
|
| 1505 |
+
cumcond = Or(cond_n, cumcond).simplify()
|
| 1506 |
+
if cumcond is not true:
|
| 1507 |
+
newargs.append((Undefined, Not(cumcond).simplify()))
|
| 1508 |
+
|
| 1509 |
+
return Piecewise(*newargs, evaluate=False)
|
| 1510 |
+
|
| 1511 |
+
if deep:
|
| 1512 |
+
return expr.replace(Piecewise, make_exclusive)
|
| 1513 |
+
elif isinstance(expr, Piecewise):
|
| 1514 |
+
return make_exclusive(*expr.args)
|
| 1515 |
+
else:
|
| 1516 |
+
return exp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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.calculus.accumulationbounds import AccumBounds
|
| 2 |
+
from sympy.core.function import (expand_mul, expand_trig)
|
| 3 |
+
from sympy.core.numbers import (E, I, Integer, Rational, nan, oo, pi, zoo)
|
| 4 |
+
from sympy.core.singleton import S
|
| 5 |
+
from sympy.core.symbol import (Symbol, symbols)
|
| 6 |
+
from sympy.functions.elementary.complexes import (im, re)
|
| 7 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
| 8 |
+
from sympy.functions.elementary.hyperbolic import (acosh, acoth, acsch, asech, asinh, atanh, cosh, coth, csch, sech, sinh, tanh)
|
| 9 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 10 |
+
from sympy.functions.elementary.trigonometric import (acos, asin, cos, cot, sec, sin, tan)
|
| 11 |
+
from sympy.series.order import O
|
| 12 |
+
|
| 13 |
+
from sympy.core.expr import unchanged
|
| 14 |
+
from sympy.core.function import ArgumentIndexError, PoleError
|
| 15 |
+
from sympy.testing.pytest import raises
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_sinh():
|
| 19 |
+
x, y = symbols('x,y')
|
| 20 |
+
|
| 21 |
+
k = Symbol('k', integer=True)
|
| 22 |
+
|
| 23 |
+
assert sinh(nan) is nan
|
| 24 |
+
assert sinh(zoo) is nan
|
| 25 |
+
|
| 26 |
+
assert sinh(oo) is oo
|
| 27 |
+
assert sinh(-oo) is -oo
|
| 28 |
+
|
| 29 |
+
assert sinh(0) == 0
|
| 30 |
+
|
| 31 |
+
assert unchanged(sinh, 1)
|
| 32 |
+
assert sinh(-1) == -sinh(1)
|
| 33 |
+
|
| 34 |
+
assert unchanged(sinh, x)
|
| 35 |
+
assert sinh(-x) == -sinh(x)
|
| 36 |
+
|
| 37 |
+
assert unchanged(sinh, pi)
|
| 38 |
+
assert sinh(-pi) == -sinh(pi)
|
| 39 |
+
|
| 40 |
+
assert unchanged(sinh, 2**1024 * E)
|
| 41 |
+
assert sinh(-2**1024 * E) == -sinh(2**1024 * E)
|
| 42 |
+
|
| 43 |
+
assert sinh(pi*I) == 0
|
| 44 |
+
assert sinh(-pi*I) == 0
|
| 45 |
+
assert sinh(2*pi*I) == 0
|
| 46 |
+
assert sinh(-2*pi*I) == 0
|
| 47 |
+
assert sinh(-3*10**73*pi*I) == 0
|
| 48 |
+
assert sinh(7*10**103*pi*I) == 0
|
| 49 |
+
|
| 50 |
+
assert sinh(pi*I/2) == I
|
| 51 |
+
assert sinh(-pi*I/2) == -I
|
| 52 |
+
assert sinh(pi*I*Rational(5, 2)) == I
|
| 53 |
+
assert sinh(pi*I*Rational(7, 2)) == -I
|
| 54 |
+
|
| 55 |
+
assert sinh(pi*I/3) == S.Half*sqrt(3)*I
|
| 56 |
+
assert sinh(pi*I*Rational(-2, 3)) == Rational(-1, 2)*sqrt(3)*I
|
| 57 |
+
|
| 58 |
+
assert sinh(pi*I/4) == S.Half*sqrt(2)*I
|
| 59 |
+
assert sinh(-pi*I/4) == Rational(-1, 2)*sqrt(2)*I
|
| 60 |
+
assert sinh(pi*I*Rational(17, 4)) == S.Half*sqrt(2)*I
|
| 61 |
+
assert sinh(pi*I*Rational(-3, 4)) == Rational(-1, 2)*sqrt(2)*I
|
| 62 |
+
|
| 63 |
+
assert sinh(pi*I/6) == S.Half*I
|
| 64 |
+
assert sinh(-pi*I/6) == Rational(-1, 2)*I
|
| 65 |
+
assert sinh(pi*I*Rational(7, 6)) == Rational(-1, 2)*I
|
| 66 |
+
assert sinh(pi*I*Rational(-5, 6)) == Rational(-1, 2)*I
|
| 67 |
+
|
| 68 |
+
assert sinh(pi*I/105) == sin(pi/105)*I
|
| 69 |
+
assert sinh(-pi*I/105) == -sin(pi/105)*I
|
| 70 |
+
|
| 71 |
+
assert unchanged(sinh, 2 + 3*I)
|
| 72 |
+
|
| 73 |
+
assert sinh(x*I) == sin(x)*I
|
| 74 |
+
|
| 75 |
+
assert sinh(k*pi*I) == 0
|
| 76 |
+
assert sinh(17*k*pi*I) == 0
|
| 77 |
+
|
| 78 |
+
assert sinh(k*pi*I/2) == sin(k*pi/2)*I
|
| 79 |
+
|
| 80 |
+
assert sinh(x).as_real_imag(deep=False) == (cos(im(x))*sinh(re(x)),
|
| 81 |
+
sin(im(x))*cosh(re(x)))
|
| 82 |
+
x = Symbol('x', extended_real=True)
|
| 83 |
+
assert sinh(x).as_real_imag(deep=False) == (sinh(x), 0)
|
| 84 |
+
|
| 85 |
+
x = Symbol('x', real=True)
|
| 86 |
+
assert sinh(I*x).is_finite is True
|
| 87 |
+
assert sinh(x).is_real is True
|
| 88 |
+
assert sinh(I).is_real is False
|
| 89 |
+
p = Symbol('p', positive=True)
|
| 90 |
+
assert sinh(p).is_zero is False
|
| 91 |
+
assert sinh(0, evaluate=False).is_zero is True
|
| 92 |
+
assert sinh(2*pi*I, evaluate=False).is_zero is True
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def test_sinh_series():
|
| 96 |
+
x = Symbol('x')
|
| 97 |
+
assert sinh(x).series(x, 0, 10) == \
|
| 98 |
+
x + x**3/6 + x**5/120 + x**7/5040 + x**9/362880 + O(x**10)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def test_sinh_fdiff():
|
| 102 |
+
x = Symbol('x')
|
| 103 |
+
raises(ArgumentIndexError, lambda: sinh(x).fdiff(2))
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def test_cosh():
|
| 107 |
+
x, y = symbols('x,y')
|
| 108 |
+
|
| 109 |
+
k = Symbol('k', integer=True)
|
| 110 |
+
|
| 111 |
+
assert cosh(nan) is nan
|
| 112 |
+
assert cosh(zoo) is nan
|
| 113 |
+
|
| 114 |
+
assert cosh(oo) is oo
|
| 115 |
+
assert cosh(-oo) is oo
|
| 116 |
+
|
| 117 |
+
assert cosh(0) == 1
|
| 118 |
+
|
| 119 |
+
assert unchanged(cosh, 1)
|
| 120 |
+
assert cosh(-1) == cosh(1)
|
| 121 |
+
|
| 122 |
+
assert unchanged(cosh, x)
|
| 123 |
+
assert cosh(-x) == cosh(x)
|
| 124 |
+
|
| 125 |
+
assert cosh(pi*I) == cos(pi)
|
| 126 |
+
assert cosh(-pi*I) == cos(pi)
|
| 127 |
+
|
| 128 |
+
assert unchanged(cosh, 2**1024 * E)
|
| 129 |
+
assert cosh(-2**1024 * E) == cosh(2**1024 * E)
|
| 130 |
+
|
| 131 |
+
assert cosh(pi*I/2) == 0
|
| 132 |
+
assert cosh(-pi*I/2) == 0
|
| 133 |
+
assert cosh((-3*10**73 + 1)*pi*I/2) == 0
|
| 134 |
+
assert cosh((7*10**103 + 1)*pi*I/2) == 0
|
| 135 |
+
|
| 136 |
+
assert cosh(pi*I) == -1
|
| 137 |
+
assert cosh(-pi*I) == -1
|
| 138 |
+
assert cosh(5*pi*I) == -1
|
| 139 |
+
assert cosh(8*pi*I) == 1
|
| 140 |
+
|
| 141 |
+
assert cosh(pi*I/3) == S.Half
|
| 142 |
+
assert cosh(pi*I*Rational(-2, 3)) == Rational(-1, 2)
|
| 143 |
+
|
| 144 |
+
assert cosh(pi*I/4) == S.Half*sqrt(2)
|
| 145 |
+
assert cosh(-pi*I/4) == S.Half*sqrt(2)
|
| 146 |
+
assert cosh(pi*I*Rational(11, 4)) == Rational(-1, 2)*sqrt(2)
|
| 147 |
+
assert cosh(pi*I*Rational(-3, 4)) == Rational(-1, 2)*sqrt(2)
|
| 148 |
+
|
| 149 |
+
assert cosh(pi*I/6) == S.Half*sqrt(3)
|
| 150 |
+
assert cosh(-pi*I/6) == S.Half*sqrt(3)
|
| 151 |
+
assert cosh(pi*I*Rational(7, 6)) == Rational(-1, 2)*sqrt(3)
|
| 152 |
+
assert cosh(pi*I*Rational(-5, 6)) == Rational(-1, 2)*sqrt(3)
|
| 153 |
+
|
| 154 |
+
assert cosh(pi*I/105) == cos(pi/105)
|
| 155 |
+
assert cosh(-pi*I/105) == cos(pi/105)
|
| 156 |
+
|
| 157 |
+
assert unchanged(cosh, 2 + 3*I)
|
| 158 |
+
|
| 159 |
+
assert cosh(x*I) == cos(x)
|
| 160 |
+
|
| 161 |
+
assert cosh(k*pi*I) == cos(k*pi)
|
| 162 |
+
assert cosh(17*k*pi*I) == cos(17*k*pi)
|
| 163 |
+
|
| 164 |
+
assert unchanged(cosh, k*pi)
|
| 165 |
+
|
| 166 |
+
assert cosh(x).as_real_imag(deep=False) == (cos(im(x))*cosh(re(x)),
|
| 167 |
+
sin(im(x))*sinh(re(x)))
|
| 168 |
+
x = Symbol('x', extended_real=True)
|
| 169 |
+
assert cosh(x).as_real_imag(deep=False) == (cosh(x), 0)
|
| 170 |
+
|
| 171 |
+
x = Symbol('x', real=True)
|
| 172 |
+
assert cosh(I*x).is_finite is True
|
| 173 |
+
assert cosh(I*x).is_real is True
|
| 174 |
+
assert cosh(I*2 + 1).is_real is False
|
| 175 |
+
assert cosh(5*I*S.Pi/2, evaluate=False).is_zero is True
|
| 176 |
+
assert cosh(x).is_zero is False
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def test_cosh_series():
|
| 180 |
+
x = Symbol('x')
|
| 181 |
+
assert cosh(x).series(x, 0, 10) == \
|
| 182 |
+
1 + x**2/2 + x**4/24 + x**6/720 + x**8/40320 + O(x**10)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def test_cosh_fdiff():
|
| 186 |
+
x = Symbol('x')
|
| 187 |
+
raises(ArgumentIndexError, lambda: cosh(x).fdiff(2))
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def test_tanh():
|
| 191 |
+
x, y = symbols('x,y')
|
| 192 |
+
|
| 193 |
+
k = Symbol('k', integer=True)
|
| 194 |
+
|
| 195 |
+
assert tanh(nan) is nan
|
| 196 |
+
assert tanh(zoo) is nan
|
| 197 |
+
|
| 198 |
+
assert tanh(oo) == 1
|
| 199 |
+
assert tanh(-oo) == -1
|
| 200 |
+
|
| 201 |
+
assert tanh(0) == 0
|
| 202 |
+
|
| 203 |
+
assert unchanged(tanh, 1)
|
| 204 |
+
assert tanh(-1) == -tanh(1)
|
| 205 |
+
|
| 206 |
+
assert unchanged(tanh, x)
|
| 207 |
+
assert tanh(-x) == -tanh(x)
|
| 208 |
+
|
| 209 |
+
assert unchanged(tanh, pi)
|
| 210 |
+
assert tanh(-pi) == -tanh(pi)
|
| 211 |
+
|
| 212 |
+
assert unchanged(tanh, 2**1024 * E)
|
| 213 |
+
assert tanh(-2**1024 * E) == -tanh(2**1024 * E)
|
| 214 |
+
|
| 215 |
+
assert tanh(pi*I) == 0
|
| 216 |
+
assert tanh(-pi*I) == 0
|
| 217 |
+
assert tanh(2*pi*I) == 0
|
| 218 |
+
assert tanh(-2*pi*I) == 0
|
| 219 |
+
assert tanh(-3*10**73*pi*I) == 0
|
| 220 |
+
assert tanh(7*10**103*pi*I) == 0
|
| 221 |
+
|
| 222 |
+
assert tanh(pi*I/2) is zoo
|
| 223 |
+
assert tanh(-pi*I/2) is zoo
|
| 224 |
+
assert tanh(pi*I*Rational(5, 2)) is zoo
|
| 225 |
+
assert tanh(pi*I*Rational(7, 2)) is zoo
|
| 226 |
+
|
| 227 |
+
assert tanh(pi*I/3) == sqrt(3)*I
|
| 228 |
+
assert tanh(pi*I*Rational(-2, 3)) == sqrt(3)*I
|
| 229 |
+
|
| 230 |
+
assert tanh(pi*I/4) == I
|
| 231 |
+
assert tanh(-pi*I/4) == -I
|
| 232 |
+
assert tanh(pi*I*Rational(17, 4)) == I
|
| 233 |
+
assert tanh(pi*I*Rational(-3, 4)) == I
|
| 234 |
+
|
| 235 |
+
assert tanh(pi*I/6) == I/sqrt(3)
|
| 236 |
+
assert tanh(-pi*I/6) == -I/sqrt(3)
|
| 237 |
+
assert tanh(pi*I*Rational(7, 6)) == I/sqrt(3)
|
| 238 |
+
assert tanh(pi*I*Rational(-5, 6)) == I/sqrt(3)
|
| 239 |
+
|
| 240 |
+
assert tanh(pi*I/105) == tan(pi/105)*I
|
| 241 |
+
assert tanh(-pi*I/105) == -tan(pi/105)*I
|
| 242 |
+
|
| 243 |
+
assert unchanged(tanh, 2 + 3*I)
|
| 244 |
+
|
| 245 |
+
assert tanh(x*I) == tan(x)*I
|
| 246 |
+
|
| 247 |
+
assert tanh(k*pi*I) == 0
|
| 248 |
+
assert tanh(17*k*pi*I) == 0
|
| 249 |
+
|
| 250 |
+
assert tanh(k*pi*I/2) == tan(k*pi/2)*I
|
| 251 |
+
|
| 252 |
+
assert tanh(x).as_real_imag(deep=False) == (sinh(re(x))*cosh(re(x))/(cos(im(x))**2
|
| 253 |
+
+ sinh(re(x))**2),
|
| 254 |
+
sin(im(x))*cos(im(x))/(cos(im(x))**2 + sinh(re(x))**2))
|
| 255 |
+
x = Symbol('x', extended_real=True)
|
| 256 |
+
assert tanh(x).as_real_imag(deep=False) == (tanh(x), 0)
|
| 257 |
+
assert tanh(I*pi/3 + 1).is_real is False
|
| 258 |
+
assert tanh(x).is_real is True
|
| 259 |
+
assert tanh(I*pi*x/2).is_real is None
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def test_tanh_series():
|
| 263 |
+
x = Symbol('x')
|
| 264 |
+
assert tanh(x).series(x, 0, 10) == \
|
| 265 |
+
x - x**3/3 + 2*x**5/15 - 17*x**7/315 + 62*x**9/2835 + O(x**10)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def test_tanh_fdiff():
|
| 269 |
+
x = Symbol('x')
|
| 270 |
+
raises(ArgumentIndexError, lambda: tanh(x).fdiff(2))
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
def test_coth():
|
| 274 |
+
x, y = symbols('x,y')
|
| 275 |
+
|
| 276 |
+
k = Symbol('k', integer=True)
|
| 277 |
+
|
| 278 |
+
assert coth(nan) is nan
|
| 279 |
+
assert coth(zoo) is nan
|
| 280 |
+
|
| 281 |
+
assert coth(oo) == 1
|
| 282 |
+
assert coth(-oo) == -1
|
| 283 |
+
|
| 284 |
+
assert coth(0) is zoo
|
| 285 |
+
assert unchanged(coth, 1)
|
| 286 |
+
assert coth(-1) == -coth(1)
|
| 287 |
+
|
| 288 |
+
assert unchanged(coth, x)
|
| 289 |
+
assert coth(-x) == -coth(x)
|
| 290 |
+
|
| 291 |
+
assert coth(pi*I) == -I*cot(pi)
|
| 292 |
+
assert coth(-pi*I) == cot(pi)*I
|
| 293 |
+
|
| 294 |
+
assert unchanged(coth, 2**1024 * E)
|
| 295 |
+
assert coth(-2**1024 * E) == -coth(2**1024 * E)
|
| 296 |
+
|
| 297 |
+
assert coth(pi*I) == -I*cot(pi)
|
| 298 |
+
assert coth(-pi*I) == I*cot(pi)
|
| 299 |
+
assert coth(2*pi*I) == -I*cot(2*pi)
|
| 300 |
+
assert coth(-2*pi*I) == I*cot(2*pi)
|
| 301 |
+
assert coth(-3*10**73*pi*I) == I*cot(3*10**73*pi)
|
| 302 |
+
assert coth(7*10**103*pi*I) == -I*cot(7*10**103*pi)
|
| 303 |
+
|
| 304 |
+
assert coth(pi*I/2) == 0
|
| 305 |
+
assert coth(-pi*I/2) == 0
|
| 306 |
+
assert coth(pi*I*Rational(5, 2)) == 0
|
| 307 |
+
assert coth(pi*I*Rational(7, 2)) == 0
|
| 308 |
+
|
| 309 |
+
assert coth(pi*I/3) == -I/sqrt(3)
|
| 310 |
+
assert coth(pi*I*Rational(-2, 3)) == -I/sqrt(3)
|
| 311 |
+
|
| 312 |
+
assert coth(pi*I/4) == -I
|
| 313 |
+
assert coth(-pi*I/4) == I
|
| 314 |
+
assert coth(pi*I*Rational(17, 4)) == -I
|
| 315 |
+
assert coth(pi*I*Rational(-3, 4)) == -I
|
| 316 |
+
|
| 317 |
+
assert coth(pi*I/6) == -sqrt(3)*I
|
| 318 |
+
assert coth(-pi*I/6) == sqrt(3)*I
|
| 319 |
+
assert coth(pi*I*Rational(7, 6)) == -sqrt(3)*I
|
| 320 |
+
assert coth(pi*I*Rational(-5, 6)) == -sqrt(3)*I
|
| 321 |
+
|
| 322 |
+
assert coth(pi*I/105) == -cot(pi/105)*I
|
| 323 |
+
assert coth(-pi*I/105) == cot(pi/105)*I
|
| 324 |
+
|
| 325 |
+
assert unchanged(coth, 2 + 3*I)
|
| 326 |
+
|
| 327 |
+
assert coth(x*I) == -cot(x)*I
|
| 328 |
+
|
| 329 |
+
assert coth(k*pi*I) == -cot(k*pi)*I
|
| 330 |
+
assert coth(17*k*pi*I) == -cot(17*k*pi)*I
|
| 331 |
+
|
| 332 |
+
assert coth(k*pi*I) == -cot(k*pi)*I
|
| 333 |
+
|
| 334 |
+
assert coth(log(tan(2))) == coth(log(-tan(2)))
|
| 335 |
+
assert coth(1 + I*pi/2) == tanh(1)
|
| 336 |
+
|
| 337 |
+
assert coth(x).as_real_imag(deep=False) == (sinh(re(x))*cosh(re(x))/(sin(im(x))**2
|
| 338 |
+
+ sinh(re(x))**2),
|
| 339 |
+
-sin(im(x))*cos(im(x))/(sin(im(x))**2 + sinh(re(x))**2))
|
| 340 |
+
x = Symbol('x', extended_real=True)
|
| 341 |
+
assert coth(x).as_real_imag(deep=False) == (coth(x), 0)
|
| 342 |
+
|
| 343 |
+
assert expand_trig(coth(2*x)) == (coth(x)**2 + 1)/(2*coth(x))
|
| 344 |
+
assert expand_trig(coth(3*x)) == (coth(x)**3 + 3*coth(x))/(1 + 3*coth(x)**2)
|
| 345 |
+
|
| 346 |
+
assert expand_trig(coth(x + y)) == (1 + coth(x)*coth(y))/(coth(x) + coth(y))
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
def test_coth_series():
|
| 350 |
+
x = Symbol('x')
|
| 351 |
+
assert coth(x).series(x, 0, 8) == \
|
| 352 |
+
1/x + x/3 - x**3/45 + 2*x**5/945 - x**7/4725 + O(x**8)
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
def test_coth_fdiff():
|
| 356 |
+
x = Symbol('x')
|
| 357 |
+
raises(ArgumentIndexError, lambda: coth(x).fdiff(2))
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
def test_csch():
|
| 361 |
+
x, y = symbols('x,y')
|
| 362 |
+
|
| 363 |
+
k = Symbol('k', integer=True)
|
| 364 |
+
n = Symbol('n', positive=True)
|
| 365 |
+
|
| 366 |
+
assert csch(nan) is nan
|
| 367 |
+
assert csch(zoo) is nan
|
| 368 |
+
|
| 369 |
+
assert csch(oo) == 0
|
| 370 |
+
assert csch(-oo) == 0
|
| 371 |
+
|
| 372 |
+
assert csch(0) is zoo
|
| 373 |
+
|
| 374 |
+
assert csch(-1) == -csch(1)
|
| 375 |
+
|
| 376 |
+
assert csch(-x) == -csch(x)
|
| 377 |
+
assert csch(-pi) == -csch(pi)
|
| 378 |
+
assert csch(-2**1024 * E) == -csch(2**1024 * E)
|
| 379 |
+
|
| 380 |
+
assert csch(pi*I) is zoo
|
| 381 |
+
assert csch(-pi*I) is zoo
|
| 382 |
+
assert csch(2*pi*I) is zoo
|
| 383 |
+
assert csch(-2*pi*I) is zoo
|
| 384 |
+
assert csch(-3*10**73*pi*I) is zoo
|
| 385 |
+
assert csch(7*10**103*pi*I) is zoo
|
| 386 |
+
|
| 387 |
+
assert csch(pi*I/2) == -I
|
| 388 |
+
assert csch(-pi*I/2) == I
|
| 389 |
+
assert csch(pi*I*Rational(5, 2)) == -I
|
| 390 |
+
assert csch(pi*I*Rational(7, 2)) == I
|
| 391 |
+
|
| 392 |
+
assert csch(pi*I/3) == -2/sqrt(3)*I
|
| 393 |
+
assert csch(pi*I*Rational(-2, 3)) == 2/sqrt(3)*I
|
| 394 |
+
|
| 395 |
+
assert csch(pi*I/4) == -sqrt(2)*I
|
| 396 |
+
assert csch(-pi*I/4) == sqrt(2)*I
|
| 397 |
+
assert csch(pi*I*Rational(7, 4)) == sqrt(2)*I
|
| 398 |
+
assert csch(pi*I*Rational(-3, 4)) == sqrt(2)*I
|
| 399 |
+
|
| 400 |
+
assert csch(pi*I/6) == -2*I
|
| 401 |
+
assert csch(-pi*I/6) == 2*I
|
| 402 |
+
assert csch(pi*I*Rational(7, 6)) == 2*I
|
| 403 |
+
assert csch(pi*I*Rational(-7, 6)) == -2*I
|
| 404 |
+
assert csch(pi*I*Rational(-5, 6)) == 2*I
|
| 405 |
+
|
| 406 |
+
assert csch(pi*I/105) == -1/sin(pi/105)*I
|
| 407 |
+
assert csch(-pi*I/105) == 1/sin(pi/105)*I
|
| 408 |
+
|
| 409 |
+
assert csch(x*I) == -1/sin(x)*I
|
| 410 |
+
|
| 411 |
+
assert csch(k*pi*I) is zoo
|
| 412 |
+
assert csch(17*k*pi*I) is zoo
|
| 413 |
+
|
| 414 |
+
assert csch(k*pi*I/2) == -1/sin(k*pi/2)*I
|
| 415 |
+
|
| 416 |
+
assert csch(n).is_real is True
|
| 417 |
+
|
| 418 |
+
assert expand_trig(csch(x + y)) == 1/(sinh(x)*cosh(y) + cosh(x)*sinh(y))
|
| 419 |
+
|
| 420 |
+
|
| 421 |
+
def test_csch_series():
|
| 422 |
+
x = Symbol('x')
|
| 423 |
+
assert csch(x).series(x, 0, 10) == \
|
| 424 |
+
1/ x - x/6 + 7*x**3/360 - 31*x**5/15120 + 127*x**7/604800 \
|
| 425 |
+
- 73*x**9/3421440 + O(x**10)
|
| 426 |
+
|
| 427 |
+
|
| 428 |
+
def test_csch_fdiff():
|
| 429 |
+
x = Symbol('x')
|
| 430 |
+
raises(ArgumentIndexError, lambda: csch(x).fdiff(2))
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
def test_sech():
|
| 434 |
+
x, y = symbols('x, y')
|
| 435 |
+
|
| 436 |
+
k = Symbol('k', integer=True)
|
| 437 |
+
n = Symbol('n', positive=True)
|
| 438 |
+
|
| 439 |
+
assert sech(nan) is nan
|
| 440 |
+
assert sech(zoo) is nan
|
| 441 |
+
|
| 442 |
+
assert sech(oo) == 0
|
| 443 |
+
assert sech(-oo) == 0
|
| 444 |
+
|
| 445 |
+
assert sech(0) == 1
|
| 446 |
+
|
| 447 |
+
assert sech(-1) == sech(1)
|
| 448 |
+
assert sech(-x) == sech(x)
|
| 449 |
+
|
| 450 |
+
assert sech(pi*I) == sec(pi)
|
| 451 |
+
|
| 452 |
+
assert sech(-pi*I) == sec(pi)
|
| 453 |
+
assert sech(-2**1024 * E) == sech(2**1024 * E)
|
| 454 |
+
|
| 455 |
+
assert sech(pi*I/2) is zoo
|
| 456 |
+
assert sech(-pi*I/2) is zoo
|
| 457 |
+
assert sech((-3*10**73 + 1)*pi*I/2) is zoo
|
| 458 |
+
assert sech((7*10**103 + 1)*pi*I/2) is zoo
|
| 459 |
+
|
| 460 |
+
assert sech(pi*I) == -1
|
| 461 |
+
assert sech(-pi*I) == -1
|
| 462 |
+
assert sech(5*pi*I) == -1
|
| 463 |
+
assert sech(8*pi*I) == 1
|
| 464 |
+
|
| 465 |
+
assert sech(pi*I/3) == 2
|
| 466 |
+
assert sech(pi*I*Rational(-2, 3)) == -2
|
| 467 |
+
|
| 468 |
+
assert sech(pi*I/4) == sqrt(2)
|
| 469 |
+
assert sech(-pi*I/4) == sqrt(2)
|
| 470 |
+
assert sech(pi*I*Rational(5, 4)) == -sqrt(2)
|
| 471 |
+
assert sech(pi*I*Rational(-5, 4)) == -sqrt(2)
|
| 472 |
+
|
| 473 |
+
assert sech(pi*I/6) == 2/sqrt(3)
|
| 474 |
+
assert sech(-pi*I/6) == 2/sqrt(3)
|
| 475 |
+
assert sech(pi*I*Rational(7, 6)) == -2/sqrt(3)
|
| 476 |
+
assert sech(pi*I*Rational(-5, 6)) == -2/sqrt(3)
|
| 477 |
+
|
| 478 |
+
assert sech(pi*I/105) == 1/cos(pi/105)
|
| 479 |
+
assert sech(-pi*I/105) == 1/cos(pi/105)
|
| 480 |
+
|
| 481 |
+
assert sech(x*I) == 1/cos(x)
|
| 482 |
+
|
| 483 |
+
assert sech(k*pi*I) == 1/cos(k*pi)
|
| 484 |
+
assert sech(17*k*pi*I) == 1/cos(17*k*pi)
|
| 485 |
+
|
| 486 |
+
assert sech(n).is_real is True
|
| 487 |
+
|
| 488 |
+
assert expand_trig(sech(x + y)) == 1/(cosh(x)*cosh(y) + sinh(x)*sinh(y))
|
| 489 |
+
|
| 490 |
+
|
| 491 |
+
def test_sech_series():
|
| 492 |
+
x = Symbol('x')
|
| 493 |
+
assert sech(x).series(x, 0, 10) == \
|
| 494 |
+
1 - x**2/2 + 5*x**4/24 - 61*x**6/720 + 277*x**8/8064 + O(x**10)
|
| 495 |
+
|
| 496 |
+
|
| 497 |
+
def test_sech_fdiff():
|
| 498 |
+
x = Symbol('x')
|
| 499 |
+
raises(ArgumentIndexError, lambda: sech(x).fdiff(2))
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
def test_asinh():
|
| 503 |
+
x, y = symbols('x,y')
|
| 504 |
+
assert unchanged(asinh, x)
|
| 505 |
+
assert asinh(-x) == -asinh(x)
|
| 506 |
+
|
| 507 |
+
# at specific points
|
| 508 |
+
assert asinh(nan) is nan
|
| 509 |
+
assert asinh( 0) == 0
|
| 510 |
+
assert asinh(+1) == log(sqrt(2) + 1)
|
| 511 |
+
|
| 512 |
+
assert asinh(-1) == log(sqrt(2) - 1)
|
| 513 |
+
assert asinh(I) == pi*I/2
|
| 514 |
+
assert asinh(-I) == -pi*I/2
|
| 515 |
+
assert asinh(I/2) == pi*I/6
|
| 516 |
+
assert asinh(-I/2) == -pi*I/6
|
| 517 |
+
|
| 518 |
+
# at infinites
|
| 519 |
+
assert asinh(oo) is oo
|
| 520 |
+
assert asinh(-oo) is -oo
|
| 521 |
+
|
| 522 |
+
assert asinh(I*oo) is oo
|
| 523 |
+
assert asinh(-I *oo) is -oo
|
| 524 |
+
|
| 525 |
+
assert asinh(zoo) is zoo
|
| 526 |
+
|
| 527 |
+
# properties
|
| 528 |
+
assert asinh(I *(sqrt(3) - 1)/(2**Rational(3, 2))) == pi*I/12
|
| 529 |
+
assert asinh(-I *(sqrt(3) - 1)/(2**Rational(3, 2))) == -pi*I/12
|
| 530 |
+
|
| 531 |
+
assert asinh(I*(sqrt(5) - 1)/4) == pi*I/10
|
| 532 |
+
assert asinh(-I*(sqrt(5) - 1)/4) == -pi*I/10
|
| 533 |
+
|
| 534 |
+
assert asinh(I*(sqrt(5) + 1)/4) == pi*I*Rational(3, 10)
|
| 535 |
+
assert asinh(-I*(sqrt(5) + 1)/4) == pi*I*Rational(-3, 10)
|
| 536 |
+
|
| 537 |
+
# reality
|
| 538 |
+
assert asinh(S(2)).is_real is True
|
| 539 |
+
assert asinh(S(2)).is_finite is True
|
| 540 |
+
assert asinh(S(-2)).is_real is True
|
| 541 |
+
assert asinh(S(oo)).is_extended_real is True
|
| 542 |
+
assert asinh(-S(oo)).is_real is False
|
| 543 |
+
assert (asinh(2) - oo) == -oo
|
| 544 |
+
assert asinh(symbols('y', real=True)).is_real is True
|
| 545 |
+
|
| 546 |
+
# Symmetry
|
| 547 |
+
assert asinh(Rational(-1, 2)) == -asinh(S.Half)
|
| 548 |
+
|
| 549 |
+
# inverse composition
|
| 550 |
+
assert unchanged(asinh, sinh(Symbol('v1')))
|
| 551 |
+
|
| 552 |
+
assert asinh(sinh(0, evaluate=False)) == 0
|
| 553 |
+
assert asinh(sinh(-3, evaluate=False)) == -3
|
| 554 |
+
assert asinh(sinh(2, evaluate=False)) == 2
|
| 555 |
+
assert asinh(sinh(I, evaluate=False)) == I
|
| 556 |
+
assert asinh(sinh(-I, evaluate=False)) == -I
|
| 557 |
+
assert asinh(sinh(5*I, evaluate=False)) == -2*I*pi + 5*I
|
| 558 |
+
assert asinh(sinh(15 + 11*I)) == 15 - 4*I*pi + 11*I
|
| 559 |
+
assert asinh(sinh(-73 + 97*I)) == 73 - 97*I + 31*I*pi
|
| 560 |
+
assert asinh(sinh(-7 - 23*I)) == 7 - 7*I*pi + 23*I
|
| 561 |
+
assert asinh(sinh(13 - 3*I)) == -13 - I*pi + 3*I
|
| 562 |
+
p = Symbol('p', positive=True)
|
| 563 |
+
assert asinh(p).is_zero is False
|
| 564 |
+
assert asinh(sinh(0, evaluate=False), evaluate=False).is_zero is True
|
| 565 |
+
|
| 566 |
+
|
| 567 |
+
def test_asinh_rewrite():
|
| 568 |
+
x = Symbol('x')
|
| 569 |
+
assert asinh(x).rewrite(log) == log(x + sqrt(x**2 + 1))
|
| 570 |
+
assert asinh(x).rewrite(atanh) == atanh(x/sqrt(1 + x**2))
|
| 571 |
+
assert asinh(x).rewrite(asin) == -I*asin(I*x, evaluate=False)
|
| 572 |
+
assert asinh(x*(1 + I)).rewrite(asin) == -I*asin(I*x*(1+I))
|
| 573 |
+
assert asinh(x).rewrite(acos) == I*acos(I*x, evaluate=False) - I*pi/2
|
| 574 |
+
|
| 575 |
+
|
| 576 |
+
def test_asinh_leading_term():
|
| 577 |
+
x = Symbol('x')
|
| 578 |
+
assert asinh(x).as_leading_term(x, cdir=1) == x
|
| 579 |
+
# Tests concerning branch points
|
| 580 |
+
assert asinh(x + I).as_leading_term(x, cdir=1) == I*pi/2
|
| 581 |
+
assert asinh(x - I).as_leading_term(x, cdir=1) == -I*pi/2
|
| 582 |
+
assert asinh(1/x).as_leading_term(x, cdir=1) == -log(x) + log(2)
|
| 583 |
+
assert asinh(1/x).as_leading_term(x, cdir=-1) == log(x) - log(2) - I*pi
|
| 584 |
+
# Tests concerning points lying on branch cuts
|
| 585 |
+
assert asinh(x + 2*I).as_leading_term(x, cdir=1) == I*asin(2)
|
| 586 |
+
assert asinh(x + 2*I).as_leading_term(x, cdir=-1) == -I*asin(2) + I*pi
|
| 587 |
+
assert asinh(x - 2*I).as_leading_term(x, cdir=1) == -I*pi + I*asin(2)
|
| 588 |
+
assert asinh(x - 2*I).as_leading_term(x, cdir=-1) == -I*asin(2)
|
| 589 |
+
# Tests concerning re(ndir) == 0
|
| 590 |
+
assert asinh(2*I + I*x - x**2).as_leading_term(x, cdir=1) == log(2 - sqrt(3)) + I*pi/2
|
| 591 |
+
assert asinh(2*I + I*x - x**2).as_leading_term(x, cdir=-1) == log(2 - sqrt(3)) + I*pi/2
|
| 592 |
+
|
| 593 |
+
|
| 594 |
+
def test_asinh_series():
|
| 595 |
+
x = Symbol('x')
|
| 596 |
+
assert asinh(x).series(x, 0, 8) == \
|
| 597 |
+
x - x**3/6 + 3*x**5/40 - 5*x**7/112 + O(x**8)
|
| 598 |
+
t5 = asinh(x).taylor_term(5, x)
|
| 599 |
+
assert t5 == 3*x**5/40
|
| 600 |
+
assert asinh(x).taylor_term(7, x, t5, 0) == -5*x**7/112
|
| 601 |
+
|
| 602 |
+
|
| 603 |
+
def test_asinh_nseries():
|
| 604 |
+
x = Symbol('x')
|
| 605 |
+
# Tests concerning branch points
|
| 606 |
+
assert asinh(x + I)._eval_nseries(x, 4, None) == I*pi/2 + \
|
| 607 |
+
sqrt(x)*(1 - I) + x**(S(3)/2)*(S(1)/12 + I/12) + x**(S(5)/2)*(-S(3)/160 + 3*I/160) + \
|
| 608 |
+
x**(S(7)/2)*(-S(5)/896 - 5*I/896) + O(x**4)
|
| 609 |
+
assert asinh(x - I)._eval_nseries(x, 4, None) == -I*pi/2 + \
|
| 610 |
+
sqrt(x)*(1 + I) + x**(S(3)/2)*(S(1)/12 - I/12) + x**(S(5)/2)*(-S(3)/160 - 3*I/160) + \
|
| 611 |
+
x**(S(7)/2)*(-S(5)/896 + 5*I/896) + O(x**4)
|
| 612 |
+
# Tests concerning points lying on branch cuts
|
| 613 |
+
assert asinh(x + 2*I)._eval_nseries(x, 4, None, cdir=1) == I*asin(2) - \
|
| 614 |
+
sqrt(3)*I*x/3 + sqrt(3)*x**2/9 + sqrt(3)*I*x**3/18 + O(x**4)
|
| 615 |
+
assert asinh(x + 2*I)._eval_nseries(x, 4, None, cdir=-1) == I*pi - I*asin(2) + \
|
| 616 |
+
sqrt(3)*I*x/3 - sqrt(3)*x**2/9 - sqrt(3)*I*x**3/18 + O(x**4)
|
| 617 |
+
assert asinh(x - 2*I)._eval_nseries(x, 4, None, cdir=1) == I*asin(2) - I*pi + \
|
| 618 |
+
sqrt(3)*I*x/3 + sqrt(3)*x**2/9 - sqrt(3)*I*x**3/18 + O(x**4)
|
| 619 |
+
assert asinh(x - 2*I)._eval_nseries(x, 4, None, cdir=-1) == -I*asin(2) - \
|
| 620 |
+
sqrt(3)*I*x/3 - sqrt(3)*x**2/9 + sqrt(3)*I*x**3/18 + O(x**4)
|
| 621 |
+
# Tests concerning re(ndir) == 0
|
| 622 |
+
assert asinh(2*I + I*x - x**2)._eval_nseries(x, 4, None) == I*pi/2 + log(2 - sqrt(3)) - \
|
| 623 |
+
sqrt(3)*x/3 + x**2*(sqrt(3)/9 - sqrt(3)*I/3) + x**3*(-sqrt(3)/18 + 2*sqrt(3)*I/9) + O(x**4)
|
| 624 |
+
|
| 625 |
+
|
| 626 |
+
def test_asinh_fdiff():
|
| 627 |
+
x = Symbol('x')
|
| 628 |
+
raises(ArgumentIndexError, lambda: asinh(x).fdiff(2))
|
| 629 |
+
|
| 630 |
+
|
| 631 |
+
def test_acosh():
|
| 632 |
+
x = Symbol('x')
|
| 633 |
+
|
| 634 |
+
assert unchanged(acosh, -x)
|
| 635 |
+
|
| 636 |
+
#at specific points
|
| 637 |
+
assert acosh(1) == 0
|
| 638 |
+
assert acosh(-1) == pi*I
|
| 639 |
+
assert acosh(0) == I*pi/2
|
| 640 |
+
assert acosh(S.Half) == I*pi/3
|
| 641 |
+
assert acosh(Rational(-1, 2)) == pi*I*Rational(2, 3)
|
| 642 |
+
assert acosh(nan) is nan
|
| 643 |
+
|
| 644 |
+
# at infinites
|
| 645 |
+
assert acosh(oo) is oo
|
| 646 |
+
assert acosh(-oo) is oo
|
| 647 |
+
|
| 648 |
+
assert acosh(I*oo) == oo + I*pi/2
|
| 649 |
+
assert acosh(-I*oo) == oo - I*pi/2
|
| 650 |
+
|
| 651 |
+
assert acosh(zoo) is zoo
|
| 652 |
+
|
| 653 |
+
assert acosh(I) == log(I*(1 + sqrt(2)))
|
| 654 |
+
assert acosh(-I) == log(-I*(1 + sqrt(2)))
|
| 655 |
+
assert acosh((sqrt(3) - 1)/(2*sqrt(2))) == pi*I*Rational(5, 12)
|
| 656 |
+
assert acosh(-(sqrt(3) - 1)/(2*sqrt(2))) == pi*I*Rational(7, 12)
|
| 657 |
+
assert acosh(sqrt(2)/2) == I*pi/4
|
| 658 |
+
assert acosh(-sqrt(2)/2) == I*pi*Rational(3, 4)
|
| 659 |
+
assert acosh(sqrt(3)/2) == I*pi/6
|
| 660 |
+
assert acosh(-sqrt(3)/2) == I*pi*Rational(5, 6)
|
| 661 |
+
assert acosh(sqrt(2 + sqrt(2))/2) == I*pi/8
|
| 662 |
+
assert acosh(-sqrt(2 + sqrt(2))/2) == I*pi*Rational(7, 8)
|
| 663 |
+
assert acosh(sqrt(2 - sqrt(2))/2) == I*pi*Rational(3, 8)
|
| 664 |
+
assert acosh(-sqrt(2 - sqrt(2))/2) == I*pi*Rational(5, 8)
|
| 665 |
+
assert acosh((1 + sqrt(3))/(2*sqrt(2))) == I*pi/12
|
| 666 |
+
assert acosh(-(1 + sqrt(3))/(2*sqrt(2))) == I*pi*Rational(11, 12)
|
| 667 |
+
assert acosh((sqrt(5) + 1)/4) == I*pi/5
|
| 668 |
+
assert acosh(-(sqrt(5) + 1)/4) == I*pi*Rational(4, 5)
|
| 669 |
+
|
| 670 |
+
assert str(acosh(5*I).n(6)) == '2.31244 + 1.5708*I'
|
| 671 |
+
assert str(acosh(-5*I).n(6)) == '2.31244 - 1.5708*I'
|
| 672 |
+
|
| 673 |
+
# inverse composition
|
| 674 |
+
assert unchanged(acosh, Symbol('v1'))
|
| 675 |
+
|
| 676 |
+
assert acosh(cosh(-3, evaluate=False)) == 3
|
| 677 |
+
assert acosh(cosh(3, evaluate=False)) == 3
|
| 678 |
+
assert acosh(cosh(0, evaluate=False)) == 0
|
| 679 |
+
assert acosh(cosh(I, evaluate=False)) == I
|
| 680 |
+
assert acosh(cosh(-I, evaluate=False)) == I
|
| 681 |
+
assert acosh(cosh(7*I, evaluate=False)) == -2*I*pi + 7*I
|
| 682 |
+
assert acosh(cosh(1 + I)) == 1 + I
|
| 683 |
+
assert acosh(cosh(3 - 3*I)) == 3 - 3*I
|
| 684 |
+
assert acosh(cosh(-3 + 2*I)) == 3 - 2*I
|
| 685 |
+
assert acosh(cosh(-5 - 17*I)) == 5 - 6*I*pi + 17*I
|
| 686 |
+
assert acosh(cosh(-21 + 11*I)) == 21 - 11*I + 4*I*pi
|
| 687 |
+
assert acosh(cosh(cosh(1) + I)) == cosh(1) + I
|
| 688 |
+
assert acosh(1, evaluate=False).is_zero is True
|
| 689 |
+
|
| 690 |
+
# Reality
|
| 691 |
+
assert acosh(S(2)).is_real is True
|
| 692 |
+
assert acosh(S(2)).is_extended_real is True
|
| 693 |
+
assert acosh(oo).is_extended_real is True
|
| 694 |
+
assert acosh(S(2)).is_finite is True
|
| 695 |
+
assert acosh(S(1) / 5).is_real is False
|
| 696 |
+
assert (acosh(2) - oo) == -oo
|
| 697 |
+
assert acosh(symbols('y', real=True)).is_real is None
|
| 698 |
+
|
| 699 |
+
|
| 700 |
+
def test_acosh_rewrite():
|
| 701 |
+
x = Symbol('x')
|
| 702 |
+
assert acosh(x).rewrite(log) == log(x + sqrt(x - 1)*sqrt(x + 1))
|
| 703 |
+
assert acosh(x).rewrite(asin) == sqrt(x - 1)*(-asin(x) + pi/2)/sqrt(1 - x)
|
| 704 |
+
assert acosh(x).rewrite(asinh) == sqrt(x - 1)*(I*asinh(I*x, evaluate=False) + pi/2)/sqrt(1 - x)
|
| 705 |
+
assert acosh(x).rewrite(atanh) == \
|
| 706 |
+
(sqrt(x - 1)*sqrt(x + 1)*atanh(sqrt(x**2 - 1)/x)/sqrt(x**2 - 1) +
|
| 707 |
+
pi*sqrt(x - 1)*(-x*sqrt(x**(-2)) + 1)/(2*sqrt(1 - x)))
|
| 708 |
+
x = Symbol('x', positive=True)
|
| 709 |
+
assert acosh(x).rewrite(atanh) == \
|
| 710 |
+
sqrt(x - 1)*sqrt(x + 1)*atanh(sqrt(x**2 - 1)/x)/sqrt(x**2 - 1)
|
| 711 |
+
|
| 712 |
+
|
| 713 |
+
def test_acosh_leading_term():
|
| 714 |
+
x = Symbol('x')
|
| 715 |
+
# Tests concerning branch points
|
| 716 |
+
assert acosh(x).as_leading_term(x) == I*pi/2
|
| 717 |
+
assert acosh(x + 1).as_leading_term(x) == sqrt(2)*sqrt(x)
|
| 718 |
+
assert acosh(x - 1).as_leading_term(x) == I*pi
|
| 719 |
+
assert acosh(1/x).as_leading_term(x, cdir=1) == -log(x) + log(2)
|
| 720 |
+
assert acosh(1/x).as_leading_term(x, cdir=-1) == -log(x) + log(2) + 2*I*pi
|
| 721 |
+
# Tests concerning points lying on branch cuts
|
| 722 |
+
assert acosh(I*x - 2).as_leading_term(x, cdir=1) == acosh(-2)
|
| 723 |
+
assert acosh(-I*x - 2).as_leading_term(x, cdir=1) == -2*I*pi + acosh(-2)
|
| 724 |
+
assert acosh(x**2 - I*x + S(1)/3).as_leading_term(x, cdir=1) == -acosh(S(1)/3)
|
| 725 |
+
assert acosh(x**2 - I*x + S(1)/3).as_leading_term(x, cdir=-1) == acosh(S(1)/3)
|
| 726 |
+
assert acosh(1/(I*x - 3)).as_leading_term(x, cdir=1) == -acosh(-S(1)/3)
|
| 727 |
+
assert acosh(1/(I*x - 3)).as_leading_term(x, cdir=-1) == acosh(-S(1)/3)
|
| 728 |
+
# Tests concerning im(ndir) == 0
|
| 729 |
+
assert acosh(-I*x**2 + x - 2).as_leading_term(x, cdir=1) == log(sqrt(3) + 2) - I*pi
|
| 730 |
+
assert acosh(-I*x**2 + x - 2).as_leading_term(x, cdir=-1) == log(sqrt(3) + 2) - I*pi
|
| 731 |
+
|
| 732 |
+
|
| 733 |
+
def test_acosh_series():
|
| 734 |
+
x = Symbol('x')
|
| 735 |
+
assert acosh(x).series(x, 0, 8) == \
|
| 736 |
+
-I*x + pi*I/2 - I*x**3/6 - 3*I*x**5/40 - 5*I*x**7/112 + O(x**8)
|
| 737 |
+
t5 = acosh(x).taylor_term(5, x)
|
| 738 |
+
assert t5 == - 3*I*x**5/40
|
| 739 |
+
assert acosh(x).taylor_term(7, x, t5, 0) == - 5*I*x**7/112
|
| 740 |
+
|
| 741 |
+
|
| 742 |
+
def test_acosh_nseries():
|
| 743 |
+
x = Symbol('x')
|
| 744 |
+
# Tests concerning branch points
|
| 745 |
+
assert acosh(x + 1)._eval_nseries(x, 4, None) == sqrt(2)*sqrt(x) - \
|
| 746 |
+
sqrt(2)*x**(S(3)/2)/12 + 3*sqrt(2)*x**(S(5)/2)/160 - 5*sqrt(2)*x**(S(7)/2)/896 + O(x**4)
|
| 747 |
+
# Tests concerning points lying on branch cuts
|
| 748 |
+
assert acosh(x - 1)._eval_nseries(x, 4, None) == I*pi - \
|
| 749 |
+
sqrt(2)*I*sqrt(x) - sqrt(2)*I*x**(S(3)/2)/12 - 3*sqrt(2)*I*x**(S(5)/2)/160 - \
|
| 750 |
+
5*sqrt(2)*I*x**(S(7)/2)/896 + O(x**4)
|
| 751 |
+
assert acosh(I*x - 2)._eval_nseries(x, 4, None, cdir=1) == acosh(-2) - \
|
| 752 |
+
sqrt(3)*I*x/3 + sqrt(3)*x**2/9 + sqrt(3)*I*x**3/18 + O(x**4)
|
| 753 |
+
assert acosh(-I*x - 2)._eval_nseries(x, 4, None, cdir=1) == acosh(-2) - \
|
| 754 |
+
2*I*pi + sqrt(3)*I*x/3 + sqrt(3)*x**2/9 - sqrt(3)*I*x**3/18 + O(x**4)
|
| 755 |
+
assert acosh(1/(I*x - 3))._eval_nseries(x, 4, None, cdir=1) == -acosh(-S(1)/3) + \
|
| 756 |
+
sqrt(2)*x/12 + 17*sqrt(2)*I*x**2/576 - 443*sqrt(2)*x**3/41472 + O(x**4)
|
| 757 |
+
assert acosh(1/(I*x - 3))._eval_nseries(x, 4, None, cdir=-1) == acosh(-S(1)/3) - \
|
| 758 |
+
sqrt(2)*x/12 - 17*sqrt(2)*I*x**2/576 + 443*sqrt(2)*x**3/41472 + O(x**4)
|
| 759 |
+
# Tests concerning im(ndir) == 0
|
| 760 |
+
assert acosh(-I*x**2 + x - 2)._eval_nseries(x, 4, None) == -I*pi + log(sqrt(3) + 2) - \
|
| 761 |
+
sqrt(3)*x/3 + x**2*(-sqrt(3)/9 + sqrt(3)*I/3) + x**3*(-sqrt(3)/18 + 2*sqrt(3)*I/9) + O(x**4)
|
| 762 |
+
|
| 763 |
+
|
| 764 |
+
def test_acosh_fdiff():
|
| 765 |
+
x = Symbol('x')
|
| 766 |
+
raises(ArgumentIndexError, lambda: acosh(x).fdiff(2))
|
| 767 |
+
|
| 768 |
+
|
| 769 |
+
def test_asech():
|
| 770 |
+
x = Symbol('x')
|
| 771 |
+
|
| 772 |
+
assert unchanged(asech, -x)
|
| 773 |
+
|
| 774 |
+
# values at fixed points
|
| 775 |
+
assert asech(1) == 0
|
| 776 |
+
assert asech(-1) == pi*I
|
| 777 |
+
assert asech(0) is oo
|
| 778 |
+
assert asech(2) == I*pi/3
|
| 779 |
+
assert asech(-2) == 2*I*pi / 3
|
| 780 |
+
assert asech(nan) is nan
|
| 781 |
+
|
| 782 |
+
# at infinites
|
| 783 |
+
assert asech(oo) == I*pi/2
|
| 784 |
+
assert asech(-oo) == I*pi/2
|
| 785 |
+
assert asech(zoo) == I*AccumBounds(-pi/2, pi/2)
|
| 786 |
+
|
| 787 |
+
assert asech(I) == log(1 + sqrt(2)) - I*pi/2
|
| 788 |
+
assert asech(-I) == log(1 + sqrt(2)) + I*pi/2
|
| 789 |
+
assert asech(sqrt(2) - sqrt(6)) == 11*I*pi / 12
|
| 790 |
+
assert asech(sqrt(2 - 2/sqrt(5))) == I*pi / 10
|
| 791 |
+
assert asech(-sqrt(2 - 2/sqrt(5))) == 9*I*pi / 10
|
| 792 |
+
assert asech(2 / sqrt(2 + sqrt(2))) == I*pi / 8
|
| 793 |
+
assert asech(-2 / sqrt(2 + sqrt(2))) == 7*I*pi / 8
|
| 794 |
+
assert asech(sqrt(5) - 1) == I*pi / 5
|
| 795 |
+
assert asech(1 - sqrt(5)) == 4*I*pi / 5
|
| 796 |
+
assert asech(-sqrt(2*(2 + sqrt(2)))) == 5*I*pi / 8
|
| 797 |
+
|
| 798 |
+
# properties
|
| 799 |
+
# asech(x) == acosh(1/x)
|
| 800 |
+
assert asech(sqrt(2)) == acosh(1/sqrt(2))
|
| 801 |
+
assert asech(2/sqrt(3)) == acosh(sqrt(3)/2)
|
| 802 |
+
assert asech(2/sqrt(2 + sqrt(2))) == acosh(sqrt(2 + sqrt(2))/2)
|
| 803 |
+
assert asech(2) == acosh(S.Half)
|
| 804 |
+
|
| 805 |
+
# reality
|
| 806 |
+
assert asech(S(2)).is_real is False
|
| 807 |
+
assert asech(-S(1) / 3).is_real is False
|
| 808 |
+
assert asech(S(2) / 3).is_finite is True
|
| 809 |
+
assert asech(S(0)).is_real is False
|
| 810 |
+
assert asech(S(0)).is_extended_real is True
|
| 811 |
+
assert asech(symbols('y', real=True)).is_real is None
|
| 812 |
+
|
| 813 |
+
# asech(x) == I*acos(1/x)
|
| 814 |
+
# (Note: the exact formula is asech(x) == +/- I*acos(1/x))
|
| 815 |
+
assert asech(-sqrt(2)) == I*acos(-1/sqrt(2))
|
| 816 |
+
assert asech(-2/sqrt(3)) == I*acos(-sqrt(3)/2)
|
| 817 |
+
assert asech(-S(2)) == I*acos(Rational(-1, 2))
|
| 818 |
+
assert asech(-2/sqrt(2)) == I*acos(-sqrt(2)/2)
|
| 819 |
+
|
| 820 |
+
# sech(asech(x)) / x == 1
|
| 821 |
+
assert expand_mul(sech(asech(sqrt(6) - sqrt(2))) / (sqrt(6) - sqrt(2))) == 1
|
| 822 |
+
assert expand_mul(sech(asech(sqrt(6) + sqrt(2))) / (sqrt(6) + sqrt(2))) == 1
|
| 823 |
+
assert (sech(asech(sqrt(2 + 2/sqrt(5)))) / (sqrt(2 + 2/sqrt(5)))).simplify() == 1
|
| 824 |
+
assert (sech(asech(-sqrt(2 + 2/sqrt(5)))) / (-sqrt(2 + 2/sqrt(5)))).simplify() == 1
|
| 825 |
+
assert (sech(asech(sqrt(2*(2 + sqrt(2))))) / (sqrt(2*(2 + sqrt(2))))).simplify() == 1
|
| 826 |
+
assert expand_mul(sech(asech(1 + sqrt(5))) / (1 + sqrt(5))) == 1
|
| 827 |
+
assert expand_mul(sech(asech(-1 - sqrt(5))) / (-1 - sqrt(5))) == 1
|
| 828 |
+
assert expand_mul(sech(asech(-sqrt(6) - sqrt(2))) / (-sqrt(6) - sqrt(2))) == 1
|
| 829 |
+
|
| 830 |
+
# numerical evaluation
|
| 831 |
+
assert str(asech(5*I).n(6)) == '0.19869 - 1.5708*I'
|
| 832 |
+
assert str(asech(-5*I).n(6)) == '0.19869 + 1.5708*I'
|
| 833 |
+
|
| 834 |
+
|
| 835 |
+
def test_asech_leading_term():
|
| 836 |
+
x = Symbol('x')
|
| 837 |
+
# Tests concerning branch points
|
| 838 |
+
assert asech(x).as_leading_term(x, cdir=1) == -log(x) + log(2)
|
| 839 |
+
assert asech(x).as_leading_term(x, cdir=-1) == -log(x) + log(2) + 2*I*pi
|
| 840 |
+
assert asech(x + 1).as_leading_term(x, cdir=1) == sqrt(2)*I*sqrt(x)
|
| 841 |
+
assert asech(1/x).as_leading_term(x, cdir=1) == I*pi/2
|
| 842 |
+
# Tests concerning points lying on branch cuts
|
| 843 |
+
assert asech(x - 1).as_leading_term(x, cdir=1) == I*pi
|
| 844 |
+
assert asech(I*x + 3).as_leading_term(x, cdir=1) == -asech(3)
|
| 845 |
+
assert asech(-I*x + 3).as_leading_term(x, cdir=1) == asech(3)
|
| 846 |
+
assert asech(I*x - 3).as_leading_term(x, cdir=1) == -asech(-3)
|
| 847 |
+
assert asech(-I*x - 3).as_leading_term(x, cdir=1) == asech(-3)
|
| 848 |
+
assert asech(I*x - S(1)/3).as_leading_term(x, cdir=1) == -2*I*pi + asech(-S(1)/3)
|
| 849 |
+
assert asech(I*x - S(1)/3).as_leading_term(x, cdir=-1) == asech(-S(1)/3)
|
| 850 |
+
# Tests concerning im(ndir) == 0
|
| 851 |
+
assert asech(-I*x**2 + x - 3).as_leading_term(x, cdir=1) == log(-S(1)/3 + 2*sqrt(2)*I/3)
|
| 852 |
+
assert asech(-I*x**2 + x - 3).as_leading_term(x, cdir=-1) == log(-S(1)/3 + 2*sqrt(2)*I/3)
|
| 853 |
+
|
| 854 |
+
|
| 855 |
+
def test_asech_series():
|
| 856 |
+
x = Symbol('x')
|
| 857 |
+
assert asech(x).series(x, 0, 9, cdir=1) == log(2) - log(x) - x**2/4 - 3*x**4/32 \
|
| 858 |
+
- 5*x**6/96 - 35*x**8/1024 + O(x**9)
|
| 859 |
+
assert asech(x).series(x, 0, 9, cdir=-1) == I*pi + log(2) - log(-x) - x**2/4 - \
|
| 860 |
+
3*x**4/32 - 5*x**6/96 - 35*x**8/1024 + O(x**9)
|
| 861 |
+
t6 = asech(x).taylor_term(6, x)
|
| 862 |
+
assert t6 == -5*x**6/96
|
| 863 |
+
assert asech(x).taylor_term(8, x, t6, 0) == -35*x**8/1024
|
| 864 |
+
|
| 865 |
+
|
| 866 |
+
def test_asech_nseries():
|
| 867 |
+
x = Symbol('x')
|
| 868 |
+
# Tests concerning branch points
|
| 869 |
+
assert asech(x + 1)._eval_nseries(x, 4, None) == sqrt(2)*sqrt(-x) + 5*sqrt(2)*(-x)**(S(3)/2)/12 + \
|
| 870 |
+
43*sqrt(2)*(-x)**(S(5)/2)/160 + 177*sqrt(2)*(-x)**(S(7)/2)/896 + O(x**4)
|
| 871 |
+
# Tests concerning points lying on branch cuts
|
| 872 |
+
assert asech(x - 1)._eval_nseries(x, 4, None) == I*pi + sqrt(2)*sqrt(x) + \
|
| 873 |
+
5*sqrt(2)*x**(S(3)/2)/12 + 43*sqrt(2)*x**(S(5)/2)/160 + 177*sqrt(2)*x**(S(7)/2)/896 + O(x**4)
|
| 874 |
+
assert asech(I*x + 3)._eval_nseries(x, 4, None) == -asech(3) + sqrt(2)*x/12 - \
|
| 875 |
+
17*sqrt(2)*I*x**2/576 - 443*sqrt(2)*x**3/41472 + O(x**4)
|
| 876 |
+
assert asech(-I*x + 3)._eval_nseries(x, 4, None) == asech(3) + sqrt(2)*x/12 + \
|
| 877 |
+
17*sqrt(2)*I*x**2/576 - 443*sqrt(2)*x**3/41472 + O(x**4)
|
| 878 |
+
assert asech(I*x - 3)._eval_nseries(x, 4, None) == -asech(-3) - sqrt(2)*x/12 - \
|
| 879 |
+
17*sqrt(2)*I*x**2/576 + 443*sqrt(2)*x**3/41472 + O(x**4)
|
| 880 |
+
assert asech(-I*x - 3)._eval_nseries(x, 4, None) == asech(-3) - sqrt(2)*x/12 + \
|
| 881 |
+
17*sqrt(2)*I*x**2/576 + 443*sqrt(2)*x**3/41472 + O(x**4)
|
| 882 |
+
# Tests concerning im(ndir) == 0
|
| 883 |
+
assert asech(-I*x**2 + x - 2)._eval_nseries(x, 3, None) == 2*I*pi/3 + sqrt(3)*I*x/6 + \
|
| 884 |
+
x**2*(sqrt(3)/6 + 7*sqrt(3)*I/72) + O(x**3)
|
| 885 |
+
|
| 886 |
+
|
| 887 |
+
def test_asech_rewrite():
|
| 888 |
+
x = Symbol('x')
|
| 889 |
+
assert asech(x).rewrite(log) == log(1/x + sqrt(1/x - 1) * sqrt(1/x + 1))
|
| 890 |
+
assert asech(x).rewrite(acosh) == acosh(1/x)
|
| 891 |
+
assert asech(x).rewrite(asinh) == sqrt(-1 + 1/x)*(I*asinh(I/x, evaluate=False) + pi/2)/sqrt(1 - 1/x)
|
| 892 |
+
assert asech(x).rewrite(atanh) == \
|
| 893 |
+
sqrt(x + 1)*sqrt(1/(x + 1))*atanh(sqrt(1 - x**2)) + I*pi*(-sqrt(x)*sqrt(1/x) + 1 - I*sqrt(x**2)/(2*sqrt(-x**2)) - I*sqrt(-x)/(2*sqrt(x)))
|
| 894 |
+
|
| 895 |
+
|
| 896 |
+
def test_asech_fdiff():
|
| 897 |
+
x = Symbol('x')
|
| 898 |
+
raises(ArgumentIndexError, lambda: asech(x).fdiff(2))
|
| 899 |
+
|
| 900 |
+
|
| 901 |
+
def test_acsch():
|
| 902 |
+
x = Symbol('x')
|
| 903 |
+
|
| 904 |
+
assert unchanged(acsch, x)
|
| 905 |
+
assert acsch(-x) == -acsch(x)
|
| 906 |
+
|
| 907 |
+
# values at fixed points
|
| 908 |
+
assert acsch(1) == log(1 + sqrt(2))
|
| 909 |
+
assert acsch(-1) == - log(1 + sqrt(2))
|
| 910 |
+
assert acsch(0) is zoo
|
| 911 |
+
assert acsch(2) == log((1+sqrt(5))/2)
|
| 912 |
+
assert acsch(-2) == - log((1+sqrt(5))/2)
|
| 913 |
+
|
| 914 |
+
assert acsch(I) == - I*pi/2
|
| 915 |
+
assert acsch(-I) == I*pi/2
|
| 916 |
+
assert acsch(-I*(sqrt(6) + sqrt(2))) == I*pi / 12
|
| 917 |
+
assert acsch(I*(sqrt(2) + sqrt(6))) == -I*pi / 12
|
| 918 |
+
assert acsch(-I*(1 + sqrt(5))) == I*pi / 10
|
| 919 |
+
assert acsch(I*(1 + sqrt(5))) == -I*pi / 10
|
| 920 |
+
assert acsch(-I*2 / sqrt(2 - sqrt(2))) == I*pi / 8
|
| 921 |
+
assert acsch(I*2 / sqrt(2 - sqrt(2))) == -I*pi / 8
|
| 922 |
+
assert acsch(-I*2) == I*pi / 6
|
| 923 |
+
assert acsch(I*2) == -I*pi / 6
|
| 924 |
+
assert acsch(-I*sqrt(2 + 2/sqrt(5))) == I*pi / 5
|
| 925 |
+
assert acsch(I*sqrt(2 + 2/sqrt(5))) == -I*pi / 5
|
| 926 |
+
assert acsch(-I*sqrt(2)) == I*pi / 4
|
| 927 |
+
assert acsch(I*sqrt(2)) == -I*pi / 4
|
| 928 |
+
assert acsch(-I*(sqrt(5)-1)) == 3*I*pi / 10
|
| 929 |
+
assert acsch(I*(sqrt(5)-1)) == -3*I*pi / 10
|
| 930 |
+
assert acsch(-I*2 / sqrt(3)) == I*pi / 3
|
| 931 |
+
assert acsch(I*2 / sqrt(3)) == -I*pi / 3
|
| 932 |
+
assert acsch(-I*2 / sqrt(2 + sqrt(2))) == 3*I*pi / 8
|
| 933 |
+
assert acsch(I*2 / sqrt(2 + sqrt(2))) == -3*I*pi / 8
|
| 934 |
+
assert acsch(-I*sqrt(2 - 2/sqrt(5))) == 2*I*pi / 5
|
| 935 |
+
assert acsch(I*sqrt(2 - 2/sqrt(5))) == -2*I*pi / 5
|
| 936 |
+
assert acsch(-I*(sqrt(6) - sqrt(2))) == 5*I*pi / 12
|
| 937 |
+
assert acsch(I*(sqrt(6) - sqrt(2))) == -5*I*pi / 12
|
| 938 |
+
assert acsch(nan) is nan
|
| 939 |
+
|
| 940 |
+
# properties
|
| 941 |
+
# acsch(x) == asinh(1/x)
|
| 942 |
+
assert acsch(-I*sqrt(2)) == asinh(I/sqrt(2))
|
| 943 |
+
assert acsch(-I*2 / sqrt(3)) == asinh(I*sqrt(3) / 2)
|
| 944 |
+
|
| 945 |
+
# reality
|
| 946 |
+
assert acsch(S(2)).is_real is True
|
| 947 |
+
assert acsch(S(2)).is_finite is True
|
| 948 |
+
assert acsch(S(-2)).is_real is True
|
| 949 |
+
assert acsch(S(oo)).is_extended_real is True
|
| 950 |
+
assert acsch(-S(oo)).is_real is True
|
| 951 |
+
assert (acsch(2) - oo) == -oo
|
| 952 |
+
assert acsch(symbols('y', extended_real=True)).is_extended_real is True
|
| 953 |
+
|
| 954 |
+
# acsch(x) == -I*asin(I/x)
|
| 955 |
+
assert acsch(-I*sqrt(2)) == -I*asin(-1/sqrt(2))
|
| 956 |
+
assert acsch(-I*2 / sqrt(3)) == -I*asin(-sqrt(3)/2)
|
| 957 |
+
|
| 958 |
+
# csch(acsch(x)) / x == 1
|
| 959 |
+
assert expand_mul(csch(acsch(-I*(sqrt(6) + sqrt(2)))) / (-I*(sqrt(6) + sqrt(2)))) == 1
|
| 960 |
+
assert expand_mul(csch(acsch(I*(1 + sqrt(5)))) / (I*(1 + sqrt(5)))) == 1
|
| 961 |
+
assert (csch(acsch(I*sqrt(2 - 2/sqrt(5)))) / (I*sqrt(2 - 2/sqrt(5)))).simplify() == 1
|
| 962 |
+
assert (csch(acsch(-I*sqrt(2 - 2/sqrt(5)))) / (-I*sqrt(2 - 2/sqrt(5)))).simplify() == 1
|
| 963 |
+
|
| 964 |
+
# numerical evaluation
|
| 965 |
+
assert str(acsch(5*I+1).n(6)) == '0.0391819 - 0.193363*I'
|
| 966 |
+
assert str(acsch(-5*I+1).n(6)) == '0.0391819 + 0.193363*I'
|
| 967 |
+
|
| 968 |
+
|
| 969 |
+
def test_acsch_infinities():
|
| 970 |
+
assert acsch(oo) == 0
|
| 971 |
+
assert acsch(-oo) == 0
|
| 972 |
+
assert acsch(zoo) == 0
|
| 973 |
+
|
| 974 |
+
|
| 975 |
+
def test_acsch_leading_term():
|
| 976 |
+
x = Symbol('x')
|
| 977 |
+
assert acsch(1/x).as_leading_term(x) == x
|
| 978 |
+
# Tests concerning branch points
|
| 979 |
+
assert acsch(x + I).as_leading_term(x) == -I*pi/2
|
| 980 |
+
assert acsch(x - I).as_leading_term(x) == I*pi/2
|
| 981 |
+
# Tests concerning points lying on branch cuts
|
| 982 |
+
assert acsch(x).as_leading_term(x, cdir=1) == -log(x) + log(2)
|
| 983 |
+
assert acsch(x).as_leading_term(x, cdir=-1) == log(x) - log(2) - I*pi
|
| 984 |
+
assert acsch(x + I/2).as_leading_term(x, cdir=1) == -I*pi - acsch(I/2)
|
| 985 |
+
assert acsch(x + I/2).as_leading_term(x, cdir=-1) == acsch(I/2)
|
| 986 |
+
assert acsch(x - I/2).as_leading_term(x, cdir=1) == -acsch(I/2)
|
| 987 |
+
assert acsch(x - I/2).as_leading_term(x, cdir=-1) == acsch(I/2) + I*pi
|
| 988 |
+
# Tests concerning re(ndir) == 0
|
| 989 |
+
assert acsch(I/2 + I*x - x**2).as_leading_term(x, cdir=1) == log(2 - sqrt(3)) - I*pi/2
|
| 990 |
+
assert acsch(I/2 + I*x - x**2).as_leading_term(x, cdir=-1) == log(2 - sqrt(3)) - I*pi/2
|
| 991 |
+
|
| 992 |
+
|
| 993 |
+
def test_acsch_series():
|
| 994 |
+
x = Symbol('x')
|
| 995 |
+
assert acsch(x).series(x, 0, 9) == log(2) - log(x) + x**2/4 - 3*x**4/32 \
|
| 996 |
+
+ 5*x**6/96 - 35*x**8/1024 + O(x**9)
|
| 997 |
+
t4 = acsch(x).taylor_term(4, x)
|
| 998 |
+
assert t4 == -3*x**4/32
|
| 999 |
+
assert acsch(x).taylor_term(6, x, t4, 0) == 5*x**6/96
|
| 1000 |
+
|
| 1001 |
+
|
| 1002 |
+
def test_acsch_nseries():
|
| 1003 |
+
x = Symbol('x')
|
| 1004 |
+
# Tests concerning branch points
|
| 1005 |
+
assert acsch(x + I)._eval_nseries(x, 4, None) == -I*pi/2 + I*sqrt(x) + \
|
| 1006 |
+
sqrt(x) + 5*I*x**(S(3)/2)/12 - 5*x**(S(3)/2)/12 - 43*I*x**(S(5)/2)/160 - \
|
| 1007 |
+
43*x**(S(5)/2)/160 - 177*I*x**(S(7)/2)/896 + 177*x**(S(7)/2)/896 + O(x**4)
|
| 1008 |
+
assert acsch(x - I)._eval_nseries(x, 4, None) == I*pi/2 - I*sqrt(x) + \
|
| 1009 |
+
sqrt(x) - 5*I*x**(S(3)/2)/12 - 5*x**(S(3)/2)/12 + 43*I*x**(S(5)/2)/160 - \
|
| 1010 |
+
43*x**(S(5)/2)/160 + 177*I*x**(S(7)/2)/896 + 177*x**(S(7)/2)/896 + O(x**4)
|
| 1011 |
+
# Tests concerning points lying on branch cuts
|
| 1012 |
+
assert acsch(x + I/2)._eval_nseries(x, 4, None, cdir=1) == -acsch(I/2) - \
|
| 1013 |
+
I*pi + 4*sqrt(3)*I*x/3 - 8*sqrt(3)*x**2/9 - 16*sqrt(3)*I*x**3/9 + O(x**4)
|
| 1014 |
+
assert acsch(x + I/2)._eval_nseries(x, 4, None, cdir=-1) == acsch(I/2) - \
|
| 1015 |
+
4*sqrt(3)*I*x/3 + 8*sqrt(3)*x**2/9 + 16*sqrt(3)*I*x**3/9 + O(x**4)
|
| 1016 |
+
assert acsch(x - I/2)._eval_nseries(x, 4, None, cdir=1) == -acsch(I/2) - \
|
| 1017 |
+
4*sqrt(3)*I*x/3 - 8*sqrt(3)*x**2/9 + 16*sqrt(3)*I*x**3/9 + O(x**4)
|
| 1018 |
+
assert acsch(x - I/2)._eval_nseries(x, 4, None, cdir=-1) == I*pi + \
|
| 1019 |
+
acsch(I/2) + 4*sqrt(3)*I*x/3 + 8*sqrt(3)*x**2/9 - 16*sqrt(3)*I*x**3/9 + O(x**4)
|
| 1020 |
+
# TODO: Tests concerning re(ndir) == 0
|
| 1021 |
+
assert acsch(I/2 + I*x - x**2)._eval_nseries(x, 4, None) == -I*pi/2 + \
|
| 1022 |
+
log(2 - sqrt(3)) + 4*sqrt(3)*x/3 + x**2*(-8*sqrt(3)/9 + 4*sqrt(3)*I/3) + \
|
| 1023 |
+
x**3*(16*sqrt(3)/9 - 16*sqrt(3)*I/9) + O(x**4)
|
| 1024 |
+
|
| 1025 |
+
|
| 1026 |
+
def test_acsch_rewrite():
|
| 1027 |
+
x = Symbol('x')
|
| 1028 |
+
assert acsch(x).rewrite(log) == log(1/x + sqrt(1/x**2 + 1))
|
| 1029 |
+
assert acsch(x).rewrite(asinh) == asinh(1/x)
|
| 1030 |
+
assert acsch(x).rewrite(atanh) == (sqrt(-x**2)*(-sqrt(-(x**2 + 1)**2)
|
| 1031 |
+
*atanh(sqrt(x**2 + 1))/(x**2 + 1)
|
| 1032 |
+
+ pi/2)/x)
|
| 1033 |
+
|
| 1034 |
+
|
| 1035 |
+
def test_acsch_fdiff():
|
| 1036 |
+
x = Symbol('x')
|
| 1037 |
+
raises(ArgumentIndexError, lambda: acsch(x).fdiff(2))
|
| 1038 |
+
|
| 1039 |
+
|
| 1040 |
+
def test_atanh():
|
| 1041 |
+
x = Symbol('x')
|
| 1042 |
+
|
| 1043 |
+
# at specific points
|
| 1044 |
+
assert atanh(0) == 0
|
| 1045 |
+
assert atanh(I) == I*pi/4
|
| 1046 |
+
assert atanh(-I) == -I*pi/4
|
| 1047 |
+
assert atanh(1) is oo
|
| 1048 |
+
assert atanh(-1) is -oo
|
| 1049 |
+
assert atanh(nan) is nan
|
| 1050 |
+
|
| 1051 |
+
# at infinites
|
| 1052 |
+
assert atanh(oo) == -I*pi/2
|
| 1053 |
+
assert atanh(-oo) == I*pi/2
|
| 1054 |
+
|
| 1055 |
+
assert atanh(I*oo) == I*pi/2
|
| 1056 |
+
assert atanh(-I*oo) == -I*pi/2
|
| 1057 |
+
|
| 1058 |
+
assert atanh(zoo) == I*AccumBounds(-pi/2, pi/2)
|
| 1059 |
+
|
| 1060 |
+
# properties
|
| 1061 |
+
assert atanh(-x) == -atanh(x)
|
| 1062 |
+
|
| 1063 |
+
# reality
|
| 1064 |
+
assert atanh(S(2)).is_real is False
|
| 1065 |
+
assert atanh(S(-1)/5).is_real is True
|
| 1066 |
+
assert atanh(symbols('y', extended_real=True)).is_real is None
|
| 1067 |
+
assert atanh(S(1)).is_real is False
|
| 1068 |
+
assert atanh(S(1)).is_extended_real is True
|
| 1069 |
+
assert atanh(S(-1)).is_real is False
|
| 1070 |
+
|
| 1071 |
+
# special values
|
| 1072 |
+
assert atanh(I/sqrt(3)) == I*pi/6
|
| 1073 |
+
assert atanh(-I/sqrt(3)) == -I*pi/6
|
| 1074 |
+
assert atanh(I*sqrt(3)) == I*pi/3
|
| 1075 |
+
assert atanh(-I*sqrt(3)) == -I*pi/3
|
| 1076 |
+
assert atanh(I*(1 + sqrt(2))) == pi*I*Rational(3, 8)
|
| 1077 |
+
assert atanh(I*(sqrt(2) - 1)) == pi*I/8
|
| 1078 |
+
assert atanh(I*(1 - sqrt(2))) == -pi*I/8
|
| 1079 |
+
assert atanh(-I*(1 + sqrt(2))) == pi*I*Rational(-3, 8)
|
| 1080 |
+
assert atanh(I*sqrt(5 + 2*sqrt(5))) == I*pi*Rational(2, 5)
|
| 1081 |
+
assert atanh(-I*sqrt(5 + 2*sqrt(5))) == I*pi*Rational(-2, 5)
|
| 1082 |
+
assert atanh(I*(2 - sqrt(3))) == pi*I/12
|
| 1083 |
+
assert atanh(I*(sqrt(3) - 2)) == -pi*I/12
|
| 1084 |
+
assert atanh(oo) == -I*pi/2
|
| 1085 |
+
|
| 1086 |
+
# Symmetry
|
| 1087 |
+
assert atanh(Rational(-1, 2)) == -atanh(S.Half)
|
| 1088 |
+
|
| 1089 |
+
# inverse composition
|
| 1090 |
+
assert unchanged(atanh, tanh(Symbol('v1')))
|
| 1091 |
+
|
| 1092 |
+
assert atanh(tanh(-5, evaluate=False)) == -5
|
| 1093 |
+
assert atanh(tanh(0, evaluate=False)) == 0
|
| 1094 |
+
assert atanh(tanh(7, evaluate=False)) == 7
|
| 1095 |
+
assert atanh(tanh(I, evaluate=False)) == I
|
| 1096 |
+
assert atanh(tanh(-I, evaluate=False)) == -I
|
| 1097 |
+
assert atanh(tanh(-11*I, evaluate=False)) == -11*I + 4*I*pi
|
| 1098 |
+
assert atanh(tanh(3 + I)) == 3 + I
|
| 1099 |
+
assert atanh(tanh(4 + 5*I)) == 4 - 2*I*pi + 5*I
|
| 1100 |
+
assert atanh(tanh(pi/2)) == pi/2
|
| 1101 |
+
assert atanh(tanh(pi)) == pi
|
| 1102 |
+
assert atanh(tanh(-3 + 7*I)) == -3 - 2*I*pi + 7*I
|
| 1103 |
+
assert atanh(tanh(9 - I*2/3)) == 9 - I*2/3
|
| 1104 |
+
assert atanh(tanh(-32 - 123*I)) == -32 - 123*I + 39*I*pi
|
| 1105 |
+
|
| 1106 |
+
|
| 1107 |
+
def test_atanh_rewrite():
|
| 1108 |
+
x = Symbol('x')
|
| 1109 |
+
assert atanh(x).rewrite(log) == (log(1 + x) - log(1 - x)) / 2
|
| 1110 |
+
assert atanh(x).rewrite(asinh) == \
|
| 1111 |
+
pi*x/(2*sqrt(-x**2)) - sqrt(-x)*sqrt(1 - x**2)*sqrt(1/(x**2 - 1))*asinh(sqrt(1/(x**2 - 1)))/sqrt(x)
|
| 1112 |
+
|
| 1113 |
+
|
| 1114 |
+
def test_atanh_leading_term():
|
| 1115 |
+
x = Symbol('x')
|
| 1116 |
+
assert atanh(x).as_leading_term(x) == x
|
| 1117 |
+
# Tests concerning branch points
|
| 1118 |
+
assert atanh(x + 1).as_leading_term(x, cdir=1) == -log(x)/2 + log(2)/2 - I*pi/2
|
| 1119 |
+
assert atanh(x + 1).as_leading_term(x, cdir=-1) == -log(x)/2 + log(2)/2 + I*pi/2
|
| 1120 |
+
assert atanh(x - 1).as_leading_term(x, cdir=1) == log(x)/2 - log(2)/2
|
| 1121 |
+
assert atanh(x - 1).as_leading_term(x, cdir=-1) == log(x)/2 - log(2)/2
|
| 1122 |
+
assert atanh(1/x).as_leading_term(x, cdir=1) == -I*pi/2
|
| 1123 |
+
assert atanh(1/x).as_leading_term(x, cdir=-1) == I*pi/2
|
| 1124 |
+
# Tests concerning points lying on branch cuts
|
| 1125 |
+
assert atanh(I*x + 2).as_leading_term(x, cdir=1) == atanh(2) + I*pi
|
| 1126 |
+
assert atanh(-I*x + 2).as_leading_term(x, cdir=1) == atanh(2)
|
| 1127 |
+
assert atanh(I*x - 2).as_leading_term(x, cdir=1) == -atanh(2)
|
| 1128 |
+
assert atanh(-I*x - 2).as_leading_term(x, cdir=1) == -I*pi - atanh(2)
|
| 1129 |
+
# Tests concerning im(ndir) == 0
|
| 1130 |
+
assert atanh(-I*x**2 + x - 2).as_leading_term(x, cdir=1) == -log(3)/2 - I*pi/2
|
| 1131 |
+
assert atanh(-I*x**2 + x - 2).as_leading_term(x, cdir=-1) == -log(3)/2 - I*pi/2
|
| 1132 |
+
|
| 1133 |
+
|
| 1134 |
+
def test_atanh_series():
|
| 1135 |
+
x = Symbol('x')
|
| 1136 |
+
assert atanh(x).series(x, 0, 10) == \
|
| 1137 |
+
x + x**3/3 + x**5/5 + x**7/7 + x**9/9 + O(x**10)
|
| 1138 |
+
|
| 1139 |
+
|
| 1140 |
+
def test_atanh_nseries():
|
| 1141 |
+
x = Symbol('x')
|
| 1142 |
+
# Tests concerning branch points
|
| 1143 |
+
assert atanh(x + 1)._eval_nseries(x, 4, None, cdir=1) == -I*pi/2 + log(2)/2 - \
|
| 1144 |
+
log(x)/2 + x/4 - x**2/16 + x**3/48 + O(x**4)
|
| 1145 |
+
assert atanh(x + 1)._eval_nseries(x, 4, None, cdir=-1) == I*pi/2 + log(2)/2 - \
|
| 1146 |
+
log(x)/2 + x/4 - x**2/16 + x**3/48 + O(x**4)
|
| 1147 |
+
assert atanh(x - 1)._eval_nseries(x, 4, None, cdir=1) == -log(2)/2 + log(x)/2 + \
|
| 1148 |
+
x/4 + x**2/16 + x**3/48 + O(x**4)
|
| 1149 |
+
assert atanh(x - 1)._eval_nseries(x, 4, None, cdir=-1) == -log(2)/2 + log(x)/2 + \
|
| 1150 |
+
x/4 + x**2/16 + x**3/48 + O(x**4)
|
| 1151 |
+
# Tests concerning points lying on branch cuts
|
| 1152 |
+
assert atanh(I*x + 2)._eval_nseries(x, 4, None, cdir=1) == I*pi + atanh(2) - \
|
| 1153 |
+
I*x/3 - 2*x**2/9 + 13*I*x**3/81 + O(x**4)
|
| 1154 |
+
assert atanh(I*x + 2)._eval_nseries(x, 4, None, cdir=-1) == atanh(2) - I*x/3 - \
|
| 1155 |
+
2*x**2/9 + 13*I*x**3/81 + O(x**4)
|
| 1156 |
+
assert atanh(I*x - 2)._eval_nseries(x, 4, None, cdir=1) == -atanh(2) - I*x/3 + \
|
| 1157 |
+
2*x**2/9 + 13*I*x**3/81 + O(x**4)
|
| 1158 |
+
assert atanh(I*x - 2)._eval_nseries(x, 4, None, cdir=-1) == -atanh(2) - I*pi - \
|
| 1159 |
+
I*x/3 + 2*x**2/9 + 13*I*x**3/81 + O(x**4)
|
| 1160 |
+
# Tests concerning im(ndir) == 0
|
| 1161 |
+
assert atanh(-I*x**2 + x - 2)._eval_nseries(x, 4, None) == -I*pi/2 - log(3)/2 - x/3 + \
|
| 1162 |
+
x**2*(-S(1)/4 + I/2) + x**2*(S(1)/36 - I/6) + x**3*(-S(1)/6 + I/2) + x**3*(S(1)/162 - I/18) + O(x**4)
|
| 1163 |
+
|
| 1164 |
+
|
| 1165 |
+
def test_atanh_fdiff():
|
| 1166 |
+
x = Symbol('x')
|
| 1167 |
+
raises(ArgumentIndexError, lambda: atanh(x).fdiff(2))
|
| 1168 |
+
|
| 1169 |
+
|
| 1170 |
+
def test_acoth():
|
| 1171 |
+
x = Symbol('x')
|
| 1172 |
+
|
| 1173 |
+
#at specific points
|
| 1174 |
+
assert acoth(0) == I*pi/2
|
| 1175 |
+
assert acoth(I) == -I*pi/4
|
| 1176 |
+
assert acoth(-I) == I*pi/4
|
| 1177 |
+
assert acoth(1) is oo
|
| 1178 |
+
assert acoth(-1) is -oo
|
| 1179 |
+
assert acoth(nan) is nan
|
| 1180 |
+
|
| 1181 |
+
# at infinites
|
| 1182 |
+
assert acoth(oo) == 0
|
| 1183 |
+
assert acoth(-oo) == 0
|
| 1184 |
+
assert acoth(I*oo) == 0
|
| 1185 |
+
assert acoth(-I*oo) == 0
|
| 1186 |
+
assert acoth(zoo) == 0
|
| 1187 |
+
|
| 1188 |
+
#properties
|
| 1189 |
+
assert acoth(-x) == -acoth(x)
|
| 1190 |
+
|
| 1191 |
+
assert acoth(I/sqrt(3)) == -I*pi/3
|
| 1192 |
+
assert acoth(-I/sqrt(3)) == I*pi/3
|
| 1193 |
+
assert acoth(I*sqrt(3)) == -I*pi/6
|
| 1194 |
+
assert acoth(-I*sqrt(3)) == I*pi/6
|
| 1195 |
+
assert acoth(I*(1 + sqrt(2))) == -pi*I/8
|
| 1196 |
+
assert acoth(-I*(sqrt(2) + 1)) == pi*I/8
|
| 1197 |
+
assert acoth(I*(1 - sqrt(2))) == pi*I*Rational(3, 8)
|
| 1198 |
+
assert acoth(I*(sqrt(2) - 1)) == pi*I*Rational(-3, 8)
|
| 1199 |
+
assert acoth(I*sqrt(5 + 2*sqrt(5))) == -I*pi/10
|
| 1200 |
+
assert acoth(-I*sqrt(5 + 2*sqrt(5))) == I*pi/10
|
| 1201 |
+
assert acoth(I*(2 + sqrt(3))) == -pi*I/12
|
| 1202 |
+
assert acoth(-I*(2 + sqrt(3))) == pi*I/12
|
| 1203 |
+
assert acoth(I*(2 - sqrt(3))) == pi*I*Rational(-5, 12)
|
| 1204 |
+
assert acoth(I*(sqrt(3) - 2)) == pi*I*Rational(5, 12)
|
| 1205 |
+
|
| 1206 |
+
# reality
|
| 1207 |
+
assert acoth(S(2)).is_real is True
|
| 1208 |
+
assert acoth(S(2)).is_finite is True
|
| 1209 |
+
assert acoth(S(2)).is_extended_real is True
|
| 1210 |
+
assert acoth(S(-2)).is_real is True
|
| 1211 |
+
assert acoth(S(1)).is_real is False
|
| 1212 |
+
assert acoth(S(1)).is_extended_real is True
|
| 1213 |
+
assert acoth(S(-1)).is_real is False
|
| 1214 |
+
assert acoth(symbols('y', real=True)).is_real is None
|
| 1215 |
+
|
| 1216 |
+
# Symmetry
|
| 1217 |
+
assert acoth(Rational(-1, 2)) == -acoth(S.Half)
|
| 1218 |
+
|
| 1219 |
+
|
| 1220 |
+
def test_acoth_rewrite():
|
| 1221 |
+
x = Symbol('x')
|
| 1222 |
+
assert acoth(x).rewrite(log) == (log(1 + 1/x) - log(1 - 1/x)) / 2
|
| 1223 |
+
assert acoth(x).rewrite(atanh) == atanh(1/x)
|
| 1224 |
+
assert acoth(x).rewrite(asinh) == \
|
| 1225 |
+
x*sqrt(x**(-2))*asinh(sqrt(1/(x**2 - 1))) + I*pi*(sqrt((x - 1)/x)*sqrt(x/(x - 1)) - sqrt(x/(x + 1))*sqrt(1 + 1/x))/2
|
| 1226 |
+
|
| 1227 |
+
|
| 1228 |
+
def test_acoth_leading_term():
|
| 1229 |
+
x = Symbol('x')
|
| 1230 |
+
# Tests concerning branch points
|
| 1231 |
+
assert acoth(x + 1).as_leading_term(x, cdir=1) == -log(x)/2 + log(2)/2
|
| 1232 |
+
assert acoth(x + 1).as_leading_term(x, cdir=-1) == -log(x)/2 + log(2)/2
|
| 1233 |
+
assert acoth(x - 1).as_leading_term(x, cdir=1) == log(x)/2 - log(2)/2 + I*pi/2
|
| 1234 |
+
assert acoth(x - 1).as_leading_term(x, cdir=-1) == log(x)/2 - log(2)/2 - I*pi/2
|
| 1235 |
+
# Tests concerning points lying on branch cuts
|
| 1236 |
+
assert acoth(x).as_leading_term(x, cdir=-1) == I*pi/2
|
| 1237 |
+
assert acoth(x).as_leading_term(x, cdir=1) == -I*pi/2
|
| 1238 |
+
assert acoth(I*x + 1/2).as_leading_term(x, cdir=1) == acoth(1/2)
|
| 1239 |
+
assert acoth(-I*x + 1/2).as_leading_term(x, cdir=1) == acoth(1/2) + I*pi
|
| 1240 |
+
assert acoth(I*x - 1/2).as_leading_term(x, cdir=1) == -I*pi - acoth(1/2)
|
| 1241 |
+
assert acoth(-I*x - 1/2).as_leading_term(x, cdir=1) == -acoth(1/2)
|
| 1242 |
+
# Tests concerning im(ndir) == 0
|
| 1243 |
+
assert acoth(-I*x**2 - x - S(1)/2).as_leading_term(x, cdir=1) == -log(3)/2 + I*pi/2
|
| 1244 |
+
assert acoth(-I*x**2 - x - S(1)/2).as_leading_term(x, cdir=-1) == -log(3)/2 + I*pi/2
|
| 1245 |
+
|
| 1246 |
+
|
| 1247 |
+
def test_acoth_series():
|
| 1248 |
+
x = Symbol('x')
|
| 1249 |
+
assert acoth(x).series(x, 0, 10) == \
|
| 1250 |
+
-I*pi/2 + x + x**3/3 + x**5/5 + x**7/7 + x**9/9 + O(x**10)
|
| 1251 |
+
|
| 1252 |
+
|
| 1253 |
+
def test_acoth_nseries():
|
| 1254 |
+
x = Symbol('x')
|
| 1255 |
+
# Tests concerning branch points
|
| 1256 |
+
assert acoth(x + 1)._eval_nseries(x, 4, None) == log(2)/2 - log(x)/2 + x/4 - \
|
| 1257 |
+
x**2/16 + x**3/48 + O(x**4)
|
| 1258 |
+
assert acoth(x - 1)._eval_nseries(x, 4, None, cdir=1) == I*pi/2 - log(2)/2 + \
|
| 1259 |
+
log(x)/2 + x/4 + x**2/16 + x**3/48 + O(x**4)
|
| 1260 |
+
assert acoth(x - 1)._eval_nseries(x, 4, None, cdir=-1) == -I*pi/2 - log(2)/2 + \
|
| 1261 |
+
log(x)/2 + x/4 + x**2/16 + x**3/48 + O(x**4)
|
| 1262 |
+
# Tests concerning points lying on branch cuts
|
| 1263 |
+
assert acoth(I*x + S(1)/2)._eval_nseries(x, 4, None, cdir=1) == acoth(S(1)/2) + \
|
| 1264 |
+
4*I*x/3 - 8*x**2/9 - 112*I*x**3/81 + O(x**4)
|
| 1265 |
+
assert acoth(I*x + S(1)/2)._eval_nseries(x, 4, None, cdir=-1) == I*pi + \
|
| 1266 |
+
acoth(S(1)/2) + 4*I*x/3 - 8*x**2/9 - 112*I*x**3/81 + O(x**4)
|
| 1267 |
+
assert acoth(I*x - S(1)/2)._eval_nseries(x, 4, None, cdir=1) == -acoth(S(1)/2) - \
|
| 1268 |
+
I*pi + 4*I*x/3 + 8*x**2/9 - 112*I*x**3/81 + O(x**4)
|
| 1269 |
+
assert acoth(I*x - S(1)/2)._eval_nseries(x, 4, None, cdir=-1) == -acoth(S(1)/2) + \
|
| 1270 |
+
4*I*x/3 + 8*x**2/9 - 112*I*x**3/81 + O(x**4)
|
| 1271 |
+
# Tests concerning im(ndir) == 0
|
| 1272 |
+
assert acoth(-I*x**2 - x - S(1)/2)._eval_nseries(x, 4, None) == I*pi/2 - log(3)/2 - \
|
| 1273 |
+
4*x/3 + x**2*(-S(8)/9 + 2*I/3) - 2*I*x**2 + x**3*(S(104)/81 - 16*I/9) - 8*x**3/3 + O(x**4)
|
| 1274 |
+
|
| 1275 |
+
|
| 1276 |
+
def test_acoth_fdiff():
|
| 1277 |
+
x = Symbol('x')
|
| 1278 |
+
raises(ArgumentIndexError, lambda: acoth(x).fdiff(2))
|
| 1279 |
+
|
| 1280 |
+
|
| 1281 |
+
def test_inverses():
|
| 1282 |
+
x = Symbol('x')
|
| 1283 |
+
assert sinh(x).inverse() == asinh
|
| 1284 |
+
raises(AttributeError, lambda: cosh(x).inverse())
|
| 1285 |
+
assert tanh(x).inverse() == atanh
|
| 1286 |
+
assert coth(x).inverse() == acoth
|
| 1287 |
+
assert asinh(x).inverse() == sinh
|
| 1288 |
+
assert acosh(x).inverse() == cosh
|
| 1289 |
+
assert atanh(x).inverse() == tanh
|
| 1290 |
+
assert acoth(x).inverse() == coth
|
| 1291 |
+
assert asech(x).inverse() == sech
|
| 1292 |
+
assert acsch(x).inverse() == csch
|
| 1293 |
+
|
| 1294 |
+
|
| 1295 |
+
def test_leading_term():
|
| 1296 |
+
x = Symbol('x')
|
| 1297 |
+
assert cosh(x).as_leading_term(x) == 1
|
| 1298 |
+
assert coth(x).as_leading_term(x) == 1/x
|
| 1299 |
+
for func in [sinh, tanh]:
|
| 1300 |
+
assert func(x).as_leading_term(x) == x
|
| 1301 |
+
for func in [sinh, cosh, tanh, coth]:
|
| 1302 |
+
for ar in (1/x, S.Half):
|
| 1303 |
+
eq = func(ar)
|
| 1304 |
+
assert eq.as_leading_term(x) == eq
|
| 1305 |
+
for func in [csch, sech]:
|
| 1306 |
+
eq = func(S.Half)
|
| 1307 |
+
assert eq.as_leading_term(x) == eq
|
| 1308 |
+
|
| 1309 |
+
|
| 1310 |
+
def test_complex():
|
| 1311 |
+
a, b = symbols('a,b', real=True)
|
| 1312 |
+
z = a + b*I
|
| 1313 |
+
for func in [sinh, cosh, tanh, coth, sech, csch]:
|
| 1314 |
+
assert func(z).conjugate() == func(a - b*I)
|
| 1315 |
+
for deep in [True, False]:
|
| 1316 |
+
assert sinh(z).expand(
|
| 1317 |
+
complex=True, deep=deep) == sinh(a)*cos(b) + I*cosh(a)*sin(b)
|
| 1318 |
+
assert cosh(z).expand(
|
| 1319 |
+
complex=True, deep=deep) == cosh(a)*cos(b) + I*sinh(a)*sin(b)
|
| 1320 |
+
assert tanh(z).expand(complex=True, deep=deep) == sinh(a)*cosh(
|
| 1321 |
+
a)/(cos(b)**2 + sinh(a)**2) + I*sin(b)*cos(b)/(cos(b)**2 + sinh(a)**2)
|
| 1322 |
+
assert coth(z).expand(complex=True, deep=deep) == sinh(a)*cosh(
|
| 1323 |
+
a)/(sin(b)**2 + sinh(a)**2) - I*sin(b)*cos(b)/(sin(b)**2 + sinh(a)**2)
|
| 1324 |
+
assert csch(z).expand(complex=True, deep=deep) == cos(b) * sinh(a) / (sin(b)**2\
|
| 1325 |
+
*cosh(a)**2 + cos(b)**2 * sinh(a)**2) - I*sin(b) * cosh(a) / (sin(b)**2\
|
| 1326 |
+
*cosh(a)**2 + cos(b)**2 * sinh(a)**2)
|
| 1327 |
+
assert sech(z).expand(complex=True, deep=deep) == cos(b) * cosh(a) / (sin(b)**2\
|
| 1328 |
+
*sinh(a)**2 + cos(b)**2 * cosh(a)**2) - I*sin(b) * sinh(a) / (sin(b)**2\
|
| 1329 |
+
*sinh(a)**2 + cos(b)**2 * cosh(a)**2)
|
| 1330 |
+
|
| 1331 |
+
|
| 1332 |
+
def test_complex_2899():
|
| 1333 |
+
a, b = symbols('a,b', real=True)
|
| 1334 |
+
for deep in [True, False]:
|
| 1335 |
+
for func in [sinh, cosh, tanh, coth]:
|
| 1336 |
+
assert func(a).expand(complex=True, deep=deep) == func(a)
|
| 1337 |
+
|
| 1338 |
+
|
| 1339 |
+
def test_simplifications():
|
| 1340 |
+
x = Symbol('x')
|
| 1341 |
+
assert sinh(asinh(x)) == x
|
| 1342 |
+
assert sinh(acosh(x)) == sqrt(x - 1) * sqrt(x + 1)
|
| 1343 |
+
assert sinh(atanh(x)) == x/sqrt(1 - x**2)
|
| 1344 |
+
assert sinh(acoth(x)) == 1/(sqrt(x - 1) * sqrt(x + 1))
|
| 1345 |
+
|
| 1346 |
+
assert cosh(asinh(x)) == sqrt(1 + x**2)
|
| 1347 |
+
assert cosh(acosh(x)) == x
|
| 1348 |
+
assert cosh(atanh(x)) == 1/sqrt(1 - x**2)
|
| 1349 |
+
assert cosh(acoth(x)) == x/(sqrt(x - 1) * sqrt(x + 1))
|
| 1350 |
+
|
| 1351 |
+
assert tanh(asinh(x)) == x/sqrt(1 + x**2)
|
| 1352 |
+
assert tanh(acosh(x)) == sqrt(x - 1) * sqrt(x + 1) / x
|
| 1353 |
+
assert tanh(atanh(x)) == x
|
| 1354 |
+
assert tanh(acoth(x)) == 1/x
|
| 1355 |
+
|
| 1356 |
+
assert coth(asinh(x)) == sqrt(1 + x**2)/x
|
| 1357 |
+
assert coth(acosh(x)) == x/(sqrt(x - 1) * sqrt(x + 1))
|
| 1358 |
+
assert coth(atanh(x)) == 1/x
|
| 1359 |
+
assert coth(acoth(x)) == x
|
| 1360 |
+
|
| 1361 |
+
assert csch(asinh(x)) == 1/x
|
| 1362 |
+
assert csch(acosh(x)) == 1/(sqrt(x - 1) * sqrt(x + 1))
|
| 1363 |
+
assert csch(atanh(x)) == sqrt(1 - x**2)/x
|
| 1364 |
+
assert csch(acoth(x)) == sqrt(x - 1) * sqrt(x + 1)
|
| 1365 |
+
|
| 1366 |
+
assert sech(asinh(x)) == 1/sqrt(1 + x**2)
|
| 1367 |
+
assert sech(acosh(x)) == 1/x
|
| 1368 |
+
assert sech(atanh(x)) == sqrt(1 - x**2)
|
| 1369 |
+
assert sech(acoth(x)) == sqrt(x - 1) * sqrt(x + 1)/x
|
| 1370 |
+
|
| 1371 |
+
|
| 1372 |
+
def test_issue_4136():
|
| 1373 |
+
assert cosh(asinh(Integer(3)/2)) == sqrt(Integer(13)/4)
|
| 1374 |
+
|
| 1375 |
+
|
| 1376 |
+
def test_sinh_rewrite():
|
| 1377 |
+
x = Symbol('x')
|
| 1378 |
+
assert sinh(x).rewrite(exp) == (exp(x) - exp(-x))/2 \
|
| 1379 |
+
== sinh(x).rewrite('tractable')
|
| 1380 |
+
assert sinh(x).rewrite(cosh) == -I*cosh(x + I*pi/2)
|
| 1381 |
+
tanh_half = tanh(S.Half*x)
|
| 1382 |
+
assert sinh(x).rewrite(tanh) == 2*tanh_half/(1 - tanh_half**2)
|
| 1383 |
+
coth_half = coth(S.Half*x)
|
| 1384 |
+
assert sinh(x).rewrite(coth) == 2*coth_half/(coth_half**2 - 1)
|
| 1385 |
+
|
| 1386 |
+
|
| 1387 |
+
def test_cosh_rewrite():
|
| 1388 |
+
x = Symbol('x')
|
| 1389 |
+
assert cosh(x).rewrite(exp) == (exp(x) + exp(-x))/2 \
|
| 1390 |
+
== cosh(x).rewrite('tractable')
|
| 1391 |
+
assert cosh(x).rewrite(sinh) == -I*sinh(x + I*pi/2, evaluate=False)
|
| 1392 |
+
tanh_half = tanh(S.Half*x)**2
|
| 1393 |
+
assert cosh(x).rewrite(tanh) == (1 + tanh_half)/(1 - tanh_half)
|
| 1394 |
+
coth_half = coth(S.Half*x)**2
|
| 1395 |
+
assert cosh(x).rewrite(coth) == (coth_half + 1)/(coth_half - 1)
|
| 1396 |
+
|
| 1397 |
+
|
| 1398 |
+
def test_tanh_rewrite():
|
| 1399 |
+
x = Symbol('x')
|
| 1400 |
+
assert tanh(x).rewrite(exp) == (exp(x) - exp(-x))/(exp(x) + exp(-x)) \
|
| 1401 |
+
== tanh(x).rewrite('tractable')
|
| 1402 |
+
assert tanh(x).rewrite(sinh) == I*sinh(x)/sinh(I*pi/2 - x, evaluate=False)
|
| 1403 |
+
assert tanh(x).rewrite(cosh) == I*cosh(I*pi/2 - x, evaluate=False)/cosh(x)
|
| 1404 |
+
assert tanh(x).rewrite(coth) == 1/coth(x)
|
| 1405 |
+
|
| 1406 |
+
|
| 1407 |
+
def test_coth_rewrite():
|
| 1408 |
+
x = Symbol('x')
|
| 1409 |
+
assert coth(x).rewrite(exp) == (exp(x) + exp(-x))/(exp(x) - exp(-x)) \
|
| 1410 |
+
== coth(x).rewrite('tractable')
|
| 1411 |
+
assert coth(x).rewrite(sinh) == -I*sinh(I*pi/2 - x, evaluate=False)/sinh(x)
|
| 1412 |
+
assert coth(x).rewrite(cosh) == -I*cosh(x)/cosh(I*pi/2 - x, evaluate=False)
|
| 1413 |
+
assert coth(x).rewrite(tanh) == 1/tanh(x)
|
| 1414 |
+
|
| 1415 |
+
|
| 1416 |
+
def test_csch_rewrite():
|
| 1417 |
+
x = Symbol('x')
|
| 1418 |
+
assert csch(x).rewrite(exp) == 1 / (exp(x)/2 - exp(-x)/2) \
|
| 1419 |
+
== csch(x).rewrite('tractable')
|
| 1420 |
+
assert csch(x).rewrite(cosh) == I/cosh(x + I*pi/2, evaluate=False)
|
| 1421 |
+
tanh_half = tanh(S.Half*x)
|
| 1422 |
+
assert csch(x).rewrite(tanh) == (1 - tanh_half**2)/(2*tanh_half)
|
| 1423 |
+
coth_half = coth(S.Half*x)
|
| 1424 |
+
assert csch(x).rewrite(coth) == (coth_half**2 - 1)/(2*coth_half)
|
| 1425 |
+
|
| 1426 |
+
|
| 1427 |
+
def test_sech_rewrite():
|
| 1428 |
+
x = Symbol('x')
|
| 1429 |
+
assert sech(x).rewrite(exp) == 1 / (exp(x)/2 + exp(-x)/2) \
|
| 1430 |
+
== sech(x).rewrite('tractable')
|
| 1431 |
+
assert sech(x).rewrite(sinh) == I/sinh(x + I*pi/2, evaluate=False)
|
| 1432 |
+
tanh_half = tanh(S.Half*x)**2
|
| 1433 |
+
assert sech(x).rewrite(tanh) == (1 - tanh_half)/(1 + tanh_half)
|
| 1434 |
+
coth_half = coth(S.Half*x)**2
|
| 1435 |
+
assert sech(x).rewrite(coth) == (coth_half - 1)/(coth_half + 1)
|
| 1436 |
+
|
| 1437 |
+
|
| 1438 |
+
def test_derivs():
|
| 1439 |
+
x = Symbol('x')
|
| 1440 |
+
assert coth(x).diff(x) == -sinh(x)**(-2)
|
| 1441 |
+
assert sinh(x).diff(x) == cosh(x)
|
| 1442 |
+
assert cosh(x).diff(x) == sinh(x)
|
| 1443 |
+
assert tanh(x).diff(x) == -tanh(x)**2 + 1
|
| 1444 |
+
assert csch(x).diff(x) == -coth(x)*csch(x)
|
| 1445 |
+
assert sech(x).diff(x) == -tanh(x)*sech(x)
|
| 1446 |
+
assert acoth(x).diff(x) == 1/(-x**2 + 1)
|
| 1447 |
+
assert asinh(x).diff(x) == 1/sqrt(x**2 + 1)
|
| 1448 |
+
assert acosh(x).diff(x) == 1/(sqrt(x - 1)*sqrt(x + 1))
|
| 1449 |
+
assert acosh(x).diff(x) == acosh(x).rewrite(log).diff(x).together()
|
| 1450 |
+
assert atanh(x).diff(x) == 1/(-x**2 + 1)
|
| 1451 |
+
assert asech(x).diff(x) == -1/(x*sqrt(1 - x**2))
|
| 1452 |
+
assert acsch(x).diff(x) == -1/(x**2*sqrt(1 + x**(-2)))
|
| 1453 |
+
|
| 1454 |
+
|
| 1455 |
+
def test_sinh_expansion():
|
| 1456 |
+
x, y = symbols('x,y')
|
| 1457 |
+
assert sinh(x+y).expand(trig=True) == sinh(x)*cosh(y) + cosh(x)*sinh(y)
|
| 1458 |
+
assert sinh(2*x).expand(trig=True) == 2*sinh(x)*cosh(x)
|
| 1459 |
+
assert sinh(3*x).expand(trig=True).expand() == \
|
| 1460 |
+
sinh(x)**3 + 3*sinh(x)*cosh(x)**2
|
| 1461 |
+
|
| 1462 |
+
|
| 1463 |
+
def test_cosh_expansion():
|
| 1464 |
+
x, y = symbols('x,y')
|
| 1465 |
+
assert cosh(x+y).expand(trig=True) == cosh(x)*cosh(y) + sinh(x)*sinh(y)
|
| 1466 |
+
assert cosh(2*x).expand(trig=True) == cosh(x)**2 + sinh(x)**2
|
| 1467 |
+
assert cosh(3*x).expand(trig=True).expand() == \
|
| 1468 |
+
3*sinh(x)**2*cosh(x) + cosh(x)**3
|
| 1469 |
+
|
| 1470 |
+
def test_cosh_positive():
|
| 1471 |
+
# See issue 11721
|
| 1472 |
+
# cosh(x) is positive for real values of x
|
| 1473 |
+
k = symbols('k', real=True)
|
| 1474 |
+
n = symbols('n', integer=True)
|
| 1475 |
+
|
| 1476 |
+
assert cosh(k, evaluate=False).is_positive is True
|
| 1477 |
+
assert cosh(k + 2*n*pi*I, evaluate=False).is_positive is True
|
| 1478 |
+
assert cosh(I*pi/4, evaluate=False).is_positive is True
|
| 1479 |
+
assert cosh(3*I*pi/4, evaluate=False).is_positive is False
|
| 1480 |
+
|
| 1481 |
+
def test_cosh_nonnegative():
|
| 1482 |
+
k = symbols('k', real=True)
|
| 1483 |
+
n = symbols('n', integer=True)
|
| 1484 |
+
|
| 1485 |
+
assert cosh(k, evaluate=False).is_nonnegative is True
|
| 1486 |
+
assert cosh(k + 2*n*pi*I, evaluate=False).is_nonnegative is True
|
| 1487 |
+
assert cosh(I*pi/4, evaluate=False).is_nonnegative is True
|
| 1488 |
+
assert cosh(3*I*pi/4, evaluate=False).is_nonnegative is False
|
| 1489 |
+
assert cosh(S.Zero, evaluate=False).is_nonnegative is True
|
| 1490 |
+
|
| 1491 |
+
def test_real_assumptions():
|
| 1492 |
+
z = Symbol('z', real=False)
|
| 1493 |
+
assert sinh(z).is_real is None
|
| 1494 |
+
assert cosh(z).is_real is None
|
| 1495 |
+
assert tanh(z).is_real is None
|
| 1496 |
+
assert sech(z).is_real is None
|
| 1497 |
+
assert csch(z).is_real is None
|
| 1498 |
+
assert coth(z).is_real is None
|
| 1499 |
+
|
| 1500 |
+
def test_sign_assumptions():
|
| 1501 |
+
p = Symbol('p', positive=True)
|
| 1502 |
+
n = Symbol('n', negative=True)
|
| 1503 |
+
assert sinh(n).is_negative is True
|
| 1504 |
+
assert sinh(p).is_positive is True
|
| 1505 |
+
assert cosh(n).is_positive is True
|
| 1506 |
+
assert cosh(p).is_positive is True
|
| 1507 |
+
assert tanh(n).is_negative is True
|
| 1508 |
+
assert tanh(p).is_positive is True
|
| 1509 |
+
assert csch(n).is_negative is True
|
| 1510 |
+
assert csch(p).is_positive is True
|
| 1511 |
+
assert sech(n).is_positive is True
|
| 1512 |
+
assert sech(p).is_positive is True
|
| 1513 |
+
assert coth(n).is_negative is True
|
| 1514 |
+
assert coth(p).is_positive is True
|
| 1515 |
+
|
| 1516 |
+
|
| 1517 |
+
def test_issue_25847():
|
| 1518 |
+
x = Symbol('x')
|
| 1519 |
+
|
| 1520 |
+
#atanh
|
| 1521 |
+
assert atanh(sin(x)/x).as_leading_term(x) == atanh(sin(x)/x)
|
| 1522 |
+
raises(PoleError, lambda: atanh(exp(1/x)).as_leading_term(x))
|
| 1523 |
+
|
| 1524 |
+
#asinh
|
| 1525 |
+
assert asinh(sin(x)/x).as_leading_term(x) == log(1 + sqrt(2))
|
| 1526 |
+
raises(PoleError, lambda: asinh(exp(1/x)).as_leading_term(x))
|
| 1527 |
+
|
| 1528 |
+
#acosh
|
| 1529 |
+
assert acosh(sin(x)/x).as_leading_term(x) == 0
|
| 1530 |
+
raises(PoleError, lambda: acosh(exp(1/x)).as_leading_term(x))
|
| 1531 |
+
|
| 1532 |
+
#acoth
|
| 1533 |
+
assert acoth(sin(x)/x).as_leading_term(x) == acoth(sin(x)/x)
|
| 1534 |
+
raises(PoleError, lambda: acoth(exp(1/x)).as_leading_term(x))
|
| 1535 |
+
|
| 1536 |
+
#asech
|
| 1537 |
+
assert asech(sinh(x)/x).as_leading_term(x) == 0
|
| 1538 |
+
raises(PoleError, lambda: asech(exp(1/x)).as_leading_term(x))
|
| 1539 |
+
|
| 1540 |
+
#acsch
|
| 1541 |
+
assert acsch(sin(x)/x).as_leading_term(x) == log(1 + sqrt(2))
|
| 1542 |
+
raises(PoleError, lambda: acsch(exp(1/x)).as_leading_term(x))
|
parrot/lib/python3.10/site-packages/sympy/matrices/benchmarks/__pycache__/bench_matrix.cpython-310.pyc
ADDED
|
Binary file (885 Bytes). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/funcmatrix.cpython-310.pyc
ADDED
|
Binary file (4.23 kB). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/matrices/expressions/__pycache__/slice.cpython-310.pyc
ADDED
|
Binary file (3.55 kB). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/testing/__pycache__/quality_unicode.cpython-310.pyc
ADDED
|
Binary file (2.24 kB). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/testing/pytest.py
ADDED
|
@@ -0,0 +1,401 @@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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""py.test hacks to support XFAIL/XPASS"""
|
| 2 |
+
|
| 3 |
+
import sys
|
| 4 |
+
import re
|
| 5 |
+
import functools
|
| 6 |
+
import os
|
| 7 |
+
import contextlib
|
| 8 |
+
import warnings
|
| 9 |
+
import inspect
|
| 10 |
+
import pathlib
|
| 11 |
+
from typing import Any, Callable
|
| 12 |
+
|
| 13 |
+
from sympy.utilities.exceptions import SymPyDeprecationWarning
|
| 14 |
+
# Imported here for backwards compatibility. Note: do not import this from
|
| 15 |
+
# here in library code (importing sympy.pytest in library code will break the
|
| 16 |
+
# pytest integration).
|
| 17 |
+
from sympy.utilities.exceptions import ignore_warnings # noqa:F401
|
| 18 |
+
|
| 19 |
+
ON_CI = os.getenv('CI', None) == "true"
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
import pytest
|
| 23 |
+
USE_PYTEST = getattr(sys, '_running_pytest', False)
|
| 24 |
+
except ImportError:
|
| 25 |
+
USE_PYTEST = False
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
raises: Callable[[Any, Any], Any]
|
| 29 |
+
XFAIL: Callable[[Any], Any]
|
| 30 |
+
skip: Callable[[Any], Any]
|
| 31 |
+
SKIP: Callable[[Any], Any]
|
| 32 |
+
slow: Callable[[Any], Any]
|
| 33 |
+
tooslow: Callable[[Any], Any]
|
| 34 |
+
nocache_fail: Callable[[Any], Any]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
if USE_PYTEST:
|
| 38 |
+
raises = pytest.raises
|
| 39 |
+
skip = pytest.skip
|
| 40 |
+
XFAIL = pytest.mark.xfail
|
| 41 |
+
SKIP = pytest.mark.skip
|
| 42 |
+
slow = pytest.mark.slow
|
| 43 |
+
tooslow = pytest.mark.tooslow
|
| 44 |
+
nocache_fail = pytest.mark.nocache_fail
|
| 45 |
+
from _pytest.outcomes import Failed
|
| 46 |
+
|
| 47 |
+
else:
|
| 48 |
+
# Not using pytest so define the things that would have been imported from
|
| 49 |
+
# there.
|
| 50 |
+
|
| 51 |
+
# _pytest._code.code.ExceptionInfo
|
| 52 |
+
class ExceptionInfo:
|
| 53 |
+
def __init__(self, value):
|
| 54 |
+
self.value = value
|
| 55 |
+
|
| 56 |
+
def __repr__(self):
|
| 57 |
+
return "<ExceptionInfo {!r}>".format(self.value)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def raises(expectedException, code=None):
|
| 61 |
+
"""
|
| 62 |
+
Tests that ``code`` raises the exception ``expectedException``.
|
| 63 |
+
|
| 64 |
+
``code`` may be a callable, such as a lambda expression or function
|
| 65 |
+
name.
|
| 66 |
+
|
| 67 |
+
If ``code`` is not given or None, ``raises`` will return a context
|
| 68 |
+
manager for use in ``with`` statements; the code to execute then
|
| 69 |
+
comes from the scope of the ``with``.
|
| 70 |
+
|
| 71 |
+
``raises()`` does nothing if the callable raises the expected exception,
|
| 72 |
+
otherwise it raises an AssertionError.
|
| 73 |
+
|
| 74 |
+
Examples
|
| 75 |
+
========
|
| 76 |
+
|
| 77 |
+
>>> from sympy.testing.pytest import raises
|
| 78 |
+
|
| 79 |
+
>>> raises(ZeroDivisionError, lambda: 1/0)
|
| 80 |
+
<ExceptionInfo ZeroDivisionError(...)>
|
| 81 |
+
>>> raises(ZeroDivisionError, lambda: 1/2)
|
| 82 |
+
Traceback (most recent call last):
|
| 83 |
+
...
|
| 84 |
+
Failed: DID NOT RAISE
|
| 85 |
+
|
| 86 |
+
>>> with raises(ZeroDivisionError):
|
| 87 |
+
... n = 1/0
|
| 88 |
+
>>> with raises(ZeroDivisionError):
|
| 89 |
+
... n = 1/2
|
| 90 |
+
Traceback (most recent call last):
|
| 91 |
+
...
|
| 92 |
+
Failed: DID NOT RAISE
|
| 93 |
+
|
| 94 |
+
Note that you cannot test multiple statements via
|
| 95 |
+
``with raises``:
|
| 96 |
+
|
| 97 |
+
>>> with raises(ZeroDivisionError):
|
| 98 |
+
... n = 1/0 # will execute and raise, aborting the ``with``
|
| 99 |
+
... n = 9999/0 # never executed
|
| 100 |
+
|
| 101 |
+
This is just what ``with`` is supposed to do: abort the
|
| 102 |
+
contained statement sequence at the first exception and let
|
| 103 |
+
the context manager deal with the exception.
|
| 104 |
+
|
| 105 |
+
To test multiple statements, you'll need a separate ``with``
|
| 106 |
+
for each:
|
| 107 |
+
|
| 108 |
+
>>> with raises(ZeroDivisionError):
|
| 109 |
+
... n = 1/0 # will execute and raise
|
| 110 |
+
>>> with raises(ZeroDivisionError):
|
| 111 |
+
... n = 9999/0 # will also execute and raise
|
| 112 |
+
|
| 113 |
+
"""
|
| 114 |
+
if code is None:
|
| 115 |
+
return RaisesContext(expectedException)
|
| 116 |
+
elif callable(code):
|
| 117 |
+
try:
|
| 118 |
+
code()
|
| 119 |
+
except expectedException as e:
|
| 120 |
+
return ExceptionInfo(e)
|
| 121 |
+
raise Failed("DID NOT RAISE")
|
| 122 |
+
elif isinstance(code, str):
|
| 123 |
+
raise TypeError(
|
| 124 |
+
'\'raises(xxx, "code")\' has been phased out; '
|
| 125 |
+
'change \'raises(xxx, "expression")\' '
|
| 126 |
+
'to \'raises(xxx, lambda: expression)\', '
|
| 127 |
+
'\'raises(xxx, "statement")\' '
|
| 128 |
+
'to \'with raises(xxx): statement\'')
|
| 129 |
+
else:
|
| 130 |
+
raise TypeError(
|
| 131 |
+
'raises() expects a callable for the 2nd argument.')
|
| 132 |
+
|
| 133 |
+
class RaisesContext:
|
| 134 |
+
def __init__(self, expectedException):
|
| 135 |
+
self.expectedException = expectedException
|
| 136 |
+
|
| 137 |
+
def __enter__(self):
|
| 138 |
+
return None
|
| 139 |
+
|
| 140 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
| 141 |
+
if exc_type is None:
|
| 142 |
+
raise Failed("DID NOT RAISE")
|
| 143 |
+
return issubclass(exc_type, self.expectedException)
|
| 144 |
+
|
| 145 |
+
class XFail(Exception):
|
| 146 |
+
pass
|
| 147 |
+
|
| 148 |
+
class XPass(Exception):
|
| 149 |
+
pass
|
| 150 |
+
|
| 151 |
+
class Skipped(Exception):
|
| 152 |
+
pass
|
| 153 |
+
|
| 154 |
+
class Failed(Exception): # type: ignore
|
| 155 |
+
pass
|
| 156 |
+
|
| 157 |
+
def XFAIL(func):
|
| 158 |
+
def wrapper():
|
| 159 |
+
try:
|
| 160 |
+
func()
|
| 161 |
+
except Exception as e:
|
| 162 |
+
message = str(e)
|
| 163 |
+
if message != "Timeout":
|
| 164 |
+
raise XFail(func.__name__)
|
| 165 |
+
else:
|
| 166 |
+
raise Skipped("Timeout")
|
| 167 |
+
raise XPass(func.__name__)
|
| 168 |
+
|
| 169 |
+
wrapper = functools.update_wrapper(wrapper, func)
|
| 170 |
+
return wrapper
|
| 171 |
+
|
| 172 |
+
def skip(str):
|
| 173 |
+
raise Skipped(str)
|
| 174 |
+
|
| 175 |
+
def SKIP(reason):
|
| 176 |
+
"""Similar to ``skip()``, but this is a decorator. """
|
| 177 |
+
def wrapper(func):
|
| 178 |
+
def func_wrapper():
|
| 179 |
+
raise Skipped(reason)
|
| 180 |
+
|
| 181 |
+
func_wrapper = functools.update_wrapper(func_wrapper, func)
|
| 182 |
+
return func_wrapper
|
| 183 |
+
|
| 184 |
+
return wrapper
|
| 185 |
+
|
| 186 |
+
def slow(func):
|
| 187 |
+
func._slow = True
|
| 188 |
+
|
| 189 |
+
def func_wrapper():
|
| 190 |
+
func()
|
| 191 |
+
|
| 192 |
+
func_wrapper = functools.update_wrapper(func_wrapper, func)
|
| 193 |
+
func_wrapper.__wrapped__ = func
|
| 194 |
+
return func_wrapper
|
| 195 |
+
|
| 196 |
+
def tooslow(func):
|
| 197 |
+
func._slow = True
|
| 198 |
+
func._tooslow = True
|
| 199 |
+
|
| 200 |
+
def func_wrapper():
|
| 201 |
+
skip("Too slow")
|
| 202 |
+
|
| 203 |
+
func_wrapper = functools.update_wrapper(func_wrapper, func)
|
| 204 |
+
func_wrapper.__wrapped__ = func
|
| 205 |
+
return func_wrapper
|
| 206 |
+
|
| 207 |
+
def nocache_fail(func):
|
| 208 |
+
"Dummy decorator for marking tests that fail when cache is disabled"
|
| 209 |
+
return func
|
| 210 |
+
|
| 211 |
+
@contextlib.contextmanager
|
| 212 |
+
def warns(warningcls, *, match='', test_stacklevel=True):
|
| 213 |
+
'''
|
| 214 |
+
Like raises but tests that warnings are emitted.
|
| 215 |
+
|
| 216 |
+
>>> from sympy.testing.pytest import warns
|
| 217 |
+
>>> import warnings
|
| 218 |
+
|
| 219 |
+
>>> with warns(UserWarning):
|
| 220 |
+
... warnings.warn('deprecated', UserWarning, stacklevel=2)
|
| 221 |
+
|
| 222 |
+
>>> with warns(UserWarning):
|
| 223 |
+
... pass
|
| 224 |
+
Traceback (most recent call last):
|
| 225 |
+
...
|
| 226 |
+
Failed: DID NOT WARN. No warnings of type UserWarning\
|
| 227 |
+
was emitted. The list of emitted warnings is: [].
|
| 228 |
+
|
| 229 |
+
``test_stacklevel`` makes it check that the ``stacklevel`` parameter to
|
| 230 |
+
``warn()`` is set so that the warning shows the user line of code (the
|
| 231 |
+
code under the warns() context manager). Set this to False if this is
|
| 232 |
+
ambiguous or if the context manager does not test the direct user code
|
| 233 |
+
that emits the warning.
|
| 234 |
+
|
| 235 |
+
If the warning is a ``SymPyDeprecationWarning``, this additionally tests
|
| 236 |
+
that the ``active_deprecations_target`` is a real target in the
|
| 237 |
+
``active-deprecations.md`` file.
|
| 238 |
+
|
| 239 |
+
'''
|
| 240 |
+
# Absorbs all warnings in warnrec
|
| 241 |
+
with warnings.catch_warnings(record=True) as warnrec:
|
| 242 |
+
# Any warning other than the one we are looking for is an error
|
| 243 |
+
warnings.simplefilter("error")
|
| 244 |
+
warnings.filterwarnings("always", category=warningcls)
|
| 245 |
+
# Now run the test
|
| 246 |
+
yield warnrec
|
| 247 |
+
|
| 248 |
+
# Raise if expected warning not found
|
| 249 |
+
if not any(issubclass(w.category, warningcls) for w in warnrec):
|
| 250 |
+
msg = ('Failed: DID NOT WARN.'
|
| 251 |
+
' No warnings of type %s was emitted.'
|
| 252 |
+
' The list of emitted warnings is: %s.'
|
| 253 |
+
) % (warningcls, [w.message for w in warnrec])
|
| 254 |
+
raise Failed(msg)
|
| 255 |
+
|
| 256 |
+
# We don't include the match in the filter above because it would then
|
| 257 |
+
# fall to the error filter, so we instead manually check that it matches
|
| 258 |
+
# here
|
| 259 |
+
for w in warnrec:
|
| 260 |
+
# Should always be true due to the filters above
|
| 261 |
+
assert issubclass(w.category, warningcls)
|
| 262 |
+
if not re.compile(match, re.I).match(str(w.message)):
|
| 263 |
+
raise Failed(f"Failed: WRONG MESSAGE. A warning with of the correct category ({warningcls.__name__}) was issued, but it did not match the given match regex ({match!r})")
|
| 264 |
+
|
| 265 |
+
if test_stacklevel:
|
| 266 |
+
for f in inspect.stack():
|
| 267 |
+
thisfile = f.filename
|
| 268 |
+
file = os.path.split(thisfile)[1]
|
| 269 |
+
if file.startswith('test_'):
|
| 270 |
+
break
|
| 271 |
+
elif file == 'doctest.py':
|
| 272 |
+
# skip the stacklevel testing in the doctests of this
|
| 273 |
+
# function
|
| 274 |
+
return
|
| 275 |
+
else:
|
| 276 |
+
raise RuntimeError("Could not find the file for the given warning to test the stacklevel")
|
| 277 |
+
for w in warnrec:
|
| 278 |
+
if w.filename != thisfile:
|
| 279 |
+
msg = f'''\
|
| 280 |
+
Failed: Warning has the wrong stacklevel. The warning stacklevel needs to be
|
| 281 |
+
set so that the line of code shown in the warning message is user code that
|
| 282 |
+
calls the deprecated code (the current stacklevel is showing code from
|
| 283 |
+
{w.filename} (line {w.lineno}), expected {thisfile})'''.replace('\n', ' ')
|
| 284 |
+
raise Failed(msg)
|
| 285 |
+
|
| 286 |
+
if warningcls == SymPyDeprecationWarning:
|
| 287 |
+
this_file = pathlib.Path(__file__)
|
| 288 |
+
active_deprecations_file = (this_file.parent.parent.parent / 'doc' /
|
| 289 |
+
'src' / 'explanation' /
|
| 290 |
+
'active-deprecations.md')
|
| 291 |
+
if not active_deprecations_file.exists():
|
| 292 |
+
# We can only test that the active_deprecations_target works if we are
|
| 293 |
+
# in the git repo.
|
| 294 |
+
return
|
| 295 |
+
targets = []
|
| 296 |
+
for w in warnrec:
|
| 297 |
+
targets.append(w.message.active_deprecations_target)
|
| 298 |
+
with open(active_deprecations_file, encoding="utf-8") as f:
|
| 299 |
+
text = f.read()
|
| 300 |
+
for target in targets:
|
| 301 |
+
if f'({target})=' not in text:
|
| 302 |
+
raise Failed(f"The active deprecations target {target!r} does not appear to be a valid target in the active-deprecations.md file ({active_deprecations_file}).")
|
| 303 |
+
|
| 304 |
+
def _both_exp_pow(func):
|
| 305 |
+
"""
|
| 306 |
+
Decorator used to run the test twice: the first time `e^x` is represented
|
| 307 |
+
as ``Pow(E, x)``, the second time as ``exp(x)`` (exponential object is not
|
| 308 |
+
a power).
|
| 309 |
+
|
| 310 |
+
This is a temporary trick helping to manage the elimination of the class
|
| 311 |
+
``exp`` in favor of a replacement by ``Pow(E, ...)``.
|
| 312 |
+
"""
|
| 313 |
+
from sympy.core.parameters import _exp_is_pow
|
| 314 |
+
|
| 315 |
+
def func_wrap():
|
| 316 |
+
with _exp_is_pow(True):
|
| 317 |
+
func()
|
| 318 |
+
with _exp_is_pow(False):
|
| 319 |
+
func()
|
| 320 |
+
|
| 321 |
+
wrapper = functools.update_wrapper(func_wrap, func)
|
| 322 |
+
return wrapper
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
@contextlib.contextmanager
|
| 326 |
+
def warns_deprecated_sympy():
|
| 327 |
+
'''
|
| 328 |
+
Shorthand for ``warns(SymPyDeprecationWarning)``
|
| 329 |
+
|
| 330 |
+
This is the recommended way to test that ``SymPyDeprecationWarning`` is
|
| 331 |
+
emitted for deprecated features in SymPy. To test for other warnings use
|
| 332 |
+
``warns``. To suppress warnings without asserting that they are emitted
|
| 333 |
+
use ``ignore_warnings``.
|
| 334 |
+
|
| 335 |
+
.. note::
|
| 336 |
+
|
| 337 |
+
``warns_deprecated_sympy()`` is only intended for internal use in the
|
| 338 |
+
SymPy test suite to test that a deprecation warning triggers properly.
|
| 339 |
+
All other code in the SymPy codebase, including documentation examples,
|
| 340 |
+
should not use deprecated behavior.
|
| 341 |
+
|
| 342 |
+
If you are a user of SymPy and you want to disable
|
| 343 |
+
SymPyDeprecationWarnings, use ``warnings`` filters (see
|
| 344 |
+
:ref:`silencing-sympy-deprecation-warnings`).
|
| 345 |
+
|
| 346 |
+
>>> from sympy.testing.pytest import warns_deprecated_sympy
|
| 347 |
+
>>> from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 348 |
+
>>> with warns_deprecated_sympy():
|
| 349 |
+
... sympy_deprecation_warning("Don't use",
|
| 350 |
+
... deprecated_since_version="1.0",
|
| 351 |
+
... active_deprecations_target="active-deprecations")
|
| 352 |
+
|
| 353 |
+
>>> with warns_deprecated_sympy():
|
| 354 |
+
... pass
|
| 355 |
+
Traceback (most recent call last):
|
| 356 |
+
...
|
| 357 |
+
Failed: DID NOT WARN. No warnings of type \
|
| 358 |
+
SymPyDeprecationWarning was emitted. The list of emitted warnings is: [].
|
| 359 |
+
|
| 360 |
+
.. note::
|
| 361 |
+
|
| 362 |
+
Sometimes the stacklevel test will fail because the same warning is
|
| 363 |
+
emitted multiple times. In this case, you can use
|
| 364 |
+
:func:`sympy.utilities.exceptions.ignore_warnings` in the code to
|
| 365 |
+
prevent the ``SymPyDeprecationWarning`` from being emitted again
|
| 366 |
+
recursively. In rare cases it is impossible to have a consistent
|
| 367 |
+
``stacklevel`` for deprecation warnings because different ways of
|
| 368 |
+
calling a function will produce different call stacks.. In those cases,
|
| 369 |
+
use ``warns(SymPyDeprecationWarning)`` instead.
|
| 370 |
+
|
| 371 |
+
See Also
|
| 372 |
+
========
|
| 373 |
+
sympy.utilities.exceptions.SymPyDeprecationWarning
|
| 374 |
+
sympy.utilities.exceptions.sympy_deprecation_warning
|
| 375 |
+
sympy.utilities.decorator.deprecated
|
| 376 |
+
|
| 377 |
+
'''
|
| 378 |
+
with warns(SymPyDeprecationWarning):
|
| 379 |
+
yield
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
def _running_under_pyodide():
|
| 383 |
+
"""Test if running under pyodide."""
|
| 384 |
+
try:
|
| 385 |
+
import pyodide_js # type: ignore # noqa
|
| 386 |
+
except ImportError:
|
| 387 |
+
return False
|
| 388 |
+
else:
|
| 389 |
+
return True
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
def skip_under_pyodide(message):
|
| 393 |
+
"""Decorator to skip a test if running under pyodide."""
|
| 394 |
+
def decorator(test_func):
|
| 395 |
+
@functools.wraps(test_func)
|
| 396 |
+
def test_wrapper():
|
| 397 |
+
if _running_under_pyodide():
|
| 398 |
+
skip(message)
|
| 399 |
+
return test_func()
|
| 400 |
+
return test_wrapper
|
| 401 |
+
return decorator
|
parrot/lib/python3.10/site-packages/sympy/testing/randtest.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
.. deprecated:: 1.10
|
| 3 |
+
|
| 4 |
+
``sympy.testing.randtest`` functions have been moved to
|
| 5 |
+
:mod:`sympy.core.random`.
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 9 |
+
|
| 10 |
+
sympy_deprecation_warning("The sympy.testing.randtest submodule is deprecated. Use sympy.core.random instead.",
|
| 11 |
+
deprecated_since_version="1.10",
|
| 12 |
+
active_deprecations_target="deprecated-sympy-testing-randtest")
|
| 13 |
+
|
| 14 |
+
from sympy.core.random import ( # noqa:F401
|
| 15 |
+
random_complex_number,
|
| 16 |
+
verify_numerically,
|
| 17 |
+
test_derivative_numerically,
|
| 18 |
+
_randrange,
|
| 19 |
+
_randint)
|
parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (171 Bytes). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/diagnose_imports.cpython-310.pyc
ADDED
|
Binary file (6.51 kB). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/test_code_quality.cpython-310.pyc
ADDED
|
Binary file (16.2 kB). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/test_deprecated.cpython-310.pyc
ADDED
|
Binary file (459 Bytes). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/test_pytest.cpython-310.pyc
ADDED
|
Binary file (7.84 kB). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/testing/tests/__pycache__/test_runtests_pytest.cpython-310.pyc
ADDED
|
Binary file (5.76 kB). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/testing/tests/test_module_imports.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Checks that SymPy does not contain indirect imports.
|
| 3 |
+
|
| 4 |
+
An indirect import is importing a symbol from a module that itself imported the
|
| 5 |
+
symbol from elsewhere. Such a constellation makes it harder to diagnose
|
| 6 |
+
inter-module dependencies and import order problems, and is therefore strongly
|
| 7 |
+
discouraged.
|
| 8 |
+
|
| 9 |
+
(Indirect imports from end-user code is fine and in fact a best practice.)
|
| 10 |
+
|
| 11 |
+
Implementation note: Forcing Python into actually unloading already-imported
|
| 12 |
+
submodules is a tricky and partly undocumented process. To avoid these issues,
|
| 13 |
+
the actual diagnostic code is in bin/diagnose_imports, which is run as a
|
| 14 |
+
separate, pristine Python process.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import subprocess
|
| 18 |
+
import sys
|
| 19 |
+
from os.path import abspath, dirname, join, normpath
|
| 20 |
+
import inspect
|
| 21 |
+
|
| 22 |
+
from sympy.testing.pytest import XFAIL
|
| 23 |
+
|
| 24 |
+
@XFAIL
|
| 25 |
+
def test_module_imports_are_direct():
|
| 26 |
+
my_filename = abspath(inspect.getfile(inspect.currentframe()))
|
| 27 |
+
my_dirname = dirname(my_filename)
|
| 28 |
+
diagnose_imports_filename = join(my_dirname, 'diagnose_imports.py')
|
| 29 |
+
diagnose_imports_filename = normpath(diagnose_imports_filename)
|
| 30 |
+
|
| 31 |
+
process = subprocess.Popen(
|
| 32 |
+
[
|
| 33 |
+
sys.executable,
|
| 34 |
+
normpath(diagnose_imports_filename),
|
| 35 |
+
'--problems',
|
| 36 |
+
'--by-importer'
|
| 37 |
+
],
|
| 38 |
+
stdout=subprocess.PIPE,
|
| 39 |
+
stderr=subprocess.STDOUT,
|
| 40 |
+
bufsize=-1)
|
| 41 |
+
output, _ = process.communicate()
|
| 42 |
+
assert output == '', "There are import problems:\n" + output.decode()
|
parrot/lib/python3.10/site-packages/sympy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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
|
| 3 |
+
from sympy.testing.pytest import (raises, warns, ignore_warnings,
|
| 4 |
+
warns_deprecated_sympy, Failed)
|
| 5 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Test callables
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_expected_exception_is_silent_callable():
|
| 13 |
+
def f():
|
| 14 |
+
raise ValueError()
|
| 15 |
+
raises(ValueError, f)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Under pytest raises will raise Failed rather than AssertionError
|
| 19 |
+
def test_lack_of_exception_triggers_AssertionError_callable():
|
| 20 |
+
try:
|
| 21 |
+
raises(Exception, lambda: 1 + 1)
|
| 22 |
+
assert False
|
| 23 |
+
except Failed as e:
|
| 24 |
+
assert "DID NOT RAISE" in str(e)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_unexpected_exception_is_passed_through_callable():
|
| 28 |
+
def f():
|
| 29 |
+
raise ValueError("some error message")
|
| 30 |
+
try:
|
| 31 |
+
raises(TypeError, f)
|
| 32 |
+
assert False
|
| 33 |
+
except ValueError as e:
|
| 34 |
+
assert str(e) == "some error message"
|
| 35 |
+
|
| 36 |
+
# Test with statement
|
| 37 |
+
|
| 38 |
+
def test_expected_exception_is_silent_with():
|
| 39 |
+
with raises(ValueError):
|
| 40 |
+
raise ValueError()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def test_lack_of_exception_triggers_AssertionError_with():
|
| 44 |
+
try:
|
| 45 |
+
with raises(Exception):
|
| 46 |
+
1 + 1
|
| 47 |
+
assert False
|
| 48 |
+
except Failed as e:
|
| 49 |
+
assert "DID NOT RAISE" in str(e)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def test_unexpected_exception_is_passed_through_with():
|
| 53 |
+
try:
|
| 54 |
+
with raises(TypeError):
|
| 55 |
+
raise ValueError("some error message")
|
| 56 |
+
assert False
|
| 57 |
+
except ValueError as e:
|
| 58 |
+
assert str(e) == "some error message"
|
| 59 |
+
|
| 60 |
+
# Now we can use raises() instead of try/catch
|
| 61 |
+
# to test that a specific exception class is raised
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def test_second_argument_should_be_callable_or_string():
|
| 65 |
+
raises(TypeError, lambda: raises("irrelevant", 42))
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_warns_catches_warning():
|
| 69 |
+
with warnings.catch_warnings(record=True) as w:
|
| 70 |
+
with warns(UserWarning):
|
| 71 |
+
warnings.warn('this is the warning message')
|
| 72 |
+
assert len(w) == 0
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def test_warns_raises_without_warning():
|
| 76 |
+
with raises(Failed):
|
| 77 |
+
with warns(UserWarning):
|
| 78 |
+
pass
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def test_warns_hides_other_warnings():
|
| 82 |
+
with raises(RuntimeWarning):
|
| 83 |
+
with warns(UserWarning):
|
| 84 |
+
warnings.warn('this is the warning message', UserWarning)
|
| 85 |
+
warnings.warn('this is the other message', RuntimeWarning)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def test_warns_continues_after_warning():
|
| 89 |
+
with warnings.catch_warnings(record=True) as w:
|
| 90 |
+
finished = False
|
| 91 |
+
with warns(UserWarning):
|
| 92 |
+
warnings.warn('this is the warning message')
|
| 93 |
+
finished = True
|
| 94 |
+
assert finished
|
| 95 |
+
assert len(w) == 0
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def test_warns_many_warnings():
|
| 99 |
+
with warns(UserWarning):
|
| 100 |
+
warnings.warn('this is the warning message', UserWarning)
|
| 101 |
+
warnings.warn('this is the other warning message', UserWarning)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def test_warns_match_matching():
|
| 105 |
+
with warnings.catch_warnings(record=True) as w:
|
| 106 |
+
with warns(UserWarning, match='this is the warning message'):
|
| 107 |
+
warnings.warn('this is the warning message', UserWarning)
|
| 108 |
+
assert len(w) == 0
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def test_warns_match_non_matching():
|
| 112 |
+
with warnings.catch_warnings(record=True) as w:
|
| 113 |
+
with raises(Failed):
|
| 114 |
+
with warns(UserWarning, match='this is the warning message'):
|
| 115 |
+
warnings.warn('this is not the expected warning message', UserWarning)
|
| 116 |
+
assert len(w) == 0
|
| 117 |
+
|
| 118 |
+
def _warn_sympy_deprecation(stacklevel=3):
|
| 119 |
+
sympy_deprecation_warning(
|
| 120 |
+
"feature",
|
| 121 |
+
active_deprecations_target="active-deprecations",
|
| 122 |
+
deprecated_since_version="0.0.0",
|
| 123 |
+
stacklevel=stacklevel,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
def test_warns_deprecated_sympy_catches_warning():
|
| 127 |
+
with warnings.catch_warnings(record=True) as w:
|
| 128 |
+
with warns_deprecated_sympy():
|
| 129 |
+
_warn_sympy_deprecation()
|
| 130 |
+
assert len(w) == 0
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def test_warns_deprecated_sympy_raises_without_warning():
|
| 134 |
+
with raises(Failed):
|
| 135 |
+
with warns_deprecated_sympy():
|
| 136 |
+
pass
|
| 137 |
+
|
| 138 |
+
def test_warns_deprecated_sympy_wrong_stacklevel():
|
| 139 |
+
with raises(Failed):
|
| 140 |
+
with warns_deprecated_sympy():
|
| 141 |
+
_warn_sympy_deprecation(stacklevel=1)
|
| 142 |
+
|
| 143 |
+
def test_warns_deprecated_sympy_doesnt_hide_other_warnings():
|
| 144 |
+
# Unlike pytest's deprecated_call, we should not hide other warnings.
|
| 145 |
+
with raises(RuntimeWarning):
|
| 146 |
+
with warns_deprecated_sympy():
|
| 147 |
+
_warn_sympy_deprecation()
|
| 148 |
+
warnings.warn('this is the other message', RuntimeWarning)
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def test_warns_deprecated_sympy_continues_after_warning():
|
| 152 |
+
with warnings.catch_warnings(record=True) as w:
|
| 153 |
+
finished = False
|
| 154 |
+
with warns_deprecated_sympy():
|
| 155 |
+
_warn_sympy_deprecation()
|
| 156 |
+
finished = True
|
| 157 |
+
assert finished
|
| 158 |
+
assert len(w) == 0
|
| 159 |
+
|
| 160 |
+
def test_ignore_ignores_warning():
|
| 161 |
+
with warnings.catch_warnings(record=True) as w:
|
| 162 |
+
with ignore_warnings(UserWarning):
|
| 163 |
+
warnings.warn('this is the warning message')
|
| 164 |
+
assert len(w) == 0
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def test_ignore_does_not_raise_without_warning():
|
| 168 |
+
with warnings.catch_warnings(record=True) as w:
|
| 169 |
+
with ignore_warnings(UserWarning):
|
| 170 |
+
pass
|
| 171 |
+
assert len(w) == 0
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def test_ignore_allows_other_warnings():
|
| 175 |
+
with warnings.catch_warnings(record=True) as w:
|
| 176 |
+
# This is needed when pytest is run as -Werror
|
| 177 |
+
# the setting is reverted at the end of the catch_Warnings block.
|
| 178 |
+
warnings.simplefilter("always")
|
| 179 |
+
with ignore_warnings(UserWarning):
|
| 180 |
+
warnings.warn('this is the warning message', UserWarning)
|
| 181 |
+
warnings.warn('this is the other message', RuntimeWarning)
|
| 182 |
+
assert len(w) == 1
|
| 183 |
+
assert isinstance(w[0].message, RuntimeWarning)
|
| 184 |
+
assert str(w[0].message) == 'this is the other message'
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def test_ignore_continues_after_warning():
|
| 188 |
+
with warnings.catch_warnings(record=True) as w:
|
| 189 |
+
finished = False
|
| 190 |
+
with ignore_warnings(UserWarning):
|
| 191 |
+
warnings.warn('this is the warning message')
|
| 192 |
+
finished = True
|
| 193 |
+
assert finished
|
| 194 |
+
assert len(w) == 0
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
def test_ignore_many_warnings():
|
| 198 |
+
with warnings.catch_warnings(record=True) as w:
|
| 199 |
+
# This is needed when pytest is run as -Werror
|
| 200 |
+
# the setting is reverted at the end of the catch_Warnings block.
|
| 201 |
+
warnings.simplefilter("always")
|
| 202 |
+
with ignore_warnings(UserWarning):
|
| 203 |
+
warnings.warn('this is the warning message', UserWarning)
|
| 204 |
+
warnings.warn('this is the other message', RuntimeWarning)
|
| 205 |
+
warnings.warn('this is the warning message', UserWarning)
|
| 206 |
+
warnings.warn('this is the other message', RuntimeWarning)
|
| 207 |
+
warnings.warn('this is the other message', RuntimeWarning)
|
| 208 |
+
assert len(w) == 3
|
| 209 |
+
for wi in w:
|
| 210 |
+
assert isinstance(wi.message, RuntimeWarning)
|
| 211 |
+
assert str(wi.message) == 'this is the other message'
|
parrot/lib/python3.10/site-packages/sympy/testing/tests/test_runtests_pytest.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pathlib
|
| 2 |
+
from typing import List
|
| 3 |
+
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from sympy.testing.runtests_pytest import (
|
| 7 |
+
make_absolute_path,
|
| 8 |
+
sympy_dir,
|
| 9 |
+
update_args_with_paths,
|
| 10 |
+
update_args_with_rootdir,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def test_update_args_with_rootdir():
|
| 15 |
+
"""`--rootdir` and directory three above this added as arguments."""
|
| 16 |
+
args = update_args_with_rootdir([])
|
| 17 |
+
assert args == ['--rootdir', str(pathlib.Path(__file__).parents[3])]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class TestMakeAbsolutePath:
|
| 21 |
+
|
| 22 |
+
@staticmethod
|
| 23 |
+
@pytest.mark.parametrize(
|
| 24 |
+
'partial_path', ['sympy', 'sympy/core', 'sympy/nonexistant_directory'],
|
| 25 |
+
)
|
| 26 |
+
def test_valid_partial_path(partial_path: str):
|
| 27 |
+
"""Paths that start with `sympy` are valid."""
|
| 28 |
+
_ = make_absolute_path(partial_path)
|
| 29 |
+
|
| 30 |
+
@staticmethod
|
| 31 |
+
@pytest.mark.parametrize(
|
| 32 |
+
'partial_path', ['not_sympy', 'also/not/sympy'],
|
| 33 |
+
)
|
| 34 |
+
def test_invalid_partial_path_raises_value_error(partial_path: str):
|
| 35 |
+
"""A `ValueError` is raises on paths that don't start with `sympy`."""
|
| 36 |
+
with pytest.raises(ValueError):
|
| 37 |
+
_ = make_absolute_path(partial_path)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class TestUpdateArgsWithPaths:
|
| 41 |
+
|
| 42 |
+
@staticmethod
|
| 43 |
+
def test_no_paths():
|
| 44 |
+
"""If no paths are passed, only `sympy` and `doc/src` are appended.
|
| 45 |
+
|
| 46 |
+
`sympy` and `doc/src` are the `testpaths` stated in `pytest.ini`. They
|
| 47 |
+
need to be manually added as if any path-related arguments are passed
|
| 48 |
+
to `pytest.main` then the settings in `pytest.ini` may be ignored.
|
| 49 |
+
|
| 50 |
+
"""
|
| 51 |
+
paths = []
|
| 52 |
+
args = update_args_with_paths(paths=paths, keywords=None, args=[])
|
| 53 |
+
expected = [
|
| 54 |
+
str(pathlib.Path(sympy_dir(), 'sympy')),
|
| 55 |
+
str(pathlib.Path(sympy_dir(), 'doc/src')),
|
| 56 |
+
]
|
| 57 |
+
assert args == expected
|
| 58 |
+
|
| 59 |
+
@staticmethod
|
| 60 |
+
@pytest.mark.parametrize(
|
| 61 |
+
'path',
|
| 62 |
+
['sympy/core/tests/test_basic.py', '_basic']
|
| 63 |
+
)
|
| 64 |
+
def test_one_file(path: str):
|
| 65 |
+
"""Single files/paths, full or partial, are matched correctly."""
|
| 66 |
+
args = update_args_with_paths(paths=[path], keywords=None, args=[])
|
| 67 |
+
expected = [
|
| 68 |
+
str(pathlib.Path(sympy_dir(), 'sympy/core/tests/test_basic.py')),
|
| 69 |
+
]
|
| 70 |
+
assert args == expected
|
| 71 |
+
|
| 72 |
+
@staticmethod
|
| 73 |
+
def test_partial_path_from_root():
|
| 74 |
+
"""Partial paths from the root directly are matched correctly."""
|
| 75 |
+
args = update_args_with_paths(paths=['sympy/functions'], keywords=None, args=[])
|
| 76 |
+
expected = [str(pathlib.Path(sympy_dir(), 'sympy/functions'))]
|
| 77 |
+
assert args == expected
|
| 78 |
+
|
| 79 |
+
@staticmethod
|
| 80 |
+
def test_multiple_paths_from_root():
|
| 81 |
+
"""Multiple paths, partial or full, are matched correctly."""
|
| 82 |
+
paths = ['sympy/core/tests/test_basic.py', 'sympy/functions']
|
| 83 |
+
args = update_args_with_paths(paths=paths, keywords=None, args=[])
|
| 84 |
+
expected = [
|
| 85 |
+
str(pathlib.Path(sympy_dir(), 'sympy/core/tests/test_basic.py')),
|
| 86 |
+
str(pathlib.Path(sympy_dir(), 'sympy/functions')),
|
| 87 |
+
]
|
| 88 |
+
assert args == expected
|
| 89 |
+
|
| 90 |
+
@staticmethod
|
| 91 |
+
@pytest.mark.parametrize(
|
| 92 |
+
'paths, expected_paths',
|
| 93 |
+
[
|
| 94 |
+
(
|
| 95 |
+
['/core', '/util'],
|
| 96 |
+
[
|
| 97 |
+
'doc/src/modules/utilities',
|
| 98 |
+
'doc/src/reference/public/utilities',
|
| 99 |
+
'sympy/core',
|
| 100 |
+
'sympy/logic/utilities',
|
| 101 |
+
'sympy/utilities',
|
| 102 |
+
]
|
| 103 |
+
),
|
| 104 |
+
]
|
| 105 |
+
)
|
| 106 |
+
def test_multiple_paths_from_non_root(paths: List[str], expected_paths: List[str]):
|
| 107 |
+
"""Multiple partial paths are matched correctly."""
|
| 108 |
+
args = update_args_with_paths(paths=paths, keywords=None, args=[])
|
| 109 |
+
assert len(args) == len(expected_paths)
|
| 110 |
+
for arg, expected in zip(sorted(args), expected_paths):
|
| 111 |
+
assert expected in arg
|
| 112 |
+
|
| 113 |
+
@staticmethod
|
| 114 |
+
@pytest.mark.parametrize(
|
| 115 |
+
'paths',
|
| 116 |
+
[
|
| 117 |
+
|
| 118 |
+
[],
|
| 119 |
+
['sympy/physics'],
|
| 120 |
+
['sympy/physics/mechanics'],
|
| 121 |
+
['sympy/physics/mechanics/tests'],
|
| 122 |
+
['sympy/physics/mechanics/tests/test_kane3.py'],
|
| 123 |
+
]
|
| 124 |
+
)
|
| 125 |
+
def test_string_as_keyword(paths: List[str]):
|
| 126 |
+
"""String keywords are matched correctly."""
|
| 127 |
+
keywords = ('bicycle', )
|
| 128 |
+
args = update_args_with_paths(paths=paths, keywords=keywords, args=[])
|
| 129 |
+
expected_args = ['sympy/physics/mechanics/tests/test_kane3.py::test_bicycle']
|
| 130 |
+
assert len(args) == len(expected_args)
|
| 131 |
+
for arg, expected in zip(sorted(args), expected_args):
|
| 132 |
+
assert expected in arg
|
| 133 |
+
|
| 134 |
+
@staticmethod
|
| 135 |
+
@pytest.mark.parametrize(
|
| 136 |
+
'paths',
|
| 137 |
+
[
|
| 138 |
+
|
| 139 |
+
[],
|
| 140 |
+
['sympy/core'],
|
| 141 |
+
['sympy/core/tests'],
|
| 142 |
+
['sympy/core/tests/test_sympify.py'],
|
| 143 |
+
]
|
| 144 |
+
)
|
| 145 |
+
def test_integer_as_keyword(paths: List[str]):
|
| 146 |
+
"""Integer keywords are matched correctly."""
|
| 147 |
+
keywords = ('3538', )
|
| 148 |
+
args = update_args_with_paths(paths=paths, keywords=keywords, args=[])
|
| 149 |
+
expected_args = ['sympy/core/tests/test_sympify.py::test_issue_3538']
|
| 150 |
+
assert len(args) == len(expected_args)
|
| 151 |
+
for arg, expected in zip(sorted(args), expected_args):
|
| 152 |
+
assert expected in arg
|
| 153 |
+
|
| 154 |
+
@staticmethod
|
| 155 |
+
def test_multiple_keywords():
|
| 156 |
+
"""Multiple keywords are matched correctly."""
|
| 157 |
+
keywords = ('bicycle', '3538')
|
| 158 |
+
args = update_args_with_paths(paths=[], keywords=keywords, args=[])
|
| 159 |
+
expected_args = [
|
| 160 |
+
'sympy/core/tests/test_sympify.py::test_issue_3538',
|
| 161 |
+
'sympy/physics/mechanics/tests/test_kane3.py::test_bicycle',
|
| 162 |
+
]
|
| 163 |
+
assert len(args) == len(expected_args)
|
| 164 |
+
for arg, expected in zip(sorted(args), expected_args):
|
| 165 |
+
assert expected in arg
|
| 166 |
+
|
| 167 |
+
@staticmethod
|
| 168 |
+
def test_keyword_match_in_multiple_files():
|
| 169 |
+
"""Keywords are matched across multiple files."""
|
| 170 |
+
keywords = ('1130', )
|
| 171 |
+
args = update_args_with_paths(paths=[], keywords=keywords, args=[])
|
| 172 |
+
expected_args = [
|
| 173 |
+
'sympy/integrals/tests/test_heurisch.py::test_heurisch_symbolic_coeffs_1130',
|
| 174 |
+
'sympy/utilities/tests/test_lambdify.py::test_python_div_zero_issue_11306',
|
| 175 |
+
]
|
| 176 |
+
assert len(args) == len(expected_args)
|
| 177 |
+
for arg, expected in zip(sorted(args), expected_args):
|
| 178 |
+
assert expected in arg
|
parrot/lib/python3.10/site-packages/sympy/unify/__init__.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Unification in SymPy
|
| 2 |
+
|
| 3 |
+
See sympy.unify.core docstring for algorithmic details
|
| 4 |
+
|
| 5 |
+
See http://matthewrocklin.com/blog/work/2012/11/01/Unification/ for discussion
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from .usympy import unify, rebuild
|
| 9 |
+
from .rewrite import rewriterule
|
| 10 |
+
|
| 11 |
+
__all__ = [
|
| 12 |
+
'unify', 'rebuild',
|
| 13 |
+
|
| 14 |
+
'rewriterule',
|
| 15 |
+
]
|
parrot/lib/python3.10/site-packages/sympy/unify/__pycache__/core.cpython-310.pyc
ADDED
|
Binary file (8.79 kB). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/unify/core.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Generic Unification algorithm for expression trees with lists of children
|
| 2 |
+
|
| 3 |
+
This implementation is a direct translation of
|
| 4 |
+
|
| 5 |
+
Artificial Intelligence: A Modern Approach by Stuart Russel and Peter Norvig
|
| 6 |
+
Second edition, section 9.2, page 276
|
| 7 |
+
|
| 8 |
+
It is modified in the following ways:
|
| 9 |
+
|
| 10 |
+
1. We allow associative and commutative Compound expressions. This results in
|
| 11 |
+
combinatorial blowup.
|
| 12 |
+
2. We explore the tree lazily.
|
| 13 |
+
3. We provide generic interfaces to symbolic algebra libraries in Python.
|
| 14 |
+
|
| 15 |
+
A more traditional version can be found here
|
| 16 |
+
http://aima.cs.berkeley.edu/python/logic.html
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
from sympy.utilities.iterables import kbins
|
| 20 |
+
|
| 21 |
+
class Compound:
|
| 22 |
+
""" A little class to represent an interior node in the tree
|
| 23 |
+
|
| 24 |
+
This is analogous to SymPy.Basic for non-Atoms
|
| 25 |
+
"""
|
| 26 |
+
def __init__(self, op, args):
|
| 27 |
+
self.op = op
|
| 28 |
+
self.args = args
|
| 29 |
+
|
| 30 |
+
def __eq__(self, other):
|
| 31 |
+
return (type(self) is type(other) and self.op == other.op and
|
| 32 |
+
self.args == other.args)
|
| 33 |
+
|
| 34 |
+
def __hash__(self):
|
| 35 |
+
return hash((type(self), self.op, self.args))
|
| 36 |
+
|
| 37 |
+
def __str__(self):
|
| 38 |
+
return "%s[%s]" % (str(self.op), ', '.join(map(str, self.args)))
|
| 39 |
+
|
| 40 |
+
class Variable:
|
| 41 |
+
""" A Wild token """
|
| 42 |
+
def __init__(self, arg):
|
| 43 |
+
self.arg = arg
|
| 44 |
+
|
| 45 |
+
def __eq__(self, other):
|
| 46 |
+
return type(self) is type(other) and self.arg == other.arg
|
| 47 |
+
|
| 48 |
+
def __hash__(self):
|
| 49 |
+
return hash((type(self), self.arg))
|
| 50 |
+
|
| 51 |
+
def __str__(self):
|
| 52 |
+
return "Variable(%s)" % str(self.arg)
|
| 53 |
+
|
| 54 |
+
class CondVariable:
|
| 55 |
+
""" A wild token that matches conditionally.
|
| 56 |
+
|
| 57 |
+
arg - a wild token.
|
| 58 |
+
valid - an additional constraining function on a match.
|
| 59 |
+
"""
|
| 60 |
+
def __init__(self, arg, valid):
|
| 61 |
+
self.arg = arg
|
| 62 |
+
self.valid = valid
|
| 63 |
+
|
| 64 |
+
def __eq__(self, other):
|
| 65 |
+
return (type(self) is type(other) and
|
| 66 |
+
self.arg == other.arg and
|
| 67 |
+
self.valid == other.valid)
|
| 68 |
+
|
| 69 |
+
def __hash__(self):
|
| 70 |
+
return hash((type(self), self.arg, self.valid))
|
| 71 |
+
|
| 72 |
+
def __str__(self):
|
| 73 |
+
return "CondVariable(%s)" % str(self.arg)
|
| 74 |
+
|
| 75 |
+
def unify(x, y, s=None, **fns):
|
| 76 |
+
""" Unify two expressions.
|
| 77 |
+
|
| 78 |
+
Parameters
|
| 79 |
+
==========
|
| 80 |
+
|
| 81 |
+
x, y - expression trees containing leaves, Compounds and Variables.
|
| 82 |
+
s - a mapping of variables to subtrees.
|
| 83 |
+
|
| 84 |
+
Returns
|
| 85 |
+
=======
|
| 86 |
+
|
| 87 |
+
lazy sequence of mappings {Variable: subtree}
|
| 88 |
+
|
| 89 |
+
Examples
|
| 90 |
+
========
|
| 91 |
+
|
| 92 |
+
>>> from sympy.unify.core import unify, Compound, Variable
|
| 93 |
+
>>> expr = Compound("Add", ("x", "y"))
|
| 94 |
+
>>> pattern = Compound("Add", ("x", Variable("a")))
|
| 95 |
+
>>> next(unify(expr, pattern, {}))
|
| 96 |
+
{Variable(a): 'y'}
|
| 97 |
+
"""
|
| 98 |
+
s = s or {}
|
| 99 |
+
|
| 100 |
+
if x == y:
|
| 101 |
+
yield s
|
| 102 |
+
elif isinstance(x, (Variable, CondVariable)):
|
| 103 |
+
yield from unify_var(x, y, s, **fns)
|
| 104 |
+
elif isinstance(y, (Variable, CondVariable)):
|
| 105 |
+
yield from unify_var(y, x, s, **fns)
|
| 106 |
+
elif isinstance(x, Compound) and isinstance(y, Compound):
|
| 107 |
+
is_commutative = fns.get('is_commutative', lambda x: False)
|
| 108 |
+
is_associative = fns.get('is_associative', lambda x: False)
|
| 109 |
+
for sop in unify(x.op, y.op, s, **fns):
|
| 110 |
+
if is_associative(x) and is_associative(y):
|
| 111 |
+
a, b = (x, y) if len(x.args) < len(y.args) else (y, x)
|
| 112 |
+
if is_commutative(x) and is_commutative(y):
|
| 113 |
+
combs = allcombinations(a.args, b.args, 'commutative')
|
| 114 |
+
else:
|
| 115 |
+
combs = allcombinations(a.args, b.args, 'associative')
|
| 116 |
+
for aaargs, bbargs in combs:
|
| 117 |
+
aa = [unpack(Compound(a.op, arg)) for arg in aaargs]
|
| 118 |
+
bb = [unpack(Compound(b.op, arg)) for arg in bbargs]
|
| 119 |
+
yield from unify(aa, bb, sop, **fns)
|
| 120 |
+
elif len(x.args) == len(y.args):
|
| 121 |
+
yield from unify(x.args, y.args, sop, **fns)
|
| 122 |
+
|
| 123 |
+
elif is_args(x) and is_args(y) and len(x) == len(y):
|
| 124 |
+
if len(x) == 0:
|
| 125 |
+
yield s
|
| 126 |
+
else:
|
| 127 |
+
for shead in unify(x[0], y[0], s, **fns):
|
| 128 |
+
yield from unify(x[1:], y[1:], shead, **fns)
|
| 129 |
+
|
| 130 |
+
def unify_var(var, x, s, **fns):
|
| 131 |
+
if var in s:
|
| 132 |
+
yield from unify(s[var], x, s, **fns)
|
| 133 |
+
elif occur_check(var, x):
|
| 134 |
+
pass
|
| 135 |
+
elif isinstance(var, CondVariable) and var.valid(x):
|
| 136 |
+
yield assoc(s, var, x)
|
| 137 |
+
elif isinstance(var, Variable):
|
| 138 |
+
yield assoc(s, var, x)
|
| 139 |
+
|
| 140 |
+
def occur_check(var, x):
|
| 141 |
+
""" var occurs in subtree owned by x? """
|
| 142 |
+
if var == x:
|
| 143 |
+
return True
|
| 144 |
+
elif isinstance(x, Compound):
|
| 145 |
+
return occur_check(var, x.args)
|
| 146 |
+
elif is_args(x):
|
| 147 |
+
if any(occur_check(var, xi) for xi in x): return True
|
| 148 |
+
return False
|
| 149 |
+
|
| 150 |
+
def assoc(d, key, val):
|
| 151 |
+
""" Return copy of d with key associated to val """
|
| 152 |
+
d = d.copy()
|
| 153 |
+
d[key] = val
|
| 154 |
+
return d
|
| 155 |
+
|
| 156 |
+
def is_args(x):
|
| 157 |
+
""" Is x a traditional iterable? """
|
| 158 |
+
return type(x) in (tuple, list, set)
|
| 159 |
+
|
| 160 |
+
def unpack(x):
|
| 161 |
+
if isinstance(x, Compound) and len(x.args) == 1:
|
| 162 |
+
return x.args[0]
|
| 163 |
+
else:
|
| 164 |
+
return x
|
| 165 |
+
|
| 166 |
+
def allcombinations(A, B, ordered):
|
| 167 |
+
"""
|
| 168 |
+
Restructure A and B to have the same number of elements.
|
| 169 |
+
|
| 170 |
+
Parameters
|
| 171 |
+
==========
|
| 172 |
+
|
| 173 |
+
ordered must be either 'commutative' or 'associative'.
|
| 174 |
+
|
| 175 |
+
A and B can be rearranged so that the larger of the two lists is
|
| 176 |
+
reorganized into smaller sublists.
|
| 177 |
+
|
| 178 |
+
Examples
|
| 179 |
+
========
|
| 180 |
+
|
| 181 |
+
>>> from sympy.unify.core import allcombinations
|
| 182 |
+
>>> for x in allcombinations((1, 2, 3), (5, 6), 'associative'): print(x)
|
| 183 |
+
(((1,), (2, 3)), ((5,), (6,)))
|
| 184 |
+
(((1, 2), (3,)), ((5,), (6,)))
|
| 185 |
+
|
| 186 |
+
>>> for x in allcombinations((1, 2, 3), (5, 6), 'commutative'): print(x)
|
| 187 |
+
(((1,), (2, 3)), ((5,), (6,)))
|
| 188 |
+
(((1, 2), (3,)), ((5,), (6,)))
|
| 189 |
+
(((1,), (3, 2)), ((5,), (6,)))
|
| 190 |
+
(((1, 3), (2,)), ((5,), (6,)))
|
| 191 |
+
(((2,), (1, 3)), ((5,), (6,)))
|
| 192 |
+
(((2, 1), (3,)), ((5,), (6,)))
|
| 193 |
+
(((2,), (3, 1)), ((5,), (6,)))
|
| 194 |
+
(((2, 3), (1,)), ((5,), (6,)))
|
| 195 |
+
(((3,), (1, 2)), ((5,), (6,)))
|
| 196 |
+
(((3, 1), (2,)), ((5,), (6,)))
|
| 197 |
+
(((3,), (2, 1)), ((5,), (6,)))
|
| 198 |
+
(((3, 2), (1,)), ((5,), (6,)))
|
| 199 |
+
"""
|
| 200 |
+
|
| 201 |
+
if ordered == "commutative":
|
| 202 |
+
ordered = 11
|
| 203 |
+
if ordered == "associative":
|
| 204 |
+
ordered = None
|
| 205 |
+
sm, bg = (A, B) if len(A) < len(B) else (B, A)
|
| 206 |
+
for part in kbins(list(range(len(bg))), len(sm), ordered=ordered):
|
| 207 |
+
if bg == B:
|
| 208 |
+
yield tuple((a,) for a in A), partition(B, part)
|
| 209 |
+
else:
|
| 210 |
+
yield partition(A, part), tuple((b,) for b in B)
|
| 211 |
+
|
| 212 |
+
def partition(it, part):
|
| 213 |
+
""" Partition a tuple/list into pieces defined by indices.
|
| 214 |
+
|
| 215 |
+
Examples
|
| 216 |
+
========
|
| 217 |
+
|
| 218 |
+
>>> from sympy.unify.core import partition
|
| 219 |
+
>>> partition((10, 20, 30, 40), [[0, 1, 2], [3]])
|
| 220 |
+
((10, 20, 30), (40,))
|
| 221 |
+
"""
|
| 222 |
+
return type(it)([index(it, ind) for ind in part])
|
| 223 |
+
|
| 224 |
+
def index(it, ind):
|
| 225 |
+
""" Fancy indexing into an indexable iterable (tuple, list).
|
| 226 |
+
|
| 227 |
+
Examples
|
| 228 |
+
========
|
| 229 |
+
|
| 230 |
+
>>> from sympy.unify.core import index
|
| 231 |
+
>>> index([10, 20, 30], (1, 2, 0))
|
| 232 |
+
[20, 30, 10]
|
| 233 |
+
"""
|
| 234 |
+
return type(it)([it[i] for i in ind])
|
parrot/lib/python3.10/site-packages/sympy/unify/rewrite.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Functions to support rewriting of SymPy expressions """
|
| 2 |
+
|
| 3 |
+
from sympy.core.expr import Expr
|
| 4 |
+
from sympy.assumptions import ask
|
| 5 |
+
from sympy.strategies.tools import subs
|
| 6 |
+
from sympy.unify.usympy import rebuild, unify
|
| 7 |
+
|
| 8 |
+
def rewriterule(source, target, variables=(), condition=None, assume=None):
|
| 9 |
+
""" Rewrite rule.
|
| 10 |
+
|
| 11 |
+
Transform expressions that match source into expressions that match target
|
| 12 |
+
treating all ``variables`` as wilds.
|
| 13 |
+
|
| 14 |
+
Examples
|
| 15 |
+
========
|
| 16 |
+
|
| 17 |
+
>>> from sympy.abc import w, x, y, z
|
| 18 |
+
>>> from sympy.unify.rewrite import rewriterule
|
| 19 |
+
>>> from sympy import default_sort_key
|
| 20 |
+
>>> rl = rewriterule(x + y, x**y, [x, y])
|
| 21 |
+
>>> sorted(rl(z + 3), key=default_sort_key)
|
| 22 |
+
[3**z, z**3]
|
| 23 |
+
|
| 24 |
+
Use ``condition`` to specify additional requirements. Inputs are taken in
|
| 25 |
+
the same order as is found in variables.
|
| 26 |
+
|
| 27 |
+
>>> rl = rewriterule(x + y, x**y, [x, y], lambda x, y: x.is_integer)
|
| 28 |
+
>>> list(rl(z + 3))
|
| 29 |
+
[3**z]
|
| 30 |
+
|
| 31 |
+
Use ``assume`` to specify additional requirements using new assumptions.
|
| 32 |
+
|
| 33 |
+
>>> from sympy.assumptions import Q
|
| 34 |
+
>>> rl = rewriterule(x + y, x**y, [x, y], assume=Q.integer(x))
|
| 35 |
+
>>> list(rl(z + 3))
|
| 36 |
+
[3**z]
|
| 37 |
+
|
| 38 |
+
Assumptions for the local context are provided at rule runtime
|
| 39 |
+
|
| 40 |
+
>>> list(rl(w + z, Q.integer(z)))
|
| 41 |
+
[z**w]
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
def rewrite_rl(expr, assumptions=True):
|
| 45 |
+
for match in unify(source, expr, {}, variables=variables):
|
| 46 |
+
if (condition and
|
| 47 |
+
not condition(*[match.get(var, var) for var in variables])):
|
| 48 |
+
continue
|
| 49 |
+
if (assume and not ask(assume.xreplace(match), assumptions)):
|
| 50 |
+
continue
|
| 51 |
+
expr2 = subs(match)(target)
|
| 52 |
+
if isinstance(expr2, Expr):
|
| 53 |
+
expr2 = rebuild(expr2)
|
| 54 |
+
yield expr2
|
| 55 |
+
return rewrite_rl
|
parrot/lib/python3.10/site-packages/sympy/unify/tests/__init__.py
ADDED
|
File without changes
|
parrot/lib/python3.10/site-packages/sympy/unify/tests/test_rewrite.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.unify.rewrite import rewriterule
|
| 2 |
+
from sympy.core.basic import Basic
|
| 3 |
+
from sympy.core.singleton import S
|
| 4 |
+
from sympy.core.symbol import Symbol
|
| 5 |
+
from sympy.functions.elementary.trigonometric import sin
|
| 6 |
+
from sympy.abc import x, y
|
| 7 |
+
from sympy.strategies.rl import rebuild
|
| 8 |
+
from sympy.assumptions import Q
|
| 9 |
+
|
| 10 |
+
p, q = Symbol('p'), Symbol('q')
|
| 11 |
+
|
| 12 |
+
def test_simple():
|
| 13 |
+
rl = rewriterule(Basic(p, S(1)), Basic(p, S(2)), variables=(p,))
|
| 14 |
+
assert list(rl(Basic(S(3), S(1)))) == [Basic(S(3), S(2))]
|
| 15 |
+
|
| 16 |
+
p1 = p**2
|
| 17 |
+
p2 = p**3
|
| 18 |
+
rl = rewriterule(p1, p2, variables=(p,))
|
| 19 |
+
|
| 20 |
+
expr = x**2
|
| 21 |
+
assert list(rl(expr)) == [x**3]
|
| 22 |
+
|
| 23 |
+
def test_simple_variables():
|
| 24 |
+
rl = rewriterule(Basic(x, S(1)), Basic(x, S(2)), variables=(x,))
|
| 25 |
+
assert list(rl(Basic(S(3), S(1)))) == [Basic(S(3), S(2))]
|
| 26 |
+
|
| 27 |
+
rl = rewriterule(x**2, x**3, variables=(x,))
|
| 28 |
+
assert list(rl(y**2)) == [y**3]
|
| 29 |
+
|
| 30 |
+
def test_moderate():
|
| 31 |
+
p1 = p**2 + q**3
|
| 32 |
+
p2 = (p*q)**4
|
| 33 |
+
rl = rewriterule(p1, p2, (p, q))
|
| 34 |
+
|
| 35 |
+
expr = x**2 + y**3
|
| 36 |
+
assert list(rl(expr)) == [(x*y)**4]
|
| 37 |
+
|
| 38 |
+
def test_sincos():
|
| 39 |
+
p1 = sin(p)**2 + sin(p)**2
|
| 40 |
+
p2 = 1
|
| 41 |
+
rl = rewriterule(p1, p2, (p, q))
|
| 42 |
+
|
| 43 |
+
assert list(rl(sin(x)**2 + sin(x)**2)) == [1]
|
| 44 |
+
assert list(rl(sin(y)**2 + sin(y)**2)) == [1]
|
| 45 |
+
|
| 46 |
+
def test_Exprs_ok():
|
| 47 |
+
rl = rewriterule(p+q, q+p, (p, q))
|
| 48 |
+
next(rl(x+y)).is_commutative
|
| 49 |
+
str(next(rl(x+y)))
|
| 50 |
+
|
| 51 |
+
def test_condition_simple():
|
| 52 |
+
rl = rewriterule(x, x+1, [x], lambda x: x < 10)
|
| 53 |
+
assert not list(rl(S(15)))
|
| 54 |
+
assert rebuild(next(rl(S(5)))) == 6
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def test_condition_multiple():
|
| 58 |
+
rl = rewriterule(x + y, x**y, [x,y], lambda x, y: x.is_integer)
|
| 59 |
+
|
| 60 |
+
a = Symbol('a')
|
| 61 |
+
b = Symbol('b', integer=True)
|
| 62 |
+
expr = a + b
|
| 63 |
+
assert list(rl(expr)) == [b**a]
|
| 64 |
+
|
| 65 |
+
c = Symbol('c', integer=True)
|
| 66 |
+
d = Symbol('d', integer=True)
|
| 67 |
+
assert set(rl(c + d)) == {c**d, d**c}
|
| 68 |
+
|
| 69 |
+
def test_assumptions():
|
| 70 |
+
rl = rewriterule(x + y, x**y, [x, y], assume=Q.integer(x))
|
| 71 |
+
|
| 72 |
+
a, b = map(Symbol, 'ab')
|
| 73 |
+
expr = a + b
|
| 74 |
+
assert list(rl(expr, Q.integer(b))) == [b**a]
|
parrot/lib/python3.10/site-packages/sympy/unify/tests/test_unify.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.unify.core import Compound, Variable, CondVariable, allcombinations
|
| 2 |
+
from sympy.unify import core
|
| 3 |
+
|
| 4 |
+
a,b,c = 'a', 'b', 'c'
|
| 5 |
+
w,x,y,z = map(Variable, 'wxyz')
|
| 6 |
+
|
| 7 |
+
C = Compound
|
| 8 |
+
|
| 9 |
+
def is_associative(x):
|
| 10 |
+
return isinstance(x, Compound) and (x.op in ('Add', 'Mul', 'CAdd', 'CMul'))
|
| 11 |
+
def is_commutative(x):
|
| 12 |
+
return isinstance(x, Compound) and (x.op in ('CAdd', 'CMul'))
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def unify(a, b, s={}):
|
| 16 |
+
return core.unify(a, b, s=s, is_associative=is_associative,
|
| 17 |
+
is_commutative=is_commutative)
|
| 18 |
+
|
| 19 |
+
def test_basic():
|
| 20 |
+
assert list(unify(a, x, {})) == [{x: a}]
|
| 21 |
+
assert list(unify(a, x, {x: 10})) == []
|
| 22 |
+
assert list(unify(1, x, {})) == [{x: 1}]
|
| 23 |
+
assert list(unify(a, a, {})) == [{}]
|
| 24 |
+
assert list(unify((w, x), (y, z), {})) == [{w: y, x: z}]
|
| 25 |
+
assert list(unify(x, (a, b), {})) == [{x: (a, b)}]
|
| 26 |
+
|
| 27 |
+
assert list(unify((a, b), (x, x), {})) == []
|
| 28 |
+
assert list(unify((y, z), (x, x), {}))!= []
|
| 29 |
+
assert list(unify((a, (b, c)), (a, (x, y)), {})) == [{x: b, y: c}]
|
| 30 |
+
|
| 31 |
+
def test_ops():
|
| 32 |
+
assert list(unify(C('Add', (a,b,c)), C('Add', (a,x,y)), {})) == \
|
| 33 |
+
[{x:b, y:c}]
|
| 34 |
+
assert list(unify(C('Add', (C('Mul', (1,2)), b,c)), C('Add', (x,y,c)), {})) == \
|
| 35 |
+
[{x: C('Mul', (1,2)), y:b}]
|
| 36 |
+
|
| 37 |
+
def test_associative():
|
| 38 |
+
c1 = C('Add', (1,2,3))
|
| 39 |
+
c2 = C('Add', (x,y))
|
| 40 |
+
assert tuple(unify(c1, c2, {})) == ({x: 1, y: C('Add', (2, 3))},
|
| 41 |
+
{x: C('Add', (1, 2)), y: 3})
|
| 42 |
+
|
| 43 |
+
def test_commutative():
|
| 44 |
+
c1 = C('CAdd', (1,2,3))
|
| 45 |
+
c2 = C('CAdd', (x,y))
|
| 46 |
+
result = list(unify(c1, c2, {}))
|
| 47 |
+
assert {x: 1, y: C('CAdd', (2, 3))} in result
|
| 48 |
+
assert ({x: 2, y: C('CAdd', (1, 3))} in result or
|
| 49 |
+
{x: 2, y: C('CAdd', (3, 1))} in result)
|
| 50 |
+
|
| 51 |
+
def _test_combinations_assoc():
|
| 52 |
+
assert set(allcombinations((1,2,3), (a,b), True)) == \
|
| 53 |
+
{(((1, 2), (3,)), (a, b)), (((1,), (2, 3)), (a, b))}
|
| 54 |
+
|
| 55 |
+
def _test_combinations_comm():
|
| 56 |
+
assert set(allcombinations((1,2,3), (a,b), None)) == \
|
| 57 |
+
{(((1,), (2, 3)), ('a', 'b')), (((2,), (3, 1)), ('a', 'b')),
|
| 58 |
+
(((3,), (1, 2)), ('a', 'b')), (((1, 2), (3,)), ('a', 'b')),
|
| 59 |
+
(((2, 3), (1,)), ('a', 'b')), (((3, 1), (2,)), ('a', 'b'))}
|
| 60 |
+
|
| 61 |
+
def test_allcombinations():
|
| 62 |
+
assert set(allcombinations((1,2), (1,2), 'commutative')) ==\
|
| 63 |
+
{(((1,),(2,)), ((1,),(2,))), (((1,),(2,)), ((2,),(1,)))}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def test_commutativity():
|
| 67 |
+
c1 = Compound('CAdd', (a, b))
|
| 68 |
+
c2 = Compound('CAdd', (x, y))
|
| 69 |
+
assert is_commutative(c1) and is_commutative(c2)
|
| 70 |
+
assert len(list(unify(c1, c2, {}))) == 2
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_CondVariable():
|
| 74 |
+
expr = C('CAdd', (1, 2))
|
| 75 |
+
x = Variable('x')
|
| 76 |
+
y = CondVariable('y', lambda a: a % 2 == 0)
|
| 77 |
+
z = CondVariable('z', lambda a: a > 3)
|
| 78 |
+
pattern = C('CAdd', (x, y))
|
| 79 |
+
assert list(unify(expr, pattern, {})) == \
|
| 80 |
+
[{x: 1, y: 2}]
|
| 81 |
+
|
| 82 |
+
z = CondVariable('z', lambda a: a > 3)
|
| 83 |
+
pattern = C('CAdd', (z, y))
|
| 84 |
+
|
| 85 |
+
assert list(unify(expr, pattern, {})) == []
|
| 86 |
+
|
| 87 |
+
def test_defaultdict():
|
| 88 |
+
assert next(unify(Variable('x'), 'foo')) == {Variable('x'): 'foo'}
|
parrot/lib/python3.10/site-packages/sympy/unify/usympy.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" SymPy interface to Unification engine
|
| 2 |
+
|
| 3 |
+
See sympy.unify for module level docstring
|
| 4 |
+
See sympy.unify.core for algorithmic docstring """
|
| 5 |
+
|
| 6 |
+
from sympy.core import Basic, Add, Mul, Pow
|
| 7 |
+
from sympy.core.operations import AssocOp, LatticeOp
|
| 8 |
+
from sympy.matrices import MatAdd, MatMul, MatrixExpr
|
| 9 |
+
from sympy.sets.sets import Union, Intersection, FiniteSet
|
| 10 |
+
from sympy.unify.core import Compound, Variable, CondVariable
|
| 11 |
+
from sympy.unify import core
|
| 12 |
+
|
| 13 |
+
basic_new_legal = [MatrixExpr]
|
| 14 |
+
eval_false_legal = [AssocOp, Pow, FiniteSet]
|
| 15 |
+
illegal = [LatticeOp]
|
| 16 |
+
|
| 17 |
+
def sympy_associative(op):
|
| 18 |
+
assoc_ops = (AssocOp, MatAdd, MatMul, Union, Intersection, FiniteSet)
|
| 19 |
+
return any(issubclass(op, aop) for aop in assoc_ops)
|
| 20 |
+
|
| 21 |
+
def sympy_commutative(op):
|
| 22 |
+
comm_ops = (Add, MatAdd, Union, Intersection, FiniteSet)
|
| 23 |
+
return any(issubclass(op, cop) for cop in comm_ops)
|
| 24 |
+
|
| 25 |
+
def is_associative(x):
|
| 26 |
+
return isinstance(x, Compound) and sympy_associative(x.op)
|
| 27 |
+
|
| 28 |
+
def is_commutative(x):
|
| 29 |
+
if not isinstance(x, Compound):
|
| 30 |
+
return False
|
| 31 |
+
if sympy_commutative(x.op):
|
| 32 |
+
return True
|
| 33 |
+
if issubclass(x.op, Mul):
|
| 34 |
+
return all(construct(arg).is_commutative for arg in x.args)
|
| 35 |
+
|
| 36 |
+
def mk_matchtype(typ):
|
| 37 |
+
def matchtype(x):
|
| 38 |
+
return (isinstance(x, typ) or
|
| 39 |
+
isinstance(x, Compound) and issubclass(x.op, typ))
|
| 40 |
+
return matchtype
|
| 41 |
+
|
| 42 |
+
def deconstruct(s, variables=()):
|
| 43 |
+
""" Turn a SymPy object into a Compound """
|
| 44 |
+
if s in variables:
|
| 45 |
+
return Variable(s)
|
| 46 |
+
if isinstance(s, (Variable, CondVariable)):
|
| 47 |
+
return s
|
| 48 |
+
if not isinstance(s, Basic) or s.is_Atom:
|
| 49 |
+
return s
|
| 50 |
+
return Compound(s.__class__,
|
| 51 |
+
tuple(deconstruct(arg, variables) for arg in s.args))
|
| 52 |
+
|
| 53 |
+
def construct(t):
|
| 54 |
+
""" Turn a Compound into a SymPy object """
|
| 55 |
+
if isinstance(t, (Variable, CondVariable)):
|
| 56 |
+
return t.arg
|
| 57 |
+
if not isinstance(t, Compound):
|
| 58 |
+
return t
|
| 59 |
+
if any(issubclass(t.op, cls) for cls in eval_false_legal):
|
| 60 |
+
return t.op(*map(construct, t.args), evaluate=False)
|
| 61 |
+
elif any(issubclass(t.op, cls) for cls in basic_new_legal):
|
| 62 |
+
return Basic.__new__(t.op, *map(construct, t.args))
|
| 63 |
+
else:
|
| 64 |
+
return t.op(*map(construct, t.args))
|
| 65 |
+
|
| 66 |
+
def rebuild(s):
|
| 67 |
+
""" Rebuild a SymPy expression.
|
| 68 |
+
|
| 69 |
+
This removes harm caused by Expr-Rules interactions.
|
| 70 |
+
"""
|
| 71 |
+
return construct(deconstruct(s))
|
| 72 |
+
|
| 73 |
+
def unify(x, y, s=None, variables=(), **kwargs):
|
| 74 |
+
""" Structural unification of two expressions/patterns.
|
| 75 |
+
|
| 76 |
+
Examples
|
| 77 |
+
========
|
| 78 |
+
|
| 79 |
+
>>> from sympy.unify.usympy import unify
|
| 80 |
+
>>> from sympy import Basic, S
|
| 81 |
+
>>> from sympy.abc import x, y, z, p, q
|
| 82 |
+
|
| 83 |
+
>>> next(unify(Basic(S(1), S(2)), Basic(S(1), x), variables=[x]))
|
| 84 |
+
{x: 2}
|
| 85 |
+
|
| 86 |
+
>>> expr = 2*x + y + z
|
| 87 |
+
>>> pattern = 2*p + q
|
| 88 |
+
>>> next(unify(expr, pattern, {}, variables=(p, q)))
|
| 89 |
+
{p: x, q: y + z}
|
| 90 |
+
|
| 91 |
+
Unification supports commutative and associative matching
|
| 92 |
+
|
| 93 |
+
>>> expr = x + y + z
|
| 94 |
+
>>> pattern = p + q
|
| 95 |
+
>>> len(list(unify(expr, pattern, {}, variables=(p, q))))
|
| 96 |
+
12
|
| 97 |
+
|
| 98 |
+
Symbols not indicated to be variables are treated as literal,
|
| 99 |
+
else they are wild-like and match anything in a sub-expression.
|
| 100 |
+
|
| 101 |
+
>>> expr = x*y*z + 3
|
| 102 |
+
>>> pattern = x*y + 3
|
| 103 |
+
>>> next(unify(expr, pattern, {}, variables=[x, y]))
|
| 104 |
+
{x: y, y: x*z}
|
| 105 |
+
|
| 106 |
+
The x and y of the pattern above were in a Mul and matched factors
|
| 107 |
+
in the Mul of expr. Here, a single symbol matches an entire term:
|
| 108 |
+
|
| 109 |
+
>>> expr = x*y + 3
|
| 110 |
+
>>> pattern = p + 3
|
| 111 |
+
>>> next(unify(expr, pattern, {}, variables=[p]))
|
| 112 |
+
{p: x*y}
|
| 113 |
+
|
| 114 |
+
"""
|
| 115 |
+
decons = lambda x: deconstruct(x, variables)
|
| 116 |
+
s = s or {}
|
| 117 |
+
s = {decons(k): decons(v) for k, v in s.items()}
|
| 118 |
+
|
| 119 |
+
ds = core.unify(decons(x), decons(y), s,
|
| 120 |
+
is_associative=is_associative,
|
| 121 |
+
is_commutative=is_commutative,
|
| 122 |
+
**kwargs)
|
| 123 |
+
for d in ds:
|
| 124 |
+
yield {construct(k): construct(v) for k, v in d.items()}
|
parrot/lib/python3.10/site-packages/sympy/vector/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (1.74 kB). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/vector/__pycache__/basisdependent.cpython-310.pyc
ADDED
|
Binary file (12.2 kB). View file
|
|
|
parrot/lib/python3.10/site-packages/sympy/vector/__pycache__/coordsysrect.cpython-310.pyc
ADDED
|
Binary file (31.4 kB). View file
|
|
|