ZTWHHH commited on
Commit
2c3c556
·
verified ·
1 Parent(s): 151a8d6

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +6 -0
  2. mplug_owl2/include/openssl/bnerr.h +47 -0
  3. mplug_owl2/include/openssl/httperr.h +55 -0
  4. mplug_owl2/include/openssl/opensslv.h +114 -0
  5. mplug_owl2/include/openssl/proverr.h +148 -0
  6. mplug_owl2/include/openssl/srp.h +285 -0
  7. openflamingo/lib/python3.10/site-packages/sympy/functions/__pycache__/__init__.cpython-310.pyc +0 -0
  8. openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/__init__.py +1 -0
  9. openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/tests/__init__.py +0 -0
  10. openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/tests/__pycache__/__init__.cpython-310.pyc +0 -0
  11. openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/tests/__pycache__/test_comb_numbers.cpython-310.pyc +0 -0
  12. openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/tests/test_comb_factorials.py +653 -0
  13. openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/tests/test_comb_numbers.py +1241 -0
  14. openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/__init__.cpython-310.pyc +0 -0
  15. openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/beta_functions.cpython-310.pyc +0 -0
  16. openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/delta_functions.cpython-310.pyc +0 -0
  17. openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/gamma_functions.cpython-310.pyc +0 -0
  18. openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/hyper.cpython-310.pyc +0 -0
  19. openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/mathieu_functions.cpython-310.pyc +0 -0
  20. openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/zeta_functions.cpython-310.pyc +0 -0
  21. openflamingo/lib/python3.10/site-packages/sympy/functions/special/benchmarks/__init__.py +0 -0
  22. openflamingo/lib/python3.10/site-packages/sympy/functions/special/benchmarks/__pycache__/bench_special.cpython-310.pyc +0 -0
  23. openflamingo/lib/python3.10/site-packages/sympy/functions/special/benchmarks/bench_special.py +8 -0
  24. openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/__init__.cpython-310.pyc +0 -0
  25. openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_bessel.cpython-310.pyc +0 -0
  26. openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_beta_functions.cpython-310.pyc +0 -0
  27. openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_bsplines.cpython-310.pyc +0 -0
  28. openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_delta_functions.cpython-310.pyc +0 -0
  29. openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_hyper.cpython-310.pyc +0 -0
  30. openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_spec_polynomials.cpython-310.pyc +0 -0
  31. openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_spherical_harmonics.cpython-310.pyc +0 -0
  32. openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_tensor_functions.cpython-310.pyc +0 -0
  33. openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_zeta_functions.cpython-310.pyc +0 -0
  34. openflamingo/lib/python3.10/site-packages/sympy/matrices/eigen.py +1346 -0
  35. openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_commonmatrix.py +1266 -0
  36. openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_determinant.py +280 -0
  37. openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_domains.py +113 -0
  38. openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_interactions.py +77 -0
  39. openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_reductions.py +351 -0
  40. openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_repmatrix.py +49 -0
  41. openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_solvers.py +601 -0
  42. openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_sparsetools.py +132 -0
  43. phi4/bin/bunzip2 +3 -0
  44. phi4/bin/lzma +3 -0
  45. phi4/bin/unxz +3 -0
  46. phi4/bin/x86_64-conda_cos7-linux-gnu-ld +3 -0
  47. phi4/bin/xz +3 -0
  48. phi4/bin/xzcat +3 -0
  49. phi4/compiler_compat/README +2 -0
  50. phi4/conda-meta/libuuid-1.41.5-h5eee18b_0.json +81 -0
.gitattributes CHANGED
@@ -732,3 +732,9 @@ mplug_owl2/x86_64-conda-linux-gnu/bin/ld filter=lfs diff=lfs merge=lfs -text
732
  mplug_owl2/x86_64-conda_cos7-linux-gnu/bin/ld filter=lfs diff=lfs merge=lfs -text
733
  llava_video/bin/python filter=lfs diff=lfs merge=lfs -text
734
  openflamingo/lib/python3.10/site-packages/transformers/models/speecht5/__pycache__/modeling_speecht5.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
732
  mplug_owl2/x86_64-conda_cos7-linux-gnu/bin/ld filter=lfs diff=lfs merge=lfs -text
733
  llava_video/bin/python filter=lfs diff=lfs merge=lfs -text
734
  openflamingo/lib/python3.10/site-packages/transformers/models/speecht5/__pycache__/modeling_speecht5.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
735
+ phi4/bin/xzcat filter=lfs diff=lfs merge=lfs -text
736
+ phi4/bin/xz filter=lfs diff=lfs merge=lfs -text
737
+ phi4/bin/lzma filter=lfs diff=lfs merge=lfs -text
738
+ phi4/bin/bunzip2 filter=lfs diff=lfs merge=lfs -text
739
+ phi4/bin/x86_64-conda_cos7-linux-gnu-ld filter=lfs diff=lfs merge=lfs -text
740
+ phi4/bin/unxz filter=lfs diff=lfs merge=lfs -text
mplug_owl2/include/openssl/bnerr.h ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Generated by util/mkerr.pl DO NOT EDIT
3
+ * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
4
+ *
5
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
6
+ * this file except in compliance with the License. You can obtain a copy
7
+ * in the file LICENSE in the source distribution or at
8
+ * https://www.openssl.org/source/license.html
9
+ */
10
+
11
+ #ifndef OPENSSL_BNERR_H
12
+ # define OPENSSL_BNERR_H
13
+ # pragma once
14
+
15
+ # include <openssl/opensslconf.h>
16
+ # include <openssl/symhacks.h>
17
+ # include <openssl/cryptoerr_legacy.h>
18
+
19
+
20
+
21
+ /*
22
+ * BN reason codes.
23
+ */
24
+ # define BN_R_ARG2_LT_ARG3 100
25
+ # define BN_R_BAD_RECIPROCAL 101
26
+ # define BN_R_BIGNUM_TOO_LONG 114
27
+ # define BN_R_BITS_TOO_SMALL 118
28
+ # define BN_R_CALLED_WITH_EVEN_MODULUS 102
29
+ # define BN_R_DIV_BY_ZERO 103
30
+ # define BN_R_ENCODING_ERROR 104
31
+ # define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105
32
+ # define BN_R_INPUT_NOT_REDUCED 110
33
+ # define BN_R_INVALID_LENGTH 106
34
+ # define BN_R_INVALID_RANGE 115
35
+ # define BN_R_INVALID_SHIFT 119
36
+ # define BN_R_NOT_A_SQUARE 111
37
+ # define BN_R_NOT_INITIALIZED 107
38
+ # define BN_R_NO_INVERSE 108
39
+ # define BN_R_NO_PRIME_CANDIDATE 121
40
+ # define BN_R_NO_SOLUTION 116
41
+ # define BN_R_NO_SUITABLE_DIGEST 120
42
+ # define BN_R_PRIVATE_KEY_TOO_LARGE 117
43
+ # define BN_R_P_IS_NOT_PRIME 112
44
+ # define BN_R_TOO_MANY_ITERATIONS 113
45
+ # define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109
46
+
47
+ #endif
mplug_owl2/include/openssl/httperr.h ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Generated by util/mkerr.pl DO NOT EDIT
3
+ * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4
+ *
5
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
6
+ * this file except in compliance with the License. You can obtain a copy
7
+ * in the file LICENSE in the source distribution or at
8
+ * https://www.openssl.org/source/license.html
9
+ */
10
+
11
+ #ifndef OPENSSL_HTTPERR_H
12
+ # define OPENSSL_HTTPERR_H
13
+ # pragma once
14
+
15
+ # include <openssl/opensslconf.h>
16
+ # include <openssl/symhacks.h>
17
+ # include <openssl/cryptoerr_legacy.h>
18
+
19
+
20
+
21
+ /*
22
+ * HTTP reason codes.
23
+ */
24
+ # define HTTP_R_ASN1_LEN_EXCEEDS_MAX_RESP_LEN 108
25
+ # define HTTP_R_CONNECT_FAILURE 100
26
+ # define HTTP_R_ERROR_PARSING_ASN1_LENGTH 109
27
+ # define HTTP_R_ERROR_PARSING_CONTENT_LENGTH 119
28
+ # define HTTP_R_ERROR_PARSING_URL 101
29
+ # define HTTP_R_ERROR_RECEIVING 103
30
+ # define HTTP_R_ERROR_SENDING 102
31
+ # define HTTP_R_FAILED_READING_DATA 128
32
+ # define HTTP_R_HEADER_PARSE_ERROR 126
33
+ # define HTTP_R_INCONSISTENT_CONTENT_LENGTH 120
34
+ # define HTTP_R_INVALID_PORT_NUMBER 123
35
+ # define HTTP_R_INVALID_URL_PATH 125
36
+ # define HTTP_R_INVALID_URL_SCHEME 124
37
+ # define HTTP_R_MAX_RESP_LEN_EXCEEDED 117
38
+ # define HTTP_R_MISSING_ASN1_ENCODING 110
39
+ # define HTTP_R_MISSING_CONTENT_TYPE 121
40
+ # define HTTP_R_MISSING_REDIRECT_LOCATION 111
41
+ # define HTTP_R_RECEIVED_ERROR 105
42
+ # define HTTP_R_RECEIVED_WRONG_HTTP_VERSION 106
43
+ # define HTTP_R_REDIRECTION_FROM_HTTPS_TO_HTTP 112
44
+ # define HTTP_R_REDIRECTION_NOT_ENABLED 116
45
+ # define HTTP_R_RESPONSE_LINE_TOO_LONG 113
46
+ # define HTTP_R_RESPONSE_PARSE_ERROR 104
47
+ # define HTTP_R_RETRY_TIMEOUT 129
48
+ # define HTTP_R_SERVER_CANCELED_CONNECTION 127
49
+ # define HTTP_R_SOCK_NOT_SUPPORTED 122
50
+ # define HTTP_R_STATUS_CODE_UNSUPPORTED 114
51
+ # define HTTP_R_TLS_NOT_ENABLED 107
52
+ # define HTTP_R_TOO_MANY_REDIRECTIONS 115
53
+ # define HTTP_R_UNEXPECTED_CONTENT_TYPE 118
54
+
55
+ #endif
mplug_owl2/include/openssl/opensslv.h ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * WARNING: do not edit!
3
+ * Generated by Makefile from include/openssl/opensslv.h.in
4
+ *
5
+ * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
6
+ *
7
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
8
+ * this file except in compliance with the License. You can obtain a copy
9
+ * in the file LICENSE in the source distribution or at
10
+ * https://www.openssl.org/source/license.html
11
+ */
12
+
13
+ #ifndef OPENSSL_OPENSSLV_H
14
+ # define OPENSSL_OPENSSLV_H
15
+ # pragma once
16
+
17
+ # ifdef __cplusplus
18
+ extern "C" {
19
+ # endif
20
+
21
+ /*
22
+ * SECTION 1: VERSION DATA. These will change for each release
23
+ */
24
+
25
+ /*
26
+ * Base version macros
27
+ *
28
+ * These macros express version number MAJOR.MINOR.PATCH exactly
29
+ */
30
+ # define OPENSSL_VERSION_MAJOR 3
31
+ # define OPENSSL_VERSION_MINOR 0
32
+ # define OPENSSL_VERSION_PATCH 16
33
+
34
+ /*
35
+ * Additional version information
36
+ *
37
+ * These are also part of the new version scheme, but aren't part
38
+ * of the version number itself.
39
+ */
40
+
41
+ /* Could be: #define OPENSSL_VERSION_PRE_RELEASE "-alpha.1" */
42
+ # define OPENSSL_VERSION_PRE_RELEASE ""
43
+ /* Could be: #define OPENSSL_VERSION_BUILD_METADATA "+fips" */
44
+ /* Could be: #define OPENSSL_VERSION_BUILD_METADATA "+vendor.1" */
45
+ # define OPENSSL_VERSION_BUILD_METADATA ""
46
+
47
+ /*
48
+ * Note: The OpenSSL Project will never define OPENSSL_VERSION_BUILD_METADATA
49
+ * to be anything but the empty string. Its use is entirely reserved for
50
+ * others
51
+ */
52
+
53
+ /*
54
+ * Shared library version
55
+ *
56
+ * This is strictly to express ABI version, which may or may not
57
+ * be related to the API version expressed with the macros above.
58
+ * This is defined in free form.
59
+ */
60
+ # define OPENSSL_SHLIB_VERSION 3
61
+
62
+ /*
63
+ * SECTION 2: USEFUL MACROS
64
+ */
65
+
66
+ /* For checking general API compatibility when preprocessing */
67
+ # define OPENSSL_VERSION_PREREQ(maj,min) \
68
+ ((OPENSSL_VERSION_MAJOR << 16) + OPENSSL_VERSION_MINOR >= ((maj) << 16) + (min))
69
+
70
+ /*
71
+ * Macros to get the version in easily digested string form, both the short
72
+ * "MAJOR.MINOR.PATCH" variant (where MAJOR, MINOR and PATCH are replaced
73
+ * with the values from the corresponding OPENSSL_VERSION_ macros) and the
74
+ * longer variant with OPENSSL_VERSION_PRE_RELEASE_STR and
75
+ * OPENSSL_VERSION_BUILD_METADATA_STR appended.
76
+ */
77
+ # define OPENSSL_VERSION_STR "3.0.16"
78
+ # define OPENSSL_FULL_VERSION_STR "3.0.16"
79
+
80
+ /*
81
+ * SECTION 3: ADDITIONAL METADATA
82
+ *
83
+ * These strings are defined separately to allow them to be parsable.
84
+ */
85
+ # define OPENSSL_RELEASE_DATE "11 Feb 2025"
86
+
87
+ /*
88
+ * SECTION 4: BACKWARD COMPATIBILITY
89
+ */
90
+
91
+ # define OPENSSL_VERSION_TEXT "OpenSSL 3.0.16 11 Feb 2025"
92
+
93
+ /* Synthesize OPENSSL_VERSION_NUMBER with the layout 0xMNN00PPSL */
94
+ # ifdef OPENSSL_VERSION_PRE_RELEASE
95
+ # define _OPENSSL_VERSION_PRE_RELEASE 0x0L
96
+ # else
97
+ # define _OPENSSL_VERSION_PRE_RELEASE 0xfL
98
+ # endif
99
+ # define OPENSSL_VERSION_NUMBER \
100
+ ( (OPENSSL_VERSION_MAJOR<<28) \
101
+ |(OPENSSL_VERSION_MINOR<<20) \
102
+ |(OPENSSL_VERSION_PATCH<<4) \
103
+ |_OPENSSL_VERSION_PRE_RELEASE )
104
+
105
+ # ifdef __cplusplus
106
+ }
107
+ # endif
108
+
109
+ # include <openssl/macros.h>
110
+ # ifndef OPENSSL_NO_DEPRECATED_3_0
111
+ # define HEADER_OPENSSLV_H
112
+ # endif
113
+
114
+ #endif /* OPENSSL_OPENSSLV_H */
mplug_owl2/include/openssl/proverr.h ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Generated by util/mkerr.pl DO NOT EDIT
3
+ * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4
+ *
5
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
6
+ * this file except in compliance with the License. You can obtain a copy
7
+ * in the file LICENSE in the source distribution or at
8
+ * https://www.openssl.org/source/license.html
9
+ */
10
+
11
+ #ifndef OPENSSL_PROVERR_H
12
+ # define OPENSSL_PROVERR_H
13
+ # pragma once
14
+
15
+ # include <openssl/opensslconf.h>
16
+ # include <openssl/symhacks.h>
17
+ # include <openssl/cryptoerr_legacy.h>
18
+
19
+
20
+
21
+ /*
22
+ * PROV reason codes.
23
+ */
24
+ # define PROV_R_ADDITIONAL_INPUT_TOO_LONG 184
25
+ # define PROV_R_ALGORITHM_MISMATCH 173
26
+ # define PROV_R_ALREADY_INSTANTIATED 185
27
+ # define PROV_R_BAD_DECRYPT 100
28
+ # define PROV_R_BAD_ENCODING 141
29
+ # define PROV_R_BAD_LENGTH 142
30
+ # define PROV_R_BAD_TLS_CLIENT_VERSION 161
31
+ # define PROV_R_BN_ERROR 160
32
+ # define PROV_R_CIPHER_OPERATION_FAILED 102
33
+ # define PROV_R_DERIVATION_FUNCTION_INIT_FAILED 205
34
+ # define PROV_R_DIGEST_NOT_ALLOWED 174
35
+ # define PROV_R_ENTROPY_SOURCE_STRENGTH_TOO_WEAK 186
36
+ # define PROV_R_ERROR_INSTANTIATING_DRBG 188
37
+ # define PROV_R_ERROR_RETRIEVING_ENTROPY 189
38
+ # define PROV_R_ERROR_RETRIEVING_NONCE 190
39
+ # define PROV_R_FAILED_DURING_DERIVATION 164
40
+ # define PROV_R_FAILED_TO_CREATE_LOCK 180
41
+ # define PROV_R_FAILED_TO_DECRYPT 162
42
+ # define PROV_R_FAILED_TO_GENERATE_KEY 121
43
+ # define PROV_R_FAILED_TO_GET_PARAMETER 103
44
+ # define PROV_R_FAILED_TO_SET_PARAMETER 104
45
+ # define PROV_R_FAILED_TO_SIGN 175
46
+ # define PROV_R_FIPS_MODULE_CONDITIONAL_ERROR 227
47
+ # define PROV_R_FIPS_MODULE_ENTERING_ERROR_STATE 224
48
+ # define PROV_R_FIPS_MODULE_IN_ERROR_STATE 225
49
+ # define PROV_R_GENERATE_ERROR 191
50
+ # define PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE 165
51
+ # define PROV_R_INDICATOR_INTEGRITY_FAILURE 210
52
+ # define PROV_R_INSUFFICIENT_DRBG_STRENGTH 181
53
+ # define PROV_R_INVALID_AAD 108
54
+ # define PROV_R_INVALID_CONFIG_DATA 211
55
+ # define PROV_R_INVALID_CONSTANT_LENGTH 157
56
+ # define PROV_R_INVALID_CURVE 176
57
+ # define PROV_R_INVALID_CUSTOM_LENGTH 111
58
+ # define PROV_R_INVALID_DATA 115
59
+ # define PROV_R_INVALID_DIGEST 122
60
+ # define PROV_R_INVALID_DIGEST_LENGTH 166
61
+ # define PROV_R_INVALID_DIGEST_SIZE 218
62
+ # define PROV_R_INVALID_INPUT_LENGTH 230
63
+ # define PROV_R_INVALID_ITERATION_COUNT 123
64
+ # define PROV_R_INVALID_IV_LENGTH 109
65
+ # define PROV_R_INVALID_KEY 158
66
+ # define PROV_R_INVALID_KEY_LENGTH 105
67
+ # define PROV_R_INVALID_MAC 151
68
+ # define PROV_R_INVALID_MGF1_MD 167
69
+ # define PROV_R_INVALID_MODE 125
70
+ # define PROV_R_INVALID_OUTPUT_LENGTH 217
71
+ # define PROV_R_INVALID_PADDING_MODE 168
72
+ # define PROV_R_INVALID_PUBINFO 198
73
+ # define PROV_R_INVALID_SALT_LENGTH 112
74
+ # define PROV_R_INVALID_SEED_LENGTH 154
75
+ # define PROV_R_INVALID_SIGNATURE_SIZE 179
76
+ # define PROV_R_INVALID_STATE 212
77
+ # define PROV_R_INVALID_TAG 110
78
+ # define PROV_R_INVALID_TAG_LENGTH 118
79
+ # define PROV_R_INVALID_UKM_LENGTH 200
80
+ # define PROV_R_INVALID_X931_DIGEST 170
81
+ # define PROV_R_IN_ERROR_STATE 192
82
+ # define PROV_R_KEY_SETUP_FAILED 101
83
+ # define PROV_R_KEY_SIZE_TOO_SMALL 171
84
+ # define PROV_R_LENGTH_TOO_LARGE 202
85
+ # define PROV_R_MISMATCHING_DOMAIN_PARAMETERS 203
86
+ # define PROV_R_MISSING_CEK_ALG 144
87
+ # define PROV_R_MISSING_CIPHER 155
88
+ # define PROV_R_MISSING_CONFIG_DATA 213
89
+ # define PROV_R_MISSING_CONSTANT 156
90
+ # define PROV_R_MISSING_KEY 128
91
+ # define PROV_R_MISSING_MAC 150
92
+ # define PROV_R_MISSING_MESSAGE_DIGEST 129
93
+ # define PROV_R_MISSING_OID 209
94
+ # define PROV_R_MISSING_PASS 130
95
+ # define PROV_R_MISSING_SALT 131
96
+ # define PROV_R_MISSING_SECRET 132
97
+ # define PROV_R_MISSING_SEED 140
98
+ # define PROV_R_MISSING_SESSION_ID 133
99
+ # define PROV_R_MISSING_TYPE 134
100
+ # define PROV_R_MISSING_XCGHASH 135
101
+ # define PROV_R_MODULE_INTEGRITY_FAILURE 214
102
+ # define PROV_R_NOT_A_PRIVATE_KEY 221
103
+ # define PROV_R_NOT_A_PUBLIC_KEY 220
104
+ # define PROV_R_NOT_INSTANTIATED 193
105
+ # define PROV_R_NOT_PARAMETERS 226
106
+ # define PROV_R_NOT_SUPPORTED 136
107
+ # define PROV_R_NOT_XOF_OR_INVALID_LENGTH 113
108
+ # define PROV_R_NO_KEY_SET 114
109
+ # define PROV_R_NO_PARAMETERS_SET 177
110
+ # define PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 178
111
+ # define PROV_R_OUTPUT_BUFFER_TOO_SMALL 106
112
+ # define PROV_R_PARENT_CANNOT_GENERATE_RANDOM_NUMBERS 228
113
+ # define PROV_R_PARENT_CANNOT_SUPPLY_ENTROPY_SEED 187
114
+ # define PROV_R_PARENT_LOCKING_NOT_ENABLED 182
115
+ # define PROV_R_PARENT_STRENGTH_TOO_WEAK 194
116
+ # define PROV_R_PATH_MUST_BE_ABSOLUTE 219
117
+ # define PROV_R_PERSONALISATION_STRING_TOO_LONG 195
118
+ # define PROV_R_PSS_SALTLEN_TOO_SMALL 172
119
+ # define PROV_R_REQUEST_TOO_LARGE_FOR_DRBG 196
120
+ # define PROV_R_REQUIRE_CTR_MODE_CIPHER 206
121
+ # define PROV_R_RESEED_ERROR 197
122
+ # define PROV_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES 222
123
+ # define PROV_R_SEED_SOURCES_MUST_NOT_HAVE_A_PARENT 229
124
+ # define PROV_R_SELF_TEST_KAT_FAILURE 215
125
+ # define PROV_R_SELF_TEST_POST_FAILURE 216
126
+ # define PROV_R_TAG_NOT_NEEDED 120
127
+ # define PROV_R_TAG_NOT_SET 119
128
+ # define PROV_R_TOO_MANY_RECORDS 126
129
+ # define PROV_R_UNABLE_TO_FIND_CIPHERS 207
130
+ # define PROV_R_UNABLE_TO_GET_PARENT_STRENGTH 199
131
+ # define PROV_R_UNABLE_TO_GET_PASSPHRASE 159
132
+ # define PROV_R_UNABLE_TO_INITIALISE_CIPHERS 208
133
+ # define PROV_R_UNABLE_TO_LOAD_SHA256 147
134
+ # define PROV_R_UNABLE_TO_LOCK_PARENT 201
135
+ # define PROV_R_UNABLE_TO_RESEED 204
136
+ # define PROV_R_UNSUPPORTED_CEK_ALG 145
137
+ # define PROV_R_UNSUPPORTED_KEY_SIZE 153
138
+ # define PROV_R_UNSUPPORTED_MAC_TYPE 137
139
+ # define PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS 152
140
+ # define PROV_R_URI_AUTHORITY_UNSUPPORTED 223
141
+ # define PROV_R_VALUE_ERROR 138
142
+ # define PROV_R_WRONG_FINAL_BLOCK_LENGTH 107
143
+ # define PROV_R_WRONG_OUTPUT_BUFFER_SIZE 139
144
+ # define PROV_R_XOF_DIGESTS_NOT_ALLOWED 183
145
+ # define PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE 148
146
+ # define PROV_R_XTS_DUPLICATED_KEYS 149
147
+
148
+ #endif
mplug_owl2/include/openssl/srp.h ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * WARNING: do not edit!
3
+ * Generated by Makefile from include/openssl/srp.h.in
4
+ *
5
+ * Copyright 2004-2021 The OpenSSL Project Authors. All Rights Reserved.
6
+ * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
7
+ *
8
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
9
+ * this file except in compliance with the License. You can obtain a copy
10
+ * in the file LICENSE in the source distribution or at
11
+ * https://www.openssl.org/source/license.html
12
+ *
13
+ * Originally written by Christophe Renou and Peter Sylvester,
14
+ * for the EdelKey project.
15
+ */
16
+
17
+
18
+
19
+ #ifndef OPENSSL_SRP_H
20
+ # define OPENSSL_SRP_H
21
+ # pragma once
22
+
23
+ # include <openssl/macros.h>
24
+ # ifndef OPENSSL_NO_DEPRECATED_3_0
25
+ # define HEADER_SRP_H
26
+ # endif
27
+
28
+ #include <openssl/opensslconf.h>
29
+
30
+ #ifndef OPENSSL_NO_SRP
31
+ # include <stdio.h>
32
+ # include <string.h>
33
+ # include <openssl/safestack.h>
34
+ # include <openssl/bn.h>
35
+ # include <openssl/crypto.h>
36
+
37
+ # ifdef __cplusplus
38
+ extern "C" {
39
+ # endif
40
+
41
+ # ifndef OPENSSL_NO_DEPRECATED_3_0
42
+
43
+ typedef struct SRP_gN_cache_st {
44
+ char *b64_bn;
45
+ BIGNUM *bn;
46
+ } SRP_gN_cache;
47
+ SKM_DEFINE_STACK_OF_INTERNAL(SRP_gN_cache, SRP_gN_cache, SRP_gN_cache)
48
+ #define sk_SRP_gN_cache_num(sk) OPENSSL_sk_num(ossl_check_const_SRP_gN_cache_sk_type(sk))
49
+ #define sk_SRP_gN_cache_value(sk, idx) ((SRP_gN_cache *)OPENSSL_sk_value(ossl_check_const_SRP_gN_cache_sk_type(sk), (idx)))
50
+ #define sk_SRP_gN_cache_new(cmp) ((STACK_OF(SRP_gN_cache) *)OPENSSL_sk_new(ossl_check_SRP_gN_cache_compfunc_type(cmp)))
51
+ #define sk_SRP_gN_cache_new_null() ((STACK_OF(SRP_gN_cache) *)OPENSSL_sk_new_null())
52
+ #define sk_SRP_gN_cache_new_reserve(cmp, n) ((STACK_OF(SRP_gN_cache) *)OPENSSL_sk_new_reserve(ossl_check_SRP_gN_cache_compfunc_type(cmp), (n)))
53
+ #define sk_SRP_gN_cache_reserve(sk, n) OPENSSL_sk_reserve(ossl_check_SRP_gN_cache_sk_type(sk), (n))
54
+ #define sk_SRP_gN_cache_free(sk) OPENSSL_sk_free(ossl_check_SRP_gN_cache_sk_type(sk))
55
+ #define sk_SRP_gN_cache_zero(sk) OPENSSL_sk_zero(ossl_check_SRP_gN_cache_sk_type(sk))
56
+ #define sk_SRP_gN_cache_delete(sk, i) ((SRP_gN_cache *)OPENSSL_sk_delete(ossl_check_SRP_gN_cache_sk_type(sk), (i)))
57
+ #define sk_SRP_gN_cache_delete_ptr(sk, ptr) ((SRP_gN_cache *)OPENSSL_sk_delete_ptr(ossl_check_SRP_gN_cache_sk_type(sk), ossl_check_SRP_gN_cache_type(ptr)))
58
+ #define sk_SRP_gN_cache_push(sk, ptr) OPENSSL_sk_push(ossl_check_SRP_gN_cache_sk_type(sk), ossl_check_SRP_gN_cache_type(ptr))
59
+ #define sk_SRP_gN_cache_unshift(sk, ptr) OPENSSL_sk_unshift(ossl_check_SRP_gN_cache_sk_type(sk), ossl_check_SRP_gN_cache_type(ptr))
60
+ #define sk_SRP_gN_cache_pop(sk) ((SRP_gN_cache *)OPENSSL_sk_pop(ossl_check_SRP_gN_cache_sk_type(sk)))
61
+ #define sk_SRP_gN_cache_shift(sk) ((SRP_gN_cache *)OPENSSL_sk_shift(ossl_check_SRP_gN_cache_sk_type(sk)))
62
+ #define sk_SRP_gN_cache_pop_free(sk, freefunc) OPENSSL_sk_pop_free(ossl_check_SRP_gN_cache_sk_type(sk),ossl_check_SRP_gN_cache_freefunc_type(freefunc))
63
+ #define sk_SRP_gN_cache_insert(sk, ptr, idx) OPENSSL_sk_insert(ossl_check_SRP_gN_cache_sk_type(sk), ossl_check_SRP_gN_cache_type(ptr), (idx))
64
+ #define sk_SRP_gN_cache_set(sk, idx, ptr) ((SRP_gN_cache *)OPENSSL_sk_set(ossl_check_SRP_gN_cache_sk_type(sk), (idx), ossl_check_SRP_gN_cache_type(ptr)))
65
+ #define sk_SRP_gN_cache_find(sk, ptr) OPENSSL_sk_find(ossl_check_SRP_gN_cache_sk_type(sk), ossl_check_SRP_gN_cache_type(ptr))
66
+ #define sk_SRP_gN_cache_find_ex(sk, ptr) OPENSSL_sk_find_ex(ossl_check_SRP_gN_cache_sk_type(sk), ossl_check_SRP_gN_cache_type(ptr))
67
+ #define sk_SRP_gN_cache_find_all(sk, ptr, pnum) OPENSSL_sk_find_all(ossl_check_SRP_gN_cache_sk_type(sk), ossl_check_SRP_gN_cache_type(ptr), pnum)
68
+ #define sk_SRP_gN_cache_sort(sk) OPENSSL_sk_sort(ossl_check_SRP_gN_cache_sk_type(sk))
69
+ #define sk_SRP_gN_cache_is_sorted(sk) OPENSSL_sk_is_sorted(ossl_check_const_SRP_gN_cache_sk_type(sk))
70
+ #define sk_SRP_gN_cache_dup(sk) ((STACK_OF(SRP_gN_cache) *)OPENSSL_sk_dup(ossl_check_const_SRP_gN_cache_sk_type(sk)))
71
+ #define sk_SRP_gN_cache_deep_copy(sk, copyfunc, freefunc) ((STACK_OF(SRP_gN_cache) *)OPENSSL_sk_deep_copy(ossl_check_const_SRP_gN_cache_sk_type(sk), ossl_check_SRP_gN_cache_copyfunc_type(copyfunc), ossl_check_SRP_gN_cache_freefunc_type(freefunc)))
72
+ #define sk_SRP_gN_cache_set_cmp_func(sk, cmp) ((sk_SRP_gN_cache_compfunc)OPENSSL_sk_set_cmp_func(ossl_check_SRP_gN_cache_sk_type(sk), ossl_check_SRP_gN_cache_compfunc_type(cmp)))
73
+
74
+
75
+
76
+ typedef struct SRP_user_pwd_st {
77
+ /* Owned by us. */
78
+ char *id;
79
+ BIGNUM *s;
80
+ BIGNUM *v;
81
+ /* Not owned by us. */
82
+ const BIGNUM *g;
83
+ const BIGNUM *N;
84
+ /* Owned by us. */
85
+ char *info;
86
+ } SRP_user_pwd;
87
+ SKM_DEFINE_STACK_OF_INTERNAL(SRP_user_pwd, SRP_user_pwd, SRP_user_pwd)
88
+ #define sk_SRP_user_pwd_num(sk) OPENSSL_sk_num(ossl_check_const_SRP_user_pwd_sk_type(sk))
89
+ #define sk_SRP_user_pwd_value(sk, idx) ((SRP_user_pwd *)OPENSSL_sk_value(ossl_check_const_SRP_user_pwd_sk_type(sk), (idx)))
90
+ #define sk_SRP_user_pwd_new(cmp) ((STACK_OF(SRP_user_pwd) *)OPENSSL_sk_new(ossl_check_SRP_user_pwd_compfunc_type(cmp)))
91
+ #define sk_SRP_user_pwd_new_null() ((STACK_OF(SRP_user_pwd) *)OPENSSL_sk_new_null())
92
+ #define sk_SRP_user_pwd_new_reserve(cmp, n) ((STACK_OF(SRP_user_pwd) *)OPENSSL_sk_new_reserve(ossl_check_SRP_user_pwd_compfunc_type(cmp), (n)))
93
+ #define sk_SRP_user_pwd_reserve(sk, n) OPENSSL_sk_reserve(ossl_check_SRP_user_pwd_sk_type(sk), (n))
94
+ #define sk_SRP_user_pwd_free(sk) OPENSSL_sk_free(ossl_check_SRP_user_pwd_sk_type(sk))
95
+ #define sk_SRP_user_pwd_zero(sk) OPENSSL_sk_zero(ossl_check_SRP_user_pwd_sk_type(sk))
96
+ #define sk_SRP_user_pwd_delete(sk, i) ((SRP_user_pwd *)OPENSSL_sk_delete(ossl_check_SRP_user_pwd_sk_type(sk), (i)))
97
+ #define sk_SRP_user_pwd_delete_ptr(sk, ptr) ((SRP_user_pwd *)OPENSSL_sk_delete_ptr(ossl_check_SRP_user_pwd_sk_type(sk), ossl_check_SRP_user_pwd_type(ptr)))
98
+ #define sk_SRP_user_pwd_push(sk, ptr) OPENSSL_sk_push(ossl_check_SRP_user_pwd_sk_type(sk), ossl_check_SRP_user_pwd_type(ptr))
99
+ #define sk_SRP_user_pwd_unshift(sk, ptr) OPENSSL_sk_unshift(ossl_check_SRP_user_pwd_sk_type(sk), ossl_check_SRP_user_pwd_type(ptr))
100
+ #define sk_SRP_user_pwd_pop(sk) ((SRP_user_pwd *)OPENSSL_sk_pop(ossl_check_SRP_user_pwd_sk_type(sk)))
101
+ #define sk_SRP_user_pwd_shift(sk) ((SRP_user_pwd *)OPENSSL_sk_shift(ossl_check_SRP_user_pwd_sk_type(sk)))
102
+ #define sk_SRP_user_pwd_pop_free(sk, freefunc) OPENSSL_sk_pop_free(ossl_check_SRP_user_pwd_sk_type(sk),ossl_check_SRP_user_pwd_freefunc_type(freefunc))
103
+ #define sk_SRP_user_pwd_insert(sk, ptr, idx) OPENSSL_sk_insert(ossl_check_SRP_user_pwd_sk_type(sk), ossl_check_SRP_user_pwd_type(ptr), (idx))
104
+ #define sk_SRP_user_pwd_set(sk, idx, ptr) ((SRP_user_pwd *)OPENSSL_sk_set(ossl_check_SRP_user_pwd_sk_type(sk), (idx), ossl_check_SRP_user_pwd_type(ptr)))
105
+ #define sk_SRP_user_pwd_find(sk, ptr) OPENSSL_sk_find(ossl_check_SRP_user_pwd_sk_type(sk), ossl_check_SRP_user_pwd_type(ptr))
106
+ #define sk_SRP_user_pwd_find_ex(sk, ptr) OPENSSL_sk_find_ex(ossl_check_SRP_user_pwd_sk_type(sk), ossl_check_SRP_user_pwd_type(ptr))
107
+ #define sk_SRP_user_pwd_find_all(sk, ptr, pnum) OPENSSL_sk_find_all(ossl_check_SRP_user_pwd_sk_type(sk), ossl_check_SRP_user_pwd_type(ptr), pnum)
108
+ #define sk_SRP_user_pwd_sort(sk) OPENSSL_sk_sort(ossl_check_SRP_user_pwd_sk_type(sk))
109
+ #define sk_SRP_user_pwd_is_sorted(sk) OPENSSL_sk_is_sorted(ossl_check_const_SRP_user_pwd_sk_type(sk))
110
+ #define sk_SRP_user_pwd_dup(sk) ((STACK_OF(SRP_user_pwd) *)OPENSSL_sk_dup(ossl_check_const_SRP_user_pwd_sk_type(sk)))
111
+ #define sk_SRP_user_pwd_deep_copy(sk, copyfunc, freefunc) ((STACK_OF(SRP_user_pwd) *)OPENSSL_sk_deep_copy(ossl_check_const_SRP_user_pwd_sk_type(sk), ossl_check_SRP_user_pwd_copyfunc_type(copyfunc), ossl_check_SRP_user_pwd_freefunc_type(freefunc)))
112
+ #define sk_SRP_user_pwd_set_cmp_func(sk, cmp) ((sk_SRP_user_pwd_compfunc)OPENSSL_sk_set_cmp_func(ossl_check_SRP_user_pwd_sk_type(sk), ossl_check_SRP_user_pwd_compfunc_type(cmp)))
113
+
114
+
115
+ OSSL_DEPRECATEDIN_3_0
116
+ SRP_user_pwd *SRP_user_pwd_new(void);
117
+ OSSL_DEPRECATEDIN_3_0
118
+ void SRP_user_pwd_free(SRP_user_pwd *user_pwd);
119
+
120
+ OSSL_DEPRECATEDIN_3_0
121
+ void SRP_user_pwd_set_gN(SRP_user_pwd *user_pwd, const BIGNUM *g,
122
+ const BIGNUM *N);
123
+ OSSL_DEPRECATEDIN_3_0
124
+ int SRP_user_pwd_set1_ids(SRP_user_pwd *user_pwd, const char *id,
125
+ const char *info);
126
+ OSSL_DEPRECATEDIN_3_0
127
+ int SRP_user_pwd_set0_sv(SRP_user_pwd *user_pwd, BIGNUM *s, BIGNUM *v);
128
+
129
+ typedef struct SRP_VBASE_st {
130
+ STACK_OF(SRP_user_pwd) *users_pwd;
131
+ STACK_OF(SRP_gN_cache) *gN_cache;
132
+ /* to simulate a user */
133
+ char *seed_key;
134
+ const BIGNUM *default_g;
135
+ const BIGNUM *default_N;
136
+ } SRP_VBASE;
137
+
138
+ /*
139
+ * Internal structure storing N and g pair
140
+ */
141
+ typedef struct SRP_gN_st {
142
+ char *id;
143
+ const BIGNUM *g;
144
+ const BIGNUM *N;
145
+ } SRP_gN;
146
+ SKM_DEFINE_STACK_OF_INTERNAL(SRP_gN, SRP_gN, SRP_gN)
147
+ #define sk_SRP_gN_num(sk) OPENSSL_sk_num(ossl_check_const_SRP_gN_sk_type(sk))
148
+ #define sk_SRP_gN_value(sk, idx) ((SRP_gN *)OPENSSL_sk_value(ossl_check_const_SRP_gN_sk_type(sk), (idx)))
149
+ #define sk_SRP_gN_new(cmp) ((STACK_OF(SRP_gN) *)OPENSSL_sk_new(ossl_check_SRP_gN_compfunc_type(cmp)))
150
+ #define sk_SRP_gN_new_null() ((STACK_OF(SRP_gN) *)OPENSSL_sk_new_null())
151
+ #define sk_SRP_gN_new_reserve(cmp, n) ((STACK_OF(SRP_gN) *)OPENSSL_sk_new_reserve(ossl_check_SRP_gN_compfunc_type(cmp), (n)))
152
+ #define sk_SRP_gN_reserve(sk, n) OPENSSL_sk_reserve(ossl_check_SRP_gN_sk_type(sk), (n))
153
+ #define sk_SRP_gN_free(sk) OPENSSL_sk_free(ossl_check_SRP_gN_sk_type(sk))
154
+ #define sk_SRP_gN_zero(sk) OPENSSL_sk_zero(ossl_check_SRP_gN_sk_type(sk))
155
+ #define sk_SRP_gN_delete(sk, i) ((SRP_gN *)OPENSSL_sk_delete(ossl_check_SRP_gN_sk_type(sk), (i)))
156
+ #define sk_SRP_gN_delete_ptr(sk, ptr) ((SRP_gN *)OPENSSL_sk_delete_ptr(ossl_check_SRP_gN_sk_type(sk), ossl_check_SRP_gN_type(ptr)))
157
+ #define sk_SRP_gN_push(sk, ptr) OPENSSL_sk_push(ossl_check_SRP_gN_sk_type(sk), ossl_check_SRP_gN_type(ptr))
158
+ #define sk_SRP_gN_unshift(sk, ptr) OPENSSL_sk_unshift(ossl_check_SRP_gN_sk_type(sk), ossl_check_SRP_gN_type(ptr))
159
+ #define sk_SRP_gN_pop(sk) ((SRP_gN *)OPENSSL_sk_pop(ossl_check_SRP_gN_sk_type(sk)))
160
+ #define sk_SRP_gN_shift(sk) ((SRP_gN *)OPENSSL_sk_shift(ossl_check_SRP_gN_sk_type(sk)))
161
+ #define sk_SRP_gN_pop_free(sk, freefunc) OPENSSL_sk_pop_free(ossl_check_SRP_gN_sk_type(sk),ossl_check_SRP_gN_freefunc_type(freefunc))
162
+ #define sk_SRP_gN_insert(sk, ptr, idx) OPENSSL_sk_insert(ossl_check_SRP_gN_sk_type(sk), ossl_check_SRP_gN_type(ptr), (idx))
163
+ #define sk_SRP_gN_set(sk, idx, ptr) ((SRP_gN *)OPENSSL_sk_set(ossl_check_SRP_gN_sk_type(sk), (idx), ossl_check_SRP_gN_type(ptr)))
164
+ #define sk_SRP_gN_find(sk, ptr) OPENSSL_sk_find(ossl_check_SRP_gN_sk_type(sk), ossl_check_SRP_gN_type(ptr))
165
+ #define sk_SRP_gN_find_ex(sk, ptr) OPENSSL_sk_find_ex(ossl_check_SRP_gN_sk_type(sk), ossl_check_SRP_gN_type(ptr))
166
+ #define sk_SRP_gN_find_all(sk, ptr, pnum) OPENSSL_sk_find_all(ossl_check_SRP_gN_sk_type(sk), ossl_check_SRP_gN_type(ptr), pnum)
167
+ #define sk_SRP_gN_sort(sk) OPENSSL_sk_sort(ossl_check_SRP_gN_sk_type(sk))
168
+ #define sk_SRP_gN_is_sorted(sk) OPENSSL_sk_is_sorted(ossl_check_const_SRP_gN_sk_type(sk))
169
+ #define sk_SRP_gN_dup(sk) ((STACK_OF(SRP_gN) *)OPENSSL_sk_dup(ossl_check_const_SRP_gN_sk_type(sk)))
170
+ #define sk_SRP_gN_deep_copy(sk, copyfunc, freefunc) ((STACK_OF(SRP_gN) *)OPENSSL_sk_deep_copy(ossl_check_const_SRP_gN_sk_type(sk), ossl_check_SRP_gN_copyfunc_type(copyfunc), ossl_check_SRP_gN_freefunc_type(freefunc)))
171
+ #define sk_SRP_gN_set_cmp_func(sk, cmp) ((sk_SRP_gN_compfunc)OPENSSL_sk_set_cmp_func(ossl_check_SRP_gN_sk_type(sk), ossl_check_SRP_gN_compfunc_type(cmp)))
172
+
173
+
174
+
175
+ OSSL_DEPRECATEDIN_3_0
176
+ SRP_VBASE *SRP_VBASE_new(char *seed_key);
177
+ OSSL_DEPRECATEDIN_3_0
178
+ void SRP_VBASE_free(SRP_VBASE *vb);
179
+ OSSL_DEPRECATEDIN_3_0
180
+ int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file);
181
+
182
+ OSSL_DEPRECATEDIN_3_0
183
+ int SRP_VBASE_add0_user(SRP_VBASE *vb, SRP_user_pwd *user_pwd);
184
+
185
+ /* NOTE: unlike in SRP_VBASE_get_by_user, caller owns the returned pointer.*/
186
+ OSSL_DEPRECATEDIN_3_0
187
+ SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username);
188
+
189
+ OSSL_DEPRECATEDIN_3_0
190
+ char *SRP_create_verifier_ex(const char *user, const char *pass, char **salt,
191
+ char **verifier, const char *N, const char *g,
192
+ OSSL_LIB_CTX *libctx, const char *propq);
193
+ OSSL_DEPRECATEDIN_3_0
194
+ char *SRP_create_verifier(const char *user, const char *pass, char **salt,
195
+ char **verifier, const char *N, const char *g);
196
+ OSSL_DEPRECATEDIN_3_0
197
+ int SRP_create_verifier_BN_ex(const char *user, const char *pass, BIGNUM **salt,
198
+ BIGNUM **verifier, const BIGNUM *N,
199
+ const BIGNUM *g, OSSL_LIB_CTX *libctx,
200
+ const char *propq);
201
+ OSSL_DEPRECATEDIN_3_0
202
+ int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
203
+ BIGNUM **verifier, const BIGNUM *N,
204
+ const BIGNUM *g);
205
+
206
+ # define SRP_NO_ERROR 0
207
+ # define SRP_ERR_VBASE_INCOMPLETE_FILE 1
208
+ # define SRP_ERR_VBASE_BN_LIB 2
209
+ # define SRP_ERR_OPEN_FILE 3
210
+ # define SRP_ERR_MEMORY 4
211
+
212
+ # define DB_srptype 0
213
+ # define DB_srpverifier 1
214
+ # define DB_srpsalt 2
215
+ # define DB_srpid 3
216
+ # define DB_srpgN 4
217
+ # define DB_srpinfo 5
218
+ # undef DB_NUMBER
219
+ # define DB_NUMBER 6
220
+
221
+ # define DB_SRP_INDEX 'I'
222
+ # define DB_SRP_VALID 'V'
223
+ # define DB_SRP_REVOKED 'R'
224
+ # define DB_SRP_MODIF 'v'
225
+
226
+ /* see srp.c */
227
+ OSSL_DEPRECATEDIN_3_0
228
+ char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N);
229
+ OSSL_DEPRECATEDIN_3_0
230
+ SRP_gN *SRP_get_default_gN(const char *id);
231
+
232
+ /* server side .... */
233
+ OSSL_DEPRECATEDIN_3_0
234
+ BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
235
+ const BIGNUM *b, const BIGNUM *N);
236
+ OSSL_DEPRECATEDIN_3_0
237
+ BIGNUM *SRP_Calc_B_ex(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
238
+ const BIGNUM *v, OSSL_LIB_CTX *libctx, const char *propq);
239
+ OSSL_DEPRECATEDIN_3_0
240
+ BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
241
+ const BIGNUM *v);
242
+
243
+ OSSL_DEPRECATEDIN_3_0
244
+ int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N);
245
+ OSSL_DEPRECATEDIN_3_0
246
+ BIGNUM *SRP_Calc_u_ex(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N,
247
+ OSSL_LIB_CTX *libctx, const char *propq);
248
+ OSSL_DEPRECATEDIN_3_0
249
+ BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N);
250
+
251
+ /* client side .... */
252
+
253
+ OSSL_DEPRECATEDIN_3_0
254
+ BIGNUM *SRP_Calc_x_ex(const BIGNUM *s, const char *user, const char *pass,
255
+ OSSL_LIB_CTX *libctx, const char *propq);
256
+ OSSL_DEPRECATEDIN_3_0
257
+ BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass);
258
+ OSSL_DEPRECATEDIN_3_0
259
+ BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g);
260
+ OSSL_DEPRECATEDIN_3_0
261
+ BIGNUM *SRP_Calc_client_key_ex(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
262
+ const BIGNUM *x, const BIGNUM *a, const BIGNUM *u,
263
+ OSSL_LIB_CTX *libctx, const char *propq);
264
+ OSSL_DEPRECATEDIN_3_0
265
+ BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
266
+ const BIGNUM *x, const BIGNUM *a, const BIGNUM *u);
267
+ OSSL_DEPRECATEDIN_3_0
268
+ int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N);
269
+
270
+ # define SRP_MINIMAL_N 1024
271
+
272
+ # endif /* OPENSSL_NO_DEPRECATED_3_0 */
273
+
274
+ /* This method ignores the configured seed and fails for an unknown user. */
275
+ # ifndef OPENSSL_NO_DEPRECATED_1_1_0
276
+ OSSL_DEPRECATEDIN_1_1_0
277
+ SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username);
278
+ # endif
279
+
280
+ # ifdef __cplusplus
281
+ }
282
+ # endif
283
+ # endif
284
+
285
+ #endif
openflamingo/lib/python3.10/site-packages/sympy/functions/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (5.82 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # Stub __init__.py for sympy.functions.combinatorial
openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/tests/__init__.py ADDED
File without changes
openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/tests/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (193 Bytes). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/tests/__pycache__/test_comb_numbers.cpython-310.pyc ADDED
Binary file (50.4 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/tests/test_comb_factorials.py ADDED
@@ -0,0 +1,653 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.concrete.products import Product
2
+ from sympy.core.function import expand_func
3
+ from sympy.core.mod import Mod
4
+ from sympy.core.mul import Mul
5
+ from sympy.core import EulerGamma
6
+ from sympy.core.numbers import (Float, I, Rational, nan, oo, pi, zoo)
7
+ from sympy.core.relational import Eq
8
+ from sympy.core.singleton import S
9
+ from sympy.core.symbol import (Dummy, Symbol, symbols)
10
+ from sympy.functions.combinatorial.factorials import (ff, rf, binomial, factorial, factorial2)
11
+ from sympy.functions.elementary.miscellaneous import sqrt
12
+ from sympy.functions.elementary.piecewise import Piecewise
13
+ from sympy.functions.special.gamma_functions import (gamma, polygamma)
14
+ from sympy.polys.polytools import Poly
15
+ from sympy.series.order import O
16
+ from sympy.simplify.simplify import simplify
17
+ from sympy.core.expr import unchanged
18
+ from sympy.core.function import ArgumentIndexError
19
+ from sympy.functions.combinatorial.factorials import subfactorial
20
+ from sympy.functions.special.gamma_functions import uppergamma
21
+ from sympy.testing.pytest import XFAIL, raises, slow
22
+
23
+ #Solves and Fixes Issue #10388 - This is the updated test for the same solved issue
24
+
25
+ def test_rf_eval_apply():
26
+ x, y = symbols('x,y')
27
+ n, k = symbols('n k', integer=True)
28
+ m = Symbol('m', integer=True, nonnegative=True)
29
+
30
+ assert rf(nan, y) is nan
31
+ assert rf(x, nan) is nan
32
+
33
+ assert unchanged(rf, x, y)
34
+
35
+ assert rf(oo, 0) == 1
36
+ assert rf(-oo, 0) == 1
37
+
38
+ assert rf(oo, 6) is oo
39
+ assert rf(-oo, 7) is -oo
40
+ assert rf(-oo, 6) is oo
41
+
42
+ assert rf(oo, -6) is oo
43
+ assert rf(-oo, -7) is oo
44
+
45
+ assert rf(-1, pi) == 0
46
+ assert rf(-5, 1 + I) == 0
47
+
48
+ assert unchanged(rf, -3, k)
49
+ assert unchanged(rf, x, Symbol('k', integer=False))
50
+ assert rf(-3, Symbol('k', integer=False)) == 0
51
+ assert rf(Symbol('x', negative=True, integer=True), Symbol('k', integer=False)) == 0
52
+
53
+ assert rf(x, 0) == 1
54
+ assert rf(x, 1) == x
55
+ assert rf(x, 2) == x*(x + 1)
56
+ assert rf(x, 3) == x*(x + 1)*(x + 2)
57
+ assert rf(x, 5) == x*(x + 1)*(x + 2)*(x + 3)*(x + 4)
58
+
59
+ assert rf(x, -1) == 1/(x - 1)
60
+ assert rf(x, -2) == 1/((x - 1)*(x - 2))
61
+ assert rf(x, -3) == 1/((x - 1)*(x - 2)*(x - 3))
62
+
63
+ assert rf(1, 100) == factorial(100)
64
+
65
+ assert rf(x**2 + 3*x, 2) == (x**2 + 3*x)*(x**2 + 3*x + 1)
66
+ assert isinstance(rf(x**2 + 3*x, 2), Mul)
67
+ assert rf(x**3 + x, -2) == 1/((x**3 + x - 1)*(x**3 + x - 2))
68
+
69
+ assert rf(Poly(x**2 + 3*x, x), 2) == Poly(x**4 + 8*x**3 + 19*x**2 + 12*x, x)
70
+ assert isinstance(rf(Poly(x**2 + 3*x, x), 2), Poly)
71
+ raises(ValueError, lambda: rf(Poly(x**2 + 3*x, x, y), 2))
72
+ assert rf(Poly(x**3 + x, x), -2) == 1/(x**6 - 9*x**5 + 35*x**4 - 75*x**3 + 94*x**2 - 66*x + 20)
73
+ raises(ValueError, lambda: rf(Poly(x**3 + x, x, y), -2))
74
+
75
+ assert rf(x, m).is_integer is None
76
+ assert rf(n, k).is_integer is None
77
+ assert rf(n, m).is_integer is True
78
+ assert rf(n, k + pi).is_integer is False
79
+ assert rf(n, m + pi).is_integer is False
80
+ assert rf(pi, m).is_integer is False
81
+
82
+ def check(x, k, o, n):
83
+ a, b = Dummy(), Dummy()
84
+ r = lambda x, k: o(a, b).rewrite(n).subs({a:x,b:k})
85
+ for i in range(-5,5):
86
+ for j in range(-5,5):
87
+ assert o(i, j) == r(i, j), (o, n, i, j)
88
+ check(x, k, rf, ff)
89
+ check(x, k, rf, binomial)
90
+ check(n, k, rf, factorial)
91
+ check(x, y, rf, factorial)
92
+ check(x, y, rf, binomial)
93
+
94
+ assert rf(x, k).rewrite(ff) == ff(x + k - 1, k)
95
+ assert rf(x, k).rewrite(gamma) == Piecewise(
96
+ (gamma(k + x)/gamma(x), x > 0),
97
+ ((-1)**k*gamma(1 - x)/gamma(-k - x + 1), True))
98
+ assert rf(5, k).rewrite(gamma) == gamma(k + 5)/24
99
+ assert rf(x, k).rewrite(binomial) == factorial(k)*binomial(x + k - 1, k)
100
+ assert rf(n, k).rewrite(factorial) == Piecewise(
101
+ (factorial(k + n - 1)/factorial(n - 1), n > 0),
102
+ ((-1)**k*factorial(-n)/factorial(-k - n), True))
103
+ assert rf(5, k).rewrite(factorial) == factorial(k + 4)/24
104
+ assert rf(x, y).rewrite(factorial) == rf(x, y)
105
+ assert rf(x, y).rewrite(binomial) == rf(x, y)
106
+
107
+ import random
108
+ from mpmath import rf as mpmath_rf
109
+ for i in range(100):
110
+ x = -500 + 500 * random.random()
111
+ k = -500 + 500 * random.random()
112
+ assert (abs(mpmath_rf(x, k) - rf(x, k)) < 10**(-15))
113
+
114
+
115
+ def test_ff_eval_apply():
116
+ x, y = symbols('x,y')
117
+ n, k = symbols('n k', integer=True)
118
+ m = Symbol('m', integer=True, nonnegative=True)
119
+
120
+ assert ff(nan, y) is nan
121
+ assert ff(x, nan) is nan
122
+
123
+ assert unchanged(ff, x, y)
124
+
125
+ assert ff(oo, 0) == 1
126
+ assert ff(-oo, 0) == 1
127
+
128
+ assert ff(oo, 6) is oo
129
+ assert ff(-oo, 7) is -oo
130
+ assert ff(-oo, 6) is oo
131
+
132
+ assert ff(oo, -6) is oo
133
+ assert ff(-oo, -7) is oo
134
+
135
+ assert ff(x, 0) == 1
136
+ assert ff(x, 1) == x
137
+ assert ff(x, 2) == x*(x - 1)
138
+ assert ff(x, 3) == x*(x - 1)*(x - 2)
139
+ assert ff(x, 5) == x*(x - 1)*(x - 2)*(x - 3)*(x - 4)
140
+
141
+ assert ff(x, -1) == 1/(x + 1)
142
+ assert ff(x, -2) == 1/((x + 1)*(x + 2))
143
+ assert ff(x, -3) == 1/((x + 1)*(x + 2)*(x + 3))
144
+
145
+ assert ff(100, 100) == factorial(100)
146
+
147
+ assert ff(2*x**2 - 5*x, 2) == (2*x**2 - 5*x)*(2*x**2 - 5*x - 1)
148
+ assert isinstance(ff(2*x**2 - 5*x, 2), Mul)
149
+ assert ff(x**2 + 3*x, -2) == 1/((x**2 + 3*x + 1)*(x**2 + 3*x + 2))
150
+
151
+ assert ff(Poly(2*x**2 - 5*x, x), 2) == Poly(4*x**4 - 28*x**3 + 59*x**2 - 35*x, x)
152
+ assert isinstance(ff(Poly(2*x**2 - 5*x, x), 2), Poly)
153
+ raises(ValueError, lambda: ff(Poly(2*x**2 - 5*x, x, y), 2))
154
+ assert ff(Poly(x**2 + 3*x, x), -2) == 1/(x**4 + 12*x**3 + 49*x**2 + 78*x + 40)
155
+ raises(ValueError, lambda: ff(Poly(x**2 + 3*x, x, y), -2))
156
+
157
+
158
+ assert ff(x, m).is_integer is None
159
+ assert ff(n, k).is_integer is None
160
+ assert ff(n, m).is_integer is True
161
+ assert ff(n, k + pi).is_integer is False
162
+ assert ff(n, m + pi).is_integer is False
163
+ assert ff(pi, m).is_integer is False
164
+
165
+ assert isinstance(ff(x, x), ff)
166
+ assert ff(n, n) == factorial(n)
167
+
168
+ def check(x, k, o, n):
169
+ a, b = Dummy(), Dummy()
170
+ r = lambda x, k: o(a, b).rewrite(n).subs({a:x,b:k})
171
+ for i in range(-5,5):
172
+ for j in range(-5,5):
173
+ assert o(i, j) == r(i, j), (o, n)
174
+ check(x, k, ff, rf)
175
+ check(x, k, ff, gamma)
176
+ check(n, k, ff, factorial)
177
+ check(x, k, ff, binomial)
178
+ check(x, y, ff, factorial)
179
+ check(x, y, ff, binomial)
180
+
181
+ assert ff(x, k).rewrite(rf) == rf(x - k + 1, k)
182
+ assert ff(x, k).rewrite(gamma) == Piecewise(
183
+ (gamma(x + 1)/gamma(-k + x + 1), x >= 0),
184
+ ((-1)**k*gamma(k - x)/gamma(-x), True))
185
+ assert ff(5, k).rewrite(gamma) == 120/gamma(6 - k)
186
+ assert ff(n, k).rewrite(factorial) == Piecewise(
187
+ (factorial(n)/factorial(-k + n), n >= 0),
188
+ ((-1)**k*factorial(k - n - 1)/factorial(-n - 1), True))
189
+ assert ff(5, k).rewrite(factorial) == 120/factorial(5 - k)
190
+ assert ff(x, k).rewrite(binomial) == factorial(k) * binomial(x, k)
191
+ assert ff(x, y).rewrite(factorial) == ff(x, y)
192
+ assert ff(x, y).rewrite(binomial) == ff(x, y)
193
+
194
+ import random
195
+ from mpmath import ff as mpmath_ff
196
+ for i in range(100):
197
+ x = -500 + 500 * random.random()
198
+ k = -500 + 500 * random.random()
199
+ a = mpmath_ff(x, k)
200
+ b = ff(x, k)
201
+ assert (abs(a - b) < abs(a) * 10**(-15))
202
+
203
+
204
+ def test_rf_ff_eval_hiprec():
205
+ maple = Float('6.9109401292234329956525265438452')
206
+ us = ff(18, Rational(2, 3)).evalf(32)
207
+ assert abs(us - maple)/us < 1e-31
208
+
209
+ maple = Float('6.8261540131125511557924466355367')
210
+ us = rf(18, Rational(2, 3)).evalf(32)
211
+ assert abs(us - maple)/us < 1e-31
212
+
213
+ maple = Float('34.007346127440197150854651814225')
214
+ us = rf(Float('4.4', 32), Float('2.2', 32));
215
+ assert abs(us - maple)/us < 1e-31
216
+
217
+
218
+ def test_rf_lambdify_mpmath():
219
+ from sympy.utilities.lambdify import lambdify
220
+ x, y = symbols('x,y')
221
+ f = lambdify((x,y), rf(x, y), 'mpmath')
222
+ maple = Float('34.007346127440197')
223
+ us = f(4.4, 2.2)
224
+ assert abs(us - maple)/us < 1e-15
225
+
226
+
227
+ def test_factorial():
228
+ x = Symbol('x')
229
+ n = Symbol('n', integer=True)
230
+ k = Symbol('k', integer=True, nonnegative=True)
231
+ r = Symbol('r', integer=False)
232
+ s = Symbol('s', integer=False, negative=True)
233
+ t = Symbol('t', nonnegative=True)
234
+ u = Symbol('u', noninteger=True)
235
+
236
+ assert factorial(-2) is zoo
237
+ assert factorial(0) == 1
238
+ assert factorial(7) == 5040
239
+ assert factorial(19) == 121645100408832000
240
+ assert factorial(31) == 8222838654177922817725562880000000
241
+ assert factorial(n).func == factorial
242
+ assert factorial(2*n).func == factorial
243
+
244
+ assert factorial(x).is_integer is None
245
+ assert factorial(n).is_integer is None
246
+ assert factorial(k).is_integer
247
+ assert factorial(r).is_integer is None
248
+
249
+ assert factorial(n).is_positive is None
250
+ assert factorial(k).is_positive
251
+
252
+ assert factorial(x).is_real is None
253
+ assert factorial(n).is_real is None
254
+ assert factorial(k).is_real is True
255
+ assert factorial(r).is_real is None
256
+ assert factorial(s).is_real is True
257
+ assert factorial(t).is_real is True
258
+ assert factorial(u).is_real is True
259
+
260
+ assert factorial(x).is_composite is None
261
+ assert factorial(n).is_composite is None
262
+ assert factorial(k).is_composite is None
263
+ assert factorial(k + 3).is_composite is True
264
+ assert factorial(r).is_composite is None
265
+ assert factorial(s).is_composite is None
266
+ assert factorial(t).is_composite is None
267
+ assert factorial(u).is_composite is None
268
+
269
+ assert factorial(oo) is oo
270
+
271
+
272
+ def test_factorial_Mod():
273
+ pr = Symbol('pr', prime=True)
274
+ p, q = 10**9 + 9, 10**9 + 33 # prime modulo
275
+ r, s = 10**7 + 5, 33333333 # composite modulo
276
+ assert Mod(factorial(pr - 1), pr) == pr - 1
277
+ assert Mod(factorial(pr - 1), -pr) == -1
278
+ assert Mod(factorial(r - 1, evaluate=False), r) == 0
279
+ assert Mod(factorial(s - 1, evaluate=False), s) == 0
280
+ assert Mod(factorial(p - 1, evaluate=False), p) == p - 1
281
+ assert Mod(factorial(q - 1, evaluate=False), q) == q - 1
282
+ assert Mod(factorial(p - 50, evaluate=False), p) == 854928834
283
+ assert Mod(factorial(q - 1800, evaluate=False), q) == 905504050
284
+ assert Mod(factorial(153, evaluate=False), r) == Mod(factorial(153), r)
285
+ assert Mod(factorial(255, evaluate=False), s) == Mod(factorial(255), s)
286
+ assert Mod(factorial(4, evaluate=False), 3) == S.Zero
287
+ assert Mod(factorial(5, evaluate=False), 6) == S.Zero
288
+
289
+
290
+ def test_factorial_diff():
291
+ n = Symbol('n', integer=True)
292
+
293
+ assert factorial(n).diff(n) == \
294
+ gamma(1 + n)*polygamma(0, 1 + n)
295
+ assert factorial(n**2).diff(n) == \
296
+ 2*n*gamma(1 + n**2)*polygamma(0, 1 + n**2)
297
+ raises(ArgumentIndexError, lambda: factorial(n**2).fdiff(2))
298
+
299
+
300
+ def test_factorial_series():
301
+ n = Symbol('n', integer=True)
302
+
303
+ assert factorial(n).series(n, 0, 3) == \
304
+ 1 - n*EulerGamma + n**2*(EulerGamma**2/2 + pi**2/12) + O(n**3)
305
+
306
+
307
+ def test_factorial_rewrite():
308
+ n = Symbol('n', integer=True)
309
+ k = Symbol('k', integer=True, nonnegative=True)
310
+
311
+ assert factorial(n).rewrite(gamma) == gamma(n + 1)
312
+ _i = Dummy('i')
313
+ assert factorial(k).rewrite(Product).dummy_eq(Product(_i, (_i, 1, k)))
314
+ assert factorial(n).rewrite(Product) == factorial(n)
315
+
316
+
317
+ def test_factorial2():
318
+ n = Symbol('n', integer=True)
319
+
320
+ assert factorial2(-1) == 1
321
+ assert factorial2(0) == 1
322
+ assert factorial2(7) == 105
323
+ assert factorial2(8) == 384
324
+
325
+ # The following is exhaustive
326
+ tt = Symbol('tt', integer=True, nonnegative=True)
327
+ tte = Symbol('tte', even=True, nonnegative=True)
328
+ tpe = Symbol('tpe', even=True, positive=True)
329
+ tto = Symbol('tto', odd=True, nonnegative=True)
330
+ tf = Symbol('tf', integer=True, nonnegative=False)
331
+ tfe = Symbol('tfe', even=True, nonnegative=False)
332
+ tfo = Symbol('tfo', odd=True, nonnegative=False)
333
+ ft = Symbol('ft', integer=False, nonnegative=True)
334
+ ff = Symbol('ff', integer=False, nonnegative=False)
335
+ fn = Symbol('fn', integer=False)
336
+ nt = Symbol('nt', nonnegative=True)
337
+ nf = Symbol('nf', nonnegative=False)
338
+ nn = Symbol('nn')
339
+ z = Symbol('z', zero=True)
340
+ #Solves and Fixes Issue #10388 - This is the updated test for the same solved issue
341
+ raises(ValueError, lambda: factorial2(oo))
342
+ raises(ValueError, lambda: factorial2(Rational(5, 2)))
343
+ raises(ValueError, lambda: factorial2(-4))
344
+ assert factorial2(n).is_integer is None
345
+ assert factorial2(tt - 1).is_integer
346
+ assert factorial2(tte - 1).is_integer
347
+ assert factorial2(tpe - 3).is_integer
348
+ assert factorial2(tto - 4).is_integer
349
+ assert factorial2(tto - 2).is_integer
350
+ assert factorial2(tf).is_integer is None
351
+ assert factorial2(tfe).is_integer is None
352
+ assert factorial2(tfo).is_integer is None
353
+ assert factorial2(ft).is_integer is None
354
+ assert factorial2(ff).is_integer is None
355
+ assert factorial2(fn).is_integer is None
356
+ assert factorial2(nt).is_integer is None
357
+ assert factorial2(nf).is_integer is None
358
+ assert factorial2(nn).is_integer is None
359
+
360
+ assert factorial2(n).is_positive is None
361
+ assert factorial2(tt - 1).is_positive is True
362
+ assert factorial2(tte - 1).is_positive is True
363
+ assert factorial2(tpe - 3).is_positive is True
364
+ assert factorial2(tpe - 1).is_positive is True
365
+ assert factorial2(tto - 2).is_positive is True
366
+ assert factorial2(tto - 1).is_positive is True
367
+ assert factorial2(tf).is_positive is None
368
+ assert factorial2(tfe).is_positive is None
369
+ assert factorial2(tfo).is_positive is None
370
+ assert factorial2(ft).is_positive is None
371
+ assert factorial2(ff).is_positive is None
372
+ assert factorial2(fn).is_positive is None
373
+ assert factorial2(nt).is_positive is None
374
+ assert factorial2(nf).is_positive is None
375
+ assert factorial2(nn).is_positive is None
376
+
377
+ assert factorial2(tt).is_even is None
378
+ assert factorial2(tt).is_odd is None
379
+ assert factorial2(tte).is_even is None
380
+ assert factorial2(tte).is_odd is None
381
+ assert factorial2(tte + 2).is_even is True
382
+ assert factorial2(tpe).is_even is True
383
+ assert factorial2(tpe).is_odd is False
384
+ assert factorial2(tto).is_odd is True
385
+ assert factorial2(tf).is_even is None
386
+ assert factorial2(tf).is_odd is None
387
+ assert factorial2(tfe).is_even is None
388
+ assert factorial2(tfe).is_odd is None
389
+ assert factorial2(tfo).is_even is False
390
+ assert factorial2(tfo).is_odd is None
391
+ assert factorial2(z).is_even is False
392
+ assert factorial2(z).is_odd is True
393
+
394
+
395
+ def test_factorial2_rewrite():
396
+ n = Symbol('n', integer=True)
397
+ assert factorial2(n).rewrite(gamma) == \
398
+ 2**(n/2)*Piecewise((1, Eq(Mod(n, 2), 0)), (sqrt(2)/sqrt(pi), Eq(Mod(n, 2), 1)))*gamma(n/2 + 1)
399
+ assert factorial2(2*n).rewrite(gamma) == 2**n*gamma(n + 1)
400
+ assert factorial2(2*n + 1).rewrite(gamma) == \
401
+ sqrt(2)*2**(n + S.Half)*gamma(n + Rational(3, 2))/sqrt(pi)
402
+
403
+
404
+ def test_binomial():
405
+ x = Symbol('x')
406
+ n = Symbol('n', integer=True)
407
+ nz = Symbol('nz', integer=True, nonzero=True)
408
+ k = Symbol('k', integer=True)
409
+ kp = Symbol('kp', integer=True, positive=True)
410
+ kn = Symbol('kn', integer=True, negative=True)
411
+ u = Symbol('u', negative=True)
412
+ v = Symbol('v', nonnegative=True)
413
+ p = Symbol('p', positive=True)
414
+ z = Symbol('z', zero=True)
415
+ nt = Symbol('nt', integer=False)
416
+ kt = Symbol('kt', integer=False)
417
+ a = Symbol('a', integer=True, nonnegative=True)
418
+ b = Symbol('b', integer=True, nonnegative=True)
419
+
420
+ assert binomial(0, 0) == 1
421
+ assert binomial(1, 1) == 1
422
+ assert binomial(10, 10) == 1
423
+ assert binomial(n, z) == 1
424
+ assert binomial(1, 2) == 0
425
+ assert binomial(-1, 2) == 1
426
+ assert binomial(1, -1) == 0
427
+ assert binomial(-1, 1) == -1
428
+ assert binomial(-1, -1) == 0
429
+ assert binomial(S.Half, S.Half) == 1
430
+ assert binomial(-10, 1) == -10
431
+ assert binomial(-10, 7) == -11440
432
+ assert binomial(n, -1) == 0 # holds for all integers (negative, zero, positive)
433
+ assert binomial(kp, -1) == 0
434
+ assert binomial(nz, 0) == 1
435
+ assert expand_func(binomial(n, 1)) == n
436
+ assert expand_func(binomial(n, 2)) == n*(n - 1)/2
437
+ assert expand_func(binomial(n, n - 2)) == n*(n - 1)/2
438
+ assert expand_func(binomial(n, n - 1)) == n
439
+ assert binomial(n, 3).func == binomial
440
+ assert binomial(n, 3).expand(func=True) == n**3/6 - n**2/2 + n/3
441
+ assert expand_func(binomial(n, 3)) == n*(n - 2)*(n - 1)/6
442
+ assert binomial(n, n).func == binomial # e.g. (-1, -1) == 0, (2, 2) == 1
443
+ assert binomial(n, n + 1).func == binomial # e.g. (-1, 0) == 1
444
+ assert binomial(kp, kp + 1) == 0
445
+ assert binomial(kn, kn) == 0 # issue #14529
446
+ assert binomial(n, u).func == binomial
447
+ assert binomial(kp, u).func == binomial
448
+ assert binomial(n, p).func == binomial
449
+ assert binomial(n, k).func == binomial
450
+ assert binomial(n, n + p).func == binomial
451
+ assert binomial(kp, kp + p).func == binomial
452
+
453
+ assert expand_func(binomial(n, n - 3)) == n*(n - 2)*(n - 1)/6
454
+
455
+ assert binomial(n, k).is_integer
456
+ assert binomial(nt, k).is_integer is None
457
+ assert binomial(x, nt).is_integer is False
458
+
459
+ assert binomial(gamma(25), 6) == 79232165267303928292058750056084441948572511312165380965440075720159859792344339983120618959044048198214221915637090855535036339620413440000
460
+ assert binomial(1324, 47) == 906266255662694632984994480774946083064699457235920708992926525848438478406790323869952
461
+ assert binomial(1735, 43) == 190910140420204130794758005450919715396159959034348676124678207874195064798202216379800
462
+ assert binomial(2512, 53) == 213894469313832631145798303740098720367984955243020898718979538096223399813295457822575338958939834177325304000
463
+ assert binomial(3383, 52) == 27922807788818096863529701501764372757272890613101645521813434902890007725667814813832027795881839396839287659777235
464
+ assert binomial(4321, 51) == 124595639629264868916081001263541480185227731958274383287107643816863897851139048158022599533438936036467601690983780576
465
+
466
+ assert binomial(a, b).is_nonnegative is True
467
+ assert binomial(-1, 2, evaluate=False).is_nonnegative is True
468
+ assert binomial(10, 5, evaluate=False).is_nonnegative is True
469
+ assert binomial(10, -3, evaluate=False).is_nonnegative is True
470
+ assert binomial(-10, -3, evaluate=False).is_nonnegative is True
471
+ assert binomial(-10, 2, evaluate=False).is_nonnegative is True
472
+ assert binomial(-10, 1, evaluate=False).is_nonnegative is False
473
+ assert binomial(-10, 7, evaluate=False).is_nonnegative is False
474
+
475
+ # issue #14625
476
+ for _ in (pi, -pi, nt, v, a):
477
+ assert binomial(_, _) == 1
478
+ assert binomial(_, _ - 1) == _
479
+ assert isinstance(binomial(u, u), binomial)
480
+ assert isinstance(binomial(u, u - 1), binomial)
481
+ assert isinstance(binomial(x, x), binomial)
482
+ assert isinstance(binomial(x, x - 1), binomial)
483
+
484
+ #issue #18802
485
+ assert expand_func(binomial(x + 1, x)) == x + 1
486
+ assert expand_func(binomial(x, x - 1)) == x
487
+ assert expand_func(binomial(x + 1, x - 1)) == x*(x + 1)/2
488
+ assert expand_func(binomial(x**2 + 1, x**2)) == x**2 + 1
489
+
490
+ # issue #13980 and #13981
491
+ assert binomial(-7, -5) == 0
492
+ assert binomial(-23, -12) == 0
493
+ assert binomial(Rational(13, 2), -10) == 0
494
+ assert binomial(-49, -51) == 0
495
+
496
+ assert binomial(19, Rational(-7, 2)) == S(-68719476736)/(911337863661225*pi)
497
+ assert binomial(0, Rational(3, 2)) == S(-2)/(3*pi)
498
+ assert binomial(-3, Rational(-7, 2)) is zoo
499
+ assert binomial(kn, kt) is zoo
500
+
501
+ assert binomial(nt, kt).func == binomial
502
+ assert binomial(nt, Rational(15, 6)) == 8*gamma(nt + 1)/(15*sqrt(pi)*gamma(nt - Rational(3, 2)))
503
+ assert binomial(Rational(20, 3), Rational(-10, 8)) == gamma(Rational(23, 3))/(gamma(Rational(-1, 4))*gamma(Rational(107, 12)))
504
+ assert binomial(Rational(19, 2), Rational(-7, 2)) == Rational(-1615, 8388608)
505
+ assert binomial(Rational(-13, 5), Rational(-7, 8)) == gamma(Rational(-8, 5))/(gamma(Rational(-29, 40))*gamma(Rational(1, 8)))
506
+ assert binomial(Rational(-19, 8), Rational(-13, 5)) == gamma(Rational(-11, 8))/(gamma(Rational(-8, 5))*gamma(Rational(49, 40)))
507
+
508
+ # binomial for complexes
509
+ assert binomial(I, Rational(-89, 8)) == gamma(1 + I)/(gamma(Rational(-81, 8))*gamma(Rational(97, 8) + I))
510
+ assert binomial(I, 2*I) == gamma(1 + I)/(gamma(1 - I)*gamma(1 + 2*I))
511
+ assert binomial(-7, I) is zoo
512
+ assert binomial(Rational(-7, 6), I) == gamma(Rational(-1, 6))/(gamma(Rational(-1, 6) - I)*gamma(1 + I))
513
+ assert binomial((1+2*I), (1+3*I)) == gamma(2 + 2*I)/(gamma(1 - I)*gamma(2 + 3*I))
514
+ assert binomial(I, 5) == Rational(1, 3) - I/S(12)
515
+ assert binomial((2*I + 3), 7) == -13*I/S(63)
516
+ assert isinstance(binomial(I, n), binomial)
517
+ assert expand_func(binomial(3, 2, evaluate=False)) == 3
518
+ assert expand_func(binomial(n, 0, evaluate=False)) == 1
519
+ assert expand_func(binomial(n, -2, evaluate=False)) == 0
520
+ assert expand_func(binomial(n, k)) == binomial(n, k)
521
+
522
+
523
+ def test_binomial_Mod():
524
+ p, q = 10**5 + 3, 10**9 + 33 # prime modulo
525
+ r = 10**7 + 5 # composite modulo
526
+
527
+ # A few tests to get coverage
528
+ # Lucas Theorem
529
+ assert Mod(binomial(156675, 4433, evaluate=False), p) == Mod(binomial(156675, 4433), p)
530
+
531
+ # factorial Mod
532
+ assert Mod(binomial(1234, 432, evaluate=False), q) == Mod(binomial(1234, 432), q)
533
+
534
+ # binomial factorize
535
+ assert Mod(binomial(253, 113, evaluate=False), r) == Mod(binomial(253, 113), r)
536
+
537
+ # using Granville's generalisation of Lucas' Theorem
538
+ assert Mod(binomial(10**18, 10**12, evaluate=False), p*p) == 3744312326
539
+
540
+
541
+ @slow
542
+ def test_binomial_Mod_slow():
543
+ p, q = 10**5 + 3, 10**9 + 33 # prime modulo
544
+ r, s = 10**7 + 5, 33333333 # composite modulo
545
+
546
+ n, k, m = symbols('n k m')
547
+ assert (binomial(n, k) % q).subs({n: s, k: p}) == Mod(binomial(s, p), q)
548
+ assert (binomial(n, k) % m).subs({n: 8, k: 5, m: 13}) == 4
549
+ assert (binomial(9, k) % 7).subs(k, 2) == 1
550
+
551
+ # Lucas Theorem
552
+ assert Mod(binomial(123456, 43253, evaluate=False), p) == Mod(binomial(123456, 43253), p)
553
+ assert Mod(binomial(-178911, 237, evaluate=False), p) == Mod(-binomial(178911 + 237 - 1, 237), p)
554
+ assert Mod(binomial(-178911, 238, evaluate=False), p) == Mod(binomial(178911 + 238 - 1, 238), p)
555
+
556
+ # factorial Mod
557
+ assert Mod(binomial(9734, 451, evaluate=False), q) == Mod(binomial(9734, 451), q)
558
+ assert Mod(binomial(-10733, 4459, evaluate=False), q) == Mod(binomial(-10733, 4459), q)
559
+ assert Mod(binomial(-15733, 4458, evaluate=False), q) == Mod(binomial(-15733, 4458), q)
560
+ assert Mod(binomial(23, -38, evaluate=False), q) is S.Zero
561
+ assert Mod(binomial(23, 38, evaluate=False), q) is S.Zero
562
+
563
+ # binomial factorize
564
+ assert Mod(binomial(753, 119, evaluate=False), r) == Mod(binomial(753, 119), r)
565
+ assert Mod(binomial(3781, 948, evaluate=False), s) == Mod(binomial(3781, 948), s)
566
+ assert Mod(binomial(25773, 1793, evaluate=False), s) == Mod(binomial(25773, 1793), s)
567
+ assert Mod(binomial(-753, 118, evaluate=False), r) == Mod(binomial(-753, 118), r)
568
+ assert Mod(binomial(-25773, 1793, evaluate=False), s) == Mod(binomial(-25773, 1793), s)
569
+
570
+
571
+ def test_binomial_diff():
572
+ n = Symbol('n', integer=True)
573
+ k = Symbol('k', integer=True)
574
+
575
+ assert binomial(n, k).diff(n) == \
576
+ (-polygamma(0, 1 + n - k) + polygamma(0, 1 + n))*binomial(n, k)
577
+ assert binomial(n**2, k**3).diff(n) == \
578
+ 2*n*(-polygamma(
579
+ 0, 1 + n**2 - k**3) + polygamma(0, 1 + n**2))*binomial(n**2, k**3)
580
+
581
+ assert binomial(n, k).diff(k) == \
582
+ (-polygamma(0, 1 + k) + polygamma(0, 1 + n - k))*binomial(n, k)
583
+ assert binomial(n**2, k**3).diff(k) == \
584
+ 3*k**2*(-polygamma(
585
+ 0, 1 + k**3) + polygamma(0, 1 + n**2 - k**3))*binomial(n**2, k**3)
586
+ raises(ArgumentIndexError, lambda: binomial(n, k).fdiff(3))
587
+
588
+
589
+ def test_binomial_rewrite():
590
+ n = Symbol('n', integer=True)
591
+ k = Symbol('k', integer=True)
592
+ x = Symbol('x')
593
+
594
+ assert binomial(n, k).rewrite(
595
+ factorial) == factorial(n)/(factorial(k)*factorial(n - k))
596
+ assert binomial(
597
+ n, k).rewrite(gamma) == gamma(n + 1)/(gamma(k + 1)*gamma(n - k + 1))
598
+ assert binomial(n, k).rewrite(ff) == ff(n, k) / factorial(k)
599
+ assert binomial(n, x).rewrite(ff) == binomial(n, x)
600
+
601
+
602
+ @XFAIL
603
+ def test_factorial_simplify_fail():
604
+ # simplify(factorial(x + 1).diff(x) - ((x + 1)*factorial(x)).diff(x))) == 0
605
+ from sympy.abc import x
606
+ assert simplify(x*polygamma(0, x + 1) - x*polygamma(0, x + 2) +
607
+ polygamma(0, x + 1) - polygamma(0, x + 2) + 1) == 0
608
+
609
+
610
+ def test_subfactorial():
611
+ assert all(subfactorial(i) == ans for i, ans in enumerate(
612
+ [1, 0, 1, 2, 9, 44, 265, 1854, 14833, 133496]))
613
+ assert subfactorial(oo) is oo
614
+ assert subfactorial(nan) is nan
615
+ assert subfactorial(23) == 9510425471055777937262
616
+ assert unchanged(subfactorial, 2.2)
617
+
618
+ x = Symbol('x')
619
+ assert subfactorial(x).rewrite(uppergamma) == uppergamma(x + 1, -1)/S.Exp1
620
+
621
+ tt = Symbol('tt', integer=True, nonnegative=True)
622
+ tf = Symbol('tf', integer=True, nonnegative=False)
623
+ tn = Symbol('tf', integer=True)
624
+ ft = Symbol('ft', integer=False, nonnegative=True)
625
+ ff = Symbol('ff', integer=False, nonnegative=False)
626
+ fn = Symbol('ff', integer=False)
627
+ nt = Symbol('nt', nonnegative=True)
628
+ nf = Symbol('nf', nonnegative=False)
629
+ nn = Symbol('nf')
630
+ te = Symbol('te', even=True, nonnegative=True)
631
+ to = Symbol('to', odd=True, nonnegative=True)
632
+ assert subfactorial(tt).is_integer
633
+ assert subfactorial(tf).is_integer is None
634
+ assert subfactorial(tn).is_integer is None
635
+ assert subfactorial(ft).is_integer is None
636
+ assert subfactorial(ff).is_integer is None
637
+ assert subfactorial(fn).is_integer is None
638
+ assert subfactorial(nt).is_integer is None
639
+ assert subfactorial(nf).is_integer is None
640
+ assert subfactorial(nn).is_integer is None
641
+ assert subfactorial(tt).is_nonnegative
642
+ assert subfactorial(tf).is_nonnegative is None
643
+ assert subfactorial(tn).is_nonnegative is None
644
+ assert subfactorial(ft).is_nonnegative is None
645
+ assert subfactorial(ff).is_nonnegative is None
646
+ assert subfactorial(fn).is_nonnegative is None
647
+ assert subfactorial(nt).is_nonnegative is None
648
+ assert subfactorial(nf).is_nonnegative is None
649
+ assert subfactorial(nn).is_nonnegative is None
650
+ assert subfactorial(tt).is_even is None
651
+ assert subfactorial(tt).is_odd is None
652
+ assert subfactorial(te).is_odd is True
653
+ assert subfactorial(to).is_even is True
openflamingo/lib/python3.10/site-packages/sympy/functions/combinatorial/tests/test_comb_numbers.py ADDED
@@ -0,0 +1,1241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import string
2
+
3
+ from sympy.concrete.products import Product
4
+ from sympy.concrete.summations import Sum
5
+ from sympy.core.function import (diff, expand_func)
6
+ from sympy.core import (EulerGamma, TribonacciConstant)
7
+ from sympy.core.numbers import (Float, I, Rational, oo, pi)
8
+ from sympy.core.singleton import S
9
+ from sympy.core.symbol import (Dummy, Symbol, symbols)
10
+ from sympy.functions.combinatorial.numbers import carmichael
11
+ from sympy.functions.elementary.complexes import (im, re)
12
+ from sympy.functions.elementary.integers import floor
13
+ from sympy.polys.polytools import cancel
14
+ from sympy.series.limits import limit, Limit
15
+ from sympy.series.order import O
16
+ from sympy.functions import (
17
+ bernoulli, harmonic, bell, fibonacci, tribonacci, lucas, euler, catalan,
18
+ genocchi, andre, partition, divisor_sigma, udivisor_sigma, legendre_symbol,
19
+ jacobi_symbol, kronecker_symbol, mobius,
20
+ primenu, primeomega, totient, reduced_totient, primepi,
21
+ motzkin, binomial, gamma, sqrt, cbrt, hyper, log, digamma,
22
+ trigamma, polygamma, factorial, sin, cos, cot, polylog, zeta, dirichlet_eta)
23
+ from sympy.functions.combinatorial.numbers import _nT
24
+ from sympy.ntheory.factor_ import factorint
25
+
26
+ from sympy.core.expr import unchanged
27
+ from sympy.core.numbers import GoldenRatio, Integer
28
+
29
+ from sympy.testing.pytest import raises, nocache_fail, warns_deprecated_sympy
30
+ from sympy.abc import x
31
+
32
+
33
+ def test_carmichael():
34
+ with warns_deprecated_sympy():
35
+ assert carmichael.is_prime(2821) == False
36
+
37
+
38
+ def test_bernoulli():
39
+ assert bernoulli(0) == 1
40
+ assert bernoulli(1) == Rational(1, 2)
41
+ assert bernoulli(2) == Rational(1, 6)
42
+ assert bernoulli(3) == 0
43
+ assert bernoulli(4) == Rational(-1, 30)
44
+ assert bernoulli(5) == 0
45
+ assert bernoulli(6) == Rational(1, 42)
46
+ assert bernoulli(7) == 0
47
+ assert bernoulli(8) == Rational(-1, 30)
48
+ assert bernoulli(10) == Rational(5, 66)
49
+ assert bernoulli(1000001) == 0
50
+
51
+ assert bernoulli(0, x) == 1
52
+ assert bernoulli(1, x) == x - S.Half
53
+ assert bernoulli(2, x) == x**2 - x + Rational(1, 6)
54
+ assert bernoulli(3, x) == x**3 - (3*x**2)/2 + x/2
55
+
56
+ # Should be fast; computed with mpmath
57
+ b = bernoulli(1000)
58
+ assert b.p % 10**10 == 7950421099
59
+ assert b.q == 342999030
60
+
61
+ b = bernoulli(10**6, evaluate=False).evalf()
62
+ assert str(b) == '-2.23799235765713e+4767529'
63
+
64
+ # Issue #8527
65
+ l = Symbol('l', integer=True)
66
+ m = Symbol('m', integer=True, nonnegative=True)
67
+ n = Symbol('n', integer=True, positive=True)
68
+ assert isinstance(bernoulli(2 * l + 1), bernoulli)
69
+ assert isinstance(bernoulli(2 * m + 1), bernoulli)
70
+ assert bernoulli(2 * n + 1) == 0
71
+
72
+ assert bernoulli(x, 1) == bernoulli(x)
73
+
74
+ assert str(bernoulli(0.0, 2.3).evalf(n=10)) == '1.000000000'
75
+ assert str(bernoulli(1.0).evalf(n=10)) == '0.5000000000'
76
+ assert str(bernoulli(1.2).evalf(n=10)) == '0.4195995367'
77
+ assert str(bernoulli(1.2, 0.8).evalf(n=10)) == '0.2144830348'
78
+ assert str(bernoulli(1.2, -0.8).evalf(n=10)) == '-1.158865646 - 0.6745558744*I'
79
+ assert str(bernoulli(3.0, 1j).evalf(n=10)) == '1.5 - 0.5*I'
80
+ assert str(bernoulli(I).evalf(n=10)) == '0.9268485643 - 0.5821580598*I'
81
+ assert str(bernoulli(I, I).evalf(n=10)) == '0.1267792071 + 0.01947413152*I'
82
+ assert bernoulli(x).evalf() == bernoulli(x)
83
+
84
+
85
+ def test_bernoulli_rewrite():
86
+ from sympy.functions.elementary.piecewise import Piecewise
87
+ n = Symbol('n', integer=True, nonnegative=True)
88
+
89
+ assert bernoulli(-1).rewrite(zeta) == pi**2/6
90
+ assert bernoulli(-2).rewrite(zeta) == 2*zeta(3)
91
+ assert not bernoulli(n, -3).rewrite(zeta).has(harmonic)
92
+ assert bernoulli(-4, x).rewrite(zeta) == 4*zeta(5, x)
93
+ assert isinstance(bernoulli(n, x).rewrite(zeta), Piecewise)
94
+ assert bernoulli(n+1, x).rewrite(zeta) == -(n+1) * zeta(-n, x)
95
+
96
+
97
+ def test_fibonacci():
98
+ assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3]
99
+ assert fibonacci(100) == 354224848179261915075
100
+ assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7]
101
+ assert lucas(100) == 792070839848372253127
102
+
103
+ assert fibonacci(1, x) == 1
104
+ assert fibonacci(2, x) == x
105
+ assert fibonacci(3, x) == x**2 + 1
106
+ assert fibonacci(4, x) == x**3 + 2*x
107
+
108
+ # issue #8800
109
+ n = Dummy('n')
110
+ assert fibonacci(n).limit(n, S.Infinity) is S.Infinity
111
+ assert lucas(n).limit(n, S.Infinity) is S.Infinity
112
+
113
+ assert fibonacci(n).rewrite(sqrt) == \
114
+ 2**(-n)*sqrt(5)*((1 + sqrt(5))**n - (-sqrt(5) + 1)**n) / 5
115
+ assert fibonacci(n).rewrite(sqrt).subs(n, 10).expand() == fibonacci(10)
116
+ assert fibonacci(n).rewrite(GoldenRatio).subs(n,10).evalf() == \
117
+ Float(fibonacci(10))
118
+ assert lucas(n).rewrite(sqrt) == \
119
+ (fibonacci(n-1).rewrite(sqrt) + fibonacci(n+1).rewrite(sqrt)).simplify()
120
+ assert lucas(n).rewrite(sqrt).subs(n, 10).expand() == lucas(10)
121
+ raises(ValueError, lambda: fibonacci(-3, x))
122
+
123
+
124
+ def test_tribonacci():
125
+ assert [tribonacci(n) for n in range(8)] == [0, 1, 1, 2, 4, 7, 13, 24]
126
+ assert tribonacci(100) == 98079530178586034536500564
127
+
128
+ assert tribonacci(0, x) == 0
129
+ assert tribonacci(1, x) == 1
130
+ assert tribonacci(2, x) == x**2
131
+ assert tribonacci(3, x) == x**4 + x
132
+ assert tribonacci(4, x) == x**6 + 2*x**3 + 1
133
+ assert tribonacci(5, x) == x**8 + 3*x**5 + 3*x**2
134
+
135
+ n = Dummy('n')
136
+ assert tribonacci(n).limit(n, S.Infinity) is S.Infinity
137
+
138
+ w = (-1 + S.ImaginaryUnit * sqrt(3)) / 2
139
+ a = (1 + cbrt(19 + 3*sqrt(33)) + cbrt(19 - 3*sqrt(33))) / 3
140
+ b = (1 + w*cbrt(19 + 3*sqrt(33)) + w**2*cbrt(19 - 3*sqrt(33))) / 3
141
+ c = (1 + w**2*cbrt(19 + 3*sqrt(33)) + w*cbrt(19 - 3*sqrt(33))) / 3
142
+ assert tribonacci(n).rewrite(sqrt) == \
143
+ (a**(n + 1)/((a - b)*(a - c))
144
+ + b**(n + 1)/((b - a)*(b - c))
145
+ + c**(n + 1)/((c - a)*(c - b)))
146
+ assert tribonacci(n).rewrite(sqrt).subs(n, 4).simplify() == tribonacci(4)
147
+ assert tribonacci(n).rewrite(GoldenRatio).subs(n,10).evalf() == \
148
+ Float(tribonacci(10))
149
+ assert tribonacci(n).rewrite(TribonacciConstant) == floor(
150
+ 3*TribonacciConstant**n*(102*sqrt(33) + 586)**Rational(1, 3)/
151
+ (-2*(102*sqrt(33) + 586)**Rational(1, 3) + 4 + (102*sqrt(33)
152
+ + 586)**Rational(2, 3)) + S.Half)
153
+ raises(ValueError, lambda: tribonacci(-1, x))
154
+
155
+
156
+ @nocache_fail
157
+ def test_bell():
158
+ assert [bell(n) for n in range(8)] == [1, 1, 2, 5, 15, 52, 203, 877]
159
+
160
+ assert bell(0, x) == 1
161
+ assert bell(1, x) == x
162
+ assert bell(2, x) == x**2 + x
163
+ assert bell(5, x) == x**5 + 10*x**4 + 25*x**3 + 15*x**2 + x
164
+ assert bell(oo) is S.Infinity
165
+ raises(ValueError, lambda: bell(oo, x))
166
+
167
+ raises(ValueError, lambda: bell(-1))
168
+ raises(ValueError, lambda: bell(S.Half))
169
+
170
+ X = symbols('x:6')
171
+ # X = (x0, x1, .. x5)
172
+ # at the same time: X[1] = x1, X[2] = x2 for standard readablity.
173
+ # but we must supply zero-based indexed object X[1:] = (x1, .. x5)
174
+
175
+ assert bell(6, 2, X[1:]) == 6*X[5]*X[1] + 15*X[4]*X[2] + 10*X[3]**2
176
+ assert bell(
177
+ 6, 3, X[1:]) == 15*X[4]*X[1]**2 + 60*X[3]*X[2]*X[1] + 15*X[2]**3
178
+
179
+ X = (1, 10, 100, 1000, 10000)
180
+ assert bell(6, 2, X) == (6 + 15 + 10)*10000
181
+
182
+ X = (1, 2, 3, 3, 5)
183
+ assert bell(6, 2, X) == 6*5 + 15*3*2 + 10*3**2
184
+
185
+ X = (1, 2, 3, 5)
186
+ assert bell(6, 3, X) == 15*5 + 60*3*2 + 15*2**3
187
+
188
+ # Dobinski's formula
189
+ n = Symbol('n', integer=True, nonnegative=True)
190
+ # For large numbers, this is too slow
191
+ # For nonintegers, there are significant precision errors
192
+ for i in [0, 2, 3, 7, 13, 42, 55]:
193
+ # Running without the cache this is either very slow or goes into an
194
+ # infinite loop.
195
+ assert bell(i).evalf() == bell(n).rewrite(Sum).evalf(subs={n: i})
196
+
197
+ m = Symbol("m")
198
+ assert bell(m).rewrite(Sum) == bell(m)
199
+ assert bell(n, m).rewrite(Sum) == bell(n, m)
200
+ # issue 9184
201
+ n = Dummy('n')
202
+ assert bell(n).limit(n, S.Infinity) is S.Infinity
203
+
204
+
205
+ def test_harmonic():
206
+ n = Symbol("n")
207
+ m = Symbol("m")
208
+
209
+ assert harmonic(n, 0) == n
210
+ assert harmonic(n).evalf() == harmonic(n)
211
+ assert harmonic(n, 1) == harmonic(n)
212
+ assert harmonic(1, n) == 1
213
+
214
+ assert harmonic(0, 1) == 0
215
+ assert harmonic(1, 1) == 1
216
+ assert harmonic(2, 1) == Rational(3, 2)
217
+ assert harmonic(3, 1) == Rational(11, 6)
218
+ assert harmonic(4, 1) == Rational(25, 12)
219
+ assert harmonic(0, 2) == 0
220
+ assert harmonic(1, 2) == 1
221
+ assert harmonic(2, 2) == Rational(5, 4)
222
+ assert harmonic(3, 2) == Rational(49, 36)
223
+ assert harmonic(4, 2) == Rational(205, 144)
224
+ assert harmonic(0, 3) == 0
225
+ assert harmonic(1, 3) == 1
226
+ assert harmonic(2, 3) == Rational(9, 8)
227
+ assert harmonic(3, 3) == Rational(251, 216)
228
+ assert harmonic(4, 3) == Rational(2035, 1728)
229
+
230
+ assert harmonic(oo, -1) is S.NaN
231
+ assert harmonic(oo, 0) is oo
232
+ assert harmonic(oo, S.Half) is oo
233
+ assert harmonic(oo, 1) is oo
234
+ assert harmonic(oo, 2) == (pi**2)/6
235
+ assert harmonic(oo, 3) == zeta(3)
236
+ assert harmonic(oo, Dummy(negative=True)) is S.NaN
237
+ ip = Dummy(integer=True, positive=True)
238
+ if (1/ip <= 1) is True: #---------------------------------+
239
+ assert None, 'delete this if-block and the next line' #|
240
+ ip = Dummy(even=True, positive=True) #--------------------+
241
+ assert harmonic(oo, 1/ip) is oo
242
+ assert harmonic(oo, 1 + ip) is zeta(1 + ip)
243
+
244
+ assert harmonic(0, m) == 0
245
+ assert harmonic(-1, -1) == 0
246
+ assert harmonic(-1, 0) == -1
247
+ assert harmonic(-1, 1) is S.ComplexInfinity
248
+ assert harmonic(-1, 2) is S.NaN
249
+ assert harmonic(-3, -2) == -5
250
+ assert harmonic(-3, -3) == 9
251
+
252
+
253
+ def test_harmonic_rational():
254
+ ne = S(6)
255
+ no = S(5)
256
+ pe = S(8)
257
+ po = S(9)
258
+ qe = S(10)
259
+ qo = S(13)
260
+
261
+ Heee = harmonic(ne + pe/qe)
262
+ Aeee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
263
+ + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
264
+ + pi*sqrt(2*sqrt(5)/5 + 1)/2 + Rational(13944145, 4720968))
265
+
266
+ Heeo = harmonic(ne + pe/qo)
267
+ Aeeo = (-log(26) + 2*log(sin(pi*Rational(3, 13)))*cos(pi*Rational(4, 13)) + 2*log(sin(pi*Rational(2, 13)))*cos(pi*Rational(32, 13))
268
+ + 2*log(sin(pi*Rational(5, 13)))*cos(pi*Rational(80, 13)) - 2*log(sin(pi*Rational(6, 13)))*cos(pi*Rational(5, 13))
269
+ - 2*log(sin(pi*Rational(4, 13)))*cos(pi/13) + pi*cot(pi*Rational(5, 13))/2 - 2*log(sin(pi/13))*cos(pi*Rational(3, 13))
270
+ + Rational(2422020029, 702257080))
271
+
272
+ Heoe = harmonic(ne + po/qe)
273
+ Aeoe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4)
274
+ + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
275
+ + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
276
+ + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4)
277
+ + Rational(11818877030, 4286604231) + pi*sqrt(2*sqrt(5) + 5)/2)
278
+
279
+ Heoo = harmonic(ne + po/qo)
280
+ Aeoo = (-log(26) + 2*log(sin(pi*Rational(3, 13)))*cos(pi*Rational(54, 13)) + 2*log(sin(pi*Rational(4, 13)))*cos(pi*Rational(6, 13))
281
+ + 2*log(sin(pi*Rational(6, 13)))*cos(pi*Rational(108, 13)) - 2*log(sin(pi*Rational(5, 13)))*cos(pi/13)
282
+ - 2*log(sin(pi/13))*cos(pi*Rational(5, 13)) + pi*cot(pi*Rational(4, 13))/2
283
+ - 2*log(sin(pi*Rational(2, 13)))*cos(pi*Rational(3, 13)) + Rational(11669332571, 3628714320))
284
+
285
+ Hoee = harmonic(no + pe/qe)
286
+ Aoee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
287
+ + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
288
+ + pi*sqrt(2*sqrt(5)/5 + 1)/2 + Rational(779405, 277704))
289
+
290
+ Hoeo = harmonic(no + pe/qo)
291
+ Aoeo = (-log(26) + 2*log(sin(pi*Rational(3, 13)))*cos(pi*Rational(4, 13)) + 2*log(sin(pi*Rational(2, 13)))*cos(pi*Rational(32, 13))
292
+ + 2*log(sin(pi*Rational(5, 13)))*cos(pi*Rational(80, 13)) - 2*log(sin(pi*Rational(6, 13)))*cos(pi*Rational(5, 13))
293
+ - 2*log(sin(pi*Rational(4, 13)))*cos(pi/13) + pi*cot(pi*Rational(5, 13))/2
294
+ - 2*log(sin(pi/13))*cos(pi*Rational(3, 13)) + Rational(53857323, 16331560))
295
+
296
+ Hooe = harmonic(no + po/qe)
297
+ Aooe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4)
298
+ + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
299
+ + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
300
+ + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4)
301
+ + Rational(486853480, 186374097) + pi*sqrt(2*sqrt(5) + 5)/2)
302
+
303
+ Hooo = harmonic(no + po/qo)
304
+ Aooo = (-log(26) + 2*log(sin(pi*Rational(3, 13)))*cos(pi*Rational(54, 13)) + 2*log(sin(pi*Rational(4, 13)))*cos(pi*Rational(6, 13))
305
+ + 2*log(sin(pi*Rational(6, 13)))*cos(pi*Rational(108, 13)) - 2*log(sin(pi*Rational(5, 13)))*cos(pi/13)
306
+ - 2*log(sin(pi/13))*cos(pi*Rational(5, 13)) + pi*cot(pi*Rational(4, 13))/2
307
+ - 2*log(sin(pi*Rational(2, 13)))*cos(3*pi/13) + Rational(383693479, 125128080))
308
+
309
+ H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo]
310
+ A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo]
311
+ for h, a in zip(H, A):
312
+ e = expand_func(h).doit()
313
+ assert cancel(e/a) == 1
314
+ assert abs(h.n() - a.n()) < 1e-12
315
+
316
+
317
+ def test_harmonic_evalf():
318
+ assert str(harmonic(1.5).evalf(n=10)) == '1.280372306'
319
+ assert str(harmonic(1.5, 2).evalf(n=10)) == '1.154576311' # issue 7443
320
+ assert str(harmonic(4.0, -3).evalf(n=10)) == '100.0000000'
321
+ assert str(harmonic(7.0, 1.0).evalf(n=10)) == '2.592857143'
322
+ assert str(harmonic(1, pi).evalf(n=10)) == '1.000000000'
323
+ assert str(harmonic(2, pi).evalf(n=10)) == '1.113314732'
324
+ assert str(harmonic(1000.0, pi).evalf(n=10)) == '1.176241563'
325
+ assert str(harmonic(I).evalf(n=10)) == '0.6718659855 + 1.076674047*I'
326
+ assert str(harmonic(I, I).evalf(n=10)) == '-0.3970915266 + 1.9629689*I'
327
+
328
+ assert harmonic(-1.0, 1).evalf() is S.NaN
329
+ assert harmonic(-2.0, 2.0).evalf() is S.NaN
330
+
331
+ def test_harmonic_rewrite():
332
+ from sympy.functions.elementary.piecewise import Piecewise
333
+ n = Symbol("n")
334
+ m = Symbol("m", integer=True, positive=True)
335
+ x1 = Symbol("x1", positive=True)
336
+ x2 = Symbol("x2", negative=True)
337
+
338
+ assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma
339
+ assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma
340
+ assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma
341
+
342
+ assert harmonic(n,3).rewrite(polygamma) == polygamma(2, n + 1)/2 - polygamma(2, 1)/2
343
+ assert isinstance(harmonic(n,m).rewrite(polygamma), Piecewise)
344
+
345
+ assert expand_func(harmonic(n+4)) == harmonic(n) + 1/(n + 4) + 1/(n + 3) + 1/(n + 2) + 1/(n + 1)
346
+ assert expand_func(harmonic(n-4)) == harmonic(n) - 1/(n - 1) - 1/(n - 2) - 1/(n - 3) - 1/n
347
+
348
+ assert harmonic(n, m).rewrite("tractable") == harmonic(n, m).rewrite(polygamma)
349
+ assert harmonic(n, x1).rewrite("tractable") == harmonic(n, x1)
350
+ assert harmonic(n, x1 + 1).rewrite("tractable") == zeta(x1 + 1) - zeta(x1 + 1, n + 1)
351
+ assert harmonic(n, x2).rewrite("tractable") == zeta(x2) - zeta(x2, n + 1)
352
+
353
+ _k = Dummy("k")
354
+ assert harmonic(n).rewrite(Sum).dummy_eq(Sum(1/_k, (_k, 1, n)))
355
+ assert harmonic(n, m).rewrite(Sum).dummy_eq(Sum(_k**(-m), (_k, 1, n)))
356
+
357
+
358
+ def test_harmonic_calculus():
359
+ y = Symbol("y", positive=True)
360
+ z = Symbol("z", negative=True)
361
+ assert harmonic(x, 1).limit(x, 0) == 0
362
+ assert harmonic(x, y).limit(x, 0) == 0
363
+ assert harmonic(x, 1).series(x, y, 2) == \
364
+ harmonic(y) + (x - y)*zeta(2, y + 1) + O((x - y)**2, (x, y))
365
+ assert limit(harmonic(x, y), x, oo) == harmonic(oo, y)
366
+ assert limit(harmonic(x, y + 1), x, oo) == zeta(y + 1)
367
+ assert limit(harmonic(x, y - 1), x, oo) == harmonic(oo, y - 1)
368
+ assert limit(harmonic(x, z), x, oo) == Limit(harmonic(x, z), x, oo, dir='-')
369
+ assert limit(harmonic(x, z + 1), x, oo) == oo
370
+ assert limit(harmonic(x, z + 2), x, oo) == harmonic(oo, z + 2)
371
+ assert limit(harmonic(x, z - 1), x, oo) == Limit(harmonic(x, z - 1), x, oo, dir='-')
372
+
373
+
374
+ def test_euler():
375
+ assert euler(0) == 1
376
+ assert euler(1) == 0
377
+ assert euler(2) == -1
378
+ assert euler(3) == 0
379
+ assert euler(4) == 5
380
+ assert euler(6) == -61
381
+ assert euler(8) == 1385
382
+
383
+ assert euler(20, evaluate=False) != 370371188237525
384
+
385
+ n = Symbol('n', integer=True)
386
+ assert euler(n) != -1
387
+ assert euler(n).subs(n, 2) == -1
388
+
389
+ assert euler(-1) == S.Pi / 2
390
+ assert euler(-1, 1) == 2*log(2)
391
+ assert euler(-2).evalf() == (2*S.Catalan).evalf()
392
+ assert euler(-3).evalf() == (S.Pi**3 / 16).evalf()
393
+ assert str(euler(2.3).evalf(n=10)) == '-1.052850274'
394
+ assert str(euler(1.2, 3.4).evalf(n=10)) == '3.575613489'
395
+ assert str(euler(I).evalf(n=10)) == '1.248446443 - 0.7675445124*I'
396
+ assert str(euler(I, I).evalf(n=10)) == '0.04812930469 + 0.01052411008*I'
397
+
398
+ assert euler(20).evalf() == 370371188237525.0
399
+ assert euler(20, evaluate=False).evalf() == 370371188237525.0
400
+
401
+ assert euler(n).rewrite(Sum) == euler(n)
402
+ n = Symbol('n', integer=True, nonnegative=True)
403
+ assert euler(2*n + 1).rewrite(Sum) == 0
404
+ _j = Dummy('j')
405
+ _k = Dummy('k')
406
+ assert euler(2*n).rewrite(Sum).dummy_eq(
407
+ I*Sum((-1)**_j*2**(-_k)*I**(-_k)*(-2*_j + _k)**(2*n + 1)*
408
+ binomial(_k, _j)/_k, (_j, 0, _k), (_k, 1, 2*n + 1)))
409
+
410
+
411
+ def test_euler_odd():
412
+ n = Symbol('n', odd=True, positive=True)
413
+ assert euler(n) == 0
414
+ n = Symbol('n', odd=True)
415
+ assert euler(n) != 0
416
+
417
+
418
+ def test_euler_polynomials():
419
+ assert euler(0, x) == 1
420
+ assert euler(1, x) == x - S.Half
421
+ assert euler(2, x) == x**2 - x
422
+ assert euler(3, x) == x**3 - (3*x**2)/2 + Rational(1, 4)
423
+ m = Symbol('m')
424
+ assert isinstance(euler(m, x), euler)
425
+ from sympy.core.numbers import Float
426
+ A = Float('-0.46237208575048694923364757452876131e8') # from Maple
427
+ B = euler(19, S.Pi).evalf(32)
428
+ assert abs((A - B)/A) < 1e-31
429
+
430
+
431
+ def test_euler_polynomial_rewrite():
432
+ m = Symbol('m')
433
+ A = euler(m, x).rewrite('Sum');
434
+ assert A.subs({m:3, x:5}).doit() == euler(3, 5)
435
+
436
+
437
+ def test_catalan():
438
+ n = Symbol('n', integer=True)
439
+ m = Symbol('m', integer=True, positive=True)
440
+ k = Symbol('k', integer=True, nonnegative=True)
441
+ p = Symbol('p', nonnegative=True)
442
+
443
+ catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786]
444
+ for i, c in enumerate(catalans):
445
+ assert catalan(i) == c
446
+ assert catalan(n).rewrite(factorial).subs(n, i) == c
447
+ assert catalan(n).rewrite(Product).subs(n, i).doit() == c
448
+
449
+ assert unchanged(catalan, x)
450
+ assert catalan(2*x).rewrite(binomial) == binomial(4*x, 2*x)/(2*x + 1)
451
+ assert catalan(S.Half).rewrite(gamma) == 8/(3*pi)
452
+ assert catalan(S.Half).rewrite(factorial).rewrite(gamma) ==\
453
+ 8 / (3 * pi)
454
+ assert catalan(3*x).rewrite(gamma) == 4**(
455
+ 3*x)*gamma(3*x + S.Half)/(sqrt(pi)*gamma(3*x + 2))
456
+ assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2,), 1)
457
+
458
+ assert catalan(n).rewrite(factorial) == factorial(2*n) / (factorial(n + 1)
459
+ * factorial(n))
460
+ assert isinstance(catalan(n).rewrite(Product), catalan)
461
+ assert isinstance(catalan(m).rewrite(Product), Product)
462
+
463
+ assert diff(catalan(x), x) == (polygamma(
464
+ 0, x + S.Half) - polygamma(0, x + 2) + log(4))*catalan(x)
465
+
466
+ assert catalan(x).evalf() == catalan(x)
467
+ c = catalan(S.Half).evalf()
468
+ assert str(c) == '0.848826363156775'
469
+ c = catalan(I).evalf(3)
470
+ assert str((re(c), im(c))) == '(0.398, -0.0209)'
471
+
472
+ # Assumptions
473
+ assert catalan(p).is_positive is True
474
+ assert catalan(k).is_integer is True
475
+ assert catalan(m+3).is_composite is True
476
+
477
+
478
+ def test_genocchi():
479
+ genocchis = [0, -1, -1, 0, 1, 0, -3, 0, 17]
480
+ for n, g in enumerate(genocchis):
481
+ assert genocchi(n) == g
482
+
483
+ m = Symbol('m', integer=True)
484
+ n = Symbol('n', integer=True, positive=True)
485
+ assert unchanged(genocchi, m)
486
+ assert genocchi(2*n + 1) == 0
487
+ gn = 2 * (1 - 2**n) * bernoulli(n)
488
+ assert genocchi(n).rewrite(bernoulli).factor() == gn.factor()
489
+ gnx = 2 * (bernoulli(n, x) - 2**n * bernoulli(n, (x+1) / 2))
490
+ assert genocchi(n, x).rewrite(bernoulli).factor() == gnx.factor()
491
+ assert genocchi(2 * n).is_odd
492
+ assert genocchi(2 * n).is_even is False
493
+ assert genocchi(2 * n + 1).is_even
494
+ assert genocchi(n).is_integer
495
+ assert genocchi(4 * n).is_positive
496
+ # these are the only 2 prime Genocchi numbers
497
+ assert genocchi(6, evaluate=False).is_prime == S(-3).is_prime
498
+ assert genocchi(8, evaluate=False).is_prime
499
+ assert genocchi(4 * n + 2).is_negative
500
+ assert genocchi(4 * n + 1).is_negative is False
501
+ assert genocchi(4 * n - 2).is_negative
502
+
503
+ g0 = genocchi(0, evaluate=False)
504
+ assert g0.is_positive is False
505
+ assert g0.is_negative is False
506
+ assert g0.is_even is True
507
+ assert g0.is_odd is False
508
+
509
+ assert genocchi(0, x) == 0
510
+ assert genocchi(1, x) == -1
511
+ assert genocchi(2, x) == 1 - 2*x
512
+ assert genocchi(3, x) == 3*x - 3*x**2
513
+ assert genocchi(4, x) == -1 + 6*x**2 - 4*x**3
514
+ y = Symbol("y")
515
+ assert genocchi(5, (x+y)**100) == -5*(x+y)**400 + 10*(x+y)**300 - 5*(x+y)**100
516
+
517
+ assert str(genocchi(5.0, 4.0).evalf(n=10)) == '-660.0000000'
518
+ assert str(genocchi(Rational(5, 4)).evalf(n=10)) == '-1.104286457'
519
+ assert str(genocchi(-2).evalf(n=10)) == '3.606170709'
520
+ assert str(genocchi(1.3, 3.7).evalf(n=10)) == '-1.847375373'
521
+ assert str(genocchi(I, 1.0).evalf(n=10)) == '-0.3161917278 - 1.45311955*I'
522
+
523
+ n = Symbol('n')
524
+ assert genocchi(n, x).rewrite(dirichlet_eta) == -2*n * dirichlet_eta(1-n, x)
525
+
526
+
527
+ def test_andre():
528
+ nums = [1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521]
529
+ for n, a in enumerate(nums):
530
+ assert andre(n) == a
531
+ assert andre(S.Infinity) == S.Infinity
532
+ assert andre(-1) == -log(2)
533
+ assert andre(-2) == -2*S.Catalan
534
+ assert andre(-3) == 3*zeta(3)/16
535
+ assert andre(-5) == -15*zeta(5)/256
536
+ # In fact andre(-2*n) is related to the Dirichlet *beta* function
537
+ # at 2*n, but SymPy doesn't implement that (or general L-functions)
538
+ assert unchanged(andre, -4)
539
+
540
+ n = Symbol('n', integer=True, nonnegative=True)
541
+ assert unchanged(andre, n)
542
+ assert andre(n).is_integer is True
543
+ assert andre(n).is_positive is True
544
+
545
+ assert str(andre(10, evaluate=False).evalf(n=10)) == '50521.00000'
546
+ assert str(andre(-1, evaluate=False).evalf(n=10)) == '-0.6931471806'
547
+ assert str(andre(-2, evaluate=False).evalf(n=10)) == '-1.831931188'
548
+ assert str(andre(-4, evaluate=False).evalf(n=10)) == '1.977889103'
549
+ assert str(andre(I, evaluate=False).evalf(n=10)) == '2.378417833 + 0.6343322845*I'
550
+
551
+ assert andre(x).rewrite(polylog) == \
552
+ (-I)**(x+1) * polylog(-x, I) + I**(x+1) * polylog(-x, -I)
553
+ assert andre(x).rewrite(zeta) == \
554
+ 2 * gamma(x+1) / (2*pi)**(x+1) * \
555
+ (zeta(x+1, Rational(1,4)) - cos(pi*x) * zeta(x+1, Rational(3,4)))
556
+
557
+
558
+ @nocache_fail
559
+ def test_partition():
560
+ partition_nums = [1, 1, 2, 3, 5, 7, 11, 15, 22]
561
+ for n, p in enumerate(partition_nums):
562
+ assert partition(n) == p
563
+
564
+ x = Symbol('x')
565
+ y = Symbol('y', real=True)
566
+ m = Symbol('m', integer=True)
567
+ n = Symbol('n', integer=True, negative=True)
568
+ p = Symbol('p', integer=True, nonnegative=True)
569
+ assert partition(m).is_integer
570
+ assert not partition(m).is_negative
571
+ assert partition(m).is_nonnegative
572
+ assert partition(n).is_zero
573
+ assert partition(p).is_positive
574
+ assert partition(x).subs(x, 7) == 15
575
+ assert partition(y).subs(y, 8) == 22
576
+ raises(TypeError, lambda: partition(Rational(5, 4)))
577
+
578
+
579
+ def test_divisor_sigma():
580
+ # error
581
+ m = Symbol('m', integer=False)
582
+ raises(TypeError, lambda: divisor_sigma(m))
583
+ raises(TypeError, lambda: divisor_sigma(4.5))
584
+ raises(TypeError, lambda: divisor_sigma(1, m))
585
+ raises(TypeError, lambda: divisor_sigma(1, 4.5))
586
+ m = Symbol('m', positive=False)
587
+ raises(ValueError, lambda: divisor_sigma(m))
588
+ raises(ValueError, lambda: divisor_sigma(0))
589
+ m = Symbol('m', negative=True)
590
+ raises(ValueError, lambda: divisor_sigma(1, m))
591
+ raises(ValueError, lambda: divisor_sigma(1, -1))
592
+
593
+ # special case
594
+ p = Symbol('p', prime=True)
595
+ k = Symbol('k', integer=True)
596
+ assert divisor_sigma(p, 1) == p + 1
597
+ assert divisor_sigma(p, k) == p**k + 1
598
+
599
+ # property
600
+ n = Symbol('n', integer=True, positive=True)
601
+ assert divisor_sigma(n).is_integer is True
602
+ assert divisor_sigma(n).is_positive is True
603
+
604
+ # symbolic
605
+ k = Symbol('k', integer=True, zero=False)
606
+ assert divisor_sigma(4, k) == 2**(2*k) + 2**k + 1
607
+ assert divisor_sigma(6, k) == (2**k + 1) * (3**k + 1)
608
+
609
+ # Integer
610
+ assert divisor_sigma(23450) == 50592
611
+ assert divisor_sigma(23450, 0) == 24
612
+ assert divisor_sigma(23450, 1) == 50592
613
+ assert divisor_sigma(23450, 2) == 730747500
614
+ assert divisor_sigma(23450, 3) == 14666785333344
615
+
616
+
617
+ def test_udivisor_sigma():
618
+ # error
619
+ m = Symbol('m', integer=False)
620
+ raises(TypeError, lambda: udivisor_sigma(m))
621
+ raises(TypeError, lambda: udivisor_sigma(4.5))
622
+ raises(TypeError, lambda: udivisor_sigma(1, m))
623
+ raises(TypeError, lambda: udivisor_sigma(1, 4.5))
624
+ m = Symbol('m', positive=False)
625
+ raises(ValueError, lambda: udivisor_sigma(m))
626
+ raises(ValueError, lambda: udivisor_sigma(0))
627
+ m = Symbol('m', negative=True)
628
+ raises(ValueError, lambda: udivisor_sigma(1, m))
629
+ raises(ValueError, lambda: udivisor_sigma(1, -1))
630
+
631
+ # special case
632
+ p = Symbol('p', prime=True)
633
+ k = Symbol('k', integer=True)
634
+ assert udivisor_sigma(p, 1) == p + 1
635
+ assert udivisor_sigma(p, k) == p**k + 1
636
+
637
+ # property
638
+ n = Symbol('n', integer=True, positive=True)
639
+ assert udivisor_sigma(n).is_integer is True
640
+ assert udivisor_sigma(n).is_positive is True
641
+
642
+ # Integer
643
+ A034444 = [1, 2, 2, 2, 2, 4, 2, 2, 2, 4, 2, 4, 2, 4, 4, 2, 2, 4, 2, 4,
644
+ 4, 4, 2, 4, 2, 4, 2, 4, 2, 8, 2, 2, 4, 4, 4, 4, 2, 4, 4, 4,
645
+ 2, 8, 2, 4, 4, 4, 2, 4, 2, 4, 4, 4, 2, 4, 4, 4, 4, 4, 2, 8]
646
+ for n, val in enumerate(A034444, 1):
647
+ assert udivisor_sigma(n, 0) == val
648
+ A034448 = [1, 3, 4, 5, 6, 12, 8, 9, 10, 18, 12, 20, 14, 24, 24, 17, 18,
649
+ 30, 20, 30, 32, 36, 24, 36, 26, 42, 28, 40, 30, 72, 32, 33,
650
+ 48, 54, 48, 50, 38, 60, 56, 54, 42, 96, 44, 60, 60, 72, 48]
651
+ for n, val in enumerate(A034448, 1):
652
+ assert udivisor_sigma(n, 1) == val
653
+ A034676 = [1, 5, 10, 17, 26, 50, 50, 65, 82, 130, 122, 170, 170, 250,
654
+ 260, 257, 290, 410, 362, 442, 500, 610, 530, 650, 626, 850,
655
+ 730, 850, 842, 1300, 962, 1025, 1220, 1450, 1300, 1394, 1370]
656
+ for n, val in enumerate(A034676, 1):
657
+ assert udivisor_sigma(n, 2) == val
658
+
659
+
660
+ def test_legendre_symbol():
661
+ # error
662
+ m = Symbol('m', integer=False)
663
+ raises(TypeError, lambda: legendre_symbol(m, 3))
664
+ raises(TypeError, lambda: legendre_symbol(4.5, 3))
665
+ raises(TypeError, lambda: legendre_symbol(1, m))
666
+ raises(TypeError, lambda: legendre_symbol(1, 4.5))
667
+ m = Symbol('m', prime=False)
668
+ raises(ValueError, lambda: legendre_symbol(1, m))
669
+ raises(ValueError, lambda: legendre_symbol(1, 6))
670
+ m = Symbol('m', odd=False)
671
+ raises(ValueError, lambda: legendre_symbol(1, m))
672
+ raises(ValueError, lambda: legendre_symbol(1, 2))
673
+
674
+ # special case
675
+ p = Symbol('p', prime=True)
676
+ k = Symbol('k', integer=True)
677
+ assert legendre_symbol(p*k, p) == 0
678
+ assert legendre_symbol(1, p) == 1
679
+
680
+ # property
681
+ n = Symbol('n')
682
+ m = Symbol('m')
683
+ assert legendre_symbol(m, n).is_integer is True
684
+ assert legendre_symbol(m, n).is_prime is False
685
+
686
+ # Integer
687
+ assert legendre_symbol(5, 11) == 1
688
+ assert legendre_symbol(25, 41) == 1
689
+ assert legendre_symbol(67, 101) == -1
690
+ assert legendre_symbol(0, 13) == 0
691
+ assert legendre_symbol(9, 3) == 0
692
+
693
+
694
+ def test_jacobi_symbol():
695
+ # error
696
+ m = Symbol('m', integer=False)
697
+ raises(TypeError, lambda: jacobi_symbol(m, 3))
698
+ raises(TypeError, lambda: jacobi_symbol(4.5, 3))
699
+ raises(TypeError, lambda: jacobi_symbol(1, m))
700
+ raises(TypeError, lambda: jacobi_symbol(1, 4.5))
701
+ m = Symbol('m', positive=False)
702
+ raises(ValueError, lambda: jacobi_symbol(1, m))
703
+ raises(ValueError, lambda: jacobi_symbol(1, -6))
704
+ m = Symbol('m', odd=False)
705
+ raises(ValueError, lambda: jacobi_symbol(1, m))
706
+ raises(ValueError, lambda: jacobi_symbol(1, 2))
707
+
708
+ # special case
709
+ p = Symbol('p', integer=True)
710
+ k = Symbol('k', integer=True)
711
+ assert jacobi_symbol(p*k, p) == 0
712
+ assert jacobi_symbol(1, p) == 1
713
+ assert jacobi_symbol(1, 1) == 1
714
+ assert jacobi_symbol(0, 1) == 1
715
+
716
+ # property
717
+ n = Symbol('n')
718
+ m = Symbol('m')
719
+ assert jacobi_symbol(m, n).is_integer is True
720
+ assert jacobi_symbol(m, n).is_prime is False
721
+
722
+ # Integer
723
+ assert jacobi_symbol(25, 41) == 1
724
+ assert jacobi_symbol(-23, 83) == -1
725
+ assert jacobi_symbol(3, 9) == 0
726
+ assert jacobi_symbol(42, 97) == -1
727
+ assert jacobi_symbol(3, 5) == -1
728
+ assert jacobi_symbol(7, 9) == 1
729
+ assert jacobi_symbol(0, 3) == 0
730
+ assert jacobi_symbol(0, 1) == 1
731
+ assert jacobi_symbol(2, 1) == 1
732
+ assert jacobi_symbol(1, 3) == 1
733
+
734
+
735
+ def test_kronecker_symbol():
736
+ # error
737
+ m = Symbol('m', integer=False)
738
+ raises(TypeError, lambda: kronecker_symbol(m, 3))
739
+ raises(TypeError, lambda: kronecker_symbol(4.5, 3))
740
+ raises(TypeError, lambda: kronecker_symbol(1, m))
741
+ raises(TypeError, lambda: kronecker_symbol(1, 4.5))
742
+
743
+ # special case
744
+ p = Symbol('p', integer=True)
745
+ assert kronecker_symbol(1, p) == 1
746
+ assert kronecker_symbol(1, 1) == 1
747
+ assert kronecker_symbol(0, 1) == 1
748
+
749
+ # property
750
+ n = Symbol('n')
751
+ m = Symbol('m')
752
+ assert kronecker_symbol(m, n).is_integer is True
753
+ assert kronecker_symbol(m, n).is_prime is False
754
+
755
+ # Integer
756
+ for n in range(3, 10, 2):
757
+ for a in range(-n, n):
758
+ val = kronecker_symbol(a, n)
759
+ assert val == jacobi_symbol(a, n)
760
+ minus = kronecker_symbol(a, -n)
761
+ if a < 0:
762
+ assert -minus == val
763
+ else:
764
+ assert minus == val
765
+ even = kronecker_symbol(a, 2 * n)
766
+ if a % 2 == 0:
767
+ assert even == 0
768
+ elif a % 8 in [1, 7]:
769
+ assert even == val
770
+ else:
771
+ assert -even == val
772
+ assert kronecker_symbol(1, 0) == kronecker_symbol(-1, 0) == 1
773
+ assert kronecker_symbol(0, 0) == 0
774
+
775
+
776
+ def test_mobius():
777
+ # error
778
+ m = Symbol('m', integer=False)
779
+ raises(TypeError, lambda: mobius(m))
780
+ raises(TypeError, lambda: mobius(4.5))
781
+ m = Symbol('m', positive=False)
782
+ raises(ValueError, lambda: mobius(m))
783
+ raises(ValueError, lambda: mobius(-3))
784
+
785
+ # special case
786
+ p = Symbol('p', prime=True)
787
+ assert mobius(p) == -1
788
+
789
+ # property
790
+ n = Symbol('n', integer=True, positive=True)
791
+ assert mobius(n).is_integer is True
792
+ assert mobius(n).is_prime is False
793
+
794
+ # symbolic
795
+ n = Symbol('n', integer=True, positive=True)
796
+ k = Symbol('k', integer=True, positive=True)
797
+ assert mobius(n**2) == 0
798
+ assert mobius(4*n) == 0
799
+ assert isinstance(mobius(n**k), mobius)
800
+ assert mobius(n**(k+1)) == 0
801
+ assert isinstance(mobius(3**k), mobius)
802
+ assert mobius(3**(k+1)) == 0
803
+ m = Symbol('m')
804
+ assert isinstance(mobius(4*m), mobius)
805
+
806
+ # Integer
807
+ assert mobius(13*7) == 1
808
+ assert mobius(1) == 1
809
+ assert mobius(13*7*5) == -1
810
+ assert mobius(13**2) == 0
811
+ A008683 = [1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0,
812
+ -1, 0, 1, 1, -1, 0, 0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1,
813
+ 1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0, 0, 0, 1, 0, -1, 0, 1, 0]
814
+ for n, val in enumerate(A008683, 1):
815
+ assert mobius(n) == val
816
+
817
+
818
+ def test_primenu():
819
+ # error
820
+ m = Symbol('m', integer=False)
821
+ raises(TypeError, lambda: primenu(m))
822
+ raises(TypeError, lambda: primenu(4.5))
823
+ m = Symbol('m', positive=False)
824
+ raises(ValueError, lambda: primenu(m))
825
+ raises(ValueError, lambda: primenu(0))
826
+
827
+ # special case
828
+ p = Symbol('p', prime=True)
829
+ assert primenu(p) == 1
830
+
831
+ # property
832
+ n = Symbol('n', integer=True, positive=True)
833
+ assert primenu(n).is_integer is True
834
+ assert primenu(n).is_nonnegative is True
835
+
836
+ # Integer
837
+ assert primenu(7*13) == 2
838
+ assert primenu(2*17*19) == 3
839
+ assert primenu(2**3 * 17 * 19**2) == 3
840
+ A001221 = [0, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2,
841
+ 1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 1, 2, 2, 2, 2]
842
+ for n, val in enumerate(A001221, 1):
843
+ assert primenu(n) == val
844
+
845
+
846
+ def test_primeomega():
847
+ # error
848
+ m = Symbol('m', integer=False)
849
+ raises(TypeError, lambda: primeomega(m))
850
+ raises(TypeError, lambda: primeomega(4.5))
851
+ m = Symbol('m', positive=False)
852
+ raises(ValueError, lambda: primeomega(m))
853
+ raises(ValueError, lambda: primeomega(0))
854
+
855
+ # special case
856
+ p = Symbol('p', prime=True)
857
+ assert primeomega(p) == 1
858
+
859
+ # property
860
+ n = Symbol('n', integer=True, positive=True)
861
+ assert primeomega(n).is_integer is True
862
+ assert primeomega(n).is_nonnegative is True
863
+
864
+ # Integer
865
+ assert primeomega(7*13) == 2
866
+ assert primeomega(2*17*19) == 3
867
+ assert primeomega(2**3 * 17 * 19**2) == 6
868
+ A001222 = [0, 1, 1, 2, 1, 2, 1, 3, 2, 2, 1, 3, 1, 2, 2, 4, 1, 3,
869
+ 1, 3, 2, 2, 1, 4, 2, 2, 3, 3, 1, 3, 1, 5, 2, 2, 2, 4]
870
+ for n, val in enumerate(A001222, 1):
871
+ assert primeomega(n) == val
872
+
873
+
874
+ def test_totient():
875
+ # error
876
+ m = Symbol('m', integer=False)
877
+ raises(TypeError, lambda: totient(m))
878
+ raises(TypeError, lambda: totient(4.5))
879
+ m = Symbol('m', positive=False)
880
+ raises(ValueError, lambda: totient(m))
881
+ raises(ValueError, lambda: totient(0))
882
+
883
+ # special case
884
+ p = Symbol('p', prime=True)
885
+ assert totient(p) == p - 1
886
+
887
+ # property
888
+ n = Symbol('n', integer=True, positive=True)
889
+ assert totient(n).is_integer is True
890
+ assert totient(n).is_positive is True
891
+
892
+ # Integer
893
+ assert totient(7*13) == totient(factorint(7*13)) == (7-1)*(13-1)
894
+ assert totient(2*17*19) == totient(factorint(2*17*19)) == (17-1)*(19-1)
895
+ assert totient(2**3 * 17 * 19**2) == totient({2: 3, 17: 1, 19: 2}) == 2**2 * (17-1) * 19*(19-1)
896
+ A000010 = [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16,
897
+ 6, 18, 8, 12, 10, 22, 8, 20, 12, 18, 12, 28, 8, 30, 16,
898
+ 20, 16, 24, 12, 36, 18, 24, 16, 40, 12, 42, 20, 24, 22]
899
+ for n, val in enumerate(A000010, 1):
900
+ assert totient(n) == val
901
+
902
+
903
+ def test_reduced_totient():
904
+ # error
905
+ m = Symbol('m', integer=False)
906
+ raises(TypeError, lambda: reduced_totient(m))
907
+ raises(TypeError, lambda: reduced_totient(4.5))
908
+ m = Symbol('m', positive=False)
909
+ raises(ValueError, lambda: reduced_totient(m))
910
+ raises(ValueError, lambda: reduced_totient(0))
911
+
912
+ # special case
913
+ p = Symbol('p', prime=True)
914
+ assert reduced_totient(p) == p - 1
915
+
916
+ # property
917
+ n = Symbol('n', integer=True, positive=True)
918
+ assert reduced_totient(n).is_integer is True
919
+ assert reduced_totient(n).is_positive is True
920
+
921
+ # Integer
922
+ assert reduced_totient(7*13) == reduced_totient(factorint(7*13)) == 12
923
+ assert reduced_totient(2*17*19) == reduced_totient(factorint(2*17*19)) == 144
924
+ assert reduced_totient(2**2 * 11) == reduced_totient({2: 2, 11: 1}) == 10
925
+ assert reduced_totient(2**3 * 17 * 19**2) == reduced_totient({2: 3, 17: 1, 19: 2}) == 2736
926
+ A002322 = [1, 1, 2, 2, 4, 2, 6, 2, 6, 4, 10, 2, 12, 6, 4, 4, 16, 6,
927
+ 18, 4, 6, 10, 22, 2, 20, 12, 18, 6, 28, 4, 30, 8, 10, 16,
928
+ 12, 6, 36, 18, 12, 4, 40, 6, 42, 10, 12, 22, 46, 4, 42]
929
+ for n, val in enumerate(A002322, 1):
930
+ assert reduced_totient(n) == val
931
+
932
+
933
+ def test_primepi():
934
+ # error
935
+ z = Symbol('z', real=False)
936
+ raises(TypeError, lambda: primepi(z))
937
+ raises(TypeError, lambda: primepi(I))
938
+
939
+ # property
940
+ n = Symbol('n', integer=True, positive=True)
941
+ assert primepi(n).is_integer is True
942
+ assert primepi(n).is_nonnegative is True
943
+
944
+ # infinity
945
+ assert primepi(oo) == oo
946
+ assert primepi(-oo) == 0
947
+
948
+ # symbol
949
+ x = Symbol('x')
950
+ assert isinstance(primepi(x), primepi)
951
+
952
+ # Integer
953
+ assert primepi(0) == 0
954
+ A000720 = [0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 8,
955
+ 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 11, 11, 11, 11, 11, 11,
956
+ 12, 12, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15]
957
+ for n, val in enumerate(A000720, 1):
958
+ assert primepi(n) == primepi(n + 0.5) == val
959
+
960
+
961
+ def test__nT():
962
+ assert [_nT(i, j) for i in range(5) for j in range(i + 2)] == [
963
+ 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 2, 1, 1, 0]
964
+ check = [_nT(10, i) for i in range(11)]
965
+ assert check == [0, 1, 5, 8, 9, 7, 5, 3, 2, 1, 1]
966
+ assert all(type(i) is int for i in check)
967
+ assert _nT(10, 5) == 7
968
+ assert _nT(100, 98) == 2
969
+ assert _nT(100, 100) == 1
970
+ assert _nT(10, 3) == 8
971
+
972
+
973
+ def test_nC_nP_nT():
974
+ from sympy.utilities.iterables import (
975
+ multiset_permutations, multiset_combinations, multiset_partitions,
976
+ partitions, subsets, permutations)
977
+ from sympy.functions.combinatorial.numbers import (
978
+ nP, nC, nT, stirling, _stirling1, _stirling2, _multiset_histogram, _AOP_product)
979
+
980
+ from sympy.combinatorics.permutations import Permutation
981
+ from sympy.core.random import choice
982
+
983
+ c = string.ascii_lowercase
984
+ for i in range(100):
985
+ s = ''.join(choice(c) for i in range(7))
986
+ u = len(s) == len(set(s))
987
+ try:
988
+ tot = 0
989
+ for i in range(8):
990
+ check = nP(s, i)
991
+ tot += check
992
+ assert len(list(multiset_permutations(s, i))) == check
993
+ if u:
994
+ assert nP(len(s), i) == check
995
+ assert nP(s) == tot
996
+ except AssertionError:
997
+ print(s, i, 'failed perm test')
998
+ raise ValueError()
999
+
1000
+ for i in range(100):
1001
+ s = ''.join(choice(c) for i in range(7))
1002
+ u = len(s) == len(set(s))
1003
+ try:
1004
+ tot = 0
1005
+ for i in range(8):
1006
+ check = nC(s, i)
1007
+ tot += check
1008
+ assert len(list(multiset_combinations(s, i))) == check
1009
+ if u:
1010
+ assert nC(len(s), i) == check
1011
+ assert nC(s) == tot
1012
+ if u:
1013
+ assert nC(len(s)) == tot
1014
+ except AssertionError:
1015
+ print(s, i, 'failed combo test')
1016
+ raise ValueError()
1017
+
1018
+ for i in range(1, 10):
1019
+ tot = 0
1020
+ for j in range(1, i + 2):
1021
+ check = nT(i, j)
1022
+ assert check.is_Integer
1023
+ tot += check
1024
+ assert sum(1 for p in partitions(i, j, size=True) if p[0] == j) == check
1025
+ assert nT(i) == tot
1026
+
1027
+ for i in range(1, 10):
1028
+ tot = 0
1029
+ for j in range(1, i + 2):
1030
+ check = nT(range(i), j)
1031
+ tot += check
1032
+ assert len(list(multiset_partitions(list(range(i)), j))) == check
1033
+ assert nT(range(i)) == tot
1034
+
1035
+ for i in range(100):
1036
+ s = ''.join(choice(c) for i in range(7))
1037
+ u = len(s) == len(set(s))
1038
+ try:
1039
+ tot = 0
1040
+ for i in range(1, 8):
1041
+ check = nT(s, i)
1042
+ tot += check
1043
+ assert len(list(multiset_partitions(s, i))) == check
1044
+ if u:
1045
+ assert nT(range(len(s)), i) == check
1046
+ if u:
1047
+ assert nT(range(len(s))) == tot
1048
+ assert nT(s) == tot
1049
+ except AssertionError:
1050
+ print(s, i, 'failed partition test')
1051
+ raise ValueError()
1052
+
1053
+ # tests for Stirling numbers of the first kind that are not tested in the
1054
+ # above
1055
+ assert [stirling(9, i, kind=1) for i in range(11)] == [
1056
+ 0, 40320, 109584, 118124, 67284, 22449, 4536, 546, 36, 1, 0]
1057
+ perms = list(permutations(range(4)))
1058
+ assert [sum(1 for p in perms if Permutation(p).cycles == i)
1059
+ for i in range(5)] == [0, 6, 11, 6, 1] == [
1060
+ stirling(4, i, kind=1) for i in range(5)]
1061
+ # http://oeis.org/A008275
1062
+ assert [stirling(n, k, signed=1)
1063
+ for n in range(10) for k in range(1, n + 1)] == [
1064
+ 1, -1,
1065
+ 1, 2, -3,
1066
+ 1, -6, 11, -6,
1067
+ 1, 24, -50, 35, -10,
1068
+ 1, -120, 274, -225, 85, -15,
1069
+ 1, 720, -1764, 1624, -735, 175, -21,
1070
+ 1, -5040, 13068, -13132, 6769, -1960, 322, -28,
1071
+ 1, 40320, -109584, 118124, -67284, 22449, -4536, 546, -36, 1]
1072
+ # https://en.wikipedia.org/wiki/Stirling_numbers_of_the_first_kind
1073
+ assert [stirling(n, k, kind=1)
1074
+ for n in range(10) for k in range(n+1)] == [
1075
+ 1,
1076
+ 0, 1,
1077
+ 0, 1, 1,
1078
+ 0, 2, 3, 1,
1079
+ 0, 6, 11, 6, 1,
1080
+ 0, 24, 50, 35, 10, 1,
1081
+ 0, 120, 274, 225, 85, 15, 1,
1082
+ 0, 720, 1764, 1624, 735, 175, 21, 1,
1083
+ 0, 5040, 13068, 13132, 6769, 1960, 322, 28, 1,
1084
+ 0, 40320, 109584, 118124, 67284, 22449, 4536, 546, 36, 1]
1085
+ # https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind
1086
+ assert [stirling(n, k, kind=2)
1087
+ for n in range(10) for k in range(n+1)] == [
1088
+ 1,
1089
+ 0, 1,
1090
+ 0, 1, 1,
1091
+ 0, 1, 3, 1,
1092
+ 0, 1, 7, 6, 1,
1093
+ 0, 1, 15, 25, 10, 1,
1094
+ 0, 1, 31, 90, 65, 15, 1,
1095
+ 0, 1, 63, 301, 350, 140, 21, 1,
1096
+ 0, 1, 127, 966, 1701, 1050, 266, 28, 1,
1097
+ 0, 1, 255, 3025, 7770, 6951, 2646, 462, 36, 1]
1098
+ assert stirling(3, 4, kind=1) == stirling(3, 4, kind=1) == 0
1099
+ raises(ValueError, lambda: stirling(-2, 2))
1100
+
1101
+ # Assertion that the return type is SymPy Integer.
1102
+ assert isinstance(_stirling1(6, 3), Integer)
1103
+ assert isinstance(_stirling2(6, 3), Integer)
1104
+
1105
+ def delta(p):
1106
+ if len(p) == 1:
1107
+ return oo
1108
+ return min(abs(i[0] - i[1]) for i in subsets(p, 2))
1109
+ parts = multiset_partitions(range(5), 3)
1110
+ d = 2
1111
+ assert (sum(1 for p in parts if all(delta(i) >= d for i in p)) ==
1112
+ stirling(5, 3, d=d) == 7)
1113
+
1114
+ # other coverage tests
1115
+ assert nC('abb', 2) == nC('aab', 2) == 2
1116
+ assert nP(3, 3, replacement=True) == nP('aabc', 3, replacement=True) == 27
1117
+ assert nP(3, 4) == 0
1118
+ assert nP('aabc', 5) == 0
1119
+ assert nC(4, 2, replacement=True) == nC('abcdd', 2, replacement=True) == \
1120
+ len(list(multiset_combinations('aabbccdd', 2))) == 10
1121
+ assert nC('abcdd') == sum(nC('abcdd', i) for i in range(6)) == 24
1122
+ assert nC(list('abcdd'), 4) == 4
1123
+ assert nT('aaaa') == nT(4) == len(list(partitions(4))) == 5
1124
+ assert nT('aaab') == len(list(multiset_partitions('aaab'))) == 7
1125
+ assert nC('aabb'*3, 3) == 4 # aaa, bbb, abb, baa
1126
+ assert dict(_AOP_product((4,1,1,1))) == {
1127
+ 0: 1, 1: 4, 2: 7, 3: 8, 4: 8, 5: 7, 6: 4, 7: 1}
1128
+ # the following was the first t that showed a problem in a previous form of
1129
+ # the function, so it's not as random as it may appear
1130
+ t = (3, 9, 4, 6, 6, 5, 5, 2, 10, 4)
1131
+ assert sum(_AOP_product(t)[i] for i in range(55)) == 58212000
1132
+ raises(ValueError, lambda: _multiset_histogram({1:'a'}))
1133
+
1134
+
1135
+ def test_PR_14617():
1136
+ from sympy.functions.combinatorial.numbers import nT
1137
+ for n in (0, []):
1138
+ for k in (-1, 0, 1):
1139
+ if k == 0:
1140
+ assert nT(n, k) == 1
1141
+ else:
1142
+ assert nT(n, k) == 0
1143
+
1144
+
1145
+ def test_issue_8496():
1146
+ n = Symbol("n")
1147
+ k = Symbol("k")
1148
+
1149
+ raises(TypeError, lambda: catalan(n, k))
1150
+
1151
+
1152
+ def test_issue_8601():
1153
+ n = Symbol('n', integer=True, negative=True)
1154
+
1155
+ assert catalan(n - 1) is S.Zero
1156
+ assert catalan(Rational(-1, 2)) is S.ComplexInfinity
1157
+ assert catalan(-S.One) == Rational(-1, 2)
1158
+ c1 = catalan(-5.6).evalf()
1159
+ assert str(c1) == '6.93334070531408e-5'
1160
+ c2 = catalan(-35.4).evalf()
1161
+ assert str(c2) == '-4.14189164517449e-24'
1162
+
1163
+
1164
+ def test_motzkin():
1165
+ assert motzkin.is_motzkin(4) == True
1166
+ assert motzkin.is_motzkin(9) == True
1167
+ assert motzkin.is_motzkin(10) == False
1168
+ assert motzkin.find_motzkin_numbers_in_range(10,200) == [21, 51, 127]
1169
+ assert motzkin.find_motzkin_numbers_in_range(10,400) == [21, 51, 127, 323]
1170
+ assert motzkin.find_motzkin_numbers_in_range(10,1600) == [21, 51, 127, 323, 835]
1171
+ assert motzkin.find_first_n_motzkins(5) == [1, 1, 2, 4, 9]
1172
+ assert motzkin.find_first_n_motzkins(7) == [1, 1, 2, 4, 9, 21, 51]
1173
+ assert motzkin.find_first_n_motzkins(10) == [1, 1, 2, 4, 9, 21, 51, 127, 323, 835]
1174
+ raises(ValueError, lambda: motzkin.eval(77.58))
1175
+ raises(ValueError, lambda: motzkin.eval(-8))
1176
+ raises(ValueError, lambda: motzkin.find_motzkin_numbers_in_range(-2,7))
1177
+ raises(ValueError, lambda: motzkin.find_motzkin_numbers_in_range(13,7))
1178
+ raises(ValueError, lambda: motzkin.find_first_n_motzkins(112.8))
1179
+
1180
+
1181
+ def test_nD_derangements():
1182
+ from sympy.utilities.iterables import (partitions, multiset,
1183
+ multiset_derangements, multiset_permutations)
1184
+ from sympy.functions.combinatorial.numbers import nD
1185
+
1186
+ got = []
1187
+ for i in partitions(8, k=4):
1188
+ s = []
1189
+ it = 0
1190
+ for k, v in i.items():
1191
+ for i in range(v):
1192
+ s.extend([it]*k)
1193
+ it += 1
1194
+ ms = multiset(s)
1195
+ c1 = sum(1 for i in multiset_permutations(s) if
1196
+ all(i != j for i, j in zip(i, s)))
1197
+ assert c1 == nD(ms) == nD(ms, 0) == nD(ms, 1)
1198
+ v = [tuple(i) for i in multiset_derangements(s)]
1199
+ c2 = len(v)
1200
+ assert c2 == len(set(v))
1201
+ assert c1 == c2
1202
+ got.append(c1)
1203
+ assert got == [1, 4, 6, 12, 24, 24, 61, 126, 315, 780, 297, 772,
1204
+ 2033, 5430, 14833]
1205
+
1206
+ assert nD('1112233456', brute=True) == nD('1112233456') == 16356
1207
+ assert nD('') == nD([]) == nD({}) == 0
1208
+ assert nD({1: 0}) == 0
1209
+ raises(ValueError, lambda: nD({1: -1}))
1210
+ assert nD('112') == 0
1211
+ assert nD(i='112') == 0
1212
+ assert [nD(n=i) for i in range(6)] == [0, 0, 1, 2, 9, 44]
1213
+ assert nD((i for i in range(4))) == nD('0123') == 9
1214
+ assert nD(m=(i for i in range(4))) == 3
1215
+ assert nD(m={0: 1, 1: 1, 2: 1, 3: 1}) == 3
1216
+ assert nD(m=[0, 1, 2, 3]) == 3
1217
+ raises(TypeError, lambda: nD(m=0))
1218
+ raises(TypeError, lambda: nD(-1))
1219
+ assert nD({-1: 1, -2: 1}) == 1
1220
+ assert nD(m={0: 3}) == 0
1221
+ raises(ValueError, lambda: nD(i='123', n=3))
1222
+ raises(ValueError, lambda: nD(i='123', m=(1,2)))
1223
+ raises(ValueError, lambda: nD(n=0, m=(1,2)))
1224
+ raises(ValueError, lambda: nD({1: -1}))
1225
+ raises(ValueError, lambda: nD(m={-1: 1, 2: 1}))
1226
+ raises(ValueError, lambda: nD(m={1: -1, 2: 1}))
1227
+ raises(ValueError, lambda: nD(m=[-1, 2]))
1228
+ raises(TypeError, lambda: nD({1: x}))
1229
+ raises(TypeError, lambda: nD(m={1: x}))
1230
+ raises(TypeError, lambda: nD(m={x: 1}))
1231
+
1232
+
1233
+ def test_deprecated_ntheory_symbolic_functions():
1234
+ from sympy.testing.pytest import warns_deprecated_sympy
1235
+
1236
+ with warns_deprecated_sympy():
1237
+ assert not carmichael.is_carmichael(3)
1238
+ with warns_deprecated_sympy():
1239
+ assert carmichael.find_carmichael_numbers_in_range(10, 20) == []
1240
+ with warns_deprecated_sympy():
1241
+ assert carmichael.find_first_n_carmichaels(1)
openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (181 Bytes). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/beta_functions.cpython-310.pyc ADDED
Binary file (13.3 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/delta_functions.cpython-310.pyc ADDED
Binary file (18.6 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/gamma_functions.cpython-310.pyc ADDED
Binary file (41.6 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/hyper.cpython-310.pyc ADDED
Binary file (41.3 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/mathieu_functions.cpython-310.pyc ADDED
Binary file (7.07 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/__pycache__/zeta_functions.cpython-310.pyc ADDED
Binary file (23 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/benchmarks/__init__.py ADDED
File without changes
openflamingo/lib/python3.10/site-packages/sympy/functions/special/benchmarks/__pycache__/bench_special.cpython-310.pyc ADDED
Binary file (481 Bytes). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/benchmarks/bench_special.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.symbol import symbols
2
+ from sympy.functions.special.spherical_harmonics import Ynm
3
+
4
+ x, y = symbols('x,y')
5
+
6
+
7
+ def timeit_Ynm_xy():
8
+ Ynm(1, 1, x, y)
openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (187 Bytes). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_bessel.cpython-310.pyc ADDED
Binary file (31.4 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_beta_functions.cpython-310.pyc ADDED
Binary file (3.84 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_bsplines.cpython-310.pyc ADDED
Binary file (6.62 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_delta_functions.cpython-310.pyc ADDED
Binary file (5.95 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_hyper.cpython-310.pyc ADDED
Binary file (15.8 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_spec_polynomials.cpython-310.pyc ADDED
Binary file (18.5 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_spherical_harmonics.cpython-310.pyc ADDED
Binary file (3.67 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_tensor_functions.cpython-310.pyc ADDED
Binary file (4.58 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/functions/special/tests/__pycache__/test_zeta_functions.cpython-310.pyc ADDED
Binary file (10.6 kB). View file
 
openflamingo/lib/python3.10/site-packages/sympy/matrices/eigen.py ADDED
@@ -0,0 +1,1346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from types import FunctionType
2
+ from collections import Counter
3
+
4
+ from mpmath import mp, workprec
5
+ from mpmath.libmp.libmpf import prec_to_dps
6
+
7
+ from sympy.core.sorting import default_sort_key
8
+ from sympy.core.evalf import DEFAULT_MAXPREC, PrecisionExhausted
9
+ from sympy.core.logic import fuzzy_and, fuzzy_or
10
+ from sympy.core.numbers import Float
11
+ from sympy.core.sympify import _sympify
12
+ from sympy.functions.elementary.miscellaneous import sqrt
13
+ from sympy.polys import roots, CRootOf, ZZ, QQ, EX
14
+ from sympy.polys.matrices import DomainMatrix
15
+ from sympy.polys.matrices.eigen import dom_eigenvects, dom_eigenvects_to_sympy
16
+ from sympy.polys.polytools import gcd
17
+
18
+ from .exceptions import MatrixError, NonSquareMatrixError
19
+ from .determinant import _find_reasonable_pivot
20
+
21
+ from .utilities import _iszero, _simplify
22
+
23
+
24
+ __doctest_requires__ = {
25
+ ('_is_indefinite',
26
+ '_is_negative_definite',
27
+ '_is_negative_semidefinite',
28
+ '_is_positive_definite',
29
+ '_is_positive_semidefinite'): ['matplotlib'],
30
+ }
31
+
32
+
33
+ def _eigenvals_eigenvects_mpmath(M):
34
+ norm2 = lambda v: mp.sqrt(sum(i**2 for i in v))
35
+
36
+ v1 = None
37
+ prec = max(x._prec for x in M.atoms(Float))
38
+ eps = 2**-prec
39
+
40
+ while prec < DEFAULT_MAXPREC:
41
+ with workprec(prec):
42
+ A = mp.matrix(M.evalf(n=prec_to_dps(prec)))
43
+ E, ER = mp.eig(A)
44
+ v2 = norm2([i for e in E for i in (mp.re(e), mp.im(e))])
45
+ if v1 is not None and mp.fabs(v1 - v2) < eps:
46
+ return E, ER
47
+ v1 = v2
48
+ prec *= 2
49
+
50
+ # we get here because the next step would have taken us
51
+ # past MAXPREC or because we never took a step; in case
52
+ # of the latter, we refuse to send back a solution since
53
+ # it would not have been verified; we also resist taking
54
+ # a small step to arrive exactly at MAXPREC since then
55
+ # the two calculations might be artificially close.
56
+ raise PrecisionExhausted
57
+
58
+
59
+ def _eigenvals_mpmath(M, multiple=False):
60
+ """Compute eigenvalues using mpmath"""
61
+ E, _ = _eigenvals_eigenvects_mpmath(M)
62
+ result = [_sympify(x) for x in E]
63
+ if multiple:
64
+ return result
65
+ return dict(Counter(result))
66
+
67
+
68
+ def _eigenvects_mpmath(M):
69
+ E, ER = _eigenvals_eigenvects_mpmath(M)
70
+ result = []
71
+ for i in range(M.rows):
72
+ eigenval = _sympify(E[i])
73
+ eigenvect = _sympify(ER[:, i])
74
+ result.append((eigenval, 1, [eigenvect]))
75
+
76
+ return result
77
+
78
+
79
+ # This function is a candidate for caching if it gets implemented for matrices.
80
+ def _eigenvals(
81
+ M, error_when_incomplete=True, *, simplify=False, multiple=False,
82
+ rational=False, **flags):
83
+ r"""Compute eigenvalues of the matrix.
84
+
85
+ Parameters
86
+ ==========
87
+
88
+ error_when_incomplete : bool, optional
89
+ If it is set to ``True``, it will raise an error if not all
90
+ eigenvalues are computed. This is caused by ``roots`` not returning
91
+ a full list of eigenvalues.
92
+
93
+ simplify : bool or function, optional
94
+ If it is set to ``True``, it attempts to return the most
95
+ simplified form of expressions returned by applying default
96
+ simplification method in every routine.
97
+
98
+ If it is set to ``False``, it will skip simplification in this
99
+ particular routine to save computation resources.
100
+
101
+ If a function is passed to, it will attempt to apply
102
+ the particular function as simplification method.
103
+
104
+ rational : bool, optional
105
+ If it is set to ``True``, every floating point numbers would be
106
+ replaced with rationals before computation. It can solve some
107
+ issues of ``roots`` routine not working well with floats.
108
+
109
+ multiple : bool, optional
110
+ If it is set to ``True``, the result will be in the form of a
111
+ list.
112
+
113
+ If it is set to ``False``, the result will be in the form of a
114
+ dictionary.
115
+
116
+ Returns
117
+ =======
118
+
119
+ eigs : list or dict
120
+ Eigenvalues of a matrix. The return format would be specified by
121
+ the key ``multiple``.
122
+
123
+ Raises
124
+ ======
125
+
126
+ MatrixError
127
+ If not enough roots had got computed.
128
+
129
+ NonSquareMatrixError
130
+ If attempted to compute eigenvalues from a non-square matrix.
131
+
132
+ Examples
133
+ ========
134
+
135
+ >>> from sympy import Matrix
136
+ >>> M = Matrix(3, 3, [0, 1, 1, 1, 0, 0, 1, 1, 1])
137
+ >>> M.eigenvals()
138
+ {-1: 1, 0: 1, 2: 1}
139
+
140
+ See Also
141
+ ========
142
+
143
+ MatrixBase.charpoly
144
+ eigenvects
145
+
146
+ Notes
147
+ =====
148
+
149
+ Eigenvalues of a matrix $A$ can be computed by solving a matrix
150
+ equation $\det(A - \lambda I) = 0$
151
+
152
+ It's not always possible to return radical solutions for
153
+ eigenvalues for matrices larger than $4, 4$ shape due to
154
+ Abel-Ruffini theorem.
155
+
156
+ If there is no radical solution is found for the eigenvalue,
157
+ it may return eigenvalues in the form of
158
+ :class:`sympy.polys.rootoftools.ComplexRootOf`.
159
+ """
160
+ if not M:
161
+ if multiple:
162
+ return []
163
+ return {}
164
+
165
+ if not M.is_square:
166
+ raise NonSquareMatrixError("{} must be a square matrix.".format(M))
167
+
168
+ if M._rep.domain not in (ZZ, QQ):
169
+ # Skip this check for ZZ/QQ because it can be slow
170
+ if all(x.is_number for x in M) and M.has(Float):
171
+ return _eigenvals_mpmath(M, multiple=multiple)
172
+
173
+ if rational:
174
+ from sympy.simplify import nsimplify
175
+ M = M.applyfunc(
176
+ lambda x: nsimplify(x, rational=True) if x.has(Float) else x)
177
+
178
+ if multiple:
179
+ return _eigenvals_list(
180
+ M, error_when_incomplete=error_when_incomplete, simplify=simplify,
181
+ **flags)
182
+ return _eigenvals_dict(
183
+ M, error_when_incomplete=error_when_incomplete, simplify=simplify,
184
+ **flags)
185
+
186
+
187
+ eigenvals_error_message = \
188
+ "It is not always possible to express the eigenvalues of a matrix " + \
189
+ "of size 5x5 or higher in radicals. " + \
190
+ "We have CRootOf, but domains other than the rationals are not " + \
191
+ "currently supported. " + \
192
+ "If there are no symbols in the matrix, " + \
193
+ "it should still be possible to compute numeric approximations " + \
194
+ "of the eigenvalues using " + \
195
+ "M.evalf().eigenvals() or M.charpoly().nroots()."
196
+
197
+
198
+ def _eigenvals_list(
199
+ M, error_when_incomplete=True, simplify=False, **flags):
200
+ iblocks = M.strongly_connected_components()
201
+ all_eigs = []
202
+ is_dom = M._rep.domain in (ZZ, QQ)
203
+ for b in iblocks:
204
+
205
+ # Fast path for a 1x1 block:
206
+ if is_dom and len(b) == 1:
207
+ index = b[0]
208
+ val = M[index, index]
209
+ all_eigs.append(val)
210
+ continue
211
+
212
+ block = M[b, b]
213
+
214
+ if isinstance(simplify, FunctionType):
215
+ charpoly = block.charpoly(simplify=simplify)
216
+ else:
217
+ charpoly = block.charpoly()
218
+
219
+ eigs = roots(charpoly, multiple=True, **flags)
220
+
221
+ if len(eigs) != block.rows:
222
+ try:
223
+ eigs = charpoly.all_roots(multiple=True)
224
+ except NotImplementedError:
225
+ if error_when_incomplete:
226
+ raise MatrixError(eigenvals_error_message)
227
+ else:
228
+ eigs = []
229
+
230
+ all_eigs += eigs
231
+
232
+ if not simplify:
233
+ return all_eigs
234
+ if not isinstance(simplify, FunctionType):
235
+ simplify = _simplify
236
+ return [simplify(value) for value in all_eigs]
237
+
238
+
239
+ def _eigenvals_dict(
240
+ M, error_when_incomplete=True, simplify=False, **flags):
241
+ iblocks = M.strongly_connected_components()
242
+ all_eigs = {}
243
+ is_dom = M._rep.domain in (ZZ, QQ)
244
+ for b in iblocks:
245
+
246
+ # Fast path for a 1x1 block:
247
+ if is_dom and len(b) == 1:
248
+ index = b[0]
249
+ val = M[index, index]
250
+ all_eigs[val] = all_eigs.get(val, 0) + 1
251
+ continue
252
+
253
+ block = M[b, b]
254
+
255
+ if isinstance(simplify, FunctionType):
256
+ charpoly = block.charpoly(simplify=simplify)
257
+ else:
258
+ charpoly = block.charpoly()
259
+
260
+ eigs = roots(charpoly, multiple=False, **flags)
261
+
262
+ if sum(eigs.values()) != block.rows:
263
+ try:
264
+ eigs = dict(charpoly.all_roots(multiple=False))
265
+ except NotImplementedError:
266
+ if error_when_incomplete:
267
+ raise MatrixError(eigenvals_error_message)
268
+ else:
269
+ eigs = {}
270
+
271
+ for k, v in eigs.items():
272
+ if k in all_eigs:
273
+ all_eigs[k] += v
274
+ else:
275
+ all_eigs[k] = v
276
+
277
+ if not simplify:
278
+ return all_eigs
279
+ if not isinstance(simplify, FunctionType):
280
+ simplify = _simplify
281
+ return {simplify(key): value for key, value in all_eigs.items()}
282
+
283
+
284
+ def _eigenspace(M, eigenval, iszerofunc=_iszero, simplify=False):
285
+ """Get a basis for the eigenspace for a particular eigenvalue"""
286
+ m = M - M.eye(M.rows) * eigenval
287
+ ret = m.nullspace(iszerofunc=iszerofunc)
288
+
289
+ # The nullspace for a real eigenvalue should be non-trivial.
290
+ # If we didn't find an eigenvector, try once more a little harder
291
+ if len(ret) == 0 and simplify:
292
+ ret = m.nullspace(iszerofunc=iszerofunc, simplify=True)
293
+ if len(ret) == 0:
294
+ raise NotImplementedError(
295
+ "Can't evaluate eigenvector for eigenvalue {}".format(eigenval))
296
+ return ret
297
+
298
+
299
+ def _eigenvects_DOM(M, **kwargs):
300
+ DOM = DomainMatrix.from_Matrix(M, field=True, extension=True)
301
+ DOM = DOM.to_dense()
302
+
303
+ if DOM.domain != EX:
304
+ rational, algebraic = dom_eigenvects(DOM)
305
+ eigenvects = dom_eigenvects_to_sympy(
306
+ rational, algebraic, M.__class__, **kwargs)
307
+ eigenvects = sorted(eigenvects, key=lambda x: default_sort_key(x[0]))
308
+
309
+ return eigenvects
310
+ return None
311
+
312
+
313
+ def _eigenvects_sympy(M, iszerofunc, simplify=True, **flags):
314
+ eigenvals = M.eigenvals(rational=False, **flags)
315
+
316
+ # Make sure that we have all roots in radical form
317
+ for x in eigenvals:
318
+ if x.has(CRootOf):
319
+ raise MatrixError(
320
+ "Eigenvector computation is not implemented if the matrix have "
321
+ "eigenvalues in CRootOf form")
322
+
323
+ eigenvals = sorted(eigenvals.items(), key=default_sort_key)
324
+ ret = []
325
+ for val, mult in eigenvals:
326
+ vects = _eigenspace(M, val, iszerofunc=iszerofunc, simplify=simplify)
327
+ ret.append((val, mult, vects))
328
+ return ret
329
+
330
+
331
+ # This functions is a candidate for caching if it gets implemented for matrices.
332
+ def _eigenvects(M, error_when_incomplete=True, iszerofunc=_iszero, *, chop=False, **flags):
333
+ """Compute eigenvectors of the matrix.
334
+
335
+ Parameters
336
+ ==========
337
+
338
+ error_when_incomplete : bool, optional
339
+ Raise an error when not all eigenvalues are computed. This is
340
+ caused by ``roots`` not returning a full list of eigenvalues.
341
+
342
+ iszerofunc : function, optional
343
+ Specifies a zero testing function to be used in ``rref``.
344
+
345
+ Default value is ``_iszero``, which uses SymPy's naive and fast
346
+ default assumption handler.
347
+
348
+ It can also accept any user-specified zero testing function, if it
349
+ is formatted as a function which accepts a single symbolic argument
350
+ and returns ``True`` if it is tested as zero and ``False`` if it
351
+ is tested as non-zero, and ``None`` if it is undecidable.
352
+
353
+ simplify : bool or function, optional
354
+ If ``True``, ``as_content_primitive()`` will be used to tidy up
355
+ normalization artifacts.
356
+
357
+ It will also be used by the ``nullspace`` routine.
358
+
359
+ chop : bool or positive number, optional
360
+ If the matrix contains any Floats, they will be changed to Rationals
361
+ for computation purposes, but the answers will be returned after
362
+ being evaluated with evalf. The ``chop`` flag is passed to ``evalf``.
363
+ When ``chop=True`` a default precision will be used; a number will
364
+ be interpreted as the desired level of precision.
365
+
366
+ Returns
367
+ =======
368
+
369
+ ret : [(eigenval, multiplicity, eigenspace), ...]
370
+ A ragged list containing tuples of data obtained by ``eigenvals``
371
+ and ``nullspace``.
372
+
373
+ ``eigenspace`` is a list containing the ``eigenvector`` for each
374
+ eigenvalue.
375
+
376
+ ``eigenvector`` is a vector in the form of a ``Matrix``. e.g.
377
+ a vector of length 3 is returned as ``Matrix([a_1, a_2, a_3])``.
378
+
379
+ Raises
380
+ ======
381
+
382
+ NotImplementedError
383
+ If failed to compute nullspace.
384
+
385
+ Examples
386
+ ========
387
+
388
+ >>> from sympy import Matrix
389
+ >>> M = Matrix(3, 3, [0, 1, 1, 1, 0, 0, 1, 1, 1])
390
+ >>> M.eigenvects()
391
+ [(-1, 1, [Matrix([
392
+ [-1],
393
+ [ 1],
394
+ [ 0]])]), (0, 1, [Matrix([
395
+ [ 0],
396
+ [-1],
397
+ [ 1]])]), (2, 1, [Matrix([
398
+ [2/3],
399
+ [1/3],
400
+ [ 1]])])]
401
+
402
+ See Also
403
+ ========
404
+
405
+ eigenvals
406
+ MatrixBase.nullspace
407
+ """
408
+ simplify = flags.get('simplify', True)
409
+ primitive = flags.get('simplify', False)
410
+ flags.pop('simplify', None) # remove this if it's there
411
+ flags.pop('multiple', None) # remove this if it's there
412
+
413
+ if not isinstance(simplify, FunctionType):
414
+ simpfunc = _simplify if simplify else lambda x: x
415
+
416
+ has_floats = M.has(Float)
417
+ if has_floats:
418
+ if all(x.is_number for x in M):
419
+ return _eigenvects_mpmath(M)
420
+ from sympy.simplify import nsimplify
421
+ M = M.applyfunc(lambda x: nsimplify(x, rational=True))
422
+
423
+ ret = _eigenvects_DOM(M)
424
+ if ret is None:
425
+ ret = _eigenvects_sympy(M, iszerofunc, simplify=simplify, **flags)
426
+
427
+ if primitive:
428
+ # if the primitive flag is set, get rid of any common
429
+ # integer denominators
430
+ def denom_clean(l):
431
+ return [(v / gcd(list(v))).applyfunc(simpfunc) for v in l]
432
+
433
+ ret = [(val, mult, denom_clean(es)) for val, mult, es in ret]
434
+
435
+ if has_floats:
436
+ # if we had floats to start with, turn the eigenvectors to floats
437
+ ret = [(val.evalf(chop=chop), mult, [v.evalf(chop=chop) for v in es])
438
+ for val, mult, es in ret]
439
+
440
+ return ret
441
+
442
+
443
+ def _is_diagonalizable_with_eigen(M, reals_only=False):
444
+ """See _is_diagonalizable. This function returns the bool along with the
445
+ eigenvectors to avoid calculating them again in functions like
446
+ ``diagonalize``."""
447
+
448
+ if not M.is_square:
449
+ return False, []
450
+
451
+ eigenvecs = M.eigenvects(simplify=True)
452
+
453
+ for val, mult, basis in eigenvecs:
454
+ if reals_only and not val.is_real: # if we have a complex eigenvalue
455
+ return False, eigenvecs
456
+
457
+ if mult != len(basis): # if the geometric multiplicity doesn't equal the algebraic
458
+ return False, eigenvecs
459
+
460
+ return True, eigenvecs
461
+
462
+ def _is_diagonalizable(M, reals_only=False, **kwargs):
463
+ """Returns ``True`` if a matrix is diagonalizable.
464
+
465
+ Parameters
466
+ ==========
467
+
468
+ reals_only : bool, optional
469
+ If ``True``, it tests whether the matrix can be diagonalized
470
+ to contain only real numbers on the diagonal.
471
+
472
+
473
+ If ``False``, it tests whether the matrix can be diagonalized
474
+ at all, even with numbers that may not be real.
475
+
476
+ Examples
477
+ ========
478
+
479
+ Example of a diagonalizable matrix:
480
+
481
+ >>> from sympy import Matrix
482
+ >>> M = Matrix([[1, 2, 0], [0, 3, 0], [2, -4, 2]])
483
+ >>> M.is_diagonalizable()
484
+ True
485
+
486
+ Example of a non-diagonalizable matrix:
487
+
488
+ >>> M = Matrix([[0, 1], [0, 0]])
489
+ >>> M.is_diagonalizable()
490
+ False
491
+
492
+ Example of a matrix that is diagonalized in terms of non-real entries:
493
+
494
+ >>> M = Matrix([[0, 1], [-1, 0]])
495
+ >>> M.is_diagonalizable(reals_only=False)
496
+ True
497
+ >>> M.is_diagonalizable(reals_only=True)
498
+ False
499
+
500
+ See Also
501
+ ========
502
+
503
+ sympy.matrices.matrixbase.MatrixBase.is_diagonal
504
+ diagonalize
505
+ """
506
+ if not M.is_square:
507
+ return False
508
+
509
+ if all(e.is_real for e in M) and M.is_symmetric():
510
+ return True
511
+
512
+ if all(e.is_complex for e in M) and M.is_hermitian:
513
+ return True
514
+
515
+ return _is_diagonalizable_with_eigen(M, reals_only=reals_only)[0]
516
+
517
+
518
+ #G&VL, Matrix Computations, Algo 5.4.2
519
+ def _householder_vector(x):
520
+ if not x.cols == 1:
521
+ raise ValueError("Input must be a column matrix")
522
+ v = x.copy()
523
+ v_plus = x.copy()
524
+ v_minus = x.copy()
525
+ q = x[0, 0] / abs(x[0, 0])
526
+ norm_x = x.norm()
527
+ v_plus[0, 0] = x[0, 0] + q * norm_x
528
+ v_minus[0, 0] = x[0, 0] - q * norm_x
529
+ if x[1:, 0].norm() == 0:
530
+ bet = 0
531
+ v[0, 0] = 1
532
+ else:
533
+ if v_plus.norm() <= v_minus.norm():
534
+ v = v_plus
535
+ else:
536
+ v = v_minus
537
+ v = v / v[0]
538
+ bet = 2 / (v.norm() ** 2)
539
+ return v, bet
540
+
541
+
542
+ def _bidiagonal_decmp_hholder(M):
543
+ m = M.rows
544
+ n = M.cols
545
+ A = M.as_mutable()
546
+ U, V = A.eye(m), A.eye(n)
547
+ for i in range(min(m, n)):
548
+ v, bet = _householder_vector(A[i:, i])
549
+ hh_mat = A.eye(m - i) - bet * v * v.H
550
+ A[i:, i:] = hh_mat * A[i:, i:]
551
+ temp = A.eye(m)
552
+ temp[i:, i:] = hh_mat
553
+ U = U * temp
554
+ if i + 1 <= n - 2:
555
+ v, bet = _householder_vector(A[i, i+1:].T)
556
+ hh_mat = A.eye(n - i - 1) - bet * v * v.H
557
+ A[i:, i+1:] = A[i:, i+1:] * hh_mat
558
+ temp = A.eye(n)
559
+ temp[i+1:, i+1:] = hh_mat
560
+ V = temp * V
561
+ return U, A, V
562
+
563
+
564
+ def _eval_bidiag_hholder(M):
565
+ m = M.rows
566
+ n = M.cols
567
+ A = M.as_mutable()
568
+ for i in range(min(m, n)):
569
+ v, bet = _householder_vector(A[i:, i])
570
+ hh_mat = A.eye(m-i) - bet * v * v.H
571
+ A[i:, i:] = hh_mat * A[i:, i:]
572
+ if i + 1 <= n - 2:
573
+ v, bet = _householder_vector(A[i, i+1:].T)
574
+ hh_mat = A.eye(n - i - 1) - bet * v * v.H
575
+ A[i:, i+1:] = A[i:, i+1:] * hh_mat
576
+ return A
577
+
578
+
579
+ def _bidiagonal_decomposition(M, upper=True):
580
+ """
581
+ Returns $(U,B,V.H)$ for
582
+
583
+ $$A = UBV^{H}$$
584
+
585
+ where $A$ is the input matrix, and $B$ is its Bidiagonalized form
586
+
587
+ Note: Bidiagonal Computation can hang for symbolic matrices.
588
+
589
+ Parameters
590
+ ==========
591
+
592
+ upper : bool. Whether to do upper bidiagnalization or lower.
593
+ True for upper and False for lower.
594
+
595
+ References
596
+ ==========
597
+
598
+ .. [1] Algorithm 5.4.2, Matrix computations by Golub and Van Loan, 4th edition
599
+ .. [2] Complex Matrix Bidiagonalization, https://github.com/vslobody/Householder-Bidiagonalization
600
+
601
+ """
602
+
603
+ if not isinstance(upper, bool):
604
+ raise ValueError("upper must be a boolean")
605
+
606
+ if upper:
607
+ return _bidiagonal_decmp_hholder(M)
608
+
609
+ X = _bidiagonal_decmp_hholder(M.H)
610
+ return X[2].H, X[1].H, X[0].H
611
+
612
+
613
+ def _bidiagonalize(M, upper=True):
614
+ """
615
+ Returns $B$, the Bidiagonalized form of the input matrix.
616
+
617
+ Note: Bidiagonal Computation can hang for symbolic matrices.
618
+
619
+ Parameters
620
+ ==========
621
+
622
+ upper : bool. Whether to do upper bidiagnalization or lower.
623
+ True for upper and False for lower.
624
+
625
+ References
626
+ ==========
627
+
628
+ .. [1] Algorithm 5.4.2, Matrix computations by Golub and Van Loan, 4th edition
629
+ .. [2] Complex Matrix Bidiagonalization : https://github.com/vslobody/Householder-Bidiagonalization
630
+
631
+ """
632
+
633
+ if not isinstance(upper, bool):
634
+ raise ValueError("upper must be a boolean")
635
+
636
+ if upper:
637
+ return _eval_bidiag_hholder(M)
638
+ return _eval_bidiag_hholder(M.H).H
639
+
640
+
641
+ def _diagonalize(M, reals_only=False, sort=False, normalize=False):
642
+ """
643
+ Return (P, D), where D is diagonal and
644
+
645
+ D = P^-1 * M * P
646
+
647
+ where M is current matrix.
648
+
649
+ Parameters
650
+ ==========
651
+
652
+ reals_only : bool. Whether to throw an error if complex numbers are need
653
+ to diagonalize. (Default: False)
654
+
655
+ sort : bool. Sort the eigenvalues along the diagonal. (Default: False)
656
+
657
+ normalize : bool. If True, normalize the columns of P. (Default: False)
658
+
659
+ Examples
660
+ ========
661
+
662
+ >>> from sympy import Matrix
663
+ >>> M = Matrix(3, 3, [1, 2, 0, 0, 3, 0, 2, -4, 2])
664
+ >>> M
665
+ Matrix([
666
+ [1, 2, 0],
667
+ [0, 3, 0],
668
+ [2, -4, 2]])
669
+ >>> (P, D) = M.diagonalize()
670
+ >>> D
671
+ Matrix([
672
+ [1, 0, 0],
673
+ [0, 2, 0],
674
+ [0, 0, 3]])
675
+ >>> P
676
+ Matrix([
677
+ [-1, 0, -1],
678
+ [ 0, 0, -1],
679
+ [ 2, 1, 2]])
680
+ >>> P.inv() * M * P
681
+ Matrix([
682
+ [1, 0, 0],
683
+ [0, 2, 0],
684
+ [0, 0, 3]])
685
+
686
+ See Also
687
+ ========
688
+
689
+ sympy.matrices.matrixbase.MatrixBase.is_diagonal
690
+ is_diagonalizable
691
+ """
692
+
693
+ if not M.is_square:
694
+ raise NonSquareMatrixError()
695
+
696
+ is_diagonalizable, eigenvecs = _is_diagonalizable_with_eigen(M,
697
+ reals_only=reals_only)
698
+
699
+ if not is_diagonalizable:
700
+ raise MatrixError("Matrix is not diagonalizable")
701
+
702
+ if sort:
703
+ eigenvecs = sorted(eigenvecs, key=default_sort_key)
704
+
705
+ p_cols, diag = [], []
706
+
707
+ for val, mult, basis in eigenvecs:
708
+ diag += [val] * mult
709
+ p_cols += basis
710
+
711
+ if normalize:
712
+ p_cols = [v / v.norm() for v in p_cols]
713
+
714
+ return M.hstack(*p_cols), M.diag(*diag)
715
+
716
+
717
+ def _fuzzy_positive_definite(M):
718
+ positive_diagonals = M._has_positive_diagonals()
719
+ if positive_diagonals is False:
720
+ return False
721
+
722
+ if positive_diagonals and M.is_strongly_diagonally_dominant:
723
+ return True
724
+
725
+ return None
726
+
727
+
728
+ def _fuzzy_positive_semidefinite(M):
729
+ nonnegative_diagonals = M._has_nonnegative_diagonals()
730
+ if nonnegative_diagonals is False:
731
+ return False
732
+
733
+ if nonnegative_diagonals and M.is_weakly_diagonally_dominant:
734
+ return True
735
+
736
+ return None
737
+
738
+
739
+ def _is_positive_definite(M):
740
+ if not M.is_hermitian:
741
+ if not M.is_square:
742
+ return False
743
+ M = M + M.H
744
+
745
+ fuzzy = _fuzzy_positive_definite(M)
746
+ if fuzzy is not None:
747
+ return fuzzy
748
+
749
+ return _is_positive_definite_GE(M)
750
+
751
+
752
+ def _is_positive_semidefinite(M):
753
+ if not M.is_hermitian:
754
+ if not M.is_square:
755
+ return False
756
+ M = M + M.H
757
+
758
+ fuzzy = _fuzzy_positive_semidefinite(M)
759
+ if fuzzy is not None:
760
+ return fuzzy
761
+
762
+ return _is_positive_semidefinite_cholesky(M)
763
+
764
+
765
+ def _is_negative_definite(M):
766
+ return _is_positive_definite(-M)
767
+
768
+
769
+ def _is_negative_semidefinite(M):
770
+ return _is_positive_semidefinite(-M)
771
+
772
+
773
+ def _is_indefinite(M):
774
+ if M.is_hermitian:
775
+ eigen = M.eigenvals()
776
+ args1 = [x.is_positive for x in eigen.keys()]
777
+ any_positive = fuzzy_or(args1)
778
+ args2 = [x.is_negative for x in eigen.keys()]
779
+ any_negative = fuzzy_or(args2)
780
+
781
+ return fuzzy_and([any_positive, any_negative])
782
+
783
+ elif M.is_square:
784
+ return (M + M.H).is_indefinite
785
+
786
+ return False
787
+
788
+
789
+ def _is_positive_definite_GE(M):
790
+ """A division-free gaussian elimination method for testing
791
+ positive-definiteness."""
792
+ M = M.as_mutable()
793
+ size = M.rows
794
+
795
+ for i in range(size):
796
+ is_positive = M[i, i].is_positive
797
+ if is_positive is not True:
798
+ return is_positive
799
+ for j in range(i+1, size):
800
+ M[j, i+1:] = M[i, i] * M[j, i+1:] - M[j, i] * M[i, i+1:]
801
+ return True
802
+
803
+
804
+ def _is_positive_semidefinite_cholesky(M):
805
+ """Uses Cholesky factorization with complete pivoting
806
+
807
+ References
808
+ ==========
809
+
810
+ .. [1] http://eprints.ma.man.ac.uk/1199/1/covered/MIMS_ep2008_116.pdf
811
+
812
+ .. [2] https://www.value-at-risk.net/cholesky-factorization/
813
+ """
814
+ M = M.as_mutable()
815
+ for k in range(M.rows):
816
+ diags = [M[i, i] for i in range(k, M.rows)]
817
+ pivot, pivot_val, nonzero, _ = _find_reasonable_pivot(diags)
818
+
819
+ if nonzero:
820
+ return None
821
+
822
+ if pivot is None:
823
+ for i in range(k+1, M.rows):
824
+ for j in range(k, M.cols):
825
+ iszero = M[i, j].is_zero
826
+ if iszero is None:
827
+ return None
828
+ elif iszero is False:
829
+ return False
830
+ return True
831
+
832
+ if M[k, k].is_negative or pivot_val.is_negative:
833
+ return False
834
+ elif not (M[k, k].is_nonnegative and pivot_val.is_nonnegative):
835
+ return None
836
+
837
+ if pivot > 0:
838
+ M.col_swap(k, k+pivot)
839
+ M.row_swap(k, k+pivot)
840
+
841
+ M[k, k] = sqrt(M[k, k])
842
+ M[k, k+1:] /= M[k, k]
843
+ M[k+1:, k+1:] -= M[k, k+1:].H * M[k, k+1:]
844
+
845
+ return M[-1, -1].is_nonnegative
846
+
847
+
848
+ _doc_positive_definite = \
849
+ r"""Finds out the definiteness of a matrix.
850
+
851
+ Explanation
852
+ ===========
853
+
854
+ A square real matrix $A$ is:
855
+
856
+ - A positive definite matrix if $x^T A x > 0$
857
+ for all non-zero real vectors $x$.
858
+ - A positive semidefinite matrix if $x^T A x \geq 0$
859
+ for all non-zero real vectors $x$.
860
+ - A negative definite matrix if $x^T A x < 0$
861
+ for all non-zero real vectors $x$.
862
+ - A negative semidefinite matrix if $x^T A x \leq 0$
863
+ for all non-zero real vectors $x$.
864
+ - An indefinite matrix if there exists non-zero real vectors
865
+ $x, y$ with $x^T A x > 0 > y^T A y$.
866
+
867
+ A square complex matrix $A$ is:
868
+
869
+ - A positive definite matrix if $\text{re}(x^H A x) > 0$
870
+ for all non-zero complex vectors $x$.
871
+ - A positive semidefinite matrix if $\text{re}(x^H A x) \geq 0$
872
+ for all non-zero complex vectors $x$.
873
+ - A negative definite matrix if $\text{re}(x^H A x) < 0$
874
+ for all non-zero complex vectors $x$.
875
+ - A negative semidefinite matrix if $\text{re}(x^H A x) \leq 0$
876
+ for all non-zero complex vectors $x$.
877
+ - An indefinite matrix if there exists non-zero complex vectors
878
+ $x, y$ with $\text{re}(x^H A x) > 0 > \text{re}(y^H A y)$.
879
+
880
+ A matrix need not be symmetric or hermitian to be positive definite.
881
+
882
+ - A real non-symmetric matrix is positive definite if and only if
883
+ $\frac{A + A^T}{2}$ is positive definite.
884
+ - A complex non-hermitian matrix is positive definite if and only if
885
+ $\frac{A + A^H}{2}$ is positive definite.
886
+
887
+ And this extension can apply for all the definitions above.
888
+
889
+ However, for complex cases, you can restrict the definition of
890
+ $\text{re}(x^H A x) > 0$ to $x^H A x > 0$ and require the matrix
891
+ to be hermitian.
892
+ But we do not present this restriction for computation because you
893
+ can check ``M.is_hermitian`` independently with this and use
894
+ the same procedure.
895
+
896
+ Examples
897
+ ========
898
+
899
+ An example of symmetric positive definite matrix:
900
+
901
+ .. plot::
902
+ :context: reset
903
+ :format: doctest
904
+ :include-source: True
905
+
906
+ >>> from sympy import Matrix, symbols
907
+ >>> from sympy.plotting import plot3d
908
+ >>> a, b = symbols('a b')
909
+ >>> x = Matrix([a, b])
910
+
911
+ >>> A = Matrix([[1, 0], [0, 1]])
912
+ >>> A.is_positive_definite
913
+ True
914
+ >>> A.is_positive_semidefinite
915
+ True
916
+
917
+ >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
918
+
919
+ An example of symmetric positive semidefinite matrix:
920
+
921
+ .. plot::
922
+ :context: close-figs
923
+ :format: doctest
924
+ :include-source: True
925
+
926
+ >>> A = Matrix([[1, -1], [-1, 1]])
927
+ >>> A.is_positive_definite
928
+ False
929
+ >>> A.is_positive_semidefinite
930
+ True
931
+
932
+ >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
933
+
934
+ An example of symmetric negative definite matrix:
935
+
936
+ .. plot::
937
+ :context: close-figs
938
+ :format: doctest
939
+ :include-source: True
940
+
941
+ >>> A = Matrix([[-1, 0], [0, -1]])
942
+ >>> A.is_negative_definite
943
+ True
944
+ >>> A.is_negative_semidefinite
945
+ True
946
+ >>> A.is_indefinite
947
+ False
948
+
949
+ >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
950
+
951
+ An example of symmetric indefinite matrix:
952
+
953
+ .. plot::
954
+ :context: close-figs
955
+ :format: doctest
956
+ :include-source: True
957
+
958
+ >>> A = Matrix([[1, 2], [2, -1]])
959
+ >>> A.is_indefinite
960
+ True
961
+
962
+ >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
963
+
964
+ An example of non-symmetric positive definite matrix.
965
+
966
+ .. plot::
967
+ :context: close-figs
968
+ :format: doctest
969
+ :include-source: True
970
+
971
+ >>> A = Matrix([[1, 2], [-2, 1]])
972
+ >>> A.is_positive_definite
973
+ True
974
+ >>> A.is_positive_semidefinite
975
+ True
976
+
977
+ >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))
978
+
979
+ Notes
980
+ =====
981
+
982
+ Although some people trivialize the definition of positive definite
983
+ matrices only for symmetric or hermitian matrices, this restriction
984
+ is not correct because it does not classify all instances of
985
+ positive definite matrices from the definition $x^T A x > 0$ or
986
+ $\text{re}(x^H A x) > 0$.
987
+
988
+ For instance, ``Matrix([[1, 2], [-2, 1]])`` presented in
989
+ the example above is an example of real positive definite matrix
990
+ that is not symmetric.
991
+
992
+ However, since the following formula holds true;
993
+
994
+ .. math::
995
+ \text{re}(x^H A x) > 0 \iff
996
+ \text{re}(x^H \frac{A + A^H}{2} x) > 0
997
+
998
+ We can classify all positive definite matrices that may or may not
999
+ be symmetric or hermitian by transforming the matrix to
1000
+ $\frac{A + A^T}{2}$ or $\frac{A + A^H}{2}$
1001
+ (which is guaranteed to be always real symmetric or complex
1002
+ hermitian) and we can defer most of the studies to symmetric or
1003
+ hermitian positive definite matrices.
1004
+
1005
+ But it is a different problem for the existence of Cholesky
1006
+ decomposition. Because even though a non symmetric or a non
1007
+ hermitian matrix can be positive definite, Cholesky or LDL
1008
+ decomposition does not exist because the decompositions require the
1009
+ matrix to be symmetric or hermitian.
1010
+
1011
+ References
1012
+ ==========
1013
+
1014
+ .. [1] https://en.wikipedia.org/wiki/Definiteness_of_a_matrix#Eigenvalues
1015
+
1016
+ .. [2] https://mathworld.wolfram.com/PositiveDefiniteMatrix.html
1017
+
1018
+ .. [3] Johnson, C. R. "Positive Definite Matrices." Amer.
1019
+ Math. Monthly 77, 259-264 1970.
1020
+ """
1021
+
1022
+ _is_positive_definite.__doc__ = _doc_positive_definite
1023
+ _is_positive_semidefinite.__doc__ = _doc_positive_definite
1024
+ _is_negative_definite.__doc__ = _doc_positive_definite
1025
+ _is_negative_semidefinite.__doc__ = _doc_positive_definite
1026
+ _is_indefinite.__doc__ = _doc_positive_definite
1027
+
1028
+
1029
+ def _jordan_form(M, calc_transform=True, *, chop=False):
1030
+ """Return $(P, J)$ where $J$ is a Jordan block
1031
+ matrix and $P$ is a matrix such that $M = P J P^{-1}$
1032
+
1033
+ Parameters
1034
+ ==========
1035
+
1036
+ calc_transform : bool
1037
+ If ``False``, then only $J$ is returned.
1038
+
1039
+ chop : bool
1040
+ All matrices are converted to exact types when computing
1041
+ eigenvalues and eigenvectors. As a result, there may be
1042
+ approximation errors. If ``chop==True``, these errors
1043
+ will be truncated.
1044
+
1045
+ Examples
1046
+ ========
1047
+
1048
+ >>> from sympy import Matrix
1049
+ >>> M = Matrix([[ 6, 5, -2, -3], [-3, -1, 3, 3], [ 2, 1, -2, -3], [-1, 1, 5, 5]])
1050
+ >>> P, J = M.jordan_form()
1051
+ >>> J
1052
+ Matrix([
1053
+ [2, 1, 0, 0],
1054
+ [0, 2, 0, 0],
1055
+ [0, 0, 2, 1],
1056
+ [0, 0, 0, 2]])
1057
+
1058
+ See Also
1059
+ ========
1060
+
1061
+ jordan_block
1062
+ """
1063
+
1064
+ if not M.is_square:
1065
+ raise NonSquareMatrixError("Only square matrices have Jordan forms")
1066
+
1067
+ mat = M
1068
+ has_floats = M.has(Float)
1069
+
1070
+ if has_floats:
1071
+ try:
1072
+ max_prec = max(term._prec for term in M.values() if isinstance(term, Float))
1073
+ except ValueError:
1074
+ # if no term in the matrix is explicitly a Float calling max()
1075
+ # will throw a error so setting max_prec to default value of 53
1076
+ max_prec = 53
1077
+
1078
+ # setting minimum max_dps to 15 to prevent loss of precision in
1079
+ # matrix containing non evaluated expressions
1080
+ max_dps = max(prec_to_dps(max_prec), 15)
1081
+
1082
+ def restore_floats(*args):
1083
+ """If ``has_floats`` is `True`, cast all ``args`` as
1084
+ matrices of floats."""
1085
+
1086
+ if has_floats:
1087
+ args = [m.evalf(n=max_dps, chop=chop) for m in args]
1088
+ if len(args) == 1:
1089
+ return args[0]
1090
+
1091
+ return args
1092
+
1093
+ # cache calculations for some speedup
1094
+ mat_cache = {}
1095
+
1096
+ def eig_mat(val, pow):
1097
+ """Cache computations of ``(M - val*I)**pow`` for quick
1098
+ retrieval"""
1099
+
1100
+ if (val, pow) in mat_cache:
1101
+ return mat_cache[(val, pow)]
1102
+
1103
+ if (val, pow - 1) in mat_cache:
1104
+ mat_cache[(val, pow)] = mat_cache[(val, pow - 1)].multiply(
1105
+ mat_cache[(val, 1)], dotprodsimp=None)
1106
+ else:
1107
+ mat_cache[(val, pow)] = (mat - val*M.eye(M.rows)).pow(pow)
1108
+
1109
+ return mat_cache[(val, pow)]
1110
+
1111
+ # helper functions
1112
+ def nullity_chain(val, algebraic_multiplicity):
1113
+ """Calculate the sequence [0, nullity(E), nullity(E**2), ...]
1114
+ until it is constant where ``E = M - val*I``"""
1115
+
1116
+ # mat.rank() is faster than computing the null space,
1117
+ # so use the rank-nullity theorem
1118
+ cols = M.cols
1119
+ ret = [0]
1120
+ nullity = cols - eig_mat(val, 1).rank()
1121
+ i = 2
1122
+
1123
+ while nullity != ret[-1]:
1124
+ ret.append(nullity)
1125
+
1126
+ if nullity == algebraic_multiplicity:
1127
+ break
1128
+
1129
+ nullity = cols - eig_mat(val, i).rank()
1130
+ i += 1
1131
+
1132
+ # Due to issues like #7146 and #15872, SymPy sometimes
1133
+ # gives the wrong rank. In this case, raise an error
1134
+ # instead of returning an incorrect matrix
1135
+ if nullity < ret[-1] or nullity > algebraic_multiplicity:
1136
+ raise MatrixError(
1137
+ "SymPy had encountered an inconsistent "
1138
+ "result while computing Jordan block: "
1139
+ "{}".format(M))
1140
+
1141
+ return ret
1142
+
1143
+ def blocks_from_nullity_chain(d):
1144
+ """Return a list of the size of each Jordan block.
1145
+ If d_n is the nullity of E**n, then the number
1146
+ of Jordan blocks of size n is
1147
+
1148
+ 2*d_n - d_(n-1) - d_(n+1)"""
1149
+
1150
+ # d[0] is always the number of columns, so skip past it
1151
+ mid = [2*d[n] - d[n - 1] - d[n + 1] for n in range(1, len(d) - 1)]
1152
+ # d is assumed to plateau with "d[ len(d) ] == d[-1]", so
1153
+ # 2*d_n - d_(n-1) - d_(n+1) == d_n - d_(n-1)
1154
+ end = [d[-1] - d[-2]] if len(d) > 1 else [d[0]]
1155
+
1156
+ return mid + end
1157
+
1158
+ def pick_vec(small_basis, big_basis):
1159
+ """Picks a vector from big_basis that isn't in
1160
+ the subspace spanned by small_basis"""
1161
+
1162
+ if len(small_basis) == 0:
1163
+ return big_basis[0]
1164
+
1165
+ for v in big_basis:
1166
+ _, pivots = M.hstack(*(small_basis + [v])).echelon_form(
1167
+ with_pivots=True)
1168
+
1169
+ if pivots[-1] == len(small_basis):
1170
+ return v
1171
+
1172
+ # roots doesn't like Floats, so replace them with Rationals
1173
+ if has_floats:
1174
+ from sympy.simplify import nsimplify
1175
+ mat = mat.applyfunc(lambda x: nsimplify(x, rational=True))
1176
+
1177
+ # first calculate the jordan block structure
1178
+ eigs = mat.eigenvals()
1179
+
1180
+ # Make sure that we have all roots in radical form
1181
+ for x in eigs:
1182
+ if x.has(CRootOf):
1183
+ raise MatrixError(
1184
+ "Jordan normal form is not implemented if the matrix have "
1185
+ "eigenvalues in CRootOf form")
1186
+
1187
+ # most matrices have distinct eigenvalues
1188
+ # and so are diagonalizable. In this case, don't
1189
+ # do extra work!
1190
+ if len(eigs.keys()) == mat.cols:
1191
+ blocks = sorted(eigs.keys(), key=default_sort_key)
1192
+ jordan_mat = mat.diag(*blocks)
1193
+
1194
+ if not calc_transform:
1195
+ return restore_floats(jordan_mat)
1196
+
1197
+ jordan_basis = [eig_mat(eig, 1).nullspace()[0]
1198
+ for eig in blocks]
1199
+ basis_mat = mat.hstack(*jordan_basis)
1200
+
1201
+ return restore_floats(basis_mat, jordan_mat)
1202
+
1203
+ block_structure = []
1204
+
1205
+ for eig in sorted(eigs.keys(), key=default_sort_key):
1206
+ algebraic_multiplicity = eigs[eig]
1207
+ chain = nullity_chain(eig, algebraic_multiplicity)
1208
+ block_sizes = blocks_from_nullity_chain(chain)
1209
+
1210
+ # if block_sizes = = [a, b, c, ...], then the number of
1211
+ # Jordan blocks of size 1 is a, of size 2 is b, etc.
1212
+ # create an array that has (eig, block_size) with one
1213
+ # entry for each block
1214
+ size_nums = [(i+1, num) for i, num in enumerate(block_sizes)]
1215
+
1216
+ # we expect larger Jordan blocks to come earlier
1217
+ size_nums.reverse()
1218
+
1219
+ block_structure.extend(
1220
+ [(eig, size) for size, num in size_nums for _ in range(num)])
1221
+
1222
+ jordan_form_size = sum(size for eig, size in block_structure)
1223
+
1224
+ if jordan_form_size != M.rows:
1225
+ raise MatrixError(
1226
+ "SymPy had encountered an inconsistent result while "
1227
+ "computing Jordan block. : {}".format(M))
1228
+
1229
+ blocks = (mat.jordan_block(size=size, eigenvalue=eig) for eig, size in block_structure)
1230
+ jordan_mat = mat.diag(*blocks)
1231
+
1232
+ if not calc_transform:
1233
+ return restore_floats(jordan_mat)
1234
+
1235
+ # For each generalized eigenspace, calculate a basis.
1236
+ # We start by looking for a vector in null( (A - eig*I)**n )
1237
+ # which isn't in null( (A - eig*I)**(n-1) ) where n is
1238
+ # the size of the Jordan block
1239
+ #
1240
+ # Ideally we'd just loop through block_structure and
1241
+ # compute each generalized eigenspace. However, this
1242
+ # causes a lot of unneeded computation. Instead, we
1243
+ # go through the eigenvalues separately, since we know
1244
+ # their generalized eigenspaces must have bases that
1245
+ # are linearly independent.
1246
+ jordan_basis = []
1247
+
1248
+ for eig in sorted(eigs.keys(), key=default_sort_key):
1249
+ eig_basis = []
1250
+
1251
+ for block_eig, size in block_structure:
1252
+ if block_eig != eig:
1253
+ continue
1254
+
1255
+ null_big = (eig_mat(eig, size)).nullspace()
1256
+ null_small = (eig_mat(eig, size - 1)).nullspace()
1257
+
1258
+ # we want to pick something that is in the big basis
1259
+ # and not the small, but also something that is independent
1260
+ # of any other generalized eigenvectors from a different
1261
+ # generalized eigenspace sharing the same eigenvalue.
1262
+ vec = pick_vec(null_small + eig_basis, null_big)
1263
+ new_vecs = [eig_mat(eig, i).multiply(vec, dotprodsimp=None)
1264
+ for i in range(size)]
1265
+
1266
+ eig_basis.extend(new_vecs)
1267
+ jordan_basis.extend(reversed(new_vecs))
1268
+
1269
+ basis_mat = mat.hstack(*jordan_basis)
1270
+
1271
+ return restore_floats(basis_mat, jordan_mat)
1272
+
1273
+
1274
+ def _left_eigenvects(M, **flags):
1275
+ """Returns left eigenvectors and eigenvalues.
1276
+
1277
+ This function returns the list of triples (eigenval, multiplicity,
1278
+ basis) for the left eigenvectors. Options are the same as for
1279
+ eigenvects(), i.e. the ``**flags`` arguments gets passed directly to
1280
+ eigenvects().
1281
+
1282
+ Examples
1283
+ ========
1284
+
1285
+ >>> from sympy import Matrix
1286
+ >>> M = Matrix([[0, 1, 1], [1, 0, 0], [1, 1, 1]])
1287
+ >>> M.eigenvects()
1288
+ [(-1, 1, [Matrix([
1289
+ [-1],
1290
+ [ 1],
1291
+ [ 0]])]), (0, 1, [Matrix([
1292
+ [ 0],
1293
+ [-1],
1294
+ [ 1]])]), (2, 1, [Matrix([
1295
+ [2/3],
1296
+ [1/3],
1297
+ [ 1]])])]
1298
+ >>> M.left_eigenvects()
1299
+ [(-1, 1, [Matrix([[-2, 1, 1]])]), (0, 1, [Matrix([[-1, -1, 1]])]), (2,
1300
+ 1, [Matrix([[1, 1, 1]])])]
1301
+
1302
+ """
1303
+
1304
+ eigs = M.transpose().eigenvects(**flags)
1305
+
1306
+ return [(val, mult, [l.transpose() for l in basis]) for val, mult, basis in eigs]
1307
+
1308
+
1309
+ def _singular_values(M):
1310
+ """Compute the singular values of a Matrix
1311
+
1312
+ Examples
1313
+ ========
1314
+
1315
+ >>> from sympy import Matrix, Symbol
1316
+ >>> x = Symbol('x', real=True)
1317
+ >>> M = Matrix([[0, 1, 0], [0, x, 0], [-1, 0, 0]])
1318
+ >>> M.singular_values()
1319
+ [sqrt(x**2 + 1), 1, 0]
1320
+
1321
+ See Also
1322
+ ========
1323
+
1324
+ condition_number
1325
+ """
1326
+
1327
+ if M.rows >= M.cols:
1328
+ valmultpairs = M.H.multiply(M).eigenvals()
1329
+ else:
1330
+ valmultpairs = M.multiply(M.H).eigenvals()
1331
+
1332
+ # Expands result from eigenvals into a simple list
1333
+ vals = []
1334
+
1335
+ for k, v in valmultpairs.items():
1336
+ vals += [sqrt(k)] * v # dangerous! same k in several spots!
1337
+
1338
+ # Pad with zeros if singular values are computed in reverse way,
1339
+ # to give consistent format.
1340
+ if len(vals) < M.cols:
1341
+ vals += [M.zero] * (M.cols - len(vals))
1342
+
1343
+ # sort them in descending order
1344
+ vals.sort(reverse=True, key=default_sort_key)
1345
+
1346
+ return vals
openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_commonmatrix.py ADDED
@@ -0,0 +1,1266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Code for testing deprecated matrix classes. New test code should not be added
3
+ # here. Instead, add it to test_matrixbase.py.
4
+ #
5
+ # This entire test module and the corresponding sympy/matrices/common.py
6
+ # module will be removed in a future release.
7
+ #
8
+ from sympy.testing.pytest import raises, XFAIL, warns_deprecated_sympy
9
+
10
+ from sympy.assumptions import Q
11
+ from sympy.core.expr import Expr
12
+ from sympy.core.add import Add
13
+ from sympy.core.function import Function
14
+ from sympy.core.kind import NumberKind, UndefinedKind
15
+ from sympy.core.numbers import I, Integer, oo, pi, Rational
16
+ from sympy.core.singleton import S
17
+ from sympy.core.symbol import Symbol, symbols
18
+ from sympy.functions.elementary.complexes import Abs
19
+ from sympy.functions.elementary.exponential import exp
20
+ from sympy.functions.elementary.miscellaneous import sqrt
21
+ from sympy.functions.elementary.trigonometric import cos, sin
22
+ from sympy.matrices.exceptions import ShapeError, NonSquareMatrixError
23
+ from sympy.matrices.kind import MatrixKind
24
+ from sympy.matrices.common import (
25
+ _MinimalMatrix, _CastableMatrix, MatrixShaping, MatrixProperties,
26
+ MatrixOperations, MatrixArithmetic, MatrixSpecial)
27
+ from sympy.matrices.matrices import MatrixCalculus
28
+ from sympy.matrices import (Matrix, diag, eye,
29
+ matrix_multiply_elementwise, ones, zeros, SparseMatrix, banded,
30
+ MutableDenseMatrix, MutableSparseMatrix, ImmutableDenseMatrix,
31
+ ImmutableSparseMatrix)
32
+ from sympy.polys.polytools import Poly
33
+ from sympy.utilities.iterables import flatten
34
+ from sympy.tensor.array.dense_ndim_array import ImmutableDenseNDimArray as Array
35
+
36
+ from sympy.abc import x, y, z
37
+
38
+
39
+ def test_matrix_deprecated_isinstance():
40
+
41
+ # Test that e.g. isinstance(M, MatrixCommon) still gives True when M is a
42
+ # Matrix for each of the deprecated matrix classes.
43
+
44
+ from sympy.matrices.common import (
45
+ MatrixRequired,
46
+ MatrixShaping,
47
+ MatrixSpecial,
48
+ MatrixProperties,
49
+ MatrixOperations,
50
+ MatrixArithmetic,
51
+ MatrixCommon
52
+ )
53
+ from sympy.matrices.matrices import (
54
+ MatrixDeterminant,
55
+ MatrixReductions,
56
+ MatrixSubspaces,
57
+ MatrixEigen,
58
+ MatrixCalculus,
59
+ MatrixDeprecated
60
+ )
61
+ from sympy import (
62
+ Matrix,
63
+ ImmutableMatrix,
64
+ SparseMatrix,
65
+ ImmutableSparseMatrix
66
+ )
67
+ all_mixins = (
68
+ MatrixRequired,
69
+ MatrixShaping,
70
+ MatrixSpecial,
71
+ MatrixProperties,
72
+ MatrixOperations,
73
+ MatrixArithmetic,
74
+ MatrixCommon,
75
+ MatrixDeterminant,
76
+ MatrixReductions,
77
+ MatrixSubspaces,
78
+ MatrixEigen,
79
+ MatrixCalculus,
80
+ MatrixDeprecated
81
+ )
82
+ all_matrices = (
83
+ Matrix,
84
+ ImmutableMatrix,
85
+ SparseMatrix,
86
+ ImmutableSparseMatrix
87
+ )
88
+
89
+ Ms = [M([[1, 2], [3, 4]]) for M in all_matrices]
90
+ t = ()
91
+
92
+ for mixin in all_mixins:
93
+ for M in Ms:
94
+ with warns_deprecated_sympy():
95
+ assert isinstance(M, mixin) is True
96
+ with warns_deprecated_sympy():
97
+ assert isinstance(t, mixin) is False
98
+
99
+
100
+ # classes to test the deprecated matrix classes. We use warns_deprecated_sympy
101
+ # to suppress the deprecation warnings because subclassing the deprecated
102
+ # classes causes a warning to be raised.
103
+
104
+ with warns_deprecated_sympy():
105
+ class ShapingOnlyMatrix(_MinimalMatrix, _CastableMatrix, MatrixShaping):
106
+ pass
107
+
108
+
109
+ def eye_Shaping(n):
110
+ return ShapingOnlyMatrix(n, n, lambda i, j: int(i == j))
111
+
112
+
113
+ def zeros_Shaping(n):
114
+ return ShapingOnlyMatrix(n, n, lambda i, j: 0)
115
+
116
+
117
+ with warns_deprecated_sympy():
118
+ class PropertiesOnlyMatrix(_MinimalMatrix, _CastableMatrix, MatrixProperties):
119
+ pass
120
+
121
+
122
+ def eye_Properties(n):
123
+ return PropertiesOnlyMatrix(n, n, lambda i, j: int(i == j))
124
+
125
+
126
+ def zeros_Properties(n):
127
+ return PropertiesOnlyMatrix(n, n, lambda i, j: 0)
128
+
129
+
130
+ with warns_deprecated_sympy():
131
+ class OperationsOnlyMatrix(_MinimalMatrix, _CastableMatrix, MatrixOperations):
132
+ pass
133
+
134
+
135
+ def eye_Operations(n):
136
+ return OperationsOnlyMatrix(n, n, lambda i, j: int(i == j))
137
+
138
+
139
+ def zeros_Operations(n):
140
+ return OperationsOnlyMatrix(n, n, lambda i, j: 0)
141
+
142
+
143
+ with warns_deprecated_sympy():
144
+ class ArithmeticOnlyMatrix(_MinimalMatrix, _CastableMatrix, MatrixArithmetic):
145
+ pass
146
+
147
+
148
+ def eye_Arithmetic(n):
149
+ return ArithmeticOnlyMatrix(n, n, lambda i, j: int(i == j))
150
+
151
+
152
+ def zeros_Arithmetic(n):
153
+ return ArithmeticOnlyMatrix(n, n, lambda i, j: 0)
154
+
155
+
156
+ with warns_deprecated_sympy():
157
+ class SpecialOnlyMatrix(_MinimalMatrix, _CastableMatrix, MatrixSpecial):
158
+ pass
159
+
160
+
161
+ with warns_deprecated_sympy():
162
+ class CalculusOnlyMatrix(_MinimalMatrix, _CastableMatrix, MatrixCalculus):
163
+ pass
164
+
165
+
166
+ def test__MinimalMatrix():
167
+ x = _MinimalMatrix(2, 3, [1, 2, 3, 4, 5, 6])
168
+ assert x.rows == 2
169
+ assert x.cols == 3
170
+ assert x[2] == 3
171
+ assert x[1, 1] == 5
172
+ assert list(x) == [1, 2, 3, 4, 5, 6]
173
+ assert list(x[1, :]) == [4, 5, 6]
174
+ assert list(x[:, 1]) == [2, 5]
175
+ assert list(x[:, :]) == list(x)
176
+ assert x[:, :] == x
177
+ assert _MinimalMatrix(x) == x
178
+ assert _MinimalMatrix([[1, 2, 3], [4, 5, 6]]) == x
179
+ assert _MinimalMatrix(([1, 2, 3], [4, 5, 6])) == x
180
+ assert _MinimalMatrix([(1, 2, 3), (4, 5, 6)]) == x
181
+ assert _MinimalMatrix(((1, 2, 3), (4, 5, 6))) == x
182
+ assert not (_MinimalMatrix([[1, 2], [3, 4], [5, 6]]) == x)
183
+
184
+
185
+ def test_kind():
186
+ assert Matrix([[1, 2], [3, 4]]).kind == MatrixKind(NumberKind)
187
+ assert Matrix([[0, 0], [0, 0]]).kind == MatrixKind(NumberKind)
188
+ assert Matrix(0, 0, []).kind == MatrixKind(NumberKind)
189
+ assert Matrix([[x]]).kind == MatrixKind(NumberKind)
190
+ assert Matrix([[1, Matrix([[1]])]]).kind == MatrixKind(UndefinedKind)
191
+ assert SparseMatrix([[1]]).kind == MatrixKind(NumberKind)
192
+ assert SparseMatrix([[1, Matrix([[1]])]]).kind == MatrixKind(UndefinedKind)
193
+
194
+
195
+ # ShapingOnlyMatrix tests
196
+ def test_vec():
197
+ m = ShapingOnlyMatrix(2, 2, [1, 3, 2, 4])
198
+ m_vec = m.vec()
199
+ assert m_vec.cols == 1
200
+ for i in range(4):
201
+ assert m_vec[i] == i + 1
202
+
203
+
204
+ def test_todok():
205
+ a, b, c, d = symbols('a:d')
206
+ m1 = MutableDenseMatrix([[a, b], [c, d]])
207
+ m2 = ImmutableDenseMatrix([[a, b], [c, d]])
208
+ m3 = MutableSparseMatrix([[a, b], [c, d]])
209
+ m4 = ImmutableSparseMatrix([[a, b], [c, d]])
210
+ assert m1.todok() == m2.todok() == m3.todok() == m4.todok() == \
211
+ {(0, 0): a, (0, 1): b, (1, 0): c, (1, 1): d}
212
+
213
+
214
+ def test_tolist():
215
+ lst = [[S.One, S.Half, x*y, S.Zero], [x, y, z, x**2], [y, -S.One, z*x, 3]]
216
+ flat_lst = [S.One, S.Half, x*y, S.Zero, x, y, z, x**2, y, -S.One, z*x, 3]
217
+ m = ShapingOnlyMatrix(3, 4, flat_lst)
218
+ assert m.tolist() == lst
219
+
220
+ def test_todod():
221
+ m = ShapingOnlyMatrix(3, 2, [[S.One, 0], [0, S.Half], [x, 0]])
222
+ dict = {0: {0: S.One}, 1: {1: S.Half}, 2: {0: x}}
223
+ assert m.todod() == dict
224
+
225
+ def test_row_col_del():
226
+ e = ShapingOnlyMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
227
+ raises(IndexError, lambda: e.row_del(5))
228
+ raises(IndexError, lambda: e.row_del(-5))
229
+ raises(IndexError, lambda: e.col_del(5))
230
+ raises(IndexError, lambda: e.col_del(-5))
231
+
232
+ assert e.row_del(2) == e.row_del(-1) == Matrix([[1, 2, 3], [4, 5, 6]])
233
+ assert e.col_del(2) == e.col_del(-1) == Matrix([[1, 2], [4, 5], [7, 8]])
234
+
235
+ assert e.row_del(1) == e.row_del(-2) == Matrix([[1, 2, 3], [7, 8, 9]])
236
+ assert e.col_del(1) == e.col_del(-2) == Matrix([[1, 3], [4, 6], [7, 9]])
237
+
238
+
239
+ def test_get_diag_blocks1():
240
+ a = Matrix([[1, 2], [2, 3]])
241
+ b = Matrix([[3, x], [y, 3]])
242
+ c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
243
+ assert a.get_diag_blocks() == [a]
244
+ assert b.get_diag_blocks() == [b]
245
+ assert c.get_diag_blocks() == [c]
246
+
247
+
248
+ def test_get_diag_blocks2():
249
+ a = Matrix([[1, 2], [2, 3]])
250
+ b = Matrix([[3, x], [y, 3]])
251
+ c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
252
+ A, B, C, D = diag(a, b, b), diag(a, b, c), diag(a, c, b), diag(c, c, b)
253
+ A = ShapingOnlyMatrix(A.rows, A.cols, A)
254
+ B = ShapingOnlyMatrix(B.rows, B.cols, B)
255
+ C = ShapingOnlyMatrix(C.rows, C.cols, C)
256
+ D = ShapingOnlyMatrix(D.rows, D.cols, D)
257
+
258
+ assert A.get_diag_blocks() == [a, b, b]
259
+ assert B.get_diag_blocks() == [a, b, c]
260
+ assert C.get_diag_blocks() == [a, c, b]
261
+ assert D.get_diag_blocks() == [c, c, b]
262
+
263
+
264
+ def test_shape():
265
+ m = ShapingOnlyMatrix(1, 2, [0, 0])
266
+ assert m.shape == (1, 2)
267
+
268
+
269
+ def test_reshape():
270
+ m0 = eye_Shaping(3)
271
+ assert m0.reshape(1, 9) == Matrix(1, 9, (1, 0, 0, 0, 1, 0, 0, 0, 1))
272
+ m1 = ShapingOnlyMatrix(3, 4, lambda i, j: i + j)
273
+ assert m1.reshape(
274
+ 4, 3) == Matrix(((0, 1, 2), (3, 1, 2), (3, 4, 2), (3, 4, 5)))
275
+ assert m1.reshape(2, 6) == Matrix(((0, 1, 2, 3, 1, 2), (3, 4, 2, 3, 4, 5)))
276
+
277
+
278
+ def test_row_col():
279
+ m = ShapingOnlyMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
280
+ assert m.row(0) == Matrix(1, 3, [1, 2, 3])
281
+ assert m.col(0) == Matrix(3, 1, [1, 4, 7])
282
+
283
+
284
+ def test_row_join():
285
+ assert eye_Shaping(3).row_join(Matrix([7, 7, 7])) == \
286
+ Matrix([[1, 0, 0, 7],
287
+ [0, 1, 0, 7],
288
+ [0, 0, 1, 7]])
289
+
290
+
291
+ def test_col_join():
292
+ assert eye_Shaping(3).col_join(Matrix([[7, 7, 7]])) == \
293
+ Matrix([[1, 0, 0],
294
+ [0, 1, 0],
295
+ [0, 0, 1],
296
+ [7, 7, 7]])
297
+
298
+
299
+ def test_row_insert():
300
+ r4 = Matrix([[4, 4, 4]])
301
+ for i in range(-4, 5):
302
+ l = [1, 0, 0]
303
+ l.insert(i, 4)
304
+ assert flatten(eye_Shaping(3).row_insert(i, r4).col(0).tolist()) == l
305
+
306
+
307
+ def test_col_insert():
308
+ c4 = Matrix([4, 4, 4])
309
+ for i in range(-4, 5):
310
+ l = [0, 0, 0]
311
+ l.insert(i, 4)
312
+ assert flatten(zeros_Shaping(3).col_insert(i, c4).row(0).tolist()) == l
313
+ # issue 13643
314
+ assert eye_Shaping(6).col_insert(3, Matrix([[2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2]])) == \
315
+ Matrix([[1, 0, 0, 2, 2, 0, 0, 0],
316
+ [0, 1, 0, 2, 2, 0, 0, 0],
317
+ [0, 0, 1, 2, 2, 0, 0, 0],
318
+ [0, 0, 0, 2, 2, 1, 0, 0],
319
+ [0, 0, 0, 2, 2, 0, 1, 0],
320
+ [0, 0, 0, 2, 2, 0, 0, 1]])
321
+
322
+
323
+ def test_extract():
324
+ m = ShapingOnlyMatrix(4, 3, lambda i, j: i*3 + j)
325
+ assert m.extract([0, 1, 3], [0, 1]) == Matrix(3, 2, [0, 1, 3, 4, 9, 10])
326
+ assert m.extract([0, 3], [0, 0, 2]) == Matrix(2, 3, [0, 0, 2, 9, 9, 11])
327
+ assert m.extract(range(4), range(3)) == m
328
+ raises(IndexError, lambda: m.extract([4], [0]))
329
+ raises(IndexError, lambda: m.extract([0], [3]))
330
+
331
+
332
+ def test_hstack():
333
+ m = ShapingOnlyMatrix(4, 3, lambda i, j: i*3 + j)
334
+ m2 = ShapingOnlyMatrix(3, 4, lambda i, j: i*3 + j)
335
+ assert m == m.hstack(m)
336
+ assert m.hstack(m, m, m) == ShapingOnlyMatrix.hstack(m, m, m) == Matrix([
337
+ [0, 1, 2, 0, 1, 2, 0, 1, 2],
338
+ [3, 4, 5, 3, 4, 5, 3, 4, 5],
339
+ [6, 7, 8, 6, 7, 8, 6, 7, 8],
340
+ [9, 10, 11, 9, 10, 11, 9, 10, 11]])
341
+ raises(ShapeError, lambda: m.hstack(m, m2))
342
+ assert Matrix.hstack() == Matrix()
343
+
344
+ # test regression #12938
345
+ M1 = Matrix.zeros(0, 0)
346
+ M2 = Matrix.zeros(0, 1)
347
+ M3 = Matrix.zeros(0, 2)
348
+ M4 = Matrix.zeros(0, 3)
349
+ m = ShapingOnlyMatrix.hstack(M1, M2, M3, M4)
350
+ assert m.rows == 0 and m.cols == 6
351
+
352
+
353
+ def test_vstack():
354
+ m = ShapingOnlyMatrix(4, 3, lambda i, j: i*3 + j)
355
+ m2 = ShapingOnlyMatrix(3, 4, lambda i, j: i*3 + j)
356
+ assert m == m.vstack(m)
357
+ assert m.vstack(m, m, m) == ShapingOnlyMatrix.vstack(m, m, m) == Matrix([
358
+ [0, 1, 2],
359
+ [3, 4, 5],
360
+ [6, 7, 8],
361
+ [9, 10, 11],
362
+ [0, 1, 2],
363
+ [3, 4, 5],
364
+ [6, 7, 8],
365
+ [9, 10, 11],
366
+ [0, 1, 2],
367
+ [3, 4, 5],
368
+ [6, 7, 8],
369
+ [9, 10, 11]])
370
+ raises(ShapeError, lambda: m.vstack(m, m2))
371
+ assert Matrix.vstack() == Matrix()
372
+
373
+
374
+ # PropertiesOnlyMatrix tests
375
+ def test_atoms():
376
+ m = PropertiesOnlyMatrix(2, 2, [1, 2, x, 1 - 1/x])
377
+ assert m.atoms() == {S.One, S(2), S.NegativeOne, x}
378
+ assert m.atoms(Symbol) == {x}
379
+
380
+
381
+ def test_free_symbols():
382
+ assert PropertiesOnlyMatrix([[x], [0]]).free_symbols == {x}
383
+
384
+
385
+ def test_has():
386
+ A = PropertiesOnlyMatrix(((x, y), (2, 3)))
387
+ assert A.has(x)
388
+ assert not A.has(z)
389
+ assert A.has(Symbol)
390
+
391
+ A = PropertiesOnlyMatrix(((2, y), (2, 3)))
392
+ assert not A.has(x)
393
+
394
+
395
+ def test_is_anti_symmetric():
396
+ x = symbols('x')
397
+ assert PropertiesOnlyMatrix(2, 1, [1, 2]).is_anti_symmetric() is False
398
+ m = PropertiesOnlyMatrix(3, 3, [0, x**2 + 2*x + 1, y, -(x + 1)**2, 0, x*y, -y, -x*y, 0])
399
+ assert m.is_anti_symmetric() is True
400
+ assert m.is_anti_symmetric(simplify=False) is False
401
+ assert m.is_anti_symmetric(simplify=lambda x: x) is False
402
+
403
+ m = PropertiesOnlyMatrix(3, 3, [x.expand() for x in m])
404
+ assert m.is_anti_symmetric(simplify=False) is True
405
+ m = PropertiesOnlyMatrix(3, 3, [x.expand() for x in [S.One] + list(m)[1:]])
406
+ assert m.is_anti_symmetric() is False
407
+
408
+
409
+ def test_diagonal_symmetrical():
410
+ m = PropertiesOnlyMatrix(2, 2, [0, 1, 1, 0])
411
+ assert not m.is_diagonal()
412
+ assert m.is_symmetric()
413
+ assert m.is_symmetric(simplify=False)
414
+
415
+ m = PropertiesOnlyMatrix(2, 2, [1, 0, 0, 1])
416
+ assert m.is_diagonal()
417
+
418
+ m = PropertiesOnlyMatrix(3, 3, diag(1, 2, 3))
419
+ assert m.is_diagonal()
420
+ assert m.is_symmetric()
421
+
422
+ m = PropertiesOnlyMatrix(3, 3, [1, 0, 0, 0, 2, 0, 0, 0, 3])
423
+ assert m == diag(1, 2, 3)
424
+
425
+ m = PropertiesOnlyMatrix(2, 3, zeros(2, 3))
426
+ assert not m.is_symmetric()
427
+ assert m.is_diagonal()
428
+
429
+ m = PropertiesOnlyMatrix(((5, 0), (0, 6), (0, 0)))
430
+ assert m.is_diagonal()
431
+
432
+ m = PropertiesOnlyMatrix(((5, 0, 0), (0, 6, 0)))
433
+ assert m.is_diagonal()
434
+
435
+ m = Matrix(3, 3, [1, x**2 + 2*x + 1, y, (x + 1)**2, 2, 0, y, 0, 3])
436
+ assert m.is_symmetric()
437
+ assert not m.is_symmetric(simplify=False)
438
+ assert m.expand().is_symmetric(simplify=False)
439
+
440
+
441
+ def test_is_hermitian():
442
+ a = PropertiesOnlyMatrix([[1, I], [-I, 1]])
443
+ assert a.is_hermitian
444
+ a = PropertiesOnlyMatrix([[2*I, I], [-I, 1]])
445
+ assert a.is_hermitian is False
446
+ a = PropertiesOnlyMatrix([[x, I], [-I, 1]])
447
+ assert a.is_hermitian is None
448
+ a = PropertiesOnlyMatrix([[x, 1], [-I, 1]])
449
+ assert a.is_hermitian is False
450
+
451
+
452
+ def test_is_Identity():
453
+ assert eye_Properties(3).is_Identity
454
+ assert not PropertiesOnlyMatrix(zeros(3)).is_Identity
455
+ assert not PropertiesOnlyMatrix(ones(3)).is_Identity
456
+ # issue 6242
457
+ assert not PropertiesOnlyMatrix([[1, 0, 0]]).is_Identity
458
+
459
+
460
+ def test_is_symbolic():
461
+ a = PropertiesOnlyMatrix([[x, x], [x, x]])
462
+ assert a.is_symbolic() is True
463
+ a = PropertiesOnlyMatrix([[1, 2, 3, 4], [5, 6, 7, 8]])
464
+ assert a.is_symbolic() is False
465
+ a = PropertiesOnlyMatrix([[1, 2, 3, 4], [5, 6, x, 8]])
466
+ assert a.is_symbolic() is True
467
+ a = PropertiesOnlyMatrix([[1, x, 3]])
468
+ assert a.is_symbolic() is True
469
+ a = PropertiesOnlyMatrix([[1, 2, 3]])
470
+ assert a.is_symbolic() is False
471
+ a = PropertiesOnlyMatrix([[1], [x], [3]])
472
+ assert a.is_symbolic() is True
473
+ a = PropertiesOnlyMatrix([[1], [2], [3]])
474
+ assert a.is_symbolic() is False
475
+
476
+
477
+ def test_is_upper():
478
+ a = PropertiesOnlyMatrix([[1, 2, 3]])
479
+ assert a.is_upper is True
480
+ a = PropertiesOnlyMatrix([[1], [2], [3]])
481
+ assert a.is_upper is False
482
+
483
+
484
+ def test_is_lower():
485
+ a = PropertiesOnlyMatrix([[1, 2, 3]])
486
+ assert a.is_lower is False
487
+ a = PropertiesOnlyMatrix([[1], [2], [3]])
488
+ assert a.is_lower is True
489
+
490
+
491
+ def test_is_square():
492
+ m = PropertiesOnlyMatrix([[1], [1]])
493
+ m2 = PropertiesOnlyMatrix([[2, 2], [2, 2]])
494
+ assert not m.is_square
495
+ assert m2.is_square
496
+
497
+
498
+ def test_is_symmetric():
499
+ m = PropertiesOnlyMatrix(2, 2, [0, 1, 1, 0])
500
+ assert m.is_symmetric()
501
+ m = PropertiesOnlyMatrix(2, 2, [0, 1, 0, 1])
502
+ assert not m.is_symmetric()
503
+
504
+
505
+ def test_is_hessenberg():
506
+ A = PropertiesOnlyMatrix([[3, 4, 1], [2, 4, 5], [0, 1, 2]])
507
+ assert A.is_upper_hessenberg
508
+ A = PropertiesOnlyMatrix(3, 3, [3, 2, 0, 4, 4, 1, 1, 5, 2])
509
+ assert A.is_lower_hessenberg
510
+ A = PropertiesOnlyMatrix(3, 3, [3, 2, -1, 4, 4, 1, 1, 5, 2])
511
+ assert A.is_lower_hessenberg is False
512
+ assert A.is_upper_hessenberg is False
513
+
514
+ A = PropertiesOnlyMatrix([[3, 4, 1], [2, 4, 5], [3, 1, 2]])
515
+ assert not A.is_upper_hessenberg
516
+
517
+
518
+ def test_is_zero():
519
+ assert PropertiesOnlyMatrix(0, 0, []).is_zero_matrix
520
+ assert PropertiesOnlyMatrix([[0, 0], [0, 0]]).is_zero_matrix
521
+ assert PropertiesOnlyMatrix(zeros(3, 4)).is_zero_matrix
522
+ assert not PropertiesOnlyMatrix(eye(3)).is_zero_matrix
523
+ assert PropertiesOnlyMatrix([[x, 0], [0, 0]]).is_zero_matrix == None
524
+ assert PropertiesOnlyMatrix([[x, 1], [0, 0]]).is_zero_matrix == False
525
+ a = Symbol('a', nonzero=True)
526
+ assert PropertiesOnlyMatrix([[a, 0], [0, 0]]).is_zero_matrix == False
527
+
528
+
529
+ def test_values():
530
+ assert set(PropertiesOnlyMatrix(2, 2, [0, 1, 2, 3]
531
+ ).values()) == {1, 2, 3}
532
+ x = Symbol('x', real=True)
533
+ assert set(PropertiesOnlyMatrix(2, 2, [x, 0, 0, 1]
534
+ ).values()) == {x, 1}
535
+
536
+
537
+ # OperationsOnlyMatrix tests
538
+ def test_applyfunc():
539
+ m0 = OperationsOnlyMatrix(eye(3))
540
+ assert m0.applyfunc(lambda x: 2*x) == eye(3)*2
541
+ assert m0.applyfunc(lambda x: 0) == zeros(3)
542
+ assert m0.applyfunc(lambda x: 1) == ones(3)
543
+
544
+
545
+ def test_adjoint():
546
+ dat = [[0, I], [1, 0]]
547
+ ans = OperationsOnlyMatrix([[0, 1], [-I, 0]])
548
+ assert ans.adjoint() == Matrix(dat)
549
+
550
+
551
+ def test_as_real_imag():
552
+ m1 = OperationsOnlyMatrix(2, 2, [1, 2, 3, 4])
553
+ m3 = OperationsOnlyMatrix(2, 2,
554
+ [1 + S.ImaginaryUnit, 2 + 2*S.ImaginaryUnit,
555
+ 3 + 3*S.ImaginaryUnit, 4 + 4*S.ImaginaryUnit])
556
+
557
+ a, b = m3.as_real_imag()
558
+ assert a == m1
559
+ assert b == m1
560
+
561
+
562
+ def test_conjugate():
563
+ M = OperationsOnlyMatrix([[0, I, 5],
564
+ [1, 2, 0]])
565
+
566
+ assert M.T == Matrix([[0, 1],
567
+ [I, 2],
568
+ [5, 0]])
569
+
570
+ assert M.C == Matrix([[0, -I, 5],
571
+ [1, 2, 0]])
572
+ assert M.C == M.conjugate()
573
+
574
+ assert M.H == M.T.C
575
+ assert M.H == Matrix([[ 0, 1],
576
+ [-I, 2],
577
+ [ 5, 0]])
578
+
579
+
580
+ def test_doit():
581
+ a = OperationsOnlyMatrix([[Add(x, x, evaluate=False)]])
582
+ assert a[0] != 2*x
583
+ assert a.doit() == Matrix([[2*x]])
584
+
585
+
586
+ def test_evalf():
587
+ a = OperationsOnlyMatrix(2, 1, [sqrt(5), 6])
588
+ assert all(a.evalf()[i] == a[i].evalf() for i in range(2))
589
+ assert all(a.evalf(2)[i] == a[i].evalf(2) for i in range(2))
590
+ assert all(a.n(2)[i] == a[i].n(2) for i in range(2))
591
+
592
+
593
+ def test_expand():
594
+ m0 = OperationsOnlyMatrix([[x*(x + y), 2], [((x + y)*y)*x, x*(y + x*(x + y))]])
595
+ # Test if expand() returns a matrix
596
+ m1 = m0.expand()
597
+ assert m1 == Matrix(
598
+ [[x*y + x**2, 2], [x*y**2 + y*x**2, x*y + y*x**2 + x**3]])
599
+
600
+ a = Symbol('a', real=True)
601
+
602
+ assert OperationsOnlyMatrix(1, 1, [exp(I*a)]).expand(complex=True) == \
603
+ Matrix([cos(a) + I*sin(a)])
604
+
605
+
606
+ def test_refine():
607
+ m0 = OperationsOnlyMatrix([[Abs(x)**2, sqrt(x**2)],
608
+ [sqrt(x**2)*Abs(y)**2, sqrt(y**2)*Abs(x)**2]])
609
+ m1 = m0.refine(Q.real(x) & Q.real(y))
610
+ assert m1 == Matrix([[x**2, Abs(x)], [y**2*Abs(x), x**2*Abs(y)]])
611
+
612
+ m1 = m0.refine(Q.positive(x) & Q.positive(y))
613
+ assert m1 == Matrix([[x**2, x], [x*y**2, x**2*y]])
614
+
615
+ m1 = m0.refine(Q.negative(x) & Q.negative(y))
616
+ assert m1 == Matrix([[x**2, -x], [-x*y**2, -x**2*y]])
617
+
618
+
619
+ def test_replace():
620
+ F, G = symbols('F, G', cls=Function)
621
+ K = OperationsOnlyMatrix(2, 2, lambda i, j: G(i+j))
622
+ M = OperationsOnlyMatrix(2, 2, lambda i, j: F(i+j))
623
+ N = M.replace(F, G)
624
+ assert N == K
625
+
626
+
627
+ def test_replace_map():
628
+ F, G = symbols('F, G', cls=Function)
629
+ K = OperationsOnlyMatrix(2, 2, [(G(0), {F(0): G(0)}), (G(1), {F(1): G(1)}), (G(1), {F(1) \
630
+ : G(1)}), (G(2), {F(2): G(2)})])
631
+ M = OperationsOnlyMatrix(2, 2, lambda i, j: F(i+j))
632
+ N = M.replace(F, G, True)
633
+ assert N == K
634
+
635
+
636
+ def test_rot90():
637
+ A = Matrix([[1, 2], [3, 4]])
638
+ assert A == A.rot90(0) == A.rot90(4)
639
+ assert A.rot90(2) == A.rot90(-2) == A.rot90(6) == Matrix(((4, 3), (2, 1)))
640
+ assert A.rot90(3) == A.rot90(-1) == A.rot90(7) == Matrix(((2, 4), (1, 3)))
641
+ assert A.rot90() == A.rot90(-7) == A.rot90(-3) == Matrix(((3, 1), (4, 2)))
642
+
643
+ def test_simplify():
644
+ n = Symbol('n')
645
+ f = Function('f')
646
+
647
+ M = OperationsOnlyMatrix([[ 1/x + 1/y, (x + x*y) / x ],
648
+ [ (f(x) + y*f(x))/f(x), 2 * (1/n - cos(n * pi)/n) / pi ]])
649
+ assert M.simplify() == Matrix([[ (x + y)/(x * y), 1 + y ],
650
+ [ 1 + y, 2*((1 - 1*cos(pi*n))/(pi*n)) ]])
651
+ eq = (1 + x)**2
652
+ M = OperationsOnlyMatrix([[eq]])
653
+ assert M.simplify() == Matrix([[eq]])
654
+ assert M.simplify(ratio=oo) == Matrix([[eq.simplify(ratio=oo)]])
655
+
656
+ # https://github.com/sympy/sympy/issues/19353
657
+ m = Matrix([[30, 2], [3, 4]])
658
+ assert (1/(m.trace())).simplify() == Rational(1, 34)
659
+
660
+
661
+ def test_subs():
662
+ assert OperationsOnlyMatrix([[1, x], [x, 4]]).subs(x, 5) == Matrix([[1, 5], [5, 4]])
663
+ assert OperationsOnlyMatrix([[x, 2], [x + y, 4]]).subs([[x, -1], [y, -2]]) == \
664
+ Matrix([[-1, 2], [-3, 4]])
665
+ assert OperationsOnlyMatrix([[x, 2], [x + y, 4]]).subs([(x, -1), (y, -2)]) == \
666
+ Matrix([[-1, 2], [-3, 4]])
667
+ assert OperationsOnlyMatrix([[x, 2], [x + y, 4]]).subs({x: -1, y: -2}) == \
668
+ Matrix([[-1, 2], [-3, 4]])
669
+ assert OperationsOnlyMatrix([[x*y]]).subs({x: y - 1, y: x - 1}, simultaneous=True) == \
670
+ Matrix([[(x - 1)*(y - 1)]])
671
+
672
+
673
+ def test_trace():
674
+ M = OperationsOnlyMatrix([[1, 0, 0],
675
+ [0, 5, 0],
676
+ [0, 0, 8]])
677
+ assert M.trace() == 14
678
+
679
+
680
+ def test_xreplace():
681
+ assert OperationsOnlyMatrix([[1, x], [x, 4]]).xreplace({x: 5}) == \
682
+ Matrix([[1, 5], [5, 4]])
683
+ assert OperationsOnlyMatrix([[x, 2], [x + y, 4]]).xreplace({x: -1, y: -2}) == \
684
+ Matrix([[-1, 2], [-3, 4]])
685
+
686
+
687
+ def test_permute():
688
+ a = OperationsOnlyMatrix(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
689
+
690
+ raises(IndexError, lambda: a.permute([[0, 5]]))
691
+ raises(ValueError, lambda: a.permute(Symbol('x')))
692
+ b = a.permute_rows([[0, 2], [0, 1]])
693
+ assert a.permute([[0, 2], [0, 1]]) == b == Matrix([
694
+ [5, 6, 7, 8],
695
+ [9, 10, 11, 12],
696
+ [1, 2, 3, 4]])
697
+
698
+ b = a.permute_cols([[0, 2], [0, 1]])
699
+ assert a.permute([[0, 2], [0, 1]], orientation='cols') == b ==\
700
+ Matrix([
701
+ [ 2, 3, 1, 4],
702
+ [ 6, 7, 5, 8],
703
+ [10, 11, 9, 12]])
704
+
705
+ b = a.permute_cols([[0, 2], [0, 1]], direction='backward')
706
+ assert a.permute([[0, 2], [0, 1]], orientation='cols', direction='backward') == b ==\
707
+ Matrix([
708
+ [ 3, 1, 2, 4],
709
+ [ 7, 5, 6, 8],
710
+ [11, 9, 10, 12]])
711
+
712
+ assert a.permute([1, 2, 0, 3]) == Matrix([
713
+ [5, 6, 7, 8],
714
+ [9, 10, 11, 12],
715
+ [1, 2, 3, 4]])
716
+
717
+ from sympy.combinatorics import Permutation
718
+ assert a.permute(Permutation([1, 2, 0, 3])) == Matrix([
719
+ [5, 6, 7, 8],
720
+ [9, 10, 11, 12],
721
+ [1, 2, 3, 4]])
722
+
723
+ def test_upper_triangular():
724
+
725
+ A = OperationsOnlyMatrix([
726
+ [1, 1, 1, 1],
727
+ [1, 1, 1, 1],
728
+ [1, 1, 1, 1],
729
+ [1, 1, 1, 1]
730
+ ])
731
+
732
+ R = A.upper_triangular(2)
733
+ assert R == OperationsOnlyMatrix([
734
+ [0, 0, 1, 1],
735
+ [0, 0, 0, 1],
736
+ [0, 0, 0, 0],
737
+ [0, 0, 0, 0]
738
+ ])
739
+
740
+ R = A.upper_triangular(-2)
741
+ assert R == OperationsOnlyMatrix([
742
+ [1, 1, 1, 1],
743
+ [1, 1, 1, 1],
744
+ [1, 1, 1, 1],
745
+ [0, 1, 1, 1]
746
+ ])
747
+
748
+ R = A.upper_triangular()
749
+ assert R == OperationsOnlyMatrix([
750
+ [1, 1, 1, 1],
751
+ [0, 1, 1, 1],
752
+ [0, 0, 1, 1],
753
+ [0, 0, 0, 1]
754
+ ])
755
+
756
+ def test_lower_triangular():
757
+ A = OperationsOnlyMatrix([
758
+ [1, 1, 1, 1],
759
+ [1, 1, 1, 1],
760
+ [1, 1, 1, 1],
761
+ [1, 1, 1, 1]
762
+ ])
763
+
764
+ L = A.lower_triangular()
765
+ assert L == ArithmeticOnlyMatrix([
766
+ [1, 0, 0, 0],
767
+ [1, 1, 0, 0],
768
+ [1, 1, 1, 0],
769
+ [1, 1, 1, 1]])
770
+
771
+ L = A.lower_triangular(2)
772
+ assert L == ArithmeticOnlyMatrix([
773
+ [1, 1, 1, 0],
774
+ [1, 1, 1, 1],
775
+ [1, 1, 1, 1],
776
+ [1, 1, 1, 1]
777
+ ])
778
+
779
+ L = A.lower_triangular(-2)
780
+ assert L == ArithmeticOnlyMatrix([
781
+ [0, 0, 0, 0],
782
+ [0, 0, 0, 0],
783
+ [1, 0, 0, 0],
784
+ [1, 1, 0, 0]
785
+ ])
786
+
787
+
788
+ # ArithmeticOnlyMatrix tests
789
+ def test_abs():
790
+ m = ArithmeticOnlyMatrix([[1, -2], [x, y]])
791
+ assert abs(m) == ArithmeticOnlyMatrix([[1, 2], [Abs(x), Abs(y)]])
792
+
793
+
794
+ def test_add():
795
+ m = ArithmeticOnlyMatrix([[1, 2, 3], [x, y, x], [2*y, -50, z*x]])
796
+ assert m + m == ArithmeticOnlyMatrix([[2, 4, 6], [2*x, 2*y, 2*x], [4*y, -100, 2*z*x]])
797
+ n = ArithmeticOnlyMatrix(1, 2, [1, 2])
798
+ raises(ShapeError, lambda: m + n)
799
+
800
+
801
+ def test_multiplication():
802
+ a = ArithmeticOnlyMatrix((
803
+ (1, 2),
804
+ (3, 1),
805
+ (0, 6),
806
+ ))
807
+
808
+ b = ArithmeticOnlyMatrix((
809
+ (1, 2),
810
+ (3, 0),
811
+ ))
812
+
813
+ raises(ShapeError, lambda: b*a)
814
+ raises(TypeError, lambda: a*{})
815
+
816
+ c = a*b
817
+ assert c[0, 0] == 7
818
+ assert c[0, 1] == 2
819
+ assert c[1, 0] == 6
820
+ assert c[1, 1] == 6
821
+ assert c[2, 0] == 18
822
+ assert c[2, 1] == 0
823
+
824
+ try:
825
+ eval('c = a @ b')
826
+ except SyntaxError:
827
+ pass
828
+ else:
829
+ assert c[0, 0] == 7
830
+ assert c[0, 1] == 2
831
+ assert c[1, 0] == 6
832
+ assert c[1, 1] == 6
833
+ assert c[2, 0] == 18
834
+ assert c[2, 1] == 0
835
+
836
+ h = a.multiply_elementwise(c)
837
+ assert h == matrix_multiply_elementwise(a, c)
838
+ assert h[0, 0] == 7
839
+ assert h[0, 1] == 4
840
+ assert h[1, 0] == 18
841
+ assert h[1, 1] == 6
842
+ assert h[2, 0] == 0
843
+ assert h[2, 1] == 0
844
+ raises(ShapeError, lambda: a.multiply_elementwise(b))
845
+
846
+ c = b * Symbol("x")
847
+ assert isinstance(c, ArithmeticOnlyMatrix)
848
+ assert c[0, 0] == x
849
+ assert c[0, 1] == 2*x
850
+ assert c[1, 0] == 3*x
851
+ assert c[1, 1] == 0
852
+
853
+ c2 = x * b
854
+ assert c == c2
855
+
856
+ c = 5 * b
857
+ assert isinstance(c, ArithmeticOnlyMatrix)
858
+ assert c[0, 0] == 5
859
+ assert c[0, 1] == 2*5
860
+ assert c[1, 0] == 3*5
861
+ assert c[1, 1] == 0
862
+
863
+ try:
864
+ eval('c = 5 @ b')
865
+ except SyntaxError:
866
+ pass
867
+ else:
868
+ assert isinstance(c, ArithmeticOnlyMatrix)
869
+ assert c[0, 0] == 5
870
+ assert c[0, 1] == 2*5
871
+ assert c[1, 0] == 3*5
872
+ assert c[1, 1] == 0
873
+
874
+ # https://github.com/sympy/sympy/issues/22353
875
+ A = Matrix(ones(3, 1))
876
+ _h = -Rational(1, 2)
877
+ B = Matrix([_h, _h, _h])
878
+ assert A.multiply_elementwise(B) == Matrix([
879
+ [_h],
880
+ [_h],
881
+ [_h]])
882
+
883
+
884
+ def test_matmul():
885
+ a = Matrix([[1, 2], [3, 4]])
886
+
887
+ assert a.__matmul__(2) == NotImplemented
888
+
889
+ assert a.__rmatmul__(2) == NotImplemented
890
+
891
+ #This is done this way because @ is only supported in Python 3.5+
892
+ #To check 2@a case
893
+ try:
894
+ eval('2 @ a')
895
+ except SyntaxError:
896
+ pass
897
+ except TypeError: #TypeError is raised in case of NotImplemented is returned
898
+ pass
899
+
900
+ #Check a@2 case
901
+ try:
902
+ eval('a @ 2')
903
+ except SyntaxError:
904
+ pass
905
+ except TypeError: #TypeError is raised in case of NotImplemented is returned
906
+ pass
907
+
908
+
909
+ def test_non_matmul():
910
+ """
911
+ Test that if explicitly specified as non-matrix, mul reverts
912
+ to scalar multiplication.
913
+ """
914
+ class foo(Expr):
915
+ is_Matrix=False
916
+ is_MatrixLike=False
917
+ shape = (1, 1)
918
+
919
+ A = Matrix([[1, 2], [3, 4]])
920
+ b = foo()
921
+ assert b*A == Matrix([[b, 2*b], [3*b, 4*b]])
922
+ assert A*b == Matrix([[b, 2*b], [3*b, 4*b]])
923
+
924
+
925
+ def test_power():
926
+ raises(NonSquareMatrixError, lambda: Matrix((1, 2))**2)
927
+
928
+ A = ArithmeticOnlyMatrix([[2, 3], [4, 5]])
929
+ assert (A**5)[:] == (6140, 8097, 10796, 14237)
930
+ A = ArithmeticOnlyMatrix([[2, 1, 3], [4, 2, 4], [6, 12, 1]])
931
+ assert (A**3)[:] == (290, 262, 251, 448, 440, 368, 702, 954, 433)
932
+ assert A**0 == eye(3)
933
+ assert A**1 == A
934
+ assert (ArithmeticOnlyMatrix([[2]]) ** 100)[0, 0] == 2**100
935
+ assert ArithmeticOnlyMatrix([[1, 2], [3, 4]])**Integer(2) == ArithmeticOnlyMatrix([[7, 10], [15, 22]])
936
+ A = Matrix([[1,2],[4,5]])
937
+ assert A.pow(20, method='cayley') == A.pow(20, method='multiply')
938
+
939
+ def test_neg():
940
+ n = ArithmeticOnlyMatrix(1, 2, [1, 2])
941
+ assert -n == ArithmeticOnlyMatrix(1, 2, [-1, -2])
942
+
943
+
944
+ def test_sub():
945
+ n = ArithmeticOnlyMatrix(1, 2, [1, 2])
946
+ assert n - n == ArithmeticOnlyMatrix(1, 2, [0, 0])
947
+
948
+
949
+ def test_div():
950
+ n = ArithmeticOnlyMatrix(1, 2, [1, 2])
951
+ assert n/2 == ArithmeticOnlyMatrix(1, 2, [S.Half, S(2)/2])
952
+
953
+ # SpecialOnlyMatrix tests
954
+ def test_eye():
955
+ assert list(SpecialOnlyMatrix.eye(2, 2)) == [1, 0, 0, 1]
956
+ assert list(SpecialOnlyMatrix.eye(2)) == [1, 0, 0, 1]
957
+ assert type(SpecialOnlyMatrix.eye(2)) == SpecialOnlyMatrix
958
+ assert type(SpecialOnlyMatrix.eye(2, cls=Matrix)) == Matrix
959
+
960
+
961
+ def test_ones():
962
+ assert list(SpecialOnlyMatrix.ones(2, 2)) == [1, 1, 1, 1]
963
+ assert list(SpecialOnlyMatrix.ones(2)) == [1, 1, 1, 1]
964
+ assert SpecialOnlyMatrix.ones(2, 3) == Matrix([[1, 1, 1], [1, 1, 1]])
965
+ assert type(SpecialOnlyMatrix.ones(2)) == SpecialOnlyMatrix
966
+ assert type(SpecialOnlyMatrix.ones(2, cls=Matrix)) == Matrix
967
+
968
+
969
+ def test_zeros():
970
+ assert list(SpecialOnlyMatrix.zeros(2, 2)) == [0, 0, 0, 0]
971
+ assert list(SpecialOnlyMatrix.zeros(2)) == [0, 0, 0, 0]
972
+ assert SpecialOnlyMatrix.zeros(2, 3) == Matrix([[0, 0, 0], [0, 0, 0]])
973
+ assert type(SpecialOnlyMatrix.zeros(2)) == SpecialOnlyMatrix
974
+ assert type(SpecialOnlyMatrix.zeros(2, cls=Matrix)) == Matrix
975
+
976
+
977
+ def test_diag_make():
978
+ diag = SpecialOnlyMatrix.diag
979
+ a = Matrix([[1, 2], [2, 3]])
980
+ b = Matrix([[3, x], [y, 3]])
981
+ c = Matrix([[3, x, 3], [y, 3, z], [x, y, z]])
982
+ assert diag(a, b, b) == Matrix([
983
+ [1, 2, 0, 0, 0, 0],
984
+ [2, 3, 0, 0, 0, 0],
985
+ [0, 0, 3, x, 0, 0],
986
+ [0, 0, y, 3, 0, 0],
987
+ [0, 0, 0, 0, 3, x],
988
+ [0, 0, 0, 0, y, 3],
989
+ ])
990
+ assert diag(a, b, c) == Matrix([
991
+ [1, 2, 0, 0, 0, 0, 0],
992
+ [2, 3, 0, 0, 0, 0, 0],
993
+ [0, 0, 3, x, 0, 0, 0],
994
+ [0, 0, y, 3, 0, 0, 0],
995
+ [0, 0, 0, 0, 3, x, 3],
996
+ [0, 0, 0, 0, y, 3, z],
997
+ [0, 0, 0, 0, x, y, z],
998
+ ])
999
+ assert diag(a, c, b) == Matrix([
1000
+ [1, 2, 0, 0, 0, 0, 0],
1001
+ [2, 3, 0, 0, 0, 0, 0],
1002
+ [0, 0, 3, x, 3, 0, 0],
1003
+ [0, 0, y, 3, z, 0, 0],
1004
+ [0, 0, x, y, z, 0, 0],
1005
+ [0, 0, 0, 0, 0, 3, x],
1006
+ [0, 0, 0, 0, 0, y, 3],
1007
+ ])
1008
+ a = Matrix([x, y, z])
1009
+ b = Matrix([[1, 2], [3, 4]])
1010
+ c = Matrix([[5, 6]])
1011
+ # this "wandering diagonal" is what makes this
1012
+ # a block diagonal where each block is independent
1013
+ # of the others
1014
+ assert diag(a, 7, b, c) == Matrix([
1015
+ [x, 0, 0, 0, 0, 0],
1016
+ [y, 0, 0, 0, 0, 0],
1017
+ [z, 0, 0, 0, 0, 0],
1018
+ [0, 7, 0, 0, 0, 0],
1019
+ [0, 0, 1, 2, 0, 0],
1020
+ [0, 0, 3, 4, 0, 0],
1021
+ [0, 0, 0, 0, 5, 6]])
1022
+ raises(ValueError, lambda: diag(a, 7, b, c, rows=5))
1023
+ assert diag(1) == Matrix([[1]])
1024
+ assert diag(1, rows=2) == Matrix([[1, 0], [0, 0]])
1025
+ assert diag(1, cols=2) == Matrix([[1, 0], [0, 0]])
1026
+ assert diag(1, rows=3, cols=2) == Matrix([[1, 0], [0, 0], [0, 0]])
1027
+ assert diag(*[2, 3]) == Matrix([
1028
+ [2, 0],
1029
+ [0, 3]])
1030
+ assert diag(Matrix([2, 3])) == Matrix([
1031
+ [2],
1032
+ [3]])
1033
+ assert diag([1, [2, 3], 4], unpack=False) == \
1034
+ diag([[1], [2, 3], [4]], unpack=False) == Matrix([
1035
+ [1, 0],
1036
+ [2, 3],
1037
+ [4, 0]])
1038
+ assert type(diag(1)) == SpecialOnlyMatrix
1039
+ assert type(diag(1, cls=Matrix)) == Matrix
1040
+ assert Matrix.diag([1, 2, 3]) == Matrix.diag(1, 2, 3)
1041
+ assert Matrix.diag([1, 2, 3], unpack=False).shape == (3, 1)
1042
+ assert Matrix.diag([[1, 2, 3]]).shape == (3, 1)
1043
+ assert Matrix.diag([[1, 2, 3]], unpack=False).shape == (1, 3)
1044
+ assert Matrix.diag([[[1, 2, 3]]]).shape == (1, 3)
1045
+ # kerning can be used to move the starting point
1046
+ assert Matrix.diag(ones(0, 2), 1, 2) == Matrix([
1047
+ [0, 0, 1, 0],
1048
+ [0, 0, 0, 2]])
1049
+ assert Matrix.diag(ones(2, 0), 1, 2) == Matrix([
1050
+ [0, 0],
1051
+ [0, 0],
1052
+ [1, 0],
1053
+ [0, 2]])
1054
+
1055
+
1056
+ def test_diagonal():
1057
+ m = Matrix(3, 3, range(9))
1058
+ d = m.diagonal()
1059
+ assert d == m.diagonal(0)
1060
+ assert tuple(d) == (0, 4, 8)
1061
+ assert tuple(m.diagonal(1)) == (1, 5)
1062
+ assert tuple(m.diagonal(-1)) == (3, 7)
1063
+ assert tuple(m.diagonal(2)) == (2,)
1064
+ assert type(m.diagonal()) == type(m)
1065
+ s = SparseMatrix(3, 3, {(1, 1): 1})
1066
+ assert type(s.diagonal()) == type(s)
1067
+ assert type(m) != type(s)
1068
+ raises(ValueError, lambda: m.diagonal(3))
1069
+ raises(ValueError, lambda: m.diagonal(-3))
1070
+ raises(ValueError, lambda: m.diagonal(pi))
1071
+ M = ones(2, 3)
1072
+ assert banded({i: list(M.diagonal(i))
1073
+ for i in range(1-M.rows, M.cols)}) == M
1074
+
1075
+
1076
+ def test_jordan_block():
1077
+ assert SpecialOnlyMatrix.jordan_block(3, 2) == SpecialOnlyMatrix.jordan_block(3, eigenvalue=2) \
1078
+ == SpecialOnlyMatrix.jordan_block(size=3, eigenvalue=2) \
1079
+ == SpecialOnlyMatrix.jordan_block(3, 2, band='upper') \
1080
+ == SpecialOnlyMatrix.jordan_block(
1081
+ size=3, eigenval=2, eigenvalue=2) \
1082
+ == Matrix([
1083
+ [2, 1, 0],
1084
+ [0, 2, 1],
1085
+ [0, 0, 2]])
1086
+
1087
+ assert SpecialOnlyMatrix.jordan_block(3, 2, band='lower') == Matrix([
1088
+ [2, 0, 0],
1089
+ [1, 2, 0],
1090
+ [0, 1, 2]])
1091
+ # missing eigenvalue
1092
+ raises(ValueError, lambda: SpecialOnlyMatrix.jordan_block(2))
1093
+ # non-integral size
1094
+ raises(ValueError, lambda: SpecialOnlyMatrix.jordan_block(3.5, 2))
1095
+ # size not specified
1096
+ raises(ValueError, lambda: SpecialOnlyMatrix.jordan_block(eigenvalue=2))
1097
+ # inconsistent eigenvalue
1098
+ raises(ValueError,
1099
+ lambda: SpecialOnlyMatrix.jordan_block(
1100
+ eigenvalue=2, eigenval=4))
1101
+
1102
+ # Using alias keyword
1103
+ assert SpecialOnlyMatrix.jordan_block(size=3, eigenvalue=2) == \
1104
+ SpecialOnlyMatrix.jordan_block(size=3, eigenval=2)
1105
+
1106
+
1107
+ def test_orthogonalize():
1108
+ m = Matrix([[1, 2], [3, 4]])
1109
+ assert m.orthogonalize(Matrix([[2], [1]])) == [Matrix([[2], [1]])]
1110
+ assert m.orthogonalize(Matrix([[2], [1]]), normalize=True) == \
1111
+ [Matrix([[2*sqrt(5)/5], [sqrt(5)/5]])]
1112
+ assert m.orthogonalize(Matrix([[1], [2]]), Matrix([[-1], [4]])) == \
1113
+ [Matrix([[1], [2]]), Matrix([[Rational(-12, 5)], [Rational(6, 5)]])]
1114
+ assert m.orthogonalize(Matrix([[0], [0]]), Matrix([[-1], [4]])) == \
1115
+ [Matrix([[-1], [4]])]
1116
+ assert m.orthogonalize(Matrix([[0], [0]])) == []
1117
+
1118
+ n = Matrix([[9, 1, 9], [3, 6, 10], [8, 5, 2]])
1119
+ vecs = [Matrix([[-5], [1]]), Matrix([[-5], [2]]), Matrix([[-5], [-2]])]
1120
+ assert n.orthogonalize(*vecs) == \
1121
+ [Matrix([[-5], [1]]), Matrix([[Rational(5, 26)], [Rational(25, 26)]])]
1122
+
1123
+ vecs = [Matrix([0, 0, 0]), Matrix([1, 2, 3]), Matrix([1, 4, 5])]
1124
+ raises(ValueError, lambda: Matrix.orthogonalize(*vecs, rankcheck=True))
1125
+
1126
+ vecs = [Matrix([1, 2, 3]), Matrix([4, 5, 6]), Matrix([7, 8, 9])]
1127
+ raises(ValueError, lambda: Matrix.orthogonalize(*vecs, rankcheck=True))
1128
+
1129
+ def test_wilkinson():
1130
+
1131
+ wminus, wplus = Matrix.wilkinson(1)
1132
+ assert wminus == Matrix([
1133
+ [-1, 1, 0],
1134
+ [1, 0, 1],
1135
+ [0, 1, 1]])
1136
+ assert wplus == Matrix([
1137
+ [1, 1, 0],
1138
+ [1, 0, 1],
1139
+ [0, 1, 1]])
1140
+
1141
+ wminus, wplus = Matrix.wilkinson(3)
1142
+ assert wminus == Matrix([
1143
+ [-3, 1, 0, 0, 0, 0, 0],
1144
+ [1, -2, 1, 0, 0, 0, 0],
1145
+ [0, 1, -1, 1, 0, 0, 0],
1146
+ [0, 0, 1, 0, 1, 0, 0],
1147
+ [0, 0, 0, 1, 1, 1, 0],
1148
+ [0, 0, 0, 0, 1, 2, 1],
1149
+
1150
+ [0, 0, 0, 0, 0, 1, 3]])
1151
+
1152
+ assert wplus == Matrix([
1153
+ [3, 1, 0, 0, 0, 0, 0],
1154
+ [1, 2, 1, 0, 0, 0, 0],
1155
+ [0, 1, 1, 1, 0, 0, 0],
1156
+ [0, 0, 1, 0, 1, 0, 0],
1157
+ [0, 0, 0, 1, 1, 1, 0],
1158
+ [0, 0, 0, 0, 1, 2, 1],
1159
+ [0, 0, 0, 0, 0, 1, 3]])
1160
+
1161
+
1162
+ # CalculusOnlyMatrix tests
1163
+ @XFAIL
1164
+ def test_diff():
1165
+ x, y = symbols('x y')
1166
+ m = CalculusOnlyMatrix(2, 1, [x, y])
1167
+ # TODO: currently not working as ``_MinimalMatrix`` cannot be sympified:
1168
+ assert m.diff(x) == Matrix(2, 1, [1, 0])
1169
+
1170
+
1171
+ def test_integrate():
1172
+ x, y = symbols('x y')
1173
+ m = CalculusOnlyMatrix(2, 1, [x, y])
1174
+ assert m.integrate(x) == Matrix(2, 1, [x**2/2, y*x])
1175
+
1176
+
1177
+ def test_jacobian2():
1178
+ rho, phi = symbols("rho,phi")
1179
+ X = CalculusOnlyMatrix(3, 1, [rho*cos(phi), rho*sin(phi), rho**2])
1180
+ Y = CalculusOnlyMatrix(2, 1, [rho, phi])
1181
+ J = Matrix([
1182
+ [cos(phi), -rho*sin(phi)],
1183
+ [sin(phi), rho*cos(phi)],
1184
+ [ 2*rho, 0],
1185
+ ])
1186
+ assert X.jacobian(Y) == J
1187
+
1188
+ m = CalculusOnlyMatrix(2, 2, [1, 2, 3, 4])
1189
+ m2 = CalculusOnlyMatrix(4, 1, [1, 2, 3, 4])
1190
+ raises(TypeError, lambda: m.jacobian(Matrix([1, 2])))
1191
+ raises(TypeError, lambda: m2.jacobian(m))
1192
+
1193
+
1194
+ def test_limit():
1195
+ x, y = symbols('x y')
1196
+ m = CalculusOnlyMatrix(2, 1, [1/x, y])
1197
+ assert m.limit(x, 5) == Matrix(2, 1, [Rational(1, 5), y])
1198
+
1199
+
1200
+ def test_issue_13774():
1201
+ M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
1202
+ v = [1, 1, 1]
1203
+ raises(TypeError, lambda: M*v)
1204
+ raises(TypeError, lambda: v*M)
1205
+
1206
+ def test_companion():
1207
+ x = Symbol('x')
1208
+ y = Symbol('y')
1209
+ raises(ValueError, lambda: Matrix.companion(1))
1210
+ raises(ValueError, lambda: Matrix.companion(Poly([1], x)))
1211
+ raises(ValueError, lambda: Matrix.companion(Poly([2, 1], x)))
1212
+ raises(ValueError, lambda: Matrix.companion(Poly(x*y, [x, y])))
1213
+
1214
+ c0, c1, c2 = symbols('c0:3')
1215
+ assert Matrix.companion(Poly([1, c0], x)) == Matrix([-c0])
1216
+ assert Matrix.companion(Poly([1, c1, c0], x)) == \
1217
+ Matrix([[0, -c0], [1, -c1]])
1218
+ assert Matrix.companion(Poly([1, c2, c1, c0], x)) == \
1219
+ Matrix([[0, 0, -c0], [1, 0, -c1], [0, 1, -c2]])
1220
+
1221
+ def test_issue_10589():
1222
+ x, y, z = symbols("x, y z")
1223
+ M1 = Matrix([x, y, z])
1224
+ M1 = M1.subs(zip([x, y, z], [1, 2, 3]))
1225
+ assert M1 == Matrix([[1], [2], [3]])
1226
+
1227
+ M2 = Matrix([[x, x, x, x, x], [x, x, x, x, x], [x, x, x, x, x]])
1228
+ M2 = M2.subs(zip([x], [1]))
1229
+ assert M2 == Matrix([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])
1230
+
1231
+ def test_rmul_pr19860():
1232
+ class Foo(ImmutableDenseMatrix):
1233
+ _op_priority = MutableDenseMatrix._op_priority + 0.01
1234
+
1235
+ a = Matrix(2, 2, [1, 2, 3, 4])
1236
+ b = Foo(2, 2, [1, 2, 3, 4])
1237
+
1238
+ # This would throw a RecursionError: maximum recursion depth
1239
+ # since b always has higher priority even after a.as_mutable()
1240
+ c = a*b
1241
+
1242
+ assert isinstance(c, Foo)
1243
+ assert c == Matrix([[7, 10], [15, 22]])
1244
+
1245
+
1246
+ def test_issue_18956():
1247
+ A = Array([[1, 2], [3, 4]])
1248
+ B = Matrix([[1,2],[3,4]])
1249
+ raises(TypeError, lambda: B + A)
1250
+ raises(TypeError, lambda: A + B)
1251
+
1252
+
1253
+ def test__eq__():
1254
+ class My(object):
1255
+ def __iter__(self):
1256
+ yield 1
1257
+ yield 2
1258
+ return
1259
+ def __getitem__(self, i):
1260
+ return list(self)[i]
1261
+ a = Matrix(2, 1, [1, 2])
1262
+ assert a != My()
1263
+ class My_sympy(My):
1264
+ def _sympy_(self):
1265
+ return Matrix(self)
1266
+ assert a == My_sympy()
openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_determinant.py ADDED
@@ -0,0 +1,280 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import pytest
3
+ from sympy.core.numbers import I
4
+ from sympy.core.numbers import Rational
5
+ from sympy.core.symbol import (Symbol, symbols)
6
+ from sympy.functions.elementary.miscellaneous import sqrt
7
+ from sympy.polys.polytools import Poly
8
+ from sympy.matrices import Matrix, eye, ones
9
+ from sympy.abc import x, y, z
10
+ from sympy.testing.pytest import raises
11
+ from sympy.matrices.exceptions import NonSquareMatrixError
12
+ from sympy.functions.combinatorial.factorials import factorial, subfactorial
13
+
14
+
15
+ @pytest.mark.parametrize("method", [
16
+ # Evaluating these directly because they are never reached via M.det()
17
+ Matrix._eval_det_bareiss, Matrix._eval_det_berkowitz,
18
+ Matrix._eval_det_bird, Matrix._eval_det_laplace, Matrix._eval_det_lu
19
+ ])
20
+ @pytest.mark.parametrize("M, sol", [
21
+ (Matrix(), 1),
22
+ (Matrix([[0]]), 0),
23
+ (Matrix([[5]]), 5),
24
+ ])
25
+ def test_eval_determinant(method, M, sol):
26
+ assert method(M) == sol
27
+
28
+
29
+ @pytest.mark.parametrize("method", [
30
+ "domain-ge", "bareiss", "berkowitz", "bird", "laplace", "lu"])
31
+ @pytest.mark.parametrize("M, sol", [
32
+ (Matrix(( (-3, 2),
33
+ ( 8, -5) )), -1),
34
+ (Matrix(( (x, 1),
35
+ (y, 2*y) )), 2*x*y - y),
36
+ (Matrix(( (1, 1, 1),
37
+ (1, 2, 3),
38
+ (1, 3, 6) )), 1),
39
+ (Matrix(( ( 3, -2, 0, 5),
40
+ (-2, 1, -2, 2),
41
+ ( 0, -2, 5, 0),
42
+ ( 5, 0, 3, 4) )), -289),
43
+ (Matrix(( ( 1, 2, 3, 4),
44
+ ( 5, 6, 7, 8),
45
+ ( 9, 10, 11, 12),
46
+ (13, 14, 15, 16) )), 0),
47
+ (Matrix(( (3, 2, 0, 0, 0),
48
+ (0, 3, 2, 0, 0),
49
+ (0, 0, 3, 2, 0),
50
+ (0, 0, 0, 3, 2),
51
+ (2, 0, 0, 0, 3) )), 275),
52
+ (Matrix(( ( 3, 0, 0, 0),
53
+ (-2, 1, 0, 0),
54
+ ( 0, -2, 5, 0),
55
+ ( 5, 0, 3, 4) )), 60),
56
+ (Matrix(( ( 1, 0, 0, 0),
57
+ ( 5, 0, 0, 0),
58
+ ( 9, 10, 11, 0),
59
+ (13, 14, 15, 16) )), 0),
60
+ (Matrix(( (3, 2, 0, 0, 0),
61
+ (0, 3, 2, 0, 0),
62
+ (0, 0, 3, 2, 0),
63
+ (0, 0, 0, 3, 2),
64
+ (0, 0, 0, 0, 3) )), 243),
65
+ (Matrix(( (1, 0, 1, 2, 12),
66
+ (2, 0, 1, 1, 4),
67
+ (2, 1, 1, -1, 3),
68
+ (3, 2, -1, 1, 8),
69
+ (1, 1, 1, 0, 6) )), -55),
70
+ (Matrix(( (-5, 2, 3, 4, 5),
71
+ ( 1, -4, 3, 4, 5),
72
+ ( 1, 2, -3, 4, 5),
73
+ ( 1, 2, 3, -2, 5),
74
+ ( 1, 2, 3, 4, -1) )), 11664),
75
+ (Matrix(( ( 2, 7, -1, 3, 2),
76
+ ( 0, 0, 1, 0, 1),
77
+ (-2, 0, 7, 0, 2),
78
+ (-3, -2, 4, 5, 3),
79
+ ( 1, 0, 0, 0, 1) )), 123),
80
+ (Matrix(( (x, y, z),
81
+ (1, 0, 0),
82
+ (y, z, x) )), z**2 - x*y),
83
+ ])
84
+ def test_determinant(method, M, sol):
85
+ assert M.det(method=method) == sol
86
+
87
+
88
+ def test_issue_13835():
89
+ a = symbols('a')
90
+ M = lambda n: Matrix([[i + a*j for i in range(n)]
91
+ for j in range(n)])
92
+ assert M(5).det() == 0
93
+ assert M(6).det() == 0
94
+ assert M(7).det() == 0
95
+
96
+
97
+ def test_issue_14517():
98
+ M = Matrix([
99
+ [ 0, 10*I, 10*I, 0],
100
+ [10*I, 0, 0, 10*I],
101
+ [10*I, 0, 5 + 2*I, 10*I],
102
+ [ 0, 10*I, 10*I, 5 + 2*I]])
103
+ ev = M.eigenvals()
104
+ # test one random eigenvalue, the computation is a little slow
105
+ test_ev = random.choice(list(ev.keys()))
106
+ assert (M - test_ev*eye(4)).det() == 0
107
+
108
+
109
+ @pytest.mark.parametrize("method", [
110
+ "bareis", "det_lu", "det_LU", "Bareis", "BAREISS", "BERKOWITZ", "LU"])
111
+ @pytest.mark.parametrize("M, sol", [
112
+ (Matrix(( ( 3, -2, 0, 5),
113
+ (-2, 1, -2, 2),
114
+ ( 0, -2, 5, 0),
115
+ ( 5, 0, 3, 4) )), -289),
116
+ (Matrix(( (-5, 2, 3, 4, 5),
117
+ ( 1, -4, 3, 4, 5),
118
+ ( 1, 2, -3, 4, 5),
119
+ ( 1, 2, 3, -2, 5),
120
+ ( 1, 2, 3, 4, -1) )), 11664),
121
+ ])
122
+ def test_legacy_det(method, M, sol):
123
+ # Minimal support for legacy keys for 'method' in det()
124
+ # Partially copied from test_determinant()
125
+ assert M.det(method=method) == sol
126
+
127
+
128
+ def eye_Determinant(n):
129
+ return Matrix(n, n, lambda i, j: int(i == j))
130
+
131
+ def zeros_Determinant(n):
132
+ return Matrix(n, n, lambda i, j: 0)
133
+
134
+ def test_det():
135
+ a = Matrix(2, 3, [1, 2, 3, 4, 5, 6])
136
+ raises(NonSquareMatrixError, lambda: a.det())
137
+
138
+ z = zeros_Determinant(2)
139
+ ey = eye_Determinant(2)
140
+ assert z.det() == 0
141
+ assert ey.det() == 1
142
+
143
+ x = Symbol('x')
144
+ a = Matrix(0, 0, [])
145
+ b = Matrix(1, 1, [5])
146
+ c = Matrix(2, 2, [1, 2, 3, 4])
147
+ d = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 8])
148
+ e = Matrix(4, 4,
149
+ [x, 1, 2, 3, 4, 5, 6, 7, 2, 9, 10, 11, 12, 13, 14, 14])
150
+ from sympy.abc import i, j, k, l, m, n
151
+ f = Matrix(3, 3, [i, l, m, 0, j, n, 0, 0, k])
152
+ g = Matrix(3, 3, [i, 0, 0, l, j, 0, m, n, k])
153
+ h = Matrix(3, 3, [x**3, 0, 0, i, x**-1, 0, j, k, x**-2])
154
+ # the method keyword for `det` doesn't kick in until 4x4 matrices,
155
+ # so there is no need to test all methods on smaller ones
156
+
157
+ assert a.det() == 1
158
+ assert b.det() == 5
159
+ assert c.det() == -2
160
+ assert d.det() == 3
161
+ assert e.det() == 4*x - 24
162
+ assert e.det(method="domain-ge") == 4*x - 24
163
+ assert e.det(method='bareiss') == 4*x - 24
164
+ assert e.det(method='berkowitz') == 4*x - 24
165
+ assert f.det() == i*j*k
166
+ assert g.det() == i*j*k
167
+ assert h.det() == 1
168
+ raises(ValueError, lambda: e.det(iszerofunc="test"))
169
+
170
+ def test_permanent():
171
+ M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
172
+ assert M.per() == 450
173
+ for i in range(1, 12):
174
+ assert ones(i, i).per() == ones(i, i).T.per() == factorial(i)
175
+ assert (ones(i, i)-eye(i)).per() == (ones(i, i)-eye(i)).T.per() == subfactorial(i)
176
+
177
+ a1, a2, a3, a4, a5 = symbols('a_1 a_2 a_3 a_4 a_5')
178
+ M = Matrix([a1, a2, a3, a4, a5])
179
+ assert M.per() == M.T.per() == a1 + a2 + a3 + a4 + a5
180
+
181
+ def test_adjugate():
182
+ x = Symbol('x')
183
+ e = Matrix(4, 4,
184
+ [x, 1, 2, 3, 4, 5, 6, 7, 2, 9, 10, 11, 12, 13, 14, 14])
185
+
186
+ adj = Matrix([
187
+ [ 4, -8, 4, 0],
188
+ [ 76, -14*x - 68, 14*x - 8, -4*x + 24],
189
+ [-122, 17*x + 142, -21*x + 4, 8*x - 48],
190
+ [ 48, -4*x - 72, 8*x, -4*x + 24]])
191
+ assert e.adjugate() == adj
192
+ assert e.adjugate(method='bareiss') == adj
193
+ assert e.adjugate(method='berkowitz') == adj
194
+ assert e.adjugate(method='bird') == adj
195
+ assert e.adjugate(method='laplace') == adj
196
+
197
+ a = Matrix(2, 3, [1, 2, 3, 4, 5, 6])
198
+ raises(NonSquareMatrixError, lambda: a.adjugate())
199
+
200
+ def test_util():
201
+ R = Rational
202
+
203
+ v1 = Matrix(1, 3, [1, 2, 3])
204
+ v2 = Matrix(1, 3, [3, 4, 5])
205
+ assert v1.norm() == sqrt(14)
206
+ assert v1.project(v2) == Matrix(1, 3, [R(39)/25, R(52)/25, R(13)/5])
207
+ assert Matrix.zeros(1, 2) == Matrix(1, 2, [0, 0])
208
+ assert ones(1, 2) == Matrix(1, 2, [1, 1])
209
+ assert v1.copy() == v1
210
+ # cofactor
211
+ assert eye(3) == eye(3).cofactor_matrix()
212
+ test = Matrix([[1, 3, 2], [2, 6, 3], [2, 3, 6]])
213
+ assert test.cofactor_matrix() == \
214
+ Matrix([[27, -6, -6], [-12, 2, 3], [-3, 1, 0]])
215
+ test = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
216
+ assert test.cofactor_matrix() == \
217
+ Matrix([[-3, 6, -3], [6, -12, 6], [-3, 6, -3]])
218
+
219
+ def test_cofactor_and_minors():
220
+ x = Symbol('x')
221
+ e = Matrix(4, 4,
222
+ [x, 1, 2, 3, 4, 5, 6, 7, 2, 9, 10, 11, 12, 13, 14, 14])
223
+
224
+ m = Matrix([
225
+ [ x, 1, 3],
226
+ [ 2, 9, 11],
227
+ [12, 13, 14]])
228
+ cm = Matrix([
229
+ [ 4, 76, -122, 48],
230
+ [-8, -14*x - 68, 17*x + 142, -4*x - 72],
231
+ [ 4, 14*x - 8, -21*x + 4, 8*x],
232
+ [ 0, -4*x + 24, 8*x - 48, -4*x + 24]])
233
+ sub = Matrix([
234
+ [x, 1, 2],
235
+ [4, 5, 6],
236
+ [2, 9, 10]])
237
+
238
+ assert e.minor_submatrix(1, 2) == m
239
+ assert e.minor_submatrix(-1, -1) == sub
240
+ assert e.minor(1, 2) == -17*x - 142
241
+ assert e.cofactor(1, 2) == 17*x + 142
242
+ assert e.cofactor_matrix() == cm
243
+ assert e.cofactor_matrix(method="bareiss") == cm
244
+ assert e.cofactor_matrix(method="berkowitz") == cm
245
+ assert e.cofactor_matrix(method="bird") == cm
246
+ assert e.cofactor_matrix(method="laplace") == cm
247
+
248
+ raises(ValueError, lambda: e.cofactor(4, 5))
249
+ raises(ValueError, lambda: e.minor(4, 5))
250
+ raises(ValueError, lambda: e.minor_submatrix(4, 5))
251
+
252
+ a = Matrix(2, 3, [1, 2, 3, 4, 5, 6])
253
+ assert a.minor_submatrix(0, 0) == Matrix([[5, 6]])
254
+
255
+ raises(ValueError, lambda:
256
+ Matrix(0, 0, []).minor_submatrix(0, 0))
257
+ raises(NonSquareMatrixError, lambda: a.cofactor(0, 0))
258
+ raises(NonSquareMatrixError, lambda: a.minor(0, 0))
259
+ raises(NonSquareMatrixError, lambda: a.cofactor_matrix())
260
+
261
+ def test_charpoly():
262
+ x, y = Symbol('x'), Symbol('y')
263
+ z, t = Symbol('z'), Symbol('t')
264
+
265
+ from sympy.abc import a,b,c
266
+
267
+ m = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
268
+
269
+ assert eye_Determinant(3).charpoly(x) == Poly((x - 1)**3, x)
270
+ assert eye_Determinant(3).charpoly(y) == Poly((y - 1)**3, y)
271
+ assert m.charpoly() == Poly(x**3 - 15*x**2 - 18*x, x)
272
+ raises(NonSquareMatrixError, lambda: Matrix([[1], [2]]).charpoly())
273
+ n = Matrix(4, 4, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
274
+ assert n.charpoly() == Poly(x**4, x)
275
+
276
+ n = Matrix(4, 4, [45, 0, 0, 0, 0, 23, 0, 0, 0, 0, 87, 0, 0, 0, 0, 12])
277
+ assert n.charpoly() == Poly(x**4 - 167*x**3 + 8811*x**2 - 173457*x + 1080540, x)
278
+
279
+ n = Matrix(3, 3, [x, 0, 0, a, y, 0, b, c, z])
280
+ assert n.charpoly() == Poly(t**3 - (x+y+z)*t**2 + t*(x*y+y*z+x*z) - x*y*z, t)
openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_domains.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Test Matrix/DomainMatrix interaction.
2
+
3
+
4
+ from sympy import GF, ZZ, QQ, EXRAW
5
+ from sympy.polys.matrices import DomainMatrix, DM
6
+
7
+ from sympy import (
8
+ Matrix,
9
+ MutableMatrix,
10
+ ImmutableMatrix,
11
+ SparseMatrix,
12
+ MutableDenseMatrix,
13
+ ImmutableDenseMatrix,
14
+ MutableSparseMatrix,
15
+ ImmutableSparseMatrix,
16
+ )
17
+ from sympy import symbols, S, sqrt
18
+
19
+ from sympy.testing.pytest import raises
20
+
21
+
22
+ x, y = symbols('x y')
23
+
24
+
25
+ MATRIX_TYPES = (
26
+ Matrix,
27
+ MutableMatrix,
28
+ ImmutableMatrix,
29
+ SparseMatrix,
30
+ MutableDenseMatrix,
31
+ ImmutableDenseMatrix,
32
+ MutableSparseMatrix,
33
+ ImmutableSparseMatrix,
34
+ )
35
+ IMMUTABLE = (
36
+ ImmutableMatrix,
37
+ ImmutableDenseMatrix,
38
+ ImmutableSparseMatrix,
39
+ )
40
+
41
+
42
+ def DMs(items, domain):
43
+ return DM(items, domain).to_sparse()
44
+
45
+
46
+ def test_Matrix_rep_domain():
47
+
48
+ for Mat in MATRIX_TYPES:
49
+
50
+ M = Mat([[1, 2], [3, 4]])
51
+ assert M._rep == DMs([[1, 2], [3, 4]], ZZ)
52
+ assert (M / 2)._rep == DMs([[(1,2), 1], [(3,2), 2]], QQ)
53
+ if not isinstance(M, IMMUTABLE):
54
+ M[0, 0] = x
55
+ assert M._rep == DMs([[x, 2], [3, 4]], EXRAW)
56
+
57
+ M = Mat([[S(1)/2, 2], [3, 4]])
58
+ assert M._rep == DMs([[(1,2), 2], [3, 4]], QQ)
59
+ if not isinstance(M, IMMUTABLE):
60
+ M[0, 0] = x
61
+ assert M._rep == DMs([[x, 2], [3, 4]], EXRAW)
62
+
63
+ dM = DMs([[1, 2], [3, 4]], ZZ)
64
+ assert Mat._fromrep(dM)._rep == dM
65
+
66
+ # XXX: This is not intended. Perhaps it should be coerced to EXRAW?
67
+ # The private _fromrep method is never called like this but perhaps it
68
+ # should be guarded.
69
+ #
70
+ # It is not clear how to integrate domains other than ZZ, QQ and EXRAW with
71
+ # the rest of Matrix or if the public type for this needs to be something
72
+ # different from Matrix somehow.
73
+ K = QQ.algebraic_field(sqrt(2))
74
+ dM = DM([[1, 2], [3, 4]], K)
75
+ assert Mat._fromrep(dM)._rep.domain == K
76
+
77
+
78
+ def test_Matrix_to_DM():
79
+
80
+ M = Matrix([[1, 2], [3, 4]])
81
+ assert M.to_DM() == DMs([[1, 2], [3, 4]], ZZ)
82
+ assert M.to_DM() is not M._rep
83
+ assert M.to_DM(field=True) == DMs([[1, 2], [3, 4]], QQ)
84
+ assert M.to_DM(domain=QQ) == DMs([[1, 2], [3, 4]], QQ)
85
+ assert M.to_DM(domain=QQ[x]) == DMs([[1, 2], [3, 4]], QQ[x])
86
+ assert M.to_DM(domain=GF(3)) == DMs([[1, 2], [0, 1]], GF(3))
87
+
88
+ M = Matrix([[1, 2], [3, 4]])
89
+ M[0, 0] = x
90
+ assert M._rep.domain == EXRAW
91
+ M[0, 0] = 1
92
+ assert M.to_DM() == DMs([[1, 2], [3, 4]], ZZ)
93
+
94
+ M = Matrix([[S(1)/2, 2], [3, 4]])
95
+ assert M.to_DM() == DMs([[QQ(1,2), 2], [3, 4]], QQ)
96
+
97
+ M = Matrix([[x, 2], [3, 4]])
98
+ assert M.to_DM() == DMs([[x, 2], [3, 4]], ZZ[x])
99
+ assert M.to_DM(field=True) == DMs([[x, 2], [3, 4]], ZZ.frac_field(x))
100
+
101
+ M = Matrix([[1/x, 2], [3, 4]])
102
+ assert M.to_DM() == DMs([[1/x, 2], [3, 4]], ZZ.frac_field(x))
103
+
104
+ M = Matrix([[1, sqrt(2)], [3, 4]])
105
+ K = QQ.algebraic_field(sqrt(2))
106
+ sqrt2 = K.from_sympy(sqrt(2)) # XXX: Maybe K(sqrt(2)) should work
107
+ M_K = DomainMatrix([[K(1), sqrt2], [K(3), K(4)]], (2, 2), K)
108
+ assert M.to_DM() == DMs([[1, sqrt(2)], [3, 4]], EXRAW)
109
+ assert M.to_DM(extension=True) == M_K.to_sparse()
110
+
111
+ # Options cannot be used with the domain parameter
112
+ M = Matrix([[1, 2], [3, 4]])
113
+ raises(TypeError, lambda: M.to_DM(domain=QQ, field=True))
openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_interactions.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ We have a few different kind of Matrices
3
+ Matrix, ImmutableMatrix, MatrixExpr
4
+
5
+ Here we test the extent to which they cooperate
6
+ """
7
+
8
+ from sympy.core.symbol import symbols
9
+ from sympy.matrices import (Matrix, MatrixSymbol, eye, Identity,
10
+ ImmutableMatrix)
11
+ from sympy.matrices.expressions import MatrixExpr, MatAdd
12
+ from sympy.matrices.matrixbase import classof
13
+ from sympy.testing.pytest import raises
14
+
15
+ SM = MatrixSymbol('X', 3, 3)
16
+ SV = MatrixSymbol('v', 3, 1)
17
+ MM = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
18
+ IM = ImmutableMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
19
+ meye = eye(3)
20
+ imeye = ImmutableMatrix(eye(3))
21
+ ideye = Identity(3)
22
+ a, b, c = symbols('a,b,c')
23
+
24
+
25
+ def test_IM_MM():
26
+ assert isinstance(MM + IM, ImmutableMatrix)
27
+ assert isinstance(IM + MM, ImmutableMatrix)
28
+ assert isinstance(2*IM + MM, ImmutableMatrix)
29
+ assert MM.equals(IM)
30
+
31
+
32
+ def test_ME_MM():
33
+ assert isinstance(Identity(3) + MM, MatrixExpr)
34
+ assert isinstance(SM + MM, MatAdd)
35
+ assert isinstance(MM + SM, MatAdd)
36
+ assert (Identity(3) + MM)[1, 1] == 6
37
+
38
+
39
+ def test_equality():
40
+ a, b, c = Identity(3), eye(3), ImmutableMatrix(eye(3))
41
+ for x in [a, b, c]:
42
+ for y in [a, b, c]:
43
+ assert x.equals(y)
44
+
45
+
46
+ def test_matrix_symbol_MM():
47
+ X = MatrixSymbol('X', 3, 3)
48
+ Y = eye(3) + X
49
+ assert Y[1, 1] == 1 + X[1, 1]
50
+
51
+
52
+ def test_matrix_symbol_vector_matrix_multiplication():
53
+ A = MM * SV
54
+ B = IM * SV
55
+ assert A == B
56
+ C = (SV.T * MM.T).T
57
+ assert B == C
58
+ D = (SV.T * IM.T).T
59
+ assert C == D
60
+
61
+
62
+ def test_indexing_interactions():
63
+ assert (a * IM)[1, 1] == 5*a
64
+ assert (SM + IM)[1, 1] == SM[1, 1] + IM[1, 1]
65
+ assert (SM * IM)[1, 1] == SM[1, 0]*IM[0, 1] + SM[1, 1]*IM[1, 1] + \
66
+ SM[1, 2]*IM[2, 1]
67
+
68
+
69
+ def test_classof():
70
+ A = Matrix(3, 3, range(9))
71
+ B = ImmutableMatrix(3, 3, range(9))
72
+ C = MatrixSymbol('C', 3, 3)
73
+ assert classof(A, A) == Matrix
74
+ assert classof(B, B) == ImmutableMatrix
75
+ assert classof(A, B) == ImmutableMatrix
76
+ assert classof(B, A) == ImmutableMatrix
77
+ raises(TypeError, lambda: classof(A, C))
openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_reductions.py ADDED
@@ -0,0 +1,351 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.numbers import I
2
+ from sympy.core.symbol import symbols
3
+ from sympy.testing.pytest import raises
4
+ from sympy.matrices import Matrix, zeros, eye
5
+ from sympy.core.symbol import Symbol
6
+ from sympy.core.numbers import Rational
7
+ from sympy.functions.elementary.miscellaneous import sqrt
8
+ from sympy.simplify.simplify import simplify
9
+ from sympy.abc import x
10
+
11
+
12
+ # Matrix tests
13
+ def test_row_op():
14
+ e = eye(3)
15
+
16
+ raises(ValueError, lambda: e.elementary_row_op("abc"))
17
+ raises(ValueError, lambda: e.elementary_row_op())
18
+ raises(ValueError, lambda: e.elementary_row_op('n->kn', row=5, k=5))
19
+ raises(ValueError, lambda: e.elementary_row_op('n->kn', row=-5, k=5))
20
+ raises(ValueError, lambda: e.elementary_row_op('n<->m', row1=1, row2=5))
21
+ raises(ValueError, lambda: e.elementary_row_op('n<->m', row1=5, row2=1))
22
+ raises(ValueError, lambda: e.elementary_row_op('n<->m', row1=-5, row2=1))
23
+ raises(ValueError, lambda: e.elementary_row_op('n<->m', row1=1, row2=-5))
24
+ raises(ValueError, lambda: e.elementary_row_op('n->n+km', row1=1, row2=5, k=5))
25
+ raises(ValueError, lambda: e.elementary_row_op('n->n+km', row1=5, row2=1, k=5))
26
+ raises(ValueError, lambda: e.elementary_row_op('n->n+km', row1=-5, row2=1, k=5))
27
+ raises(ValueError, lambda: e.elementary_row_op('n->n+km', row1=1, row2=-5, k=5))
28
+ raises(ValueError, lambda: e.elementary_row_op('n->n+km', row1=1, row2=1, k=5))
29
+
30
+ # test various ways to set arguments
31
+ assert e.elementary_row_op("n->kn", 0, 5) == Matrix([[5, 0, 0], [0, 1, 0], [0, 0, 1]])
32
+ assert e.elementary_row_op("n->kn", 1, 5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
33
+ assert e.elementary_row_op("n->kn", row=1, k=5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
34
+ assert e.elementary_row_op("n->kn", row1=1, k=5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
35
+ assert e.elementary_row_op("n<->m", 0, 1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
36
+ assert e.elementary_row_op("n<->m", row1=0, row2=1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
37
+ assert e.elementary_row_op("n<->m", row=0, row2=1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
38
+ assert e.elementary_row_op("n->n+km", 0, 5, 1) == Matrix([[1, 5, 0], [0, 1, 0], [0, 0, 1]])
39
+ assert e.elementary_row_op("n->n+km", row=0, k=5, row2=1) == Matrix([[1, 5, 0], [0, 1, 0], [0, 0, 1]])
40
+ assert e.elementary_row_op("n->n+km", row1=0, k=5, row2=1) == Matrix([[1, 5, 0], [0, 1, 0], [0, 0, 1]])
41
+
42
+ # make sure the matrix doesn't change size
43
+ a = Matrix(2, 3, [0]*6)
44
+ assert a.elementary_row_op("n->kn", 1, 5) == Matrix(2, 3, [0]*6)
45
+ assert a.elementary_row_op("n<->m", 0, 1) == Matrix(2, 3, [0]*6)
46
+ assert a.elementary_row_op("n->n+km", 0, 5, 1) == Matrix(2, 3, [0]*6)
47
+
48
+
49
+ def test_col_op():
50
+ e = eye(3)
51
+
52
+ raises(ValueError, lambda: e.elementary_col_op("abc"))
53
+ raises(ValueError, lambda: e.elementary_col_op())
54
+ raises(ValueError, lambda: e.elementary_col_op('n->kn', col=5, k=5))
55
+ raises(ValueError, lambda: e.elementary_col_op('n->kn', col=-5, k=5))
56
+ raises(ValueError, lambda: e.elementary_col_op('n<->m', col1=1, col2=5))
57
+ raises(ValueError, lambda: e.elementary_col_op('n<->m', col1=5, col2=1))
58
+ raises(ValueError, lambda: e.elementary_col_op('n<->m', col1=-5, col2=1))
59
+ raises(ValueError, lambda: e.elementary_col_op('n<->m', col1=1, col2=-5))
60
+ raises(ValueError, lambda: e.elementary_col_op('n->n+km', col1=1, col2=5, k=5))
61
+ raises(ValueError, lambda: e.elementary_col_op('n->n+km', col1=5, col2=1, k=5))
62
+ raises(ValueError, lambda: e.elementary_col_op('n->n+km', col1=-5, col2=1, k=5))
63
+ raises(ValueError, lambda: e.elementary_col_op('n->n+km', col1=1, col2=-5, k=5))
64
+ raises(ValueError, lambda: e.elementary_col_op('n->n+km', col1=1, col2=1, k=5))
65
+
66
+ # test various ways to set arguments
67
+ assert e.elementary_col_op("n->kn", 0, 5) == Matrix([[5, 0, 0], [0, 1, 0], [0, 0, 1]])
68
+ assert e.elementary_col_op("n->kn", 1, 5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
69
+ assert e.elementary_col_op("n->kn", col=1, k=5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
70
+ assert e.elementary_col_op("n->kn", col1=1, k=5) == Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 1]])
71
+ assert e.elementary_col_op("n<->m", 0, 1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
72
+ assert e.elementary_col_op("n<->m", col1=0, col2=1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
73
+ assert e.elementary_col_op("n<->m", col=0, col2=1) == Matrix([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
74
+ assert e.elementary_col_op("n->n+km", 0, 5, 1) == Matrix([[1, 0, 0], [5, 1, 0], [0, 0, 1]])
75
+ assert e.elementary_col_op("n->n+km", col=0, k=5, col2=1) == Matrix([[1, 0, 0], [5, 1, 0], [0, 0, 1]])
76
+ assert e.elementary_col_op("n->n+km", col1=0, k=5, col2=1) == Matrix([[1, 0, 0], [5, 1, 0], [0, 0, 1]])
77
+
78
+ # make sure the matrix doesn't change size
79
+ a = Matrix(2, 3, [0]*6)
80
+ assert a.elementary_col_op("n->kn", 1, 5) == Matrix(2, 3, [0]*6)
81
+ assert a.elementary_col_op("n<->m", 0, 1) == Matrix(2, 3, [0]*6)
82
+ assert a.elementary_col_op("n->n+km", 0, 5, 1) == Matrix(2, 3, [0]*6)
83
+
84
+
85
+ def test_is_echelon():
86
+ zro = zeros(3)
87
+ ident = eye(3)
88
+
89
+ assert zro.is_echelon
90
+ assert ident.is_echelon
91
+
92
+ a = Matrix(0, 0, [])
93
+ assert a.is_echelon
94
+
95
+ a = Matrix(2, 3, [3, 2, 1, 0, 0, 6])
96
+ assert a.is_echelon
97
+
98
+ a = Matrix(2, 3, [0, 0, 6, 3, 2, 1])
99
+ assert not a.is_echelon
100
+
101
+ x = Symbol('x')
102
+ a = Matrix(3, 1, [x, 0, 0])
103
+ assert a.is_echelon
104
+
105
+ a = Matrix(3, 1, [x, x, 0])
106
+ assert not a.is_echelon
107
+
108
+ a = Matrix(3, 3, [0, 0, 0, 1, 2, 3, 0, 0, 0])
109
+ assert not a.is_echelon
110
+
111
+
112
+ def test_echelon_form():
113
+ # echelon form is not unique, but the result
114
+ # must be row-equivalent to the original matrix
115
+ # and it must be in echelon form.
116
+
117
+ a = zeros(3)
118
+ e = eye(3)
119
+
120
+ # we can assume the zero matrix and the identity matrix shouldn't change
121
+ assert a.echelon_form() == a
122
+ assert e.echelon_form() == e
123
+
124
+ a = Matrix(0, 0, [])
125
+ assert a.echelon_form() == a
126
+
127
+ a = Matrix(1, 1, [5])
128
+ assert a.echelon_form() == a
129
+
130
+ # now we get to the real tests
131
+
132
+ def verify_row_null_space(mat, rows, nulls):
133
+ for v in nulls:
134
+ assert all(t.is_zero for t in a_echelon*v)
135
+ for v in rows:
136
+ if not all(t.is_zero for t in v):
137
+ assert not all(t.is_zero for t in a_echelon*v.transpose())
138
+
139
+ a = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
140
+ nulls = [Matrix([
141
+ [ 1],
142
+ [-2],
143
+ [ 1]])]
144
+ rows = [a[i, :] for i in range(a.rows)]
145
+ a_echelon = a.echelon_form()
146
+ assert a_echelon.is_echelon
147
+ verify_row_null_space(a, rows, nulls)
148
+
149
+
150
+ a = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 8])
151
+ nulls = []
152
+ rows = [a[i, :] for i in range(a.rows)]
153
+ a_echelon = a.echelon_form()
154
+ assert a_echelon.is_echelon
155
+ verify_row_null_space(a, rows, nulls)
156
+
157
+ a = Matrix(3, 3, [2, 1, 3, 0, 0, 0, 2, 1, 3])
158
+ nulls = [Matrix([
159
+ [Rational(-1, 2)],
160
+ [ 1],
161
+ [ 0]]),
162
+ Matrix([
163
+ [Rational(-3, 2)],
164
+ [ 0],
165
+ [ 1]])]
166
+ rows = [a[i, :] for i in range(a.rows)]
167
+ a_echelon = a.echelon_form()
168
+ assert a_echelon.is_echelon
169
+ verify_row_null_space(a, rows, nulls)
170
+
171
+ # this one requires a row swap
172
+ a = Matrix(3, 3, [2, 1, 3, 0, 0, 0, 1, 1, 3])
173
+ nulls = [Matrix([
174
+ [ 0],
175
+ [ -3],
176
+ [ 1]])]
177
+ rows = [a[i, :] for i in range(a.rows)]
178
+ a_echelon = a.echelon_form()
179
+ assert a_echelon.is_echelon
180
+ verify_row_null_space(a, rows, nulls)
181
+
182
+ a = Matrix(3, 3, [0, 3, 3, 0, 2, 2, 0, 1, 1])
183
+ nulls = [Matrix([
184
+ [1],
185
+ [0],
186
+ [0]]),
187
+ Matrix([
188
+ [ 0],
189
+ [-1],
190
+ [ 1]])]
191
+ rows = [a[i, :] for i in range(a.rows)]
192
+ a_echelon = a.echelon_form()
193
+ assert a_echelon.is_echelon
194
+ verify_row_null_space(a, rows, nulls)
195
+
196
+ a = Matrix(2, 3, [2, 2, 3, 3, 3, 0])
197
+ nulls = [Matrix([
198
+ [-1],
199
+ [1],
200
+ [0]])]
201
+ rows = [a[i, :] for i in range(a.rows)]
202
+ a_echelon = a.echelon_form()
203
+ assert a_echelon.is_echelon
204
+ verify_row_null_space(a, rows, nulls)
205
+
206
+
207
+ def test_rref():
208
+ e = Matrix(0, 0, [])
209
+ assert e.rref(pivots=False) == e
210
+
211
+ e = Matrix(1, 1, [1])
212
+ a = Matrix(1, 1, [5])
213
+ assert e.rref(pivots=False) == a.rref(pivots=False) == e
214
+
215
+ a = Matrix(3, 1, [1, 2, 3])
216
+ assert a.rref(pivots=False) == Matrix([[1], [0], [0]])
217
+
218
+ a = Matrix(1, 3, [1, 2, 3])
219
+ assert a.rref(pivots=False) == Matrix([[1, 2, 3]])
220
+
221
+ a = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
222
+ assert a.rref(pivots=False) == Matrix([
223
+ [1, 0, -1],
224
+ [0, 1, 2],
225
+ [0, 0, 0]])
226
+
227
+ a = Matrix(3, 3, [1, 2, 3, 1, 2, 3, 1, 2, 3])
228
+ b = Matrix(3, 3, [1, 2, 3, 0, 0, 0, 0, 0, 0])
229
+ c = Matrix(3, 3, [0, 0, 0, 1, 2, 3, 0, 0, 0])
230
+ d = Matrix(3, 3, [0, 0, 0, 0, 0, 0, 1, 2, 3])
231
+ assert a.rref(pivots=False) == \
232
+ b.rref(pivots=False) == \
233
+ c.rref(pivots=False) == \
234
+ d.rref(pivots=False) == b
235
+
236
+ e = eye(3)
237
+ z = zeros(3)
238
+ assert e.rref(pivots=False) == e
239
+ assert z.rref(pivots=False) == z
240
+
241
+ a = Matrix([
242
+ [ 0, 0, 1, 2, 2, -5, 3],
243
+ [-1, 5, 2, 2, 1, -7, 5],
244
+ [ 0, 0, -2, -3, -3, 8, -5],
245
+ [-1, 5, 0, -1, -2, 1, 0]])
246
+ mat, pivot_offsets = a.rref()
247
+ assert mat == Matrix([
248
+ [1, -5, 0, 0, 1, 1, -1],
249
+ [0, 0, 1, 0, 0, -1, 1],
250
+ [0, 0, 0, 1, 1, -2, 1],
251
+ [0, 0, 0, 0, 0, 0, 0]])
252
+ assert pivot_offsets == (0, 2, 3)
253
+
254
+ a = Matrix([[Rational(1, 19), Rational(1, 5), 2, 3],
255
+ [ 4, 5, 6, 7],
256
+ [ 8, 9, 10, 11],
257
+ [ 12, 13, 14, 15]])
258
+ assert a.rref(pivots=False) == Matrix([
259
+ [1, 0, 0, Rational(-76, 157)],
260
+ [0, 1, 0, Rational(-5, 157)],
261
+ [0, 0, 1, Rational(238, 157)],
262
+ [0, 0, 0, 0]])
263
+
264
+ x = Symbol('x')
265
+ a = Matrix(2, 3, [x, 1, 1, sqrt(x), x, 1])
266
+ for i, j in zip(a.rref(pivots=False),
267
+ [1, 0, sqrt(x)*(-x + 1)/(-x**Rational(5, 2) + x),
268
+ 0, 1, 1/(sqrt(x) + x + 1)]):
269
+ assert simplify(i - j).is_zero
270
+
271
+
272
+ def test_rref_rhs():
273
+ a, b, c, d = symbols('a b c d')
274
+ A = Matrix([[0, 0], [0, 0], [1, 2], [3, 4]])
275
+ B = Matrix([a, b, c, d])
276
+ assert A.rref_rhs(B) == (Matrix([
277
+ [1, 0],
278
+ [0, 1],
279
+ [0, 0],
280
+ [0, 0]]), Matrix([
281
+ [ -2*c + d],
282
+ [3*c/2 - d/2],
283
+ [ a],
284
+ [ b]]))
285
+
286
+
287
+ def test_issue_17827():
288
+ C = Matrix([
289
+ [3, 4, -1, 1],
290
+ [9, 12, -3, 3],
291
+ [0, 2, 1, 3],
292
+ [2, 3, 0, -2],
293
+ [0, 3, 3, -5],
294
+ [8, 15, 0, 6]
295
+ ])
296
+ # Tests for row/col within valid range
297
+ D = C.elementary_row_op('n<->m', row1=2, row2=5)
298
+ E = C.elementary_row_op('n->n+km', row1=5, row2=3, k=-4)
299
+ F = C.elementary_row_op('n->kn', row=5, k=2)
300
+ assert(D[5, :] == Matrix([[0, 2, 1, 3]]))
301
+ assert(E[5, :] == Matrix([[0, 3, 0, 14]]))
302
+ assert(F[5, :] == Matrix([[16, 30, 0, 12]]))
303
+ # Tests for row/col out of range
304
+ raises(ValueError, lambda: C.elementary_row_op('n<->m', row1=2, row2=6))
305
+ raises(ValueError, lambda: C.elementary_row_op('n->kn', row=7, k=2))
306
+ raises(ValueError, lambda: C.elementary_row_op('n->n+km', row1=-1, row2=5, k=2))
307
+
308
+ def test_rank():
309
+ m = Matrix([[1, 2], [x, 1 - 1/x]])
310
+ assert m.rank() == 2
311
+ n = Matrix(3, 3, range(1, 10))
312
+ assert n.rank() == 2
313
+ p = zeros(3)
314
+ assert p.rank() == 0
315
+
316
+ def test_issue_11434():
317
+ ax, ay, bx, by, cx, cy, dx, dy, ex, ey, t0, t1 = \
318
+ symbols('a_x a_y b_x b_y c_x c_y d_x d_y e_x e_y t_0 t_1')
319
+ M = Matrix([[ax, ay, ax*t0, ay*t0, 0],
320
+ [bx, by, bx*t0, by*t0, 0],
321
+ [cx, cy, cx*t0, cy*t0, 1],
322
+ [dx, dy, dx*t0, dy*t0, 1],
323
+ [ex, ey, 2*ex*t1 - ex*t0, 2*ey*t1 - ey*t0, 0]])
324
+ assert M.rank() == 4
325
+
326
+ def test_rank_regression_from_so():
327
+ # see:
328
+ # https://stackoverflow.com/questions/19072700/why-does-sympy-give-me-the-wrong-answer-when-i-row-reduce-a-symbolic-matrix
329
+
330
+ nu, lamb = symbols('nu, lambda')
331
+ A = Matrix([[-3*nu, 1, 0, 0],
332
+ [ 3*nu, -2*nu - 1, 2, 0],
333
+ [ 0, 2*nu, (-1*nu) - lamb - 2, 3],
334
+ [ 0, 0, nu + lamb, -3]])
335
+ expected_reduced = Matrix([[1, 0, 0, 1/(nu**2*(-lamb - nu))],
336
+ [0, 1, 0, 3/(nu*(-lamb - nu))],
337
+ [0, 0, 1, 3/(-lamb - nu)],
338
+ [0, 0, 0, 0]])
339
+ expected_pivots = (0, 1, 2)
340
+
341
+ reduced, pivots = A.rref()
342
+
343
+ assert simplify(expected_reduced - reduced) == zeros(*A.shape)
344
+ assert pivots == expected_pivots
345
+
346
+ def test_issue_15872():
347
+ A = Matrix([[1, 1, 1, 0], [-2, -1, 0, -1], [0, 0, -1, -1], [0, 0, 2, 1]])
348
+ B = A - Matrix.eye(4) * I
349
+ assert B.rank() == 3
350
+ assert (B**2).rank() == 2
351
+ assert (B**3).rank() == 2
openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_repmatrix.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.testing.pytest import raises
2
+ from sympy.matrices.exceptions import NonSquareMatrixError, NonInvertibleMatrixError
3
+
4
+ from sympy import Matrix, Rational
5
+
6
+
7
+ def test_lll():
8
+ A = Matrix([[1, 0, 0, 0, -20160],
9
+ [0, 1, 0, 0, 33768],
10
+ [0, 0, 1, 0, 39578],
11
+ [0, 0, 0, 1, 47757]])
12
+ L = Matrix([[ 10, -3, -2, 8, -4],
13
+ [ 3, -9, 8, 1, -11],
14
+ [ -3, 13, -9, -3, -9],
15
+ [-12, -7, -11, 9, -1]])
16
+ T = Matrix([[ 10, -3, -2, 8],
17
+ [ 3, -9, 8, 1],
18
+ [ -3, 13, -9, -3],
19
+ [-12, -7, -11, 9]])
20
+ assert A.lll() == L
21
+ assert A.lll_transform() == (L, T)
22
+ assert T * A == L
23
+
24
+
25
+ def test_matrix_inv_mod():
26
+ A = Matrix(2, 1, [1, 0])
27
+ raises(NonSquareMatrixError, lambda: A.inv_mod(2))
28
+ A = Matrix(2, 2, [1, 0, 0, 0])
29
+ raises(NonInvertibleMatrixError, lambda: A.inv_mod(2))
30
+ A = Matrix(2, 2, [1, 2, 3, 4])
31
+ Ai = Matrix(2, 2, [1, 1, 0, 1])
32
+ assert A.inv_mod(3) == Ai
33
+ A = Matrix(2, 2, [1, 0, 0, 1])
34
+ assert A.inv_mod(2) == A
35
+ A = Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
36
+ raises(NonInvertibleMatrixError, lambda: A.inv_mod(5))
37
+ A = Matrix(3, 3, [5, 1, 3, 2, 6, 0, 2, 1, 1])
38
+ Ai = Matrix(3, 3, [6, 8, 0, 1, 5, 6, 5, 6, 4])
39
+ assert A.inv_mod(9) == Ai
40
+ A = Matrix(3, 3, [1, 6, -3, 4, 1, -5, 3, -5, 5])
41
+ Ai = Matrix(3, 3, [4, 3, 3, 1, 2, 5, 1, 5, 1])
42
+ assert A.inv_mod(6) == Ai
43
+ A = Matrix(3, 3, [1, 6, 1, 4, 1, 5, 3, 2, 5])
44
+ Ai = Matrix(3, 3, [6, 0, 3, 6, 6, 4, 1, 6, 1])
45
+ assert A.inv_mod(7) == Ai
46
+ A = Matrix([[1, 2], [3, Rational(3,4)]])
47
+ raises(ValueError, lambda: A.inv_mod(2))
48
+ A = Matrix([[1, 2], [3, 4]])
49
+ raises(TypeError, lambda: A.inv_mod(Rational(1, 2)))
openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_solvers.py ADDED
@@ -0,0 +1,601 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from sympy.core.function import expand_mul
3
+ from sympy.core.numbers import (I, Rational)
4
+ from sympy.core.singleton import S
5
+ from sympy.core.symbol import (Symbol, symbols)
6
+ from sympy.core.sympify import sympify
7
+ from sympy.simplify.simplify import simplify
8
+ from sympy.matrices.exceptions import (ShapeError, NonSquareMatrixError)
9
+ from sympy.matrices import (
10
+ ImmutableMatrix, Matrix, eye, ones, ImmutableDenseMatrix, dotprodsimp)
11
+ from sympy.matrices.determinant import _det_laplace
12
+ from sympy.testing.pytest import raises
13
+ from sympy.matrices.exceptions import NonInvertibleMatrixError
14
+ from sympy.polys.matrices.exceptions import DMShapeError
15
+ from sympy.solvers.solveset import linsolve
16
+ from sympy.abc import x, y
17
+
18
+ def test_issue_17247_expression_blowup_29():
19
+ M = Matrix(S('''[
20
+ [ -3/4, 45/32 - 37*I/16, 0, 0],
21
+ [-149/64 + 49*I/32, -177/128 - 1369*I/128, 0, -2063/256 + 541*I/128],
22
+ [ 0, 9/4 + 55*I/16, 2473/256 + 137*I/64, 0],
23
+ [ 0, 0, 0, -177/128 - 1369*I/128]]'''))
24
+ with dotprodsimp(True):
25
+ assert M.gauss_jordan_solve(ones(4, 1)) == (Matrix(S('''[
26
+ [ -32549314808672/3306971225785 - 17397006745216*I/3306971225785],
27
+ [ 67439348256/3306971225785 - 9167503335872*I/3306971225785],
28
+ [-15091965363354518272/21217636514687010905 + 16890163109293858304*I/21217636514687010905],
29
+ [ -11328/952745 + 87616*I/952745]]''')), Matrix(0, 1, []))
30
+
31
+ def test_issue_17247_expression_blowup_30():
32
+ M = Matrix(S('''[
33
+ [ -3/4, 45/32 - 37*I/16, 0, 0],
34
+ [-149/64 + 49*I/32, -177/128 - 1369*I/128, 0, -2063/256 + 541*I/128],
35
+ [ 0, 9/4 + 55*I/16, 2473/256 + 137*I/64, 0],
36
+ [ 0, 0, 0, -177/128 - 1369*I/128]]'''))
37
+ with dotprodsimp(True):
38
+ assert M.cholesky_solve(ones(4, 1)) == Matrix(S('''[
39
+ [ -32549314808672/3306971225785 - 17397006745216*I/3306971225785],
40
+ [ 67439348256/3306971225785 - 9167503335872*I/3306971225785],
41
+ [-15091965363354518272/21217636514687010905 + 16890163109293858304*I/21217636514687010905],
42
+ [ -11328/952745 + 87616*I/952745]]'''))
43
+
44
+ # @XFAIL # This calculation hangs with dotprodsimp.
45
+ # def test_issue_17247_expression_blowup_31():
46
+ # M = Matrix([
47
+ # [x + 1, 1 - x, 0, 0],
48
+ # [1 - x, x + 1, 0, x + 1],
49
+ # [ 0, 1 - x, x + 1, 0],
50
+ # [ 0, 0, 0, x + 1]])
51
+ # with dotprodsimp(True):
52
+ # assert M.LDLsolve(ones(4, 1)) == Matrix([
53
+ # [(x + 1)/(4*x)],
54
+ # [(x - 1)/(4*x)],
55
+ # [(x + 1)/(4*x)],
56
+ # [ 1/(x + 1)]])
57
+
58
+
59
+ def test_LUsolve_iszerofunc():
60
+ # taken from https://github.com/sympy/sympy/issues/24679
61
+
62
+ M = Matrix([[(x + 1)**2 - (x**2 + 2*x + 1), x], [x, 0]])
63
+ b = Matrix([1, 1])
64
+ is_zero_func = lambda e: False if e._random() else True
65
+
66
+ x_exp = Matrix([1/x, (1-(-x**2 - 2*x + (x+1)**2 - 1)/x)/x])
67
+
68
+ assert (x_exp - M.LUsolve(b, iszerofunc=is_zero_func)) == Matrix([0, 0])
69
+
70
+
71
+ def test_issue_17247_expression_blowup_32():
72
+ M = Matrix([
73
+ [x + 1, 1 - x, 0, 0],
74
+ [1 - x, x + 1, 0, x + 1],
75
+ [ 0, 1 - x, x + 1, 0],
76
+ [ 0, 0, 0, x + 1]])
77
+ with dotprodsimp(True):
78
+ assert M.LUsolve(ones(4, 1)) == Matrix([
79
+ [(x + 1)/(4*x)],
80
+ [(x - 1)/(4*x)],
81
+ [(x + 1)/(4*x)],
82
+ [ 1/(x + 1)]])
83
+
84
+ def test_LUsolve():
85
+ A = Matrix([[2, 3, 5],
86
+ [3, 6, 2],
87
+ [8, 3, 6]])
88
+ x = Matrix(3, 1, [3, 7, 5])
89
+ b = A*x
90
+ soln = A.LUsolve(b)
91
+ assert soln == x
92
+ A = Matrix([[0, -1, 2],
93
+ [5, 10, 7],
94
+ [8, 3, 4]])
95
+ x = Matrix(3, 1, [-1, 2, 5])
96
+ b = A*x
97
+ soln = A.LUsolve(b)
98
+ assert soln == x
99
+ A = Matrix([[2, 1], [1, 0], [1, 0]]) # issue 14548
100
+ b = Matrix([3, 1, 1])
101
+ assert A.LUsolve(b) == Matrix([1, 1])
102
+ b = Matrix([3, 1, 2]) # inconsistent
103
+ raises(ValueError, lambda: A.LUsolve(b))
104
+ A = Matrix([[0, -1, 2],
105
+ [5, 10, 7],
106
+ [8, 3, 4],
107
+ [2, 3, 5],
108
+ [3, 6, 2],
109
+ [8, 3, 6]])
110
+ x = Matrix([2, 1, -4])
111
+ b = A*x
112
+ soln = A.LUsolve(b)
113
+ assert soln == x
114
+ A = Matrix([[0, -1, 2], [5, 10, 7]]) # underdetermined
115
+ x = Matrix([-1, 2, 0])
116
+ b = A*x
117
+ raises(NotImplementedError, lambda: A.LUsolve(b))
118
+
119
+ A = Matrix(4, 4, lambda i, j: 1/(i+j+1) if i != 3 else 0)
120
+ b = Matrix.zeros(4, 1)
121
+ raises(NonInvertibleMatrixError, lambda: A.LUsolve(b))
122
+
123
+
124
+ def test_QRsolve():
125
+ A = Matrix([[2, 3, 5],
126
+ [3, 6, 2],
127
+ [8, 3, 6]])
128
+ x = Matrix(3, 1, [3, 7, 5])
129
+ b = A*x
130
+ soln = A.QRsolve(b)
131
+ assert soln == x
132
+ x = Matrix([[1, 2], [3, 4], [5, 6]])
133
+ b = A*x
134
+ soln = A.QRsolve(b)
135
+ assert soln == x
136
+
137
+ A = Matrix([[0, -1, 2],
138
+ [5, 10, 7],
139
+ [8, 3, 4]])
140
+ x = Matrix(3, 1, [-1, 2, 5])
141
+ b = A*x
142
+ soln = A.QRsolve(b)
143
+ assert soln == x
144
+ x = Matrix([[7, 8], [9, 10], [11, 12]])
145
+ b = A*x
146
+ soln = A.QRsolve(b)
147
+ assert soln == x
148
+
149
+ def test_errors():
150
+ raises(ShapeError, lambda: Matrix([1]).LUsolve(Matrix([[1, 2], [3, 4]])))
151
+
152
+ def test_cholesky_solve():
153
+ A = Matrix([[2, 3, 5],
154
+ [3, 6, 2],
155
+ [8, 3, 6]])
156
+ x = Matrix(3, 1, [3, 7, 5])
157
+ b = A*x
158
+ soln = A.cholesky_solve(b)
159
+ assert soln == x
160
+ A = Matrix([[0, -1, 2],
161
+ [5, 10, 7],
162
+ [8, 3, 4]])
163
+ x = Matrix(3, 1, [-1, 2, 5])
164
+ b = A*x
165
+ soln = A.cholesky_solve(b)
166
+ assert soln == x
167
+ A = Matrix(((1, 5), (5, 1)))
168
+ x = Matrix((4, -3))
169
+ b = A*x
170
+ soln = A.cholesky_solve(b)
171
+ assert soln == x
172
+ A = Matrix(((9, 3*I), (-3*I, 5)))
173
+ x = Matrix((-2, 1))
174
+ b = A*x
175
+ soln = A.cholesky_solve(b)
176
+ assert expand_mul(soln) == x
177
+ A = Matrix(((9*I, 3), (-3 + I, 5)))
178
+ x = Matrix((2 + 3*I, -1))
179
+ b = A*x
180
+ soln = A.cholesky_solve(b)
181
+ assert expand_mul(soln) == x
182
+ a00, a01, a11, b0, b1 = symbols('a00, a01, a11, b0, b1')
183
+ A = Matrix(((a00, a01), (a01, a11)))
184
+ b = Matrix((b0, b1))
185
+ x = A.cholesky_solve(b)
186
+ assert simplify(A*x) == b
187
+
188
+
189
+ def test_LDLsolve():
190
+ A = Matrix([[2, 3, 5],
191
+ [3, 6, 2],
192
+ [8, 3, 6]])
193
+ x = Matrix(3, 1, [3, 7, 5])
194
+ b = A*x
195
+ soln = A.LDLsolve(b)
196
+ assert soln == x
197
+
198
+ A = Matrix([[0, -1, 2],
199
+ [5, 10, 7],
200
+ [8, 3, 4]])
201
+ x = Matrix(3, 1, [-1, 2, 5])
202
+ b = A*x
203
+ soln = A.LDLsolve(b)
204
+ assert soln == x
205
+
206
+ A = Matrix(((9, 3*I), (-3*I, 5)))
207
+ x = Matrix((-2, 1))
208
+ b = A*x
209
+ soln = A.LDLsolve(b)
210
+ assert expand_mul(soln) == x
211
+
212
+ A = Matrix(((9*I, 3), (-3 + I, 5)))
213
+ x = Matrix((2 + 3*I, -1))
214
+ b = A*x
215
+ soln = A.LDLsolve(b)
216
+ assert expand_mul(soln) == x
217
+
218
+ A = Matrix(((9, 3), (3, 9)))
219
+ x = Matrix((1, 1))
220
+ b = A * x
221
+ soln = A.LDLsolve(b)
222
+ assert expand_mul(soln) == x
223
+
224
+ A = Matrix([[-5, -3, -4], [-3, -7, 7]])
225
+ x = Matrix([[8], [7], [-2]])
226
+ b = A * x
227
+ raises(NotImplementedError, lambda: A.LDLsolve(b))
228
+
229
+
230
+ def test_lower_triangular_solve():
231
+
232
+ raises(NonSquareMatrixError,
233
+ lambda: Matrix([1, 0]).lower_triangular_solve(Matrix([0, 1])))
234
+ raises(ShapeError,
235
+ lambda: Matrix([[1, 0], [0, 1]]).lower_triangular_solve(Matrix([1])))
236
+ raises(ValueError,
237
+ lambda: Matrix([[2, 1], [1, 2]]).lower_triangular_solve(
238
+ Matrix([[1, 0], [0, 1]])))
239
+
240
+ A = Matrix([[1, 0], [0, 1]])
241
+ B = Matrix([[x, y], [y, x]])
242
+ C = Matrix([[4, 8], [2, 9]])
243
+
244
+ assert A.lower_triangular_solve(B) == B
245
+ assert A.lower_triangular_solve(C) == C
246
+
247
+
248
+ def test_upper_triangular_solve():
249
+
250
+ raises(NonSquareMatrixError,
251
+ lambda: Matrix([1, 0]).upper_triangular_solve(Matrix([0, 1])))
252
+ raises(ShapeError,
253
+ lambda: Matrix([[1, 0], [0, 1]]).upper_triangular_solve(Matrix([1])))
254
+ raises(TypeError,
255
+ lambda: Matrix([[2, 1], [1, 2]]).upper_triangular_solve(
256
+ Matrix([[1, 0], [0, 1]])))
257
+
258
+ A = Matrix([[1, 0], [0, 1]])
259
+ B = Matrix([[x, y], [y, x]])
260
+ C = Matrix([[2, 4], [3, 8]])
261
+
262
+ assert A.upper_triangular_solve(B) == B
263
+ assert A.upper_triangular_solve(C) == C
264
+
265
+
266
+ def test_diagonal_solve():
267
+ raises(TypeError, lambda: Matrix([1, 1]).diagonal_solve(Matrix([1])))
268
+ A = Matrix([[1, 0], [0, 1]])*2
269
+ B = Matrix([[x, y], [y, x]])
270
+ assert A.diagonal_solve(B) == B/2
271
+
272
+ A = Matrix([[1, 0], [1, 2]])
273
+ raises(TypeError, lambda: A.diagonal_solve(B))
274
+
275
+ def test_pinv_solve():
276
+ # Fully determined system (unique result, identical to other solvers).
277
+ A = Matrix([[1, 5], [7, 9]])
278
+ B = Matrix([12, 13])
279
+ assert A.pinv_solve(B) == A.cholesky_solve(B)
280
+ assert A.pinv_solve(B) == A.LDLsolve(B)
281
+ assert A.pinv_solve(B) == Matrix([sympify('-43/26'), sympify('71/26')])
282
+ assert A * A.pinv() * B == B
283
+ # Fully determined, with two-dimensional B matrix.
284
+ B = Matrix([[12, 13, 14], [15, 16, 17]])
285
+ assert A.pinv_solve(B) == A.cholesky_solve(B)
286
+ assert A.pinv_solve(B) == A.LDLsolve(B)
287
+ assert A.pinv_solve(B) == Matrix([[-33, -37, -41], [69, 75, 81]]) / 26
288
+ assert A * A.pinv() * B == B
289
+ # Underdetermined system (infinite results).
290
+ A = Matrix([[1, 0, 1], [0, 1, 1]])
291
+ B = Matrix([5, 7])
292
+ solution = A.pinv_solve(B)
293
+ w = {}
294
+ for s in solution.atoms(Symbol):
295
+ # Extract dummy symbols used in the solution.
296
+ w[s.name] = s
297
+ assert solution == Matrix([[w['w0_0']/3 + w['w1_0']/3 - w['w2_0']/3 + 1],
298
+ [w['w0_0']/3 + w['w1_0']/3 - w['w2_0']/3 + 3],
299
+ [-w['w0_0']/3 - w['w1_0']/3 + w['w2_0']/3 + 4]])
300
+ assert A * A.pinv() * B == B
301
+ # Overdetermined system (least squares results).
302
+ A = Matrix([[1, 0], [0, 0], [0, 1]])
303
+ B = Matrix([3, 2, 1])
304
+ assert A.pinv_solve(B) == Matrix([3, 1])
305
+ # Proof the solution is not exact.
306
+ assert A * A.pinv() * B != B
307
+
308
+ def test_pinv_rank_deficient():
309
+ # Test the four properties of the pseudoinverse for various matrices.
310
+ As = [Matrix([[1, 1, 1], [2, 2, 2]]),
311
+ Matrix([[1, 0], [0, 0]]),
312
+ Matrix([[1, 2], [2, 4], [3, 6]])]
313
+
314
+ for A in As:
315
+ A_pinv = A.pinv(method="RD")
316
+ AAp = A * A_pinv
317
+ ApA = A_pinv * A
318
+ assert simplify(AAp * A) == A
319
+ assert simplify(ApA * A_pinv) == A_pinv
320
+ assert AAp.H == AAp
321
+ assert ApA.H == ApA
322
+
323
+ for A in As:
324
+ A_pinv = A.pinv(method="ED")
325
+ AAp = A * A_pinv
326
+ ApA = A_pinv * A
327
+ assert simplify(AAp * A) == A
328
+ assert simplify(ApA * A_pinv) == A_pinv
329
+ assert AAp.H == AAp
330
+ assert ApA.H == ApA
331
+
332
+ # Test solving with rank-deficient matrices.
333
+ A = Matrix([[1, 0], [0, 0]])
334
+ # Exact, non-unique solution.
335
+ B = Matrix([3, 0])
336
+ solution = A.pinv_solve(B)
337
+ w1 = solution.atoms(Symbol).pop()
338
+ assert w1.name == 'w1_0'
339
+ assert solution == Matrix([3, w1])
340
+ assert A * A.pinv() * B == B
341
+ # Least squares, non-unique solution.
342
+ B = Matrix([3, 1])
343
+ solution = A.pinv_solve(B)
344
+ w1 = solution.atoms(Symbol).pop()
345
+ assert w1.name == 'w1_0'
346
+ assert solution == Matrix([3, w1])
347
+ assert A * A.pinv() * B != B
348
+
349
+ def test_gauss_jordan_solve():
350
+
351
+ # Square, full rank, unique solution
352
+ A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
353
+ b = Matrix([3, 6, 9])
354
+ sol, params = A.gauss_jordan_solve(b)
355
+ assert sol == Matrix([[-1], [2], [0]])
356
+ assert params == Matrix(0, 1, [])
357
+
358
+ # Square, full rank, unique solution, B has more columns than rows
359
+ A = eye(3)
360
+ B = Matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
361
+ sol, params = A.gauss_jordan_solve(B)
362
+ assert sol == B
363
+ assert params == Matrix(0, 4, [])
364
+
365
+ # Square, reduced rank, parametrized solution
366
+ A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
367
+ b = Matrix([3, 6, 9])
368
+ sol, params, freevar = A.gauss_jordan_solve(b, freevar=True)
369
+ w = {}
370
+ for s in sol.atoms(Symbol):
371
+ # Extract dummy symbols used in the solution.
372
+ w[s.name] = s
373
+ assert sol == Matrix([[w['tau0'] - 1], [-2*w['tau0'] + 2], [w['tau0']]])
374
+ assert params == Matrix([[w['tau0']]])
375
+ assert freevar == [2]
376
+
377
+ # Square, reduced rank, parametrized solution, B has two columns
378
+ A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
379
+ B = Matrix([[3, 4], [6, 8], [9, 12]])
380
+ sol, params, freevar = A.gauss_jordan_solve(B, freevar=True)
381
+ w = {}
382
+ for s in sol.atoms(Symbol):
383
+ # Extract dummy symbols used in the solution.
384
+ w[s.name] = s
385
+ assert sol == Matrix([[w['tau0'] - 1, w['tau1'] - Rational(4, 3)],
386
+ [-2*w['tau0'] + 2, -2*w['tau1'] + Rational(8, 3)],
387
+ [w['tau0'], w['tau1']],])
388
+ assert params == Matrix([[w['tau0'], w['tau1']]])
389
+ assert freevar == [2]
390
+
391
+ # Square, reduced rank, parametrized solution
392
+ A = Matrix([[1, 2, 3], [2, 4, 6], [3, 6, 9]])
393
+ b = Matrix([0, 0, 0])
394
+ sol, params = A.gauss_jordan_solve(b)
395
+ w = {}
396
+ for s in sol.atoms(Symbol):
397
+ w[s.name] = s
398
+ assert sol == Matrix([[-2*w['tau0'] - 3*w['tau1']],
399
+ [w['tau0']], [w['tau1']]])
400
+ assert params == Matrix([[w['tau0']], [w['tau1']]])
401
+
402
+ # Square, reduced rank, parametrized solution
403
+ A = Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
404
+ b = Matrix([0, 0, 0])
405
+ sol, params = A.gauss_jordan_solve(b)
406
+ w = {}
407
+ for s in sol.atoms(Symbol):
408
+ w[s.name] = s
409
+ assert sol == Matrix([[w['tau0']], [w['tau1']], [w['tau2']]])
410
+ assert params == Matrix([[w['tau0']], [w['tau1']], [w['tau2']]])
411
+
412
+ # Square, reduced rank, no solution
413
+ A = Matrix([[1, 2, 3], [2, 4, 6], [3, 6, 9]])
414
+ b = Matrix([0, 0, 1])
415
+ raises(ValueError, lambda: A.gauss_jordan_solve(b))
416
+
417
+ # Rectangular, tall, full rank, unique solution
418
+ A = Matrix([[1, 5, 3], [2, 1, 6], [1, 7, 9], [1, 4, 3]])
419
+ b = Matrix([0, 0, 1, 0])
420
+ sol, params = A.gauss_jordan_solve(b)
421
+ assert sol == Matrix([[Rational(-1, 2)], [0], [Rational(1, 6)]])
422
+ assert params == Matrix(0, 1, [])
423
+
424
+ # Rectangular, tall, full rank, unique solution, B has less columns than rows
425
+ A = Matrix([[1, 5, 3], [2, 1, 6], [1, 7, 9], [1, 4, 3]])
426
+ B = Matrix([[0,0], [0, 0], [1, 2], [0, 0]])
427
+ sol, params = A.gauss_jordan_solve(B)
428
+ assert sol == Matrix([[Rational(-1, 2), Rational(-2, 2)], [0, 0], [Rational(1, 6), Rational(2, 6)]])
429
+ assert params == Matrix(0, 2, [])
430
+
431
+ # Rectangular, tall, full rank, no solution
432
+ A = Matrix([[1, 5, 3], [2, 1, 6], [1, 7, 9], [1, 4, 3]])
433
+ b = Matrix([0, 0, 0, 1])
434
+ raises(ValueError, lambda: A.gauss_jordan_solve(b))
435
+
436
+ # Rectangular, tall, full rank, no solution, B has two columns (2nd has no solution)
437
+ A = Matrix([[1, 5, 3], [2, 1, 6], [1, 7, 9], [1, 4, 3]])
438
+ B = Matrix([[0,0], [0, 0], [1, 0], [0, 1]])
439
+ raises(ValueError, lambda: A.gauss_jordan_solve(B))
440
+
441
+ # Rectangular, tall, full rank, no solution, B has two columns (1st has no solution)
442
+ A = Matrix([[1, 5, 3], [2, 1, 6], [1, 7, 9], [1, 4, 3]])
443
+ B = Matrix([[0,0], [0, 0], [0, 1], [1, 0]])
444
+ raises(ValueError, lambda: A.gauss_jordan_solve(B))
445
+
446
+ # Rectangular, tall, reduced rank, parametrized solution
447
+ A = Matrix([[1, 5, 3], [2, 10, 6], [3, 15, 9], [1, 4, 3]])
448
+ b = Matrix([0, 0, 0, 1])
449
+ sol, params = A.gauss_jordan_solve(b)
450
+ w = {}
451
+ for s in sol.atoms(Symbol):
452
+ w[s.name] = s
453
+ assert sol == Matrix([[-3*w['tau0'] + 5], [-1], [w['tau0']]])
454
+ assert params == Matrix([[w['tau0']]])
455
+
456
+ # Rectangular, tall, reduced rank, no solution
457
+ A = Matrix([[1, 5, 3], [2, 10, 6], [3, 15, 9], [1, 4, 3]])
458
+ b = Matrix([0, 0, 1, 1])
459
+ raises(ValueError, lambda: A.gauss_jordan_solve(b))
460
+
461
+ # Rectangular, wide, full rank, parametrized solution
462
+ A = Matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 1, 12]])
463
+ b = Matrix([1, 1, 1])
464
+ sol, params = A.gauss_jordan_solve(b)
465
+ w = {}
466
+ for s in sol.atoms(Symbol):
467
+ w[s.name] = s
468
+ assert sol == Matrix([[2*w['tau0'] - 1], [-3*w['tau0'] + 1], [0],
469
+ [w['tau0']]])
470
+ assert params == Matrix([[w['tau0']]])
471
+
472
+ # Rectangular, wide, reduced rank, parametrized solution
473
+ A = Matrix([[1, 2, 3, 4], [5, 6, 7, 8], [2, 4, 6, 8]])
474
+ b = Matrix([0, 1, 0])
475
+ sol, params = A.gauss_jordan_solve(b)
476
+ w = {}
477
+ for s in sol.atoms(Symbol):
478
+ w[s.name] = s
479
+ assert sol == Matrix([[w['tau0'] + 2*w['tau1'] + S.Half],
480
+ [-2*w['tau0'] - 3*w['tau1'] - Rational(1, 4)],
481
+ [w['tau0']], [w['tau1']]])
482
+ assert params == Matrix([[w['tau0']], [w['tau1']]])
483
+ # watch out for clashing symbols
484
+ x0, x1, x2, _x0 = symbols('_tau0 _tau1 _tau2 tau1')
485
+ M = Matrix([[0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, _x0]])
486
+ A = M[:, :-1]
487
+ b = M[:, -1:]
488
+ sol, params = A.gauss_jordan_solve(b)
489
+ assert params == Matrix(3, 1, [x0, x1, x2])
490
+ assert sol == Matrix(5, 1, [x0, 0, x1, _x0, x2])
491
+
492
+ # Rectangular, wide, reduced rank, no solution
493
+ A = Matrix([[1, 2, 3, 4], [5, 6, 7, 8], [2, 4, 6, 8]])
494
+ b = Matrix([1, 1, 1])
495
+ raises(ValueError, lambda: A.gauss_jordan_solve(b))
496
+
497
+ # Test for immutable matrix
498
+ A = ImmutableMatrix([[1, 0], [0, 1]])
499
+ B = ImmutableMatrix([1, 2])
500
+ sol, params = A.gauss_jordan_solve(B)
501
+ assert sol == ImmutableMatrix([1, 2])
502
+ assert params == ImmutableMatrix(0, 1, [])
503
+ assert sol.__class__ == ImmutableDenseMatrix
504
+ assert params.__class__ == ImmutableDenseMatrix
505
+
506
+ # Test placement of free variables
507
+ A = Matrix([[1, 0, 0, 0], [0, 0, 0, 1]])
508
+ b = Matrix([1, 1])
509
+ sol, params = A.gauss_jordan_solve(b)
510
+ w = {}
511
+ for s in sol.atoms(Symbol):
512
+ w[s.name] = s
513
+ assert sol == Matrix([[1], [w['tau0']], [w['tau1']], [1]])
514
+ assert params == Matrix([[w['tau0']], [w['tau1']]])
515
+
516
+
517
+ def test_linsolve_underdetermined_AND_gauss_jordan_solve():
518
+ #Test placement of free variables as per issue 19815
519
+ A = Matrix([[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
520
+ [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
521
+ [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
522
+ [0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
523
+ [0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
524
+ [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
525
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
526
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]])
527
+ B = Matrix([1, 2, 1, 1, 1, 1, 1, 2])
528
+ sol, params = A.gauss_jordan_solve(B)
529
+ w = {}
530
+ for s in sol.atoms(Symbol):
531
+ w[s.name] = s
532
+ assert params == Matrix([[w['tau0']], [w['tau1']], [w['tau2']],
533
+ [w['tau3']], [w['tau4']], [w['tau5']]])
534
+ assert sol == Matrix([[1 - 1*w['tau2']],
535
+ [w['tau2']],
536
+ [1 - 1*w['tau0'] + w['tau1']],
537
+ [w['tau0']],
538
+ [w['tau3'] + w['tau4']],
539
+ [-1*w['tau3'] - 1*w['tau4'] - 1*w['tau1']],
540
+ [1 - 1*w['tau2']],
541
+ [w['tau1']],
542
+ [w['tau2']],
543
+ [w['tau3']],
544
+ [w['tau4']],
545
+ [1 - 1*w['tau5']],
546
+ [w['tau5']],
547
+ [1]])
548
+
549
+ from sympy.abc import j,f
550
+ # https://github.com/sympy/sympy/issues/20046
551
+ A = Matrix([
552
+ [1, 1, 1, 1, 1, 1, 1, 1, 1],
553
+ [0, -1, 0, -1, 0, -1, 0, -1, -j],
554
+ [0, 0, 0, 0, 1, 1, 1, 1, f]
555
+ ])
556
+
557
+ sol_1=Matrix(list(linsolve(A))[0])
558
+
559
+ tau0, tau1, tau2, tau3, tau4 = symbols('tau:5')
560
+
561
+ assert sol_1 == Matrix([[-f - j - tau0 + tau2 + tau4 + 1],
562
+ [j - tau1 - tau2 - tau4],
563
+ [tau0],
564
+ [tau1],
565
+ [f - tau2 - tau3 - tau4],
566
+ [tau2],
567
+ [tau3],
568
+ [tau4]])
569
+
570
+ # https://github.com/sympy/sympy/issues/19815
571
+ sol_2 = A[:, : -1 ] * sol_1 - A[:, -1 ]
572
+ assert sol_2 == Matrix([[0], [0], [0]])
573
+
574
+
575
+ @pytest.mark.parametrize("det_method", ["bird", "laplace"])
576
+ @pytest.mark.parametrize("M, rhs", [
577
+ (Matrix([[2, 3, 5], [3, 6, 2], [8, 3, 6]]), Matrix(3, 1, [3, 7, 5])),
578
+ (Matrix([[2, 3, 5], [3, 6, 2], [8, 3, 6]]),
579
+ Matrix([[1, 2], [3, 4], [5, 6]])),
580
+ (Matrix(2, 2, symbols("a:4")), Matrix(2, 1, symbols("b:2"))),
581
+ ])
582
+ def test_cramer_solve(det_method, M, rhs):
583
+ assert simplify(M.cramer_solve(rhs, det_method=det_method) - M.LUsolve(rhs)
584
+ ) == Matrix.zeros(M.rows, rhs.cols)
585
+
586
+
587
+ @pytest.mark.parametrize("det_method, error", [
588
+ ("bird", DMShapeError), (_det_laplace, NonSquareMatrixError)])
589
+ def test_cramer_solve_errors(det_method, error):
590
+ # Non-square matrix
591
+ A = Matrix([[0, -1, 2], [5, 10, 7]])
592
+ b = Matrix([-2, 15])
593
+ raises(error, lambda: A.cramer_solve(b, det_method=det_method))
594
+
595
+
596
+ def test_solve():
597
+ A = Matrix([[1,2], [2,4]])
598
+ b = Matrix([[3], [4]])
599
+ raises(ValueError, lambda: A.solve(b)) #no solution
600
+ b = Matrix([[ 4], [8]])
601
+ raises(ValueError, lambda: A.solve(b)) #infinite solution
openflamingo/lib/python3.10/site-packages/sympy/matrices/tests/test_sparsetools.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.matrices.sparsetools import _doktocsr, _csrtodok, banded
2
+ from sympy.matrices.dense import (Matrix, eye, ones, zeros)
3
+ from sympy.matrices import SparseMatrix
4
+ from sympy.testing.pytest import raises
5
+
6
+
7
+ def test_doktocsr():
8
+ a = SparseMatrix([[1, 2, 0, 0], [0, 3, 9, 0], [0, 1, 4, 0]])
9
+ b = SparseMatrix(4, 6, [10, 20, 0, 0, 0, 0, 0, 30, 0, 40, 0, 0, 0, 0, 50,
10
+ 60, 70, 0, 0, 0, 0, 0, 0, 80])
11
+ c = SparseMatrix(4, 4, [0, 0, 0, 0, 0, 12, 0, 2, 15, 0, 12, 0, 0, 0, 0, 4])
12
+ d = SparseMatrix(10, 10, {(1, 1): 12, (3, 5): 7, (7, 8): 12})
13
+ e = SparseMatrix([[0, 0, 0], [1, 0, 2], [3, 0, 0]])
14
+ f = SparseMatrix(7, 8, {(2, 3): 5, (4, 5):12})
15
+ assert _doktocsr(a) == [[1, 2, 3, 9, 1, 4], [0, 1, 1, 2, 1, 2],
16
+ [0, 2, 4, 6], [3, 4]]
17
+ assert _doktocsr(b) == [[10, 20, 30, 40, 50, 60, 70, 80],
18
+ [0, 1, 1, 3, 2, 3, 4, 5], [0, 2, 4, 7, 8], [4, 6]]
19
+ assert _doktocsr(c) == [[12, 2, 15, 12, 4], [1, 3, 0, 2, 3],
20
+ [0, 0, 2, 4, 5], [4, 4]]
21
+ assert _doktocsr(d) == [[12, 7, 12], [1, 5, 8],
22
+ [0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3], [10, 10]]
23
+ assert _doktocsr(e) == [[1, 2, 3], [0, 2, 0], [0, 0, 2, 3], [3, 3]]
24
+ assert _doktocsr(f) == [[5, 12], [3, 5], [0, 0, 0, 1, 1, 2, 2, 2], [7, 8]]
25
+
26
+
27
+ def test_csrtodok():
28
+ h = [[5, 7, 5], [2, 1, 3], [0, 1, 1, 3], [3, 4]]
29
+ g = [[12, 5, 4], [2, 4, 2], [0, 1, 2, 3], [3, 7]]
30
+ i = [[1, 3, 12], [0, 2, 4], [0, 2, 3], [2, 5]]
31
+ j = [[11, 15, 12, 15], [2, 4, 1, 2], [0, 1, 1, 2, 3, 4], [5, 8]]
32
+ k = [[1, 3], [2, 1], [0, 1, 1, 2], [3, 3]]
33
+ m = _csrtodok(h)
34
+ assert isinstance(m, SparseMatrix)
35
+ assert m == SparseMatrix(3, 4,
36
+ {(0, 2): 5, (2, 1): 7, (2, 3): 5})
37
+ assert _csrtodok(g) == SparseMatrix(3, 7,
38
+ {(0, 2): 12, (1, 4): 5, (2, 2): 4})
39
+ assert _csrtodok(i) == SparseMatrix([[1, 0, 3, 0, 0], [0, 0, 0, 0, 12]])
40
+ assert _csrtodok(j) == SparseMatrix(5, 8,
41
+ {(0, 2): 11, (2, 4): 15, (3, 1): 12, (4, 2): 15})
42
+ assert _csrtodok(k) == SparseMatrix(3, 3, {(0, 2): 1, (2, 1): 3})
43
+
44
+
45
+ def test_banded():
46
+ raises(TypeError, lambda: banded())
47
+ raises(TypeError, lambda: banded(1))
48
+ raises(TypeError, lambda: banded(1, 2))
49
+ raises(TypeError, lambda: banded(1, 2, 3))
50
+ raises(TypeError, lambda: banded(1, 2, 3, 4))
51
+ raises(ValueError, lambda: banded({0: (1, 2)}, rows=1))
52
+ raises(ValueError, lambda: banded({0: (1, 2)}, cols=1))
53
+ raises(ValueError, lambda: banded(1, {0: (1, 2)}))
54
+ raises(ValueError, lambda: banded(2, 1, {0: (1, 2)}))
55
+ raises(ValueError, lambda: banded(1, 2, {0: (1, 2)}))
56
+
57
+ assert isinstance(banded(2, 4, {}), SparseMatrix)
58
+ assert banded(2, 4, {}) == zeros(2, 4)
59
+ assert banded({0: 0, 1: 0}) == zeros(0)
60
+ assert banded({0: Matrix([1, 2])}) == Matrix([1, 2])
61
+ assert banded({1: [1, 2, 3, 0], -1: [4, 5, 6]}) == \
62
+ banded({1: (1, 2, 3), -1: (4, 5, 6)}) == \
63
+ Matrix([
64
+ [0, 1, 0, 0],
65
+ [4, 0, 2, 0],
66
+ [0, 5, 0, 3],
67
+ [0, 0, 6, 0]])
68
+ assert banded(3, 4, {-1: 1, 0: 2, 1: 3}) == \
69
+ Matrix([
70
+ [2, 3, 0, 0],
71
+ [1, 2, 3, 0],
72
+ [0, 1, 2, 3]])
73
+ s = lambda d: (1 + d)**2
74
+ assert banded(5, {0: s, 2: s}) == \
75
+ Matrix([
76
+ [1, 0, 1, 0, 0],
77
+ [0, 4, 0, 4, 0],
78
+ [0, 0, 9, 0, 9],
79
+ [0, 0, 0, 16, 0],
80
+ [0, 0, 0, 0, 25]])
81
+ assert banded(2, {0: 1}) == \
82
+ Matrix([
83
+ [1, 0],
84
+ [0, 1]])
85
+ assert banded(2, 3, {0: 1}) == \
86
+ Matrix([
87
+ [1, 0, 0],
88
+ [0, 1, 0]])
89
+ vert = Matrix([1, 2, 3])
90
+ assert banded({0: vert}, cols=3) == \
91
+ Matrix([
92
+ [1, 0, 0],
93
+ [2, 1, 0],
94
+ [3, 2, 1],
95
+ [0, 3, 2],
96
+ [0, 0, 3]])
97
+ assert banded(4, {0: ones(2)}) == \
98
+ Matrix([
99
+ [1, 1, 0, 0],
100
+ [1, 1, 0, 0],
101
+ [0, 0, 1, 1],
102
+ [0, 0, 1, 1]])
103
+ raises(ValueError, lambda: banded({0: 2, 1: ones(2)}, rows=5))
104
+ assert banded({0: 2, 2: (ones(2),)*3}) == \
105
+ Matrix([
106
+ [2, 0, 1, 1, 0, 0, 0, 0],
107
+ [0, 2, 1, 1, 0, 0, 0, 0],
108
+ [0, 0, 2, 0, 1, 1, 0, 0],
109
+ [0, 0, 0, 2, 1, 1, 0, 0],
110
+ [0, 0, 0, 0, 2, 0, 1, 1],
111
+ [0, 0, 0, 0, 0, 2, 1, 1]])
112
+ raises(ValueError, lambda: banded({0: (2,)*5, 1: (ones(2),)*3}))
113
+ u2 = Matrix([[1, 1], [0, 1]])
114
+ assert banded({0: (2,)*5, 1: (u2,)*3}) == \
115
+ Matrix([
116
+ [2, 1, 1, 0, 0, 0, 0],
117
+ [0, 2, 1, 0, 0, 0, 0],
118
+ [0, 0, 2, 1, 1, 0, 0],
119
+ [0, 0, 0, 2, 1, 0, 0],
120
+ [0, 0, 0, 0, 2, 1, 1],
121
+ [0, 0, 0, 0, 0, 0, 1]])
122
+ assert banded({0:(0, ones(2)), 2: 2}) == \
123
+ Matrix([
124
+ [0, 0, 2],
125
+ [0, 1, 1],
126
+ [0, 1, 1]])
127
+ raises(ValueError, lambda: banded({0: (0, ones(2)), 1: 2}))
128
+ assert banded({0: 1}, cols=3) == banded({0: 1}, rows=3) == eye(3)
129
+ assert banded({1: 1}, rows=3) == Matrix([
130
+ [0, 1, 0],
131
+ [0, 0, 1],
132
+ [0, 0, 0]])
phi4/bin/bunzip2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8a514cce807cb1656a3bcd59794401e7d63c9554267e9acc77097a406092a8ed
3
+ size 299464
phi4/bin/lzma ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:07d32f3060c130f5192e1441831d7fce2f9c4a9612b347a0181296419bc04856
3
+ size 108336
phi4/bin/unxz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:07d32f3060c130f5192e1441831d7fce2f9c4a9612b347a0181296419bc04856
3
+ size 108336
phi4/bin/x86_64-conda_cos7-linux-gnu-ld ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:aaaab6b3200c6f71e5f2970b01a074c958d5af546e5f43c011192307f69d9cac
3
+ size 2195376
phi4/bin/xz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:07d32f3060c130f5192e1441831d7fce2f9c4a9612b347a0181296419bc04856
3
+ size 108336
phi4/bin/xzcat ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:07d32f3060c130f5192e1441831d7fce2f9c4a9612b347a0181296419bc04856
3
+ size 108336
phi4/compiler_compat/README ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Files in this folder are to enhance backwards compatibility of anaconda software with older compilers.
2
+ See: https://github.com/conda/conda/issues/6030 for more information.
phi4/conda-meta/libuuid-1.41.5-h5eee18b_0.json ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "build": "h5eee18b_0",
3
+ "build_number": 0,
4
+ "channel": "https://repo.anaconda.com/pkgs/main",
5
+ "constrains": [],
6
+ "depends": [
7
+ "libgcc-ng >=11.2.0"
8
+ ],
9
+ "extracted_package_dir": "/opt/conda/pkgs/libuuid-1.41.5-h5eee18b_0",
10
+ "files": [
11
+ "include/uuid/uuid.h",
12
+ "lib/libuuid.a",
13
+ "lib/libuuid.so",
14
+ "lib/libuuid.so.1",
15
+ "lib/libuuid.so.1.3.0",
16
+ "lib/pkgconfig/uuid.pc"
17
+ ],
18
+ "fn": "libuuid-1.41.5-h5eee18b_0.conda",
19
+ "license": "BSD-3-Clause",
20
+ "link": {
21
+ "source": "/opt/conda/pkgs/libuuid-1.41.5-h5eee18b_0",
22
+ "type": 1
23
+ },
24
+ "md5": "4a6a2354414c9080327274aa514e5299",
25
+ "name": "libuuid",
26
+ "package_tarball_full_path": "/opt/conda/pkgs/libuuid-1.41.5-h5eee18b_0.conda",
27
+ "paths_data": {
28
+ "paths": [
29
+ {
30
+ "_path": "include/uuid/uuid.h",
31
+ "path_type": "hardlink",
32
+ "sha256": "926b9441cae3c113950827ef438cb0b07657f6ec1d2fe5f3ba557662ddbb526b",
33
+ "sha256_in_prefix": "926b9441cae3c113950827ef438cb0b07657f6ec1d2fe5f3ba557662ddbb526b",
34
+ "size_in_bytes": 3910
35
+ },
36
+ {
37
+ "_path": "lib/libuuid.a",
38
+ "path_type": "hardlink",
39
+ "sha256": "d16861859d7ad6a76c11296ef77000e95f64d75330ce6365f679c1c88c0ecabd",
40
+ "sha256_in_prefix": "d16861859d7ad6a76c11296ef77000e95f64d75330ce6365f679c1c88c0ecabd",
41
+ "size_in_bytes": 53390
42
+ },
43
+ {
44
+ "_path": "lib/libuuid.so",
45
+ "path_type": "softlink",
46
+ "sha256": "b42fa6cf1dcaca6b84e8155c4649d6bad561eaca2e8fa9473db178dbaa4aec53",
47
+ "size_in_bytes": 35944
48
+ },
49
+ {
50
+ "_path": "lib/libuuid.so.1",
51
+ "path_type": "softlink",
52
+ "sha256": "b42fa6cf1dcaca6b84e8155c4649d6bad561eaca2e8fa9473db178dbaa4aec53",
53
+ "size_in_bytes": 35944
54
+ },
55
+ {
56
+ "_path": "lib/libuuid.so.1.3.0",
57
+ "path_type": "hardlink",
58
+ "sha256": "b42fa6cf1dcaca6b84e8155c4649d6bad561eaca2e8fa9473db178dbaa4aec53",
59
+ "sha256_in_prefix": "b42fa6cf1dcaca6b84e8155c4649d6bad561eaca2e8fa9473db178dbaa4aec53",
60
+ "size_in_bytes": 35944
61
+ },
62
+ {
63
+ "_path": "lib/pkgconfig/uuid.pc",
64
+ "file_mode": "text",
65
+ "path_type": "hardlink",
66
+ "prefix_placeholder": "/croot/libuuid_1668082679328/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold",
67
+ "sha256": "ad771b4cb15ca6fcc526199ebbc1c4ff31e1cf96f01fb4710b14462327ebbef1",
68
+ "sha256_in_prefix": "e27debe8eb6ff313bb5f980796b5d0230e6dba1820b4dfe9c049bd3b086dc9ab",
69
+ "size_in_bytes": 1208
70
+ }
71
+ ],
72
+ "paths_version": 1
73
+ },
74
+ "requested_spec": "None",
75
+ "sha256": "2a401aafabac51b7736cfe12d2ab205d29052640ea8183253c9d0a8e7ed0d49a",
76
+ "size": 28110,
77
+ "subdir": "linux-64",
78
+ "timestamp": 1668082729000,
79
+ "url": "https://repo.anaconda.com/pkgs/main/linux-64/libuuid-1.41.5-h5eee18b_0.conda",
80
+ "version": "1.41.5"
81
+ }