Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +4 -0
- llm_tutorial/merged_models/en-ja-base_0.33+instruct/special_tokens_map.json +51 -0
- llm_tutorial/merged_models/en-ja-base_0.33/model.safetensors.index.json +1 -0
- llm_tutorial/merged_models/en-ja-base_dare-linear/model.safetensors.index.json +1 -0
- llm_tutorial/merged_models4/ja-en_en-ja_ties02/tokenizer_config.json +18 -0
- llm_tutorial/merged_models4/ja-en_en-ja_ties03-instruct/model.safetensors.index.json +1 -0
- llm_tutorial/merged_models4/ja-en_en-ja_ties03/special_tokens_map.json +10 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/markupsafe/__pycache__/_native.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/markupsafe/_native.py +63 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/markupsafe/_speedups.pyi +9 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/mpmath-1.3.0.dist-info/LICENSE +27 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/mpmath-1.3.0.dist-info/METADATA +233 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/mpmath-1.3.0.dist-info/RECORD +180 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/mpmath-1.3.0.dist-info/WHEEL +5 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/cublas/lib/libcublas.so.11 +3 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/cudnn/lib/libcudnn_cnn_train.so.8 +3 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/cudnn/lib/libcudnn_ops_infer.so.8 +3 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/curand/lib/libcurand.so.10 +3 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__init__.py +8 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__pycache__/expr_with_intlimits.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__pycache__/expr_with_limits.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__pycache__/guess.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__pycache__/products.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__pycache__/summations.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/gosper.py +227 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/guess.py +473 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/tests/__init__.py +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/tests/__pycache__/test_guess.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/tests/__pycache__/test_products.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/tests/test_gosper.py +204 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/tests/test_guess.py +82 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__init__.py +72 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/decompositions.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/dense.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/graph.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/inverse.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/normalforms.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/reductions.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/sparsetools.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/utilities.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/dense.py +1093 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/eigen.py +1346 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/expressions/permutation.py +303 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/expressions/tests/__pycache__/__init__.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matmul.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/graph.py +279 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/immutable.py +196 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/repmatrix.py +1025 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/solvers.py +942 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/sparsetools.py +300 -0
.gitattributes
CHANGED
|
@@ -264,3 +264,7 @@ tuning-competition-baseline/.venv/lib/python3.11/site-packages/torch/_inductor/_
|
|
| 264 |
.venv/lib/python3.11/site-packages/numpy.libs/libquadmath-96973f99.so.0.0.0 filter=lfs diff=lfs merge=lfs -text
|
| 265 |
.venv/lib/python3.11/site-packages/numpy.libs/libgfortran-040039e1.so.5.0.0 filter=lfs diff=lfs merge=lfs -text
|
| 266 |
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/stats/__pycache__/stochastic_process_types.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
.venv/lib/python3.11/site-packages/numpy.libs/libquadmath-96973f99.so.0.0.0 filter=lfs diff=lfs merge=lfs -text
|
| 265 |
.venv/lib/python3.11/site-packages/numpy.libs/libgfortran-040039e1.so.5.0.0 filter=lfs diff=lfs merge=lfs -text
|
| 266 |
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/stats/__pycache__/stochastic_process_types.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 267 |
+
tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/curand/lib/libcurand.so.10 filter=lfs diff=lfs merge=lfs -text
|
| 268 |
+
tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/cudnn/lib/libcudnn_ops_infer.so.8 filter=lfs diff=lfs merge=lfs -text
|
| 269 |
+
tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/cublas/lib/libcublas.so.11 filter=lfs diff=lfs merge=lfs -text
|
| 270 |
+
tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/cudnn/lib/libcudnn_cnn_train.so.8 filter=lfs diff=lfs merge=lfs -text
|
llm_tutorial/merged_models/en-ja-base_0.33+instruct/special_tokens_map.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": {
|
| 3 |
+
"content": "<s>",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"cls_token": {
|
| 10 |
+
"content": "<CLS|LLM-jp>",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"eos_token": {
|
| 17 |
+
"content": "</s>",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"mask_token": {
|
| 24 |
+
"content": "<MASK|LLM-jp>",
|
| 25 |
+
"lstrip": false,
|
| 26 |
+
"normalized": false,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
},
|
| 30 |
+
"pad_token": {
|
| 31 |
+
"content": "<PAD|LLM-jp>",
|
| 32 |
+
"lstrip": false,
|
| 33 |
+
"normalized": false,
|
| 34 |
+
"rstrip": false,
|
| 35 |
+
"single_word": false
|
| 36 |
+
},
|
| 37 |
+
"sep_token": {
|
| 38 |
+
"content": "<SEP|LLM-jp>",
|
| 39 |
+
"lstrip": false,
|
| 40 |
+
"normalized": false,
|
| 41 |
+
"rstrip": false,
|
| 42 |
+
"single_word": false
|
| 43 |
+
},
|
| 44 |
+
"unk_token": {
|
| 45 |
+
"content": "<unk>",
|
| 46 |
+
"lstrip": false,
|
| 47 |
+
"normalized": false,
|
| 48 |
+
"rstrip": false,
|
| 49 |
+
"single_word": false
|
| 50 |
+
}
|
| 51 |
+
}
|
llm_tutorial/merged_models/en-ja-base_0.33/model.safetensors.index.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"metadata": {"mergekit_version": "0.0.5.1", "total_size": 7565826048}, "weight_map": {"lm_head.weight": "model-00001-of-00002.safetensors", "model.embed_tokens.weight": "model-00001-of-00002.safetensors", "model.layers.0.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.20.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.21.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.22.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.23.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.23.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.3.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.4.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.5.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.6.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.7.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.8.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.9.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.norm.weight": "model-00002-of-00002.safetensors"}}
|
llm_tutorial/merged_models/en-ja-base_dare-linear/model.safetensors.index.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"metadata": {"mergekit_version": "0.0.5.1", "total_size": 7565826048}, "weight_map": {"lm_head.weight": "model-00001-of-00002.safetensors", "model.embed_tokens.weight": "model-00001-of-00002.safetensors", "model.layers.0.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.20.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.21.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.22.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.23.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.23.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.3.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.4.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.5.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.6.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.7.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.8.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.9.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.norm.weight": "model-00002-of-00002.safetensors"}}
|
llm_tutorial/merged_models4/ja-en_en-ja_ties02/tokenizer_config.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_bos_token": true,
|
| 3 |
+
"add_eos_token": false,
|
| 4 |
+
"unk_token": "<unk>",
|
| 5 |
+
"bos_token": "<s>",
|
| 6 |
+
"eos_token": "</s>",
|
| 7 |
+
"pad_token": "<PAD|LLM-jp>",
|
| 8 |
+
"cls_token": "<CLS|LLM-jp>",
|
| 9 |
+
"sep_token": "<SEP|LLM-jp>",
|
| 10 |
+
"eod_token": "</s>",
|
| 11 |
+
"mask_token": "<MASK|LLM-jp>",
|
| 12 |
+
"extra_ids": 0,
|
| 13 |
+
"sp_model_kwargs": {},
|
| 14 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 15 |
+
"clean_up_tokenization_spaces": false,
|
| 16 |
+
"special_tokens_map_file": null,
|
| 17 |
+
"tokenizer_class": "PreTrainedTokenizerFast"
|
| 18 |
+
}
|
llm_tutorial/merged_models4/ja-en_en-ja_ties03-instruct/model.safetensors.index.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"metadata": {"mergekit_version": "0.0.5.1", "total_size": 7565826048}, "weight_map": {"lm_head.weight": "model-00001-of-00002.safetensors", "model.embed_tokens.weight": "model-00001-of-00002.safetensors", "model.layers.0.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.10.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.11.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.12.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.13.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.14.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.18.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.19.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.20.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.20.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.21.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.21.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.22.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.k_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.o_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.q_proj.weight": "model-00001-of-00002.safetensors", "model.layers.22.self_attn.v_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.input_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.23.mlp.down_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.mlp.gate_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.mlp.up_proj.weight": "model-00001-of-00002.safetensors", "model.layers.23.post_attention_layernorm.weight": "model-00001-of-00002.safetensors", "model.layers.23.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.23.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.24.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.25.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.26.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.27.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.3.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.3.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.4.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.4.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.5.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.5.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.6.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.6.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.7.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.7.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.8.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.8.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.input_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.9.mlp.down_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.mlp.gate_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.mlp.up_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.post_attention_layernorm.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.k_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.o_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.q_proj.weight": "model-00002-of-00002.safetensors", "model.layers.9.self_attn.v_proj.weight": "model-00002-of-00002.safetensors", "model.norm.weight": "model-00002-of-00002.safetensors"}}
|
llm_tutorial/merged_models4/ja-en_en-ja_ties03/special_tokens_map.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": "<s>",
|
| 3 |
+
"cls_token": "<CLS|LLM-jp>",
|
| 4 |
+
"eod_token": "</s>",
|
| 5 |
+
"eos_token": "</s>",
|
| 6 |
+
"mask_token": "<MASK|LLM-jp>",
|
| 7 |
+
"pad_token": "<PAD|LLM-jp>",
|
| 8 |
+
"sep_token": "<SEP|LLM-jp>",
|
| 9 |
+
"unk_token": "<unk>"
|
| 10 |
+
}
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/markupsafe/__pycache__/_native.cpython-311.pyc
ADDED
|
Binary file (2.76 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/markupsafe/_native.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import typing as t
|
| 2 |
+
|
| 3 |
+
from . import Markup
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def escape(s: t.Any) -> Markup:
|
| 7 |
+
"""Replace the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in
|
| 8 |
+
the string with HTML-safe sequences. Use this if you need to display
|
| 9 |
+
text that might contain such characters in HTML.
|
| 10 |
+
|
| 11 |
+
If the object has an ``__html__`` method, it is called and the
|
| 12 |
+
return value is assumed to already be safe for HTML.
|
| 13 |
+
|
| 14 |
+
:param s: An object to be converted to a string and escaped.
|
| 15 |
+
:return: A :class:`Markup` string with the escaped text.
|
| 16 |
+
"""
|
| 17 |
+
if hasattr(s, "__html__"):
|
| 18 |
+
return Markup(s.__html__())
|
| 19 |
+
|
| 20 |
+
return Markup(
|
| 21 |
+
str(s)
|
| 22 |
+
.replace("&", "&")
|
| 23 |
+
.replace(">", ">")
|
| 24 |
+
.replace("<", "<")
|
| 25 |
+
.replace("'", "'")
|
| 26 |
+
.replace('"', """)
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def escape_silent(s: t.Optional[t.Any]) -> Markup:
|
| 31 |
+
"""Like :func:`escape` but treats ``None`` as the empty string.
|
| 32 |
+
Useful with optional values, as otherwise you get the string
|
| 33 |
+
``'None'`` when the value is ``None``.
|
| 34 |
+
|
| 35 |
+
>>> escape(None)
|
| 36 |
+
Markup('None')
|
| 37 |
+
>>> escape_silent(None)
|
| 38 |
+
Markup('')
|
| 39 |
+
"""
|
| 40 |
+
if s is None:
|
| 41 |
+
return Markup()
|
| 42 |
+
|
| 43 |
+
return escape(s)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def soft_str(s: t.Any) -> str:
|
| 47 |
+
"""Convert an object to a string if it isn't already. This preserves
|
| 48 |
+
a :class:`Markup` string rather than converting it back to a basic
|
| 49 |
+
string, so it will still be marked as safe and won't be escaped
|
| 50 |
+
again.
|
| 51 |
+
|
| 52 |
+
>>> value = escape("<User 1>")
|
| 53 |
+
>>> value
|
| 54 |
+
Markup('<User 1>')
|
| 55 |
+
>>> escape(str(value))
|
| 56 |
+
Markup('&lt;User 1&gt;')
|
| 57 |
+
>>> escape(soft_str(value))
|
| 58 |
+
Markup('<User 1>')
|
| 59 |
+
"""
|
| 60 |
+
if not isinstance(s, str):
|
| 61 |
+
return str(s)
|
| 62 |
+
|
| 63 |
+
return s
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/markupsafe/_speedups.pyi
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
+
from . import Markup
|
| 5 |
+
|
| 6 |
+
def escape(s: Any) -> Markup: ...
|
| 7 |
+
def escape_silent(s: Optional[Any]) -> Markup: ...
|
| 8 |
+
def soft_str(s: Any) -> str: ...
|
| 9 |
+
def soft_unicode(s: Any) -> str: ...
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/mpmath-1.3.0.dist-info/LICENSE
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Copyright (c) 2005-2021 Fredrik Johansson and mpmath contributors
|
| 2 |
+
|
| 3 |
+
All rights reserved.
|
| 4 |
+
|
| 5 |
+
Redistribution and use in source and binary forms, with or without
|
| 6 |
+
modification, are permitted provided that the following conditions are met:
|
| 7 |
+
|
| 8 |
+
a. Redistributions of source code must retain the above copyright notice,
|
| 9 |
+
this list of conditions and the following disclaimer.
|
| 10 |
+
b. Redistributions in binary form must reproduce the above copyright
|
| 11 |
+
notice, this list of conditions and the following disclaimer in the
|
| 12 |
+
documentation and/or other materials provided with the distribution.
|
| 13 |
+
c. Neither the name of the copyright holder nor the names of its
|
| 14 |
+
contributors may be used to endorse or promote products derived
|
| 15 |
+
from this software without specific prior written permission.
|
| 16 |
+
|
| 17 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 18 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 19 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 20 |
+
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
|
| 21 |
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 22 |
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 23 |
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 24 |
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
| 25 |
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
| 26 |
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
| 27 |
+
DAMAGE.
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/mpmath-1.3.0.dist-info/METADATA
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.1
|
| 2 |
+
Name: mpmath
|
| 3 |
+
Version: 1.3.0
|
| 4 |
+
Summary: Python library for arbitrary-precision floating-point arithmetic
|
| 5 |
+
Home-page: http://mpmath.org/
|
| 6 |
+
Author: Fredrik Johansson
|
| 7 |
+
Author-email: [email protected]
|
| 8 |
+
License: BSD
|
| 9 |
+
Project-URL: Source, https://github.com/fredrik-johansson/mpmath
|
| 10 |
+
Project-URL: Tracker, https://github.com/fredrik-johansson/mpmath/issues
|
| 11 |
+
Project-URL: Documentation, http://mpmath.org/doc/current/
|
| 12 |
+
Classifier: License :: OSI Approved :: BSD License
|
| 13 |
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
| 14 |
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
| 15 |
+
Classifier: Programming Language :: Python
|
| 16 |
+
Classifier: Programming Language :: Python :: 2
|
| 17 |
+
Classifier: Programming Language :: Python :: 2.7
|
| 18 |
+
Classifier: Programming Language :: Python :: 3
|
| 19 |
+
Classifier: Programming Language :: Python :: 3.5
|
| 20 |
+
Classifier: Programming Language :: Python :: 3.6
|
| 21 |
+
Classifier: Programming Language :: Python :: 3.7
|
| 22 |
+
Classifier: Programming Language :: Python :: 3.8
|
| 23 |
+
Classifier: Programming Language :: Python :: 3.9
|
| 24 |
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
| 25 |
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
| 26 |
+
License-File: LICENSE
|
| 27 |
+
Provides-Extra: develop
|
| 28 |
+
Requires-Dist: pytest (>=4.6) ; extra == 'develop'
|
| 29 |
+
Requires-Dist: pycodestyle ; extra == 'develop'
|
| 30 |
+
Requires-Dist: pytest-cov ; extra == 'develop'
|
| 31 |
+
Requires-Dist: codecov ; extra == 'develop'
|
| 32 |
+
Requires-Dist: wheel ; extra == 'develop'
|
| 33 |
+
Provides-Extra: docs
|
| 34 |
+
Requires-Dist: sphinx ; extra == 'docs'
|
| 35 |
+
Provides-Extra: gmpy
|
| 36 |
+
Requires-Dist: gmpy2 (>=2.1.0a4) ; (platform_python_implementation != "PyPy") and extra == 'gmpy'
|
| 37 |
+
Provides-Extra: tests
|
| 38 |
+
Requires-Dist: pytest (>=4.6) ; extra == 'tests'
|
| 39 |
+
|
| 40 |
+
mpmath
|
| 41 |
+
======
|
| 42 |
+
|
| 43 |
+
|pypi version| |Build status| |Code coverage status| |Zenodo Badge|
|
| 44 |
+
|
| 45 |
+
.. |pypi version| image:: https://img.shields.io/pypi/v/mpmath.svg
|
| 46 |
+
:target: https://pypi.python.org/pypi/mpmath
|
| 47 |
+
.. |Build status| image:: https://github.com/fredrik-johansson/mpmath/workflows/test/badge.svg
|
| 48 |
+
:target: https://github.com/fredrik-johansson/mpmath/actions?workflow=test
|
| 49 |
+
.. |Code coverage status| image:: https://codecov.io/gh/fredrik-johansson/mpmath/branch/master/graph/badge.svg
|
| 50 |
+
:target: https://codecov.io/gh/fredrik-johansson/mpmath
|
| 51 |
+
.. |Zenodo Badge| image:: https://zenodo.org/badge/2934512.svg
|
| 52 |
+
:target: https://zenodo.org/badge/latestdoi/2934512
|
| 53 |
+
|
| 54 |
+
A Python library for arbitrary-precision floating-point arithmetic.
|
| 55 |
+
|
| 56 |
+
Website: http://mpmath.org/
|
| 57 |
+
Main author: Fredrik Johansson <[email protected]>
|
| 58 |
+
|
| 59 |
+
Mpmath is free software released under the New BSD License (see the
|
| 60 |
+
LICENSE file for details)
|
| 61 |
+
|
| 62 |
+
0. History and credits
|
| 63 |
+
----------------------
|
| 64 |
+
|
| 65 |
+
The following people (among others) have contributed major patches
|
| 66 |
+
or new features to mpmath:
|
| 67 |
+
|
| 68 |
+
* Pearu Peterson <[email protected]>
|
| 69 |
+
* Mario Pernici <[email protected]>
|
| 70 |
+
* Ondrej Certik <[email protected]>
|
| 71 |
+
* Vinzent Steinberg <[email protected]>
|
| 72 |
+
* Nimish Telang <[email protected]>
|
| 73 |
+
* Mike Taschuk <[email protected]>
|
| 74 |
+
* Case Van Horsen <[email protected]>
|
| 75 |
+
* Jorn Baayen <[email protected]>
|
| 76 |
+
* Chris Smith <[email protected]>
|
| 77 |
+
* Juan Arias de Reyna <[email protected]>
|
| 78 |
+
* Ioannis Tziakos <[email protected]>
|
| 79 |
+
* Aaron Meurer <[email protected]>
|
| 80 |
+
* Stefan Krastanov <[email protected]>
|
| 81 |
+
* Ken Allen <[email protected]>
|
| 82 |
+
* Timo Hartmann <[email protected]>
|
| 83 |
+
* Sergey B Kirpichev <[email protected]>
|
| 84 |
+
* Kris Kuhlman <[email protected]>
|
| 85 |
+
* Paul Masson <[email protected]>
|
| 86 |
+
* Michael Kagalenko <[email protected]>
|
| 87 |
+
* Jonathan Warner <[email protected]>
|
| 88 |
+
* Max Gaukler <[email protected]>
|
| 89 |
+
* Guillermo Navas-Palencia <[email protected]>
|
| 90 |
+
* Nike Dattani <[email protected]>
|
| 91 |
+
|
| 92 |
+
Numerous other people have contributed by reporting bugs,
|
| 93 |
+
requesting new features, or suggesting improvements to the
|
| 94 |
+
documentation.
|
| 95 |
+
|
| 96 |
+
For a detailed changelog, including individual contributions,
|
| 97 |
+
see the CHANGES file.
|
| 98 |
+
|
| 99 |
+
Fredrik's work on mpmath during summer 2008 was sponsored by Google
|
| 100 |
+
as part of the Google Summer of Code program.
|
| 101 |
+
|
| 102 |
+
Fredrik's work on mpmath during summer 2009 was sponsored by the
|
| 103 |
+
American Institute of Mathematics under the support of the National Science
|
| 104 |
+
Foundation Grant No. 0757627 (FRG: L-functions and Modular Forms).
|
| 105 |
+
|
| 106 |
+
Any opinions, findings, and conclusions or recommendations expressed in this
|
| 107 |
+
material are those of the author(s) and do not necessarily reflect the
|
| 108 |
+
views of the sponsors.
|
| 109 |
+
|
| 110 |
+
Credit also goes to:
|
| 111 |
+
|
| 112 |
+
* The authors of the GMP library and the Python wrapper
|
| 113 |
+
gmpy, enabling mpmath to become much faster at
|
| 114 |
+
high precision
|
| 115 |
+
* The authors of MPFR, pari/gp, MPFUN, and other arbitrary-
|
| 116 |
+
precision libraries, whose documentation has been helpful
|
| 117 |
+
for implementing many of the algorithms in mpmath
|
| 118 |
+
* Wikipedia contributors; Abramowitz & Stegun; Gradshteyn & Ryzhik;
|
| 119 |
+
Wolfram Research for MathWorld and the Wolfram Functions site.
|
| 120 |
+
These are the main references used for special functions
|
| 121 |
+
implementations.
|
| 122 |
+
* George Brandl for developing the Sphinx documentation tool
|
| 123 |
+
used to build mpmath's documentation
|
| 124 |
+
|
| 125 |
+
Release history:
|
| 126 |
+
|
| 127 |
+
* Version 1.3.0 released on March 7, 2023
|
| 128 |
+
* Version 1.2.0 released on February 1, 2021
|
| 129 |
+
* Version 1.1.0 released on December 11, 2018
|
| 130 |
+
* Version 1.0.0 released on September 27, 2017
|
| 131 |
+
* Version 0.19 released on June 10, 2014
|
| 132 |
+
* Version 0.18 released on December 31, 2013
|
| 133 |
+
* Version 0.17 released on February 1, 2011
|
| 134 |
+
* Version 0.16 released on September 24, 2010
|
| 135 |
+
* Version 0.15 released on June 6, 2010
|
| 136 |
+
* Version 0.14 released on February 5, 2010
|
| 137 |
+
* Version 0.13 released on August 13, 2009
|
| 138 |
+
* Version 0.12 released on June 9, 2009
|
| 139 |
+
* Version 0.11 released on January 26, 2009
|
| 140 |
+
* Version 0.10 released on October 15, 2008
|
| 141 |
+
* Version 0.9 released on August 23, 2008
|
| 142 |
+
* Version 0.8 released on April 20, 2008
|
| 143 |
+
* Version 0.7 released on March 12, 2008
|
| 144 |
+
* Version 0.6 released on January 13, 2008
|
| 145 |
+
* Version 0.5 released on November 24, 2007
|
| 146 |
+
* Version 0.4 released on November 3, 2007
|
| 147 |
+
* Version 0.3 released on October 5, 2007
|
| 148 |
+
* Version 0.2 released on October 2, 2007
|
| 149 |
+
* Version 0.1 released on September 27, 2007
|
| 150 |
+
|
| 151 |
+
1. Download & installation
|
| 152 |
+
--------------------------
|
| 153 |
+
|
| 154 |
+
Mpmath requires Python 2.7 or 3.5 (or later versions). It has been tested
|
| 155 |
+
with CPython 2.7, 3.5 through 3.7 and for PyPy.
|
| 156 |
+
|
| 157 |
+
The latest release of mpmath can be downloaded from the mpmath
|
| 158 |
+
website and from https://github.com/fredrik-johansson/mpmath/releases
|
| 159 |
+
|
| 160 |
+
It should also be available in the Python Package Index at
|
| 161 |
+
https://pypi.python.org/pypi/mpmath
|
| 162 |
+
|
| 163 |
+
To install latest release of Mpmath with pip, simply run
|
| 164 |
+
|
| 165 |
+
``pip install mpmath``
|
| 166 |
+
|
| 167 |
+
Or unpack the mpmath archive and run
|
| 168 |
+
|
| 169 |
+
``python setup.py install``
|
| 170 |
+
|
| 171 |
+
Mpmath can also be installed using
|
| 172 |
+
|
| 173 |
+
``python -m easy_install mpmath``
|
| 174 |
+
|
| 175 |
+
The latest development code is available from
|
| 176 |
+
https://github.com/fredrik-johansson/mpmath
|
| 177 |
+
|
| 178 |
+
See the main documentation for more detailed instructions.
|
| 179 |
+
|
| 180 |
+
2. Running tests
|
| 181 |
+
----------------
|
| 182 |
+
|
| 183 |
+
The unit tests in mpmath/tests/ can be run via the script
|
| 184 |
+
runtests.py, but it is recommended to run them with py.test
|
| 185 |
+
(https://pytest.org/), especially
|
| 186 |
+
to generate more useful reports in case there are failures.
|
| 187 |
+
|
| 188 |
+
You may also want to check out the demo scripts in the demo
|
| 189 |
+
directory.
|
| 190 |
+
|
| 191 |
+
The master branch is automatically tested by Travis CI.
|
| 192 |
+
|
| 193 |
+
3. Documentation
|
| 194 |
+
----------------
|
| 195 |
+
|
| 196 |
+
Documentation in reStructuredText format is available in the
|
| 197 |
+
doc directory included with the source package. These files
|
| 198 |
+
are human-readable, but can be compiled to prettier HTML using
|
| 199 |
+
the build.py script (requires Sphinx, http://sphinx.pocoo.org/).
|
| 200 |
+
|
| 201 |
+
See setup.txt in the documentation for more information.
|
| 202 |
+
|
| 203 |
+
The most recent documentation is also available in HTML format:
|
| 204 |
+
|
| 205 |
+
http://mpmath.org/doc/current/
|
| 206 |
+
|
| 207 |
+
4. Known problems
|
| 208 |
+
-----------------
|
| 209 |
+
|
| 210 |
+
Mpmath is a work in progress. Major issues include:
|
| 211 |
+
|
| 212 |
+
* Some functions may return incorrect values when given extremely
|
| 213 |
+
large arguments or arguments very close to singularities.
|
| 214 |
+
|
| 215 |
+
* Directed rounding works for arithmetic operations. It is implemented
|
| 216 |
+
heuristically for other operations, and their results may be off by one
|
| 217 |
+
or two units in the last place (even if otherwise accurate).
|
| 218 |
+
|
| 219 |
+
* Some IEEE 754 features are not available. Inifinities and NaN are
|
| 220 |
+
partially supported; denormal rounding is currently not available
|
| 221 |
+
at all.
|
| 222 |
+
|
| 223 |
+
* The interface for switching precision and rounding is not finalized.
|
| 224 |
+
The current method is not threadsafe.
|
| 225 |
+
|
| 226 |
+
5. Help and bug reports
|
| 227 |
+
-----------------------
|
| 228 |
+
|
| 229 |
+
General questions and comments can be sent to the mpmath mailinglist,
|
| 230 | |
| 231 |
+
|
| 232 |
+
You can also report bugs and send patches to the mpmath issue tracker,
|
| 233 |
+
https://github.com/fredrik-johansson/mpmath/issues
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/mpmath-1.3.0.dist-info/RECORD
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
| 6 |
+
mpmath-1.3.0.dist-info/top_level.txt,sha256=BUVWrh8EVlkOhM1n3X9S8msTaVcC-3s6Sjt60avHYus,7
|
| 7 |
+
mpmath/__init__.py,sha256=skFYTSwfwDBLChAV6pI3SdewgAQR3UBtyrfIK_Jdn-g,8765
|
| 8 |
+
mpmath/__pycache__/__init__.cpython-311.pyc,,
|
| 9 |
+
mpmath/__pycache__/ctx_base.cpython-311.pyc,,
|
| 10 |
+
mpmath/__pycache__/ctx_fp.cpython-311.pyc,,
|
| 11 |
+
mpmath/__pycache__/ctx_iv.cpython-311.pyc,,
|
| 12 |
+
mpmath/__pycache__/ctx_mp.cpython-311.pyc,,
|
| 13 |
+
mpmath/__pycache__/ctx_mp_python.cpython-311.pyc,,
|
| 14 |
+
mpmath/__pycache__/function_docs.cpython-311.pyc,,
|
| 15 |
+
mpmath/__pycache__/identification.cpython-311.pyc,,
|
| 16 |
+
mpmath/__pycache__/math2.cpython-311.pyc,,
|
| 17 |
+
mpmath/__pycache__/rational.cpython-311.pyc,,
|
| 18 |
+
mpmath/__pycache__/usertools.cpython-311.pyc,,
|
| 19 |
+
mpmath/__pycache__/visualization.cpython-311.pyc,,
|
| 20 |
+
mpmath/calculus/__init__.py,sha256=UAgCIJ1YmaeyTqpNzjBlCZGeIzLtUZMEEpl99VWNjus,162
|
| 21 |
+
mpmath/calculus/__pycache__/__init__.cpython-311.pyc,,
|
| 22 |
+
mpmath/calculus/__pycache__/approximation.cpython-311.pyc,,
|
| 23 |
+
mpmath/calculus/__pycache__/calculus.cpython-311.pyc,,
|
| 24 |
+
mpmath/calculus/__pycache__/differentiation.cpython-311.pyc,,
|
| 25 |
+
mpmath/calculus/__pycache__/extrapolation.cpython-311.pyc,,
|
| 26 |
+
mpmath/calculus/__pycache__/inverselaplace.cpython-311.pyc,,
|
| 27 |
+
mpmath/calculus/__pycache__/odes.cpython-311.pyc,,
|
| 28 |
+
mpmath/calculus/__pycache__/optimization.cpython-311.pyc,,
|
| 29 |
+
mpmath/calculus/__pycache__/polynomials.cpython-311.pyc,,
|
| 30 |
+
mpmath/calculus/__pycache__/quadrature.cpython-311.pyc,,
|
| 31 |
+
mpmath/calculus/approximation.py,sha256=vyzu3YI6r63Oq1KFHrQz02mGXAcH23emqNYhJuUaFZ4,8817
|
| 32 |
+
mpmath/calculus/calculus.py,sha256=A0gSp0hxSyEDfugJViY3CeWalF-vK701YftzrjSQzQ4,112
|
| 33 |
+
mpmath/calculus/differentiation.py,sha256=2L6CBj8xtX9iip98NPbKsLtwtRjxi571wYmTMHFeL90,20226
|
| 34 |
+
mpmath/calculus/extrapolation.py,sha256=xM0rvk2DFEF4iR1Jhl-Y3aS93iW9VVJX7y9IGpmzC-A,73306
|
| 35 |
+
mpmath/calculus/inverselaplace.py,sha256=5-pn8N_t0PtgBTXixsXZ4xxrihK2J5gYsVfTKfDx4gA,36056
|
| 36 |
+
mpmath/calculus/odes.py,sha256=gaHiw7IJjsONNTAa6izFPZpmcg9uyTp8MULnGdzTIGo,9908
|
| 37 |
+
mpmath/calculus/optimization.py,sha256=bKnShXElBOmVOIOlFeksDsYCp9fYSmYwKmXDt0z26MM,32856
|
| 38 |
+
mpmath/calculus/polynomials.py,sha256=D16BhU_SHbVi06IxNwABHR-H77IylndNsN3muPTuFYs,7877
|
| 39 |
+
mpmath/calculus/quadrature.py,sha256=n-avtS8E43foV-5tr5lofgOBaiMUYE8AJjQcWI9QcKk,42432
|
| 40 |
+
mpmath/ctx_base.py,sha256=rfjmfMyA55x8R_cWFINUwWVTElfZmyx5erKDdauSEVw,15985
|
| 41 |
+
mpmath/ctx_fp.py,sha256=ctUjx_NoU0iFWk05cXDYCL2ZtLZOlWs1n6Zao3pbG2g,6572
|
| 42 |
+
mpmath/ctx_iv.py,sha256=tqdMr-GDfkZk1EhoGeCAajy7pQv-RWtrVqhYjfI8r4g,17211
|
| 43 |
+
mpmath/ctx_mp.py,sha256=d3r4t7xHNqSFtmqsA9Btq1Npy3WTM-pcM2_jeCyECxY,49452
|
| 44 |
+
mpmath/ctx_mp_python.py,sha256=3olYWo4lk1SnQ0A_IaZ181qqG8u5pxGat_v-L4Qtn3Y,37815
|
| 45 |
+
mpmath/function_docs.py,sha256=g4PP8n6ILXmHcLyA50sxK6Tmp_Z4_pRN-wDErU8D1i4,283512
|
| 46 |
+
mpmath/functions/__init__.py,sha256=YXVdhqv-6LKm6cr5xxtTNTtuD9zDPKGQl8GmS0xz2xo,330
|
| 47 |
+
mpmath/functions/__pycache__/__init__.cpython-311.pyc,,
|
| 48 |
+
mpmath/functions/__pycache__/bessel.cpython-311.pyc,,
|
| 49 |
+
mpmath/functions/__pycache__/elliptic.cpython-311.pyc,,
|
| 50 |
+
mpmath/functions/__pycache__/expintegrals.cpython-311.pyc,,
|
| 51 |
+
mpmath/functions/__pycache__/factorials.cpython-311.pyc,,
|
| 52 |
+
mpmath/functions/__pycache__/functions.cpython-311.pyc,,
|
| 53 |
+
mpmath/functions/__pycache__/hypergeometric.cpython-311.pyc,,
|
| 54 |
+
mpmath/functions/__pycache__/orthogonal.cpython-311.pyc,,
|
| 55 |
+
mpmath/functions/__pycache__/qfunctions.cpython-311.pyc,,
|
| 56 |
+
mpmath/functions/__pycache__/rszeta.cpython-311.pyc,,
|
| 57 |
+
mpmath/functions/__pycache__/signals.cpython-311.pyc,,
|
| 58 |
+
mpmath/functions/__pycache__/theta.cpython-311.pyc,,
|
| 59 |
+
mpmath/functions/__pycache__/zeta.cpython-311.pyc,,
|
| 60 |
+
mpmath/functions/__pycache__/zetazeros.cpython-311.pyc,,
|
| 61 |
+
mpmath/functions/bessel.py,sha256=dUPLu8frlK-vmf3-irX_7uvwyw4xccv6EIizmIZ88kM,37938
|
| 62 |
+
mpmath/functions/elliptic.py,sha256=qz0yVMb4lWEeOTDL_DWz5u5awmGIPKAsuZFJXgwHJNU,42237
|
| 63 |
+
mpmath/functions/expintegrals.py,sha256=75X_MRdYc1F_X73bgNiOJqwRlS2hqAzcFLl3RM2tCDc,11644
|
| 64 |
+
mpmath/functions/factorials.py,sha256=8_6kCR7e4k1GwxiAOJu0NRadeF4jA28qx4hidhu4ILk,5273
|
| 65 |
+
mpmath/functions/functions.py,sha256=ub2JExvqzCWLkm5yAm72Fr6fdWmZZUknq9_3w9MEigI,18100
|
| 66 |
+
mpmath/functions/hypergeometric.py,sha256=Z0OMAMC4ylK42n_SnamyFVnUx6zHLyCLCoJDSZ1JrHY,51570
|
| 67 |
+
mpmath/functions/orthogonal.py,sha256=FabkxKfBoSseA5flWu1a3re-2BYaew9augqIsT8LaLw,16097
|
| 68 |
+
mpmath/functions/qfunctions.py,sha256=a3EHGKQt_jMd4x9I772Jz-TGFnGY-arWqPvZGz9QSe0,7633
|
| 69 |
+
mpmath/functions/rszeta.py,sha256=yuUVp4ilIyDmXyE3WTBxDDjwfEJNypJnbPS-xPH5How,46184
|
| 70 |
+
mpmath/functions/signals.py,sha256=ELotwQaW1CDpv-eeJzOZ5c23NhfaZcj9_Gkb3psvS0Q,703
|
| 71 |
+
mpmath/functions/theta.py,sha256=KggOocczoMG6_HMoal4oEP7iZ4SKOou9JFE-WzY2r3M,37320
|
| 72 |
+
mpmath/functions/zeta.py,sha256=ue7JY7GXA0oX8q08sQJl2CSRrZ7kOt8HsftpVjnTwrE,36410
|
| 73 |
+
mpmath/functions/zetazeros.py,sha256=uq6TVyZBcY2MLX7VSdVfn0TOkowBLM9fXtnySEwaNzw,30858
|
| 74 |
+
mpmath/identification.py,sha256=7aMdngRAaeL_MafDUNbmEIlGQSklHDZ8pmPFt-OLgkw,29253
|
| 75 |
+
mpmath/libmp/__init__.py,sha256=UCDjLZw4brbklaCmSixCcPdLdHkz8sF_-6F_wr0duAg,3790
|
| 76 |
+
mpmath/libmp/__pycache__/__init__.cpython-311.pyc,,
|
| 77 |
+
mpmath/libmp/__pycache__/backend.cpython-311.pyc,,
|
| 78 |
+
mpmath/libmp/__pycache__/gammazeta.cpython-311.pyc,,
|
| 79 |
+
mpmath/libmp/__pycache__/libelefun.cpython-311.pyc,,
|
| 80 |
+
mpmath/libmp/__pycache__/libhyper.cpython-311.pyc,,
|
| 81 |
+
mpmath/libmp/__pycache__/libintmath.cpython-311.pyc,,
|
| 82 |
+
mpmath/libmp/__pycache__/libmpc.cpython-311.pyc,,
|
| 83 |
+
mpmath/libmp/__pycache__/libmpf.cpython-311.pyc,,
|
| 84 |
+
mpmath/libmp/__pycache__/libmpi.cpython-311.pyc,,
|
| 85 |
+
mpmath/libmp/backend.py,sha256=26A8pUkaGov26vrrFNQVyWJ5LDtK8sl3UHrYLecaTjA,3360
|
| 86 |
+
mpmath/libmp/gammazeta.py,sha256=Xqdw6PMoswDaSca_sOs-IglRuk3fb8c9p43M_lbcrlc,71469
|
| 87 |
+
mpmath/libmp/libelefun.py,sha256=joBZP4FOdxPfieWso1LPtSr6dHydpG_LQiF_bYQYWMg,43861
|
| 88 |
+
mpmath/libmp/libhyper.py,sha256=J9fmdDF6u27EcssEWvBuVaAa3hFjPvPN1SgRgu1dEbc,36624
|
| 89 |
+
mpmath/libmp/libintmath.py,sha256=aIRT0rkUZ_sdGQf3TNCLd-pBMvtQWjssbvFLfK7U0jc,16688
|
| 90 |
+
mpmath/libmp/libmpc.py,sha256=KBndUjs5YVS32-Id3fflDfYgpdW1Prx6zfo8Ez5Qbrs,26875
|
| 91 |
+
mpmath/libmp/libmpf.py,sha256=vpP0kNVkScbCVoZogJ4Watl4I7Ce0d4dzHVjfVe57so,45021
|
| 92 |
+
mpmath/libmp/libmpi.py,sha256=u0I5Eiwkqa-4-dXETi5k7MuaxBeZbvCAPFtl93U9YF0,27622
|
| 93 |
+
mpmath/math2.py,sha256=O5Dglg81SsW0wfHDUJcXOD8-cCaLvbVIvyw0sVmRbpI,18561
|
| 94 |
+
mpmath/matrices/__init__.py,sha256=ETzGDciYbq9ftiKwaMbJ15EI-KNXHrzRb-ZHehhqFjs,94
|
| 95 |
+
mpmath/matrices/__pycache__/__init__.cpython-311.pyc,,
|
| 96 |
+
mpmath/matrices/__pycache__/calculus.cpython-311.pyc,,
|
| 97 |
+
mpmath/matrices/__pycache__/eigen.cpython-311.pyc,,
|
| 98 |
+
mpmath/matrices/__pycache__/eigen_symmetric.cpython-311.pyc,,
|
| 99 |
+
mpmath/matrices/__pycache__/linalg.cpython-311.pyc,,
|
| 100 |
+
mpmath/matrices/__pycache__/matrices.cpython-311.pyc,,
|
| 101 |
+
mpmath/matrices/calculus.py,sha256=PNRq-p2nxgT-fzC54K2depi8ddhdx6Q86G8qpUiHeUY,18609
|
| 102 |
+
mpmath/matrices/eigen.py,sha256=GbDXI3CixzEdXxr1G86uUWkAngAvd-05MmSQ-Tsu_5k,24394
|
| 103 |
+
mpmath/matrices/eigen_symmetric.py,sha256=FPKPeQr1cGYw6Y6ea32a1YdEWQDLP6JlQHEA2WfNLYg,58534
|
| 104 |
+
mpmath/matrices/linalg.py,sha256=04C3ijzMFom7ob5fXBCDfyPPdo3BIboIeE8x2A6vqF0,26958
|
| 105 |
+
mpmath/matrices/matrices.py,sha256=o78Eq62EHQnxcsR0LBoWDEGREOoN4L2iDM1q3dQrw0o,32331
|
| 106 |
+
mpmath/rational.py,sha256=64d56fvZXngYZT7nOAHeFRUX77eJ1A0R3rpfWBU-mSo,5976
|
| 107 |
+
mpmath/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 108 |
+
mpmath/tests/__pycache__/__init__.cpython-311.pyc,,
|
| 109 |
+
mpmath/tests/__pycache__/extratest_gamma.cpython-311.pyc,,
|
| 110 |
+
mpmath/tests/__pycache__/extratest_zeta.cpython-311.pyc,,
|
| 111 |
+
mpmath/tests/__pycache__/runtests.cpython-311.pyc,,
|
| 112 |
+
mpmath/tests/__pycache__/test_basic_ops.cpython-311.pyc,,
|
| 113 |
+
mpmath/tests/__pycache__/test_bitwise.cpython-311.pyc,,
|
| 114 |
+
mpmath/tests/__pycache__/test_calculus.cpython-311.pyc,,
|
| 115 |
+
mpmath/tests/__pycache__/test_compatibility.cpython-311.pyc,,
|
| 116 |
+
mpmath/tests/__pycache__/test_convert.cpython-311.pyc,,
|
| 117 |
+
mpmath/tests/__pycache__/test_diff.cpython-311.pyc,,
|
| 118 |
+
mpmath/tests/__pycache__/test_division.cpython-311.pyc,,
|
| 119 |
+
mpmath/tests/__pycache__/test_eigen.cpython-311.pyc,,
|
| 120 |
+
mpmath/tests/__pycache__/test_eigen_symmetric.cpython-311.pyc,,
|
| 121 |
+
mpmath/tests/__pycache__/test_elliptic.cpython-311.pyc,,
|
| 122 |
+
mpmath/tests/__pycache__/test_fp.cpython-311.pyc,,
|
| 123 |
+
mpmath/tests/__pycache__/test_functions.cpython-311.pyc,,
|
| 124 |
+
mpmath/tests/__pycache__/test_functions2.cpython-311.pyc,,
|
| 125 |
+
mpmath/tests/__pycache__/test_gammazeta.cpython-311.pyc,,
|
| 126 |
+
mpmath/tests/__pycache__/test_hp.cpython-311.pyc,,
|
| 127 |
+
mpmath/tests/__pycache__/test_identify.cpython-311.pyc,,
|
| 128 |
+
mpmath/tests/__pycache__/test_interval.cpython-311.pyc,,
|
| 129 |
+
mpmath/tests/__pycache__/test_levin.cpython-311.pyc,,
|
| 130 |
+
mpmath/tests/__pycache__/test_linalg.cpython-311.pyc,,
|
| 131 |
+
mpmath/tests/__pycache__/test_matrices.cpython-311.pyc,,
|
| 132 |
+
mpmath/tests/__pycache__/test_mpmath.cpython-311.pyc,,
|
| 133 |
+
mpmath/tests/__pycache__/test_ode.cpython-311.pyc,,
|
| 134 |
+
mpmath/tests/__pycache__/test_pickle.cpython-311.pyc,,
|
| 135 |
+
mpmath/tests/__pycache__/test_power.cpython-311.pyc,,
|
| 136 |
+
mpmath/tests/__pycache__/test_quad.cpython-311.pyc,,
|
| 137 |
+
mpmath/tests/__pycache__/test_rootfinding.cpython-311.pyc,,
|
| 138 |
+
mpmath/tests/__pycache__/test_special.cpython-311.pyc,,
|
| 139 |
+
mpmath/tests/__pycache__/test_str.cpython-311.pyc,,
|
| 140 |
+
mpmath/tests/__pycache__/test_summation.cpython-311.pyc,,
|
| 141 |
+
mpmath/tests/__pycache__/test_trig.cpython-311.pyc,,
|
| 142 |
+
mpmath/tests/__pycache__/test_visualization.cpython-311.pyc,,
|
| 143 |
+
mpmath/tests/__pycache__/torture.cpython-311.pyc,,
|
| 144 |
+
mpmath/tests/extratest_gamma.py,sha256=xidhXUelILcxtiPGoTBHjqUOKIJzEaZ_v3nntGQyWZQ,7228
|
| 145 |
+
mpmath/tests/extratest_zeta.py,sha256=sg10j9RhjBpV2EdUqyYhGV2ERWvM--EvwwGIz6HTmlw,1003
|
| 146 |
+
mpmath/tests/runtests.py,sha256=7NUV82F3K_5AhU8mCLUFf5OibtT7uloFCwPyM3l71wM,5189
|
| 147 |
+
mpmath/tests/test_basic_ops.py,sha256=dsB8DRG-GrPzBaZ-bIauYabaeqXbfqBo9SIP9BqcTSs,15348
|
| 148 |
+
mpmath/tests/test_bitwise.py,sha256=-nLYhgQbhDza3SQM63BhktYntACagqMYx9ib3dPnTKM,7686
|
| 149 |
+
mpmath/tests/test_calculus.py,sha256=4oxtNfMpO4RLLoOzrv7r9-h8BcqfBsJIE6UpsHe7c4w,9187
|
| 150 |
+
mpmath/tests/test_compatibility.py,sha256=_t3ASZ3jhfAMnN1voWX7PDNIDzn-3PokkJGIdT1x7y0,2306
|
| 151 |
+
mpmath/tests/test_convert.py,sha256=JPcDcTJIWh5prIxjx5DM1aNWgqlUoF2KpHvAgK3uHi4,8834
|
| 152 |
+
mpmath/tests/test_diff.py,sha256=qjiF8NxQ8vueuZ5ZHGPQ-kjcj_I7Jh_fEdFtaA8DzEI,2466
|
| 153 |
+
mpmath/tests/test_division.py,sha256=6lUeZfmaBWvvszdqlWLMHgXPjVsxvW1WZpd4-jFWCpU,5340
|
| 154 |
+
mpmath/tests/test_eigen.py,sha256=2mnqVATGbsJkvSVHPpitfAk881twFfb3LsO3XikV9Hs,3905
|
| 155 |
+
mpmath/tests/test_eigen_symmetric.py,sha256=v0VimCicIU2owASDMBaP-t-30uq-pXcsglt95KBtNO4,8778
|
| 156 |
+
mpmath/tests/test_elliptic.py,sha256=Kjiwq9Bb6N_OOzzWewGQ1M_PMa7vRs42V0t90gloZxo,26225
|
| 157 |
+
mpmath/tests/test_fp.py,sha256=AJo0FTyH4BuUnUsv176LD956om308KGYndy-b54KGxM,89997
|
| 158 |
+
mpmath/tests/test_functions.py,sha256=b47VywdomoOX6KmMmz9-iv2IqVIydwKSuUw2pWlFHrY,30955
|
| 159 |
+
mpmath/tests/test_functions2.py,sha256=vlw2RWhL1oTcifnOMDx1a_YzN96UgNNIE5STeKRv1HY,96990
|
| 160 |
+
mpmath/tests/test_gammazeta.py,sha256=AB34O0DV7AlEf9Z4brnCadeQU5-uAwhWRw5FZas65DA,27917
|
| 161 |
+
mpmath/tests/test_hp.py,sha256=6hcENu6Te2klPEiTSeLBIRPlH7PADlJwFKbx8xpnOhg,10461
|
| 162 |
+
mpmath/tests/test_identify.py,sha256=lGUIPfrB2paTg0cFUo64GmMzF77F9gs9FQjX7gxGHV8,692
|
| 163 |
+
mpmath/tests/test_interval.py,sha256=TjYd7a9ca6iRJiLjw06isLeZTuGoGAPmgleDZ0cYfJ0,17527
|
| 164 |
+
mpmath/tests/test_levin.py,sha256=P8M11yV1dj_gdSNv5xuwCzFiF86QyRDtPMjURy6wJ28,5090
|
| 165 |
+
mpmath/tests/test_linalg.py,sha256=miKEnwB8iwWV13hi1bF1cg3hgB4rTKOR0fvDVfWmXds,10440
|
| 166 |
+
mpmath/tests/test_matrices.py,sha256=qyA4Ml2CvNvW034lzB01G6wVgNr7UrgZqh2wkMXtpzM,7944
|
| 167 |
+
mpmath/tests/test_mpmath.py,sha256=LVyJUeofiaxW-zLKWVBCz59L9UQsjlW0Ts9_oBiEv_4,196
|
| 168 |
+
mpmath/tests/test_ode.py,sha256=zAxexBH4fnmFNO4bvEHbug1NJWC5zqfFaVDlYijowkY,1822
|
| 169 |
+
mpmath/tests/test_pickle.py,sha256=Y8CKmDLFsJHUqG8CDaBw5ilrPP4YT1xijVduLpQ7XFE,401
|
| 170 |
+
mpmath/tests/test_power.py,sha256=sz_K02SmNxpa6Kb1uJLN_N4tXTJGdQ___vPRshEN7Gk,5227
|
| 171 |
+
mpmath/tests/test_quad.py,sha256=49Ltft0vZ_kdKLL5s-Kj-BzAVoF5LPVEUeNUzdOkghI,3893
|
| 172 |
+
mpmath/tests/test_rootfinding.py,sha256=umQegEaKHmYOEl5jEyoD-VLKDtXsTJJkepKEr4c0dC0,3132
|
| 173 |
+
mpmath/tests/test_special.py,sha256=YbMIoMIkJEvvKYIzS0CXthJFG0--j6un7-tcE6b7FPM,2848
|
| 174 |
+
mpmath/tests/test_str.py,sha256=0WsGD9hMPRi8zcuYMA9Cu2mOvQiCFskPwMsMf8lBDK4,544
|
| 175 |
+
mpmath/tests/test_summation.py,sha256=fdNlsvRVOsbWxbhlyDLDaEO2S8kTJrRMKIvB5-aNci0,2035
|
| 176 |
+
mpmath/tests/test_trig.py,sha256=zPtkIEnZaThxcWur4k7BX8-2Jmj-AhO191Svv7ANYUU,4799
|
| 177 |
+
mpmath/tests/test_visualization.py,sha256=1PqtkoUx-WsKYgTRiu5o9pBc85kwhf1lzU2eobDQCJM,944
|
| 178 |
+
mpmath/tests/torture.py,sha256=LD95oES7JY2KroELK-m-jhvtbvZaKChnt0Cq7kFMNCw,7868
|
| 179 |
+
mpmath/usertools.py,sha256=a-TDw7XSRsPdBEffxOooDV4WDFfuXnO58P75dcAD87I,3029
|
| 180 |
+
mpmath/visualization.py,sha256=pnnbjcd9AhFVRBZavYX5gjx4ytK_kXoDDisYR6EpXhs,10627
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/mpmath-1.3.0.dist-info/WHEEL
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Wheel-Version: 1.0
|
| 2 |
+
Generator: bdist_wheel (0.38.4)
|
| 3 |
+
Root-Is-Purelib: true
|
| 4 |
+
Tag: py3-none-any
|
| 5 |
+
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/cublas/lib/libcublas.so.11
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3b81d170cd613cf9ee24d30b483f7b6d8170d6d32a0354fc207d09c943ae3f62
|
| 3 |
+
size 94729912
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/cudnn/lib/libcudnn_cnn_train.so.8
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:801895419e9de24de496099ffff3eef143e82c487cde68c4e9197e96fbea11db
|
| 3 |
+
size 102270568
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/cudnn/lib/libcudnn_ops_infer.so.8
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6e69b36512b6999005de457763369d44743a80f9eb1416e09c4e6029e4ab380d
|
| 3 |
+
size 97560024
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/curand/lib/libcurand.so.10
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:97813df916ebe8613efb8d57127b9119403332a64d929bfd172452c140e101de
|
| 3 |
+
size 101334448
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .products import product, Product
|
| 2 |
+
from .summations import summation, Sum
|
| 3 |
+
|
| 4 |
+
__all__ = [
|
| 5 |
+
'product', 'Product',
|
| 6 |
+
|
| 7 |
+
'summation', 'Sum',
|
| 8 |
+
]
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__pycache__/expr_with_intlimits.cpython-311.pyc
ADDED
|
Binary file (14.5 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__pycache__/expr_with_limits.cpython-311.pyc
ADDED
|
Binary file (31 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__pycache__/guess.cpython-311.pyc
ADDED
|
Binary file (27.8 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__pycache__/products.cpython-311.pyc
ADDED
|
Binary file (25 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/__pycache__/summations.cpython-311.pyc
ADDED
|
Binary file (70.9 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/gosper.py
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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Dummy, symbols
|
| 4 |
+
from sympy.polys import Poly, parallel_poly_from_expr, factor
|
| 5 |
+
from sympy.utilities.iterables import is_sequence
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def gosper_normal(f, g, n, polys=True):
|
| 9 |
+
r"""
|
| 10 |
+
Compute the Gosper's normal form of ``f`` and ``g``.
|
| 11 |
+
|
| 12 |
+
Explanation
|
| 13 |
+
===========
|
| 14 |
+
|
| 15 |
+
Given relatively prime univariate polynomials ``f`` and ``g``,
|
| 16 |
+
rewrite their quotient to a normal form defined as follows:
|
| 17 |
+
|
| 18 |
+
.. math::
|
| 19 |
+
\frac{f(n)}{g(n)} = Z \cdot \frac{A(n) C(n+1)}{B(n) C(n)}
|
| 20 |
+
|
| 21 |
+
where ``Z`` is an arbitrary constant and ``A``, ``B``, ``C`` are
|
| 22 |
+
monic polynomials in ``n`` with the following properties:
|
| 23 |
+
|
| 24 |
+
1. `\gcd(A(n), B(n+h)) = 1 \forall h \in \mathbb{N}`
|
| 25 |
+
2. `\gcd(B(n), C(n+1)) = 1`
|
| 26 |
+
3. `\gcd(A(n), C(n)) = 1`
|
| 27 |
+
|
| 28 |
+
This normal form, or rational factorization in other words, is a
|
| 29 |
+
crucial step in Gosper's algorithm and in solving of difference
|
| 30 |
+
equations. It can be also used to decide if two hypergeometric
|
| 31 |
+
terms are similar or not.
|
| 32 |
+
|
| 33 |
+
This procedure will return a tuple containing elements of this
|
| 34 |
+
factorization in the form ``(Z*A, B, C)``.
|
| 35 |
+
|
| 36 |
+
Examples
|
| 37 |
+
========
|
| 38 |
+
|
| 39 |
+
>>> from sympy.concrete.gosper import gosper_normal
|
| 40 |
+
>>> from sympy.abc import n
|
| 41 |
+
|
| 42 |
+
>>> gosper_normal(4*n+5, 2*(4*n+1)*(2*n+3), n, polys=False)
|
| 43 |
+
(1/4, n + 3/2, n + 1/4)
|
| 44 |
+
|
| 45 |
+
"""
|
| 46 |
+
(p, q), opt = parallel_poly_from_expr(
|
| 47 |
+
(f, g), n, field=True, extension=True)
|
| 48 |
+
|
| 49 |
+
a, A = p.LC(), p.monic()
|
| 50 |
+
b, B = q.LC(), q.monic()
|
| 51 |
+
|
| 52 |
+
C, Z = A.one, a/b
|
| 53 |
+
h = Dummy('h')
|
| 54 |
+
|
| 55 |
+
D = Poly(n + h, n, h, domain=opt.domain)
|
| 56 |
+
|
| 57 |
+
R = A.resultant(B.compose(D))
|
| 58 |
+
roots = set(R.ground_roots().keys())
|
| 59 |
+
|
| 60 |
+
for r in set(roots):
|
| 61 |
+
if not r.is_Integer or r < 0:
|
| 62 |
+
roots.remove(r)
|
| 63 |
+
|
| 64 |
+
for i in sorted(roots):
|
| 65 |
+
d = A.gcd(B.shift(+i))
|
| 66 |
+
|
| 67 |
+
A = A.quo(d)
|
| 68 |
+
B = B.quo(d.shift(-i))
|
| 69 |
+
|
| 70 |
+
for j in range(1, i + 1):
|
| 71 |
+
C *= d.shift(-j)
|
| 72 |
+
|
| 73 |
+
A = A.mul_ground(Z)
|
| 74 |
+
|
| 75 |
+
if not polys:
|
| 76 |
+
A = A.as_expr()
|
| 77 |
+
B = B.as_expr()
|
| 78 |
+
C = C.as_expr()
|
| 79 |
+
|
| 80 |
+
return A, B, C
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def gosper_term(f, n):
|
| 84 |
+
r"""
|
| 85 |
+
Compute Gosper's hypergeometric term for ``f``.
|
| 86 |
+
|
| 87 |
+
Explanation
|
| 88 |
+
===========
|
| 89 |
+
|
| 90 |
+
Suppose ``f`` is a hypergeometric term such that:
|
| 91 |
+
|
| 92 |
+
.. math::
|
| 93 |
+
s_n = \sum_{k=0}^{n-1} f_k
|
| 94 |
+
|
| 95 |
+
and `f_k` does not depend on `n`. Returns a hypergeometric
|
| 96 |
+
term `g_n` such that `g_{n+1} - g_n = f_n`.
|
| 97 |
+
|
| 98 |
+
Examples
|
| 99 |
+
========
|
| 100 |
+
|
| 101 |
+
>>> from sympy.concrete.gosper import gosper_term
|
| 102 |
+
>>> from sympy import factorial
|
| 103 |
+
>>> from sympy.abc import n
|
| 104 |
+
|
| 105 |
+
>>> gosper_term((4*n + 1)*factorial(n)/factorial(2*n + 1), n)
|
| 106 |
+
(-n - 1/2)/(n + 1/4)
|
| 107 |
+
|
| 108 |
+
"""
|
| 109 |
+
from sympy.simplify import hypersimp
|
| 110 |
+
r = hypersimp(f, n)
|
| 111 |
+
|
| 112 |
+
if r is None:
|
| 113 |
+
return None # 'f' is *not* a hypergeometric term
|
| 114 |
+
|
| 115 |
+
p, q = r.as_numer_denom()
|
| 116 |
+
|
| 117 |
+
A, B, C = gosper_normal(p, q, n)
|
| 118 |
+
B = B.shift(-1)
|
| 119 |
+
|
| 120 |
+
N = S(A.degree())
|
| 121 |
+
M = S(B.degree())
|
| 122 |
+
K = S(C.degree())
|
| 123 |
+
|
| 124 |
+
if (N != M) or (A.LC() != B.LC()):
|
| 125 |
+
D = {K - max(N, M)}
|
| 126 |
+
elif not N:
|
| 127 |
+
D = {K - N + 1, S.Zero}
|
| 128 |
+
else:
|
| 129 |
+
D = {K - N + 1, (B.nth(N - 1) - A.nth(N - 1))/A.LC()}
|
| 130 |
+
|
| 131 |
+
for d in set(D):
|
| 132 |
+
if not d.is_Integer or d < 0:
|
| 133 |
+
D.remove(d)
|
| 134 |
+
|
| 135 |
+
if not D:
|
| 136 |
+
return None # 'f(n)' is *not* Gosper-summable
|
| 137 |
+
|
| 138 |
+
d = max(D)
|
| 139 |
+
|
| 140 |
+
coeffs = symbols('c:%s' % (d + 1), cls=Dummy)
|
| 141 |
+
domain = A.get_domain().inject(*coeffs)
|
| 142 |
+
|
| 143 |
+
x = Poly(coeffs, n, domain=domain)
|
| 144 |
+
H = A*x.shift(1) - B*x - C
|
| 145 |
+
|
| 146 |
+
from sympy.solvers.solvers import solve
|
| 147 |
+
solution = solve(H.coeffs(), coeffs)
|
| 148 |
+
|
| 149 |
+
if solution is None:
|
| 150 |
+
return None # 'f(n)' is *not* Gosper-summable
|
| 151 |
+
|
| 152 |
+
x = x.as_expr().subs(solution)
|
| 153 |
+
|
| 154 |
+
for coeff in coeffs:
|
| 155 |
+
if coeff not in solution:
|
| 156 |
+
x = x.subs(coeff, 0)
|
| 157 |
+
|
| 158 |
+
if x.is_zero:
|
| 159 |
+
return None # 'f(n)' is *not* Gosper-summable
|
| 160 |
+
else:
|
| 161 |
+
return B.as_expr()*x/C.as_expr()
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def gosper_sum(f, k):
|
| 165 |
+
r"""
|
| 166 |
+
Gosper's hypergeometric summation algorithm.
|
| 167 |
+
|
| 168 |
+
Explanation
|
| 169 |
+
===========
|
| 170 |
+
|
| 171 |
+
Given a hypergeometric term ``f`` such that:
|
| 172 |
+
|
| 173 |
+
.. math ::
|
| 174 |
+
s_n = \sum_{k=0}^{n-1} f_k
|
| 175 |
+
|
| 176 |
+
and `f(n)` does not depend on `n`, returns `g_{n} - g(0)` where
|
| 177 |
+
`g_{n+1} - g_n = f_n`, or ``None`` if `s_n` cannot be expressed
|
| 178 |
+
in closed form as a sum of hypergeometric terms.
|
| 179 |
+
|
| 180 |
+
Examples
|
| 181 |
+
========
|
| 182 |
+
|
| 183 |
+
>>> from sympy.concrete.gosper import gosper_sum
|
| 184 |
+
>>> from sympy import factorial
|
| 185 |
+
>>> from sympy.abc import n, k
|
| 186 |
+
|
| 187 |
+
>>> f = (4*k + 1)*factorial(k)/factorial(2*k + 1)
|
| 188 |
+
>>> gosper_sum(f, (k, 0, n))
|
| 189 |
+
(-factorial(n) + 2*factorial(2*n + 1))/factorial(2*n + 1)
|
| 190 |
+
>>> _.subs(n, 2) == sum(f.subs(k, i) for i in [0, 1, 2])
|
| 191 |
+
True
|
| 192 |
+
>>> gosper_sum(f, (k, 3, n))
|
| 193 |
+
(-60*factorial(n) + factorial(2*n + 1))/(60*factorial(2*n + 1))
|
| 194 |
+
>>> _.subs(n, 5) == sum(f.subs(k, i) for i in [3, 4, 5])
|
| 195 |
+
True
|
| 196 |
+
|
| 197 |
+
References
|
| 198 |
+
==========
|
| 199 |
+
|
| 200 |
+
.. [1] Marko Petkovsek, Herbert S. Wilf, Doron Zeilberger, A = B,
|
| 201 |
+
AK Peters, Ltd., Wellesley, MA, USA, 1997, pp. 73--100
|
| 202 |
+
|
| 203 |
+
"""
|
| 204 |
+
indefinite = False
|
| 205 |
+
|
| 206 |
+
if is_sequence(k):
|
| 207 |
+
k, a, b = k
|
| 208 |
+
else:
|
| 209 |
+
indefinite = True
|
| 210 |
+
|
| 211 |
+
g = gosper_term(f, k)
|
| 212 |
+
|
| 213 |
+
if g is None:
|
| 214 |
+
return None
|
| 215 |
+
|
| 216 |
+
if indefinite:
|
| 217 |
+
result = f*g
|
| 218 |
+
else:
|
| 219 |
+
result = (f*(g + 1)).subs(k, b) - (f*g).subs(k, a)
|
| 220 |
+
|
| 221 |
+
if result is S.NaN:
|
| 222 |
+
try:
|
| 223 |
+
result = (f*(g + 1)).limit(k, b) - (f*g).limit(k, a)
|
| 224 |
+
except NotImplementedError:
|
| 225 |
+
result = None
|
| 226 |
+
|
| 227 |
+
return factor(result)
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/guess.py
ADDED
|
@@ -0,0 +1,473 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Various algorithms for helping identifying numbers and sequences."""
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
from sympy.concrete.products import (Product, product)
|
| 5 |
+
from sympy.core import Function, S
|
| 6 |
+
from sympy.core.add import Add
|
| 7 |
+
from sympy.core.numbers import Integer, Rational
|
| 8 |
+
from sympy.core.symbol import Symbol, symbols
|
| 9 |
+
from sympy.core.sympify import sympify
|
| 10 |
+
from sympy.functions.elementary.exponential import exp
|
| 11 |
+
from sympy.functions.elementary.integers import floor
|
| 12 |
+
from sympy.integrals.integrals import integrate
|
| 13 |
+
from sympy.polys.polyfuncs import rational_interpolate as rinterp
|
| 14 |
+
from sympy.polys.polytools import lcm
|
| 15 |
+
from sympy.simplify.radsimp import denom
|
| 16 |
+
from sympy.utilities import public
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@public
|
| 20 |
+
def find_simple_recurrence_vector(l):
|
| 21 |
+
"""
|
| 22 |
+
This function is used internally by other functions from the
|
| 23 |
+
sympy.concrete.guess module. While most users may want to rather use the
|
| 24 |
+
function find_simple_recurrence when looking for recurrence relations
|
| 25 |
+
among rational numbers, the current function may still be useful when
|
| 26 |
+
some post-processing has to be done.
|
| 27 |
+
|
| 28 |
+
Explanation
|
| 29 |
+
===========
|
| 30 |
+
|
| 31 |
+
The function returns a vector of length n when a recurrence relation of
|
| 32 |
+
order n is detected in the sequence of rational numbers v.
|
| 33 |
+
|
| 34 |
+
If the returned vector has a length 1, then the returned value is always
|
| 35 |
+
the list [0], which means that no relation has been found.
|
| 36 |
+
|
| 37 |
+
While the functions is intended to be used with rational numbers, it should
|
| 38 |
+
work for other kinds of real numbers except for some cases involving
|
| 39 |
+
quadratic numbers; for that reason it should be used with some caution when
|
| 40 |
+
the argument is not a list of rational numbers.
|
| 41 |
+
|
| 42 |
+
Examples
|
| 43 |
+
========
|
| 44 |
+
|
| 45 |
+
>>> from sympy.concrete.guess import find_simple_recurrence_vector
|
| 46 |
+
>>> from sympy import fibonacci
|
| 47 |
+
>>> find_simple_recurrence_vector([fibonacci(k) for k in range(12)])
|
| 48 |
+
[1, -1, -1]
|
| 49 |
+
|
| 50 |
+
See Also
|
| 51 |
+
========
|
| 52 |
+
|
| 53 |
+
See the function sympy.concrete.guess.find_simple_recurrence which is more
|
| 54 |
+
user-friendly.
|
| 55 |
+
|
| 56 |
+
"""
|
| 57 |
+
q1 = [0]
|
| 58 |
+
q2 = [1]
|
| 59 |
+
b, z = 0, len(l) >> 1
|
| 60 |
+
while len(q2) <= z:
|
| 61 |
+
while l[b]==0:
|
| 62 |
+
b += 1
|
| 63 |
+
if b == len(l):
|
| 64 |
+
c = 1
|
| 65 |
+
for x in q2:
|
| 66 |
+
c = lcm(c, denom(x))
|
| 67 |
+
if q2[0]*c < 0: c = -c
|
| 68 |
+
for k in range(len(q2)):
|
| 69 |
+
q2[k] = int(q2[k]*c)
|
| 70 |
+
return q2
|
| 71 |
+
a = S.One/l[b]
|
| 72 |
+
m = [a]
|
| 73 |
+
for k in range(b+1, len(l)):
|
| 74 |
+
m.append(-sum(l[j+1]*m[b-j-1] for j in range(b, k))*a)
|
| 75 |
+
l, m = m, [0] * max(len(q2), b+len(q1))
|
| 76 |
+
for k, q in enumerate(q2):
|
| 77 |
+
m[k] = a*q
|
| 78 |
+
for k, q in enumerate(q1):
|
| 79 |
+
m[k+b] += q
|
| 80 |
+
while m[-1]==0: m.pop() # because trailing zeros can occur
|
| 81 |
+
q1, q2, b = q2, m, 1
|
| 82 |
+
return [0]
|
| 83 |
+
|
| 84 |
+
@public
|
| 85 |
+
def find_simple_recurrence(v, A=Function('a'), N=Symbol('n')):
|
| 86 |
+
"""
|
| 87 |
+
Detects and returns a recurrence relation from a sequence of several integer
|
| 88 |
+
(or rational) terms. The name of the function in the returned expression is
|
| 89 |
+
'a' by default; the main variable is 'n' by default. The smallest index in
|
| 90 |
+
the returned expression is always n (and never n-1, n-2, etc.).
|
| 91 |
+
|
| 92 |
+
Examples
|
| 93 |
+
========
|
| 94 |
+
|
| 95 |
+
>>> from sympy.concrete.guess import find_simple_recurrence
|
| 96 |
+
>>> from sympy import fibonacci
|
| 97 |
+
>>> find_simple_recurrence([fibonacci(k) for k in range(12)])
|
| 98 |
+
-a(n) - a(n + 1) + a(n + 2)
|
| 99 |
+
|
| 100 |
+
>>> from sympy import Function, Symbol
|
| 101 |
+
>>> a = [1, 1, 1]
|
| 102 |
+
>>> for k in range(15): a.append(5*a[-1]-3*a[-2]+8*a[-3])
|
| 103 |
+
>>> find_simple_recurrence(a, A=Function('f'), N=Symbol('i'))
|
| 104 |
+
-8*f(i) + 3*f(i + 1) - 5*f(i + 2) + f(i + 3)
|
| 105 |
+
|
| 106 |
+
"""
|
| 107 |
+
p = find_simple_recurrence_vector(v)
|
| 108 |
+
n = len(p)
|
| 109 |
+
if n <= 1: return S.Zero
|
| 110 |
+
|
| 111 |
+
return Add(*[A(N+n-1-k)*p[k] for k in range(n)])
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
@public
|
| 115 |
+
def rationalize(x, maxcoeff=10000):
|
| 116 |
+
"""
|
| 117 |
+
Helps identifying a rational number from a float (or mpmath.mpf) value by
|
| 118 |
+
using a continued fraction. The algorithm stops as soon as a large partial
|
| 119 |
+
quotient is detected (greater than 10000 by default).
|
| 120 |
+
|
| 121 |
+
Examples
|
| 122 |
+
========
|
| 123 |
+
|
| 124 |
+
>>> from sympy.concrete.guess import rationalize
|
| 125 |
+
>>> from mpmath import cos, pi
|
| 126 |
+
>>> rationalize(cos(pi/3))
|
| 127 |
+
1/2
|
| 128 |
+
|
| 129 |
+
>>> from mpmath import mpf
|
| 130 |
+
>>> rationalize(mpf("0.333333333333333"))
|
| 131 |
+
1/3
|
| 132 |
+
|
| 133 |
+
While the function is rather intended to help 'identifying' rational
|
| 134 |
+
values, it may be used in some cases for approximating real numbers.
|
| 135 |
+
(Though other functions may be more relevant in that case.)
|
| 136 |
+
|
| 137 |
+
>>> rationalize(pi, maxcoeff = 250)
|
| 138 |
+
355/113
|
| 139 |
+
|
| 140 |
+
See Also
|
| 141 |
+
========
|
| 142 |
+
|
| 143 |
+
Several other methods can approximate a real number as a rational, like:
|
| 144 |
+
|
| 145 |
+
* fractions.Fraction.from_decimal
|
| 146 |
+
* fractions.Fraction.from_float
|
| 147 |
+
* mpmath.identify
|
| 148 |
+
* mpmath.pslq by using the following syntax: mpmath.pslq([x, 1])
|
| 149 |
+
* mpmath.findpoly by using the following syntax: mpmath.findpoly(x, 1)
|
| 150 |
+
* sympy.simplify.nsimplify (which is a more general function)
|
| 151 |
+
|
| 152 |
+
The main difference between the current function and all these variants is
|
| 153 |
+
that control focuses on magnitude of partial quotients here rather than on
|
| 154 |
+
global precision of the approximation. If the real is "known to be" a
|
| 155 |
+
rational number, the current function should be able to detect it correctly
|
| 156 |
+
with the default settings even when denominator is great (unless its
|
| 157 |
+
expansion contains unusually big partial quotients) which may occur
|
| 158 |
+
when studying sequences of increasing numbers. If the user cares more
|
| 159 |
+
on getting simple fractions, other methods may be more convenient.
|
| 160 |
+
|
| 161 |
+
"""
|
| 162 |
+
p0, p1 = 0, 1
|
| 163 |
+
q0, q1 = 1, 0
|
| 164 |
+
a = floor(x)
|
| 165 |
+
while a < maxcoeff or q1==0:
|
| 166 |
+
p = a*p1 + p0
|
| 167 |
+
q = a*q1 + q0
|
| 168 |
+
p0, p1 = p1, p
|
| 169 |
+
q0, q1 = q1, q
|
| 170 |
+
if x==a: break
|
| 171 |
+
x = 1/(x-a)
|
| 172 |
+
a = floor(x)
|
| 173 |
+
return sympify(p) / q
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
@public
|
| 177 |
+
def guess_generating_function_rational(v, X=Symbol('x')):
|
| 178 |
+
"""
|
| 179 |
+
Tries to "guess" a rational generating function for a sequence of rational
|
| 180 |
+
numbers v.
|
| 181 |
+
|
| 182 |
+
Examples
|
| 183 |
+
========
|
| 184 |
+
|
| 185 |
+
>>> from sympy.concrete.guess import guess_generating_function_rational
|
| 186 |
+
>>> from sympy import fibonacci
|
| 187 |
+
>>> l = [fibonacci(k) for k in range(5,15)]
|
| 188 |
+
>>> guess_generating_function_rational(l)
|
| 189 |
+
(3*x + 5)/(-x**2 - x + 1)
|
| 190 |
+
|
| 191 |
+
See Also
|
| 192 |
+
========
|
| 193 |
+
|
| 194 |
+
sympy.series.approximants
|
| 195 |
+
mpmath.pade
|
| 196 |
+
|
| 197 |
+
"""
|
| 198 |
+
# a) compute the denominator as q
|
| 199 |
+
q = find_simple_recurrence_vector(v)
|
| 200 |
+
n = len(q)
|
| 201 |
+
if n <= 1: return None
|
| 202 |
+
# b) compute the numerator as p
|
| 203 |
+
p = [sum(v[i-k]*q[k] for k in range(min(i+1, n)))
|
| 204 |
+
for i in range(len(v)>>1)]
|
| 205 |
+
return (sum(p[k]*X**k for k in range(len(p)))
|
| 206 |
+
/ sum(q[k]*X**k for k in range(n)))
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
@public
|
| 210 |
+
def guess_generating_function(v, X=Symbol('x'), types=['all'], maxsqrtn=2):
|
| 211 |
+
"""
|
| 212 |
+
Tries to "guess" a generating function for a sequence of rational numbers v.
|
| 213 |
+
Only a few patterns are implemented yet.
|
| 214 |
+
|
| 215 |
+
Explanation
|
| 216 |
+
===========
|
| 217 |
+
|
| 218 |
+
The function returns a dictionary where keys are the name of a given type of
|
| 219 |
+
generating function. Six types are currently implemented:
|
| 220 |
+
|
| 221 |
+
type | formal definition
|
| 222 |
+
-------+----------------------------------------------------------------
|
| 223 |
+
ogf | f(x) = Sum( a_k * x^k , k: 0..infinity )
|
| 224 |
+
egf | f(x) = Sum( a_k * x^k / k! , k: 0..infinity )
|
| 225 |
+
lgf | f(x) = Sum( (-1)^(k+1) a_k * x^k / k , k: 1..infinity )
|
| 226 |
+
| (with initial index being hold as 1 rather than 0)
|
| 227 |
+
hlgf | f(x) = Sum( a_k * x^k / k , k: 1..infinity )
|
| 228 |
+
| (with initial index being hold as 1 rather than 0)
|
| 229 |
+
lgdogf | f(x) = derivate( log(Sum( a_k * x^k, k: 0..infinity )), x)
|
| 230 |
+
lgdegf | f(x) = derivate( log(Sum( a_k * x^k / k!, k: 0..infinity )), x)
|
| 231 |
+
|
| 232 |
+
In order to spare time, the user can select only some types of generating
|
| 233 |
+
functions (default being ['all']). While forgetting to use a list in the
|
| 234 |
+
case of a single type may seem to work most of the time as in: types='ogf'
|
| 235 |
+
this (convenient) syntax may lead to unexpected extra results in some cases.
|
| 236 |
+
|
| 237 |
+
Discarding a type when calling the function does not mean that the type will
|
| 238 |
+
not be present in the returned dictionary; it only means that no extra
|
| 239 |
+
computation will be performed for that type, but the function may still add
|
| 240 |
+
it in the result when it can be easily converted from another type.
|
| 241 |
+
|
| 242 |
+
Two generating functions (lgdogf and lgdegf) are not even computed if the
|
| 243 |
+
initial term of the sequence is 0; it may be useful in that case to try
|
| 244 |
+
again after having removed the leading zeros.
|
| 245 |
+
|
| 246 |
+
Examples
|
| 247 |
+
========
|
| 248 |
+
|
| 249 |
+
>>> from sympy.concrete.guess import guess_generating_function as ggf
|
| 250 |
+
>>> ggf([k+1 for k in range(12)], types=['ogf', 'lgf', 'hlgf'])
|
| 251 |
+
{'hlgf': 1/(1 - x), 'lgf': 1/(x + 1), 'ogf': 1/(x**2 - 2*x + 1)}
|
| 252 |
+
|
| 253 |
+
>>> from sympy import sympify
|
| 254 |
+
>>> l = sympify("[3/2, 11/2, 0, -121/2, -363/2, 121]")
|
| 255 |
+
>>> ggf(l)
|
| 256 |
+
{'ogf': (x + 3/2)/(11*x**2 - 3*x + 1)}
|
| 257 |
+
|
| 258 |
+
>>> from sympy import fibonacci
|
| 259 |
+
>>> ggf([fibonacci(k) for k in range(5, 15)], types=['ogf'])
|
| 260 |
+
{'ogf': (3*x + 5)/(-x**2 - x + 1)}
|
| 261 |
+
|
| 262 |
+
>>> from sympy import factorial
|
| 263 |
+
>>> ggf([factorial(k) for k in range(12)], types=['ogf', 'egf', 'lgf'])
|
| 264 |
+
{'egf': 1/(1 - x)}
|
| 265 |
+
|
| 266 |
+
>>> ggf([k+1 for k in range(12)], types=['egf'])
|
| 267 |
+
{'egf': (x + 1)*exp(x), 'lgdegf': (x + 2)/(x + 1)}
|
| 268 |
+
|
| 269 |
+
N-th root of a rational function can also be detected (below is an example
|
| 270 |
+
coming from the sequence A108626 from https://oeis.org).
|
| 271 |
+
The greatest n-th root to be tested is specified as maxsqrtn (default 2).
|
| 272 |
+
|
| 273 |
+
>>> ggf([1, 2, 5, 14, 41, 124, 383, 1200, 3799, 12122, 38919])['ogf']
|
| 274 |
+
sqrt(1/(x**4 + 2*x**2 - 4*x + 1))
|
| 275 |
+
|
| 276 |
+
References
|
| 277 |
+
==========
|
| 278 |
+
|
| 279 |
+
.. [1] "Concrete Mathematics", R.L. Graham, D.E. Knuth, O. Patashnik
|
| 280 |
+
.. [2] https://oeis.org/wiki/Generating_functions
|
| 281 |
+
|
| 282 |
+
"""
|
| 283 |
+
# List of all types of all g.f. known by the algorithm
|
| 284 |
+
if 'all' in types:
|
| 285 |
+
types = ('ogf', 'egf', 'lgf', 'hlgf', 'lgdogf', 'lgdegf')
|
| 286 |
+
|
| 287 |
+
result = {}
|
| 288 |
+
|
| 289 |
+
# Ordinary Generating Function (ogf)
|
| 290 |
+
if 'ogf' in types:
|
| 291 |
+
# Perform some convolutions of the sequence with itself
|
| 292 |
+
t = [1] + [0]*(len(v) - 1)
|
| 293 |
+
for d in range(max(1, maxsqrtn)):
|
| 294 |
+
t = [sum(t[n-i]*v[i] for i in range(n+1)) for n in range(len(v))]
|
| 295 |
+
g = guess_generating_function_rational(t, X=X)
|
| 296 |
+
if g:
|
| 297 |
+
result['ogf'] = g**Rational(1, d+1)
|
| 298 |
+
break
|
| 299 |
+
|
| 300 |
+
# Exponential Generating Function (egf)
|
| 301 |
+
if 'egf' in types:
|
| 302 |
+
# Transform sequence (division by factorial)
|
| 303 |
+
w, f = [], S.One
|
| 304 |
+
for i, k in enumerate(v):
|
| 305 |
+
f *= i if i else 1
|
| 306 |
+
w.append(k/f)
|
| 307 |
+
# Perform some convolutions of the sequence with itself
|
| 308 |
+
t = [1] + [0]*(len(w) - 1)
|
| 309 |
+
for d in range(max(1, maxsqrtn)):
|
| 310 |
+
t = [sum(t[n-i]*w[i] for i in range(n+1)) for n in range(len(w))]
|
| 311 |
+
g = guess_generating_function_rational(t, X=X)
|
| 312 |
+
if g:
|
| 313 |
+
result['egf'] = g**Rational(1, d+1)
|
| 314 |
+
break
|
| 315 |
+
|
| 316 |
+
# Logarithmic Generating Function (lgf)
|
| 317 |
+
if 'lgf' in types:
|
| 318 |
+
# Transform sequence (multiplication by (-1)^(n+1) / n)
|
| 319 |
+
w, f = [], S.NegativeOne
|
| 320 |
+
for i, k in enumerate(v):
|
| 321 |
+
f = -f
|
| 322 |
+
w.append(f*k/Integer(i+1))
|
| 323 |
+
# Perform some convolutions of the sequence with itself
|
| 324 |
+
t = [1] + [0]*(len(w) - 1)
|
| 325 |
+
for d in range(max(1, maxsqrtn)):
|
| 326 |
+
t = [sum(t[n-i]*w[i] for i in range(n+1)) for n in range(len(w))]
|
| 327 |
+
g = guess_generating_function_rational(t, X=X)
|
| 328 |
+
if g:
|
| 329 |
+
result['lgf'] = g**Rational(1, d+1)
|
| 330 |
+
break
|
| 331 |
+
|
| 332 |
+
# Hyperbolic logarithmic Generating Function (hlgf)
|
| 333 |
+
if 'hlgf' in types:
|
| 334 |
+
# Transform sequence (division by n+1)
|
| 335 |
+
w = []
|
| 336 |
+
for i, k in enumerate(v):
|
| 337 |
+
w.append(k/Integer(i+1))
|
| 338 |
+
# Perform some convolutions of the sequence with itself
|
| 339 |
+
t = [1] + [0]*(len(w) - 1)
|
| 340 |
+
for d in range(max(1, maxsqrtn)):
|
| 341 |
+
t = [sum(t[n-i]*w[i] for i in range(n+1)) for n in range(len(w))]
|
| 342 |
+
g = guess_generating_function_rational(t, X=X)
|
| 343 |
+
if g:
|
| 344 |
+
result['hlgf'] = g**Rational(1, d+1)
|
| 345 |
+
break
|
| 346 |
+
|
| 347 |
+
# Logarithmic derivative of ordinary generating Function (lgdogf)
|
| 348 |
+
if v[0] != 0 and ('lgdogf' in types
|
| 349 |
+
or ('ogf' in types and 'ogf' not in result)):
|
| 350 |
+
# Transform sequence by computing f'(x)/f(x)
|
| 351 |
+
# because log(f(x)) = integrate( f'(x)/f(x) )
|
| 352 |
+
a, w = sympify(v[0]), []
|
| 353 |
+
for n in range(len(v)-1):
|
| 354 |
+
w.append(
|
| 355 |
+
(v[n+1]*(n+1) - sum(w[-i-1]*v[i+1] for i in range(n)))/a)
|
| 356 |
+
# Perform some convolutions of the sequence with itself
|
| 357 |
+
t = [1] + [0]*(len(w) - 1)
|
| 358 |
+
for d in range(max(1, maxsqrtn)):
|
| 359 |
+
t = [sum(t[n-i]*w[i] for i in range(n+1)) for n in range(len(w))]
|
| 360 |
+
g = guess_generating_function_rational(t, X=X)
|
| 361 |
+
if g:
|
| 362 |
+
result['lgdogf'] = g**Rational(1, d+1)
|
| 363 |
+
if 'ogf' not in result:
|
| 364 |
+
result['ogf'] = exp(integrate(result['lgdogf'], X))
|
| 365 |
+
break
|
| 366 |
+
|
| 367 |
+
# Logarithmic derivative of exponential generating Function (lgdegf)
|
| 368 |
+
if v[0] != 0 and ('lgdegf' in types
|
| 369 |
+
or ('egf' in types and 'egf' not in result)):
|
| 370 |
+
# Transform sequence / step 1 (division by factorial)
|
| 371 |
+
z, f = [], S.One
|
| 372 |
+
for i, k in enumerate(v):
|
| 373 |
+
f *= i if i else 1
|
| 374 |
+
z.append(k/f)
|
| 375 |
+
# Transform sequence / step 2 by computing f'(x)/f(x)
|
| 376 |
+
# because log(f(x)) = integrate( f'(x)/f(x) )
|
| 377 |
+
a, w = z[0], []
|
| 378 |
+
for n in range(len(z)-1):
|
| 379 |
+
w.append(
|
| 380 |
+
(z[n+1]*(n+1) - sum(w[-i-1]*z[i+1] for i in range(n)))/a)
|
| 381 |
+
# Perform some convolutions of the sequence with itself
|
| 382 |
+
t = [1] + [0]*(len(w) - 1)
|
| 383 |
+
for d in range(max(1, maxsqrtn)):
|
| 384 |
+
t = [sum(t[n-i]*w[i] for i in range(n+1)) for n in range(len(w))]
|
| 385 |
+
g = guess_generating_function_rational(t, X=X)
|
| 386 |
+
if g:
|
| 387 |
+
result['lgdegf'] = g**Rational(1, d+1)
|
| 388 |
+
if 'egf' not in result:
|
| 389 |
+
result['egf'] = exp(integrate(result['lgdegf'], X))
|
| 390 |
+
break
|
| 391 |
+
|
| 392 |
+
return result
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
@public
|
| 396 |
+
def guess(l, all=False, evaluate=True, niter=2, variables=None):
|
| 397 |
+
"""
|
| 398 |
+
This function is adapted from the Rate.m package for Mathematica
|
| 399 |
+
written by Christian Krattenthaler.
|
| 400 |
+
It tries to guess a formula from a given sequence of rational numbers.
|
| 401 |
+
|
| 402 |
+
Explanation
|
| 403 |
+
===========
|
| 404 |
+
|
| 405 |
+
In order to speed up the process, the 'all' variable is set to False by
|
| 406 |
+
default, stopping the computation as some results are returned during an
|
| 407 |
+
iteration; the variable can be set to True if more iterations are needed
|
| 408 |
+
(other formulas may be found; however they may be equivalent to the first
|
| 409 |
+
ones).
|
| 410 |
+
|
| 411 |
+
Another option is the 'evaluate' variable (default is True); setting it
|
| 412 |
+
to False will leave the involved products unevaluated.
|
| 413 |
+
|
| 414 |
+
By default, the number of iterations is set to 2 but a greater value (up
|
| 415 |
+
to len(l)-1) can be specified with the optional 'niter' variable.
|
| 416 |
+
More and more convoluted results are found when the order of the
|
| 417 |
+
iteration gets higher:
|
| 418 |
+
|
| 419 |
+
* first iteration returns polynomial or rational functions;
|
| 420 |
+
* second iteration returns products of rising factorials and their
|
| 421 |
+
inverses;
|
| 422 |
+
* third iteration returns products of products of rising factorials
|
| 423 |
+
and their inverses;
|
| 424 |
+
* etc.
|
| 425 |
+
|
| 426 |
+
The returned formulas contain symbols i0, i1, i2, ... where the main
|
| 427 |
+
variables is i0 (and auxiliary variables are i1, i2, ...). A list of
|
| 428 |
+
other symbols can be provided in the 'variables' option; the length of
|
| 429 |
+
the least should be the value of 'niter' (more is acceptable but only
|
| 430 |
+
the first symbols will be used); in this case, the main variable will be
|
| 431 |
+
the first symbol in the list.
|
| 432 |
+
|
| 433 |
+
Examples
|
| 434 |
+
========
|
| 435 |
+
|
| 436 |
+
>>> from sympy.concrete.guess import guess
|
| 437 |
+
>>> guess([1,2,6,24,120], evaluate=False)
|
| 438 |
+
[Product(i1 + 1, (i1, 1, i0 - 1))]
|
| 439 |
+
|
| 440 |
+
>>> from sympy import symbols
|
| 441 |
+
>>> r = guess([1,2,7,42,429,7436,218348,10850216], niter=4)
|
| 442 |
+
>>> i0 = symbols("i0")
|
| 443 |
+
>>> [r[0].subs(i0,n).doit() for n in range(1,10)]
|
| 444 |
+
[1, 2, 7, 42, 429, 7436, 218348, 10850216, 911835460]
|
| 445 |
+
"""
|
| 446 |
+
if any(a==0 for a in l[:-1]):
|
| 447 |
+
return []
|
| 448 |
+
N = len(l)
|
| 449 |
+
niter = min(N-1, niter)
|
| 450 |
+
myprod = product if evaluate else Product
|
| 451 |
+
g = []
|
| 452 |
+
res = []
|
| 453 |
+
if variables is None:
|
| 454 |
+
symb = symbols('i:'+str(niter))
|
| 455 |
+
else:
|
| 456 |
+
symb = variables
|
| 457 |
+
for k, s in enumerate(symb):
|
| 458 |
+
g.append(l)
|
| 459 |
+
n, r = len(l), []
|
| 460 |
+
for i in range(n-2-1, -1, -1):
|
| 461 |
+
ri = rinterp(enumerate(g[k][:-1], start=1), i, X=s)
|
| 462 |
+
if ((denom(ri).subs({s:n}) != 0)
|
| 463 |
+
and (ri.subs({s:n}) - g[k][-1] == 0)
|
| 464 |
+
and ri not in r):
|
| 465 |
+
r.append(ri)
|
| 466 |
+
if r:
|
| 467 |
+
for i in range(k-1, -1, -1):
|
| 468 |
+
r = [g[i][0]
|
| 469 |
+
* myprod(v, (symb[i+1], 1, symb[i]-1)) for v in r]
|
| 470 |
+
if not all: return r
|
| 471 |
+
res += r
|
| 472 |
+
l = [Rational(l[i+1], l[i]) for i in range(N-k-1)]
|
| 473 |
+
return res
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/tests/__init__.py
ADDED
|
File without changes
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/tests/__pycache__/test_guess.cpython-311.pyc
ADDED
|
Binary file (8.82 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/tests/__pycache__/test_products.cpython-311.pyc
ADDED
|
Binary file (37.1 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/tests/test_gosper.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 3 |
+
from sympy.core.numbers import (Rational, pi)
|
| 4 |
+
from sympy.core.singleton import S
|
| 5 |
+
from sympy.core.symbol import Symbol
|
| 6 |
+
from sympy.functions.combinatorial.factorials import (binomial, factorial)
|
| 7 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
| 8 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 9 |
+
from sympy.functions.special.gamma_functions import gamma
|
| 10 |
+
from sympy.polys.polytools import Poly
|
| 11 |
+
from sympy.simplify.simplify import simplify
|
| 12 |
+
from sympy.concrete.gosper import gosper_normal, gosper_sum, gosper_term
|
| 13 |
+
from sympy.abc import a, b, j, k, m, n, r, x
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_gosper_normal():
|
| 17 |
+
eq = 4*n + 5, 2*(4*n + 1)*(2*n + 3), n
|
| 18 |
+
assert gosper_normal(*eq) == \
|
| 19 |
+
(Poly(Rational(1, 4), n), Poly(n + Rational(3, 2)), Poly(n + Rational(1, 4)))
|
| 20 |
+
assert gosper_normal(*eq, polys=False) == \
|
| 21 |
+
(Rational(1, 4), n + Rational(3, 2), n + Rational(1, 4))
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_gosper_term():
|
| 25 |
+
assert gosper_term((4*k + 1)*factorial(
|
| 26 |
+
k)/factorial(2*k + 1), k) == (-k - S.Half)/(k + Rational(1, 4))
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_gosper_sum():
|
| 30 |
+
assert gosper_sum(1, (k, 0, n)) == 1 + n
|
| 31 |
+
assert gosper_sum(k, (k, 0, n)) == n*(1 + n)/2
|
| 32 |
+
assert gosper_sum(k**2, (k, 0, n)) == n*(1 + n)*(1 + 2*n)/6
|
| 33 |
+
assert gosper_sum(k**3, (k, 0, n)) == n**2*(1 + n)**2/4
|
| 34 |
+
|
| 35 |
+
assert gosper_sum(2**k, (k, 0, n)) == 2*2**n - 1
|
| 36 |
+
|
| 37 |
+
assert gosper_sum(factorial(k), (k, 0, n)) is None
|
| 38 |
+
assert gosper_sum(binomial(n, k), (k, 0, n)) is None
|
| 39 |
+
|
| 40 |
+
assert gosper_sum(factorial(k)/k**2, (k, 0, n)) is None
|
| 41 |
+
assert gosper_sum((k - 3)*factorial(k), (k, 0, n)) is None
|
| 42 |
+
|
| 43 |
+
assert gosper_sum(k*factorial(k), k) == factorial(k)
|
| 44 |
+
assert gosper_sum(
|
| 45 |
+
k*factorial(k), (k, 0, n)) == n*factorial(n) + factorial(n) - 1
|
| 46 |
+
|
| 47 |
+
assert gosper_sum((-1)**k*binomial(n, k), (k, 0, n)) == 0
|
| 48 |
+
assert gosper_sum((
|
| 49 |
+
-1)**k*binomial(n, k), (k, 0, m)) == -(-1)**m*(m - n)*binomial(n, m)/n
|
| 50 |
+
|
| 51 |
+
assert gosper_sum((4*k + 1)*factorial(k)/factorial(2*k + 1), (k, 0, n)) == \
|
| 52 |
+
(2*factorial(2*n + 1) - factorial(n))/factorial(2*n + 1)
|
| 53 |
+
|
| 54 |
+
# issue 6033:
|
| 55 |
+
assert gosper_sum(
|
| 56 |
+
n*(n + a + b)*a**n*b**n/(factorial(n + a)*factorial(n + b)), \
|
| 57 |
+
(n, 0, m)).simplify() == -exp(m*log(a) + m*log(b))*gamma(a + 1) \
|
| 58 |
+
*gamma(b + 1)/(gamma(a)*gamma(b)*gamma(a + m + 1)*gamma(b + m + 1)) \
|
| 59 |
+
+ 1/(gamma(a)*gamma(b))
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_gosper_sum_indefinite():
|
| 63 |
+
assert gosper_sum(k, k) == k*(k - 1)/2
|
| 64 |
+
assert gosper_sum(k**2, k) == k*(k - 1)*(2*k - 1)/6
|
| 65 |
+
|
| 66 |
+
assert gosper_sum(1/(k*(k + 1)), k) == -1/k
|
| 67 |
+
assert gosper_sum(-(27*k**4 + 158*k**3 + 430*k**2 + 678*k + 445)*gamma(2*k
|
| 68 |
+
+ 4)/(3*(3*k + 7)*gamma(3*k + 6)), k) == \
|
| 69 |
+
(3*k + 5)*(k**2 + 2*k + 5)*gamma(2*k + 4)/gamma(3*k + 6)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_gosper_sum_parametric():
|
| 73 |
+
assert gosper_sum(binomial(S.Half, m - j + 1)*binomial(S.Half, m + j), (j, 1, n)) == \
|
| 74 |
+
n*(1 + m - n)*(-1 + 2*m + 2*n)*binomial(S.Half, 1 + m - n)* \
|
| 75 |
+
binomial(S.Half, m + n)/(m*(1 + 2*m))
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def test_gosper_sum_algebraic():
|
| 79 |
+
assert gosper_sum(
|
| 80 |
+
n**2 + sqrt(2), (n, 0, m)) == (m + 1)*(2*m**2 + m + 6*sqrt(2))/6
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def test_gosper_sum_iterated():
|
| 84 |
+
f1 = binomial(2*k, k)/4**k
|
| 85 |
+
f2 = (1 + 2*n)*binomial(2*n, n)/4**n
|
| 86 |
+
f3 = (1 + 2*n)*(3 + 2*n)*binomial(2*n, n)/(3*4**n)
|
| 87 |
+
f4 = (1 + 2*n)*(3 + 2*n)*(5 + 2*n)*binomial(2*n, n)/(15*4**n)
|
| 88 |
+
f5 = (1 + 2*n)*(3 + 2*n)*(5 + 2*n)*(7 + 2*n)*binomial(2*n, n)/(105*4**n)
|
| 89 |
+
|
| 90 |
+
assert gosper_sum(f1, (k, 0, n)) == f2
|
| 91 |
+
assert gosper_sum(f2, (n, 0, n)) == f3
|
| 92 |
+
assert gosper_sum(f3, (n, 0, n)) == f4
|
| 93 |
+
assert gosper_sum(f4, (n, 0, n)) == f5
|
| 94 |
+
|
| 95 |
+
# the AeqB tests test expressions given in
|
| 96 |
+
# www.math.upenn.edu/~wilf/AeqB.pdf
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def test_gosper_sum_AeqB_part1():
|
| 100 |
+
f1a = n**4
|
| 101 |
+
f1b = n**3*2**n
|
| 102 |
+
f1c = 1/(n**2 + sqrt(5)*n - 1)
|
| 103 |
+
f1d = n**4*4**n/binomial(2*n, n)
|
| 104 |
+
f1e = factorial(3*n)/(factorial(n)*factorial(n + 1)*factorial(n + 2)*27**n)
|
| 105 |
+
f1f = binomial(2*n, n)**2/((n + 1)*4**(2*n))
|
| 106 |
+
f1g = (4*n - 1)*binomial(2*n, n)**2/((2*n - 1)**2*4**(2*n))
|
| 107 |
+
f1h = n*factorial(n - S.Half)**2/factorial(n + 1)**2
|
| 108 |
+
|
| 109 |
+
g1a = m*(m + 1)*(2*m + 1)*(3*m**2 + 3*m - 1)/30
|
| 110 |
+
g1b = 26 + 2**(m + 1)*(m**3 - 3*m**2 + 9*m - 13)
|
| 111 |
+
g1c = (m + 1)*(m*(m**2 - 7*m + 3)*sqrt(5) - (
|
| 112 |
+
3*m**3 - 7*m**2 + 19*m - 6))/(2*m**3*sqrt(5) + m**4 + 5*m**2 - 1)/6
|
| 113 |
+
g1d = Rational(-2, 231) + 2*4**m*(m + 1)*(63*m**4 + 112*m**3 + 18*m**2 -
|
| 114 |
+
22*m + 3)/(693*binomial(2*m, m))
|
| 115 |
+
g1e = Rational(-9, 2) + (81*m**2 + 261*m + 200)*factorial(
|
| 116 |
+
3*m + 2)/(40*27**m*factorial(m)*factorial(m + 1)*factorial(m + 2))
|
| 117 |
+
g1f = (2*m + 1)**2*binomial(2*m, m)**2/(4**(2*m)*(m + 1))
|
| 118 |
+
g1g = -binomial(2*m, m)**2/4**(2*m)
|
| 119 |
+
g1h = 4*pi -(2*m + 1)**2*(3*m + 4)*factorial(m - S.Half)**2/factorial(m + 1)**2
|
| 120 |
+
|
| 121 |
+
g = gosper_sum(f1a, (n, 0, m))
|
| 122 |
+
assert g is not None and simplify(g - g1a) == 0
|
| 123 |
+
g = gosper_sum(f1b, (n, 0, m))
|
| 124 |
+
assert g is not None and simplify(g - g1b) == 0
|
| 125 |
+
g = gosper_sum(f1c, (n, 0, m))
|
| 126 |
+
assert g is not None and simplify(g - g1c) == 0
|
| 127 |
+
g = gosper_sum(f1d, (n, 0, m))
|
| 128 |
+
assert g is not None and simplify(g - g1d) == 0
|
| 129 |
+
g = gosper_sum(f1e, (n, 0, m))
|
| 130 |
+
assert g is not None and simplify(g - g1e) == 0
|
| 131 |
+
g = gosper_sum(f1f, (n, 0, m))
|
| 132 |
+
assert g is not None and simplify(g - g1f) == 0
|
| 133 |
+
g = gosper_sum(f1g, (n, 0, m))
|
| 134 |
+
assert g is not None and simplify(g - g1g) == 0
|
| 135 |
+
g = gosper_sum(f1h, (n, 0, m))
|
| 136 |
+
# need to call rewrite(gamma) here because we have terms involving
|
| 137 |
+
# factorial(1/2)
|
| 138 |
+
assert g is not None and simplify(g - g1h).rewrite(gamma) == 0
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def test_gosper_sum_AeqB_part2():
|
| 142 |
+
f2a = n**2*a**n
|
| 143 |
+
f2b = (n - r/2)*binomial(r, n)
|
| 144 |
+
f2c = factorial(n - 1)**2/(factorial(n - x)*factorial(n + x))
|
| 145 |
+
|
| 146 |
+
g2a = -a*(a + 1)/(a - 1)**3 + a**(
|
| 147 |
+
m + 1)*(a**2*m**2 - 2*a*m**2 + m**2 - 2*a*m + 2*m + a + 1)/(a - 1)**3
|
| 148 |
+
g2b = (m - r)*binomial(r, m)/2
|
| 149 |
+
ff = factorial(1 - x)*factorial(1 + x)
|
| 150 |
+
g2c = 1/ff*(
|
| 151 |
+
1 - 1/x**2) + factorial(m)**2/(x**2*factorial(m - x)*factorial(m + x))
|
| 152 |
+
|
| 153 |
+
g = gosper_sum(f2a, (n, 0, m))
|
| 154 |
+
assert g is not None and simplify(g - g2a) == 0
|
| 155 |
+
g = gosper_sum(f2b, (n, 0, m))
|
| 156 |
+
assert g is not None and simplify(g - g2b) == 0
|
| 157 |
+
g = gosper_sum(f2c, (n, 1, m))
|
| 158 |
+
assert g is not None and simplify(g - g2c) == 0
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def test_gosper_nan():
|
| 162 |
+
a = Symbol('a', positive=True)
|
| 163 |
+
b = Symbol('b', positive=True)
|
| 164 |
+
n = Symbol('n', integer=True)
|
| 165 |
+
m = Symbol('m', integer=True)
|
| 166 |
+
f2d = n*(n + a + b)*a**n*b**n/(factorial(n + a)*factorial(n + b))
|
| 167 |
+
g2d = 1/(factorial(a - 1)*factorial(
|
| 168 |
+
b - 1)) - a**(m + 1)*b**(m + 1)/(factorial(a + m)*factorial(b + m))
|
| 169 |
+
g = gosper_sum(f2d, (n, 0, m))
|
| 170 |
+
assert simplify(g - g2d) == 0
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def test_gosper_sum_AeqB_part3():
|
| 174 |
+
f3a = 1/n**4
|
| 175 |
+
f3b = (6*n + 3)/(4*n**4 + 8*n**3 + 8*n**2 + 4*n + 3)
|
| 176 |
+
f3c = 2**n*(n**2 - 2*n - 1)/(n**2*(n + 1)**2)
|
| 177 |
+
f3d = n**2*4**n/((n + 1)*(n + 2))
|
| 178 |
+
f3e = 2**n/(n + 1)
|
| 179 |
+
f3f = 4*(n - 1)*(n**2 - 2*n - 1)/(n**2*(n + 1)**2*(n - 2)**2*(n - 3)**2)
|
| 180 |
+
f3g = (n**4 - 14*n**2 - 24*n - 9)*2**n/(n**2*(n + 1)**2*(n + 2)**2*
|
| 181 |
+
(n + 3)**2)
|
| 182 |
+
|
| 183 |
+
# g3a -> no closed form
|
| 184 |
+
g3b = m*(m + 2)/(2*m**2 + 4*m + 3)
|
| 185 |
+
g3c = 2**m/m**2 - 2
|
| 186 |
+
g3d = Rational(2, 3) + 4**(m + 1)*(m - 1)/(m + 2)/3
|
| 187 |
+
# g3e -> no closed form
|
| 188 |
+
g3f = -(Rational(-1, 16) + 1/((m - 2)**2*(m + 1)**2)) # the AeqB key is wrong
|
| 189 |
+
g3g = Rational(-2, 9) + 2**(m + 1)/((m + 1)**2*(m + 3)**2)
|
| 190 |
+
|
| 191 |
+
g = gosper_sum(f3a, (n, 1, m))
|
| 192 |
+
assert g is None
|
| 193 |
+
g = gosper_sum(f3b, (n, 1, m))
|
| 194 |
+
assert g is not None and simplify(g - g3b) == 0
|
| 195 |
+
g = gosper_sum(f3c, (n, 1, m - 1))
|
| 196 |
+
assert g is not None and simplify(g - g3c) == 0
|
| 197 |
+
g = gosper_sum(f3d, (n, 1, m))
|
| 198 |
+
assert g is not None and simplify(g - g3d) == 0
|
| 199 |
+
g = gosper_sum(f3e, (n, 0, m - 1))
|
| 200 |
+
assert g is None
|
| 201 |
+
g = gosper_sum(f3f, (n, 4, m))
|
| 202 |
+
assert g is not None and simplify(g - g3f) == 0
|
| 203 |
+
g = gosper_sum(f3g, (n, 1, m))
|
| 204 |
+
assert g is not None and simplify(g - g3g) == 0
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/concrete/tests/test_guess.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.concrete.guess import (
|
| 2 |
+
find_simple_recurrence_vector,
|
| 3 |
+
find_simple_recurrence,
|
| 4 |
+
rationalize,
|
| 5 |
+
guess_generating_function_rational,
|
| 6 |
+
guess_generating_function,
|
| 7 |
+
guess
|
| 8 |
+
)
|
| 9 |
+
from sympy.concrete.products import Product
|
| 10 |
+
from sympy.core.function import Function
|
| 11 |
+
from sympy.core.numbers import Rational
|
| 12 |
+
from sympy.core.singleton import S
|
| 13 |
+
from sympy.core.symbol import (Symbol, symbols)
|
| 14 |
+
from sympy.core.sympify import sympify
|
| 15 |
+
from sympy.functions.combinatorial.factorials import (RisingFactorial, factorial)
|
| 16 |
+
from sympy.functions.combinatorial.numbers import fibonacci
|
| 17 |
+
from sympy.functions.elementary.exponential import exp
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_find_simple_recurrence_vector():
|
| 21 |
+
assert find_simple_recurrence_vector(
|
| 22 |
+
[fibonacci(k) for k in range(12)]) == [1, -1, -1]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_find_simple_recurrence():
|
| 26 |
+
a = Function('a')
|
| 27 |
+
n = Symbol('n')
|
| 28 |
+
assert find_simple_recurrence([fibonacci(k) for k in range(12)]) == (
|
| 29 |
+
-a(n) - a(n + 1) + a(n + 2))
|
| 30 |
+
|
| 31 |
+
f = Function('a')
|
| 32 |
+
i = Symbol('n')
|
| 33 |
+
a = [1, 1, 1]
|
| 34 |
+
for k in range(15): a.append(5*a[-1]-3*a[-2]+8*a[-3])
|
| 35 |
+
assert find_simple_recurrence(a, A=f, N=i) == (
|
| 36 |
+
-8*f(i) + 3*f(i + 1) - 5*f(i + 2) + f(i + 3))
|
| 37 |
+
assert find_simple_recurrence([0, 2, 15, 74, 12, 3, 0,
|
| 38 |
+
1, 2, 85, 4, 5, 63]) == 0
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_rationalize():
|
| 42 |
+
from mpmath import cos, pi, mpf
|
| 43 |
+
assert rationalize(cos(pi/3)) == S.Half
|
| 44 |
+
assert rationalize(mpf("0.333333333333333")) == Rational(1, 3)
|
| 45 |
+
assert rationalize(mpf("-0.333333333333333")) == Rational(-1, 3)
|
| 46 |
+
assert rationalize(pi, maxcoeff = 250) == Rational(355, 113)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_guess_generating_function_rational():
|
| 50 |
+
x = Symbol('x')
|
| 51 |
+
assert guess_generating_function_rational([fibonacci(k)
|
| 52 |
+
for k in range(5, 15)]) == ((3*x + 5)/(-x**2 - x + 1))
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def test_guess_generating_function():
|
| 56 |
+
x = Symbol('x')
|
| 57 |
+
assert guess_generating_function([fibonacci(k)
|
| 58 |
+
for k in range(5, 15)])['ogf'] == ((3*x + 5)/(-x**2 - x + 1))
|
| 59 |
+
assert guess_generating_function(
|
| 60 |
+
[1, 2, 5, 14, 41, 124, 383, 1200, 3799, 12122, 38919])['ogf'] == (
|
| 61 |
+
(1/(x**4 + 2*x**2 - 4*x + 1))**S.Half)
|
| 62 |
+
assert guess_generating_function(sympify(
|
| 63 |
+
"[3/2, 11/2, 0, -121/2, -363/2, 121, 4719/2, 11495/2, -8712, -178717/2]")
|
| 64 |
+
)['ogf'] == (x + Rational(3, 2))/(11*x**2 - 3*x + 1)
|
| 65 |
+
assert guess_generating_function([factorial(k) for k in range(12)],
|
| 66 |
+
types=['egf'])['egf'] == 1/(-x + 1)
|
| 67 |
+
assert guess_generating_function([k+1 for k in range(12)],
|
| 68 |
+
types=['egf']) == {'egf': (x + 1)*exp(x), 'lgdegf': (x + 2)/(x + 1)}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def test_guess():
|
| 72 |
+
i0, i1 = symbols('i0 i1')
|
| 73 |
+
assert guess([1, 2, 6, 24, 120], evaluate=False) == [Product(i1 + 1, (i1, 1, i0 - 1))]
|
| 74 |
+
assert guess([1, 2, 6, 24, 120]) == [RisingFactorial(2, i0 - 1)]
|
| 75 |
+
assert guess([1, 2, 7, 42, 429, 7436, 218348, 10850216], niter=4) == [
|
| 76 |
+
2**(i0 - 1)*(Rational(27, 16))**(i0**2/2 - 3*i0/2 +
|
| 77 |
+
1)*Product(RisingFactorial(Rational(5, 3), i1 - 1)*RisingFactorial(Rational(7, 3), i1
|
| 78 |
+
- 1)/(RisingFactorial(Rational(3, 2), i1 - 1)*RisingFactorial(Rational(5, 2), i1 -
|
| 79 |
+
1)), (i1, 1, i0 - 1))]
|
| 80 |
+
assert guess([1, 0, 2]) == []
|
| 81 |
+
x, y = symbols('x y')
|
| 82 |
+
assert guess([1, 2, 6, 24, 120], variables=[x, y]) == [RisingFactorial(2, x - 1)]
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__init__.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""A module that handles matrices.
|
| 2 |
+
|
| 3 |
+
Includes functions for fast creating matrices like zero, one/eye, random
|
| 4 |
+
matrix, etc.
|
| 5 |
+
"""
|
| 6 |
+
from .exceptions import ShapeError, NonSquareMatrixError
|
| 7 |
+
from .kind import MatrixKind
|
| 8 |
+
from .dense import (
|
| 9 |
+
GramSchmidt, casoratian, diag, eye, hessian, jordan_cell,
|
| 10 |
+
list2numpy, matrix2numpy, matrix_multiply_elementwise, ones,
|
| 11 |
+
randMatrix, rot_axis1, rot_axis2, rot_axis3, rot_ccw_axis1,
|
| 12 |
+
rot_ccw_axis2, rot_ccw_axis3, rot_givens,
|
| 13 |
+
symarray, wronskian, zeros)
|
| 14 |
+
from .dense import MutableDenseMatrix
|
| 15 |
+
from .matrixbase import DeferredVector, MatrixBase
|
| 16 |
+
|
| 17 |
+
MutableMatrix = MutableDenseMatrix
|
| 18 |
+
Matrix = MutableMatrix
|
| 19 |
+
|
| 20 |
+
from .sparse import MutableSparseMatrix
|
| 21 |
+
from .sparsetools import banded
|
| 22 |
+
from .immutable import ImmutableDenseMatrix, ImmutableSparseMatrix
|
| 23 |
+
|
| 24 |
+
ImmutableMatrix = ImmutableDenseMatrix
|
| 25 |
+
SparseMatrix = MutableSparseMatrix
|
| 26 |
+
|
| 27 |
+
from .expressions import (
|
| 28 |
+
MatrixSlice, BlockDiagMatrix, BlockMatrix, FunctionMatrix, Identity,
|
| 29 |
+
Inverse, MatAdd, MatMul, MatPow, MatrixExpr, MatrixSymbol, Trace,
|
| 30 |
+
Transpose, ZeroMatrix, OneMatrix, blockcut, block_collapse, matrix_symbols, Adjoint,
|
| 31 |
+
hadamard_product, HadamardProduct, HadamardPower, Determinant, det,
|
| 32 |
+
diagonalize_vector, DiagMatrix, DiagonalMatrix, DiagonalOf, trace,
|
| 33 |
+
DotProduct, kronecker_product, KroneckerProduct,
|
| 34 |
+
PermutationMatrix, MatrixPermute, MatrixSet, Permanent, per)
|
| 35 |
+
|
| 36 |
+
from .utilities import dotprodsimp
|
| 37 |
+
|
| 38 |
+
__all__ = [
|
| 39 |
+
'ShapeError', 'NonSquareMatrixError', 'MatrixKind',
|
| 40 |
+
|
| 41 |
+
'GramSchmidt', 'casoratian', 'diag', 'eye', 'hessian', 'jordan_cell',
|
| 42 |
+
'list2numpy', 'matrix2numpy', 'matrix_multiply_elementwise', 'ones',
|
| 43 |
+
'randMatrix', 'rot_axis1', 'rot_axis2', 'rot_axis3', 'symarray',
|
| 44 |
+
'wronskian', 'zeros', 'rot_ccw_axis1', 'rot_ccw_axis2', 'rot_ccw_axis3',
|
| 45 |
+
'rot_givens',
|
| 46 |
+
|
| 47 |
+
'MutableDenseMatrix',
|
| 48 |
+
|
| 49 |
+
'DeferredVector', 'MatrixBase',
|
| 50 |
+
|
| 51 |
+
'Matrix', 'MutableMatrix',
|
| 52 |
+
|
| 53 |
+
'MutableSparseMatrix',
|
| 54 |
+
|
| 55 |
+
'banded',
|
| 56 |
+
|
| 57 |
+
'ImmutableDenseMatrix', 'ImmutableSparseMatrix',
|
| 58 |
+
|
| 59 |
+
'ImmutableMatrix', 'SparseMatrix',
|
| 60 |
+
|
| 61 |
+
'MatrixSlice', 'BlockDiagMatrix', 'BlockMatrix', 'FunctionMatrix',
|
| 62 |
+
'Identity', 'Inverse', 'MatAdd', 'MatMul', 'MatPow', 'MatrixExpr',
|
| 63 |
+
'MatrixSymbol', 'Trace', 'Transpose', 'ZeroMatrix', 'OneMatrix',
|
| 64 |
+
'blockcut', 'block_collapse', 'matrix_symbols', 'Adjoint',
|
| 65 |
+
'hadamard_product', 'HadamardProduct', 'HadamardPower', 'Determinant',
|
| 66 |
+
'det', 'diagonalize_vector', 'DiagMatrix', 'DiagonalMatrix',
|
| 67 |
+
'DiagonalOf', 'trace', 'DotProduct', 'kronecker_product',
|
| 68 |
+
'KroneckerProduct', 'PermutationMatrix', 'MatrixPermute', 'MatrixSet',
|
| 69 |
+
'Permanent', 'per',
|
| 70 |
+
|
| 71 |
+
'dotprodsimp',
|
| 72 |
+
]
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/decompositions.cpython-311.pyc
ADDED
|
Binary file (56.1 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/dense.cpython-311.pyc
ADDED
|
Binary file (37.9 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/graph.cpython-311.pyc
ADDED
|
Binary file (10.6 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/inverse.cpython-311.pyc
ADDED
|
Binary file (18.7 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/normalforms.cpython-311.pyc
ADDED
|
Binary file (5.79 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/reductions.cpython-311.pyc
ADDED
|
Binary file (15.8 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/sparsetools.cpython-311.pyc
ADDED
|
Binary file (12.2 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/__pycache__/utilities.cpython-311.pyc
ADDED
|
Binary file (3.62 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/dense.py
ADDED
|
@@ -0,0 +1,1093 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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ol import Symbol
|
| 6 |
+
from sympy.core.sympify import sympify
|
| 7 |
+
from sympy.functions.elementary.trigonometric import cos, sin
|
| 8 |
+
from sympy.utilities.decorator import doctest_depends_on
|
| 9 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 10 |
+
from sympy.utilities.iterables import is_sequence
|
| 11 |
+
|
| 12 |
+
from .exceptions import ShapeError
|
| 13 |
+
from .decompositions import _cholesky, _LDLdecomposition
|
| 14 |
+
from .matrixbase import MatrixBase
|
| 15 |
+
from .repmatrix import MutableRepMatrix, RepMatrix
|
| 16 |
+
from .solvers import _lower_triangular_solve, _upper_triangular_solve
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
__doctest_requires__ = {('symarray',): ['numpy']}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _iszero(x):
|
| 23 |
+
"""Returns True if x is zero."""
|
| 24 |
+
return x.is_zero
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class DenseMatrix(RepMatrix):
|
| 28 |
+
"""Matrix implementation based on DomainMatrix as the internal representation"""
|
| 29 |
+
|
| 30 |
+
#
|
| 31 |
+
# DenseMatrix is a superclass for both MutableDenseMatrix and
|
| 32 |
+
# ImmutableDenseMatrix. Methods shared by both classes but not for the
|
| 33 |
+
# Sparse classes should be implemented here.
|
| 34 |
+
#
|
| 35 |
+
|
| 36 |
+
is_MatrixExpr = False # type: bool
|
| 37 |
+
|
| 38 |
+
_op_priority = 10.01
|
| 39 |
+
_class_priority = 4
|
| 40 |
+
|
| 41 |
+
@property
|
| 42 |
+
def _mat(self):
|
| 43 |
+
sympy_deprecation_warning(
|
| 44 |
+
"""
|
| 45 |
+
The private _mat attribute of Matrix is deprecated. Use the
|
| 46 |
+
.flat() method instead.
|
| 47 |
+
""",
|
| 48 |
+
deprecated_since_version="1.9",
|
| 49 |
+
active_deprecations_target="deprecated-private-matrix-attributes"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
return self.flat()
|
| 53 |
+
|
| 54 |
+
def _eval_inverse(self, **kwargs):
|
| 55 |
+
return self.inv(method=kwargs.get('method', 'GE'),
|
| 56 |
+
iszerofunc=kwargs.get('iszerofunc', _iszero),
|
| 57 |
+
try_block_diag=kwargs.get('try_block_diag', False))
|
| 58 |
+
|
| 59 |
+
def as_immutable(self):
|
| 60 |
+
"""Returns an Immutable version of this Matrix
|
| 61 |
+
"""
|
| 62 |
+
from .immutable import ImmutableDenseMatrix as cls
|
| 63 |
+
return cls._fromrep(self._rep.copy())
|
| 64 |
+
|
| 65 |
+
def as_mutable(self):
|
| 66 |
+
"""Returns a mutable version of this matrix
|
| 67 |
+
|
| 68 |
+
Examples
|
| 69 |
+
========
|
| 70 |
+
|
| 71 |
+
>>> from sympy import ImmutableMatrix
|
| 72 |
+
>>> X = ImmutableMatrix([[1, 2], [3, 4]])
|
| 73 |
+
>>> Y = X.as_mutable()
|
| 74 |
+
>>> Y[1, 1] = 5 # Can set values in Y
|
| 75 |
+
>>> Y
|
| 76 |
+
Matrix([
|
| 77 |
+
[1, 2],
|
| 78 |
+
[3, 5]])
|
| 79 |
+
"""
|
| 80 |
+
return Matrix(self)
|
| 81 |
+
|
| 82 |
+
def cholesky(self, hermitian=True):
|
| 83 |
+
return _cholesky(self, hermitian=hermitian)
|
| 84 |
+
|
| 85 |
+
def LDLdecomposition(self, hermitian=True):
|
| 86 |
+
return _LDLdecomposition(self, hermitian=hermitian)
|
| 87 |
+
|
| 88 |
+
def lower_triangular_solve(self, rhs):
|
| 89 |
+
return _lower_triangular_solve(self, rhs)
|
| 90 |
+
|
| 91 |
+
def upper_triangular_solve(self, rhs):
|
| 92 |
+
return _upper_triangular_solve(self, rhs)
|
| 93 |
+
|
| 94 |
+
cholesky.__doc__ = _cholesky.__doc__
|
| 95 |
+
LDLdecomposition.__doc__ = _LDLdecomposition.__doc__
|
| 96 |
+
lower_triangular_solve.__doc__ = _lower_triangular_solve.__doc__
|
| 97 |
+
upper_triangular_solve.__doc__ = _upper_triangular_solve.__doc__
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def _force_mutable(x):
|
| 101 |
+
"""Return a matrix as a Matrix, otherwise return x."""
|
| 102 |
+
if getattr(x, 'is_Matrix', False):
|
| 103 |
+
return x.as_mutable()
|
| 104 |
+
elif isinstance(x, Basic):
|
| 105 |
+
return x
|
| 106 |
+
elif hasattr(x, '__array__'):
|
| 107 |
+
a = x.__array__()
|
| 108 |
+
if len(a.shape) == 0:
|
| 109 |
+
return sympify(a)
|
| 110 |
+
return Matrix(x)
|
| 111 |
+
return x
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
class MutableDenseMatrix(DenseMatrix, MutableRepMatrix):
|
| 115 |
+
|
| 116 |
+
def simplify(self, **kwargs):
|
| 117 |
+
"""Applies simplify to the elements of a matrix in place.
|
| 118 |
+
|
| 119 |
+
This is a shortcut for M.applyfunc(lambda x: simplify(x, ratio, measure))
|
| 120 |
+
|
| 121 |
+
See Also
|
| 122 |
+
========
|
| 123 |
+
|
| 124 |
+
sympy.simplify.simplify.simplify
|
| 125 |
+
"""
|
| 126 |
+
from sympy.simplify.simplify import simplify as _simplify
|
| 127 |
+
for (i, j), element in self.todok().items():
|
| 128 |
+
self[i, j] = _simplify(element, **kwargs)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
MutableMatrix = Matrix = MutableDenseMatrix
|
| 132 |
+
|
| 133 |
+
###########
|
| 134 |
+
# Numpy Utility Functions:
|
| 135 |
+
# list2numpy, matrix2numpy, symmarray
|
| 136 |
+
###########
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def list2numpy(l, dtype=object): # pragma: no cover
|
| 140 |
+
"""Converts Python list of SymPy expressions to a NumPy array.
|
| 141 |
+
|
| 142 |
+
See Also
|
| 143 |
+
========
|
| 144 |
+
|
| 145 |
+
matrix2numpy
|
| 146 |
+
"""
|
| 147 |
+
from numpy import empty
|
| 148 |
+
a = empty(len(l), dtype)
|
| 149 |
+
for i, s in enumerate(l):
|
| 150 |
+
a[i] = s
|
| 151 |
+
return a
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def matrix2numpy(m, dtype=object): # pragma: no cover
|
| 155 |
+
"""Converts SymPy's matrix to a NumPy array.
|
| 156 |
+
|
| 157 |
+
See Also
|
| 158 |
+
========
|
| 159 |
+
|
| 160 |
+
list2numpy
|
| 161 |
+
"""
|
| 162 |
+
from numpy import empty
|
| 163 |
+
a = empty(m.shape, dtype)
|
| 164 |
+
for i in range(m.rows):
|
| 165 |
+
for j in range(m.cols):
|
| 166 |
+
a[i, j] = m[i, j]
|
| 167 |
+
return a
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
###########
|
| 171 |
+
# Rotation matrices:
|
| 172 |
+
# rot_givens, rot_axis[123], rot_ccw_axis[123]
|
| 173 |
+
###########
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def rot_givens(i, j, theta, dim=3):
|
| 177 |
+
r"""Returns a a Givens rotation matrix, a a rotation in the
|
| 178 |
+
plane spanned by two coordinates axes.
|
| 179 |
+
|
| 180 |
+
Explanation
|
| 181 |
+
===========
|
| 182 |
+
|
| 183 |
+
The Givens rotation corresponds to a generalization of rotation
|
| 184 |
+
matrices to any number of dimensions, given by:
|
| 185 |
+
|
| 186 |
+
.. math::
|
| 187 |
+
G(i, j, \theta) =
|
| 188 |
+
\begin{bmatrix}
|
| 189 |
+
1 & \cdots & 0 & \cdots & 0 & \cdots & 0 \\
|
| 190 |
+
\vdots & \ddots & \vdots & & \vdots & & \vdots \\
|
| 191 |
+
0 & \cdots & c & \cdots & -s & \cdots & 0 \\
|
| 192 |
+
\vdots & & \vdots & \ddots & \vdots & & \vdots \\
|
| 193 |
+
0 & \cdots & s & \cdots & c & \cdots & 0 \\
|
| 194 |
+
\vdots & & \vdots & & \vdots & \ddots & \vdots \\
|
| 195 |
+
0 & \cdots & 0 & \cdots & 0 & \cdots & 1
|
| 196 |
+
\end{bmatrix}
|
| 197 |
+
|
| 198 |
+
Where $c = \cos(\theta)$ and $s = \sin(\theta)$ appear at the intersections
|
| 199 |
+
``i``\th and ``j``\th rows and columns.
|
| 200 |
+
|
| 201 |
+
For fixed ``i > j``\, the non-zero elements of a Givens matrix are
|
| 202 |
+
given by:
|
| 203 |
+
|
| 204 |
+
- $g_{kk} = 1$ for $k \ne i,\,j$
|
| 205 |
+
- $g_{kk} = c$ for $k = i,\,j$
|
| 206 |
+
- $g_{ji} = -g_{ij} = -s$
|
| 207 |
+
|
| 208 |
+
Parameters
|
| 209 |
+
==========
|
| 210 |
+
|
| 211 |
+
i : int between ``0`` and ``dim - 1``
|
| 212 |
+
Represents first axis
|
| 213 |
+
j : int between ``0`` and ``dim - 1``
|
| 214 |
+
Represents second axis
|
| 215 |
+
dim : int bigger than 1
|
| 216 |
+
Number of dimentions. Defaults to 3.
|
| 217 |
+
|
| 218 |
+
Examples
|
| 219 |
+
========
|
| 220 |
+
|
| 221 |
+
>>> from sympy import pi, rot_givens
|
| 222 |
+
|
| 223 |
+
A counterclockwise rotation of pi/3 (60 degrees) around
|
| 224 |
+
the third axis (z-axis):
|
| 225 |
+
|
| 226 |
+
>>> rot_givens(1, 0, pi/3)
|
| 227 |
+
Matrix([
|
| 228 |
+
[ 1/2, -sqrt(3)/2, 0],
|
| 229 |
+
[sqrt(3)/2, 1/2, 0],
|
| 230 |
+
[ 0, 0, 1]])
|
| 231 |
+
|
| 232 |
+
If we rotate by pi/2 (90 degrees):
|
| 233 |
+
|
| 234 |
+
>>> rot_givens(1, 0, pi/2)
|
| 235 |
+
Matrix([
|
| 236 |
+
[0, -1, 0],
|
| 237 |
+
[1, 0, 0],
|
| 238 |
+
[0, 0, 1]])
|
| 239 |
+
|
| 240 |
+
This can be generalized to any number
|
| 241 |
+
of dimensions:
|
| 242 |
+
|
| 243 |
+
>>> rot_givens(1, 0, pi/2, dim=4)
|
| 244 |
+
Matrix([
|
| 245 |
+
[0, -1, 0, 0],
|
| 246 |
+
[1, 0, 0, 0],
|
| 247 |
+
[0, 0, 1, 0],
|
| 248 |
+
[0, 0, 0, 1]])
|
| 249 |
+
|
| 250 |
+
References
|
| 251 |
+
==========
|
| 252 |
+
|
| 253 |
+
.. [1] https://en.wikipedia.org/wiki/Givens_rotation
|
| 254 |
+
|
| 255 |
+
See Also
|
| 256 |
+
========
|
| 257 |
+
|
| 258 |
+
rot_axis1: Returns a rotation matrix for a rotation of theta (in radians)
|
| 259 |
+
about the 1-axis (clockwise around the x axis)
|
| 260 |
+
rot_axis2: Returns a rotation matrix for a rotation of theta (in radians)
|
| 261 |
+
about the 2-axis (clockwise around the y axis)
|
| 262 |
+
rot_axis3: Returns a rotation matrix for a rotation of theta (in radians)
|
| 263 |
+
about the 3-axis (clockwise around the z axis)
|
| 264 |
+
rot_ccw_axis1: Returns a rotation matrix for a rotation of theta (in radians)
|
| 265 |
+
about the 1-axis (counterclockwise around the x axis)
|
| 266 |
+
rot_ccw_axis2: Returns a rotation matrix for a rotation of theta (in radians)
|
| 267 |
+
about the 2-axis (counterclockwise around the y axis)
|
| 268 |
+
rot_ccw_axis3: Returns a rotation matrix for a rotation of theta (in radians)
|
| 269 |
+
about the 3-axis (counterclockwise around the z axis)
|
| 270 |
+
"""
|
| 271 |
+
if not isinstance(dim, int) or dim < 2:
|
| 272 |
+
raise ValueError('dim must be an integer biggen than one, '
|
| 273 |
+
'got {}.'.format(dim))
|
| 274 |
+
|
| 275 |
+
if i == j:
|
| 276 |
+
raise ValueError('i and j must be different, '
|
| 277 |
+
'got ({}, {})'.format(i, j))
|
| 278 |
+
|
| 279 |
+
for ij in [i, j]:
|
| 280 |
+
if not isinstance(ij, int) or ij < 0 or ij > dim - 1:
|
| 281 |
+
raise ValueError('i and j must be integers between 0 and '
|
| 282 |
+
'{}, got i={} and j={}.'.format(dim-1, i, j))
|
| 283 |
+
|
| 284 |
+
theta = sympify(theta)
|
| 285 |
+
c = cos(theta)
|
| 286 |
+
s = sin(theta)
|
| 287 |
+
M = eye(dim)
|
| 288 |
+
M[i, i] = c
|
| 289 |
+
M[j, j] = c
|
| 290 |
+
M[i, j] = s
|
| 291 |
+
M[j, i] = -s
|
| 292 |
+
return M
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
def rot_axis3(theta):
|
| 296 |
+
r"""Returns a rotation matrix for a rotation of theta (in radians)
|
| 297 |
+
about the 3-axis.
|
| 298 |
+
|
| 299 |
+
Explanation
|
| 300 |
+
===========
|
| 301 |
+
|
| 302 |
+
For a right-handed coordinate system, this corresponds to a
|
| 303 |
+
clockwise rotation around the `z`-axis, given by:
|
| 304 |
+
|
| 305 |
+
.. math::
|
| 306 |
+
|
| 307 |
+
R = \begin{bmatrix}
|
| 308 |
+
\cos(\theta) & \sin(\theta) & 0 \\
|
| 309 |
+
-\sin(\theta) & \cos(\theta) & 0 \\
|
| 310 |
+
0 & 0 & 1
|
| 311 |
+
\end{bmatrix}
|
| 312 |
+
|
| 313 |
+
Examples
|
| 314 |
+
========
|
| 315 |
+
|
| 316 |
+
>>> from sympy import pi, rot_axis3
|
| 317 |
+
|
| 318 |
+
A rotation of pi/3 (60 degrees):
|
| 319 |
+
|
| 320 |
+
>>> theta = pi/3
|
| 321 |
+
>>> rot_axis3(theta)
|
| 322 |
+
Matrix([
|
| 323 |
+
[ 1/2, sqrt(3)/2, 0],
|
| 324 |
+
[-sqrt(3)/2, 1/2, 0],
|
| 325 |
+
[ 0, 0, 1]])
|
| 326 |
+
|
| 327 |
+
If we rotate by pi/2 (90 degrees):
|
| 328 |
+
|
| 329 |
+
>>> rot_axis3(pi/2)
|
| 330 |
+
Matrix([
|
| 331 |
+
[ 0, 1, 0],
|
| 332 |
+
[-1, 0, 0],
|
| 333 |
+
[ 0, 0, 1]])
|
| 334 |
+
|
| 335 |
+
See Also
|
| 336 |
+
========
|
| 337 |
+
|
| 338 |
+
rot_givens: Returns a Givens rotation matrix (generalized rotation for
|
| 339 |
+
any number of dimensions)
|
| 340 |
+
rot_ccw_axis3: Returns a rotation matrix for a rotation of theta (in radians)
|
| 341 |
+
about the 3-axis (counterclockwise around the z axis)
|
| 342 |
+
rot_axis1: Returns a rotation matrix for a rotation of theta (in radians)
|
| 343 |
+
about the 1-axis (clockwise around the x axis)
|
| 344 |
+
rot_axis2: Returns a rotation matrix for a rotation of theta (in radians)
|
| 345 |
+
about the 2-axis (clockwise around the y axis)
|
| 346 |
+
"""
|
| 347 |
+
return rot_givens(0, 1, theta, dim=3)
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
def rot_axis2(theta):
|
| 351 |
+
r"""Returns a rotation matrix for a rotation of theta (in radians)
|
| 352 |
+
about the 2-axis.
|
| 353 |
+
|
| 354 |
+
Explanation
|
| 355 |
+
===========
|
| 356 |
+
|
| 357 |
+
For a right-handed coordinate system, this corresponds to a
|
| 358 |
+
clockwise rotation around the `y`-axis, given by:
|
| 359 |
+
|
| 360 |
+
.. math::
|
| 361 |
+
|
| 362 |
+
R = \begin{bmatrix}
|
| 363 |
+
\cos(\theta) & 0 & -\sin(\theta) \\
|
| 364 |
+
0 & 1 & 0 \\
|
| 365 |
+
\sin(\theta) & 0 & \cos(\theta)
|
| 366 |
+
\end{bmatrix}
|
| 367 |
+
|
| 368 |
+
Examples
|
| 369 |
+
========
|
| 370 |
+
|
| 371 |
+
>>> from sympy import pi, rot_axis2
|
| 372 |
+
|
| 373 |
+
A rotation of pi/3 (60 degrees):
|
| 374 |
+
|
| 375 |
+
>>> theta = pi/3
|
| 376 |
+
>>> rot_axis2(theta)
|
| 377 |
+
Matrix([
|
| 378 |
+
[ 1/2, 0, -sqrt(3)/2],
|
| 379 |
+
[ 0, 1, 0],
|
| 380 |
+
[sqrt(3)/2, 0, 1/2]])
|
| 381 |
+
|
| 382 |
+
If we rotate by pi/2 (90 degrees):
|
| 383 |
+
|
| 384 |
+
>>> rot_axis2(pi/2)
|
| 385 |
+
Matrix([
|
| 386 |
+
[0, 0, -1],
|
| 387 |
+
[0, 1, 0],
|
| 388 |
+
[1, 0, 0]])
|
| 389 |
+
|
| 390 |
+
See Also
|
| 391 |
+
========
|
| 392 |
+
|
| 393 |
+
rot_givens: Returns a Givens rotation matrix (generalized rotation for
|
| 394 |
+
any number of dimensions)
|
| 395 |
+
rot_ccw_axis2: Returns a rotation matrix for a rotation of theta (in radians)
|
| 396 |
+
about the 2-axis (clockwise around the y axis)
|
| 397 |
+
rot_axis1: Returns a rotation matrix for a rotation of theta (in radians)
|
| 398 |
+
about the 1-axis (counterclockwise around the x axis)
|
| 399 |
+
rot_axis3: Returns a rotation matrix for a rotation of theta (in radians)
|
| 400 |
+
about the 3-axis (counterclockwise around the z axis)
|
| 401 |
+
"""
|
| 402 |
+
return rot_givens(2, 0, theta, dim=3)
|
| 403 |
+
|
| 404 |
+
|
| 405 |
+
def rot_axis1(theta):
|
| 406 |
+
r"""Returns a rotation matrix for a rotation of theta (in radians)
|
| 407 |
+
about the 1-axis.
|
| 408 |
+
|
| 409 |
+
Explanation
|
| 410 |
+
===========
|
| 411 |
+
|
| 412 |
+
For a right-handed coordinate system, this corresponds to a
|
| 413 |
+
clockwise rotation around the `x`-axis, given by:
|
| 414 |
+
|
| 415 |
+
.. math::
|
| 416 |
+
|
| 417 |
+
R = \begin{bmatrix}
|
| 418 |
+
1 & 0 & 0 \\
|
| 419 |
+
0 & \cos(\theta) & \sin(\theta) \\
|
| 420 |
+
0 & -\sin(\theta) & \cos(\theta)
|
| 421 |
+
\end{bmatrix}
|
| 422 |
+
|
| 423 |
+
Examples
|
| 424 |
+
========
|
| 425 |
+
|
| 426 |
+
>>> from sympy import pi, rot_axis1
|
| 427 |
+
|
| 428 |
+
A rotation of pi/3 (60 degrees):
|
| 429 |
+
|
| 430 |
+
>>> theta = pi/3
|
| 431 |
+
>>> rot_axis1(theta)
|
| 432 |
+
Matrix([
|
| 433 |
+
[1, 0, 0],
|
| 434 |
+
[0, 1/2, sqrt(3)/2],
|
| 435 |
+
[0, -sqrt(3)/2, 1/2]])
|
| 436 |
+
|
| 437 |
+
If we rotate by pi/2 (90 degrees):
|
| 438 |
+
|
| 439 |
+
>>> rot_axis1(pi/2)
|
| 440 |
+
Matrix([
|
| 441 |
+
[1, 0, 0],
|
| 442 |
+
[0, 0, 1],
|
| 443 |
+
[0, -1, 0]])
|
| 444 |
+
|
| 445 |
+
See Also
|
| 446 |
+
========
|
| 447 |
+
|
| 448 |
+
rot_givens: Returns a Givens rotation matrix (generalized rotation for
|
| 449 |
+
any number of dimensions)
|
| 450 |
+
rot_ccw_axis1: Returns a rotation matrix for a rotation of theta (in radians)
|
| 451 |
+
about the 1-axis (counterclockwise around the x axis)
|
| 452 |
+
rot_axis2: Returns a rotation matrix for a rotation of theta (in radians)
|
| 453 |
+
about the 2-axis (clockwise around the y axis)
|
| 454 |
+
rot_axis3: Returns a rotation matrix for a rotation of theta (in radians)
|
| 455 |
+
about the 3-axis (clockwise around the z axis)
|
| 456 |
+
"""
|
| 457 |
+
return rot_givens(1, 2, theta, dim=3)
|
| 458 |
+
|
| 459 |
+
|
| 460 |
+
def rot_ccw_axis3(theta):
|
| 461 |
+
r"""Returns a rotation matrix for a rotation of theta (in radians)
|
| 462 |
+
about the 3-axis.
|
| 463 |
+
|
| 464 |
+
Explanation
|
| 465 |
+
===========
|
| 466 |
+
|
| 467 |
+
For a right-handed coordinate system, this corresponds to a
|
| 468 |
+
counterclockwise rotation around the `z`-axis, given by:
|
| 469 |
+
|
| 470 |
+
.. math::
|
| 471 |
+
|
| 472 |
+
R = \begin{bmatrix}
|
| 473 |
+
\cos(\theta) & -\sin(\theta) & 0 \\
|
| 474 |
+
\sin(\theta) & \cos(\theta) & 0 \\
|
| 475 |
+
0 & 0 & 1
|
| 476 |
+
\end{bmatrix}
|
| 477 |
+
|
| 478 |
+
Examples
|
| 479 |
+
========
|
| 480 |
+
|
| 481 |
+
>>> from sympy import pi, rot_ccw_axis3
|
| 482 |
+
|
| 483 |
+
A rotation of pi/3 (60 degrees):
|
| 484 |
+
|
| 485 |
+
>>> theta = pi/3
|
| 486 |
+
>>> rot_ccw_axis3(theta)
|
| 487 |
+
Matrix([
|
| 488 |
+
[ 1/2, -sqrt(3)/2, 0],
|
| 489 |
+
[sqrt(3)/2, 1/2, 0],
|
| 490 |
+
[ 0, 0, 1]])
|
| 491 |
+
|
| 492 |
+
If we rotate by pi/2 (90 degrees):
|
| 493 |
+
|
| 494 |
+
>>> rot_ccw_axis3(pi/2)
|
| 495 |
+
Matrix([
|
| 496 |
+
[0, -1, 0],
|
| 497 |
+
[1, 0, 0],
|
| 498 |
+
[0, 0, 1]])
|
| 499 |
+
|
| 500 |
+
See Also
|
| 501 |
+
========
|
| 502 |
+
|
| 503 |
+
rot_givens: Returns a Givens rotation matrix (generalized rotation for
|
| 504 |
+
any number of dimensions)
|
| 505 |
+
rot_axis3: Returns a rotation matrix for a rotation of theta (in radians)
|
| 506 |
+
about the 3-axis (clockwise around the z axis)
|
| 507 |
+
rot_ccw_axis1: Returns a rotation matrix for a rotation of theta (in radians)
|
| 508 |
+
about the 1-axis (counterclockwise around the x axis)
|
| 509 |
+
rot_ccw_axis2: Returns a rotation matrix for a rotation of theta (in radians)
|
| 510 |
+
about the 2-axis (counterclockwise around the y axis)
|
| 511 |
+
"""
|
| 512 |
+
return rot_givens(1, 0, theta, dim=3)
|
| 513 |
+
|
| 514 |
+
|
| 515 |
+
def rot_ccw_axis2(theta):
|
| 516 |
+
r"""Returns a rotation matrix for a rotation of theta (in radians)
|
| 517 |
+
about the 2-axis.
|
| 518 |
+
|
| 519 |
+
Explanation
|
| 520 |
+
===========
|
| 521 |
+
|
| 522 |
+
For a right-handed coordinate system, this corresponds to a
|
| 523 |
+
counterclockwise rotation around the `y`-axis, given by:
|
| 524 |
+
|
| 525 |
+
.. math::
|
| 526 |
+
|
| 527 |
+
R = \begin{bmatrix}
|
| 528 |
+
\cos(\theta) & 0 & \sin(\theta) \\
|
| 529 |
+
0 & 1 & 0 \\
|
| 530 |
+
-\sin(\theta) & 0 & \cos(\theta)
|
| 531 |
+
\end{bmatrix}
|
| 532 |
+
|
| 533 |
+
Examples
|
| 534 |
+
========
|
| 535 |
+
|
| 536 |
+
>>> from sympy import pi, rot_ccw_axis2
|
| 537 |
+
|
| 538 |
+
A rotation of pi/3 (60 degrees):
|
| 539 |
+
|
| 540 |
+
>>> theta = pi/3
|
| 541 |
+
>>> rot_ccw_axis2(theta)
|
| 542 |
+
Matrix([
|
| 543 |
+
[ 1/2, 0, sqrt(3)/2],
|
| 544 |
+
[ 0, 1, 0],
|
| 545 |
+
[-sqrt(3)/2, 0, 1/2]])
|
| 546 |
+
|
| 547 |
+
If we rotate by pi/2 (90 degrees):
|
| 548 |
+
|
| 549 |
+
>>> rot_ccw_axis2(pi/2)
|
| 550 |
+
Matrix([
|
| 551 |
+
[ 0, 0, 1],
|
| 552 |
+
[ 0, 1, 0],
|
| 553 |
+
[-1, 0, 0]])
|
| 554 |
+
|
| 555 |
+
See Also
|
| 556 |
+
========
|
| 557 |
+
|
| 558 |
+
rot_givens: Returns a Givens rotation matrix (generalized rotation for
|
| 559 |
+
any number of dimensions)
|
| 560 |
+
rot_axis2: Returns a rotation matrix for a rotation of theta (in radians)
|
| 561 |
+
about the 2-axis (clockwise around the y axis)
|
| 562 |
+
rot_ccw_axis1: Returns a rotation matrix for a rotation of theta (in radians)
|
| 563 |
+
about the 1-axis (counterclockwise around the x axis)
|
| 564 |
+
rot_ccw_axis3: Returns a rotation matrix for a rotation of theta (in radians)
|
| 565 |
+
about the 3-axis (counterclockwise around the z axis)
|
| 566 |
+
"""
|
| 567 |
+
return rot_givens(0, 2, theta, dim=3)
|
| 568 |
+
|
| 569 |
+
|
| 570 |
+
def rot_ccw_axis1(theta):
|
| 571 |
+
r"""Returns a rotation matrix for a rotation of theta (in radians)
|
| 572 |
+
about the 1-axis.
|
| 573 |
+
|
| 574 |
+
Explanation
|
| 575 |
+
===========
|
| 576 |
+
|
| 577 |
+
For a right-handed coordinate system, this corresponds to a
|
| 578 |
+
counterclockwise rotation around the `x`-axis, given by:
|
| 579 |
+
|
| 580 |
+
.. math::
|
| 581 |
+
|
| 582 |
+
R = \begin{bmatrix}
|
| 583 |
+
1 & 0 & 0 \\
|
| 584 |
+
0 & \cos(\theta) & -\sin(\theta) \\
|
| 585 |
+
0 & \sin(\theta) & \cos(\theta)
|
| 586 |
+
\end{bmatrix}
|
| 587 |
+
|
| 588 |
+
Examples
|
| 589 |
+
========
|
| 590 |
+
|
| 591 |
+
>>> from sympy import pi, rot_ccw_axis1
|
| 592 |
+
|
| 593 |
+
A rotation of pi/3 (60 degrees):
|
| 594 |
+
|
| 595 |
+
>>> theta = pi/3
|
| 596 |
+
>>> rot_ccw_axis1(theta)
|
| 597 |
+
Matrix([
|
| 598 |
+
[1, 0, 0],
|
| 599 |
+
[0, 1/2, -sqrt(3)/2],
|
| 600 |
+
[0, sqrt(3)/2, 1/2]])
|
| 601 |
+
|
| 602 |
+
If we rotate by pi/2 (90 degrees):
|
| 603 |
+
|
| 604 |
+
>>> rot_ccw_axis1(pi/2)
|
| 605 |
+
Matrix([
|
| 606 |
+
[1, 0, 0],
|
| 607 |
+
[0, 0, -1],
|
| 608 |
+
[0, 1, 0]])
|
| 609 |
+
|
| 610 |
+
See Also
|
| 611 |
+
========
|
| 612 |
+
|
| 613 |
+
rot_givens: Returns a Givens rotation matrix (generalized rotation for
|
| 614 |
+
any number of dimensions)
|
| 615 |
+
rot_axis1: Returns a rotation matrix for a rotation of theta (in radians)
|
| 616 |
+
about the 1-axis (clockwise around the x axis)
|
| 617 |
+
rot_ccw_axis2: Returns a rotation matrix for a rotation of theta (in radians)
|
| 618 |
+
about the 2-axis (counterclockwise around the y axis)
|
| 619 |
+
rot_ccw_axis3: Returns a rotation matrix for a rotation of theta (in radians)
|
| 620 |
+
about the 3-axis (counterclockwise around the z axis)
|
| 621 |
+
"""
|
| 622 |
+
return rot_givens(2, 1, theta, dim=3)
|
| 623 |
+
|
| 624 |
+
|
| 625 |
+
@doctest_depends_on(modules=('numpy',))
|
| 626 |
+
def symarray(prefix, shape, **kwargs): # pragma: no cover
|
| 627 |
+
r"""Create a numpy ndarray of symbols (as an object array).
|
| 628 |
+
|
| 629 |
+
The created symbols are named ``prefix_i1_i2_``... You should thus provide a
|
| 630 |
+
non-empty prefix if you want your symbols to be unique for different output
|
| 631 |
+
arrays, as SymPy symbols with identical names are the same object.
|
| 632 |
+
|
| 633 |
+
Parameters
|
| 634 |
+
----------
|
| 635 |
+
|
| 636 |
+
prefix : string
|
| 637 |
+
A prefix prepended to the name of every symbol.
|
| 638 |
+
|
| 639 |
+
shape : int or tuple
|
| 640 |
+
Shape of the created array. If an int, the array is one-dimensional; for
|
| 641 |
+
more than one dimension the shape must be a tuple.
|
| 642 |
+
|
| 643 |
+
\*\*kwargs : dict
|
| 644 |
+
keyword arguments passed on to Symbol
|
| 645 |
+
|
| 646 |
+
Examples
|
| 647 |
+
========
|
| 648 |
+
These doctests require numpy.
|
| 649 |
+
|
| 650 |
+
>>> from sympy import symarray
|
| 651 |
+
>>> symarray('', 3)
|
| 652 |
+
[_0 _1 _2]
|
| 653 |
+
|
| 654 |
+
If you want multiple symarrays to contain distinct symbols, you *must*
|
| 655 |
+
provide unique prefixes:
|
| 656 |
+
|
| 657 |
+
>>> a = symarray('', 3)
|
| 658 |
+
>>> b = symarray('', 3)
|
| 659 |
+
>>> a[0] == b[0]
|
| 660 |
+
True
|
| 661 |
+
>>> a = symarray('a', 3)
|
| 662 |
+
>>> b = symarray('b', 3)
|
| 663 |
+
>>> a[0] == b[0]
|
| 664 |
+
False
|
| 665 |
+
|
| 666 |
+
Creating symarrays with a prefix:
|
| 667 |
+
|
| 668 |
+
>>> symarray('a', 3)
|
| 669 |
+
[a_0 a_1 a_2]
|
| 670 |
+
|
| 671 |
+
For more than one dimension, the shape must be given as a tuple:
|
| 672 |
+
|
| 673 |
+
>>> symarray('a', (2, 3))
|
| 674 |
+
[[a_0_0 a_0_1 a_0_2]
|
| 675 |
+
[a_1_0 a_1_1 a_1_2]]
|
| 676 |
+
>>> symarray('a', (2, 3, 2))
|
| 677 |
+
[[[a_0_0_0 a_0_0_1]
|
| 678 |
+
[a_0_1_0 a_0_1_1]
|
| 679 |
+
[a_0_2_0 a_0_2_1]]
|
| 680 |
+
<BLANKLINE>
|
| 681 |
+
[[a_1_0_0 a_1_0_1]
|
| 682 |
+
[a_1_1_0 a_1_1_1]
|
| 683 |
+
[a_1_2_0 a_1_2_1]]]
|
| 684 |
+
|
| 685 |
+
For setting assumptions of the underlying Symbols:
|
| 686 |
+
|
| 687 |
+
>>> [s.is_real for s in symarray('a', 2, real=True)]
|
| 688 |
+
[True, True]
|
| 689 |
+
"""
|
| 690 |
+
from numpy import empty, ndindex
|
| 691 |
+
arr = empty(shape, dtype=object)
|
| 692 |
+
for index in ndindex(shape):
|
| 693 |
+
arr[index] = Symbol('%s_%s' % (prefix, '_'.join(map(str, index))),
|
| 694 |
+
**kwargs)
|
| 695 |
+
return arr
|
| 696 |
+
|
| 697 |
+
|
| 698 |
+
###############
|
| 699 |
+
# Functions
|
| 700 |
+
###############
|
| 701 |
+
|
| 702 |
+
def casoratian(seqs, n, zero=True):
|
| 703 |
+
"""Given linear difference operator L of order 'k' and homogeneous
|
| 704 |
+
equation Ly = 0 we want to compute kernel of L, which is a set
|
| 705 |
+
of 'k' sequences: a(n), b(n), ... z(n).
|
| 706 |
+
|
| 707 |
+
Solutions of L are linearly independent iff their Casoratian,
|
| 708 |
+
denoted as C(a, b, ..., z), do not vanish for n = 0.
|
| 709 |
+
|
| 710 |
+
Casoratian is defined by k x k determinant::
|
| 711 |
+
|
| 712 |
+
+ a(n) b(n) . . . z(n) +
|
| 713 |
+
| a(n+1) b(n+1) . . . z(n+1) |
|
| 714 |
+
| . . . . |
|
| 715 |
+
| . . . . |
|
| 716 |
+
| . . . . |
|
| 717 |
+
+ a(n+k-1) b(n+k-1) . . . z(n+k-1) +
|
| 718 |
+
|
| 719 |
+
It proves very useful in rsolve_hyper() where it is applied
|
| 720 |
+
to a generating set of a recurrence to factor out linearly
|
| 721 |
+
dependent solutions and return a basis:
|
| 722 |
+
|
| 723 |
+
>>> from sympy import Symbol, casoratian, factorial
|
| 724 |
+
>>> n = Symbol('n', integer=True)
|
| 725 |
+
|
| 726 |
+
Exponential and factorial are linearly independent:
|
| 727 |
+
|
| 728 |
+
>>> casoratian([2**n, factorial(n)], n) != 0
|
| 729 |
+
True
|
| 730 |
+
|
| 731 |
+
"""
|
| 732 |
+
|
| 733 |
+
seqs = list(map(sympify, seqs))
|
| 734 |
+
|
| 735 |
+
if not zero:
|
| 736 |
+
f = lambda i, j: seqs[j].subs(n, n + i)
|
| 737 |
+
else:
|
| 738 |
+
f = lambda i, j: seqs[j].subs(n, i)
|
| 739 |
+
|
| 740 |
+
k = len(seqs)
|
| 741 |
+
|
| 742 |
+
return Matrix(k, k, f).det()
|
| 743 |
+
|
| 744 |
+
|
| 745 |
+
def eye(*args, **kwargs):
|
| 746 |
+
"""Create square identity matrix n x n
|
| 747 |
+
|
| 748 |
+
See Also
|
| 749 |
+
========
|
| 750 |
+
|
| 751 |
+
diag
|
| 752 |
+
zeros
|
| 753 |
+
ones
|
| 754 |
+
"""
|
| 755 |
+
|
| 756 |
+
return Matrix.eye(*args, **kwargs)
|
| 757 |
+
|
| 758 |
+
|
| 759 |
+
def diag(*values, strict=True, unpack=False, **kwargs):
|
| 760 |
+
"""Returns a matrix with the provided values placed on the
|
| 761 |
+
diagonal. If non-square matrices are included, they will
|
| 762 |
+
produce a block-diagonal matrix.
|
| 763 |
+
|
| 764 |
+
Examples
|
| 765 |
+
========
|
| 766 |
+
|
| 767 |
+
This version of diag is a thin wrapper to Matrix.diag that differs
|
| 768 |
+
in that it treats all lists like matrices -- even when a single list
|
| 769 |
+
is given. If this is not desired, either put a `*` before the list or
|
| 770 |
+
set `unpack=True`.
|
| 771 |
+
|
| 772 |
+
>>> from sympy import diag
|
| 773 |
+
|
| 774 |
+
>>> diag([1, 2, 3], unpack=True) # = diag(1,2,3) or diag(*[1,2,3])
|
| 775 |
+
Matrix([
|
| 776 |
+
[1, 0, 0],
|
| 777 |
+
[0, 2, 0],
|
| 778 |
+
[0, 0, 3]])
|
| 779 |
+
|
| 780 |
+
>>> diag([1, 2, 3]) # a column vector
|
| 781 |
+
Matrix([
|
| 782 |
+
[1],
|
| 783 |
+
[2],
|
| 784 |
+
[3]])
|
| 785 |
+
|
| 786 |
+
See Also
|
| 787 |
+
========
|
| 788 |
+
.matrixbase.MatrixBase.eye
|
| 789 |
+
.matrixbase.MatrixBase.diagonal
|
| 790 |
+
.matrixbase.MatrixBase.diag
|
| 791 |
+
.expressions.blockmatrix.BlockMatrix
|
| 792 |
+
"""
|
| 793 |
+
return Matrix.diag(*values, strict=strict, unpack=unpack, **kwargs)
|
| 794 |
+
|
| 795 |
+
|
| 796 |
+
def GramSchmidt(vlist, orthonormal=False):
|
| 797 |
+
"""Apply the Gram-Schmidt process to a set of vectors.
|
| 798 |
+
|
| 799 |
+
Parameters
|
| 800 |
+
==========
|
| 801 |
+
|
| 802 |
+
vlist : List of Matrix
|
| 803 |
+
Vectors to be orthogonalized for.
|
| 804 |
+
|
| 805 |
+
orthonormal : Bool, optional
|
| 806 |
+
If true, return an orthonormal basis.
|
| 807 |
+
|
| 808 |
+
Returns
|
| 809 |
+
=======
|
| 810 |
+
|
| 811 |
+
vlist : List of Matrix
|
| 812 |
+
Orthogonalized vectors
|
| 813 |
+
|
| 814 |
+
Notes
|
| 815 |
+
=====
|
| 816 |
+
|
| 817 |
+
This routine is mostly duplicate from ``Matrix.orthogonalize``,
|
| 818 |
+
except for some difference that this always raises error when
|
| 819 |
+
linearly dependent vectors are found, and the keyword ``normalize``
|
| 820 |
+
has been named as ``orthonormal`` in this function.
|
| 821 |
+
|
| 822 |
+
See Also
|
| 823 |
+
========
|
| 824 |
+
|
| 825 |
+
.matrixbase.MatrixBase.orthogonalize
|
| 826 |
+
|
| 827 |
+
References
|
| 828 |
+
==========
|
| 829 |
+
|
| 830 |
+
.. [1] https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process
|
| 831 |
+
"""
|
| 832 |
+
return MutableDenseMatrix.orthogonalize(
|
| 833 |
+
*vlist, normalize=orthonormal, rankcheck=True
|
| 834 |
+
)
|
| 835 |
+
|
| 836 |
+
|
| 837 |
+
def hessian(f, varlist, constraints=()):
|
| 838 |
+
"""Compute Hessian matrix for a function f wrt parameters in varlist
|
| 839 |
+
which may be given as a sequence or a row/column vector. A list of
|
| 840 |
+
constraints may optionally be given.
|
| 841 |
+
|
| 842 |
+
Examples
|
| 843 |
+
========
|
| 844 |
+
|
| 845 |
+
>>> from sympy import Function, hessian, pprint
|
| 846 |
+
>>> from sympy.abc import x, y
|
| 847 |
+
>>> f = Function('f')(x, y)
|
| 848 |
+
>>> g1 = Function('g')(x, y)
|
| 849 |
+
>>> g2 = x**2 + 3*y
|
| 850 |
+
>>> pprint(hessian(f, (x, y), [g1, g2]))
|
| 851 |
+
[ d d ]
|
| 852 |
+
[ 0 0 --(g(x, y)) --(g(x, y)) ]
|
| 853 |
+
[ dx dy ]
|
| 854 |
+
[ ]
|
| 855 |
+
[ 0 0 2*x 3 ]
|
| 856 |
+
[ ]
|
| 857 |
+
[ 2 2 ]
|
| 858 |
+
[d d d ]
|
| 859 |
+
[--(g(x, y)) 2*x ---(f(x, y)) -----(f(x, y))]
|
| 860 |
+
[dx 2 dy dx ]
|
| 861 |
+
[ dx ]
|
| 862 |
+
[ ]
|
| 863 |
+
[ 2 2 ]
|
| 864 |
+
[d d d ]
|
| 865 |
+
[--(g(x, y)) 3 -----(f(x, y)) ---(f(x, y)) ]
|
| 866 |
+
[dy dy dx 2 ]
|
| 867 |
+
[ dy ]
|
| 868 |
+
|
| 869 |
+
References
|
| 870 |
+
==========
|
| 871 |
+
|
| 872 |
+
.. [1] https://en.wikipedia.org/wiki/Hessian_matrix
|
| 873 |
+
|
| 874 |
+
See Also
|
| 875 |
+
========
|
| 876 |
+
|
| 877 |
+
sympy.matrices.matrixbase.MatrixBase.jacobian
|
| 878 |
+
wronskian
|
| 879 |
+
"""
|
| 880 |
+
# f is the expression representing a function f, return regular matrix
|
| 881 |
+
if isinstance(varlist, MatrixBase):
|
| 882 |
+
if 1 not in varlist.shape:
|
| 883 |
+
raise ShapeError("`varlist` must be a column or row vector.")
|
| 884 |
+
if varlist.cols == 1:
|
| 885 |
+
varlist = varlist.T
|
| 886 |
+
varlist = varlist.tolist()[0]
|
| 887 |
+
if is_sequence(varlist):
|
| 888 |
+
n = len(varlist)
|
| 889 |
+
if not n:
|
| 890 |
+
raise ShapeError("`len(varlist)` must not be zero.")
|
| 891 |
+
else:
|
| 892 |
+
raise ValueError("Improper variable list in hessian function")
|
| 893 |
+
if not getattr(f, 'diff'):
|
| 894 |
+
# check differentiability
|
| 895 |
+
raise ValueError("Function `f` (%s) is not differentiable" % f)
|
| 896 |
+
m = len(constraints)
|
| 897 |
+
N = m + n
|
| 898 |
+
out = zeros(N)
|
| 899 |
+
for k, g in enumerate(constraints):
|
| 900 |
+
if not getattr(g, 'diff'):
|
| 901 |
+
# check differentiability
|
| 902 |
+
raise ValueError("Function `f` (%s) is not differentiable" % f)
|
| 903 |
+
for i in range(n):
|
| 904 |
+
out[k, i + m] = g.diff(varlist[i])
|
| 905 |
+
for i in range(n):
|
| 906 |
+
for j in range(i, n):
|
| 907 |
+
out[i + m, j + m] = f.diff(varlist[i]).diff(varlist[j])
|
| 908 |
+
for i in range(N):
|
| 909 |
+
for j in range(i + 1, N):
|
| 910 |
+
out[j, i] = out[i, j]
|
| 911 |
+
return out
|
| 912 |
+
|
| 913 |
+
|
| 914 |
+
def jordan_cell(eigenval, n):
|
| 915 |
+
"""
|
| 916 |
+
Create a Jordan block:
|
| 917 |
+
|
| 918 |
+
Examples
|
| 919 |
+
========
|
| 920 |
+
|
| 921 |
+
>>> from sympy import jordan_cell
|
| 922 |
+
>>> from sympy.abc import x
|
| 923 |
+
>>> jordan_cell(x, 4)
|
| 924 |
+
Matrix([
|
| 925 |
+
[x, 1, 0, 0],
|
| 926 |
+
[0, x, 1, 0],
|
| 927 |
+
[0, 0, x, 1],
|
| 928 |
+
[0, 0, 0, x]])
|
| 929 |
+
"""
|
| 930 |
+
|
| 931 |
+
return Matrix.jordan_block(size=n, eigenvalue=eigenval)
|
| 932 |
+
|
| 933 |
+
|
| 934 |
+
def matrix_multiply_elementwise(A, B):
|
| 935 |
+
"""Return the Hadamard product (elementwise product) of A and B
|
| 936 |
+
|
| 937 |
+
>>> from sympy import Matrix, matrix_multiply_elementwise
|
| 938 |
+
>>> A = Matrix([[0, 1, 2], [3, 4, 5]])
|
| 939 |
+
>>> B = Matrix([[1, 10, 100], [100, 10, 1]])
|
| 940 |
+
>>> matrix_multiply_elementwise(A, B)
|
| 941 |
+
Matrix([
|
| 942 |
+
[ 0, 10, 200],
|
| 943 |
+
[300, 40, 5]])
|
| 944 |
+
|
| 945 |
+
See Also
|
| 946 |
+
========
|
| 947 |
+
|
| 948 |
+
sympy.matrices.matrixbase.MatrixBase.__mul__
|
| 949 |
+
"""
|
| 950 |
+
return A.multiply_elementwise(B)
|
| 951 |
+
|
| 952 |
+
|
| 953 |
+
def ones(*args, **kwargs):
|
| 954 |
+
"""Returns a matrix of ones with ``rows`` rows and ``cols`` columns;
|
| 955 |
+
if ``cols`` is omitted a square matrix will be returned.
|
| 956 |
+
|
| 957 |
+
See Also
|
| 958 |
+
========
|
| 959 |
+
|
| 960 |
+
zeros
|
| 961 |
+
eye
|
| 962 |
+
diag
|
| 963 |
+
"""
|
| 964 |
+
|
| 965 |
+
if 'c' in kwargs:
|
| 966 |
+
kwargs['cols'] = kwargs.pop('c')
|
| 967 |
+
|
| 968 |
+
return Matrix.ones(*args, **kwargs)
|
| 969 |
+
|
| 970 |
+
|
| 971 |
+
def randMatrix(r, c=None, min=0, max=99, seed=None, symmetric=False,
|
| 972 |
+
percent=100, prng=None):
|
| 973 |
+
"""Create random matrix with dimensions ``r`` x ``c``. If ``c`` is omitted
|
| 974 |
+
the matrix will be square. If ``symmetric`` is True the matrix must be
|
| 975 |
+
square. If ``percent`` is less than 100 then only approximately the given
|
| 976 |
+
percentage of elements will be non-zero.
|
| 977 |
+
|
| 978 |
+
The pseudo-random number generator used to generate matrix is chosen in the
|
| 979 |
+
following way.
|
| 980 |
+
|
| 981 |
+
* If ``prng`` is supplied, it will be used as random number generator.
|
| 982 |
+
It should be an instance of ``random.Random``, or at least have
|
| 983 |
+
``randint`` and ``shuffle`` methods with same signatures.
|
| 984 |
+
* if ``prng`` is not supplied but ``seed`` is supplied, then new
|
| 985 |
+
``random.Random`` with given ``seed`` will be created;
|
| 986 |
+
* otherwise, a new ``random.Random`` with default seed will be used.
|
| 987 |
+
|
| 988 |
+
Examples
|
| 989 |
+
========
|
| 990 |
+
|
| 991 |
+
>>> from sympy import randMatrix
|
| 992 |
+
>>> randMatrix(3) # doctest:+SKIP
|
| 993 |
+
[25, 45, 27]
|
| 994 |
+
[44, 54, 9]
|
| 995 |
+
[23, 96, 46]
|
| 996 |
+
>>> randMatrix(3, 2) # doctest:+SKIP
|
| 997 |
+
[87, 29]
|
| 998 |
+
[23, 37]
|
| 999 |
+
[90, 26]
|
| 1000 |
+
>>> randMatrix(3, 3, 0, 2) # doctest:+SKIP
|
| 1001 |
+
[0, 2, 0]
|
| 1002 |
+
[2, 0, 1]
|
| 1003 |
+
[0, 0, 1]
|
| 1004 |
+
>>> randMatrix(3, symmetric=True) # doctest:+SKIP
|
| 1005 |
+
[85, 26, 29]
|
| 1006 |
+
[26, 71, 43]
|
| 1007 |
+
[29, 43, 57]
|
| 1008 |
+
>>> A = randMatrix(3, seed=1)
|
| 1009 |
+
>>> B = randMatrix(3, seed=2)
|
| 1010 |
+
>>> A == B
|
| 1011 |
+
False
|
| 1012 |
+
>>> A == randMatrix(3, seed=1)
|
| 1013 |
+
True
|
| 1014 |
+
>>> randMatrix(3, symmetric=True, percent=50) # doctest:+SKIP
|
| 1015 |
+
[77, 70, 0],
|
| 1016 |
+
[70, 0, 0],
|
| 1017 |
+
[ 0, 0, 88]
|
| 1018 |
+
"""
|
| 1019 |
+
# Note that ``Random()`` is equivalent to ``Random(None)``
|
| 1020 |
+
prng = prng or random.Random(seed)
|
| 1021 |
+
|
| 1022 |
+
if c is None:
|
| 1023 |
+
c = r
|
| 1024 |
+
|
| 1025 |
+
if symmetric and r != c:
|
| 1026 |
+
raise ValueError('For symmetric matrices, r must equal c, but %i != %i' % (r, c))
|
| 1027 |
+
|
| 1028 |
+
ij = range(r * c)
|
| 1029 |
+
if percent != 100:
|
| 1030 |
+
ij = prng.sample(ij, int(len(ij)*percent // 100))
|
| 1031 |
+
|
| 1032 |
+
m = zeros(r, c)
|
| 1033 |
+
|
| 1034 |
+
if not symmetric:
|
| 1035 |
+
for ijk in ij:
|
| 1036 |
+
i, j = divmod(ijk, c)
|
| 1037 |
+
m[i, j] = prng.randint(min, max)
|
| 1038 |
+
else:
|
| 1039 |
+
for ijk in ij:
|
| 1040 |
+
i, j = divmod(ijk, c)
|
| 1041 |
+
if i <= j:
|
| 1042 |
+
m[i, j] = m[j, i] = prng.randint(min, max)
|
| 1043 |
+
|
| 1044 |
+
return m
|
| 1045 |
+
|
| 1046 |
+
|
| 1047 |
+
def wronskian(functions, var, method='bareiss'):
|
| 1048 |
+
"""
|
| 1049 |
+
Compute Wronskian for [] of functions
|
| 1050 |
+
|
| 1051 |
+
::
|
| 1052 |
+
|
| 1053 |
+
| f1 f2 ... fn |
|
| 1054 |
+
| f1' f2' ... fn' |
|
| 1055 |
+
| . . . . |
|
| 1056 |
+
W(f1, ..., fn) = | . . . . |
|
| 1057 |
+
| . . . . |
|
| 1058 |
+
| (n) (n) (n) |
|
| 1059 |
+
| D (f1) D (f2) ... D (fn) |
|
| 1060 |
+
|
| 1061 |
+
see: https://en.wikipedia.org/wiki/Wronskian
|
| 1062 |
+
|
| 1063 |
+
See Also
|
| 1064 |
+
========
|
| 1065 |
+
|
| 1066 |
+
sympy.matrices.matrixbase.MatrixBase.jacobian
|
| 1067 |
+
hessian
|
| 1068 |
+
"""
|
| 1069 |
+
|
| 1070 |
+
functions = [sympify(f) for f in functions]
|
| 1071 |
+
n = len(functions)
|
| 1072 |
+
if n == 0:
|
| 1073 |
+
return S.One
|
| 1074 |
+
W = Matrix(n, n, lambda i, j: functions[i].diff(var, j))
|
| 1075 |
+
return W.det(method)
|
| 1076 |
+
|
| 1077 |
+
|
| 1078 |
+
def zeros(*args, **kwargs):
|
| 1079 |
+
"""Returns a matrix of zeros with ``rows`` rows and ``cols`` columns;
|
| 1080 |
+
if ``cols`` is omitted a square matrix will be returned.
|
| 1081 |
+
|
| 1082 |
+
See Also
|
| 1083 |
+
========
|
| 1084 |
+
|
| 1085 |
+
ones
|
| 1086 |
+
eye
|
| 1087 |
+
diag
|
| 1088 |
+
"""
|
| 1089 |
+
|
| 1090 |
+
if 'c' in kwargs:
|
| 1091 |
+
kwargs['cols'] = kwargs.pop('c')
|
| 1092 |
+
|
| 1093 |
+
return Matrix.zeros(*args, **kwargs)
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/eigen.py
ADDED
|
@@ -0,0 +1,1346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import S
|
| 2 |
+
from sympy.core.sympify import _sympify
|
| 3 |
+
from sympy.functions import KroneckerDelta
|
| 4 |
+
|
| 5 |
+
from .matexpr import MatrixExpr
|
| 6 |
+
from .special import ZeroMatrix, Identity, OneMatrix
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class PermutationMatrix(MatrixExpr):
|
| 10 |
+
"""A Permutation Matrix
|
| 11 |
+
|
| 12 |
+
Parameters
|
| 13 |
+
==========
|
| 14 |
+
|
| 15 |
+
perm : Permutation
|
| 16 |
+
The permutation the matrix uses.
|
| 17 |
+
|
| 18 |
+
The size of the permutation determines the matrix size.
|
| 19 |
+
|
| 20 |
+
See the documentation of
|
| 21 |
+
:class:`sympy.combinatorics.permutations.Permutation` for
|
| 22 |
+
the further information of how to create a permutation object.
|
| 23 |
+
|
| 24 |
+
Examples
|
| 25 |
+
========
|
| 26 |
+
|
| 27 |
+
>>> from sympy import Matrix, PermutationMatrix
|
| 28 |
+
>>> from sympy.combinatorics import Permutation
|
| 29 |
+
|
| 30 |
+
Creating a permutation matrix:
|
| 31 |
+
|
| 32 |
+
>>> p = Permutation(1, 2, 0)
|
| 33 |
+
>>> P = PermutationMatrix(p)
|
| 34 |
+
>>> P = P.as_explicit()
|
| 35 |
+
>>> P
|
| 36 |
+
Matrix([
|
| 37 |
+
[0, 1, 0],
|
| 38 |
+
[0, 0, 1],
|
| 39 |
+
[1, 0, 0]])
|
| 40 |
+
|
| 41 |
+
Permuting a matrix row and column:
|
| 42 |
+
|
| 43 |
+
>>> M = Matrix([0, 1, 2])
|
| 44 |
+
>>> Matrix(P*M)
|
| 45 |
+
Matrix([
|
| 46 |
+
[1],
|
| 47 |
+
[2],
|
| 48 |
+
[0]])
|
| 49 |
+
|
| 50 |
+
>>> Matrix(M.T*P)
|
| 51 |
+
Matrix([[2, 0, 1]])
|
| 52 |
+
|
| 53 |
+
See Also
|
| 54 |
+
========
|
| 55 |
+
|
| 56 |
+
sympy.combinatorics.permutations.Permutation
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
def __new__(cls, perm):
|
| 60 |
+
from sympy.combinatorics.permutations import Permutation
|
| 61 |
+
|
| 62 |
+
perm = _sympify(perm)
|
| 63 |
+
if not isinstance(perm, Permutation):
|
| 64 |
+
raise ValueError(
|
| 65 |
+
"{} must be a SymPy Permutation instance.".format(perm))
|
| 66 |
+
|
| 67 |
+
return super().__new__(cls, perm)
|
| 68 |
+
|
| 69 |
+
@property
|
| 70 |
+
def shape(self):
|
| 71 |
+
size = self.args[0].size
|
| 72 |
+
return (size, size)
|
| 73 |
+
|
| 74 |
+
@property
|
| 75 |
+
def is_Identity(self):
|
| 76 |
+
return self.args[0].is_Identity
|
| 77 |
+
|
| 78 |
+
def doit(self, **hints):
|
| 79 |
+
if self.is_Identity:
|
| 80 |
+
return Identity(self.rows)
|
| 81 |
+
return self
|
| 82 |
+
|
| 83 |
+
def _entry(self, i, j, **kwargs):
|
| 84 |
+
perm = self.args[0]
|
| 85 |
+
return KroneckerDelta(perm.apply(i), j)
|
| 86 |
+
|
| 87 |
+
def _eval_power(self, exp):
|
| 88 |
+
return PermutationMatrix(self.args[0] ** exp).doit()
|
| 89 |
+
|
| 90 |
+
def _eval_inverse(self):
|
| 91 |
+
return PermutationMatrix(self.args[0] ** -1)
|
| 92 |
+
|
| 93 |
+
_eval_transpose = _eval_adjoint = _eval_inverse
|
| 94 |
+
|
| 95 |
+
def _eval_determinant(self):
|
| 96 |
+
sign = self.args[0].signature()
|
| 97 |
+
if sign == 1:
|
| 98 |
+
return S.One
|
| 99 |
+
elif sign == -1:
|
| 100 |
+
return S.NegativeOne
|
| 101 |
+
raise NotImplementedError
|
| 102 |
+
|
| 103 |
+
def _eval_rewrite_as_BlockDiagMatrix(self, *args, **kwargs):
|
| 104 |
+
from sympy.combinatorics.permutations import Permutation
|
| 105 |
+
from .blockmatrix import BlockDiagMatrix
|
| 106 |
+
|
| 107 |
+
perm = self.args[0]
|
| 108 |
+
full_cyclic_form = perm.full_cyclic_form
|
| 109 |
+
|
| 110 |
+
cycles_picks = []
|
| 111 |
+
|
| 112 |
+
# Stage 1. Decompose the cycles into the blockable form.
|
| 113 |
+
a, b, c = 0, 0, 0
|
| 114 |
+
flag = False
|
| 115 |
+
for cycle in full_cyclic_form:
|
| 116 |
+
l = len(cycle)
|
| 117 |
+
m = max(cycle)
|
| 118 |
+
|
| 119 |
+
if not flag:
|
| 120 |
+
if m + 1 > a + l:
|
| 121 |
+
flag = True
|
| 122 |
+
temp = [cycle]
|
| 123 |
+
b = m
|
| 124 |
+
c = l
|
| 125 |
+
else:
|
| 126 |
+
cycles_picks.append([cycle])
|
| 127 |
+
a += l
|
| 128 |
+
|
| 129 |
+
else:
|
| 130 |
+
if m > b:
|
| 131 |
+
if m + 1 == a + c + l:
|
| 132 |
+
temp.append(cycle)
|
| 133 |
+
cycles_picks.append(temp)
|
| 134 |
+
flag = False
|
| 135 |
+
a = m+1
|
| 136 |
+
else:
|
| 137 |
+
b = m
|
| 138 |
+
temp.append(cycle)
|
| 139 |
+
c += l
|
| 140 |
+
else:
|
| 141 |
+
if b + 1 == a + c + l:
|
| 142 |
+
temp.append(cycle)
|
| 143 |
+
cycles_picks.append(temp)
|
| 144 |
+
flag = False
|
| 145 |
+
a = b+1
|
| 146 |
+
else:
|
| 147 |
+
temp.append(cycle)
|
| 148 |
+
c += l
|
| 149 |
+
|
| 150 |
+
# Stage 2. Normalize each decomposed cycles and build matrix.
|
| 151 |
+
p = 0
|
| 152 |
+
args = []
|
| 153 |
+
for pick in cycles_picks:
|
| 154 |
+
new_cycles = []
|
| 155 |
+
l = 0
|
| 156 |
+
for cycle in pick:
|
| 157 |
+
new_cycle = [i - p for i in cycle]
|
| 158 |
+
new_cycles.append(new_cycle)
|
| 159 |
+
l += len(cycle)
|
| 160 |
+
p += l
|
| 161 |
+
perm = Permutation(new_cycles)
|
| 162 |
+
mat = PermutationMatrix(perm)
|
| 163 |
+
args.append(mat)
|
| 164 |
+
|
| 165 |
+
return BlockDiagMatrix(*args)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
class MatrixPermute(MatrixExpr):
|
| 169 |
+
r"""Symbolic representation for permuting matrix rows or columns.
|
| 170 |
+
|
| 171 |
+
Parameters
|
| 172 |
+
==========
|
| 173 |
+
|
| 174 |
+
perm : Permutation, PermutationMatrix
|
| 175 |
+
The permutation to use for permuting the matrix.
|
| 176 |
+
The permutation can be resized to the suitable one,
|
| 177 |
+
|
| 178 |
+
axis : 0 or 1
|
| 179 |
+
The axis to permute alongside.
|
| 180 |
+
If `0`, it will permute the matrix rows.
|
| 181 |
+
If `1`, it will permute the matrix columns.
|
| 182 |
+
|
| 183 |
+
Notes
|
| 184 |
+
=====
|
| 185 |
+
|
| 186 |
+
This follows the same notation used in
|
| 187 |
+
:meth:`sympy.matrices.matrixbase.MatrixBase.permute`.
|
| 188 |
+
|
| 189 |
+
Examples
|
| 190 |
+
========
|
| 191 |
+
|
| 192 |
+
>>> from sympy import Matrix, MatrixPermute
|
| 193 |
+
>>> from sympy.combinatorics import Permutation
|
| 194 |
+
|
| 195 |
+
Permuting the matrix rows:
|
| 196 |
+
|
| 197 |
+
>>> p = Permutation(1, 2, 0)
|
| 198 |
+
>>> A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
| 199 |
+
>>> B = MatrixPermute(A, p, axis=0)
|
| 200 |
+
>>> B.as_explicit()
|
| 201 |
+
Matrix([
|
| 202 |
+
[4, 5, 6],
|
| 203 |
+
[7, 8, 9],
|
| 204 |
+
[1, 2, 3]])
|
| 205 |
+
|
| 206 |
+
Permuting the matrix columns:
|
| 207 |
+
|
| 208 |
+
>>> B = MatrixPermute(A, p, axis=1)
|
| 209 |
+
>>> B.as_explicit()
|
| 210 |
+
Matrix([
|
| 211 |
+
[2, 3, 1],
|
| 212 |
+
[5, 6, 4],
|
| 213 |
+
[8, 9, 7]])
|
| 214 |
+
|
| 215 |
+
See Also
|
| 216 |
+
========
|
| 217 |
+
|
| 218 |
+
sympy.matrices.matrixbase.MatrixBase.permute
|
| 219 |
+
"""
|
| 220 |
+
def __new__(cls, mat, perm, axis=S.Zero):
|
| 221 |
+
from sympy.combinatorics.permutations import Permutation
|
| 222 |
+
|
| 223 |
+
mat = _sympify(mat)
|
| 224 |
+
if not mat.is_Matrix:
|
| 225 |
+
raise ValueError(
|
| 226 |
+
"{} must be a SymPy matrix instance.".format(perm))
|
| 227 |
+
|
| 228 |
+
perm = _sympify(perm)
|
| 229 |
+
if isinstance(perm, PermutationMatrix):
|
| 230 |
+
perm = perm.args[0]
|
| 231 |
+
|
| 232 |
+
if not isinstance(perm, Permutation):
|
| 233 |
+
raise ValueError(
|
| 234 |
+
"{} must be a SymPy Permutation or a PermutationMatrix " \
|
| 235 |
+
"instance".format(perm))
|
| 236 |
+
|
| 237 |
+
axis = _sympify(axis)
|
| 238 |
+
if axis not in (0, 1):
|
| 239 |
+
raise ValueError("The axis must be 0 or 1.")
|
| 240 |
+
|
| 241 |
+
mat_size = mat.shape[axis]
|
| 242 |
+
if mat_size != perm.size:
|
| 243 |
+
try:
|
| 244 |
+
perm = perm.resize(mat_size)
|
| 245 |
+
except ValueError:
|
| 246 |
+
raise ValueError(
|
| 247 |
+
"Size does not match between the permutation {} "
|
| 248 |
+
"and the matrix {} threaded over the axis {} "
|
| 249 |
+
"and cannot be converted."
|
| 250 |
+
.format(perm, mat, axis))
|
| 251 |
+
|
| 252 |
+
return super().__new__(cls, mat, perm, axis)
|
| 253 |
+
|
| 254 |
+
def doit(self, deep=True, **hints):
|
| 255 |
+
mat, perm, axis = self.args
|
| 256 |
+
|
| 257 |
+
if deep:
|
| 258 |
+
mat = mat.doit(deep=deep, **hints)
|
| 259 |
+
perm = perm.doit(deep=deep, **hints)
|
| 260 |
+
|
| 261 |
+
if perm.is_Identity:
|
| 262 |
+
return mat
|
| 263 |
+
|
| 264 |
+
if mat.is_Identity:
|
| 265 |
+
if axis is S.Zero:
|
| 266 |
+
return PermutationMatrix(perm)
|
| 267 |
+
elif axis is S.One:
|
| 268 |
+
return PermutationMatrix(perm**-1)
|
| 269 |
+
|
| 270 |
+
if isinstance(mat, (ZeroMatrix, OneMatrix)):
|
| 271 |
+
return mat
|
| 272 |
+
|
| 273 |
+
if isinstance(mat, MatrixPermute) and mat.args[2] == axis:
|
| 274 |
+
return MatrixPermute(mat.args[0], perm * mat.args[1], axis)
|
| 275 |
+
|
| 276 |
+
return self
|
| 277 |
+
|
| 278 |
+
@property
|
| 279 |
+
def shape(self):
|
| 280 |
+
return self.args[0].shape
|
| 281 |
+
|
| 282 |
+
def _entry(self, i, j, **kwargs):
|
| 283 |
+
mat, perm, axis = self.args
|
| 284 |
+
|
| 285 |
+
if axis == 0:
|
| 286 |
+
return mat[perm.apply(i), j]
|
| 287 |
+
elif axis == 1:
|
| 288 |
+
return mat[i, perm.apply(j)]
|
| 289 |
+
|
| 290 |
+
def _eval_rewrite_as_MatMul(self, *args, **kwargs):
|
| 291 |
+
from .matmul import MatMul
|
| 292 |
+
|
| 293 |
+
mat, perm, axis = self.args
|
| 294 |
+
|
| 295 |
+
deep = kwargs.get("deep", True)
|
| 296 |
+
|
| 297 |
+
if deep:
|
| 298 |
+
mat = mat.rewrite(MatMul)
|
| 299 |
+
|
| 300 |
+
if axis == 0:
|
| 301 |
+
return MatMul(PermutationMatrix(perm), mat)
|
| 302 |
+
elif axis == 1:
|
| 303 |
+
return MatMul(mat, PermutationMatrix(perm**-1))
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/expressions/tests/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (233 Bytes). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/expressions/tests/__pycache__/test_matmul.cpython-311.pyc
ADDED
|
Binary file (19.1 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/graph.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.utilities.iterables import \
|
| 2 |
+
flatten, connected_components, strongly_connected_components
|
| 3 |
+
from .exceptions import NonSquareMatrixError
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def _connected_components(M):
|
| 7 |
+
"""Returns the list of connected vertices of the graph when
|
| 8 |
+
a square matrix is viewed as a weighted graph.
|
| 9 |
+
|
| 10 |
+
Examples
|
| 11 |
+
========
|
| 12 |
+
|
| 13 |
+
>>> from sympy import Matrix
|
| 14 |
+
>>> A = Matrix([
|
| 15 |
+
... [66, 0, 0, 68, 0, 0, 0, 0, 67],
|
| 16 |
+
... [0, 55, 0, 0, 0, 0, 54, 53, 0],
|
| 17 |
+
... [0, 0, 0, 0, 1, 2, 0, 0, 0],
|
| 18 |
+
... [86, 0, 0, 88, 0, 0, 0, 0, 87],
|
| 19 |
+
... [0, 0, 10, 0, 11, 12, 0, 0, 0],
|
| 20 |
+
... [0, 0, 20, 0, 21, 22, 0, 0, 0],
|
| 21 |
+
... [0, 45, 0, 0, 0, 0, 44, 43, 0],
|
| 22 |
+
... [0, 35, 0, 0, 0, 0, 34, 33, 0],
|
| 23 |
+
... [76, 0, 0, 78, 0, 0, 0, 0, 77]])
|
| 24 |
+
>>> A.connected_components()
|
| 25 |
+
[[0, 3, 8], [1, 6, 7], [2, 4, 5]]
|
| 26 |
+
|
| 27 |
+
Notes
|
| 28 |
+
=====
|
| 29 |
+
|
| 30 |
+
Even if any symbolic elements of the matrix can be indeterminate
|
| 31 |
+
to be zero mathematically, this only takes the account of the
|
| 32 |
+
structural aspect of the matrix, so they will considered to be
|
| 33 |
+
nonzero.
|
| 34 |
+
"""
|
| 35 |
+
if not M.is_square:
|
| 36 |
+
raise NonSquareMatrixError
|
| 37 |
+
|
| 38 |
+
V = range(M.rows)
|
| 39 |
+
E = sorted(M.todok().keys())
|
| 40 |
+
return connected_components((V, E))
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _strongly_connected_components(M):
|
| 44 |
+
"""Returns the list of strongly connected vertices of the graph when
|
| 45 |
+
a square matrix is viewed as a weighted graph.
|
| 46 |
+
|
| 47 |
+
Examples
|
| 48 |
+
========
|
| 49 |
+
|
| 50 |
+
>>> from sympy import Matrix
|
| 51 |
+
>>> A = Matrix([
|
| 52 |
+
... [44, 0, 0, 0, 43, 0, 45, 0, 0],
|
| 53 |
+
... [0, 66, 62, 61, 0, 68, 0, 60, 67],
|
| 54 |
+
... [0, 0, 22, 21, 0, 0, 0, 20, 0],
|
| 55 |
+
... [0, 0, 12, 11, 0, 0, 0, 10, 0],
|
| 56 |
+
... [34, 0, 0, 0, 33, 0, 35, 0, 0],
|
| 57 |
+
... [0, 86, 82, 81, 0, 88, 0, 80, 87],
|
| 58 |
+
... [54, 0, 0, 0, 53, 0, 55, 0, 0],
|
| 59 |
+
... [0, 0, 2, 1, 0, 0, 0, 0, 0],
|
| 60 |
+
... [0, 76, 72, 71, 0, 78, 0, 70, 77]])
|
| 61 |
+
>>> A.strongly_connected_components()
|
| 62 |
+
[[0, 4, 6], [2, 3, 7], [1, 5, 8]]
|
| 63 |
+
"""
|
| 64 |
+
if not M.is_square:
|
| 65 |
+
raise NonSquareMatrixError
|
| 66 |
+
|
| 67 |
+
# RepMatrix uses the more efficient DomainMatrix.scc() method
|
| 68 |
+
rep = getattr(M, '_rep', None)
|
| 69 |
+
if rep is not None:
|
| 70 |
+
return rep.scc()
|
| 71 |
+
|
| 72 |
+
V = range(M.rows)
|
| 73 |
+
E = sorted(M.todok().keys())
|
| 74 |
+
return strongly_connected_components((V, E))
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def _connected_components_decomposition(M):
|
| 78 |
+
"""Decomposes a square matrix into block diagonal form only
|
| 79 |
+
using the permutations.
|
| 80 |
+
|
| 81 |
+
Explanation
|
| 82 |
+
===========
|
| 83 |
+
|
| 84 |
+
The decomposition is in a form of $A = P^{-1} B P$ where $P$ is a
|
| 85 |
+
permutation matrix and $B$ is a block diagonal matrix.
|
| 86 |
+
|
| 87 |
+
Returns
|
| 88 |
+
=======
|
| 89 |
+
|
| 90 |
+
P, B : PermutationMatrix, BlockDiagMatrix
|
| 91 |
+
*P* is a permutation matrix for the similarity transform
|
| 92 |
+
as in the explanation. And *B* is the block diagonal matrix of
|
| 93 |
+
the result of the permutation.
|
| 94 |
+
|
| 95 |
+
If you would like to get the diagonal blocks from the
|
| 96 |
+
BlockDiagMatrix, see
|
| 97 |
+
:meth:`~sympy.matrices.expressions.blockmatrix.BlockDiagMatrix.get_diag_blocks`.
|
| 98 |
+
|
| 99 |
+
Examples
|
| 100 |
+
========
|
| 101 |
+
|
| 102 |
+
>>> from sympy import Matrix, pprint
|
| 103 |
+
>>> A = Matrix([
|
| 104 |
+
... [66, 0, 0, 68, 0, 0, 0, 0, 67],
|
| 105 |
+
... [0, 55, 0, 0, 0, 0, 54, 53, 0],
|
| 106 |
+
... [0, 0, 0, 0, 1, 2, 0, 0, 0],
|
| 107 |
+
... [86, 0, 0, 88, 0, 0, 0, 0, 87],
|
| 108 |
+
... [0, 0, 10, 0, 11, 12, 0, 0, 0],
|
| 109 |
+
... [0, 0, 20, 0, 21, 22, 0, 0, 0],
|
| 110 |
+
... [0, 45, 0, 0, 0, 0, 44, 43, 0],
|
| 111 |
+
... [0, 35, 0, 0, 0, 0, 34, 33, 0],
|
| 112 |
+
... [76, 0, 0, 78, 0, 0, 0, 0, 77]])
|
| 113 |
+
|
| 114 |
+
>>> P, B = A.connected_components_decomposition()
|
| 115 |
+
>>> pprint(P)
|
| 116 |
+
PermutationMatrix((1 3)(2 8 5 7 4 6))
|
| 117 |
+
>>> pprint(B)
|
| 118 |
+
[[66 68 67] ]
|
| 119 |
+
[[ ] ]
|
| 120 |
+
[[86 88 87] 0 0 ]
|
| 121 |
+
[[ ] ]
|
| 122 |
+
[[76 78 77] ]
|
| 123 |
+
[ ]
|
| 124 |
+
[ [55 54 53] ]
|
| 125 |
+
[ [ ] ]
|
| 126 |
+
[ 0 [45 44 43] 0 ]
|
| 127 |
+
[ [ ] ]
|
| 128 |
+
[ [35 34 33] ]
|
| 129 |
+
[ ]
|
| 130 |
+
[ [0 1 2 ]]
|
| 131 |
+
[ [ ]]
|
| 132 |
+
[ 0 0 [10 11 12]]
|
| 133 |
+
[ [ ]]
|
| 134 |
+
[ [20 21 22]]
|
| 135 |
+
|
| 136 |
+
>>> P = P.as_explicit()
|
| 137 |
+
>>> B = B.as_explicit()
|
| 138 |
+
>>> P.T*B*P == A
|
| 139 |
+
True
|
| 140 |
+
|
| 141 |
+
Notes
|
| 142 |
+
=====
|
| 143 |
+
|
| 144 |
+
This problem corresponds to the finding of the connected components
|
| 145 |
+
of a graph, when a matrix is viewed as a weighted graph.
|
| 146 |
+
"""
|
| 147 |
+
from sympy.combinatorics.permutations import Permutation
|
| 148 |
+
from sympy.matrices.expressions.blockmatrix import BlockDiagMatrix
|
| 149 |
+
from sympy.matrices.expressions.permutation import PermutationMatrix
|
| 150 |
+
|
| 151 |
+
iblocks = M.connected_components()
|
| 152 |
+
|
| 153 |
+
p = Permutation(flatten(iblocks))
|
| 154 |
+
P = PermutationMatrix(p)
|
| 155 |
+
|
| 156 |
+
blocks = []
|
| 157 |
+
for b in iblocks:
|
| 158 |
+
blocks.append(M[b, b])
|
| 159 |
+
B = BlockDiagMatrix(*blocks)
|
| 160 |
+
return P, B
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def _strongly_connected_components_decomposition(M, lower=True):
|
| 164 |
+
"""Decomposes a square matrix into block triangular form only
|
| 165 |
+
using the permutations.
|
| 166 |
+
|
| 167 |
+
Explanation
|
| 168 |
+
===========
|
| 169 |
+
|
| 170 |
+
The decomposition is in a form of $A = P^{-1} B P$ where $P$ is a
|
| 171 |
+
permutation matrix and $B$ is a block diagonal matrix.
|
| 172 |
+
|
| 173 |
+
Parameters
|
| 174 |
+
==========
|
| 175 |
+
|
| 176 |
+
lower : bool
|
| 177 |
+
Makes $B$ lower block triangular when ``True``.
|
| 178 |
+
Otherwise, makes $B$ upper block triangular.
|
| 179 |
+
|
| 180 |
+
Returns
|
| 181 |
+
=======
|
| 182 |
+
|
| 183 |
+
P, B : PermutationMatrix, BlockMatrix
|
| 184 |
+
*P* is a permutation matrix for the similarity transform
|
| 185 |
+
as in the explanation. And *B* is the block triangular matrix of
|
| 186 |
+
the result of the permutation.
|
| 187 |
+
|
| 188 |
+
Examples
|
| 189 |
+
========
|
| 190 |
+
|
| 191 |
+
>>> from sympy import Matrix, pprint
|
| 192 |
+
>>> A = Matrix([
|
| 193 |
+
... [44, 0, 0, 0, 43, 0, 45, 0, 0],
|
| 194 |
+
... [0, 66, 62, 61, 0, 68, 0, 60, 67],
|
| 195 |
+
... [0, 0, 22, 21, 0, 0, 0, 20, 0],
|
| 196 |
+
... [0, 0, 12, 11, 0, 0, 0, 10, 0],
|
| 197 |
+
... [34, 0, 0, 0, 33, 0, 35, 0, 0],
|
| 198 |
+
... [0, 86, 82, 81, 0, 88, 0, 80, 87],
|
| 199 |
+
... [54, 0, 0, 0, 53, 0, 55, 0, 0],
|
| 200 |
+
... [0, 0, 2, 1, 0, 0, 0, 0, 0],
|
| 201 |
+
... [0, 76, 72, 71, 0, 78, 0, 70, 77]])
|
| 202 |
+
|
| 203 |
+
A lower block triangular decomposition:
|
| 204 |
+
|
| 205 |
+
>>> P, B = A.strongly_connected_components_decomposition()
|
| 206 |
+
>>> pprint(P)
|
| 207 |
+
PermutationMatrix((8)(1 4 3 2 6)(5 7))
|
| 208 |
+
>>> pprint(B)
|
| 209 |
+
[[44 43 45] [0 0 0] [0 0 0] ]
|
| 210 |
+
[[ ] [ ] [ ] ]
|
| 211 |
+
[[34 33 35] [0 0 0] [0 0 0] ]
|
| 212 |
+
[[ ] [ ] [ ] ]
|
| 213 |
+
[[54 53 55] [0 0 0] [0 0 0] ]
|
| 214 |
+
[ ]
|
| 215 |
+
[ [0 0 0] [22 21 20] [0 0 0] ]
|
| 216 |
+
[ [ ] [ ] [ ] ]
|
| 217 |
+
[ [0 0 0] [12 11 10] [0 0 0] ]
|
| 218 |
+
[ [ ] [ ] [ ] ]
|
| 219 |
+
[ [0 0 0] [2 1 0 ] [0 0 0] ]
|
| 220 |
+
[ ]
|
| 221 |
+
[ [0 0 0] [62 61 60] [66 68 67]]
|
| 222 |
+
[ [ ] [ ] [ ]]
|
| 223 |
+
[ [0 0 0] [82 81 80] [86 88 87]]
|
| 224 |
+
[ [ ] [ ] [ ]]
|
| 225 |
+
[ [0 0 0] [72 71 70] [76 78 77]]
|
| 226 |
+
|
| 227 |
+
>>> P = P.as_explicit()
|
| 228 |
+
>>> B = B.as_explicit()
|
| 229 |
+
>>> P.T * B * P == A
|
| 230 |
+
True
|
| 231 |
+
|
| 232 |
+
An upper block triangular decomposition:
|
| 233 |
+
|
| 234 |
+
>>> P, B = A.strongly_connected_components_decomposition(lower=False)
|
| 235 |
+
>>> pprint(P)
|
| 236 |
+
PermutationMatrix((0 1 5 7 4 3 2 8 6))
|
| 237 |
+
>>> pprint(B)
|
| 238 |
+
[[66 68 67] [62 61 60] [0 0 0] ]
|
| 239 |
+
[[ ] [ ] [ ] ]
|
| 240 |
+
[[86 88 87] [82 81 80] [0 0 0] ]
|
| 241 |
+
[[ ] [ ] [ ] ]
|
| 242 |
+
[[76 78 77] [72 71 70] [0 0 0] ]
|
| 243 |
+
[ ]
|
| 244 |
+
[ [0 0 0] [22 21 20] [0 0 0] ]
|
| 245 |
+
[ [ ] [ ] [ ] ]
|
| 246 |
+
[ [0 0 0] [12 11 10] [0 0 0] ]
|
| 247 |
+
[ [ ] [ ] [ ] ]
|
| 248 |
+
[ [0 0 0] [2 1 0 ] [0 0 0] ]
|
| 249 |
+
[ ]
|
| 250 |
+
[ [0 0 0] [0 0 0] [44 43 45]]
|
| 251 |
+
[ [ ] [ ] [ ]]
|
| 252 |
+
[ [0 0 0] [0 0 0] [34 33 35]]
|
| 253 |
+
[ [ ] [ ] [ ]]
|
| 254 |
+
[ [0 0 0] [0 0 0] [54 53 55]]
|
| 255 |
+
|
| 256 |
+
>>> P = P.as_explicit()
|
| 257 |
+
>>> B = B.as_explicit()
|
| 258 |
+
>>> P.T * B * P == A
|
| 259 |
+
True
|
| 260 |
+
"""
|
| 261 |
+
from sympy.combinatorics.permutations import Permutation
|
| 262 |
+
from sympy.matrices.expressions.blockmatrix import BlockMatrix
|
| 263 |
+
from sympy.matrices.expressions.permutation import PermutationMatrix
|
| 264 |
+
|
| 265 |
+
iblocks = M.strongly_connected_components()
|
| 266 |
+
if not lower:
|
| 267 |
+
iblocks = list(reversed(iblocks))
|
| 268 |
+
|
| 269 |
+
p = Permutation(flatten(iblocks))
|
| 270 |
+
P = PermutationMatrix(p)
|
| 271 |
+
|
| 272 |
+
rows = []
|
| 273 |
+
for a in iblocks:
|
| 274 |
+
cols = []
|
| 275 |
+
for b in iblocks:
|
| 276 |
+
cols.append(M[a, b])
|
| 277 |
+
rows.append(cols)
|
| 278 |
+
B = BlockMatrix(rows)
|
| 279 |
+
return P, B
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/immut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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mpmath.matrices.matrices import _matrix
|
| 2 |
+
|
| 3 |
+
from sympy.core import Basic, Dict, Tuple
|
| 4 |
+
from sympy.core.numbers import Integer
|
| 5 |
+
from sympy.core.cache import cacheit
|
| 6 |
+
from sympy.core.sympify import _sympy_converter as sympify_converter, _sympify
|
| 7 |
+
from sympy.matrices.dense import DenseMatrix
|
| 8 |
+
from sympy.matrices.expressions import MatrixExpr
|
| 9 |
+
from sympy.matrices.matrixbase import MatrixBase
|
| 10 |
+
from sympy.matrices.repmatrix import RepMatrix
|
| 11 |
+
from sympy.matrices.sparse import SparseRepMatrix
|
| 12 |
+
from sympy.multipledispatch import dispatch
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def sympify_matrix(arg):
|
| 16 |
+
return arg.as_immutable()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
sympify_converter[MatrixBase] = sympify_matrix
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def sympify_mpmath_matrix(arg):
|
| 23 |
+
mat = [_sympify(x) for x in arg]
|
| 24 |
+
return ImmutableDenseMatrix(arg.rows, arg.cols, mat)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
sympify_converter[_matrix] = sympify_mpmath_matrix
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class ImmutableRepMatrix(RepMatrix, MatrixExpr): # type: ignore
|
| 31 |
+
"""Immutable matrix based on RepMatrix
|
| 32 |
+
|
| 33 |
+
Uses DomainMAtrix as the internal representation.
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
#
|
| 37 |
+
# This is a subclass of RepMatrix that adds/overrides some methods to make
|
| 38 |
+
# the instances Basic and immutable. ImmutableRepMatrix is a superclass for
|
| 39 |
+
# both ImmutableDenseMatrix and ImmutableSparseMatrix.
|
| 40 |
+
#
|
| 41 |
+
|
| 42 |
+
def __new__(cls, *args, **kwargs):
|
| 43 |
+
return cls._new(*args, **kwargs)
|
| 44 |
+
|
| 45 |
+
__hash__ = MatrixExpr.__hash__
|
| 46 |
+
|
| 47 |
+
def copy(self):
|
| 48 |
+
return self
|
| 49 |
+
|
| 50 |
+
@property
|
| 51 |
+
def cols(self):
|
| 52 |
+
return self._cols
|
| 53 |
+
|
| 54 |
+
@property
|
| 55 |
+
def rows(self):
|
| 56 |
+
return self._rows
|
| 57 |
+
|
| 58 |
+
@property
|
| 59 |
+
def shape(self):
|
| 60 |
+
return self._rows, self._cols
|
| 61 |
+
|
| 62 |
+
def as_immutable(self):
|
| 63 |
+
return self
|
| 64 |
+
|
| 65 |
+
def _entry(self, i, j, **kwargs):
|
| 66 |
+
return self[i, j]
|
| 67 |
+
|
| 68 |
+
def __setitem__(self, *args):
|
| 69 |
+
raise TypeError("Cannot set values of {}".format(self.__class__))
|
| 70 |
+
|
| 71 |
+
def is_diagonalizable(self, reals_only=False, **kwargs):
|
| 72 |
+
return super().is_diagonalizable(
|
| 73 |
+
reals_only=reals_only, **kwargs)
|
| 74 |
+
|
| 75 |
+
is_diagonalizable.__doc__ = SparseRepMatrix.is_diagonalizable.__doc__
|
| 76 |
+
is_diagonalizable = cacheit(is_diagonalizable)
|
| 77 |
+
|
| 78 |
+
def analytic_func(self, f, x):
|
| 79 |
+
return self.as_mutable().analytic_func(f, x).as_immutable()
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class ImmutableDenseMatrix(DenseMatrix, ImmutableRepMatrix): # type: ignore
|
| 83 |
+
"""Create an immutable version of a matrix.
|
| 84 |
+
|
| 85 |
+
Examples
|
| 86 |
+
========
|
| 87 |
+
|
| 88 |
+
>>> from sympy import eye, ImmutableMatrix
|
| 89 |
+
>>> ImmutableMatrix(eye(3))
|
| 90 |
+
Matrix([
|
| 91 |
+
[1, 0, 0],
|
| 92 |
+
[0, 1, 0],
|
| 93 |
+
[0, 0, 1]])
|
| 94 |
+
>>> _[0, 0] = 42
|
| 95 |
+
Traceback (most recent call last):
|
| 96 |
+
...
|
| 97 |
+
TypeError: Cannot set values of ImmutableDenseMatrix
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
# MatrixExpr is set as NotIterable, but we want explicit matrices to be
|
| 101 |
+
# iterable
|
| 102 |
+
_iterable = True
|
| 103 |
+
_class_priority = 8
|
| 104 |
+
_op_priority = 10.001
|
| 105 |
+
|
| 106 |
+
@classmethod
|
| 107 |
+
def _new(cls, *args, **kwargs):
|
| 108 |
+
if len(args) == 1 and isinstance(args[0], ImmutableDenseMatrix):
|
| 109 |
+
return args[0]
|
| 110 |
+
if kwargs.get('copy', True) is False:
|
| 111 |
+
if len(args) != 3:
|
| 112 |
+
raise TypeError("'copy=False' requires a matrix be initialized as rows,cols,[list]")
|
| 113 |
+
rows, cols, flat_list = args
|
| 114 |
+
else:
|
| 115 |
+
rows, cols, flat_list = cls._handle_creation_inputs(*args, **kwargs)
|
| 116 |
+
flat_list = list(flat_list) # create a shallow copy
|
| 117 |
+
|
| 118 |
+
rep = cls._flat_list_to_DomainMatrix(rows, cols, flat_list)
|
| 119 |
+
|
| 120 |
+
return cls._fromrep(rep)
|
| 121 |
+
|
| 122 |
+
@classmethod
|
| 123 |
+
def _fromrep(cls, rep):
|
| 124 |
+
rows, cols = rep.shape
|
| 125 |
+
flat_list = rep.to_sympy().to_list_flat()
|
| 126 |
+
obj = Basic.__new__(cls,
|
| 127 |
+
Integer(rows),
|
| 128 |
+
Integer(cols),
|
| 129 |
+
Tuple(*flat_list, sympify=False))
|
| 130 |
+
obj._rows = rows
|
| 131 |
+
obj._cols = cols
|
| 132 |
+
obj._rep = rep
|
| 133 |
+
return obj
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
# make sure ImmutableDenseMatrix is aliased as ImmutableMatrix
|
| 137 |
+
ImmutableMatrix = ImmutableDenseMatrix
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
class ImmutableSparseMatrix(SparseRepMatrix, ImmutableRepMatrix): # type:ignore
|
| 141 |
+
"""Create an immutable version of a sparse matrix.
|
| 142 |
+
|
| 143 |
+
Examples
|
| 144 |
+
========
|
| 145 |
+
|
| 146 |
+
>>> from sympy import eye, ImmutableSparseMatrix
|
| 147 |
+
>>> ImmutableSparseMatrix(1, 1, {})
|
| 148 |
+
Matrix([[0]])
|
| 149 |
+
>>> ImmutableSparseMatrix(eye(3))
|
| 150 |
+
Matrix([
|
| 151 |
+
[1, 0, 0],
|
| 152 |
+
[0, 1, 0],
|
| 153 |
+
[0, 0, 1]])
|
| 154 |
+
>>> _[0, 0] = 42
|
| 155 |
+
Traceback (most recent call last):
|
| 156 |
+
...
|
| 157 |
+
TypeError: Cannot set values of ImmutableSparseMatrix
|
| 158 |
+
>>> _.shape
|
| 159 |
+
(3, 3)
|
| 160 |
+
"""
|
| 161 |
+
is_Matrix = True
|
| 162 |
+
_class_priority = 9
|
| 163 |
+
|
| 164 |
+
@classmethod
|
| 165 |
+
def _new(cls, *args, **kwargs):
|
| 166 |
+
rows, cols, smat = cls._handle_creation_inputs(*args, **kwargs)
|
| 167 |
+
|
| 168 |
+
rep = cls._smat_to_DomainMatrix(rows, cols, smat)
|
| 169 |
+
|
| 170 |
+
return cls._fromrep(rep)
|
| 171 |
+
|
| 172 |
+
@classmethod
|
| 173 |
+
def _fromrep(cls, rep):
|
| 174 |
+
rows, cols = rep.shape
|
| 175 |
+
smat = rep.to_sympy().to_dok()
|
| 176 |
+
obj = Basic.__new__(cls, Integer(rows), Integer(cols), Dict(smat))
|
| 177 |
+
obj._rows = rows
|
| 178 |
+
obj._cols = cols
|
| 179 |
+
obj._rep = rep
|
| 180 |
+
return obj
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
@dispatch(ImmutableDenseMatrix, ImmutableDenseMatrix)
|
| 184 |
+
def _eval_is_eq(lhs, rhs): # noqa:F811
|
| 185 |
+
"""Helper method for Equality with matrices.sympy.
|
| 186 |
+
|
| 187 |
+
Relational automatically converts matrices to ImmutableDenseMatrix
|
| 188 |
+
instances, so this method only applies here. Returns True if the
|
| 189 |
+
matrices are definitively the same, False if they are definitively
|
| 190 |
+
different, and None if undetermined (e.g. if they contain Symbols).
|
| 191 |
+
Returning None triggers default handling of Equalities.
|
| 192 |
+
|
| 193 |
+
"""
|
| 194 |
+
if lhs.shape != rhs.shape:
|
| 195 |
+
return False
|
| 196 |
+
return (lhs - rhs).is_zero_matrix
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/repmatrix.py
ADDED
|
@@ -0,0 +1,1025 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import defaultdict
|
| 2 |
+
|
| 3 |
+
from operator import index as index_
|
| 4 |
+
|
| 5 |
+
from sympy.core.expr import Expr
|
| 6 |
+
from sympy.core.kind import Kind, NumberKind, UndefinedKind
|
| 7 |
+
from sympy.core.numbers import Integer, Rational
|
| 8 |
+
from sympy.core.sympify import _sympify, SympifyError
|
| 9 |
+
from sympy.core.singleton import S
|
| 10 |
+
from sympy.polys.domains import ZZ, QQ, GF, EXRAW
|
| 11 |
+
from sympy.polys.matrices import DomainMatrix
|
| 12 |
+
from sympy.polys.matrices.exceptions import DMNonInvertibleMatrixError
|
| 13 |
+
from sympy.polys.polyerrors import CoercionFailed
|
| 14 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 15 |
+
from sympy.utilities.iterables import is_sequence
|
| 16 |
+
from sympy.utilities.misc import filldedent, as_int
|
| 17 |
+
|
| 18 |
+
from .exceptions import ShapeError, NonSquareMatrixError, NonInvertibleMatrixError
|
| 19 |
+
from .matrixbase import classof, MatrixBase
|
| 20 |
+
from .kind import MatrixKind
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class RepMatrix(MatrixBase):
|
| 24 |
+
"""Matrix implementation based on DomainMatrix as an internal representation.
|
| 25 |
+
|
| 26 |
+
The RepMatrix class is a superclass for Matrix, ImmutableMatrix,
|
| 27 |
+
SparseMatrix and ImmutableSparseMatrix which are the main usable matrix
|
| 28 |
+
classes in SymPy. Most methods on this class are simply forwarded to
|
| 29 |
+
DomainMatrix.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
#
|
| 33 |
+
# MatrixBase is the common superclass for all of the usable explicit matrix
|
| 34 |
+
# classes in SymPy. The idea is that MatrixBase is an abstract class though
|
| 35 |
+
# and that subclasses will implement the lower-level methods.
|
| 36 |
+
#
|
| 37 |
+
# RepMatrix is a subclass of MatrixBase that uses DomainMatrix as an
|
| 38 |
+
# internal representation and delegates lower-level methods to
|
| 39 |
+
# DomainMatrix. All of SymPy's standard explicit matrix classes subclass
|
| 40 |
+
# RepMatrix and so use DomainMatrix internally.
|
| 41 |
+
#
|
| 42 |
+
# A RepMatrix uses an internal DomainMatrix with the domain set to ZZ, QQ
|
| 43 |
+
# or EXRAW. The EXRAW domain is equivalent to the previous implementation
|
| 44 |
+
# of Matrix that used Expr for the elements. The ZZ and QQ domains are used
|
| 45 |
+
# when applicable just because they are compatible with the previous
|
| 46 |
+
# implementation but are much more efficient. Other domains such as QQ[x]
|
| 47 |
+
# are not used because they differ from Expr in some way (e.g. automatic
|
| 48 |
+
# expansion of powers and products).
|
| 49 |
+
#
|
| 50 |
+
|
| 51 |
+
_rep: DomainMatrix
|
| 52 |
+
|
| 53 |
+
def __eq__(self, other):
|
| 54 |
+
# Skip sympify for mutable matrices...
|
| 55 |
+
if not isinstance(other, RepMatrix):
|
| 56 |
+
try:
|
| 57 |
+
other = _sympify(other)
|
| 58 |
+
except SympifyError:
|
| 59 |
+
return NotImplemented
|
| 60 |
+
if not isinstance(other, RepMatrix):
|
| 61 |
+
return NotImplemented
|
| 62 |
+
|
| 63 |
+
return self._rep.unify_eq(other._rep)
|
| 64 |
+
|
| 65 |
+
def to_DM(self, domain=None, **kwargs):
|
| 66 |
+
"""Convert to a :class:`~.DomainMatrix`.
|
| 67 |
+
|
| 68 |
+
Examples
|
| 69 |
+
========
|
| 70 |
+
|
| 71 |
+
>>> from sympy import Matrix
|
| 72 |
+
>>> M = Matrix([[1, 2], [3, 4]])
|
| 73 |
+
>>> M.to_DM()
|
| 74 |
+
DomainMatrix({0: {0: 1, 1: 2}, 1: {0: 3, 1: 4}}, (2, 2), ZZ)
|
| 75 |
+
|
| 76 |
+
The :meth:`DomainMatrix.to_Matrix` method can be used to convert back:
|
| 77 |
+
|
| 78 |
+
>>> M.to_DM().to_Matrix() == M
|
| 79 |
+
True
|
| 80 |
+
|
| 81 |
+
The domain can be given explicitly or otherwise it will be chosen by
|
| 82 |
+
:func:`construct_domain`. Any keyword arguments (besides ``domain``)
|
| 83 |
+
are passed to :func:`construct_domain`:
|
| 84 |
+
|
| 85 |
+
>>> from sympy import QQ, symbols
|
| 86 |
+
>>> x = symbols('x')
|
| 87 |
+
>>> M = Matrix([[x, 1], [1, x]])
|
| 88 |
+
>>> M
|
| 89 |
+
Matrix([
|
| 90 |
+
[x, 1],
|
| 91 |
+
[1, x]])
|
| 92 |
+
>>> M.to_DM().domain
|
| 93 |
+
ZZ[x]
|
| 94 |
+
>>> M.to_DM(field=True).domain
|
| 95 |
+
ZZ(x)
|
| 96 |
+
>>> M.to_DM(domain=QQ[x]).domain
|
| 97 |
+
QQ[x]
|
| 98 |
+
|
| 99 |
+
See Also
|
| 100 |
+
========
|
| 101 |
+
|
| 102 |
+
DomainMatrix
|
| 103 |
+
DomainMatrix.to_Matrix
|
| 104 |
+
DomainMatrix.convert_to
|
| 105 |
+
DomainMatrix.choose_domain
|
| 106 |
+
construct_domain
|
| 107 |
+
"""
|
| 108 |
+
if domain is not None:
|
| 109 |
+
if kwargs:
|
| 110 |
+
raise TypeError("Options cannot be used with domain parameter")
|
| 111 |
+
return self._rep.convert_to(domain)
|
| 112 |
+
|
| 113 |
+
rep = self._rep
|
| 114 |
+
dom = rep.domain
|
| 115 |
+
|
| 116 |
+
# If the internal DomainMatrix is already ZZ or QQ then we can maybe
|
| 117 |
+
# bypass calling construct_domain or performing any conversions. Some
|
| 118 |
+
# kwargs might affect this though e.g. field=True (not sure if there
|
| 119 |
+
# are others).
|
| 120 |
+
if not kwargs:
|
| 121 |
+
if dom.is_ZZ:
|
| 122 |
+
return rep.copy()
|
| 123 |
+
elif dom.is_QQ:
|
| 124 |
+
# All elements might be integers
|
| 125 |
+
try:
|
| 126 |
+
return rep.convert_to(ZZ)
|
| 127 |
+
except CoercionFailed:
|
| 128 |
+
pass
|
| 129 |
+
return rep.copy()
|
| 130 |
+
|
| 131 |
+
# Let construct_domain choose a domain
|
| 132 |
+
rep_dom = rep.choose_domain(**kwargs)
|
| 133 |
+
|
| 134 |
+
# XXX: There should be an option to construct_domain to choose EXRAW
|
| 135 |
+
# instead of EX. At least converting to EX does not initially trigger
|
| 136 |
+
# EX.simplify which is what we want here but should probably be
|
| 137 |
+
# considered a bug in EX. Perhaps also this could be handled in
|
| 138 |
+
# DomainMatrix.choose_domain rather than here...
|
| 139 |
+
if rep_dom.domain.is_EX:
|
| 140 |
+
rep_dom = rep_dom.convert_to(EXRAW)
|
| 141 |
+
|
| 142 |
+
return rep_dom
|
| 143 |
+
|
| 144 |
+
@classmethod
|
| 145 |
+
def _unify_element_sympy(cls, rep, element):
|
| 146 |
+
domain = rep.domain
|
| 147 |
+
element = _sympify(element)
|
| 148 |
+
|
| 149 |
+
if domain != EXRAW:
|
| 150 |
+
# The domain can only be ZZ, QQ or EXRAW
|
| 151 |
+
if element.is_Integer:
|
| 152 |
+
new_domain = domain
|
| 153 |
+
elif element.is_Rational:
|
| 154 |
+
new_domain = QQ
|
| 155 |
+
else:
|
| 156 |
+
new_domain = EXRAW
|
| 157 |
+
|
| 158 |
+
# XXX: This converts the domain for all elements in the matrix
|
| 159 |
+
# which can be slow. This happens e.g. if __setitem__ changes one
|
| 160 |
+
# element to something that does not fit in the domain
|
| 161 |
+
if new_domain != domain:
|
| 162 |
+
rep = rep.convert_to(new_domain)
|
| 163 |
+
domain = new_domain
|
| 164 |
+
|
| 165 |
+
if domain != EXRAW:
|
| 166 |
+
element = new_domain.from_sympy(element)
|
| 167 |
+
|
| 168 |
+
if domain == EXRAW and not isinstance(element, Expr):
|
| 169 |
+
sympy_deprecation_warning(
|
| 170 |
+
"""
|
| 171 |
+
non-Expr objects in a Matrix is deprecated. Matrix represents
|
| 172 |
+
a mathematical matrix. To represent a container of non-numeric
|
| 173 |
+
entities, Use a list of lists, TableForm, NumPy array, or some
|
| 174 |
+
other data structure instead.
|
| 175 |
+
""",
|
| 176 |
+
deprecated_since_version="1.9",
|
| 177 |
+
active_deprecations_target="deprecated-non-expr-in-matrix",
|
| 178 |
+
stacklevel=4,
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
return rep, element
|
| 182 |
+
|
| 183 |
+
@classmethod
|
| 184 |
+
def _dod_to_DomainMatrix(cls, rows, cols, dod, types):
|
| 185 |
+
|
| 186 |
+
if not all(issubclass(typ, Expr) for typ in types):
|
| 187 |
+
sympy_deprecation_warning(
|
| 188 |
+
"""
|
| 189 |
+
non-Expr objects in a Matrix is deprecated. Matrix represents
|
| 190 |
+
a mathematical matrix. To represent a container of non-numeric
|
| 191 |
+
entities, Use a list of lists, TableForm, NumPy array, or some
|
| 192 |
+
other data structure instead.
|
| 193 |
+
""",
|
| 194 |
+
deprecated_since_version="1.9",
|
| 195 |
+
active_deprecations_target="deprecated-non-expr-in-matrix",
|
| 196 |
+
stacklevel=6,
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
rep = DomainMatrix(dod, (rows, cols), EXRAW)
|
| 200 |
+
|
| 201 |
+
if all(issubclass(typ, Rational) for typ in types):
|
| 202 |
+
if all(issubclass(typ, Integer) for typ in types):
|
| 203 |
+
rep = rep.convert_to(ZZ)
|
| 204 |
+
else:
|
| 205 |
+
rep = rep.convert_to(QQ)
|
| 206 |
+
|
| 207 |
+
return rep
|
| 208 |
+
|
| 209 |
+
@classmethod
|
| 210 |
+
def _flat_list_to_DomainMatrix(cls, rows, cols, flat_list):
|
| 211 |
+
|
| 212 |
+
elements_dod = defaultdict(dict)
|
| 213 |
+
for n, element in enumerate(flat_list):
|
| 214 |
+
if element != 0:
|
| 215 |
+
i, j = divmod(n, cols)
|
| 216 |
+
elements_dod[i][j] = element
|
| 217 |
+
|
| 218 |
+
types = set(map(type, flat_list))
|
| 219 |
+
|
| 220 |
+
rep = cls._dod_to_DomainMatrix(rows, cols, elements_dod, types)
|
| 221 |
+
return rep
|
| 222 |
+
|
| 223 |
+
@classmethod
|
| 224 |
+
def _smat_to_DomainMatrix(cls, rows, cols, smat):
|
| 225 |
+
|
| 226 |
+
elements_dod = defaultdict(dict)
|
| 227 |
+
for (i, j), element in smat.items():
|
| 228 |
+
if element != 0:
|
| 229 |
+
elements_dod[i][j] = element
|
| 230 |
+
|
| 231 |
+
types = set(map(type, smat.values()))
|
| 232 |
+
|
| 233 |
+
rep = cls._dod_to_DomainMatrix(rows, cols, elements_dod, types)
|
| 234 |
+
return rep
|
| 235 |
+
|
| 236 |
+
def flat(self):
|
| 237 |
+
return self._rep.to_sympy().to_list_flat()
|
| 238 |
+
|
| 239 |
+
def _eval_tolist(self):
|
| 240 |
+
return self._rep.to_sympy().to_list()
|
| 241 |
+
|
| 242 |
+
def _eval_todok(self):
|
| 243 |
+
return self._rep.to_sympy().to_dok()
|
| 244 |
+
|
| 245 |
+
@classmethod
|
| 246 |
+
def _eval_from_dok(cls, rows, cols, dok):
|
| 247 |
+
return cls._fromrep(cls._smat_to_DomainMatrix(rows, cols, dok))
|
| 248 |
+
|
| 249 |
+
def _eval_values(self):
|
| 250 |
+
return list(self._eval_iter_values())
|
| 251 |
+
|
| 252 |
+
def _eval_iter_values(self):
|
| 253 |
+
rep = self._rep
|
| 254 |
+
K = rep.domain
|
| 255 |
+
values = rep.iter_values()
|
| 256 |
+
if not K.is_EXRAW:
|
| 257 |
+
values = map(K.to_sympy, values)
|
| 258 |
+
return values
|
| 259 |
+
|
| 260 |
+
def _eval_iter_items(self):
|
| 261 |
+
rep = self._rep
|
| 262 |
+
K = rep.domain
|
| 263 |
+
to_sympy = K.to_sympy
|
| 264 |
+
items = rep.iter_items()
|
| 265 |
+
if not K.is_EXRAW:
|
| 266 |
+
items = ((i, to_sympy(v)) for i, v in items)
|
| 267 |
+
return items
|
| 268 |
+
|
| 269 |
+
def copy(self):
|
| 270 |
+
return self._fromrep(self._rep.copy())
|
| 271 |
+
|
| 272 |
+
@property
|
| 273 |
+
def kind(self) -> MatrixKind:
|
| 274 |
+
domain = self._rep.domain
|
| 275 |
+
element_kind: Kind
|
| 276 |
+
if domain in (ZZ, QQ):
|
| 277 |
+
element_kind = NumberKind
|
| 278 |
+
elif domain == EXRAW:
|
| 279 |
+
kinds = {e.kind for e in self.values()}
|
| 280 |
+
if len(kinds) == 1:
|
| 281 |
+
[element_kind] = kinds
|
| 282 |
+
else:
|
| 283 |
+
element_kind = UndefinedKind
|
| 284 |
+
else: # pragma: no cover
|
| 285 |
+
raise RuntimeError("Domain should only be ZZ, QQ or EXRAW")
|
| 286 |
+
return MatrixKind(element_kind)
|
| 287 |
+
|
| 288 |
+
def _eval_has(self, *patterns):
|
| 289 |
+
# if the matrix has any zeros, see if S.Zero
|
| 290 |
+
# has the pattern. If _smat is full length,
|
| 291 |
+
# the matrix has no zeros.
|
| 292 |
+
zhas = False
|
| 293 |
+
dok = self.todok()
|
| 294 |
+
if len(dok) != self.rows*self.cols:
|
| 295 |
+
zhas = S.Zero.has(*patterns)
|
| 296 |
+
return zhas or any(value.has(*patterns) for value in dok.values())
|
| 297 |
+
|
| 298 |
+
def _eval_is_Identity(self):
|
| 299 |
+
if not all(self[i, i] == 1 for i in range(self.rows)):
|
| 300 |
+
return False
|
| 301 |
+
return len(self.todok()) == self.rows
|
| 302 |
+
|
| 303 |
+
def _eval_is_symmetric(self, simpfunc):
|
| 304 |
+
diff = (self - self.T).applyfunc(simpfunc)
|
| 305 |
+
return len(diff.values()) == 0
|
| 306 |
+
|
| 307 |
+
def _eval_transpose(self):
|
| 308 |
+
"""Returns the transposed SparseMatrix of this SparseMatrix.
|
| 309 |
+
|
| 310 |
+
Examples
|
| 311 |
+
========
|
| 312 |
+
|
| 313 |
+
>>> from sympy import SparseMatrix
|
| 314 |
+
>>> a = SparseMatrix(((1, 2), (3, 4)))
|
| 315 |
+
>>> a
|
| 316 |
+
Matrix([
|
| 317 |
+
[1, 2],
|
| 318 |
+
[3, 4]])
|
| 319 |
+
>>> a.T
|
| 320 |
+
Matrix([
|
| 321 |
+
[1, 3],
|
| 322 |
+
[2, 4]])
|
| 323 |
+
"""
|
| 324 |
+
return self._fromrep(self._rep.transpose())
|
| 325 |
+
|
| 326 |
+
def _eval_col_join(self, other):
|
| 327 |
+
return self._fromrep(self._rep.vstack(other._rep))
|
| 328 |
+
|
| 329 |
+
def _eval_row_join(self, other):
|
| 330 |
+
return self._fromrep(self._rep.hstack(other._rep))
|
| 331 |
+
|
| 332 |
+
def _eval_extract(self, rowsList, colsList):
|
| 333 |
+
return self._fromrep(self._rep.extract(rowsList, colsList))
|
| 334 |
+
|
| 335 |
+
def __getitem__(self, key):
|
| 336 |
+
return _getitem_RepMatrix(self, key)
|
| 337 |
+
|
| 338 |
+
@classmethod
|
| 339 |
+
def _eval_zeros(cls, rows, cols):
|
| 340 |
+
rep = DomainMatrix.zeros((rows, cols), ZZ)
|
| 341 |
+
return cls._fromrep(rep)
|
| 342 |
+
|
| 343 |
+
@classmethod
|
| 344 |
+
def _eval_eye(cls, rows, cols):
|
| 345 |
+
rep = DomainMatrix.eye((rows, cols), ZZ)
|
| 346 |
+
return cls._fromrep(rep)
|
| 347 |
+
|
| 348 |
+
def _eval_add(self, other):
|
| 349 |
+
return classof(self, other)._fromrep(self._rep + other._rep)
|
| 350 |
+
|
| 351 |
+
def _eval_matrix_mul(self, other):
|
| 352 |
+
return classof(self, other)._fromrep(self._rep * other._rep)
|
| 353 |
+
|
| 354 |
+
def _eval_matrix_mul_elementwise(self, other):
|
| 355 |
+
selfrep, otherrep = self._rep.unify(other._rep)
|
| 356 |
+
newrep = selfrep.mul_elementwise(otherrep)
|
| 357 |
+
return classof(self, other)._fromrep(newrep)
|
| 358 |
+
|
| 359 |
+
def _eval_scalar_mul(self, other):
|
| 360 |
+
rep, other = self._unify_element_sympy(self._rep, other)
|
| 361 |
+
return self._fromrep(rep.scalarmul(other))
|
| 362 |
+
|
| 363 |
+
def _eval_scalar_rmul(self, other):
|
| 364 |
+
rep, other = self._unify_element_sympy(self._rep, other)
|
| 365 |
+
return self._fromrep(rep.rscalarmul(other))
|
| 366 |
+
|
| 367 |
+
def _eval_Abs(self):
|
| 368 |
+
return self._fromrep(self._rep.applyfunc(abs))
|
| 369 |
+
|
| 370 |
+
def _eval_conjugate(self):
|
| 371 |
+
rep = self._rep
|
| 372 |
+
domain = rep.domain
|
| 373 |
+
if domain in (ZZ, QQ):
|
| 374 |
+
return self.copy()
|
| 375 |
+
else:
|
| 376 |
+
return self._fromrep(rep.applyfunc(lambda e: e.conjugate()))
|
| 377 |
+
|
| 378 |
+
def equals(self, other, failing_expression=False):
|
| 379 |
+
"""Applies ``equals`` to corresponding elements of the matrices,
|
| 380 |
+
trying to prove that the elements are equivalent, returning True
|
| 381 |
+
if they are, False if any pair is not, and None (or the first
|
| 382 |
+
failing expression if failing_expression is True) if it cannot
|
| 383 |
+
be decided if the expressions are equivalent or not. This is, in
|
| 384 |
+
general, an expensive operation.
|
| 385 |
+
|
| 386 |
+
Examples
|
| 387 |
+
========
|
| 388 |
+
|
| 389 |
+
>>> from sympy import Matrix
|
| 390 |
+
>>> from sympy.abc import x
|
| 391 |
+
>>> A = Matrix([x*(x - 1), 0])
|
| 392 |
+
>>> B = Matrix([x**2 - x, 0])
|
| 393 |
+
>>> A == B
|
| 394 |
+
False
|
| 395 |
+
>>> A.simplify() == B.simplify()
|
| 396 |
+
True
|
| 397 |
+
>>> A.equals(B)
|
| 398 |
+
True
|
| 399 |
+
>>> A.equals(2)
|
| 400 |
+
False
|
| 401 |
+
|
| 402 |
+
See Also
|
| 403 |
+
========
|
| 404 |
+
sympy.core.expr.Expr.equals
|
| 405 |
+
"""
|
| 406 |
+
if self.shape != getattr(other, 'shape', None):
|
| 407 |
+
return False
|
| 408 |
+
|
| 409 |
+
rv = True
|
| 410 |
+
for i in range(self.rows):
|
| 411 |
+
for j in range(self.cols):
|
| 412 |
+
ans = self[i, j].equals(other[i, j], failing_expression)
|
| 413 |
+
if ans is False:
|
| 414 |
+
return False
|
| 415 |
+
elif ans is not True and rv is True:
|
| 416 |
+
rv = ans
|
| 417 |
+
return rv
|
| 418 |
+
|
| 419 |
+
def inv_mod(M, m):
|
| 420 |
+
r"""
|
| 421 |
+
Returns the inverse of the integer matrix ``M`` modulo ``m``.
|
| 422 |
+
|
| 423 |
+
Examples
|
| 424 |
+
========
|
| 425 |
+
|
| 426 |
+
>>> from sympy import Matrix
|
| 427 |
+
>>> A = Matrix(2, 2, [1, 2, 3, 4])
|
| 428 |
+
>>> A.inv_mod(5)
|
| 429 |
+
Matrix([
|
| 430 |
+
[3, 1],
|
| 431 |
+
[4, 2]])
|
| 432 |
+
>>> A.inv_mod(3)
|
| 433 |
+
Matrix([
|
| 434 |
+
[1, 1],
|
| 435 |
+
[0, 1]])
|
| 436 |
+
|
| 437 |
+
"""
|
| 438 |
+
|
| 439 |
+
if not M.is_square:
|
| 440 |
+
raise NonSquareMatrixError()
|
| 441 |
+
|
| 442 |
+
try:
|
| 443 |
+
m = as_int(m)
|
| 444 |
+
except ValueError:
|
| 445 |
+
raise TypeError("inv_mod: modulus m must be an integer")
|
| 446 |
+
|
| 447 |
+
K = GF(m, symmetric=False)
|
| 448 |
+
|
| 449 |
+
try:
|
| 450 |
+
dM = M.to_DM(K)
|
| 451 |
+
except CoercionFailed:
|
| 452 |
+
raise ValueError("inv_mod: matrix entries must be integers")
|
| 453 |
+
|
| 454 |
+
try:
|
| 455 |
+
dMi = dM.inv()
|
| 456 |
+
except DMNonInvertibleMatrixError as exc:
|
| 457 |
+
msg = f'Matrix is not invertible (mod {m})'
|
| 458 |
+
raise NonInvertibleMatrixError(msg) from exc
|
| 459 |
+
|
| 460 |
+
return dMi.to_Matrix()
|
| 461 |
+
|
| 462 |
+
def lll(self, delta=0.75):
|
| 463 |
+
"""LLL-reduced basis for the rowspace of a matrix of integers.
|
| 464 |
+
|
| 465 |
+
Performs the Lenstra–Lenstra–Lovász (LLL) basis reduction algorithm.
|
| 466 |
+
|
| 467 |
+
The implementation is provided by :class:`~DomainMatrix`. See
|
| 468 |
+
:meth:`~DomainMatrix.lll` for more details.
|
| 469 |
+
|
| 470 |
+
Examples
|
| 471 |
+
========
|
| 472 |
+
|
| 473 |
+
>>> from sympy import Matrix
|
| 474 |
+
>>> M = Matrix([[1, 0, 0, 0, -20160],
|
| 475 |
+
... [0, 1, 0, 0, 33768],
|
| 476 |
+
... [0, 0, 1, 0, 39578],
|
| 477 |
+
... [0, 0, 0, 1, 47757]])
|
| 478 |
+
>>> M.lll()
|
| 479 |
+
Matrix([
|
| 480 |
+
[ 10, -3, -2, 8, -4],
|
| 481 |
+
[ 3, -9, 8, 1, -11],
|
| 482 |
+
[ -3, 13, -9, -3, -9],
|
| 483 |
+
[-12, -7, -11, 9, -1]])
|
| 484 |
+
|
| 485 |
+
See Also
|
| 486 |
+
========
|
| 487 |
+
|
| 488 |
+
lll_transform
|
| 489 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.lll
|
| 490 |
+
"""
|
| 491 |
+
delta = QQ.from_sympy(_sympify(delta))
|
| 492 |
+
dM = self._rep.convert_to(ZZ)
|
| 493 |
+
basis = dM.lll(delta=delta)
|
| 494 |
+
return self._fromrep(basis)
|
| 495 |
+
|
| 496 |
+
def lll_transform(self, delta=0.75):
|
| 497 |
+
"""LLL-reduced basis and transformation matrix.
|
| 498 |
+
|
| 499 |
+
Performs the Lenstra–Lenstra–Lovász (LLL) basis reduction algorithm.
|
| 500 |
+
|
| 501 |
+
The implementation is provided by :class:`~DomainMatrix`. See
|
| 502 |
+
:meth:`~DomainMatrix.lll_transform` for more details.
|
| 503 |
+
|
| 504 |
+
Examples
|
| 505 |
+
========
|
| 506 |
+
|
| 507 |
+
>>> from sympy import Matrix
|
| 508 |
+
>>> M = Matrix([[1, 0, 0, 0, -20160],
|
| 509 |
+
... [0, 1, 0, 0, 33768],
|
| 510 |
+
... [0, 0, 1, 0, 39578],
|
| 511 |
+
... [0, 0, 0, 1, 47757]])
|
| 512 |
+
>>> B, T = M.lll_transform()
|
| 513 |
+
>>> B
|
| 514 |
+
Matrix([
|
| 515 |
+
[ 10, -3, -2, 8, -4],
|
| 516 |
+
[ 3, -9, 8, 1, -11],
|
| 517 |
+
[ -3, 13, -9, -3, -9],
|
| 518 |
+
[-12, -7, -11, 9, -1]])
|
| 519 |
+
>>> T
|
| 520 |
+
Matrix([
|
| 521 |
+
[ 10, -3, -2, 8],
|
| 522 |
+
[ 3, -9, 8, 1],
|
| 523 |
+
[ -3, 13, -9, -3],
|
| 524 |
+
[-12, -7, -11, 9]])
|
| 525 |
+
|
| 526 |
+
The transformation matrix maps the original basis to the LLL-reduced
|
| 527 |
+
basis:
|
| 528 |
+
|
| 529 |
+
>>> T * M == B
|
| 530 |
+
True
|
| 531 |
+
|
| 532 |
+
See Also
|
| 533 |
+
========
|
| 534 |
+
|
| 535 |
+
lll
|
| 536 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.lll_transform
|
| 537 |
+
"""
|
| 538 |
+
delta = QQ.from_sympy(_sympify(delta))
|
| 539 |
+
dM = self._rep.convert_to(ZZ)
|
| 540 |
+
basis, transform = dM.lll_transform(delta=delta)
|
| 541 |
+
B = self._fromrep(basis)
|
| 542 |
+
T = self._fromrep(transform)
|
| 543 |
+
return B, T
|
| 544 |
+
|
| 545 |
+
|
| 546 |
+
class MutableRepMatrix(RepMatrix):
|
| 547 |
+
"""Mutable matrix based on DomainMatrix as the internal representation"""
|
| 548 |
+
|
| 549 |
+
#
|
| 550 |
+
# MutableRepMatrix is a subclass of RepMatrix that adds/overrides methods
|
| 551 |
+
# to make the instances mutable. MutableRepMatrix is a superclass for both
|
| 552 |
+
# MutableDenseMatrix and MutableSparseMatrix.
|
| 553 |
+
#
|
| 554 |
+
|
| 555 |
+
is_zero = False
|
| 556 |
+
|
| 557 |
+
def __new__(cls, *args, **kwargs):
|
| 558 |
+
return cls._new(*args, **kwargs)
|
| 559 |
+
|
| 560 |
+
@classmethod
|
| 561 |
+
def _new(cls, *args, copy=True, **kwargs):
|
| 562 |
+
if copy is False:
|
| 563 |
+
# The input was rows, cols, [list].
|
| 564 |
+
# It should be used directly without creating a copy.
|
| 565 |
+
if len(args) != 3:
|
| 566 |
+
raise TypeError("'copy=False' requires a matrix be initialized as rows,cols,[list]")
|
| 567 |
+
rows, cols, flat_list = args
|
| 568 |
+
else:
|
| 569 |
+
rows, cols, flat_list = cls._handle_creation_inputs(*args, **kwargs)
|
| 570 |
+
flat_list = list(flat_list) # create a shallow copy
|
| 571 |
+
|
| 572 |
+
rep = cls._flat_list_to_DomainMatrix(rows, cols, flat_list)
|
| 573 |
+
|
| 574 |
+
return cls._fromrep(rep)
|
| 575 |
+
|
| 576 |
+
@classmethod
|
| 577 |
+
def _fromrep(cls, rep):
|
| 578 |
+
obj = super().__new__(cls)
|
| 579 |
+
obj.rows, obj.cols = rep.shape
|
| 580 |
+
obj._rep = rep
|
| 581 |
+
return obj
|
| 582 |
+
|
| 583 |
+
def copy(self):
|
| 584 |
+
return self._fromrep(self._rep.copy())
|
| 585 |
+
|
| 586 |
+
def as_mutable(self):
|
| 587 |
+
return self.copy()
|
| 588 |
+
|
| 589 |
+
def __setitem__(self, key, value):
|
| 590 |
+
"""
|
| 591 |
+
|
| 592 |
+
Examples
|
| 593 |
+
========
|
| 594 |
+
|
| 595 |
+
>>> from sympy import Matrix, I, zeros, ones
|
| 596 |
+
>>> m = Matrix(((1, 2+I), (3, 4)))
|
| 597 |
+
>>> m
|
| 598 |
+
Matrix([
|
| 599 |
+
[1, 2 + I],
|
| 600 |
+
[3, 4]])
|
| 601 |
+
>>> m[1, 0] = 9
|
| 602 |
+
>>> m
|
| 603 |
+
Matrix([
|
| 604 |
+
[1, 2 + I],
|
| 605 |
+
[9, 4]])
|
| 606 |
+
>>> m[1, 0] = [[0, 1]]
|
| 607 |
+
|
| 608 |
+
To replace row r you assign to position r*m where m
|
| 609 |
+
is the number of columns:
|
| 610 |
+
|
| 611 |
+
>>> M = zeros(4)
|
| 612 |
+
>>> m = M.cols
|
| 613 |
+
>>> M[3*m] = ones(1, m)*2; M
|
| 614 |
+
Matrix([
|
| 615 |
+
[0, 0, 0, 0],
|
| 616 |
+
[0, 0, 0, 0],
|
| 617 |
+
[0, 0, 0, 0],
|
| 618 |
+
[2, 2, 2, 2]])
|
| 619 |
+
|
| 620 |
+
And to replace column c you can assign to position c:
|
| 621 |
+
|
| 622 |
+
>>> M[2] = ones(m, 1)*4; M
|
| 623 |
+
Matrix([
|
| 624 |
+
[0, 0, 4, 0],
|
| 625 |
+
[0, 0, 4, 0],
|
| 626 |
+
[0, 0, 4, 0],
|
| 627 |
+
[2, 2, 4, 2]])
|
| 628 |
+
"""
|
| 629 |
+
rv = self._setitem(key, value)
|
| 630 |
+
if rv is not None:
|
| 631 |
+
i, j, value = rv
|
| 632 |
+
self._rep, value = self._unify_element_sympy(self._rep, value)
|
| 633 |
+
self._rep.rep.setitem(i, j, value)
|
| 634 |
+
|
| 635 |
+
def _eval_col_del(self, col):
|
| 636 |
+
self._rep = DomainMatrix.hstack(self._rep[:,:col], self._rep[:,col+1:])
|
| 637 |
+
self.cols -= 1
|
| 638 |
+
|
| 639 |
+
def _eval_row_del(self, row):
|
| 640 |
+
self._rep = DomainMatrix.vstack(self._rep[:row,:], self._rep[row+1:, :])
|
| 641 |
+
self.rows -= 1
|
| 642 |
+
|
| 643 |
+
def _eval_col_insert(self, col, other):
|
| 644 |
+
other = self._new(other)
|
| 645 |
+
return self.hstack(self[:,:col], other, self[:,col:])
|
| 646 |
+
|
| 647 |
+
def _eval_row_insert(self, row, other):
|
| 648 |
+
other = self._new(other)
|
| 649 |
+
return self.vstack(self[:row,:], other, self[row:,:])
|
| 650 |
+
|
| 651 |
+
def col_op(self, j, f):
|
| 652 |
+
"""In-place operation on col j using two-arg functor whose args are
|
| 653 |
+
interpreted as (self[i, j], i).
|
| 654 |
+
|
| 655 |
+
Examples
|
| 656 |
+
========
|
| 657 |
+
|
| 658 |
+
>>> from sympy import eye
|
| 659 |
+
>>> M = eye(3)
|
| 660 |
+
>>> M.col_op(1, lambda v, i: v + 2*M[i, 0]); M
|
| 661 |
+
Matrix([
|
| 662 |
+
[1, 2, 0],
|
| 663 |
+
[0, 1, 0],
|
| 664 |
+
[0, 0, 1]])
|
| 665 |
+
|
| 666 |
+
See Also
|
| 667 |
+
========
|
| 668 |
+
col
|
| 669 |
+
row_op
|
| 670 |
+
"""
|
| 671 |
+
for i in range(self.rows):
|
| 672 |
+
self[i, j] = f(self[i, j], i)
|
| 673 |
+
|
| 674 |
+
def col_swap(self, i, j):
|
| 675 |
+
"""Swap the two given columns of the matrix in-place.
|
| 676 |
+
|
| 677 |
+
Examples
|
| 678 |
+
========
|
| 679 |
+
|
| 680 |
+
>>> from sympy import Matrix
|
| 681 |
+
>>> M = Matrix([[1, 0], [1, 0]])
|
| 682 |
+
>>> M
|
| 683 |
+
Matrix([
|
| 684 |
+
[1, 0],
|
| 685 |
+
[1, 0]])
|
| 686 |
+
>>> M.col_swap(0, 1)
|
| 687 |
+
>>> M
|
| 688 |
+
Matrix([
|
| 689 |
+
[0, 1],
|
| 690 |
+
[0, 1]])
|
| 691 |
+
|
| 692 |
+
See Also
|
| 693 |
+
========
|
| 694 |
+
|
| 695 |
+
col
|
| 696 |
+
row_swap
|
| 697 |
+
"""
|
| 698 |
+
for k in range(0, self.rows):
|
| 699 |
+
self[k, i], self[k, j] = self[k, j], self[k, i]
|
| 700 |
+
|
| 701 |
+
def row_op(self, i, f):
|
| 702 |
+
"""In-place operation on row ``i`` using two-arg functor whose args are
|
| 703 |
+
interpreted as ``(self[i, j], j)``.
|
| 704 |
+
|
| 705 |
+
Examples
|
| 706 |
+
========
|
| 707 |
+
|
| 708 |
+
>>> from sympy import eye
|
| 709 |
+
>>> M = eye(3)
|
| 710 |
+
>>> M.row_op(1, lambda v, j: v + 2*M[0, j]); M
|
| 711 |
+
Matrix([
|
| 712 |
+
[1, 0, 0],
|
| 713 |
+
[2, 1, 0],
|
| 714 |
+
[0, 0, 1]])
|
| 715 |
+
|
| 716 |
+
See Also
|
| 717 |
+
========
|
| 718 |
+
row
|
| 719 |
+
zip_row_op
|
| 720 |
+
col_op
|
| 721 |
+
|
| 722 |
+
"""
|
| 723 |
+
for j in range(self.cols):
|
| 724 |
+
self[i, j] = f(self[i, j], j)
|
| 725 |
+
|
| 726 |
+
#The next three methods give direct support for the most common row operations inplace.
|
| 727 |
+
def row_mult(self,i,factor):
|
| 728 |
+
"""Multiply the given row by the given factor in-place.
|
| 729 |
+
|
| 730 |
+
Examples
|
| 731 |
+
========
|
| 732 |
+
|
| 733 |
+
>>> from sympy import eye
|
| 734 |
+
>>> M = eye(3)
|
| 735 |
+
>>> M.row_mult(1,7); M
|
| 736 |
+
Matrix([
|
| 737 |
+
[1, 0, 0],
|
| 738 |
+
[0, 7, 0],
|
| 739 |
+
[0, 0, 1]])
|
| 740 |
+
|
| 741 |
+
"""
|
| 742 |
+
for j in range(self.cols):
|
| 743 |
+
self[i,j] *= factor
|
| 744 |
+
|
| 745 |
+
def row_add(self,s,t,k):
|
| 746 |
+
"""Add k times row s (source) to row t (target) in place.
|
| 747 |
+
|
| 748 |
+
Examples
|
| 749 |
+
========
|
| 750 |
+
|
| 751 |
+
>>> from sympy import eye
|
| 752 |
+
>>> M = eye(3)
|
| 753 |
+
>>> M.row_add(0, 2,3); M
|
| 754 |
+
Matrix([
|
| 755 |
+
[1, 0, 0],
|
| 756 |
+
[0, 1, 0],
|
| 757 |
+
[3, 0, 1]])
|
| 758 |
+
"""
|
| 759 |
+
|
| 760 |
+
for j in range(self.cols):
|
| 761 |
+
self[t,j] += k*self[s,j]
|
| 762 |
+
|
| 763 |
+
def row_swap(self, i, j):
|
| 764 |
+
"""Swap the two given rows of the matrix in-place.
|
| 765 |
+
|
| 766 |
+
Examples
|
| 767 |
+
========
|
| 768 |
+
|
| 769 |
+
>>> from sympy import Matrix
|
| 770 |
+
>>> M = Matrix([[0, 1], [1, 0]])
|
| 771 |
+
>>> M
|
| 772 |
+
Matrix([
|
| 773 |
+
[0, 1],
|
| 774 |
+
[1, 0]])
|
| 775 |
+
>>> M.row_swap(0, 1)
|
| 776 |
+
>>> M
|
| 777 |
+
Matrix([
|
| 778 |
+
[1, 0],
|
| 779 |
+
[0, 1]])
|
| 780 |
+
|
| 781 |
+
See Also
|
| 782 |
+
========
|
| 783 |
+
|
| 784 |
+
row
|
| 785 |
+
col_swap
|
| 786 |
+
"""
|
| 787 |
+
for k in range(0, self.cols):
|
| 788 |
+
self[i, k], self[j, k] = self[j, k], self[i, k]
|
| 789 |
+
|
| 790 |
+
def zip_row_op(self, i, k, f):
|
| 791 |
+
"""In-place operation on row ``i`` using two-arg functor whose args are
|
| 792 |
+
interpreted as ``(self[i, j], self[k, j])``.
|
| 793 |
+
|
| 794 |
+
Examples
|
| 795 |
+
========
|
| 796 |
+
|
| 797 |
+
>>> from sympy import eye
|
| 798 |
+
>>> M = eye(3)
|
| 799 |
+
>>> M.zip_row_op(1, 0, lambda v, u: v + 2*u); M
|
| 800 |
+
Matrix([
|
| 801 |
+
[1, 0, 0],
|
| 802 |
+
[2, 1, 0],
|
| 803 |
+
[0, 0, 1]])
|
| 804 |
+
|
| 805 |
+
See Also
|
| 806 |
+
========
|
| 807 |
+
row
|
| 808 |
+
row_op
|
| 809 |
+
col_op
|
| 810 |
+
|
| 811 |
+
"""
|
| 812 |
+
for j in range(self.cols):
|
| 813 |
+
self[i, j] = f(self[i, j], self[k, j])
|
| 814 |
+
|
| 815 |
+
def copyin_list(self, key, value):
|
| 816 |
+
"""Copy in elements from a list.
|
| 817 |
+
|
| 818 |
+
Parameters
|
| 819 |
+
==========
|
| 820 |
+
|
| 821 |
+
key : slice
|
| 822 |
+
The section of this matrix to replace.
|
| 823 |
+
value : iterable
|
| 824 |
+
The iterable to copy values from.
|
| 825 |
+
|
| 826 |
+
Examples
|
| 827 |
+
========
|
| 828 |
+
|
| 829 |
+
>>> from sympy import eye
|
| 830 |
+
>>> I = eye(3)
|
| 831 |
+
>>> I[:2, 0] = [1, 2] # col
|
| 832 |
+
>>> I
|
| 833 |
+
Matrix([
|
| 834 |
+
[1, 0, 0],
|
| 835 |
+
[2, 1, 0],
|
| 836 |
+
[0, 0, 1]])
|
| 837 |
+
>>> I[1, :2] = [[3, 4]]
|
| 838 |
+
>>> I
|
| 839 |
+
Matrix([
|
| 840 |
+
[1, 0, 0],
|
| 841 |
+
[3, 4, 0],
|
| 842 |
+
[0, 0, 1]])
|
| 843 |
+
|
| 844 |
+
See Also
|
| 845 |
+
========
|
| 846 |
+
|
| 847 |
+
copyin_matrix
|
| 848 |
+
"""
|
| 849 |
+
if not is_sequence(value):
|
| 850 |
+
raise TypeError("`value` must be an ordered iterable, not %s." % type(value))
|
| 851 |
+
return self.copyin_matrix(key, type(self)(value))
|
| 852 |
+
|
| 853 |
+
def copyin_matrix(self, key, value):
|
| 854 |
+
"""Copy in values from a matrix into the given bounds.
|
| 855 |
+
|
| 856 |
+
Parameters
|
| 857 |
+
==========
|
| 858 |
+
|
| 859 |
+
key : slice
|
| 860 |
+
The section of this matrix to replace.
|
| 861 |
+
value : Matrix
|
| 862 |
+
The matrix to copy values from.
|
| 863 |
+
|
| 864 |
+
Examples
|
| 865 |
+
========
|
| 866 |
+
|
| 867 |
+
>>> from sympy import Matrix, eye
|
| 868 |
+
>>> M = Matrix([[0, 1], [2, 3], [4, 5]])
|
| 869 |
+
>>> I = eye(3)
|
| 870 |
+
>>> I[:3, :2] = M
|
| 871 |
+
>>> I
|
| 872 |
+
Matrix([
|
| 873 |
+
[0, 1, 0],
|
| 874 |
+
[2, 3, 0],
|
| 875 |
+
[4, 5, 1]])
|
| 876 |
+
>>> I[0, 1] = M
|
| 877 |
+
>>> I
|
| 878 |
+
Matrix([
|
| 879 |
+
[0, 0, 1],
|
| 880 |
+
[2, 2, 3],
|
| 881 |
+
[4, 4, 5]])
|
| 882 |
+
|
| 883 |
+
See Also
|
| 884 |
+
========
|
| 885 |
+
|
| 886 |
+
copyin_list
|
| 887 |
+
"""
|
| 888 |
+
rlo, rhi, clo, chi = self.key2bounds(key)
|
| 889 |
+
shape = value.shape
|
| 890 |
+
dr, dc = rhi - rlo, chi - clo
|
| 891 |
+
if shape != (dr, dc):
|
| 892 |
+
raise ShapeError(filldedent("The Matrix `value` doesn't have the "
|
| 893 |
+
"same dimensions "
|
| 894 |
+
"as the in sub-Matrix given by `key`."))
|
| 895 |
+
|
| 896 |
+
for i in range(value.rows):
|
| 897 |
+
for j in range(value.cols):
|
| 898 |
+
self[i + rlo, j + clo] = value[i, j]
|
| 899 |
+
|
| 900 |
+
def fill(self, value):
|
| 901 |
+
"""Fill self with the given value.
|
| 902 |
+
|
| 903 |
+
Notes
|
| 904 |
+
=====
|
| 905 |
+
|
| 906 |
+
Unless many values are going to be deleted (i.e. set to zero)
|
| 907 |
+
this will create a matrix that is slower than a dense matrix in
|
| 908 |
+
operations.
|
| 909 |
+
|
| 910 |
+
Examples
|
| 911 |
+
========
|
| 912 |
+
|
| 913 |
+
>>> from sympy import SparseMatrix
|
| 914 |
+
>>> M = SparseMatrix.zeros(3); M
|
| 915 |
+
Matrix([
|
| 916 |
+
[0, 0, 0],
|
| 917 |
+
[0, 0, 0],
|
| 918 |
+
[0, 0, 0]])
|
| 919 |
+
>>> M.fill(1); M
|
| 920 |
+
Matrix([
|
| 921 |
+
[1, 1, 1],
|
| 922 |
+
[1, 1, 1],
|
| 923 |
+
[1, 1, 1]])
|
| 924 |
+
|
| 925 |
+
See Also
|
| 926 |
+
========
|
| 927 |
+
|
| 928 |
+
zeros
|
| 929 |
+
ones
|
| 930 |
+
"""
|
| 931 |
+
value = _sympify(value)
|
| 932 |
+
if not value:
|
| 933 |
+
self._rep = DomainMatrix.zeros(self.shape, EXRAW)
|
| 934 |
+
else:
|
| 935 |
+
elements_dod = {i: dict.fromkeys(range(self.cols), value) for i in range(self.rows)}
|
| 936 |
+
self._rep = DomainMatrix(elements_dod, self.shape, EXRAW)
|
| 937 |
+
|
| 938 |
+
|
| 939 |
+
def _getitem_RepMatrix(self, key):
|
| 940 |
+
"""Return portion of self defined by key. If the key involves a slice
|
| 941 |
+
then a list will be returned (if key is a single slice) or a matrix
|
| 942 |
+
(if key was a tuple involving a slice).
|
| 943 |
+
|
| 944 |
+
Examples
|
| 945 |
+
========
|
| 946 |
+
|
| 947 |
+
>>> from sympy import Matrix, I
|
| 948 |
+
>>> m = Matrix([
|
| 949 |
+
... [1, 2 + I],
|
| 950 |
+
... [3, 4 ]])
|
| 951 |
+
|
| 952 |
+
If the key is a tuple that does not involve a slice then that element
|
| 953 |
+
is returned:
|
| 954 |
+
|
| 955 |
+
>>> m[1, 0]
|
| 956 |
+
3
|
| 957 |
+
|
| 958 |
+
When a tuple key involves a slice, a matrix is returned. Here, the
|
| 959 |
+
first column is selected (all rows, column 0):
|
| 960 |
+
|
| 961 |
+
>>> m[:, 0]
|
| 962 |
+
Matrix([
|
| 963 |
+
[1],
|
| 964 |
+
[3]])
|
| 965 |
+
|
| 966 |
+
If the slice is not a tuple then it selects from the underlying
|
| 967 |
+
list of elements that are arranged in row order and a list is
|
| 968 |
+
returned if a slice is involved:
|
| 969 |
+
|
| 970 |
+
>>> m[0]
|
| 971 |
+
1
|
| 972 |
+
>>> m[::2]
|
| 973 |
+
[1, 3]
|
| 974 |
+
"""
|
| 975 |
+
if isinstance(key, tuple):
|
| 976 |
+
i, j = key
|
| 977 |
+
try:
|
| 978 |
+
return self._rep.getitem_sympy(index_(i), index_(j))
|
| 979 |
+
except (TypeError, IndexError):
|
| 980 |
+
if (isinstance(i, Expr) and not i.is_number) or (isinstance(j, Expr) and not j.is_number):
|
| 981 |
+
if ((j < 0) is True) or ((j >= self.shape[1]) is True) or\
|
| 982 |
+
((i < 0) is True) or ((i >= self.shape[0]) is True):
|
| 983 |
+
raise ValueError("index out of boundary")
|
| 984 |
+
from sympy.matrices.expressions.matexpr import MatrixElement
|
| 985 |
+
return MatrixElement(self, i, j)
|
| 986 |
+
|
| 987 |
+
if isinstance(i, slice):
|
| 988 |
+
i = range(self.rows)[i]
|
| 989 |
+
elif is_sequence(i):
|
| 990 |
+
pass
|
| 991 |
+
else:
|
| 992 |
+
i = [i]
|
| 993 |
+
if isinstance(j, slice):
|
| 994 |
+
j = range(self.cols)[j]
|
| 995 |
+
elif is_sequence(j):
|
| 996 |
+
pass
|
| 997 |
+
else:
|
| 998 |
+
j = [j]
|
| 999 |
+
return self.extract(i, j)
|
| 1000 |
+
|
| 1001 |
+
else:
|
| 1002 |
+
# Index/slice like a flattened list
|
| 1003 |
+
rows, cols = self.shape
|
| 1004 |
+
|
| 1005 |
+
# Raise the appropriate exception:
|
| 1006 |
+
if not rows * cols:
|
| 1007 |
+
return [][key]
|
| 1008 |
+
|
| 1009 |
+
rep = self._rep.rep
|
| 1010 |
+
domain = rep.domain
|
| 1011 |
+
is_slice = isinstance(key, slice)
|
| 1012 |
+
|
| 1013 |
+
if is_slice:
|
| 1014 |
+
values = [rep.getitem(*divmod(n, cols)) for n in range(rows * cols)[key]]
|
| 1015 |
+
else:
|
| 1016 |
+
values = [rep.getitem(*divmod(index_(key), cols))]
|
| 1017 |
+
|
| 1018 |
+
if domain != EXRAW:
|
| 1019 |
+
to_sympy = domain.to_sympy
|
| 1020 |
+
values = [to_sympy(val) for val in values]
|
| 1021 |
+
|
| 1022 |
+
if is_slice:
|
| 1023 |
+
return values
|
| 1024 |
+
else:
|
| 1025 |
+
return values[0]
|
tuning-competition-baseline/.ven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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.function import expand_mul
|
| 2 |
+
from sympy.core.symbol import Dummy, uniquely_named_symbol, symbols
|
| 3 |
+
from sympy.utilities.iterables import numbered_symbols
|
| 4 |
+
|
| 5 |
+
from .exceptions import ShapeError, NonSquareMatrixError, NonInvertibleMatrixError
|
| 6 |
+
from .eigen import _fuzzy_positive_definite
|
| 7 |
+
from .utilities import _get_intermediate_simp, _iszero
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _diagonal_solve(M, rhs):
|
| 11 |
+
"""Solves ``Ax = B`` efficiently, where A is a diagonal Matrix,
|
| 12 |
+
with non-zero diagonal entries.
|
| 13 |
+
|
| 14 |
+
Examples
|
| 15 |
+
========
|
| 16 |
+
|
| 17 |
+
>>> from sympy import Matrix, eye
|
| 18 |
+
>>> A = eye(2)*2
|
| 19 |
+
>>> B = Matrix([[1, 2], [3, 4]])
|
| 20 |
+
>>> A.diagonal_solve(B) == B/2
|
| 21 |
+
True
|
| 22 |
+
|
| 23 |
+
See Also
|
| 24 |
+
========
|
| 25 |
+
|
| 26 |
+
sympy.matrices.dense.DenseMatrix.lower_triangular_solve
|
| 27 |
+
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
|
| 28 |
+
gauss_jordan_solve
|
| 29 |
+
cholesky_solve
|
| 30 |
+
LDLsolve
|
| 31 |
+
LUsolve
|
| 32 |
+
QRsolve
|
| 33 |
+
pinv_solve
|
| 34 |
+
cramer_solve
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
if not M.is_diagonal():
|
| 38 |
+
raise TypeError("Matrix should be diagonal")
|
| 39 |
+
if rhs.rows != M.rows:
|
| 40 |
+
raise TypeError("Size mismatch")
|
| 41 |
+
|
| 42 |
+
return M._new(
|
| 43 |
+
rhs.rows, rhs.cols, lambda i, j: rhs[i, j] / M[i, i])
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _lower_triangular_solve(M, rhs):
|
| 47 |
+
"""Solves ``Ax = B``, where A is a lower triangular matrix.
|
| 48 |
+
|
| 49 |
+
See Also
|
| 50 |
+
========
|
| 51 |
+
|
| 52 |
+
upper_triangular_solve
|
| 53 |
+
gauss_jordan_solve
|
| 54 |
+
cholesky_solve
|
| 55 |
+
diagonal_solve
|
| 56 |
+
LDLsolve
|
| 57 |
+
LUsolve
|
| 58 |
+
QRsolve
|
| 59 |
+
pinv_solve
|
| 60 |
+
cramer_solve
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
from .dense import MutableDenseMatrix
|
| 64 |
+
|
| 65 |
+
if not M.is_square:
|
| 66 |
+
raise NonSquareMatrixError("Matrix must be square.")
|
| 67 |
+
if rhs.rows != M.rows:
|
| 68 |
+
raise ShapeError("Matrices size mismatch.")
|
| 69 |
+
if not M.is_lower:
|
| 70 |
+
raise ValueError("Matrix must be lower triangular.")
|
| 71 |
+
|
| 72 |
+
dps = _get_intermediate_simp()
|
| 73 |
+
X = MutableDenseMatrix.zeros(M.rows, rhs.cols)
|
| 74 |
+
|
| 75 |
+
for j in range(rhs.cols):
|
| 76 |
+
for i in range(M.rows):
|
| 77 |
+
if M[i, i] == 0:
|
| 78 |
+
raise TypeError("Matrix must be non-singular.")
|
| 79 |
+
|
| 80 |
+
X[i, j] = dps((rhs[i, j] - sum(M[i, k]*X[k, j]
|
| 81 |
+
for k in range(i))) / M[i, i])
|
| 82 |
+
|
| 83 |
+
return M._new(X)
|
| 84 |
+
|
| 85 |
+
def _lower_triangular_solve_sparse(M, rhs):
|
| 86 |
+
"""Solves ``Ax = B``, where A is a lower triangular matrix.
|
| 87 |
+
|
| 88 |
+
See Also
|
| 89 |
+
========
|
| 90 |
+
|
| 91 |
+
upper_triangular_solve
|
| 92 |
+
gauss_jordan_solve
|
| 93 |
+
cholesky_solve
|
| 94 |
+
diagonal_solve
|
| 95 |
+
LDLsolve
|
| 96 |
+
LUsolve
|
| 97 |
+
QRsolve
|
| 98 |
+
pinv_solve
|
| 99 |
+
cramer_solve
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
if not M.is_square:
|
| 103 |
+
raise NonSquareMatrixError("Matrix must be square.")
|
| 104 |
+
if rhs.rows != M.rows:
|
| 105 |
+
raise ShapeError("Matrices size mismatch.")
|
| 106 |
+
if not M.is_lower:
|
| 107 |
+
raise ValueError("Matrix must be lower triangular.")
|
| 108 |
+
|
| 109 |
+
dps = _get_intermediate_simp()
|
| 110 |
+
rows = [[] for i in range(M.rows)]
|
| 111 |
+
|
| 112 |
+
for i, j, v in M.row_list():
|
| 113 |
+
if i > j:
|
| 114 |
+
rows[i].append((j, v))
|
| 115 |
+
|
| 116 |
+
X = rhs.as_mutable()
|
| 117 |
+
|
| 118 |
+
for j in range(rhs.cols):
|
| 119 |
+
for i in range(rhs.rows):
|
| 120 |
+
for u, v in rows[i]:
|
| 121 |
+
X[i, j] -= v*X[u, j]
|
| 122 |
+
|
| 123 |
+
X[i, j] = dps(X[i, j] / M[i, i])
|
| 124 |
+
|
| 125 |
+
return M._new(X)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def _upper_triangular_solve(M, rhs):
|
| 129 |
+
"""Solves ``Ax = B``, where A is an upper triangular matrix.
|
| 130 |
+
|
| 131 |
+
See Also
|
| 132 |
+
========
|
| 133 |
+
|
| 134 |
+
lower_triangular_solve
|
| 135 |
+
gauss_jordan_solve
|
| 136 |
+
cholesky_solve
|
| 137 |
+
diagonal_solve
|
| 138 |
+
LDLsolve
|
| 139 |
+
LUsolve
|
| 140 |
+
QRsolve
|
| 141 |
+
pinv_solve
|
| 142 |
+
cramer_solve
|
| 143 |
+
"""
|
| 144 |
+
|
| 145 |
+
from .dense import MutableDenseMatrix
|
| 146 |
+
|
| 147 |
+
if not M.is_square:
|
| 148 |
+
raise NonSquareMatrixError("Matrix must be square.")
|
| 149 |
+
if rhs.rows != M.rows:
|
| 150 |
+
raise ShapeError("Matrix size mismatch.")
|
| 151 |
+
if not M.is_upper:
|
| 152 |
+
raise TypeError("Matrix is not upper triangular.")
|
| 153 |
+
|
| 154 |
+
dps = _get_intermediate_simp()
|
| 155 |
+
X = MutableDenseMatrix.zeros(M.rows, rhs.cols)
|
| 156 |
+
|
| 157 |
+
for j in range(rhs.cols):
|
| 158 |
+
for i in reversed(range(M.rows)):
|
| 159 |
+
if M[i, i] == 0:
|
| 160 |
+
raise ValueError("Matrix must be non-singular.")
|
| 161 |
+
|
| 162 |
+
X[i, j] = dps((rhs[i, j] - sum(M[i, k]*X[k, j]
|
| 163 |
+
for k in range(i + 1, M.rows))) / M[i, i])
|
| 164 |
+
|
| 165 |
+
return M._new(X)
|
| 166 |
+
|
| 167 |
+
def _upper_triangular_solve_sparse(M, rhs):
|
| 168 |
+
"""Solves ``Ax = B``, where A is an upper triangular matrix.
|
| 169 |
+
|
| 170 |
+
See Also
|
| 171 |
+
========
|
| 172 |
+
|
| 173 |
+
lower_triangular_solve
|
| 174 |
+
gauss_jordan_solve
|
| 175 |
+
cholesky_solve
|
| 176 |
+
diagonal_solve
|
| 177 |
+
LDLsolve
|
| 178 |
+
LUsolve
|
| 179 |
+
QRsolve
|
| 180 |
+
pinv_solve
|
| 181 |
+
cramer_solve
|
| 182 |
+
"""
|
| 183 |
+
|
| 184 |
+
if not M.is_square:
|
| 185 |
+
raise NonSquareMatrixError("Matrix must be square.")
|
| 186 |
+
if rhs.rows != M.rows:
|
| 187 |
+
raise ShapeError("Matrix size mismatch.")
|
| 188 |
+
if not M.is_upper:
|
| 189 |
+
raise TypeError("Matrix is not upper triangular.")
|
| 190 |
+
|
| 191 |
+
dps = _get_intermediate_simp()
|
| 192 |
+
rows = [[] for i in range(M.rows)]
|
| 193 |
+
|
| 194 |
+
for i, j, v in M.row_list():
|
| 195 |
+
if i < j:
|
| 196 |
+
rows[i].append((j, v))
|
| 197 |
+
|
| 198 |
+
X = rhs.as_mutable()
|
| 199 |
+
|
| 200 |
+
for j in range(rhs.cols):
|
| 201 |
+
for i in reversed(range(rhs.rows)):
|
| 202 |
+
for u, v in reversed(rows[i]):
|
| 203 |
+
X[i, j] -= v*X[u, j]
|
| 204 |
+
|
| 205 |
+
X[i, j] = dps(X[i, j] / M[i, i])
|
| 206 |
+
|
| 207 |
+
return M._new(X)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def _cholesky_solve(M, rhs):
|
| 211 |
+
"""Solves ``Ax = B`` using Cholesky decomposition,
|
| 212 |
+
for a general square non-singular matrix.
|
| 213 |
+
For a non-square matrix with rows > cols,
|
| 214 |
+
the least squares solution is returned.
|
| 215 |
+
|
| 216 |
+
See Also
|
| 217 |
+
========
|
| 218 |
+
|
| 219 |
+
sympy.matrices.dense.DenseMatrix.lower_triangular_solve
|
| 220 |
+
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
|
| 221 |
+
gauss_jordan_solve
|
| 222 |
+
diagonal_solve
|
| 223 |
+
LDLsolve
|
| 224 |
+
LUsolve
|
| 225 |
+
QRsolve
|
| 226 |
+
pinv_solve
|
| 227 |
+
cramer_solve
|
| 228 |
+
"""
|
| 229 |
+
|
| 230 |
+
if M.rows < M.cols:
|
| 231 |
+
raise NotImplementedError(
|
| 232 |
+
'Under-determined System. Try M.gauss_jordan_solve(rhs)')
|
| 233 |
+
|
| 234 |
+
hermitian = True
|
| 235 |
+
reform = False
|
| 236 |
+
|
| 237 |
+
if M.is_symmetric():
|
| 238 |
+
hermitian = False
|
| 239 |
+
elif not M.is_hermitian:
|
| 240 |
+
reform = True
|
| 241 |
+
|
| 242 |
+
if reform or _fuzzy_positive_definite(M) is False:
|
| 243 |
+
H = M.H
|
| 244 |
+
M = H.multiply(M)
|
| 245 |
+
rhs = H.multiply(rhs)
|
| 246 |
+
hermitian = not M.is_symmetric()
|
| 247 |
+
|
| 248 |
+
L = M.cholesky(hermitian=hermitian)
|
| 249 |
+
Y = L.lower_triangular_solve(rhs)
|
| 250 |
+
|
| 251 |
+
if hermitian:
|
| 252 |
+
return (L.H).upper_triangular_solve(Y)
|
| 253 |
+
else:
|
| 254 |
+
return (L.T).upper_triangular_solve(Y)
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
def _LDLsolve(M, rhs):
|
| 258 |
+
"""Solves ``Ax = B`` using LDL decomposition,
|
| 259 |
+
for a general square and non-singular matrix.
|
| 260 |
+
|
| 261 |
+
For a non-square matrix with rows > cols,
|
| 262 |
+
the least squares solution is returned.
|
| 263 |
+
|
| 264 |
+
Examples
|
| 265 |
+
========
|
| 266 |
+
|
| 267 |
+
>>> from sympy import Matrix, eye
|
| 268 |
+
>>> A = eye(2)*2
|
| 269 |
+
>>> B = Matrix([[1, 2], [3, 4]])
|
| 270 |
+
>>> A.LDLsolve(B) == B/2
|
| 271 |
+
True
|
| 272 |
+
|
| 273 |
+
See Also
|
| 274 |
+
========
|
| 275 |
+
|
| 276 |
+
sympy.matrices.dense.DenseMatrix.LDLdecomposition
|
| 277 |
+
sympy.matrices.dense.DenseMatrix.lower_triangular_solve
|
| 278 |
+
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
|
| 279 |
+
gauss_jordan_solve
|
| 280 |
+
cholesky_solve
|
| 281 |
+
diagonal_solve
|
| 282 |
+
LUsolve
|
| 283 |
+
QRsolve
|
| 284 |
+
pinv_solve
|
| 285 |
+
cramer_solve
|
| 286 |
+
"""
|
| 287 |
+
|
| 288 |
+
if M.rows < M.cols:
|
| 289 |
+
raise NotImplementedError(
|
| 290 |
+
'Under-determined System. Try M.gauss_jordan_solve(rhs)')
|
| 291 |
+
|
| 292 |
+
hermitian = True
|
| 293 |
+
reform = False
|
| 294 |
+
|
| 295 |
+
if M.is_symmetric():
|
| 296 |
+
hermitian = False
|
| 297 |
+
elif not M.is_hermitian:
|
| 298 |
+
reform = True
|
| 299 |
+
|
| 300 |
+
if reform or _fuzzy_positive_definite(M) is False:
|
| 301 |
+
H = M.H
|
| 302 |
+
M = H.multiply(M)
|
| 303 |
+
rhs = H.multiply(rhs)
|
| 304 |
+
hermitian = not M.is_symmetric()
|
| 305 |
+
|
| 306 |
+
L, D = M.LDLdecomposition(hermitian=hermitian)
|
| 307 |
+
Y = L.lower_triangular_solve(rhs)
|
| 308 |
+
Z = D.diagonal_solve(Y)
|
| 309 |
+
|
| 310 |
+
if hermitian:
|
| 311 |
+
return (L.H).upper_triangular_solve(Z)
|
| 312 |
+
else:
|
| 313 |
+
return (L.T).upper_triangular_solve(Z)
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
def _LUsolve(M, rhs, iszerofunc=_iszero):
|
| 317 |
+
"""Solve the linear system ``Ax = rhs`` for ``x`` where ``A = M``.
|
| 318 |
+
|
| 319 |
+
This is for symbolic matrices, for real or complex ones use
|
| 320 |
+
mpmath.lu_solve or mpmath.qr_solve.
|
| 321 |
+
|
| 322 |
+
See Also
|
| 323 |
+
========
|
| 324 |
+
|
| 325 |
+
sympy.matrices.dense.DenseMatrix.lower_triangular_solve
|
| 326 |
+
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
|
| 327 |
+
gauss_jordan_solve
|
| 328 |
+
cholesky_solve
|
| 329 |
+
diagonal_solve
|
| 330 |
+
LDLsolve
|
| 331 |
+
QRsolve
|
| 332 |
+
pinv_solve
|
| 333 |
+
LUdecomposition
|
| 334 |
+
cramer_solve
|
| 335 |
+
"""
|
| 336 |
+
|
| 337 |
+
if rhs.rows != M.rows:
|
| 338 |
+
raise ShapeError(
|
| 339 |
+
"``M`` and ``rhs`` must have the same number of rows.")
|
| 340 |
+
|
| 341 |
+
m = M.rows
|
| 342 |
+
n = M.cols
|
| 343 |
+
|
| 344 |
+
if m < n:
|
| 345 |
+
raise NotImplementedError("Underdetermined systems not supported.")
|
| 346 |
+
|
| 347 |
+
try:
|
| 348 |
+
A, perm = M.LUdecomposition_Simple(
|
| 349 |
+
iszerofunc=iszerofunc, rankcheck=True)
|
| 350 |
+
except ValueError:
|
| 351 |
+
raise NonInvertibleMatrixError("Matrix det == 0; not invertible.")
|
| 352 |
+
|
| 353 |
+
dps = _get_intermediate_simp()
|
| 354 |
+
b = rhs.permute_rows(perm).as_mutable()
|
| 355 |
+
|
| 356 |
+
# forward substitution, all diag entries are scaled to 1
|
| 357 |
+
for i in range(m):
|
| 358 |
+
for j in range(min(i, n)):
|
| 359 |
+
scale = A[i, j]
|
| 360 |
+
b.zip_row_op(i, j, lambda x, y: dps(x - y * scale))
|
| 361 |
+
|
| 362 |
+
# consistency check for overdetermined systems
|
| 363 |
+
if m > n:
|
| 364 |
+
for i in range(n, m):
|
| 365 |
+
for j in range(b.cols):
|
| 366 |
+
if not iszerofunc(b[i, j]):
|
| 367 |
+
raise ValueError("The system is inconsistent.")
|
| 368 |
+
|
| 369 |
+
b = b[0:n, :] # truncate zero rows if consistent
|
| 370 |
+
|
| 371 |
+
# backward substitution
|
| 372 |
+
for i in range(n - 1, -1, -1):
|
| 373 |
+
for j in range(i + 1, n):
|
| 374 |
+
scale = A[i, j]
|
| 375 |
+
b.zip_row_op(i, j, lambda x, y: dps(x - y * scale))
|
| 376 |
+
|
| 377 |
+
scale = A[i, i]
|
| 378 |
+
b.row_op(i, lambda x, _: dps(x / scale))
|
| 379 |
+
|
| 380 |
+
return rhs.__class__(b)
|
| 381 |
+
|
| 382 |
+
|
| 383 |
+
def _QRsolve(M, b):
|
| 384 |
+
"""Solve the linear system ``Ax = b``.
|
| 385 |
+
|
| 386 |
+
``M`` is the matrix ``A``, the method argument is the vector
|
| 387 |
+
``b``. The method returns the solution vector ``x``. If ``b`` is a
|
| 388 |
+
matrix, the system is solved for each column of ``b`` and the
|
| 389 |
+
return value is a matrix of the same shape as ``b``.
|
| 390 |
+
|
| 391 |
+
This method is slower (approximately by a factor of 2) but
|
| 392 |
+
more stable for floating-point arithmetic than the LUsolve method.
|
| 393 |
+
However, LUsolve usually uses an exact arithmetic, so you do not need
|
| 394 |
+
to use QRsolve.
|
| 395 |
+
|
| 396 |
+
This is mainly for educational purposes and symbolic matrices, for real
|
| 397 |
+
(or complex) matrices use mpmath.qr_solve.
|
| 398 |
+
|
| 399 |
+
See Also
|
| 400 |
+
========
|
| 401 |
+
|
| 402 |
+
sympy.matrices.dense.DenseMatrix.lower_triangular_solve
|
| 403 |
+
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
|
| 404 |
+
gauss_jordan_solve
|
| 405 |
+
cholesky_solve
|
| 406 |
+
diagonal_solve
|
| 407 |
+
LDLsolve
|
| 408 |
+
LUsolve
|
| 409 |
+
pinv_solve
|
| 410 |
+
QRdecomposition
|
| 411 |
+
cramer_solve
|
| 412 |
+
"""
|
| 413 |
+
|
| 414 |
+
dps = _get_intermediate_simp(expand_mul, expand_mul)
|
| 415 |
+
Q, R = M.QRdecomposition()
|
| 416 |
+
y = Q.T * b
|
| 417 |
+
|
| 418 |
+
# back substitution to solve R*x = y:
|
| 419 |
+
# We build up the result "backwards" in the vector 'x' and reverse it
|
| 420 |
+
# only in the end.
|
| 421 |
+
x = []
|
| 422 |
+
n = R.rows
|
| 423 |
+
|
| 424 |
+
for j in range(n - 1, -1, -1):
|
| 425 |
+
tmp = y[j, :]
|
| 426 |
+
|
| 427 |
+
for k in range(j + 1, n):
|
| 428 |
+
tmp -= R[j, k] * x[n - 1 - k]
|
| 429 |
+
|
| 430 |
+
tmp = dps(tmp)
|
| 431 |
+
|
| 432 |
+
x.append(tmp / R[j, j])
|
| 433 |
+
|
| 434 |
+
return M.vstack(*x[::-1])
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
def _gauss_jordan_solve(M, B, freevar=False):
|
| 438 |
+
"""
|
| 439 |
+
Solves ``Ax = B`` using Gauss Jordan elimination.
|
| 440 |
+
|
| 441 |
+
There may be zero, one, or infinite solutions. If one solution
|
| 442 |
+
exists, it will be returned. If infinite solutions exist, it will
|
| 443 |
+
be returned parametrically. If no solutions exist, It will throw
|
| 444 |
+
ValueError.
|
| 445 |
+
|
| 446 |
+
Parameters
|
| 447 |
+
==========
|
| 448 |
+
|
| 449 |
+
B : Matrix
|
| 450 |
+
The right hand side of the equation to be solved for. Must have
|
| 451 |
+
the same number of rows as matrix A.
|
| 452 |
+
|
| 453 |
+
freevar : boolean, optional
|
| 454 |
+
Flag, when set to `True` will return the indices of the free
|
| 455 |
+
variables in the solutions (column Matrix), for a system that is
|
| 456 |
+
undetermined (e.g. A has more columns than rows), for which
|
| 457 |
+
infinite solutions are possible, in terms of arbitrary
|
| 458 |
+
values of free variables. Default `False`.
|
| 459 |
+
|
| 460 |
+
Returns
|
| 461 |
+
=======
|
| 462 |
+
|
| 463 |
+
x : Matrix
|
| 464 |
+
The matrix that will satisfy ``Ax = B``. Will have as many rows as
|
| 465 |
+
matrix A has columns, and as many columns as matrix B.
|
| 466 |
+
|
| 467 |
+
params : Matrix
|
| 468 |
+
If the system is underdetermined (e.g. A has more columns than
|
| 469 |
+
rows), infinite solutions are possible, in terms of arbitrary
|
| 470 |
+
parameters. These arbitrary parameters are returned as params
|
| 471 |
+
Matrix.
|
| 472 |
+
|
| 473 |
+
free_var_index : List, optional
|
| 474 |
+
If the system is underdetermined (e.g. A has more columns than
|
| 475 |
+
rows), infinite solutions are possible, in terms of arbitrary
|
| 476 |
+
values of free variables. Then the indices of the free variables
|
| 477 |
+
in the solutions (column Matrix) are returned by free_var_index,
|
| 478 |
+
if the flag `freevar` is set to `True`.
|
| 479 |
+
|
| 480 |
+
Examples
|
| 481 |
+
========
|
| 482 |
+
|
| 483 |
+
>>> from sympy import Matrix
|
| 484 |
+
>>> A = Matrix([[1, 2, 1, 1], [1, 2, 2, -1], [2, 4, 0, 6]])
|
| 485 |
+
>>> B = Matrix([7, 12, 4])
|
| 486 |
+
>>> sol, params = A.gauss_jordan_solve(B)
|
| 487 |
+
>>> sol
|
| 488 |
+
Matrix([
|
| 489 |
+
[-2*tau0 - 3*tau1 + 2],
|
| 490 |
+
[ tau0],
|
| 491 |
+
[ 2*tau1 + 5],
|
| 492 |
+
[ tau1]])
|
| 493 |
+
>>> params
|
| 494 |
+
Matrix([
|
| 495 |
+
[tau0],
|
| 496 |
+
[tau1]])
|
| 497 |
+
>>> taus_zeroes = { tau:0 for tau in params }
|
| 498 |
+
>>> sol_unique = sol.xreplace(taus_zeroes)
|
| 499 |
+
>>> sol_unique
|
| 500 |
+
Matrix([
|
| 501 |
+
[2],
|
| 502 |
+
[0],
|
| 503 |
+
[5],
|
| 504 |
+
[0]])
|
| 505 |
+
|
| 506 |
+
|
| 507 |
+
>>> A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
|
| 508 |
+
>>> B = Matrix([3, 6, 9])
|
| 509 |
+
>>> sol, params = A.gauss_jordan_solve(B)
|
| 510 |
+
>>> sol
|
| 511 |
+
Matrix([
|
| 512 |
+
[-1],
|
| 513 |
+
[ 2],
|
| 514 |
+
[ 0]])
|
| 515 |
+
>>> params
|
| 516 |
+
Matrix(0, 1, [])
|
| 517 |
+
|
| 518 |
+
>>> A = Matrix([[2, -7], [-1, 4]])
|
| 519 |
+
>>> B = Matrix([[-21, 3], [12, -2]])
|
| 520 |
+
>>> sol, params = A.gauss_jordan_solve(B)
|
| 521 |
+
>>> sol
|
| 522 |
+
Matrix([
|
| 523 |
+
[0, -2],
|
| 524 |
+
[3, -1]])
|
| 525 |
+
>>> params
|
| 526 |
+
Matrix(0, 2, [])
|
| 527 |
+
|
| 528 |
+
|
| 529 |
+
>>> from sympy import Matrix
|
| 530 |
+
>>> A = Matrix([[1, 2, 1, 1], [1, 2, 2, -1], [2, 4, 0, 6]])
|
| 531 |
+
>>> B = Matrix([7, 12, 4])
|
| 532 |
+
>>> sol, params, freevars = A.gauss_jordan_solve(B, freevar=True)
|
| 533 |
+
>>> sol
|
| 534 |
+
Matrix([
|
| 535 |
+
[-2*tau0 - 3*tau1 + 2],
|
| 536 |
+
[ tau0],
|
| 537 |
+
[ 2*tau1 + 5],
|
| 538 |
+
[ tau1]])
|
| 539 |
+
>>> params
|
| 540 |
+
Matrix([
|
| 541 |
+
[tau0],
|
| 542 |
+
[tau1]])
|
| 543 |
+
>>> freevars
|
| 544 |
+
[1, 3]
|
| 545 |
+
|
| 546 |
+
|
| 547 |
+
See Also
|
| 548 |
+
========
|
| 549 |
+
|
| 550 |
+
sympy.matrices.dense.DenseMatrix.lower_triangular_solve
|
| 551 |
+
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
|
| 552 |
+
cholesky_solve
|
| 553 |
+
diagonal_solve
|
| 554 |
+
LDLsolve
|
| 555 |
+
LUsolve
|
| 556 |
+
QRsolve
|
| 557 |
+
pinv
|
| 558 |
+
|
| 559 |
+
References
|
| 560 |
+
==========
|
| 561 |
+
|
| 562 |
+
.. [1] https://en.wikipedia.org/wiki/Gaussian_elimination
|
| 563 |
+
|
| 564 |
+
"""
|
| 565 |
+
|
| 566 |
+
from sympy.matrices import Matrix, zeros
|
| 567 |
+
|
| 568 |
+
cls = M.__class__
|
| 569 |
+
aug = M.hstack(M.copy(), B.copy())
|
| 570 |
+
B_cols = B.cols
|
| 571 |
+
row, col = aug[:, :-B_cols].shape
|
| 572 |
+
|
| 573 |
+
# solve by reduced row echelon form
|
| 574 |
+
A, pivots = aug.rref(simplify=True)
|
| 575 |
+
A, v = A[:, :-B_cols], A[:, -B_cols:]
|
| 576 |
+
pivots = list(filter(lambda p: p < col, pivots))
|
| 577 |
+
rank = len(pivots)
|
| 578 |
+
|
| 579 |
+
# Get index of free symbols (free parameters)
|
| 580 |
+
# non-pivots columns are free variables
|
| 581 |
+
free_var_index = [c for c in range(A.cols) if c not in pivots]
|
| 582 |
+
|
| 583 |
+
# Bring to block form
|
| 584 |
+
permutation = Matrix(pivots + free_var_index).T
|
| 585 |
+
|
| 586 |
+
# check for existence of solutions
|
| 587 |
+
# rank of aug Matrix should be equal to rank of coefficient matrix
|
| 588 |
+
if not v[rank:, :].is_zero_matrix:
|
| 589 |
+
raise ValueError("Linear system has no solution")
|
| 590 |
+
|
| 591 |
+
# Free parameters
|
| 592 |
+
# what are current unnumbered free symbol names?
|
| 593 |
+
name = uniquely_named_symbol('tau', [aug],
|
| 594 |
+
compare=lambda i: str(i).rstrip('1234567890'),
|
| 595 |
+
modify=lambda s: '_' + s).name
|
| 596 |
+
gen = numbered_symbols(name)
|
| 597 |
+
tau = Matrix([next(gen) for k in range((col - rank)*B_cols)]).reshape(
|
| 598 |
+
col - rank, B_cols)
|
| 599 |
+
|
| 600 |
+
# Full parametric solution
|
| 601 |
+
V = A[:rank, free_var_index]
|
| 602 |
+
vt = v[:rank, :]
|
| 603 |
+
free_sol = tau.vstack(vt - V * tau, tau)
|
| 604 |
+
|
| 605 |
+
# Undo permutation
|
| 606 |
+
sol = zeros(col, B_cols)
|
| 607 |
+
|
| 608 |
+
for k in range(col):
|
| 609 |
+
sol[permutation[k], :] = free_sol[k,:]
|
| 610 |
+
|
| 611 |
+
sol, tau = cls(sol), cls(tau)
|
| 612 |
+
|
| 613 |
+
if freevar:
|
| 614 |
+
return sol, tau, free_var_index
|
| 615 |
+
else:
|
| 616 |
+
return sol, tau
|
| 617 |
+
|
| 618 |
+
|
| 619 |
+
def _pinv_solve(M, B, arbitrary_matrix=None):
|
| 620 |
+
"""Solve ``Ax = B`` using the Moore-Penrose pseudoinverse.
|
| 621 |
+
|
| 622 |
+
There may be zero, one, or infinite solutions. If one solution
|
| 623 |
+
exists, it will be returned. If infinite solutions exist, one will
|
| 624 |
+
be returned based on the value of arbitrary_matrix. If no solutions
|
| 625 |
+
exist, the least-squares solution is returned.
|
| 626 |
+
|
| 627 |
+
Parameters
|
| 628 |
+
==========
|
| 629 |
+
|
| 630 |
+
B : Matrix
|
| 631 |
+
The right hand side of the equation to be solved for. Must have
|
| 632 |
+
the same number of rows as matrix A.
|
| 633 |
+
arbitrary_matrix : Matrix
|
| 634 |
+
If the system is underdetermined (e.g. A has more columns than
|
| 635 |
+
rows), infinite solutions are possible, in terms of an arbitrary
|
| 636 |
+
matrix. This parameter may be set to a specific matrix to use
|
| 637 |
+
for that purpose; if so, it must be the same shape as x, with as
|
| 638 |
+
many rows as matrix A has columns, and as many columns as matrix
|
| 639 |
+
B. If left as None, an appropriate matrix containing dummy
|
| 640 |
+
symbols in the form of ``wn_m`` will be used, with n and m being
|
| 641 |
+
row and column position of each symbol.
|
| 642 |
+
|
| 643 |
+
Returns
|
| 644 |
+
=======
|
| 645 |
+
|
| 646 |
+
x : Matrix
|
| 647 |
+
The matrix that will satisfy ``Ax = B``. Will have as many rows as
|
| 648 |
+
matrix A has columns, and as many columns as matrix B.
|
| 649 |
+
|
| 650 |
+
Examples
|
| 651 |
+
========
|
| 652 |
+
|
| 653 |
+
>>> from sympy import Matrix
|
| 654 |
+
>>> A = Matrix([[1, 2, 3], [4, 5, 6]])
|
| 655 |
+
>>> B = Matrix([7, 8])
|
| 656 |
+
>>> A.pinv_solve(B)
|
| 657 |
+
Matrix([
|
| 658 |
+
[ _w0_0/6 - _w1_0/3 + _w2_0/6 - 55/18],
|
| 659 |
+
[-_w0_0/3 + 2*_w1_0/3 - _w2_0/3 + 1/9],
|
| 660 |
+
[ _w0_0/6 - _w1_0/3 + _w2_0/6 + 59/18]])
|
| 661 |
+
>>> A.pinv_solve(B, arbitrary_matrix=Matrix([0, 0, 0]))
|
| 662 |
+
Matrix([
|
| 663 |
+
[-55/18],
|
| 664 |
+
[ 1/9],
|
| 665 |
+
[ 59/18]])
|
| 666 |
+
|
| 667 |
+
See Also
|
| 668 |
+
========
|
| 669 |
+
|
| 670 |
+
sympy.matrices.dense.DenseMatrix.lower_triangular_solve
|
| 671 |
+
sympy.matrices.dense.DenseMatrix.upper_triangular_solve
|
| 672 |
+
gauss_jordan_solve
|
| 673 |
+
cholesky_solve
|
| 674 |
+
diagonal_solve
|
| 675 |
+
LDLsolve
|
| 676 |
+
LUsolve
|
| 677 |
+
QRsolve
|
| 678 |
+
pinv
|
| 679 |
+
|
| 680 |
+
Notes
|
| 681 |
+
=====
|
| 682 |
+
|
| 683 |
+
This may return either exact solutions or least squares solutions.
|
| 684 |
+
To determine which, check ``A * A.pinv() * B == B``. It will be
|
| 685 |
+
True if exact solutions exist, and False if only a least-squares
|
| 686 |
+
solution exists. Be aware that the left hand side of that equation
|
| 687 |
+
may need to be simplified to correctly compare to the right hand
|
| 688 |
+
side.
|
| 689 |
+
|
| 690 |
+
References
|
| 691 |
+
==========
|
| 692 |
+
|
| 693 |
+
.. [1] https://en.wikipedia.org/wiki/Moore-Penrose_pseudoinverse#Obtaining_all_solutions_of_a_linear_system
|
| 694 |
+
|
| 695 |
+
"""
|
| 696 |
+
|
| 697 |
+
from sympy.matrices import eye
|
| 698 |
+
|
| 699 |
+
A = M
|
| 700 |
+
A_pinv = M.pinv()
|
| 701 |
+
|
| 702 |
+
if arbitrary_matrix is None:
|
| 703 |
+
rows, cols = A.cols, B.cols
|
| 704 |
+
w = symbols('w:{}_:{}'.format(rows, cols), cls=Dummy)
|
| 705 |
+
arbitrary_matrix = M.__class__(cols, rows, w).T
|
| 706 |
+
|
| 707 |
+
return A_pinv.multiply(B) + (eye(A.cols) -
|
| 708 |
+
A_pinv.multiply(A)).multiply(arbitrary_matrix)
|
| 709 |
+
|
| 710 |
+
|
| 711 |
+
def _cramer_solve(M, rhs, det_method="laplace"):
|
| 712 |
+
"""Solves system of linear equations using Cramer's rule.
|
| 713 |
+
|
| 714 |
+
This method is relatively inefficient compared to other methods.
|
| 715 |
+
However it only uses a single division, assuming a division-free determinant
|
| 716 |
+
method is provided. This is helpful to minimize the chance of divide-by-zero
|
| 717 |
+
cases in symbolic solutions to linear systems.
|
| 718 |
+
|
| 719 |
+
Parameters
|
| 720 |
+
==========
|
| 721 |
+
M : Matrix
|
| 722 |
+
The matrix representing the left hand side of the equation.
|
| 723 |
+
rhs : Matrix
|
| 724 |
+
The matrix representing the right hand side of the equation.
|
| 725 |
+
det_method : str or callable
|
| 726 |
+
The method to use to calculate the determinant of the matrix.
|
| 727 |
+
The default is ``'laplace'``. If a callable is passed, it should take a
|
| 728 |
+
single argument, the matrix, and return the determinant of the matrix.
|
| 729 |
+
|
| 730 |
+
Returns
|
| 731 |
+
=======
|
| 732 |
+
x : Matrix
|
| 733 |
+
The matrix that will satisfy ``Ax = B``. Will have as many rows as
|
| 734 |
+
matrix A has columns, and as many columns as matrix B.
|
| 735 |
+
|
| 736 |
+
Examples
|
| 737 |
+
========
|
| 738 |
+
|
| 739 |
+
>>> from sympy import Matrix
|
| 740 |
+
>>> A = Matrix([[0, -6, 1], [0, -6, -1], [-5, -2, 3]])
|
| 741 |
+
>>> B = Matrix([[-30, -9], [-18, -27], [-26, 46]])
|
| 742 |
+
>>> x = A.cramer_solve(B)
|
| 743 |
+
>>> x
|
| 744 |
+
Matrix([
|
| 745 |
+
[ 0, -5],
|
| 746 |
+
[ 4, 3],
|
| 747 |
+
[-6, 9]])
|
| 748 |
+
|
| 749 |
+
References
|
| 750 |
+
==========
|
| 751 |
+
|
| 752 |
+
.. [1] https://en.wikipedia.org/wiki/Cramer%27s_rule#Explicit_formulas_for_small_systems
|
| 753 |
+
|
| 754 |
+
"""
|
| 755 |
+
from .dense import zeros
|
| 756 |
+
|
| 757 |
+
def entry(i, j):
|
| 758 |
+
return rhs[i, sol] if j == col else M[i, j]
|
| 759 |
+
|
| 760 |
+
if det_method == "bird":
|
| 761 |
+
from .determinant import _det_bird
|
| 762 |
+
det = _det_bird
|
| 763 |
+
elif det_method == "laplace":
|
| 764 |
+
from .determinant import _det_laplace
|
| 765 |
+
det = _det_laplace
|
| 766 |
+
elif isinstance(det_method, str):
|
| 767 |
+
det = lambda matrix: matrix.det(method=det_method)
|
| 768 |
+
else:
|
| 769 |
+
det = det_method
|
| 770 |
+
det_M = det(M)
|
| 771 |
+
x = zeros(*rhs.shape)
|
| 772 |
+
for sol in range(rhs.shape[1]):
|
| 773 |
+
for col in range(rhs.shape[0]):
|
| 774 |
+
x[col, sol] = det(M.__class__(*M.shape, entry)) / det_M
|
| 775 |
+
return M.__class__(x)
|
| 776 |
+
|
| 777 |
+
|
| 778 |
+
def _solve(M, rhs, method='GJ'):
|
| 779 |
+
"""Solves linear equation where the unique solution exists.
|
| 780 |
+
|
| 781 |
+
Parameters
|
| 782 |
+
==========
|
| 783 |
+
|
| 784 |
+
rhs : Matrix
|
| 785 |
+
Vector representing the right hand side of the linear equation.
|
| 786 |
+
|
| 787 |
+
method : string, optional
|
| 788 |
+
If set to ``'GJ'`` or ``'GE'``, the Gauss-Jordan elimination will be
|
| 789 |
+
used, which is implemented in the routine ``gauss_jordan_solve``.
|
| 790 |
+
|
| 791 |
+
If set to ``'LU'``, ``LUsolve`` routine will be used.
|
| 792 |
+
|
| 793 |
+
If set to ``'QR'``, ``QRsolve`` routine will be used.
|
| 794 |
+
|
| 795 |
+
If set to ``'PINV'``, ``pinv_solve`` routine will be used.
|
| 796 |
+
|
| 797 |
+
If set to ``'CRAMER'``, ``cramer_solve`` routine will be used.
|
| 798 |
+
|
| 799 |
+
It also supports the methods available for special linear systems
|
| 800 |
+
|
| 801 |
+
For positive definite systems:
|
| 802 |
+
|
| 803 |
+
If set to ``'CH'``, ``cholesky_solve`` routine will be used.
|
| 804 |
+
|
| 805 |
+
If set to ``'LDL'``, ``LDLsolve`` routine will be used.
|
| 806 |
+
|
| 807 |
+
To use a different method and to compute the solution via the
|
| 808 |
+
inverse, use a method defined in the .inv() docstring.
|
| 809 |
+
|
| 810 |
+
Returns
|
| 811 |
+
=======
|
| 812 |
+
|
| 813 |
+
solutions : Matrix
|
| 814 |
+
Vector representing the solution.
|
| 815 |
+
|
| 816 |
+
Raises
|
| 817 |
+
======
|
| 818 |
+
|
| 819 |
+
ValueError
|
| 820 |
+
If there is not a unique solution then a ``ValueError`` will be
|
| 821 |
+
raised.
|
| 822 |
+
|
| 823 |
+
If ``M`` is not square, a ``ValueError`` and a different routine
|
| 824 |
+
for solving the system will be suggested.
|
| 825 |
+
"""
|
| 826 |
+
|
| 827 |
+
if method in ('GJ', 'GE'):
|
| 828 |
+
try:
|
| 829 |
+
soln, param = M.gauss_jordan_solve(rhs)
|
| 830 |
+
|
| 831 |
+
if param:
|
| 832 |
+
raise NonInvertibleMatrixError("Matrix det == 0; not invertible. "
|
| 833 |
+
"Try ``M.gauss_jordan_solve(rhs)`` to obtain a parametric solution.")
|
| 834 |
+
|
| 835 |
+
except ValueError:
|
| 836 |
+
raise NonInvertibleMatrixError("Matrix det == 0; not invertible.")
|
| 837 |
+
|
| 838 |
+
return soln
|
| 839 |
+
|
| 840 |
+
elif method == 'LU':
|
| 841 |
+
return M.LUsolve(rhs)
|
| 842 |
+
elif method == 'CH':
|
| 843 |
+
return M.cholesky_solve(rhs)
|
| 844 |
+
elif method == 'QR':
|
| 845 |
+
return M.QRsolve(rhs)
|
| 846 |
+
elif method == 'LDL':
|
| 847 |
+
return M.LDLsolve(rhs)
|
| 848 |
+
elif method == 'PINV':
|
| 849 |
+
return M.pinv_solve(rhs)
|
| 850 |
+
elif method == 'CRAMER':
|
| 851 |
+
return M.cramer_solve(rhs)
|
| 852 |
+
else:
|
| 853 |
+
return M.inv(method=method).multiply(rhs)
|
| 854 |
+
|
| 855 |
+
|
| 856 |
+
def _solve_least_squares(M, rhs, method='CH'):
|
| 857 |
+
"""Return the least-square fit to the data.
|
| 858 |
+
|
| 859 |
+
Parameters
|
| 860 |
+
==========
|
| 861 |
+
|
| 862 |
+
rhs : Matrix
|
| 863 |
+
Vector representing the right hand side of the linear equation.
|
| 864 |
+
|
| 865 |
+
method : string or boolean, optional
|
| 866 |
+
If set to ``'CH'``, ``cholesky_solve`` routine will be used.
|
| 867 |
+
|
| 868 |
+
If set to ``'LDL'``, ``LDLsolve`` routine will be used.
|
| 869 |
+
|
| 870 |
+
If set to ``'QR'``, ``QRsolve`` routine will be used.
|
| 871 |
+
|
| 872 |
+
If set to ``'PINV'``, ``pinv_solve`` routine will be used.
|
| 873 |
+
|
| 874 |
+
Otherwise, the conjugate of ``M`` will be used to create a system
|
| 875 |
+
of equations that is passed to ``solve`` along with the hint
|
| 876 |
+
defined by ``method``.
|
| 877 |
+
|
| 878 |
+
Returns
|
| 879 |
+
=======
|
| 880 |
+
|
| 881 |
+
solutions : Matrix
|
| 882 |
+
Vector representing the solution.
|
| 883 |
+
|
| 884 |
+
Examples
|
| 885 |
+
========
|
| 886 |
+
|
| 887 |
+
>>> from sympy import Matrix, ones
|
| 888 |
+
>>> A = Matrix([1, 2, 3])
|
| 889 |
+
>>> B = Matrix([2, 3, 4])
|
| 890 |
+
>>> S = Matrix(A.row_join(B))
|
| 891 |
+
>>> S
|
| 892 |
+
Matrix([
|
| 893 |
+
[1, 2],
|
| 894 |
+
[2, 3],
|
| 895 |
+
[3, 4]])
|
| 896 |
+
|
| 897 |
+
If each line of S represent coefficients of Ax + By
|
| 898 |
+
and x and y are [2, 3] then S*xy is:
|
| 899 |
+
|
| 900 |
+
>>> r = S*Matrix([2, 3]); r
|
| 901 |
+
Matrix([
|
| 902 |
+
[ 8],
|
| 903 |
+
[13],
|
| 904 |
+
[18]])
|
| 905 |
+
|
| 906 |
+
But let's add 1 to the middle value and then solve for the
|
| 907 |
+
least-squares value of xy:
|
| 908 |
+
|
| 909 |
+
>>> xy = S.solve_least_squares(Matrix([8, 14, 18])); xy
|
| 910 |
+
Matrix([
|
| 911 |
+
[ 5/3],
|
| 912 |
+
[10/3]])
|
| 913 |
+
|
| 914 |
+
The error is given by S*xy - r:
|
| 915 |
+
|
| 916 |
+
>>> S*xy - r
|
| 917 |
+
Matrix([
|
| 918 |
+
[1/3],
|
| 919 |
+
[1/3],
|
| 920 |
+
[1/3]])
|
| 921 |
+
>>> _.norm().n(2)
|
| 922 |
+
0.58
|
| 923 |
+
|
| 924 |
+
If a different xy is used, the norm will be higher:
|
| 925 |
+
|
| 926 |
+
>>> xy += ones(2, 1)/10
|
| 927 |
+
>>> (S*xy - r).norm().n(2)
|
| 928 |
+
1.5
|
| 929 |
+
|
| 930 |
+
"""
|
| 931 |
+
|
| 932 |
+
if method == 'CH':
|
| 933 |
+
return M.cholesky_solve(rhs)
|
| 934 |
+
elif method == 'QR':
|
| 935 |
+
return M.QRsolve(rhs)
|
| 936 |
+
elif method == 'LDL':
|
| 937 |
+
return M.LDLsolve(rhs)
|
| 938 |
+
elif method == 'PINV':
|
| 939 |
+
return M.pinv_solve(rhs)
|
| 940 |
+
else:
|
| 941 |
+
t = M.H
|
| 942 |
+
return (t * M).solve(t * rhs, method=method)
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/sympy/matrices/sparsetools.py
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.containers import Dict
|
| 2 |
+
from sympy.core.symbol import Dummy
|
| 3 |
+
from sympy.utilities.iterables import is_sequence
|
| 4 |
+
from sympy.utilities.misc import as_int, filldedent
|
| 5 |
+
|
| 6 |
+
from .sparse import MutableSparseMatrix as SparseMatrix
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def _doktocsr(dok):
|
| 10 |
+
"""Converts a sparse matrix to Compressed Sparse Row (CSR) format.
|
| 11 |
+
|
| 12 |
+
Parameters
|
| 13 |
+
==========
|
| 14 |
+
|
| 15 |
+
A : contains non-zero elements sorted by key (row, column)
|
| 16 |
+
JA : JA[i] is the column corresponding to A[i]
|
| 17 |
+
IA : IA[i] contains the index in A for the first non-zero element
|
| 18 |
+
of row[i]. Thus IA[i+1] - IA[i] gives number of non-zero
|
| 19 |
+
elements row[i]. The length of IA is always 1 more than the
|
| 20 |
+
number of rows in the matrix.
|
| 21 |
+
|
| 22 |
+
Examples
|
| 23 |
+
========
|
| 24 |
+
|
| 25 |
+
>>> from sympy.matrices.sparsetools import _doktocsr
|
| 26 |
+
>>> from sympy import SparseMatrix, diag
|
| 27 |
+
>>> m = SparseMatrix(diag(1, 2, 3))
|
| 28 |
+
>>> m[2, 0] = -1
|
| 29 |
+
>>> _doktocsr(m)
|
| 30 |
+
[[1, 2, -1, 3], [0, 1, 0, 2], [0, 1, 2, 4], [3, 3]]
|
| 31 |
+
|
| 32 |
+
"""
|
| 33 |
+
row, JA, A = [list(i) for i in zip(*dok.row_list())]
|
| 34 |
+
IA = [0]*((row[0] if row else 0) + 1)
|
| 35 |
+
for i, r in enumerate(row):
|
| 36 |
+
IA.extend([i]*(r - row[i - 1])) # if i = 0 nothing is extended
|
| 37 |
+
IA.extend([len(A)]*(dok.rows - len(IA) + 1))
|
| 38 |
+
shape = [dok.rows, dok.cols]
|
| 39 |
+
return [A, JA, IA, shape]
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def _csrtodok(csr):
|
| 43 |
+
"""Converts a CSR representation to DOK representation.
|
| 44 |
+
|
| 45 |
+
Examples
|
| 46 |
+
========
|
| 47 |
+
|
| 48 |
+
>>> from sympy.matrices.sparsetools import _csrtodok
|
| 49 |
+
>>> _csrtodok([[5, 8, 3, 6], [0, 1, 2, 1], [0, 0, 2, 3, 4], [4, 3]])
|
| 50 |
+
Matrix([
|
| 51 |
+
[0, 0, 0],
|
| 52 |
+
[5, 8, 0],
|
| 53 |
+
[0, 0, 3],
|
| 54 |
+
[0, 6, 0]])
|
| 55 |
+
|
| 56 |
+
"""
|
| 57 |
+
smat = {}
|
| 58 |
+
A, JA, IA, shape = csr
|
| 59 |
+
for i in range(len(IA) - 1):
|
| 60 |
+
indices = slice(IA[i], IA[i + 1])
|
| 61 |
+
for l, m in zip(A[indices], JA[indices]):
|
| 62 |
+
smat[i, m] = l
|
| 63 |
+
return SparseMatrix(*shape, smat)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def banded(*args, **kwargs):
|
| 67 |
+
"""Returns a SparseMatrix from the given dictionary describing
|
| 68 |
+
the diagonals of the matrix. The keys are positive for upper
|
| 69 |
+
diagonals and negative for those below the main diagonal. The
|
| 70 |
+
values may be:
|
| 71 |
+
|
| 72 |
+
* expressions or single-argument functions,
|
| 73 |
+
|
| 74 |
+
* lists or tuples of values,
|
| 75 |
+
|
| 76 |
+
* matrices
|
| 77 |
+
|
| 78 |
+
Unless dimensions are given, the size of the returned matrix will
|
| 79 |
+
be large enough to contain the largest non-zero value provided.
|
| 80 |
+
|
| 81 |
+
kwargs
|
| 82 |
+
======
|
| 83 |
+
|
| 84 |
+
rows : rows of the resulting matrix; computed if
|
| 85 |
+
not given.
|
| 86 |
+
|
| 87 |
+
cols : columns of the resulting matrix; computed if
|
| 88 |
+
not given.
|
| 89 |
+
|
| 90 |
+
Examples
|
| 91 |
+
========
|
| 92 |
+
|
| 93 |
+
>>> from sympy import banded, ones, Matrix
|
| 94 |
+
>>> from sympy.abc import x
|
| 95 |
+
|
| 96 |
+
If explicit values are given in tuples,
|
| 97 |
+
the matrix will autosize to contain all values, otherwise
|
| 98 |
+
a single value is filled onto the entire diagonal:
|
| 99 |
+
|
| 100 |
+
>>> banded({1: (1, 2, 3), -1: (4, 5, 6), 0: x})
|
| 101 |
+
Matrix([
|
| 102 |
+
[x, 1, 0, 0],
|
| 103 |
+
[4, x, 2, 0],
|
| 104 |
+
[0, 5, x, 3],
|
| 105 |
+
[0, 0, 6, x]])
|
| 106 |
+
|
| 107 |
+
A function accepting a single argument can be used to fill the
|
| 108 |
+
diagonal as a function of diagonal index (which starts at 0).
|
| 109 |
+
The size (or shape) of the matrix must be given to obtain more
|
| 110 |
+
than a 1x1 matrix:
|
| 111 |
+
|
| 112 |
+
>>> s = lambda d: (1 + d)**2
|
| 113 |
+
>>> banded(5, {0: s, 2: s, -2: 2})
|
| 114 |
+
Matrix([
|
| 115 |
+
[1, 0, 1, 0, 0],
|
| 116 |
+
[0, 4, 0, 4, 0],
|
| 117 |
+
[2, 0, 9, 0, 9],
|
| 118 |
+
[0, 2, 0, 16, 0],
|
| 119 |
+
[0, 0, 2, 0, 25]])
|
| 120 |
+
|
| 121 |
+
The diagonal of matrices placed on a diagonal will coincide
|
| 122 |
+
with the indicated diagonal:
|
| 123 |
+
|
| 124 |
+
>>> vert = Matrix([1, 2, 3])
|
| 125 |
+
>>> banded({0: vert}, cols=3)
|
| 126 |
+
Matrix([
|
| 127 |
+
[1, 0, 0],
|
| 128 |
+
[2, 1, 0],
|
| 129 |
+
[3, 2, 1],
|
| 130 |
+
[0, 3, 2],
|
| 131 |
+
[0, 0, 3]])
|
| 132 |
+
|
| 133 |
+
>>> banded(4, {0: ones(2)})
|
| 134 |
+
Matrix([
|
| 135 |
+
[1, 1, 0, 0],
|
| 136 |
+
[1, 1, 0, 0],
|
| 137 |
+
[0, 0, 1, 1],
|
| 138 |
+
[0, 0, 1, 1]])
|
| 139 |
+
|
| 140 |
+
Errors are raised if the designated size will not hold
|
| 141 |
+
all values an integral number of times. Here, the rows
|
| 142 |
+
are designated as odd (but an even number is required to
|
| 143 |
+
hold the off-diagonal 2x2 ones):
|
| 144 |
+
|
| 145 |
+
>>> banded({0: 2, 1: ones(2)}, rows=5)
|
| 146 |
+
Traceback (most recent call last):
|
| 147 |
+
...
|
| 148 |
+
ValueError:
|
| 149 |
+
sequence does not fit an integral number of times in the matrix
|
| 150 |
+
|
| 151 |
+
And here, an even number of rows is given...but the square
|
| 152 |
+
matrix has an even number of columns, too. As we saw
|
| 153 |
+
in the previous example, an odd number is required:
|
| 154 |
+
|
| 155 |
+
>>> banded(4, {0: 2, 1: ones(2)}) # trying to make 4x4 and cols must be odd
|
| 156 |
+
Traceback (most recent call last):
|
| 157 |
+
...
|
| 158 |
+
ValueError:
|
| 159 |
+
sequence does not fit an integral number of times in the matrix
|
| 160 |
+
|
| 161 |
+
A way around having to count rows is to enclosing matrix elements
|
| 162 |
+
in a tuple and indicate the desired number of them to the right:
|
| 163 |
+
|
| 164 |
+
>>> banded({0: 2, 2: (ones(2),)*3})
|
| 165 |
+
Matrix([
|
| 166 |
+
[2, 0, 1, 1, 0, 0, 0, 0],
|
| 167 |
+
[0, 2, 1, 1, 0, 0, 0, 0],
|
| 168 |
+
[0, 0, 2, 0, 1, 1, 0, 0],
|
| 169 |
+
[0, 0, 0, 2, 1, 1, 0, 0],
|
| 170 |
+
[0, 0, 0, 0, 2, 0, 1, 1],
|
| 171 |
+
[0, 0, 0, 0, 0, 2, 1, 1]])
|
| 172 |
+
|
| 173 |
+
An error will be raised if more than one value
|
| 174 |
+
is written to a given entry. Here, the ones overlap
|
| 175 |
+
with the main diagonal if they are placed on the
|
| 176 |
+
first diagonal:
|
| 177 |
+
|
| 178 |
+
>>> banded({0: (2,)*5, 1: (ones(2),)*3})
|
| 179 |
+
Traceback (most recent call last):
|
| 180 |
+
...
|
| 181 |
+
ValueError: collision at (1, 1)
|
| 182 |
+
|
| 183 |
+
By placing a 0 at the bottom left of the 2x2 matrix of
|
| 184 |
+
ones, the collision is avoided:
|
| 185 |
+
|
| 186 |
+
>>> u2 = Matrix([
|
| 187 |
+
... [1, 1],
|
| 188 |
+
... [0, 1]])
|
| 189 |
+
>>> banded({0: [2]*5, 1: [u2]*3})
|
| 190 |
+
Matrix([
|
| 191 |
+
[2, 1, 1, 0, 0, 0, 0],
|
| 192 |
+
[0, 2, 1, 0, 0, 0, 0],
|
| 193 |
+
[0, 0, 2, 1, 1, 0, 0],
|
| 194 |
+
[0, 0, 0, 2, 1, 0, 0],
|
| 195 |
+
[0, 0, 0, 0, 2, 1, 1],
|
| 196 |
+
[0, 0, 0, 0, 0, 0, 1]])
|
| 197 |
+
"""
|
| 198 |
+
try:
|
| 199 |
+
if len(args) not in (1, 2, 3):
|
| 200 |
+
raise TypeError
|
| 201 |
+
if not isinstance(args[-1], (dict, Dict)):
|
| 202 |
+
raise TypeError
|
| 203 |
+
if len(args) == 1:
|
| 204 |
+
rows = kwargs.get('rows', None)
|
| 205 |
+
cols = kwargs.get('cols', None)
|
| 206 |
+
if rows is not None:
|
| 207 |
+
rows = as_int(rows)
|
| 208 |
+
if cols is not None:
|
| 209 |
+
cols = as_int(cols)
|
| 210 |
+
elif len(args) == 2:
|
| 211 |
+
rows = cols = as_int(args[0])
|
| 212 |
+
else:
|
| 213 |
+
rows, cols = map(as_int, args[:2])
|
| 214 |
+
# fails with ValueError if any keys are not ints
|
| 215 |
+
_ = all(as_int(k) for k in args[-1])
|
| 216 |
+
except (ValueError, TypeError):
|
| 217 |
+
raise TypeError(filldedent(
|
| 218 |
+
'''unrecognized input to banded:
|
| 219 |
+
expecting [[row,] col,] {int: value}'''))
|
| 220 |
+
def rc(d):
|
| 221 |
+
# return row,col coord of diagonal start
|
| 222 |
+
r = -d if d < 0 else 0
|
| 223 |
+
c = 0 if r else d
|
| 224 |
+
return r, c
|
| 225 |
+
smat = {}
|
| 226 |
+
undone = []
|
| 227 |
+
tba = Dummy()
|
| 228 |
+
# first handle objects with size
|
| 229 |
+
for d, v in args[-1].items():
|
| 230 |
+
r, c = rc(d)
|
| 231 |
+
# note: only list and tuple are recognized since this
|
| 232 |
+
# will allow other Basic objects like Tuple
|
| 233 |
+
# into the matrix if so desired
|
| 234 |
+
if isinstance(v, (list, tuple)):
|
| 235 |
+
extra = 0
|
| 236 |
+
for i, vi in enumerate(v):
|
| 237 |
+
i += extra
|
| 238 |
+
if is_sequence(vi):
|
| 239 |
+
vi = SparseMatrix(vi)
|
| 240 |
+
smat[r + i, c + i] = vi
|
| 241 |
+
extra += min(vi.shape) - 1
|
| 242 |
+
else:
|
| 243 |
+
smat[r + i, c + i] = vi
|
| 244 |
+
elif is_sequence(v):
|
| 245 |
+
v = SparseMatrix(v)
|
| 246 |
+
rv, cv = v.shape
|
| 247 |
+
if rows and cols:
|
| 248 |
+
nr, xr = divmod(rows - r, rv)
|
| 249 |
+
nc, xc = divmod(cols - c, cv)
|
| 250 |
+
x = xr or xc
|
| 251 |
+
do = min(nr, nc)
|
| 252 |
+
elif rows:
|
| 253 |
+
do, x = divmod(rows - r, rv)
|
| 254 |
+
elif cols:
|
| 255 |
+
do, x = divmod(cols - c, cv)
|
| 256 |
+
else:
|
| 257 |
+
do = 1
|
| 258 |
+
x = 0
|
| 259 |
+
if x:
|
| 260 |
+
raise ValueError(filldedent('''
|
| 261 |
+
sequence does not fit an integral number of times
|
| 262 |
+
in the matrix'''))
|
| 263 |
+
j = min(v.shape)
|
| 264 |
+
for i in range(do):
|
| 265 |
+
smat[r, c] = v
|
| 266 |
+
r += j
|
| 267 |
+
c += j
|
| 268 |
+
elif v:
|
| 269 |
+
smat[r, c] = tba
|
| 270 |
+
undone.append((d, v))
|
| 271 |
+
s = SparseMatrix(None, smat) # to expand matrices
|
| 272 |
+
smat = s.todok()
|
| 273 |
+
# check for dim errors here
|
| 274 |
+
if rows is not None and rows < s.rows:
|
| 275 |
+
raise ValueError('Designated rows %s < needed %s' % (rows, s.rows))
|
| 276 |
+
if cols is not None and cols < s.cols:
|
| 277 |
+
raise ValueError('Designated cols %s < needed %s' % (cols, s.cols))
|
| 278 |
+
if rows is cols is None:
|
| 279 |
+
rows = s.rows
|
| 280 |
+
cols = s.cols
|
| 281 |
+
elif rows is not None and cols is None:
|
| 282 |
+
cols = max(rows, s.cols)
|
| 283 |
+
elif cols is not None and rows is None:
|
| 284 |
+
rows = max(cols, s.rows)
|
| 285 |
+
def update(i, j, v):
|
| 286 |
+
# update smat and make sure there are
|
| 287 |
+
# no collisions
|
| 288 |
+
if v:
|
| 289 |
+
if (i, j) in smat and smat[i, j] not in (tba, v):
|
| 290 |
+
raise ValueError('collision at %s' % ((i, j),))
|
| 291 |
+
smat[i, j] = v
|
| 292 |
+
if undone:
|
| 293 |
+
for d, vi in undone:
|
| 294 |
+
r, c = rc(d)
|
| 295 |
+
v = vi if callable(vi) else lambda _: vi
|
| 296 |
+
i = 0
|
| 297 |
+
while r + i < rows and c + i < cols:
|
| 298 |
+
update(r + i, c + i, v(i))
|
| 299 |
+
i += 1
|
| 300 |
+
return SparseMatrix(rows, cols, smat)
|