Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_cosh_ops.h +50 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/_index_put_impl.h +44 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/_pdist_forward_ops.h +39 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/_sparse_sum_compositeimplicitautograd_dispatch.h +25 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/_validate_sparse_bsr_tensor_args.h +30 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/add_meta_dispatch.h +26 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/cartesian_prod_native.h +21 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/celu_ops.h +50 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/chunk_ops.h +28 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/diagflat_native.h +21 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/digamma_compositeexplicitautogradnonfunctional_dispatch.h +24 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/fake_quantize_per_tensor_affine_cachemask.h +39 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/fbgemm_linear_fp16_weight_native.h +21 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/gelu_meta_dispatch.h +26 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/huber_loss_backward_compositeexplicitautograd_dispatch.h +23 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/linalg_svdvals.h +39 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/max_unpool3d_cuda_dispatch.h +28 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/mkldnn_max_pool3d_compositeexplicitautograd_dispatch.h +24 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/narrow_ops.h +39 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/renorm_meta_dispatch.h +26 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/slow_conv_dilated2d_cuda_dispatch.h +24 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/special_legendre_polynomial_p_compositeexplicitautogradnonfunctional_dispatch.h +23 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/thnn_conv2d_ops.h +39 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/values_copy_ops.h +39 -0
- videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/view_as_complex_copy.h +39 -0
- vllm/lib/python3.10/site-packages/sympy/concrete/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/sympy/polys/__init__.py +130 -0
- vllm/lib/python3.10/site-packages/sympy/polys/appellseqs.py +269 -0
- vllm/lib/python3.10/site-packages/sympy/polys/compatibility.py +1142 -0
- vllm/lib/python3.10/site-packages/sympy/polys/densearith.py +1875 -0
- vllm/lib/python3.10/site-packages/sympy/polys/densetools.py +1363 -0
- vllm/lib/python3.10/site-packages/sympy/polys/dispersion.py +212 -0
- vllm/lib/python3.10/site-packages/sympy/polys/distributedmodules.py +739 -0
- vllm/lib/python3.10/site-packages/sympy/polys/domainmatrix.py +12 -0
- vllm/lib/python3.10/site-packages/sympy/polys/euclidtools.py +1912 -0
- vllm/lib/python3.10/site-packages/sympy/polys/factortools.py +1648 -0
- vllm/lib/python3.10/site-packages/sympy/polys/galoistools.py +2532 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/__init__.py +15 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/_dfm.py +898 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/_typing.py +16 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/ddm.py +1029 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/dense.py +824 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/dfm.py +35 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/domainmatrix.py +0 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/domainscalar.py +122 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/eigen.py +90 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/exceptions.py +67 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/linsolve.py +230 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/lll.py +94 -0
- vllm/lib/python3.10/site-packages/sympy/polys/matrices/normalforms.py +406 -0
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_cosh_ops.h
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Operator.h
|
| 4 |
+
|
| 5 |
+
#include <tuple>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 9 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 10 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 11 |
+
#include <ATen/core/ATen_fwd.h>
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
namespace _ops {
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
struct TORCH_API _foreach_cosh {
|
| 18 |
+
using schema = ::std::vector<at::Tensor> (at::TensorList);
|
| 19 |
+
using ptr_schema = schema*;
|
| 20 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 21 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_foreach_cosh")
|
| 22 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
|
| 23 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_foreach_cosh(Tensor[] self) -> Tensor[]")
|
| 24 |
+
static ::std::vector<at::Tensor> call(at::TensorList self);
|
| 25 |
+
static ::std::vector<at::Tensor> redispatch(c10::DispatchKeySet dispatchKeySet, at::TensorList self);
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
struct TORCH_API _foreach_cosh_ {
|
| 29 |
+
using schema = void (at::TensorList);
|
| 30 |
+
using ptr_schema = schema*;
|
| 31 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 32 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_foreach_cosh_")
|
| 33 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
|
| 34 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_foreach_cosh_(Tensor(a!)[] self) -> ()")
|
| 35 |
+
static void call(at::TensorList self);
|
| 36 |
+
static void redispatch(c10::DispatchKeySet dispatchKeySet, at::TensorList self);
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
struct TORCH_API _foreach_cosh_out {
|
| 40 |
+
using schema = void (at::TensorList, at::TensorList);
|
| 41 |
+
using ptr_schema = schema*;
|
| 42 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 43 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_foreach_cosh")
|
| 44 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "out")
|
| 45 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_foreach_cosh.out(Tensor[] self, *, Tensor(a!)[] out) -> ()")
|
| 46 |
+
static void call(at::TensorList self, at::TensorList out);
|
| 47 |
+
static void redispatch(c10::DispatchKeySet dispatchKeySet, at::TensorList self, at::TensorList out);
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
}} // namespace at::_ops
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/_index_put_impl.h
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Function.h
|
| 4 |
+
|
| 5 |
+
#include <ATen/Context.h>
|
| 6 |
+
#include <ATen/DeviceGuard.h>
|
| 7 |
+
#include <ATen/TensorUtils.h>
|
| 8 |
+
#include <ATen/TracerMode.h>
|
| 9 |
+
#include <ATen/core/Generator.h>
|
| 10 |
+
#include <ATen/core/Reduction.h>
|
| 11 |
+
#include <ATen/core/Tensor.h>
|
| 12 |
+
#include <c10/core/Scalar.h>
|
| 13 |
+
#include <c10/core/Storage.h>
|
| 14 |
+
#include <c10/core/TensorOptions.h>
|
| 15 |
+
#include <c10/util/Deprecated.h>
|
| 16 |
+
#include <c10/util/Optional.h>
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
#include <ATen/ops/_index_put_impl_ops.h>
|
| 21 |
+
|
| 22 |
+
namespace at {
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
// aten::_index_put_impl_(Tensor(a!) self, Tensor?[] indices, Tensor values, bool accumulate=False, bool unsafe=False) -> Tensor(a!)
|
| 26 |
+
inline at::Tensor & _index_put_impl_(at::Tensor & self, const c10::List<c10::optional<at::Tensor>> & indices, const at::Tensor & values, bool accumulate=false, bool unsafe=false) {
|
| 27 |
+
return at::_ops::_index_put_impl_::call(self, indices, values, accumulate, unsafe);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// aten::_index_put_impl.out(Tensor self, Tensor?[] indices, Tensor values, bool accumulate=False, bool unsafe=False, *, Tensor(a!) out) -> Tensor(a!)
|
| 31 |
+
inline at::Tensor & _index_put_impl_out(at::Tensor & out, const at::Tensor & self, const c10::List<c10::optional<at::Tensor>> & indices, const at::Tensor & values, bool accumulate=false, bool unsafe=false) {
|
| 32 |
+
return at::_ops::_index_put_impl_out::call(self, indices, values, accumulate, unsafe, out);
|
| 33 |
+
}
|
| 34 |
+
// aten::_index_put_impl.out(Tensor self, Tensor?[] indices, Tensor values, bool accumulate=False, bool unsafe=False, *, Tensor(a!) out) -> Tensor(a!)
|
| 35 |
+
inline at::Tensor & _index_put_impl_outf(const at::Tensor & self, const c10::List<c10::optional<at::Tensor>> & indices, const at::Tensor & values, bool accumulate, bool unsafe, at::Tensor & out) {
|
| 36 |
+
return at::_ops::_index_put_impl_out::call(self, indices, values, accumulate, unsafe, out);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
// aten::_index_put_impl(Tensor self, Tensor?[] indices, Tensor values, bool accumulate=False, bool unsafe=False) -> Tensor
|
| 40 |
+
inline at::Tensor _index_put_impl(const at::Tensor & self, const c10::List<c10::optional<at::Tensor>> & indices, const at::Tensor & values, bool accumulate=false, bool unsafe=false) {
|
| 41 |
+
return at::_ops::_index_put_impl::call(self, indices, values, accumulate, unsafe);
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
}
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/_pdist_forward_ops.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Operator.h
|
| 4 |
+
|
| 5 |
+
#include <tuple>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 9 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 10 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 11 |
+
#include <ATen/core/ATen_fwd.h>
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
namespace _ops {
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
struct TORCH_API _pdist_forward {
|
| 18 |
+
using schema = at::Tensor (const at::Tensor &, double);
|
| 19 |
+
using ptr_schema = schema*;
|
| 20 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 21 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_pdist_forward")
|
| 22 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
|
| 23 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_pdist_forward(Tensor self, float p=2) -> Tensor")
|
| 24 |
+
static at::Tensor call(const at::Tensor & self, double p);
|
| 25 |
+
static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, double p);
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
struct TORCH_API _pdist_forward_out {
|
| 29 |
+
using schema = at::Tensor & (const at::Tensor &, double, at::Tensor &);
|
| 30 |
+
using ptr_schema = schema*;
|
| 31 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 32 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_pdist_forward")
|
| 33 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "out")
|
| 34 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_pdist_forward.out(Tensor self, float p=2, *, Tensor(a!) out) -> Tensor(a!)")
|
| 35 |
+
static at::Tensor & call(const at::Tensor & self, double p, at::Tensor & out);
|
| 36 |
+
static at::Tensor & redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, double p, at::Tensor & out);
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
}} // namespace at::_ops
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/_sparse_sum_compositeimplicitautograd_dispatch.h
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace compositeimplicitautograd {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor _sparse_sum(const at::Tensor & self);
|
| 21 |
+
TORCH_API at::Tensor _sparse_sum(const at::Tensor & self, at::ScalarType dtype);
|
| 22 |
+
TORCH_API at::Tensor _sparse_sum(const at::Tensor & self, at::IntArrayRef dim, at::ScalarType dtype);
|
| 23 |
+
|
| 24 |
+
} // namespace compositeimplicitautograd
|
| 25 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/_validate_sparse_bsr_tensor_args.h
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Function.h
|
| 4 |
+
|
| 5 |
+
#include <ATen/Context.h>
|
| 6 |
+
#include <ATen/DeviceGuard.h>
|
| 7 |
+
#include <ATen/TensorUtils.h>
|
| 8 |
+
#include <ATen/TracerMode.h>
|
| 9 |
+
#include <ATen/core/Generator.h>
|
| 10 |
+
#include <ATen/core/Reduction.h>
|
| 11 |
+
#include <ATen/core/Tensor.h>
|
| 12 |
+
#include <c10/core/Scalar.h>
|
| 13 |
+
#include <c10/core/Storage.h>
|
| 14 |
+
#include <c10/core/TensorOptions.h>
|
| 15 |
+
#include <c10/util/Deprecated.h>
|
| 16 |
+
#include <c10/util/Optional.h>
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
#include <ATen/ops/_validate_sparse_bsr_tensor_args_ops.h>
|
| 21 |
+
|
| 22 |
+
namespace at {
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
// aten::_validate_sparse_bsr_tensor_args(Tensor crow_indices, Tensor col_indices, Tensor values, int[] size) -> ()
|
| 26 |
+
inline void _validate_sparse_bsr_tensor_args(const at::Tensor & crow_indices, const at::Tensor & col_indices, const at::Tensor & values, at::IntArrayRef size) {
|
| 27 |
+
return at::_ops::_validate_sparse_bsr_tensor_args::call(crow_indices, col_indices, values, size);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
}
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/add_meta_dispatch.h
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace meta {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor add(const at::Tensor & self, const at::Tensor & other, const at::Scalar & alpha=1);
|
| 21 |
+
TORCH_API at::Tensor & add_out(at::Tensor & out, const at::Tensor & self, const at::Tensor & other, const at::Scalar & alpha=1);
|
| 22 |
+
TORCH_API at::Tensor & add_outf(const at::Tensor & self, const at::Tensor & other, const at::Scalar & alpha, at::Tensor & out);
|
| 23 |
+
TORCH_API at::Tensor & add_(at::Tensor & self, const at::Tensor & other, const at::Scalar & alpha=1);
|
| 24 |
+
|
| 25 |
+
} // namespace meta
|
| 26 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/cartesian_prod_native.h
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from NativeFunction.h
|
| 4 |
+
|
| 5 |
+
#include <c10/core/Scalar.h>
|
| 6 |
+
#include <c10/core/Storage.h>
|
| 7 |
+
#include <c10/core/TensorOptions.h>
|
| 8 |
+
#include <c10/util/Deprecated.h>
|
| 9 |
+
#include <c10/util/Optional.h>
|
| 10 |
+
#include <c10/core/QScheme.h>
|
| 11 |
+
#include <ATen/core/Reduction.h>
|
| 12 |
+
#include <ATen/core/Tensor.h>
|
| 13 |
+
#include <tuple>
|
| 14 |
+
#include <vector>
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
namespace at {
|
| 18 |
+
namespace native {
|
| 19 |
+
TORCH_API at::Tensor cartesian_prod(at::TensorList tensors);
|
| 20 |
+
} // namespace native
|
| 21 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/celu_ops.h
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Operator.h
|
| 4 |
+
|
| 5 |
+
#include <tuple>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 9 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 10 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 11 |
+
#include <ATen/core/ATen_fwd.h>
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
namespace _ops {
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
struct TORCH_API celu {
|
| 18 |
+
using schema = at::Tensor (const at::Tensor &, const at::Scalar &);
|
| 19 |
+
using ptr_schema = schema*;
|
| 20 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 21 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::celu")
|
| 22 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
|
| 23 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "celu(Tensor self, Scalar alpha=1.0) -> Tensor")
|
| 24 |
+
static at::Tensor call(const at::Tensor & self, const at::Scalar & alpha);
|
| 25 |
+
static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, const at::Scalar & alpha);
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
struct TORCH_API celu_ {
|
| 29 |
+
using schema = at::Tensor & (at::Tensor &, const at::Scalar &);
|
| 30 |
+
using ptr_schema = schema*;
|
| 31 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 32 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::celu_")
|
| 33 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
|
| 34 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "celu_(Tensor(a!) self, Scalar alpha=1.0) -> Tensor(a!)")
|
| 35 |
+
static at::Tensor & call(at::Tensor & self, const at::Scalar & alpha);
|
| 36 |
+
static at::Tensor & redispatch(c10::DispatchKeySet dispatchKeySet, at::Tensor & self, const at::Scalar & alpha);
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
struct TORCH_API celu_out {
|
| 40 |
+
using schema = at::Tensor & (const at::Tensor &, const at::Scalar &, at::Tensor &);
|
| 41 |
+
using ptr_schema = schema*;
|
| 42 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 43 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::celu")
|
| 44 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "out")
|
| 45 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "celu.out(Tensor self, Scalar alpha=1.0, *, Tensor(a!) out) -> Tensor(a!)")
|
| 46 |
+
static at::Tensor & call(const at::Tensor & self, const at::Scalar & alpha, at::Tensor & out);
|
| 47 |
+
static at::Tensor & redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, const at::Scalar & alpha, at::Tensor & out);
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
}} // namespace at::_ops
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/chunk_ops.h
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Operator.h
|
| 4 |
+
|
| 5 |
+
#include <tuple>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 9 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 10 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 11 |
+
#include <ATen/core/ATen_fwd.h>
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
namespace _ops {
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
struct TORCH_API chunk {
|
| 18 |
+
using schema = ::std::vector<at::Tensor> (const at::Tensor &, int64_t, int64_t);
|
| 19 |
+
using ptr_schema = schema*;
|
| 20 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 21 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::chunk")
|
| 22 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
|
| 23 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "chunk(Tensor(a -> *) self, int chunks, int dim=0) -> Tensor(a)[]")
|
| 24 |
+
static ::std::vector<at::Tensor> call(const at::Tensor & self, int64_t chunks, int64_t dim);
|
| 25 |
+
static ::std::vector<at::Tensor> redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, int64_t chunks, int64_t dim);
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
}} // namespace at::_ops
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/diagflat_native.h
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from NativeFunction.h
|
| 4 |
+
|
| 5 |
+
#include <c10/core/Scalar.h>
|
| 6 |
+
#include <c10/core/Storage.h>
|
| 7 |
+
#include <c10/core/TensorOptions.h>
|
| 8 |
+
#include <c10/util/Deprecated.h>
|
| 9 |
+
#include <c10/util/Optional.h>
|
| 10 |
+
#include <c10/core/QScheme.h>
|
| 11 |
+
#include <ATen/core/Reduction.h>
|
| 12 |
+
#include <ATen/core/Tensor.h>
|
| 13 |
+
#include <tuple>
|
| 14 |
+
#include <vector>
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
namespace at {
|
| 18 |
+
namespace native {
|
| 19 |
+
TORCH_API at::Tensor diagflat(const at::Tensor & self, int64_t offset=0);
|
| 20 |
+
} // namespace native
|
| 21 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/digamma_compositeexplicitautogradnonfunctional_dispatch.h
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace compositeexplicitautogradnonfunctional {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor digamma(const at::Tensor & self);
|
| 21 |
+
TORCH_API at::Tensor & digamma_(at::Tensor & self);
|
| 22 |
+
|
| 23 |
+
} // namespace compositeexplicitautogradnonfunctional
|
| 24 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/fake_quantize_per_tensor_affine_cachemask.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Function.h
|
| 4 |
+
|
| 5 |
+
#include <ATen/Context.h>
|
| 6 |
+
#include <ATen/DeviceGuard.h>
|
| 7 |
+
#include <ATen/TensorUtils.h>
|
| 8 |
+
#include <ATen/TracerMode.h>
|
| 9 |
+
#include <ATen/core/Generator.h>
|
| 10 |
+
#include <ATen/core/Reduction.h>
|
| 11 |
+
#include <ATen/core/Tensor.h>
|
| 12 |
+
#include <c10/core/Scalar.h>
|
| 13 |
+
#include <c10/core/Storage.h>
|
| 14 |
+
#include <c10/core/TensorOptions.h>
|
| 15 |
+
#include <c10/util/Deprecated.h>
|
| 16 |
+
#include <c10/util/Optional.h>
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
#include <ATen/ops/fake_quantize_per_tensor_affine_cachemask_ops.h>
|
| 21 |
+
|
| 22 |
+
namespace at {
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
// aten::fake_quantize_per_tensor_affine_cachemask(Tensor self, float scale, int zero_point, int quant_min, int quant_max) -> (Tensor output, Tensor mask)
|
| 26 |
+
inline ::std::tuple<at::Tensor,at::Tensor> fake_quantize_per_tensor_affine_cachemask(const at::Tensor & self, double scale, int64_t zero_point, int64_t quant_min, int64_t quant_max) {
|
| 27 |
+
return at::_ops::fake_quantize_per_tensor_affine_cachemask::call(self, scale, zero_point, quant_min, quant_max);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// aten::fake_quantize_per_tensor_affine_cachemask.out(Tensor self, float scale, int zero_point, int quant_min, int quant_max, *, Tensor(a!) out0, Tensor(b!) out1) -> (Tensor(a!), Tensor(b!))
|
| 31 |
+
inline ::std::tuple<at::Tensor &,at::Tensor &> fake_quantize_per_tensor_affine_cachemask_out(at::Tensor & out0, at::Tensor & out1, const at::Tensor & self, double scale, int64_t zero_point, int64_t quant_min, int64_t quant_max) {
|
| 32 |
+
return at::_ops::fake_quantize_per_tensor_affine_cachemask_out::call(self, scale, zero_point, quant_min, quant_max, out0, out1);
|
| 33 |
+
}
|
| 34 |
+
// aten::fake_quantize_per_tensor_affine_cachemask.out(Tensor self, float scale, int zero_point, int quant_min, int quant_max, *, Tensor(a!) out0, Tensor(b!) out1) -> (Tensor(a!), Tensor(b!))
|
| 35 |
+
inline ::std::tuple<at::Tensor &,at::Tensor &> fake_quantize_per_tensor_affine_cachemask_outf(const at::Tensor & self, double scale, int64_t zero_point, int64_t quant_min, int64_t quant_max, at::Tensor & out0, at::Tensor & out1) {
|
| 36 |
+
return at::_ops::fake_quantize_per_tensor_affine_cachemask_out::call(self, scale, zero_point, quant_min, quant_max, out0, out1);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
}
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/fbgemm_linear_fp16_weight_native.h
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from NativeFunction.h
|
| 4 |
+
|
| 5 |
+
#include <c10/core/Scalar.h>
|
| 6 |
+
#include <c10/core/Storage.h>
|
| 7 |
+
#include <c10/core/TensorOptions.h>
|
| 8 |
+
#include <c10/util/Deprecated.h>
|
| 9 |
+
#include <c10/util/Optional.h>
|
| 10 |
+
#include <c10/core/QScheme.h>
|
| 11 |
+
#include <ATen/core/Reduction.h>
|
| 12 |
+
#include <ATen/core/Tensor.h>
|
| 13 |
+
#include <tuple>
|
| 14 |
+
#include <vector>
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
namespace at {
|
| 18 |
+
namespace native {
|
| 19 |
+
TORCH_API at::Tensor fbgemm_linear_fp16_weight(const at::Tensor & input, const at::Tensor & packed_weight, const at::Tensor & bias);
|
| 20 |
+
} // namespace native
|
| 21 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/gelu_meta_dispatch.h
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace meta {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor gelu(const at::Tensor & self, c10::string_view approximate="none");
|
| 21 |
+
TORCH_API at::Tensor & gelu_out(at::Tensor & out, const at::Tensor & self, c10::string_view approximate="none");
|
| 22 |
+
TORCH_API at::Tensor & gelu_outf(const at::Tensor & self, c10::string_view approximate, at::Tensor & out);
|
| 23 |
+
TORCH_API at::Tensor & gelu_(at::Tensor & self, c10::string_view approximate="none");
|
| 24 |
+
|
| 25 |
+
} // namespace meta
|
| 26 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/huber_loss_backward_compositeexplicitautograd_dispatch.h
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace compositeexplicitautograd {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor huber_loss_backward(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & target, int64_t reduction, double delta);
|
| 21 |
+
|
| 22 |
+
} // namespace compositeexplicitautograd
|
| 23 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/linalg_svdvals.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Function.h
|
| 4 |
+
|
| 5 |
+
#include <ATen/Context.h>
|
| 6 |
+
#include <ATen/DeviceGuard.h>
|
| 7 |
+
#include <ATen/TensorUtils.h>
|
| 8 |
+
#include <ATen/TracerMode.h>
|
| 9 |
+
#include <ATen/core/Generator.h>
|
| 10 |
+
#include <ATen/core/Reduction.h>
|
| 11 |
+
#include <ATen/core/Tensor.h>
|
| 12 |
+
#include <c10/core/Scalar.h>
|
| 13 |
+
#include <c10/core/Storage.h>
|
| 14 |
+
#include <c10/core/TensorOptions.h>
|
| 15 |
+
#include <c10/util/Deprecated.h>
|
| 16 |
+
#include <c10/util/Optional.h>
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
#include <ATen/ops/linalg_svdvals_ops.h>
|
| 21 |
+
|
| 22 |
+
namespace at {
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
// aten::linalg_svdvals(Tensor A, *, str? driver=None) -> Tensor
|
| 26 |
+
inline at::Tensor linalg_svdvals(const at::Tensor & A, c10::optional<c10::string_view> driver=c10::nullopt) {
|
| 27 |
+
return at::_ops::linalg_svdvals::call(A, driver);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// aten::linalg_svdvals.out(Tensor A, *, str? driver=None, Tensor(a!) out) -> Tensor(a!)
|
| 31 |
+
inline at::Tensor & linalg_svdvals_out(at::Tensor & out, const at::Tensor & A, c10::optional<c10::string_view> driver=c10::nullopt) {
|
| 32 |
+
return at::_ops::linalg_svdvals_out::call(A, driver, out);
|
| 33 |
+
}
|
| 34 |
+
// aten::linalg_svdvals.out(Tensor A, *, str? driver=None, Tensor(a!) out) -> Tensor(a!)
|
| 35 |
+
inline at::Tensor & linalg_svdvals_outf(const at::Tensor & A, c10::optional<c10::string_view> driver, at::Tensor & out) {
|
| 36 |
+
return at::_ops::linalg_svdvals_out::call(A, driver, out);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
}
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/max_unpool3d_cuda_dispatch.h
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace cuda {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor max_unpool3d(const at::Tensor & self, const at::Tensor & indices, at::IntArrayRef output_size, at::IntArrayRef stride, at::IntArrayRef padding);
|
| 21 |
+
TORCH_API at::Tensor max_unpool3d_symint(const at::Tensor & self, const at::Tensor & indices, c10::SymIntArrayRef output_size, at::IntArrayRef stride, at::IntArrayRef padding);
|
| 22 |
+
TORCH_API at::Tensor & max_unpool3d_out(at::Tensor & out, const at::Tensor & self, const at::Tensor & indices, at::IntArrayRef output_size, at::IntArrayRef stride, at::IntArrayRef padding);
|
| 23 |
+
TORCH_API at::Tensor & max_unpool3d_outf(const at::Tensor & self, const at::Tensor & indices, at::IntArrayRef output_size, at::IntArrayRef stride, at::IntArrayRef padding, at::Tensor & out);
|
| 24 |
+
TORCH_API at::Tensor & max_unpool3d_symint_out(at::Tensor & out, const at::Tensor & self, const at::Tensor & indices, c10::SymIntArrayRef output_size, at::IntArrayRef stride, at::IntArrayRef padding);
|
| 25 |
+
TORCH_API at::Tensor & max_unpool3d_symint_outf(const at::Tensor & self, const at::Tensor & indices, c10::SymIntArrayRef output_size, at::IntArrayRef stride, at::IntArrayRef padding, at::Tensor & out);
|
| 26 |
+
|
| 27 |
+
} // namespace cuda
|
| 28 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/mkldnn_max_pool3d_compositeexplicitautograd_dispatch.h
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace compositeexplicitautograd {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor & mkldnn_max_pool3d_out(at::Tensor & out, const at::Tensor & self, at::IntArrayRef kernel_size, at::IntArrayRef stride={}, at::IntArrayRef padding=0, at::IntArrayRef dilation=1, bool ceil_mode=false);
|
| 21 |
+
TORCH_API at::Tensor & mkldnn_max_pool3d_outf(const at::Tensor & self, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding, at::IntArrayRef dilation, bool ceil_mode, at::Tensor & out);
|
| 22 |
+
|
| 23 |
+
} // namespace compositeexplicitautograd
|
| 24 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/narrow_ops.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Operator.h
|
| 4 |
+
|
| 5 |
+
#include <tuple>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 9 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 10 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 11 |
+
#include <ATen/core/ATen_fwd.h>
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
namespace _ops {
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
struct TORCH_API narrow {
|
| 18 |
+
using schema = at::Tensor (const at::Tensor &, int64_t, c10::SymInt, c10::SymInt);
|
| 19 |
+
using ptr_schema = schema*;
|
| 20 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 21 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::narrow")
|
| 22 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
|
| 23 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "narrow(Tensor(a) self, int dim, SymInt start, SymInt length) -> Tensor(a)")
|
| 24 |
+
static at::Tensor call(const at::Tensor & self, int64_t dim, c10::SymInt start, c10::SymInt length);
|
| 25 |
+
static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, int64_t dim, c10::SymInt start, c10::SymInt length);
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
struct TORCH_API narrow_Tensor {
|
| 29 |
+
using schema = at::Tensor (const at::Tensor &, int64_t, const at::Tensor &, c10::SymInt);
|
| 30 |
+
using ptr_schema = schema*;
|
| 31 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 32 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::narrow")
|
| 33 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "Tensor")
|
| 34 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "narrow.Tensor(Tensor(a) self, int dim, Tensor start, SymInt length) -> Tensor(a)")
|
| 35 |
+
static at::Tensor call(const at::Tensor & self, int64_t dim, const at::Tensor & start, c10::SymInt length);
|
| 36 |
+
static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, int64_t dim, const at::Tensor & start, c10::SymInt length);
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
}} // namespace at::_ops
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/renorm_meta_dispatch.h
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace meta {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor renorm(const at::Tensor & self, const at::Scalar & p, int64_t dim, const at::Scalar & maxnorm);
|
| 21 |
+
TORCH_API at::Tensor & renorm_out(at::Tensor & out, const at::Tensor & self, const at::Scalar & p, int64_t dim, const at::Scalar & maxnorm);
|
| 22 |
+
TORCH_API at::Tensor & renorm_outf(const at::Tensor & self, const at::Scalar & p, int64_t dim, const at::Scalar & maxnorm, at::Tensor & out);
|
| 23 |
+
TORCH_API at::Tensor & renorm_(at::Tensor & self, const at::Scalar & p, int64_t dim, const at::Scalar & maxnorm);
|
| 24 |
+
|
| 25 |
+
} // namespace meta
|
| 26 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/slow_conv_dilated2d_cuda_dispatch.h
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace cuda {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor slow_conv_dilated2d(const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, const c10::optional<at::Tensor> & bias={}, at::IntArrayRef stride=1, at::IntArrayRef padding=0, at::IntArrayRef dilation=1);
|
| 21 |
+
TORCH_API at::Tensor slow_conv_dilated2d_symint(const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, const c10::optional<at::Tensor> & bias={}, c10::SymIntArrayRef stride=c10::SymInt(1), c10::SymIntArrayRef padding=c10::SymInt(0), c10::SymIntArrayRef dilation=c10::SymInt(1));
|
| 22 |
+
|
| 23 |
+
} // namespace cuda
|
| 24 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/special_legendre_polynomial_p_compositeexplicitautogradnonfunctional_dispatch.h
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace compositeexplicitautogradnonfunctional {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor special_legendre_polynomial_p(const at::Tensor & x, const at::Tensor & n);
|
| 21 |
+
|
| 22 |
+
} // namespace compositeexplicitautogradnonfunctional
|
| 23 |
+
} // namespace at
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/thnn_conv2d_ops.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Operator.h
|
| 4 |
+
|
| 5 |
+
#include <tuple>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 9 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 10 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 11 |
+
#include <ATen/core/ATen_fwd.h>
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
namespace _ops {
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
struct TORCH_API thnn_conv2d_out {
|
| 18 |
+
using schema = at::Tensor & (const at::Tensor &, const at::Tensor &, c10::SymIntArrayRef, const c10::optional<at::Tensor> &, c10::SymIntArrayRef, c10::SymIntArrayRef, at::Tensor &);
|
| 19 |
+
using ptr_schema = schema*;
|
| 20 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 21 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::thnn_conv2d")
|
| 22 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "out")
|
| 23 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "thnn_conv2d.out(Tensor self, Tensor weight, SymInt[2] kernel_size, Tensor? bias=None, SymInt[2] stride=1, SymInt[2] padding=0, *, Tensor(a!) out) -> Tensor(a!)")
|
| 24 |
+
static at::Tensor & call(const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, const c10::optional<at::Tensor> & bias, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, at::Tensor & out);
|
| 25 |
+
static at::Tensor & redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, const c10::optional<at::Tensor> & bias, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, at::Tensor & out);
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
struct TORCH_API thnn_conv2d {
|
| 29 |
+
using schema = at::Tensor (const at::Tensor &, const at::Tensor &, c10::SymIntArrayRef, const c10::optional<at::Tensor> &, c10::SymIntArrayRef, c10::SymIntArrayRef);
|
| 30 |
+
using ptr_schema = schema*;
|
| 31 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 32 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::thnn_conv2d")
|
| 33 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
|
| 34 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "thnn_conv2d(Tensor self, Tensor weight, SymInt[2] kernel_size, Tensor? bias=None, SymInt[2] stride=1, SymInt[2] padding=0) -> Tensor")
|
| 35 |
+
static at::Tensor call(const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, const c10::optional<at::Tensor> & bias, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding);
|
| 36 |
+
static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, const c10::optional<at::Tensor> & bias, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding);
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
}} // namespace at::_ops
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/values_copy_ops.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Operator.h
|
| 4 |
+
|
| 5 |
+
#include <tuple>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 9 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 10 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 11 |
+
#include <ATen/core/ATen_fwd.h>
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
namespace _ops {
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
struct TORCH_API values_copy {
|
| 18 |
+
using schema = at::Tensor (const at::Tensor &);
|
| 19 |
+
using ptr_schema = schema*;
|
| 20 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 21 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::values_copy")
|
| 22 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
|
| 23 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "values_copy(Tensor self) -> Tensor")
|
| 24 |
+
static at::Tensor call(const at::Tensor & self);
|
| 25 |
+
static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self);
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
struct TORCH_API values_copy_out {
|
| 29 |
+
using schema = at::Tensor & (const at::Tensor &, at::Tensor &);
|
| 30 |
+
using ptr_schema = schema*;
|
| 31 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 32 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::values_copy")
|
| 33 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "out")
|
| 34 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "values_copy.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)")
|
| 35 |
+
static at::Tensor & call(const at::Tensor & self, at::Tensor & out);
|
| 36 |
+
static at::Tensor & redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, at::Tensor & out);
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
}} // namespace at::_ops
|
videollama2/lib/python3.10/site-packages/torch/include/ATen/ops/view_as_complex_copy.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Function.h
|
| 4 |
+
|
| 5 |
+
#include <ATen/Context.h>
|
| 6 |
+
#include <ATen/DeviceGuard.h>
|
| 7 |
+
#include <ATen/TensorUtils.h>
|
| 8 |
+
#include <ATen/TracerMode.h>
|
| 9 |
+
#include <ATen/core/Generator.h>
|
| 10 |
+
#include <ATen/core/Reduction.h>
|
| 11 |
+
#include <ATen/core/Tensor.h>
|
| 12 |
+
#include <c10/core/Scalar.h>
|
| 13 |
+
#include <c10/core/Storage.h>
|
| 14 |
+
#include <c10/core/TensorOptions.h>
|
| 15 |
+
#include <c10/util/Deprecated.h>
|
| 16 |
+
#include <c10/util/Optional.h>
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
#include <ATen/ops/view_as_complex_copy_ops.h>
|
| 21 |
+
|
| 22 |
+
namespace at {
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
// aten::view_as_complex_copy(Tensor self) -> Tensor
|
| 26 |
+
inline at::Tensor view_as_complex_copy(const at::Tensor & self) {
|
| 27 |
+
return at::_ops::view_as_complex_copy::call(self);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// aten::view_as_complex_copy.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)
|
| 31 |
+
inline at::Tensor & view_as_complex_copy_out(at::Tensor & out, const at::Tensor & self) {
|
| 32 |
+
return at::_ops::view_as_complex_copy_out::call(self, out);
|
| 33 |
+
}
|
| 34 |
+
// aten::view_as_complex_copy.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)
|
| 35 |
+
inline at::Tensor & view_as_complex_copy_outf(const at::Tensor & self, at::Tensor & out) {
|
| 36 |
+
return at::_ops::view_as_complex_copy_out::call(self, out);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
}
|
vllm/lib/python3.10/site-packages/sympy/concrete/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (321 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/sympy/polys/__init__.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Polynomial manipulation algorithms and algebraic objects. """
|
| 2 |
+
|
| 3 |
+
__all__ = [
|
| 4 |
+
'Poly', 'PurePoly', 'poly_from_expr', 'parallel_poly_from_expr', 'degree',
|
| 5 |
+
'total_degree', 'degree_list', 'LC', 'LM', 'LT', 'pdiv', 'prem', 'pquo',
|
| 6 |
+
'pexquo', 'div', 'rem', 'quo', 'exquo', 'half_gcdex', 'gcdex', 'invert',
|
| 7 |
+
'subresultants', 'resultant', 'discriminant', 'cofactors', 'gcd_list',
|
| 8 |
+
'gcd', 'lcm_list', 'lcm', 'terms_gcd', 'trunc', 'monic', 'content',
|
| 9 |
+
'primitive', 'compose', 'decompose', 'sturm', 'gff_list', 'gff',
|
| 10 |
+
'sqf_norm', 'sqf_part', 'sqf_list', 'sqf', 'factor_list', 'factor',
|
| 11 |
+
'intervals', 'refine_root', 'count_roots', 'all_roots', 'real_roots',
|
| 12 |
+
'nroots', 'ground_roots', 'nth_power_roots_poly', 'cancel', 'reduced',
|
| 13 |
+
'groebner', 'is_zero_dimensional', 'GroebnerBasis', 'poly',
|
| 14 |
+
|
| 15 |
+
'symmetrize', 'horner', 'interpolate', 'rational_interpolate', 'viete',
|
| 16 |
+
|
| 17 |
+
'together',
|
| 18 |
+
|
| 19 |
+
'BasePolynomialError', 'ExactQuotientFailed', 'PolynomialDivisionFailed',
|
| 20 |
+
'OperationNotSupported', 'HeuristicGCDFailed', 'HomomorphismFailed',
|
| 21 |
+
'IsomorphismFailed', 'ExtraneousFactors', 'EvaluationFailed',
|
| 22 |
+
'RefinementFailed', 'CoercionFailed', 'NotInvertible', 'NotReversible',
|
| 23 |
+
'NotAlgebraic', 'DomainError', 'PolynomialError', 'UnificationFailed',
|
| 24 |
+
'GeneratorsError', 'GeneratorsNeeded', 'ComputationFailed',
|
| 25 |
+
'UnivariatePolynomialError', 'MultivariatePolynomialError',
|
| 26 |
+
'PolificationFailed', 'OptionError', 'FlagError',
|
| 27 |
+
|
| 28 |
+
'minpoly', 'minimal_polynomial', 'primitive_element', 'field_isomorphism',
|
| 29 |
+
'to_number_field', 'isolate', 'round_two', 'prime_decomp',
|
| 30 |
+
'prime_valuation', 'galois_group',
|
| 31 |
+
|
| 32 |
+
'itermonomials', 'Monomial',
|
| 33 |
+
|
| 34 |
+
'lex', 'grlex', 'grevlex', 'ilex', 'igrlex', 'igrevlex',
|
| 35 |
+
|
| 36 |
+
'CRootOf', 'rootof', 'RootOf', 'ComplexRootOf', 'RootSum',
|
| 37 |
+
|
| 38 |
+
'roots',
|
| 39 |
+
|
| 40 |
+
'Domain', 'FiniteField', 'IntegerRing', 'RationalField', 'RealField',
|
| 41 |
+
'ComplexField', 'PythonFiniteField', 'GMPYFiniteField',
|
| 42 |
+
'PythonIntegerRing', 'GMPYIntegerRing', 'PythonRational',
|
| 43 |
+
'GMPYRationalField', 'AlgebraicField', 'PolynomialRing', 'FractionField',
|
| 44 |
+
'ExpressionDomain', 'FF_python', 'FF_gmpy', 'ZZ_python', 'ZZ_gmpy',
|
| 45 |
+
'QQ_python', 'QQ_gmpy', 'GF', 'FF', 'ZZ', 'QQ', 'ZZ_I', 'QQ_I', 'RR',
|
| 46 |
+
'CC', 'EX', 'EXRAW',
|
| 47 |
+
|
| 48 |
+
'construct_domain',
|
| 49 |
+
|
| 50 |
+
'swinnerton_dyer_poly', 'cyclotomic_poly', 'symmetric_poly',
|
| 51 |
+
'random_poly', 'interpolating_poly',
|
| 52 |
+
|
| 53 |
+
'jacobi_poly', 'chebyshevt_poly', 'chebyshevu_poly', 'hermite_poly',
|
| 54 |
+
'hermite_prob_poly', 'legendre_poly', 'laguerre_poly',
|
| 55 |
+
|
| 56 |
+
'bernoulli_poly', 'bernoulli_c_poly', 'genocchi_poly', 'euler_poly',
|
| 57 |
+
'andre_poly',
|
| 58 |
+
|
| 59 |
+
'apart', 'apart_list', 'assemble_partfrac_list',
|
| 60 |
+
|
| 61 |
+
'Options',
|
| 62 |
+
|
| 63 |
+
'ring', 'xring', 'vring', 'sring',
|
| 64 |
+
|
| 65 |
+
'field', 'xfield', 'vfield', 'sfield'
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
from .polytools import (Poly, PurePoly, poly_from_expr,
|
| 69 |
+
parallel_poly_from_expr, degree, total_degree, degree_list, LC, LM,
|
| 70 |
+
LT, pdiv, prem, pquo, pexquo, div, rem, quo, exquo, half_gcdex, gcdex,
|
| 71 |
+
invert, subresultants, resultant, discriminant, cofactors, gcd_list,
|
| 72 |
+
gcd, lcm_list, lcm, terms_gcd, trunc, monic, content, primitive,
|
| 73 |
+
compose, decompose, sturm, gff_list, gff, sqf_norm, sqf_part,
|
| 74 |
+
sqf_list, sqf, factor_list, factor, intervals, refine_root,
|
| 75 |
+
count_roots, all_roots, real_roots, nroots, ground_roots,
|
| 76 |
+
nth_power_roots_poly, cancel, reduced, groebner, is_zero_dimensional,
|
| 77 |
+
GroebnerBasis, poly)
|
| 78 |
+
|
| 79 |
+
from .polyfuncs import (symmetrize, horner, interpolate,
|
| 80 |
+
rational_interpolate, viete)
|
| 81 |
+
|
| 82 |
+
from .rationaltools import together
|
| 83 |
+
|
| 84 |
+
from .polyerrors import (BasePolynomialError, ExactQuotientFailed,
|
| 85 |
+
PolynomialDivisionFailed, OperationNotSupported, HeuristicGCDFailed,
|
| 86 |
+
HomomorphismFailed, IsomorphismFailed, ExtraneousFactors,
|
| 87 |
+
EvaluationFailed, RefinementFailed, CoercionFailed, NotInvertible,
|
| 88 |
+
NotReversible, NotAlgebraic, DomainError, PolynomialError,
|
| 89 |
+
UnificationFailed, GeneratorsError, GeneratorsNeeded,
|
| 90 |
+
ComputationFailed, UnivariatePolynomialError,
|
| 91 |
+
MultivariatePolynomialError, PolificationFailed, OptionError,
|
| 92 |
+
FlagError)
|
| 93 |
+
|
| 94 |
+
from .numberfields import (minpoly, minimal_polynomial, primitive_element,
|
| 95 |
+
field_isomorphism, to_number_field, isolate, round_two, prime_decomp,
|
| 96 |
+
prime_valuation, galois_group)
|
| 97 |
+
|
| 98 |
+
from .monomials import itermonomials, Monomial
|
| 99 |
+
|
| 100 |
+
from .orderings import lex, grlex, grevlex, ilex, igrlex, igrevlex
|
| 101 |
+
|
| 102 |
+
from .rootoftools import CRootOf, rootof, RootOf, ComplexRootOf, RootSum
|
| 103 |
+
|
| 104 |
+
from .polyroots import roots
|
| 105 |
+
|
| 106 |
+
from .domains import (Domain, FiniteField, IntegerRing, RationalField,
|
| 107 |
+
RealField, ComplexField, PythonFiniteField, GMPYFiniteField,
|
| 108 |
+
PythonIntegerRing, GMPYIntegerRing, PythonRational, GMPYRationalField,
|
| 109 |
+
AlgebraicField, PolynomialRing, FractionField, ExpressionDomain,
|
| 110 |
+
FF_python, FF_gmpy, ZZ_python, ZZ_gmpy, QQ_python, QQ_gmpy, GF, FF,
|
| 111 |
+
ZZ, QQ, ZZ_I, QQ_I, RR, CC, EX, EXRAW)
|
| 112 |
+
|
| 113 |
+
from .constructor import construct_domain
|
| 114 |
+
|
| 115 |
+
from .specialpolys import (swinnerton_dyer_poly, cyclotomic_poly,
|
| 116 |
+
symmetric_poly, random_poly, interpolating_poly)
|
| 117 |
+
|
| 118 |
+
from .orthopolys import (jacobi_poly, chebyshevt_poly, chebyshevu_poly,
|
| 119 |
+
hermite_poly, hermite_prob_poly, legendre_poly, laguerre_poly)
|
| 120 |
+
|
| 121 |
+
from .appellseqs import (bernoulli_poly, bernoulli_c_poly, genocchi_poly,
|
| 122 |
+
euler_poly, andre_poly)
|
| 123 |
+
|
| 124 |
+
from .partfrac import apart, apart_list, assemble_partfrac_list
|
| 125 |
+
|
| 126 |
+
from .polyoptions import Options
|
| 127 |
+
|
| 128 |
+
from .rings import ring, xring, vring, sring
|
| 129 |
+
|
| 130 |
+
from .fields import field, xfield, vfield, sfield
|
vllm/lib/python3.10/site-packages/sympy/polys/appellseqs.py
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
r"""
|
| 2 |
+
Efficient functions for generating Appell sequences.
|
| 3 |
+
|
| 4 |
+
An Appell sequence is a zero-indexed sequence of polynomials `p_i(x)`
|
| 5 |
+
satisfying `p_{i+1}'(x)=(i+1)p_i(x)` for all `i`. This definition leads
|
| 6 |
+
to the following iterative algorithm:
|
| 7 |
+
|
| 8 |
+
.. math :: p_0(x) = c_0,\ p_i(x) = i \int_0^x p_{i-1}(t)\,dt + c_i
|
| 9 |
+
|
| 10 |
+
The constant coefficients `c_i` are usually determined from the
|
| 11 |
+
just-evaluated integral and `i`.
|
| 12 |
+
|
| 13 |
+
Appell sequences satisfy the following identity from umbral calculus:
|
| 14 |
+
|
| 15 |
+
.. math :: p_n(x+y) = \sum_{k=0}^n \binom{n}{k} p_k(x) y^{n-k}
|
| 16 |
+
|
| 17 |
+
References
|
| 18 |
+
==========
|
| 19 |
+
|
| 20 |
+
.. [1] https://en.wikipedia.org/wiki/Appell_sequence
|
| 21 |
+
.. [2] Peter Luschny, "An introduction to the Bernoulli function",
|
| 22 |
+
https://arxiv.org/abs/2009.06743
|
| 23 |
+
"""
|
| 24 |
+
from sympy.polys.densearith import dup_mul_ground, dup_sub_ground, dup_quo_ground
|
| 25 |
+
from sympy.polys.densetools import dup_eval, dup_integrate
|
| 26 |
+
from sympy.polys.domains import ZZ, QQ
|
| 27 |
+
from sympy.polys.polytools import named_poly
|
| 28 |
+
from sympy.utilities import public
|
| 29 |
+
|
| 30 |
+
def dup_bernoulli(n, K):
|
| 31 |
+
"""Low-level implementation of Bernoulli polynomials."""
|
| 32 |
+
if n < 1:
|
| 33 |
+
return [K.one]
|
| 34 |
+
p = [K.one, K(-1,2)]
|
| 35 |
+
for i in range(2, n+1):
|
| 36 |
+
p = dup_integrate(dup_mul_ground(p, K(i), K), 1, K)
|
| 37 |
+
if i % 2 == 0:
|
| 38 |
+
p = dup_sub_ground(p, dup_eval(p, K(1,2), K) * K(1<<(i-1), (1<<i)-1), K)
|
| 39 |
+
return p
|
| 40 |
+
|
| 41 |
+
@public
|
| 42 |
+
def bernoulli_poly(n, x=None, polys=False):
|
| 43 |
+
r"""Generates the Bernoulli polynomial `\operatorname{B}_n(x)`.
|
| 44 |
+
|
| 45 |
+
`\operatorname{B}_n(x)` is the unique polynomial satisfying
|
| 46 |
+
|
| 47 |
+
.. math :: \int_{x}^{x+1} \operatorname{B}_n(t) \,dt = x^n.
|
| 48 |
+
|
| 49 |
+
Based on this, we have for nonnegative integer `s` and integer
|
| 50 |
+
`a` and `b`
|
| 51 |
+
|
| 52 |
+
.. math :: \sum_{k=a}^{b} k^s = \frac{\operatorname{B}_{s+1}(b+1) -
|
| 53 |
+
\operatorname{B}_{s+1}(a)}{s+1}
|
| 54 |
+
|
| 55 |
+
which is related to Jakob Bernoulli's original motivation for introducing
|
| 56 |
+
the Bernoulli numbers, the values of these polynomials at `x = 1`.
|
| 57 |
+
|
| 58 |
+
Examples
|
| 59 |
+
========
|
| 60 |
+
|
| 61 |
+
>>> from sympy import summation
|
| 62 |
+
>>> from sympy.abc import x
|
| 63 |
+
>>> from sympy.polys import bernoulli_poly
|
| 64 |
+
>>> bernoulli_poly(5, x)
|
| 65 |
+
x**5 - 5*x**4/2 + 5*x**3/3 - x/6
|
| 66 |
+
|
| 67 |
+
>>> def psum(p, a, b):
|
| 68 |
+
... return (bernoulli_poly(p+1,b+1) - bernoulli_poly(p+1,a)) / (p+1)
|
| 69 |
+
>>> psum(4, -6, 27)
|
| 70 |
+
3144337
|
| 71 |
+
>>> summation(x**4, (x, -6, 27))
|
| 72 |
+
3144337
|
| 73 |
+
|
| 74 |
+
>>> psum(1, 1, x).factor()
|
| 75 |
+
x*(x + 1)/2
|
| 76 |
+
>>> psum(2, 1, x).factor()
|
| 77 |
+
x*(x + 1)*(2*x + 1)/6
|
| 78 |
+
>>> psum(3, 1, x).factor()
|
| 79 |
+
x**2*(x + 1)**2/4
|
| 80 |
+
|
| 81 |
+
Parameters
|
| 82 |
+
==========
|
| 83 |
+
|
| 84 |
+
n : int
|
| 85 |
+
Degree of the polynomial.
|
| 86 |
+
x : optional
|
| 87 |
+
polys : bool, optional
|
| 88 |
+
If True, return a Poly, otherwise (default) return an expression.
|
| 89 |
+
|
| 90 |
+
See Also
|
| 91 |
+
========
|
| 92 |
+
|
| 93 |
+
sympy.functions.combinatorial.numbers.bernoulli
|
| 94 |
+
|
| 95 |
+
References
|
| 96 |
+
==========
|
| 97 |
+
|
| 98 |
+
.. [1] https://en.wikipedia.org/wiki/Bernoulli_polynomials
|
| 99 |
+
"""
|
| 100 |
+
return named_poly(n, dup_bernoulli, QQ, "Bernoulli polynomial", (x,), polys)
|
| 101 |
+
|
| 102 |
+
def dup_bernoulli_c(n, K):
|
| 103 |
+
"""Low-level implementation of central Bernoulli polynomials."""
|
| 104 |
+
p = [K.one]
|
| 105 |
+
for i in range(1, n+1):
|
| 106 |
+
p = dup_integrate(dup_mul_ground(p, K(i), K), 1, K)
|
| 107 |
+
if i % 2 == 0:
|
| 108 |
+
p = dup_sub_ground(p, dup_eval(p, K.one, K) * K((1<<(i-1))-1, (1<<i)-1), K)
|
| 109 |
+
return p
|
| 110 |
+
|
| 111 |
+
@public
|
| 112 |
+
def bernoulli_c_poly(n, x=None, polys=False):
|
| 113 |
+
r"""Generates the central Bernoulli polynomial `\operatorname{B}_n^c(x)`.
|
| 114 |
+
|
| 115 |
+
These are scaled and shifted versions of the plain Bernoulli polynomials,
|
| 116 |
+
done in such a way that `\operatorname{B}_n^c(x)` is an even or odd function
|
| 117 |
+
for even or odd `n` respectively:
|
| 118 |
+
|
| 119 |
+
.. math :: \operatorname{B}_n^c(x) = 2^n \operatorname{B}_n
|
| 120 |
+
\left(\frac{x+1}{2}\right)
|
| 121 |
+
|
| 122 |
+
Parameters
|
| 123 |
+
==========
|
| 124 |
+
|
| 125 |
+
n : int
|
| 126 |
+
Degree of the polynomial.
|
| 127 |
+
x : optional
|
| 128 |
+
polys : bool, optional
|
| 129 |
+
If True, return a Poly, otherwise (default) return an expression.
|
| 130 |
+
"""
|
| 131 |
+
return named_poly(n, dup_bernoulli_c, QQ, "central Bernoulli polynomial", (x,), polys)
|
| 132 |
+
|
| 133 |
+
def dup_genocchi(n, K):
|
| 134 |
+
"""Low-level implementation of Genocchi polynomials."""
|
| 135 |
+
if n < 1:
|
| 136 |
+
return [K.zero]
|
| 137 |
+
p = [-K.one]
|
| 138 |
+
for i in range(2, n+1):
|
| 139 |
+
p = dup_integrate(dup_mul_ground(p, K(i), K), 1, K)
|
| 140 |
+
if i % 2 == 0:
|
| 141 |
+
p = dup_sub_ground(p, dup_eval(p, K.one, K) // K(2), K)
|
| 142 |
+
return p
|
| 143 |
+
|
| 144 |
+
@public
|
| 145 |
+
def genocchi_poly(n, x=None, polys=False):
|
| 146 |
+
r"""Generates the Genocchi polynomial `\operatorname{G}_n(x)`.
|
| 147 |
+
|
| 148 |
+
`\operatorname{G}_n(x)` is twice the difference between the plain and
|
| 149 |
+
central Bernoulli polynomials, so has degree `n-1`:
|
| 150 |
+
|
| 151 |
+
.. math :: \operatorname{G}_n(x) = 2 (\operatorname{B}_n(x) -
|
| 152 |
+
\operatorname{B}_n^c(x))
|
| 153 |
+
|
| 154 |
+
The factor of 2 in the definition endows `\operatorname{G}_n(x)` with
|
| 155 |
+
integer coefficients.
|
| 156 |
+
|
| 157 |
+
Parameters
|
| 158 |
+
==========
|
| 159 |
+
|
| 160 |
+
n : int
|
| 161 |
+
Degree of the polynomial plus one.
|
| 162 |
+
x : optional
|
| 163 |
+
polys : bool, optional
|
| 164 |
+
If True, return a Poly, otherwise (default) return an expression.
|
| 165 |
+
|
| 166 |
+
See Also
|
| 167 |
+
========
|
| 168 |
+
|
| 169 |
+
sympy.functions.combinatorial.numbers.genocchi
|
| 170 |
+
"""
|
| 171 |
+
return named_poly(n, dup_genocchi, ZZ, "Genocchi polynomial", (x,), polys)
|
| 172 |
+
|
| 173 |
+
def dup_euler(n, K):
|
| 174 |
+
"""Low-level implementation of Euler polynomials."""
|
| 175 |
+
return dup_quo_ground(dup_genocchi(n+1, ZZ), K(-n-1), K)
|
| 176 |
+
|
| 177 |
+
@public
|
| 178 |
+
def euler_poly(n, x=None, polys=False):
|
| 179 |
+
r"""Generates the Euler polynomial `\operatorname{E}_n(x)`.
|
| 180 |
+
|
| 181 |
+
These are scaled and reindexed versions of the Genocchi polynomials:
|
| 182 |
+
|
| 183 |
+
.. math :: \operatorname{E}_n(x) = -\frac{\operatorname{G}_{n+1}(x)}{n+1}
|
| 184 |
+
|
| 185 |
+
Parameters
|
| 186 |
+
==========
|
| 187 |
+
|
| 188 |
+
n : int
|
| 189 |
+
Degree of the polynomial.
|
| 190 |
+
x : optional
|
| 191 |
+
polys : bool, optional
|
| 192 |
+
If True, return a Poly, otherwise (default) return an expression.
|
| 193 |
+
|
| 194 |
+
See Also
|
| 195 |
+
========
|
| 196 |
+
|
| 197 |
+
sympy.functions.combinatorial.numbers.euler
|
| 198 |
+
"""
|
| 199 |
+
return named_poly(n, dup_euler, QQ, "Euler polynomial", (x,), polys)
|
| 200 |
+
|
| 201 |
+
def dup_andre(n, K):
|
| 202 |
+
"""Low-level implementation of Andre polynomials."""
|
| 203 |
+
p = [K.one]
|
| 204 |
+
for i in range(1, n+1):
|
| 205 |
+
p = dup_integrate(dup_mul_ground(p, K(i), K), 1, K)
|
| 206 |
+
if i % 2 == 0:
|
| 207 |
+
p = dup_sub_ground(p, dup_eval(p, K.one, K), K)
|
| 208 |
+
return p
|
| 209 |
+
|
| 210 |
+
@public
|
| 211 |
+
def andre_poly(n, x=None, polys=False):
|
| 212 |
+
r"""Generates the Andre polynomial `\mathcal{A}_n(x)`.
|
| 213 |
+
|
| 214 |
+
This is the Appell sequence where the constant coefficients form the sequence
|
| 215 |
+
of Euler numbers ``euler(n)``. As such they have integer coefficients
|
| 216 |
+
and parities matching the parity of `n`.
|
| 217 |
+
|
| 218 |
+
Luschny calls these the *Swiss-knife polynomials* because their values
|
| 219 |
+
at 0 and 1 can be simply transformed into both the Bernoulli and Euler
|
| 220 |
+
numbers. Here they are called the Andre polynomials because
|
| 221 |
+
`|\mathcal{A}_n(n\bmod 2)|` for `n \ge 0` generates what Luschny calls
|
| 222 |
+
the *Andre numbers*, A000111 in the OEIS.
|
| 223 |
+
|
| 224 |
+
Examples
|
| 225 |
+
========
|
| 226 |
+
|
| 227 |
+
>>> from sympy import bernoulli, euler, genocchi
|
| 228 |
+
>>> from sympy.abc import x
|
| 229 |
+
>>> from sympy.polys import andre_poly
|
| 230 |
+
>>> andre_poly(9, x)
|
| 231 |
+
x**9 - 36*x**7 + 630*x**5 - 5124*x**3 + 12465*x
|
| 232 |
+
|
| 233 |
+
>>> [andre_poly(n, 0) for n in range(11)]
|
| 234 |
+
[1, 0, -1, 0, 5, 0, -61, 0, 1385, 0, -50521]
|
| 235 |
+
>>> [euler(n) for n in range(11)]
|
| 236 |
+
[1, 0, -1, 0, 5, 0, -61, 0, 1385, 0, -50521]
|
| 237 |
+
>>> [andre_poly(n-1, 1) * n / (4**n - 2**n) for n in range(1, 11)]
|
| 238 |
+
[1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66]
|
| 239 |
+
>>> [bernoulli(n) for n in range(1, 11)]
|
| 240 |
+
[1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66]
|
| 241 |
+
>>> [-andre_poly(n-1, -1) * n / (-2)**(n-1) for n in range(1, 11)]
|
| 242 |
+
[-1, -1, 0, 1, 0, -3, 0, 17, 0, -155]
|
| 243 |
+
>>> [genocchi(n) for n in range(1, 11)]
|
| 244 |
+
[-1, -1, 0, 1, 0, -3, 0, 17, 0, -155]
|
| 245 |
+
|
| 246 |
+
>>> [abs(andre_poly(n, n%2)) for n in range(11)]
|
| 247 |
+
[1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521]
|
| 248 |
+
|
| 249 |
+
Parameters
|
| 250 |
+
==========
|
| 251 |
+
|
| 252 |
+
n : int
|
| 253 |
+
Degree of the polynomial.
|
| 254 |
+
x : optional
|
| 255 |
+
polys : bool, optional
|
| 256 |
+
If True, return a Poly, otherwise (default) return an expression.
|
| 257 |
+
|
| 258 |
+
See Also
|
| 259 |
+
========
|
| 260 |
+
|
| 261 |
+
sympy.functions.combinatorial.numbers.andre
|
| 262 |
+
|
| 263 |
+
References
|
| 264 |
+
==========
|
| 265 |
+
|
| 266 |
+
.. [1] Peter Luschny, "An introduction to the Bernoulli function",
|
| 267 |
+
https://arxiv.org/abs/2009.06743
|
| 268 |
+
"""
|
| 269 |
+
return named_poly(n, dup_andre, ZZ, "Andre polynomial", (x,), polys)
|
vllm/lib/python3.10/site-packages/sympy/polys/compatibility.py
ADDED
|
@@ -0,0 +1,1142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Compatibility interface between dense and sparse polys. """
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
from sympy.polys.densearith import dup_add_term
|
| 5 |
+
from sympy.polys.densearith import dmp_add_term
|
| 6 |
+
from sympy.polys.densearith import dup_sub_term
|
| 7 |
+
from sympy.polys.densearith import dmp_sub_term
|
| 8 |
+
from sympy.polys.densearith import dup_mul_term
|
| 9 |
+
from sympy.polys.densearith import dmp_mul_term
|
| 10 |
+
from sympy.polys.densearith import dup_add_ground
|
| 11 |
+
from sympy.polys.densearith import dmp_add_ground
|
| 12 |
+
from sympy.polys.densearith import dup_sub_ground
|
| 13 |
+
from sympy.polys.densearith import dmp_sub_ground
|
| 14 |
+
from sympy.polys.densearith import dup_mul_ground
|
| 15 |
+
from sympy.polys.densearith import dmp_mul_ground
|
| 16 |
+
from sympy.polys.densearith import dup_quo_ground
|
| 17 |
+
from sympy.polys.densearith import dmp_quo_ground
|
| 18 |
+
from sympy.polys.densearith import dup_exquo_ground
|
| 19 |
+
from sympy.polys.densearith import dmp_exquo_ground
|
| 20 |
+
from sympy.polys.densearith import dup_lshift
|
| 21 |
+
from sympy.polys.densearith import dup_rshift
|
| 22 |
+
from sympy.polys.densearith import dup_abs
|
| 23 |
+
from sympy.polys.densearith import dmp_abs
|
| 24 |
+
from sympy.polys.densearith import dup_neg
|
| 25 |
+
from sympy.polys.densearith import dmp_neg
|
| 26 |
+
from sympy.polys.densearith import dup_add
|
| 27 |
+
from sympy.polys.densearith import dmp_add
|
| 28 |
+
from sympy.polys.densearith import dup_sub
|
| 29 |
+
from sympy.polys.densearith import dmp_sub
|
| 30 |
+
from sympy.polys.densearith import dup_add_mul
|
| 31 |
+
from sympy.polys.densearith import dmp_add_mul
|
| 32 |
+
from sympy.polys.densearith import dup_sub_mul
|
| 33 |
+
from sympy.polys.densearith import dmp_sub_mul
|
| 34 |
+
from sympy.polys.densearith import dup_mul
|
| 35 |
+
from sympy.polys.densearith import dmp_mul
|
| 36 |
+
from sympy.polys.densearith import dup_sqr
|
| 37 |
+
from sympy.polys.densearith import dmp_sqr
|
| 38 |
+
from sympy.polys.densearith import dup_pow
|
| 39 |
+
from sympy.polys.densearith import dmp_pow
|
| 40 |
+
from sympy.polys.densearith import dup_pdiv
|
| 41 |
+
from sympy.polys.densearith import dup_prem
|
| 42 |
+
from sympy.polys.densearith import dup_pquo
|
| 43 |
+
from sympy.polys.densearith import dup_pexquo
|
| 44 |
+
from sympy.polys.densearith import dmp_pdiv
|
| 45 |
+
from sympy.polys.densearith import dmp_prem
|
| 46 |
+
from sympy.polys.densearith import dmp_pquo
|
| 47 |
+
from sympy.polys.densearith import dmp_pexquo
|
| 48 |
+
from sympy.polys.densearith import dup_rr_div
|
| 49 |
+
from sympy.polys.densearith import dmp_rr_div
|
| 50 |
+
from sympy.polys.densearith import dup_ff_div
|
| 51 |
+
from sympy.polys.densearith import dmp_ff_div
|
| 52 |
+
from sympy.polys.densearith import dup_div
|
| 53 |
+
from sympy.polys.densearith import dup_rem
|
| 54 |
+
from sympy.polys.densearith import dup_quo
|
| 55 |
+
from sympy.polys.densearith import dup_exquo
|
| 56 |
+
from sympy.polys.densearith import dmp_div
|
| 57 |
+
from sympy.polys.densearith import dmp_rem
|
| 58 |
+
from sympy.polys.densearith import dmp_quo
|
| 59 |
+
from sympy.polys.densearith import dmp_exquo
|
| 60 |
+
from sympy.polys.densearith import dup_max_norm
|
| 61 |
+
from sympy.polys.densearith import dmp_max_norm
|
| 62 |
+
from sympy.polys.densearith import dup_l1_norm
|
| 63 |
+
from sympy.polys.densearith import dmp_l1_norm
|
| 64 |
+
from sympy.polys.densearith import dup_l2_norm_squared
|
| 65 |
+
from sympy.polys.densearith import dmp_l2_norm_squared
|
| 66 |
+
from sympy.polys.densearith import dup_expand
|
| 67 |
+
from sympy.polys.densearith import dmp_expand
|
| 68 |
+
from sympy.polys.densebasic import dup_LC
|
| 69 |
+
from sympy.polys.densebasic import dmp_LC
|
| 70 |
+
from sympy.polys.densebasic import dup_TC
|
| 71 |
+
from sympy.polys.densebasic import dmp_TC
|
| 72 |
+
from sympy.polys.densebasic import dmp_ground_LC
|
| 73 |
+
from sympy.polys.densebasic import dmp_ground_TC
|
| 74 |
+
from sympy.polys.densebasic import dup_degree
|
| 75 |
+
from sympy.polys.densebasic import dmp_degree
|
| 76 |
+
from sympy.polys.densebasic import dmp_degree_in
|
| 77 |
+
from sympy.polys.densebasic import dmp_to_dict
|
| 78 |
+
from sympy.polys.densetools import dup_integrate
|
| 79 |
+
from sympy.polys.densetools import dmp_integrate
|
| 80 |
+
from sympy.polys.densetools import dmp_integrate_in
|
| 81 |
+
from sympy.polys.densetools import dup_diff
|
| 82 |
+
from sympy.polys.densetools import dmp_diff
|
| 83 |
+
from sympy.polys.densetools import dmp_diff_in
|
| 84 |
+
from sympy.polys.densetools import dup_eval
|
| 85 |
+
from sympy.polys.densetools import dmp_eval
|
| 86 |
+
from sympy.polys.densetools import dmp_eval_in
|
| 87 |
+
from sympy.polys.densetools import dmp_eval_tail
|
| 88 |
+
from sympy.polys.densetools import dmp_diff_eval_in
|
| 89 |
+
from sympy.polys.densetools import dup_trunc
|
| 90 |
+
from sympy.polys.densetools import dmp_trunc
|
| 91 |
+
from sympy.polys.densetools import dmp_ground_trunc
|
| 92 |
+
from sympy.polys.densetools import dup_monic
|
| 93 |
+
from sympy.polys.densetools import dmp_ground_monic
|
| 94 |
+
from sympy.polys.densetools import dup_content
|
| 95 |
+
from sympy.polys.densetools import dmp_ground_content
|
| 96 |
+
from sympy.polys.densetools import dup_primitive
|
| 97 |
+
from sympy.polys.densetools import dmp_ground_primitive
|
| 98 |
+
from sympy.polys.densetools import dup_extract
|
| 99 |
+
from sympy.polys.densetools import dmp_ground_extract
|
| 100 |
+
from sympy.polys.densetools import dup_real_imag
|
| 101 |
+
from sympy.polys.densetools import dup_mirror
|
| 102 |
+
from sympy.polys.densetools import dup_scale
|
| 103 |
+
from sympy.polys.densetools import dup_shift
|
| 104 |
+
from sympy.polys.densetools import dmp_shift
|
| 105 |
+
from sympy.polys.densetools import dup_transform
|
| 106 |
+
from sympy.polys.densetools import dup_compose
|
| 107 |
+
from sympy.polys.densetools import dmp_compose
|
| 108 |
+
from sympy.polys.densetools import dup_decompose
|
| 109 |
+
from sympy.polys.densetools import dmp_lift
|
| 110 |
+
from sympy.polys.densetools import dup_sign_variations
|
| 111 |
+
from sympy.polys.densetools import dup_clear_denoms
|
| 112 |
+
from sympy.polys.densetools import dmp_clear_denoms
|
| 113 |
+
from sympy.polys.densetools import dup_revert
|
| 114 |
+
from sympy.polys.euclidtools import dup_half_gcdex
|
| 115 |
+
from sympy.polys.euclidtools import dmp_half_gcdex
|
| 116 |
+
from sympy.polys.euclidtools import dup_gcdex
|
| 117 |
+
from sympy.polys.euclidtools import dmp_gcdex
|
| 118 |
+
from sympy.polys.euclidtools import dup_invert
|
| 119 |
+
from sympy.polys.euclidtools import dmp_invert
|
| 120 |
+
from sympy.polys.euclidtools import dup_euclidean_prs
|
| 121 |
+
from sympy.polys.euclidtools import dmp_euclidean_prs
|
| 122 |
+
from sympy.polys.euclidtools import dup_primitive_prs
|
| 123 |
+
from sympy.polys.euclidtools import dmp_primitive_prs
|
| 124 |
+
from sympy.polys.euclidtools import dup_inner_subresultants
|
| 125 |
+
from sympy.polys.euclidtools import dup_subresultants
|
| 126 |
+
from sympy.polys.euclidtools import dup_prs_resultant
|
| 127 |
+
from sympy.polys.euclidtools import dup_resultant
|
| 128 |
+
from sympy.polys.euclidtools import dmp_inner_subresultants
|
| 129 |
+
from sympy.polys.euclidtools import dmp_subresultants
|
| 130 |
+
from sympy.polys.euclidtools import dmp_prs_resultant
|
| 131 |
+
from sympy.polys.euclidtools import dmp_zz_modular_resultant
|
| 132 |
+
from sympy.polys.euclidtools import dmp_zz_collins_resultant
|
| 133 |
+
from sympy.polys.euclidtools import dmp_qq_collins_resultant
|
| 134 |
+
from sympy.polys.euclidtools import dmp_resultant
|
| 135 |
+
from sympy.polys.euclidtools import dup_discriminant
|
| 136 |
+
from sympy.polys.euclidtools import dmp_discriminant
|
| 137 |
+
from sympy.polys.euclidtools import dup_rr_prs_gcd
|
| 138 |
+
from sympy.polys.euclidtools import dup_ff_prs_gcd
|
| 139 |
+
from sympy.polys.euclidtools import dmp_rr_prs_gcd
|
| 140 |
+
from sympy.polys.euclidtools import dmp_ff_prs_gcd
|
| 141 |
+
from sympy.polys.euclidtools import dup_zz_heu_gcd
|
| 142 |
+
from sympy.polys.euclidtools import dmp_zz_heu_gcd
|
| 143 |
+
from sympy.polys.euclidtools import dup_qq_heu_gcd
|
| 144 |
+
from sympy.polys.euclidtools import dmp_qq_heu_gcd
|
| 145 |
+
from sympy.polys.euclidtools import dup_inner_gcd
|
| 146 |
+
from sympy.polys.euclidtools import dmp_inner_gcd
|
| 147 |
+
from sympy.polys.euclidtools import dup_gcd
|
| 148 |
+
from sympy.polys.euclidtools import dmp_gcd
|
| 149 |
+
from sympy.polys.euclidtools import dup_rr_lcm
|
| 150 |
+
from sympy.polys.euclidtools import dup_ff_lcm
|
| 151 |
+
from sympy.polys.euclidtools import dup_lcm
|
| 152 |
+
from sympy.polys.euclidtools import dmp_rr_lcm
|
| 153 |
+
from sympy.polys.euclidtools import dmp_ff_lcm
|
| 154 |
+
from sympy.polys.euclidtools import dmp_lcm
|
| 155 |
+
from sympy.polys.euclidtools import dmp_content
|
| 156 |
+
from sympy.polys.euclidtools import dmp_primitive
|
| 157 |
+
from sympy.polys.euclidtools import dup_cancel
|
| 158 |
+
from sympy.polys.euclidtools import dmp_cancel
|
| 159 |
+
from sympy.polys.factortools import dup_trial_division
|
| 160 |
+
from sympy.polys.factortools import dmp_trial_division
|
| 161 |
+
from sympy.polys.factortools import dup_zz_mignotte_bound
|
| 162 |
+
from sympy.polys.factortools import dmp_zz_mignotte_bound
|
| 163 |
+
from sympy.polys.factortools import dup_zz_hensel_step
|
| 164 |
+
from sympy.polys.factortools import dup_zz_hensel_lift
|
| 165 |
+
from sympy.polys.factortools import dup_zz_zassenhaus
|
| 166 |
+
from sympy.polys.factortools import dup_zz_irreducible_p
|
| 167 |
+
from sympy.polys.factortools import dup_cyclotomic_p
|
| 168 |
+
from sympy.polys.factortools import dup_zz_cyclotomic_poly
|
| 169 |
+
from sympy.polys.factortools import dup_zz_cyclotomic_factor
|
| 170 |
+
from sympy.polys.factortools import dup_zz_factor_sqf
|
| 171 |
+
from sympy.polys.factortools import dup_zz_factor
|
| 172 |
+
from sympy.polys.factortools import dmp_zz_wang_non_divisors
|
| 173 |
+
from sympy.polys.factortools import dmp_zz_wang_lead_coeffs
|
| 174 |
+
from sympy.polys.factortools import dup_zz_diophantine
|
| 175 |
+
from sympy.polys.factortools import dmp_zz_diophantine
|
| 176 |
+
from sympy.polys.factortools import dmp_zz_wang_hensel_lifting
|
| 177 |
+
from sympy.polys.factortools import dmp_zz_wang
|
| 178 |
+
from sympy.polys.factortools import dmp_zz_factor
|
| 179 |
+
from sympy.polys.factortools import dup_qq_i_factor
|
| 180 |
+
from sympy.polys.factortools import dup_zz_i_factor
|
| 181 |
+
from sympy.polys.factortools import dmp_qq_i_factor
|
| 182 |
+
from sympy.polys.factortools import dmp_zz_i_factor
|
| 183 |
+
from sympy.polys.factortools import dup_ext_factor
|
| 184 |
+
from sympy.polys.factortools import dmp_ext_factor
|
| 185 |
+
from sympy.polys.factortools import dup_gf_factor
|
| 186 |
+
from sympy.polys.factortools import dmp_gf_factor
|
| 187 |
+
from sympy.polys.factortools import dup_factor_list
|
| 188 |
+
from sympy.polys.factortools import dup_factor_list_include
|
| 189 |
+
from sympy.polys.factortools import dmp_factor_list
|
| 190 |
+
from sympy.polys.factortools import dmp_factor_list_include
|
| 191 |
+
from sympy.polys.factortools import dup_irreducible_p
|
| 192 |
+
from sympy.polys.factortools import dmp_irreducible_p
|
| 193 |
+
from sympy.polys.rootisolation import dup_sturm
|
| 194 |
+
from sympy.polys.rootisolation import dup_root_upper_bound
|
| 195 |
+
from sympy.polys.rootisolation import dup_root_lower_bound
|
| 196 |
+
from sympy.polys.rootisolation import dup_step_refine_real_root
|
| 197 |
+
from sympy.polys.rootisolation import dup_inner_refine_real_root
|
| 198 |
+
from sympy.polys.rootisolation import dup_outer_refine_real_root
|
| 199 |
+
from sympy.polys.rootisolation import dup_refine_real_root
|
| 200 |
+
from sympy.polys.rootisolation import dup_inner_isolate_real_roots
|
| 201 |
+
from sympy.polys.rootisolation import dup_inner_isolate_positive_roots
|
| 202 |
+
from sympy.polys.rootisolation import dup_inner_isolate_negative_roots
|
| 203 |
+
from sympy.polys.rootisolation import dup_isolate_real_roots_sqf
|
| 204 |
+
from sympy.polys.rootisolation import dup_isolate_real_roots
|
| 205 |
+
from sympy.polys.rootisolation import dup_isolate_real_roots_list
|
| 206 |
+
from sympy.polys.rootisolation import dup_count_real_roots
|
| 207 |
+
from sympy.polys.rootisolation import dup_count_complex_roots
|
| 208 |
+
from sympy.polys.rootisolation import dup_isolate_complex_roots_sqf
|
| 209 |
+
from sympy.polys.rootisolation import dup_isolate_all_roots_sqf
|
| 210 |
+
from sympy.polys.rootisolation import dup_isolate_all_roots
|
| 211 |
+
|
| 212 |
+
from sympy.polys.sqfreetools import (
|
| 213 |
+
dup_sqf_p, dmp_sqf_p, dmp_norm, dup_sqf_norm, dmp_sqf_norm,
|
| 214 |
+
dup_gf_sqf_part, dmp_gf_sqf_part, dup_sqf_part, dmp_sqf_part,
|
| 215 |
+
dup_gf_sqf_list, dmp_gf_sqf_list, dup_sqf_list, dup_sqf_list_include,
|
| 216 |
+
dmp_sqf_list, dmp_sqf_list_include, dup_gff_list, dmp_gff_list)
|
| 217 |
+
|
| 218 |
+
from sympy.polys.galoistools import (
|
| 219 |
+
gf_degree, gf_LC, gf_TC, gf_strip, gf_from_dict,
|
| 220 |
+
gf_to_dict, gf_from_int_poly, gf_to_int_poly, gf_neg, gf_add_ground, gf_sub_ground,
|
| 221 |
+
gf_mul_ground, gf_quo_ground, gf_add, gf_sub, gf_mul, gf_sqr, gf_add_mul, gf_sub_mul,
|
| 222 |
+
gf_expand, gf_div, gf_rem, gf_quo, gf_exquo, gf_lshift, gf_rshift, gf_pow, gf_pow_mod,
|
| 223 |
+
gf_gcd, gf_lcm, gf_cofactors, gf_gcdex, gf_monic, gf_diff, gf_eval, gf_multi_eval,
|
| 224 |
+
gf_compose, gf_compose_mod, gf_trace_map, gf_random, gf_irreducible, gf_irred_p_ben_or,
|
| 225 |
+
gf_irred_p_rabin, gf_irreducible_p, gf_sqf_p, gf_sqf_part, gf_Qmatrix,
|
| 226 |
+
gf_berlekamp, gf_ddf_zassenhaus, gf_edf_zassenhaus, gf_ddf_shoup, gf_edf_shoup,
|
| 227 |
+
gf_zassenhaus, gf_shoup, gf_factor_sqf, gf_factor)
|
| 228 |
+
|
| 229 |
+
from sympy.utilities import public
|
| 230 |
+
|
| 231 |
+
@public
|
| 232 |
+
class IPolys:
|
| 233 |
+
symbols = None
|
| 234 |
+
ngens = None
|
| 235 |
+
domain = None
|
| 236 |
+
order = None
|
| 237 |
+
gens = None
|
| 238 |
+
|
| 239 |
+
def drop(self, gen):
|
| 240 |
+
pass
|
| 241 |
+
|
| 242 |
+
def clone(self, symbols=None, domain=None, order=None):
|
| 243 |
+
pass
|
| 244 |
+
|
| 245 |
+
def to_ground(self):
|
| 246 |
+
pass
|
| 247 |
+
|
| 248 |
+
def ground_new(self, element):
|
| 249 |
+
pass
|
| 250 |
+
|
| 251 |
+
def domain_new(self, element):
|
| 252 |
+
pass
|
| 253 |
+
|
| 254 |
+
def from_dict(self, d):
|
| 255 |
+
pass
|
| 256 |
+
|
| 257 |
+
def wrap(self, element):
|
| 258 |
+
from sympy.polys.rings import PolyElement
|
| 259 |
+
if isinstance(element, PolyElement):
|
| 260 |
+
if element.ring == self:
|
| 261 |
+
return element
|
| 262 |
+
else:
|
| 263 |
+
raise NotImplementedError("domain conversions")
|
| 264 |
+
else:
|
| 265 |
+
return self.ground_new(element)
|
| 266 |
+
|
| 267 |
+
def to_dense(self, element):
|
| 268 |
+
return self.wrap(element).to_dense()
|
| 269 |
+
|
| 270 |
+
def from_dense(self, element):
|
| 271 |
+
return self.from_dict(dmp_to_dict(element, self.ngens-1, self.domain))
|
| 272 |
+
|
| 273 |
+
def dup_add_term(self, f, c, i):
|
| 274 |
+
return self.from_dense(dup_add_term(self.to_dense(f), c, i, self.domain))
|
| 275 |
+
def dmp_add_term(self, f, c, i):
|
| 276 |
+
return self.from_dense(dmp_add_term(self.to_dense(f), self.wrap(c).drop(0).to_dense(), i, self.ngens-1, self.domain))
|
| 277 |
+
def dup_sub_term(self, f, c, i):
|
| 278 |
+
return self.from_dense(dup_sub_term(self.to_dense(f), c, i, self.domain))
|
| 279 |
+
def dmp_sub_term(self, f, c, i):
|
| 280 |
+
return self.from_dense(dmp_sub_term(self.to_dense(f), self.wrap(c).drop(0).to_dense(), i, self.ngens-1, self.domain))
|
| 281 |
+
def dup_mul_term(self, f, c, i):
|
| 282 |
+
return self.from_dense(dup_mul_term(self.to_dense(f), c, i, self.domain))
|
| 283 |
+
def dmp_mul_term(self, f, c, i):
|
| 284 |
+
return self.from_dense(dmp_mul_term(self.to_dense(f), self.wrap(c).drop(0).to_dense(), i, self.ngens-1, self.domain))
|
| 285 |
+
|
| 286 |
+
def dup_add_ground(self, f, c):
|
| 287 |
+
return self.from_dense(dup_add_ground(self.to_dense(f), c, self.domain))
|
| 288 |
+
def dmp_add_ground(self, f, c):
|
| 289 |
+
return self.from_dense(dmp_add_ground(self.to_dense(f), c, self.ngens-1, self.domain))
|
| 290 |
+
def dup_sub_ground(self, f, c):
|
| 291 |
+
return self.from_dense(dup_sub_ground(self.to_dense(f), c, self.domain))
|
| 292 |
+
def dmp_sub_ground(self, f, c):
|
| 293 |
+
return self.from_dense(dmp_sub_ground(self.to_dense(f), c, self.ngens-1, self.domain))
|
| 294 |
+
def dup_mul_ground(self, f, c):
|
| 295 |
+
return self.from_dense(dup_mul_ground(self.to_dense(f), c, self.domain))
|
| 296 |
+
def dmp_mul_ground(self, f, c):
|
| 297 |
+
return self.from_dense(dmp_mul_ground(self.to_dense(f), c, self.ngens-1, self.domain))
|
| 298 |
+
def dup_quo_ground(self, f, c):
|
| 299 |
+
return self.from_dense(dup_quo_ground(self.to_dense(f), c, self.domain))
|
| 300 |
+
def dmp_quo_ground(self, f, c):
|
| 301 |
+
return self.from_dense(dmp_quo_ground(self.to_dense(f), c, self.ngens-1, self.domain))
|
| 302 |
+
def dup_exquo_ground(self, f, c):
|
| 303 |
+
return self.from_dense(dup_exquo_ground(self.to_dense(f), c, self.domain))
|
| 304 |
+
def dmp_exquo_ground(self, f, c):
|
| 305 |
+
return self.from_dense(dmp_exquo_ground(self.to_dense(f), c, self.ngens-1, self.domain))
|
| 306 |
+
|
| 307 |
+
def dup_lshift(self, f, n):
|
| 308 |
+
return self.from_dense(dup_lshift(self.to_dense(f), n, self.domain))
|
| 309 |
+
def dup_rshift(self, f, n):
|
| 310 |
+
return self.from_dense(dup_rshift(self.to_dense(f), n, self.domain))
|
| 311 |
+
|
| 312 |
+
def dup_abs(self, f):
|
| 313 |
+
return self.from_dense(dup_abs(self.to_dense(f), self.domain))
|
| 314 |
+
def dmp_abs(self, f):
|
| 315 |
+
return self.from_dense(dmp_abs(self.to_dense(f), self.ngens-1, self.domain))
|
| 316 |
+
|
| 317 |
+
def dup_neg(self, f):
|
| 318 |
+
return self.from_dense(dup_neg(self.to_dense(f), self.domain))
|
| 319 |
+
def dmp_neg(self, f):
|
| 320 |
+
return self.from_dense(dmp_neg(self.to_dense(f), self.ngens-1, self.domain))
|
| 321 |
+
|
| 322 |
+
def dup_add(self, f, g):
|
| 323 |
+
return self.from_dense(dup_add(self.to_dense(f), self.to_dense(g), self.domain))
|
| 324 |
+
def dmp_add(self, f, g):
|
| 325 |
+
return self.from_dense(dmp_add(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 326 |
+
|
| 327 |
+
def dup_sub(self, f, g):
|
| 328 |
+
return self.from_dense(dup_sub(self.to_dense(f), self.to_dense(g), self.domain))
|
| 329 |
+
def dmp_sub(self, f, g):
|
| 330 |
+
return self.from_dense(dmp_sub(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 331 |
+
|
| 332 |
+
def dup_add_mul(self, f, g, h):
|
| 333 |
+
return self.from_dense(dup_add_mul(self.to_dense(f), self.to_dense(g), self.to_dense(h), self.domain))
|
| 334 |
+
def dmp_add_mul(self, f, g, h):
|
| 335 |
+
return self.from_dense(dmp_add_mul(self.to_dense(f), self.to_dense(g), self.to_dense(h), self.ngens-1, self.domain))
|
| 336 |
+
def dup_sub_mul(self, f, g, h):
|
| 337 |
+
return self.from_dense(dup_sub_mul(self.to_dense(f), self.to_dense(g), self.to_dense(h), self.domain))
|
| 338 |
+
def dmp_sub_mul(self, f, g, h):
|
| 339 |
+
return self.from_dense(dmp_sub_mul(self.to_dense(f), self.to_dense(g), self.to_dense(h), self.ngens-1, self.domain))
|
| 340 |
+
|
| 341 |
+
def dup_mul(self, f, g):
|
| 342 |
+
return self.from_dense(dup_mul(self.to_dense(f), self.to_dense(g), self.domain))
|
| 343 |
+
def dmp_mul(self, f, g):
|
| 344 |
+
return self.from_dense(dmp_mul(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 345 |
+
|
| 346 |
+
def dup_sqr(self, f):
|
| 347 |
+
return self.from_dense(dup_sqr(self.to_dense(f), self.domain))
|
| 348 |
+
def dmp_sqr(self, f):
|
| 349 |
+
return self.from_dense(dmp_sqr(self.to_dense(f), self.ngens-1, self.domain))
|
| 350 |
+
def dup_pow(self, f, n):
|
| 351 |
+
return self.from_dense(dup_pow(self.to_dense(f), n, self.domain))
|
| 352 |
+
def dmp_pow(self, f, n):
|
| 353 |
+
return self.from_dense(dmp_pow(self.to_dense(f), n, self.ngens-1, self.domain))
|
| 354 |
+
|
| 355 |
+
def dup_pdiv(self, f, g):
|
| 356 |
+
q, r = dup_pdiv(self.to_dense(f), self.to_dense(g), self.domain)
|
| 357 |
+
return (self.from_dense(q), self.from_dense(r))
|
| 358 |
+
def dup_prem(self, f, g):
|
| 359 |
+
return self.from_dense(dup_prem(self.to_dense(f), self.to_dense(g), self.domain))
|
| 360 |
+
def dup_pquo(self, f, g):
|
| 361 |
+
return self.from_dense(dup_pquo(self.to_dense(f), self.to_dense(g), self.domain))
|
| 362 |
+
def dup_pexquo(self, f, g):
|
| 363 |
+
return self.from_dense(dup_pexquo(self.to_dense(f), self.to_dense(g), self.domain))
|
| 364 |
+
|
| 365 |
+
def dmp_pdiv(self, f, g):
|
| 366 |
+
q, r = dmp_pdiv(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 367 |
+
return (self.from_dense(q), self.from_dense(r))
|
| 368 |
+
def dmp_prem(self, f, g):
|
| 369 |
+
return self.from_dense(dmp_prem(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 370 |
+
def dmp_pquo(self, f, g):
|
| 371 |
+
return self.from_dense(dmp_pquo(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 372 |
+
def dmp_pexquo(self, f, g):
|
| 373 |
+
return self.from_dense(dmp_pexquo(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 374 |
+
|
| 375 |
+
def dup_rr_div(self, f, g):
|
| 376 |
+
q, r = dup_rr_div(self.to_dense(f), self.to_dense(g), self.domain)
|
| 377 |
+
return (self.from_dense(q), self.from_dense(r))
|
| 378 |
+
def dmp_rr_div(self, f, g):
|
| 379 |
+
q, r = dmp_rr_div(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 380 |
+
return (self.from_dense(q), self.from_dense(r))
|
| 381 |
+
def dup_ff_div(self, f, g):
|
| 382 |
+
q, r = dup_ff_div(self.to_dense(f), self.to_dense(g), self.domain)
|
| 383 |
+
return (self.from_dense(q), self.from_dense(r))
|
| 384 |
+
def dmp_ff_div(self, f, g):
|
| 385 |
+
q, r = dmp_ff_div(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 386 |
+
return (self.from_dense(q), self.from_dense(r))
|
| 387 |
+
|
| 388 |
+
def dup_div(self, f, g):
|
| 389 |
+
q, r = dup_div(self.to_dense(f), self.to_dense(g), self.domain)
|
| 390 |
+
return (self.from_dense(q), self.from_dense(r))
|
| 391 |
+
def dup_rem(self, f, g):
|
| 392 |
+
return self.from_dense(dup_rem(self.to_dense(f), self.to_dense(g), self.domain))
|
| 393 |
+
def dup_quo(self, f, g):
|
| 394 |
+
return self.from_dense(dup_quo(self.to_dense(f), self.to_dense(g), self.domain))
|
| 395 |
+
def dup_exquo(self, f, g):
|
| 396 |
+
return self.from_dense(dup_exquo(self.to_dense(f), self.to_dense(g), self.domain))
|
| 397 |
+
|
| 398 |
+
def dmp_div(self, f, g):
|
| 399 |
+
q, r = dmp_div(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 400 |
+
return (self.from_dense(q), self.from_dense(r))
|
| 401 |
+
def dmp_rem(self, f, g):
|
| 402 |
+
return self.from_dense(dmp_rem(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 403 |
+
def dmp_quo(self, f, g):
|
| 404 |
+
return self.from_dense(dmp_quo(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 405 |
+
def dmp_exquo(self, f, g):
|
| 406 |
+
return self.from_dense(dmp_exquo(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 407 |
+
|
| 408 |
+
def dup_max_norm(self, f):
|
| 409 |
+
return dup_max_norm(self.to_dense(f), self.domain)
|
| 410 |
+
def dmp_max_norm(self, f):
|
| 411 |
+
return dmp_max_norm(self.to_dense(f), self.ngens-1, self.domain)
|
| 412 |
+
|
| 413 |
+
def dup_l1_norm(self, f):
|
| 414 |
+
return dup_l1_norm(self.to_dense(f), self.domain)
|
| 415 |
+
def dmp_l1_norm(self, f):
|
| 416 |
+
return dmp_l1_norm(self.to_dense(f), self.ngens-1, self.domain)
|
| 417 |
+
|
| 418 |
+
def dup_l2_norm_squared(self, f):
|
| 419 |
+
return dup_l2_norm_squared(self.to_dense(f), self.domain)
|
| 420 |
+
def dmp_l2_norm_squared(self, f):
|
| 421 |
+
return dmp_l2_norm_squared(self.to_dense(f), self.ngens-1, self.domain)
|
| 422 |
+
|
| 423 |
+
def dup_expand(self, polys):
|
| 424 |
+
return self.from_dense(dup_expand(list(map(self.to_dense, polys)), self.domain))
|
| 425 |
+
def dmp_expand(self, polys):
|
| 426 |
+
return self.from_dense(dmp_expand(list(map(self.to_dense, polys)), self.ngens-1, self.domain))
|
| 427 |
+
|
| 428 |
+
def dup_LC(self, f):
|
| 429 |
+
return dup_LC(self.to_dense(f), self.domain)
|
| 430 |
+
def dmp_LC(self, f):
|
| 431 |
+
LC = dmp_LC(self.to_dense(f), self.domain)
|
| 432 |
+
if isinstance(LC, list):
|
| 433 |
+
return self[1:].from_dense(LC)
|
| 434 |
+
else:
|
| 435 |
+
return LC
|
| 436 |
+
def dup_TC(self, f):
|
| 437 |
+
return dup_TC(self.to_dense(f), self.domain)
|
| 438 |
+
def dmp_TC(self, f):
|
| 439 |
+
TC = dmp_TC(self.to_dense(f), self.domain)
|
| 440 |
+
if isinstance(TC, list):
|
| 441 |
+
return self[1:].from_dense(TC)
|
| 442 |
+
else:
|
| 443 |
+
return TC
|
| 444 |
+
|
| 445 |
+
def dmp_ground_LC(self, f):
|
| 446 |
+
return dmp_ground_LC(self.to_dense(f), self.ngens-1, self.domain)
|
| 447 |
+
def dmp_ground_TC(self, f):
|
| 448 |
+
return dmp_ground_TC(self.to_dense(f), self.ngens-1, self.domain)
|
| 449 |
+
|
| 450 |
+
def dup_degree(self, f):
|
| 451 |
+
return dup_degree(self.to_dense(f))
|
| 452 |
+
def dmp_degree(self, f):
|
| 453 |
+
return dmp_degree(self.to_dense(f), self.ngens-1)
|
| 454 |
+
def dmp_degree_in(self, f, j):
|
| 455 |
+
return dmp_degree_in(self.to_dense(f), j, self.ngens-1)
|
| 456 |
+
def dup_integrate(self, f, m):
|
| 457 |
+
return self.from_dense(dup_integrate(self.to_dense(f), m, self.domain))
|
| 458 |
+
def dmp_integrate(self, f, m):
|
| 459 |
+
return self.from_dense(dmp_integrate(self.to_dense(f), m, self.ngens-1, self.domain))
|
| 460 |
+
|
| 461 |
+
def dup_diff(self, f, m):
|
| 462 |
+
return self.from_dense(dup_diff(self.to_dense(f), m, self.domain))
|
| 463 |
+
def dmp_diff(self, f, m):
|
| 464 |
+
return self.from_dense(dmp_diff(self.to_dense(f), m, self.ngens-1, self.domain))
|
| 465 |
+
|
| 466 |
+
def dmp_diff_in(self, f, m, j):
|
| 467 |
+
return self.from_dense(dmp_diff_in(self.to_dense(f), m, j, self.ngens-1, self.domain))
|
| 468 |
+
def dmp_integrate_in(self, f, m, j):
|
| 469 |
+
return self.from_dense(dmp_integrate_in(self.to_dense(f), m, j, self.ngens-1, self.domain))
|
| 470 |
+
|
| 471 |
+
def dup_eval(self, f, a):
|
| 472 |
+
return dup_eval(self.to_dense(f), a, self.domain)
|
| 473 |
+
def dmp_eval(self, f, a):
|
| 474 |
+
result = dmp_eval(self.to_dense(f), a, self.ngens-1, self.domain)
|
| 475 |
+
return self[1:].from_dense(result)
|
| 476 |
+
|
| 477 |
+
def dmp_eval_in(self, f, a, j):
|
| 478 |
+
result = dmp_eval_in(self.to_dense(f), a, j, self.ngens-1, self.domain)
|
| 479 |
+
return self.drop(j).from_dense(result)
|
| 480 |
+
def dmp_diff_eval_in(self, f, m, a, j):
|
| 481 |
+
result = dmp_diff_eval_in(self.to_dense(f), m, a, j, self.ngens-1, self.domain)
|
| 482 |
+
return self.drop(j).from_dense(result)
|
| 483 |
+
|
| 484 |
+
def dmp_eval_tail(self, f, A):
|
| 485 |
+
result = dmp_eval_tail(self.to_dense(f), A, self.ngens-1, self.domain)
|
| 486 |
+
if isinstance(result, list):
|
| 487 |
+
return self[:-len(A)].from_dense(result)
|
| 488 |
+
else:
|
| 489 |
+
return result
|
| 490 |
+
|
| 491 |
+
def dup_trunc(self, f, p):
|
| 492 |
+
return self.from_dense(dup_trunc(self.to_dense(f), p, self.domain))
|
| 493 |
+
def dmp_trunc(self, f, g):
|
| 494 |
+
return self.from_dense(dmp_trunc(self.to_dense(f), self[1:].to_dense(g), self.ngens-1, self.domain))
|
| 495 |
+
def dmp_ground_trunc(self, f, p):
|
| 496 |
+
return self.from_dense(dmp_ground_trunc(self.to_dense(f), p, self.ngens-1, self.domain))
|
| 497 |
+
|
| 498 |
+
def dup_monic(self, f):
|
| 499 |
+
return self.from_dense(dup_monic(self.to_dense(f), self.domain))
|
| 500 |
+
def dmp_ground_monic(self, f):
|
| 501 |
+
return self.from_dense(dmp_ground_monic(self.to_dense(f), self.ngens-1, self.domain))
|
| 502 |
+
|
| 503 |
+
def dup_extract(self, f, g):
|
| 504 |
+
c, F, G = dup_extract(self.to_dense(f), self.to_dense(g), self.domain)
|
| 505 |
+
return (c, self.from_dense(F), self.from_dense(G))
|
| 506 |
+
def dmp_ground_extract(self, f, g):
|
| 507 |
+
c, F, G = dmp_ground_extract(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 508 |
+
return (c, self.from_dense(F), self.from_dense(G))
|
| 509 |
+
|
| 510 |
+
def dup_real_imag(self, f):
|
| 511 |
+
p, q = dup_real_imag(self.wrap(f).drop(1).to_dense(), self.domain)
|
| 512 |
+
return (self.from_dense(p), self.from_dense(q))
|
| 513 |
+
|
| 514 |
+
def dup_mirror(self, f):
|
| 515 |
+
return self.from_dense(dup_mirror(self.to_dense(f), self.domain))
|
| 516 |
+
def dup_scale(self, f, a):
|
| 517 |
+
return self.from_dense(dup_scale(self.to_dense(f), a, self.domain))
|
| 518 |
+
def dup_shift(self, f, a):
|
| 519 |
+
return self.from_dense(dup_shift(self.to_dense(f), a, self.domain))
|
| 520 |
+
def dmp_shift(self, f, a):
|
| 521 |
+
return self.from_dense(dmp_shift(self.to_dense(f), a, self.ngens-1, self.domain))
|
| 522 |
+
def dup_transform(self, f, p, q):
|
| 523 |
+
return self.from_dense(dup_transform(self.to_dense(f), self.to_dense(p), self.to_dense(q), self.domain))
|
| 524 |
+
|
| 525 |
+
def dup_compose(self, f, g):
|
| 526 |
+
return self.from_dense(dup_compose(self.to_dense(f), self.to_dense(g), self.domain))
|
| 527 |
+
def dmp_compose(self, f, g):
|
| 528 |
+
return self.from_dense(dmp_compose(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 529 |
+
|
| 530 |
+
def dup_decompose(self, f):
|
| 531 |
+
components = dup_decompose(self.to_dense(f), self.domain)
|
| 532 |
+
return list(map(self.from_dense, components))
|
| 533 |
+
|
| 534 |
+
def dmp_lift(self, f):
|
| 535 |
+
result = dmp_lift(self.to_dense(f), self.ngens-1, self.domain)
|
| 536 |
+
return self.to_ground().from_dense(result)
|
| 537 |
+
|
| 538 |
+
def dup_sign_variations(self, f):
|
| 539 |
+
return dup_sign_variations(self.to_dense(f), self.domain)
|
| 540 |
+
|
| 541 |
+
def dup_clear_denoms(self, f, convert=False):
|
| 542 |
+
c, F = dup_clear_denoms(self.to_dense(f), self.domain, convert=convert)
|
| 543 |
+
if convert:
|
| 544 |
+
ring = self.clone(domain=self.domain.get_ring())
|
| 545 |
+
else:
|
| 546 |
+
ring = self
|
| 547 |
+
return (c, ring.from_dense(F))
|
| 548 |
+
def dmp_clear_denoms(self, f, convert=False):
|
| 549 |
+
c, F = dmp_clear_denoms(self.to_dense(f), self.ngens-1, self.domain, convert=convert)
|
| 550 |
+
if convert:
|
| 551 |
+
ring = self.clone(domain=self.domain.get_ring())
|
| 552 |
+
else:
|
| 553 |
+
ring = self
|
| 554 |
+
return (c, ring.from_dense(F))
|
| 555 |
+
|
| 556 |
+
def dup_revert(self, f, n):
|
| 557 |
+
return self.from_dense(dup_revert(self.to_dense(f), n, self.domain))
|
| 558 |
+
|
| 559 |
+
def dup_half_gcdex(self, f, g):
|
| 560 |
+
s, h = dup_half_gcdex(self.to_dense(f), self.to_dense(g), self.domain)
|
| 561 |
+
return (self.from_dense(s), self.from_dense(h))
|
| 562 |
+
def dmp_half_gcdex(self, f, g):
|
| 563 |
+
s, h = dmp_half_gcdex(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 564 |
+
return (self.from_dense(s), self.from_dense(h))
|
| 565 |
+
def dup_gcdex(self, f, g):
|
| 566 |
+
s, t, h = dup_gcdex(self.to_dense(f), self.to_dense(g), self.domain)
|
| 567 |
+
return (self.from_dense(s), self.from_dense(t), self.from_dense(h))
|
| 568 |
+
def dmp_gcdex(self, f, g):
|
| 569 |
+
s, t, h = dmp_gcdex(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 570 |
+
return (self.from_dense(s), self.from_dense(t), self.from_dense(h))
|
| 571 |
+
|
| 572 |
+
def dup_invert(self, f, g):
|
| 573 |
+
return self.from_dense(dup_invert(self.to_dense(f), self.to_dense(g), self.domain))
|
| 574 |
+
def dmp_invert(self, f, g):
|
| 575 |
+
return self.from_dense(dmp_invert(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain))
|
| 576 |
+
|
| 577 |
+
def dup_euclidean_prs(self, f, g):
|
| 578 |
+
prs = dup_euclidean_prs(self.to_dense(f), self.to_dense(g), self.domain)
|
| 579 |
+
return list(map(self.from_dense, prs))
|
| 580 |
+
def dmp_euclidean_prs(self, f, g):
|
| 581 |
+
prs = dmp_euclidean_prs(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 582 |
+
return list(map(self.from_dense, prs))
|
| 583 |
+
def dup_primitive_prs(self, f, g):
|
| 584 |
+
prs = dup_primitive_prs(self.to_dense(f), self.to_dense(g), self.domain)
|
| 585 |
+
return list(map(self.from_dense, prs))
|
| 586 |
+
def dmp_primitive_prs(self, f, g):
|
| 587 |
+
prs = dmp_primitive_prs(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 588 |
+
return list(map(self.from_dense, prs))
|
| 589 |
+
|
| 590 |
+
def dup_inner_subresultants(self, f, g):
|
| 591 |
+
prs, sres = dup_inner_subresultants(self.to_dense(f), self.to_dense(g), self.domain)
|
| 592 |
+
return (list(map(self.from_dense, prs)), sres)
|
| 593 |
+
def dmp_inner_subresultants(self, f, g):
|
| 594 |
+
prs, sres = dmp_inner_subresultants(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 595 |
+
return (list(map(self.from_dense, prs)), sres)
|
| 596 |
+
|
| 597 |
+
def dup_subresultants(self, f, g):
|
| 598 |
+
prs = dup_subresultants(self.to_dense(f), self.to_dense(g), self.domain)
|
| 599 |
+
return list(map(self.from_dense, prs))
|
| 600 |
+
def dmp_subresultants(self, f, g):
|
| 601 |
+
prs = dmp_subresultants(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 602 |
+
return list(map(self.from_dense, prs))
|
| 603 |
+
|
| 604 |
+
def dup_prs_resultant(self, f, g):
|
| 605 |
+
res, prs = dup_prs_resultant(self.to_dense(f), self.to_dense(g), self.domain)
|
| 606 |
+
return (res, list(map(self.from_dense, prs)))
|
| 607 |
+
def dmp_prs_resultant(self, f, g):
|
| 608 |
+
res, prs = dmp_prs_resultant(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 609 |
+
return (self[1:].from_dense(res), list(map(self.from_dense, prs)))
|
| 610 |
+
|
| 611 |
+
def dmp_zz_modular_resultant(self, f, g, p):
|
| 612 |
+
res = dmp_zz_modular_resultant(self.to_dense(f), self.to_dense(g), self.domain_new(p), self.ngens-1, self.domain)
|
| 613 |
+
return self[1:].from_dense(res)
|
| 614 |
+
def dmp_zz_collins_resultant(self, f, g):
|
| 615 |
+
res = dmp_zz_collins_resultant(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 616 |
+
return self[1:].from_dense(res)
|
| 617 |
+
def dmp_qq_collins_resultant(self, f, g):
|
| 618 |
+
res = dmp_qq_collins_resultant(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 619 |
+
return self[1:].from_dense(res)
|
| 620 |
+
|
| 621 |
+
def dup_resultant(self, f, g): #, includePRS=False):
|
| 622 |
+
return dup_resultant(self.to_dense(f), self.to_dense(g), self.domain) #, includePRS=includePRS)
|
| 623 |
+
def dmp_resultant(self, f, g): #, includePRS=False):
|
| 624 |
+
res = dmp_resultant(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain) #, includePRS=includePRS)
|
| 625 |
+
if isinstance(res, list):
|
| 626 |
+
return self[1:].from_dense(res)
|
| 627 |
+
else:
|
| 628 |
+
return res
|
| 629 |
+
|
| 630 |
+
def dup_discriminant(self, f):
|
| 631 |
+
return dup_discriminant(self.to_dense(f), self.domain)
|
| 632 |
+
def dmp_discriminant(self, f):
|
| 633 |
+
disc = dmp_discriminant(self.to_dense(f), self.ngens-1, self.domain)
|
| 634 |
+
if isinstance(disc, list):
|
| 635 |
+
return self[1:].from_dense(disc)
|
| 636 |
+
else:
|
| 637 |
+
return disc
|
| 638 |
+
|
| 639 |
+
def dup_rr_prs_gcd(self, f, g):
|
| 640 |
+
H, F, G = dup_rr_prs_gcd(self.to_dense(f), self.to_dense(g), self.domain)
|
| 641 |
+
return (self.from_dense(H), self.from_dense(F), self.from_dense(G))
|
| 642 |
+
def dup_ff_prs_gcd(self, f, g):
|
| 643 |
+
H, F, G = dup_ff_prs_gcd(self.to_dense(f), self.to_dense(g), self.domain)
|
| 644 |
+
return (self.from_dense(H), self.from_dense(F), self.from_dense(G))
|
| 645 |
+
def dmp_rr_prs_gcd(self, f, g):
|
| 646 |
+
H, F, G = dmp_rr_prs_gcd(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 647 |
+
return (self.from_dense(H), self.from_dense(F), self.from_dense(G))
|
| 648 |
+
def dmp_ff_prs_gcd(self, f, g):
|
| 649 |
+
H, F, G = dmp_ff_prs_gcd(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 650 |
+
return (self.from_dense(H), self.from_dense(F), self.from_dense(G))
|
| 651 |
+
def dup_zz_heu_gcd(self, f, g):
|
| 652 |
+
H, F, G = dup_zz_heu_gcd(self.to_dense(f), self.to_dense(g), self.domain)
|
| 653 |
+
return (self.from_dense(H), self.from_dense(F), self.from_dense(G))
|
| 654 |
+
def dmp_zz_heu_gcd(self, f, g):
|
| 655 |
+
H, F, G = dmp_zz_heu_gcd(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 656 |
+
return (self.from_dense(H), self.from_dense(F), self.from_dense(G))
|
| 657 |
+
def dup_qq_heu_gcd(self, f, g):
|
| 658 |
+
H, F, G = dup_qq_heu_gcd(self.to_dense(f), self.to_dense(g), self.domain)
|
| 659 |
+
return (self.from_dense(H), self.from_dense(F), self.from_dense(G))
|
| 660 |
+
def dmp_qq_heu_gcd(self, f, g):
|
| 661 |
+
H, F, G = dmp_qq_heu_gcd(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 662 |
+
return (self.from_dense(H), self.from_dense(F), self.from_dense(G))
|
| 663 |
+
def dup_inner_gcd(self, f, g):
|
| 664 |
+
H, F, G = dup_inner_gcd(self.to_dense(f), self.to_dense(g), self.domain)
|
| 665 |
+
return (self.from_dense(H), self.from_dense(F), self.from_dense(G))
|
| 666 |
+
def dmp_inner_gcd(self, f, g):
|
| 667 |
+
H, F, G = dmp_inner_gcd(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 668 |
+
return (self.from_dense(H), self.from_dense(F), self.from_dense(G))
|
| 669 |
+
def dup_gcd(self, f, g):
|
| 670 |
+
H = dup_gcd(self.to_dense(f), self.to_dense(g), self.domain)
|
| 671 |
+
return self.from_dense(H)
|
| 672 |
+
def dmp_gcd(self, f, g):
|
| 673 |
+
H = dmp_gcd(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 674 |
+
return self.from_dense(H)
|
| 675 |
+
def dup_rr_lcm(self, f, g):
|
| 676 |
+
H = dup_rr_lcm(self.to_dense(f), self.to_dense(g), self.domain)
|
| 677 |
+
return self.from_dense(H)
|
| 678 |
+
def dup_ff_lcm(self, f, g):
|
| 679 |
+
H = dup_ff_lcm(self.to_dense(f), self.to_dense(g), self.domain)
|
| 680 |
+
return self.from_dense(H)
|
| 681 |
+
def dup_lcm(self, f, g):
|
| 682 |
+
H = dup_lcm(self.to_dense(f), self.to_dense(g), self.domain)
|
| 683 |
+
return self.from_dense(H)
|
| 684 |
+
def dmp_rr_lcm(self, f, g):
|
| 685 |
+
H = dmp_rr_lcm(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 686 |
+
return self.from_dense(H)
|
| 687 |
+
def dmp_ff_lcm(self, f, g):
|
| 688 |
+
H = dmp_ff_lcm(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 689 |
+
return self.from_dense(H)
|
| 690 |
+
def dmp_lcm(self, f, g):
|
| 691 |
+
H = dmp_lcm(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain)
|
| 692 |
+
return self.from_dense(H)
|
| 693 |
+
|
| 694 |
+
def dup_content(self, f):
|
| 695 |
+
cont = dup_content(self.to_dense(f), self.domain)
|
| 696 |
+
return cont
|
| 697 |
+
def dup_primitive(self, f):
|
| 698 |
+
cont, prim = dup_primitive(self.to_dense(f), self.domain)
|
| 699 |
+
return cont, self.from_dense(prim)
|
| 700 |
+
|
| 701 |
+
def dmp_content(self, f):
|
| 702 |
+
cont = dmp_content(self.to_dense(f), self.ngens-1, self.domain)
|
| 703 |
+
if isinstance(cont, list):
|
| 704 |
+
return self[1:].from_dense(cont)
|
| 705 |
+
else:
|
| 706 |
+
return cont
|
| 707 |
+
def dmp_primitive(self, f):
|
| 708 |
+
cont, prim = dmp_primitive(self.to_dense(f), self.ngens-1, self.domain)
|
| 709 |
+
if isinstance(cont, list):
|
| 710 |
+
return (self[1:].from_dense(cont), self.from_dense(prim))
|
| 711 |
+
else:
|
| 712 |
+
return (cont, self.from_dense(prim))
|
| 713 |
+
|
| 714 |
+
def dmp_ground_content(self, f):
|
| 715 |
+
cont = dmp_ground_content(self.to_dense(f), self.ngens-1, self.domain)
|
| 716 |
+
return cont
|
| 717 |
+
def dmp_ground_primitive(self, f):
|
| 718 |
+
cont, prim = dmp_ground_primitive(self.to_dense(f), self.ngens-1, self.domain)
|
| 719 |
+
return (cont, self.from_dense(prim))
|
| 720 |
+
|
| 721 |
+
def dup_cancel(self, f, g, include=True):
|
| 722 |
+
result = dup_cancel(self.to_dense(f), self.to_dense(g), self.domain, include=include)
|
| 723 |
+
if not include:
|
| 724 |
+
cf, cg, F, G = result
|
| 725 |
+
return (cf, cg, self.from_dense(F), self.from_dense(G))
|
| 726 |
+
else:
|
| 727 |
+
F, G = result
|
| 728 |
+
return (self.from_dense(F), self.from_dense(G))
|
| 729 |
+
def dmp_cancel(self, f, g, include=True):
|
| 730 |
+
result = dmp_cancel(self.to_dense(f), self.to_dense(g), self.ngens-1, self.domain, include=include)
|
| 731 |
+
if not include:
|
| 732 |
+
cf, cg, F, G = result
|
| 733 |
+
return (cf, cg, self.from_dense(F), self.from_dense(G))
|
| 734 |
+
else:
|
| 735 |
+
F, G = result
|
| 736 |
+
return (self.from_dense(F), self.from_dense(G))
|
| 737 |
+
|
| 738 |
+
def dup_trial_division(self, f, factors):
|
| 739 |
+
factors = dup_trial_division(self.to_dense(f), list(map(self.to_dense, factors)), self.domain)
|
| 740 |
+
return [ (self.from_dense(g), k) for g, k in factors ]
|
| 741 |
+
def dmp_trial_division(self, f, factors):
|
| 742 |
+
factors = dmp_trial_division(self.to_dense(f), list(map(self.to_dense, factors)), self.ngens-1, self.domain)
|
| 743 |
+
return [ (self.from_dense(g), k) for g, k in factors ]
|
| 744 |
+
|
| 745 |
+
def dup_zz_mignotte_bound(self, f):
|
| 746 |
+
return dup_zz_mignotte_bound(self.to_dense(f), self.domain)
|
| 747 |
+
def dmp_zz_mignotte_bound(self, f):
|
| 748 |
+
return dmp_zz_mignotte_bound(self.to_dense(f), self.ngens-1, self.domain)
|
| 749 |
+
|
| 750 |
+
def dup_zz_hensel_step(self, m, f, g, h, s, t):
|
| 751 |
+
D = self.to_dense
|
| 752 |
+
G, H, S, T = dup_zz_hensel_step(m, D(f), D(g), D(h), D(s), D(t), self.domain)
|
| 753 |
+
return (self.from_dense(G), self.from_dense(H), self.from_dense(S), self.from_dense(T))
|
| 754 |
+
def dup_zz_hensel_lift(self, p, f, f_list, l):
|
| 755 |
+
D = self.to_dense
|
| 756 |
+
polys = dup_zz_hensel_lift(p, D(f), list(map(D, f_list)), l, self.domain)
|
| 757 |
+
return list(map(self.from_dense, polys))
|
| 758 |
+
|
| 759 |
+
def dup_zz_zassenhaus(self, f):
|
| 760 |
+
factors = dup_zz_zassenhaus(self.to_dense(f), self.domain)
|
| 761 |
+
return [ (self.from_dense(g), k) for g, k in factors ]
|
| 762 |
+
|
| 763 |
+
def dup_zz_irreducible_p(self, f):
|
| 764 |
+
return dup_zz_irreducible_p(self.to_dense(f), self.domain)
|
| 765 |
+
def dup_cyclotomic_p(self, f, irreducible=False):
|
| 766 |
+
return dup_cyclotomic_p(self.to_dense(f), self.domain, irreducible=irreducible)
|
| 767 |
+
def dup_zz_cyclotomic_poly(self, n):
|
| 768 |
+
F = dup_zz_cyclotomic_poly(n, self.domain)
|
| 769 |
+
return self.from_dense(F)
|
| 770 |
+
def dup_zz_cyclotomic_factor(self, f):
|
| 771 |
+
result = dup_zz_cyclotomic_factor(self.to_dense(f), self.domain)
|
| 772 |
+
if result is None:
|
| 773 |
+
return result
|
| 774 |
+
else:
|
| 775 |
+
return list(map(self.from_dense, result))
|
| 776 |
+
|
| 777 |
+
# E: List[ZZ], cs: ZZ, ct: ZZ
|
| 778 |
+
def dmp_zz_wang_non_divisors(self, E, cs, ct):
|
| 779 |
+
return dmp_zz_wang_non_divisors(E, cs, ct, self.domain)
|
| 780 |
+
|
| 781 |
+
# f: Poly, T: List[(Poly, int)], ct: ZZ, A: List[ZZ]
|
| 782 |
+
#def dmp_zz_wang_test_points(f, T, ct, A):
|
| 783 |
+
# dmp_zz_wang_test_points(self.to_dense(f), T, ct, A, self.ngens-1, self.domain)
|
| 784 |
+
|
| 785 |
+
# f: Poly, T: List[(Poly, int)], cs: ZZ, E: List[ZZ], H: List[Poly], A: List[ZZ]
|
| 786 |
+
def dmp_zz_wang_lead_coeffs(self, f, T, cs, E, H, A):
|
| 787 |
+
mv = self[1:]
|
| 788 |
+
T = [ (mv.to_dense(t), k) for t, k in T ]
|
| 789 |
+
uv = self[:1]
|
| 790 |
+
H = list(map(uv.to_dense, H))
|
| 791 |
+
f, HH, CC = dmp_zz_wang_lead_coeffs(self.to_dense(f), T, cs, E, H, A, self.ngens-1, self.domain)
|
| 792 |
+
return self.from_dense(f), list(map(uv.from_dense, HH)), list(map(mv.from_dense, CC))
|
| 793 |
+
|
| 794 |
+
# f: List[Poly], m: int, p: ZZ
|
| 795 |
+
def dup_zz_diophantine(self, F, m, p):
|
| 796 |
+
result = dup_zz_diophantine(list(map(self.to_dense, F)), m, p, self.domain)
|
| 797 |
+
return list(map(self.from_dense, result))
|
| 798 |
+
|
| 799 |
+
# f: List[Poly], c: List[Poly], A: List[ZZ], d: int, p: ZZ
|
| 800 |
+
def dmp_zz_diophantine(self, F, c, A, d, p):
|
| 801 |
+
result = dmp_zz_diophantine(list(map(self.to_dense, F)), self.to_dense(c), A, d, p, self.ngens-1, self.domain)
|
| 802 |
+
return list(map(self.from_dense, result))
|
| 803 |
+
|
| 804 |
+
# f: Poly, H: List[Poly], LC: List[Poly], A: List[ZZ], p: ZZ
|
| 805 |
+
def dmp_zz_wang_hensel_lifting(self, f, H, LC, A, p):
|
| 806 |
+
uv = self[:1]
|
| 807 |
+
mv = self[1:]
|
| 808 |
+
H = list(map(uv.to_dense, H))
|
| 809 |
+
LC = list(map(mv.to_dense, LC))
|
| 810 |
+
result = dmp_zz_wang_hensel_lifting(self.to_dense(f), H, LC, A, p, self.ngens-1, self.domain)
|
| 811 |
+
return list(map(self.from_dense, result))
|
| 812 |
+
|
| 813 |
+
def dmp_zz_wang(self, f, mod=None, seed=None):
|
| 814 |
+
factors = dmp_zz_wang(self.to_dense(f), self.ngens-1, self.domain, mod=mod, seed=seed)
|
| 815 |
+
return [ self.from_dense(g) for g in factors ]
|
| 816 |
+
|
| 817 |
+
def dup_zz_factor_sqf(self, f):
|
| 818 |
+
coeff, factors = dup_zz_factor_sqf(self.to_dense(f), self.domain)
|
| 819 |
+
return (coeff, [ self.from_dense(g) for g in factors ])
|
| 820 |
+
|
| 821 |
+
def dup_zz_factor(self, f):
|
| 822 |
+
coeff, factors = dup_zz_factor(self.to_dense(f), self.domain)
|
| 823 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 824 |
+
def dmp_zz_factor(self, f):
|
| 825 |
+
coeff, factors = dmp_zz_factor(self.to_dense(f), self.ngens-1, self.domain)
|
| 826 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 827 |
+
|
| 828 |
+
def dup_qq_i_factor(self, f):
|
| 829 |
+
coeff, factors = dup_qq_i_factor(self.to_dense(f), self.domain)
|
| 830 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 831 |
+
def dmp_qq_i_factor(self, f):
|
| 832 |
+
coeff, factors = dmp_qq_i_factor(self.to_dense(f), self.ngens-1, self.domain)
|
| 833 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 834 |
+
|
| 835 |
+
def dup_zz_i_factor(self, f):
|
| 836 |
+
coeff, factors = dup_zz_i_factor(self.to_dense(f), self.domain)
|
| 837 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 838 |
+
def dmp_zz_i_factor(self, f):
|
| 839 |
+
coeff, factors = dmp_zz_i_factor(self.to_dense(f), self.ngens-1, self.domain)
|
| 840 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 841 |
+
|
| 842 |
+
def dup_ext_factor(self, f):
|
| 843 |
+
coeff, factors = dup_ext_factor(self.to_dense(f), self.domain)
|
| 844 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 845 |
+
def dmp_ext_factor(self, f):
|
| 846 |
+
coeff, factors = dmp_ext_factor(self.to_dense(f), self.ngens-1, self.domain)
|
| 847 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 848 |
+
|
| 849 |
+
def dup_gf_factor(self, f):
|
| 850 |
+
coeff, factors = dup_gf_factor(self.to_dense(f), self.domain)
|
| 851 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 852 |
+
def dmp_gf_factor(self, f):
|
| 853 |
+
coeff, factors = dmp_gf_factor(self.to_dense(f), self.ngens-1, self.domain)
|
| 854 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 855 |
+
|
| 856 |
+
def dup_factor_list(self, f):
|
| 857 |
+
coeff, factors = dup_factor_list(self.to_dense(f), self.domain)
|
| 858 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 859 |
+
def dup_factor_list_include(self, f):
|
| 860 |
+
factors = dup_factor_list_include(self.to_dense(f), self.domain)
|
| 861 |
+
return [ (self.from_dense(g), k) for g, k in factors ]
|
| 862 |
+
|
| 863 |
+
def dmp_factor_list(self, f):
|
| 864 |
+
coeff, factors = dmp_factor_list(self.to_dense(f), self.ngens-1, self.domain)
|
| 865 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 866 |
+
def dmp_factor_list_include(self, f):
|
| 867 |
+
factors = dmp_factor_list_include(self.to_dense(f), self.ngens-1, self.domain)
|
| 868 |
+
return [ (self.from_dense(g), k) for g, k in factors ]
|
| 869 |
+
|
| 870 |
+
def dup_irreducible_p(self, f):
|
| 871 |
+
return dup_irreducible_p(self.to_dense(f), self.domain)
|
| 872 |
+
def dmp_irreducible_p(self, f):
|
| 873 |
+
return dmp_irreducible_p(self.to_dense(f), self.ngens-1, self.domain)
|
| 874 |
+
|
| 875 |
+
def dup_sturm(self, f):
|
| 876 |
+
seq = dup_sturm(self.to_dense(f), self.domain)
|
| 877 |
+
return list(map(self.from_dense, seq))
|
| 878 |
+
|
| 879 |
+
def dup_sqf_p(self, f):
|
| 880 |
+
return dup_sqf_p(self.to_dense(f), self.domain)
|
| 881 |
+
def dmp_sqf_p(self, f):
|
| 882 |
+
return dmp_sqf_p(self.to_dense(f), self.ngens-1, self.domain)
|
| 883 |
+
|
| 884 |
+
def dmp_norm(self, f):
|
| 885 |
+
n = dmp_norm(self.to_dense(f), self.ngens-1, self.domain)
|
| 886 |
+
return self.to_ground().from_dense(n)
|
| 887 |
+
|
| 888 |
+
def dup_sqf_norm(self, f):
|
| 889 |
+
s, F, R = dup_sqf_norm(self.to_dense(f), self.domain)
|
| 890 |
+
return (s, self.from_dense(F), self.to_ground().from_dense(R))
|
| 891 |
+
def dmp_sqf_norm(self, f):
|
| 892 |
+
s, F, R = dmp_sqf_norm(self.to_dense(f), self.ngens-1, self.domain)
|
| 893 |
+
return (s, self.from_dense(F), self.to_ground().from_dense(R))
|
| 894 |
+
|
| 895 |
+
def dup_gf_sqf_part(self, f):
|
| 896 |
+
return self.from_dense(dup_gf_sqf_part(self.to_dense(f), self.domain))
|
| 897 |
+
def dmp_gf_sqf_part(self, f):
|
| 898 |
+
return self.from_dense(dmp_gf_sqf_part(self.to_dense(f), self.domain))
|
| 899 |
+
def dup_sqf_part(self, f):
|
| 900 |
+
return self.from_dense(dup_sqf_part(self.to_dense(f), self.domain))
|
| 901 |
+
def dmp_sqf_part(self, f):
|
| 902 |
+
return self.from_dense(dmp_sqf_part(self.to_dense(f), self.ngens-1, self.domain))
|
| 903 |
+
|
| 904 |
+
def dup_gf_sqf_list(self, f, all=False):
|
| 905 |
+
coeff, factors = dup_gf_sqf_list(self.to_dense(f), self.domain, all=all)
|
| 906 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 907 |
+
def dmp_gf_sqf_list(self, f, all=False):
|
| 908 |
+
coeff, factors = dmp_gf_sqf_list(self.to_dense(f), self.ngens-1, self.domain, all=all)
|
| 909 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 910 |
+
|
| 911 |
+
def dup_sqf_list(self, f, all=False):
|
| 912 |
+
coeff, factors = dup_sqf_list(self.to_dense(f), self.domain, all=all)
|
| 913 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 914 |
+
def dup_sqf_list_include(self, f, all=False):
|
| 915 |
+
factors = dup_sqf_list_include(self.to_dense(f), self.domain, all=all)
|
| 916 |
+
return [ (self.from_dense(g), k) for g, k in factors ]
|
| 917 |
+
def dmp_sqf_list(self, f, all=False):
|
| 918 |
+
coeff, factors = dmp_sqf_list(self.to_dense(f), self.ngens-1, self.domain, all=all)
|
| 919 |
+
return (coeff, [ (self.from_dense(g), k) for g, k in factors ])
|
| 920 |
+
def dmp_sqf_list_include(self, f, all=False):
|
| 921 |
+
factors = dmp_sqf_list_include(self.to_dense(f), self.ngens-1, self.domain, all=all)
|
| 922 |
+
return [ (self.from_dense(g), k) for g, k in factors ]
|
| 923 |
+
|
| 924 |
+
def dup_gff_list(self, f):
|
| 925 |
+
factors = dup_gff_list(self.to_dense(f), self.domain)
|
| 926 |
+
return [ (self.from_dense(g), k) for g, k in factors ]
|
| 927 |
+
def dmp_gff_list(self, f):
|
| 928 |
+
factors = dmp_gff_list(self.to_dense(f), self.ngens-1, self.domain)
|
| 929 |
+
return [ (self.from_dense(g), k) for g, k in factors ]
|
| 930 |
+
|
| 931 |
+
def dup_root_upper_bound(self, f):
|
| 932 |
+
return dup_root_upper_bound(self.to_dense(f), self.domain)
|
| 933 |
+
def dup_root_lower_bound(self, f):
|
| 934 |
+
return dup_root_lower_bound(self.to_dense(f), self.domain)
|
| 935 |
+
|
| 936 |
+
def dup_step_refine_real_root(self, f, M, fast=False):
|
| 937 |
+
return dup_step_refine_real_root(self.to_dense(f), M, self.domain, fast=fast)
|
| 938 |
+
def dup_inner_refine_real_root(self, f, M, eps=None, steps=None, disjoint=None, fast=False, mobius=False):
|
| 939 |
+
return dup_inner_refine_real_root(self.to_dense(f), M, self.domain, eps=eps, steps=steps, disjoint=disjoint, fast=fast, mobius=mobius)
|
| 940 |
+
def dup_outer_refine_real_root(self, f, s, t, eps=None, steps=None, disjoint=None, fast=False):
|
| 941 |
+
return dup_outer_refine_real_root(self.to_dense(f), s, t, self.domain, eps=eps, steps=steps, disjoint=disjoint, fast=fast)
|
| 942 |
+
def dup_refine_real_root(self, f, s, t, eps=None, steps=None, disjoint=None, fast=False):
|
| 943 |
+
return dup_refine_real_root(self.to_dense(f), s, t, self.domain, eps=eps, steps=steps, disjoint=disjoint, fast=fast)
|
| 944 |
+
def dup_inner_isolate_real_roots(self, f, eps=None, fast=False):
|
| 945 |
+
return dup_inner_isolate_real_roots(self.to_dense(f), self.domain, eps=eps, fast=fast)
|
| 946 |
+
def dup_inner_isolate_positive_roots(self, f, eps=None, inf=None, sup=None, fast=False, mobius=False):
|
| 947 |
+
return dup_inner_isolate_positive_roots(self.to_dense(f), self.domain, eps=eps, inf=inf, sup=sup, fast=fast, mobius=mobius)
|
| 948 |
+
def dup_inner_isolate_negative_roots(self, f, inf=None, sup=None, eps=None, fast=False, mobius=False):
|
| 949 |
+
return dup_inner_isolate_negative_roots(self.to_dense(f), self.domain, inf=inf, sup=sup, eps=eps, fast=fast, mobius=mobius)
|
| 950 |
+
def dup_isolate_real_roots_sqf(self, f, eps=None, inf=None, sup=None, fast=False, blackbox=False):
|
| 951 |
+
return dup_isolate_real_roots_sqf(self.to_dense(f), self.domain, eps=eps, inf=inf, sup=sup, fast=fast, blackbox=blackbox)
|
| 952 |
+
def dup_isolate_real_roots(self, f, eps=None, inf=None, sup=None, basis=False, fast=False):
|
| 953 |
+
return dup_isolate_real_roots(self.to_dense(f), self.domain, eps=eps, inf=inf, sup=sup, basis=basis, fast=fast)
|
| 954 |
+
def dup_isolate_real_roots_list(self, polys, eps=None, inf=None, sup=None, strict=False, basis=False, fast=False):
|
| 955 |
+
return dup_isolate_real_roots_list(list(map(self.to_dense, polys)), self.domain, eps=eps, inf=inf, sup=sup, strict=strict, basis=basis, fast=fast)
|
| 956 |
+
def dup_count_real_roots(self, f, inf=None, sup=None):
|
| 957 |
+
return dup_count_real_roots(self.to_dense(f), self.domain, inf=inf, sup=sup)
|
| 958 |
+
def dup_count_complex_roots(self, f, inf=None, sup=None, exclude=None):
|
| 959 |
+
return dup_count_complex_roots(self.to_dense(f), self.domain, inf=inf, sup=sup, exclude=exclude)
|
| 960 |
+
def dup_isolate_complex_roots_sqf(self, f, eps=None, inf=None, sup=None, blackbox=False):
|
| 961 |
+
return dup_isolate_complex_roots_sqf(self.to_dense(f), self.domain, eps=eps, inf=inf, sup=sup, blackbox=blackbox)
|
| 962 |
+
def dup_isolate_all_roots_sqf(self, f, eps=None, inf=None, sup=None, fast=False, blackbox=False):
|
| 963 |
+
return dup_isolate_all_roots_sqf(self.to_dense(f), self.domain, eps=eps, inf=inf, sup=sup, fast=fast, blackbox=blackbox)
|
| 964 |
+
def dup_isolate_all_roots(self, f, eps=None, inf=None, sup=None, fast=False):
|
| 965 |
+
return dup_isolate_all_roots(self.to_dense(f), self.domain, eps=eps, inf=inf, sup=sup, fast=fast)
|
| 966 |
+
|
| 967 |
+
def fateman_poly_F_1(self):
|
| 968 |
+
from sympy.polys.specialpolys import dmp_fateman_poly_F_1
|
| 969 |
+
return tuple(map(self.from_dense, dmp_fateman_poly_F_1(self.ngens-1, self.domain)))
|
| 970 |
+
def fateman_poly_F_2(self):
|
| 971 |
+
from sympy.polys.specialpolys import dmp_fateman_poly_F_2
|
| 972 |
+
return tuple(map(self.from_dense, dmp_fateman_poly_F_2(self.ngens-1, self.domain)))
|
| 973 |
+
def fateman_poly_F_3(self):
|
| 974 |
+
from sympy.polys.specialpolys import dmp_fateman_poly_F_3
|
| 975 |
+
return tuple(map(self.from_dense, dmp_fateman_poly_F_3(self.ngens-1, self.domain)))
|
| 976 |
+
|
| 977 |
+
def to_gf_dense(self, element):
|
| 978 |
+
return gf_strip([ self.domain.dom.convert(c, self.domain) for c in self.wrap(element).to_dense() ])
|
| 979 |
+
|
| 980 |
+
def from_gf_dense(self, element):
|
| 981 |
+
return self.from_dict(dmp_to_dict(element, self.ngens-1, self.domain.dom))
|
| 982 |
+
|
| 983 |
+
def gf_degree(self, f):
|
| 984 |
+
return gf_degree(self.to_gf_dense(f))
|
| 985 |
+
|
| 986 |
+
def gf_LC(self, f):
|
| 987 |
+
return gf_LC(self.to_gf_dense(f), self.domain.dom)
|
| 988 |
+
def gf_TC(self, f):
|
| 989 |
+
return gf_TC(self.to_gf_dense(f), self.domain.dom)
|
| 990 |
+
|
| 991 |
+
def gf_strip(self, f):
|
| 992 |
+
return self.from_gf_dense(gf_strip(self.to_gf_dense(f)))
|
| 993 |
+
def gf_trunc(self, f):
|
| 994 |
+
return self.from_gf_dense(gf_strip(self.to_gf_dense(f), self.domain.mod))
|
| 995 |
+
def gf_normal(self, f):
|
| 996 |
+
return self.from_gf_dense(gf_strip(self.to_gf_dense(f), self.domain.mod, self.domain.dom))
|
| 997 |
+
|
| 998 |
+
def gf_from_dict(self, f):
|
| 999 |
+
return self.from_gf_dense(gf_from_dict(f, self.domain.mod, self.domain.dom))
|
| 1000 |
+
def gf_to_dict(self, f, symmetric=True):
|
| 1001 |
+
return gf_to_dict(self.to_gf_dense(f), self.domain.mod, symmetric=symmetric)
|
| 1002 |
+
|
| 1003 |
+
def gf_from_int_poly(self, f):
|
| 1004 |
+
return self.from_gf_dense(gf_from_int_poly(f, self.domain.mod))
|
| 1005 |
+
def gf_to_int_poly(self, f, symmetric=True):
|
| 1006 |
+
return gf_to_int_poly(self.to_gf_dense(f), self.domain.mod, symmetric=symmetric)
|
| 1007 |
+
|
| 1008 |
+
def gf_neg(self, f):
|
| 1009 |
+
return self.from_gf_dense(gf_neg(self.to_gf_dense(f), self.domain.mod, self.domain.dom))
|
| 1010 |
+
|
| 1011 |
+
def gf_add_ground(self, f, a):
|
| 1012 |
+
return self.from_gf_dense(gf_add_ground(self.to_gf_dense(f), a, self.domain.mod, self.domain.dom))
|
| 1013 |
+
def gf_sub_ground(self, f, a):
|
| 1014 |
+
return self.from_gf_dense(gf_sub_ground(self.to_gf_dense(f), a, self.domain.mod, self.domain.dom))
|
| 1015 |
+
def gf_mul_ground(self, f, a):
|
| 1016 |
+
return self.from_gf_dense(gf_mul_ground(self.to_gf_dense(f), a, self.domain.mod, self.domain.dom))
|
| 1017 |
+
def gf_quo_ground(self, f, a):
|
| 1018 |
+
return self.from_gf_dense(gf_quo_ground(self.to_gf_dense(f), a, self.domain.mod, self.domain.dom))
|
| 1019 |
+
|
| 1020 |
+
def gf_add(self, f, g):
|
| 1021 |
+
return self.from_gf_dense(gf_add(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1022 |
+
def gf_sub(self, f, g):
|
| 1023 |
+
return self.from_gf_dense(gf_sub(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1024 |
+
def gf_mul(self, f, g):
|
| 1025 |
+
return self.from_gf_dense(gf_mul(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1026 |
+
def gf_sqr(self, f):
|
| 1027 |
+
return self.from_gf_dense(gf_sqr(self.to_gf_dense(f), self.domain.mod, self.domain.dom))
|
| 1028 |
+
|
| 1029 |
+
def gf_add_mul(self, f, g, h):
|
| 1030 |
+
return self.from_gf_dense(gf_add_mul(self.to_gf_dense(f), self.to_gf_dense(g), self.to_gf_dense(h), self.domain.mod, self.domain.dom))
|
| 1031 |
+
def gf_sub_mul(self, f, g, h):
|
| 1032 |
+
return self.from_gf_dense(gf_sub_mul(self.to_gf_dense(f), self.to_gf_dense(g), self.to_gf_dense(h), self.domain.mod, self.domain.dom))
|
| 1033 |
+
|
| 1034 |
+
def gf_expand(self, F):
|
| 1035 |
+
return self.from_gf_dense(gf_expand(list(map(self.to_gf_dense, F)), self.domain.mod, self.domain.dom))
|
| 1036 |
+
|
| 1037 |
+
def gf_div(self, f, g):
|
| 1038 |
+
q, r = gf_div(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom)
|
| 1039 |
+
return self.from_gf_dense(q), self.from_gf_dense(r)
|
| 1040 |
+
def gf_rem(self, f, g):
|
| 1041 |
+
return self.from_gf_dense(gf_rem(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1042 |
+
def gf_quo(self, f, g):
|
| 1043 |
+
return self.from_gf_dense(gf_quo(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1044 |
+
def gf_exquo(self, f, g):
|
| 1045 |
+
return self.from_gf_dense(gf_exquo(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1046 |
+
|
| 1047 |
+
def gf_lshift(self, f, n):
|
| 1048 |
+
return self.from_gf_dense(gf_lshift(self.to_gf_dense(f), n, self.domain.dom))
|
| 1049 |
+
def gf_rshift(self, f, n):
|
| 1050 |
+
return self.from_gf_dense(gf_rshift(self.to_gf_dense(f), n, self.domain.dom))
|
| 1051 |
+
|
| 1052 |
+
def gf_pow(self, f, n):
|
| 1053 |
+
return self.from_gf_dense(gf_pow(self.to_gf_dense(f), n, self.domain.mod, self.domain.dom))
|
| 1054 |
+
def gf_pow_mod(self, f, n, g):
|
| 1055 |
+
return self.from_gf_dense(gf_pow_mod(self.to_gf_dense(f), n, self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1056 |
+
|
| 1057 |
+
def gf_cofactors(self, f, g):
|
| 1058 |
+
h, cff, cfg = gf_cofactors(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom)
|
| 1059 |
+
return self.from_gf_dense(h), self.from_gf_dense(cff), self.from_gf_dense(cfg)
|
| 1060 |
+
def gf_gcd(self, f, g):
|
| 1061 |
+
return self.from_gf_dense(gf_gcd(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1062 |
+
def gf_lcm(self, f, g):
|
| 1063 |
+
return self.from_gf_dense(gf_lcm(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1064 |
+
def gf_gcdex(self, f, g):
|
| 1065 |
+
return self.from_gf_dense(gf_gcdex(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1066 |
+
|
| 1067 |
+
def gf_monic(self, f):
|
| 1068 |
+
return self.from_gf_dense(gf_monic(self.to_gf_dense(f), self.domain.mod, self.domain.dom))
|
| 1069 |
+
def gf_diff(self, f):
|
| 1070 |
+
return self.from_gf_dense(gf_diff(self.to_gf_dense(f), self.domain.mod, self.domain.dom))
|
| 1071 |
+
|
| 1072 |
+
def gf_eval(self, f, a):
|
| 1073 |
+
return gf_eval(self.to_gf_dense(f), a, self.domain.mod, self.domain.dom)
|
| 1074 |
+
def gf_multi_eval(self, f, A):
|
| 1075 |
+
return gf_multi_eval(self.to_gf_dense(f), A, self.domain.mod, self.domain.dom)
|
| 1076 |
+
|
| 1077 |
+
def gf_compose(self, f, g):
|
| 1078 |
+
return self.from_gf_dense(gf_compose(self.to_gf_dense(f), self.to_gf_dense(g), self.domain.mod, self.domain.dom))
|
| 1079 |
+
def gf_compose_mod(self, g, h, f):
|
| 1080 |
+
return self.from_gf_dense(gf_compose_mod(self.to_gf_dense(g), self.to_gf_dense(h), self.to_gf_dense(f), self.domain.mod, self.domain.dom))
|
| 1081 |
+
|
| 1082 |
+
def gf_trace_map(self, a, b, c, n, f):
|
| 1083 |
+
a = self.to_gf_dense(a)
|
| 1084 |
+
b = self.to_gf_dense(b)
|
| 1085 |
+
c = self.to_gf_dense(c)
|
| 1086 |
+
f = self.to_gf_dense(f)
|
| 1087 |
+
U, V = gf_trace_map(a, b, c, n, f, self.domain.mod, self.domain.dom)
|
| 1088 |
+
return self.from_gf_dense(U), self.from_gf_dense(V)
|
| 1089 |
+
|
| 1090 |
+
def gf_random(self, n):
|
| 1091 |
+
return self.from_gf_dense(gf_random(n, self.domain.mod, self.domain.dom))
|
| 1092 |
+
def gf_irreducible(self, n):
|
| 1093 |
+
return self.from_gf_dense(gf_irreducible(n, self.domain.mod, self.domain.dom))
|
| 1094 |
+
|
| 1095 |
+
def gf_irred_p_ben_or(self, f):
|
| 1096 |
+
return gf_irred_p_ben_or(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1097 |
+
def gf_irred_p_rabin(self, f):
|
| 1098 |
+
return gf_irred_p_rabin(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1099 |
+
def gf_irreducible_p(self, f):
|
| 1100 |
+
return gf_irreducible_p(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1101 |
+
def gf_sqf_p(self, f):
|
| 1102 |
+
return gf_sqf_p(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1103 |
+
|
| 1104 |
+
def gf_sqf_part(self, f):
|
| 1105 |
+
return self.from_gf_dense(gf_sqf_part(self.to_gf_dense(f), self.domain.mod, self.domain.dom))
|
| 1106 |
+
def gf_sqf_list(self, f, all=False):
|
| 1107 |
+
coeff, factors = gf_sqf_part(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1108 |
+
return coeff, [ (self.from_gf_dense(g), k) for g, k in factors ]
|
| 1109 |
+
|
| 1110 |
+
def gf_Qmatrix(self, f):
|
| 1111 |
+
return gf_Qmatrix(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1112 |
+
def gf_berlekamp(self, f):
|
| 1113 |
+
factors = gf_berlekamp(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1114 |
+
return [ self.from_gf_dense(g) for g in factors ]
|
| 1115 |
+
|
| 1116 |
+
def gf_ddf_zassenhaus(self, f):
|
| 1117 |
+
factors = gf_ddf_zassenhaus(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1118 |
+
return [ (self.from_gf_dense(g), k) for g, k in factors ]
|
| 1119 |
+
def gf_edf_zassenhaus(self, f, n):
|
| 1120 |
+
factors = gf_edf_zassenhaus(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1121 |
+
return [ self.from_gf_dense(g) for g in factors ]
|
| 1122 |
+
|
| 1123 |
+
def gf_ddf_shoup(self, f):
|
| 1124 |
+
factors = gf_ddf_shoup(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1125 |
+
return [ (self.from_gf_dense(g), k) for g, k in factors ]
|
| 1126 |
+
def gf_edf_shoup(self, f, n):
|
| 1127 |
+
factors = gf_edf_shoup(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1128 |
+
return [ self.from_gf_dense(g) for g in factors ]
|
| 1129 |
+
|
| 1130 |
+
def gf_zassenhaus(self, f):
|
| 1131 |
+
factors = gf_zassenhaus(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1132 |
+
return [ self.from_gf_dense(g) for g in factors ]
|
| 1133 |
+
def gf_shoup(self, f):
|
| 1134 |
+
factors = gf_shoup(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1135 |
+
return [ self.from_gf_dense(g) for g in factors ]
|
| 1136 |
+
|
| 1137 |
+
def gf_factor_sqf(self, f, method=None):
|
| 1138 |
+
coeff, factors = gf_factor_sqf(self.to_gf_dense(f), self.domain.mod, self.domain.dom, method=method)
|
| 1139 |
+
return coeff, [ self.from_gf_dense(g) for g in factors ]
|
| 1140 |
+
def gf_factor(self, f):
|
| 1141 |
+
coeff, factors = gf_factor(self.to_gf_dense(f), self.domain.mod, self.domain.dom)
|
| 1142 |
+
return coeff, [ (self.from_gf_dense(g), k) for g, k in factors ]
|
vllm/lib/python3.10/site-packages/sympy/polys/densearith.py
ADDED
|
@@ -0,0 +1,1875 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Arithmetics for dense recursive polynomials in ``K[x]`` or ``K[X]``. """
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
from sympy.polys.densebasic import (
|
| 5 |
+
dup_slice,
|
| 6 |
+
dup_LC, dmp_LC,
|
| 7 |
+
dup_degree, dmp_degree,
|
| 8 |
+
dup_strip, dmp_strip,
|
| 9 |
+
dmp_zero_p, dmp_zero,
|
| 10 |
+
dmp_one_p, dmp_one,
|
| 11 |
+
dmp_ground, dmp_zeros)
|
| 12 |
+
from sympy.polys.polyerrors import (ExactQuotientFailed, PolynomialDivisionFailed)
|
| 13 |
+
|
| 14 |
+
def dup_add_term(f, c, i, K):
|
| 15 |
+
"""
|
| 16 |
+
Add ``c*x**i`` to ``f`` in ``K[x]``.
|
| 17 |
+
|
| 18 |
+
Examples
|
| 19 |
+
========
|
| 20 |
+
|
| 21 |
+
>>> from sympy.polys import ring, ZZ
|
| 22 |
+
>>> R, x = ring("x", ZZ)
|
| 23 |
+
|
| 24 |
+
>>> R.dup_add_term(x**2 - 1, ZZ(2), 4)
|
| 25 |
+
2*x**4 + x**2 - 1
|
| 26 |
+
|
| 27 |
+
"""
|
| 28 |
+
if not c:
|
| 29 |
+
return f
|
| 30 |
+
|
| 31 |
+
n = len(f)
|
| 32 |
+
m = n - i - 1
|
| 33 |
+
|
| 34 |
+
if i == n - 1:
|
| 35 |
+
return dup_strip([f[0] + c] + f[1:])
|
| 36 |
+
else:
|
| 37 |
+
if i >= n:
|
| 38 |
+
return [c] + [K.zero]*(i - n) + f
|
| 39 |
+
else:
|
| 40 |
+
return f[:m] + [f[m] + c] + f[m + 1:]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def dmp_add_term(f, c, i, u, K):
|
| 44 |
+
"""
|
| 45 |
+
Add ``c(x_2..x_u)*x_0**i`` to ``f`` in ``K[X]``.
|
| 46 |
+
|
| 47 |
+
Examples
|
| 48 |
+
========
|
| 49 |
+
|
| 50 |
+
>>> from sympy.polys import ring, ZZ
|
| 51 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 52 |
+
|
| 53 |
+
>>> R.dmp_add_term(x*y + 1, 2, 2)
|
| 54 |
+
2*x**2 + x*y + 1
|
| 55 |
+
|
| 56 |
+
"""
|
| 57 |
+
if not u:
|
| 58 |
+
return dup_add_term(f, c, i, K)
|
| 59 |
+
|
| 60 |
+
v = u - 1
|
| 61 |
+
|
| 62 |
+
if dmp_zero_p(c, v):
|
| 63 |
+
return f
|
| 64 |
+
|
| 65 |
+
n = len(f)
|
| 66 |
+
m = n - i - 1
|
| 67 |
+
|
| 68 |
+
if i == n - 1:
|
| 69 |
+
return dmp_strip([dmp_add(f[0], c, v, K)] + f[1:], u)
|
| 70 |
+
else:
|
| 71 |
+
if i >= n:
|
| 72 |
+
return [c] + dmp_zeros(i - n, v, K) + f
|
| 73 |
+
else:
|
| 74 |
+
return f[:m] + [dmp_add(f[m], c, v, K)] + f[m + 1:]
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def dup_sub_term(f, c, i, K):
|
| 78 |
+
"""
|
| 79 |
+
Subtract ``c*x**i`` from ``f`` in ``K[x]``.
|
| 80 |
+
|
| 81 |
+
Examples
|
| 82 |
+
========
|
| 83 |
+
|
| 84 |
+
>>> from sympy.polys import ring, ZZ
|
| 85 |
+
>>> R, x = ring("x", ZZ)
|
| 86 |
+
|
| 87 |
+
>>> R.dup_sub_term(2*x**4 + x**2 - 1, ZZ(2), 4)
|
| 88 |
+
x**2 - 1
|
| 89 |
+
|
| 90 |
+
"""
|
| 91 |
+
if not c:
|
| 92 |
+
return f
|
| 93 |
+
|
| 94 |
+
n = len(f)
|
| 95 |
+
m = n - i - 1
|
| 96 |
+
|
| 97 |
+
if i == n - 1:
|
| 98 |
+
return dup_strip([f[0] - c] + f[1:])
|
| 99 |
+
else:
|
| 100 |
+
if i >= n:
|
| 101 |
+
return [-c] + [K.zero]*(i - n) + f
|
| 102 |
+
else:
|
| 103 |
+
return f[:m] + [f[m] - c] + f[m + 1:]
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def dmp_sub_term(f, c, i, u, K):
|
| 107 |
+
"""
|
| 108 |
+
Subtract ``c(x_2..x_u)*x_0**i`` from ``f`` in ``K[X]``.
|
| 109 |
+
|
| 110 |
+
Examples
|
| 111 |
+
========
|
| 112 |
+
|
| 113 |
+
>>> from sympy.polys import ring, ZZ
|
| 114 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 115 |
+
|
| 116 |
+
>>> R.dmp_sub_term(2*x**2 + x*y + 1, 2, 2)
|
| 117 |
+
x*y + 1
|
| 118 |
+
|
| 119 |
+
"""
|
| 120 |
+
if not u:
|
| 121 |
+
return dup_add_term(f, -c, i, K)
|
| 122 |
+
|
| 123 |
+
v = u - 1
|
| 124 |
+
|
| 125 |
+
if dmp_zero_p(c, v):
|
| 126 |
+
return f
|
| 127 |
+
|
| 128 |
+
n = len(f)
|
| 129 |
+
m = n - i - 1
|
| 130 |
+
|
| 131 |
+
if i == n - 1:
|
| 132 |
+
return dmp_strip([dmp_sub(f[0], c, v, K)] + f[1:], u)
|
| 133 |
+
else:
|
| 134 |
+
if i >= n:
|
| 135 |
+
return [dmp_neg(c, v, K)] + dmp_zeros(i - n, v, K) + f
|
| 136 |
+
else:
|
| 137 |
+
return f[:m] + [dmp_sub(f[m], c, v, K)] + f[m + 1:]
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def dup_mul_term(f, c, i, K):
|
| 141 |
+
"""
|
| 142 |
+
Multiply ``f`` by ``c*x**i`` in ``K[x]``.
|
| 143 |
+
|
| 144 |
+
Examples
|
| 145 |
+
========
|
| 146 |
+
|
| 147 |
+
>>> from sympy.polys import ring, ZZ
|
| 148 |
+
>>> R, x = ring("x", ZZ)
|
| 149 |
+
|
| 150 |
+
>>> R.dup_mul_term(x**2 - 1, ZZ(3), 2)
|
| 151 |
+
3*x**4 - 3*x**2
|
| 152 |
+
|
| 153 |
+
"""
|
| 154 |
+
if not c or not f:
|
| 155 |
+
return []
|
| 156 |
+
else:
|
| 157 |
+
return [ cf * c for cf in f ] + [K.zero]*i
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def dmp_mul_term(f, c, i, u, K):
|
| 161 |
+
"""
|
| 162 |
+
Multiply ``f`` by ``c(x_2..x_u)*x_0**i`` in ``K[X]``.
|
| 163 |
+
|
| 164 |
+
Examples
|
| 165 |
+
========
|
| 166 |
+
|
| 167 |
+
>>> from sympy.polys import ring, ZZ
|
| 168 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 169 |
+
|
| 170 |
+
>>> R.dmp_mul_term(x**2*y + x, 3*y, 2)
|
| 171 |
+
3*x**4*y**2 + 3*x**3*y
|
| 172 |
+
|
| 173 |
+
"""
|
| 174 |
+
if not u:
|
| 175 |
+
return dup_mul_term(f, c, i, K)
|
| 176 |
+
|
| 177 |
+
v = u - 1
|
| 178 |
+
|
| 179 |
+
if dmp_zero_p(f, u):
|
| 180 |
+
return f
|
| 181 |
+
if dmp_zero_p(c, v):
|
| 182 |
+
return dmp_zero(u)
|
| 183 |
+
else:
|
| 184 |
+
return [ dmp_mul(cf, c, v, K) for cf in f ] + dmp_zeros(i, v, K)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def dup_add_ground(f, c, K):
|
| 188 |
+
"""
|
| 189 |
+
Add an element of the ground domain to ``f``.
|
| 190 |
+
|
| 191 |
+
Examples
|
| 192 |
+
========
|
| 193 |
+
|
| 194 |
+
>>> from sympy.polys import ring, ZZ
|
| 195 |
+
>>> R, x = ring("x", ZZ)
|
| 196 |
+
|
| 197 |
+
>>> R.dup_add_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
|
| 198 |
+
x**3 + 2*x**2 + 3*x + 8
|
| 199 |
+
|
| 200 |
+
"""
|
| 201 |
+
return dup_add_term(f, c, 0, K)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def dmp_add_ground(f, c, u, K):
|
| 205 |
+
"""
|
| 206 |
+
Add an element of the ground domain to ``f``.
|
| 207 |
+
|
| 208 |
+
Examples
|
| 209 |
+
========
|
| 210 |
+
|
| 211 |
+
>>> from sympy.polys import ring, ZZ
|
| 212 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 213 |
+
|
| 214 |
+
>>> R.dmp_add_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
|
| 215 |
+
x**3 + 2*x**2 + 3*x + 8
|
| 216 |
+
|
| 217 |
+
"""
|
| 218 |
+
return dmp_add_term(f, dmp_ground(c, u - 1), 0, u, K)
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def dup_sub_ground(f, c, K):
|
| 222 |
+
"""
|
| 223 |
+
Subtract an element of the ground domain from ``f``.
|
| 224 |
+
|
| 225 |
+
Examples
|
| 226 |
+
========
|
| 227 |
+
|
| 228 |
+
>>> from sympy.polys import ring, ZZ
|
| 229 |
+
>>> R, x = ring("x", ZZ)
|
| 230 |
+
|
| 231 |
+
>>> R.dup_sub_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
|
| 232 |
+
x**3 + 2*x**2 + 3*x
|
| 233 |
+
|
| 234 |
+
"""
|
| 235 |
+
return dup_sub_term(f, c, 0, K)
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
def dmp_sub_ground(f, c, u, K):
|
| 239 |
+
"""
|
| 240 |
+
Subtract an element of the ground domain from ``f``.
|
| 241 |
+
|
| 242 |
+
Examples
|
| 243 |
+
========
|
| 244 |
+
|
| 245 |
+
>>> from sympy.polys import ring, ZZ
|
| 246 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 247 |
+
|
| 248 |
+
>>> R.dmp_sub_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
|
| 249 |
+
x**3 + 2*x**2 + 3*x
|
| 250 |
+
|
| 251 |
+
"""
|
| 252 |
+
return dmp_sub_term(f, dmp_ground(c, u - 1), 0, u, K)
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def dup_mul_ground(f, c, K):
|
| 256 |
+
"""
|
| 257 |
+
Multiply ``f`` by a constant value in ``K[x]``.
|
| 258 |
+
|
| 259 |
+
Examples
|
| 260 |
+
========
|
| 261 |
+
|
| 262 |
+
>>> from sympy.polys import ring, ZZ
|
| 263 |
+
>>> R, x = ring("x", ZZ)
|
| 264 |
+
|
| 265 |
+
>>> R.dup_mul_ground(x**2 + 2*x - 1, ZZ(3))
|
| 266 |
+
3*x**2 + 6*x - 3
|
| 267 |
+
|
| 268 |
+
"""
|
| 269 |
+
if not c or not f:
|
| 270 |
+
return []
|
| 271 |
+
else:
|
| 272 |
+
return [ cf * c for cf in f ]
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def dmp_mul_ground(f, c, u, K):
|
| 276 |
+
"""
|
| 277 |
+
Multiply ``f`` by a constant value in ``K[X]``.
|
| 278 |
+
|
| 279 |
+
Examples
|
| 280 |
+
========
|
| 281 |
+
|
| 282 |
+
>>> from sympy.polys import ring, ZZ
|
| 283 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 284 |
+
|
| 285 |
+
>>> R.dmp_mul_ground(2*x + 2*y, ZZ(3))
|
| 286 |
+
6*x + 6*y
|
| 287 |
+
|
| 288 |
+
"""
|
| 289 |
+
if not u:
|
| 290 |
+
return dup_mul_ground(f, c, K)
|
| 291 |
+
|
| 292 |
+
v = u - 1
|
| 293 |
+
|
| 294 |
+
return [ dmp_mul_ground(cf, c, v, K) for cf in f ]
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
def dup_quo_ground(f, c, K):
|
| 298 |
+
"""
|
| 299 |
+
Quotient by a constant in ``K[x]``.
|
| 300 |
+
|
| 301 |
+
Examples
|
| 302 |
+
========
|
| 303 |
+
|
| 304 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 305 |
+
|
| 306 |
+
>>> R, x = ring("x", ZZ)
|
| 307 |
+
>>> R.dup_quo_ground(3*x**2 + 2, ZZ(2))
|
| 308 |
+
x**2 + 1
|
| 309 |
+
|
| 310 |
+
>>> R, x = ring("x", QQ)
|
| 311 |
+
>>> R.dup_quo_ground(3*x**2 + 2, QQ(2))
|
| 312 |
+
3/2*x**2 + 1
|
| 313 |
+
|
| 314 |
+
"""
|
| 315 |
+
if not c:
|
| 316 |
+
raise ZeroDivisionError('polynomial division')
|
| 317 |
+
if not f:
|
| 318 |
+
return f
|
| 319 |
+
|
| 320 |
+
if K.is_Field:
|
| 321 |
+
return [ K.quo(cf, c) for cf in f ]
|
| 322 |
+
else:
|
| 323 |
+
return [ cf // c for cf in f ]
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
def dmp_quo_ground(f, c, u, K):
|
| 327 |
+
"""
|
| 328 |
+
Quotient by a constant in ``K[X]``.
|
| 329 |
+
|
| 330 |
+
Examples
|
| 331 |
+
========
|
| 332 |
+
|
| 333 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 334 |
+
|
| 335 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 336 |
+
>>> R.dmp_quo_ground(2*x**2*y + 3*x, ZZ(2))
|
| 337 |
+
x**2*y + x
|
| 338 |
+
|
| 339 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 340 |
+
>>> R.dmp_quo_ground(2*x**2*y + 3*x, QQ(2))
|
| 341 |
+
x**2*y + 3/2*x
|
| 342 |
+
|
| 343 |
+
"""
|
| 344 |
+
if not u:
|
| 345 |
+
return dup_quo_ground(f, c, K)
|
| 346 |
+
|
| 347 |
+
v = u - 1
|
| 348 |
+
|
| 349 |
+
return [ dmp_quo_ground(cf, c, v, K) for cf in f ]
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
def dup_exquo_ground(f, c, K):
|
| 353 |
+
"""
|
| 354 |
+
Exact quotient by a constant in ``K[x]``.
|
| 355 |
+
|
| 356 |
+
Examples
|
| 357 |
+
========
|
| 358 |
+
|
| 359 |
+
>>> from sympy.polys import ring, QQ
|
| 360 |
+
>>> R, x = ring("x", QQ)
|
| 361 |
+
|
| 362 |
+
>>> R.dup_exquo_ground(x**2 + 2, QQ(2))
|
| 363 |
+
1/2*x**2 + 1
|
| 364 |
+
|
| 365 |
+
"""
|
| 366 |
+
if not c:
|
| 367 |
+
raise ZeroDivisionError('polynomial division')
|
| 368 |
+
if not f:
|
| 369 |
+
return f
|
| 370 |
+
|
| 371 |
+
return [ K.exquo(cf, c) for cf in f ]
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
def dmp_exquo_ground(f, c, u, K):
|
| 375 |
+
"""
|
| 376 |
+
Exact quotient by a constant in ``K[X]``.
|
| 377 |
+
|
| 378 |
+
Examples
|
| 379 |
+
========
|
| 380 |
+
|
| 381 |
+
>>> from sympy.polys import ring, QQ
|
| 382 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 383 |
+
|
| 384 |
+
>>> R.dmp_exquo_ground(x**2*y + 2*x, QQ(2))
|
| 385 |
+
1/2*x**2*y + x
|
| 386 |
+
|
| 387 |
+
"""
|
| 388 |
+
if not u:
|
| 389 |
+
return dup_exquo_ground(f, c, K)
|
| 390 |
+
|
| 391 |
+
v = u - 1
|
| 392 |
+
|
| 393 |
+
return [ dmp_exquo_ground(cf, c, v, K) for cf in f ]
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
def dup_lshift(f, n, K):
|
| 397 |
+
"""
|
| 398 |
+
Efficiently multiply ``f`` by ``x**n`` in ``K[x]``.
|
| 399 |
+
|
| 400 |
+
Examples
|
| 401 |
+
========
|
| 402 |
+
|
| 403 |
+
>>> from sympy.polys import ring, ZZ
|
| 404 |
+
>>> R, x = ring("x", ZZ)
|
| 405 |
+
|
| 406 |
+
>>> R.dup_lshift(x**2 + 1, 2)
|
| 407 |
+
x**4 + x**2
|
| 408 |
+
|
| 409 |
+
"""
|
| 410 |
+
if not f:
|
| 411 |
+
return f
|
| 412 |
+
else:
|
| 413 |
+
return f + [K.zero]*n
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
def dup_rshift(f, n, K):
|
| 417 |
+
"""
|
| 418 |
+
Efficiently divide ``f`` by ``x**n`` in ``K[x]``.
|
| 419 |
+
|
| 420 |
+
Examples
|
| 421 |
+
========
|
| 422 |
+
|
| 423 |
+
>>> from sympy.polys import ring, ZZ
|
| 424 |
+
>>> R, x = ring("x", ZZ)
|
| 425 |
+
|
| 426 |
+
>>> R.dup_rshift(x**4 + x**2, 2)
|
| 427 |
+
x**2 + 1
|
| 428 |
+
>>> R.dup_rshift(x**4 + x**2 + 2, 2)
|
| 429 |
+
x**2 + 1
|
| 430 |
+
|
| 431 |
+
"""
|
| 432 |
+
return f[:-n]
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
def dup_abs(f, K):
|
| 436 |
+
"""
|
| 437 |
+
Make all coefficients positive in ``K[x]``.
|
| 438 |
+
|
| 439 |
+
Examples
|
| 440 |
+
========
|
| 441 |
+
|
| 442 |
+
>>> from sympy.polys import ring, ZZ
|
| 443 |
+
>>> R, x = ring("x", ZZ)
|
| 444 |
+
|
| 445 |
+
>>> R.dup_abs(x**2 - 1)
|
| 446 |
+
x**2 + 1
|
| 447 |
+
|
| 448 |
+
"""
|
| 449 |
+
return [ K.abs(coeff) for coeff in f ]
|
| 450 |
+
|
| 451 |
+
|
| 452 |
+
def dmp_abs(f, u, K):
|
| 453 |
+
"""
|
| 454 |
+
Make all coefficients positive in ``K[X]``.
|
| 455 |
+
|
| 456 |
+
Examples
|
| 457 |
+
========
|
| 458 |
+
|
| 459 |
+
>>> from sympy.polys import ring, ZZ
|
| 460 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 461 |
+
|
| 462 |
+
>>> R.dmp_abs(x**2*y - x)
|
| 463 |
+
x**2*y + x
|
| 464 |
+
|
| 465 |
+
"""
|
| 466 |
+
if not u:
|
| 467 |
+
return dup_abs(f, K)
|
| 468 |
+
|
| 469 |
+
v = u - 1
|
| 470 |
+
|
| 471 |
+
return [ dmp_abs(cf, v, K) for cf in f ]
|
| 472 |
+
|
| 473 |
+
|
| 474 |
+
def dup_neg(f, K):
|
| 475 |
+
"""
|
| 476 |
+
Negate a polynomial in ``K[x]``.
|
| 477 |
+
|
| 478 |
+
Examples
|
| 479 |
+
========
|
| 480 |
+
|
| 481 |
+
>>> from sympy.polys import ring, ZZ
|
| 482 |
+
>>> R, x = ring("x", ZZ)
|
| 483 |
+
|
| 484 |
+
>>> R.dup_neg(x**2 - 1)
|
| 485 |
+
-x**2 + 1
|
| 486 |
+
|
| 487 |
+
"""
|
| 488 |
+
return [ -coeff for coeff in f ]
|
| 489 |
+
|
| 490 |
+
|
| 491 |
+
def dmp_neg(f, u, K):
|
| 492 |
+
"""
|
| 493 |
+
Negate a polynomial in ``K[X]``.
|
| 494 |
+
|
| 495 |
+
Examples
|
| 496 |
+
========
|
| 497 |
+
|
| 498 |
+
>>> from sympy.polys import ring, ZZ
|
| 499 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 500 |
+
|
| 501 |
+
>>> R.dmp_neg(x**2*y - x)
|
| 502 |
+
-x**2*y + x
|
| 503 |
+
|
| 504 |
+
"""
|
| 505 |
+
if not u:
|
| 506 |
+
return dup_neg(f, K)
|
| 507 |
+
|
| 508 |
+
v = u - 1
|
| 509 |
+
|
| 510 |
+
return [ dmp_neg(cf, v, K) for cf in f ]
|
| 511 |
+
|
| 512 |
+
|
| 513 |
+
def dup_add(f, g, K):
|
| 514 |
+
"""
|
| 515 |
+
Add dense polynomials in ``K[x]``.
|
| 516 |
+
|
| 517 |
+
Examples
|
| 518 |
+
========
|
| 519 |
+
|
| 520 |
+
>>> from sympy.polys import ring, ZZ
|
| 521 |
+
>>> R, x = ring("x", ZZ)
|
| 522 |
+
|
| 523 |
+
>>> R.dup_add(x**2 - 1, x - 2)
|
| 524 |
+
x**2 + x - 3
|
| 525 |
+
|
| 526 |
+
"""
|
| 527 |
+
if not f:
|
| 528 |
+
return g
|
| 529 |
+
if not g:
|
| 530 |
+
return f
|
| 531 |
+
|
| 532 |
+
df = dup_degree(f)
|
| 533 |
+
dg = dup_degree(g)
|
| 534 |
+
|
| 535 |
+
if df == dg:
|
| 536 |
+
return dup_strip([ a + b for a, b in zip(f, g) ])
|
| 537 |
+
else:
|
| 538 |
+
k = abs(df - dg)
|
| 539 |
+
|
| 540 |
+
if df > dg:
|
| 541 |
+
h, f = f[:k], f[k:]
|
| 542 |
+
else:
|
| 543 |
+
h, g = g[:k], g[k:]
|
| 544 |
+
|
| 545 |
+
return h + [ a + b for a, b in zip(f, g) ]
|
| 546 |
+
|
| 547 |
+
|
| 548 |
+
def dmp_add(f, g, u, K):
|
| 549 |
+
"""
|
| 550 |
+
Add dense polynomials in ``K[X]``.
|
| 551 |
+
|
| 552 |
+
Examples
|
| 553 |
+
========
|
| 554 |
+
|
| 555 |
+
>>> from sympy.polys import ring, ZZ
|
| 556 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 557 |
+
|
| 558 |
+
>>> R.dmp_add(x**2 + y, x**2*y + x)
|
| 559 |
+
x**2*y + x**2 + x + y
|
| 560 |
+
|
| 561 |
+
"""
|
| 562 |
+
if not u:
|
| 563 |
+
return dup_add(f, g, K)
|
| 564 |
+
|
| 565 |
+
df = dmp_degree(f, u)
|
| 566 |
+
|
| 567 |
+
if df < 0:
|
| 568 |
+
return g
|
| 569 |
+
|
| 570 |
+
dg = dmp_degree(g, u)
|
| 571 |
+
|
| 572 |
+
if dg < 0:
|
| 573 |
+
return f
|
| 574 |
+
|
| 575 |
+
v = u - 1
|
| 576 |
+
|
| 577 |
+
if df == dg:
|
| 578 |
+
return dmp_strip([ dmp_add(a, b, v, K) for a, b in zip(f, g) ], u)
|
| 579 |
+
else:
|
| 580 |
+
k = abs(df - dg)
|
| 581 |
+
|
| 582 |
+
if df > dg:
|
| 583 |
+
h, f = f[:k], f[k:]
|
| 584 |
+
else:
|
| 585 |
+
h, g = g[:k], g[k:]
|
| 586 |
+
|
| 587 |
+
return h + [ dmp_add(a, b, v, K) for a, b in zip(f, g) ]
|
| 588 |
+
|
| 589 |
+
|
| 590 |
+
def dup_sub(f, g, K):
|
| 591 |
+
"""
|
| 592 |
+
Subtract dense polynomials in ``K[x]``.
|
| 593 |
+
|
| 594 |
+
Examples
|
| 595 |
+
========
|
| 596 |
+
|
| 597 |
+
>>> from sympy.polys import ring, ZZ
|
| 598 |
+
>>> R, x = ring("x", ZZ)
|
| 599 |
+
|
| 600 |
+
>>> R.dup_sub(x**2 - 1, x - 2)
|
| 601 |
+
x**2 - x + 1
|
| 602 |
+
|
| 603 |
+
"""
|
| 604 |
+
if not f:
|
| 605 |
+
return dup_neg(g, K)
|
| 606 |
+
if not g:
|
| 607 |
+
return f
|
| 608 |
+
|
| 609 |
+
df = dup_degree(f)
|
| 610 |
+
dg = dup_degree(g)
|
| 611 |
+
|
| 612 |
+
if df == dg:
|
| 613 |
+
return dup_strip([ a - b for a, b in zip(f, g) ])
|
| 614 |
+
else:
|
| 615 |
+
k = abs(df - dg)
|
| 616 |
+
|
| 617 |
+
if df > dg:
|
| 618 |
+
h, f = f[:k], f[k:]
|
| 619 |
+
else:
|
| 620 |
+
h, g = dup_neg(g[:k], K), g[k:]
|
| 621 |
+
|
| 622 |
+
return h + [ a - b for a, b in zip(f, g) ]
|
| 623 |
+
|
| 624 |
+
|
| 625 |
+
def dmp_sub(f, g, u, K):
|
| 626 |
+
"""
|
| 627 |
+
Subtract dense polynomials in ``K[X]``.
|
| 628 |
+
|
| 629 |
+
Examples
|
| 630 |
+
========
|
| 631 |
+
|
| 632 |
+
>>> from sympy.polys import ring, ZZ
|
| 633 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 634 |
+
|
| 635 |
+
>>> R.dmp_sub(x**2 + y, x**2*y + x)
|
| 636 |
+
-x**2*y + x**2 - x + y
|
| 637 |
+
|
| 638 |
+
"""
|
| 639 |
+
if not u:
|
| 640 |
+
return dup_sub(f, g, K)
|
| 641 |
+
|
| 642 |
+
df = dmp_degree(f, u)
|
| 643 |
+
|
| 644 |
+
if df < 0:
|
| 645 |
+
return dmp_neg(g, u, K)
|
| 646 |
+
|
| 647 |
+
dg = dmp_degree(g, u)
|
| 648 |
+
|
| 649 |
+
if dg < 0:
|
| 650 |
+
return f
|
| 651 |
+
|
| 652 |
+
v = u - 1
|
| 653 |
+
|
| 654 |
+
if df == dg:
|
| 655 |
+
return dmp_strip([ dmp_sub(a, b, v, K) for a, b in zip(f, g) ], u)
|
| 656 |
+
else:
|
| 657 |
+
k = abs(df - dg)
|
| 658 |
+
|
| 659 |
+
if df > dg:
|
| 660 |
+
h, f = f[:k], f[k:]
|
| 661 |
+
else:
|
| 662 |
+
h, g = dmp_neg(g[:k], u, K), g[k:]
|
| 663 |
+
|
| 664 |
+
return h + [ dmp_sub(a, b, v, K) for a, b in zip(f, g) ]
|
| 665 |
+
|
| 666 |
+
|
| 667 |
+
def dup_add_mul(f, g, h, K):
|
| 668 |
+
"""
|
| 669 |
+
Returns ``f + g*h`` where ``f, g, h`` are in ``K[x]``.
|
| 670 |
+
|
| 671 |
+
Examples
|
| 672 |
+
========
|
| 673 |
+
|
| 674 |
+
>>> from sympy.polys import ring, ZZ
|
| 675 |
+
>>> R, x = ring("x", ZZ)
|
| 676 |
+
|
| 677 |
+
>>> R.dup_add_mul(x**2 - 1, x - 2, x + 2)
|
| 678 |
+
2*x**2 - 5
|
| 679 |
+
|
| 680 |
+
"""
|
| 681 |
+
return dup_add(f, dup_mul(g, h, K), K)
|
| 682 |
+
|
| 683 |
+
|
| 684 |
+
def dmp_add_mul(f, g, h, u, K):
|
| 685 |
+
"""
|
| 686 |
+
Returns ``f + g*h`` where ``f, g, h`` are in ``K[X]``.
|
| 687 |
+
|
| 688 |
+
Examples
|
| 689 |
+
========
|
| 690 |
+
|
| 691 |
+
>>> from sympy.polys import ring, ZZ
|
| 692 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 693 |
+
|
| 694 |
+
>>> R.dmp_add_mul(x**2 + y, x, x + 2)
|
| 695 |
+
2*x**2 + 2*x + y
|
| 696 |
+
|
| 697 |
+
"""
|
| 698 |
+
return dmp_add(f, dmp_mul(g, h, u, K), u, K)
|
| 699 |
+
|
| 700 |
+
|
| 701 |
+
def dup_sub_mul(f, g, h, K):
|
| 702 |
+
"""
|
| 703 |
+
Returns ``f - g*h`` where ``f, g, h`` are in ``K[x]``.
|
| 704 |
+
|
| 705 |
+
Examples
|
| 706 |
+
========
|
| 707 |
+
|
| 708 |
+
>>> from sympy.polys import ring, ZZ
|
| 709 |
+
>>> R, x = ring("x", ZZ)
|
| 710 |
+
|
| 711 |
+
>>> R.dup_sub_mul(x**2 - 1, x - 2, x + 2)
|
| 712 |
+
3
|
| 713 |
+
|
| 714 |
+
"""
|
| 715 |
+
return dup_sub(f, dup_mul(g, h, K), K)
|
| 716 |
+
|
| 717 |
+
|
| 718 |
+
def dmp_sub_mul(f, g, h, u, K):
|
| 719 |
+
"""
|
| 720 |
+
Returns ``f - g*h`` where ``f, g, h`` are in ``K[X]``.
|
| 721 |
+
|
| 722 |
+
Examples
|
| 723 |
+
========
|
| 724 |
+
|
| 725 |
+
>>> from sympy.polys import ring, ZZ
|
| 726 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 727 |
+
|
| 728 |
+
>>> R.dmp_sub_mul(x**2 + y, x, x + 2)
|
| 729 |
+
-2*x + y
|
| 730 |
+
|
| 731 |
+
"""
|
| 732 |
+
return dmp_sub(f, dmp_mul(g, h, u, K), u, K)
|
| 733 |
+
|
| 734 |
+
|
| 735 |
+
def dup_mul(f, g, K):
|
| 736 |
+
"""
|
| 737 |
+
Multiply dense polynomials in ``K[x]``.
|
| 738 |
+
|
| 739 |
+
Examples
|
| 740 |
+
========
|
| 741 |
+
|
| 742 |
+
>>> from sympy.polys import ring, ZZ
|
| 743 |
+
>>> R, x = ring("x", ZZ)
|
| 744 |
+
|
| 745 |
+
>>> R.dup_mul(x - 2, x + 2)
|
| 746 |
+
x**2 - 4
|
| 747 |
+
|
| 748 |
+
"""
|
| 749 |
+
if f == g:
|
| 750 |
+
return dup_sqr(f, K)
|
| 751 |
+
|
| 752 |
+
if not (f and g):
|
| 753 |
+
return []
|
| 754 |
+
|
| 755 |
+
df = dup_degree(f)
|
| 756 |
+
dg = dup_degree(g)
|
| 757 |
+
|
| 758 |
+
n = max(df, dg) + 1
|
| 759 |
+
|
| 760 |
+
if n < 100:
|
| 761 |
+
h = []
|
| 762 |
+
|
| 763 |
+
for i in range(0, df + dg + 1):
|
| 764 |
+
coeff = K.zero
|
| 765 |
+
|
| 766 |
+
for j in range(max(0, i - dg), min(df, i) + 1):
|
| 767 |
+
coeff += f[j]*g[i - j]
|
| 768 |
+
|
| 769 |
+
h.append(coeff)
|
| 770 |
+
|
| 771 |
+
return dup_strip(h)
|
| 772 |
+
else:
|
| 773 |
+
# Use Karatsuba's algorithm (divide and conquer), see e.g.:
|
| 774 |
+
# Joris van der Hoeven, Relax But Don't Be Too Lazy,
|
| 775 |
+
# J. Symbolic Computation, 11 (2002), section 3.1.1.
|
| 776 |
+
n2 = n//2
|
| 777 |
+
|
| 778 |
+
fl, gl = dup_slice(f, 0, n2, K), dup_slice(g, 0, n2, K)
|
| 779 |
+
|
| 780 |
+
fh = dup_rshift(dup_slice(f, n2, n, K), n2, K)
|
| 781 |
+
gh = dup_rshift(dup_slice(g, n2, n, K), n2, K)
|
| 782 |
+
|
| 783 |
+
lo, hi = dup_mul(fl, gl, K), dup_mul(fh, gh, K)
|
| 784 |
+
|
| 785 |
+
mid = dup_mul(dup_add(fl, fh, K), dup_add(gl, gh, K), K)
|
| 786 |
+
mid = dup_sub(mid, dup_add(lo, hi, K), K)
|
| 787 |
+
|
| 788 |
+
return dup_add(dup_add(lo, dup_lshift(mid, n2, K), K),
|
| 789 |
+
dup_lshift(hi, 2*n2, K), K)
|
| 790 |
+
|
| 791 |
+
|
| 792 |
+
def dmp_mul(f, g, u, K):
|
| 793 |
+
"""
|
| 794 |
+
Multiply dense polynomials in ``K[X]``.
|
| 795 |
+
|
| 796 |
+
Examples
|
| 797 |
+
========
|
| 798 |
+
|
| 799 |
+
>>> from sympy.polys import ring, ZZ
|
| 800 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 801 |
+
|
| 802 |
+
>>> R.dmp_mul(x*y + 1, x)
|
| 803 |
+
x**2*y + x
|
| 804 |
+
|
| 805 |
+
"""
|
| 806 |
+
if not u:
|
| 807 |
+
return dup_mul(f, g, K)
|
| 808 |
+
|
| 809 |
+
if f == g:
|
| 810 |
+
return dmp_sqr(f, u, K)
|
| 811 |
+
|
| 812 |
+
df = dmp_degree(f, u)
|
| 813 |
+
|
| 814 |
+
if df < 0:
|
| 815 |
+
return f
|
| 816 |
+
|
| 817 |
+
dg = dmp_degree(g, u)
|
| 818 |
+
|
| 819 |
+
if dg < 0:
|
| 820 |
+
return g
|
| 821 |
+
|
| 822 |
+
h, v = [], u - 1
|
| 823 |
+
|
| 824 |
+
for i in range(0, df + dg + 1):
|
| 825 |
+
coeff = dmp_zero(v)
|
| 826 |
+
|
| 827 |
+
for j in range(max(0, i - dg), min(df, i) + 1):
|
| 828 |
+
coeff = dmp_add(coeff, dmp_mul(f[j], g[i - j], v, K), v, K)
|
| 829 |
+
|
| 830 |
+
h.append(coeff)
|
| 831 |
+
|
| 832 |
+
return dmp_strip(h, u)
|
| 833 |
+
|
| 834 |
+
|
| 835 |
+
def dup_sqr(f, K):
|
| 836 |
+
"""
|
| 837 |
+
Square dense polynomials in ``K[x]``.
|
| 838 |
+
|
| 839 |
+
Examples
|
| 840 |
+
========
|
| 841 |
+
|
| 842 |
+
>>> from sympy.polys import ring, ZZ
|
| 843 |
+
>>> R, x = ring("x", ZZ)
|
| 844 |
+
|
| 845 |
+
>>> R.dup_sqr(x**2 + 1)
|
| 846 |
+
x**4 + 2*x**2 + 1
|
| 847 |
+
|
| 848 |
+
"""
|
| 849 |
+
df, h = len(f) - 1, []
|
| 850 |
+
|
| 851 |
+
for i in range(0, 2*df + 1):
|
| 852 |
+
c = K.zero
|
| 853 |
+
|
| 854 |
+
jmin = max(0, i - df)
|
| 855 |
+
jmax = min(i, df)
|
| 856 |
+
|
| 857 |
+
n = jmax - jmin + 1
|
| 858 |
+
|
| 859 |
+
jmax = jmin + n // 2 - 1
|
| 860 |
+
|
| 861 |
+
for j in range(jmin, jmax + 1):
|
| 862 |
+
c += f[j]*f[i - j]
|
| 863 |
+
|
| 864 |
+
c += c
|
| 865 |
+
|
| 866 |
+
if n & 1:
|
| 867 |
+
elem = f[jmax + 1]
|
| 868 |
+
c += elem**2
|
| 869 |
+
|
| 870 |
+
h.append(c)
|
| 871 |
+
|
| 872 |
+
return dup_strip(h)
|
| 873 |
+
|
| 874 |
+
|
| 875 |
+
def dmp_sqr(f, u, K):
|
| 876 |
+
"""
|
| 877 |
+
Square dense polynomials in ``K[X]``.
|
| 878 |
+
|
| 879 |
+
Examples
|
| 880 |
+
========
|
| 881 |
+
|
| 882 |
+
>>> from sympy.polys import ring, ZZ
|
| 883 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 884 |
+
|
| 885 |
+
>>> R.dmp_sqr(x**2 + x*y + y**2)
|
| 886 |
+
x**4 + 2*x**3*y + 3*x**2*y**2 + 2*x*y**3 + y**4
|
| 887 |
+
|
| 888 |
+
"""
|
| 889 |
+
if not u:
|
| 890 |
+
return dup_sqr(f, K)
|
| 891 |
+
|
| 892 |
+
df = dmp_degree(f, u)
|
| 893 |
+
|
| 894 |
+
if df < 0:
|
| 895 |
+
return f
|
| 896 |
+
|
| 897 |
+
h, v = [], u - 1
|
| 898 |
+
|
| 899 |
+
for i in range(0, 2*df + 1):
|
| 900 |
+
c = dmp_zero(v)
|
| 901 |
+
|
| 902 |
+
jmin = max(0, i - df)
|
| 903 |
+
jmax = min(i, df)
|
| 904 |
+
|
| 905 |
+
n = jmax - jmin + 1
|
| 906 |
+
|
| 907 |
+
jmax = jmin + n // 2 - 1
|
| 908 |
+
|
| 909 |
+
for j in range(jmin, jmax + 1):
|
| 910 |
+
c = dmp_add(c, dmp_mul(f[j], f[i - j], v, K), v, K)
|
| 911 |
+
|
| 912 |
+
c = dmp_mul_ground(c, K(2), v, K)
|
| 913 |
+
|
| 914 |
+
if n & 1:
|
| 915 |
+
elem = dmp_sqr(f[jmax + 1], v, K)
|
| 916 |
+
c = dmp_add(c, elem, v, K)
|
| 917 |
+
|
| 918 |
+
h.append(c)
|
| 919 |
+
|
| 920 |
+
return dmp_strip(h, u)
|
| 921 |
+
|
| 922 |
+
|
| 923 |
+
def dup_pow(f, n, K):
|
| 924 |
+
"""
|
| 925 |
+
Raise ``f`` to the ``n``-th power in ``K[x]``.
|
| 926 |
+
|
| 927 |
+
Examples
|
| 928 |
+
========
|
| 929 |
+
|
| 930 |
+
>>> from sympy.polys import ring, ZZ
|
| 931 |
+
>>> R, x = ring("x", ZZ)
|
| 932 |
+
|
| 933 |
+
>>> R.dup_pow(x - 2, 3)
|
| 934 |
+
x**3 - 6*x**2 + 12*x - 8
|
| 935 |
+
|
| 936 |
+
"""
|
| 937 |
+
if not n:
|
| 938 |
+
return [K.one]
|
| 939 |
+
if n < 0:
|
| 940 |
+
raise ValueError("Cannot raise polynomial to a negative power")
|
| 941 |
+
if n == 1 or not f or f == [K.one]:
|
| 942 |
+
return f
|
| 943 |
+
|
| 944 |
+
g = [K.one]
|
| 945 |
+
|
| 946 |
+
while True:
|
| 947 |
+
n, m = n//2, n
|
| 948 |
+
|
| 949 |
+
if m % 2:
|
| 950 |
+
g = dup_mul(g, f, K)
|
| 951 |
+
|
| 952 |
+
if not n:
|
| 953 |
+
break
|
| 954 |
+
|
| 955 |
+
f = dup_sqr(f, K)
|
| 956 |
+
|
| 957 |
+
return g
|
| 958 |
+
|
| 959 |
+
|
| 960 |
+
def dmp_pow(f, n, u, K):
|
| 961 |
+
"""
|
| 962 |
+
Raise ``f`` to the ``n``-th power in ``K[X]``.
|
| 963 |
+
|
| 964 |
+
Examples
|
| 965 |
+
========
|
| 966 |
+
|
| 967 |
+
>>> from sympy.polys import ring, ZZ
|
| 968 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 969 |
+
|
| 970 |
+
>>> R.dmp_pow(x*y + 1, 3)
|
| 971 |
+
x**3*y**3 + 3*x**2*y**2 + 3*x*y + 1
|
| 972 |
+
|
| 973 |
+
"""
|
| 974 |
+
if not u:
|
| 975 |
+
return dup_pow(f, n, K)
|
| 976 |
+
|
| 977 |
+
if not n:
|
| 978 |
+
return dmp_one(u, K)
|
| 979 |
+
if n < 0:
|
| 980 |
+
raise ValueError("Cannot raise polynomial to a negative power")
|
| 981 |
+
if n == 1 or dmp_zero_p(f, u) or dmp_one_p(f, u, K):
|
| 982 |
+
return f
|
| 983 |
+
|
| 984 |
+
g = dmp_one(u, K)
|
| 985 |
+
|
| 986 |
+
while True:
|
| 987 |
+
n, m = n//2, n
|
| 988 |
+
|
| 989 |
+
if m & 1:
|
| 990 |
+
g = dmp_mul(g, f, u, K)
|
| 991 |
+
|
| 992 |
+
if not n:
|
| 993 |
+
break
|
| 994 |
+
|
| 995 |
+
f = dmp_sqr(f, u, K)
|
| 996 |
+
|
| 997 |
+
return g
|
| 998 |
+
|
| 999 |
+
|
| 1000 |
+
def dup_pdiv(f, g, K):
|
| 1001 |
+
"""
|
| 1002 |
+
Polynomial pseudo-division in ``K[x]``.
|
| 1003 |
+
|
| 1004 |
+
Examples
|
| 1005 |
+
========
|
| 1006 |
+
|
| 1007 |
+
>>> from sympy.polys import ring, ZZ
|
| 1008 |
+
>>> R, x = ring("x", ZZ)
|
| 1009 |
+
|
| 1010 |
+
>>> R.dup_pdiv(x**2 + 1, 2*x - 4)
|
| 1011 |
+
(2*x + 4, 20)
|
| 1012 |
+
|
| 1013 |
+
"""
|
| 1014 |
+
df = dup_degree(f)
|
| 1015 |
+
dg = dup_degree(g)
|
| 1016 |
+
|
| 1017 |
+
q, r, dr = [], f, df
|
| 1018 |
+
|
| 1019 |
+
if not g:
|
| 1020 |
+
raise ZeroDivisionError("polynomial division")
|
| 1021 |
+
elif df < dg:
|
| 1022 |
+
return q, r
|
| 1023 |
+
|
| 1024 |
+
N = df - dg + 1
|
| 1025 |
+
lc_g = dup_LC(g, K)
|
| 1026 |
+
|
| 1027 |
+
while True:
|
| 1028 |
+
lc_r = dup_LC(r, K)
|
| 1029 |
+
j, N = dr - dg, N - 1
|
| 1030 |
+
|
| 1031 |
+
Q = dup_mul_ground(q, lc_g, K)
|
| 1032 |
+
q = dup_add_term(Q, lc_r, j, K)
|
| 1033 |
+
|
| 1034 |
+
R = dup_mul_ground(r, lc_g, K)
|
| 1035 |
+
G = dup_mul_term(g, lc_r, j, K)
|
| 1036 |
+
r = dup_sub(R, G, K)
|
| 1037 |
+
|
| 1038 |
+
_dr, dr = dr, dup_degree(r)
|
| 1039 |
+
|
| 1040 |
+
if dr < dg:
|
| 1041 |
+
break
|
| 1042 |
+
elif not (dr < _dr):
|
| 1043 |
+
raise PolynomialDivisionFailed(f, g, K)
|
| 1044 |
+
|
| 1045 |
+
c = lc_g**N
|
| 1046 |
+
|
| 1047 |
+
q = dup_mul_ground(q, c, K)
|
| 1048 |
+
r = dup_mul_ground(r, c, K)
|
| 1049 |
+
|
| 1050 |
+
return q, r
|
| 1051 |
+
|
| 1052 |
+
|
| 1053 |
+
def dup_prem(f, g, K):
|
| 1054 |
+
"""
|
| 1055 |
+
Polynomial pseudo-remainder in ``K[x]``.
|
| 1056 |
+
|
| 1057 |
+
Examples
|
| 1058 |
+
========
|
| 1059 |
+
|
| 1060 |
+
>>> from sympy.polys import ring, ZZ
|
| 1061 |
+
>>> R, x = ring("x", ZZ)
|
| 1062 |
+
|
| 1063 |
+
>>> R.dup_prem(x**2 + 1, 2*x - 4)
|
| 1064 |
+
20
|
| 1065 |
+
|
| 1066 |
+
"""
|
| 1067 |
+
df = dup_degree(f)
|
| 1068 |
+
dg = dup_degree(g)
|
| 1069 |
+
|
| 1070 |
+
r, dr = f, df
|
| 1071 |
+
|
| 1072 |
+
if not g:
|
| 1073 |
+
raise ZeroDivisionError("polynomial division")
|
| 1074 |
+
elif df < dg:
|
| 1075 |
+
return r
|
| 1076 |
+
|
| 1077 |
+
N = df - dg + 1
|
| 1078 |
+
lc_g = dup_LC(g, K)
|
| 1079 |
+
|
| 1080 |
+
while True:
|
| 1081 |
+
lc_r = dup_LC(r, K)
|
| 1082 |
+
j, N = dr - dg, N - 1
|
| 1083 |
+
|
| 1084 |
+
R = dup_mul_ground(r, lc_g, K)
|
| 1085 |
+
G = dup_mul_term(g, lc_r, j, K)
|
| 1086 |
+
r = dup_sub(R, G, K)
|
| 1087 |
+
|
| 1088 |
+
_dr, dr = dr, dup_degree(r)
|
| 1089 |
+
|
| 1090 |
+
if dr < dg:
|
| 1091 |
+
break
|
| 1092 |
+
elif not (dr < _dr):
|
| 1093 |
+
raise PolynomialDivisionFailed(f, g, K)
|
| 1094 |
+
|
| 1095 |
+
return dup_mul_ground(r, lc_g**N, K)
|
| 1096 |
+
|
| 1097 |
+
|
| 1098 |
+
def dup_pquo(f, g, K):
|
| 1099 |
+
"""
|
| 1100 |
+
Polynomial exact pseudo-quotient in ``K[X]``.
|
| 1101 |
+
|
| 1102 |
+
Examples
|
| 1103 |
+
========
|
| 1104 |
+
|
| 1105 |
+
>>> from sympy.polys import ring, ZZ
|
| 1106 |
+
>>> R, x = ring("x", ZZ)
|
| 1107 |
+
|
| 1108 |
+
>>> R.dup_pquo(x**2 - 1, 2*x - 2)
|
| 1109 |
+
2*x + 2
|
| 1110 |
+
|
| 1111 |
+
>>> R.dup_pquo(x**2 + 1, 2*x - 4)
|
| 1112 |
+
2*x + 4
|
| 1113 |
+
|
| 1114 |
+
"""
|
| 1115 |
+
return dup_pdiv(f, g, K)[0]
|
| 1116 |
+
|
| 1117 |
+
|
| 1118 |
+
def dup_pexquo(f, g, K):
|
| 1119 |
+
"""
|
| 1120 |
+
Polynomial pseudo-quotient in ``K[x]``.
|
| 1121 |
+
|
| 1122 |
+
Examples
|
| 1123 |
+
========
|
| 1124 |
+
|
| 1125 |
+
>>> from sympy.polys import ring, ZZ
|
| 1126 |
+
>>> R, x = ring("x", ZZ)
|
| 1127 |
+
|
| 1128 |
+
>>> R.dup_pexquo(x**2 - 1, 2*x - 2)
|
| 1129 |
+
2*x + 2
|
| 1130 |
+
|
| 1131 |
+
>>> R.dup_pexquo(x**2 + 1, 2*x - 4)
|
| 1132 |
+
Traceback (most recent call last):
|
| 1133 |
+
...
|
| 1134 |
+
ExactQuotientFailed: [2, -4] does not divide [1, 0, 1]
|
| 1135 |
+
|
| 1136 |
+
"""
|
| 1137 |
+
q, r = dup_pdiv(f, g, K)
|
| 1138 |
+
|
| 1139 |
+
if not r:
|
| 1140 |
+
return q
|
| 1141 |
+
else:
|
| 1142 |
+
raise ExactQuotientFailed(f, g)
|
| 1143 |
+
|
| 1144 |
+
|
| 1145 |
+
def dmp_pdiv(f, g, u, K):
|
| 1146 |
+
"""
|
| 1147 |
+
Polynomial pseudo-division in ``K[X]``.
|
| 1148 |
+
|
| 1149 |
+
Examples
|
| 1150 |
+
========
|
| 1151 |
+
|
| 1152 |
+
>>> from sympy.polys import ring, ZZ
|
| 1153 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1154 |
+
|
| 1155 |
+
>>> R.dmp_pdiv(x**2 + x*y, 2*x + 2)
|
| 1156 |
+
(2*x + 2*y - 2, -4*y + 4)
|
| 1157 |
+
|
| 1158 |
+
"""
|
| 1159 |
+
if not u:
|
| 1160 |
+
return dup_pdiv(f, g, K)
|
| 1161 |
+
|
| 1162 |
+
df = dmp_degree(f, u)
|
| 1163 |
+
dg = dmp_degree(g, u)
|
| 1164 |
+
|
| 1165 |
+
if dg < 0:
|
| 1166 |
+
raise ZeroDivisionError("polynomial division")
|
| 1167 |
+
|
| 1168 |
+
q, r, dr = dmp_zero(u), f, df
|
| 1169 |
+
|
| 1170 |
+
if df < dg:
|
| 1171 |
+
return q, r
|
| 1172 |
+
|
| 1173 |
+
N = df - dg + 1
|
| 1174 |
+
lc_g = dmp_LC(g, K)
|
| 1175 |
+
|
| 1176 |
+
while True:
|
| 1177 |
+
lc_r = dmp_LC(r, K)
|
| 1178 |
+
j, N = dr - dg, N - 1
|
| 1179 |
+
|
| 1180 |
+
Q = dmp_mul_term(q, lc_g, 0, u, K)
|
| 1181 |
+
q = dmp_add_term(Q, lc_r, j, u, K)
|
| 1182 |
+
|
| 1183 |
+
R = dmp_mul_term(r, lc_g, 0, u, K)
|
| 1184 |
+
G = dmp_mul_term(g, lc_r, j, u, K)
|
| 1185 |
+
r = dmp_sub(R, G, u, K)
|
| 1186 |
+
|
| 1187 |
+
_dr, dr = dr, dmp_degree(r, u)
|
| 1188 |
+
|
| 1189 |
+
if dr < dg:
|
| 1190 |
+
break
|
| 1191 |
+
elif not (dr < _dr):
|
| 1192 |
+
raise PolynomialDivisionFailed(f, g, K)
|
| 1193 |
+
|
| 1194 |
+
c = dmp_pow(lc_g, N, u - 1, K)
|
| 1195 |
+
|
| 1196 |
+
q = dmp_mul_term(q, c, 0, u, K)
|
| 1197 |
+
r = dmp_mul_term(r, c, 0, u, K)
|
| 1198 |
+
|
| 1199 |
+
return q, r
|
| 1200 |
+
|
| 1201 |
+
|
| 1202 |
+
def dmp_prem(f, g, u, K):
|
| 1203 |
+
"""
|
| 1204 |
+
Polynomial pseudo-remainder in ``K[X]``.
|
| 1205 |
+
|
| 1206 |
+
Examples
|
| 1207 |
+
========
|
| 1208 |
+
|
| 1209 |
+
>>> from sympy.polys import ring, ZZ
|
| 1210 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1211 |
+
|
| 1212 |
+
>>> R.dmp_prem(x**2 + x*y, 2*x + 2)
|
| 1213 |
+
-4*y + 4
|
| 1214 |
+
|
| 1215 |
+
"""
|
| 1216 |
+
if not u:
|
| 1217 |
+
return dup_prem(f, g, K)
|
| 1218 |
+
|
| 1219 |
+
df = dmp_degree(f, u)
|
| 1220 |
+
dg = dmp_degree(g, u)
|
| 1221 |
+
|
| 1222 |
+
if dg < 0:
|
| 1223 |
+
raise ZeroDivisionError("polynomial division")
|
| 1224 |
+
|
| 1225 |
+
r, dr = f, df
|
| 1226 |
+
|
| 1227 |
+
if df < dg:
|
| 1228 |
+
return r
|
| 1229 |
+
|
| 1230 |
+
N = df - dg + 1
|
| 1231 |
+
lc_g = dmp_LC(g, K)
|
| 1232 |
+
|
| 1233 |
+
while True:
|
| 1234 |
+
lc_r = dmp_LC(r, K)
|
| 1235 |
+
j, N = dr - dg, N - 1
|
| 1236 |
+
|
| 1237 |
+
R = dmp_mul_term(r, lc_g, 0, u, K)
|
| 1238 |
+
G = dmp_mul_term(g, lc_r, j, u, K)
|
| 1239 |
+
r = dmp_sub(R, G, u, K)
|
| 1240 |
+
|
| 1241 |
+
_dr, dr = dr, dmp_degree(r, u)
|
| 1242 |
+
|
| 1243 |
+
if dr < dg:
|
| 1244 |
+
break
|
| 1245 |
+
elif not (dr < _dr):
|
| 1246 |
+
raise PolynomialDivisionFailed(f, g, K)
|
| 1247 |
+
|
| 1248 |
+
c = dmp_pow(lc_g, N, u - 1, K)
|
| 1249 |
+
|
| 1250 |
+
return dmp_mul_term(r, c, 0, u, K)
|
| 1251 |
+
|
| 1252 |
+
|
| 1253 |
+
def dmp_pquo(f, g, u, K):
|
| 1254 |
+
"""
|
| 1255 |
+
Polynomial exact pseudo-quotient in ``K[X]``.
|
| 1256 |
+
|
| 1257 |
+
Examples
|
| 1258 |
+
========
|
| 1259 |
+
|
| 1260 |
+
>>> from sympy.polys import ring, ZZ
|
| 1261 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1262 |
+
|
| 1263 |
+
>>> f = x**2 + x*y
|
| 1264 |
+
>>> g = 2*x + 2*y
|
| 1265 |
+
>>> h = 2*x + 2
|
| 1266 |
+
|
| 1267 |
+
>>> R.dmp_pquo(f, g)
|
| 1268 |
+
2*x
|
| 1269 |
+
|
| 1270 |
+
>>> R.dmp_pquo(f, h)
|
| 1271 |
+
2*x + 2*y - 2
|
| 1272 |
+
|
| 1273 |
+
"""
|
| 1274 |
+
return dmp_pdiv(f, g, u, K)[0]
|
| 1275 |
+
|
| 1276 |
+
|
| 1277 |
+
def dmp_pexquo(f, g, u, K):
|
| 1278 |
+
"""
|
| 1279 |
+
Polynomial pseudo-quotient in ``K[X]``.
|
| 1280 |
+
|
| 1281 |
+
Examples
|
| 1282 |
+
========
|
| 1283 |
+
|
| 1284 |
+
>>> from sympy.polys import ring, ZZ
|
| 1285 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1286 |
+
|
| 1287 |
+
>>> f = x**2 + x*y
|
| 1288 |
+
>>> g = 2*x + 2*y
|
| 1289 |
+
>>> h = 2*x + 2
|
| 1290 |
+
|
| 1291 |
+
>>> R.dmp_pexquo(f, g)
|
| 1292 |
+
2*x
|
| 1293 |
+
|
| 1294 |
+
>>> R.dmp_pexquo(f, h)
|
| 1295 |
+
Traceback (most recent call last):
|
| 1296 |
+
...
|
| 1297 |
+
ExactQuotientFailed: [[2], [2]] does not divide [[1], [1, 0], []]
|
| 1298 |
+
|
| 1299 |
+
"""
|
| 1300 |
+
q, r = dmp_pdiv(f, g, u, K)
|
| 1301 |
+
|
| 1302 |
+
if dmp_zero_p(r, u):
|
| 1303 |
+
return q
|
| 1304 |
+
else:
|
| 1305 |
+
raise ExactQuotientFailed(f, g)
|
| 1306 |
+
|
| 1307 |
+
|
| 1308 |
+
def dup_rr_div(f, g, K):
|
| 1309 |
+
"""
|
| 1310 |
+
Univariate division with remainder over a ring.
|
| 1311 |
+
|
| 1312 |
+
Examples
|
| 1313 |
+
========
|
| 1314 |
+
|
| 1315 |
+
>>> from sympy.polys import ring, ZZ
|
| 1316 |
+
>>> R, x = ring("x", ZZ)
|
| 1317 |
+
|
| 1318 |
+
>>> R.dup_rr_div(x**2 + 1, 2*x - 4)
|
| 1319 |
+
(0, x**2 + 1)
|
| 1320 |
+
|
| 1321 |
+
"""
|
| 1322 |
+
df = dup_degree(f)
|
| 1323 |
+
dg = dup_degree(g)
|
| 1324 |
+
|
| 1325 |
+
q, r, dr = [], f, df
|
| 1326 |
+
|
| 1327 |
+
if not g:
|
| 1328 |
+
raise ZeroDivisionError("polynomial division")
|
| 1329 |
+
elif df < dg:
|
| 1330 |
+
return q, r
|
| 1331 |
+
|
| 1332 |
+
lc_g = dup_LC(g, K)
|
| 1333 |
+
|
| 1334 |
+
while True:
|
| 1335 |
+
lc_r = dup_LC(r, K)
|
| 1336 |
+
|
| 1337 |
+
if lc_r % lc_g:
|
| 1338 |
+
break
|
| 1339 |
+
|
| 1340 |
+
c = K.exquo(lc_r, lc_g)
|
| 1341 |
+
j = dr - dg
|
| 1342 |
+
|
| 1343 |
+
q = dup_add_term(q, c, j, K)
|
| 1344 |
+
h = dup_mul_term(g, c, j, K)
|
| 1345 |
+
r = dup_sub(r, h, K)
|
| 1346 |
+
|
| 1347 |
+
_dr, dr = dr, dup_degree(r)
|
| 1348 |
+
|
| 1349 |
+
if dr < dg:
|
| 1350 |
+
break
|
| 1351 |
+
elif not (dr < _dr):
|
| 1352 |
+
raise PolynomialDivisionFailed(f, g, K)
|
| 1353 |
+
|
| 1354 |
+
return q, r
|
| 1355 |
+
|
| 1356 |
+
|
| 1357 |
+
def dmp_rr_div(f, g, u, K):
|
| 1358 |
+
"""
|
| 1359 |
+
Multivariate division with remainder over a ring.
|
| 1360 |
+
|
| 1361 |
+
Examples
|
| 1362 |
+
========
|
| 1363 |
+
|
| 1364 |
+
>>> from sympy.polys import ring, ZZ
|
| 1365 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1366 |
+
|
| 1367 |
+
>>> R.dmp_rr_div(x**2 + x*y, 2*x + 2)
|
| 1368 |
+
(0, x**2 + x*y)
|
| 1369 |
+
|
| 1370 |
+
"""
|
| 1371 |
+
if not u:
|
| 1372 |
+
return dup_rr_div(f, g, K)
|
| 1373 |
+
|
| 1374 |
+
df = dmp_degree(f, u)
|
| 1375 |
+
dg = dmp_degree(g, u)
|
| 1376 |
+
|
| 1377 |
+
if dg < 0:
|
| 1378 |
+
raise ZeroDivisionError("polynomial division")
|
| 1379 |
+
|
| 1380 |
+
q, r, dr = dmp_zero(u), f, df
|
| 1381 |
+
|
| 1382 |
+
if df < dg:
|
| 1383 |
+
return q, r
|
| 1384 |
+
|
| 1385 |
+
lc_g, v = dmp_LC(g, K), u - 1
|
| 1386 |
+
|
| 1387 |
+
while True:
|
| 1388 |
+
lc_r = dmp_LC(r, K)
|
| 1389 |
+
c, R = dmp_rr_div(lc_r, lc_g, v, K)
|
| 1390 |
+
|
| 1391 |
+
if not dmp_zero_p(R, v):
|
| 1392 |
+
break
|
| 1393 |
+
|
| 1394 |
+
j = dr - dg
|
| 1395 |
+
|
| 1396 |
+
q = dmp_add_term(q, c, j, u, K)
|
| 1397 |
+
h = dmp_mul_term(g, c, j, u, K)
|
| 1398 |
+
r = dmp_sub(r, h, u, K)
|
| 1399 |
+
|
| 1400 |
+
_dr, dr = dr, dmp_degree(r, u)
|
| 1401 |
+
|
| 1402 |
+
if dr < dg:
|
| 1403 |
+
break
|
| 1404 |
+
elif not (dr < _dr):
|
| 1405 |
+
raise PolynomialDivisionFailed(f, g, K)
|
| 1406 |
+
|
| 1407 |
+
return q, r
|
| 1408 |
+
|
| 1409 |
+
|
| 1410 |
+
def dup_ff_div(f, g, K):
|
| 1411 |
+
"""
|
| 1412 |
+
Polynomial division with remainder over a field.
|
| 1413 |
+
|
| 1414 |
+
Examples
|
| 1415 |
+
========
|
| 1416 |
+
|
| 1417 |
+
>>> from sympy.polys import ring, QQ
|
| 1418 |
+
>>> R, x = ring("x", QQ)
|
| 1419 |
+
|
| 1420 |
+
>>> R.dup_ff_div(x**2 + 1, 2*x - 4)
|
| 1421 |
+
(1/2*x + 1, 5)
|
| 1422 |
+
|
| 1423 |
+
"""
|
| 1424 |
+
df = dup_degree(f)
|
| 1425 |
+
dg = dup_degree(g)
|
| 1426 |
+
|
| 1427 |
+
q, r, dr = [], f, df
|
| 1428 |
+
|
| 1429 |
+
if not g:
|
| 1430 |
+
raise ZeroDivisionError("polynomial division")
|
| 1431 |
+
elif df < dg:
|
| 1432 |
+
return q, r
|
| 1433 |
+
|
| 1434 |
+
lc_g = dup_LC(g, K)
|
| 1435 |
+
|
| 1436 |
+
while True:
|
| 1437 |
+
lc_r = dup_LC(r, K)
|
| 1438 |
+
|
| 1439 |
+
c = K.exquo(lc_r, lc_g)
|
| 1440 |
+
j = dr - dg
|
| 1441 |
+
|
| 1442 |
+
q = dup_add_term(q, c, j, K)
|
| 1443 |
+
h = dup_mul_term(g, c, j, K)
|
| 1444 |
+
r = dup_sub(r, h, K)
|
| 1445 |
+
|
| 1446 |
+
_dr, dr = dr, dup_degree(r)
|
| 1447 |
+
|
| 1448 |
+
if dr < dg:
|
| 1449 |
+
break
|
| 1450 |
+
elif dr == _dr and not K.is_Exact:
|
| 1451 |
+
# remove leading term created by rounding error
|
| 1452 |
+
r = dup_strip(r[1:])
|
| 1453 |
+
dr = dup_degree(r)
|
| 1454 |
+
if dr < dg:
|
| 1455 |
+
break
|
| 1456 |
+
elif not (dr < _dr):
|
| 1457 |
+
raise PolynomialDivisionFailed(f, g, K)
|
| 1458 |
+
|
| 1459 |
+
return q, r
|
| 1460 |
+
|
| 1461 |
+
|
| 1462 |
+
def dmp_ff_div(f, g, u, K):
|
| 1463 |
+
"""
|
| 1464 |
+
Polynomial division with remainder over a field.
|
| 1465 |
+
|
| 1466 |
+
Examples
|
| 1467 |
+
========
|
| 1468 |
+
|
| 1469 |
+
>>> from sympy.polys import ring, QQ
|
| 1470 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 1471 |
+
|
| 1472 |
+
>>> R.dmp_ff_div(x**2 + x*y, 2*x + 2)
|
| 1473 |
+
(1/2*x + 1/2*y - 1/2, -y + 1)
|
| 1474 |
+
|
| 1475 |
+
"""
|
| 1476 |
+
if not u:
|
| 1477 |
+
return dup_ff_div(f, g, K)
|
| 1478 |
+
|
| 1479 |
+
df = dmp_degree(f, u)
|
| 1480 |
+
dg = dmp_degree(g, u)
|
| 1481 |
+
|
| 1482 |
+
if dg < 0:
|
| 1483 |
+
raise ZeroDivisionError("polynomial division")
|
| 1484 |
+
|
| 1485 |
+
q, r, dr = dmp_zero(u), f, df
|
| 1486 |
+
|
| 1487 |
+
if df < dg:
|
| 1488 |
+
return q, r
|
| 1489 |
+
|
| 1490 |
+
lc_g, v = dmp_LC(g, K), u - 1
|
| 1491 |
+
|
| 1492 |
+
while True:
|
| 1493 |
+
lc_r = dmp_LC(r, K)
|
| 1494 |
+
c, R = dmp_ff_div(lc_r, lc_g, v, K)
|
| 1495 |
+
|
| 1496 |
+
if not dmp_zero_p(R, v):
|
| 1497 |
+
break
|
| 1498 |
+
|
| 1499 |
+
j = dr - dg
|
| 1500 |
+
|
| 1501 |
+
q = dmp_add_term(q, c, j, u, K)
|
| 1502 |
+
h = dmp_mul_term(g, c, j, u, K)
|
| 1503 |
+
r = dmp_sub(r, h, u, K)
|
| 1504 |
+
|
| 1505 |
+
_dr, dr = dr, dmp_degree(r, u)
|
| 1506 |
+
|
| 1507 |
+
if dr < dg:
|
| 1508 |
+
break
|
| 1509 |
+
elif not (dr < _dr):
|
| 1510 |
+
raise PolynomialDivisionFailed(f, g, K)
|
| 1511 |
+
|
| 1512 |
+
return q, r
|
| 1513 |
+
|
| 1514 |
+
|
| 1515 |
+
def dup_div(f, g, K):
|
| 1516 |
+
"""
|
| 1517 |
+
Polynomial division with remainder in ``K[x]``.
|
| 1518 |
+
|
| 1519 |
+
Examples
|
| 1520 |
+
========
|
| 1521 |
+
|
| 1522 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 1523 |
+
|
| 1524 |
+
>>> R, x = ring("x", ZZ)
|
| 1525 |
+
>>> R.dup_div(x**2 + 1, 2*x - 4)
|
| 1526 |
+
(0, x**2 + 1)
|
| 1527 |
+
|
| 1528 |
+
>>> R, x = ring("x", QQ)
|
| 1529 |
+
>>> R.dup_div(x**2 + 1, 2*x - 4)
|
| 1530 |
+
(1/2*x + 1, 5)
|
| 1531 |
+
|
| 1532 |
+
"""
|
| 1533 |
+
if K.is_Field:
|
| 1534 |
+
return dup_ff_div(f, g, K)
|
| 1535 |
+
else:
|
| 1536 |
+
return dup_rr_div(f, g, K)
|
| 1537 |
+
|
| 1538 |
+
|
| 1539 |
+
def dup_rem(f, g, K):
|
| 1540 |
+
"""
|
| 1541 |
+
Returns polynomial remainder in ``K[x]``.
|
| 1542 |
+
|
| 1543 |
+
Examples
|
| 1544 |
+
========
|
| 1545 |
+
|
| 1546 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 1547 |
+
|
| 1548 |
+
>>> R, x = ring("x", ZZ)
|
| 1549 |
+
>>> R.dup_rem(x**2 + 1, 2*x - 4)
|
| 1550 |
+
x**2 + 1
|
| 1551 |
+
|
| 1552 |
+
>>> R, x = ring("x", QQ)
|
| 1553 |
+
>>> R.dup_rem(x**2 + 1, 2*x - 4)
|
| 1554 |
+
5
|
| 1555 |
+
|
| 1556 |
+
"""
|
| 1557 |
+
return dup_div(f, g, K)[1]
|
| 1558 |
+
|
| 1559 |
+
|
| 1560 |
+
def dup_quo(f, g, K):
|
| 1561 |
+
"""
|
| 1562 |
+
Returns exact polynomial quotient in ``K[x]``.
|
| 1563 |
+
|
| 1564 |
+
Examples
|
| 1565 |
+
========
|
| 1566 |
+
|
| 1567 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 1568 |
+
|
| 1569 |
+
>>> R, x = ring("x", ZZ)
|
| 1570 |
+
>>> R.dup_quo(x**2 + 1, 2*x - 4)
|
| 1571 |
+
0
|
| 1572 |
+
|
| 1573 |
+
>>> R, x = ring("x", QQ)
|
| 1574 |
+
>>> R.dup_quo(x**2 + 1, 2*x - 4)
|
| 1575 |
+
1/2*x + 1
|
| 1576 |
+
|
| 1577 |
+
"""
|
| 1578 |
+
return dup_div(f, g, K)[0]
|
| 1579 |
+
|
| 1580 |
+
|
| 1581 |
+
def dup_exquo(f, g, K):
|
| 1582 |
+
"""
|
| 1583 |
+
Returns polynomial quotient in ``K[x]``.
|
| 1584 |
+
|
| 1585 |
+
Examples
|
| 1586 |
+
========
|
| 1587 |
+
|
| 1588 |
+
>>> from sympy.polys import ring, ZZ
|
| 1589 |
+
>>> R, x = ring("x", ZZ)
|
| 1590 |
+
|
| 1591 |
+
>>> R.dup_exquo(x**2 - 1, x - 1)
|
| 1592 |
+
x + 1
|
| 1593 |
+
|
| 1594 |
+
>>> R.dup_exquo(x**2 + 1, 2*x - 4)
|
| 1595 |
+
Traceback (most recent call last):
|
| 1596 |
+
...
|
| 1597 |
+
ExactQuotientFailed: [2, -4] does not divide [1, 0, 1]
|
| 1598 |
+
|
| 1599 |
+
"""
|
| 1600 |
+
q, r = dup_div(f, g, K)
|
| 1601 |
+
|
| 1602 |
+
if not r:
|
| 1603 |
+
return q
|
| 1604 |
+
else:
|
| 1605 |
+
raise ExactQuotientFailed(f, g)
|
| 1606 |
+
|
| 1607 |
+
|
| 1608 |
+
def dmp_div(f, g, u, K):
|
| 1609 |
+
"""
|
| 1610 |
+
Polynomial division with remainder in ``K[X]``.
|
| 1611 |
+
|
| 1612 |
+
Examples
|
| 1613 |
+
========
|
| 1614 |
+
|
| 1615 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 1616 |
+
|
| 1617 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1618 |
+
>>> R.dmp_div(x**2 + x*y, 2*x + 2)
|
| 1619 |
+
(0, x**2 + x*y)
|
| 1620 |
+
|
| 1621 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 1622 |
+
>>> R.dmp_div(x**2 + x*y, 2*x + 2)
|
| 1623 |
+
(1/2*x + 1/2*y - 1/2, -y + 1)
|
| 1624 |
+
|
| 1625 |
+
"""
|
| 1626 |
+
if K.is_Field:
|
| 1627 |
+
return dmp_ff_div(f, g, u, K)
|
| 1628 |
+
else:
|
| 1629 |
+
return dmp_rr_div(f, g, u, K)
|
| 1630 |
+
|
| 1631 |
+
|
| 1632 |
+
def dmp_rem(f, g, u, K):
|
| 1633 |
+
"""
|
| 1634 |
+
Returns polynomial remainder in ``K[X]``.
|
| 1635 |
+
|
| 1636 |
+
Examples
|
| 1637 |
+
========
|
| 1638 |
+
|
| 1639 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 1640 |
+
|
| 1641 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1642 |
+
>>> R.dmp_rem(x**2 + x*y, 2*x + 2)
|
| 1643 |
+
x**2 + x*y
|
| 1644 |
+
|
| 1645 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 1646 |
+
>>> R.dmp_rem(x**2 + x*y, 2*x + 2)
|
| 1647 |
+
-y + 1
|
| 1648 |
+
|
| 1649 |
+
"""
|
| 1650 |
+
return dmp_div(f, g, u, K)[1]
|
| 1651 |
+
|
| 1652 |
+
|
| 1653 |
+
def dmp_quo(f, g, u, K):
|
| 1654 |
+
"""
|
| 1655 |
+
Returns exact polynomial quotient in ``K[X]``.
|
| 1656 |
+
|
| 1657 |
+
Examples
|
| 1658 |
+
========
|
| 1659 |
+
|
| 1660 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 1661 |
+
|
| 1662 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1663 |
+
>>> R.dmp_quo(x**2 + x*y, 2*x + 2)
|
| 1664 |
+
0
|
| 1665 |
+
|
| 1666 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 1667 |
+
>>> R.dmp_quo(x**2 + x*y, 2*x + 2)
|
| 1668 |
+
1/2*x + 1/2*y - 1/2
|
| 1669 |
+
|
| 1670 |
+
"""
|
| 1671 |
+
return dmp_div(f, g, u, K)[0]
|
| 1672 |
+
|
| 1673 |
+
|
| 1674 |
+
def dmp_exquo(f, g, u, K):
|
| 1675 |
+
"""
|
| 1676 |
+
Returns polynomial quotient in ``K[X]``.
|
| 1677 |
+
|
| 1678 |
+
Examples
|
| 1679 |
+
========
|
| 1680 |
+
|
| 1681 |
+
>>> from sympy.polys import ring, ZZ
|
| 1682 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1683 |
+
|
| 1684 |
+
>>> f = x**2 + x*y
|
| 1685 |
+
>>> g = x + y
|
| 1686 |
+
>>> h = 2*x + 2
|
| 1687 |
+
|
| 1688 |
+
>>> R.dmp_exquo(f, g)
|
| 1689 |
+
x
|
| 1690 |
+
|
| 1691 |
+
>>> R.dmp_exquo(f, h)
|
| 1692 |
+
Traceback (most recent call last):
|
| 1693 |
+
...
|
| 1694 |
+
ExactQuotientFailed: [[2], [2]] does not divide [[1], [1, 0], []]
|
| 1695 |
+
|
| 1696 |
+
"""
|
| 1697 |
+
q, r = dmp_div(f, g, u, K)
|
| 1698 |
+
|
| 1699 |
+
if dmp_zero_p(r, u):
|
| 1700 |
+
return q
|
| 1701 |
+
else:
|
| 1702 |
+
raise ExactQuotientFailed(f, g)
|
| 1703 |
+
|
| 1704 |
+
|
| 1705 |
+
def dup_max_norm(f, K):
|
| 1706 |
+
"""
|
| 1707 |
+
Returns maximum norm of a polynomial in ``K[x]``.
|
| 1708 |
+
|
| 1709 |
+
Examples
|
| 1710 |
+
========
|
| 1711 |
+
|
| 1712 |
+
>>> from sympy.polys import ring, ZZ
|
| 1713 |
+
>>> R, x = ring("x", ZZ)
|
| 1714 |
+
|
| 1715 |
+
>>> R.dup_max_norm(-x**2 + 2*x - 3)
|
| 1716 |
+
3
|
| 1717 |
+
|
| 1718 |
+
"""
|
| 1719 |
+
if not f:
|
| 1720 |
+
return K.zero
|
| 1721 |
+
else:
|
| 1722 |
+
return max(dup_abs(f, K))
|
| 1723 |
+
|
| 1724 |
+
|
| 1725 |
+
def dmp_max_norm(f, u, K):
|
| 1726 |
+
"""
|
| 1727 |
+
Returns maximum norm of a polynomial in ``K[X]``.
|
| 1728 |
+
|
| 1729 |
+
Examples
|
| 1730 |
+
========
|
| 1731 |
+
|
| 1732 |
+
>>> from sympy.polys import ring, ZZ
|
| 1733 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1734 |
+
|
| 1735 |
+
>>> R.dmp_max_norm(2*x*y - x - 3)
|
| 1736 |
+
3
|
| 1737 |
+
|
| 1738 |
+
"""
|
| 1739 |
+
if not u:
|
| 1740 |
+
return dup_max_norm(f, K)
|
| 1741 |
+
|
| 1742 |
+
v = u - 1
|
| 1743 |
+
|
| 1744 |
+
return max(dmp_max_norm(c, v, K) for c in f)
|
| 1745 |
+
|
| 1746 |
+
|
| 1747 |
+
def dup_l1_norm(f, K):
|
| 1748 |
+
"""
|
| 1749 |
+
Returns l1 norm of a polynomial in ``K[x]``.
|
| 1750 |
+
|
| 1751 |
+
Examples
|
| 1752 |
+
========
|
| 1753 |
+
|
| 1754 |
+
>>> from sympy.polys import ring, ZZ
|
| 1755 |
+
>>> R, x = ring("x", ZZ)
|
| 1756 |
+
|
| 1757 |
+
>>> R.dup_l1_norm(2*x**3 - 3*x**2 + 1)
|
| 1758 |
+
6
|
| 1759 |
+
|
| 1760 |
+
"""
|
| 1761 |
+
if not f:
|
| 1762 |
+
return K.zero
|
| 1763 |
+
else:
|
| 1764 |
+
return sum(dup_abs(f, K))
|
| 1765 |
+
|
| 1766 |
+
|
| 1767 |
+
def dmp_l1_norm(f, u, K):
|
| 1768 |
+
"""
|
| 1769 |
+
Returns l1 norm of a polynomial in ``K[X]``.
|
| 1770 |
+
|
| 1771 |
+
Examples
|
| 1772 |
+
========
|
| 1773 |
+
|
| 1774 |
+
>>> from sympy.polys import ring, ZZ
|
| 1775 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1776 |
+
|
| 1777 |
+
>>> R.dmp_l1_norm(2*x*y - x - 3)
|
| 1778 |
+
6
|
| 1779 |
+
|
| 1780 |
+
"""
|
| 1781 |
+
if not u:
|
| 1782 |
+
return dup_l1_norm(f, K)
|
| 1783 |
+
|
| 1784 |
+
v = u - 1
|
| 1785 |
+
|
| 1786 |
+
return sum(dmp_l1_norm(c, v, K) for c in f)
|
| 1787 |
+
|
| 1788 |
+
|
| 1789 |
+
def dup_l2_norm_squared(f, K):
|
| 1790 |
+
"""
|
| 1791 |
+
Returns squared l2 norm of a polynomial in ``K[x]``.
|
| 1792 |
+
|
| 1793 |
+
Examples
|
| 1794 |
+
========
|
| 1795 |
+
|
| 1796 |
+
>>> from sympy.polys import ring, ZZ
|
| 1797 |
+
>>> R, x = ring("x", ZZ)
|
| 1798 |
+
|
| 1799 |
+
>>> R.dup_l2_norm_squared(2*x**3 - 3*x**2 + 1)
|
| 1800 |
+
14
|
| 1801 |
+
|
| 1802 |
+
"""
|
| 1803 |
+
return sum([coeff**2 for coeff in f], K.zero)
|
| 1804 |
+
|
| 1805 |
+
|
| 1806 |
+
def dmp_l2_norm_squared(f, u, K):
|
| 1807 |
+
"""
|
| 1808 |
+
Returns squared l2 norm of a polynomial in ``K[X]``.
|
| 1809 |
+
|
| 1810 |
+
Examples
|
| 1811 |
+
========
|
| 1812 |
+
|
| 1813 |
+
>>> from sympy.polys import ring, ZZ
|
| 1814 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1815 |
+
|
| 1816 |
+
>>> R.dmp_l2_norm_squared(2*x*y - x - 3)
|
| 1817 |
+
14
|
| 1818 |
+
|
| 1819 |
+
"""
|
| 1820 |
+
if not u:
|
| 1821 |
+
return dup_l2_norm_squared(f, K)
|
| 1822 |
+
|
| 1823 |
+
v = u - 1
|
| 1824 |
+
|
| 1825 |
+
return sum(dmp_l2_norm_squared(c, v, K) for c in f)
|
| 1826 |
+
|
| 1827 |
+
|
| 1828 |
+
def dup_expand(polys, K):
|
| 1829 |
+
"""
|
| 1830 |
+
Multiply together several polynomials in ``K[x]``.
|
| 1831 |
+
|
| 1832 |
+
Examples
|
| 1833 |
+
========
|
| 1834 |
+
|
| 1835 |
+
>>> from sympy.polys import ring, ZZ
|
| 1836 |
+
>>> R, x = ring("x", ZZ)
|
| 1837 |
+
|
| 1838 |
+
>>> R.dup_expand([x**2 - 1, x, 2])
|
| 1839 |
+
2*x**3 - 2*x
|
| 1840 |
+
|
| 1841 |
+
"""
|
| 1842 |
+
if not polys:
|
| 1843 |
+
return [K.one]
|
| 1844 |
+
|
| 1845 |
+
f = polys[0]
|
| 1846 |
+
|
| 1847 |
+
for g in polys[1:]:
|
| 1848 |
+
f = dup_mul(f, g, K)
|
| 1849 |
+
|
| 1850 |
+
return f
|
| 1851 |
+
|
| 1852 |
+
|
| 1853 |
+
def dmp_expand(polys, u, K):
|
| 1854 |
+
"""
|
| 1855 |
+
Multiply together several polynomials in ``K[X]``.
|
| 1856 |
+
|
| 1857 |
+
Examples
|
| 1858 |
+
========
|
| 1859 |
+
|
| 1860 |
+
>>> from sympy.polys import ring, ZZ
|
| 1861 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1862 |
+
|
| 1863 |
+
>>> R.dmp_expand([x**2 + y**2, x + 1])
|
| 1864 |
+
x**3 + x**2 + x*y**2 + y**2
|
| 1865 |
+
|
| 1866 |
+
"""
|
| 1867 |
+
if not polys:
|
| 1868 |
+
return dmp_one(u, K)
|
| 1869 |
+
|
| 1870 |
+
f = polys[0]
|
| 1871 |
+
|
| 1872 |
+
for g in polys[1:]:
|
| 1873 |
+
f = dmp_mul(f, g, u, K)
|
| 1874 |
+
|
| 1875 |
+
return f
|
vllm/lib/python3.10/site-packages/sympy/polys/densetools.py
ADDED
|
@@ -0,0 +1,1363 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
| 7 |
+
dup_add, dmp_add,
|
| 8 |
+
dup_sub, dmp_sub,
|
| 9 |
+
dup_mul, dmp_mul,
|
| 10 |
+
dup_sqr,
|
| 11 |
+
dup_div,
|
| 12 |
+
dup_rem, dmp_rem,
|
| 13 |
+
dmp_expand,
|
| 14 |
+
dup_mul_ground, dmp_mul_ground,
|
| 15 |
+
dup_quo_ground, dmp_quo_ground,
|
| 16 |
+
dup_exquo_ground, dmp_exquo_ground,
|
| 17 |
+
)
|
| 18 |
+
from sympy.polys.densebasic import (
|
| 19 |
+
dup_strip, dmp_strip,
|
| 20 |
+
dup_convert, dmp_convert,
|
| 21 |
+
dup_degree, dmp_degree,
|
| 22 |
+
dmp_to_dict,
|
| 23 |
+
dmp_from_dict,
|
| 24 |
+
dup_LC, dmp_LC, dmp_ground_LC,
|
| 25 |
+
dup_TC, dmp_TC,
|
| 26 |
+
dmp_zero, dmp_ground,
|
| 27 |
+
dmp_zero_p,
|
| 28 |
+
dup_to_raw_dict, dup_from_raw_dict,
|
| 29 |
+
dmp_zeros
|
| 30 |
+
)
|
| 31 |
+
from sympy.polys.polyerrors import (
|
| 32 |
+
MultivariatePolynomialError,
|
| 33 |
+
DomainError
|
| 34 |
+
)
|
| 35 |
+
from sympy.utilities import variations
|
| 36 |
+
|
| 37 |
+
from math import ceil as _ceil, log2 as _log2
|
| 38 |
+
|
| 39 |
+
def dup_integrate(f, m, K):
|
| 40 |
+
"""
|
| 41 |
+
Computes the indefinite integral of ``f`` in ``K[x]``.
|
| 42 |
+
|
| 43 |
+
Examples
|
| 44 |
+
========
|
| 45 |
+
|
| 46 |
+
>>> from sympy.polys import ring, QQ
|
| 47 |
+
>>> R, x = ring("x", QQ)
|
| 48 |
+
|
| 49 |
+
>>> R.dup_integrate(x**2 + 2*x, 1)
|
| 50 |
+
1/3*x**3 + x**2
|
| 51 |
+
>>> R.dup_integrate(x**2 + 2*x, 2)
|
| 52 |
+
1/12*x**4 + 1/3*x**3
|
| 53 |
+
|
| 54 |
+
"""
|
| 55 |
+
if m <= 0 or not f:
|
| 56 |
+
return f
|
| 57 |
+
|
| 58 |
+
g = [K.zero]*m
|
| 59 |
+
|
| 60 |
+
for i, c in enumerate(reversed(f)):
|
| 61 |
+
n = i + 1
|
| 62 |
+
|
| 63 |
+
for j in range(1, m):
|
| 64 |
+
n *= i + j + 1
|
| 65 |
+
|
| 66 |
+
g.insert(0, K.exquo(c, K(n)))
|
| 67 |
+
|
| 68 |
+
return g
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def dmp_integrate(f, m, u, K):
|
| 72 |
+
"""
|
| 73 |
+
Computes the indefinite integral of ``f`` in ``x_0`` in ``K[X]``.
|
| 74 |
+
|
| 75 |
+
Examples
|
| 76 |
+
========
|
| 77 |
+
|
| 78 |
+
>>> from sympy.polys import ring, QQ
|
| 79 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 80 |
+
|
| 81 |
+
>>> R.dmp_integrate(x + 2*y, 1)
|
| 82 |
+
1/2*x**2 + 2*x*y
|
| 83 |
+
>>> R.dmp_integrate(x + 2*y, 2)
|
| 84 |
+
1/6*x**3 + x**2*y
|
| 85 |
+
|
| 86 |
+
"""
|
| 87 |
+
if not u:
|
| 88 |
+
return dup_integrate(f, m, K)
|
| 89 |
+
|
| 90 |
+
if m <= 0 or dmp_zero_p(f, u):
|
| 91 |
+
return f
|
| 92 |
+
|
| 93 |
+
g, v = dmp_zeros(m, u - 1, K), u - 1
|
| 94 |
+
|
| 95 |
+
for i, c in enumerate(reversed(f)):
|
| 96 |
+
n = i + 1
|
| 97 |
+
|
| 98 |
+
for j in range(1, m):
|
| 99 |
+
n *= i + j + 1
|
| 100 |
+
|
| 101 |
+
g.insert(0, dmp_quo_ground(c, K(n), v, K))
|
| 102 |
+
|
| 103 |
+
return g
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def _rec_integrate_in(g, m, v, i, j, K):
|
| 107 |
+
"""Recursive helper for :func:`dmp_integrate_in`."""
|
| 108 |
+
if i == j:
|
| 109 |
+
return dmp_integrate(g, m, v, K)
|
| 110 |
+
|
| 111 |
+
w, i = v - 1, i + 1
|
| 112 |
+
|
| 113 |
+
return dmp_strip([ _rec_integrate_in(c, m, w, i, j, K) for c in g ], v)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def dmp_integrate_in(f, m, j, u, K):
|
| 117 |
+
"""
|
| 118 |
+
Computes the indefinite integral of ``f`` in ``x_j`` in ``K[X]``.
|
| 119 |
+
|
| 120 |
+
Examples
|
| 121 |
+
========
|
| 122 |
+
|
| 123 |
+
>>> from sympy.polys import ring, QQ
|
| 124 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 125 |
+
|
| 126 |
+
>>> R.dmp_integrate_in(x + 2*y, 1, 0)
|
| 127 |
+
1/2*x**2 + 2*x*y
|
| 128 |
+
>>> R.dmp_integrate_in(x + 2*y, 1, 1)
|
| 129 |
+
x*y + y**2
|
| 130 |
+
|
| 131 |
+
"""
|
| 132 |
+
if j < 0 or j > u:
|
| 133 |
+
raise IndexError("0 <= j <= u expected, got u = %d, j = %d" % (u, j))
|
| 134 |
+
|
| 135 |
+
return _rec_integrate_in(f, m, u, 0, j, K)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def dup_diff(f, m, K):
|
| 139 |
+
"""
|
| 140 |
+
``m``-th order derivative of a polynomial in ``K[x]``.
|
| 141 |
+
|
| 142 |
+
Examples
|
| 143 |
+
========
|
| 144 |
+
|
| 145 |
+
>>> from sympy.polys import ring, ZZ
|
| 146 |
+
>>> R, x = ring("x", ZZ)
|
| 147 |
+
|
| 148 |
+
>>> R.dup_diff(x**3 + 2*x**2 + 3*x + 4, 1)
|
| 149 |
+
3*x**2 + 4*x + 3
|
| 150 |
+
>>> R.dup_diff(x**3 + 2*x**2 + 3*x + 4, 2)
|
| 151 |
+
6*x + 4
|
| 152 |
+
|
| 153 |
+
"""
|
| 154 |
+
if m <= 0:
|
| 155 |
+
return f
|
| 156 |
+
|
| 157 |
+
n = dup_degree(f)
|
| 158 |
+
|
| 159 |
+
if n < m:
|
| 160 |
+
return []
|
| 161 |
+
|
| 162 |
+
deriv = []
|
| 163 |
+
|
| 164 |
+
if m == 1:
|
| 165 |
+
for coeff in f[:-m]:
|
| 166 |
+
deriv.append(K(n)*coeff)
|
| 167 |
+
n -= 1
|
| 168 |
+
else:
|
| 169 |
+
for coeff in f[:-m]:
|
| 170 |
+
k = n
|
| 171 |
+
|
| 172 |
+
for i in range(n - 1, n - m, -1):
|
| 173 |
+
k *= i
|
| 174 |
+
|
| 175 |
+
deriv.append(K(k)*coeff)
|
| 176 |
+
n -= 1
|
| 177 |
+
|
| 178 |
+
return dup_strip(deriv)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def dmp_diff(f, m, u, K):
|
| 182 |
+
"""
|
| 183 |
+
``m``-th order derivative in ``x_0`` of a polynomial in ``K[X]``.
|
| 184 |
+
|
| 185 |
+
Examples
|
| 186 |
+
========
|
| 187 |
+
|
| 188 |
+
>>> from sympy.polys import ring, ZZ
|
| 189 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 190 |
+
|
| 191 |
+
>>> f = x*y**2 + 2*x*y + 3*x + 2*y**2 + 3*y + 1
|
| 192 |
+
|
| 193 |
+
>>> R.dmp_diff(f, 1)
|
| 194 |
+
y**2 + 2*y + 3
|
| 195 |
+
>>> R.dmp_diff(f, 2)
|
| 196 |
+
0
|
| 197 |
+
|
| 198 |
+
"""
|
| 199 |
+
if not u:
|
| 200 |
+
return dup_diff(f, m, K)
|
| 201 |
+
if m <= 0:
|
| 202 |
+
return f
|
| 203 |
+
|
| 204 |
+
n = dmp_degree(f, u)
|
| 205 |
+
|
| 206 |
+
if n < m:
|
| 207 |
+
return dmp_zero(u)
|
| 208 |
+
|
| 209 |
+
deriv, v = [], u - 1
|
| 210 |
+
|
| 211 |
+
if m == 1:
|
| 212 |
+
for coeff in f[:-m]:
|
| 213 |
+
deriv.append(dmp_mul_ground(coeff, K(n), v, K))
|
| 214 |
+
n -= 1
|
| 215 |
+
else:
|
| 216 |
+
for coeff in f[:-m]:
|
| 217 |
+
k = n
|
| 218 |
+
|
| 219 |
+
for i in range(n - 1, n - m, -1):
|
| 220 |
+
k *= i
|
| 221 |
+
|
| 222 |
+
deriv.append(dmp_mul_ground(coeff, K(k), v, K))
|
| 223 |
+
n -= 1
|
| 224 |
+
|
| 225 |
+
return dmp_strip(deriv, u)
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def _rec_diff_in(g, m, v, i, j, K):
|
| 229 |
+
"""Recursive helper for :func:`dmp_diff_in`."""
|
| 230 |
+
if i == j:
|
| 231 |
+
return dmp_diff(g, m, v, K)
|
| 232 |
+
|
| 233 |
+
w, i = v - 1, i + 1
|
| 234 |
+
|
| 235 |
+
return dmp_strip([ _rec_diff_in(c, m, w, i, j, K) for c in g ], v)
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
def dmp_diff_in(f, m, j, u, K):
|
| 239 |
+
"""
|
| 240 |
+
``m``-th order derivative in ``x_j`` of a polynomial in ``K[X]``.
|
| 241 |
+
|
| 242 |
+
Examples
|
| 243 |
+
========
|
| 244 |
+
|
| 245 |
+
>>> from sympy.polys import ring, ZZ
|
| 246 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 247 |
+
|
| 248 |
+
>>> f = x*y**2 + 2*x*y + 3*x + 2*y**2 + 3*y + 1
|
| 249 |
+
|
| 250 |
+
>>> R.dmp_diff_in(f, 1, 0)
|
| 251 |
+
y**2 + 2*y + 3
|
| 252 |
+
>>> R.dmp_diff_in(f, 1, 1)
|
| 253 |
+
2*x*y + 2*x + 4*y + 3
|
| 254 |
+
|
| 255 |
+
"""
|
| 256 |
+
if j < 0 or j > u:
|
| 257 |
+
raise IndexError("0 <= j <= %s expected, got %s" % (u, j))
|
| 258 |
+
|
| 259 |
+
return _rec_diff_in(f, m, u, 0, j, K)
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def dup_eval(f, a, K):
|
| 263 |
+
"""
|
| 264 |
+
Evaluate a polynomial at ``x = a`` in ``K[x]`` using Horner scheme.
|
| 265 |
+
|
| 266 |
+
Examples
|
| 267 |
+
========
|
| 268 |
+
|
| 269 |
+
>>> from sympy.polys import ring, ZZ
|
| 270 |
+
>>> R, x = ring("x", ZZ)
|
| 271 |
+
|
| 272 |
+
>>> R.dup_eval(x**2 + 2*x + 3, 2)
|
| 273 |
+
11
|
| 274 |
+
|
| 275 |
+
"""
|
| 276 |
+
if not a:
|
| 277 |
+
return K.convert(dup_TC(f, K))
|
| 278 |
+
|
| 279 |
+
result = K.zero
|
| 280 |
+
|
| 281 |
+
for c in f:
|
| 282 |
+
result *= a
|
| 283 |
+
result += c
|
| 284 |
+
|
| 285 |
+
return result
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
def dmp_eval(f, a, u, K):
|
| 289 |
+
"""
|
| 290 |
+
Evaluate a polynomial at ``x_0 = a`` in ``K[X]`` using the Horner scheme.
|
| 291 |
+
|
| 292 |
+
Examples
|
| 293 |
+
========
|
| 294 |
+
|
| 295 |
+
>>> from sympy.polys import ring, ZZ
|
| 296 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 297 |
+
|
| 298 |
+
>>> R.dmp_eval(2*x*y + 3*x + y + 2, 2)
|
| 299 |
+
5*y + 8
|
| 300 |
+
|
| 301 |
+
"""
|
| 302 |
+
if not u:
|
| 303 |
+
return dup_eval(f, a, K)
|
| 304 |
+
|
| 305 |
+
if not a:
|
| 306 |
+
return dmp_TC(f, K)
|
| 307 |
+
|
| 308 |
+
result, v = dmp_LC(f, K), u - 1
|
| 309 |
+
|
| 310 |
+
for coeff in f[1:]:
|
| 311 |
+
result = dmp_mul_ground(result, a, v, K)
|
| 312 |
+
result = dmp_add(result, coeff, v, K)
|
| 313 |
+
|
| 314 |
+
return result
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
def _rec_eval_in(g, a, v, i, j, K):
|
| 318 |
+
"""Recursive helper for :func:`dmp_eval_in`."""
|
| 319 |
+
if i == j:
|
| 320 |
+
return dmp_eval(g, a, v, K)
|
| 321 |
+
|
| 322 |
+
v, i = v - 1, i + 1
|
| 323 |
+
|
| 324 |
+
return dmp_strip([ _rec_eval_in(c, a, v, i, j, K) for c in g ], v)
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def dmp_eval_in(f, a, j, u, K):
|
| 328 |
+
"""
|
| 329 |
+
Evaluate a polynomial at ``x_j = a`` in ``K[X]`` using the Horner scheme.
|
| 330 |
+
|
| 331 |
+
Examples
|
| 332 |
+
========
|
| 333 |
+
|
| 334 |
+
>>> from sympy.polys import ring, ZZ
|
| 335 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 336 |
+
|
| 337 |
+
>>> f = 2*x*y + 3*x + y + 2
|
| 338 |
+
|
| 339 |
+
>>> R.dmp_eval_in(f, 2, 0)
|
| 340 |
+
5*y + 8
|
| 341 |
+
>>> R.dmp_eval_in(f, 2, 1)
|
| 342 |
+
7*x + 4
|
| 343 |
+
|
| 344 |
+
"""
|
| 345 |
+
if j < 0 or j > u:
|
| 346 |
+
raise IndexError("0 <= j <= %s expected, got %s" % (u, j))
|
| 347 |
+
|
| 348 |
+
return _rec_eval_in(f, a, u, 0, j, K)
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
def _rec_eval_tail(g, i, A, u, K):
|
| 352 |
+
"""Recursive helper for :func:`dmp_eval_tail`."""
|
| 353 |
+
if i == u:
|
| 354 |
+
return dup_eval(g, A[-1], K)
|
| 355 |
+
else:
|
| 356 |
+
h = [ _rec_eval_tail(c, i + 1, A, u, K) for c in g ]
|
| 357 |
+
|
| 358 |
+
if i < u - len(A) + 1:
|
| 359 |
+
return h
|
| 360 |
+
else:
|
| 361 |
+
return dup_eval(h, A[-u + i - 1], K)
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def dmp_eval_tail(f, A, u, K):
|
| 365 |
+
"""
|
| 366 |
+
Evaluate a polynomial at ``x_j = a_j, ...`` in ``K[X]``.
|
| 367 |
+
|
| 368 |
+
Examples
|
| 369 |
+
========
|
| 370 |
+
|
| 371 |
+
>>> from sympy.polys import ring, ZZ
|
| 372 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 373 |
+
|
| 374 |
+
>>> f = 2*x*y + 3*x + y + 2
|
| 375 |
+
|
| 376 |
+
>>> R.dmp_eval_tail(f, [2])
|
| 377 |
+
7*x + 4
|
| 378 |
+
>>> R.dmp_eval_tail(f, [2, 2])
|
| 379 |
+
18
|
| 380 |
+
|
| 381 |
+
"""
|
| 382 |
+
if not A:
|
| 383 |
+
return f
|
| 384 |
+
|
| 385 |
+
if dmp_zero_p(f, u):
|
| 386 |
+
return dmp_zero(u - len(A))
|
| 387 |
+
|
| 388 |
+
e = _rec_eval_tail(f, 0, A, u, K)
|
| 389 |
+
|
| 390 |
+
if u == len(A) - 1:
|
| 391 |
+
return e
|
| 392 |
+
else:
|
| 393 |
+
return dmp_strip(e, u - len(A))
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
def _rec_diff_eval(g, m, a, v, i, j, K):
|
| 397 |
+
"""Recursive helper for :func:`dmp_diff_eval`."""
|
| 398 |
+
if i == j:
|
| 399 |
+
return dmp_eval(dmp_diff(g, m, v, K), a, v, K)
|
| 400 |
+
|
| 401 |
+
v, i = v - 1, i + 1
|
| 402 |
+
|
| 403 |
+
return dmp_strip([ _rec_diff_eval(c, m, a, v, i, j, K) for c in g ], v)
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
def dmp_diff_eval_in(f, m, a, j, u, K):
|
| 407 |
+
"""
|
| 408 |
+
Differentiate and evaluate a polynomial in ``x_j`` at ``a`` in ``K[X]``.
|
| 409 |
+
|
| 410 |
+
Examples
|
| 411 |
+
========
|
| 412 |
+
|
| 413 |
+
>>> from sympy.polys import ring, ZZ
|
| 414 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 415 |
+
|
| 416 |
+
>>> f = x*y**2 + 2*x*y + 3*x + 2*y**2 + 3*y + 1
|
| 417 |
+
|
| 418 |
+
>>> R.dmp_diff_eval_in(f, 1, 2, 0)
|
| 419 |
+
y**2 + 2*y + 3
|
| 420 |
+
>>> R.dmp_diff_eval_in(f, 1, 2, 1)
|
| 421 |
+
6*x + 11
|
| 422 |
+
|
| 423 |
+
"""
|
| 424 |
+
if j > u:
|
| 425 |
+
raise IndexError("-%s <= j < %s expected, got %s" % (u, u, j))
|
| 426 |
+
if not j:
|
| 427 |
+
return dmp_eval(dmp_diff(f, m, u, K), a, u, K)
|
| 428 |
+
|
| 429 |
+
return _rec_diff_eval(f, m, a, u, 0, j, K)
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
def dup_trunc(f, p, K):
|
| 433 |
+
"""
|
| 434 |
+
Reduce a ``K[x]`` polynomial modulo a constant ``p`` in ``K``.
|
| 435 |
+
|
| 436 |
+
Examples
|
| 437 |
+
========
|
| 438 |
+
|
| 439 |
+
>>> from sympy.polys import ring, ZZ
|
| 440 |
+
>>> R, x = ring("x", ZZ)
|
| 441 |
+
|
| 442 |
+
>>> R.dup_trunc(2*x**3 + 3*x**2 + 5*x + 7, ZZ(3))
|
| 443 |
+
-x**3 - x + 1
|
| 444 |
+
|
| 445 |
+
"""
|
| 446 |
+
if K.is_ZZ:
|
| 447 |
+
g = []
|
| 448 |
+
|
| 449 |
+
for c in f:
|
| 450 |
+
c = c % p
|
| 451 |
+
|
| 452 |
+
if c > p // 2:
|
| 453 |
+
g.append(c - p)
|
| 454 |
+
else:
|
| 455 |
+
g.append(c)
|
| 456 |
+
elif K.is_FiniteField:
|
| 457 |
+
# XXX: python-flint's nmod does not support %
|
| 458 |
+
pi = int(p)
|
| 459 |
+
g = [ K(int(c) % pi) for c in f ]
|
| 460 |
+
else:
|
| 461 |
+
g = [ c % p for c in f ]
|
| 462 |
+
|
| 463 |
+
return dup_strip(g)
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
def dmp_trunc(f, p, u, K):
|
| 467 |
+
"""
|
| 468 |
+
Reduce a ``K[X]`` polynomial modulo a polynomial ``p`` in ``K[Y]``.
|
| 469 |
+
|
| 470 |
+
Examples
|
| 471 |
+
========
|
| 472 |
+
|
| 473 |
+
>>> from sympy.polys import ring, ZZ
|
| 474 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 475 |
+
|
| 476 |
+
>>> f = 3*x**2*y + 8*x**2 + 5*x*y + 6*x + 2*y + 3
|
| 477 |
+
>>> g = (y - 1).drop(x)
|
| 478 |
+
|
| 479 |
+
>>> R.dmp_trunc(f, g)
|
| 480 |
+
11*x**2 + 11*x + 5
|
| 481 |
+
|
| 482 |
+
"""
|
| 483 |
+
return dmp_strip([ dmp_rem(c, p, u - 1, K) for c in f ], u)
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
def dmp_ground_trunc(f, p, u, K):
|
| 487 |
+
"""
|
| 488 |
+
Reduce a ``K[X]`` polynomial modulo a constant ``p`` in ``K``.
|
| 489 |
+
|
| 490 |
+
Examples
|
| 491 |
+
========
|
| 492 |
+
|
| 493 |
+
>>> from sympy.polys import ring, ZZ
|
| 494 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 495 |
+
|
| 496 |
+
>>> f = 3*x**2*y + 8*x**2 + 5*x*y + 6*x + 2*y + 3
|
| 497 |
+
|
| 498 |
+
>>> R.dmp_ground_trunc(f, ZZ(3))
|
| 499 |
+
-x**2 - x*y - y
|
| 500 |
+
|
| 501 |
+
"""
|
| 502 |
+
if not u:
|
| 503 |
+
return dup_trunc(f, p, K)
|
| 504 |
+
|
| 505 |
+
v = u - 1
|
| 506 |
+
|
| 507 |
+
return dmp_strip([ dmp_ground_trunc(c, p, v, K) for c in f ], u)
|
| 508 |
+
|
| 509 |
+
|
| 510 |
+
def dup_monic(f, K):
|
| 511 |
+
"""
|
| 512 |
+
Divide all coefficients by ``LC(f)`` in ``K[x]``.
|
| 513 |
+
|
| 514 |
+
Examples
|
| 515 |
+
========
|
| 516 |
+
|
| 517 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 518 |
+
|
| 519 |
+
>>> R, x = ring("x", ZZ)
|
| 520 |
+
>>> R.dup_monic(3*x**2 + 6*x + 9)
|
| 521 |
+
x**2 + 2*x + 3
|
| 522 |
+
|
| 523 |
+
>>> R, x = ring("x", QQ)
|
| 524 |
+
>>> R.dup_monic(3*x**2 + 4*x + 2)
|
| 525 |
+
x**2 + 4/3*x + 2/3
|
| 526 |
+
|
| 527 |
+
"""
|
| 528 |
+
if not f:
|
| 529 |
+
return f
|
| 530 |
+
|
| 531 |
+
lc = dup_LC(f, K)
|
| 532 |
+
|
| 533 |
+
if K.is_one(lc):
|
| 534 |
+
return f
|
| 535 |
+
else:
|
| 536 |
+
return dup_exquo_ground(f, lc, K)
|
| 537 |
+
|
| 538 |
+
|
| 539 |
+
def dmp_ground_monic(f, u, K):
|
| 540 |
+
"""
|
| 541 |
+
Divide all coefficients by ``LC(f)`` in ``K[X]``.
|
| 542 |
+
|
| 543 |
+
Examples
|
| 544 |
+
========
|
| 545 |
+
|
| 546 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 547 |
+
|
| 548 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 549 |
+
>>> f = 3*x**2*y + 6*x**2 + 3*x*y + 9*y + 3
|
| 550 |
+
|
| 551 |
+
>>> R.dmp_ground_monic(f)
|
| 552 |
+
x**2*y + 2*x**2 + x*y + 3*y + 1
|
| 553 |
+
|
| 554 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 555 |
+
>>> f = 3*x**2*y + 8*x**2 + 5*x*y + 6*x + 2*y + 3
|
| 556 |
+
|
| 557 |
+
>>> R.dmp_ground_monic(f)
|
| 558 |
+
x**2*y + 8/3*x**2 + 5/3*x*y + 2*x + 2/3*y + 1
|
| 559 |
+
|
| 560 |
+
"""
|
| 561 |
+
if not u:
|
| 562 |
+
return dup_monic(f, K)
|
| 563 |
+
|
| 564 |
+
if dmp_zero_p(f, u):
|
| 565 |
+
return f
|
| 566 |
+
|
| 567 |
+
lc = dmp_ground_LC(f, u, K)
|
| 568 |
+
|
| 569 |
+
if K.is_one(lc):
|
| 570 |
+
return f
|
| 571 |
+
else:
|
| 572 |
+
return dmp_exquo_ground(f, lc, u, K)
|
| 573 |
+
|
| 574 |
+
|
| 575 |
+
def dup_content(f, K):
|
| 576 |
+
"""
|
| 577 |
+
Compute the GCD of coefficients of ``f`` in ``K[x]``.
|
| 578 |
+
|
| 579 |
+
Examples
|
| 580 |
+
========
|
| 581 |
+
|
| 582 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 583 |
+
|
| 584 |
+
>>> R, x = ring("x", ZZ)
|
| 585 |
+
>>> f = 6*x**2 + 8*x + 12
|
| 586 |
+
|
| 587 |
+
>>> R.dup_content(f)
|
| 588 |
+
2
|
| 589 |
+
|
| 590 |
+
>>> R, x = ring("x", QQ)
|
| 591 |
+
>>> f = 6*x**2 + 8*x + 12
|
| 592 |
+
|
| 593 |
+
>>> R.dup_content(f)
|
| 594 |
+
2
|
| 595 |
+
|
| 596 |
+
"""
|
| 597 |
+
from sympy.polys.domains import QQ
|
| 598 |
+
|
| 599 |
+
if not f:
|
| 600 |
+
return K.zero
|
| 601 |
+
|
| 602 |
+
cont = K.zero
|
| 603 |
+
|
| 604 |
+
if K == QQ:
|
| 605 |
+
for c in f:
|
| 606 |
+
cont = K.gcd(cont, c)
|
| 607 |
+
else:
|
| 608 |
+
for c in f:
|
| 609 |
+
cont = K.gcd(cont, c)
|
| 610 |
+
|
| 611 |
+
if K.is_one(cont):
|
| 612 |
+
break
|
| 613 |
+
|
| 614 |
+
return cont
|
| 615 |
+
|
| 616 |
+
|
| 617 |
+
def dmp_ground_content(f, u, K):
|
| 618 |
+
"""
|
| 619 |
+
Compute the GCD of coefficients of ``f`` in ``K[X]``.
|
| 620 |
+
|
| 621 |
+
Examples
|
| 622 |
+
========
|
| 623 |
+
|
| 624 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 625 |
+
|
| 626 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 627 |
+
>>> f = 2*x*y + 6*x + 4*y + 12
|
| 628 |
+
|
| 629 |
+
>>> R.dmp_ground_content(f)
|
| 630 |
+
2
|
| 631 |
+
|
| 632 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 633 |
+
>>> f = 2*x*y + 6*x + 4*y + 12
|
| 634 |
+
|
| 635 |
+
>>> R.dmp_ground_content(f)
|
| 636 |
+
2
|
| 637 |
+
|
| 638 |
+
"""
|
| 639 |
+
from sympy.polys.domains import QQ
|
| 640 |
+
|
| 641 |
+
if not u:
|
| 642 |
+
return dup_content(f, K)
|
| 643 |
+
|
| 644 |
+
if dmp_zero_p(f, u):
|
| 645 |
+
return K.zero
|
| 646 |
+
|
| 647 |
+
cont, v = K.zero, u - 1
|
| 648 |
+
|
| 649 |
+
if K == QQ:
|
| 650 |
+
for c in f:
|
| 651 |
+
cont = K.gcd(cont, dmp_ground_content(c, v, K))
|
| 652 |
+
else:
|
| 653 |
+
for c in f:
|
| 654 |
+
cont = K.gcd(cont, dmp_ground_content(c, v, K))
|
| 655 |
+
|
| 656 |
+
if K.is_one(cont):
|
| 657 |
+
break
|
| 658 |
+
|
| 659 |
+
return cont
|
| 660 |
+
|
| 661 |
+
|
| 662 |
+
def dup_primitive(f, K):
|
| 663 |
+
"""
|
| 664 |
+
Compute content and the primitive form of ``f`` in ``K[x]``.
|
| 665 |
+
|
| 666 |
+
Examples
|
| 667 |
+
========
|
| 668 |
+
|
| 669 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 670 |
+
|
| 671 |
+
>>> R, x = ring("x", ZZ)
|
| 672 |
+
>>> f = 6*x**2 + 8*x + 12
|
| 673 |
+
|
| 674 |
+
>>> R.dup_primitive(f)
|
| 675 |
+
(2, 3*x**2 + 4*x + 6)
|
| 676 |
+
|
| 677 |
+
>>> R, x = ring("x", QQ)
|
| 678 |
+
>>> f = 6*x**2 + 8*x + 12
|
| 679 |
+
|
| 680 |
+
>>> R.dup_primitive(f)
|
| 681 |
+
(2, 3*x**2 + 4*x + 6)
|
| 682 |
+
|
| 683 |
+
"""
|
| 684 |
+
if not f:
|
| 685 |
+
return K.zero, f
|
| 686 |
+
|
| 687 |
+
cont = dup_content(f, K)
|
| 688 |
+
|
| 689 |
+
if K.is_one(cont):
|
| 690 |
+
return cont, f
|
| 691 |
+
else:
|
| 692 |
+
return cont, dup_quo_ground(f, cont, K)
|
| 693 |
+
|
| 694 |
+
|
| 695 |
+
def dmp_ground_primitive(f, u, K):
|
| 696 |
+
"""
|
| 697 |
+
Compute content and the primitive form of ``f`` in ``K[X]``.
|
| 698 |
+
|
| 699 |
+
Examples
|
| 700 |
+
========
|
| 701 |
+
|
| 702 |
+
>>> from sympy.polys import ring, ZZ, QQ
|
| 703 |
+
|
| 704 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 705 |
+
>>> f = 2*x*y + 6*x + 4*y + 12
|
| 706 |
+
|
| 707 |
+
>>> R.dmp_ground_primitive(f)
|
| 708 |
+
(2, x*y + 3*x + 2*y + 6)
|
| 709 |
+
|
| 710 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 711 |
+
>>> f = 2*x*y + 6*x + 4*y + 12
|
| 712 |
+
|
| 713 |
+
>>> R.dmp_ground_primitive(f)
|
| 714 |
+
(2, x*y + 3*x + 2*y + 6)
|
| 715 |
+
|
| 716 |
+
"""
|
| 717 |
+
if not u:
|
| 718 |
+
return dup_primitive(f, K)
|
| 719 |
+
|
| 720 |
+
if dmp_zero_p(f, u):
|
| 721 |
+
return K.zero, f
|
| 722 |
+
|
| 723 |
+
cont = dmp_ground_content(f, u, K)
|
| 724 |
+
|
| 725 |
+
if K.is_one(cont):
|
| 726 |
+
return cont, f
|
| 727 |
+
else:
|
| 728 |
+
return cont, dmp_quo_ground(f, cont, u, K)
|
| 729 |
+
|
| 730 |
+
|
| 731 |
+
def dup_extract(f, g, K):
|
| 732 |
+
"""
|
| 733 |
+
Extract common content from a pair of polynomials in ``K[x]``.
|
| 734 |
+
|
| 735 |
+
Examples
|
| 736 |
+
========
|
| 737 |
+
|
| 738 |
+
>>> from sympy.polys import ring, ZZ
|
| 739 |
+
>>> R, x = ring("x", ZZ)
|
| 740 |
+
|
| 741 |
+
>>> R.dup_extract(6*x**2 + 12*x + 18, 4*x**2 + 8*x + 12)
|
| 742 |
+
(2, 3*x**2 + 6*x + 9, 2*x**2 + 4*x + 6)
|
| 743 |
+
|
| 744 |
+
"""
|
| 745 |
+
fc = dup_content(f, K)
|
| 746 |
+
gc = dup_content(g, K)
|
| 747 |
+
|
| 748 |
+
gcd = K.gcd(fc, gc)
|
| 749 |
+
|
| 750 |
+
if not K.is_one(gcd):
|
| 751 |
+
f = dup_quo_ground(f, gcd, K)
|
| 752 |
+
g = dup_quo_ground(g, gcd, K)
|
| 753 |
+
|
| 754 |
+
return gcd, f, g
|
| 755 |
+
|
| 756 |
+
|
| 757 |
+
def dmp_ground_extract(f, g, u, K):
|
| 758 |
+
"""
|
| 759 |
+
Extract common content from a pair of polynomials in ``K[X]``.
|
| 760 |
+
|
| 761 |
+
Examples
|
| 762 |
+
========
|
| 763 |
+
|
| 764 |
+
>>> from sympy.polys import ring, ZZ
|
| 765 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 766 |
+
|
| 767 |
+
>>> R.dmp_ground_extract(6*x*y + 12*x + 18, 4*x*y + 8*x + 12)
|
| 768 |
+
(2, 3*x*y + 6*x + 9, 2*x*y + 4*x + 6)
|
| 769 |
+
|
| 770 |
+
"""
|
| 771 |
+
fc = dmp_ground_content(f, u, K)
|
| 772 |
+
gc = dmp_ground_content(g, u, K)
|
| 773 |
+
|
| 774 |
+
gcd = K.gcd(fc, gc)
|
| 775 |
+
|
| 776 |
+
if not K.is_one(gcd):
|
| 777 |
+
f = dmp_quo_ground(f, gcd, u, K)
|
| 778 |
+
g = dmp_quo_ground(g, gcd, u, K)
|
| 779 |
+
|
| 780 |
+
return gcd, f, g
|
| 781 |
+
|
| 782 |
+
|
| 783 |
+
def dup_real_imag(f, K):
|
| 784 |
+
"""
|
| 785 |
+
Find ``f1`` and ``f2``, such that ``f(x+I*y) = f1(x,y) + f2(x,y)*I``.
|
| 786 |
+
|
| 787 |
+
Examples
|
| 788 |
+
========
|
| 789 |
+
|
| 790 |
+
>>> from sympy.polys import ring, ZZ
|
| 791 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 792 |
+
|
| 793 |
+
>>> R.dup_real_imag(x**3 + x**2 + x + 1)
|
| 794 |
+
(x**3 + x**2 - 3*x*y**2 + x - y**2 + 1, 3*x**2*y + 2*x*y - y**3 + y)
|
| 795 |
+
|
| 796 |
+
>>> from sympy.abc import x, y, z
|
| 797 |
+
>>> from sympy import I
|
| 798 |
+
>>> (z**3 + z**2 + z + 1).subs(z, x+I*y).expand().collect(I)
|
| 799 |
+
x**3 + x**2 - 3*x*y**2 + x - y**2 + I*(3*x**2*y + 2*x*y - y**3 + y) + 1
|
| 800 |
+
|
| 801 |
+
"""
|
| 802 |
+
if not K.is_ZZ and not K.is_QQ:
|
| 803 |
+
raise DomainError("computing real and imaginary parts is not supported over %s" % K)
|
| 804 |
+
|
| 805 |
+
f1 = dmp_zero(1)
|
| 806 |
+
f2 = dmp_zero(1)
|
| 807 |
+
|
| 808 |
+
if not f:
|
| 809 |
+
return f1, f2
|
| 810 |
+
|
| 811 |
+
g = [[[K.one, K.zero]], [[K.one], []]]
|
| 812 |
+
h = dmp_ground(f[0], 2)
|
| 813 |
+
|
| 814 |
+
for c in f[1:]:
|
| 815 |
+
h = dmp_mul(h, g, 2, K)
|
| 816 |
+
h = dmp_add_term(h, dmp_ground(c, 1), 0, 2, K)
|
| 817 |
+
|
| 818 |
+
H = dup_to_raw_dict(h)
|
| 819 |
+
|
| 820 |
+
for k, h in H.items():
|
| 821 |
+
m = k % 4
|
| 822 |
+
|
| 823 |
+
if not m:
|
| 824 |
+
f1 = dmp_add(f1, h, 1, K)
|
| 825 |
+
elif m == 1:
|
| 826 |
+
f2 = dmp_add(f2, h, 1, K)
|
| 827 |
+
elif m == 2:
|
| 828 |
+
f1 = dmp_sub(f1, h, 1, K)
|
| 829 |
+
else:
|
| 830 |
+
f2 = dmp_sub(f2, h, 1, K)
|
| 831 |
+
|
| 832 |
+
return f1, f2
|
| 833 |
+
|
| 834 |
+
|
| 835 |
+
def dup_mirror(f, K):
|
| 836 |
+
"""
|
| 837 |
+
Evaluate efficiently the composition ``f(-x)`` in ``K[x]``.
|
| 838 |
+
|
| 839 |
+
Examples
|
| 840 |
+
========
|
| 841 |
+
|
| 842 |
+
>>> from sympy.polys import ring, ZZ
|
| 843 |
+
>>> R, x = ring("x", ZZ)
|
| 844 |
+
|
| 845 |
+
>>> R.dup_mirror(x**3 + 2*x**2 - 4*x + 2)
|
| 846 |
+
-x**3 + 2*x**2 + 4*x + 2
|
| 847 |
+
|
| 848 |
+
"""
|
| 849 |
+
f = list(f)
|
| 850 |
+
|
| 851 |
+
for i in range(len(f) - 2, -1, -2):
|
| 852 |
+
f[i] = -f[i]
|
| 853 |
+
|
| 854 |
+
return f
|
| 855 |
+
|
| 856 |
+
|
| 857 |
+
def dup_scale(f, a, K):
|
| 858 |
+
"""
|
| 859 |
+
Evaluate efficiently composition ``f(a*x)`` in ``K[x]``.
|
| 860 |
+
|
| 861 |
+
Examples
|
| 862 |
+
========
|
| 863 |
+
|
| 864 |
+
>>> from sympy.polys import ring, ZZ
|
| 865 |
+
>>> R, x = ring("x", ZZ)
|
| 866 |
+
|
| 867 |
+
>>> R.dup_scale(x**2 - 2*x + 1, ZZ(2))
|
| 868 |
+
4*x**2 - 4*x + 1
|
| 869 |
+
|
| 870 |
+
"""
|
| 871 |
+
f, n, b = list(f), len(f) - 1, a
|
| 872 |
+
|
| 873 |
+
for i in range(n - 1, -1, -1):
|
| 874 |
+
f[i], b = b*f[i], b*a
|
| 875 |
+
|
| 876 |
+
return f
|
| 877 |
+
|
| 878 |
+
|
| 879 |
+
def dup_shift(f, a, K):
|
| 880 |
+
"""
|
| 881 |
+
Evaluate efficiently Taylor shift ``f(x + a)`` in ``K[x]``.
|
| 882 |
+
|
| 883 |
+
Examples
|
| 884 |
+
========
|
| 885 |
+
|
| 886 |
+
>>> from sympy.polys import ring, ZZ
|
| 887 |
+
>>> R, x = ring("x", ZZ)
|
| 888 |
+
|
| 889 |
+
>>> R.dup_shift(x**2 - 2*x + 1, ZZ(2))
|
| 890 |
+
x**2 + 2*x + 1
|
| 891 |
+
|
| 892 |
+
"""
|
| 893 |
+
f, n = list(f), len(f) - 1
|
| 894 |
+
|
| 895 |
+
for i in range(n, 0, -1):
|
| 896 |
+
for j in range(0, i):
|
| 897 |
+
f[j + 1] += a*f[j]
|
| 898 |
+
|
| 899 |
+
return f
|
| 900 |
+
|
| 901 |
+
|
| 902 |
+
def dmp_shift(f, a, u, K):
|
| 903 |
+
"""
|
| 904 |
+
Evaluate efficiently Taylor shift ``f(X + A)`` in ``K[X]``.
|
| 905 |
+
|
| 906 |
+
Examples
|
| 907 |
+
========
|
| 908 |
+
|
| 909 |
+
>>> from sympy import symbols, ring, ZZ
|
| 910 |
+
>>> x, y = symbols('x y')
|
| 911 |
+
>>> R, _, _ = ring([x, y], ZZ)
|
| 912 |
+
|
| 913 |
+
>>> p = x**2*y + 2*x*y + 3*x + 4*y + 5
|
| 914 |
+
|
| 915 |
+
>>> R.dmp_shift(R(p), [ZZ(1), ZZ(2)])
|
| 916 |
+
x**2*y + 2*x**2 + 4*x*y + 11*x + 7*y + 22
|
| 917 |
+
|
| 918 |
+
>>> p.subs({x: x + 1, y: y + 2}).expand()
|
| 919 |
+
x**2*y + 2*x**2 + 4*x*y + 11*x + 7*y + 22
|
| 920 |
+
"""
|
| 921 |
+
if not u:
|
| 922 |
+
return dup_shift(f, a[0], K)
|
| 923 |
+
|
| 924 |
+
if dmp_zero_p(f, u):
|
| 925 |
+
return f
|
| 926 |
+
|
| 927 |
+
a0, a1 = a[0], a[1:]
|
| 928 |
+
|
| 929 |
+
f = [ dmp_shift(c, a1, u-1, K) for c in f ]
|
| 930 |
+
n = len(f) - 1
|
| 931 |
+
|
| 932 |
+
for i in range(n, 0, -1):
|
| 933 |
+
for j in range(0, i):
|
| 934 |
+
afj = dmp_mul_ground(f[j], a0, u-1, K)
|
| 935 |
+
f[j + 1] = dmp_add(f[j + 1], afj, u-1, K)
|
| 936 |
+
|
| 937 |
+
return f
|
| 938 |
+
|
| 939 |
+
|
| 940 |
+
def dup_transform(f, p, q, K):
|
| 941 |
+
"""
|
| 942 |
+
Evaluate functional transformation ``q**n * f(p/q)`` in ``K[x]``.
|
| 943 |
+
|
| 944 |
+
Examples
|
| 945 |
+
========
|
| 946 |
+
|
| 947 |
+
>>> from sympy.polys import ring, ZZ
|
| 948 |
+
>>> R, x = ring("x", ZZ)
|
| 949 |
+
|
| 950 |
+
>>> R.dup_transform(x**2 - 2*x + 1, x**2 + 1, x - 1)
|
| 951 |
+
x**4 - 2*x**3 + 5*x**2 - 4*x + 4
|
| 952 |
+
|
| 953 |
+
"""
|
| 954 |
+
if not f:
|
| 955 |
+
return []
|
| 956 |
+
|
| 957 |
+
n = len(f) - 1
|
| 958 |
+
h, Q = [f[0]], [[K.one]]
|
| 959 |
+
|
| 960 |
+
for i in range(0, n):
|
| 961 |
+
Q.append(dup_mul(Q[-1], q, K))
|
| 962 |
+
|
| 963 |
+
for c, q in zip(f[1:], Q[1:]):
|
| 964 |
+
h = dup_mul(h, p, K)
|
| 965 |
+
q = dup_mul_ground(q, c, K)
|
| 966 |
+
h = dup_add(h, q, K)
|
| 967 |
+
|
| 968 |
+
return h
|
| 969 |
+
|
| 970 |
+
|
| 971 |
+
def dup_compose(f, g, K):
|
| 972 |
+
"""
|
| 973 |
+
Evaluate functional composition ``f(g)`` in ``K[x]``.
|
| 974 |
+
|
| 975 |
+
Examples
|
| 976 |
+
========
|
| 977 |
+
|
| 978 |
+
>>> from sympy.polys import ring, ZZ
|
| 979 |
+
>>> R, x = ring("x", ZZ)
|
| 980 |
+
|
| 981 |
+
>>> R.dup_compose(x**2 + x, x - 1)
|
| 982 |
+
x**2 - x
|
| 983 |
+
|
| 984 |
+
"""
|
| 985 |
+
if len(g) <= 1:
|
| 986 |
+
return dup_strip([dup_eval(f, dup_LC(g, K), K)])
|
| 987 |
+
|
| 988 |
+
if not f:
|
| 989 |
+
return []
|
| 990 |
+
|
| 991 |
+
h = [f[0]]
|
| 992 |
+
|
| 993 |
+
for c in f[1:]:
|
| 994 |
+
h = dup_mul(h, g, K)
|
| 995 |
+
h = dup_add_term(h, c, 0, K)
|
| 996 |
+
|
| 997 |
+
return h
|
| 998 |
+
|
| 999 |
+
|
| 1000 |
+
def dmp_compose(f, g, u, K):
|
| 1001 |
+
"""
|
| 1002 |
+
Evaluate functional composition ``f(g)`` in ``K[X]``.
|
| 1003 |
+
|
| 1004 |
+
Examples
|
| 1005 |
+
========
|
| 1006 |
+
|
| 1007 |
+
>>> from sympy.polys import ring, ZZ
|
| 1008 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1009 |
+
|
| 1010 |
+
>>> R.dmp_compose(x*y + 2*x + y, y)
|
| 1011 |
+
y**2 + 3*y
|
| 1012 |
+
|
| 1013 |
+
"""
|
| 1014 |
+
if not u:
|
| 1015 |
+
return dup_compose(f, g, K)
|
| 1016 |
+
|
| 1017 |
+
if dmp_zero_p(f, u):
|
| 1018 |
+
return f
|
| 1019 |
+
|
| 1020 |
+
h = [f[0]]
|
| 1021 |
+
|
| 1022 |
+
for c in f[1:]:
|
| 1023 |
+
h = dmp_mul(h, g, u, K)
|
| 1024 |
+
h = dmp_add_term(h, c, 0, u, K)
|
| 1025 |
+
|
| 1026 |
+
return h
|
| 1027 |
+
|
| 1028 |
+
|
| 1029 |
+
def _dup_right_decompose(f, s, K):
|
| 1030 |
+
"""Helper function for :func:`_dup_decompose`."""
|
| 1031 |
+
n = len(f) - 1
|
| 1032 |
+
lc = dup_LC(f, K)
|
| 1033 |
+
|
| 1034 |
+
f = dup_to_raw_dict(f)
|
| 1035 |
+
g = { s: K.one }
|
| 1036 |
+
|
| 1037 |
+
r = n // s
|
| 1038 |
+
|
| 1039 |
+
for i in range(1, s):
|
| 1040 |
+
coeff = K.zero
|
| 1041 |
+
|
| 1042 |
+
for j in range(0, i):
|
| 1043 |
+
if not n + j - i in f:
|
| 1044 |
+
continue
|
| 1045 |
+
|
| 1046 |
+
if not s - j in g:
|
| 1047 |
+
continue
|
| 1048 |
+
|
| 1049 |
+
fc, gc = f[n + j - i], g[s - j]
|
| 1050 |
+
coeff += (i - r*j)*fc*gc
|
| 1051 |
+
|
| 1052 |
+
g[s - i] = K.quo(coeff, i*r*lc)
|
| 1053 |
+
|
| 1054 |
+
return dup_from_raw_dict(g, K)
|
| 1055 |
+
|
| 1056 |
+
|
| 1057 |
+
def _dup_left_decompose(f, h, K):
|
| 1058 |
+
"""Helper function for :func:`_dup_decompose`."""
|
| 1059 |
+
g, i = {}, 0
|
| 1060 |
+
|
| 1061 |
+
while f:
|
| 1062 |
+
q, r = dup_div(f, h, K)
|
| 1063 |
+
|
| 1064 |
+
if dup_degree(r) > 0:
|
| 1065 |
+
return None
|
| 1066 |
+
else:
|
| 1067 |
+
g[i] = dup_LC(r, K)
|
| 1068 |
+
f, i = q, i + 1
|
| 1069 |
+
|
| 1070 |
+
return dup_from_raw_dict(g, K)
|
| 1071 |
+
|
| 1072 |
+
|
| 1073 |
+
def _dup_decompose(f, K):
|
| 1074 |
+
"""Helper function for :func:`dup_decompose`."""
|
| 1075 |
+
df = len(f) - 1
|
| 1076 |
+
|
| 1077 |
+
for s in range(2, df):
|
| 1078 |
+
if df % s != 0:
|
| 1079 |
+
continue
|
| 1080 |
+
|
| 1081 |
+
h = _dup_right_decompose(f, s, K)
|
| 1082 |
+
|
| 1083 |
+
if h is not None:
|
| 1084 |
+
g = _dup_left_decompose(f, h, K)
|
| 1085 |
+
|
| 1086 |
+
if g is not None:
|
| 1087 |
+
return g, h
|
| 1088 |
+
|
| 1089 |
+
return None
|
| 1090 |
+
|
| 1091 |
+
|
| 1092 |
+
def dup_decompose(f, K):
|
| 1093 |
+
"""
|
| 1094 |
+
Computes functional decomposition of ``f`` in ``K[x]``.
|
| 1095 |
+
|
| 1096 |
+
Given a univariate polynomial ``f`` with coefficients in a field of
|
| 1097 |
+
characteristic zero, returns list ``[f_1, f_2, ..., f_n]``, where::
|
| 1098 |
+
|
| 1099 |
+
f = f_1 o f_2 o ... f_n = f_1(f_2(... f_n))
|
| 1100 |
+
|
| 1101 |
+
and ``f_2, ..., f_n`` are monic and homogeneous polynomials of at
|
| 1102 |
+
least second degree.
|
| 1103 |
+
|
| 1104 |
+
Unlike factorization, complete functional decompositions of
|
| 1105 |
+
polynomials are not unique, consider examples:
|
| 1106 |
+
|
| 1107 |
+
1. ``f o g = f(x + b) o (g - b)``
|
| 1108 |
+
2. ``x**n o x**m = x**m o x**n``
|
| 1109 |
+
3. ``T_n o T_m = T_m o T_n``
|
| 1110 |
+
|
| 1111 |
+
where ``T_n`` and ``T_m`` are Chebyshev polynomials.
|
| 1112 |
+
|
| 1113 |
+
Examples
|
| 1114 |
+
========
|
| 1115 |
+
|
| 1116 |
+
>>> from sympy.polys import ring, ZZ
|
| 1117 |
+
>>> R, x = ring("x", ZZ)
|
| 1118 |
+
|
| 1119 |
+
>>> R.dup_decompose(x**4 - 2*x**3 + x**2)
|
| 1120 |
+
[x**2, x**2 - x]
|
| 1121 |
+
|
| 1122 |
+
References
|
| 1123 |
+
==========
|
| 1124 |
+
|
| 1125 |
+
.. [1] [Kozen89]_
|
| 1126 |
+
|
| 1127 |
+
"""
|
| 1128 |
+
F = []
|
| 1129 |
+
|
| 1130 |
+
while True:
|
| 1131 |
+
result = _dup_decompose(f, K)
|
| 1132 |
+
|
| 1133 |
+
if result is not None:
|
| 1134 |
+
f, h = result
|
| 1135 |
+
F = [h] + F
|
| 1136 |
+
else:
|
| 1137 |
+
break
|
| 1138 |
+
|
| 1139 |
+
return [f] + F
|
| 1140 |
+
|
| 1141 |
+
|
| 1142 |
+
def dmp_lift(f, u, K):
|
| 1143 |
+
"""
|
| 1144 |
+
Convert algebraic coefficients to integers in ``K[X]``.
|
| 1145 |
+
|
| 1146 |
+
Examples
|
| 1147 |
+
========
|
| 1148 |
+
|
| 1149 |
+
>>> from sympy.polys import ring, QQ
|
| 1150 |
+
>>> from sympy import I
|
| 1151 |
+
|
| 1152 |
+
>>> K = QQ.algebraic_field(I)
|
| 1153 |
+
>>> R, x = ring("x", K)
|
| 1154 |
+
|
| 1155 |
+
>>> f = x**2 + K([QQ(1), QQ(0)])*x + K([QQ(2), QQ(0)])
|
| 1156 |
+
|
| 1157 |
+
>>> R.dmp_lift(f)
|
| 1158 |
+
x**8 + 2*x**6 + 9*x**4 - 8*x**2 + 16
|
| 1159 |
+
|
| 1160 |
+
"""
|
| 1161 |
+
if K.is_GaussianField:
|
| 1162 |
+
K1 = K.as_AlgebraicField()
|
| 1163 |
+
f = dmp_convert(f, u, K, K1)
|
| 1164 |
+
K = K1
|
| 1165 |
+
|
| 1166 |
+
if not K.is_Algebraic:
|
| 1167 |
+
raise DomainError(
|
| 1168 |
+
'computation can be done only in an algebraic domain')
|
| 1169 |
+
|
| 1170 |
+
F, monoms, polys = dmp_to_dict(f, u), [], []
|
| 1171 |
+
|
| 1172 |
+
for monom, coeff in F.items():
|
| 1173 |
+
if not coeff.is_ground:
|
| 1174 |
+
monoms.append(monom)
|
| 1175 |
+
|
| 1176 |
+
perms = variations([-1, 1], len(monoms), repetition=True)
|
| 1177 |
+
|
| 1178 |
+
for perm in perms:
|
| 1179 |
+
G = dict(F)
|
| 1180 |
+
|
| 1181 |
+
for sign, monom in zip(perm, monoms):
|
| 1182 |
+
if sign == -1:
|
| 1183 |
+
G[monom] = -G[monom]
|
| 1184 |
+
|
| 1185 |
+
polys.append(dmp_from_dict(G, u, K))
|
| 1186 |
+
|
| 1187 |
+
return dmp_convert(dmp_expand(polys, u, K), u, K, K.dom)
|
| 1188 |
+
|
| 1189 |
+
|
| 1190 |
+
def dup_sign_variations(f, K):
|
| 1191 |
+
"""
|
| 1192 |
+
Compute the number of sign variations of ``f`` in ``K[x]``.
|
| 1193 |
+
|
| 1194 |
+
Examples
|
| 1195 |
+
========
|
| 1196 |
+
|
| 1197 |
+
>>> from sympy.polys import ring, ZZ
|
| 1198 |
+
>>> R, x = ring("x", ZZ)
|
| 1199 |
+
|
| 1200 |
+
>>> R.dup_sign_variations(x**4 - x**2 - x + 1)
|
| 1201 |
+
2
|
| 1202 |
+
|
| 1203 |
+
"""
|
| 1204 |
+
prev, k = K.zero, 0
|
| 1205 |
+
|
| 1206 |
+
for coeff in f:
|
| 1207 |
+
if K.is_negative(coeff*prev):
|
| 1208 |
+
k += 1
|
| 1209 |
+
|
| 1210 |
+
if coeff:
|
| 1211 |
+
prev = coeff
|
| 1212 |
+
|
| 1213 |
+
return k
|
| 1214 |
+
|
| 1215 |
+
|
| 1216 |
+
def dup_clear_denoms(f, K0, K1=None, convert=False):
|
| 1217 |
+
"""
|
| 1218 |
+
Clear denominators, i.e. transform ``K_0`` to ``K_1``.
|
| 1219 |
+
|
| 1220 |
+
Examples
|
| 1221 |
+
========
|
| 1222 |
+
|
| 1223 |
+
>>> from sympy.polys import ring, QQ
|
| 1224 |
+
>>> R, x = ring("x", QQ)
|
| 1225 |
+
|
| 1226 |
+
>>> f = QQ(1,2)*x + QQ(1,3)
|
| 1227 |
+
|
| 1228 |
+
>>> R.dup_clear_denoms(f, convert=False)
|
| 1229 |
+
(6, 3*x + 2)
|
| 1230 |
+
>>> R.dup_clear_denoms(f, convert=True)
|
| 1231 |
+
(6, 3*x + 2)
|
| 1232 |
+
|
| 1233 |
+
"""
|
| 1234 |
+
if K1 is None:
|
| 1235 |
+
if K0.has_assoc_Ring:
|
| 1236 |
+
K1 = K0.get_ring()
|
| 1237 |
+
else:
|
| 1238 |
+
K1 = K0
|
| 1239 |
+
|
| 1240 |
+
common = K1.one
|
| 1241 |
+
|
| 1242 |
+
for c in f:
|
| 1243 |
+
common = K1.lcm(common, K0.denom(c))
|
| 1244 |
+
|
| 1245 |
+
if K1.is_one(common):
|
| 1246 |
+
if not convert:
|
| 1247 |
+
return common, f
|
| 1248 |
+
else:
|
| 1249 |
+
return common, dup_convert(f, K0, K1)
|
| 1250 |
+
|
| 1251 |
+
# Use quo rather than exquo to handle inexact domains by discarding the
|
| 1252 |
+
# remainder.
|
| 1253 |
+
f = [K0.numer(c)*K1.quo(common, K0.denom(c)) for c in f]
|
| 1254 |
+
|
| 1255 |
+
if not convert:
|
| 1256 |
+
return common, dup_convert(f, K1, K0)
|
| 1257 |
+
else:
|
| 1258 |
+
return common, f
|
| 1259 |
+
|
| 1260 |
+
|
| 1261 |
+
def _rec_clear_denoms(g, v, K0, K1):
|
| 1262 |
+
"""Recursive helper for :func:`dmp_clear_denoms`."""
|
| 1263 |
+
common = K1.one
|
| 1264 |
+
|
| 1265 |
+
if not v:
|
| 1266 |
+
for c in g:
|
| 1267 |
+
common = K1.lcm(common, K0.denom(c))
|
| 1268 |
+
else:
|
| 1269 |
+
w = v - 1
|
| 1270 |
+
|
| 1271 |
+
for c in g:
|
| 1272 |
+
common = K1.lcm(common, _rec_clear_denoms(c, w, K0, K1))
|
| 1273 |
+
|
| 1274 |
+
return common
|
| 1275 |
+
|
| 1276 |
+
|
| 1277 |
+
def dmp_clear_denoms(f, u, K0, K1=None, convert=False):
|
| 1278 |
+
"""
|
| 1279 |
+
Clear denominators, i.e. transform ``K_0`` to ``K_1``.
|
| 1280 |
+
|
| 1281 |
+
Examples
|
| 1282 |
+
========
|
| 1283 |
+
|
| 1284 |
+
>>> from sympy.polys import ring, QQ
|
| 1285 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 1286 |
+
|
| 1287 |
+
>>> f = QQ(1,2)*x + QQ(1,3)*y + 1
|
| 1288 |
+
|
| 1289 |
+
>>> R.dmp_clear_denoms(f, convert=False)
|
| 1290 |
+
(6, 3*x + 2*y + 6)
|
| 1291 |
+
>>> R.dmp_clear_denoms(f, convert=True)
|
| 1292 |
+
(6, 3*x + 2*y + 6)
|
| 1293 |
+
|
| 1294 |
+
"""
|
| 1295 |
+
if not u:
|
| 1296 |
+
return dup_clear_denoms(f, K0, K1, convert=convert)
|
| 1297 |
+
|
| 1298 |
+
if K1 is None:
|
| 1299 |
+
if K0.has_assoc_Ring:
|
| 1300 |
+
K1 = K0.get_ring()
|
| 1301 |
+
else:
|
| 1302 |
+
K1 = K0
|
| 1303 |
+
|
| 1304 |
+
common = _rec_clear_denoms(f, u, K0, K1)
|
| 1305 |
+
|
| 1306 |
+
if not K1.is_one(common):
|
| 1307 |
+
f = dmp_mul_ground(f, common, u, K0)
|
| 1308 |
+
|
| 1309 |
+
if not convert:
|
| 1310 |
+
return common, f
|
| 1311 |
+
else:
|
| 1312 |
+
return common, dmp_convert(f, u, K0, K1)
|
| 1313 |
+
|
| 1314 |
+
|
| 1315 |
+
def dup_revert(f, n, K):
|
| 1316 |
+
"""
|
| 1317 |
+
Compute ``f**(-1)`` mod ``x**n`` using Newton iteration.
|
| 1318 |
+
|
| 1319 |
+
This function computes first ``2**n`` terms of a polynomial that
|
| 1320 |
+
is a result of inversion of a polynomial modulo ``x**n``. This is
|
| 1321 |
+
useful to efficiently compute series expansion of ``1/f``.
|
| 1322 |
+
|
| 1323 |
+
Examples
|
| 1324 |
+
========
|
| 1325 |
+
|
| 1326 |
+
>>> from sympy.polys import ring, QQ
|
| 1327 |
+
>>> R, x = ring("x", QQ)
|
| 1328 |
+
|
| 1329 |
+
>>> f = -QQ(1,720)*x**6 + QQ(1,24)*x**4 - QQ(1,2)*x**2 + 1
|
| 1330 |
+
|
| 1331 |
+
>>> R.dup_revert(f, 8)
|
| 1332 |
+
61/720*x**6 + 5/24*x**4 + 1/2*x**2 + 1
|
| 1333 |
+
|
| 1334 |
+
"""
|
| 1335 |
+
g = [K.revert(dup_TC(f, K))]
|
| 1336 |
+
h = [K.one, K.zero, K.zero]
|
| 1337 |
+
|
| 1338 |
+
N = int(_ceil(_log2(n)))
|
| 1339 |
+
|
| 1340 |
+
for i in range(1, N + 1):
|
| 1341 |
+
a = dup_mul_ground(g, K(2), K)
|
| 1342 |
+
b = dup_mul(f, dup_sqr(g, K), K)
|
| 1343 |
+
g = dup_rem(dup_sub(a, b, K), h, K)
|
| 1344 |
+
h = dup_lshift(h, dup_degree(h), K)
|
| 1345 |
+
|
| 1346 |
+
return g
|
| 1347 |
+
|
| 1348 |
+
|
| 1349 |
+
def dmp_revert(f, g, u, K):
|
| 1350 |
+
"""
|
| 1351 |
+
Compute ``f**(-1)`` mod ``x**n`` using Newton iteration.
|
| 1352 |
+
|
| 1353 |
+
Examples
|
| 1354 |
+
========
|
| 1355 |
+
|
| 1356 |
+
>>> from sympy.polys import ring, QQ
|
| 1357 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 1358 |
+
|
| 1359 |
+
"""
|
| 1360 |
+
if not u:
|
| 1361 |
+
return dup_revert(f, g, K)
|
| 1362 |
+
else:
|
| 1363 |
+
raise MultivariatePolynomialError(f, g)
|
vllm/lib/python3.10/site-packages/sympy/polys/dispersion.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import S
|
| 2 |
+
from sympy.polys import Poly
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def dispersionset(p, q=None, *gens, **args):
|
| 6 |
+
r"""Compute the *dispersion set* of two polynomials.
|
| 7 |
+
|
| 8 |
+
For two polynomials `f(x)` and `g(x)` with `\deg f > 0`
|
| 9 |
+
and `\deg g > 0` the dispersion set `\operatorname{J}(f, g)` is defined as:
|
| 10 |
+
|
| 11 |
+
.. math::
|
| 12 |
+
\operatorname{J}(f, g)
|
| 13 |
+
& := \{a \in \mathbb{N}_0 | \gcd(f(x), g(x+a)) \neq 1\} \\
|
| 14 |
+
& = \{a \in \mathbb{N}_0 | \deg \gcd(f(x), g(x+a)) \geq 1\}
|
| 15 |
+
|
| 16 |
+
For a single polynomial one defines `\operatorname{J}(f) := \operatorname{J}(f, f)`.
|
| 17 |
+
|
| 18 |
+
Examples
|
| 19 |
+
========
|
| 20 |
+
|
| 21 |
+
>>> from sympy import poly
|
| 22 |
+
>>> from sympy.polys.dispersion import dispersion, dispersionset
|
| 23 |
+
>>> from sympy.abc import x
|
| 24 |
+
|
| 25 |
+
Dispersion set and dispersion of a simple polynomial:
|
| 26 |
+
|
| 27 |
+
>>> fp = poly((x - 3)*(x + 3), x)
|
| 28 |
+
>>> sorted(dispersionset(fp))
|
| 29 |
+
[0, 6]
|
| 30 |
+
>>> dispersion(fp)
|
| 31 |
+
6
|
| 32 |
+
|
| 33 |
+
Note that the definition of the dispersion is not symmetric:
|
| 34 |
+
|
| 35 |
+
>>> fp = poly(x**4 - 3*x**2 + 1, x)
|
| 36 |
+
>>> gp = fp.shift(-3)
|
| 37 |
+
>>> sorted(dispersionset(fp, gp))
|
| 38 |
+
[2, 3, 4]
|
| 39 |
+
>>> dispersion(fp, gp)
|
| 40 |
+
4
|
| 41 |
+
>>> sorted(dispersionset(gp, fp))
|
| 42 |
+
[]
|
| 43 |
+
>>> dispersion(gp, fp)
|
| 44 |
+
-oo
|
| 45 |
+
|
| 46 |
+
Computing the dispersion also works over field extensions:
|
| 47 |
+
|
| 48 |
+
>>> from sympy import sqrt
|
| 49 |
+
>>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>')
|
| 50 |
+
>>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')
|
| 51 |
+
>>> sorted(dispersionset(fp, gp))
|
| 52 |
+
[2]
|
| 53 |
+
>>> sorted(dispersionset(gp, fp))
|
| 54 |
+
[1, 4]
|
| 55 |
+
|
| 56 |
+
We can even perform the computations for polynomials
|
| 57 |
+
having symbolic coefficients:
|
| 58 |
+
|
| 59 |
+
>>> from sympy.abc import a
|
| 60 |
+
>>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x)
|
| 61 |
+
>>> sorted(dispersionset(fp))
|
| 62 |
+
[0, 1]
|
| 63 |
+
|
| 64 |
+
See Also
|
| 65 |
+
========
|
| 66 |
+
|
| 67 |
+
dispersion
|
| 68 |
+
|
| 69 |
+
References
|
| 70 |
+
==========
|
| 71 |
+
|
| 72 |
+
.. [1] [ManWright94]_
|
| 73 |
+
.. [2] [Koepf98]_
|
| 74 |
+
.. [3] [Abramov71]_
|
| 75 |
+
.. [4] [Man93]_
|
| 76 |
+
"""
|
| 77 |
+
# Check for valid input
|
| 78 |
+
same = False if q is not None else True
|
| 79 |
+
if same:
|
| 80 |
+
q = p
|
| 81 |
+
|
| 82 |
+
p = Poly(p, *gens, **args)
|
| 83 |
+
q = Poly(q, *gens, **args)
|
| 84 |
+
|
| 85 |
+
if not p.is_univariate or not q.is_univariate:
|
| 86 |
+
raise ValueError("Polynomials need to be univariate")
|
| 87 |
+
|
| 88 |
+
# The generator
|
| 89 |
+
if not p.gen == q.gen:
|
| 90 |
+
raise ValueError("Polynomials must have the same generator")
|
| 91 |
+
gen = p.gen
|
| 92 |
+
|
| 93 |
+
# We define the dispersion of constant polynomials to be zero
|
| 94 |
+
if p.degree() < 1 or q.degree() < 1:
|
| 95 |
+
return {0}
|
| 96 |
+
|
| 97 |
+
# Factor p and q over the rationals
|
| 98 |
+
fp = p.factor_list()
|
| 99 |
+
fq = q.factor_list() if not same else fp
|
| 100 |
+
|
| 101 |
+
# Iterate over all pairs of factors
|
| 102 |
+
J = set()
|
| 103 |
+
for s, unused in fp[1]:
|
| 104 |
+
for t, unused in fq[1]:
|
| 105 |
+
m = s.degree()
|
| 106 |
+
n = t.degree()
|
| 107 |
+
if n != m:
|
| 108 |
+
continue
|
| 109 |
+
an = s.LC()
|
| 110 |
+
bn = t.LC()
|
| 111 |
+
if not (an - bn).is_zero:
|
| 112 |
+
continue
|
| 113 |
+
# Note that the roles of `s` and `t` below are switched
|
| 114 |
+
# w.r.t. the original paper. This is for consistency
|
| 115 |
+
# with the description in the book of W. Koepf.
|
| 116 |
+
anm1 = s.coeff_monomial(gen**(m-1))
|
| 117 |
+
bnm1 = t.coeff_monomial(gen**(n-1))
|
| 118 |
+
alpha = (anm1 - bnm1) / S(n*bn)
|
| 119 |
+
if not alpha.is_integer:
|
| 120 |
+
continue
|
| 121 |
+
if alpha < 0 or alpha in J:
|
| 122 |
+
continue
|
| 123 |
+
if n > 1 and not (s - t.shift(alpha)).is_zero:
|
| 124 |
+
continue
|
| 125 |
+
J.add(alpha)
|
| 126 |
+
|
| 127 |
+
return J
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def dispersion(p, q=None, *gens, **args):
|
| 131 |
+
r"""Compute the *dispersion* of polynomials.
|
| 132 |
+
|
| 133 |
+
For two polynomials `f(x)` and `g(x)` with `\deg f > 0`
|
| 134 |
+
and `\deg g > 0` the dispersion `\operatorname{dis}(f, g)` is defined as:
|
| 135 |
+
|
| 136 |
+
.. math::
|
| 137 |
+
\operatorname{dis}(f, g)
|
| 138 |
+
& := \max\{ J(f,g) \cup \{0\} \} \\
|
| 139 |
+
& = \max\{ \{a \in \mathbb{N} | \gcd(f(x), g(x+a)) \neq 1\} \cup \{0\} \}
|
| 140 |
+
|
| 141 |
+
and for a single polynomial `\operatorname{dis}(f) := \operatorname{dis}(f, f)`.
|
| 142 |
+
Note that we make the definition `\max\{\} := -\infty`.
|
| 143 |
+
|
| 144 |
+
Examples
|
| 145 |
+
========
|
| 146 |
+
|
| 147 |
+
>>> from sympy import poly
|
| 148 |
+
>>> from sympy.polys.dispersion import dispersion, dispersionset
|
| 149 |
+
>>> from sympy.abc import x
|
| 150 |
+
|
| 151 |
+
Dispersion set and dispersion of a simple polynomial:
|
| 152 |
+
|
| 153 |
+
>>> fp = poly((x - 3)*(x + 3), x)
|
| 154 |
+
>>> sorted(dispersionset(fp))
|
| 155 |
+
[0, 6]
|
| 156 |
+
>>> dispersion(fp)
|
| 157 |
+
6
|
| 158 |
+
|
| 159 |
+
Note that the definition of the dispersion is not symmetric:
|
| 160 |
+
|
| 161 |
+
>>> fp = poly(x**4 - 3*x**2 + 1, x)
|
| 162 |
+
>>> gp = fp.shift(-3)
|
| 163 |
+
>>> sorted(dispersionset(fp, gp))
|
| 164 |
+
[2, 3, 4]
|
| 165 |
+
>>> dispersion(fp, gp)
|
| 166 |
+
4
|
| 167 |
+
>>> sorted(dispersionset(gp, fp))
|
| 168 |
+
[]
|
| 169 |
+
>>> dispersion(gp, fp)
|
| 170 |
+
-oo
|
| 171 |
+
|
| 172 |
+
The maximum of an empty set is defined to be `-\infty`
|
| 173 |
+
as seen in this example.
|
| 174 |
+
|
| 175 |
+
Computing the dispersion also works over field extensions:
|
| 176 |
+
|
| 177 |
+
>>> from sympy import sqrt
|
| 178 |
+
>>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>')
|
| 179 |
+
>>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')
|
| 180 |
+
>>> sorted(dispersionset(fp, gp))
|
| 181 |
+
[2]
|
| 182 |
+
>>> sorted(dispersionset(gp, fp))
|
| 183 |
+
[1, 4]
|
| 184 |
+
|
| 185 |
+
We can even perform the computations for polynomials
|
| 186 |
+
having symbolic coefficients:
|
| 187 |
+
|
| 188 |
+
>>> from sympy.abc import a
|
| 189 |
+
>>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x)
|
| 190 |
+
>>> sorted(dispersionset(fp))
|
| 191 |
+
[0, 1]
|
| 192 |
+
|
| 193 |
+
See Also
|
| 194 |
+
========
|
| 195 |
+
|
| 196 |
+
dispersionset
|
| 197 |
+
|
| 198 |
+
References
|
| 199 |
+
==========
|
| 200 |
+
|
| 201 |
+
.. [1] [ManWright94]_
|
| 202 |
+
.. [2] [Koepf98]_
|
| 203 |
+
.. [3] [Abramov71]_
|
| 204 |
+
.. [4] [Man93]_
|
| 205 |
+
"""
|
| 206 |
+
J = dispersionset(p, q, *gens, **args)
|
| 207 |
+
if not J:
|
| 208 |
+
# Definition for maximum of empty set
|
| 209 |
+
j = S.NegativeInfinity
|
| 210 |
+
else:
|
| 211 |
+
j = max(J)
|
| 212 |
+
return j
|
vllm/lib/python3.10/site-packages/sympy/polys/distributedmodules.py
ADDED
|
@@ -0,0 +1,739 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
r"""
|
| 2 |
+
Sparse distributed elements of free modules over multivariate (generalized)
|
| 3 |
+
polynomial rings.
|
| 4 |
+
|
| 5 |
+
This code and its data structures are very much like the distributed
|
| 6 |
+
polynomials, except that the first "exponent" of the monomial is
|
| 7 |
+
a module generator index. That is, the multi-exponent ``(i, e_1, ..., e_n)``
|
| 8 |
+
represents the "monomial" `x_1^{e_1} \cdots x_n^{e_n} f_i` of the free module
|
| 9 |
+
`F` generated by `f_1, \ldots, f_r` over (a localization of) the ring
|
| 10 |
+
`K[x_1, \ldots, x_n]`. A module element is simply stored as a list of terms
|
| 11 |
+
ordered by the monomial order. Here a term is a pair of a multi-exponent and a
|
| 12 |
+
coefficient. In general, this coefficient should never be zero (since it can
|
| 13 |
+
then be omitted). The zero module element is stored as an empty list.
|
| 14 |
+
|
| 15 |
+
The main routines are ``sdm_nf_mora`` and ``sdm_groebner`` which can be used
|
| 16 |
+
to compute, respectively, weak normal forms and standard bases. They work with
|
| 17 |
+
arbitrary (not necessarily global) monomial orders.
|
| 18 |
+
|
| 19 |
+
In general, product orders have to be used to construct valid monomial orders
|
| 20 |
+
for modules. However, ``lex`` can be used as-is.
|
| 21 |
+
|
| 22 |
+
Note that the "level" (number of variables, i.e. parameter u+1 in
|
| 23 |
+
distributedpolys.py) is never needed in this code.
|
| 24 |
+
|
| 25 |
+
The main reference for this file is [SCA],
|
| 26 |
+
"A Singular Introduction to Commutative Algebra".
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
from itertools import permutations
|
| 31 |
+
|
| 32 |
+
from sympy.polys.monomials import (
|
| 33 |
+
monomial_mul, monomial_lcm, monomial_div, monomial_deg
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
from sympy.polys.polytools import Poly
|
| 37 |
+
from sympy.polys.polyutils import parallel_dict_from_expr
|
| 38 |
+
from sympy.core.singleton import S
|
| 39 |
+
from sympy.core.sympify import sympify
|
| 40 |
+
|
| 41 |
+
# Additional monomial tools.
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def sdm_monomial_mul(M, X):
|
| 45 |
+
"""
|
| 46 |
+
Multiply tuple ``X`` representing a monomial of `K[X]` into the tuple
|
| 47 |
+
``M`` representing a monomial of `F`.
|
| 48 |
+
|
| 49 |
+
Examples
|
| 50 |
+
========
|
| 51 |
+
|
| 52 |
+
Multiplying `xy^3` into `x f_1` yields `x^2 y^3 f_1`:
|
| 53 |
+
|
| 54 |
+
>>> from sympy.polys.distributedmodules import sdm_monomial_mul
|
| 55 |
+
>>> sdm_monomial_mul((1, 1, 0), (1, 3))
|
| 56 |
+
(1, 2, 3)
|
| 57 |
+
"""
|
| 58 |
+
return (M[0],) + monomial_mul(X, M[1:])
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def sdm_monomial_deg(M):
|
| 62 |
+
"""
|
| 63 |
+
Return the total degree of ``M``.
|
| 64 |
+
|
| 65 |
+
Examples
|
| 66 |
+
========
|
| 67 |
+
|
| 68 |
+
For example, the total degree of `x^2 y f_5` is 3:
|
| 69 |
+
|
| 70 |
+
>>> from sympy.polys.distributedmodules import sdm_monomial_deg
|
| 71 |
+
>>> sdm_monomial_deg((5, 2, 1))
|
| 72 |
+
3
|
| 73 |
+
"""
|
| 74 |
+
return monomial_deg(M[1:])
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def sdm_monomial_lcm(A, B):
|
| 78 |
+
r"""
|
| 79 |
+
Return the "least common multiple" of ``A`` and ``B``.
|
| 80 |
+
|
| 81 |
+
IF `A = M e_j` and `B = N e_j`, where `M` and `N` are polynomial monomials,
|
| 82 |
+
this returns `\lcm(M, N) e_j`. Note that ``A`` and ``B`` involve distinct
|
| 83 |
+
monomials.
|
| 84 |
+
|
| 85 |
+
Otherwise the result is undefined.
|
| 86 |
+
|
| 87 |
+
Examples
|
| 88 |
+
========
|
| 89 |
+
|
| 90 |
+
>>> from sympy.polys.distributedmodules import sdm_monomial_lcm
|
| 91 |
+
>>> sdm_monomial_lcm((1, 2, 3), (1, 0, 5))
|
| 92 |
+
(1, 2, 5)
|
| 93 |
+
"""
|
| 94 |
+
return (A[0],) + monomial_lcm(A[1:], B[1:])
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def sdm_monomial_divides(A, B):
|
| 98 |
+
"""
|
| 99 |
+
Does there exist a (polynomial) monomial X such that XA = B?
|
| 100 |
+
|
| 101 |
+
Examples
|
| 102 |
+
========
|
| 103 |
+
|
| 104 |
+
Positive examples:
|
| 105 |
+
|
| 106 |
+
In the following examples, the monomial is given in terms of x, y and the
|
| 107 |
+
generator(s), f_1, f_2 etc. The tuple form of that monomial is used in
|
| 108 |
+
the call to sdm_monomial_divides.
|
| 109 |
+
Note: the generator appears last in the expression but first in the tuple
|
| 110 |
+
and other factors appear in the same order that they appear in the monomial
|
| 111 |
+
expression.
|
| 112 |
+
|
| 113 |
+
`A = f_1` divides `B = f_1`
|
| 114 |
+
|
| 115 |
+
>>> from sympy.polys.distributedmodules import sdm_monomial_divides
|
| 116 |
+
>>> sdm_monomial_divides((1, 0, 0), (1, 0, 0))
|
| 117 |
+
True
|
| 118 |
+
|
| 119 |
+
`A = f_1` divides `B = x^2 y f_1`
|
| 120 |
+
|
| 121 |
+
>>> sdm_monomial_divides((1, 0, 0), (1, 2, 1))
|
| 122 |
+
True
|
| 123 |
+
|
| 124 |
+
`A = xy f_5` divides `B = x^2 y f_5`
|
| 125 |
+
|
| 126 |
+
>>> sdm_monomial_divides((5, 1, 1), (5, 2, 1))
|
| 127 |
+
True
|
| 128 |
+
|
| 129 |
+
Negative examples:
|
| 130 |
+
|
| 131 |
+
`A = f_1` does not divide `B = f_2`
|
| 132 |
+
|
| 133 |
+
>>> sdm_monomial_divides((1, 0, 0), (2, 0, 0))
|
| 134 |
+
False
|
| 135 |
+
|
| 136 |
+
`A = x f_1` does not divide `B = f_1`
|
| 137 |
+
|
| 138 |
+
>>> sdm_monomial_divides((1, 1, 0), (1, 0, 0))
|
| 139 |
+
False
|
| 140 |
+
|
| 141 |
+
`A = xy^2 f_5` does not divide `B = y f_5`
|
| 142 |
+
|
| 143 |
+
>>> sdm_monomial_divides((5, 1, 2), (5, 0, 1))
|
| 144 |
+
False
|
| 145 |
+
"""
|
| 146 |
+
return A[0] == B[0] and all(a <= b for a, b in zip(A[1:], B[1:]))
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# The actual distributed modules code.
|
| 150 |
+
|
| 151 |
+
def sdm_LC(f, K):
|
| 152 |
+
"""Returns the leading coefficient of ``f``. """
|
| 153 |
+
if not f:
|
| 154 |
+
return K.zero
|
| 155 |
+
else:
|
| 156 |
+
return f[0][1]
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def sdm_to_dict(f):
|
| 160 |
+
"""Make a dictionary from a distributed polynomial. """
|
| 161 |
+
return dict(f)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def sdm_from_dict(d, O):
|
| 165 |
+
"""
|
| 166 |
+
Create an sdm from a dictionary.
|
| 167 |
+
|
| 168 |
+
Here ``O`` is the monomial order to use.
|
| 169 |
+
|
| 170 |
+
Examples
|
| 171 |
+
========
|
| 172 |
+
|
| 173 |
+
>>> from sympy.polys.distributedmodules import sdm_from_dict
|
| 174 |
+
>>> from sympy.polys import QQ, lex
|
| 175 |
+
>>> dic = {(1, 1, 0): QQ(1), (1, 0, 0): QQ(2), (0, 1, 0): QQ(0)}
|
| 176 |
+
>>> sdm_from_dict(dic, lex)
|
| 177 |
+
[((1, 1, 0), 1), ((1, 0, 0), 2)]
|
| 178 |
+
"""
|
| 179 |
+
return sdm_strip(sdm_sort(list(d.items()), O))
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def sdm_sort(f, O):
|
| 183 |
+
"""Sort terms in ``f`` using the given monomial order ``O``. """
|
| 184 |
+
return sorted(f, key=lambda term: O(term[0]), reverse=True)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def sdm_strip(f):
|
| 188 |
+
"""Remove terms with zero coefficients from ``f`` in ``K[X]``. """
|
| 189 |
+
return [ (monom, coeff) for monom, coeff in f if coeff ]
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
def sdm_add(f, g, O, K):
|
| 193 |
+
"""
|
| 194 |
+
Add two module elements ``f``, ``g``.
|
| 195 |
+
|
| 196 |
+
Addition is done over the ground field ``K``, monomials are ordered
|
| 197 |
+
according to ``O``.
|
| 198 |
+
|
| 199 |
+
Examples
|
| 200 |
+
========
|
| 201 |
+
|
| 202 |
+
All examples use lexicographic order.
|
| 203 |
+
|
| 204 |
+
`(xy f_1) + (f_2) = f_2 + xy f_1`
|
| 205 |
+
|
| 206 |
+
>>> from sympy.polys.distributedmodules import sdm_add
|
| 207 |
+
>>> from sympy.polys import lex, QQ
|
| 208 |
+
>>> sdm_add([((1, 1, 1), QQ(1))], [((2, 0, 0), QQ(1))], lex, QQ)
|
| 209 |
+
[((2, 0, 0), 1), ((1, 1, 1), 1)]
|
| 210 |
+
|
| 211 |
+
`(xy f_1) + (-xy f_1)` = 0`
|
| 212 |
+
|
| 213 |
+
>>> sdm_add([((1, 1, 1), QQ(1))], [((1, 1, 1), QQ(-1))], lex, QQ)
|
| 214 |
+
[]
|
| 215 |
+
|
| 216 |
+
`(f_1) + (2f_1) = 3f_1`
|
| 217 |
+
|
| 218 |
+
>>> sdm_add([((1, 0, 0), QQ(1))], [((1, 0, 0), QQ(2))], lex, QQ)
|
| 219 |
+
[((1, 0, 0), 3)]
|
| 220 |
+
|
| 221 |
+
`(yf_1) + (xf_1) = xf_1 + yf_1`
|
| 222 |
+
|
| 223 |
+
>>> sdm_add([((1, 0, 1), QQ(1))], [((1, 1, 0), QQ(1))], lex, QQ)
|
| 224 |
+
[((1, 1, 0), 1), ((1, 0, 1), 1)]
|
| 225 |
+
"""
|
| 226 |
+
h = dict(f)
|
| 227 |
+
|
| 228 |
+
for monom, c in g:
|
| 229 |
+
if monom in h:
|
| 230 |
+
coeff = h[monom] + c
|
| 231 |
+
|
| 232 |
+
if not coeff:
|
| 233 |
+
del h[monom]
|
| 234 |
+
else:
|
| 235 |
+
h[monom] = coeff
|
| 236 |
+
else:
|
| 237 |
+
h[monom] = c
|
| 238 |
+
|
| 239 |
+
return sdm_from_dict(h, O)
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
def sdm_LM(f):
|
| 243 |
+
r"""
|
| 244 |
+
Returns the leading monomial of ``f``.
|
| 245 |
+
|
| 246 |
+
Only valid if `f \ne 0`.
|
| 247 |
+
|
| 248 |
+
Examples
|
| 249 |
+
========
|
| 250 |
+
|
| 251 |
+
>>> from sympy.polys.distributedmodules import sdm_LM, sdm_from_dict
|
| 252 |
+
>>> from sympy.polys import QQ, lex
|
| 253 |
+
>>> dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(1), (4, 0, 1): QQ(1)}
|
| 254 |
+
>>> sdm_LM(sdm_from_dict(dic, lex))
|
| 255 |
+
(4, 0, 1)
|
| 256 |
+
"""
|
| 257 |
+
return f[0][0]
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
def sdm_LT(f):
|
| 261 |
+
r"""
|
| 262 |
+
Returns the leading term of ``f``.
|
| 263 |
+
|
| 264 |
+
Only valid if `f \ne 0`.
|
| 265 |
+
|
| 266 |
+
Examples
|
| 267 |
+
========
|
| 268 |
+
|
| 269 |
+
>>> from sympy.polys.distributedmodules import sdm_LT, sdm_from_dict
|
| 270 |
+
>>> from sympy.polys import QQ, lex
|
| 271 |
+
>>> dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(2), (4, 0, 1): QQ(3)}
|
| 272 |
+
>>> sdm_LT(sdm_from_dict(dic, lex))
|
| 273 |
+
((4, 0, 1), 3)
|
| 274 |
+
"""
|
| 275 |
+
return f[0]
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
def sdm_mul_term(f, term, O, K):
|
| 279 |
+
"""
|
| 280 |
+
Multiply a distributed module element ``f`` by a (polynomial) term ``term``.
|
| 281 |
+
|
| 282 |
+
Multiplication of coefficients is done over the ground field ``K``, and
|
| 283 |
+
monomials are ordered according to ``O``.
|
| 284 |
+
|
| 285 |
+
Examples
|
| 286 |
+
========
|
| 287 |
+
|
| 288 |
+
`0 f_1 = 0`
|
| 289 |
+
|
| 290 |
+
>>> from sympy.polys.distributedmodules import sdm_mul_term
|
| 291 |
+
>>> from sympy.polys import lex, QQ
|
| 292 |
+
>>> sdm_mul_term([((1, 0, 0), QQ(1))], ((0, 0), QQ(0)), lex, QQ)
|
| 293 |
+
[]
|
| 294 |
+
|
| 295 |
+
`x 0 = 0`
|
| 296 |
+
|
| 297 |
+
>>> sdm_mul_term([], ((1, 0), QQ(1)), lex, QQ)
|
| 298 |
+
[]
|
| 299 |
+
|
| 300 |
+
`(x) (f_1) = xf_1`
|
| 301 |
+
|
| 302 |
+
>>> sdm_mul_term([((1, 0, 0), QQ(1))], ((1, 0), QQ(1)), lex, QQ)
|
| 303 |
+
[((1, 1, 0), 1)]
|
| 304 |
+
|
| 305 |
+
`(2xy) (3x f_1 + 4y f_2) = 8xy^2 f_2 + 6x^2y f_1`
|
| 306 |
+
|
| 307 |
+
>>> f = [((2, 0, 1), QQ(4)), ((1, 1, 0), QQ(3))]
|
| 308 |
+
>>> sdm_mul_term(f, ((1, 1), QQ(2)), lex, QQ)
|
| 309 |
+
[((2, 1, 2), 8), ((1, 2, 1), 6)]
|
| 310 |
+
"""
|
| 311 |
+
X, c = term
|
| 312 |
+
|
| 313 |
+
if not f or not c:
|
| 314 |
+
return []
|
| 315 |
+
else:
|
| 316 |
+
if K.is_one(c):
|
| 317 |
+
return [ (sdm_monomial_mul(f_M, X), f_c) for f_M, f_c in f ]
|
| 318 |
+
else:
|
| 319 |
+
return [ (sdm_monomial_mul(f_M, X), f_c * c) for f_M, f_c in f ]
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
def sdm_zero():
|
| 323 |
+
"""Return the zero module element."""
|
| 324 |
+
return []
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def sdm_deg(f):
|
| 328 |
+
"""
|
| 329 |
+
Degree of ``f``.
|
| 330 |
+
|
| 331 |
+
This is the maximum of the degrees of all its monomials.
|
| 332 |
+
Invalid if ``f`` is zero.
|
| 333 |
+
|
| 334 |
+
Examples
|
| 335 |
+
========
|
| 336 |
+
|
| 337 |
+
>>> from sympy.polys.distributedmodules import sdm_deg
|
| 338 |
+
>>> sdm_deg([((1, 2, 3), 1), ((10, 0, 1), 1), ((2, 3, 4), 4)])
|
| 339 |
+
7
|
| 340 |
+
"""
|
| 341 |
+
return max(sdm_monomial_deg(M[0]) for M in f)
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
# Conversion
|
| 345 |
+
|
| 346 |
+
def sdm_from_vector(vec, O, K, **opts):
|
| 347 |
+
"""
|
| 348 |
+
Create an sdm from an iterable of expressions.
|
| 349 |
+
|
| 350 |
+
Coefficients are created in the ground field ``K``, and terms are ordered
|
| 351 |
+
according to monomial order ``O``. Named arguments are passed on to the
|
| 352 |
+
polys conversion code and can be used to specify for example generators.
|
| 353 |
+
|
| 354 |
+
Examples
|
| 355 |
+
========
|
| 356 |
+
|
| 357 |
+
>>> from sympy.polys.distributedmodules import sdm_from_vector
|
| 358 |
+
>>> from sympy.abc import x, y, z
|
| 359 |
+
>>> from sympy.polys import QQ, lex
|
| 360 |
+
>>> sdm_from_vector([x**2+y**2, 2*z], lex, QQ)
|
| 361 |
+
[((1, 0, 0, 1), 2), ((0, 2, 0, 0), 1), ((0, 0, 2, 0), 1)]
|
| 362 |
+
"""
|
| 363 |
+
dics, gens = parallel_dict_from_expr(sympify(vec), **opts)
|
| 364 |
+
dic = {}
|
| 365 |
+
for i, d in enumerate(dics):
|
| 366 |
+
for k, v in d.items():
|
| 367 |
+
dic[(i,) + k] = K.convert(v)
|
| 368 |
+
return sdm_from_dict(dic, O)
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
def sdm_to_vector(f, gens, K, n=None):
|
| 372 |
+
"""
|
| 373 |
+
Convert sdm ``f`` into a list of polynomial expressions.
|
| 374 |
+
|
| 375 |
+
The generators for the polynomial ring are specified via ``gens``. The rank
|
| 376 |
+
of the module is guessed, or passed via ``n``. The ground field is assumed
|
| 377 |
+
to be ``K``.
|
| 378 |
+
|
| 379 |
+
Examples
|
| 380 |
+
========
|
| 381 |
+
|
| 382 |
+
>>> from sympy.polys.distributedmodules import sdm_to_vector
|
| 383 |
+
>>> from sympy.abc import x, y, z
|
| 384 |
+
>>> from sympy.polys import QQ
|
| 385 |
+
>>> f = [((1, 0, 0, 1), QQ(2)), ((0, 2, 0, 0), QQ(1)), ((0, 0, 2, 0), QQ(1))]
|
| 386 |
+
>>> sdm_to_vector(f, [x, y, z], QQ)
|
| 387 |
+
[x**2 + y**2, 2*z]
|
| 388 |
+
"""
|
| 389 |
+
dic = sdm_to_dict(f)
|
| 390 |
+
dics = {}
|
| 391 |
+
for k, v in dic.items():
|
| 392 |
+
dics.setdefault(k[0], []).append((k[1:], v))
|
| 393 |
+
n = n or len(dics)
|
| 394 |
+
res = []
|
| 395 |
+
for k in range(n):
|
| 396 |
+
if k in dics:
|
| 397 |
+
res.append(Poly(dict(dics[k]), gens=gens, domain=K).as_expr())
|
| 398 |
+
else:
|
| 399 |
+
res.append(S.Zero)
|
| 400 |
+
return res
|
| 401 |
+
|
| 402 |
+
# Algorithms.
|
| 403 |
+
|
| 404 |
+
|
| 405 |
+
def sdm_spoly(f, g, O, K, phantom=None):
|
| 406 |
+
"""
|
| 407 |
+
Compute the generalized s-polynomial of ``f`` and ``g``.
|
| 408 |
+
|
| 409 |
+
The ground field is assumed to be ``K``, and monomials ordered according to
|
| 410 |
+
``O``.
|
| 411 |
+
|
| 412 |
+
This is invalid if either of ``f`` or ``g`` is zero.
|
| 413 |
+
|
| 414 |
+
If the leading terms of `f` and `g` involve different basis elements of
|
| 415 |
+
`F`, their s-poly is defined to be zero. Otherwise it is a certain linear
|
| 416 |
+
combination of `f` and `g` in which the leading terms cancel.
|
| 417 |
+
See [SCA, defn 2.3.6] for details.
|
| 418 |
+
|
| 419 |
+
If ``phantom`` is not ``None``, it should be a pair of module elements on
|
| 420 |
+
which to perform the same operation(s) as on ``f`` and ``g``. The in this
|
| 421 |
+
case both results are returned.
|
| 422 |
+
|
| 423 |
+
Examples
|
| 424 |
+
========
|
| 425 |
+
|
| 426 |
+
>>> from sympy.polys.distributedmodules import sdm_spoly
|
| 427 |
+
>>> from sympy.polys import QQ, lex
|
| 428 |
+
>>> f = [((2, 1, 1), QQ(1)), ((1, 0, 1), QQ(1))]
|
| 429 |
+
>>> g = [((2, 3, 0), QQ(1))]
|
| 430 |
+
>>> h = [((1, 2, 3), QQ(1))]
|
| 431 |
+
>>> sdm_spoly(f, h, lex, QQ)
|
| 432 |
+
[]
|
| 433 |
+
>>> sdm_spoly(f, g, lex, QQ)
|
| 434 |
+
[((1, 2, 1), 1)]
|
| 435 |
+
"""
|
| 436 |
+
if not f or not g:
|
| 437 |
+
return sdm_zero()
|
| 438 |
+
LM1 = sdm_LM(f)
|
| 439 |
+
LM2 = sdm_LM(g)
|
| 440 |
+
if LM1[0] != LM2[0]:
|
| 441 |
+
return sdm_zero()
|
| 442 |
+
LM1 = LM1[1:]
|
| 443 |
+
LM2 = LM2[1:]
|
| 444 |
+
lcm = monomial_lcm(LM1, LM2)
|
| 445 |
+
m1 = monomial_div(lcm, LM1)
|
| 446 |
+
m2 = monomial_div(lcm, LM2)
|
| 447 |
+
c = K.quo(-sdm_LC(f, K), sdm_LC(g, K))
|
| 448 |
+
r1 = sdm_add(sdm_mul_term(f, (m1, K.one), O, K),
|
| 449 |
+
sdm_mul_term(g, (m2, c), O, K), O, K)
|
| 450 |
+
if phantom is None:
|
| 451 |
+
return r1
|
| 452 |
+
r2 = sdm_add(sdm_mul_term(phantom[0], (m1, K.one), O, K),
|
| 453 |
+
sdm_mul_term(phantom[1], (m2, c), O, K), O, K)
|
| 454 |
+
return r1, r2
|
| 455 |
+
|
| 456 |
+
|
| 457 |
+
def sdm_ecart(f):
|
| 458 |
+
"""
|
| 459 |
+
Compute the ecart of ``f``.
|
| 460 |
+
|
| 461 |
+
This is defined to be the difference of the total degree of `f` and the
|
| 462 |
+
total degree of the leading monomial of `f` [SCA, defn 2.3.7].
|
| 463 |
+
|
| 464 |
+
Invalid if f is zero.
|
| 465 |
+
|
| 466 |
+
Examples
|
| 467 |
+
========
|
| 468 |
+
|
| 469 |
+
>>> from sympy.polys.distributedmodules import sdm_ecart
|
| 470 |
+
>>> sdm_ecart([((1, 2, 3), 1), ((1, 0, 1), 1)])
|
| 471 |
+
0
|
| 472 |
+
>>> sdm_ecart([((2, 2, 1), 1), ((1, 5, 1), 1)])
|
| 473 |
+
3
|
| 474 |
+
"""
|
| 475 |
+
return sdm_deg(f) - sdm_monomial_deg(sdm_LM(f))
|
| 476 |
+
|
| 477 |
+
|
| 478 |
+
def sdm_nf_mora(f, G, O, K, phantom=None):
|
| 479 |
+
r"""
|
| 480 |
+
Compute a weak normal form of ``f`` with respect to ``G`` and order ``O``.
|
| 481 |
+
|
| 482 |
+
The ground field is assumed to be ``K``, and monomials ordered according to
|
| 483 |
+
``O``.
|
| 484 |
+
|
| 485 |
+
Weak normal forms are defined in [SCA, defn 2.3.3]. They are not unique.
|
| 486 |
+
This function deterministically computes a weak normal form, depending on
|
| 487 |
+
the order of `G`.
|
| 488 |
+
|
| 489 |
+
The most important property of a weak normal form is the following: if
|
| 490 |
+
`R` is the ring associated with the monomial ordering (if the ordering is
|
| 491 |
+
global, we just have `R = K[x_1, \ldots, x_n]`, otherwise it is a certain
|
| 492 |
+
localization thereof), `I` any ideal of `R` and `G` a standard basis for
|
| 493 |
+
`I`, then for any `f \in R`, we have `f \in I` if and only if
|
| 494 |
+
`NF(f | G) = 0`.
|
| 495 |
+
|
| 496 |
+
This is the generalized Mora algorithm for computing weak normal forms with
|
| 497 |
+
respect to arbitrary monomial orders [SCA, algorithm 2.3.9].
|
| 498 |
+
|
| 499 |
+
If ``phantom`` is not ``None``, it should be a pair of "phantom" arguments
|
| 500 |
+
on which to perform the same computations as on ``f``, ``G``, both results
|
| 501 |
+
are then returned.
|
| 502 |
+
"""
|
| 503 |
+
from itertools import repeat
|
| 504 |
+
h = f
|
| 505 |
+
T = list(G)
|
| 506 |
+
if phantom is not None:
|
| 507 |
+
# "phantom" variables with suffix p
|
| 508 |
+
hp = phantom[0]
|
| 509 |
+
Tp = list(phantom[1])
|
| 510 |
+
phantom = True
|
| 511 |
+
else:
|
| 512 |
+
Tp = repeat([])
|
| 513 |
+
phantom = False
|
| 514 |
+
while h:
|
| 515 |
+
# TODO better data structure!!!
|
| 516 |
+
Th = [(g, sdm_ecart(g), gp) for g, gp in zip(T, Tp)
|
| 517 |
+
if sdm_monomial_divides(sdm_LM(g), sdm_LM(h))]
|
| 518 |
+
if not Th:
|
| 519 |
+
break
|
| 520 |
+
g, _, gp = min(Th, key=lambda x: x[1])
|
| 521 |
+
if sdm_ecart(g) > sdm_ecart(h):
|
| 522 |
+
T.append(h)
|
| 523 |
+
if phantom:
|
| 524 |
+
Tp.append(hp)
|
| 525 |
+
if phantom:
|
| 526 |
+
h, hp = sdm_spoly(h, g, O, K, phantom=(hp, gp))
|
| 527 |
+
else:
|
| 528 |
+
h = sdm_spoly(h, g, O, K)
|
| 529 |
+
if phantom:
|
| 530 |
+
return h, hp
|
| 531 |
+
return h
|
| 532 |
+
|
| 533 |
+
|
| 534 |
+
def sdm_nf_buchberger(f, G, O, K, phantom=None):
|
| 535 |
+
r"""
|
| 536 |
+
Compute a weak normal form of ``f`` with respect to ``G`` and order ``O``.
|
| 537 |
+
|
| 538 |
+
The ground field is assumed to be ``K``, and monomials ordered according to
|
| 539 |
+
``O``.
|
| 540 |
+
|
| 541 |
+
This is the standard Buchberger algorithm for computing weak normal forms with
|
| 542 |
+
respect to *global* monomial orders [SCA, algorithm 1.6.10].
|
| 543 |
+
|
| 544 |
+
If ``phantom`` is not ``None``, it should be a pair of "phantom" arguments
|
| 545 |
+
on which to perform the same computations as on ``f``, ``G``, both results
|
| 546 |
+
are then returned.
|
| 547 |
+
"""
|
| 548 |
+
from itertools import repeat
|
| 549 |
+
h = f
|
| 550 |
+
T = list(G)
|
| 551 |
+
if phantom is not None:
|
| 552 |
+
# "phantom" variables with suffix p
|
| 553 |
+
hp = phantom[0]
|
| 554 |
+
Tp = list(phantom[1])
|
| 555 |
+
phantom = True
|
| 556 |
+
else:
|
| 557 |
+
Tp = repeat([])
|
| 558 |
+
phantom = False
|
| 559 |
+
while h:
|
| 560 |
+
try:
|
| 561 |
+
g, gp = next((g, gp) for g, gp in zip(T, Tp)
|
| 562 |
+
if sdm_monomial_divides(sdm_LM(g), sdm_LM(h)))
|
| 563 |
+
except StopIteration:
|
| 564 |
+
break
|
| 565 |
+
if phantom:
|
| 566 |
+
h, hp = sdm_spoly(h, g, O, K, phantom=(hp, gp))
|
| 567 |
+
else:
|
| 568 |
+
h = sdm_spoly(h, g, O, K)
|
| 569 |
+
if phantom:
|
| 570 |
+
return h, hp
|
| 571 |
+
return h
|
| 572 |
+
|
| 573 |
+
|
| 574 |
+
def sdm_nf_buchberger_reduced(f, G, O, K):
|
| 575 |
+
r"""
|
| 576 |
+
Compute a reduced normal form of ``f`` with respect to ``G`` and order ``O``.
|
| 577 |
+
|
| 578 |
+
The ground field is assumed to be ``K``, and monomials ordered according to
|
| 579 |
+
``O``.
|
| 580 |
+
|
| 581 |
+
In contrast to weak normal forms, reduced normal forms *are* unique, but
|
| 582 |
+
their computation is more expensive.
|
| 583 |
+
|
| 584 |
+
This is the standard Buchberger algorithm for computing reduced normal forms
|
| 585 |
+
with respect to *global* monomial orders [SCA, algorithm 1.6.11].
|
| 586 |
+
|
| 587 |
+
The ``pantom`` option is not supported, so this normal form cannot be used
|
| 588 |
+
as a normal form for the "extended" groebner algorithm.
|
| 589 |
+
"""
|
| 590 |
+
h = sdm_zero()
|
| 591 |
+
g = f
|
| 592 |
+
while g:
|
| 593 |
+
g = sdm_nf_buchberger(g, G, O, K)
|
| 594 |
+
if g:
|
| 595 |
+
h = sdm_add(h, [sdm_LT(g)], O, K)
|
| 596 |
+
g = g[1:]
|
| 597 |
+
return h
|
| 598 |
+
|
| 599 |
+
|
| 600 |
+
def sdm_groebner(G, NF, O, K, extended=False):
|
| 601 |
+
"""
|
| 602 |
+
Compute a minimal standard basis of ``G`` with respect to order ``O``.
|
| 603 |
+
|
| 604 |
+
The algorithm uses a normal form ``NF``, for example ``sdm_nf_mora``.
|
| 605 |
+
The ground field is assumed to be ``K``, and monomials ordered according
|
| 606 |
+
to ``O``.
|
| 607 |
+
|
| 608 |
+
Let `N` denote the submodule generated by elements of `G`. A standard
|
| 609 |
+
basis for `N` is a subset `S` of `N`, such that `in(S) = in(N)`, where for
|
| 610 |
+
any subset `X` of `F`, `in(X)` denotes the submodule generated by the
|
| 611 |
+
initial forms of elements of `X`. [SCA, defn 2.3.2]
|
| 612 |
+
|
| 613 |
+
A standard basis is called minimal if no subset of it is a standard basis.
|
| 614 |
+
|
| 615 |
+
One may show that standard bases are always generating sets.
|
| 616 |
+
|
| 617 |
+
Minimal standard bases are not unique. This algorithm computes a
|
| 618 |
+
deterministic result, depending on the particular order of `G`.
|
| 619 |
+
|
| 620 |
+
If ``extended=True``, also compute the transition matrix from the initial
|
| 621 |
+
generators to the groebner basis. That is, return a list of coefficient
|
| 622 |
+
vectors, expressing the elements of the groebner basis in terms of the
|
| 623 |
+
elements of ``G``.
|
| 624 |
+
|
| 625 |
+
This functions implements the "sugar" strategy, see
|
| 626 |
+
|
| 627 |
+
Giovini et al: "One sugar cube, please" OR Selection strategies in
|
| 628 |
+
Buchberger algorithm.
|
| 629 |
+
"""
|
| 630 |
+
|
| 631 |
+
# The critical pair set.
|
| 632 |
+
# A critical pair is stored as (i, j, s, t) where (i, j) defines the pair
|
| 633 |
+
# (by indexing S), s is the sugar of the pair, and t is the lcm of their
|
| 634 |
+
# leading monomials.
|
| 635 |
+
P = []
|
| 636 |
+
|
| 637 |
+
# The eventual standard basis.
|
| 638 |
+
S = []
|
| 639 |
+
Sugars = []
|
| 640 |
+
|
| 641 |
+
def Ssugar(i, j):
|
| 642 |
+
"""Compute the sugar of the S-poly corresponding to (i, j)."""
|
| 643 |
+
LMi = sdm_LM(S[i])
|
| 644 |
+
LMj = sdm_LM(S[j])
|
| 645 |
+
return max(Sugars[i] - sdm_monomial_deg(LMi),
|
| 646 |
+
Sugars[j] - sdm_monomial_deg(LMj)) \
|
| 647 |
+
+ sdm_monomial_deg(sdm_monomial_lcm(LMi, LMj))
|
| 648 |
+
|
| 649 |
+
ourkey = lambda p: (p[2], O(p[3]), p[1])
|
| 650 |
+
|
| 651 |
+
def update(f, sugar, P):
|
| 652 |
+
"""Add f with sugar ``sugar`` to S, update P."""
|
| 653 |
+
if not f:
|
| 654 |
+
return P
|
| 655 |
+
k = len(S)
|
| 656 |
+
S.append(f)
|
| 657 |
+
Sugars.append(sugar)
|
| 658 |
+
|
| 659 |
+
LMf = sdm_LM(f)
|
| 660 |
+
|
| 661 |
+
def removethis(pair):
|
| 662 |
+
i, j, s, t = pair
|
| 663 |
+
if LMf[0] != t[0]:
|
| 664 |
+
return False
|
| 665 |
+
tik = sdm_monomial_lcm(LMf, sdm_LM(S[i]))
|
| 666 |
+
tjk = sdm_monomial_lcm(LMf, sdm_LM(S[j]))
|
| 667 |
+
return tik != t and tjk != t and sdm_monomial_divides(tik, t) and \
|
| 668 |
+
sdm_monomial_divides(tjk, t)
|
| 669 |
+
# apply the chain criterion
|
| 670 |
+
P = [p for p in P if not removethis(p)]
|
| 671 |
+
|
| 672 |
+
# new-pair set
|
| 673 |
+
N = [(i, k, Ssugar(i, k), sdm_monomial_lcm(LMf, sdm_LM(S[i])))
|
| 674 |
+
for i in range(k) if LMf[0] == sdm_LM(S[i])[0]]
|
| 675 |
+
# TODO apply the product criterion?
|
| 676 |
+
N.sort(key=ourkey)
|
| 677 |
+
remove = set()
|
| 678 |
+
for i, p in enumerate(N):
|
| 679 |
+
for j in range(i + 1, len(N)):
|
| 680 |
+
if sdm_monomial_divides(p[3], N[j][3]):
|
| 681 |
+
remove.add(j)
|
| 682 |
+
|
| 683 |
+
# TODO mergesort?
|
| 684 |
+
P.extend(reversed([p for i, p in enumerate(N) if i not in remove]))
|
| 685 |
+
P.sort(key=ourkey, reverse=True)
|
| 686 |
+
# NOTE reverse-sort, because we want to pop from the end
|
| 687 |
+
return P
|
| 688 |
+
|
| 689 |
+
# Figure out the number of generators in the ground ring.
|
| 690 |
+
try:
|
| 691 |
+
# NOTE: we look for the first non-zero vector, take its first monomial
|
| 692 |
+
# the number of generators in the ring is one less than the length
|
| 693 |
+
# (since the zeroth entry is for the module generators)
|
| 694 |
+
numgens = len(next(x[0] for x in G if x)[0]) - 1
|
| 695 |
+
except StopIteration:
|
| 696 |
+
# No non-zero elements in G ...
|
| 697 |
+
if extended:
|
| 698 |
+
return [], []
|
| 699 |
+
return []
|
| 700 |
+
|
| 701 |
+
# This list will store expressions of the elements of S in terms of the
|
| 702 |
+
# initial generators
|
| 703 |
+
coefficients = []
|
| 704 |
+
|
| 705 |
+
# First add all the elements of G to S
|
| 706 |
+
for i, f in enumerate(G):
|
| 707 |
+
P = update(f, sdm_deg(f), P)
|
| 708 |
+
if extended and f:
|
| 709 |
+
coefficients.append(sdm_from_dict({(i,) + (0,)*numgens: K(1)}, O))
|
| 710 |
+
|
| 711 |
+
# Now carry out the buchberger algorithm.
|
| 712 |
+
while P:
|
| 713 |
+
i, j, s, t = P.pop()
|
| 714 |
+
f, g = S[i], S[j]
|
| 715 |
+
if extended:
|
| 716 |
+
sp, coeff = sdm_spoly(f, g, O, K,
|
| 717 |
+
phantom=(coefficients[i], coefficients[j]))
|
| 718 |
+
h, hcoeff = NF(sp, S, O, K, phantom=(coeff, coefficients))
|
| 719 |
+
if h:
|
| 720 |
+
coefficients.append(hcoeff)
|
| 721 |
+
else:
|
| 722 |
+
h = NF(sdm_spoly(f, g, O, K), S, O, K)
|
| 723 |
+
P = update(h, Ssugar(i, j), P)
|
| 724 |
+
|
| 725 |
+
# Finally interreduce the standard basis.
|
| 726 |
+
# (TODO again, better data structures)
|
| 727 |
+
S = {(tuple(f), i) for i, f in enumerate(S)}
|
| 728 |
+
for (a, ai), (b, bi) in permutations(S, 2):
|
| 729 |
+
A = sdm_LM(a)
|
| 730 |
+
B = sdm_LM(b)
|
| 731 |
+
if sdm_monomial_divides(A, B) and (b, bi) in S and (a, ai) in S:
|
| 732 |
+
S.remove((b, bi))
|
| 733 |
+
|
| 734 |
+
L = sorted(((list(f), i) for f, i in S), key=lambda p: O(sdm_LM(p[0])),
|
| 735 |
+
reverse=True)
|
| 736 |
+
res = [x[0] for x in L]
|
| 737 |
+
if extended:
|
| 738 |
+
return res, [coefficients[i] for _, i in L]
|
| 739 |
+
return res
|
vllm/lib/python3.10/site-packages/sympy/polys/domainmatrix.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Stub module to expose DomainMatrix which has now moved to
|
| 3 |
+
sympy.polys.matrices package. It should now be imported as:
|
| 4 |
+
|
| 5 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
| 6 |
+
|
| 7 |
+
This module might be removed in future.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from sympy.polys.matrices.domainmatrix import DomainMatrix
|
| 11 |
+
|
| 12 |
+
__all__ = ['DomainMatrix']
|
vllm/lib/python3.10/site-packages/sympy/polys/euclidtools.py
ADDED
|
@@ -0,0 +1,1912 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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up_prem, dmp_prem,
|
| 15 |
+
dup_mul_ground, dmp_mul_ground,
|
| 16 |
+
dmp_mul_term,
|
| 17 |
+
dup_quo_ground, dmp_quo_ground,
|
| 18 |
+
dup_max_norm, dmp_max_norm)
|
| 19 |
+
from sympy.polys.densebasic import (
|
| 20 |
+
dup_strip, dmp_raise,
|
| 21 |
+
dmp_zero, dmp_one, dmp_ground,
|
| 22 |
+
dmp_one_p, dmp_zero_p,
|
| 23 |
+
dmp_zeros,
|
| 24 |
+
dup_degree, dmp_degree, dmp_degree_in,
|
| 25 |
+
dup_LC, dmp_LC, dmp_ground_LC,
|
| 26 |
+
dmp_multi_deflate, dmp_inflate,
|
| 27 |
+
dup_convert, dmp_convert,
|
| 28 |
+
dmp_apply_pairs)
|
| 29 |
+
from sympy.polys.densetools import (
|
| 30 |
+
dup_clear_denoms, dmp_clear_denoms,
|
| 31 |
+
dup_diff, dmp_diff,
|
| 32 |
+
dup_eval, dmp_eval, dmp_eval_in,
|
| 33 |
+
dup_trunc, dmp_ground_trunc,
|
| 34 |
+
dup_monic, dmp_ground_monic,
|
| 35 |
+
dup_primitive, dmp_ground_primitive,
|
| 36 |
+
dup_extract, dmp_ground_extract)
|
| 37 |
+
from sympy.polys.galoistools import (
|
| 38 |
+
gf_int, gf_crt)
|
| 39 |
+
from sympy.polys.polyconfig import query
|
| 40 |
+
from sympy.polys.polyerrors import (
|
| 41 |
+
MultivariatePolynomialError,
|
| 42 |
+
HeuristicGCDFailed,
|
| 43 |
+
HomomorphismFailed,
|
| 44 |
+
NotInvertible,
|
| 45 |
+
DomainError)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def dup_half_gcdex(f, g, K):
|
| 51 |
+
"""
|
| 52 |
+
Half extended Euclidean algorithm in `F[x]`.
|
| 53 |
+
|
| 54 |
+
Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``.
|
| 55 |
+
|
| 56 |
+
Examples
|
| 57 |
+
========
|
| 58 |
+
|
| 59 |
+
>>> from sympy.polys import ring, QQ
|
| 60 |
+
>>> R, x = ring("x", QQ)
|
| 61 |
+
|
| 62 |
+
>>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
|
| 63 |
+
>>> g = x**3 + x**2 - 4*x - 4
|
| 64 |
+
|
| 65 |
+
>>> R.dup_half_gcdex(f, g)
|
| 66 |
+
(-1/5*x + 3/5, x + 1)
|
| 67 |
+
|
| 68 |
+
"""
|
| 69 |
+
if not K.is_Field:
|
| 70 |
+
raise DomainError("Cannot compute half extended GCD over %s" % K)
|
| 71 |
+
|
| 72 |
+
a, b = [K.one], []
|
| 73 |
+
|
| 74 |
+
while g:
|
| 75 |
+
q, r = dup_div(f, g, K)
|
| 76 |
+
f, g = g, r
|
| 77 |
+
a, b = b, dup_sub_mul(a, q, b, K)
|
| 78 |
+
|
| 79 |
+
a = dup_quo_ground(a, dup_LC(f, K), K)
|
| 80 |
+
f = dup_monic(f, K)
|
| 81 |
+
|
| 82 |
+
return a, f
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def dmp_half_gcdex(f, g, u, K):
|
| 86 |
+
"""
|
| 87 |
+
Half extended Euclidean algorithm in `F[X]`.
|
| 88 |
+
|
| 89 |
+
Examples
|
| 90 |
+
========
|
| 91 |
+
|
| 92 |
+
>>> from sympy.polys import ring, ZZ
|
| 93 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 94 |
+
|
| 95 |
+
"""
|
| 96 |
+
if not u:
|
| 97 |
+
return dup_half_gcdex(f, g, K)
|
| 98 |
+
else:
|
| 99 |
+
raise MultivariatePolynomialError(f, g)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def dup_gcdex(f, g, K):
|
| 103 |
+
"""
|
| 104 |
+
Extended Euclidean algorithm in `F[x]`.
|
| 105 |
+
|
| 106 |
+
Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``.
|
| 107 |
+
|
| 108 |
+
Examples
|
| 109 |
+
========
|
| 110 |
+
|
| 111 |
+
>>> from sympy.polys import ring, QQ
|
| 112 |
+
>>> R, x = ring("x", QQ)
|
| 113 |
+
|
| 114 |
+
>>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
|
| 115 |
+
>>> g = x**3 + x**2 - 4*x - 4
|
| 116 |
+
|
| 117 |
+
>>> R.dup_gcdex(f, g)
|
| 118 |
+
(-1/5*x + 3/5, 1/5*x**2 - 6/5*x + 2, x + 1)
|
| 119 |
+
|
| 120 |
+
"""
|
| 121 |
+
s, h = dup_half_gcdex(f, g, K)
|
| 122 |
+
|
| 123 |
+
F = dup_sub_mul(h, s, f, K)
|
| 124 |
+
t = dup_quo(F, g, K)
|
| 125 |
+
|
| 126 |
+
return s, t, h
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def dmp_gcdex(f, g, u, K):
|
| 130 |
+
"""
|
| 131 |
+
Extended Euclidean algorithm in `F[X]`.
|
| 132 |
+
|
| 133 |
+
Examples
|
| 134 |
+
========
|
| 135 |
+
|
| 136 |
+
>>> from sympy.polys import ring, ZZ
|
| 137 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 138 |
+
|
| 139 |
+
"""
|
| 140 |
+
if not u:
|
| 141 |
+
return dup_gcdex(f, g, K)
|
| 142 |
+
else:
|
| 143 |
+
raise MultivariatePolynomialError(f, g)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def dup_invert(f, g, K):
|
| 147 |
+
"""
|
| 148 |
+
Compute multiplicative inverse of `f` modulo `g` in `F[x]`.
|
| 149 |
+
|
| 150 |
+
Examples
|
| 151 |
+
========
|
| 152 |
+
|
| 153 |
+
>>> from sympy.polys import ring, QQ
|
| 154 |
+
>>> R, x = ring("x", QQ)
|
| 155 |
+
|
| 156 |
+
>>> f = x**2 - 1
|
| 157 |
+
>>> g = 2*x - 1
|
| 158 |
+
>>> h = x - 1
|
| 159 |
+
|
| 160 |
+
>>> R.dup_invert(f, g)
|
| 161 |
+
-4/3
|
| 162 |
+
|
| 163 |
+
>>> R.dup_invert(f, h)
|
| 164 |
+
Traceback (most recent call last):
|
| 165 |
+
...
|
| 166 |
+
NotInvertible: zero divisor
|
| 167 |
+
|
| 168 |
+
"""
|
| 169 |
+
s, h = dup_half_gcdex(f, g, K)
|
| 170 |
+
|
| 171 |
+
if h == [K.one]:
|
| 172 |
+
return dup_rem(s, g, K)
|
| 173 |
+
else:
|
| 174 |
+
raise NotInvertible("zero divisor")
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def dmp_invert(f, g, u, K):
|
| 178 |
+
"""
|
| 179 |
+
Compute multiplicative inverse of `f` modulo `g` in `F[X]`.
|
| 180 |
+
|
| 181 |
+
Examples
|
| 182 |
+
========
|
| 183 |
+
|
| 184 |
+
>>> from sympy.polys import ring, QQ
|
| 185 |
+
>>> R, x = ring("x", QQ)
|
| 186 |
+
|
| 187 |
+
"""
|
| 188 |
+
if not u:
|
| 189 |
+
return dup_invert(f, g, K)
|
| 190 |
+
else:
|
| 191 |
+
raise MultivariatePolynomialError(f, g)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def dup_euclidean_prs(f, g, K):
|
| 195 |
+
"""
|
| 196 |
+
Euclidean polynomial remainder sequence (PRS) in `K[x]`.
|
| 197 |
+
|
| 198 |
+
Examples
|
| 199 |
+
========
|
| 200 |
+
|
| 201 |
+
>>> from sympy.polys import ring, QQ
|
| 202 |
+
>>> R, x = ring("x", QQ)
|
| 203 |
+
|
| 204 |
+
>>> f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
|
| 205 |
+
>>> g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
|
| 206 |
+
|
| 207 |
+
>>> prs = R.dup_euclidean_prs(f, g)
|
| 208 |
+
|
| 209 |
+
>>> prs[0]
|
| 210 |
+
x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
|
| 211 |
+
>>> prs[1]
|
| 212 |
+
3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
|
| 213 |
+
>>> prs[2]
|
| 214 |
+
-5/9*x**4 + 1/9*x**2 - 1/3
|
| 215 |
+
>>> prs[3]
|
| 216 |
+
-117/25*x**2 - 9*x + 441/25
|
| 217 |
+
>>> prs[4]
|
| 218 |
+
233150/19773*x - 102500/6591
|
| 219 |
+
>>> prs[5]
|
| 220 |
+
-1288744821/543589225
|
| 221 |
+
|
| 222 |
+
"""
|
| 223 |
+
prs = [f, g]
|
| 224 |
+
h = dup_rem(f, g, K)
|
| 225 |
+
|
| 226 |
+
while h:
|
| 227 |
+
prs.append(h)
|
| 228 |
+
f, g = g, h
|
| 229 |
+
h = dup_rem(f, g, K)
|
| 230 |
+
|
| 231 |
+
return prs
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def dmp_euclidean_prs(f, g, u, K):
|
| 235 |
+
"""
|
| 236 |
+
Euclidean polynomial remainder sequence (PRS) in `K[X]`.
|
| 237 |
+
|
| 238 |
+
Examples
|
| 239 |
+
========
|
| 240 |
+
|
| 241 |
+
>>> from sympy.polys import ring, ZZ
|
| 242 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 243 |
+
|
| 244 |
+
"""
|
| 245 |
+
if not u:
|
| 246 |
+
return dup_euclidean_prs(f, g, K)
|
| 247 |
+
else:
|
| 248 |
+
raise MultivariatePolynomialError(f, g)
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
def dup_primitive_prs(f, g, K):
|
| 252 |
+
"""
|
| 253 |
+
Primitive polynomial remainder sequence (PRS) in `K[x]`.
|
| 254 |
+
|
| 255 |
+
Examples
|
| 256 |
+
========
|
| 257 |
+
|
| 258 |
+
>>> from sympy.polys import ring, ZZ
|
| 259 |
+
>>> R, x = ring("x", ZZ)
|
| 260 |
+
|
| 261 |
+
>>> f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
|
| 262 |
+
>>> g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
|
| 263 |
+
|
| 264 |
+
>>> prs = R.dup_primitive_prs(f, g)
|
| 265 |
+
|
| 266 |
+
>>> prs[0]
|
| 267 |
+
x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
|
| 268 |
+
>>> prs[1]
|
| 269 |
+
3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
|
| 270 |
+
>>> prs[2]
|
| 271 |
+
-5*x**4 + x**2 - 3
|
| 272 |
+
>>> prs[3]
|
| 273 |
+
13*x**2 + 25*x - 49
|
| 274 |
+
>>> prs[4]
|
| 275 |
+
4663*x - 6150
|
| 276 |
+
>>> prs[5]
|
| 277 |
+
1
|
| 278 |
+
|
| 279 |
+
"""
|
| 280 |
+
prs = [f, g]
|
| 281 |
+
_, h = dup_primitive(dup_prem(f, g, K), K)
|
| 282 |
+
|
| 283 |
+
while h:
|
| 284 |
+
prs.append(h)
|
| 285 |
+
f, g = g, h
|
| 286 |
+
_, h = dup_primitive(dup_prem(f, g, K), K)
|
| 287 |
+
|
| 288 |
+
return prs
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
def dmp_primitive_prs(f, g, u, K):
|
| 292 |
+
"""
|
| 293 |
+
Primitive polynomial remainder sequence (PRS) in `K[X]`.
|
| 294 |
+
|
| 295 |
+
Examples
|
| 296 |
+
========
|
| 297 |
+
|
| 298 |
+
>>> from sympy.polys import ring, ZZ
|
| 299 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 300 |
+
|
| 301 |
+
"""
|
| 302 |
+
if not u:
|
| 303 |
+
return dup_primitive_prs(f, g, K)
|
| 304 |
+
else:
|
| 305 |
+
raise MultivariatePolynomialError(f, g)
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
def dup_inner_subresultants(f, g, K):
|
| 309 |
+
"""
|
| 310 |
+
Subresultant PRS algorithm in `K[x]`.
|
| 311 |
+
|
| 312 |
+
Computes the subresultant polynomial remainder sequence (PRS)
|
| 313 |
+
and the non-zero scalar subresultants of `f` and `g`.
|
| 314 |
+
By [1] Thm. 3, these are the constants '-c' (- to optimize
|
| 315 |
+
computation of sign).
|
| 316 |
+
The first subdeterminant is set to 1 by convention to match
|
| 317 |
+
the polynomial and the scalar subdeterminants.
|
| 318 |
+
If 'deg(f) < deg(g)', the subresultants of '(g,f)' are computed.
|
| 319 |
+
|
| 320 |
+
Examples
|
| 321 |
+
========
|
| 322 |
+
|
| 323 |
+
>>> from sympy.polys import ring, ZZ
|
| 324 |
+
>>> R, x = ring("x", ZZ)
|
| 325 |
+
|
| 326 |
+
>>> R.dup_inner_subresultants(x**2 + 1, x**2 - 1)
|
| 327 |
+
([x**2 + 1, x**2 - 1, -2], [1, 1, 4])
|
| 328 |
+
|
| 329 |
+
References
|
| 330 |
+
==========
|
| 331 |
+
|
| 332 |
+
.. [1] W.S. Brown, The Subresultant PRS Algorithm.
|
| 333 |
+
ACM Transaction of Mathematical Software 4 (1978) 237-249
|
| 334 |
+
|
| 335 |
+
"""
|
| 336 |
+
n = dup_degree(f)
|
| 337 |
+
m = dup_degree(g)
|
| 338 |
+
|
| 339 |
+
if n < m:
|
| 340 |
+
f, g = g, f
|
| 341 |
+
n, m = m, n
|
| 342 |
+
|
| 343 |
+
if not f:
|
| 344 |
+
return [], []
|
| 345 |
+
|
| 346 |
+
if not g:
|
| 347 |
+
return [f], [K.one]
|
| 348 |
+
|
| 349 |
+
R = [f, g]
|
| 350 |
+
d = n - m
|
| 351 |
+
|
| 352 |
+
b = (-K.one)**(d + 1)
|
| 353 |
+
|
| 354 |
+
h = dup_prem(f, g, K)
|
| 355 |
+
h = dup_mul_ground(h, b, K)
|
| 356 |
+
|
| 357 |
+
lc = dup_LC(g, K)
|
| 358 |
+
c = lc**d
|
| 359 |
+
|
| 360 |
+
# Conventional first scalar subdeterminant is 1
|
| 361 |
+
S = [K.one, c]
|
| 362 |
+
c = -c
|
| 363 |
+
|
| 364 |
+
while h:
|
| 365 |
+
k = dup_degree(h)
|
| 366 |
+
R.append(h)
|
| 367 |
+
|
| 368 |
+
f, g, m, d = g, h, k, m - k
|
| 369 |
+
|
| 370 |
+
b = -lc * c**d
|
| 371 |
+
|
| 372 |
+
h = dup_prem(f, g, K)
|
| 373 |
+
h = dup_quo_ground(h, b, K)
|
| 374 |
+
|
| 375 |
+
lc = dup_LC(g, K)
|
| 376 |
+
|
| 377 |
+
if d > 1: # abnormal case
|
| 378 |
+
q = c**(d - 1)
|
| 379 |
+
c = K.quo((-lc)**d, q)
|
| 380 |
+
else:
|
| 381 |
+
c = -lc
|
| 382 |
+
|
| 383 |
+
S.append(-c)
|
| 384 |
+
|
| 385 |
+
return R, S
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
def dup_subresultants(f, g, K):
|
| 389 |
+
"""
|
| 390 |
+
Computes subresultant PRS of two polynomials in `K[x]`.
|
| 391 |
+
|
| 392 |
+
Examples
|
| 393 |
+
========
|
| 394 |
+
|
| 395 |
+
>>> from sympy.polys import ring, ZZ
|
| 396 |
+
>>> R, x = ring("x", ZZ)
|
| 397 |
+
|
| 398 |
+
>>> R.dup_subresultants(x**2 + 1, x**2 - 1)
|
| 399 |
+
[x**2 + 1, x**2 - 1, -2]
|
| 400 |
+
|
| 401 |
+
"""
|
| 402 |
+
return dup_inner_subresultants(f, g, K)[0]
|
| 403 |
+
|
| 404 |
+
|
| 405 |
+
def dup_prs_resultant(f, g, K):
|
| 406 |
+
"""
|
| 407 |
+
Resultant algorithm in `K[x]` using subresultant PRS.
|
| 408 |
+
|
| 409 |
+
Examples
|
| 410 |
+
========
|
| 411 |
+
|
| 412 |
+
>>> from sympy.polys import ring, ZZ
|
| 413 |
+
>>> R, x = ring("x", ZZ)
|
| 414 |
+
|
| 415 |
+
>>> R.dup_prs_resultant(x**2 + 1, x**2 - 1)
|
| 416 |
+
(4, [x**2 + 1, x**2 - 1, -2])
|
| 417 |
+
|
| 418 |
+
"""
|
| 419 |
+
if not f or not g:
|
| 420 |
+
return (K.zero, [])
|
| 421 |
+
|
| 422 |
+
R, S = dup_inner_subresultants(f, g, K)
|
| 423 |
+
|
| 424 |
+
if dup_degree(R[-1]) > 0:
|
| 425 |
+
return (K.zero, R)
|
| 426 |
+
|
| 427 |
+
return S[-1], R
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
def dup_resultant(f, g, K, includePRS=False):
|
| 431 |
+
"""
|
| 432 |
+
Computes resultant of two polynomials in `K[x]`.
|
| 433 |
+
|
| 434 |
+
Examples
|
| 435 |
+
========
|
| 436 |
+
|
| 437 |
+
>>> from sympy.polys import ring, ZZ
|
| 438 |
+
>>> R, x = ring("x", ZZ)
|
| 439 |
+
|
| 440 |
+
>>> R.dup_resultant(x**2 + 1, x**2 - 1)
|
| 441 |
+
4
|
| 442 |
+
|
| 443 |
+
"""
|
| 444 |
+
if includePRS:
|
| 445 |
+
return dup_prs_resultant(f, g, K)
|
| 446 |
+
return dup_prs_resultant(f, g, K)[0]
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
def dmp_inner_subresultants(f, g, u, K):
|
| 450 |
+
"""
|
| 451 |
+
Subresultant PRS algorithm in `K[X]`.
|
| 452 |
+
|
| 453 |
+
Examples
|
| 454 |
+
========
|
| 455 |
+
|
| 456 |
+
>>> from sympy.polys import ring, ZZ
|
| 457 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 458 |
+
|
| 459 |
+
>>> f = 3*x**2*y - y**3 - 4
|
| 460 |
+
>>> g = x**2 + x*y**3 - 9
|
| 461 |
+
|
| 462 |
+
>>> a = 3*x*y**4 + y**3 - 27*y + 4
|
| 463 |
+
>>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
|
| 464 |
+
|
| 465 |
+
>>> prs = [f, g, a, b]
|
| 466 |
+
>>> sres = [[1], [1], [3, 0, 0, 0, 0], [-3, 0, 0, -12, 1, 0, -54, 8, 729, -216, 16]]
|
| 467 |
+
|
| 468 |
+
>>> R.dmp_inner_subresultants(f, g) == (prs, sres)
|
| 469 |
+
True
|
| 470 |
+
|
| 471 |
+
"""
|
| 472 |
+
if not u:
|
| 473 |
+
return dup_inner_subresultants(f, g, K)
|
| 474 |
+
|
| 475 |
+
n = dmp_degree(f, u)
|
| 476 |
+
m = dmp_degree(g, u)
|
| 477 |
+
|
| 478 |
+
if n < m:
|
| 479 |
+
f, g = g, f
|
| 480 |
+
n, m = m, n
|
| 481 |
+
|
| 482 |
+
if dmp_zero_p(f, u):
|
| 483 |
+
return [], []
|
| 484 |
+
|
| 485 |
+
v = u - 1
|
| 486 |
+
if dmp_zero_p(g, u):
|
| 487 |
+
return [f], [dmp_ground(K.one, v)]
|
| 488 |
+
|
| 489 |
+
R = [f, g]
|
| 490 |
+
d = n - m
|
| 491 |
+
|
| 492 |
+
b = dmp_pow(dmp_ground(-K.one, v), d + 1, v, K)
|
| 493 |
+
|
| 494 |
+
h = dmp_prem(f, g, u, K)
|
| 495 |
+
h = dmp_mul_term(h, b, 0, u, K)
|
| 496 |
+
|
| 497 |
+
lc = dmp_LC(g, K)
|
| 498 |
+
c = dmp_pow(lc, d, v, K)
|
| 499 |
+
|
| 500 |
+
S = [dmp_ground(K.one, v), c]
|
| 501 |
+
c = dmp_neg(c, v, K)
|
| 502 |
+
|
| 503 |
+
while not dmp_zero_p(h, u):
|
| 504 |
+
k = dmp_degree(h, u)
|
| 505 |
+
R.append(h)
|
| 506 |
+
|
| 507 |
+
f, g, m, d = g, h, k, m - k
|
| 508 |
+
|
| 509 |
+
b = dmp_mul(dmp_neg(lc, v, K),
|
| 510 |
+
dmp_pow(c, d, v, K), v, K)
|
| 511 |
+
|
| 512 |
+
h = dmp_prem(f, g, u, K)
|
| 513 |
+
h = [ dmp_quo(ch, b, v, K) for ch in h ]
|
| 514 |
+
|
| 515 |
+
lc = dmp_LC(g, K)
|
| 516 |
+
|
| 517 |
+
if d > 1:
|
| 518 |
+
p = dmp_pow(dmp_neg(lc, v, K), d, v, K)
|
| 519 |
+
q = dmp_pow(c, d - 1, v, K)
|
| 520 |
+
c = dmp_quo(p, q, v, K)
|
| 521 |
+
else:
|
| 522 |
+
c = dmp_neg(lc, v, K)
|
| 523 |
+
|
| 524 |
+
S.append(dmp_neg(c, v, K))
|
| 525 |
+
|
| 526 |
+
return R, S
|
| 527 |
+
|
| 528 |
+
|
| 529 |
+
def dmp_subresultants(f, g, u, K):
|
| 530 |
+
"""
|
| 531 |
+
Computes subresultant PRS of two polynomials in `K[X]`.
|
| 532 |
+
|
| 533 |
+
Examples
|
| 534 |
+
========
|
| 535 |
+
|
| 536 |
+
>>> from sympy.polys import ring, ZZ
|
| 537 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 538 |
+
|
| 539 |
+
>>> f = 3*x**2*y - y**3 - 4
|
| 540 |
+
>>> g = x**2 + x*y**3 - 9
|
| 541 |
+
|
| 542 |
+
>>> a = 3*x*y**4 + y**3 - 27*y + 4
|
| 543 |
+
>>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
|
| 544 |
+
|
| 545 |
+
>>> R.dmp_subresultants(f, g) == [f, g, a, b]
|
| 546 |
+
True
|
| 547 |
+
|
| 548 |
+
"""
|
| 549 |
+
return dmp_inner_subresultants(f, g, u, K)[0]
|
| 550 |
+
|
| 551 |
+
|
| 552 |
+
def dmp_prs_resultant(f, g, u, K):
|
| 553 |
+
"""
|
| 554 |
+
Resultant algorithm in `K[X]` using subresultant PRS.
|
| 555 |
+
|
| 556 |
+
Examples
|
| 557 |
+
========
|
| 558 |
+
|
| 559 |
+
>>> from sympy.polys import ring, ZZ
|
| 560 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 561 |
+
|
| 562 |
+
>>> f = 3*x**2*y - y**3 - 4
|
| 563 |
+
>>> g = x**2 + x*y**3 - 9
|
| 564 |
+
|
| 565 |
+
>>> a = 3*x*y**4 + y**3 - 27*y + 4
|
| 566 |
+
>>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
|
| 567 |
+
|
| 568 |
+
>>> res, prs = R.dmp_prs_resultant(f, g)
|
| 569 |
+
|
| 570 |
+
>>> res == b # resultant has n-1 variables
|
| 571 |
+
False
|
| 572 |
+
>>> res == b.drop(x)
|
| 573 |
+
True
|
| 574 |
+
>>> prs == [f, g, a, b]
|
| 575 |
+
True
|
| 576 |
+
|
| 577 |
+
"""
|
| 578 |
+
if not u:
|
| 579 |
+
return dup_prs_resultant(f, g, K)
|
| 580 |
+
|
| 581 |
+
if dmp_zero_p(f, u) or dmp_zero_p(g, u):
|
| 582 |
+
return (dmp_zero(u - 1), [])
|
| 583 |
+
|
| 584 |
+
R, S = dmp_inner_subresultants(f, g, u, K)
|
| 585 |
+
|
| 586 |
+
if dmp_degree(R[-1], u) > 0:
|
| 587 |
+
return (dmp_zero(u - 1), R)
|
| 588 |
+
|
| 589 |
+
return S[-1], R
|
| 590 |
+
|
| 591 |
+
|
| 592 |
+
def dmp_zz_modular_resultant(f, g, p, u, K):
|
| 593 |
+
"""
|
| 594 |
+
Compute resultant of `f` and `g` modulo a prime `p`.
|
| 595 |
+
|
| 596 |
+
Examples
|
| 597 |
+
========
|
| 598 |
+
|
| 599 |
+
>>> from sympy.polys import ring, ZZ
|
| 600 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 601 |
+
|
| 602 |
+
>>> f = x + y + 2
|
| 603 |
+
>>> g = 2*x*y + x + 3
|
| 604 |
+
|
| 605 |
+
>>> R.dmp_zz_modular_resultant(f, g, 5)
|
| 606 |
+
-2*y**2 + 1
|
| 607 |
+
|
| 608 |
+
"""
|
| 609 |
+
if not u:
|
| 610 |
+
return gf_int(dup_prs_resultant(f, g, K)[0] % p, p)
|
| 611 |
+
|
| 612 |
+
v = u - 1
|
| 613 |
+
|
| 614 |
+
n = dmp_degree(f, u)
|
| 615 |
+
m = dmp_degree(g, u)
|
| 616 |
+
|
| 617 |
+
N = dmp_degree_in(f, 1, u)
|
| 618 |
+
M = dmp_degree_in(g, 1, u)
|
| 619 |
+
|
| 620 |
+
B = n*M + m*N
|
| 621 |
+
|
| 622 |
+
D, a = [K.one], -K.one
|
| 623 |
+
r = dmp_zero(v)
|
| 624 |
+
|
| 625 |
+
while dup_degree(D) <= B:
|
| 626 |
+
while True:
|
| 627 |
+
a += K.one
|
| 628 |
+
|
| 629 |
+
if a == p:
|
| 630 |
+
raise HomomorphismFailed('no luck')
|
| 631 |
+
|
| 632 |
+
F = dmp_eval_in(f, gf_int(a, p), 1, u, K)
|
| 633 |
+
|
| 634 |
+
if dmp_degree(F, v) == n:
|
| 635 |
+
G = dmp_eval_in(g, gf_int(a, p), 1, u, K)
|
| 636 |
+
|
| 637 |
+
if dmp_degree(G, v) == m:
|
| 638 |
+
break
|
| 639 |
+
|
| 640 |
+
R = dmp_zz_modular_resultant(F, G, p, v, K)
|
| 641 |
+
e = dmp_eval(r, a, v, K)
|
| 642 |
+
|
| 643 |
+
if not v:
|
| 644 |
+
R = dup_strip([R])
|
| 645 |
+
e = dup_strip([e])
|
| 646 |
+
else:
|
| 647 |
+
R = [R]
|
| 648 |
+
e = [e]
|
| 649 |
+
|
| 650 |
+
d = K.invert(dup_eval(D, a, K), p)
|
| 651 |
+
d = dup_mul_ground(D, d, K)
|
| 652 |
+
d = dmp_raise(d, v, 0, K)
|
| 653 |
+
|
| 654 |
+
c = dmp_mul(d, dmp_sub(R, e, v, K), v, K)
|
| 655 |
+
r = dmp_add(r, c, v, K)
|
| 656 |
+
|
| 657 |
+
r = dmp_ground_trunc(r, p, v, K)
|
| 658 |
+
|
| 659 |
+
D = dup_mul(D, [K.one, -a], K)
|
| 660 |
+
D = dup_trunc(D, p, K)
|
| 661 |
+
|
| 662 |
+
return r
|
| 663 |
+
|
| 664 |
+
|
| 665 |
+
def _collins_crt(r, R, P, p, K):
|
| 666 |
+
"""Wrapper of CRT for Collins's resultant algorithm. """
|
| 667 |
+
return gf_int(gf_crt([r, R], [P, p], K), P*p)
|
| 668 |
+
|
| 669 |
+
|
| 670 |
+
def dmp_zz_collins_resultant(f, g, u, K):
|
| 671 |
+
"""
|
| 672 |
+
Collins's modular resultant algorithm in `Z[X]`.
|
| 673 |
+
|
| 674 |
+
Examples
|
| 675 |
+
========
|
| 676 |
+
|
| 677 |
+
>>> from sympy.polys import ring, ZZ
|
| 678 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 679 |
+
|
| 680 |
+
>>> f = x + y + 2
|
| 681 |
+
>>> g = 2*x*y + x + 3
|
| 682 |
+
|
| 683 |
+
>>> R.dmp_zz_collins_resultant(f, g)
|
| 684 |
+
-2*y**2 - 5*y + 1
|
| 685 |
+
|
| 686 |
+
"""
|
| 687 |
+
|
| 688 |
+
n = dmp_degree(f, u)
|
| 689 |
+
m = dmp_degree(g, u)
|
| 690 |
+
|
| 691 |
+
if n < 0 or m < 0:
|
| 692 |
+
return dmp_zero(u - 1)
|
| 693 |
+
|
| 694 |
+
A = dmp_max_norm(f, u, K)
|
| 695 |
+
B = dmp_max_norm(g, u, K)
|
| 696 |
+
|
| 697 |
+
a = dmp_ground_LC(f, u, K)
|
| 698 |
+
b = dmp_ground_LC(g, u, K)
|
| 699 |
+
|
| 700 |
+
v = u - 1
|
| 701 |
+
|
| 702 |
+
B = K(2)*K.factorial(K(n + m))*A**m*B**n
|
| 703 |
+
r, p, P = dmp_zero(v), K.one, K.one
|
| 704 |
+
|
| 705 |
+
from sympy.ntheory import nextprime
|
| 706 |
+
|
| 707 |
+
while P <= B:
|
| 708 |
+
p = K(nextprime(p))
|
| 709 |
+
|
| 710 |
+
while not (a % p) or not (b % p):
|
| 711 |
+
p = K(nextprime(p))
|
| 712 |
+
|
| 713 |
+
F = dmp_ground_trunc(f, p, u, K)
|
| 714 |
+
G = dmp_ground_trunc(g, p, u, K)
|
| 715 |
+
|
| 716 |
+
try:
|
| 717 |
+
R = dmp_zz_modular_resultant(F, G, p, u, K)
|
| 718 |
+
except HomomorphismFailed:
|
| 719 |
+
continue
|
| 720 |
+
|
| 721 |
+
if K.is_one(P):
|
| 722 |
+
r = R
|
| 723 |
+
else:
|
| 724 |
+
r = dmp_apply_pairs(r, R, _collins_crt, (P, p, K), v, K)
|
| 725 |
+
|
| 726 |
+
P *= p
|
| 727 |
+
|
| 728 |
+
return r
|
| 729 |
+
|
| 730 |
+
|
| 731 |
+
def dmp_qq_collins_resultant(f, g, u, K0):
|
| 732 |
+
"""
|
| 733 |
+
Collins's modular resultant algorithm in `Q[X]`.
|
| 734 |
+
|
| 735 |
+
Examples
|
| 736 |
+
========
|
| 737 |
+
|
| 738 |
+
>>> from sympy.polys import ring, QQ
|
| 739 |
+
>>> R, x,y = ring("x,y", QQ)
|
| 740 |
+
|
| 741 |
+
>>> f = QQ(1,2)*x + y + QQ(2,3)
|
| 742 |
+
>>> g = 2*x*y + x + 3
|
| 743 |
+
|
| 744 |
+
>>> R.dmp_qq_collins_resultant(f, g)
|
| 745 |
+
-2*y**2 - 7/3*y + 5/6
|
| 746 |
+
|
| 747 |
+
"""
|
| 748 |
+
n = dmp_degree(f, u)
|
| 749 |
+
m = dmp_degree(g, u)
|
| 750 |
+
|
| 751 |
+
if n < 0 or m < 0:
|
| 752 |
+
return dmp_zero(u - 1)
|
| 753 |
+
|
| 754 |
+
K1 = K0.get_ring()
|
| 755 |
+
|
| 756 |
+
cf, f = dmp_clear_denoms(f, u, K0, K1)
|
| 757 |
+
cg, g = dmp_clear_denoms(g, u, K0, K1)
|
| 758 |
+
|
| 759 |
+
f = dmp_convert(f, u, K0, K1)
|
| 760 |
+
g = dmp_convert(g, u, K0, K1)
|
| 761 |
+
|
| 762 |
+
r = dmp_zz_collins_resultant(f, g, u, K1)
|
| 763 |
+
r = dmp_convert(r, u - 1, K1, K0)
|
| 764 |
+
|
| 765 |
+
c = K0.convert(cf**m * cg**n, K1)
|
| 766 |
+
|
| 767 |
+
return dmp_quo_ground(r, c, u - 1, K0)
|
| 768 |
+
|
| 769 |
+
|
| 770 |
+
def dmp_resultant(f, g, u, K, includePRS=False):
|
| 771 |
+
"""
|
| 772 |
+
Computes resultant of two polynomials in `K[X]`.
|
| 773 |
+
|
| 774 |
+
Examples
|
| 775 |
+
========
|
| 776 |
+
|
| 777 |
+
>>> from sympy.polys import ring, ZZ
|
| 778 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 779 |
+
|
| 780 |
+
>>> f = 3*x**2*y - y**3 - 4
|
| 781 |
+
>>> g = x**2 + x*y**3 - 9
|
| 782 |
+
|
| 783 |
+
>>> R.dmp_resultant(f, g)
|
| 784 |
+
-3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
|
| 785 |
+
|
| 786 |
+
"""
|
| 787 |
+
if not u:
|
| 788 |
+
return dup_resultant(f, g, K, includePRS=includePRS)
|
| 789 |
+
|
| 790 |
+
if includePRS:
|
| 791 |
+
return dmp_prs_resultant(f, g, u, K)
|
| 792 |
+
|
| 793 |
+
if K.is_Field:
|
| 794 |
+
if K.is_QQ and query('USE_COLLINS_RESULTANT'):
|
| 795 |
+
return dmp_qq_collins_resultant(f, g, u, K)
|
| 796 |
+
else:
|
| 797 |
+
if K.is_ZZ and query('USE_COLLINS_RESULTANT'):
|
| 798 |
+
return dmp_zz_collins_resultant(f, g, u, K)
|
| 799 |
+
|
| 800 |
+
return dmp_prs_resultant(f, g, u, K)[0]
|
| 801 |
+
|
| 802 |
+
|
| 803 |
+
def dup_discriminant(f, K):
|
| 804 |
+
"""
|
| 805 |
+
Computes discriminant of a polynomial in `K[x]`.
|
| 806 |
+
|
| 807 |
+
Examples
|
| 808 |
+
========
|
| 809 |
+
|
| 810 |
+
>>> from sympy.polys import ring, ZZ
|
| 811 |
+
>>> R, x = ring("x", ZZ)
|
| 812 |
+
|
| 813 |
+
>>> R.dup_discriminant(x**2 + 2*x + 3)
|
| 814 |
+
-8
|
| 815 |
+
|
| 816 |
+
"""
|
| 817 |
+
d = dup_degree(f)
|
| 818 |
+
|
| 819 |
+
if d <= 0:
|
| 820 |
+
return K.zero
|
| 821 |
+
else:
|
| 822 |
+
s = (-1)**((d*(d - 1)) // 2)
|
| 823 |
+
c = dup_LC(f, K)
|
| 824 |
+
|
| 825 |
+
r = dup_resultant(f, dup_diff(f, 1, K), K)
|
| 826 |
+
|
| 827 |
+
return K.quo(r, c*K(s))
|
| 828 |
+
|
| 829 |
+
|
| 830 |
+
def dmp_discriminant(f, u, K):
|
| 831 |
+
"""
|
| 832 |
+
Computes discriminant of a polynomial in `K[X]`.
|
| 833 |
+
|
| 834 |
+
Examples
|
| 835 |
+
========
|
| 836 |
+
|
| 837 |
+
>>> from sympy.polys import ring, ZZ
|
| 838 |
+
>>> R, x,y,z,t = ring("x,y,z,t", ZZ)
|
| 839 |
+
|
| 840 |
+
>>> R.dmp_discriminant(x**2*y + x*z + t)
|
| 841 |
+
-4*y*t + z**2
|
| 842 |
+
|
| 843 |
+
"""
|
| 844 |
+
if not u:
|
| 845 |
+
return dup_discriminant(f, K)
|
| 846 |
+
|
| 847 |
+
d, v = dmp_degree(f, u), u - 1
|
| 848 |
+
|
| 849 |
+
if d <= 0:
|
| 850 |
+
return dmp_zero(v)
|
| 851 |
+
else:
|
| 852 |
+
s = (-1)**((d*(d - 1)) // 2)
|
| 853 |
+
c = dmp_LC(f, K)
|
| 854 |
+
|
| 855 |
+
r = dmp_resultant(f, dmp_diff(f, 1, u, K), u, K)
|
| 856 |
+
c = dmp_mul_ground(c, K(s), v, K)
|
| 857 |
+
|
| 858 |
+
return dmp_quo(r, c, v, K)
|
| 859 |
+
|
| 860 |
+
|
| 861 |
+
def _dup_rr_trivial_gcd(f, g, K):
|
| 862 |
+
"""Handle trivial cases in GCD algorithm over a ring. """
|
| 863 |
+
if not (f or g):
|
| 864 |
+
return [], [], []
|
| 865 |
+
elif not f:
|
| 866 |
+
if K.is_nonnegative(dup_LC(g, K)):
|
| 867 |
+
return g, [], [K.one]
|
| 868 |
+
else:
|
| 869 |
+
return dup_neg(g, K), [], [-K.one]
|
| 870 |
+
elif not g:
|
| 871 |
+
if K.is_nonnegative(dup_LC(f, K)):
|
| 872 |
+
return f, [K.one], []
|
| 873 |
+
else:
|
| 874 |
+
return dup_neg(f, K), [-K.one], []
|
| 875 |
+
|
| 876 |
+
return None
|
| 877 |
+
|
| 878 |
+
|
| 879 |
+
def _dup_ff_trivial_gcd(f, g, K):
|
| 880 |
+
"""Handle trivial cases in GCD algorithm over a field. """
|
| 881 |
+
if not (f or g):
|
| 882 |
+
return [], [], []
|
| 883 |
+
elif not f:
|
| 884 |
+
return dup_monic(g, K), [], [dup_LC(g, K)]
|
| 885 |
+
elif not g:
|
| 886 |
+
return dup_monic(f, K), [dup_LC(f, K)], []
|
| 887 |
+
else:
|
| 888 |
+
return None
|
| 889 |
+
|
| 890 |
+
|
| 891 |
+
def _dmp_rr_trivial_gcd(f, g, u, K):
|
| 892 |
+
"""Handle trivial cases in GCD algorithm over a ring. """
|
| 893 |
+
zero_f = dmp_zero_p(f, u)
|
| 894 |
+
zero_g = dmp_zero_p(g, u)
|
| 895 |
+
if_contain_one = dmp_one_p(f, u, K) or dmp_one_p(g, u, K)
|
| 896 |
+
|
| 897 |
+
if zero_f and zero_g:
|
| 898 |
+
return tuple(dmp_zeros(3, u, K))
|
| 899 |
+
elif zero_f:
|
| 900 |
+
if K.is_nonnegative(dmp_ground_LC(g, u, K)):
|
| 901 |
+
return g, dmp_zero(u), dmp_one(u, K)
|
| 902 |
+
else:
|
| 903 |
+
return dmp_neg(g, u, K), dmp_zero(u), dmp_ground(-K.one, u)
|
| 904 |
+
elif zero_g:
|
| 905 |
+
if K.is_nonnegative(dmp_ground_LC(f, u, K)):
|
| 906 |
+
return f, dmp_one(u, K), dmp_zero(u)
|
| 907 |
+
else:
|
| 908 |
+
return dmp_neg(f, u, K), dmp_ground(-K.one, u), dmp_zero(u)
|
| 909 |
+
elif if_contain_one:
|
| 910 |
+
return dmp_one(u, K), f, g
|
| 911 |
+
elif query('USE_SIMPLIFY_GCD'):
|
| 912 |
+
return _dmp_simplify_gcd(f, g, u, K)
|
| 913 |
+
else:
|
| 914 |
+
return None
|
| 915 |
+
|
| 916 |
+
|
| 917 |
+
def _dmp_ff_trivial_gcd(f, g, u, K):
|
| 918 |
+
"""Handle trivial cases in GCD algorithm over a field. """
|
| 919 |
+
zero_f = dmp_zero_p(f, u)
|
| 920 |
+
zero_g = dmp_zero_p(g, u)
|
| 921 |
+
|
| 922 |
+
if zero_f and zero_g:
|
| 923 |
+
return tuple(dmp_zeros(3, u, K))
|
| 924 |
+
elif zero_f:
|
| 925 |
+
return (dmp_ground_monic(g, u, K),
|
| 926 |
+
dmp_zero(u),
|
| 927 |
+
dmp_ground(dmp_ground_LC(g, u, K), u))
|
| 928 |
+
elif zero_g:
|
| 929 |
+
return (dmp_ground_monic(f, u, K),
|
| 930 |
+
dmp_ground(dmp_ground_LC(f, u, K), u),
|
| 931 |
+
dmp_zero(u))
|
| 932 |
+
elif query('USE_SIMPLIFY_GCD'):
|
| 933 |
+
return _dmp_simplify_gcd(f, g, u, K)
|
| 934 |
+
else:
|
| 935 |
+
return None
|
| 936 |
+
|
| 937 |
+
|
| 938 |
+
def _dmp_simplify_gcd(f, g, u, K):
|
| 939 |
+
"""Try to eliminate `x_0` from GCD computation in `K[X]`. """
|
| 940 |
+
df = dmp_degree(f, u)
|
| 941 |
+
dg = dmp_degree(g, u)
|
| 942 |
+
|
| 943 |
+
if df > 0 and dg > 0:
|
| 944 |
+
return None
|
| 945 |
+
|
| 946 |
+
if not (df or dg):
|
| 947 |
+
F = dmp_LC(f, K)
|
| 948 |
+
G = dmp_LC(g, K)
|
| 949 |
+
else:
|
| 950 |
+
if not df:
|
| 951 |
+
F = dmp_LC(f, K)
|
| 952 |
+
G = dmp_content(g, u, K)
|
| 953 |
+
else:
|
| 954 |
+
F = dmp_content(f, u, K)
|
| 955 |
+
G = dmp_LC(g, K)
|
| 956 |
+
|
| 957 |
+
v = u - 1
|
| 958 |
+
h = dmp_gcd(F, G, v, K)
|
| 959 |
+
|
| 960 |
+
cff = [ dmp_quo(cf, h, v, K) for cf in f ]
|
| 961 |
+
cfg = [ dmp_quo(cg, h, v, K) for cg in g ]
|
| 962 |
+
|
| 963 |
+
return [h], cff, cfg
|
| 964 |
+
|
| 965 |
+
|
| 966 |
+
def dup_rr_prs_gcd(f, g, K):
|
| 967 |
+
"""
|
| 968 |
+
Computes polynomial GCD using subresultants over a ring.
|
| 969 |
+
|
| 970 |
+
Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
|
| 971 |
+
and ``cfg = quo(g, h)``.
|
| 972 |
+
|
| 973 |
+
Examples
|
| 974 |
+
========
|
| 975 |
+
|
| 976 |
+
>>> from sympy.polys import ring, ZZ
|
| 977 |
+
>>> R, x = ring("x", ZZ)
|
| 978 |
+
|
| 979 |
+
>>> R.dup_rr_prs_gcd(x**2 - 1, x**2 - 3*x + 2)
|
| 980 |
+
(x - 1, x + 1, x - 2)
|
| 981 |
+
|
| 982 |
+
"""
|
| 983 |
+
result = _dup_rr_trivial_gcd(f, g, K)
|
| 984 |
+
|
| 985 |
+
if result is not None:
|
| 986 |
+
return result
|
| 987 |
+
|
| 988 |
+
fc, F = dup_primitive(f, K)
|
| 989 |
+
gc, G = dup_primitive(g, K)
|
| 990 |
+
|
| 991 |
+
c = K.gcd(fc, gc)
|
| 992 |
+
|
| 993 |
+
h = dup_subresultants(F, G, K)[-1]
|
| 994 |
+
_, h = dup_primitive(h, K)
|
| 995 |
+
|
| 996 |
+
c *= K.canonical_unit(dup_LC(h, K))
|
| 997 |
+
|
| 998 |
+
h = dup_mul_ground(h, c, K)
|
| 999 |
+
|
| 1000 |
+
cff = dup_quo(f, h, K)
|
| 1001 |
+
cfg = dup_quo(g, h, K)
|
| 1002 |
+
|
| 1003 |
+
return h, cff, cfg
|
| 1004 |
+
|
| 1005 |
+
|
| 1006 |
+
def dup_ff_prs_gcd(f, g, K):
|
| 1007 |
+
"""
|
| 1008 |
+
Computes polynomial GCD using subresultants over a field.
|
| 1009 |
+
|
| 1010 |
+
Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
|
| 1011 |
+
and ``cfg = quo(g, h)``.
|
| 1012 |
+
|
| 1013 |
+
Examples
|
| 1014 |
+
========
|
| 1015 |
+
|
| 1016 |
+
>>> from sympy.polys import ring, QQ
|
| 1017 |
+
>>> R, x = ring("x", QQ)
|
| 1018 |
+
|
| 1019 |
+
>>> R.dup_ff_prs_gcd(x**2 - 1, x**2 - 3*x + 2)
|
| 1020 |
+
(x - 1, x + 1, x - 2)
|
| 1021 |
+
|
| 1022 |
+
"""
|
| 1023 |
+
result = _dup_ff_trivial_gcd(f, g, K)
|
| 1024 |
+
|
| 1025 |
+
if result is not None:
|
| 1026 |
+
return result
|
| 1027 |
+
|
| 1028 |
+
h = dup_subresultants(f, g, K)[-1]
|
| 1029 |
+
h = dup_monic(h, K)
|
| 1030 |
+
|
| 1031 |
+
cff = dup_quo(f, h, K)
|
| 1032 |
+
cfg = dup_quo(g, h, K)
|
| 1033 |
+
|
| 1034 |
+
return h, cff, cfg
|
| 1035 |
+
|
| 1036 |
+
|
| 1037 |
+
def dmp_rr_prs_gcd(f, g, u, K):
|
| 1038 |
+
"""
|
| 1039 |
+
Computes polynomial GCD using subresultants over a ring.
|
| 1040 |
+
|
| 1041 |
+
Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
|
| 1042 |
+
and ``cfg = quo(g, h)``.
|
| 1043 |
+
|
| 1044 |
+
Examples
|
| 1045 |
+
========
|
| 1046 |
+
|
| 1047 |
+
>>> from sympy.polys import ring, ZZ
|
| 1048 |
+
>>> R, x,y, = ring("x,y", ZZ)
|
| 1049 |
+
|
| 1050 |
+
>>> f = x**2 + 2*x*y + y**2
|
| 1051 |
+
>>> g = x**2 + x*y
|
| 1052 |
+
|
| 1053 |
+
>>> R.dmp_rr_prs_gcd(f, g)
|
| 1054 |
+
(x + y, x + y, x)
|
| 1055 |
+
|
| 1056 |
+
"""
|
| 1057 |
+
if not u:
|
| 1058 |
+
return dup_rr_prs_gcd(f, g, K)
|
| 1059 |
+
|
| 1060 |
+
result = _dmp_rr_trivial_gcd(f, g, u, K)
|
| 1061 |
+
|
| 1062 |
+
if result is not None:
|
| 1063 |
+
return result
|
| 1064 |
+
|
| 1065 |
+
fc, F = dmp_primitive(f, u, K)
|
| 1066 |
+
gc, G = dmp_primitive(g, u, K)
|
| 1067 |
+
|
| 1068 |
+
h = dmp_subresultants(F, G, u, K)[-1]
|
| 1069 |
+
c, _, _ = dmp_rr_prs_gcd(fc, gc, u - 1, K)
|
| 1070 |
+
|
| 1071 |
+
_, h = dmp_primitive(h, u, K)
|
| 1072 |
+
h = dmp_mul_term(h, c, 0, u, K)
|
| 1073 |
+
|
| 1074 |
+
unit = K.canonical_unit(dmp_ground_LC(h, u, K))
|
| 1075 |
+
|
| 1076 |
+
if unit != K.one:
|
| 1077 |
+
h = dmp_mul_ground(h, unit, u, K)
|
| 1078 |
+
|
| 1079 |
+
cff = dmp_quo(f, h, u, K)
|
| 1080 |
+
cfg = dmp_quo(g, h, u, K)
|
| 1081 |
+
|
| 1082 |
+
return h, cff, cfg
|
| 1083 |
+
|
| 1084 |
+
|
| 1085 |
+
def dmp_ff_prs_gcd(f, g, u, K):
|
| 1086 |
+
"""
|
| 1087 |
+
Computes polynomial GCD using subresultants over a field.
|
| 1088 |
+
|
| 1089 |
+
Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
|
| 1090 |
+
and ``cfg = quo(g, h)``.
|
| 1091 |
+
|
| 1092 |
+
Examples
|
| 1093 |
+
========
|
| 1094 |
+
|
| 1095 |
+
>>> from sympy.polys import ring, QQ
|
| 1096 |
+
>>> R, x,y, = ring("x,y", QQ)
|
| 1097 |
+
|
| 1098 |
+
>>> f = QQ(1,2)*x**2 + x*y + QQ(1,2)*y**2
|
| 1099 |
+
>>> g = x**2 + x*y
|
| 1100 |
+
|
| 1101 |
+
>>> R.dmp_ff_prs_gcd(f, g)
|
| 1102 |
+
(x + y, 1/2*x + 1/2*y, x)
|
| 1103 |
+
|
| 1104 |
+
"""
|
| 1105 |
+
if not u:
|
| 1106 |
+
return dup_ff_prs_gcd(f, g, K)
|
| 1107 |
+
|
| 1108 |
+
result = _dmp_ff_trivial_gcd(f, g, u, K)
|
| 1109 |
+
|
| 1110 |
+
if result is not None:
|
| 1111 |
+
return result
|
| 1112 |
+
|
| 1113 |
+
fc, F = dmp_primitive(f, u, K)
|
| 1114 |
+
gc, G = dmp_primitive(g, u, K)
|
| 1115 |
+
|
| 1116 |
+
h = dmp_subresultants(F, G, u, K)[-1]
|
| 1117 |
+
c, _, _ = dmp_ff_prs_gcd(fc, gc, u - 1, K)
|
| 1118 |
+
|
| 1119 |
+
_, h = dmp_primitive(h, u, K)
|
| 1120 |
+
h = dmp_mul_term(h, c, 0, u, K)
|
| 1121 |
+
h = dmp_ground_monic(h, u, K)
|
| 1122 |
+
|
| 1123 |
+
cff = dmp_quo(f, h, u, K)
|
| 1124 |
+
cfg = dmp_quo(g, h, u, K)
|
| 1125 |
+
|
| 1126 |
+
return h, cff, cfg
|
| 1127 |
+
|
| 1128 |
+
HEU_GCD_MAX = 6
|
| 1129 |
+
|
| 1130 |
+
|
| 1131 |
+
def _dup_zz_gcd_interpolate(h, x, K):
|
| 1132 |
+
"""Interpolate polynomial GCD from integer GCD. """
|
| 1133 |
+
f = []
|
| 1134 |
+
|
| 1135 |
+
while h:
|
| 1136 |
+
g = h % x
|
| 1137 |
+
|
| 1138 |
+
if g > x // 2:
|
| 1139 |
+
g -= x
|
| 1140 |
+
|
| 1141 |
+
f.insert(0, g)
|
| 1142 |
+
h = (h - g) // x
|
| 1143 |
+
|
| 1144 |
+
return f
|
| 1145 |
+
|
| 1146 |
+
|
| 1147 |
+
def dup_zz_heu_gcd(f, g, K):
|
| 1148 |
+
"""
|
| 1149 |
+
Heuristic polynomial GCD in `Z[x]`.
|
| 1150 |
+
|
| 1151 |
+
Given univariate polynomials `f` and `g` in `Z[x]`, returns
|
| 1152 |
+
their GCD and cofactors, i.e. polynomials ``h``, ``cff`` and ``cfg``
|
| 1153 |
+
such that::
|
| 1154 |
+
|
| 1155 |
+
h = gcd(f, g), cff = quo(f, h) and cfg = quo(g, h)
|
| 1156 |
+
|
| 1157 |
+
The algorithm is purely heuristic which means it may fail to compute
|
| 1158 |
+
the GCD. This will be signaled by raising an exception. In this case
|
| 1159 |
+
you will need to switch to another GCD method.
|
| 1160 |
+
|
| 1161 |
+
The algorithm computes the polynomial GCD by evaluating polynomials
|
| 1162 |
+
f and g at certain points and computing (fast) integer GCD of those
|
| 1163 |
+
evaluations. The polynomial GCD is recovered from the integer image
|
| 1164 |
+
by interpolation. The final step is to verify if the result is the
|
| 1165 |
+
correct GCD. This gives cofactors as a side effect.
|
| 1166 |
+
|
| 1167 |
+
Examples
|
| 1168 |
+
========
|
| 1169 |
+
|
| 1170 |
+
>>> from sympy.polys import ring, ZZ
|
| 1171 |
+
>>> R, x = ring("x", ZZ)
|
| 1172 |
+
|
| 1173 |
+
>>> R.dup_zz_heu_gcd(x**2 - 1, x**2 - 3*x + 2)
|
| 1174 |
+
(x - 1, x + 1, x - 2)
|
| 1175 |
+
|
| 1176 |
+
References
|
| 1177 |
+
==========
|
| 1178 |
+
|
| 1179 |
+
.. [1] [Liao95]_
|
| 1180 |
+
|
| 1181 |
+
"""
|
| 1182 |
+
result = _dup_rr_trivial_gcd(f, g, K)
|
| 1183 |
+
|
| 1184 |
+
if result is not None:
|
| 1185 |
+
return result
|
| 1186 |
+
|
| 1187 |
+
df = dup_degree(f)
|
| 1188 |
+
dg = dup_degree(g)
|
| 1189 |
+
|
| 1190 |
+
gcd, f, g = dup_extract(f, g, K)
|
| 1191 |
+
|
| 1192 |
+
if df == 0 or dg == 0:
|
| 1193 |
+
return [gcd], f, g
|
| 1194 |
+
|
| 1195 |
+
f_norm = dup_max_norm(f, K)
|
| 1196 |
+
g_norm = dup_max_norm(g, K)
|
| 1197 |
+
|
| 1198 |
+
B = K(2*min(f_norm, g_norm) + 29)
|
| 1199 |
+
|
| 1200 |
+
x = max(min(B, 99*K.sqrt(B)),
|
| 1201 |
+
2*min(f_norm // abs(dup_LC(f, K)),
|
| 1202 |
+
g_norm // abs(dup_LC(g, K))) + 4)
|
| 1203 |
+
|
| 1204 |
+
for i in range(0, HEU_GCD_MAX):
|
| 1205 |
+
ff = dup_eval(f, x, K)
|
| 1206 |
+
gg = dup_eval(g, x, K)
|
| 1207 |
+
|
| 1208 |
+
if ff and gg:
|
| 1209 |
+
h = K.gcd(ff, gg)
|
| 1210 |
+
|
| 1211 |
+
cff = ff // h
|
| 1212 |
+
cfg = gg // h
|
| 1213 |
+
|
| 1214 |
+
h = _dup_zz_gcd_interpolate(h, x, K)
|
| 1215 |
+
h = dup_primitive(h, K)[1]
|
| 1216 |
+
|
| 1217 |
+
cff_, r = dup_div(f, h, K)
|
| 1218 |
+
|
| 1219 |
+
if not r:
|
| 1220 |
+
cfg_, r = dup_div(g, h, K)
|
| 1221 |
+
|
| 1222 |
+
if not r:
|
| 1223 |
+
h = dup_mul_ground(h, gcd, K)
|
| 1224 |
+
return h, cff_, cfg_
|
| 1225 |
+
|
| 1226 |
+
cff = _dup_zz_gcd_interpolate(cff, x, K)
|
| 1227 |
+
|
| 1228 |
+
h, r = dup_div(f, cff, K)
|
| 1229 |
+
|
| 1230 |
+
if not r:
|
| 1231 |
+
cfg_, r = dup_div(g, h, K)
|
| 1232 |
+
|
| 1233 |
+
if not r:
|
| 1234 |
+
h = dup_mul_ground(h, gcd, K)
|
| 1235 |
+
return h, cff, cfg_
|
| 1236 |
+
|
| 1237 |
+
cfg = _dup_zz_gcd_interpolate(cfg, x, K)
|
| 1238 |
+
|
| 1239 |
+
h, r = dup_div(g, cfg, K)
|
| 1240 |
+
|
| 1241 |
+
if not r:
|
| 1242 |
+
cff_, r = dup_div(f, h, K)
|
| 1243 |
+
|
| 1244 |
+
if not r:
|
| 1245 |
+
h = dup_mul_ground(h, gcd, K)
|
| 1246 |
+
return h, cff_, cfg
|
| 1247 |
+
|
| 1248 |
+
x = 73794*x * K.sqrt(K.sqrt(x)) // 27011
|
| 1249 |
+
|
| 1250 |
+
raise HeuristicGCDFailed('no luck')
|
| 1251 |
+
|
| 1252 |
+
|
| 1253 |
+
def _dmp_zz_gcd_interpolate(h, x, v, K):
|
| 1254 |
+
"""Interpolate polynomial GCD from integer GCD. """
|
| 1255 |
+
f = []
|
| 1256 |
+
|
| 1257 |
+
while not dmp_zero_p(h, v):
|
| 1258 |
+
g = dmp_ground_trunc(h, x, v, K)
|
| 1259 |
+
f.insert(0, g)
|
| 1260 |
+
|
| 1261 |
+
h = dmp_sub(h, g, v, K)
|
| 1262 |
+
h = dmp_quo_ground(h, x, v, K)
|
| 1263 |
+
|
| 1264 |
+
if K.is_negative(dmp_ground_LC(f, v + 1, K)):
|
| 1265 |
+
return dmp_neg(f, v + 1, K)
|
| 1266 |
+
else:
|
| 1267 |
+
return f
|
| 1268 |
+
|
| 1269 |
+
|
| 1270 |
+
def dmp_zz_heu_gcd(f, g, u, K):
|
| 1271 |
+
"""
|
| 1272 |
+
Heuristic polynomial GCD in `Z[X]`.
|
| 1273 |
+
|
| 1274 |
+
Given univariate polynomials `f` and `g` in `Z[X]`, returns
|
| 1275 |
+
their GCD and cofactors, i.e. polynomials ``h``, ``cff`` and ``cfg``
|
| 1276 |
+
such that::
|
| 1277 |
+
|
| 1278 |
+
h = gcd(f, g), cff = quo(f, h) and cfg = quo(g, h)
|
| 1279 |
+
|
| 1280 |
+
The algorithm is purely heuristic which means it may fail to compute
|
| 1281 |
+
the GCD. This will be signaled by raising an exception. In this case
|
| 1282 |
+
you will need to switch to another GCD method.
|
| 1283 |
+
|
| 1284 |
+
The algorithm computes the polynomial GCD by evaluating polynomials
|
| 1285 |
+
f and g at certain points and computing (fast) integer GCD of those
|
| 1286 |
+
evaluations. The polynomial GCD is recovered from the integer image
|
| 1287 |
+
by interpolation. The evaluation process reduces f and g variable by
|
| 1288 |
+
variable into a large integer. The final step is to verify if the
|
| 1289 |
+
interpolated polynomial is the correct GCD. This gives cofactors of
|
| 1290 |
+
the input polynomials as a side effect.
|
| 1291 |
+
|
| 1292 |
+
Examples
|
| 1293 |
+
========
|
| 1294 |
+
|
| 1295 |
+
>>> from sympy.polys import ring, ZZ
|
| 1296 |
+
>>> R, x,y, = ring("x,y", ZZ)
|
| 1297 |
+
|
| 1298 |
+
>>> f = x**2 + 2*x*y + y**2
|
| 1299 |
+
>>> g = x**2 + x*y
|
| 1300 |
+
|
| 1301 |
+
>>> R.dmp_zz_heu_gcd(f, g)
|
| 1302 |
+
(x + y, x + y, x)
|
| 1303 |
+
|
| 1304 |
+
References
|
| 1305 |
+
==========
|
| 1306 |
+
|
| 1307 |
+
.. [1] [Liao95]_
|
| 1308 |
+
|
| 1309 |
+
"""
|
| 1310 |
+
if not u:
|
| 1311 |
+
return dup_zz_heu_gcd(f, g, K)
|
| 1312 |
+
|
| 1313 |
+
result = _dmp_rr_trivial_gcd(f, g, u, K)
|
| 1314 |
+
|
| 1315 |
+
if result is not None:
|
| 1316 |
+
return result
|
| 1317 |
+
|
| 1318 |
+
gcd, f, g = dmp_ground_extract(f, g, u, K)
|
| 1319 |
+
|
| 1320 |
+
f_norm = dmp_max_norm(f, u, K)
|
| 1321 |
+
g_norm = dmp_max_norm(g, u, K)
|
| 1322 |
+
|
| 1323 |
+
B = K(2*min(f_norm, g_norm) + 29)
|
| 1324 |
+
|
| 1325 |
+
x = max(min(B, 99*K.sqrt(B)),
|
| 1326 |
+
2*min(f_norm // abs(dmp_ground_LC(f, u, K)),
|
| 1327 |
+
g_norm // abs(dmp_ground_LC(g, u, K))) + 4)
|
| 1328 |
+
|
| 1329 |
+
for i in range(0, HEU_GCD_MAX):
|
| 1330 |
+
ff = dmp_eval(f, x, u, K)
|
| 1331 |
+
gg = dmp_eval(g, x, u, K)
|
| 1332 |
+
|
| 1333 |
+
v = u - 1
|
| 1334 |
+
|
| 1335 |
+
if not (dmp_zero_p(ff, v) or dmp_zero_p(gg, v)):
|
| 1336 |
+
h, cff, cfg = dmp_zz_heu_gcd(ff, gg, v, K)
|
| 1337 |
+
|
| 1338 |
+
h = _dmp_zz_gcd_interpolate(h, x, v, K)
|
| 1339 |
+
h = dmp_ground_primitive(h, u, K)[1]
|
| 1340 |
+
|
| 1341 |
+
cff_, r = dmp_div(f, h, u, K)
|
| 1342 |
+
|
| 1343 |
+
if dmp_zero_p(r, u):
|
| 1344 |
+
cfg_, r = dmp_div(g, h, u, K)
|
| 1345 |
+
|
| 1346 |
+
if dmp_zero_p(r, u):
|
| 1347 |
+
h = dmp_mul_ground(h, gcd, u, K)
|
| 1348 |
+
return h, cff_, cfg_
|
| 1349 |
+
|
| 1350 |
+
cff = _dmp_zz_gcd_interpolate(cff, x, v, K)
|
| 1351 |
+
|
| 1352 |
+
h, r = dmp_div(f, cff, u, K)
|
| 1353 |
+
|
| 1354 |
+
if dmp_zero_p(r, u):
|
| 1355 |
+
cfg_, r = dmp_div(g, h, u, K)
|
| 1356 |
+
|
| 1357 |
+
if dmp_zero_p(r, u):
|
| 1358 |
+
h = dmp_mul_ground(h, gcd, u, K)
|
| 1359 |
+
return h, cff, cfg_
|
| 1360 |
+
|
| 1361 |
+
cfg = _dmp_zz_gcd_interpolate(cfg, x, v, K)
|
| 1362 |
+
|
| 1363 |
+
h, r = dmp_div(g, cfg, u, K)
|
| 1364 |
+
|
| 1365 |
+
if dmp_zero_p(r, u):
|
| 1366 |
+
cff_, r = dmp_div(f, h, u, K)
|
| 1367 |
+
|
| 1368 |
+
if dmp_zero_p(r, u):
|
| 1369 |
+
h = dmp_mul_ground(h, gcd, u, K)
|
| 1370 |
+
return h, cff_, cfg
|
| 1371 |
+
|
| 1372 |
+
x = 73794*x * K.sqrt(K.sqrt(x)) // 27011
|
| 1373 |
+
|
| 1374 |
+
raise HeuristicGCDFailed('no luck')
|
| 1375 |
+
|
| 1376 |
+
|
| 1377 |
+
def dup_qq_heu_gcd(f, g, K0):
|
| 1378 |
+
"""
|
| 1379 |
+
Heuristic polynomial GCD in `Q[x]`.
|
| 1380 |
+
|
| 1381 |
+
Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
|
| 1382 |
+
``cff = quo(f, h)``, and ``cfg = quo(g, h)``.
|
| 1383 |
+
|
| 1384 |
+
Examples
|
| 1385 |
+
========
|
| 1386 |
+
|
| 1387 |
+
>>> from sympy.polys import ring, QQ
|
| 1388 |
+
>>> R, x = ring("x", QQ)
|
| 1389 |
+
|
| 1390 |
+
>>> f = QQ(1,2)*x**2 + QQ(7,4)*x + QQ(3,2)
|
| 1391 |
+
>>> g = QQ(1,2)*x**2 + x
|
| 1392 |
+
|
| 1393 |
+
>>> R.dup_qq_heu_gcd(f, g)
|
| 1394 |
+
(x + 2, 1/2*x + 3/4, 1/2*x)
|
| 1395 |
+
|
| 1396 |
+
"""
|
| 1397 |
+
result = _dup_ff_trivial_gcd(f, g, K0)
|
| 1398 |
+
|
| 1399 |
+
if result is not None:
|
| 1400 |
+
return result
|
| 1401 |
+
|
| 1402 |
+
K1 = K0.get_ring()
|
| 1403 |
+
|
| 1404 |
+
cf, f = dup_clear_denoms(f, K0, K1)
|
| 1405 |
+
cg, g = dup_clear_denoms(g, K0, K1)
|
| 1406 |
+
|
| 1407 |
+
f = dup_convert(f, K0, K1)
|
| 1408 |
+
g = dup_convert(g, K0, K1)
|
| 1409 |
+
|
| 1410 |
+
h, cff, cfg = dup_zz_heu_gcd(f, g, K1)
|
| 1411 |
+
|
| 1412 |
+
h = dup_convert(h, K1, K0)
|
| 1413 |
+
|
| 1414 |
+
c = dup_LC(h, K0)
|
| 1415 |
+
h = dup_monic(h, K0)
|
| 1416 |
+
|
| 1417 |
+
cff = dup_convert(cff, K1, K0)
|
| 1418 |
+
cfg = dup_convert(cfg, K1, K0)
|
| 1419 |
+
|
| 1420 |
+
cff = dup_mul_ground(cff, K0.quo(c, cf), K0)
|
| 1421 |
+
cfg = dup_mul_ground(cfg, K0.quo(c, cg), K0)
|
| 1422 |
+
|
| 1423 |
+
return h, cff, cfg
|
| 1424 |
+
|
| 1425 |
+
|
| 1426 |
+
def dmp_qq_heu_gcd(f, g, u, K0):
|
| 1427 |
+
"""
|
| 1428 |
+
Heuristic polynomial GCD in `Q[X]`.
|
| 1429 |
+
|
| 1430 |
+
Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
|
| 1431 |
+
``cff = quo(f, h)``, and ``cfg = quo(g, h)``.
|
| 1432 |
+
|
| 1433 |
+
Examples
|
| 1434 |
+
========
|
| 1435 |
+
|
| 1436 |
+
>>> from sympy.polys import ring, QQ
|
| 1437 |
+
>>> R, x,y, = ring("x,y", QQ)
|
| 1438 |
+
|
| 1439 |
+
>>> f = QQ(1,4)*x**2 + x*y + y**2
|
| 1440 |
+
>>> g = QQ(1,2)*x**2 + x*y
|
| 1441 |
+
|
| 1442 |
+
>>> R.dmp_qq_heu_gcd(f, g)
|
| 1443 |
+
(x + 2*y, 1/4*x + 1/2*y, 1/2*x)
|
| 1444 |
+
|
| 1445 |
+
"""
|
| 1446 |
+
result = _dmp_ff_trivial_gcd(f, g, u, K0)
|
| 1447 |
+
|
| 1448 |
+
if result is not None:
|
| 1449 |
+
return result
|
| 1450 |
+
|
| 1451 |
+
K1 = K0.get_ring()
|
| 1452 |
+
|
| 1453 |
+
cf, f = dmp_clear_denoms(f, u, K0, K1)
|
| 1454 |
+
cg, g = dmp_clear_denoms(g, u, K0, K1)
|
| 1455 |
+
|
| 1456 |
+
f = dmp_convert(f, u, K0, K1)
|
| 1457 |
+
g = dmp_convert(g, u, K0, K1)
|
| 1458 |
+
|
| 1459 |
+
h, cff, cfg = dmp_zz_heu_gcd(f, g, u, K1)
|
| 1460 |
+
|
| 1461 |
+
h = dmp_convert(h, u, K1, K0)
|
| 1462 |
+
|
| 1463 |
+
c = dmp_ground_LC(h, u, K0)
|
| 1464 |
+
h = dmp_ground_monic(h, u, K0)
|
| 1465 |
+
|
| 1466 |
+
cff = dmp_convert(cff, u, K1, K0)
|
| 1467 |
+
cfg = dmp_convert(cfg, u, K1, K0)
|
| 1468 |
+
|
| 1469 |
+
cff = dmp_mul_ground(cff, K0.quo(c, cf), u, K0)
|
| 1470 |
+
cfg = dmp_mul_ground(cfg, K0.quo(c, cg), u, K0)
|
| 1471 |
+
|
| 1472 |
+
return h, cff, cfg
|
| 1473 |
+
|
| 1474 |
+
|
| 1475 |
+
def dup_inner_gcd(f, g, K):
|
| 1476 |
+
"""
|
| 1477 |
+
Computes polynomial GCD and cofactors of `f` and `g` in `K[x]`.
|
| 1478 |
+
|
| 1479 |
+
Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
|
| 1480 |
+
``cff = quo(f, h)``, and ``cfg = quo(g, h)``.
|
| 1481 |
+
|
| 1482 |
+
Examples
|
| 1483 |
+
========
|
| 1484 |
+
|
| 1485 |
+
>>> from sympy.polys import ring, ZZ
|
| 1486 |
+
>>> R, x = ring("x", ZZ)
|
| 1487 |
+
|
| 1488 |
+
>>> R.dup_inner_gcd(x**2 - 1, x**2 - 3*x + 2)
|
| 1489 |
+
(x - 1, x + 1, x - 2)
|
| 1490 |
+
|
| 1491 |
+
"""
|
| 1492 |
+
# XXX: This used to check for K.is_Exact but leads to awkward results when
|
| 1493 |
+
# the domain is something like RR[z] e.g.:
|
| 1494 |
+
#
|
| 1495 |
+
# >>> g, p, q = Poly(1, x).cancel(Poly(51.05*x*y - 1.0, x))
|
| 1496 |
+
# >>> g
|
| 1497 |
+
# 1.0
|
| 1498 |
+
# >>> p
|
| 1499 |
+
# Poly(17592186044421.0, x, domain='RR[y]')
|
| 1500 |
+
# >>> q
|
| 1501 |
+
# Poly(898081097567692.0*y*x - 17592186044421.0, x, domain='RR[y]'))
|
| 1502 |
+
#
|
| 1503 |
+
# Maybe it would be better to flatten into multivariate polynomials first.
|
| 1504 |
+
if K.is_RR or K.is_CC:
|
| 1505 |
+
try:
|
| 1506 |
+
exact = K.get_exact()
|
| 1507 |
+
except DomainError:
|
| 1508 |
+
return [K.one], f, g
|
| 1509 |
+
|
| 1510 |
+
f = dup_convert(f, K, exact)
|
| 1511 |
+
g = dup_convert(g, K, exact)
|
| 1512 |
+
|
| 1513 |
+
h, cff, cfg = dup_inner_gcd(f, g, exact)
|
| 1514 |
+
|
| 1515 |
+
h = dup_convert(h, exact, K)
|
| 1516 |
+
cff = dup_convert(cff, exact, K)
|
| 1517 |
+
cfg = dup_convert(cfg, exact, K)
|
| 1518 |
+
|
| 1519 |
+
return h, cff, cfg
|
| 1520 |
+
elif K.is_Field:
|
| 1521 |
+
if K.is_QQ and query('USE_HEU_GCD'):
|
| 1522 |
+
try:
|
| 1523 |
+
return dup_qq_heu_gcd(f, g, K)
|
| 1524 |
+
except HeuristicGCDFailed:
|
| 1525 |
+
pass
|
| 1526 |
+
|
| 1527 |
+
return dup_ff_prs_gcd(f, g, K)
|
| 1528 |
+
else:
|
| 1529 |
+
if K.is_ZZ and query('USE_HEU_GCD'):
|
| 1530 |
+
try:
|
| 1531 |
+
return dup_zz_heu_gcd(f, g, K)
|
| 1532 |
+
except HeuristicGCDFailed:
|
| 1533 |
+
pass
|
| 1534 |
+
|
| 1535 |
+
return dup_rr_prs_gcd(f, g, K)
|
| 1536 |
+
|
| 1537 |
+
|
| 1538 |
+
def _dmp_inner_gcd(f, g, u, K):
|
| 1539 |
+
"""Helper function for `dmp_inner_gcd()`. """
|
| 1540 |
+
if not K.is_Exact:
|
| 1541 |
+
try:
|
| 1542 |
+
exact = K.get_exact()
|
| 1543 |
+
except DomainError:
|
| 1544 |
+
return dmp_one(u, K), f, g
|
| 1545 |
+
|
| 1546 |
+
f = dmp_convert(f, u, K, exact)
|
| 1547 |
+
g = dmp_convert(g, u, K, exact)
|
| 1548 |
+
|
| 1549 |
+
h, cff, cfg = _dmp_inner_gcd(f, g, u, exact)
|
| 1550 |
+
|
| 1551 |
+
h = dmp_convert(h, u, exact, K)
|
| 1552 |
+
cff = dmp_convert(cff, u, exact, K)
|
| 1553 |
+
cfg = dmp_convert(cfg, u, exact, K)
|
| 1554 |
+
|
| 1555 |
+
return h, cff, cfg
|
| 1556 |
+
elif K.is_Field:
|
| 1557 |
+
if K.is_QQ and query('USE_HEU_GCD'):
|
| 1558 |
+
try:
|
| 1559 |
+
return dmp_qq_heu_gcd(f, g, u, K)
|
| 1560 |
+
except HeuristicGCDFailed:
|
| 1561 |
+
pass
|
| 1562 |
+
|
| 1563 |
+
return dmp_ff_prs_gcd(f, g, u, K)
|
| 1564 |
+
else:
|
| 1565 |
+
if K.is_ZZ and query('USE_HEU_GCD'):
|
| 1566 |
+
try:
|
| 1567 |
+
return dmp_zz_heu_gcd(f, g, u, K)
|
| 1568 |
+
except HeuristicGCDFailed:
|
| 1569 |
+
pass
|
| 1570 |
+
|
| 1571 |
+
return dmp_rr_prs_gcd(f, g, u, K)
|
| 1572 |
+
|
| 1573 |
+
|
| 1574 |
+
def dmp_inner_gcd(f, g, u, K):
|
| 1575 |
+
"""
|
| 1576 |
+
Computes polynomial GCD and cofactors of `f` and `g` in `K[X]`.
|
| 1577 |
+
|
| 1578 |
+
Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
|
| 1579 |
+
``cff = quo(f, h)``, and ``cfg = quo(g, h)``.
|
| 1580 |
+
|
| 1581 |
+
Examples
|
| 1582 |
+
========
|
| 1583 |
+
|
| 1584 |
+
>>> from sympy.polys import ring, ZZ
|
| 1585 |
+
>>> R, x,y, = ring("x,y", ZZ)
|
| 1586 |
+
|
| 1587 |
+
>>> f = x**2 + 2*x*y + y**2
|
| 1588 |
+
>>> g = x**2 + x*y
|
| 1589 |
+
|
| 1590 |
+
>>> R.dmp_inner_gcd(f, g)
|
| 1591 |
+
(x + y, x + y, x)
|
| 1592 |
+
|
| 1593 |
+
"""
|
| 1594 |
+
if not u:
|
| 1595 |
+
return dup_inner_gcd(f, g, K)
|
| 1596 |
+
|
| 1597 |
+
J, (f, g) = dmp_multi_deflate((f, g), u, K)
|
| 1598 |
+
h, cff, cfg = _dmp_inner_gcd(f, g, u, K)
|
| 1599 |
+
|
| 1600 |
+
return (dmp_inflate(h, J, u, K),
|
| 1601 |
+
dmp_inflate(cff, J, u, K),
|
| 1602 |
+
dmp_inflate(cfg, J, u, K))
|
| 1603 |
+
|
| 1604 |
+
|
| 1605 |
+
def dup_gcd(f, g, K):
|
| 1606 |
+
"""
|
| 1607 |
+
Computes polynomial GCD of `f` and `g` in `K[x]`.
|
| 1608 |
+
|
| 1609 |
+
Examples
|
| 1610 |
+
========
|
| 1611 |
+
|
| 1612 |
+
>>> from sympy.polys import ring, ZZ
|
| 1613 |
+
>>> R, x = ring("x", ZZ)
|
| 1614 |
+
|
| 1615 |
+
>>> R.dup_gcd(x**2 - 1, x**2 - 3*x + 2)
|
| 1616 |
+
x - 1
|
| 1617 |
+
|
| 1618 |
+
"""
|
| 1619 |
+
return dup_inner_gcd(f, g, K)[0]
|
| 1620 |
+
|
| 1621 |
+
|
| 1622 |
+
def dmp_gcd(f, g, u, K):
|
| 1623 |
+
"""
|
| 1624 |
+
Computes polynomial GCD of `f` and `g` in `K[X]`.
|
| 1625 |
+
|
| 1626 |
+
Examples
|
| 1627 |
+
========
|
| 1628 |
+
|
| 1629 |
+
>>> from sympy.polys import ring, ZZ
|
| 1630 |
+
>>> R, x,y, = ring("x,y", ZZ)
|
| 1631 |
+
|
| 1632 |
+
>>> f = x**2 + 2*x*y + y**2
|
| 1633 |
+
>>> g = x**2 + x*y
|
| 1634 |
+
|
| 1635 |
+
>>> R.dmp_gcd(f, g)
|
| 1636 |
+
x + y
|
| 1637 |
+
|
| 1638 |
+
"""
|
| 1639 |
+
return dmp_inner_gcd(f, g, u, K)[0]
|
| 1640 |
+
|
| 1641 |
+
|
| 1642 |
+
def dup_rr_lcm(f, g, K):
|
| 1643 |
+
"""
|
| 1644 |
+
Computes polynomial LCM over a ring in `K[x]`.
|
| 1645 |
+
|
| 1646 |
+
Examples
|
| 1647 |
+
========
|
| 1648 |
+
|
| 1649 |
+
>>> from sympy.polys import ring, ZZ
|
| 1650 |
+
>>> R, x = ring("x", ZZ)
|
| 1651 |
+
|
| 1652 |
+
>>> R.dup_rr_lcm(x**2 - 1, x**2 - 3*x + 2)
|
| 1653 |
+
x**3 - 2*x**2 - x + 2
|
| 1654 |
+
|
| 1655 |
+
"""
|
| 1656 |
+
if not f or not g:
|
| 1657 |
+
return dmp_zero(0)
|
| 1658 |
+
|
| 1659 |
+
fc, f = dup_primitive(f, K)
|
| 1660 |
+
gc, g = dup_primitive(g, K)
|
| 1661 |
+
|
| 1662 |
+
c = K.lcm(fc, gc)
|
| 1663 |
+
|
| 1664 |
+
h = dup_quo(dup_mul(f, g, K),
|
| 1665 |
+
dup_gcd(f, g, K), K)
|
| 1666 |
+
|
| 1667 |
+
u = K.canonical_unit(dup_LC(h, K))
|
| 1668 |
+
|
| 1669 |
+
return dup_mul_ground(h, c*u, K)
|
| 1670 |
+
|
| 1671 |
+
|
| 1672 |
+
def dup_ff_lcm(f, g, K):
|
| 1673 |
+
"""
|
| 1674 |
+
Computes polynomial LCM over a field in `K[x]`.
|
| 1675 |
+
|
| 1676 |
+
Examples
|
| 1677 |
+
========
|
| 1678 |
+
|
| 1679 |
+
>>> from sympy.polys import ring, QQ
|
| 1680 |
+
>>> R, x = ring("x", QQ)
|
| 1681 |
+
|
| 1682 |
+
>>> f = QQ(1,2)*x**2 + QQ(7,4)*x + QQ(3,2)
|
| 1683 |
+
>>> g = QQ(1,2)*x**2 + x
|
| 1684 |
+
|
| 1685 |
+
>>> R.dup_ff_lcm(f, g)
|
| 1686 |
+
x**3 + 7/2*x**2 + 3*x
|
| 1687 |
+
|
| 1688 |
+
"""
|
| 1689 |
+
h = dup_quo(dup_mul(f, g, K),
|
| 1690 |
+
dup_gcd(f, g, K), K)
|
| 1691 |
+
|
| 1692 |
+
return dup_monic(h, K)
|
| 1693 |
+
|
| 1694 |
+
|
| 1695 |
+
def dup_lcm(f, g, K):
|
| 1696 |
+
"""
|
| 1697 |
+
Computes polynomial LCM of `f` and `g` in `K[x]`.
|
| 1698 |
+
|
| 1699 |
+
Examples
|
| 1700 |
+
========
|
| 1701 |
+
|
| 1702 |
+
>>> from sympy.polys import ring, ZZ
|
| 1703 |
+
>>> R, x = ring("x", ZZ)
|
| 1704 |
+
|
| 1705 |
+
>>> R.dup_lcm(x**2 - 1, x**2 - 3*x + 2)
|
| 1706 |
+
x**3 - 2*x**2 - x + 2
|
| 1707 |
+
|
| 1708 |
+
"""
|
| 1709 |
+
if K.is_Field:
|
| 1710 |
+
return dup_ff_lcm(f, g, K)
|
| 1711 |
+
else:
|
| 1712 |
+
return dup_rr_lcm(f, g, K)
|
| 1713 |
+
|
| 1714 |
+
|
| 1715 |
+
def dmp_rr_lcm(f, g, u, K):
|
| 1716 |
+
"""
|
| 1717 |
+
Computes polynomial LCM over a ring in `K[X]`.
|
| 1718 |
+
|
| 1719 |
+
Examples
|
| 1720 |
+
========
|
| 1721 |
+
|
| 1722 |
+
>>> from sympy.polys import ring, ZZ
|
| 1723 |
+
>>> R, x,y, = ring("x,y", ZZ)
|
| 1724 |
+
|
| 1725 |
+
>>> f = x**2 + 2*x*y + y**2
|
| 1726 |
+
>>> g = x**2 + x*y
|
| 1727 |
+
|
| 1728 |
+
>>> R.dmp_rr_lcm(f, g)
|
| 1729 |
+
x**3 + 2*x**2*y + x*y**2
|
| 1730 |
+
|
| 1731 |
+
"""
|
| 1732 |
+
fc, f = dmp_ground_primitive(f, u, K)
|
| 1733 |
+
gc, g = dmp_ground_primitive(g, u, K)
|
| 1734 |
+
|
| 1735 |
+
c = K.lcm(fc, gc)
|
| 1736 |
+
|
| 1737 |
+
h = dmp_quo(dmp_mul(f, g, u, K),
|
| 1738 |
+
dmp_gcd(f, g, u, K), u, K)
|
| 1739 |
+
|
| 1740 |
+
return dmp_mul_ground(h, c, u, K)
|
| 1741 |
+
|
| 1742 |
+
|
| 1743 |
+
def dmp_ff_lcm(f, g, u, K):
|
| 1744 |
+
"""
|
| 1745 |
+
Computes polynomial LCM over a field in `K[X]`.
|
| 1746 |
+
|
| 1747 |
+
Examples
|
| 1748 |
+
========
|
| 1749 |
+
|
| 1750 |
+
>>> from sympy.polys import ring, QQ
|
| 1751 |
+
>>> R, x,y, = ring("x,y", QQ)
|
| 1752 |
+
|
| 1753 |
+
>>> f = QQ(1,4)*x**2 + x*y + y**2
|
| 1754 |
+
>>> g = QQ(1,2)*x**2 + x*y
|
| 1755 |
+
|
| 1756 |
+
>>> R.dmp_ff_lcm(f, g)
|
| 1757 |
+
x**3 + 4*x**2*y + 4*x*y**2
|
| 1758 |
+
|
| 1759 |
+
"""
|
| 1760 |
+
h = dmp_quo(dmp_mul(f, g, u, K),
|
| 1761 |
+
dmp_gcd(f, g, u, K), u, K)
|
| 1762 |
+
|
| 1763 |
+
return dmp_ground_monic(h, u, K)
|
| 1764 |
+
|
| 1765 |
+
|
| 1766 |
+
def dmp_lcm(f, g, u, K):
|
| 1767 |
+
"""
|
| 1768 |
+
Computes polynomial LCM of `f` and `g` in `K[X]`.
|
| 1769 |
+
|
| 1770 |
+
Examples
|
| 1771 |
+
========
|
| 1772 |
+
|
| 1773 |
+
>>> from sympy.polys import ring, ZZ
|
| 1774 |
+
>>> R, x,y, = ring("x,y", ZZ)
|
| 1775 |
+
|
| 1776 |
+
>>> f = x**2 + 2*x*y + y**2
|
| 1777 |
+
>>> g = x**2 + x*y
|
| 1778 |
+
|
| 1779 |
+
>>> R.dmp_lcm(f, g)
|
| 1780 |
+
x**3 + 2*x**2*y + x*y**2
|
| 1781 |
+
|
| 1782 |
+
"""
|
| 1783 |
+
if not u:
|
| 1784 |
+
return dup_lcm(f, g, K)
|
| 1785 |
+
|
| 1786 |
+
if K.is_Field:
|
| 1787 |
+
return dmp_ff_lcm(f, g, u, K)
|
| 1788 |
+
else:
|
| 1789 |
+
return dmp_rr_lcm(f, g, u, K)
|
| 1790 |
+
|
| 1791 |
+
|
| 1792 |
+
def dmp_content(f, u, K):
|
| 1793 |
+
"""
|
| 1794 |
+
Returns GCD of multivariate coefficients.
|
| 1795 |
+
|
| 1796 |
+
Examples
|
| 1797 |
+
========
|
| 1798 |
+
|
| 1799 |
+
>>> from sympy.polys import ring, ZZ
|
| 1800 |
+
>>> R, x,y, = ring("x,y", ZZ)
|
| 1801 |
+
|
| 1802 |
+
>>> R.dmp_content(2*x*y + 6*x + 4*y + 12)
|
| 1803 |
+
2*y + 6
|
| 1804 |
+
|
| 1805 |
+
"""
|
| 1806 |
+
cont, v = dmp_LC(f, K), u - 1
|
| 1807 |
+
|
| 1808 |
+
if dmp_zero_p(f, u):
|
| 1809 |
+
return cont
|
| 1810 |
+
|
| 1811 |
+
for c in f[1:]:
|
| 1812 |
+
cont = dmp_gcd(cont, c, v, K)
|
| 1813 |
+
|
| 1814 |
+
if dmp_one_p(cont, v, K):
|
| 1815 |
+
break
|
| 1816 |
+
|
| 1817 |
+
if K.is_negative(dmp_ground_LC(cont, v, K)):
|
| 1818 |
+
return dmp_neg(cont, v, K)
|
| 1819 |
+
else:
|
| 1820 |
+
return cont
|
| 1821 |
+
|
| 1822 |
+
|
| 1823 |
+
def dmp_primitive(f, u, K):
|
| 1824 |
+
"""
|
| 1825 |
+
Returns multivariate content and a primitive polynomial.
|
| 1826 |
+
|
| 1827 |
+
Examples
|
| 1828 |
+
========
|
| 1829 |
+
|
| 1830 |
+
>>> from sympy.polys import ring, ZZ
|
| 1831 |
+
>>> R, x,y, = ring("x,y", ZZ)
|
| 1832 |
+
|
| 1833 |
+
>>> R.dmp_primitive(2*x*y + 6*x + 4*y + 12)
|
| 1834 |
+
(2*y + 6, x + 2)
|
| 1835 |
+
|
| 1836 |
+
"""
|
| 1837 |
+
cont, v = dmp_content(f, u, K), u - 1
|
| 1838 |
+
|
| 1839 |
+
if dmp_zero_p(f, u) or dmp_one_p(cont, v, K):
|
| 1840 |
+
return cont, f
|
| 1841 |
+
else:
|
| 1842 |
+
return cont, [ dmp_quo(c, cont, v, K) for c in f ]
|
| 1843 |
+
|
| 1844 |
+
|
| 1845 |
+
def dup_cancel(f, g, K, include=True):
|
| 1846 |
+
"""
|
| 1847 |
+
Cancel common factors in a rational function `f/g`.
|
| 1848 |
+
|
| 1849 |
+
Examples
|
| 1850 |
+
========
|
| 1851 |
+
|
| 1852 |
+
>>> from sympy.polys import ring, ZZ
|
| 1853 |
+
>>> R, x = ring("x", ZZ)
|
| 1854 |
+
|
| 1855 |
+
>>> R.dup_cancel(2*x**2 - 2, x**2 - 2*x + 1)
|
| 1856 |
+
(2*x + 2, x - 1)
|
| 1857 |
+
|
| 1858 |
+
"""
|
| 1859 |
+
return dmp_cancel(f, g, 0, K, include=include)
|
| 1860 |
+
|
| 1861 |
+
|
| 1862 |
+
def dmp_cancel(f, g, u, K, include=True):
|
| 1863 |
+
"""
|
| 1864 |
+
Cancel common factors in a rational function `f/g`.
|
| 1865 |
+
|
| 1866 |
+
Examples
|
| 1867 |
+
========
|
| 1868 |
+
|
| 1869 |
+
>>> from sympy.polys import ring, ZZ
|
| 1870 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1871 |
+
|
| 1872 |
+
>>> R.dmp_cancel(2*x**2 - 2, x**2 - 2*x + 1)
|
| 1873 |
+
(2*x + 2, x - 1)
|
| 1874 |
+
|
| 1875 |
+
"""
|
| 1876 |
+
K0 = None
|
| 1877 |
+
|
| 1878 |
+
if K.is_Field and K.has_assoc_Ring:
|
| 1879 |
+
K0, K = K, K.get_ring()
|
| 1880 |
+
|
| 1881 |
+
cq, f = dmp_clear_denoms(f, u, K0, K, convert=True)
|
| 1882 |
+
cp, g = dmp_clear_denoms(g, u, K0, K, convert=True)
|
| 1883 |
+
else:
|
| 1884 |
+
cp, cq = K.one, K.one
|
| 1885 |
+
|
| 1886 |
+
_, p, q = dmp_inner_gcd(f, g, u, K)
|
| 1887 |
+
|
| 1888 |
+
if K0 is not None:
|
| 1889 |
+
_, cp, cq = K.cofactors(cp, cq)
|
| 1890 |
+
|
| 1891 |
+
p = dmp_convert(p, u, K, K0)
|
| 1892 |
+
q = dmp_convert(q, u, K, K0)
|
| 1893 |
+
|
| 1894 |
+
K = K0
|
| 1895 |
+
|
| 1896 |
+
p_neg = K.is_negative(dmp_ground_LC(p, u, K))
|
| 1897 |
+
q_neg = K.is_negative(dmp_ground_LC(q, u, K))
|
| 1898 |
+
|
| 1899 |
+
if p_neg and q_neg:
|
| 1900 |
+
p, q = dmp_neg(p, u, K), dmp_neg(q, u, K)
|
| 1901 |
+
elif p_neg:
|
| 1902 |
+
cp, p = -cp, dmp_neg(p, u, K)
|
| 1903 |
+
elif q_neg:
|
| 1904 |
+
cp, q = -cp, dmp_neg(q, u, K)
|
| 1905 |
+
|
| 1906 |
+
if not include:
|
| 1907 |
+
return cp, cq, p, q
|
| 1908 |
+
|
| 1909 |
+
p = dmp_mul_ground(p, cp, u, K)
|
| 1910 |
+
q = dmp_mul_ground(q, cq, u, K)
|
| 1911 |
+
|
| 1912 |
+
return p, q
|
vllm/lib/python3.10/site-packages/sympy/polys/factortools.py
ADDED
|
@@ -0,0 +1,1648 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Polynomial factorization routines in characteristic zero. """
|
| 2 |
+
|
| 3 |
+
from sympy.external.gmpy import GROUND_TYPES
|
| 4 |
+
|
| 5 |
+
from sympy.core.random import _randint
|
| 6 |
+
|
| 7 |
+
from sympy.polys.galoistools import (
|
| 8 |
+
gf_from_int_poly, gf_to_int_poly,
|
| 9 |
+
gf_lshift, gf_add_mul, gf_mul,
|
| 10 |
+
gf_div, gf_rem,
|
| 11 |
+
gf_gcdex,
|
| 12 |
+
gf_sqf_p,
|
| 13 |
+
gf_factor_sqf, gf_factor)
|
| 14 |
+
|
| 15 |
+
from sympy.polys.densebasic import (
|
| 16 |
+
dup_LC, dmp_LC, dmp_ground_LC,
|
| 17 |
+
dup_TC,
|
| 18 |
+
dup_convert, dmp_convert,
|
| 19 |
+
dup_degree, dmp_degree,
|
| 20 |
+
dmp_degree_in, dmp_degree_list,
|
| 21 |
+
dmp_from_dict,
|
| 22 |
+
dmp_zero_p,
|
| 23 |
+
dmp_one,
|
| 24 |
+
dmp_nest, dmp_raise,
|
| 25 |
+
dup_strip,
|
| 26 |
+
dmp_ground,
|
| 27 |
+
dup_inflate,
|
| 28 |
+
dmp_exclude, dmp_include,
|
| 29 |
+
dmp_inject, dmp_eject,
|
| 30 |
+
dup_terms_gcd, dmp_terms_gcd)
|
| 31 |
+
|
| 32 |
+
from sympy.polys.densearith import (
|
| 33 |
+
dup_neg, dmp_neg,
|
| 34 |
+
dup_add, dmp_add,
|
| 35 |
+
dup_sub, dmp_sub,
|
| 36 |
+
dup_mul, dmp_mul,
|
| 37 |
+
dup_sqr,
|
| 38 |
+
dmp_pow,
|
| 39 |
+
dup_div, dmp_div,
|
| 40 |
+
dup_quo, dmp_quo,
|
| 41 |
+
dmp_expand,
|
| 42 |
+
dmp_add_mul,
|
| 43 |
+
dup_sub_mul, dmp_sub_mul,
|
| 44 |
+
dup_lshift,
|
| 45 |
+
dup_max_norm, dmp_max_norm,
|
| 46 |
+
dup_l1_norm,
|
| 47 |
+
dup_mul_ground, dmp_mul_ground,
|
| 48 |
+
dup_quo_ground, dmp_quo_ground)
|
| 49 |
+
|
| 50 |
+
from sympy.polys.densetools import (
|
| 51 |
+
dup_clear_denoms, dmp_clear_denoms,
|
| 52 |
+
dup_trunc, dmp_ground_trunc,
|
| 53 |
+
dup_content,
|
| 54 |
+
dup_monic, dmp_ground_monic,
|
| 55 |
+
dup_primitive, dmp_ground_primitive,
|
| 56 |
+
dmp_eval_tail,
|
| 57 |
+
dmp_eval_in, dmp_diff_eval_in,
|
| 58 |
+
dup_shift, dmp_shift, dup_mirror)
|
| 59 |
+
|
| 60 |
+
from sympy.polys.euclidtools import (
|
| 61 |
+
dmp_primitive,
|
| 62 |
+
dup_inner_gcd, dmp_inner_gcd)
|
| 63 |
+
|
| 64 |
+
from sympy.polys.sqfreetools import (
|
| 65 |
+
dup_sqf_p,
|
| 66 |
+
dup_sqf_norm, dmp_sqf_norm,
|
| 67 |
+
dup_sqf_part, dmp_sqf_part,
|
| 68 |
+
_dup_check_degrees, _dmp_check_degrees,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
from sympy.polys.polyutils import _sort_factors
|
| 72 |
+
from sympy.polys.polyconfig import query
|
| 73 |
+
|
| 74 |
+
from sympy.polys.polyerrors import (
|
| 75 |
+
ExtraneousFactors, DomainError, CoercionFailed, EvaluationFailed)
|
| 76 |
+
|
| 77 |
+
from sympy.utilities import subsets
|
| 78 |
+
|
| 79 |
+
from math import ceil as _ceil, log as _log, log2 as _log2
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
if GROUND_TYPES == 'flint':
|
| 83 |
+
from flint import fmpz_poly
|
| 84 |
+
else:
|
| 85 |
+
fmpz_poly = None
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def dup_trial_division(f, factors, K):
|
| 89 |
+
"""
|
| 90 |
+
Determine multiplicities of factors for a univariate polynomial
|
| 91 |
+
using trial division.
|
| 92 |
+
|
| 93 |
+
An error will be raised if any factor does not divide ``f``.
|
| 94 |
+
"""
|
| 95 |
+
result = []
|
| 96 |
+
|
| 97 |
+
for factor in factors:
|
| 98 |
+
k = 0
|
| 99 |
+
|
| 100 |
+
while True:
|
| 101 |
+
q, r = dup_div(f, factor, K)
|
| 102 |
+
|
| 103 |
+
if not r:
|
| 104 |
+
f, k = q, k + 1
|
| 105 |
+
else:
|
| 106 |
+
break
|
| 107 |
+
|
| 108 |
+
if k == 0:
|
| 109 |
+
raise RuntimeError("trial division failed")
|
| 110 |
+
|
| 111 |
+
result.append((factor, k))
|
| 112 |
+
|
| 113 |
+
return _sort_factors(result)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def dmp_trial_division(f, factors, u, K):
|
| 117 |
+
"""
|
| 118 |
+
Determine multiplicities of factors for a multivariate polynomial
|
| 119 |
+
using trial division.
|
| 120 |
+
|
| 121 |
+
An error will be raised if any factor does not divide ``f``.
|
| 122 |
+
"""
|
| 123 |
+
result = []
|
| 124 |
+
|
| 125 |
+
for factor in factors:
|
| 126 |
+
k = 0
|
| 127 |
+
|
| 128 |
+
while True:
|
| 129 |
+
q, r = dmp_div(f, factor, u, K)
|
| 130 |
+
|
| 131 |
+
if dmp_zero_p(r, u):
|
| 132 |
+
f, k = q, k + 1
|
| 133 |
+
else:
|
| 134 |
+
break
|
| 135 |
+
|
| 136 |
+
if k == 0:
|
| 137 |
+
raise RuntimeError("trial division failed")
|
| 138 |
+
|
| 139 |
+
result.append((factor, k))
|
| 140 |
+
|
| 141 |
+
return _sort_factors(result)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def dup_zz_mignotte_bound(f, K):
|
| 145 |
+
"""
|
| 146 |
+
The Knuth-Cohen variant of Mignotte bound for
|
| 147 |
+
univariate polynomials in ``K[x]``.
|
| 148 |
+
|
| 149 |
+
Examples
|
| 150 |
+
========
|
| 151 |
+
|
| 152 |
+
>>> from sympy.polys import ring, ZZ
|
| 153 |
+
>>> R, x = ring("x", ZZ)
|
| 154 |
+
|
| 155 |
+
>>> f = x**3 + 14*x**2 + 56*x + 64
|
| 156 |
+
>>> R.dup_zz_mignotte_bound(f)
|
| 157 |
+
152
|
| 158 |
+
|
| 159 |
+
By checking ``factor(f)`` we can see that max coeff is 8
|
| 160 |
+
|
| 161 |
+
Also consider a case that ``f`` is irreducible for example
|
| 162 |
+
``f = 2*x**2 + 3*x + 4``. To avoid a bug for these cases, we return the
|
| 163 |
+
bound plus the max coefficient of ``f``
|
| 164 |
+
|
| 165 |
+
>>> f = 2*x**2 + 3*x + 4
|
| 166 |
+
>>> R.dup_zz_mignotte_bound(f)
|
| 167 |
+
6
|
| 168 |
+
|
| 169 |
+
Lastly, to see the difference between the new and the old Mignotte bound
|
| 170 |
+
consider the irreducible polynomial:
|
| 171 |
+
|
| 172 |
+
>>> f = 87*x**7 + 4*x**6 + 80*x**5 + 17*x**4 + 9*x**3 + 12*x**2 + 49*x + 26
|
| 173 |
+
>>> R.dup_zz_mignotte_bound(f)
|
| 174 |
+
744
|
| 175 |
+
|
| 176 |
+
The new Mignotte bound is 744 whereas the old one (SymPy 1.5.1) is 1937664.
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
References
|
| 180 |
+
==========
|
| 181 |
+
|
| 182 |
+
..[1] [Abbott13]_
|
| 183 |
+
|
| 184 |
+
"""
|
| 185 |
+
from sympy.functions.combinatorial.factorials import binomial
|
| 186 |
+
d = dup_degree(f)
|
| 187 |
+
delta = _ceil(d / 2)
|
| 188 |
+
delta2 = _ceil(delta / 2)
|
| 189 |
+
|
| 190 |
+
# euclidean-norm
|
| 191 |
+
eucl_norm = K.sqrt( sum( cf**2 for cf in f ) )
|
| 192 |
+
|
| 193 |
+
# biggest values of binomial coefficients (p. 538 of reference)
|
| 194 |
+
t1 = binomial(delta - 1, delta2)
|
| 195 |
+
t2 = binomial(delta - 1, delta2 - 1)
|
| 196 |
+
|
| 197 |
+
lc = K.abs(dup_LC(f, K)) # leading coefficient
|
| 198 |
+
bound = t1 * eucl_norm + t2 * lc # (p. 538 of reference)
|
| 199 |
+
bound += dup_max_norm(f, K) # add max coeff for irreducible polys
|
| 200 |
+
bound = _ceil(bound / 2) * 2 # round up to even integer
|
| 201 |
+
|
| 202 |
+
return bound
|
| 203 |
+
|
| 204 |
+
def dmp_zz_mignotte_bound(f, u, K):
|
| 205 |
+
"""Mignotte bound for multivariate polynomials in `K[X]`. """
|
| 206 |
+
a = dmp_max_norm(f, u, K)
|
| 207 |
+
b = abs(dmp_ground_LC(f, u, K))
|
| 208 |
+
n = sum(dmp_degree_list(f, u))
|
| 209 |
+
|
| 210 |
+
return K.sqrt(K(n + 1))*2**n*a*b
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def dup_zz_hensel_step(m, f, g, h, s, t, K):
|
| 214 |
+
"""
|
| 215 |
+
One step in Hensel lifting in `Z[x]`.
|
| 216 |
+
|
| 217 |
+
Given positive integer `m` and `Z[x]` polynomials `f`, `g`, `h`, `s`
|
| 218 |
+
and `t` such that::
|
| 219 |
+
|
| 220 |
+
f = g*h (mod m)
|
| 221 |
+
s*g + t*h = 1 (mod m)
|
| 222 |
+
|
| 223 |
+
lc(f) is not a zero divisor (mod m)
|
| 224 |
+
lc(h) = 1
|
| 225 |
+
|
| 226 |
+
deg(f) = deg(g) + deg(h)
|
| 227 |
+
deg(s) < deg(h)
|
| 228 |
+
deg(t) < deg(g)
|
| 229 |
+
|
| 230 |
+
returns polynomials `G`, `H`, `S` and `T`, such that::
|
| 231 |
+
|
| 232 |
+
f = G*H (mod m**2)
|
| 233 |
+
S*G + T*H = 1 (mod m**2)
|
| 234 |
+
|
| 235 |
+
References
|
| 236 |
+
==========
|
| 237 |
+
|
| 238 |
+
.. [1] [Gathen99]_
|
| 239 |
+
|
| 240 |
+
"""
|
| 241 |
+
M = m**2
|
| 242 |
+
|
| 243 |
+
e = dup_sub_mul(f, g, h, K)
|
| 244 |
+
e = dup_trunc(e, M, K)
|
| 245 |
+
|
| 246 |
+
q, r = dup_div(dup_mul(s, e, K), h, K)
|
| 247 |
+
|
| 248 |
+
q = dup_trunc(q, M, K)
|
| 249 |
+
r = dup_trunc(r, M, K)
|
| 250 |
+
|
| 251 |
+
u = dup_add(dup_mul(t, e, K), dup_mul(q, g, K), K)
|
| 252 |
+
G = dup_trunc(dup_add(g, u, K), M, K)
|
| 253 |
+
H = dup_trunc(dup_add(h, r, K), M, K)
|
| 254 |
+
|
| 255 |
+
u = dup_add(dup_mul(s, G, K), dup_mul(t, H, K), K)
|
| 256 |
+
b = dup_trunc(dup_sub(u, [K.one], K), M, K)
|
| 257 |
+
|
| 258 |
+
c, d = dup_div(dup_mul(s, b, K), H, K)
|
| 259 |
+
|
| 260 |
+
c = dup_trunc(c, M, K)
|
| 261 |
+
d = dup_trunc(d, M, K)
|
| 262 |
+
|
| 263 |
+
u = dup_add(dup_mul(t, b, K), dup_mul(c, G, K), K)
|
| 264 |
+
S = dup_trunc(dup_sub(s, d, K), M, K)
|
| 265 |
+
T = dup_trunc(dup_sub(t, u, K), M, K)
|
| 266 |
+
|
| 267 |
+
return G, H, S, T
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
def dup_zz_hensel_lift(p, f, f_list, l, K):
|
| 271 |
+
r"""
|
| 272 |
+
Multifactor Hensel lifting in `Z[x]`.
|
| 273 |
+
|
| 274 |
+
Given a prime `p`, polynomial `f` over `Z[x]` such that `lc(f)`
|
| 275 |
+
is a unit modulo `p`, monic pair-wise coprime polynomials `f_i`
|
| 276 |
+
over `Z[x]` satisfying::
|
| 277 |
+
|
| 278 |
+
f = lc(f) f_1 ... f_r (mod p)
|
| 279 |
+
|
| 280 |
+
and a positive integer `l`, returns a list of monic polynomials
|
| 281 |
+
`F_1,\ F_2,\ \dots,\ F_r` satisfying::
|
| 282 |
+
|
| 283 |
+
f = lc(f) F_1 ... F_r (mod p**l)
|
| 284 |
+
|
| 285 |
+
F_i = f_i (mod p), i = 1..r
|
| 286 |
+
|
| 287 |
+
References
|
| 288 |
+
==========
|
| 289 |
+
|
| 290 |
+
.. [1] [Gathen99]_
|
| 291 |
+
|
| 292 |
+
"""
|
| 293 |
+
r = len(f_list)
|
| 294 |
+
lc = dup_LC(f, K)
|
| 295 |
+
|
| 296 |
+
if r == 1:
|
| 297 |
+
F = dup_mul_ground(f, K.gcdex(lc, p**l)[0], K)
|
| 298 |
+
return [ dup_trunc(F, p**l, K) ]
|
| 299 |
+
|
| 300 |
+
m = p
|
| 301 |
+
k = r // 2
|
| 302 |
+
d = int(_ceil(_log2(l)))
|
| 303 |
+
|
| 304 |
+
g = gf_from_int_poly([lc], p)
|
| 305 |
+
|
| 306 |
+
for f_i in f_list[:k]:
|
| 307 |
+
g = gf_mul(g, gf_from_int_poly(f_i, p), p, K)
|
| 308 |
+
|
| 309 |
+
h = gf_from_int_poly(f_list[k], p)
|
| 310 |
+
|
| 311 |
+
for f_i in f_list[k + 1:]:
|
| 312 |
+
h = gf_mul(h, gf_from_int_poly(f_i, p), p, K)
|
| 313 |
+
|
| 314 |
+
s, t, _ = gf_gcdex(g, h, p, K)
|
| 315 |
+
|
| 316 |
+
g = gf_to_int_poly(g, p)
|
| 317 |
+
h = gf_to_int_poly(h, p)
|
| 318 |
+
s = gf_to_int_poly(s, p)
|
| 319 |
+
t = gf_to_int_poly(t, p)
|
| 320 |
+
|
| 321 |
+
for _ in range(1, d + 1):
|
| 322 |
+
(g, h, s, t), m = dup_zz_hensel_step(m, f, g, h, s, t, K), m**2
|
| 323 |
+
|
| 324 |
+
return dup_zz_hensel_lift(p, g, f_list[:k], l, K) \
|
| 325 |
+
+ dup_zz_hensel_lift(p, h, f_list[k:], l, K)
|
| 326 |
+
|
| 327 |
+
def _test_pl(fc, q, pl):
|
| 328 |
+
if q > pl // 2:
|
| 329 |
+
q = q - pl
|
| 330 |
+
if not q:
|
| 331 |
+
return True
|
| 332 |
+
return fc % q == 0
|
| 333 |
+
|
| 334 |
+
def dup_zz_zassenhaus(f, K):
|
| 335 |
+
"""Factor primitive square-free polynomials in `Z[x]`. """
|
| 336 |
+
n = dup_degree(f)
|
| 337 |
+
|
| 338 |
+
if n == 1:
|
| 339 |
+
return [f]
|
| 340 |
+
|
| 341 |
+
from sympy.ntheory import isprime
|
| 342 |
+
|
| 343 |
+
fc = f[-1]
|
| 344 |
+
A = dup_max_norm(f, K)
|
| 345 |
+
b = dup_LC(f, K)
|
| 346 |
+
B = int(abs(K.sqrt(K(n + 1))*2**n*A*b))
|
| 347 |
+
C = int((n + 1)**(2*n)*A**(2*n - 1))
|
| 348 |
+
gamma = int(_ceil(2*_log2(C)))
|
| 349 |
+
bound = int(2*gamma*_log(gamma))
|
| 350 |
+
a = []
|
| 351 |
+
# choose a prime number `p` such that `f` be square free in Z_p
|
| 352 |
+
# if there are many factors in Z_p, choose among a few different `p`
|
| 353 |
+
# the one with fewer factors
|
| 354 |
+
for px in range(3, bound + 1):
|
| 355 |
+
if not isprime(px) or b % px == 0:
|
| 356 |
+
continue
|
| 357 |
+
|
| 358 |
+
px = K.convert(px)
|
| 359 |
+
|
| 360 |
+
F = gf_from_int_poly(f, px)
|
| 361 |
+
|
| 362 |
+
if not gf_sqf_p(F, px, K):
|
| 363 |
+
continue
|
| 364 |
+
fsqfx = gf_factor_sqf(F, px, K)[1]
|
| 365 |
+
a.append((px, fsqfx))
|
| 366 |
+
if len(fsqfx) < 15 or len(a) > 4:
|
| 367 |
+
break
|
| 368 |
+
p, fsqf = min(a, key=lambda x: len(x[1]))
|
| 369 |
+
|
| 370 |
+
l = int(_ceil(_log(2*B + 1, p)))
|
| 371 |
+
|
| 372 |
+
modular = [gf_to_int_poly(ff, p) for ff in fsqf]
|
| 373 |
+
|
| 374 |
+
g = dup_zz_hensel_lift(p, f, modular, l, K)
|
| 375 |
+
|
| 376 |
+
sorted_T = range(len(g))
|
| 377 |
+
T = set(sorted_T)
|
| 378 |
+
factors, s = [], 1
|
| 379 |
+
pl = p**l
|
| 380 |
+
|
| 381 |
+
while 2*s <= len(T):
|
| 382 |
+
for S in subsets(sorted_T, s):
|
| 383 |
+
# lift the constant coefficient of the product `G` of the factors
|
| 384 |
+
# in the subset `S`; if it is does not divide `fc`, `G` does
|
| 385 |
+
# not divide the input polynomial
|
| 386 |
+
|
| 387 |
+
if b == 1:
|
| 388 |
+
q = 1
|
| 389 |
+
for i in S:
|
| 390 |
+
q = q*g[i][-1]
|
| 391 |
+
q = q % pl
|
| 392 |
+
if not _test_pl(fc, q, pl):
|
| 393 |
+
continue
|
| 394 |
+
else:
|
| 395 |
+
G = [b]
|
| 396 |
+
for i in S:
|
| 397 |
+
G = dup_mul(G, g[i], K)
|
| 398 |
+
G = dup_trunc(G, pl, K)
|
| 399 |
+
G = dup_primitive(G, K)[1]
|
| 400 |
+
q = G[-1]
|
| 401 |
+
if q and fc % q != 0:
|
| 402 |
+
continue
|
| 403 |
+
|
| 404 |
+
H = [b]
|
| 405 |
+
S = set(S)
|
| 406 |
+
T_S = T - S
|
| 407 |
+
|
| 408 |
+
if b == 1:
|
| 409 |
+
G = [b]
|
| 410 |
+
for i in S:
|
| 411 |
+
G = dup_mul(G, g[i], K)
|
| 412 |
+
G = dup_trunc(G, pl, K)
|
| 413 |
+
|
| 414 |
+
for i in T_S:
|
| 415 |
+
H = dup_mul(H, g[i], K)
|
| 416 |
+
|
| 417 |
+
H = dup_trunc(H, pl, K)
|
| 418 |
+
|
| 419 |
+
G_norm = dup_l1_norm(G, K)
|
| 420 |
+
H_norm = dup_l1_norm(H, K)
|
| 421 |
+
|
| 422 |
+
if G_norm*H_norm <= B:
|
| 423 |
+
T = T_S
|
| 424 |
+
sorted_T = [i for i in sorted_T if i not in S]
|
| 425 |
+
|
| 426 |
+
G = dup_primitive(G, K)[1]
|
| 427 |
+
f = dup_primitive(H, K)[1]
|
| 428 |
+
|
| 429 |
+
factors.append(G)
|
| 430 |
+
b = dup_LC(f, K)
|
| 431 |
+
|
| 432 |
+
break
|
| 433 |
+
else:
|
| 434 |
+
s += 1
|
| 435 |
+
|
| 436 |
+
return factors + [f]
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
def dup_zz_irreducible_p(f, K):
|
| 440 |
+
"""Test irreducibility using Eisenstein's criterion. """
|
| 441 |
+
lc = dup_LC(f, K)
|
| 442 |
+
tc = dup_TC(f, K)
|
| 443 |
+
|
| 444 |
+
e_fc = dup_content(f[1:], K)
|
| 445 |
+
|
| 446 |
+
if e_fc:
|
| 447 |
+
from sympy.ntheory import factorint
|
| 448 |
+
e_ff = factorint(int(e_fc))
|
| 449 |
+
|
| 450 |
+
for p in e_ff.keys():
|
| 451 |
+
if (lc % p) and (tc % p**2):
|
| 452 |
+
return True
|
| 453 |
+
|
| 454 |
+
|
| 455 |
+
def dup_cyclotomic_p(f, K, irreducible=False):
|
| 456 |
+
"""
|
| 457 |
+
Efficiently test if ``f`` is a cyclotomic polynomial.
|
| 458 |
+
|
| 459 |
+
Examples
|
| 460 |
+
========
|
| 461 |
+
|
| 462 |
+
>>> from sympy.polys import ring, ZZ
|
| 463 |
+
>>> R, x = ring("x", ZZ)
|
| 464 |
+
|
| 465 |
+
>>> f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1
|
| 466 |
+
>>> R.dup_cyclotomic_p(f)
|
| 467 |
+
False
|
| 468 |
+
|
| 469 |
+
>>> g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1
|
| 470 |
+
>>> R.dup_cyclotomic_p(g)
|
| 471 |
+
True
|
| 472 |
+
|
| 473 |
+
References
|
| 474 |
+
==========
|
| 475 |
+
|
| 476 |
+
Bradford, Russell J., and James H. Davenport. "Effective tests for
|
| 477 |
+
cyclotomic polynomials." In International Symposium on Symbolic and
|
| 478 |
+
Algebraic Computation, pp. 244-251. Springer, Berlin, Heidelberg, 1988.
|
| 479 |
+
|
| 480 |
+
"""
|
| 481 |
+
if K.is_QQ:
|
| 482 |
+
try:
|
| 483 |
+
K0, K = K, K.get_ring()
|
| 484 |
+
f = dup_convert(f, K0, K)
|
| 485 |
+
except CoercionFailed:
|
| 486 |
+
return False
|
| 487 |
+
elif not K.is_ZZ:
|
| 488 |
+
return False
|
| 489 |
+
|
| 490 |
+
lc = dup_LC(f, K)
|
| 491 |
+
tc = dup_TC(f, K)
|
| 492 |
+
|
| 493 |
+
if lc != 1 or (tc != -1 and tc != 1):
|
| 494 |
+
return False
|
| 495 |
+
|
| 496 |
+
if not irreducible:
|
| 497 |
+
coeff, factors = dup_factor_list(f, K)
|
| 498 |
+
|
| 499 |
+
if coeff != K.one or factors != [(f, 1)]:
|
| 500 |
+
return False
|
| 501 |
+
|
| 502 |
+
n = dup_degree(f)
|
| 503 |
+
g, h = [], []
|
| 504 |
+
|
| 505 |
+
for i in range(n, -1, -2):
|
| 506 |
+
g.insert(0, f[i])
|
| 507 |
+
|
| 508 |
+
for i in range(n - 1, -1, -2):
|
| 509 |
+
h.insert(0, f[i])
|
| 510 |
+
|
| 511 |
+
g = dup_sqr(dup_strip(g), K)
|
| 512 |
+
h = dup_sqr(dup_strip(h), K)
|
| 513 |
+
|
| 514 |
+
F = dup_sub(g, dup_lshift(h, 1, K), K)
|
| 515 |
+
|
| 516 |
+
if K.is_negative(dup_LC(F, K)):
|
| 517 |
+
F = dup_neg(F, K)
|
| 518 |
+
|
| 519 |
+
if F == f:
|
| 520 |
+
return True
|
| 521 |
+
|
| 522 |
+
g = dup_mirror(f, K)
|
| 523 |
+
|
| 524 |
+
if K.is_negative(dup_LC(g, K)):
|
| 525 |
+
g = dup_neg(g, K)
|
| 526 |
+
|
| 527 |
+
if F == g and dup_cyclotomic_p(g, K):
|
| 528 |
+
return True
|
| 529 |
+
|
| 530 |
+
G = dup_sqf_part(F, K)
|
| 531 |
+
|
| 532 |
+
if dup_sqr(G, K) == F and dup_cyclotomic_p(G, K):
|
| 533 |
+
return True
|
| 534 |
+
|
| 535 |
+
return False
|
| 536 |
+
|
| 537 |
+
|
| 538 |
+
def dup_zz_cyclotomic_poly(n, K):
|
| 539 |
+
"""Efficiently generate n-th cyclotomic polynomial. """
|
| 540 |
+
from sympy.ntheory import factorint
|
| 541 |
+
h = [K.one, -K.one]
|
| 542 |
+
|
| 543 |
+
for p, k in factorint(n).items():
|
| 544 |
+
h = dup_quo(dup_inflate(h, p, K), h, K)
|
| 545 |
+
h = dup_inflate(h, p**(k - 1), K)
|
| 546 |
+
|
| 547 |
+
return h
|
| 548 |
+
|
| 549 |
+
|
| 550 |
+
def _dup_cyclotomic_decompose(n, K):
|
| 551 |
+
from sympy.ntheory import factorint
|
| 552 |
+
|
| 553 |
+
H = [[K.one, -K.one]]
|
| 554 |
+
|
| 555 |
+
for p, k in factorint(n).items():
|
| 556 |
+
Q = [ dup_quo(dup_inflate(h, p, K), h, K) for h in H ]
|
| 557 |
+
H.extend(Q)
|
| 558 |
+
|
| 559 |
+
for i in range(1, k):
|
| 560 |
+
Q = [ dup_inflate(q, p, K) for q in Q ]
|
| 561 |
+
H.extend(Q)
|
| 562 |
+
|
| 563 |
+
return H
|
| 564 |
+
|
| 565 |
+
|
| 566 |
+
def dup_zz_cyclotomic_factor(f, K):
|
| 567 |
+
"""
|
| 568 |
+
Efficiently factor polynomials `x**n - 1` and `x**n + 1` in `Z[x]`.
|
| 569 |
+
|
| 570 |
+
Given a univariate polynomial `f` in `Z[x]` returns a list of factors
|
| 571 |
+
of `f`, provided that `f` is in the form `x**n - 1` or `x**n + 1` for
|
| 572 |
+
`n >= 1`. Otherwise returns None.
|
| 573 |
+
|
| 574 |
+
Factorization is performed using cyclotomic decomposition of `f`,
|
| 575 |
+
which makes this method much faster that any other direct factorization
|
| 576 |
+
approach (e.g. Zassenhaus's).
|
| 577 |
+
|
| 578 |
+
References
|
| 579 |
+
==========
|
| 580 |
+
|
| 581 |
+
.. [1] [Weisstein09]_
|
| 582 |
+
|
| 583 |
+
"""
|
| 584 |
+
lc_f, tc_f = dup_LC(f, K), dup_TC(f, K)
|
| 585 |
+
|
| 586 |
+
if dup_degree(f) <= 0:
|
| 587 |
+
return None
|
| 588 |
+
|
| 589 |
+
if lc_f != 1 or tc_f not in [-1, 1]:
|
| 590 |
+
return None
|
| 591 |
+
|
| 592 |
+
if any(bool(cf) for cf in f[1:-1]):
|
| 593 |
+
return None
|
| 594 |
+
|
| 595 |
+
n = dup_degree(f)
|
| 596 |
+
F = _dup_cyclotomic_decompose(n, K)
|
| 597 |
+
|
| 598 |
+
if not K.is_one(tc_f):
|
| 599 |
+
return F
|
| 600 |
+
else:
|
| 601 |
+
H = []
|
| 602 |
+
|
| 603 |
+
for h in _dup_cyclotomic_decompose(2*n, K):
|
| 604 |
+
if h not in F:
|
| 605 |
+
H.append(h)
|
| 606 |
+
|
| 607 |
+
return H
|
| 608 |
+
|
| 609 |
+
|
| 610 |
+
def dup_zz_factor_sqf(f, K):
|
| 611 |
+
"""Factor square-free (non-primitive) polynomials in `Z[x]`. """
|
| 612 |
+
cont, g = dup_primitive(f, K)
|
| 613 |
+
|
| 614 |
+
n = dup_degree(g)
|
| 615 |
+
|
| 616 |
+
if dup_LC(g, K) < 0:
|
| 617 |
+
cont, g = -cont, dup_neg(g, K)
|
| 618 |
+
|
| 619 |
+
if n <= 0:
|
| 620 |
+
return cont, []
|
| 621 |
+
elif n == 1:
|
| 622 |
+
return cont, [g]
|
| 623 |
+
|
| 624 |
+
if query('USE_IRREDUCIBLE_IN_FACTOR'):
|
| 625 |
+
if dup_zz_irreducible_p(g, K):
|
| 626 |
+
return cont, [g]
|
| 627 |
+
|
| 628 |
+
factors = None
|
| 629 |
+
|
| 630 |
+
if query('USE_CYCLOTOMIC_FACTOR'):
|
| 631 |
+
factors = dup_zz_cyclotomic_factor(g, K)
|
| 632 |
+
|
| 633 |
+
if factors is None:
|
| 634 |
+
factors = dup_zz_zassenhaus(g, K)
|
| 635 |
+
|
| 636 |
+
return cont, _sort_factors(factors, multiple=False)
|
| 637 |
+
|
| 638 |
+
|
| 639 |
+
def dup_zz_factor(f, K):
|
| 640 |
+
"""
|
| 641 |
+
Factor (non square-free) polynomials in `Z[x]`.
|
| 642 |
+
|
| 643 |
+
Given a univariate polynomial `f` in `Z[x]` computes its complete
|
| 644 |
+
factorization `f_1, ..., f_n` into irreducibles over integers::
|
| 645 |
+
|
| 646 |
+
f = content(f) f_1**k_1 ... f_n**k_n
|
| 647 |
+
|
| 648 |
+
The factorization is computed by reducing the input polynomial
|
| 649 |
+
into a primitive square-free polynomial and factoring it using
|
| 650 |
+
Zassenhaus algorithm. Trial division is used to recover the
|
| 651 |
+
multiplicities of factors.
|
| 652 |
+
|
| 653 |
+
The result is returned as a tuple consisting of::
|
| 654 |
+
|
| 655 |
+
(content(f), [(f_1, k_1), ..., (f_n, k_n))
|
| 656 |
+
|
| 657 |
+
Examples
|
| 658 |
+
========
|
| 659 |
+
|
| 660 |
+
Consider the polynomial `f = 2*x**4 - 2`::
|
| 661 |
+
|
| 662 |
+
>>> from sympy.polys import ring, ZZ
|
| 663 |
+
>>> R, x = ring("x", ZZ)
|
| 664 |
+
|
| 665 |
+
>>> R.dup_zz_factor(2*x**4 - 2)
|
| 666 |
+
(2, [(x - 1, 1), (x + 1, 1), (x**2 + 1, 1)])
|
| 667 |
+
|
| 668 |
+
In result we got the following factorization::
|
| 669 |
+
|
| 670 |
+
f = 2 (x - 1) (x + 1) (x**2 + 1)
|
| 671 |
+
|
| 672 |
+
Note that this is a complete factorization over integers,
|
| 673 |
+
however over Gaussian integers we can factor the last term.
|
| 674 |
+
|
| 675 |
+
By default, polynomials `x**n - 1` and `x**n + 1` are factored
|
| 676 |
+
using cyclotomic decomposition to speedup computations. To
|
| 677 |
+
disable this behaviour set cyclotomic=False.
|
| 678 |
+
|
| 679 |
+
References
|
| 680 |
+
==========
|
| 681 |
+
|
| 682 |
+
.. [1] [Gathen99]_
|
| 683 |
+
|
| 684 |
+
"""
|
| 685 |
+
if GROUND_TYPES == 'flint':
|
| 686 |
+
f_flint = fmpz_poly(f[::-1])
|
| 687 |
+
cont, factors = f_flint.factor()
|
| 688 |
+
factors = [(fac.coeffs()[::-1], exp) for fac, exp in factors]
|
| 689 |
+
return cont, _sort_factors(factors)
|
| 690 |
+
|
| 691 |
+
cont, g = dup_primitive(f, K)
|
| 692 |
+
|
| 693 |
+
n = dup_degree(g)
|
| 694 |
+
|
| 695 |
+
if dup_LC(g, K) < 0:
|
| 696 |
+
cont, g = -cont, dup_neg(g, K)
|
| 697 |
+
|
| 698 |
+
if n <= 0:
|
| 699 |
+
return cont, []
|
| 700 |
+
elif n == 1:
|
| 701 |
+
return cont, [(g, 1)]
|
| 702 |
+
|
| 703 |
+
if query('USE_IRREDUCIBLE_IN_FACTOR'):
|
| 704 |
+
if dup_zz_irreducible_p(g, K):
|
| 705 |
+
return cont, [(g, 1)]
|
| 706 |
+
|
| 707 |
+
g = dup_sqf_part(g, K)
|
| 708 |
+
H = None
|
| 709 |
+
|
| 710 |
+
if query('USE_CYCLOTOMIC_FACTOR'):
|
| 711 |
+
H = dup_zz_cyclotomic_factor(g, K)
|
| 712 |
+
|
| 713 |
+
if H is None:
|
| 714 |
+
H = dup_zz_zassenhaus(g, K)
|
| 715 |
+
|
| 716 |
+
factors = dup_trial_division(f, H, K)
|
| 717 |
+
|
| 718 |
+
_dup_check_degrees(f, factors)
|
| 719 |
+
|
| 720 |
+
return cont, factors
|
| 721 |
+
|
| 722 |
+
|
| 723 |
+
def dmp_zz_wang_non_divisors(E, cs, ct, K):
|
| 724 |
+
"""Wang/EEZ: Compute a set of valid divisors. """
|
| 725 |
+
result = [ cs*ct ]
|
| 726 |
+
|
| 727 |
+
for q in E:
|
| 728 |
+
q = abs(q)
|
| 729 |
+
|
| 730 |
+
for r in reversed(result):
|
| 731 |
+
while r != 1:
|
| 732 |
+
r = K.gcd(r, q)
|
| 733 |
+
q = q // r
|
| 734 |
+
|
| 735 |
+
if K.is_one(q):
|
| 736 |
+
return None
|
| 737 |
+
|
| 738 |
+
result.append(q)
|
| 739 |
+
|
| 740 |
+
return result[1:]
|
| 741 |
+
|
| 742 |
+
|
| 743 |
+
def dmp_zz_wang_test_points(f, T, ct, A, u, K):
|
| 744 |
+
"""Wang/EEZ: Test evaluation points for suitability. """
|
| 745 |
+
if not dmp_eval_tail(dmp_LC(f, K), A, u - 1, K):
|
| 746 |
+
raise EvaluationFailed('no luck')
|
| 747 |
+
|
| 748 |
+
g = dmp_eval_tail(f, A, u, K)
|
| 749 |
+
|
| 750 |
+
if not dup_sqf_p(g, K):
|
| 751 |
+
raise EvaluationFailed('no luck')
|
| 752 |
+
|
| 753 |
+
c, h = dup_primitive(g, K)
|
| 754 |
+
|
| 755 |
+
if K.is_negative(dup_LC(h, K)):
|
| 756 |
+
c, h = -c, dup_neg(h, K)
|
| 757 |
+
|
| 758 |
+
v = u - 1
|
| 759 |
+
|
| 760 |
+
E = [ dmp_eval_tail(t, A, v, K) for t, _ in T ]
|
| 761 |
+
D = dmp_zz_wang_non_divisors(E, c, ct, K)
|
| 762 |
+
|
| 763 |
+
if D is not None:
|
| 764 |
+
return c, h, E
|
| 765 |
+
else:
|
| 766 |
+
raise EvaluationFailed('no luck')
|
| 767 |
+
|
| 768 |
+
|
| 769 |
+
def dmp_zz_wang_lead_coeffs(f, T, cs, E, H, A, u, K):
|
| 770 |
+
"""Wang/EEZ: Compute correct leading coefficients. """
|
| 771 |
+
C, J, v = [], [0]*len(E), u - 1
|
| 772 |
+
|
| 773 |
+
for h in H:
|
| 774 |
+
c = dmp_one(v, K)
|
| 775 |
+
d = dup_LC(h, K)*cs
|
| 776 |
+
|
| 777 |
+
for i in reversed(range(len(E))):
|
| 778 |
+
k, e, (t, _) = 0, E[i], T[i]
|
| 779 |
+
|
| 780 |
+
while not (d % e):
|
| 781 |
+
d, k = d//e, k + 1
|
| 782 |
+
|
| 783 |
+
if k != 0:
|
| 784 |
+
c, J[i] = dmp_mul(c, dmp_pow(t, k, v, K), v, K), 1
|
| 785 |
+
|
| 786 |
+
C.append(c)
|
| 787 |
+
|
| 788 |
+
if not all(J):
|
| 789 |
+
raise ExtraneousFactors # pragma: no cover
|
| 790 |
+
|
| 791 |
+
CC, HH = [], []
|
| 792 |
+
|
| 793 |
+
for c, h in zip(C, H):
|
| 794 |
+
d = dmp_eval_tail(c, A, v, K)
|
| 795 |
+
lc = dup_LC(h, K)
|
| 796 |
+
|
| 797 |
+
if K.is_one(cs):
|
| 798 |
+
cc = lc//d
|
| 799 |
+
else:
|
| 800 |
+
g = K.gcd(lc, d)
|
| 801 |
+
d, cc = d//g, lc//g
|
| 802 |
+
h, cs = dup_mul_ground(h, d, K), cs//d
|
| 803 |
+
|
| 804 |
+
c = dmp_mul_ground(c, cc, v, K)
|
| 805 |
+
|
| 806 |
+
CC.append(c)
|
| 807 |
+
HH.append(h)
|
| 808 |
+
|
| 809 |
+
if K.is_one(cs):
|
| 810 |
+
return f, HH, CC
|
| 811 |
+
|
| 812 |
+
CCC, HHH = [], []
|
| 813 |
+
|
| 814 |
+
for c, h in zip(CC, HH):
|
| 815 |
+
CCC.append(dmp_mul_ground(c, cs, v, K))
|
| 816 |
+
HHH.append(dmp_mul_ground(h, cs, 0, K))
|
| 817 |
+
|
| 818 |
+
f = dmp_mul_ground(f, cs**(len(H) - 1), u, K)
|
| 819 |
+
|
| 820 |
+
return f, HHH, CCC
|
| 821 |
+
|
| 822 |
+
|
| 823 |
+
def dup_zz_diophantine(F, m, p, K):
|
| 824 |
+
"""Wang/EEZ: Solve univariate Diophantine equations. """
|
| 825 |
+
if len(F) == 2:
|
| 826 |
+
a, b = F
|
| 827 |
+
|
| 828 |
+
f = gf_from_int_poly(a, p)
|
| 829 |
+
g = gf_from_int_poly(b, p)
|
| 830 |
+
|
| 831 |
+
s, t, G = gf_gcdex(g, f, p, K)
|
| 832 |
+
|
| 833 |
+
s = gf_lshift(s, m, K)
|
| 834 |
+
t = gf_lshift(t, m, K)
|
| 835 |
+
|
| 836 |
+
q, s = gf_div(s, f, p, K)
|
| 837 |
+
|
| 838 |
+
t = gf_add_mul(t, q, g, p, K)
|
| 839 |
+
|
| 840 |
+
s = gf_to_int_poly(s, p)
|
| 841 |
+
t = gf_to_int_poly(t, p)
|
| 842 |
+
|
| 843 |
+
result = [s, t]
|
| 844 |
+
else:
|
| 845 |
+
G = [F[-1]]
|
| 846 |
+
|
| 847 |
+
for f in reversed(F[1:-1]):
|
| 848 |
+
G.insert(0, dup_mul(f, G[0], K))
|
| 849 |
+
|
| 850 |
+
S, T = [], [[1]]
|
| 851 |
+
|
| 852 |
+
for f, g in zip(F, G):
|
| 853 |
+
t, s = dmp_zz_diophantine([g, f], T[-1], [], 0, p, 1, K)
|
| 854 |
+
T.append(t)
|
| 855 |
+
S.append(s)
|
| 856 |
+
|
| 857 |
+
result, S = [], S + [T[-1]]
|
| 858 |
+
|
| 859 |
+
for s, f in zip(S, F):
|
| 860 |
+
s = gf_from_int_poly(s, p)
|
| 861 |
+
f = gf_from_int_poly(f, p)
|
| 862 |
+
|
| 863 |
+
r = gf_rem(gf_lshift(s, m, K), f, p, K)
|
| 864 |
+
s = gf_to_int_poly(r, p)
|
| 865 |
+
|
| 866 |
+
result.append(s)
|
| 867 |
+
|
| 868 |
+
return result
|
| 869 |
+
|
| 870 |
+
|
| 871 |
+
def dmp_zz_diophantine(F, c, A, d, p, u, K):
|
| 872 |
+
"""Wang/EEZ: Solve multivariate Diophantine equations. """
|
| 873 |
+
if not A:
|
| 874 |
+
S = [ [] for _ in F ]
|
| 875 |
+
n = dup_degree(c)
|
| 876 |
+
|
| 877 |
+
for i, coeff in enumerate(c):
|
| 878 |
+
if not coeff:
|
| 879 |
+
continue
|
| 880 |
+
|
| 881 |
+
T = dup_zz_diophantine(F, n - i, p, K)
|
| 882 |
+
|
| 883 |
+
for j, (s, t) in enumerate(zip(S, T)):
|
| 884 |
+
t = dup_mul_ground(t, coeff, K)
|
| 885 |
+
S[j] = dup_trunc(dup_add(s, t, K), p, K)
|
| 886 |
+
else:
|
| 887 |
+
n = len(A)
|
| 888 |
+
e = dmp_expand(F, u, K)
|
| 889 |
+
|
| 890 |
+
a, A = A[-1], A[:-1]
|
| 891 |
+
B, G = [], []
|
| 892 |
+
|
| 893 |
+
for f in F:
|
| 894 |
+
B.append(dmp_quo(e, f, u, K))
|
| 895 |
+
G.append(dmp_eval_in(f, a, n, u, K))
|
| 896 |
+
|
| 897 |
+
C = dmp_eval_in(c, a, n, u, K)
|
| 898 |
+
|
| 899 |
+
v = u - 1
|
| 900 |
+
|
| 901 |
+
S = dmp_zz_diophantine(G, C, A, d, p, v, K)
|
| 902 |
+
S = [ dmp_raise(s, 1, v, K) for s in S ]
|
| 903 |
+
|
| 904 |
+
for s, b in zip(S, B):
|
| 905 |
+
c = dmp_sub_mul(c, s, b, u, K)
|
| 906 |
+
|
| 907 |
+
c = dmp_ground_trunc(c, p, u, K)
|
| 908 |
+
|
| 909 |
+
m = dmp_nest([K.one, -a], n, K)
|
| 910 |
+
M = dmp_one(n, K)
|
| 911 |
+
|
| 912 |
+
for k in range(0, d):
|
| 913 |
+
if dmp_zero_p(c, u):
|
| 914 |
+
break
|
| 915 |
+
|
| 916 |
+
M = dmp_mul(M, m, u, K)
|
| 917 |
+
C = dmp_diff_eval_in(c, k + 1, a, n, u, K)
|
| 918 |
+
|
| 919 |
+
if not dmp_zero_p(C, v):
|
| 920 |
+
C = dmp_quo_ground(C, K.factorial(K(k) + 1), v, K)
|
| 921 |
+
T = dmp_zz_diophantine(G, C, A, d, p, v, K)
|
| 922 |
+
|
| 923 |
+
for i, t in enumerate(T):
|
| 924 |
+
T[i] = dmp_mul(dmp_raise(t, 1, v, K), M, u, K)
|
| 925 |
+
|
| 926 |
+
for i, (s, t) in enumerate(zip(S, T)):
|
| 927 |
+
S[i] = dmp_add(s, t, u, K)
|
| 928 |
+
|
| 929 |
+
for t, b in zip(T, B):
|
| 930 |
+
c = dmp_sub_mul(c, t, b, u, K)
|
| 931 |
+
|
| 932 |
+
c = dmp_ground_trunc(c, p, u, K)
|
| 933 |
+
|
| 934 |
+
S = [ dmp_ground_trunc(s, p, u, K) for s in S ]
|
| 935 |
+
|
| 936 |
+
return S
|
| 937 |
+
|
| 938 |
+
|
| 939 |
+
def dmp_zz_wang_hensel_lifting(f, H, LC, A, p, u, K):
|
| 940 |
+
"""Wang/EEZ: Parallel Hensel lifting algorithm. """
|
| 941 |
+
S, n, v = [f], len(A), u - 1
|
| 942 |
+
|
| 943 |
+
H = list(H)
|
| 944 |
+
|
| 945 |
+
for i, a in enumerate(reversed(A[1:])):
|
| 946 |
+
s = dmp_eval_in(S[0], a, n - i, u - i, K)
|
| 947 |
+
S.insert(0, dmp_ground_trunc(s, p, v - i, K))
|
| 948 |
+
|
| 949 |
+
d = max(dmp_degree_list(f, u)[1:])
|
| 950 |
+
|
| 951 |
+
for j, s, a in zip(range(2, n + 2), S, A):
|
| 952 |
+
G, w = list(H), j - 1
|
| 953 |
+
|
| 954 |
+
I, J = A[:j - 2], A[j - 1:]
|
| 955 |
+
|
| 956 |
+
for i, (h, lc) in enumerate(zip(H, LC)):
|
| 957 |
+
lc = dmp_ground_trunc(dmp_eval_tail(lc, J, v, K), p, w - 1, K)
|
| 958 |
+
H[i] = [lc] + dmp_raise(h[1:], 1, w - 1, K)
|
| 959 |
+
|
| 960 |
+
m = dmp_nest([K.one, -a], w, K)
|
| 961 |
+
M = dmp_one(w, K)
|
| 962 |
+
|
| 963 |
+
c = dmp_sub(s, dmp_expand(H, w, K), w, K)
|
| 964 |
+
|
| 965 |
+
dj = dmp_degree_in(s, w, w)
|
| 966 |
+
|
| 967 |
+
for k in range(0, dj):
|
| 968 |
+
if dmp_zero_p(c, w):
|
| 969 |
+
break
|
| 970 |
+
|
| 971 |
+
M = dmp_mul(M, m, w, K)
|
| 972 |
+
C = dmp_diff_eval_in(c, k + 1, a, w, w, K)
|
| 973 |
+
|
| 974 |
+
if not dmp_zero_p(C, w - 1):
|
| 975 |
+
C = dmp_quo_ground(C, K.factorial(K(k) + 1), w - 1, K)
|
| 976 |
+
T = dmp_zz_diophantine(G, C, I, d, p, w - 1, K)
|
| 977 |
+
|
| 978 |
+
for i, (h, t) in enumerate(zip(H, T)):
|
| 979 |
+
h = dmp_add_mul(h, dmp_raise(t, 1, w - 1, K), M, w, K)
|
| 980 |
+
H[i] = dmp_ground_trunc(h, p, w, K)
|
| 981 |
+
|
| 982 |
+
h = dmp_sub(s, dmp_expand(H, w, K), w, K)
|
| 983 |
+
c = dmp_ground_trunc(h, p, w, K)
|
| 984 |
+
|
| 985 |
+
if dmp_expand(H, u, K) != f:
|
| 986 |
+
raise ExtraneousFactors # pragma: no cover
|
| 987 |
+
else:
|
| 988 |
+
return H
|
| 989 |
+
|
| 990 |
+
|
| 991 |
+
def dmp_zz_wang(f, u, K, mod=None, seed=None):
|
| 992 |
+
r"""
|
| 993 |
+
Factor primitive square-free polynomials in `Z[X]`.
|
| 994 |
+
|
| 995 |
+
Given a multivariate polynomial `f` in `Z[x_1,...,x_n]`, which is
|
| 996 |
+
primitive and square-free in `x_1`, computes factorization of `f` into
|
| 997 |
+
irreducibles over integers.
|
| 998 |
+
|
| 999 |
+
The procedure is based on Wang's Enhanced Extended Zassenhaus
|
| 1000 |
+
algorithm. The algorithm works by viewing `f` as a univariate polynomial
|
| 1001 |
+
in `Z[x_2,...,x_n][x_1]`, for which an evaluation mapping is computed::
|
| 1002 |
+
|
| 1003 |
+
x_2 -> a_2, ..., x_n -> a_n
|
| 1004 |
+
|
| 1005 |
+
where `a_i`, for `i = 2, \dots, n`, are carefully chosen integers. The
|
| 1006 |
+
mapping is used to transform `f` into a univariate polynomial in `Z[x_1]`,
|
| 1007 |
+
which can be factored efficiently using Zassenhaus algorithm. The last
|
| 1008 |
+
step is to lift univariate factors to obtain true multivariate
|
| 1009 |
+
factors. For this purpose a parallel Hensel lifting procedure is used.
|
| 1010 |
+
|
| 1011 |
+
The parameter ``seed`` is passed to _randint and can be used to seed randint
|
| 1012 |
+
(when an integer) or (for testing purposes) can be a sequence of numbers.
|
| 1013 |
+
|
| 1014 |
+
References
|
| 1015 |
+
==========
|
| 1016 |
+
|
| 1017 |
+
.. [1] [Wang78]_
|
| 1018 |
+
.. [2] [Geddes92]_
|
| 1019 |
+
|
| 1020 |
+
"""
|
| 1021 |
+
from sympy.ntheory import nextprime
|
| 1022 |
+
|
| 1023 |
+
randint = _randint(seed)
|
| 1024 |
+
|
| 1025 |
+
ct, T = dmp_zz_factor(dmp_LC(f, K), u - 1, K)
|
| 1026 |
+
|
| 1027 |
+
b = dmp_zz_mignotte_bound(f, u, K)
|
| 1028 |
+
p = K(nextprime(b))
|
| 1029 |
+
|
| 1030 |
+
if mod is None:
|
| 1031 |
+
if u == 1:
|
| 1032 |
+
mod = 2
|
| 1033 |
+
else:
|
| 1034 |
+
mod = 1
|
| 1035 |
+
|
| 1036 |
+
history, configs, A, r = set(), [], [K.zero]*u, None
|
| 1037 |
+
|
| 1038 |
+
try:
|
| 1039 |
+
cs, s, E = dmp_zz_wang_test_points(f, T, ct, A, u, K)
|
| 1040 |
+
|
| 1041 |
+
_, H = dup_zz_factor_sqf(s, K)
|
| 1042 |
+
|
| 1043 |
+
r = len(H)
|
| 1044 |
+
|
| 1045 |
+
if r == 1:
|
| 1046 |
+
return [f]
|
| 1047 |
+
|
| 1048 |
+
configs = [(s, cs, E, H, A)]
|
| 1049 |
+
except EvaluationFailed:
|
| 1050 |
+
pass
|
| 1051 |
+
|
| 1052 |
+
eez_num_configs = query('EEZ_NUMBER_OF_CONFIGS')
|
| 1053 |
+
eez_num_tries = query('EEZ_NUMBER_OF_TRIES')
|
| 1054 |
+
eez_mod_step = query('EEZ_MODULUS_STEP')
|
| 1055 |
+
|
| 1056 |
+
while len(configs) < eez_num_configs:
|
| 1057 |
+
for _ in range(eez_num_tries):
|
| 1058 |
+
A = [ K(randint(-mod, mod)) for _ in range(u) ]
|
| 1059 |
+
|
| 1060 |
+
if tuple(A) not in history:
|
| 1061 |
+
history.add(tuple(A))
|
| 1062 |
+
else:
|
| 1063 |
+
continue
|
| 1064 |
+
|
| 1065 |
+
try:
|
| 1066 |
+
cs, s, E = dmp_zz_wang_test_points(f, T, ct, A, u, K)
|
| 1067 |
+
except EvaluationFailed:
|
| 1068 |
+
continue
|
| 1069 |
+
|
| 1070 |
+
_, H = dup_zz_factor_sqf(s, K)
|
| 1071 |
+
|
| 1072 |
+
rr = len(H)
|
| 1073 |
+
|
| 1074 |
+
if r is not None:
|
| 1075 |
+
if rr != r: # pragma: no cover
|
| 1076 |
+
if rr < r:
|
| 1077 |
+
configs, r = [], rr
|
| 1078 |
+
else:
|
| 1079 |
+
continue
|
| 1080 |
+
else:
|
| 1081 |
+
r = rr
|
| 1082 |
+
|
| 1083 |
+
if r == 1:
|
| 1084 |
+
return [f]
|
| 1085 |
+
|
| 1086 |
+
configs.append((s, cs, E, H, A))
|
| 1087 |
+
|
| 1088 |
+
if len(configs) == eez_num_configs:
|
| 1089 |
+
break
|
| 1090 |
+
else:
|
| 1091 |
+
mod += eez_mod_step
|
| 1092 |
+
|
| 1093 |
+
s_norm, s_arg, i = None, 0, 0
|
| 1094 |
+
|
| 1095 |
+
for s, _, _, _, _ in configs:
|
| 1096 |
+
_s_norm = dup_max_norm(s, K)
|
| 1097 |
+
|
| 1098 |
+
if s_norm is not None:
|
| 1099 |
+
if _s_norm < s_norm:
|
| 1100 |
+
s_norm = _s_norm
|
| 1101 |
+
s_arg = i
|
| 1102 |
+
else:
|
| 1103 |
+
s_norm = _s_norm
|
| 1104 |
+
|
| 1105 |
+
i += 1
|
| 1106 |
+
|
| 1107 |
+
_, cs, E, H, A = configs[s_arg]
|
| 1108 |
+
orig_f = f
|
| 1109 |
+
|
| 1110 |
+
try:
|
| 1111 |
+
f, H, LC = dmp_zz_wang_lead_coeffs(f, T, cs, E, H, A, u, K)
|
| 1112 |
+
factors = dmp_zz_wang_hensel_lifting(f, H, LC, A, p, u, K)
|
| 1113 |
+
except ExtraneousFactors: # pragma: no cover
|
| 1114 |
+
if query('EEZ_RESTART_IF_NEEDED'):
|
| 1115 |
+
return dmp_zz_wang(orig_f, u, K, mod + 1)
|
| 1116 |
+
else:
|
| 1117 |
+
raise ExtraneousFactors(
|
| 1118 |
+
"we need to restart algorithm with better parameters")
|
| 1119 |
+
|
| 1120 |
+
result = []
|
| 1121 |
+
|
| 1122 |
+
for f in factors:
|
| 1123 |
+
_, f = dmp_ground_primitive(f, u, K)
|
| 1124 |
+
|
| 1125 |
+
if K.is_negative(dmp_ground_LC(f, u, K)):
|
| 1126 |
+
f = dmp_neg(f, u, K)
|
| 1127 |
+
|
| 1128 |
+
result.append(f)
|
| 1129 |
+
|
| 1130 |
+
return result
|
| 1131 |
+
|
| 1132 |
+
|
| 1133 |
+
def dmp_zz_factor(f, u, K):
|
| 1134 |
+
r"""
|
| 1135 |
+
Factor (non square-free) polynomials in `Z[X]`.
|
| 1136 |
+
|
| 1137 |
+
Given a multivariate polynomial `f` in `Z[x]` computes its complete
|
| 1138 |
+
factorization `f_1, \dots, f_n` into irreducibles over integers::
|
| 1139 |
+
|
| 1140 |
+
f = content(f) f_1**k_1 ... f_n**k_n
|
| 1141 |
+
|
| 1142 |
+
The factorization is computed by reducing the input polynomial
|
| 1143 |
+
into a primitive square-free polynomial and factoring it using
|
| 1144 |
+
Enhanced Extended Zassenhaus (EEZ) algorithm. Trial division
|
| 1145 |
+
is used to recover the multiplicities of factors.
|
| 1146 |
+
|
| 1147 |
+
The result is returned as a tuple consisting of::
|
| 1148 |
+
|
| 1149 |
+
(content(f), [(f_1, k_1), ..., (f_n, k_n))
|
| 1150 |
+
|
| 1151 |
+
Consider polynomial `f = 2*(x**2 - y**2)`::
|
| 1152 |
+
|
| 1153 |
+
>>> from sympy.polys import ring, ZZ
|
| 1154 |
+
>>> R, x,y = ring("x,y", ZZ)
|
| 1155 |
+
|
| 1156 |
+
>>> R.dmp_zz_factor(2*x**2 - 2*y**2)
|
| 1157 |
+
(2, [(x - y, 1), (x + y, 1)])
|
| 1158 |
+
|
| 1159 |
+
In result we got the following factorization::
|
| 1160 |
+
|
| 1161 |
+
f = 2 (x - y) (x + y)
|
| 1162 |
+
|
| 1163 |
+
References
|
| 1164 |
+
==========
|
| 1165 |
+
|
| 1166 |
+
.. [1] [Gathen99]_
|
| 1167 |
+
|
| 1168 |
+
"""
|
| 1169 |
+
if not u:
|
| 1170 |
+
return dup_zz_factor(f, K)
|
| 1171 |
+
|
| 1172 |
+
if dmp_zero_p(f, u):
|
| 1173 |
+
return K.zero, []
|
| 1174 |
+
|
| 1175 |
+
cont, g = dmp_ground_primitive(f, u, K)
|
| 1176 |
+
|
| 1177 |
+
if dmp_ground_LC(g, u, K) < 0:
|
| 1178 |
+
cont, g = -cont, dmp_neg(g, u, K)
|
| 1179 |
+
|
| 1180 |
+
if all(d <= 0 for d in dmp_degree_list(g, u)):
|
| 1181 |
+
return cont, []
|
| 1182 |
+
|
| 1183 |
+
G, g = dmp_primitive(g, u, K)
|
| 1184 |
+
|
| 1185 |
+
factors = []
|
| 1186 |
+
|
| 1187 |
+
if dmp_degree(g, u) > 0:
|
| 1188 |
+
g = dmp_sqf_part(g, u, K)
|
| 1189 |
+
H = dmp_zz_wang(g, u, K)
|
| 1190 |
+
factors = dmp_trial_division(f, H, u, K)
|
| 1191 |
+
|
| 1192 |
+
for g, k in dmp_zz_factor(G, u - 1, K)[1]:
|
| 1193 |
+
factors.insert(0, ([g], k))
|
| 1194 |
+
|
| 1195 |
+
_dmp_check_degrees(f, u, factors)
|
| 1196 |
+
|
| 1197 |
+
return cont, _sort_factors(factors)
|
| 1198 |
+
|
| 1199 |
+
|
| 1200 |
+
def dup_qq_i_factor(f, K0):
|
| 1201 |
+
"""Factor univariate polynomials into irreducibles in `QQ_I[x]`. """
|
| 1202 |
+
# Factor in QQ<I>
|
| 1203 |
+
K1 = K0.as_AlgebraicField()
|
| 1204 |
+
f = dup_convert(f, K0, K1)
|
| 1205 |
+
coeff, factors = dup_factor_list(f, K1)
|
| 1206 |
+
factors = [(dup_convert(fac, K1, K0), i) for fac, i in factors]
|
| 1207 |
+
coeff = K0.convert(coeff, K1)
|
| 1208 |
+
return coeff, factors
|
| 1209 |
+
|
| 1210 |
+
|
| 1211 |
+
def dup_zz_i_factor(f, K0):
|
| 1212 |
+
"""Factor univariate polynomials into irreducibles in `ZZ_I[x]`. """
|
| 1213 |
+
# First factor in QQ_I
|
| 1214 |
+
K1 = K0.get_field()
|
| 1215 |
+
f = dup_convert(f, K0, K1)
|
| 1216 |
+
coeff, factors = dup_qq_i_factor(f, K1)
|
| 1217 |
+
|
| 1218 |
+
new_factors = []
|
| 1219 |
+
for fac, i in factors:
|
| 1220 |
+
# Extract content
|
| 1221 |
+
fac_denom, fac_num = dup_clear_denoms(fac, K1)
|
| 1222 |
+
fac_num_ZZ_I = dup_convert(fac_num, K1, K0)
|
| 1223 |
+
content, fac_prim = dmp_ground_primitive(fac_num_ZZ_I, 0, K0)
|
| 1224 |
+
|
| 1225 |
+
coeff = (coeff * content ** i) // fac_denom ** i
|
| 1226 |
+
new_factors.append((fac_prim, i))
|
| 1227 |
+
|
| 1228 |
+
factors = new_factors
|
| 1229 |
+
coeff = K0.convert(coeff, K1)
|
| 1230 |
+
return coeff, factors
|
| 1231 |
+
|
| 1232 |
+
|
| 1233 |
+
def dmp_qq_i_factor(f, u, K0):
|
| 1234 |
+
"""Factor multivariate polynomials into irreducibles in `QQ_I[X]`. """
|
| 1235 |
+
# Factor in QQ<I>
|
| 1236 |
+
K1 = K0.as_AlgebraicField()
|
| 1237 |
+
f = dmp_convert(f, u, K0, K1)
|
| 1238 |
+
coeff, factors = dmp_factor_list(f, u, K1)
|
| 1239 |
+
factors = [(dmp_convert(fac, u, K1, K0), i) for fac, i in factors]
|
| 1240 |
+
coeff = K0.convert(coeff, K1)
|
| 1241 |
+
return coeff, factors
|
| 1242 |
+
|
| 1243 |
+
|
| 1244 |
+
def dmp_zz_i_factor(f, u, K0):
|
| 1245 |
+
"""Factor multivariate polynomials into irreducibles in `ZZ_I[X]`. """
|
| 1246 |
+
# First factor in QQ_I
|
| 1247 |
+
K1 = K0.get_field()
|
| 1248 |
+
f = dmp_convert(f, u, K0, K1)
|
| 1249 |
+
coeff, factors = dmp_qq_i_factor(f, u, K1)
|
| 1250 |
+
|
| 1251 |
+
new_factors = []
|
| 1252 |
+
for fac, i in factors:
|
| 1253 |
+
# Extract content
|
| 1254 |
+
fac_denom, fac_num = dmp_clear_denoms(fac, u, K1)
|
| 1255 |
+
fac_num_ZZ_I = dmp_convert(fac_num, u, K1, K0)
|
| 1256 |
+
content, fac_prim = dmp_ground_primitive(fac_num_ZZ_I, u, K0)
|
| 1257 |
+
|
| 1258 |
+
coeff = (coeff * content ** i) // fac_denom ** i
|
| 1259 |
+
new_factors.append((fac_prim, i))
|
| 1260 |
+
|
| 1261 |
+
factors = new_factors
|
| 1262 |
+
coeff = K0.convert(coeff, K1)
|
| 1263 |
+
return coeff, factors
|
| 1264 |
+
|
| 1265 |
+
|
| 1266 |
+
def dup_ext_factor(f, K):
|
| 1267 |
+
r"""Factor univariate polynomials over algebraic number fields.
|
| 1268 |
+
|
| 1269 |
+
The domain `K` must be an algebraic number field `k(a)` (see :ref:`QQ(a)`).
|
| 1270 |
+
|
| 1271 |
+
Examples
|
| 1272 |
+
========
|
| 1273 |
+
|
| 1274 |
+
First define the algebraic number field `K = \mathbb{Q}(\sqrt{2})`:
|
| 1275 |
+
|
| 1276 |
+
>>> from sympy import QQ, sqrt
|
| 1277 |
+
>>> from sympy.polys.factortools import dup_ext_factor
|
| 1278 |
+
>>> K = QQ.algebraic_field(sqrt(2))
|
| 1279 |
+
|
| 1280 |
+
We can now factorise the polynomial `x^2 - 2` over `K`:
|
| 1281 |
+
|
| 1282 |
+
>>> p = [K(1), K(0), K(-2)] # x^2 - 2
|
| 1283 |
+
>>> p1 = [K(1), -K.unit] # x - sqrt(2)
|
| 1284 |
+
>>> p2 = [K(1), +K.unit] # x + sqrt(2)
|
| 1285 |
+
>>> dup_ext_factor(p, K) == (K.one, [(p1, 1), (p2, 1)])
|
| 1286 |
+
True
|
| 1287 |
+
|
| 1288 |
+
Usually this would be done at a higher level:
|
| 1289 |
+
|
| 1290 |
+
>>> from sympy import factor
|
| 1291 |
+
>>> from sympy.abc import x
|
| 1292 |
+
>>> factor(x**2 - 2, extension=sqrt(2))
|
| 1293 |
+
(x - sqrt(2))*(x + sqrt(2))
|
| 1294 |
+
|
| 1295 |
+
Explanation
|
| 1296 |
+
===========
|
| 1297 |
+
|
| 1298 |
+
Uses Trager's algorithm. In particular this function is algorithm
|
| 1299 |
+
``alg_factor`` from [Trager76]_.
|
| 1300 |
+
|
| 1301 |
+
If `f` is a polynomial in `k(a)[x]` then its norm `g(x)` is a polynomial in
|
| 1302 |
+
`k[x]`. If `g(x)` is square-free and has irreducible factors `g_1(x)`,
|
| 1303 |
+
`g_2(x)`, `\cdots` then the irreducible factors of `f` in `k(a)[x]` are
|
| 1304 |
+
given by `f_i(x) = \gcd(f(x), g_i(x))` where the GCD is computed in
|
| 1305 |
+
`k(a)[x]`.
|
| 1306 |
+
|
| 1307 |
+
The first step in Trager's algorithm is to find an integer shift `s` so
|
| 1308 |
+
that `f(x-sa)` has square-free norm. Then the norm is factorized in `k[x]`
|
| 1309 |
+
and the GCD of (shifted) `f` with each factor gives the shifted factors of
|
| 1310 |
+
`f`. At the end the shift is undone to recover the unshifted factors of `f`
|
| 1311 |
+
in `k(a)[x]`.
|
| 1312 |
+
|
| 1313 |
+
The algorithm reduces the problem of factorization in `k(a)[x]` to
|
| 1314 |
+
factorization in `k[x]` with the main additional steps being to compute the
|
| 1315 |
+
norm (a resultant calculation in `k[x,y]`) and some polynomial GCDs in
|
| 1316 |
+
`k(a)[x]`.
|
| 1317 |
+
|
| 1318 |
+
In practice in SymPy the base field `k` will be the rationals :ref:`QQ` and
|
| 1319 |
+
this function factorizes a polynomial with coefficients in an algebraic
|
| 1320 |
+
number field like `\mathbb{Q}(\sqrt{2})`.
|
| 1321 |
+
|
| 1322 |
+
See Also
|
| 1323 |
+
========
|
| 1324 |
+
|
| 1325 |
+
dmp_ext_factor:
|
| 1326 |
+
Analogous function for multivariate polynomials over ``k(a)``.
|
| 1327 |
+
dup_sqf_norm:
|
| 1328 |
+
Subroutine ``sqfr_norm`` also from [Trager76]_.
|
| 1329 |
+
sympy.polys.polytools.factor:
|
| 1330 |
+
The high-level function that ultimately uses this function as needed.
|
| 1331 |
+
"""
|
| 1332 |
+
n, lc = dup_degree(f), dup_LC(f, K)
|
| 1333 |
+
|
| 1334 |
+
f = dup_monic(f, K)
|
| 1335 |
+
|
| 1336 |
+
if n <= 0:
|
| 1337 |
+
return lc, []
|
| 1338 |
+
if n == 1:
|
| 1339 |
+
return lc, [(f, 1)]
|
| 1340 |
+
|
| 1341 |
+
f, F = dup_sqf_part(f, K), f
|
| 1342 |
+
s, g, r = dup_sqf_norm(f, K)
|
| 1343 |
+
|
| 1344 |
+
factors = dup_factor_list_include(r, K.dom)
|
| 1345 |
+
|
| 1346 |
+
if len(factors) == 1:
|
| 1347 |
+
return lc, [(f, n//dup_degree(f))]
|
| 1348 |
+
|
| 1349 |
+
H = s*K.unit
|
| 1350 |
+
|
| 1351 |
+
for i, (factor, _) in enumerate(factors):
|
| 1352 |
+
h = dup_convert(factor, K.dom, K)
|
| 1353 |
+
h, _, g = dup_inner_gcd(h, g, K)
|
| 1354 |
+
h = dup_shift(h, H, K)
|
| 1355 |
+
factors[i] = h
|
| 1356 |
+
|
| 1357 |
+
factors = dup_trial_division(F, factors, K)
|
| 1358 |
+
|
| 1359 |
+
_dup_check_degrees(F, factors)
|
| 1360 |
+
|
| 1361 |
+
return lc, factors
|
| 1362 |
+
|
| 1363 |
+
|
| 1364 |
+
def dmp_ext_factor(f, u, K):
|
| 1365 |
+
r"""Factor multivariate polynomials over algebraic number fields.
|
| 1366 |
+
|
| 1367 |
+
The domain `K` must be an algebraic number field `k(a)` (see :ref:`QQ(a)`).
|
| 1368 |
+
|
| 1369 |
+
Examples
|
| 1370 |
+
========
|
| 1371 |
+
|
| 1372 |
+
First define the algebraic number field `K = \mathbb{Q}(\sqrt{2})`:
|
| 1373 |
+
|
| 1374 |
+
>>> from sympy import QQ, sqrt
|
| 1375 |
+
>>> from sympy.polys.factortools import dmp_ext_factor
|
| 1376 |
+
>>> K = QQ.algebraic_field(sqrt(2))
|
| 1377 |
+
|
| 1378 |
+
We can now factorise the polynomial `x^2 y^2 - 2` over `K`:
|
| 1379 |
+
|
| 1380 |
+
>>> p = [[K(1),K(0),K(0)], [], [K(-2)]] # x**2*y**2 - 2
|
| 1381 |
+
>>> p1 = [[K(1),K(0)], [-K.unit]] # x*y - sqrt(2)
|
| 1382 |
+
>>> p2 = [[K(1),K(0)], [+K.unit]] # x*y + sqrt(2)
|
| 1383 |
+
>>> dmp_ext_factor(p, 1, K) == (K.one, [(p1, 1), (p2, 1)])
|
| 1384 |
+
True
|
| 1385 |
+
|
| 1386 |
+
Usually this would be done at a higher level:
|
| 1387 |
+
|
| 1388 |
+
>>> from sympy import factor
|
| 1389 |
+
>>> from sympy.abc import x, y
|
| 1390 |
+
>>> factor(x**2*y**2 - 2, extension=sqrt(2))
|
| 1391 |
+
(x*y - sqrt(2))*(x*y + sqrt(2))
|
| 1392 |
+
|
| 1393 |
+
Explanation
|
| 1394 |
+
===========
|
| 1395 |
+
|
| 1396 |
+
This is Trager's algorithm for multivariate polynomials. In particular this
|
| 1397 |
+
function is algorithm ``alg_factor`` from [Trager76]_.
|
| 1398 |
+
|
| 1399 |
+
See :func:`dup_ext_factor` for explanation.
|
| 1400 |
+
|
| 1401 |
+
See Also
|
| 1402 |
+
========
|
| 1403 |
+
|
| 1404 |
+
dup_ext_factor:
|
| 1405 |
+
Analogous function for univariate polynomials over ``k(a)``.
|
| 1406 |
+
dmp_sqf_norm:
|
| 1407 |
+
Multivariate version of subroutine ``sqfr_norm`` also from [Trager76]_.
|
| 1408 |
+
sympy.polys.polytools.factor:
|
| 1409 |
+
The high-level function that ultimately uses this function as needed.
|
| 1410 |
+
"""
|
| 1411 |
+
if not u:
|
| 1412 |
+
return dup_ext_factor(f, K)
|
| 1413 |
+
|
| 1414 |
+
lc = dmp_ground_LC(f, u, K)
|
| 1415 |
+
f = dmp_ground_monic(f, u, K)
|
| 1416 |
+
|
| 1417 |
+
if all(d <= 0 for d in dmp_degree_list(f, u)):
|
| 1418 |
+
return lc, []
|
| 1419 |
+
|
| 1420 |
+
f, F = dmp_sqf_part(f, u, K), f
|
| 1421 |
+
s, g, r = dmp_sqf_norm(f, u, K)
|
| 1422 |
+
|
| 1423 |
+
factors = dmp_factor_list_include(r, u, K.dom)
|
| 1424 |
+
|
| 1425 |
+
if len(factors) == 1:
|
| 1426 |
+
factors = [f]
|
| 1427 |
+
else:
|
| 1428 |
+
for i, (factor, _) in enumerate(factors):
|
| 1429 |
+
h = dmp_convert(factor, u, K.dom, K)
|
| 1430 |
+
h, _, g = dmp_inner_gcd(h, g, u, K)
|
| 1431 |
+
a = [si*K.unit for si in s]
|
| 1432 |
+
h = dmp_shift(h, a, u, K)
|
| 1433 |
+
factors[i] = h
|
| 1434 |
+
|
| 1435 |
+
result = dmp_trial_division(F, factors, u, K)
|
| 1436 |
+
|
| 1437 |
+
_dmp_check_degrees(F, u, result)
|
| 1438 |
+
|
| 1439 |
+
return lc, result
|
| 1440 |
+
|
| 1441 |
+
|
| 1442 |
+
def dup_gf_factor(f, K):
|
| 1443 |
+
"""Factor univariate polynomials over finite fields. """
|
| 1444 |
+
f = dup_convert(f, K, K.dom)
|
| 1445 |
+
|
| 1446 |
+
coeff, factors = gf_factor(f, K.mod, K.dom)
|
| 1447 |
+
|
| 1448 |
+
for i, (f, k) in enumerate(factors):
|
| 1449 |
+
factors[i] = (dup_convert(f, K.dom, K), k)
|
| 1450 |
+
|
| 1451 |
+
return K.convert(coeff, K.dom), factors
|
| 1452 |
+
|
| 1453 |
+
|
| 1454 |
+
def dmp_gf_factor(f, u, K):
|
| 1455 |
+
"""Factor multivariate polynomials over finite fields. """
|
| 1456 |
+
raise NotImplementedError('multivariate polynomials over finite fields')
|
| 1457 |
+
|
| 1458 |
+
|
| 1459 |
+
def dup_factor_list(f, K0):
|
| 1460 |
+
"""Factor univariate polynomials into irreducibles in `K[x]`. """
|
| 1461 |
+
j, f = dup_terms_gcd(f, K0)
|
| 1462 |
+
cont, f = dup_primitive(f, K0)
|
| 1463 |
+
|
| 1464 |
+
if K0.is_FiniteField:
|
| 1465 |
+
coeff, factors = dup_gf_factor(f, K0)
|
| 1466 |
+
elif K0.is_Algebraic:
|
| 1467 |
+
coeff, factors = dup_ext_factor(f, K0)
|
| 1468 |
+
elif K0.is_GaussianRing:
|
| 1469 |
+
coeff, factors = dup_zz_i_factor(f, K0)
|
| 1470 |
+
elif K0.is_GaussianField:
|
| 1471 |
+
coeff, factors = dup_qq_i_factor(f, K0)
|
| 1472 |
+
else:
|
| 1473 |
+
if not K0.is_Exact:
|
| 1474 |
+
K0_inexact, K0 = K0, K0.get_exact()
|
| 1475 |
+
f = dup_convert(f, K0_inexact, K0)
|
| 1476 |
+
else:
|
| 1477 |
+
K0_inexact = None
|
| 1478 |
+
|
| 1479 |
+
if K0.is_Field:
|
| 1480 |
+
K = K0.get_ring()
|
| 1481 |
+
|
| 1482 |
+
denom, f = dup_clear_denoms(f, K0, K)
|
| 1483 |
+
f = dup_convert(f, K0, K)
|
| 1484 |
+
else:
|
| 1485 |
+
K = K0
|
| 1486 |
+
|
| 1487 |
+
if K.is_ZZ:
|
| 1488 |
+
coeff, factors = dup_zz_factor(f, K)
|
| 1489 |
+
elif K.is_Poly:
|
| 1490 |
+
f, u = dmp_inject(f, 0, K)
|
| 1491 |
+
|
| 1492 |
+
coeff, factors = dmp_factor_list(f, u, K.dom)
|
| 1493 |
+
|
| 1494 |
+
for i, (f, k) in enumerate(factors):
|
| 1495 |
+
factors[i] = (dmp_eject(f, u, K), k)
|
| 1496 |
+
|
| 1497 |
+
coeff = K.convert(coeff, K.dom)
|
| 1498 |
+
else: # pragma: no cover
|
| 1499 |
+
raise DomainError('factorization not supported over %s' % K0)
|
| 1500 |
+
|
| 1501 |
+
if K0.is_Field:
|
| 1502 |
+
for i, (f, k) in enumerate(factors):
|
| 1503 |
+
factors[i] = (dup_convert(f, K, K0), k)
|
| 1504 |
+
|
| 1505 |
+
coeff = K0.convert(coeff, K)
|
| 1506 |
+
coeff = K0.quo(coeff, denom)
|
| 1507 |
+
|
| 1508 |
+
if K0_inexact:
|
| 1509 |
+
for i, (f, k) in enumerate(factors):
|
| 1510 |
+
max_norm = dup_max_norm(f, K0)
|
| 1511 |
+
f = dup_quo_ground(f, max_norm, K0)
|
| 1512 |
+
f = dup_convert(f, K0, K0_inexact)
|
| 1513 |
+
factors[i] = (f, k)
|
| 1514 |
+
coeff = K0.mul(coeff, K0.pow(max_norm, k))
|
| 1515 |
+
|
| 1516 |
+
coeff = K0_inexact.convert(coeff, K0)
|
| 1517 |
+
K0 = K0_inexact
|
| 1518 |
+
|
| 1519 |
+
if j:
|
| 1520 |
+
factors.insert(0, ([K0.one, K0.zero], j))
|
| 1521 |
+
|
| 1522 |
+
return coeff*cont, _sort_factors(factors)
|
| 1523 |
+
|
| 1524 |
+
|
| 1525 |
+
def dup_factor_list_include(f, K):
|
| 1526 |
+
"""Factor univariate polynomials into irreducibles in `K[x]`. """
|
| 1527 |
+
coeff, factors = dup_factor_list(f, K)
|
| 1528 |
+
|
| 1529 |
+
if not factors:
|
| 1530 |
+
return [(dup_strip([coeff]), 1)]
|
| 1531 |
+
else:
|
| 1532 |
+
g = dup_mul_ground(factors[0][0], coeff, K)
|
| 1533 |
+
return [(g, factors[0][1])] + factors[1:]
|
| 1534 |
+
|
| 1535 |
+
|
| 1536 |
+
def dmp_factor_list(f, u, K0):
|
| 1537 |
+
"""Factor multivariate polynomials into irreducibles in `K[X]`. """
|
| 1538 |
+
if not u:
|
| 1539 |
+
return dup_factor_list(f, K0)
|
| 1540 |
+
|
| 1541 |
+
J, f = dmp_terms_gcd(f, u, K0)
|
| 1542 |
+
cont, f = dmp_ground_primitive(f, u, K0)
|
| 1543 |
+
|
| 1544 |
+
if K0.is_FiniteField: # pragma: no cover
|
| 1545 |
+
coeff, factors = dmp_gf_factor(f, u, K0)
|
| 1546 |
+
elif K0.is_Algebraic:
|
| 1547 |
+
coeff, factors = dmp_ext_factor(f, u, K0)
|
| 1548 |
+
elif K0.is_GaussianRing:
|
| 1549 |
+
coeff, factors = dmp_zz_i_factor(f, u, K0)
|
| 1550 |
+
elif K0.is_GaussianField:
|
| 1551 |
+
coeff, factors = dmp_qq_i_factor(f, u, K0)
|
| 1552 |
+
else:
|
| 1553 |
+
if not K0.is_Exact:
|
| 1554 |
+
K0_inexact, K0 = K0, K0.get_exact()
|
| 1555 |
+
f = dmp_convert(f, u, K0_inexact, K0)
|
| 1556 |
+
else:
|
| 1557 |
+
K0_inexact = None
|
| 1558 |
+
|
| 1559 |
+
if K0.is_Field:
|
| 1560 |
+
K = K0.get_ring()
|
| 1561 |
+
|
| 1562 |
+
denom, f = dmp_clear_denoms(f, u, K0, K)
|
| 1563 |
+
f = dmp_convert(f, u, K0, K)
|
| 1564 |
+
else:
|
| 1565 |
+
K = K0
|
| 1566 |
+
|
| 1567 |
+
if K.is_ZZ:
|
| 1568 |
+
levels, f, v = dmp_exclude(f, u, K)
|
| 1569 |
+
coeff, factors = dmp_zz_factor(f, v, K)
|
| 1570 |
+
|
| 1571 |
+
for i, (f, k) in enumerate(factors):
|
| 1572 |
+
factors[i] = (dmp_include(f, levels, v, K), k)
|
| 1573 |
+
elif K.is_Poly:
|
| 1574 |
+
f, v = dmp_inject(f, u, K)
|
| 1575 |
+
|
| 1576 |
+
coeff, factors = dmp_factor_list(f, v, K.dom)
|
| 1577 |
+
|
| 1578 |
+
for i, (f, k) in enumerate(factors):
|
| 1579 |
+
factors[i] = (dmp_eject(f, v, K), k)
|
| 1580 |
+
|
| 1581 |
+
coeff = K.convert(coeff, K.dom)
|
| 1582 |
+
else: # pragma: no cover
|
| 1583 |
+
raise DomainError('factorization not supported over %s' % K0)
|
| 1584 |
+
|
| 1585 |
+
if K0.is_Field:
|
| 1586 |
+
for i, (f, k) in enumerate(factors):
|
| 1587 |
+
factors[i] = (dmp_convert(f, u, K, K0), k)
|
| 1588 |
+
|
| 1589 |
+
coeff = K0.convert(coeff, K)
|
| 1590 |
+
coeff = K0.quo(coeff, denom)
|
| 1591 |
+
|
| 1592 |
+
if K0_inexact:
|
| 1593 |
+
for i, (f, k) in enumerate(factors):
|
| 1594 |
+
max_norm = dmp_max_norm(f, u, K0)
|
| 1595 |
+
f = dmp_quo_ground(f, max_norm, u, K0)
|
| 1596 |
+
f = dmp_convert(f, u, K0, K0_inexact)
|
| 1597 |
+
factors[i] = (f, k)
|
| 1598 |
+
coeff = K0.mul(coeff, K0.pow(max_norm, k))
|
| 1599 |
+
|
| 1600 |
+
coeff = K0_inexact.convert(coeff, K0)
|
| 1601 |
+
K0 = K0_inexact
|
| 1602 |
+
|
| 1603 |
+
for i, j in enumerate(reversed(J)):
|
| 1604 |
+
if not j:
|
| 1605 |
+
continue
|
| 1606 |
+
|
| 1607 |
+
term = {(0,)*(u - i) + (1,) + (0,)*i: K0.one}
|
| 1608 |
+
factors.insert(0, (dmp_from_dict(term, u, K0), j))
|
| 1609 |
+
|
| 1610 |
+
return coeff*cont, _sort_factors(factors)
|
| 1611 |
+
|
| 1612 |
+
|
| 1613 |
+
def dmp_factor_list_include(f, u, K):
|
| 1614 |
+
"""Factor multivariate polynomials into irreducibles in `K[X]`. """
|
| 1615 |
+
if not u:
|
| 1616 |
+
return dup_factor_list_include(f, K)
|
| 1617 |
+
|
| 1618 |
+
coeff, factors = dmp_factor_list(f, u, K)
|
| 1619 |
+
|
| 1620 |
+
if not factors:
|
| 1621 |
+
return [(dmp_ground(coeff, u), 1)]
|
| 1622 |
+
else:
|
| 1623 |
+
g = dmp_mul_ground(factors[0][0], coeff, u, K)
|
| 1624 |
+
return [(g, factors[0][1])] + factors[1:]
|
| 1625 |
+
|
| 1626 |
+
|
| 1627 |
+
def dup_irreducible_p(f, K):
|
| 1628 |
+
"""
|
| 1629 |
+
Returns ``True`` if a univariate polynomial ``f`` has no factors
|
| 1630 |
+
over its domain.
|
| 1631 |
+
"""
|
| 1632 |
+
return dmp_irreducible_p(f, 0, K)
|
| 1633 |
+
|
| 1634 |
+
|
| 1635 |
+
def dmp_irreducible_p(f, u, K):
|
| 1636 |
+
"""
|
| 1637 |
+
Returns ``True`` if a multivariate polynomial ``f`` has no factors
|
| 1638 |
+
over its domain.
|
| 1639 |
+
"""
|
| 1640 |
+
_, factors = dmp_factor_list(f, u, K)
|
| 1641 |
+
|
| 1642 |
+
if not factors:
|
| 1643 |
+
return True
|
| 1644 |
+
elif len(factors) > 1:
|
| 1645 |
+
return False
|
| 1646 |
+
else:
|
| 1647 |
+
_, k = factors[0]
|
| 1648 |
+
return k == 1
|
vllm/lib/python3.10/site-packages/sympy/polys/galoistools.py
ADDED
|
@@ -0,0 +1,2532 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Dense univariate polynomials with coefficients in Galois fields. """
|
| 2 |
+
|
| 3 |
+
from math import ceil as _ceil, sqrt as _sqrt, prod
|
| 4 |
+
|
| 5 |
+
from sympy.core.random import uniform, _randint
|
| 6 |
+
from sympy.external.gmpy import SYMPY_INTS, MPZ, invert
|
| 7 |
+
from sympy.polys.polyconfig import query
|
| 8 |
+
from sympy.polys.polyerrors import ExactQuotientFailed
|
| 9 |
+
from sympy.polys.polyutils import _sort_factors
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def gf_crt(U, M, K=None):
|
| 13 |
+
"""
|
| 14 |
+
Chinese Remainder Theorem.
|
| 15 |
+
|
| 16 |
+
Given a set of integer residues ``u_0,...,u_n`` and a set of
|
| 17 |
+
co-prime integer moduli ``m_0,...,m_n``, returns an integer
|
| 18 |
+
``u``, such that ``u = u_i mod m_i`` for ``i = ``0,...,n``.
|
| 19 |
+
|
| 20 |
+
Examples
|
| 21 |
+
========
|
| 22 |
+
|
| 23 |
+
Consider a set of residues ``U = [49, 76, 65]``
|
| 24 |
+
and a set of moduli ``M = [99, 97, 95]``. Then we have::
|
| 25 |
+
|
| 26 |
+
>>> from sympy.polys.domains import ZZ
|
| 27 |
+
>>> from sympy.polys.galoistools import gf_crt
|
| 28 |
+
|
| 29 |
+
>>> gf_crt([49, 76, 65], [99, 97, 95], ZZ)
|
| 30 |
+
639985
|
| 31 |
+
|
| 32 |
+
This is the correct result because::
|
| 33 |
+
|
| 34 |
+
>>> [639985 % m for m in [99, 97, 95]]
|
| 35 |
+
[49, 76, 65]
|
| 36 |
+
|
| 37 |
+
Note: this is a low-level routine with no error checking.
|
| 38 |
+
|
| 39 |
+
See Also
|
| 40 |
+
========
|
| 41 |
+
|
| 42 |
+
sympy.ntheory.modular.crt : a higher level crt routine
|
| 43 |
+
sympy.ntheory.modular.solve_congruence
|
| 44 |
+
|
| 45 |
+
"""
|
| 46 |
+
p = prod(M, start=K.one)
|
| 47 |
+
v = K.zero
|
| 48 |
+
|
| 49 |
+
for u, m in zip(U, M):
|
| 50 |
+
e = p // m
|
| 51 |
+
s, _, _ = K.gcdex(e, m)
|
| 52 |
+
v += e*(u*s % m)
|
| 53 |
+
|
| 54 |
+
return v % p
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def gf_crt1(M, K):
|
| 58 |
+
"""
|
| 59 |
+
First part of the Chinese Remainder Theorem.
|
| 60 |
+
|
| 61 |
+
Examples
|
| 62 |
+
========
|
| 63 |
+
|
| 64 |
+
>>> from sympy.polys.domains import ZZ
|
| 65 |
+
>>> from sympy.polys.galoistools import gf_crt, gf_crt1, gf_crt2
|
| 66 |
+
>>> U = [49, 76, 65]
|
| 67 |
+
>>> M = [99, 97, 95]
|
| 68 |
+
|
| 69 |
+
The following two codes have the same result.
|
| 70 |
+
|
| 71 |
+
>>> gf_crt(U, M, ZZ)
|
| 72 |
+
639985
|
| 73 |
+
|
| 74 |
+
>>> p, E, S = gf_crt1(M, ZZ)
|
| 75 |
+
>>> gf_crt2(U, M, p, E, S, ZZ)
|
| 76 |
+
639985
|
| 77 |
+
|
| 78 |
+
However, it is faster when we want to fix ``M`` and
|
| 79 |
+
compute for multiple U, i.e. the following cases:
|
| 80 |
+
|
| 81 |
+
>>> p, E, S = gf_crt1(M, ZZ)
|
| 82 |
+
>>> Us = [[49, 76, 65], [23, 42, 67]]
|
| 83 |
+
>>> for U in Us:
|
| 84 |
+
... print(gf_crt2(U, M, p, E, S, ZZ))
|
| 85 |
+
639985
|
| 86 |
+
236237
|
| 87 |
+
|
| 88 |
+
See Also
|
| 89 |
+
========
|
| 90 |
+
|
| 91 |
+
sympy.ntheory.modular.crt1 : a higher level crt routine
|
| 92 |
+
sympy.polys.galoistools.gf_crt
|
| 93 |
+
sympy.polys.galoistools.gf_crt2
|
| 94 |
+
|
| 95 |
+
"""
|
| 96 |
+
E, S = [], []
|
| 97 |
+
p = prod(M, start=K.one)
|
| 98 |
+
|
| 99 |
+
for m in M:
|
| 100 |
+
E.append(p // m)
|
| 101 |
+
S.append(K.gcdex(E[-1], m)[0] % m)
|
| 102 |
+
|
| 103 |
+
return p, E, S
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def gf_crt2(U, M, p, E, S, K):
|
| 107 |
+
"""
|
| 108 |
+
Second part of the Chinese Remainder Theorem.
|
| 109 |
+
|
| 110 |
+
See ``gf_crt1`` for usage.
|
| 111 |
+
|
| 112 |
+
Examples
|
| 113 |
+
========
|
| 114 |
+
|
| 115 |
+
>>> from sympy.polys.domains import ZZ
|
| 116 |
+
>>> from sympy.polys.galoistools import gf_crt2
|
| 117 |
+
|
| 118 |
+
>>> U = [49, 76, 65]
|
| 119 |
+
>>> M = [99, 97, 95]
|
| 120 |
+
>>> p = 912285
|
| 121 |
+
>>> E = [9215, 9405, 9603]
|
| 122 |
+
>>> S = [62, 24, 12]
|
| 123 |
+
|
| 124 |
+
>>> gf_crt2(U, M, p, E, S, ZZ)
|
| 125 |
+
639985
|
| 126 |
+
|
| 127 |
+
See Also
|
| 128 |
+
========
|
| 129 |
+
|
| 130 |
+
sympy.ntheory.modular.crt2 : a higher level crt routine
|
| 131 |
+
sympy.polys.galoistools.gf_crt
|
| 132 |
+
sympy.polys.galoistools.gf_crt1
|
| 133 |
+
|
| 134 |
+
"""
|
| 135 |
+
v = K.zero
|
| 136 |
+
|
| 137 |
+
for u, m, e, s in zip(U, M, E, S):
|
| 138 |
+
v += e*(u*s % m)
|
| 139 |
+
|
| 140 |
+
return v % p
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def gf_int(a, p):
|
| 144 |
+
"""
|
| 145 |
+
Coerce ``a mod p`` to an integer in the range ``[-p/2, p/2]``.
|
| 146 |
+
|
| 147 |
+
Examples
|
| 148 |
+
========
|
| 149 |
+
|
| 150 |
+
>>> from sympy.polys.galoistools import gf_int
|
| 151 |
+
|
| 152 |
+
>>> gf_int(2, 7)
|
| 153 |
+
2
|
| 154 |
+
>>> gf_int(5, 7)
|
| 155 |
+
-2
|
| 156 |
+
|
| 157 |
+
"""
|
| 158 |
+
if a <= p // 2:
|
| 159 |
+
return a
|
| 160 |
+
else:
|
| 161 |
+
return a - p
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def gf_degree(f):
|
| 165 |
+
"""
|
| 166 |
+
Return the leading degree of ``f``.
|
| 167 |
+
|
| 168 |
+
Examples
|
| 169 |
+
========
|
| 170 |
+
|
| 171 |
+
>>> from sympy.polys.galoistools import gf_degree
|
| 172 |
+
|
| 173 |
+
>>> gf_degree([1, 1, 2, 0])
|
| 174 |
+
3
|
| 175 |
+
>>> gf_degree([])
|
| 176 |
+
-1
|
| 177 |
+
|
| 178 |
+
"""
|
| 179 |
+
return len(f) - 1
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def gf_LC(f, K):
|
| 183 |
+
"""
|
| 184 |
+
Return the leading coefficient of ``f``.
|
| 185 |
+
|
| 186 |
+
Examples
|
| 187 |
+
========
|
| 188 |
+
|
| 189 |
+
>>> from sympy.polys.domains import ZZ
|
| 190 |
+
>>> from sympy.polys.galoistools import gf_LC
|
| 191 |
+
|
| 192 |
+
>>> gf_LC([3, 0, 1], ZZ)
|
| 193 |
+
3
|
| 194 |
+
|
| 195 |
+
"""
|
| 196 |
+
if not f:
|
| 197 |
+
return K.zero
|
| 198 |
+
else:
|
| 199 |
+
return f[0]
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def gf_TC(f, K):
|
| 203 |
+
"""
|
| 204 |
+
Return the trailing coefficient of ``f``.
|
| 205 |
+
|
| 206 |
+
Examples
|
| 207 |
+
========
|
| 208 |
+
|
| 209 |
+
>>> from sympy.polys.domains import ZZ
|
| 210 |
+
>>> from sympy.polys.galoistools import gf_TC
|
| 211 |
+
|
| 212 |
+
>>> gf_TC([3, 0, 1], ZZ)
|
| 213 |
+
1
|
| 214 |
+
|
| 215 |
+
"""
|
| 216 |
+
if not f:
|
| 217 |
+
return K.zero
|
| 218 |
+
else:
|
| 219 |
+
return f[-1]
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def gf_strip(f):
|
| 223 |
+
"""
|
| 224 |
+
Remove leading zeros from ``f``.
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
Examples
|
| 228 |
+
========
|
| 229 |
+
|
| 230 |
+
>>> from sympy.polys.galoistools import gf_strip
|
| 231 |
+
|
| 232 |
+
>>> gf_strip([0, 0, 0, 3, 0, 1])
|
| 233 |
+
[3, 0, 1]
|
| 234 |
+
|
| 235 |
+
"""
|
| 236 |
+
if not f or f[0]:
|
| 237 |
+
return f
|
| 238 |
+
|
| 239 |
+
k = 0
|
| 240 |
+
|
| 241 |
+
for coeff in f:
|
| 242 |
+
if coeff:
|
| 243 |
+
break
|
| 244 |
+
else:
|
| 245 |
+
k += 1
|
| 246 |
+
|
| 247 |
+
return f[k:]
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def gf_trunc(f, p):
|
| 251 |
+
"""
|
| 252 |
+
Reduce all coefficients modulo ``p``.
|
| 253 |
+
|
| 254 |
+
Examples
|
| 255 |
+
========
|
| 256 |
+
|
| 257 |
+
>>> from sympy.polys.galoistools import gf_trunc
|
| 258 |
+
|
| 259 |
+
>>> gf_trunc([7, -2, 3], 5)
|
| 260 |
+
[2, 3, 3]
|
| 261 |
+
|
| 262 |
+
"""
|
| 263 |
+
return gf_strip([ a % p for a in f ])
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
def gf_normal(f, p, K):
|
| 267 |
+
"""
|
| 268 |
+
Normalize all coefficients in ``K``.
|
| 269 |
+
|
| 270 |
+
Examples
|
| 271 |
+
========
|
| 272 |
+
|
| 273 |
+
>>> from sympy.polys.domains import ZZ
|
| 274 |
+
>>> from sympy.polys.galoistools import gf_normal
|
| 275 |
+
|
| 276 |
+
>>> gf_normal([5, 10, 21, -3], 5, ZZ)
|
| 277 |
+
[1, 2]
|
| 278 |
+
|
| 279 |
+
"""
|
| 280 |
+
return gf_trunc(list(map(K, f)), p)
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
def gf_from_dict(f, p, K):
|
| 284 |
+
"""
|
| 285 |
+
Create a ``GF(p)[x]`` polynomial from a dict.
|
| 286 |
+
|
| 287 |
+
Examples
|
| 288 |
+
========
|
| 289 |
+
|
| 290 |
+
>>> from sympy.polys.domains import ZZ
|
| 291 |
+
>>> from sympy.polys.galoistools import gf_from_dict
|
| 292 |
+
|
| 293 |
+
>>> gf_from_dict({10: ZZ(4), 4: ZZ(33), 0: ZZ(-1)}, 5, ZZ)
|
| 294 |
+
[4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4]
|
| 295 |
+
|
| 296 |
+
"""
|
| 297 |
+
n, h = max(f.keys()), []
|
| 298 |
+
|
| 299 |
+
if isinstance(n, SYMPY_INTS):
|
| 300 |
+
for k in range(n, -1, -1):
|
| 301 |
+
h.append(f.get(k, K.zero) % p)
|
| 302 |
+
else:
|
| 303 |
+
(n,) = n
|
| 304 |
+
|
| 305 |
+
for k in range(n, -1, -1):
|
| 306 |
+
h.append(f.get((k,), K.zero) % p)
|
| 307 |
+
|
| 308 |
+
return gf_trunc(h, p)
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def gf_to_dict(f, p, symmetric=True):
|
| 312 |
+
"""
|
| 313 |
+
Convert a ``GF(p)[x]`` polynomial to a dict.
|
| 314 |
+
|
| 315 |
+
Examples
|
| 316 |
+
========
|
| 317 |
+
|
| 318 |
+
>>> from sympy.polys.galoistools import gf_to_dict
|
| 319 |
+
|
| 320 |
+
>>> gf_to_dict([4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4], 5)
|
| 321 |
+
{0: -1, 4: -2, 10: -1}
|
| 322 |
+
>>> gf_to_dict([4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4], 5, symmetric=False)
|
| 323 |
+
{0: 4, 4: 3, 10: 4}
|
| 324 |
+
|
| 325 |
+
"""
|
| 326 |
+
n, result = gf_degree(f), {}
|
| 327 |
+
|
| 328 |
+
for k in range(0, n + 1):
|
| 329 |
+
if symmetric:
|
| 330 |
+
a = gf_int(f[n - k], p)
|
| 331 |
+
else:
|
| 332 |
+
a = f[n - k]
|
| 333 |
+
|
| 334 |
+
if a:
|
| 335 |
+
result[k] = a
|
| 336 |
+
|
| 337 |
+
return result
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
def gf_from_int_poly(f, p):
|
| 341 |
+
"""
|
| 342 |
+
Create a ``GF(p)[x]`` polynomial from ``Z[x]``.
|
| 343 |
+
|
| 344 |
+
Examples
|
| 345 |
+
========
|
| 346 |
+
|
| 347 |
+
>>> from sympy.polys.galoistools import gf_from_int_poly
|
| 348 |
+
|
| 349 |
+
>>> gf_from_int_poly([7, -2, 3], 5)
|
| 350 |
+
[2, 3, 3]
|
| 351 |
+
|
| 352 |
+
"""
|
| 353 |
+
return gf_trunc(f, p)
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
def gf_to_int_poly(f, p, symmetric=True):
|
| 357 |
+
"""
|
| 358 |
+
Convert a ``GF(p)[x]`` polynomial to ``Z[x]``.
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
Examples
|
| 362 |
+
========
|
| 363 |
+
|
| 364 |
+
>>> from sympy.polys.galoistools import gf_to_int_poly
|
| 365 |
+
|
| 366 |
+
>>> gf_to_int_poly([2, 3, 3], 5)
|
| 367 |
+
[2, -2, -2]
|
| 368 |
+
>>> gf_to_int_poly([2, 3, 3], 5, symmetric=False)
|
| 369 |
+
[2, 3, 3]
|
| 370 |
+
|
| 371 |
+
"""
|
| 372 |
+
if symmetric:
|
| 373 |
+
return [ gf_int(c, p) for c in f ]
|
| 374 |
+
else:
|
| 375 |
+
return f
|
| 376 |
+
|
| 377 |
+
|
| 378 |
+
def gf_neg(f, p, K):
|
| 379 |
+
"""
|
| 380 |
+
Negate a polynomial in ``GF(p)[x]``.
|
| 381 |
+
|
| 382 |
+
Examples
|
| 383 |
+
========
|
| 384 |
+
|
| 385 |
+
>>> from sympy.polys.domains import ZZ
|
| 386 |
+
>>> from sympy.polys.galoistools import gf_neg
|
| 387 |
+
|
| 388 |
+
>>> gf_neg([3, 2, 1, 0], 5, ZZ)
|
| 389 |
+
[2, 3, 4, 0]
|
| 390 |
+
|
| 391 |
+
"""
|
| 392 |
+
return [ -coeff % p for coeff in f ]
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def gf_add_ground(f, a, p, K):
|
| 396 |
+
"""
|
| 397 |
+
Compute ``f + a`` where ``f`` in ``GF(p)[x]`` and ``a`` in ``GF(p)``.
|
| 398 |
+
|
| 399 |
+
Examples
|
| 400 |
+
========
|
| 401 |
+
|
| 402 |
+
>>> from sympy.polys.domains import ZZ
|
| 403 |
+
>>> from sympy.polys.galoistools import gf_add_ground
|
| 404 |
+
|
| 405 |
+
>>> gf_add_ground([3, 2, 4], 2, 5, ZZ)
|
| 406 |
+
[3, 2, 1]
|
| 407 |
+
|
| 408 |
+
"""
|
| 409 |
+
if not f:
|
| 410 |
+
a = a % p
|
| 411 |
+
else:
|
| 412 |
+
a = (f[-1] + a) % p
|
| 413 |
+
|
| 414 |
+
if len(f) > 1:
|
| 415 |
+
return f[:-1] + [a]
|
| 416 |
+
|
| 417 |
+
if not a:
|
| 418 |
+
return []
|
| 419 |
+
else:
|
| 420 |
+
return [a]
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
def gf_sub_ground(f, a, p, K):
|
| 424 |
+
"""
|
| 425 |
+
Compute ``f - a`` where ``f`` in ``GF(p)[x]`` and ``a`` in ``GF(p)``.
|
| 426 |
+
|
| 427 |
+
Examples
|
| 428 |
+
========
|
| 429 |
+
|
| 430 |
+
>>> from sympy.polys.domains import ZZ
|
| 431 |
+
>>> from sympy.polys.galoistools import gf_sub_ground
|
| 432 |
+
|
| 433 |
+
>>> gf_sub_ground([3, 2, 4], 2, 5, ZZ)
|
| 434 |
+
[3, 2, 2]
|
| 435 |
+
|
| 436 |
+
"""
|
| 437 |
+
if not f:
|
| 438 |
+
a = -a % p
|
| 439 |
+
else:
|
| 440 |
+
a = (f[-1] - a) % p
|
| 441 |
+
|
| 442 |
+
if len(f) > 1:
|
| 443 |
+
return f[:-1] + [a]
|
| 444 |
+
|
| 445 |
+
if not a:
|
| 446 |
+
return []
|
| 447 |
+
else:
|
| 448 |
+
return [a]
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
def gf_mul_ground(f, a, p, K):
|
| 452 |
+
"""
|
| 453 |
+
Compute ``f * a`` where ``f`` in ``GF(p)[x]`` and ``a`` in ``GF(p)``.
|
| 454 |
+
|
| 455 |
+
Examples
|
| 456 |
+
========
|
| 457 |
+
|
| 458 |
+
>>> from sympy.polys.domains import ZZ
|
| 459 |
+
>>> from sympy.polys.galoistools import gf_mul_ground
|
| 460 |
+
|
| 461 |
+
>>> gf_mul_ground([3, 2, 4], 2, 5, ZZ)
|
| 462 |
+
[1, 4, 3]
|
| 463 |
+
|
| 464 |
+
"""
|
| 465 |
+
if not a:
|
| 466 |
+
return []
|
| 467 |
+
else:
|
| 468 |
+
return [ (a*b) % p for b in f ]
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
def gf_quo_ground(f, a, p, K):
|
| 472 |
+
"""
|
| 473 |
+
Compute ``f/a`` where ``f`` in ``GF(p)[x]`` and ``a`` in ``GF(p)``.
|
| 474 |
+
|
| 475 |
+
Examples
|
| 476 |
+
========
|
| 477 |
+
|
| 478 |
+
>>> from sympy.polys.domains import ZZ
|
| 479 |
+
>>> from sympy.polys.galoistools import gf_quo_ground
|
| 480 |
+
|
| 481 |
+
>>> gf_quo_ground(ZZ.map([3, 2, 4]), ZZ(2), 5, ZZ)
|
| 482 |
+
[4, 1, 2]
|
| 483 |
+
|
| 484 |
+
"""
|
| 485 |
+
return gf_mul_ground(f, K.invert(a, p), p, K)
|
| 486 |
+
|
| 487 |
+
|
| 488 |
+
def gf_add(f, g, p, K):
|
| 489 |
+
"""
|
| 490 |
+
Add polynomials in ``GF(p)[x]``.
|
| 491 |
+
|
| 492 |
+
Examples
|
| 493 |
+
========
|
| 494 |
+
|
| 495 |
+
>>> from sympy.polys.domains import ZZ
|
| 496 |
+
>>> from sympy.polys.galoistools import gf_add
|
| 497 |
+
|
| 498 |
+
>>> gf_add([3, 2, 4], [2, 2, 2], 5, ZZ)
|
| 499 |
+
[4, 1]
|
| 500 |
+
|
| 501 |
+
"""
|
| 502 |
+
if not f:
|
| 503 |
+
return g
|
| 504 |
+
if not g:
|
| 505 |
+
return f
|
| 506 |
+
|
| 507 |
+
df = gf_degree(f)
|
| 508 |
+
dg = gf_degree(g)
|
| 509 |
+
|
| 510 |
+
if df == dg:
|
| 511 |
+
return gf_strip([ (a + b) % p for a, b in zip(f, g) ])
|
| 512 |
+
else:
|
| 513 |
+
k = abs(df - dg)
|
| 514 |
+
|
| 515 |
+
if df > dg:
|
| 516 |
+
h, f = f[:k], f[k:]
|
| 517 |
+
else:
|
| 518 |
+
h, g = g[:k], g[k:]
|
| 519 |
+
|
| 520 |
+
return h + [ (a + b) % p for a, b in zip(f, g) ]
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
def gf_sub(f, g, p, K):
|
| 524 |
+
"""
|
| 525 |
+
Subtract polynomials in ``GF(p)[x]``.
|
| 526 |
+
|
| 527 |
+
Examples
|
| 528 |
+
========
|
| 529 |
+
|
| 530 |
+
>>> from sympy.polys.domains import ZZ
|
| 531 |
+
>>> from sympy.polys.galoistools import gf_sub
|
| 532 |
+
|
| 533 |
+
>>> gf_sub([3, 2, 4], [2, 2, 2], 5, ZZ)
|
| 534 |
+
[1, 0, 2]
|
| 535 |
+
|
| 536 |
+
"""
|
| 537 |
+
if not g:
|
| 538 |
+
return f
|
| 539 |
+
if not f:
|
| 540 |
+
return gf_neg(g, p, K)
|
| 541 |
+
|
| 542 |
+
df = gf_degree(f)
|
| 543 |
+
dg = gf_degree(g)
|
| 544 |
+
|
| 545 |
+
if df == dg:
|
| 546 |
+
return gf_strip([ (a - b) % p for a, b in zip(f, g) ])
|
| 547 |
+
else:
|
| 548 |
+
k = abs(df - dg)
|
| 549 |
+
|
| 550 |
+
if df > dg:
|
| 551 |
+
h, f = f[:k], f[k:]
|
| 552 |
+
else:
|
| 553 |
+
h, g = gf_neg(g[:k], p, K), g[k:]
|
| 554 |
+
|
| 555 |
+
return h + [ (a - b) % p for a, b in zip(f, g) ]
|
| 556 |
+
|
| 557 |
+
|
| 558 |
+
def gf_mul(f, g, p, K):
|
| 559 |
+
"""
|
| 560 |
+
Multiply polynomials in ``GF(p)[x]``.
|
| 561 |
+
|
| 562 |
+
Examples
|
| 563 |
+
========
|
| 564 |
+
|
| 565 |
+
>>> from sympy.polys.domains import ZZ
|
| 566 |
+
>>> from sympy.polys.galoistools import gf_mul
|
| 567 |
+
|
| 568 |
+
>>> gf_mul([3, 2, 4], [2, 2, 2], 5, ZZ)
|
| 569 |
+
[1, 0, 3, 2, 3]
|
| 570 |
+
|
| 571 |
+
"""
|
| 572 |
+
df = gf_degree(f)
|
| 573 |
+
dg = gf_degree(g)
|
| 574 |
+
|
| 575 |
+
dh = df + dg
|
| 576 |
+
h = [0]*(dh + 1)
|
| 577 |
+
|
| 578 |
+
for i in range(0, dh + 1):
|
| 579 |
+
coeff = K.zero
|
| 580 |
+
|
| 581 |
+
for j in range(max(0, i - dg), min(i, df) + 1):
|
| 582 |
+
coeff += f[j]*g[i - j]
|
| 583 |
+
|
| 584 |
+
h[i] = coeff % p
|
| 585 |
+
|
| 586 |
+
return gf_strip(h)
|
| 587 |
+
|
| 588 |
+
|
| 589 |
+
def gf_sqr(f, p, K):
|
| 590 |
+
"""
|
| 591 |
+
Square polynomials in ``GF(p)[x]``.
|
| 592 |
+
|
| 593 |
+
Examples
|
| 594 |
+
========
|
| 595 |
+
|
| 596 |
+
>>> from sympy.polys.domains import ZZ
|
| 597 |
+
>>> from sympy.polys.galoistools import gf_sqr
|
| 598 |
+
|
| 599 |
+
>>> gf_sqr([3, 2, 4], 5, ZZ)
|
| 600 |
+
[4, 2, 3, 1, 1]
|
| 601 |
+
|
| 602 |
+
"""
|
| 603 |
+
df = gf_degree(f)
|
| 604 |
+
|
| 605 |
+
dh = 2*df
|
| 606 |
+
h = [0]*(dh + 1)
|
| 607 |
+
|
| 608 |
+
for i in range(0, dh + 1):
|
| 609 |
+
coeff = K.zero
|
| 610 |
+
|
| 611 |
+
jmin = max(0, i - df)
|
| 612 |
+
jmax = min(i, df)
|
| 613 |
+
|
| 614 |
+
n = jmax - jmin + 1
|
| 615 |
+
|
| 616 |
+
jmax = jmin + n // 2 - 1
|
| 617 |
+
|
| 618 |
+
for j in range(jmin, jmax + 1):
|
| 619 |
+
coeff += f[j]*f[i - j]
|
| 620 |
+
|
| 621 |
+
coeff += coeff
|
| 622 |
+
|
| 623 |
+
if n & 1:
|
| 624 |
+
elem = f[jmax + 1]
|
| 625 |
+
coeff += elem**2
|
| 626 |
+
|
| 627 |
+
h[i] = coeff % p
|
| 628 |
+
|
| 629 |
+
return gf_strip(h)
|
| 630 |
+
|
| 631 |
+
|
| 632 |
+
def gf_add_mul(f, g, h, p, K):
|
| 633 |
+
"""
|
| 634 |
+
Returns ``f + g*h`` where ``f``, ``g``, ``h`` in ``GF(p)[x]``.
|
| 635 |
+
|
| 636 |
+
Examples
|
| 637 |
+
========
|
| 638 |
+
|
| 639 |
+
>>> from sympy.polys.domains import ZZ
|
| 640 |
+
>>> from sympy.polys.galoistools import gf_add_mul
|
| 641 |
+
>>> gf_add_mul([3, 2, 4], [2, 2, 2], [1, 4], 5, ZZ)
|
| 642 |
+
[2, 3, 2, 2]
|
| 643 |
+
"""
|
| 644 |
+
return gf_add(f, gf_mul(g, h, p, K), p, K)
|
| 645 |
+
|
| 646 |
+
|
| 647 |
+
def gf_sub_mul(f, g, h, p, K):
|
| 648 |
+
"""
|
| 649 |
+
Compute ``f - g*h`` where ``f``, ``g``, ``h`` in ``GF(p)[x]``.
|
| 650 |
+
|
| 651 |
+
Examples
|
| 652 |
+
========
|
| 653 |
+
|
| 654 |
+
>>> from sympy.polys.domains import ZZ
|
| 655 |
+
>>> from sympy.polys.galoistools import gf_sub_mul
|
| 656 |
+
|
| 657 |
+
>>> gf_sub_mul([3, 2, 4], [2, 2, 2], [1, 4], 5, ZZ)
|
| 658 |
+
[3, 3, 2, 1]
|
| 659 |
+
|
| 660 |
+
"""
|
| 661 |
+
return gf_sub(f, gf_mul(g, h, p, K), p, K)
|
| 662 |
+
|
| 663 |
+
|
| 664 |
+
def gf_expand(F, p, K):
|
| 665 |
+
"""
|
| 666 |
+
Expand results of :func:`~.factor` in ``GF(p)[x]``.
|
| 667 |
+
|
| 668 |
+
Examples
|
| 669 |
+
========
|
| 670 |
+
|
| 671 |
+
>>> from sympy.polys.domains import ZZ
|
| 672 |
+
>>> from sympy.polys.galoistools import gf_expand
|
| 673 |
+
|
| 674 |
+
>>> gf_expand([([3, 2, 4], 1), ([2, 2], 2), ([3, 1], 3)], 5, ZZ)
|
| 675 |
+
[4, 3, 0, 3, 0, 1, 4, 1]
|
| 676 |
+
|
| 677 |
+
"""
|
| 678 |
+
if isinstance(F, tuple):
|
| 679 |
+
lc, F = F
|
| 680 |
+
else:
|
| 681 |
+
lc = K.one
|
| 682 |
+
|
| 683 |
+
g = [lc]
|
| 684 |
+
|
| 685 |
+
for f, k in F:
|
| 686 |
+
f = gf_pow(f, k, p, K)
|
| 687 |
+
g = gf_mul(g, f, p, K)
|
| 688 |
+
|
| 689 |
+
return g
|
| 690 |
+
|
| 691 |
+
|
| 692 |
+
def gf_div(f, g, p, K):
|
| 693 |
+
"""
|
| 694 |
+
Division with remainder in ``GF(p)[x]``.
|
| 695 |
+
|
| 696 |
+
Given univariate polynomials ``f`` and ``g`` with coefficients in a
|
| 697 |
+
finite field with ``p`` elements, returns polynomials ``q`` and ``r``
|
| 698 |
+
(quotient and remainder) such that ``f = q*g + r``.
|
| 699 |
+
|
| 700 |
+
Consider polynomials ``x**3 + x + 1`` and ``x**2 + x`` in GF(2)::
|
| 701 |
+
|
| 702 |
+
>>> from sympy.polys.domains import ZZ
|
| 703 |
+
>>> from sympy.polys.galoistools import gf_div, gf_add_mul
|
| 704 |
+
|
| 705 |
+
>>> gf_div(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)
|
| 706 |
+
([1, 1], [1])
|
| 707 |
+
|
| 708 |
+
As result we obtained quotient ``x + 1`` and remainder ``1``, thus::
|
| 709 |
+
|
| 710 |
+
>>> gf_add_mul(ZZ.map([1]), ZZ.map([1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)
|
| 711 |
+
[1, 0, 1, 1]
|
| 712 |
+
|
| 713 |
+
References
|
| 714 |
+
==========
|
| 715 |
+
|
| 716 |
+
.. [1] [Monagan93]_
|
| 717 |
+
.. [2] [Gathen99]_
|
| 718 |
+
|
| 719 |
+
"""
|
| 720 |
+
df = gf_degree(f)
|
| 721 |
+
dg = gf_degree(g)
|
| 722 |
+
|
| 723 |
+
if not g:
|
| 724 |
+
raise ZeroDivisionError("polynomial division")
|
| 725 |
+
elif df < dg:
|
| 726 |
+
return [], f
|
| 727 |
+
|
| 728 |
+
inv = K.invert(g[0], p)
|
| 729 |
+
|
| 730 |
+
h, dq, dr = list(f), df - dg, dg - 1
|
| 731 |
+
|
| 732 |
+
for i in range(0, df + 1):
|
| 733 |
+
coeff = h[i]
|
| 734 |
+
|
| 735 |
+
for j in range(max(0, dg - i), min(df - i, dr) + 1):
|
| 736 |
+
coeff -= h[i + j - dg] * g[dg - j]
|
| 737 |
+
|
| 738 |
+
if i <= dq:
|
| 739 |
+
coeff *= inv
|
| 740 |
+
|
| 741 |
+
h[i] = coeff % p
|
| 742 |
+
|
| 743 |
+
return h[:dq + 1], gf_strip(h[dq + 1:])
|
| 744 |
+
|
| 745 |
+
|
| 746 |
+
def gf_rem(f, g, p, K):
|
| 747 |
+
"""
|
| 748 |
+
Compute polynomial remainder in ``GF(p)[x]``.
|
| 749 |
+
|
| 750 |
+
Examples
|
| 751 |
+
========
|
| 752 |
+
|
| 753 |
+
>>> from sympy.polys.domains import ZZ
|
| 754 |
+
>>> from sympy.polys.galoistools import gf_rem
|
| 755 |
+
|
| 756 |
+
>>> gf_rem(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)
|
| 757 |
+
[1]
|
| 758 |
+
|
| 759 |
+
"""
|
| 760 |
+
return gf_div(f, g, p, K)[1]
|
| 761 |
+
|
| 762 |
+
|
| 763 |
+
def gf_quo(f, g, p, K):
|
| 764 |
+
"""
|
| 765 |
+
Compute exact quotient in ``GF(p)[x]``.
|
| 766 |
+
|
| 767 |
+
Examples
|
| 768 |
+
========
|
| 769 |
+
|
| 770 |
+
>>> from sympy.polys.domains import ZZ
|
| 771 |
+
>>> from sympy.polys.galoistools import gf_quo
|
| 772 |
+
|
| 773 |
+
>>> gf_quo(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)
|
| 774 |
+
[1, 1]
|
| 775 |
+
>>> gf_quo(ZZ.map([1, 0, 3, 2, 3]), ZZ.map([2, 2, 2]), 5, ZZ)
|
| 776 |
+
[3, 2, 4]
|
| 777 |
+
|
| 778 |
+
"""
|
| 779 |
+
df = gf_degree(f)
|
| 780 |
+
dg = gf_degree(g)
|
| 781 |
+
|
| 782 |
+
if not g:
|
| 783 |
+
raise ZeroDivisionError("polynomial division")
|
| 784 |
+
elif df < dg:
|
| 785 |
+
return []
|
| 786 |
+
|
| 787 |
+
inv = K.invert(g[0], p)
|
| 788 |
+
|
| 789 |
+
h, dq, dr = f[:], df - dg, dg - 1
|
| 790 |
+
|
| 791 |
+
for i in range(0, dq + 1):
|
| 792 |
+
coeff = h[i]
|
| 793 |
+
|
| 794 |
+
for j in range(max(0, dg - i), min(df - i, dr) + 1):
|
| 795 |
+
coeff -= h[i + j - dg] * g[dg - j]
|
| 796 |
+
|
| 797 |
+
h[i] = (coeff * inv) % p
|
| 798 |
+
|
| 799 |
+
return h[:dq + 1]
|
| 800 |
+
|
| 801 |
+
|
| 802 |
+
def gf_exquo(f, g, p, K):
|
| 803 |
+
"""
|
| 804 |
+
Compute polynomial quotient in ``GF(p)[x]``.
|
| 805 |
+
|
| 806 |
+
Examples
|
| 807 |
+
========
|
| 808 |
+
|
| 809 |
+
>>> from sympy.polys.domains import ZZ
|
| 810 |
+
>>> from sympy.polys.galoistools import gf_exquo
|
| 811 |
+
|
| 812 |
+
>>> gf_exquo(ZZ.map([1, 0, 3, 2, 3]), ZZ.map([2, 2, 2]), 5, ZZ)
|
| 813 |
+
[3, 2, 4]
|
| 814 |
+
|
| 815 |
+
>>> gf_exquo(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)
|
| 816 |
+
Traceback (most recent call last):
|
| 817 |
+
...
|
| 818 |
+
ExactQuotientFailed: [1, 1, 0] does not divide [1, 0, 1, 1]
|
| 819 |
+
|
| 820 |
+
"""
|
| 821 |
+
q, r = gf_div(f, g, p, K)
|
| 822 |
+
|
| 823 |
+
if not r:
|
| 824 |
+
return q
|
| 825 |
+
else:
|
| 826 |
+
raise ExactQuotientFailed(f, g)
|
| 827 |
+
|
| 828 |
+
|
| 829 |
+
def gf_lshift(f, n, K):
|
| 830 |
+
"""
|
| 831 |
+
Efficiently multiply ``f`` by ``x**n``.
|
| 832 |
+
|
| 833 |
+
Examples
|
| 834 |
+
========
|
| 835 |
+
|
| 836 |
+
>>> from sympy.polys.domains import ZZ
|
| 837 |
+
>>> from sympy.polys.galoistools import gf_lshift
|
| 838 |
+
|
| 839 |
+
>>> gf_lshift([3, 2, 4], 4, ZZ)
|
| 840 |
+
[3, 2, 4, 0, 0, 0, 0]
|
| 841 |
+
|
| 842 |
+
"""
|
| 843 |
+
if not f:
|
| 844 |
+
return f
|
| 845 |
+
else:
|
| 846 |
+
return f + [K.zero]*n
|
| 847 |
+
|
| 848 |
+
|
| 849 |
+
def gf_rshift(f, n, K):
|
| 850 |
+
"""
|
| 851 |
+
Efficiently divide ``f`` by ``x**n``.
|
| 852 |
+
|
| 853 |
+
Examples
|
| 854 |
+
========
|
| 855 |
+
|
| 856 |
+
>>> from sympy.polys.domains import ZZ
|
| 857 |
+
>>> from sympy.polys.galoistools import gf_rshift
|
| 858 |
+
|
| 859 |
+
>>> gf_rshift([1, 2, 3, 4, 0], 3, ZZ)
|
| 860 |
+
([1, 2], [3, 4, 0])
|
| 861 |
+
|
| 862 |
+
"""
|
| 863 |
+
if not n:
|
| 864 |
+
return f, []
|
| 865 |
+
else:
|
| 866 |
+
return f[:-n], f[-n:]
|
| 867 |
+
|
| 868 |
+
|
| 869 |
+
def gf_pow(f, n, p, K):
|
| 870 |
+
"""
|
| 871 |
+
Compute ``f**n`` in ``GF(p)[x]`` using repeated squaring.
|
| 872 |
+
|
| 873 |
+
Examples
|
| 874 |
+
========
|
| 875 |
+
|
| 876 |
+
>>> from sympy.polys.domains import ZZ
|
| 877 |
+
>>> from sympy.polys.galoistools import gf_pow
|
| 878 |
+
|
| 879 |
+
>>> gf_pow([3, 2, 4], 3, 5, ZZ)
|
| 880 |
+
[2, 4, 4, 2, 2, 1, 4]
|
| 881 |
+
|
| 882 |
+
"""
|
| 883 |
+
if not n:
|
| 884 |
+
return [K.one]
|
| 885 |
+
elif n == 1:
|
| 886 |
+
return f
|
| 887 |
+
elif n == 2:
|
| 888 |
+
return gf_sqr(f, p, K)
|
| 889 |
+
|
| 890 |
+
h = [K.one]
|
| 891 |
+
|
| 892 |
+
while True:
|
| 893 |
+
if n & 1:
|
| 894 |
+
h = gf_mul(h, f, p, K)
|
| 895 |
+
n -= 1
|
| 896 |
+
|
| 897 |
+
n >>= 1
|
| 898 |
+
|
| 899 |
+
if not n:
|
| 900 |
+
break
|
| 901 |
+
|
| 902 |
+
f = gf_sqr(f, p, K)
|
| 903 |
+
|
| 904 |
+
return h
|
| 905 |
+
|
| 906 |
+
def gf_frobenius_monomial_base(g, p, K):
|
| 907 |
+
"""
|
| 908 |
+
return the list of ``x**(i*p) mod g in Z_p`` for ``i = 0, .., n - 1``
|
| 909 |
+
where ``n = gf_degree(g)``
|
| 910 |
+
|
| 911 |
+
Examples
|
| 912 |
+
========
|
| 913 |
+
|
| 914 |
+
>>> from sympy.polys.domains import ZZ
|
| 915 |
+
>>> from sympy.polys.galoistools import gf_frobenius_monomial_base
|
| 916 |
+
>>> g = ZZ.map([1, 0, 2, 1])
|
| 917 |
+
>>> gf_frobenius_monomial_base(g, 5, ZZ)
|
| 918 |
+
[[1], [4, 4, 2], [1, 2]]
|
| 919 |
+
|
| 920 |
+
"""
|
| 921 |
+
n = gf_degree(g)
|
| 922 |
+
if n == 0:
|
| 923 |
+
return []
|
| 924 |
+
b = [0]*n
|
| 925 |
+
b[0] = [1]
|
| 926 |
+
if p < n:
|
| 927 |
+
for i in range(1, n):
|
| 928 |
+
mon = gf_lshift(b[i - 1], p, K)
|
| 929 |
+
b[i] = gf_rem(mon, g, p, K)
|
| 930 |
+
elif n > 1:
|
| 931 |
+
b[1] = gf_pow_mod([K.one, K.zero], p, g, p, K)
|
| 932 |
+
for i in range(2, n):
|
| 933 |
+
b[i] = gf_mul(b[i - 1], b[1], p, K)
|
| 934 |
+
b[i] = gf_rem(b[i], g, p, K)
|
| 935 |
+
|
| 936 |
+
return b
|
| 937 |
+
|
| 938 |
+
def gf_frobenius_map(f, g, b, p, K):
|
| 939 |
+
"""
|
| 940 |
+
compute gf_pow_mod(f, p, g, p, K) using the Frobenius map
|
| 941 |
+
|
| 942 |
+
Parameters
|
| 943 |
+
==========
|
| 944 |
+
|
| 945 |
+
f, g : polynomials in ``GF(p)[x]``
|
| 946 |
+
b : frobenius monomial base
|
| 947 |
+
p : prime number
|
| 948 |
+
K : domain
|
| 949 |
+
|
| 950 |
+
Examples
|
| 951 |
+
========
|
| 952 |
+
|
| 953 |
+
>>> from sympy.polys.domains import ZZ
|
| 954 |
+
>>> from sympy.polys.galoistools import gf_frobenius_monomial_base, gf_frobenius_map
|
| 955 |
+
>>> f = ZZ.map([2, 1, 0, 1])
|
| 956 |
+
>>> g = ZZ.map([1, 0, 2, 1])
|
| 957 |
+
>>> p = 5
|
| 958 |
+
>>> b = gf_frobenius_monomial_base(g, p, ZZ)
|
| 959 |
+
>>> r = gf_frobenius_map(f, g, b, p, ZZ)
|
| 960 |
+
>>> gf_frobenius_map(f, g, b, p, ZZ)
|
| 961 |
+
[4, 0, 3]
|
| 962 |
+
"""
|
| 963 |
+
m = gf_degree(g)
|
| 964 |
+
if gf_degree(f) >= m:
|
| 965 |
+
f = gf_rem(f, g, p, K)
|
| 966 |
+
if not f:
|
| 967 |
+
return []
|
| 968 |
+
n = gf_degree(f)
|
| 969 |
+
sf = [f[-1]]
|
| 970 |
+
for i in range(1, n + 1):
|
| 971 |
+
v = gf_mul_ground(b[i], f[n - i], p, K)
|
| 972 |
+
sf = gf_add(sf, v, p, K)
|
| 973 |
+
return sf
|
| 974 |
+
|
| 975 |
+
def _gf_pow_pnm1d2(f, n, g, b, p, K):
|
| 976 |
+
"""
|
| 977 |
+
utility function for ``gf_edf_zassenhaus``
|
| 978 |
+
Compute ``f**((p**n - 1) // 2)`` in ``GF(p)[x]/(g)``
|
| 979 |
+
``f**((p**n - 1) // 2) = (f*f**p*...*f**(p**n - 1))**((p - 1) // 2)``
|
| 980 |
+
"""
|
| 981 |
+
f = gf_rem(f, g, p, K)
|
| 982 |
+
h = f
|
| 983 |
+
r = f
|
| 984 |
+
for i in range(1, n):
|
| 985 |
+
h = gf_frobenius_map(h, g, b, p, K)
|
| 986 |
+
r = gf_mul(r, h, p, K)
|
| 987 |
+
r = gf_rem(r, g, p, K)
|
| 988 |
+
|
| 989 |
+
res = gf_pow_mod(r, (p - 1)//2, g, p, K)
|
| 990 |
+
return res
|
| 991 |
+
|
| 992 |
+
def gf_pow_mod(f, n, g, p, K):
|
| 993 |
+
"""
|
| 994 |
+
Compute ``f**n`` in ``GF(p)[x]/(g)`` using repeated squaring.
|
| 995 |
+
|
| 996 |
+
Given polynomials ``f`` and ``g`` in ``GF(p)[x]`` and a non-negative
|
| 997 |
+
integer ``n``, efficiently computes ``f**n (mod g)`` i.e. the remainder
|
| 998 |
+
of ``f**n`` from division by ``g``, using the repeated squaring algorithm.
|
| 999 |
+
|
| 1000 |
+
Examples
|
| 1001 |
+
========
|
| 1002 |
+
|
| 1003 |
+
>>> from sympy.polys.domains import ZZ
|
| 1004 |
+
>>> from sympy.polys.galoistools import gf_pow_mod
|
| 1005 |
+
|
| 1006 |
+
>>> gf_pow_mod(ZZ.map([3, 2, 4]), 3, ZZ.map([1, 1]), 5, ZZ)
|
| 1007 |
+
[]
|
| 1008 |
+
|
| 1009 |
+
References
|
| 1010 |
+
==========
|
| 1011 |
+
|
| 1012 |
+
.. [1] [Gathen99]_
|
| 1013 |
+
|
| 1014 |
+
"""
|
| 1015 |
+
if not n:
|
| 1016 |
+
return [K.one]
|
| 1017 |
+
elif n == 1:
|
| 1018 |
+
return gf_rem(f, g, p, K)
|
| 1019 |
+
elif n == 2:
|
| 1020 |
+
return gf_rem(gf_sqr(f, p, K), g, p, K)
|
| 1021 |
+
|
| 1022 |
+
h = [K.one]
|
| 1023 |
+
|
| 1024 |
+
while True:
|
| 1025 |
+
if n & 1:
|
| 1026 |
+
h = gf_mul(h, f, p, K)
|
| 1027 |
+
h = gf_rem(h, g, p, K)
|
| 1028 |
+
n -= 1
|
| 1029 |
+
|
| 1030 |
+
n >>= 1
|
| 1031 |
+
|
| 1032 |
+
if not n:
|
| 1033 |
+
break
|
| 1034 |
+
|
| 1035 |
+
f = gf_sqr(f, p, K)
|
| 1036 |
+
f = gf_rem(f, g, p, K)
|
| 1037 |
+
|
| 1038 |
+
return h
|
| 1039 |
+
|
| 1040 |
+
|
| 1041 |
+
def gf_gcd(f, g, p, K):
|
| 1042 |
+
"""
|
| 1043 |
+
Euclidean Algorithm in ``GF(p)[x]``.
|
| 1044 |
+
|
| 1045 |
+
Examples
|
| 1046 |
+
========
|
| 1047 |
+
|
| 1048 |
+
>>> from sympy.polys.domains import ZZ
|
| 1049 |
+
>>> from sympy.polys.galoistools import gf_gcd
|
| 1050 |
+
|
| 1051 |
+
>>> gf_gcd(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ)
|
| 1052 |
+
[1, 3]
|
| 1053 |
+
|
| 1054 |
+
"""
|
| 1055 |
+
while g:
|
| 1056 |
+
f, g = g, gf_rem(f, g, p, K)
|
| 1057 |
+
|
| 1058 |
+
return gf_monic(f, p, K)[1]
|
| 1059 |
+
|
| 1060 |
+
|
| 1061 |
+
def gf_lcm(f, g, p, K):
|
| 1062 |
+
"""
|
| 1063 |
+
Compute polynomial LCM in ``GF(p)[x]``.
|
| 1064 |
+
|
| 1065 |
+
Examples
|
| 1066 |
+
========
|
| 1067 |
+
|
| 1068 |
+
>>> from sympy.polys.domains import ZZ
|
| 1069 |
+
>>> from sympy.polys.galoistools import gf_lcm
|
| 1070 |
+
|
| 1071 |
+
>>> gf_lcm(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ)
|
| 1072 |
+
[1, 2, 0, 4]
|
| 1073 |
+
|
| 1074 |
+
"""
|
| 1075 |
+
if not f or not g:
|
| 1076 |
+
return []
|
| 1077 |
+
|
| 1078 |
+
h = gf_quo(gf_mul(f, g, p, K),
|
| 1079 |
+
gf_gcd(f, g, p, K), p, K)
|
| 1080 |
+
|
| 1081 |
+
return gf_monic(h, p, K)[1]
|
| 1082 |
+
|
| 1083 |
+
|
| 1084 |
+
def gf_cofactors(f, g, p, K):
|
| 1085 |
+
"""
|
| 1086 |
+
Compute polynomial GCD and cofactors in ``GF(p)[x]``.
|
| 1087 |
+
|
| 1088 |
+
Examples
|
| 1089 |
+
========
|
| 1090 |
+
|
| 1091 |
+
>>> from sympy.polys.domains import ZZ
|
| 1092 |
+
>>> from sympy.polys.galoistools import gf_cofactors
|
| 1093 |
+
|
| 1094 |
+
>>> gf_cofactors(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ)
|
| 1095 |
+
([1, 3], [3, 3], [2, 1])
|
| 1096 |
+
|
| 1097 |
+
"""
|
| 1098 |
+
if not f and not g:
|
| 1099 |
+
return ([], [], [])
|
| 1100 |
+
|
| 1101 |
+
h = gf_gcd(f, g, p, K)
|
| 1102 |
+
|
| 1103 |
+
return (h, gf_quo(f, h, p, K),
|
| 1104 |
+
gf_quo(g, h, p, K))
|
| 1105 |
+
|
| 1106 |
+
|
| 1107 |
+
def gf_gcdex(f, g, p, K):
|
| 1108 |
+
"""
|
| 1109 |
+
Extended Euclidean Algorithm in ``GF(p)[x]``.
|
| 1110 |
+
|
| 1111 |
+
Given polynomials ``f`` and ``g`` in ``GF(p)[x]``, computes polynomials
|
| 1112 |
+
``s``, ``t`` and ``h``, such that ``h = gcd(f, g)`` and ``s*f + t*g = h``.
|
| 1113 |
+
The typical application of EEA is solving polynomial diophantine equations.
|
| 1114 |
+
|
| 1115 |
+
Consider polynomials ``f = (x + 7) (x + 1)``, ``g = (x + 7) (x**2 + 1)``
|
| 1116 |
+
in ``GF(11)[x]``. Application of Extended Euclidean Algorithm gives::
|
| 1117 |
+
|
| 1118 |
+
>>> from sympy.polys.domains import ZZ
|
| 1119 |
+
>>> from sympy.polys.galoistools import gf_gcdex, gf_mul, gf_add
|
| 1120 |
+
|
| 1121 |
+
>>> s, t, g = gf_gcdex(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ)
|
| 1122 |
+
>>> s, t, g
|
| 1123 |
+
([5, 6], [6], [1, 7])
|
| 1124 |
+
|
| 1125 |
+
As result we obtained polynomials ``s = 5*x + 6`` and ``t = 6``, and
|
| 1126 |
+
additionally ``gcd(f, g) = x + 7``. This is correct because::
|
| 1127 |
+
|
| 1128 |
+
>>> S = gf_mul(s, ZZ.map([1, 8, 7]), 11, ZZ)
|
| 1129 |
+
>>> T = gf_mul(t, ZZ.map([1, 7, 1, 7]), 11, ZZ)
|
| 1130 |
+
|
| 1131 |
+
>>> gf_add(S, T, 11, ZZ) == [1, 7]
|
| 1132 |
+
True
|
| 1133 |
+
|
| 1134 |
+
References
|
| 1135 |
+
==========
|
| 1136 |
+
|
| 1137 |
+
.. [1] [Gathen99]_
|
| 1138 |
+
|
| 1139 |
+
"""
|
| 1140 |
+
if not (f or g):
|
| 1141 |
+
return [K.one], [], []
|
| 1142 |
+
|
| 1143 |
+
p0, r0 = gf_monic(f, p, K)
|
| 1144 |
+
p1, r1 = gf_monic(g, p, K)
|
| 1145 |
+
|
| 1146 |
+
if not f:
|
| 1147 |
+
return [], [K.invert(p1, p)], r1
|
| 1148 |
+
if not g:
|
| 1149 |
+
return [K.invert(p0, p)], [], r0
|
| 1150 |
+
|
| 1151 |
+
s0, s1 = [K.invert(p0, p)], []
|
| 1152 |
+
t0, t1 = [], [K.invert(p1, p)]
|
| 1153 |
+
|
| 1154 |
+
while True:
|
| 1155 |
+
Q, R = gf_div(r0, r1, p, K)
|
| 1156 |
+
|
| 1157 |
+
if not R:
|
| 1158 |
+
break
|
| 1159 |
+
|
| 1160 |
+
(lc, r1), r0 = gf_monic(R, p, K), r1
|
| 1161 |
+
|
| 1162 |
+
inv = K.invert(lc, p)
|
| 1163 |
+
|
| 1164 |
+
s = gf_sub_mul(s0, s1, Q, p, K)
|
| 1165 |
+
t = gf_sub_mul(t0, t1, Q, p, K)
|
| 1166 |
+
|
| 1167 |
+
s1, s0 = gf_mul_ground(s, inv, p, K), s1
|
| 1168 |
+
t1, t0 = gf_mul_ground(t, inv, p, K), t1
|
| 1169 |
+
|
| 1170 |
+
return s1, t1, r1
|
| 1171 |
+
|
| 1172 |
+
|
| 1173 |
+
def gf_monic(f, p, K):
|
| 1174 |
+
"""
|
| 1175 |
+
Compute LC and a monic polynomial in ``GF(p)[x]``.
|
| 1176 |
+
|
| 1177 |
+
Examples
|
| 1178 |
+
========
|
| 1179 |
+
|
| 1180 |
+
>>> from sympy.polys.domains import ZZ
|
| 1181 |
+
>>> from sympy.polys.galoistools import gf_monic
|
| 1182 |
+
|
| 1183 |
+
>>> gf_monic(ZZ.map([3, 2, 4]), 5, ZZ)
|
| 1184 |
+
(3, [1, 4, 3])
|
| 1185 |
+
|
| 1186 |
+
"""
|
| 1187 |
+
if not f:
|
| 1188 |
+
return K.zero, []
|
| 1189 |
+
else:
|
| 1190 |
+
lc = f[0]
|
| 1191 |
+
|
| 1192 |
+
if K.is_one(lc):
|
| 1193 |
+
return lc, list(f)
|
| 1194 |
+
else:
|
| 1195 |
+
return lc, gf_quo_ground(f, lc, p, K)
|
| 1196 |
+
|
| 1197 |
+
|
| 1198 |
+
def gf_diff(f, p, K):
|
| 1199 |
+
"""
|
| 1200 |
+
Differentiate polynomial in ``GF(p)[x]``.
|
| 1201 |
+
|
| 1202 |
+
Examples
|
| 1203 |
+
========
|
| 1204 |
+
|
| 1205 |
+
>>> from sympy.polys.domains import ZZ
|
| 1206 |
+
>>> from sympy.polys.galoistools import gf_diff
|
| 1207 |
+
|
| 1208 |
+
>>> gf_diff([3, 2, 4], 5, ZZ)
|
| 1209 |
+
[1, 2]
|
| 1210 |
+
|
| 1211 |
+
"""
|
| 1212 |
+
df = gf_degree(f)
|
| 1213 |
+
|
| 1214 |
+
h, n = [K.zero]*df, df
|
| 1215 |
+
|
| 1216 |
+
for coeff in f[:-1]:
|
| 1217 |
+
coeff *= K(n)
|
| 1218 |
+
coeff %= p
|
| 1219 |
+
|
| 1220 |
+
if coeff:
|
| 1221 |
+
h[df - n] = coeff
|
| 1222 |
+
|
| 1223 |
+
n -= 1
|
| 1224 |
+
|
| 1225 |
+
return gf_strip(h)
|
| 1226 |
+
|
| 1227 |
+
|
| 1228 |
+
def gf_eval(f, a, p, K):
|
| 1229 |
+
"""
|
| 1230 |
+
Evaluate ``f(a)`` in ``GF(p)`` using Horner scheme.
|
| 1231 |
+
|
| 1232 |
+
Examples
|
| 1233 |
+
========
|
| 1234 |
+
|
| 1235 |
+
>>> from sympy.polys.domains import ZZ
|
| 1236 |
+
>>> from sympy.polys.galoistools import gf_eval
|
| 1237 |
+
|
| 1238 |
+
>>> gf_eval([3, 2, 4], 2, 5, ZZ)
|
| 1239 |
+
0
|
| 1240 |
+
|
| 1241 |
+
"""
|
| 1242 |
+
result = K.zero
|
| 1243 |
+
|
| 1244 |
+
for c in f:
|
| 1245 |
+
result *= a
|
| 1246 |
+
result += c
|
| 1247 |
+
result %= p
|
| 1248 |
+
|
| 1249 |
+
return result
|
| 1250 |
+
|
| 1251 |
+
|
| 1252 |
+
def gf_multi_eval(f, A, p, K):
|
| 1253 |
+
"""
|
| 1254 |
+
Evaluate ``f(a)`` for ``a`` in ``[a_1, ..., a_n]``.
|
| 1255 |
+
|
| 1256 |
+
Examples
|
| 1257 |
+
========
|
| 1258 |
+
|
| 1259 |
+
>>> from sympy.polys.domains import ZZ
|
| 1260 |
+
>>> from sympy.polys.galoistools import gf_multi_eval
|
| 1261 |
+
|
| 1262 |
+
>>> gf_multi_eval([3, 2, 4], [0, 1, 2, 3, 4], 5, ZZ)
|
| 1263 |
+
[4, 4, 0, 2, 0]
|
| 1264 |
+
|
| 1265 |
+
"""
|
| 1266 |
+
return [ gf_eval(f, a, p, K) for a in A ]
|
| 1267 |
+
|
| 1268 |
+
|
| 1269 |
+
def gf_compose(f, g, p, K):
|
| 1270 |
+
"""
|
| 1271 |
+
Compute polynomial composition ``f(g)`` in ``GF(p)[x]``.
|
| 1272 |
+
|
| 1273 |
+
Examples
|
| 1274 |
+
========
|
| 1275 |
+
|
| 1276 |
+
>>> from sympy.polys.domains import ZZ
|
| 1277 |
+
>>> from sympy.polys.galoistools import gf_compose
|
| 1278 |
+
|
| 1279 |
+
>>> gf_compose([3, 2, 4], [2, 2, 2], 5, ZZ)
|
| 1280 |
+
[2, 4, 0, 3, 0]
|
| 1281 |
+
|
| 1282 |
+
"""
|
| 1283 |
+
if len(g) <= 1:
|
| 1284 |
+
return gf_strip([gf_eval(f, gf_LC(g, K), p, K)])
|
| 1285 |
+
|
| 1286 |
+
if not f:
|
| 1287 |
+
return []
|
| 1288 |
+
|
| 1289 |
+
h = [f[0]]
|
| 1290 |
+
|
| 1291 |
+
for c in f[1:]:
|
| 1292 |
+
h = gf_mul(h, g, p, K)
|
| 1293 |
+
h = gf_add_ground(h, c, p, K)
|
| 1294 |
+
|
| 1295 |
+
return h
|
| 1296 |
+
|
| 1297 |
+
|
| 1298 |
+
def gf_compose_mod(g, h, f, p, K):
|
| 1299 |
+
"""
|
| 1300 |
+
Compute polynomial composition ``g(h)`` in ``GF(p)[x]/(f)``.
|
| 1301 |
+
|
| 1302 |
+
Examples
|
| 1303 |
+
========
|
| 1304 |
+
|
| 1305 |
+
>>> from sympy.polys.domains import ZZ
|
| 1306 |
+
>>> from sympy.polys.galoistools import gf_compose_mod
|
| 1307 |
+
|
| 1308 |
+
>>> gf_compose_mod(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 2]), ZZ.map([4, 3]), 5, ZZ)
|
| 1309 |
+
[4]
|
| 1310 |
+
|
| 1311 |
+
"""
|
| 1312 |
+
if not g:
|
| 1313 |
+
return []
|
| 1314 |
+
|
| 1315 |
+
comp = [g[0]]
|
| 1316 |
+
|
| 1317 |
+
for a in g[1:]:
|
| 1318 |
+
comp = gf_mul(comp, h, p, K)
|
| 1319 |
+
comp = gf_add_ground(comp, a, p, K)
|
| 1320 |
+
comp = gf_rem(comp, f, p, K)
|
| 1321 |
+
|
| 1322 |
+
return comp
|
| 1323 |
+
|
| 1324 |
+
|
| 1325 |
+
def gf_trace_map(a, b, c, n, f, p, K):
|
| 1326 |
+
"""
|
| 1327 |
+
Compute polynomial trace map in ``GF(p)[x]/(f)``.
|
| 1328 |
+
|
| 1329 |
+
Given a polynomial ``f`` in ``GF(p)[x]``, polynomials ``a``, ``b``,
|
| 1330 |
+
``c`` in the quotient ring ``GF(p)[x]/(f)`` such that ``b = c**t
|
| 1331 |
+
(mod f)`` for some positive power ``t`` of ``p``, and a positive
|
| 1332 |
+
integer ``n``, returns a mapping::
|
| 1333 |
+
|
| 1334 |
+
a -> a**t**n, a + a**t + a**t**2 + ... + a**t**n (mod f)
|
| 1335 |
+
|
| 1336 |
+
In factorization context, ``b = x**p mod f`` and ``c = x mod f``.
|
| 1337 |
+
This way we can efficiently compute trace polynomials in equal
|
| 1338 |
+
degree factorization routine, much faster than with other methods,
|
| 1339 |
+
like iterated Frobenius algorithm, for large degrees.
|
| 1340 |
+
|
| 1341 |
+
Examples
|
| 1342 |
+
========
|
| 1343 |
+
|
| 1344 |
+
>>> from sympy.polys.domains import ZZ
|
| 1345 |
+
>>> from sympy.polys.galoistools import gf_trace_map
|
| 1346 |
+
|
| 1347 |
+
>>> gf_trace_map([1, 2], [4, 4], [1, 1], 4, [3, 2, 4], 5, ZZ)
|
| 1348 |
+
([1, 3], [1, 3])
|
| 1349 |
+
|
| 1350 |
+
References
|
| 1351 |
+
==========
|
| 1352 |
+
|
| 1353 |
+
.. [1] [Gathen92]_
|
| 1354 |
+
|
| 1355 |
+
"""
|
| 1356 |
+
u = gf_compose_mod(a, b, f, p, K)
|
| 1357 |
+
v = b
|
| 1358 |
+
|
| 1359 |
+
if n & 1:
|
| 1360 |
+
U = gf_add(a, u, p, K)
|
| 1361 |
+
V = b
|
| 1362 |
+
else:
|
| 1363 |
+
U = a
|
| 1364 |
+
V = c
|
| 1365 |
+
|
| 1366 |
+
n >>= 1
|
| 1367 |
+
|
| 1368 |
+
while n:
|
| 1369 |
+
u = gf_add(u, gf_compose_mod(u, v, f, p, K), p, K)
|
| 1370 |
+
v = gf_compose_mod(v, v, f, p, K)
|
| 1371 |
+
|
| 1372 |
+
if n & 1:
|
| 1373 |
+
U = gf_add(U, gf_compose_mod(u, V, f, p, K), p, K)
|
| 1374 |
+
V = gf_compose_mod(v, V, f, p, K)
|
| 1375 |
+
|
| 1376 |
+
n >>= 1
|
| 1377 |
+
|
| 1378 |
+
return gf_compose_mod(a, V, f, p, K), U
|
| 1379 |
+
|
| 1380 |
+
def _gf_trace_map(f, n, g, b, p, K):
|
| 1381 |
+
"""
|
| 1382 |
+
utility for ``gf_edf_shoup``
|
| 1383 |
+
"""
|
| 1384 |
+
f = gf_rem(f, g, p, K)
|
| 1385 |
+
h = f
|
| 1386 |
+
r = f
|
| 1387 |
+
for i in range(1, n):
|
| 1388 |
+
h = gf_frobenius_map(h, g, b, p, K)
|
| 1389 |
+
r = gf_add(r, h, p, K)
|
| 1390 |
+
r = gf_rem(r, g, p, K)
|
| 1391 |
+
return r
|
| 1392 |
+
|
| 1393 |
+
|
| 1394 |
+
def gf_random(n, p, K):
|
| 1395 |
+
"""
|
| 1396 |
+
Generate a random polynomial in ``GF(p)[x]`` of degree ``n``.
|
| 1397 |
+
|
| 1398 |
+
Examples
|
| 1399 |
+
========
|
| 1400 |
+
|
| 1401 |
+
>>> from sympy.polys.domains import ZZ
|
| 1402 |
+
>>> from sympy.polys.galoistools import gf_random
|
| 1403 |
+
>>> gf_random(10, 5, ZZ) #doctest: +SKIP
|
| 1404 |
+
[1, 2, 3, 2, 1, 1, 1, 2, 0, 4, 2]
|
| 1405 |
+
|
| 1406 |
+
"""
|
| 1407 |
+
pi = int(p)
|
| 1408 |
+
return [K.one] + [ K(int(uniform(0, pi))) for i in range(0, n) ]
|
| 1409 |
+
|
| 1410 |
+
|
| 1411 |
+
def gf_irreducible(n, p, K):
|
| 1412 |
+
"""
|
| 1413 |
+
Generate random irreducible polynomial of degree ``n`` in ``GF(p)[x]``.
|
| 1414 |
+
|
| 1415 |
+
Examples
|
| 1416 |
+
========
|
| 1417 |
+
|
| 1418 |
+
>>> from sympy.polys.domains import ZZ
|
| 1419 |
+
>>> from sympy.polys.galoistools import gf_irreducible
|
| 1420 |
+
>>> gf_irreducible(10, 5, ZZ) #doctest: +SKIP
|
| 1421 |
+
[1, 4, 2, 2, 3, 2, 4, 1, 4, 0, 4]
|
| 1422 |
+
|
| 1423 |
+
"""
|
| 1424 |
+
while True:
|
| 1425 |
+
f = gf_random(n, p, K)
|
| 1426 |
+
if gf_irreducible_p(f, p, K):
|
| 1427 |
+
return f
|
| 1428 |
+
|
| 1429 |
+
|
| 1430 |
+
def gf_irred_p_ben_or(f, p, K):
|
| 1431 |
+
"""
|
| 1432 |
+
Ben-Or's polynomial irreducibility test over finite fields.
|
| 1433 |
+
|
| 1434 |
+
Examples
|
| 1435 |
+
========
|
| 1436 |
+
|
| 1437 |
+
>>> from sympy.polys.domains import ZZ
|
| 1438 |
+
>>> from sympy.polys.galoistools import gf_irred_p_ben_or
|
| 1439 |
+
|
| 1440 |
+
>>> gf_irred_p_ben_or(ZZ.map([1, 4, 2, 2, 3, 2, 4, 1, 4, 0, 4]), 5, ZZ)
|
| 1441 |
+
True
|
| 1442 |
+
>>> gf_irred_p_ben_or(ZZ.map([3, 2, 4]), 5, ZZ)
|
| 1443 |
+
False
|
| 1444 |
+
|
| 1445 |
+
"""
|
| 1446 |
+
n = gf_degree(f)
|
| 1447 |
+
|
| 1448 |
+
if n <= 1:
|
| 1449 |
+
return True
|
| 1450 |
+
|
| 1451 |
+
_, f = gf_monic(f, p, K)
|
| 1452 |
+
if n < 5:
|
| 1453 |
+
H = h = gf_pow_mod([K.one, K.zero], p, f, p, K)
|
| 1454 |
+
|
| 1455 |
+
for i in range(0, n//2):
|
| 1456 |
+
g = gf_sub(h, [K.one, K.zero], p, K)
|
| 1457 |
+
|
| 1458 |
+
if gf_gcd(f, g, p, K) == [K.one]:
|
| 1459 |
+
h = gf_compose_mod(h, H, f, p, K)
|
| 1460 |
+
else:
|
| 1461 |
+
return False
|
| 1462 |
+
else:
|
| 1463 |
+
b = gf_frobenius_monomial_base(f, p, K)
|
| 1464 |
+
H = h = gf_frobenius_map([K.one, K.zero], f, b, p, K)
|
| 1465 |
+
for i in range(0, n//2):
|
| 1466 |
+
g = gf_sub(h, [K.one, K.zero], p, K)
|
| 1467 |
+
if gf_gcd(f, g, p, K) == [K.one]:
|
| 1468 |
+
h = gf_frobenius_map(h, f, b, p, K)
|
| 1469 |
+
else:
|
| 1470 |
+
return False
|
| 1471 |
+
|
| 1472 |
+
return True
|
| 1473 |
+
|
| 1474 |
+
|
| 1475 |
+
def gf_irred_p_rabin(f, p, K):
|
| 1476 |
+
"""
|
| 1477 |
+
Rabin's polynomial irreducibility test over finite fields.
|
| 1478 |
+
|
| 1479 |
+
Examples
|
| 1480 |
+
========
|
| 1481 |
+
|
| 1482 |
+
>>> from sympy.polys.domains import ZZ
|
| 1483 |
+
>>> from sympy.polys.galoistools import gf_irred_p_rabin
|
| 1484 |
+
|
| 1485 |
+
>>> gf_irred_p_rabin(ZZ.map([1, 4, 2, 2, 3, 2, 4, 1, 4, 0, 4]), 5, ZZ)
|
| 1486 |
+
True
|
| 1487 |
+
>>> gf_irred_p_rabin(ZZ.map([3, 2, 4]), 5, ZZ)
|
| 1488 |
+
False
|
| 1489 |
+
|
| 1490 |
+
"""
|
| 1491 |
+
n = gf_degree(f)
|
| 1492 |
+
|
| 1493 |
+
if n <= 1:
|
| 1494 |
+
return True
|
| 1495 |
+
|
| 1496 |
+
_, f = gf_monic(f, p, K)
|
| 1497 |
+
|
| 1498 |
+
x = [K.one, K.zero]
|
| 1499 |
+
|
| 1500 |
+
from sympy.ntheory import factorint
|
| 1501 |
+
|
| 1502 |
+
indices = { n//d for d in factorint(n) }
|
| 1503 |
+
|
| 1504 |
+
b = gf_frobenius_monomial_base(f, p, K)
|
| 1505 |
+
h = b[1]
|
| 1506 |
+
|
| 1507 |
+
for i in range(1, n):
|
| 1508 |
+
if i in indices:
|
| 1509 |
+
g = gf_sub(h, x, p, K)
|
| 1510 |
+
|
| 1511 |
+
if gf_gcd(f, g, p, K) != [K.one]:
|
| 1512 |
+
return False
|
| 1513 |
+
|
| 1514 |
+
h = gf_frobenius_map(h, f, b, p, K)
|
| 1515 |
+
|
| 1516 |
+
return h == x
|
| 1517 |
+
|
| 1518 |
+
_irred_methods = {
|
| 1519 |
+
'ben-or': gf_irred_p_ben_or,
|
| 1520 |
+
'rabin': gf_irred_p_rabin,
|
| 1521 |
+
}
|
| 1522 |
+
|
| 1523 |
+
|
| 1524 |
+
def gf_irreducible_p(f, p, K):
|
| 1525 |
+
"""
|
| 1526 |
+
Test irreducibility of a polynomial ``f`` in ``GF(p)[x]``.
|
| 1527 |
+
|
| 1528 |
+
Examples
|
| 1529 |
+
========
|
| 1530 |
+
|
| 1531 |
+
>>> from sympy.polys.domains import ZZ
|
| 1532 |
+
>>> from sympy.polys.galoistools import gf_irreducible_p
|
| 1533 |
+
|
| 1534 |
+
>>> gf_irreducible_p(ZZ.map([1, 4, 2, 2, 3, 2, 4, 1, 4, 0, 4]), 5, ZZ)
|
| 1535 |
+
True
|
| 1536 |
+
>>> gf_irreducible_p(ZZ.map([3, 2, 4]), 5, ZZ)
|
| 1537 |
+
False
|
| 1538 |
+
|
| 1539 |
+
"""
|
| 1540 |
+
method = query('GF_IRRED_METHOD')
|
| 1541 |
+
|
| 1542 |
+
if method is not None:
|
| 1543 |
+
irred = _irred_methods[method](f, p, K)
|
| 1544 |
+
else:
|
| 1545 |
+
irred = gf_irred_p_rabin(f, p, K)
|
| 1546 |
+
|
| 1547 |
+
return irred
|
| 1548 |
+
|
| 1549 |
+
|
| 1550 |
+
def gf_sqf_p(f, p, K):
|
| 1551 |
+
"""
|
| 1552 |
+
Return ``True`` if ``f`` is square-free in ``GF(p)[x]``.
|
| 1553 |
+
|
| 1554 |
+
Examples
|
| 1555 |
+
========
|
| 1556 |
+
|
| 1557 |
+
>>> from sympy.polys.domains import ZZ
|
| 1558 |
+
>>> from sympy.polys.galoistools import gf_sqf_p
|
| 1559 |
+
|
| 1560 |
+
>>> gf_sqf_p(ZZ.map([3, 2, 4]), 5, ZZ)
|
| 1561 |
+
True
|
| 1562 |
+
>>> gf_sqf_p(ZZ.map([2, 4, 4, 2, 2, 1, 4]), 5, ZZ)
|
| 1563 |
+
False
|
| 1564 |
+
|
| 1565 |
+
"""
|
| 1566 |
+
_, f = gf_monic(f, p, K)
|
| 1567 |
+
|
| 1568 |
+
if not f:
|
| 1569 |
+
return True
|
| 1570 |
+
else:
|
| 1571 |
+
return gf_gcd(f, gf_diff(f, p, K), p, K) == [K.one]
|
| 1572 |
+
|
| 1573 |
+
|
| 1574 |
+
def gf_sqf_part(f, p, K):
|
| 1575 |
+
"""
|
| 1576 |
+
Return square-free part of a ``GF(p)[x]`` polynomial.
|
| 1577 |
+
|
| 1578 |
+
Examples
|
| 1579 |
+
========
|
| 1580 |
+
|
| 1581 |
+
>>> from sympy.polys.domains import ZZ
|
| 1582 |
+
>>> from sympy.polys.galoistools import gf_sqf_part
|
| 1583 |
+
|
| 1584 |
+
>>> gf_sqf_part(ZZ.map([1, 1, 3, 0, 1, 0, 2, 2, 1]), 5, ZZ)
|
| 1585 |
+
[1, 4, 3]
|
| 1586 |
+
|
| 1587 |
+
"""
|
| 1588 |
+
_, sqf = gf_sqf_list(f, p, K)
|
| 1589 |
+
|
| 1590 |
+
g = [K.one]
|
| 1591 |
+
|
| 1592 |
+
for f, _ in sqf:
|
| 1593 |
+
g = gf_mul(g, f, p, K)
|
| 1594 |
+
|
| 1595 |
+
return g
|
| 1596 |
+
|
| 1597 |
+
|
| 1598 |
+
def gf_sqf_list(f, p, K, all=False):
|
| 1599 |
+
"""
|
| 1600 |
+
Return the square-free decomposition of a ``GF(p)[x]`` polynomial.
|
| 1601 |
+
|
| 1602 |
+
Given a polynomial ``f`` in ``GF(p)[x]``, returns the leading coefficient
|
| 1603 |
+
of ``f`` and a square-free decomposition ``f_1**e_1 f_2**e_2 ... f_k**e_k``
|
| 1604 |
+
such that all ``f_i`` are monic polynomials and ``(f_i, f_j)`` for ``i != j``
|
| 1605 |
+
are co-prime and ``e_1 ... e_k`` are given in increasing order. All trivial
|
| 1606 |
+
terms (i.e. ``f_i = 1``) are not included in the output.
|
| 1607 |
+
|
| 1608 |
+
Consider polynomial ``f = x**11 + 1`` over ``GF(11)[x]``::
|
| 1609 |
+
|
| 1610 |
+
>>> from sympy.polys.domains import ZZ
|
| 1611 |
+
|
| 1612 |
+
>>> from sympy.polys.galoistools import (
|
| 1613 |
+
... gf_from_dict, gf_diff, gf_sqf_list, gf_pow,
|
| 1614 |
+
... )
|
| 1615 |
+
... # doctest: +NORMALIZE_WHITESPACE
|
| 1616 |
+
|
| 1617 |
+
>>> f = gf_from_dict({11: ZZ(1), 0: ZZ(1)}, 11, ZZ)
|
| 1618 |
+
|
| 1619 |
+
Note that ``f'(x) = 0``::
|
| 1620 |
+
|
| 1621 |
+
>>> gf_diff(f, 11, ZZ)
|
| 1622 |
+
[]
|
| 1623 |
+
|
| 1624 |
+
This phenomenon does not happen in characteristic zero. However we can
|
| 1625 |
+
still compute square-free decomposition of ``f`` using ``gf_sqf()``::
|
| 1626 |
+
|
| 1627 |
+
>>> gf_sqf_list(f, 11, ZZ)
|
| 1628 |
+
(1, [([1, 1], 11)])
|
| 1629 |
+
|
| 1630 |
+
We obtained factorization ``f = (x + 1)**11``. This is correct because::
|
| 1631 |
+
|
| 1632 |
+
>>> gf_pow([1, 1], 11, 11, ZZ) == f
|
| 1633 |
+
True
|
| 1634 |
+
|
| 1635 |
+
References
|
| 1636 |
+
==========
|
| 1637 |
+
|
| 1638 |
+
.. [1] [Geddes92]_
|
| 1639 |
+
|
| 1640 |
+
"""
|
| 1641 |
+
n, sqf, factors, r = 1, False, [], int(p)
|
| 1642 |
+
|
| 1643 |
+
lc, f = gf_monic(f, p, K)
|
| 1644 |
+
|
| 1645 |
+
if gf_degree(f) < 1:
|
| 1646 |
+
return lc, []
|
| 1647 |
+
|
| 1648 |
+
while True:
|
| 1649 |
+
F = gf_diff(f, p, K)
|
| 1650 |
+
|
| 1651 |
+
if F != []:
|
| 1652 |
+
g = gf_gcd(f, F, p, K)
|
| 1653 |
+
h = gf_quo(f, g, p, K)
|
| 1654 |
+
|
| 1655 |
+
i = 1
|
| 1656 |
+
|
| 1657 |
+
while h != [K.one]:
|
| 1658 |
+
G = gf_gcd(g, h, p, K)
|
| 1659 |
+
H = gf_quo(h, G, p, K)
|
| 1660 |
+
|
| 1661 |
+
if gf_degree(H) > 0:
|
| 1662 |
+
factors.append((H, i*n))
|
| 1663 |
+
|
| 1664 |
+
g, h, i = gf_quo(g, G, p, K), G, i + 1
|
| 1665 |
+
|
| 1666 |
+
if g == [K.one]:
|
| 1667 |
+
sqf = True
|
| 1668 |
+
else:
|
| 1669 |
+
f = g
|
| 1670 |
+
|
| 1671 |
+
if not sqf:
|
| 1672 |
+
d = gf_degree(f) // r
|
| 1673 |
+
|
| 1674 |
+
for i in range(0, d + 1):
|
| 1675 |
+
f[i] = f[i*r]
|
| 1676 |
+
|
| 1677 |
+
f, n = f[:d + 1], n*r
|
| 1678 |
+
else:
|
| 1679 |
+
break
|
| 1680 |
+
|
| 1681 |
+
if all:
|
| 1682 |
+
raise ValueError("'all=True' is not supported yet")
|
| 1683 |
+
|
| 1684 |
+
return lc, factors
|
| 1685 |
+
|
| 1686 |
+
|
| 1687 |
+
def gf_Qmatrix(f, p, K):
|
| 1688 |
+
"""
|
| 1689 |
+
Calculate Berlekamp's ``Q`` matrix.
|
| 1690 |
+
|
| 1691 |
+
Examples
|
| 1692 |
+
========
|
| 1693 |
+
|
| 1694 |
+
>>> from sympy.polys.domains import ZZ
|
| 1695 |
+
>>> from sympy.polys.galoistools import gf_Qmatrix
|
| 1696 |
+
|
| 1697 |
+
>>> gf_Qmatrix([3, 2, 4], 5, ZZ)
|
| 1698 |
+
[[1, 0],
|
| 1699 |
+
[3, 4]]
|
| 1700 |
+
|
| 1701 |
+
>>> gf_Qmatrix([1, 0, 0, 0, 1], 5, ZZ)
|
| 1702 |
+
[[1, 0, 0, 0],
|
| 1703 |
+
[0, 4, 0, 0],
|
| 1704 |
+
[0, 0, 1, 0],
|
| 1705 |
+
[0, 0, 0, 4]]
|
| 1706 |
+
|
| 1707 |
+
"""
|
| 1708 |
+
n, r = gf_degree(f), int(p)
|
| 1709 |
+
|
| 1710 |
+
q = [K.one] + [K.zero]*(n - 1)
|
| 1711 |
+
Q = [list(q)] + [[]]*(n - 1)
|
| 1712 |
+
|
| 1713 |
+
for i in range(1, (n - 1)*r + 1):
|
| 1714 |
+
qq, c = [(-q[-1]*f[-1]) % p], q[-1]
|
| 1715 |
+
|
| 1716 |
+
for j in range(1, n):
|
| 1717 |
+
qq.append((q[j - 1] - c*f[-j - 1]) % p)
|
| 1718 |
+
|
| 1719 |
+
if not (i % r):
|
| 1720 |
+
Q[i//r] = list(qq)
|
| 1721 |
+
|
| 1722 |
+
q = qq
|
| 1723 |
+
|
| 1724 |
+
return Q
|
| 1725 |
+
|
| 1726 |
+
|
| 1727 |
+
def gf_Qbasis(Q, p, K):
|
| 1728 |
+
"""
|
| 1729 |
+
Compute a basis of the kernel of ``Q``.
|
| 1730 |
+
|
| 1731 |
+
Examples
|
| 1732 |
+
========
|
| 1733 |
+
|
| 1734 |
+
>>> from sympy.polys.domains import ZZ
|
| 1735 |
+
>>> from sympy.polys.galoistools import gf_Qmatrix, gf_Qbasis
|
| 1736 |
+
|
| 1737 |
+
>>> gf_Qbasis(gf_Qmatrix([1, 0, 0, 0, 1], 5, ZZ), 5, ZZ)
|
| 1738 |
+
[[1, 0, 0, 0], [0, 0, 1, 0]]
|
| 1739 |
+
|
| 1740 |
+
>>> gf_Qbasis(gf_Qmatrix([3, 2, 4], 5, ZZ), 5, ZZ)
|
| 1741 |
+
[[1, 0]]
|
| 1742 |
+
|
| 1743 |
+
"""
|
| 1744 |
+
Q, n = [ list(q) for q in Q ], len(Q)
|
| 1745 |
+
|
| 1746 |
+
for k in range(0, n):
|
| 1747 |
+
Q[k][k] = (Q[k][k] - K.one) % p
|
| 1748 |
+
|
| 1749 |
+
for k in range(0, n):
|
| 1750 |
+
for i in range(k, n):
|
| 1751 |
+
if Q[k][i]:
|
| 1752 |
+
break
|
| 1753 |
+
else:
|
| 1754 |
+
continue
|
| 1755 |
+
|
| 1756 |
+
inv = K.invert(Q[k][i], p)
|
| 1757 |
+
|
| 1758 |
+
for j in range(0, n):
|
| 1759 |
+
Q[j][i] = (Q[j][i]*inv) % p
|
| 1760 |
+
|
| 1761 |
+
for j in range(0, n):
|
| 1762 |
+
t = Q[j][k]
|
| 1763 |
+
Q[j][k] = Q[j][i]
|
| 1764 |
+
Q[j][i] = t
|
| 1765 |
+
|
| 1766 |
+
for i in range(0, n):
|
| 1767 |
+
if i != k:
|
| 1768 |
+
q = Q[k][i]
|
| 1769 |
+
|
| 1770 |
+
for j in range(0, n):
|
| 1771 |
+
Q[j][i] = (Q[j][i] - Q[j][k]*q) % p
|
| 1772 |
+
|
| 1773 |
+
for i in range(0, n):
|
| 1774 |
+
for j in range(0, n):
|
| 1775 |
+
if i == j:
|
| 1776 |
+
Q[i][j] = (K.one - Q[i][j]) % p
|
| 1777 |
+
else:
|
| 1778 |
+
Q[i][j] = (-Q[i][j]) % p
|
| 1779 |
+
|
| 1780 |
+
basis = []
|
| 1781 |
+
|
| 1782 |
+
for q in Q:
|
| 1783 |
+
if any(q):
|
| 1784 |
+
basis.append(q)
|
| 1785 |
+
|
| 1786 |
+
return basis
|
| 1787 |
+
|
| 1788 |
+
|
| 1789 |
+
def gf_berlekamp(f, p, K):
|
| 1790 |
+
"""
|
| 1791 |
+
Factor a square-free ``f`` in ``GF(p)[x]`` for small ``p``.
|
| 1792 |
+
|
| 1793 |
+
Examples
|
| 1794 |
+
========
|
| 1795 |
+
|
| 1796 |
+
>>> from sympy.polys.domains import ZZ
|
| 1797 |
+
>>> from sympy.polys.galoistools import gf_berlekamp
|
| 1798 |
+
|
| 1799 |
+
>>> gf_berlekamp([1, 0, 0, 0, 1], 5, ZZ)
|
| 1800 |
+
[[1, 0, 2], [1, 0, 3]]
|
| 1801 |
+
|
| 1802 |
+
"""
|
| 1803 |
+
Q = gf_Qmatrix(f, p, K)
|
| 1804 |
+
V = gf_Qbasis(Q, p, K)
|
| 1805 |
+
|
| 1806 |
+
for i, v in enumerate(V):
|
| 1807 |
+
V[i] = gf_strip(list(reversed(v)))
|
| 1808 |
+
|
| 1809 |
+
factors = [f]
|
| 1810 |
+
|
| 1811 |
+
for k in range(1, len(V)):
|
| 1812 |
+
for f in list(factors):
|
| 1813 |
+
s = K.zero
|
| 1814 |
+
|
| 1815 |
+
while s < p:
|
| 1816 |
+
g = gf_sub_ground(V[k], s, p, K)
|
| 1817 |
+
h = gf_gcd(f, g, p, K)
|
| 1818 |
+
|
| 1819 |
+
if h != [K.one] and h != f:
|
| 1820 |
+
factors.remove(f)
|
| 1821 |
+
|
| 1822 |
+
f = gf_quo(f, h, p, K)
|
| 1823 |
+
factors.extend([f, h])
|
| 1824 |
+
|
| 1825 |
+
if len(factors) == len(V):
|
| 1826 |
+
return _sort_factors(factors, multiple=False)
|
| 1827 |
+
|
| 1828 |
+
s += K.one
|
| 1829 |
+
|
| 1830 |
+
return _sort_factors(factors, multiple=False)
|
| 1831 |
+
|
| 1832 |
+
|
| 1833 |
+
def gf_ddf_zassenhaus(f, p, K):
|
| 1834 |
+
"""
|
| 1835 |
+
Cantor-Zassenhaus: Deterministic Distinct Degree Factorization
|
| 1836 |
+
|
| 1837 |
+
Given a monic square-free polynomial ``f`` in ``GF(p)[x]``, computes
|
| 1838 |
+
partial distinct degree factorization ``f_1 ... f_d`` of ``f`` where
|
| 1839 |
+
``deg(f_i) != deg(f_j)`` for ``i != j``. The result is returned as a
|
| 1840 |
+
list of pairs ``(f_i, e_i)`` where ``deg(f_i) > 0`` and ``e_i > 0``
|
| 1841 |
+
is an argument to the equal degree factorization routine.
|
| 1842 |
+
|
| 1843 |
+
Consider the polynomial ``x**15 - 1`` in ``GF(11)[x]``::
|
| 1844 |
+
|
| 1845 |
+
>>> from sympy.polys.domains import ZZ
|
| 1846 |
+
>>> from sympy.polys.galoistools import gf_from_dict
|
| 1847 |
+
|
| 1848 |
+
>>> f = gf_from_dict({15: ZZ(1), 0: ZZ(-1)}, 11, ZZ)
|
| 1849 |
+
|
| 1850 |
+
Distinct degree factorization gives::
|
| 1851 |
+
|
| 1852 |
+
>>> from sympy.polys.galoistools import gf_ddf_zassenhaus
|
| 1853 |
+
|
| 1854 |
+
>>> gf_ddf_zassenhaus(f, 11, ZZ)
|
| 1855 |
+
[([1, 0, 0, 0, 0, 10], 1), ([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 2)]
|
| 1856 |
+
|
| 1857 |
+
which means ``x**15 - 1 = (x**5 - 1) (x**10 + x**5 + 1)``. To obtain
|
| 1858 |
+
factorization into irreducibles, use equal degree factorization
|
| 1859 |
+
procedure (EDF) with each of the factors.
|
| 1860 |
+
|
| 1861 |
+
References
|
| 1862 |
+
==========
|
| 1863 |
+
|
| 1864 |
+
.. [1] [Gathen99]_
|
| 1865 |
+
.. [2] [Geddes92]_
|
| 1866 |
+
|
| 1867 |
+
"""
|
| 1868 |
+
i, g, factors = 1, [K.one, K.zero], []
|
| 1869 |
+
|
| 1870 |
+
b = gf_frobenius_monomial_base(f, p, K)
|
| 1871 |
+
while 2*i <= gf_degree(f):
|
| 1872 |
+
g = gf_frobenius_map(g, f, b, p, K)
|
| 1873 |
+
h = gf_gcd(f, gf_sub(g, [K.one, K.zero], p, K), p, K)
|
| 1874 |
+
|
| 1875 |
+
if h != [K.one]:
|
| 1876 |
+
factors.append((h, i))
|
| 1877 |
+
|
| 1878 |
+
f = gf_quo(f, h, p, K)
|
| 1879 |
+
g = gf_rem(g, f, p, K)
|
| 1880 |
+
b = gf_frobenius_monomial_base(f, p, K)
|
| 1881 |
+
|
| 1882 |
+
i += 1
|
| 1883 |
+
|
| 1884 |
+
if f != [K.one]:
|
| 1885 |
+
return factors + [(f, gf_degree(f))]
|
| 1886 |
+
else:
|
| 1887 |
+
return factors
|
| 1888 |
+
|
| 1889 |
+
|
| 1890 |
+
def gf_edf_zassenhaus(f, n, p, K):
|
| 1891 |
+
"""
|
| 1892 |
+
Cantor-Zassenhaus: Probabilistic Equal Degree Factorization
|
| 1893 |
+
|
| 1894 |
+
Given a monic square-free polynomial ``f`` in ``GF(p)[x]`` and
|
| 1895 |
+
an integer ``n``, such that ``n`` divides ``deg(f)``, returns all
|
| 1896 |
+
irreducible factors ``f_1,...,f_d`` of ``f``, each of degree ``n``.
|
| 1897 |
+
EDF procedure gives complete factorization over Galois fields.
|
| 1898 |
+
|
| 1899 |
+
Consider the square-free polynomial ``f = x**3 + x**2 + x + 1`` in
|
| 1900 |
+
``GF(5)[x]``. Let's compute its irreducible factors of degree one::
|
| 1901 |
+
|
| 1902 |
+
>>> from sympy.polys.domains import ZZ
|
| 1903 |
+
>>> from sympy.polys.galoistools import gf_edf_zassenhaus
|
| 1904 |
+
|
| 1905 |
+
>>> gf_edf_zassenhaus([1,1,1,1], 1, 5, ZZ)
|
| 1906 |
+
[[1, 1], [1, 2], [1, 3]]
|
| 1907 |
+
|
| 1908 |
+
Notes
|
| 1909 |
+
=====
|
| 1910 |
+
|
| 1911 |
+
The case p == 2 is handled by Cohen's Algorithm 3.4.8. The case p odd is
|
| 1912 |
+
as in Geddes Algorithm 8.9 (or Cohen's Algorithm 3.4.6).
|
| 1913 |
+
|
| 1914 |
+
References
|
| 1915 |
+
==========
|
| 1916 |
+
|
| 1917 |
+
.. [1] [Gathen99]_
|
| 1918 |
+
.. [2] [Geddes92]_ Algorithm 8.9
|
| 1919 |
+
.. [3] [Cohen93]_ Algorithm 3.4.8
|
| 1920 |
+
|
| 1921 |
+
"""
|
| 1922 |
+
factors = [f]
|
| 1923 |
+
|
| 1924 |
+
if gf_degree(f) <= n:
|
| 1925 |
+
return factors
|
| 1926 |
+
|
| 1927 |
+
N = gf_degree(f) // n
|
| 1928 |
+
if p != 2:
|
| 1929 |
+
b = gf_frobenius_monomial_base(f, p, K)
|
| 1930 |
+
|
| 1931 |
+
t = [K.one, K.zero]
|
| 1932 |
+
while len(factors) < N:
|
| 1933 |
+
if p == 2:
|
| 1934 |
+
h = r = t
|
| 1935 |
+
|
| 1936 |
+
for i in range(n - 1):
|
| 1937 |
+
r = gf_pow_mod(r, 2, f, p, K)
|
| 1938 |
+
h = gf_add(h, r, p, K)
|
| 1939 |
+
|
| 1940 |
+
g = gf_gcd(f, h, p, K)
|
| 1941 |
+
t += [K.zero, K.zero]
|
| 1942 |
+
else:
|
| 1943 |
+
r = gf_random(2 * n - 1, p, K)
|
| 1944 |
+
h = _gf_pow_pnm1d2(r, n, f, b, p, K)
|
| 1945 |
+
g = gf_gcd(f, gf_sub_ground(h, K.one, p, K), p, K)
|
| 1946 |
+
|
| 1947 |
+
if g != [K.one] and g != f:
|
| 1948 |
+
factors = gf_edf_zassenhaus(g, n, p, K) \
|
| 1949 |
+
+ gf_edf_zassenhaus(gf_quo(f, g, p, K), n, p, K)
|
| 1950 |
+
|
| 1951 |
+
return _sort_factors(factors, multiple=False)
|
| 1952 |
+
|
| 1953 |
+
|
| 1954 |
+
def gf_ddf_shoup(f, p, K):
|
| 1955 |
+
"""
|
| 1956 |
+
Kaltofen-Shoup: Deterministic Distinct Degree Factorization
|
| 1957 |
+
|
| 1958 |
+
Given a monic square-free polynomial ``f`` in ``GF(p)[x]``, computes
|
| 1959 |
+
partial distinct degree factorization ``f_1,...,f_d`` of ``f`` where
|
| 1960 |
+
``deg(f_i) != deg(f_j)`` for ``i != j``. The result is returned as a
|
| 1961 |
+
list of pairs ``(f_i, e_i)`` where ``deg(f_i) > 0`` and ``e_i > 0``
|
| 1962 |
+
is an argument to the equal degree factorization routine.
|
| 1963 |
+
|
| 1964 |
+
This algorithm is an improved version of Zassenhaus algorithm for
|
| 1965 |
+
large ``deg(f)`` and modulus ``p`` (especially for ``deg(f) ~ lg(p)``).
|
| 1966 |
+
|
| 1967 |
+
Examples
|
| 1968 |
+
========
|
| 1969 |
+
|
| 1970 |
+
>>> from sympy.polys.domains import ZZ
|
| 1971 |
+
>>> from sympy.polys.galoistools import gf_ddf_shoup, gf_from_dict
|
| 1972 |
+
|
| 1973 |
+
>>> f = gf_from_dict({6: ZZ(1), 5: ZZ(-1), 4: ZZ(1), 3: ZZ(1), 1: ZZ(-1)}, 3, ZZ)
|
| 1974 |
+
|
| 1975 |
+
>>> gf_ddf_shoup(f, 3, ZZ)
|
| 1976 |
+
[([1, 1, 0], 1), ([1, 1, 0, 1, 2], 2)]
|
| 1977 |
+
|
| 1978 |
+
References
|
| 1979 |
+
==========
|
| 1980 |
+
|
| 1981 |
+
.. [1] [Kaltofen98]_
|
| 1982 |
+
.. [2] [Shoup95]_
|
| 1983 |
+
.. [3] [Gathen92]_
|
| 1984 |
+
|
| 1985 |
+
"""
|
| 1986 |
+
n = gf_degree(f)
|
| 1987 |
+
k = int(_ceil(_sqrt(n//2)))
|
| 1988 |
+
b = gf_frobenius_monomial_base(f, p, K)
|
| 1989 |
+
h = gf_frobenius_map([K.one, K.zero], f, b, p, K)
|
| 1990 |
+
# U[i] = x**(p**i)
|
| 1991 |
+
U = [[K.one, K.zero], h] + [K.zero]*(k - 1)
|
| 1992 |
+
|
| 1993 |
+
for i in range(2, k + 1):
|
| 1994 |
+
U[i] = gf_frobenius_map(U[i-1], f, b, p, K)
|
| 1995 |
+
|
| 1996 |
+
h, U = U[k], U[:k]
|
| 1997 |
+
# V[i] = x**(p**(k*(i+1)))
|
| 1998 |
+
V = [h] + [K.zero]*(k - 1)
|
| 1999 |
+
|
| 2000 |
+
for i in range(1, k):
|
| 2001 |
+
V[i] = gf_compose_mod(V[i - 1], h, f, p, K)
|
| 2002 |
+
|
| 2003 |
+
factors = []
|
| 2004 |
+
|
| 2005 |
+
for i, v in enumerate(V):
|
| 2006 |
+
h, j = [K.one], k - 1
|
| 2007 |
+
|
| 2008 |
+
for u in U:
|
| 2009 |
+
g = gf_sub(v, u, p, K)
|
| 2010 |
+
h = gf_mul(h, g, p, K)
|
| 2011 |
+
h = gf_rem(h, f, p, K)
|
| 2012 |
+
|
| 2013 |
+
g = gf_gcd(f, h, p, K)
|
| 2014 |
+
f = gf_quo(f, g, p, K)
|
| 2015 |
+
|
| 2016 |
+
for u in reversed(U):
|
| 2017 |
+
h = gf_sub(v, u, p, K)
|
| 2018 |
+
F = gf_gcd(g, h, p, K)
|
| 2019 |
+
|
| 2020 |
+
if F != [K.one]:
|
| 2021 |
+
factors.append((F, k*(i + 1) - j))
|
| 2022 |
+
|
| 2023 |
+
g, j = gf_quo(g, F, p, K), j - 1
|
| 2024 |
+
|
| 2025 |
+
if f != [K.one]:
|
| 2026 |
+
factors.append((f, gf_degree(f)))
|
| 2027 |
+
|
| 2028 |
+
return factors
|
| 2029 |
+
|
| 2030 |
+
def gf_edf_shoup(f, n, p, K):
|
| 2031 |
+
"""
|
| 2032 |
+
Gathen-Shoup: Probabilistic Equal Degree Factorization
|
| 2033 |
+
|
| 2034 |
+
Given a monic square-free polynomial ``f`` in ``GF(p)[x]`` and integer
|
| 2035 |
+
``n`` such that ``n`` divides ``deg(f)``, returns all irreducible factors
|
| 2036 |
+
``f_1,...,f_d`` of ``f``, each of degree ``n``. This is a complete
|
| 2037 |
+
factorization over Galois fields.
|
| 2038 |
+
|
| 2039 |
+
This algorithm is an improved version of Zassenhaus algorithm for
|
| 2040 |
+
large ``deg(f)`` and modulus ``p`` (especially for ``deg(f) ~ lg(p)``).
|
| 2041 |
+
|
| 2042 |
+
Examples
|
| 2043 |
+
========
|
| 2044 |
+
|
| 2045 |
+
>>> from sympy.polys.domains import ZZ
|
| 2046 |
+
>>> from sympy.polys.galoistools import gf_edf_shoup
|
| 2047 |
+
|
| 2048 |
+
>>> gf_edf_shoup(ZZ.map([1, 2837, 2277]), 1, 2917, ZZ)
|
| 2049 |
+
[[1, 852], [1, 1985]]
|
| 2050 |
+
|
| 2051 |
+
References
|
| 2052 |
+
==========
|
| 2053 |
+
|
| 2054 |
+
.. [1] [Shoup91]_
|
| 2055 |
+
.. [2] [Gathen92]_
|
| 2056 |
+
|
| 2057 |
+
"""
|
| 2058 |
+
N, q = gf_degree(f), int(p)
|
| 2059 |
+
|
| 2060 |
+
if not N:
|
| 2061 |
+
return []
|
| 2062 |
+
if N <= n:
|
| 2063 |
+
return [f]
|
| 2064 |
+
|
| 2065 |
+
factors, x = [f], [K.one, K.zero]
|
| 2066 |
+
|
| 2067 |
+
r = gf_random(N - 1, p, K)
|
| 2068 |
+
|
| 2069 |
+
if p == 2:
|
| 2070 |
+
h = gf_pow_mod(x, q, f, p, K)
|
| 2071 |
+
H = gf_trace_map(r, h, x, n - 1, f, p, K)[1]
|
| 2072 |
+
h1 = gf_gcd(f, H, p, K)
|
| 2073 |
+
h2 = gf_quo(f, h1, p, K)
|
| 2074 |
+
|
| 2075 |
+
factors = gf_edf_shoup(h1, n, p, K) \
|
| 2076 |
+
+ gf_edf_shoup(h2, n, p, K)
|
| 2077 |
+
else:
|
| 2078 |
+
b = gf_frobenius_monomial_base(f, p, K)
|
| 2079 |
+
H = _gf_trace_map(r, n, f, b, p, K)
|
| 2080 |
+
h = gf_pow_mod(H, (q - 1)//2, f, p, K)
|
| 2081 |
+
|
| 2082 |
+
h1 = gf_gcd(f, h, p, K)
|
| 2083 |
+
h2 = gf_gcd(f, gf_sub_ground(h, K.one, p, K), p, K)
|
| 2084 |
+
h3 = gf_quo(f, gf_mul(h1, h2, p, K), p, K)
|
| 2085 |
+
|
| 2086 |
+
factors = gf_edf_shoup(h1, n, p, K) \
|
| 2087 |
+
+ gf_edf_shoup(h2, n, p, K) \
|
| 2088 |
+
+ gf_edf_shoup(h3, n, p, K)
|
| 2089 |
+
|
| 2090 |
+
return _sort_factors(factors, multiple=False)
|
| 2091 |
+
|
| 2092 |
+
|
| 2093 |
+
def gf_zassenhaus(f, p, K):
|
| 2094 |
+
"""
|
| 2095 |
+
Factor a square-free ``f`` in ``GF(p)[x]`` for medium ``p``.
|
| 2096 |
+
|
| 2097 |
+
Examples
|
| 2098 |
+
========
|
| 2099 |
+
|
| 2100 |
+
>>> from sympy.polys.domains import ZZ
|
| 2101 |
+
>>> from sympy.polys.galoistools import gf_zassenhaus
|
| 2102 |
+
|
| 2103 |
+
>>> gf_zassenhaus(ZZ.map([1, 4, 3]), 5, ZZ)
|
| 2104 |
+
[[1, 1], [1, 3]]
|
| 2105 |
+
|
| 2106 |
+
"""
|
| 2107 |
+
factors = []
|
| 2108 |
+
|
| 2109 |
+
for factor, n in gf_ddf_zassenhaus(f, p, K):
|
| 2110 |
+
factors += gf_edf_zassenhaus(factor, n, p, K)
|
| 2111 |
+
|
| 2112 |
+
return _sort_factors(factors, multiple=False)
|
| 2113 |
+
|
| 2114 |
+
|
| 2115 |
+
def gf_shoup(f, p, K):
|
| 2116 |
+
"""
|
| 2117 |
+
Factor a square-free ``f`` in ``GF(p)[x]`` for large ``p``.
|
| 2118 |
+
|
| 2119 |
+
Examples
|
| 2120 |
+
========
|
| 2121 |
+
|
| 2122 |
+
>>> from sympy.polys.domains import ZZ
|
| 2123 |
+
>>> from sympy.polys.galoistools import gf_shoup
|
| 2124 |
+
|
| 2125 |
+
>>> gf_shoup(ZZ.map([1, 4, 3]), 5, ZZ)
|
| 2126 |
+
[[1, 1], [1, 3]]
|
| 2127 |
+
|
| 2128 |
+
"""
|
| 2129 |
+
factors = []
|
| 2130 |
+
|
| 2131 |
+
for factor, n in gf_ddf_shoup(f, p, K):
|
| 2132 |
+
factors += gf_edf_shoup(factor, n, p, K)
|
| 2133 |
+
|
| 2134 |
+
return _sort_factors(factors, multiple=False)
|
| 2135 |
+
|
| 2136 |
+
_factor_methods = {
|
| 2137 |
+
'berlekamp': gf_berlekamp, # ``p`` : small
|
| 2138 |
+
'zassenhaus': gf_zassenhaus, # ``p`` : medium
|
| 2139 |
+
'shoup': gf_shoup, # ``p`` : large
|
| 2140 |
+
}
|
| 2141 |
+
|
| 2142 |
+
|
| 2143 |
+
def gf_factor_sqf(f, p, K, method=None):
|
| 2144 |
+
"""
|
| 2145 |
+
Factor a square-free polynomial ``f`` in ``GF(p)[x]``.
|
| 2146 |
+
|
| 2147 |
+
Examples
|
| 2148 |
+
========
|
| 2149 |
+
|
| 2150 |
+
>>> from sympy.polys.domains import ZZ
|
| 2151 |
+
>>> from sympy.polys.galoistools import gf_factor_sqf
|
| 2152 |
+
|
| 2153 |
+
>>> gf_factor_sqf(ZZ.map([3, 2, 4]), 5, ZZ)
|
| 2154 |
+
(3, [[1, 1], [1, 3]])
|
| 2155 |
+
|
| 2156 |
+
"""
|
| 2157 |
+
lc, f = gf_monic(f, p, K)
|
| 2158 |
+
|
| 2159 |
+
if gf_degree(f) < 1:
|
| 2160 |
+
return lc, []
|
| 2161 |
+
|
| 2162 |
+
method = method or query('GF_FACTOR_METHOD')
|
| 2163 |
+
|
| 2164 |
+
if method is not None:
|
| 2165 |
+
factors = _factor_methods[method](f, p, K)
|
| 2166 |
+
else:
|
| 2167 |
+
factors = gf_zassenhaus(f, p, K)
|
| 2168 |
+
|
| 2169 |
+
return lc, factors
|
| 2170 |
+
|
| 2171 |
+
|
| 2172 |
+
def gf_factor(f, p, K):
|
| 2173 |
+
"""
|
| 2174 |
+
Factor (non square-free) polynomials in ``GF(p)[x]``.
|
| 2175 |
+
|
| 2176 |
+
Given a possibly non square-free polynomial ``f`` in ``GF(p)[x]``,
|
| 2177 |
+
returns its complete factorization into irreducibles::
|
| 2178 |
+
|
| 2179 |
+
f_1(x)**e_1 f_2(x)**e_2 ... f_d(x)**e_d
|
| 2180 |
+
|
| 2181 |
+
where each ``f_i`` is a monic polynomial and ``gcd(f_i, f_j) == 1``,
|
| 2182 |
+
for ``i != j``. The result is given as a tuple consisting of the
|
| 2183 |
+
leading coefficient of ``f`` and a list of factors of ``f`` with
|
| 2184 |
+
their multiplicities.
|
| 2185 |
+
|
| 2186 |
+
The algorithm proceeds by first computing square-free decomposition
|
| 2187 |
+
of ``f`` and then iteratively factoring each of square-free factors.
|
| 2188 |
+
|
| 2189 |
+
Consider a non square-free polynomial ``f = (7*x + 1) (x + 2)**2`` in
|
| 2190 |
+
``GF(11)[x]``. We obtain its factorization into irreducibles as follows::
|
| 2191 |
+
|
| 2192 |
+
>>> from sympy.polys.domains import ZZ
|
| 2193 |
+
>>> from sympy.polys.galoistools import gf_factor
|
| 2194 |
+
|
| 2195 |
+
>>> gf_factor(ZZ.map([5, 2, 7, 2]), 11, ZZ)
|
| 2196 |
+
(5, [([1, 2], 1), ([1, 8], 2)])
|
| 2197 |
+
|
| 2198 |
+
We arrived with factorization ``f = 5 (x + 2) (x + 8)**2``. We did not
|
| 2199 |
+
recover the exact form of the input polynomial because we requested to
|
| 2200 |
+
get monic factors of ``f`` and its leading coefficient separately.
|
| 2201 |
+
|
| 2202 |
+
Square-free factors of ``f`` can be factored into irreducibles over
|
| 2203 |
+
``GF(p)`` using three very different methods:
|
| 2204 |
+
|
| 2205 |
+
Berlekamp
|
| 2206 |
+
efficient for very small values of ``p`` (usually ``p < 25``)
|
| 2207 |
+
Cantor-Zassenhaus
|
| 2208 |
+
efficient on average input and with "typical" ``p``
|
| 2209 |
+
Shoup-Kaltofen-Gathen
|
| 2210 |
+
efficient with very large inputs and modulus
|
| 2211 |
+
|
| 2212 |
+
If you want to use a specific factorization method, instead of the default
|
| 2213 |
+
one, set ``GF_FACTOR_METHOD`` with one of ``berlekamp``, ``zassenhaus`` or
|
| 2214 |
+
``shoup`` values.
|
| 2215 |
+
|
| 2216 |
+
References
|
| 2217 |
+
==========
|
| 2218 |
+
|
| 2219 |
+
.. [1] [Gathen99]_
|
| 2220 |
+
|
| 2221 |
+
"""
|
| 2222 |
+
lc, f = gf_monic(f, p, K)
|
| 2223 |
+
|
| 2224 |
+
if gf_degree(f) < 1:
|
| 2225 |
+
return lc, []
|
| 2226 |
+
|
| 2227 |
+
factors = []
|
| 2228 |
+
|
| 2229 |
+
for g, n in gf_sqf_list(f, p, K)[1]:
|
| 2230 |
+
for h in gf_factor_sqf(g, p, K)[1]:
|
| 2231 |
+
factors.append((h, n))
|
| 2232 |
+
|
| 2233 |
+
return lc, _sort_factors(factors)
|
| 2234 |
+
|
| 2235 |
+
|
| 2236 |
+
def gf_value(f, a):
|
| 2237 |
+
"""
|
| 2238 |
+
Value of polynomial 'f' at 'a' in field R.
|
| 2239 |
+
|
| 2240 |
+
Examples
|
| 2241 |
+
========
|
| 2242 |
+
|
| 2243 |
+
>>> from sympy.polys.galoistools import gf_value
|
| 2244 |
+
|
| 2245 |
+
>>> gf_value([1, 7, 2, 4], 11)
|
| 2246 |
+
2204
|
| 2247 |
+
|
| 2248 |
+
"""
|
| 2249 |
+
result = 0
|
| 2250 |
+
for c in f:
|
| 2251 |
+
result *= a
|
| 2252 |
+
result += c
|
| 2253 |
+
return result
|
| 2254 |
+
|
| 2255 |
+
|
| 2256 |
+
def linear_congruence(a, b, m):
|
| 2257 |
+
"""
|
| 2258 |
+
Returns the values of x satisfying a*x congruent b mod(m)
|
| 2259 |
+
|
| 2260 |
+
Here m is positive integer and a, b are natural numbers.
|
| 2261 |
+
This function returns only those values of x which are distinct mod(m).
|
| 2262 |
+
|
| 2263 |
+
Examples
|
| 2264 |
+
========
|
| 2265 |
+
|
| 2266 |
+
>>> from sympy.polys.galoistools import linear_congruence
|
| 2267 |
+
|
| 2268 |
+
>>> linear_congruence(3, 12, 15)
|
| 2269 |
+
[4, 9, 14]
|
| 2270 |
+
|
| 2271 |
+
There are 3 solutions distinct mod(15) since gcd(a, m) = gcd(3, 15) = 3.
|
| 2272 |
+
|
| 2273 |
+
References
|
| 2274 |
+
==========
|
| 2275 |
+
|
| 2276 |
+
.. [1] https://en.wikipedia.org/wiki/Linear_congruence_theorem
|
| 2277 |
+
|
| 2278 |
+
"""
|
| 2279 |
+
from sympy.polys.polytools import gcdex
|
| 2280 |
+
if a % m == 0:
|
| 2281 |
+
if b % m == 0:
|
| 2282 |
+
return list(range(m))
|
| 2283 |
+
else:
|
| 2284 |
+
return []
|
| 2285 |
+
r, _, g = gcdex(a, m)
|
| 2286 |
+
if b % g != 0:
|
| 2287 |
+
return []
|
| 2288 |
+
return [(r * b // g + t * m // g) % m for t in range(g)]
|
| 2289 |
+
|
| 2290 |
+
|
| 2291 |
+
def _raise_mod_power(x, s, p, f):
|
| 2292 |
+
"""
|
| 2293 |
+
Used in gf_csolve to generate solutions of f(x) cong 0 mod(p**(s + 1))
|
| 2294 |
+
from the solutions of f(x) cong 0 mod(p**s).
|
| 2295 |
+
|
| 2296 |
+
Examples
|
| 2297 |
+
========
|
| 2298 |
+
|
| 2299 |
+
>>> from sympy.polys.galoistools import _raise_mod_power
|
| 2300 |
+
>>> from sympy.polys.galoistools import csolve_prime
|
| 2301 |
+
|
| 2302 |
+
These is the solutions of f(x) = x**2 + x + 7 cong 0 mod(3)
|
| 2303 |
+
|
| 2304 |
+
>>> f = [1, 1, 7]
|
| 2305 |
+
>>> csolve_prime(f, 3)
|
| 2306 |
+
[1]
|
| 2307 |
+
>>> [ i for i in range(3) if not (i**2 + i + 7) % 3]
|
| 2308 |
+
[1]
|
| 2309 |
+
|
| 2310 |
+
The solutions of f(x) cong 0 mod(9) are constructed from the
|
| 2311 |
+
values returned from _raise_mod_power:
|
| 2312 |
+
|
| 2313 |
+
>>> x, s, p = 1, 1, 3
|
| 2314 |
+
>>> V = _raise_mod_power(x, s, p, f)
|
| 2315 |
+
>>> [x + v * p**s for v in V]
|
| 2316 |
+
[1, 4, 7]
|
| 2317 |
+
|
| 2318 |
+
And these are confirmed with the following:
|
| 2319 |
+
|
| 2320 |
+
>>> [ i for i in range(3**2) if not (i**2 + i + 7) % 3**2]
|
| 2321 |
+
[1, 4, 7]
|
| 2322 |
+
|
| 2323 |
+
"""
|
| 2324 |
+
from sympy.polys.domains import ZZ
|
| 2325 |
+
f_f = gf_diff(f, p, ZZ)
|
| 2326 |
+
alpha = gf_value(f_f, x)
|
| 2327 |
+
beta = - gf_value(f, x) // p**s
|
| 2328 |
+
return linear_congruence(alpha, beta, p)
|
| 2329 |
+
|
| 2330 |
+
|
| 2331 |
+
def _csolve_prime_las_vegas(f, p, seed=None):
|
| 2332 |
+
r""" Solutions of `f(x) \equiv 0 \pmod{p}`, `f(0) \not\equiv 0 \pmod{p}`.
|
| 2333 |
+
|
| 2334 |
+
Explanation
|
| 2335 |
+
===========
|
| 2336 |
+
|
| 2337 |
+
This algorithm is classified as the Las Vegas method.
|
| 2338 |
+
That is, it always returns the correct answer and solves the problem
|
| 2339 |
+
fast in many cases, but if it is unlucky, it does not answer forever.
|
| 2340 |
+
|
| 2341 |
+
Suppose the polynomial f is not a zero polynomial. Assume further
|
| 2342 |
+
that it is of degree at most p-1 and `f(0)\not\equiv 0 \pmod{p}`.
|
| 2343 |
+
These assumptions are not an essential part of the algorithm,
|
| 2344 |
+
only that it is more convenient for the function calling this
|
| 2345 |
+
function to resolve them.
|
| 2346 |
+
|
| 2347 |
+
Note that `x^{p-1} - 1 \equiv \prod_{a=1}^{p-1}(x - a) \pmod{p}`.
|
| 2348 |
+
Thus, the greatest common divisor with f is `\prod_{s \in S}(x - s)`,
|
| 2349 |
+
with S being the set of solutions to f. Furthermore,
|
| 2350 |
+
when a is randomly determined, `(x+a)^{(p-1)/2}-1` is
|
| 2351 |
+
a polynomial with (p-1)/2 randomly chosen solutions.
|
| 2352 |
+
The greatest common divisor of f may be a nontrivial factor of f.
|
| 2353 |
+
|
| 2354 |
+
When p is large and the degree of f is small,
|
| 2355 |
+
it is faster than naive solution methods.
|
| 2356 |
+
|
| 2357 |
+
Parameters
|
| 2358 |
+
==========
|
| 2359 |
+
|
| 2360 |
+
f : polynomial
|
| 2361 |
+
p : prime number
|
| 2362 |
+
|
| 2363 |
+
Returns
|
| 2364 |
+
=======
|
| 2365 |
+
|
| 2366 |
+
list[int]
|
| 2367 |
+
a list of solutions, sorted in ascending order
|
| 2368 |
+
by integers in the range [1, p). The same value
|
| 2369 |
+
does not exist in the list even if there is
|
| 2370 |
+
a multiple solution. If no solution exists, returns [].
|
| 2371 |
+
|
| 2372 |
+
Examples
|
| 2373 |
+
========
|
| 2374 |
+
|
| 2375 |
+
>>> from sympy.polys.galoistools import _csolve_prime_las_vegas
|
| 2376 |
+
>>> _csolve_prime_las_vegas([1, 4, 3], 7) # x^2 + 4x + 3 = 0 (mod 7)
|
| 2377 |
+
[4, 6]
|
| 2378 |
+
>>> _csolve_prime_las_vegas([5, 7, 1, 9], 11) # 5x^3 + 7x^2 + x + 9 = 0 (mod 11)
|
| 2379 |
+
[1, 5, 8]
|
| 2380 |
+
|
| 2381 |
+
References
|
| 2382 |
+
==========
|
| 2383 |
+
|
| 2384 |
+
.. [1] R. Crandall and C. Pomerance "Prime Numbers", 2nd Ed., Algorithm 2.3.10
|
| 2385 |
+
|
| 2386 |
+
"""
|
| 2387 |
+
from sympy.polys.domains import ZZ
|
| 2388 |
+
from sympy.ntheory import sqrt_mod
|
| 2389 |
+
randint = _randint(seed)
|
| 2390 |
+
root = set()
|
| 2391 |
+
g = gf_pow_mod([1, 0], p - 1, f, p, ZZ)
|
| 2392 |
+
g = gf_sub_ground(g, 1, p, ZZ)
|
| 2393 |
+
# We want to calculate gcd(x**(p-1) - 1, f(x))
|
| 2394 |
+
factors = [gf_gcd(f, g, p, ZZ)]
|
| 2395 |
+
while factors:
|
| 2396 |
+
f = factors.pop()
|
| 2397 |
+
# If the degree is small, solve directly
|
| 2398 |
+
if len(f) <= 1:
|
| 2399 |
+
continue
|
| 2400 |
+
if len(f) == 2:
|
| 2401 |
+
root.add(-invert(f[0], p) * f[1] % p)
|
| 2402 |
+
continue
|
| 2403 |
+
if len(f) == 3:
|
| 2404 |
+
inv = invert(f[0], p)
|
| 2405 |
+
b = f[1] * inv % p
|
| 2406 |
+
b = (b + p * (b % 2)) // 2
|
| 2407 |
+
root.update((r - b) % p for r in
|
| 2408 |
+
sqrt_mod(b**2 - f[2] * inv, p, all_roots=True))
|
| 2409 |
+
continue
|
| 2410 |
+
while True:
|
| 2411 |
+
# Determine `a` randomly and
|
| 2412 |
+
# compute gcd((x+a)**((p-1)//2)-1, f(x))
|
| 2413 |
+
a = randint(0, p - 1)
|
| 2414 |
+
g = gf_pow_mod([1, a], (p - 1) // 2, f, p, ZZ)
|
| 2415 |
+
g = gf_sub_ground(g, 1, p, ZZ)
|
| 2416 |
+
g = gf_gcd(f, g, p, ZZ)
|
| 2417 |
+
if 1 < len(g) < len(f):
|
| 2418 |
+
factors.append(g)
|
| 2419 |
+
factors.append(gf_div(f, g, p, ZZ)[0])
|
| 2420 |
+
break
|
| 2421 |
+
return sorted(root)
|
| 2422 |
+
|
| 2423 |
+
|
| 2424 |
+
def csolve_prime(f, p, e=1):
|
| 2425 |
+
r""" Solutions of `f(x) \equiv 0 \pmod{p^e}`.
|
| 2426 |
+
|
| 2427 |
+
Parameters
|
| 2428 |
+
==========
|
| 2429 |
+
|
| 2430 |
+
f : polynomial
|
| 2431 |
+
p : prime number
|
| 2432 |
+
e : positive integer
|
| 2433 |
+
|
| 2434 |
+
Returns
|
| 2435 |
+
=======
|
| 2436 |
+
|
| 2437 |
+
list[int]
|
| 2438 |
+
a list of solutions, sorted in ascending order
|
| 2439 |
+
by integers in the range [1, p**e). The same value
|
| 2440 |
+
does not exist in the list even if there is
|
| 2441 |
+
a multiple solution. If no solution exists, returns [].
|
| 2442 |
+
|
| 2443 |
+
Examples
|
| 2444 |
+
========
|
| 2445 |
+
|
| 2446 |
+
>>> from sympy.polys.galoistools import csolve_prime
|
| 2447 |
+
>>> csolve_prime([1, 1, 7], 3, 1)
|
| 2448 |
+
[1]
|
| 2449 |
+
>>> csolve_prime([1, 1, 7], 3, 2)
|
| 2450 |
+
[1, 4, 7]
|
| 2451 |
+
|
| 2452 |
+
Solutions [7, 4, 1] (mod 3**2) are generated by ``_raise_mod_power()``
|
| 2453 |
+
from solution [1] (mod 3).
|
| 2454 |
+
"""
|
| 2455 |
+
from sympy.polys.domains import ZZ
|
| 2456 |
+
g = [MPZ(int(c)) for c in f]
|
| 2457 |
+
# Convert to polynomial of degree at most p-1
|
| 2458 |
+
for i in range(len(g) - p):
|
| 2459 |
+
g[i + p - 1] += g[i]
|
| 2460 |
+
g[i] = 0
|
| 2461 |
+
g = gf_trunc(g, p)
|
| 2462 |
+
# Checks whether g(x) is divisible by x
|
| 2463 |
+
k = 0
|
| 2464 |
+
while k < len(g) and g[len(g) - k - 1] == 0:
|
| 2465 |
+
k += 1
|
| 2466 |
+
if k:
|
| 2467 |
+
g = g[:-k]
|
| 2468 |
+
root_zero = [0]
|
| 2469 |
+
else:
|
| 2470 |
+
root_zero = []
|
| 2471 |
+
if g == []:
|
| 2472 |
+
X1 = list(range(p))
|
| 2473 |
+
elif len(g)**2 < p:
|
| 2474 |
+
# The conditions under which `_csolve_prime_las_vegas` is faster than
|
| 2475 |
+
# a naive solution are worth considering.
|
| 2476 |
+
X1 = root_zero + _csolve_prime_las_vegas(g, p)
|
| 2477 |
+
else:
|
| 2478 |
+
X1 = root_zero + [i for i in range(p) if gf_eval(g, i, p, ZZ) == 0]
|
| 2479 |
+
if e == 1:
|
| 2480 |
+
return X1
|
| 2481 |
+
X = []
|
| 2482 |
+
S = list(zip(X1, [1]*len(X1)))
|
| 2483 |
+
while S:
|
| 2484 |
+
x, s = S.pop()
|
| 2485 |
+
if s == e:
|
| 2486 |
+
X.append(x)
|
| 2487 |
+
else:
|
| 2488 |
+
s1 = s + 1
|
| 2489 |
+
ps = p**s
|
| 2490 |
+
S.extend([(x + v*ps, s1) for v in _raise_mod_power(x, s, p, f)])
|
| 2491 |
+
return sorted(X)
|
| 2492 |
+
|
| 2493 |
+
|
| 2494 |
+
def gf_csolve(f, n):
|
| 2495 |
+
"""
|
| 2496 |
+
To solve f(x) congruent 0 mod(n).
|
| 2497 |
+
|
| 2498 |
+
n is divided into canonical factors and f(x) cong 0 mod(p**e) will be
|
| 2499 |
+
solved for each factor. Applying the Chinese Remainder Theorem to the
|
| 2500 |
+
results returns the final answers.
|
| 2501 |
+
|
| 2502 |
+
Examples
|
| 2503 |
+
========
|
| 2504 |
+
|
| 2505 |
+
Solve [1, 1, 7] congruent 0 mod(189):
|
| 2506 |
+
|
| 2507 |
+
>>> from sympy.polys.galoistools import gf_csolve
|
| 2508 |
+
>>> gf_csolve([1, 1, 7], 189)
|
| 2509 |
+
[13, 49, 76, 112, 139, 175]
|
| 2510 |
+
|
| 2511 |
+
See Also
|
| 2512 |
+
========
|
| 2513 |
+
|
| 2514 |
+
sympy.ntheory.residue_ntheory.polynomial_congruence : a higher level solving routine
|
| 2515 |
+
|
| 2516 |
+
References
|
| 2517 |
+
==========
|
| 2518 |
+
|
| 2519 |
+
.. [1] 'An introduction to the Theory of Numbers' 5th Edition by Ivan Niven,
|
| 2520 |
+
Zuckerman and Montgomery.
|
| 2521 |
+
|
| 2522 |
+
"""
|
| 2523 |
+
from sympy.polys.domains import ZZ
|
| 2524 |
+
from sympy.ntheory import factorint
|
| 2525 |
+
P = factorint(n)
|
| 2526 |
+
X = [csolve_prime(f, p, e) for p, e in P.items()]
|
| 2527 |
+
pools = list(map(tuple, X))
|
| 2528 |
+
perms = [[]]
|
| 2529 |
+
for pool in pools:
|
| 2530 |
+
perms = [x + [y] for x in perms for y in pool]
|
| 2531 |
+
dist_factors = [pow(p, e) for p, e in P.items()]
|
| 2532 |
+
return sorted([gf_crt(per, dist_factors, ZZ) for per in perms])
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/__init__.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
|
| 3 |
+
sympy.polys.matrices package.
|
| 4 |
+
|
| 5 |
+
The main export from this package is the DomainMatrix class which is a
|
| 6 |
+
lower-level implementation of matrices based on the polys Domains. This
|
| 7 |
+
implementation is typically a lot faster than SymPy's standard Matrix class
|
| 8 |
+
but is a work in progress and is still experimental.
|
| 9 |
+
|
| 10 |
+
"""
|
| 11 |
+
from .domainmatrix import DomainMatrix, DM
|
| 12 |
+
|
| 13 |
+
__all__ = [
|
| 14 |
+
'DomainMatrix', 'DM',
|
| 15 |
+
]
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/_dfm.py
ADDED
|
@@ -0,0 +1,898 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# sympy.polys.matrices.dfm
|
| 3 |
+
#
|
| 4 |
+
# This modules defines the DFM class which is a wrapper for dense flint
|
| 5 |
+
# matrices as found in python-flint.
|
| 6 |
+
#
|
| 7 |
+
# As of python-flint 0.4.1 matrices over the following domains can be supported
|
| 8 |
+
# by python-flint:
|
| 9 |
+
#
|
| 10 |
+
# ZZ: flint.fmpz_mat
|
| 11 |
+
# QQ: flint.fmpq_mat
|
| 12 |
+
# GF(p): flint.nmod_mat (p prime and p < ~2**62)
|
| 13 |
+
#
|
| 14 |
+
# The underlying flint library has many more domains, but these are not yet
|
| 15 |
+
# supported by python-flint.
|
| 16 |
+
#
|
| 17 |
+
# The DFM class is a wrapper for the flint matrices and provides a common
|
| 18 |
+
# interface for all supported domains that is interchangeable with the DDM
|
| 19 |
+
# and SDM classes so that DomainMatrix can be used with any as its internal
|
| 20 |
+
# matrix representation.
|
| 21 |
+
#
|
| 22 |
+
|
| 23 |
+
# TODO:
|
| 24 |
+
#
|
| 25 |
+
# Implement the following methods that are provided by python-flint:
|
| 26 |
+
#
|
| 27 |
+
# - hnf (Hermite normal form)
|
| 28 |
+
# - snf (Smith normal form)
|
| 29 |
+
# - minpoly
|
| 30 |
+
# - is_hnf
|
| 31 |
+
# - is_snf
|
| 32 |
+
# - rank
|
| 33 |
+
#
|
| 34 |
+
# The other types DDM and SDM do not have these methods and the algorithms
|
| 35 |
+
# for hnf, snf and rank are already implemented. Algorithms for minpoly,
|
| 36 |
+
# is_hnf and is_snf would need to be added.
|
| 37 |
+
#
|
| 38 |
+
# Add more methods to python-flint to expose more of Flint's functionality
|
| 39 |
+
# and also to make some of the above methods simpler or more efficient e.g.
|
| 40 |
+
# slicing, fancy indexing etc.
|
| 41 |
+
|
| 42 |
+
from sympy.external.gmpy import GROUND_TYPES
|
| 43 |
+
from sympy.external.importtools import import_module
|
| 44 |
+
from sympy.utilities.decorator import doctest_depends_on
|
| 45 |
+
|
| 46 |
+
from sympy.polys.domains import ZZ, QQ
|
| 47 |
+
|
| 48 |
+
from .exceptions import (
|
| 49 |
+
DMBadInputError,
|
| 50 |
+
DMDomainError,
|
| 51 |
+
DMNonSquareMatrixError,
|
| 52 |
+
DMNonInvertibleMatrixError,
|
| 53 |
+
DMRankError,
|
| 54 |
+
DMShapeError,
|
| 55 |
+
DMValueError,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if GROUND_TYPES != 'flint':
|
| 60 |
+
__doctest_skip__ = ['*']
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
flint = import_module('flint')
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
__all__ = ['DFM']
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@doctest_depends_on(ground_types=['flint'])
|
| 70 |
+
class DFM:
|
| 71 |
+
"""
|
| 72 |
+
Dense FLINT matrix. This class is a wrapper for matrices from python-flint.
|
| 73 |
+
|
| 74 |
+
>>> from sympy.polys.domains import ZZ
|
| 75 |
+
>>> from sympy.polys.matrices.dfm import DFM
|
| 76 |
+
>>> dfm = DFM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
| 77 |
+
>>> dfm
|
| 78 |
+
[[1, 2], [3, 4]]
|
| 79 |
+
>>> dfm.rep
|
| 80 |
+
[1, 2]
|
| 81 |
+
[3, 4]
|
| 82 |
+
>>> type(dfm.rep) # doctest: +SKIP
|
| 83 |
+
<class 'flint._flint.fmpz_mat'>
|
| 84 |
+
|
| 85 |
+
Usually, the DFM class is not instantiated directly, but is created as the
|
| 86 |
+
internal representation of :class:`~.DomainMatrix`. When
|
| 87 |
+
`SYMPY_GROUND_TYPES` is set to `flint` and `python-flint` is installed, the
|
| 88 |
+
:class:`DFM` class is used automatically as the internal representation of
|
| 89 |
+
:class:`~.DomainMatrix` in dense format if the domain is supported by
|
| 90 |
+
python-flint.
|
| 91 |
+
|
| 92 |
+
>>> from sympy.polys.matrices.domainmatrix import DM
|
| 93 |
+
>>> dM = DM([[1, 2], [3, 4]], ZZ)
|
| 94 |
+
>>> dM.rep
|
| 95 |
+
[[1, 2], [3, 4]]
|
| 96 |
+
|
| 97 |
+
A :class:`~.DomainMatrix` can be converted to :class:`DFM` by calling the
|
| 98 |
+
:meth:`to_dfm` method:
|
| 99 |
+
|
| 100 |
+
>>> dM.to_dfm()
|
| 101 |
+
[[1, 2], [3, 4]]
|
| 102 |
+
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
fmt = 'dense'
|
| 106 |
+
is_DFM = True
|
| 107 |
+
is_DDM = False
|
| 108 |
+
|
| 109 |
+
def __new__(cls, rowslist, shape, domain):
|
| 110 |
+
"""Construct from a nested list."""
|
| 111 |
+
flint_mat = cls._get_flint_func(domain)
|
| 112 |
+
|
| 113 |
+
if 0 not in shape:
|
| 114 |
+
try:
|
| 115 |
+
rep = flint_mat(rowslist)
|
| 116 |
+
except (ValueError, TypeError):
|
| 117 |
+
raise DMBadInputError(f"Input should be a list of list of {domain}")
|
| 118 |
+
else:
|
| 119 |
+
rep = flint_mat(*shape)
|
| 120 |
+
|
| 121 |
+
return cls._new(rep, shape, domain)
|
| 122 |
+
|
| 123 |
+
@classmethod
|
| 124 |
+
def _new(cls, rep, shape, domain):
|
| 125 |
+
"""Internal constructor from a flint matrix."""
|
| 126 |
+
cls._check(rep, shape, domain)
|
| 127 |
+
obj = object.__new__(cls)
|
| 128 |
+
obj.rep = rep
|
| 129 |
+
obj.shape = obj.rows, obj.cols = shape
|
| 130 |
+
obj.domain = domain
|
| 131 |
+
return obj
|
| 132 |
+
|
| 133 |
+
def _new_rep(self, rep):
|
| 134 |
+
"""Create a new DFM with the same shape and domain but a new rep."""
|
| 135 |
+
return self._new(rep, self.shape, self.domain)
|
| 136 |
+
|
| 137 |
+
@classmethod
|
| 138 |
+
def _check(cls, rep, shape, domain):
|
| 139 |
+
repshape = (rep.nrows(), rep.ncols())
|
| 140 |
+
if repshape != shape:
|
| 141 |
+
raise DMBadInputError("Shape of rep does not match shape of DFM")
|
| 142 |
+
if domain == ZZ and not isinstance(rep, flint.fmpz_mat):
|
| 143 |
+
raise RuntimeError("Rep is not a flint.fmpz_mat")
|
| 144 |
+
elif domain == QQ and not isinstance(rep, flint.fmpq_mat):
|
| 145 |
+
raise RuntimeError("Rep is not a flint.fmpq_mat")
|
| 146 |
+
elif domain not in (ZZ, QQ):
|
| 147 |
+
raise NotImplementedError("Only ZZ and QQ are supported by DFM")
|
| 148 |
+
|
| 149 |
+
@classmethod
|
| 150 |
+
def _supports_domain(cls, domain):
|
| 151 |
+
"""Return True if the given domain is supported by DFM."""
|
| 152 |
+
return domain in (ZZ, QQ)
|
| 153 |
+
|
| 154 |
+
@classmethod
|
| 155 |
+
def _get_flint_func(cls, domain):
|
| 156 |
+
"""Return the flint matrix class for the given domain."""
|
| 157 |
+
if domain == ZZ:
|
| 158 |
+
return flint.fmpz_mat
|
| 159 |
+
elif domain == QQ:
|
| 160 |
+
return flint.fmpq_mat
|
| 161 |
+
else:
|
| 162 |
+
raise NotImplementedError("Only ZZ and QQ are supported by DFM")
|
| 163 |
+
|
| 164 |
+
@property
|
| 165 |
+
def _func(self):
|
| 166 |
+
"""Callable to create a flint matrix of the same domain."""
|
| 167 |
+
return self._get_flint_func(self.domain)
|
| 168 |
+
|
| 169 |
+
def __str__(self):
|
| 170 |
+
"""Return ``str(self)``."""
|
| 171 |
+
return str(self.to_ddm())
|
| 172 |
+
|
| 173 |
+
def __repr__(self):
|
| 174 |
+
"""Return ``repr(self)``."""
|
| 175 |
+
return f'DFM{repr(self.to_ddm())[3:]}'
|
| 176 |
+
|
| 177 |
+
def __eq__(self, other):
|
| 178 |
+
"""Return ``self == other``."""
|
| 179 |
+
if not isinstance(other, DFM):
|
| 180 |
+
return NotImplemented
|
| 181 |
+
# Compare domains first because we do *not* want matrices with
|
| 182 |
+
# different domains to be equal but e.g. a flint fmpz_mat and fmpq_mat
|
| 183 |
+
# with the same entries will compare equal.
|
| 184 |
+
return self.domain == other.domain and self.rep == other.rep
|
| 185 |
+
|
| 186 |
+
@classmethod
|
| 187 |
+
def from_list(cls, rowslist, shape, domain):
|
| 188 |
+
"""Construct from a nested list."""
|
| 189 |
+
return cls(rowslist, shape, domain)
|
| 190 |
+
|
| 191 |
+
def to_list(self):
|
| 192 |
+
"""Convert to a nested list."""
|
| 193 |
+
return self.rep.tolist()
|
| 194 |
+
|
| 195 |
+
def copy(self):
|
| 196 |
+
"""Return a copy of self."""
|
| 197 |
+
return self._new_rep(self._func(self.rep))
|
| 198 |
+
|
| 199 |
+
def to_ddm(self):
|
| 200 |
+
"""Convert to a DDM."""
|
| 201 |
+
return DDM.from_list(self.to_list(), self.shape, self.domain)
|
| 202 |
+
|
| 203 |
+
def to_sdm(self):
|
| 204 |
+
"""Convert to a SDM."""
|
| 205 |
+
return SDM.from_list(self.to_list(), self.shape, self.domain)
|
| 206 |
+
|
| 207 |
+
def to_dfm(self):
|
| 208 |
+
"""Return self."""
|
| 209 |
+
return self
|
| 210 |
+
|
| 211 |
+
def to_dfm_or_ddm(self):
|
| 212 |
+
"""
|
| 213 |
+
Convert to a :class:`DFM`.
|
| 214 |
+
|
| 215 |
+
This :class:`DFM` method exists to parallel the :class:`~.DDM` and
|
| 216 |
+
:class:`~.SDM` methods. For :class:`DFM` it will always return self.
|
| 217 |
+
|
| 218 |
+
See Also
|
| 219 |
+
========
|
| 220 |
+
|
| 221 |
+
to_ddm
|
| 222 |
+
to_sdm
|
| 223 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.to_dfm_or_ddm
|
| 224 |
+
"""
|
| 225 |
+
return self
|
| 226 |
+
|
| 227 |
+
@classmethod
|
| 228 |
+
def from_ddm(cls, ddm):
|
| 229 |
+
"""Convert from a DDM."""
|
| 230 |
+
return cls.from_list(ddm.to_list(), ddm.shape, ddm.domain)
|
| 231 |
+
|
| 232 |
+
@classmethod
|
| 233 |
+
def from_list_flat(cls, elements, shape, domain):
|
| 234 |
+
"""Inverse of :meth:`to_list_flat`."""
|
| 235 |
+
func = cls._get_flint_func(domain)
|
| 236 |
+
try:
|
| 237 |
+
rep = func(*shape, elements)
|
| 238 |
+
except ValueError:
|
| 239 |
+
raise DMBadInputError(f"Incorrect number of elements for shape {shape}")
|
| 240 |
+
except TypeError:
|
| 241 |
+
raise DMBadInputError(f"Input should be a list of {domain}")
|
| 242 |
+
return cls(rep, shape, domain)
|
| 243 |
+
|
| 244 |
+
def to_list_flat(self):
|
| 245 |
+
"""Convert to a flat list."""
|
| 246 |
+
return self.rep.entries()
|
| 247 |
+
|
| 248 |
+
def to_flat_nz(self):
|
| 249 |
+
"""Convert to a flat list of non-zeros."""
|
| 250 |
+
return self.to_ddm().to_flat_nz()
|
| 251 |
+
|
| 252 |
+
@classmethod
|
| 253 |
+
def from_flat_nz(cls, elements, data, domain):
|
| 254 |
+
"""Inverse of :meth:`to_flat_nz`."""
|
| 255 |
+
return DDM.from_flat_nz(elements, data, domain).to_dfm()
|
| 256 |
+
|
| 257 |
+
def to_dod(self):
|
| 258 |
+
"""Convert to a DOD."""
|
| 259 |
+
return self.to_ddm().to_dod()
|
| 260 |
+
|
| 261 |
+
@classmethod
|
| 262 |
+
def from_dod(cls, dod, shape, domain):
|
| 263 |
+
"""Inverse of :meth:`to_dod`."""
|
| 264 |
+
return DDM.from_dod(dod, shape, domain).to_dfm()
|
| 265 |
+
|
| 266 |
+
def to_dok(self):
|
| 267 |
+
"""Convert to a DOK."""
|
| 268 |
+
return self.to_ddm().to_dok()
|
| 269 |
+
|
| 270 |
+
@classmethod
|
| 271 |
+
def from_dok(cls, dok, shape, domain):
|
| 272 |
+
"""Inverse of :math:`to_dod`."""
|
| 273 |
+
return DDM.from_dok(dok, shape, domain).to_dfm()
|
| 274 |
+
|
| 275 |
+
def iter_values(self):
|
| 276 |
+
"""Iterater over the non-zero values of the matrix."""
|
| 277 |
+
m, n = self.shape
|
| 278 |
+
rep = self.rep
|
| 279 |
+
for i in range(m):
|
| 280 |
+
for j in range(n):
|
| 281 |
+
repij = rep[i, j]
|
| 282 |
+
if repij:
|
| 283 |
+
yield rep[i, j]
|
| 284 |
+
|
| 285 |
+
def iter_items(self):
|
| 286 |
+
"""Iterate over indices and values of nonzero elements of the matrix."""
|
| 287 |
+
m, n = self.shape
|
| 288 |
+
rep = self.rep
|
| 289 |
+
for i in range(m):
|
| 290 |
+
for j in range(n):
|
| 291 |
+
repij = rep[i, j]
|
| 292 |
+
if repij:
|
| 293 |
+
yield ((i, j), repij)
|
| 294 |
+
|
| 295 |
+
def convert_to(self, domain):
|
| 296 |
+
"""Convert to a new domain."""
|
| 297 |
+
if domain == self.domain:
|
| 298 |
+
return self.copy()
|
| 299 |
+
elif domain == QQ and self.domain == ZZ:
|
| 300 |
+
return self._new(flint.fmpq_mat(self.rep), self.shape, domain)
|
| 301 |
+
elif domain == ZZ and self.domain == QQ:
|
| 302 |
+
# XXX: python-flint has no fmpz_mat.from_fmpq_mat
|
| 303 |
+
return self.to_ddm().convert_to(domain).to_dfm()
|
| 304 |
+
else:
|
| 305 |
+
# It is the callers responsibility to convert to DDM before calling
|
| 306 |
+
# this method if the domain is not supported by DFM.
|
| 307 |
+
raise NotImplementedError("Only ZZ and QQ are supported by DFM")
|
| 308 |
+
|
| 309 |
+
def getitem(self, i, j):
|
| 310 |
+
"""Get the ``(i, j)``-th entry."""
|
| 311 |
+
# XXX: flint matrices do not support negative indices
|
| 312 |
+
# XXX: They also raise ValueError instead of IndexError
|
| 313 |
+
m, n = self.shape
|
| 314 |
+
if i < 0:
|
| 315 |
+
i += m
|
| 316 |
+
if j < 0:
|
| 317 |
+
j += n
|
| 318 |
+
try:
|
| 319 |
+
return self.rep[i, j]
|
| 320 |
+
except ValueError:
|
| 321 |
+
raise IndexError(f"Invalid indices ({i}, {j}) for Matrix of shape {self.shape}")
|
| 322 |
+
|
| 323 |
+
def setitem(self, i, j, value):
|
| 324 |
+
"""Set the ``(i, j)``-th entry."""
|
| 325 |
+
# XXX: flint matrices do not support negative indices
|
| 326 |
+
# XXX: They also raise ValueError instead of IndexError
|
| 327 |
+
m, n = self.shape
|
| 328 |
+
if i < 0:
|
| 329 |
+
i += m
|
| 330 |
+
if j < 0:
|
| 331 |
+
j += n
|
| 332 |
+
try:
|
| 333 |
+
self.rep[i, j] = value
|
| 334 |
+
except ValueError:
|
| 335 |
+
raise IndexError(f"Invalid indices ({i}, {j}) for Matrix of shape {self.shape}")
|
| 336 |
+
|
| 337 |
+
def _extract(self, i_indices, j_indices):
|
| 338 |
+
"""Extract a submatrix with no checking."""
|
| 339 |
+
# Indices must be positive and in range.
|
| 340 |
+
M = self.rep
|
| 341 |
+
lol = [[M[i, j] for j in j_indices] for i in i_indices]
|
| 342 |
+
shape = (len(i_indices), len(j_indices))
|
| 343 |
+
return self.from_list(lol, shape, self.domain)
|
| 344 |
+
|
| 345 |
+
def extract(self, rowslist, colslist):
|
| 346 |
+
"""Extract a submatrix."""
|
| 347 |
+
# XXX: flint matrices do not support fancy indexing or negative indices
|
| 348 |
+
#
|
| 349 |
+
# Check and convert negative indices before calling _extract.
|
| 350 |
+
m, n = self.shape
|
| 351 |
+
|
| 352 |
+
new_rows = []
|
| 353 |
+
new_cols = []
|
| 354 |
+
|
| 355 |
+
for i in rowslist:
|
| 356 |
+
if i < 0:
|
| 357 |
+
i_pos = i + m
|
| 358 |
+
else:
|
| 359 |
+
i_pos = i
|
| 360 |
+
if not 0 <= i_pos < m:
|
| 361 |
+
raise IndexError(f"Invalid row index {i} for Matrix of shape {self.shape}")
|
| 362 |
+
new_rows.append(i_pos)
|
| 363 |
+
|
| 364 |
+
for j in colslist:
|
| 365 |
+
if j < 0:
|
| 366 |
+
j_pos = j + n
|
| 367 |
+
else:
|
| 368 |
+
j_pos = j
|
| 369 |
+
if not 0 <= j_pos < n:
|
| 370 |
+
raise IndexError(f"Invalid column index {j} for Matrix of shape {self.shape}")
|
| 371 |
+
new_cols.append(j_pos)
|
| 372 |
+
|
| 373 |
+
return self._extract(new_rows, new_cols)
|
| 374 |
+
|
| 375 |
+
def extract_slice(self, rowslice, colslice):
|
| 376 |
+
"""Slice a DFM."""
|
| 377 |
+
# XXX: flint matrices do not support slicing
|
| 378 |
+
m, n = self.shape
|
| 379 |
+
i_indices = range(m)[rowslice]
|
| 380 |
+
j_indices = range(n)[colslice]
|
| 381 |
+
return self._extract(i_indices, j_indices)
|
| 382 |
+
|
| 383 |
+
def neg(self):
|
| 384 |
+
"""Negate a DFM matrix."""
|
| 385 |
+
return self._new_rep(-self.rep)
|
| 386 |
+
|
| 387 |
+
def add(self, other):
|
| 388 |
+
"""Add two DFM matrices."""
|
| 389 |
+
return self._new_rep(self.rep + other.rep)
|
| 390 |
+
|
| 391 |
+
def sub(self, other):
|
| 392 |
+
"""Subtract two DFM matrices."""
|
| 393 |
+
return self._new_rep(self.rep - other.rep)
|
| 394 |
+
|
| 395 |
+
def mul(self, other):
|
| 396 |
+
"""Multiply a DFM matrix from the right by a scalar."""
|
| 397 |
+
return self._new_rep(self.rep * other)
|
| 398 |
+
|
| 399 |
+
def rmul(self, other):
|
| 400 |
+
"""Multiply a DFM matrix from the left by a scalar."""
|
| 401 |
+
return self._new_rep(other * self.rep)
|
| 402 |
+
|
| 403 |
+
def mul_elementwise(self, other):
|
| 404 |
+
"""Elementwise multiplication of two DFM matrices."""
|
| 405 |
+
# XXX: flint matrices do not support elementwise multiplication
|
| 406 |
+
return self.to_ddm().mul_elementwise(other.to_ddm()).to_dfm()
|
| 407 |
+
|
| 408 |
+
def matmul(self, other):
|
| 409 |
+
"""Multiply two DFM matrices."""
|
| 410 |
+
shape = (self.rows, other.cols)
|
| 411 |
+
return self._new(self.rep * other.rep, shape, self.domain)
|
| 412 |
+
|
| 413 |
+
# XXX: For the most part DomainMatrix does not expect DDM, SDM, or DFM to
|
| 414 |
+
# have arithmetic operators defined. The only exception is negation.
|
| 415 |
+
# Perhaps that should be removed.
|
| 416 |
+
|
| 417 |
+
def __neg__(self):
|
| 418 |
+
"""Negate a DFM matrix."""
|
| 419 |
+
return self.neg()
|
| 420 |
+
|
| 421 |
+
@classmethod
|
| 422 |
+
def zeros(cls, shape, domain):
|
| 423 |
+
"""Return a zero DFM matrix."""
|
| 424 |
+
func = cls._get_flint_func(domain)
|
| 425 |
+
return cls._new(func(*shape), shape, domain)
|
| 426 |
+
|
| 427 |
+
# XXX: flint matrices do not have anything like ones or eye
|
| 428 |
+
# In the methods below we convert to DDM and then back to DFM which is
|
| 429 |
+
# probably about as efficient as implementing these methods directly.
|
| 430 |
+
|
| 431 |
+
@classmethod
|
| 432 |
+
def ones(cls, shape, domain):
|
| 433 |
+
"""Return a one DFM matrix."""
|
| 434 |
+
# XXX: flint matrices do not have anything like ones
|
| 435 |
+
return DDM.ones(shape, domain).to_dfm()
|
| 436 |
+
|
| 437 |
+
@classmethod
|
| 438 |
+
def eye(cls, n, domain):
|
| 439 |
+
"""Return the identity matrix of size n."""
|
| 440 |
+
# XXX: flint matrices do not have anything like eye
|
| 441 |
+
return DDM.eye(n, domain).to_dfm()
|
| 442 |
+
|
| 443 |
+
@classmethod
|
| 444 |
+
def diag(cls, elements, domain):
|
| 445 |
+
"""Return a diagonal matrix."""
|
| 446 |
+
return DDM.diag(elements, domain).to_dfm()
|
| 447 |
+
|
| 448 |
+
def applyfunc(self, func, domain):
|
| 449 |
+
"""Apply a function to each entry of a DFM matrix."""
|
| 450 |
+
return self.to_ddm().applyfunc(func, domain).to_dfm()
|
| 451 |
+
|
| 452 |
+
def transpose(self):
|
| 453 |
+
"""Transpose a DFM matrix."""
|
| 454 |
+
return self._new(self.rep.transpose(), (self.cols, self.rows), self.domain)
|
| 455 |
+
|
| 456 |
+
def hstack(self, *others):
|
| 457 |
+
"""Horizontally stack matrices."""
|
| 458 |
+
return self.to_ddm().hstack(*[o.to_ddm() for o in others]).to_dfm()
|
| 459 |
+
|
| 460 |
+
def vstack(self, *others):
|
| 461 |
+
"""Vertically stack matrices."""
|
| 462 |
+
return self.to_ddm().vstack(*[o.to_ddm() for o in others]).to_dfm()
|
| 463 |
+
|
| 464 |
+
def diagonal(self):
|
| 465 |
+
"""Return the diagonal of a DFM matrix."""
|
| 466 |
+
M = self.rep
|
| 467 |
+
m, n = self.shape
|
| 468 |
+
return [M[i, i] for i in range(min(m, n))]
|
| 469 |
+
|
| 470 |
+
def is_upper(self):
|
| 471 |
+
"""Return ``True`` if the matrix is upper triangular."""
|
| 472 |
+
M = self.rep
|
| 473 |
+
for i in range(self.rows):
|
| 474 |
+
for j in range(i):
|
| 475 |
+
if M[i, j]:
|
| 476 |
+
return False
|
| 477 |
+
return True
|
| 478 |
+
|
| 479 |
+
def is_lower(self):
|
| 480 |
+
"""Return ``True`` if the matrix is lower triangular."""
|
| 481 |
+
M = self.rep
|
| 482 |
+
for i in range(self.rows):
|
| 483 |
+
for j in range(i + 1, self.cols):
|
| 484 |
+
if M[i, j]:
|
| 485 |
+
return False
|
| 486 |
+
return True
|
| 487 |
+
|
| 488 |
+
def is_diagonal(self):
|
| 489 |
+
"""Return ``True`` if the matrix is diagonal."""
|
| 490 |
+
return self.is_upper() and self.is_lower()
|
| 491 |
+
|
| 492 |
+
def is_zero_matrix(self):
|
| 493 |
+
"""Return ``True`` if the matrix is the zero matrix."""
|
| 494 |
+
M = self.rep
|
| 495 |
+
for i in range(self.rows):
|
| 496 |
+
for j in range(self.cols):
|
| 497 |
+
if M[i, j]:
|
| 498 |
+
return False
|
| 499 |
+
return True
|
| 500 |
+
|
| 501 |
+
def nnz(self):
|
| 502 |
+
"""Return the number of non-zero elements in the matrix."""
|
| 503 |
+
return self.to_ddm().nnz()
|
| 504 |
+
|
| 505 |
+
def scc(self):
|
| 506 |
+
"""Return the strongly connected components of the matrix."""
|
| 507 |
+
return self.to_ddm().scc()
|
| 508 |
+
|
| 509 |
+
@doctest_depends_on(ground_types='flint')
|
| 510 |
+
def det(self):
|
| 511 |
+
"""
|
| 512 |
+
Compute the determinant of the matrix using FLINT.
|
| 513 |
+
|
| 514 |
+
Examples
|
| 515 |
+
========
|
| 516 |
+
|
| 517 |
+
>>> from sympy import Matrix
|
| 518 |
+
>>> M = Matrix([[1, 2], [3, 4]])
|
| 519 |
+
>>> dfm = M.to_DM().to_dfm()
|
| 520 |
+
>>> dfm
|
| 521 |
+
[[1, 2], [3, 4]]
|
| 522 |
+
>>> dfm.det()
|
| 523 |
+
-2
|
| 524 |
+
|
| 525 |
+
Notes
|
| 526 |
+
=====
|
| 527 |
+
|
| 528 |
+
Calls the ``.det()`` method of the underlying FLINT matrix.
|
| 529 |
+
|
| 530 |
+
For :ref:`ZZ` or :ref:`QQ` this calls ``fmpz_mat_det`` or
|
| 531 |
+
``fmpq_mat_det`` respectively.
|
| 532 |
+
|
| 533 |
+
At the time of writing the implementation of ``fmpz_mat_det`` uses one
|
| 534 |
+
of several algorithms depending on the size of the matrix and bit size
|
| 535 |
+
of the entries. The algorithms used are:
|
| 536 |
+
|
| 537 |
+
- Cofactor for very small (up to 4x4) matrices.
|
| 538 |
+
- Bareiss for small (up to 25x25) matrices.
|
| 539 |
+
- Modular algorithms for larger matrices (up to 60x60) or for larger
|
| 540 |
+
matrices with large bit sizes.
|
| 541 |
+
- Modular "accelerated" for larger matrices (60x60 upwards) if the bit
|
| 542 |
+
size is smaller than the dimensions of the matrix.
|
| 543 |
+
|
| 544 |
+
The implementation of ``fmpq_mat_det`` clears denominators from each
|
| 545 |
+
row (not the whole matrix) and then calls ``fmpz_mat_det`` and divides
|
| 546 |
+
by the product of the denominators.
|
| 547 |
+
|
| 548 |
+
See Also
|
| 549 |
+
========
|
| 550 |
+
|
| 551 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.det
|
| 552 |
+
Higher level interface to compute the determinant of a matrix.
|
| 553 |
+
"""
|
| 554 |
+
# XXX: At least the first three algorithms described above should also
|
| 555 |
+
# be implemented in the pure Python DDM and SDM classes which at the
|
| 556 |
+
# time of writng just use Bareiss for all matrices and domains.
|
| 557 |
+
# Probably in Python the thresholds would be different though.
|
| 558 |
+
return self.rep.det()
|
| 559 |
+
|
| 560 |
+
@doctest_depends_on(ground_types='flint')
|
| 561 |
+
def charpoly(self):
|
| 562 |
+
"""
|
| 563 |
+
Compute the characteristic polynomial of the matrix using FLINT.
|
| 564 |
+
|
| 565 |
+
Examples
|
| 566 |
+
========
|
| 567 |
+
|
| 568 |
+
>>> from sympy import Matrix
|
| 569 |
+
>>> M = Matrix([[1, 2], [3, 4]])
|
| 570 |
+
>>> dfm = M.to_DM().to_dfm() # need ground types = 'flint'
|
| 571 |
+
>>> dfm
|
| 572 |
+
[[1, 2], [3, 4]]
|
| 573 |
+
>>> dfm.charpoly()
|
| 574 |
+
[1, -5, -2]
|
| 575 |
+
|
| 576 |
+
Notes
|
| 577 |
+
=====
|
| 578 |
+
|
| 579 |
+
Calls the ``.charpoly()`` method of the underlying FLINT matrix.
|
| 580 |
+
|
| 581 |
+
For :ref:`ZZ` or :ref:`QQ` this calls ``fmpz_mat_charpoly`` or
|
| 582 |
+
``fmpq_mat_charpoly`` respectively.
|
| 583 |
+
|
| 584 |
+
At the time of writing the implementation of ``fmpq_mat_charpoly``
|
| 585 |
+
clears a denominator from the whole matrix and then calls
|
| 586 |
+
``fmpz_mat_charpoly``. The coefficients of the characteristic
|
| 587 |
+
polynomial are then multiplied by powers of the denominator.
|
| 588 |
+
|
| 589 |
+
The ``fmpz_mat_charpoly`` method uses a modular algorithm with CRT
|
| 590 |
+
reconstruction. The modular algorithm uses ``nmod_mat_charpoly`` which
|
| 591 |
+
uses Berkowitz for small matrices and non-prime moduli or otherwise
|
| 592 |
+
the Danilevsky method.
|
| 593 |
+
|
| 594 |
+
See Also
|
| 595 |
+
========
|
| 596 |
+
|
| 597 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.charpoly
|
| 598 |
+
Higher level interface to compute the characteristic polynomial of
|
| 599 |
+
a matrix.
|
| 600 |
+
"""
|
| 601 |
+
# FLINT polynomial coefficients are in reverse order compared to SymPy.
|
| 602 |
+
return self.rep.charpoly().coeffs()[::-1]
|
| 603 |
+
|
| 604 |
+
@doctest_depends_on(ground_types='flint')
|
| 605 |
+
def inv(self):
|
| 606 |
+
"""
|
| 607 |
+
Compute the inverse of a matrix using FLINT.
|
| 608 |
+
|
| 609 |
+
Examples
|
| 610 |
+
========
|
| 611 |
+
|
| 612 |
+
>>> from sympy import Matrix, QQ
|
| 613 |
+
>>> M = Matrix([[1, 2], [3, 4]])
|
| 614 |
+
>>> dfm = M.to_DM().to_dfm().convert_to(QQ)
|
| 615 |
+
>>> dfm
|
| 616 |
+
[[1, 2], [3, 4]]
|
| 617 |
+
>>> dfm.inv()
|
| 618 |
+
[[-2, 1], [3/2, -1/2]]
|
| 619 |
+
>>> dfm.matmul(dfm.inv())
|
| 620 |
+
[[1, 0], [0, 1]]
|
| 621 |
+
|
| 622 |
+
Notes
|
| 623 |
+
=====
|
| 624 |
+
|
| 625 |
+
Calls the ``.inv()`` method of the underlying FLINT matrix.
|
| 626 |
+
|
| 627 |
+
For now this will raise an error if the domain is :ref:`ZZ` but will
|
| 628 |
+
use the FLINT method for :ref:`QQ`.
|
| 629 |
+
|
| 630 |
+
The FLINT methods for :ref:`ZZ` and :ref:`QQ` are ``fmpz_mat_inv`` and
|
| 631 |
+
``fmpq_mat_inv`` respectively. The ``fmpz_mat_inv`` method computes an
|
| 632 |
+
inverse with denominator. This is implemented by calling
|
| 633 |
+
``fmpz_mat_solve`` (see notes in :meth:`lu_solve` about the algorithm).
|
| 634 |
+
|
| 635 |
+
The ``fmpq_mat_inv`` method clears denominators from each row and then
|
| 636 |
+
multiplies those into the rhs identity matrix before calling
|
| 637 |
+
``fmpz_mat_solve``.
|
| 638 |
+
|
| 639 |
+
See Also
|
| 640 |
+
========
|
| 641 |
+
|
| 642 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.inv
|
| 643 |
+
Higher level method for computing the inverse of a matrix.
|
| 644 |
+
"""
|
| 645 |
+
# TODO: Implement similar algorithms for DDM and SDM.
|
| 646 |
+
#
|
| 647 |
+
# XXX: The flint fmpz_mat and fmpq_mat inv methods both return fmpq_mat
|
| 648 |
+
# by default. The fmpz_mat method has an optional argument to return
|
| 649 |
+
# fmpz_mat instead for unimodular matrices.
|
| 650 |
+
#
|
| 651 |
+
# The convention in DomainMatrix is to raise an error if the matrix is
|
| 652 |
+
# not over a field regardless of whether the matrix is invertible over
|
| 653 |
+
# its domain or over any associated field. Maybe DomainMatrix.inv
|
| 654 |
+
# should be changed to always return a matrix over an associated field
|
| 655 |
+
# except with a unimodular argument for returning an inverse over a
|
| 656 |
+
# ring if possible.
|
| 657 |
+
#
|
| 658 |
+
# For now we follow the existing DomainMatrix convention...
|
| 659 |
+
K = self.domain
|
| 660 |
+
m, n = self.shape
|
| 661 |
+
|
| 662 |
+
if m != n:
|
| 663 |
+
raise DMNonSquareMatrixError("cannot invert a non-square matrix")
|
| 664 |
+
|
| 665 |
+
if K == ZZ:
|
| 666 |
+
raise DMDomainError("field expected, got %s" % K)
|
| 667 |
+
elif K == QQ:
|
| 668 |
+
try:
|
| 669 |
+
return self._new_rep(self.rep.inv())
|
| 670 |
+
except ZeroDivisionError:
|
| 671 |
+
raise DMNonInvertibleMatrixError("matrix is not invertible")
|
| 672 |
+
else:
|
| 673 |
+
# If more domains are added for DFM then we will need to consider
|
| 674 |
+
# what happens here.
|
| 675 |
+
raise NotImplementedError("DFM.inv() is not implemented for %s" % K)
|
| 676 |
+
|
| 677 |
+
def lu(self):
|
| 678 |
+
"""Return the LU decomposition of the matrix."""
|
| 679 |
+
L, U, swaps = self.to_ddm().lu()
|
| 680 |
+
return L.to_dfm(), U.to_dfm(), swaps
|
| 681 |
+
|
| 682 |
+
# XXX: The lu_solve function should be renamed to solve. Whether or not it
|
| 683 |
+
# uses an LU decomposition is an implementation detail. A method called
|
| 684 |
+
# lu_solve would make sense for a situation in which an LU decomposition is
|
| 685 |
+
# reused several times to solve iwth different rhs but that would imply a
|
| 686 |
+
# different call signature.
|
| 687 |
+
#
|
| 688 |
+
# The underlying python-flint method has an algorithm= argument so we could
|
| 689 |
+
# use that and have e.g. solve_lu and solve_modular or perhaps also a
|
| 690 |
+
# method= argument to choose between the two. Flint itself has more
|
| 691 |
+
# possible algorithms to choose from than are exposed by python-flint.
|
| 692 |
+
|
| 693 |
+
@doctest_depends_on(ground_types='flint')
|
| 694 |
+
def lu_solve(self, rhs):
|
| 695 |
+
"""
|
| 696 |
+
Solve a matrix equation using FLINT.
|
| 697 |
+
|
| 698 |
+
Examples
|
| 699 |
+
========
|
| 700 |
+
|
| 701 |
+
>>> from sympy import Matrix, QQ
|
| 702 |
+
>>> M = Matrix([[1, 2], [3, 4]])
|
| 703 |
+
>>> dfm = M.to_DM().to_dfm().convert_to(QQ)
|
| 704 |
+
>>> dfm
|
| 705 |
+
[[1, 2], [3, 4]]
|
| 706 |
+
>>> rhs = Matrix([1, 2]).to_DM().to_dfm().convert_to(QQ)
|
| 707 |
+
>>> dfm.lu_solve(rhs)
|
| 708 |
+
[[0], [1/2]]
|
| 709 |
+
|
| 710 |
+
Notes
|
| 711 |
+
=====
|
| 712 |
+
|
| 713 |
+
Calls the ``.solve()`` method of the underlying FLINT matrix.
|
| 714 |
+
|
| 715 |
+
For now this will raise an error if the domain is :ref:`ZZ` but will
|
| 716 |
+
use the FLINT method for :ref:`QQ`.
|
| 717 |
+
|
| 718 |
+
The FLINT methods for :ref:`ZZ` and :ref:`QQ` are ``fmpz_mat_solve``
|
| 719 |
+
and ``fmpq_mat_solve`` respectively. The ``fmpq_mat_solve`` method
|
| 720 |
+
uses one of two algorithms:
|
| 721 |
+
|
| 722 |
+
- For small matrices (<25 rows) it clears denominators between the
|
| 723 |
+
matrix and rhs and uses ``fmpz_mat_solve``.
|
| 724 |
+
- For larger matrices it uses ``fmpq_mat_solve_dixon`` which is a
|
| 725 |
+
modular approach with CRT reconstruction over :ref:`QQ`.
|
| 726 |
+
|
| 727 |
+
The ``fmpz_mat_solve`` method uses one of four algorithms:
|
| 728 |
+
|
| 729 |
+
- For very small (<= 3x3) matrices it uses a Cramer's rule.
|
| 730 |
+
- For small (<= 15x15) matrices it uses a fraction-free LU solve.
|
| 731 |
+
- Otherwise it uses either Dixon or another multimodular approach.
|
| 732 |
+
|
| 733 |
+
See Also
|
| 734 |
+
========
|
| 735 |
+
|
| 736 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.lu_solve
|
| 737 |
+
Higher level interface to solve a matrix equation.
|
| 738 |
+
"""
|
| 739 |
+
if not self.domain == rhs.domain:
|
| 740 |
+
raise DMDomainError("Domains must match: %s != %s" % (self.domain, rhs.domain))
|
| 741 |
+
|
| 742 |
+
# XXX: As for inv we should consider whether to return a matrix over
|
| 743 |
+
# over an associated field or attempt to find a solution in the ring.
|
| 744 |
+
# For now we follow the existing DomainMatrix convention...
|
| 745 |
+
if not self.domain.is_Field:
|
| 746 |
+
raise DMDomainError("Field expected, got %s" % self.domain)
|
| 747 |
+
|
| 748 |
+
m, n = self.shape
|
| 749 |
+
j, k = rhs.shape
|
| 750 |
+
if m != j:
|
| 751 |
+
raise DMShapeError("Matrix size mismatch: %s * %s vs %s * %s" % (m, n, j, k))
|
| 752 |
+
sol_shape = (n, k)
|
| 753 |
+
|
| 754 |
+
# XXX: The Flint solve method only handles square matrices. Probably
|
| 755 |
+
# Flint has functions that could be used to solve non-square systems
|
| 756 |
+
# but they are not exposed in python-flint yet. Alternatively we could
|
| 757 |
+
# put something here using the features that are available like rref.
|
| 758 |
+
if m != n:
|
| 759 |
+
return self.to_ddm().lu_solve(rhs.to_ddm()).to_dfm()
|
| 760 |
+
|
| 761 |
+
try:
|
| 762 |
+
sol = self.rep.solve(rhs.rep)
|
| 763 |
+
except ZeroDivisionError:
|
| 764 |
+
raise DMNonInvertibleMatrixError("Matrix det == 0; not invertible.")
|
| 765 |
+
|
| 766 |
+
return self._new(sol, sol_shape, self.domain)
|
| 767 |
+
|
| 768 |
+
def nullspace(self):
|
| 769 |
+
"""Return a basis for the nullspace of the matrix."""
|
| 770 |
+
# Code to compute nullspace using flint:
|
| 771 |
+
#
|
| 772 |
+
# V, nullity = self.rep.nullspace()
|
| 773 |
+
# V_dfm = self._new_rep(V)._extract(range(self.rows), range(nullity))
|
| 774 |
+
#
|
| 775 |
+
# XXX: That gives the nullspace but does not give us nonpivots. So we
|
| 776 |
+
# use the slower DDM method anyway. It would be better to change the
|
| 777 |
+
# signature of the nullspace method to not return nonpivots.
|
| 778 |
+
#
|
| 779 |
+
# XXX: Also python-flint exposes a nullspace method for fmpz_mat but
|
| 780 |
+
# not for fmpq_mat. This is the reverse of the situation for DDM etc
|
| 781 |
+
# which only allow nullspace over a field. The nullspace method for
|
| 782 |
+
# DDM, SDM etc should be changed to allow nullspace over ZZ as well.
|
| 783 |
+
# The DomainMatrix nullspace method does allow the domain to be a ring
|
| 784 |
+
# but does not directly call the lower-level nullspace methods and uses
|
| 785 |
+
# rref_den instead. Nullspace methods should also be added to all
|
| 786 |
+
# matrix types in python-flint.
|
| 787 |
+
ddm, nonpivots = self.to_ddm().nullspace()
|
| 788 |
+
return ddm.to_dfm(), nonpivots
|
| 789 |
+
|
| 790 |
+
def nullspace_from_rref(self, pivots=None):
|
| 791 |
+
"""Return a basis for the nullspace of the matrix."""
|
| 792 |
+
# XXX: Use the flint nullspace method!!!
|
| 793 |
+
sdm, nonpivots = self.to_sdm().nullspace_from_rref(pivots=pivots)
|
| 794 |
+
return sdm.to_dfm(), nonpivots
|
| 795 |
+
|
| 796 |
+
def particular(self):
|
| 797 |
+
"""Return a particular solution to the system."""
|
| 798 |
+
return self.to_ddm().particular().to_dfm()
|
| 799 |
+
|
| 800 |
+
def _lll(self, transform=False, delta=0.99, eta=0.51, rep='zbasis', gram='approx'):
|
| 801 |
+
"""Call the fmpz_mat.lll() method but check rank to avoid segfaults."""
|
| 802 |
+
|
| 803 |
+
# XXX: There are tests that pass e.g. QQ(5,6) for delta. That fails
|
| 804 |
+
# with a TypeError in flint because if QQ is fmpq then conversion with
|
| 805 |
+
# float fails. We handle that here but there are two better fixes:
|
| 806 |
+
#
|
| 807 |
+
# - Make python-flint's fmpq convert with float(x)
|
| 808 |
+
# - Change the tests because delta should just be a float.
|
| 809 |
+
|
| 810 |
+
def to_float(x):
|
| 811 |
+
if QQ.of_type(x):
|
| 812 |
+
return float(x.numerator) / float(x.denominator)
|
| 813 |
+
else:
|
| 814 |
+
return float(x)
|
| 815 |
+
|
| 816 |
+
delta = to_float(delta)
|
| 817 |
+
eta = to_float(eta)
|
| 818 |
+
|
| 819 |
+
if not 0.25 < delta < 1:
|
| 820 |
+
raise DMValueError("delta must be between 0.25 and 1")
|
| 821 |
+
|
| 822 |
+
# XXX: The flint lll method segfaults if the matrix is not full rank.
|
| 823 |
+
m, n = self.shape
|
| 824 |
+
if self.rep.rank() != m:
|
| 825 |
+
raise DMRankError("Matrix must have full row rank for Flint LLL.")
|
| 826 |
+
|
| 827 |
+
# Actually call the flint method.
|
| 828 |
+
return self.rep.lll(transform=transform, delta=delta, eta=eta, rep=rep, gram=gram)
|
| 829 |
+
|
| 830 |
+
@doctest_depends_on(ground_types='flint')
|
| 831 |
+
def lll(self, delta=0.75):
|
| 832 |
+
"""Compute LLL-reduced basis using FLINT.
|
| 833 |
+
|
| 834 |
+
See :meth:`lll_transform` for more information.
|
| 835 |
+
|
| 836 |
+
Examples
|
| 837 |
+
========
|
| 838 |
+
|
| 839 |
+
>>> from sympy import Matrix
|
| 840 |
+
>>> M = Matrix([[1, 2, 3], [4, 5, 6]])
|
| 841 |
+
>>> M.to_DM().to_dfm().lll()
|
| 842 |
+
[[2, 1, 0], [-1, 1, 3]]
|
| 843 |
+
|
| 844 |
+
See Also
|
| 845 |
+
========
|
| 846 |
+
|
| 847 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.lll
|
| 848 |
+
Higher level interface to compute LLL-reduced basis.
|
| 849 |
+
lll_transform
|
| 850 |
+
Compute LLL-reduced basis and transform matrix.
|
| 851 |
+
"""
|
| 852 |
+
if self.domain != ZZ:
|
| 853 |
+
raise DMDomainError("ZZ expected, got %s" % self.domain)
|
| 854 |
+
elif self.rows > self.cols:
|
| 855 |
+
raise DMShapeError("Matrix must not have more rows than columns.")
|
| 856 |
+
|
| 857 |
+
rep = self._lll(delta=delta)
|
| 858 |
+
return self._new_rep(rep)
|
| 859 |
+
|
| 860 |
+
@doctest_depends_on(ground_types='flint')
|
| 861 |
+
def lll_transform(self, delta=0.75):
|
| 862 |
+
"""Compute LLL-reduced basis and transform using FLINT.
|
| 863 |
+
|
| 864 |
+
Examples
|
| 865 |
+
========
|
| 866 |
+
|
| 867 |
+
>>> from sympy import Matrix
|
| 868 |
+
>>> M = Matrix([[1, 2, 3], [4, 5, 6]]).to_DM().to_dfm()
|
| 869 |
+
>>> M_lll, T = M.lll_transform()
|
| 870 |
+
>>> M_lll
|
| 871 |
+
[[2, 1, 0], [-1, 1, 3]]
|
| 872 |
+
>>> T
|
| 873 |
+
[[-2, 1], [3, -1]]
|
| 874 |
+
>>> T.matmul(M) == M_lll
|
| 875 |
+
True
|
| 876 |
+
|
| 877 |
+
See Also
|
| 878 |
+
========
|
| 879 |
+
|
| 880 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.lll
|
| 881 |
+
Higher level interface to compute LLL-reduced basis.
|
| 882 |
+
lll
|
| 883 |
+
Compute LLL-reduced basis without transform matrix.
|
| 884 |
+
"""
|
| 885 |
+
if self.domain != ZZ:
|
| 886 |
+
raise DMDomainError("ZZ expected, got %s" % self.domain)
|
| 887 |
+
elif self.rows > self.cols:
|
| 888 |
+
raise DMShapeError("Matrix must not have more rows than columns.")
|
| 889 |
+
|
| 890 |
+
rep, T = self._lll(transform=True, delta=delta)
|
| 891 |
+
basis = self._new_rep(rep)
|
| 892 |
+
T_dfm = self._new(T, (self.rows, self.rows), self.domain)
|
| 893 |
+
return basis, T_dfm
|
| 894 |
+
|
| 895 |
+
|
| 896 |
+
# Avoid circular imports
|
| 897 |
+
from sympy.polys.matrices.ddm import DDM
|
| 898 |
+
from sympy.polys.matrices.ddm import SDM
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/_typing.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import TypeVar, Protocol
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
T = TypeVar('T')
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class RingElement(Protocol):
|
| 8 |
+
"""A ring element.
|
| 9 |
+
|
| 10 |
+
Must support ``+``, ``-``, ``*``, ``**`` and ``-``.
|
| 11 |
+
"""
|
| 12 |
+
def __add__(self: T, other: T, /) -> T: ...
|
| 13 |
+
def __sub__(self: T, other: T, /) -> T: ...
|
| 14 |
+
def __mul__(self: T, other: T, /) -> T: ...
|
| 15 |
+
def __pow__(self: T, other: int, /) -> T: ...
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
|
| 3 |
+
Module for the DDM class.
|
| 4 |
+
|
| 5 |
+
The DDM class is an internal representation used by DomainMatrix. The letters
|
| 6 |
+
DDM stand for Dense Domain Matrix. A DDM instance represents a matrix using
|
| 7 |
+
elements from a polynomial Domain (e.g. ZZ, QQ, ...) in a dense-matrix
|
| 8 |
+
representation.
|
| 9 |
+
|
| 10 |
+
Basic usage:
|
| 11 |
+
|
| 12 |
+
>>> from sympy import ZZ, QQ
|
| 13 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 14 |
+
>>> A = DDM([[ZZ(0), ZZ(1)], [ZZ(-1), ZZ(0)]], (2, 2), ZZ)
|
| 15 |
+
>>> A.shape
|
| 16 |
+
(2, 2)
|
| 17 |
+
>>> A
|
| 18 |
+
[[0, 1], [-1, 0]]
|
| 19 |
+
>>> type(A)
|
| 20 |
+
<class 'sympy.polys.matrices.ddm.DDM'>
|
| 21 |
+
>>> A @ A
|
| 22 |
+
[[-1, 0], [0, -1]]
|
| 23 |
+
|
| 24 |
+
The ddm_* functions are designed to operate on DDM as well as on an ordinary
|
| 25 |
+
list of lists:
|
| 26 |
+
|
| 27 |
+
>>> from sympy.polys.matrices.dense import ddm_idet
|
| 28 |
+
>>> ddm_idet(A, QQ)
|
| 29 |
+
1
|
| 30 |
+
>>> ddm_idet([[0, 1], [-1, 0]], QQ)
|
| 31 |
+
1
|
| 32 |
+
>>> A
|
| 33 |
+
[[-1, 0], [0, -1]]
|
| 34 |
+
|
| 35 |
+
Note that ddm_idet modifies the input matrix in-place. It is recommended to
|
| 36 |
+
use the DDM.det method as a friendlier interface to this instead which takes
|
| 37 |
+
care of copying the matrix:
|
| 38 |
+
|
| 39 |
+
>>> B = DDM([[ZZ(0), ZZ(1)], [ZZ(-1), ZZ(0)]], (2, 2), ZZ)
|
| 40 |
+
>>> B.det()
|
| 41 |
+
1
|
| 42 |
+
|
| 43 |
+
Normally DDM would not be used directly and is just part of the internal
|
| 44 |
+
representation of DomainMatrix which adds further functionality including e.g.
|
| 45 |
+
unifying domains.
|
| 46 |
+
|
| 47 |
+
The dense format used by DDM is a list of lists of elements e.g. the 2x2
|
| 48 |
+
identity matrix is like [[1, 0], [0, 1]]. The DDM class itself is a subclass
|
| 49 |
+
of list and its list items are plain lists. Elements are accessed as e.g.
|
| 50 |
+
ddm[i][j] where ddm[i] gives the ith row and ddm[i][j] gets the element in the
|
| 51 |
+
jth column of that row. Subclassing list makes e.g. iteration and indexing
|
| 52 |
+
very efficient. We do not override __getitem__ because it would lose that
|
| 53 |
+
benefit.
|
| 54 |
+
|
| 55 |
+
The core routines are implemented by the ddm_* functions defined in dense.py.
|
| 56 |
+
Those functions are intended to be able to operate on a raw list-of-lists
|
| 57 |
+
representation of matrices with most functions operating in-place. The DDM
|
| 58 |
+
class takes care of copying etc and also stores a Domain object associated
|
| 59 |
+
with its elements. This makes it possible to implement things like A + B with
|
| 60 |
+
domain checking and also shape checking so that the list of lists
|
| 61 |
+
representation is friendlier.
|
| 62 |
+
|
| 63 |
+
"""
|
| 64 |
+
from itertools import chain
|
| 65 |
+
|
| 66 |
+
from sympy.external.gmpy import GROUND_TYPES
|
| 67 |
+
from sympy.utilities.decorator import doctest_depends_on
|
| 68 |
+
|
| 69 |
+
from .exceptions import (
|
| 70 |
+
DMBadInputError,
|
| 71 |
+
DMDomainError,
|
| 72 |
+
DMNonSquareMatrixError,
|
| 73 |
+
DMShapeError,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
from sympy.polys.domains import QQ
|
| 77 |
+
|
| 78 |
+
from .dense import (
|
| 79 |
+
ddm_transpose,
|
| 80 |
+
ddm_iadd,
|
| 81 |
+
ddm_isub,
|
| 82 |
+
ddm_ineg,
|
| 83 |
+
ddm_imul,
|
| 84 |
+
ddm_irmul,
|
| 85 |
+
ddm_imatmul,
|
| 86 |
+
ddm_irref,
|
| 87 |
+
ddm_irref_den,
|
| 88 |
+
ddm_idet,
|
| 89 |
+
ddm_iinv,
|
| 90 |
+
ddm_ilu_split,
|
| 91 |
+
ddm_ilu_solve,
|
| 92 |
+
ddm_berk,
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
from .lll import ddm_lll, ddm_lll_transform
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
if GROUND_TYPES != 'flint':
|
| 99 |
+
__doctest_skip__ = ['DDM.to_dfm', 'DDM.to_dfm_or_ddm']
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class DDM(list):
|
| 103 |
+
"""Dense matrix based on polys domain elements
|
| 104 |
+
|
| 105 |
+
This is a list subclass and is a wrapper for a list of lists that supports
|
| 106 |
+
basic matrix arithmetic +, -, *, **.
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
fmt = 'dense'
|
| 110 |
+
is_DFM = False
|
| 111 |
+
is_DDM = True
|
| 112 |
+
|
| 113 |
+
def __init__(self, rowslist, shape, domain):
|
| 114 |
+
if not (isinstance(rowslist, list) and all(type(row) is list for row in rowslist)):
|
| 115 |
+
raise DMBadInputError("rowslist must be a list of lists")
|
| 116 |
+
m, n = shape
|
| 117 |
+
if len(rowslist) != m or any(len(row) != n for row in rowslist):
|
| 118 |
+
raise DMBadInputError("Inconsistent row-list/shape")
|
| 119 |
+
|
| 120 |
+
super().__init__(rowslist)
|
| 121 |
+
self.shape = (m, n)
|
| 122 |
+
self.rows = m
|
| 123 |
+
self.cols = n
|
| 124 |
+
self.domain = domain
|
| 125 |
+
|
| 126 |
+
def getitem(self, i, j):
|
| 127 |
+
return self[i][j]
|
| 128 |
+
|
| 129 |
+
def setitem(self, i, j, value):
|
| 130 |
+
self[i][j] = value
|
| 131 |
+
|
| 132 |
+
def extract_slice(self, slice1, slice2):
|
| 133 |
+
ddm = [row[slice2] for row in self[slice1]]
|
| 134 |
+
rows = len(ddm)
|
| 135 |
+
cols = len(ddm[0]) if ddm else len(range(self.shape[1])[slice2])
|
| 136 |
+
return DDM(ddm, (rows, cols), self.domain)
|
| 137 |
+
|
| 138 |
+
def extract(self, rows, cols):
|
| 139 |
+
ddm = []
|
| 140 |
+
for i in rows:
|
| 141 |
+
rowi = self[i]
|
| 142 |
+
ddm.append([rowi[j] for j in cols])
|
| 143 |
+
return DDM(ddm, (len(rows), len(cols)), self.domain)
|
| 144 |
+
|
| 145 |
+
@classmethod
|
| 146 |
+
def from_list(cls, rowslist, shape, domain):
|
| 147 |
+
"""
|
| 148 |
+
Create a :class:`DDM` from a list of lists.
|
| 149 |
+
|
| 150 |
+
Examples
|
| 151 |
+
========
|
| 152 |
+
|
| 153 |
+
>>> from sympy import ZZ
|
| 154 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 155 |
+
>>> A = DDM.from_list([[ZZ(0), ZZ(1)], [ZZ(-1), ZZ(0)]], (2, 2), ZZ)
|
| 156 |
+
>>> A
|
| 157 |
+
[[0, 1], [-1, 0]]
|
| 158 |
+
>>> A == DDM([[ZZ(0), ZZ(1)], [ZZ(-1), ZZ(0)]], (2, 2), ZZ)
|
| 159 |
+
True
|
| 160 |
+
|
| 161 |
+
See Also
|
| 162 |
+
========
|
| 163 |
+
|
| 164 |
+
from_list_flat
|
| 165 |
+
"""
|
| 166 |
+
return cls(rowslist, shape, domain)
|
| 167 |
+
|
| 168 |
+
@classmethod
|
| 169 |
+
def from_ddm(cls, other):
|
| 170 |
+
return other.copy()
|
| 171 |
+
|
| 172 |
+
def to_list(self):
|
| 173 |
+
"""
|
| 174 |
+
Convert to a list of lists.
|
| 175 |
+
|
| 176 |
+
Examples
|
| 177 |
+
========
|
| 178 |
+
|
| 179 |
+
>>> from sympy import QQ
|
| 180 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 181 |
+
>>> A = DDM([[1, 2], [3, 4]], (2, 2), QQ)
|
| 182 |
+
>>> A.to_list()
|
| 183 |
+
[[1, 2], [3, 4]]
|
| 184 |
+
|
| 185 |
+
See Also
|
| 186 |
+
========
|
| 187 |
+
|
| 188 |
+
to_list_flat
|
| 189 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.to_list
|
| 190 |
+
"""
|
| 191 |
+
return list(self)
|
| 192 |
+
|
| 193 |
+
def to_list_flat(self):
|
| 194 |
+
"""
|
| 195 |
+
Convert to a flat list of elements.
|
| 196 |
+
|
| 197 |
+
Examples
|
| 198 |
+
========
|
| 199 |
+
|
| 200 |
+
>>> from sympy import QQ
|
| 201 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 202 |
+
>>> A = DDM([[1, 2], [3, 4]], (2, 2), QQ)
|
| 203 |
+
>>> A.to_list_flat()
|
| 204 |
+
[1, 2, 3, 4]
|
| 205 |
+
>>> A == DDM.from_list_flat(A.to_list_flat(), A.shape, A.domain)
|
| 206 |
+
True
|
| 207 |
+
|
| 208 |
+
See Also
|
| 209 |
+
========
|
| 210 |
+
|
| 211 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.to_list_flat
|
| 212 |
+
"""
|
| 213 |
+
flat = []
|
| 214 |
+
for row in self:
|
| 215 |
+
flat.extend(row)
|
| 216 |
+
return flat
|
| 217 |
+
|
| 218 |
+
@classmethod
|
| 219 |
+
def from_list_flat(cls, flat, shape, domain):
|
| 220 |
+
"""
|
| 221 |
+
Create a :class:`DDM` from a flat list of elements.
|
| 222 |
+
|
| 223 |
+
Examples
|
| 224 |
+
========
|
| 225 |
+
|
| 226 |
+
>>> from sympy import QQ
|
| 227 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 228 |
+
>>> A = DDM.from_list_flat([1, 2, 3, 4], (2, 2), QQ)
|
| 229 |
+
>>> A
|
| 230 |
+
[[1, 2], [3, 4]]
|
| 231 |
+
>>> A == DDM.from_list_flat(A.to_list_flat(), A.shape, A.domain)
|
| 232 |
+
True
|
| 233 |
+
|
| 234 |
+
See Also
|
| 235 |
+
========
|
| 236 |
+
|
| 237 |
+
to_list_flat
|
| 238 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.from_list_flat
|
| 239 |
+
"""
|
| 240 |
+
assert type(flat) is list
|
| 241 |
+
rows, cols = shape
|
| 242 |
+
if not (len(flat) == rows*cols):
|
| 243 |
+
raise DMBadInputError("Inconsistent flat-list shape")
|
| 244 |
+
lol = [flat[i*cols:(i+1)*cols] for i in range(rows)]
|
| 245 |
+
return cls(lol, shape, domain)
|
| 246 |
+
|
| 247 |
+
def flatiter(self):
|
| 248 |
+
return chain.from_iterable(self)
|
| 249 |
+
|
| 250 |
+
def flat(self):
|
| 251 |
+
items = []
|
| 252 |
+
for row in self:
|
| 253 |
+
items.extend(row)
|
| 254 |
+
return items
|
| 255 |
+
|
| 256 |
+
def to_flat_nz(self):
|
| 257 |
+
"""
|
| 258 |
+
Convert to a flat list of nonzero elements and data.
|
| 259 |
+
|
| 260 |
+
Explanation
|
| 261 |
+
===========
|
| 262 |
+
|
| 263 |
+
This is used to operate on a list of the elements of a matrix and then
|
| 264 |
+
reconstruct a matrix using :meth:`from_flat_nz`. Zero elements are
|
| 265 |
+
included in the list but that may change in the future.
|
| 266 |
+
|
| 267 |
+
Examples
|
| 268 |
+
========
|
| 269 |
+
|
| 270 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 271 |
+
>>> from sympy import QQ
|
| 272 |
+
>>> A = DDM([[1, 2], [3, 4]], (2, 2), QQ)
|
| 273 |
+
>>> elements, data = A.to_flat_nz()
|
| 274 |
+
>>> elements
|
| 275 |
+
[1, 2, 3, 4]
|
| 276 |
+
>>> A == DDM.from_flat_nz(elements, data, A.domain)
|
| 277 |
+
True
|
| 278 |
+
|
| 279 |
+
See Also
|
| 280 |
+
========
|
| 281 |
+
|
| 282 |
+
from_flat_nz
|
| 283 |
+
sympy.polys.matrices.sdm.SDM.to_flat_nz
|
| 284 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.to_flat_nz
|
| 285 |
+
"""
|
| 286 |
+
return self.to_sdm().to_flat_nz()
|
| 287 |
+
|
| 288 |
+
@classmethod
|
| 289 |
+
def from_flat_nz(cls, elements, data, domain):
|
| 290 |
+
"""
|
| 291 |
+
Reconstruct a :class:`DDM` after calling :meth:`to_flat_nz`.
|
| 292 |
+
|
| 293 |
+
Examples
|
| 294 |
+
========
|
| 295 |
+
|
| 296 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 297 |
+
>>> from sympy import QQ
|
| 298 |
+
>>> A = DDM([[1, 2], [3, 4]], (2, 2), QQ)
|
| 299 |
+
>>> elements, data = A.to_flat_nz()
|
| 300 |
+
>>> elements
|
| 301 |
+
[1, 2, 3, 4]
|
| 302 |
+
>>> A == DDM.from_flat_nz(elements, data, A.domain)
|
| 303 |
+
True
|
| 304 |
+
|
| 305 |
+
See Also
|
| 306 |
+
========
|
| 307 |
+
|
| 308 |
+
to_flat_nz
|
| 309 |
+
sympy.polys.matrices.sdm.SDM.from_flat_nz
|
| 310 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.from_flat_nz
|
| 311 |
+
"""
|
| 312 |
+
return SDM.from_flat_nz(elements, data, domain).to_ddm()
|
| 313 |
+
|
| 314 |
+
def to_dod(self):
|
| 315 |
+
"""
|
| 316 |
+
Convert to a dictionary of dictionaries (dod) format.
|
| 317 |
+
|
| 318 |
+
Examples
|
| 319 |
+
========
|
| 320 |
+
|
| 321 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 322 |
+
>>> from sympy import QQ
|
| 323 |
+
>>> A = DDM([[1, 2], [3, 4]], (2, 2), QQ)
|
| 324 |
+
>>> A.to_dod()
|
| 325 |
+
{0: {0: 1, 1: 2}, 1: {0: 3, 1: 4}}
|
| 326 |
+
|
| 327 |
+
See Also
|
| 328 |
+
========
|
| 329 |
+
|
| 330 |
+
from_dod
|
| 331 |
+
sympy.polys.matrices.sdm.SDM.to_dod
|
| 332 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.to_dod
|
| 333 |
+
"""
|
| 334 |
+
dod = {}
|
| 335 |
+
for i, row in enumerate(self):
|
| 336 |
+
row = {j:e for j, e in enumerate(row) if e}
|
| 337 |
+
if row:
|
| 338 |
+
dod[i] = row
|
| 339 |
+
return dod
|
| 340 |
+
|
| 341 |
+
@classmethod
|
| 342 |
+
def from_dod(cls, dod, shape, domain):
|
| 343 |
+
"""
|
| 344 |
+
Create a :class:`DDM` from a dictionary of dictionaries (dod) format.
|
| 345 |
+
|
| 346 |
+
Examples
|
| 347 |
+
========
|
| 348 |
+
|
| 349 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 350 |
+
>>> from sympy import QQ
|
| 351 |
+
>>> dod = {0: {0: 1, 1: 2}, 1: {0: 3, 1: 4}}
|
| 352 |
+
>>> A = DDM.from_dod(dod, (2, 2), QQ)
|
| 353 |
+
>>> A
|
| 354 |
+
[[1, 2], [3, 4]]
|
| 355 |
+
|
| 356 |
+
See Also
|
| 357 |
+
========
|
| 358 |
+
|
| 359 |
+
to_dod
|
| 360 |
+
sympy.polys.matrices.sdm.SDM.from_dod
|
| 361 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.from_dod
|
| 362 |
+
"""
|
| 363 |
+
rows, cols = shape
|
| 364 |
+
lol = [[domain.zero] * cols for _ in range(rows)]
|
| 365 |
+
for i, row in dod.items():
|
| 366 |
+
for j, element in row.items():
|
| 367 |
+
lol[i][j] = element
|
| 368 |
+
return DDM(lol, shape, domain)
|
| 369 |
+
|
| 370 |
+
def to_dok(self):
|
| 371 |
+
"""
|
| 372 |
+
Convert :class:`DDM` to dictionary of keys (dok) format.
|
| 373 |
+
|
| 374 |
+
Examples
|
| 375 |
+
========
|
| 376 |
+
|
| 377 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 378 |
+
>>> from sympy import QQ
|
| 379 |
+
>>> A = DDM([[1, 2], [3, 4]], (2, 2), QQ)
|
| 380 |
+
>>> A.to_dok()
|
| 381 |
+
{(0, 0): 1, (0, 1): 2, (1, 0): 3, (1, 1): 4}
|
| 382 |
+
|
| 383 |
+
See Also
|
| 384 |
+
========
|
| 385 |
+
|
| 386 |
+
from_dok
|
| 387 |
+
sympy.polys.matrices.sdm.SDM.to_dok
|
| 388 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.to_dok
|
| 389 |
+
"""
|
| 390 |
+
dok = {}
|
| 391 |
+
for i, row in enumerate(self):
|
| 392 |
+
for j, element in enumerate(row):
|
| 393 |
+
if element:
|
| 394 |
+
dok[i, j] = element
|
| 395 |
+
return dok
|
| 396 |
+
|
| 397 |
+
@classmethod
|
| 398 |
+
def from_dok(cls, dok, shape, domain):
|
| 399 |
+
"""
|
| 400 |
+
Create a :class:`DDM` from a dictionary of keys (dok) format.
|
| 401 |
+
|
| 402 |
+
Examples
|
| 403 |
+
========
|
| 404 |
+
|
| 405 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 406 |
+
>>> from sympy import QQ
|
| 407 |
+
>>> dok = {(0, 0): 1, (0, 1): 2, (1, 0): 3, (1, 1): 4}
|
| 408 |
+
>>> A = DDM.from_dok(dok, (2, 2), QQ)
|
| 409 |
+
>>> A
|
| 410 |
+
[[1, 2], [3, 4]]
|
| 411 |
+
|
| 412 |
+
See Also
|
| 413 |
+
========
|
| 414 |
+
|
| 415 |
+
to_dok
|
| 416 |
+
sympy.polys.matrices.sdm.SDM.from_dok
|
| 417 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.from_dok
|
| 418 |
+
"""
|
| 419 |
+
rows, cols = shape
|
| 420 |
+
lol = [[domain.zero] * cols for _ in range(rows)]
|
| 421 |
+
for (i, j), element in dok.items():
|
| 422 |
+
lol[i][j] = element
|
| 423 |
+
return DDM(lol, shape, domain)
|
| 424 |
+
|
| 425 |
+
def iter_values(self):
|
| 426 |
+
"""
|
| 427 |
+
Iterater over the non-zero values of the matrix.
|
| 428 |
+
|
| 429 |
+
Examples
|
| 430 |
+
========
|
| 431 |
+
|
| 432 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 433 |
+
>>> from sympy import QQ
|
| 434 |
+
>>> A = DDM([[QQ(1), QQ(0)], [QQ(3), QQ(4)]], (2, 2), QQ)
|
| 435 |
+
>>> list(A.iter_values())
|
| 436 |
+
[1, 3, 4]
|
| 437 |
+
|
| 438 |
+
See Also
|
| 439 |
+
========
|
| 440 |
+
|
| 441 |
+
iter_items
|
| 442 |
+
to_list_flat
|
| 443 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.iter_values
|
| 444 |
+
"""
|
| 445 |
+
for row in self:
|
| 446 |
+
yield from filter(None, row)
|
| 447 |
+
|
| 448 |
+
def iter_items(self):
|
| 449 |
+
"""
|
| 450 |
+
Iterate over indices and values of nonzero elements of the matrix.
|
| 451 |
+
|
| 452 |
+
Examples
|
| 453 |
+
========
|
| 454 |
+
|
| 455 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 456 |
+
>>> from sympy import QQ
|
| 457 |
+
>>> A = DDM([[QQ(1), QQ(0)], [QQ(3), QQ(4)]], (2, 2), QQ)
|
| 458 |
+
>>> list(A.iter_items())
|
| 459 |
+
[((0, 0), 1), ((1, 0), 3), ((1, 1), 4)]
|
| 460 |
+
|
| 461 |
+
See Also
|
| 462 |
+
========
|
| 463 |
+
|
| 464 |
+
iter_values
|
| 465 |
+
to_dok
|
| 466 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.iter_items
|
| 467 |
+
"""
|
| 468 |
+
for i, row in enumerate(self):
|
| 469 |
+
for j, element in enumerate(row):
|
| 470 |
+
if element:
|
| 471 |
+
yield (i, j), element
|
| 472 |
+
|
| 473 |
+
def to_ddm(self):
|
| 474 |
+
"""
|
| 475 |
+
Convert to a :class:`DDM`.
|
| 476 |
+
|
| 477 |
+
This just returns ``self`` but exists to parallel the corresponding
|
| 478 |
+
method in other matrix types like :class:`~.SDM`.
|
| 479 |
+
|
| 480 |
+
See Also
|
| 481 |
+
========
|
| 482 |
+
|
| 483 |
+
to_sdm
|
| 484 |
+
to_dfm
|
| 485 |
+
to_dfm_or_ddm
|
| 486 |
+
sympy.polys.matrices.sdm.SDM.to_ddm
|
| 487 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.to_ddm
|
| 488 |
+
"""
|
| 489 |
+
return self
|
| 490 |
+
|
| 491 |
+
def to_sdm(self):
|
| 492 |
+
"""
|
| 493 |
+
Convert to a :class:`~.SDM`.
|
| 494 |
+
|
| 495 |
+
Examples
|
| 496 |
+
========
|
| 497 |
+
|
| 498 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 499 |
+
>>> from sympy import QQ
|
| 500 |
+
>>> A = DDM([[1, 2], [3, 4]], (2, 2), QQ)
|
| 501 |
+
>>> A.to_sdm()
|
| 502 |
+
{0: {0: 1, 1: 2}, 1: {0: 3, 1: 4}}
|
| 503 |
+
>>> type(A.to_sdm())
|
| 504 |
+
<class 'sympy.polys.matrices.sdm.SDM'>
|
| 505 |
+
|
| 506 |
+
See Also
|
| 507 |
+
========
|
| 508 |
+
|
| 509 |
+
SDM
|
| 510 |
+
sympy.polys.matrices.sdm.SDM.to_ddm
|
| 511 |
+
"""
|
| 512 |
+
return SDM.from_list(self, self.shape, self.domain)
|
| 513 |
+
|
| 514 |
+
@doctest_depends_on(ground_types=['flint'])
|
| 515 |
+
def to_dfm(self):
|
| 516 |
+
"""
|
| 517 |
+
Convert to :class:`~.DDM` to :class:`~.DFM`.
|
| 518 |
+
|
| 519 |
+
Examples
|
| 520 |
+
========
|
| 521 |
+
|
| 522 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 523 |
+
>>> from sympy import QQ
|
| 524 |
+
>>> A = DDM([[1, 2], [3, 4]], (2, 2), QQ)
|
| 525 |
+
>>> A.to_dfm()
|
| 526 |
+
[[1, 2], [3, 4]]
|
| 527 |
+
>>> type(A.to_dfm())
|
| 528 |
+
<class 'sympy.polys.matrices._dfm.DFM'>
|
| 529 |
+
|
| 530 |
+
See Also
|
| 531 |
+
========
|
| 532 |
+
|
| 533 |
+
DFM
|
| 534 |
+
sympy.polys.matrices._dfm.DFM.to_ddm
|
| 535 |
+
"""
|
| 536 |
+
return DFM(list(self), self.shape, self.domain)
|
| 537 |
+
|
| 538 |
+
@doctest_depends_on(ground_types=['flint'])
|
| 539 |
+
def to_dfm_or_ddm(self):
|
| 540 |
+
"""
|
| 541 |
+
Convert to :class:`~.DFM` if possible or otherwise return self.
|
| 542 |
+
|
| 543 |
+
Examples
|
| 544 |
+
========
|
| 545 |
+
|
| 546 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 547 |
+
>>> from sympy import QQ
|
| 548 |
+
>>> A = DDM([[1, 2], [3, 4]], (2, 2), QQ)
|
| 549 |
+
>>> A.to_dfm_or_ddm()
|
| 550 |
+
[[1, 2], [3, 4]]
|
| 551 |
+
>>> type(A.to_dfm_or_ddm())
|
| 552 |
+
<class 'sympy.polys.matrices._dfm.DFM'>
|
| 553 |
+
|
| 554 |
+
See Also
|
| 555 |
+
========
|
| 556 |
+
|
| 557 |
+
to_dfm
|
| 558 |
+
to_ddm
|
| 559 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.to_dfm_or_ddm
|
| 560 |
+
"""
|
| 561 |
+
if DFM._supports_domain(self.domain):
|
| 562 |
+
return self.to_dfm()
|
| 563 |
+
return self
|
| 564 |
+
|
| 565 |
+
def convert_to(self, K):
|
| 566 |
+
Kold = self.domain
|
| 567 |
+
if K == Kold:
|
| 568 |
+
return self.copy()
|
| 569 |
+
rows = [[K.convert_from(e, Kold) for e in row] for row in self]
|
| 570 |
+
return DDM(rows, self.shape, K)
|
| 571 |
+
|
| 572 |
+
def __str__(self):
|
| 573 |
+
rowsstr = ['[%s]' % ', '.join(map(str, row)) for row in self]
|
| 574 |
+
return '[%s]' % ', '.join(rowsstr)
|
| 575 |
+
|
| 576 |
+
def __repr__(self):
|
| 577 |
+
cls = type(self).__name__
|
| 578 |
+
rows = list.__repr__(self)
|
| 579 |
+
return '%s(%s, %s, %s)' % (cls, rows, self.shape, self.domain)
|
| 580 |
+
|
| 581 |
+
def __eq__(self, other):
|
| 582 |
+
if not isinstance(other, DDM):
|
| 583 |
+
return False
|
| 584 |
+
return (super().__eq__(other) and self.domain == other.domain)
|
| 585 |
+
|
| 586 |
+
def __ne__(self, other):
|
| 587 |
+
return not self.__eq__(other)
|
| 588 |
+
|
| 589 |
+
@classmethod
|
| 590 |
+
def zeros(cls, shape, domain):
|
| 591 |
+
z = domain.zero
|
| 592 |
+
m, n = shape
|
| 593 |
+
rowslist = [[z] * n for _ in range(m)]
|
| 594 |
+
return DDM(rowslist, shape, domain)
|
| 595 |
+
|
| 596 |
+
@classmethod
|
| 597 |
+
def ones(cls, shape, domain):
|
| 598 |
+
one = domain.one
|
| 599 |
+
m, n = shape
|
| 600 |
+
rowlist = [[one] * n for _ in range(m)]
|
| 601 |
+
return DDM(rowlist, shape, domain)
|
| 602 |
+
|
| 603 |
+
@classmethod
|
| 604 |
+
def eye(cls, size, domain):
|
| 605 |
+
if isinstance(size, tuple):
|
| 606 |
+
m, n = size
|
| 607 |
+
elif isinstance(size, int):
|
| 608 |
+
m = n = size
|
| 609 |
+
one = domain.one
|
| 610 |
+
ddm = cls.zeros((m, n), domain)
|
| 611 |
+
for i in range(min(m, n)):
|
| 612 |
+
ddm[i][i] = one
|
| 613 |
+
return ddm
|
| 614 |
+
|
| 615 |
+
def copy(self):
|
| 616 |
+
copyrows = [row[:] for row in self]
|
| 617 |
+
return DDM(copyrows, self.shape, self.domain)
|
| 618 |
+
|
| 619 |
+
def transpose(self):
|
| 620 |
+
rows, cols = self.shape
|
| 621 |
+
if rows:
|
| 622 |
+
ddmT = ddm_transpose(self)
|
| 623 |
+
else:
|
| 624 |
+
ddmT = [[]] * cols
|
| 625 |
+
return DDM(ddmT, (cols, rows), self.domain)
|
| 626 |
+
|
| 627 |
+
def __add__(a, b):
|
| 628 |
+
if not isinstance(b, DDM):
|
| 629 |
+
return NotImplemented
|
| 630 |
+
return a.add(b)
|
| 631 |
+
|
| 632 |
+
def __sub__(a, b):
|
| 633 |
+
if not isinstance(b, DDM):
|
| 634 |
+
return NotImplemented
|
| 635 |
+
return a.sub(b)
|
| 636 |
+
|
| 637 |
+
def __neg__(a):
|
| 638 |
+
return a.neg()
|
| 639 |
+
|
| 640 |
+
def __mul__(a, b):
|
| 641 |
+
if b in a.domain:
|
| 642 |
+
return a.mul(b)
|
| 643 |
+
else:
|
| 644 |
+
return NotImplemented
|
| 645 |
+
|
| 646 |
+
def __rmul__(a, b):
|
| 647 |
+
if b in a.domain:
|
| 648 |
+
return a.mul(b)
|
| 649 |
+
else:
|
| 650 |
+
return NotImplemented
|
| 651 |
+
|
| 652 |
+
def __matmul__(a, b):
|
| 653 |
+
if isinstance(b, DDM):
|
| 654 |
+
return a.matmul(b)
|
| 655 |
+
else:
|
| 656 |
+
return NotImplemented
|
| 657 |
+
|
| 658 |
+
@classmethod
|
| 659 |
+
def _check(cls, a, op, b, ashape, bshape):
|
| 660 |
+
if a.domain != b.domain:
|
| 661 |
+
msg = "Domain mismatch: %s %s %s" % (a.domain, op, b.domain)
|
| 662 |
+
raise DMDomainError(msg)
|
| 663 |
+
if ashape != bshape:
|
| 664 |
+
msg = "Shape mismatch: %s %s %s" % (a.shape, op, b.shape)
|
| 665 |
+
raise DMShapeError(msg)
|
| 666 |
+
|
| 667 |
+
def add(a, b):
|
| 668 |
+
"""a + b"""
|
| 669 |
+
a._check(a, '+', b, a.shape, b.shape)
|
| 670 |
+
c = a.copy()
|
| 671 |
+
ddm_iadd(c, b)
|
| 672 |
+
return c
|
| 673 |
+
|
| 674 |
+
def sub(a, b):
|
| 675 |
+
"""a - b"""
|
| 676 |
+
a._check(a, '-', b, a.shape, b.shape)
|
| 677 |
+
c = a.copy()
|
| 678 |
+
ddm_isub(c, b)
|
| 679 |
+
return c
|
| 680 |
+
|
| 681 |
+
def neg(a):
|
| 682 |
+
"""-a"""
|
| 683 |
+
b = a.copy()
|
| 684 |
+
ddm_ineg(b)
|
| 685 |
+
return b
|
| 686 |
+
|
| 687 |
+
def mul(a, b):
|
| 688 |
+
c = a.copy()
|
| 689 |
+
ddm_imul(c, b)
|
| 690 |
+
return c
|
| 691 |
+
|
| 692 |
+
def rmul(a, b):
|
| 693 |
+
c = a.copy()
|
| 694 |
+
ddm_irmul(c, b)
|
| 695 |
+
return c
|
| 696 |
+
|
| 697 |
+
def matmul(a, b):
|
| 698 |
+
"""a @ b (matrix product)"""
|
| 699 |
+
m, o = a.shape
|
| 700 |
+
o2, n = b.shape
|
| 701 |
+
a._check(a, '*', b, o, o2)
|
| 702 |
+
c = a.zeros((m, n), a.domain)
|
| 703 |
+
ddm_imatmul(c, a, b)
|
| 704 |
+
return c
|
| 705 |
+
|
| 706 |
+
def mul_elementwise(a, b):
|
| 707 |
+
assert a.shape == b.shape
|
| 708 |
+
assert a.domain == b.domain
|
| 709 |
+
c = [[aij * bij for aij, bij in zip(ai, bi)] for ai, bi in zip(a, b)]
|
| 710 |
+
return DDM(c, a.shape, a.domain)
|
| 711 |
+
|
| 712 |
+
def hstack(A, *B):
|
| 713 |
+
"""Horizontally stacks :py:class:`~.DDM` matrices.
|
| 714 |
+
|
| 715 |
+
Examples
|
| 716 |
+
========
|
| 717 |
+
|
| 718 |
+
>>> from sympy import ZZ
|
| 719 |
+
>>> from sympy.polys.matrices.sdm import DDM
|
| 720 |
+
|
| 721 |
+
>>> A = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
| 722 |
+
>>> B = DDM([[ZZ(5), ZZ(6)], [ZZ(7), ZZ(8)]], (2, 2), ZZ)
|
| 723 |
+
>>> A.hstack(B)
|
| 724 |
+
[[1, 2, 5, 6], [3, 4, 7, 8]]
|
| 725 |
+
|
| 726 |
+
>>> C = DDM([[ZZ(9), ZZ(10)], [ZZ(11), ZZ(12)]], (2, 2), ZZ)
|
| 727 |
+
>>> A.hstack(B, C)
|
| 728 |
+
[[1, 2, 5, 6, 9, 10], [3, 4, 7, 8, 11, 12]]
|
| 729 |
+
"""
|
| 730 |
+
Anew = list(A.copy())
|
| 731 |
+
rows, cols = A.shape
|
| 732 |
+
domain = A.domain
|
| 733 |
+
|
| 734 |
+
for Bk in B:
|
| 735 |
+
Bkrows, Bkcols = Bk.shape
|
| 736 |
+
assert Bkrows == rows
|
| 737 |
+
assert Bk.domain == domain
|
| 738 |
+
|
| 739 |
+
cols += Bkcols
|
| 740 |
+
|
| 741 |
+
for i, Bki in enumerate(Bk):
|
| 742 |
+
Anew[i].extend(Bki)
|
| 743 |
+
|
| 744 |
+
return DDM(Anew, (rows, cols), A.domain)
|
| 745 |
+
|
| 746 |
+
def vstack(A, *B):
|
| 747 |
+
"""Vertically stacks :py:class:`~.DDM` matrices.
|
| 748 |
+
|
| 749 |
+
Examples
|
| 750 |
+
========
|
| 751 |
+
|
| 752 |
+
>>> from sympy import ZZ
|
| 753 |
+
>>> from sympy.polys.matrices.sdm import DDM
|
| 754 |
+
|
| 755 |
+
>>> A = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
|
| 756 |
+
>>> B = DDM([[ZZ(5), ZZ(6)], [ZZ(7), ZZ(8)]], (2, 2), ZZ)
|
| 757 |
+
>>> A.vstack(B)
|
| 758 |
+
[[1, 2], [3, 4], [5, 6], [7, 8]]
|
| 759 |
+
|
| 760 |
+
>>> C = DDM([[ZZ(9), ZZ(10)], [ZZ(11), ZZ(12)]], (2, 2), ZZ)
|
| 761 |
+
>>> A.vstack(B, C)
|
| 762 |
+
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]
|
| 763 |
+
"""
|
| 764 |
+
Anew = list(A.copy())
|
| 765 |
+
rows, cols = A.shape
|
| 766 |
+
domain = A.domain
|
| 767 |
+
|
| 768 |
+
for Bk in B:
|
| 769 |
+
Bkrows, Bkcols = Bk.shape
|
| 770 |
+
assert Bkcols == cols
|
| 771 |
+
assert Bk.domain == domain
|
| 772 |
+
|
| 773 |
+
rows += Bkrows
|
| 774 |
+
|
| 775 |
+
Anew.extend(Bk.copy())
|
| 776 |
+
|
| 777 |
+
return DDM(Anew, (rows, cols), A.domain)
|
| 778 |
+
|
| 779 |
+
def applyfunc(self, func, domain):
|
| 780 |
+
elements = [list(map(func, row)) for row in self]
|
| 781 |
+
return DDM(elements, self.shape, domain)
|
| 782 |
+
|
| 783 |
+
def nnz(a):
|
| 784 |
+
"""Number of non-zero entries in :py:class:`~.DDM` matrix.
|
| 785 |
+
|
| 786 |
+
See Also
|
| 787 |
+
========
|
| 788 |
+
|
| 789 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.nnz
|
| 790 |
+
"""
|
| 791 |
+
return sum(sum(map(bool, row)) for row in a)
|
| 792 |
+
|
| 793 |
+
def scc(a):
|
| 794 |
+
"""Strongly connected components of a square matrix *a*.
|
| 795 |
+
|
| 796 |
+
Examples
|
| 797 |
+
========
|
| 798 |
+
|
| 799 |
+
>>> from sympy import ZZ
|
| 800 |
+
>>> from sympy.polys.matrices.sdm import DDM
|
| 801 |
+
>>> A = DDM([[ZZ(1), ZZ(0)], [ZZ(0), ZZ(1)]], (2, 2), ZZ)
|
| 802 |
+
>>> A.scc()
|
| 803 |
+
[[0], [1]]
|
| 804 |
+
|
| 805 |
+
See also
|
| 806 |
+
========
|
| 807 |
+
|
| 808 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.scc
|
| 809 |
+
|
| 810 |
+
"""
|
| 811 |
+
return a.to_sdm().scc()
|
| 812 |
+
|
| 813 |
+
@classmethod
|
| 814 |
+
def diag(cls, values, domain):
|
| 815 |
+
"""Returns a square diagonal matrix with *values* on the diagonal.
|
| 816 |
+
|
| 817 |
+
Examples
|
| 818 |
+
========
|
| 819 |
+
|
| 820 |
+
>>> from sympy import ZZ
|
| 821 |
+
>>> from sympy.polys.matrices.sdm import DDM
|
| 822 |
+
>>> DDM.diag([ZZ(1), ZZ(2), ZZ(3)], ZZ)
|
| 823 |
+
[[1, 0, 0], [0, 2, 0], [0, 0, 3]]
|
| 824 |
+
|
| 825 |
+
See also
|
| 826 |
+
========
|
| 827 |
+
|
| 828 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.diag
|
| 829 |
+
"""
|
| 830 |
+
return SDM.diag(values, domain).to_ddm()
|
| 831 |
+
|
| 832 |
+
def rref(a):
|
| 833 |
+
"""Reduced-row echelon form of a and list of pivots.
|
| 834 |
+
|
| 835 |
+
See Also
|
| 836 |
+
========
|
| 837 |
+
|
| 838 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.rref
|
| 839 |
+
Higher level interface to this function.
|
| 840 |
+
sympy.polys.matrices.dense.ddm_irref
|
| 841 |
+
The underlying algorithm.
|
| 842 |
+
"""
|
| 843 |
+
b = a.copy()
|
| 844 |
+
K = a.domain
|
| 845 |
+
partial_pivot = K.is_RealField or K.is_ComplexField
|
| 846 |
+
pivots = ddm_irref(b, _partial_pivot=partial_pivot)
|
| 847 |
+
return b, pivots
|
| 848 |
+
|
| 849 |
+
def rref_den(a):
|
| 850 |
+
"""Reduced-row echelon form of a with denominator and list of pivots
|
| 851 |
+
|
| 852 |
+
See Also
|
| 853 |
+
========
|
| 854 |
+
|
| 855 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.rref_den
|
| 856 |
+
Higher level interface to this function.
|
| 857 |
+
sympy.polys.matrices.dense.ddm_irref_den
|
| 858 |
+
The underlying algorithm.
|
| 859 |
+
"""
|
| 860 |
+
b = a.copy()
|
| 861 |
+
K = a.domain
|
| 862 |
+
denom, pivots = ddm_irref_den(b, K)
|
| 863 |
+
return b, denom, pivots
|
| 864 |
+
|
| 865 |
+
def nullspace(a):
|
| 866 |
+
"""Returns a basis for the nullspace of a.
|
| 867 |
+
|
| 868 |
+
The domain of the matrix must be a field.
|
| 869 |
+
|
| 870 |
+
See Also
|
| 871 |
+
========
|
| 872 |
+
|
| 873 |
+
rref
|
| 874 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.nullspace
|
| 875 |
+
"""
|
| 876 |
+
rref, pivots = a.rref()
|
| 877 |
+
return rref.nullspace_from_rref(pivots)
|
| 878 |
+
|
| 879 |
+
def nullspace_from_rref(a, pivots=None):
|
| 880 |
+
"""Compute the nullspace of a matrix from its rref.
|
| 881 |
+
|
| 882 |
+
The domain of the matrix can be any domain.
|
| 883 |
+
|
| 884 |
+
Returns a tuple (basis, nonpivots).
|
| 885 |
+
|
| 886 |
+
See Also
|
| 887 |
+
========
|
| 888 |
+
|
| 889 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.nullspace
|
| 890 |
+
The higher level interface to this function.
|
| 891 |
+
"""
|
| 892 |
+
m, n = a.shape
|
| 893 |
+
K = a.domain
|
| 894 |
+
|
| 895 |
+
if pivots is None:
|
| 896 |
+
pivots = []
|
| 897 |
+
last_pivot = -1
|
| 898 |
+
for i in range(m):
|
| 899 |
+
ai = a[i]
|
| 900 |
+
for j in range(last_pivot+1, n):
|
| 901 |
+
if ai[j]:
|
| 902 |
+
last_pivot = j
|
| 903 |
+
pivots.append(j)
|
| 904 |
+
break
|
| 905 |
+
|
| 906 |
+
if not pivots:
|
| 907 |
+
return (a.eye(n, K), list(range(n)))
|
| 908 |
+
|
| 909 |
+
# After rref the pivots are all one but after rref_den they may not be.
|
| 910 |
+
pivot_val = a[0][pivots[0]]
|
| 911 |
+
|
| 912 |
+
basis = []
|
| 913 |
+
nonpivots = []
|
| 914 |
+
for i in range(n):
|
| 915 |
+
if i in pivots:
|
| 916 |
+
continue
|
| 917 |
+
nonpivots.append(i)
|
| 918 |
+
vec = [pivot_val if i == j else K.zero for j in range(n)]
|
| 919 |
+
for ii, jj in enumerate(pivots):
|
| 920 |
+
vec[jj] -= a[ii][i]
|
| 921 |
+
basis.append(vec)
|
| 922 |
+
|
| 923 |
+
basis_ddm = DDM(basis, (len(basis), n), K)
|
| 924 |
+
|
| 925 |
+
return (basis_ddm, nonpivots)
|
| 926 |
+
|
| 927 |
+
def particular(a):
|
| 928 |
+
return a.to_sdm().particular().to_ddm()
|
| 929 |
+
|
| 930 |
+
def det(a):
|
| 931 |
+
"""Determinant of a"""
|
| 932 |
+
m, n = a.shape
|
| 933 |
+
if m != n:
|
| 934 |
+
raise DMNonSquareMatrixError("Determinant of non-square matrix")
|
| 935 |
+
b = a.copy()
|
| 936 |
+
K = b.domain
|
| 937 |
+
deta = ddm_idet(b, K)
|
| 938 |
+
return deta
|
| 939 |
+
|
| 940 |
+
def inv(a):
|
| 941 |
+
"""Inverse of a"""
|
| 942 |
+
m, n = a.shape
|
| 943 |
+
if m != n:
|
| 944 |
+
raise DMNonSquareMatrixError("Determinant of non-square matrix")
|
| 945 |
+
ainv = a.copy()
|
| 946 |
+
K = a.domain
|
| 947 |
+
ddm_iinv(ainv, a, K)
|
| 948 |
+
return ainv
|
| 949 |
+
|
| 950 |
+
def lu(a):
|
| 951 |
+
"""L, U decomposition of a"""
|
| 952 |
+
m, n = a.shape
|
| 953 |
+
K = a.domain
|
| 954 |
+
|
| 955 |
+
U = a.copy()
|
| 956 |
+
L = a.eye(m, K)
|
| 957 |
+
swaps = ddm_ilu_split(L, U, K)
|
| 958 |
+
|
| 959 |
+
return L, U, swaps
|
| 960 |
+
|
| 961 |
+
def lu_solve(a, b):
|
| 962 |
+
"""x where a*x = b"""
|
| 963 |
+
m, n = a.shape
|
| 964 |
+
m2, o = b.shape
|
| 965 |
+
a._check(a, 'lu_solve', b, m, m2)
|
| 966 |
+
if not a.domain.is_Field:
|
| 967 |
+
raise DMDomainError("lu_solve requires a field")
|
| 968 |
+
|
| 969 |
+
L, U, swaps = a.lu()
|
| 970 |
+
x = a.zeros((n, o), a.domain)
|
| 971 |
+
ddm_ilu_solve(x, L, U, swaps, b)
|
| 972 |
+
return x
|
| 973 |
+
|
| 974 |
+
def charpoly(a):
|
| 975 |
+
"""Coefficients of characteristic polynomial of a"""
|
| 976 |
+
K = a.domain
|
| 977 |
+
m, n = a.shape
|
| 978 |
+
if m != n:
|
| 979 |
+
raise DMNonSquareMatrixError("Charpoly of non-square matrix")
|
| 980 |
+
vec = ddm_berk(a, K)
|
| 981 |
+
coeffs = [vec[i][0] for i in range(n+1)]
|
| 982 |
+
return coeffs
|
| 983 |
+
|
| 984 |
+
def is_zero_matrix(self):
|
| 985 |
+
"""
|
| 986 |
+
Says whether this matrix has all zero entries.
|
| 987 |
+
"""
|
| 988 |
+
zero = self.domain.zero
|
| 989 |
+
return all(Mij == zero for Mij in self.flatiter())
|
| 990 |
+
|
| 991 |
+
def is_upper(self):
|
| 992 |
+
"""
|
| 993 |
+
Says whether this matrix is upper-triangular. True can be returned
|
| 994 |
+
even if the matrix is not square.
|
| 995 |
+
"""
|
| 996 |
+
zero = self.domain.zero
|
| 997 |
+
return all(Mij == zero for i, Mi in enumerate(self) for Mij in Mi[:i])
|
| 998 |
+
|
| 999 |
+
def is_lower(self):
|
| 1000 |
+
"""
|
| 1001 |
+
Says whether this matrix is lower-triangular. True can be returned
|
| 1002 |
+
even if the matrix is not square.
|
| 1003 |
+
"""
|
| 1004 |
+
zero = self.domain.zero
|
| 1005 |
+
return all(Mij == zero for i, Mi in enumerate(self) for Mij in Mi[i+1:])
|
| 1006 |
+
|
| 1007 |
+
def is_diagonal(self):
|
| 1008 |
+
"""
|
| 1009 |
+
Says whether this matrix is diagonal. True can be returned even if
|
| 1010 |
+
the matrix is not square.
|
| 1011 |
+
"""
|
| 1012 |
+
return self.is_upper() and self.is_lower()
|
| 1013 |
+
|
| 1014 |
+
def diagonal(self):
|
| 1015 |
+
"""
|
| 1016 |
+
Returns a list of the elements from the diagonal of the matrix.
|
| 1017 |
+
"""
|
| 1018 |
+
m, n = self.shape
|
| 1019 |
+
return [self[i][i] for i in range(min(m, n))]
|
| 1020 |
+
|
| 1021 |
+
def lll(A, delta=QQ(3, 4)):
|
| 1022 |
+
return ddm_lll(A, delta=delta)
|
| 1023 |
+
|
| 1024 |
+
def lll_transform(A, delta=QQ(3, 4)):
|
| 1025 |
+
return ddm_lll_transform(A, delta=delta)
|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matrix in list of lists
|
| 4 |
+
matrix representation.
|
| 5 |
+
|
| 6 |
+
These routines are used internally by the DDM class which also provides a
|
| 7 |
+
friendlier interface for them. The idea here is to implement core matrix
|
| 8 |
+
routines in a way that can be applied to any simple list representation
|
| 9 |
+
without the need to use any particular matrix class. For example we can
|
| 10 |
+
compute the RREF of a matrix like:
|
| 11 |
+
|
| 12 |
+
>>> from sympy.polys.matrices.dense import ddm_irref
|
| 13 |
+
>>> M = [[1, 2, 3], [4, 5, 6]]
|
| 14 |
+
>>> pivots = ddm_irref(M)
|
| 15 |
+
>>> M
|
| 16 |
+
[[1.0, 0.0, -1.0], [0, 1.0, 2.0]]
|
| 17 |
+
|
| 18 |
+
These are lower-level routines that work mostly in place.The routines at this
|
| 19 |
+
level should not need to know what the domain of the elements is but should
|
| 20 |
+
ideally document what operations they will use and what functions they need to
|
| 21 |
+
be provided with.
|
| 22 |
+
|
| 23 |
+
The next-level up is the DDM class which uses these routines but wraps them up
|
| 24 |
+
with an interface that handles copying etc and keeps track of the Domain of
|
| 25 |
+
the elements of the matrix:
|
| 26 |
+
|
| 27 |
+
>>> from sympy.polys.domains import QQ
|
| 28 |
+
>>> from sympy.polys.matrices.ddm import DDM
|
| 29 |
+
>>> M = DDM([[QQ(1), QQ(2), QQ(3)], [QQ(4), QQ(5), QQ(6)]], (2, 3), QQ)
|
| 30 |
+
>>> M
|
| 31 |
+
[[1, 2, 3], [4, 5, 6]]
|
| 32 |
+
>>> Mrref, pivots = M.rref()
|
| 33 |
+
>>> Mrref
|
| 34 |
+
[[1, 0, -1], [0, 1, 2]]
|
| 35 |
+
|
| 36 |
+
"""
|
| 37 |
+
from __future__ import annotations
|
| 38 |
+
from operator import mul
|
| 39 |
+
from .exceptions import (
|
| 40 |
+
DMShapeError,
|
| 41 |
+
DMDomainError,
|
| 42 |
+
DMNonInvertibleMatrixError,
|
| 43 |
+
DMNonSquareMatrixError,
|
| 44 |
+
)
|
| 45 |
+
from typing import Sequence, TypeVar
|
| 46 |
+
from sympy.polys.matrices._typing import RingElement
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
#: Type variable for the elements of the matrix
|
| 50 |
+
T = TypeVar('T')
|
| 51 |
+
|
| 52 |
+
#: Type variable for the elements of the matrix that are in a ring
|
| 53 |
+
R = TypeVar('R', bound=RingElement)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def ddm_transpose(matrix: Sequence[Sequence[T]]) -> list[list[T]]:
|
| 57 |
+
"""matrix transpose"""
|
| 58 |
+
return list(map(list, zip(*matrix)))
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def ddm_iadd(a: list[list[R]], b: Sequence[Sequence[R]]) -> None:
|
| 62 |
+
"""a += b"""
|
| 63 |
+
for ai, bi in zip(a, b):
|
| 64 |
+
for j, bij in enumerate(bi):
|
| 65 |
+
ai[j] += bij
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def ddm_isub(a: list[list[R]], b: Sequence[Sequence[R]]) -> None:
|
| 69 |
+
"""a -= b"""
|
| 70 |
+
for ai, bi in zip(a, b):
|
| 71 |
+
for j, bij in enumerate(bi):
|
| 72 |
+
ai[j] -= bij
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def ddm_ineg(a: list[list[R]]) -> None:
|
| 76 |
+
"""a <-- -a"""
|
| 77 |
+
for ai in a:
|
| 78 |
+
for j, aij in enumerate(ai):
|
| 79 |
+
ai[j] = -aij
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def ddm_imul(a: list[list[R]], b: R) -> None:
|
| 83 |
+
"""a <-- a*b"""
|
| 84 |
+
for ai in a:
|
| 85 |
+
for j, aij in enumerate(ai):
|
| 86 |
+
ai[j] = aij * b
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def ddm_irmul(a: list[list[R]], b: R) -> None:
|
| 90 |
+
"""a <-- b*a"""
|
| 91 |
+
for ai in a:
|
| 92 |
+
for j, aij in enumerate(ai):
|
| 93 |
+
ai[j] = b * aij
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def ddm_imatmul(
|
| 97 |
+
a: list[list[R]], b: Sequence[Sequence[R]], c: Sequence[Sequence[R]]
|
| 98 |
+
) -> None:
|
| 99 |
+
"""a += b @ c"""
|
| 100 |
+
cT = list(zip(*c))
|
| 101 |
+
|
| 102 |
+
for bi, ai in zip(b, a):
|
| 103 |
+
for j, cTj in enumerate(cT):
|
| 104 |
+
ai[j] = sum(map(mul, bi, cTj), ai[j])
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def ddm_irref(a, _partial_pivot=False):
|
| 108 |
+
"""In-place reduced row echelon form of a matrix.
|
| 109 |
+
|
| 110 |
+
Compute the reduced row echelon form of $a$. Modifies $a$ in place and
|
| 111 |
+
returns a list of the pivot columns.
|
| 112 |
+
|
| 113 |
+
Uses naive Gauss-Jordan elimination in the ground domain which must be a
|
| 114 |
+
field.
|
| 115 |
+
|
| 116 |
+
This routine is only really suitable for use with simple field domains like
|
| 117 |
+
:ref:`GF(p)`, :ref:`QQ` and :ref:`QQ(a)` although even for :ref:`QQ` with
|
| 118 |
+
larger matrices it is possibly more efficient to use fraction free
|
| 119 |
+
approaches.
|
| 120 |
+
|
| 121 |
+
This method is not suitable for use with rational function fields
|
| 122 |
+
(:ref:`K(x)`) because the elements will blowup leading to costly gcd
|
| 123 |
+
operations. In this case clearing denominators and using fraction free
|
| 124 |
+
approaches is likely to be more efficient.
|
| 125 |
+
|
| 126 |
+
For inexact numeric domains like :ref:`RR` and :ref:`CC` pass
|
| 127 |
+
``_partial_pivot=True`` to use partial pivoting to control rounding errors.
|
| 128 |
+
|
| 129 |
+
Examples
|
| 130 |
+
========
|
| 131 |
+
|
| 132 |
+
>>> from sympy.polys.matrices.dense import ddm_irref
|
| 133 |
+
>>> from sympy import QQ
|
| 134 |
+
>>> M = [[QQ(1), QQ(2), QQ(3)], [QQ(4), QQ(5), QQ(6)]]
|
| 135 |
+
>>> pivots = ddm_irref(M)
|
| 136 |
+
>>> M
|
| 137 |
+
[[1, 0, -1], [0, 1, 2]]
|
| 138 |
+
>>> pivots
|
| 139 |
+
[0, 1]
|
| 140 |
+
|
| 141 |
+
See Also
|
| 142 |
+
========
|
| 143 |
+
|
| 144 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.rref
|
| 145 |
+
Higher level interface to this routine.
|
| 146 |
+
ddm_irref_den
|
| 147 |
+
The fraction free version of this routine.
|
| 148 |
+
sdm_irref
|
| 149 |
+
A sparse version of this routine.
|
| 150 |
+
|
| 151 |
+
References
|
| 152 |
+
==========
|
| 153 |
+
|
| 154 |
+
.. [1] https://en.wikipedia.org/wiki/Row_echelon_form#Reduced_row_echelon_form
|
| 155 |
+
"""
|
| 156 |
+
# We compute aij**-1 below and then use multiplication instead of division
|
| 157 |
+
# in the innermost loop. The domain here is a field so either operation is
|
| 158 |
+
# defined. There are significant performance differences for some domains
|
| 159 |
+
# though. In the case of e.g. QQ or QQ(x) inversion is free but
|
| 160 |
+
# multiplication and division have the same cost so it makes no difference.
|
| 161 |
+
# In cases like GF(p), QQ<sqrt(2)>, RR or CC though multiplication is
|
| 162 |
+
# faster than division so reusing a precomputed inverse for many
|
| 163 |
+
# multiplications can be a lot faster. The biggest win is QQ<a> when
|
| 164 |
+
# deg(minpoly(a)) is large.
|
| 165 |
+
#
|
| 166 |
+
# With domains like QQ(x) this can perform badly for other reasons.
|
| 167 |
+
# Typically the initial matrix has simple denominators and the
|
| 168 |
+
# fraction-free approach with exquo (ddm_irref_den) will preserve that
|
| 169 |
+
# property throughout. The method here causes denominator blowup leading to
|
| 170 |
+
# expensive gcd reductions in the intermediate expressions. With many
|
| 171 |
+
# generators like QQ(x,y,z,...) this is extremely bad.
|
| 172 |
+
#
|
| 173 |
+
# TODO: Use a nontrivial pivoting strategy to control intermediate
|
| 174 |
+
# expression growth. Rearranging rows and/or columns could defer the most
|
| 175 |
+
# complicated elements until the end. If the first pivot is a
|
| 176 |
+
# complicated/large element then the first round of reduction will
|
| 177 |
+
# immediately introduce expression blowup across the whole matrix.
|
| 178 |
+
|
| 179 |
+
# a is (m x n)
|
| 180 |
+
m = len(a)
|
| 181 |
+
if not m:
|
| 182 |
+
return []
|
| 183 |
+
n = len(a[0])
|
| 184 |
+
|
| 185 |
+
i = 0
|
| 186 |
+
pivots = []
|
| 187 |
+
|
| 188 |
+
for j in range(n):
|
| 189 |
+
# Proper pivoting should be used for all domains for performance
|
| 190 |
+
# reasons but it is only strictly needed for RR and CC (and possibly
|
| 191 |
+
# other domains like RR(x)). This path is used by DDM.rref() if the
|
| 192 |
+
# domain is RR or CC. It uses partial (row) pivoting based on the
|
| 193 |
+
# absolute value of the pivot candidates.
|
| 194 |
+
if _partial_pivot:
|
| 195 |
+
ip = max(range(i, m), key=lambda ip: abs(a[ip][j]))
|
| 196 |
+
a[i], a[ip] = a[ip], a[i]
|
| 197 |
+
|
| 198 |
+
# pivot
|
| 199 |
+
aij = a[i][j]
|
| 200 |
+
|
| 201 |
+
# zero-pivot
|
| 202 |
+
if not aij:
|
| 203 |
+
for ip in range(i+1, m):
|
| 204 |
+
aij = a[ip][j]
|
| 205 |
+
# row-swap
|
| 206 |
+
if aij:
|
| 207 |
+
a[i], a[ip] = a[ip], a[i]
|
| 208 |
+
break
|
| 209 |
+
else:
|
| 210 |
+
# next column
|
| 211 |
+
continue
|
| 212 |
+
|
| 213 |
+
# normalise row
|
| 214 |
+
ai = a[i]
|
| 215 |
+
aijinv = aij**-1
|
| 216 |
+
for l in range(j, n):
|
| 217 |
+
ai[l] *= aijinv # ai[j] = one
|
| 218 |
+
|
| 219 |
+
# eliminate above and below to the right
|
| 220 |
+
for k, ak in enumerate(a):
|
| 221 |
+
if k == i or not ak[j]:
|
| 222 |
+
continue
|
| 223 |
+
akj = ak[j]
|
| 224 |
+
ak[j] -= akj # ak[j] = zero
|
| 225 |
+
for l in range(j+1, n):
|
| 226 |
+
ak[l] -= akj * ai[l]
|
| 227 |
+
|
| 228 |
+
# next row
|
| 229 |
+
pivots.append(j)
|
| 230 |
+
i += 1
|
| 231 |
+
|
| 232 |
+
# no more rows?
|
| 233 |
+
if i >= m:
|
| 234 |
+
break
|
| 235 |
+
|
| 236 |
+
return pivots
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def ddm_irref_den(a, K):
|
| 240 |
+
"""a <-- rref(a); return (den, pivots)
|
| 241 |
+
|
| 242 |
+
Compute the fraction-free reduced row echelon form (RREF) of $a$. Modifies
|
| 243 |
+
$a$ in place and returns a tuple containing the denominator of the RREF and
|
| 244 |
+
a list of the pivot columns.
|
| 245 |
+
|
| 246 |
+
Explanation
|
| 247 |
+
===========
|
| 248 |
+
|
| 249 |
+
The algorithm used is the fraction-free version of Gauss-Jordan elimination
|
| 250 |
+
described as FFGJ in [1]_. Here it is modified to handle zero or missing
|
| 251 |
+
pivots and to avoid redundant arithmetic.
|
| 252 |
+
|
| 253 |
+
The domain $K$ must support exact division (``K.exquo``) but does not need
|
| 254 |
+
to be a field. This method is suitable for most exact rings and fields like
|
| 255 |
+
:ref:`ZZ`, :ref:`QQ` and :ref:`QQ(a)`. In the case of :ref:`QQ` or
|
| 256 |
+
:ref:`K(x)` it might be more efficient to clear denominators and use
|
| 257 |
+
:ref:`ZZ` or :ref:`K[x]` instead.
|
| 258 |
+
|
| 259 |
+
For inexact domains like :ref:`RR` and :ref:`CC` use ``ddm_irref`` instead.
|
| 260 |
+
|
| 261 |
+
Examples
|
| 262 |
+
========
|
| 263 |
+
|
| 264 |
+
>>> from sympy.polys.matrices.dense import ddm_irref_den
|
| 265 |
+
>>> from sympy import ZZ, Matrix
|
| 266 |
+
>>> M = [[ZZ(1), ZZ(2), ZZ(3)], [ZZ(4), ZZ(5), ZZ(6)]]
|
| 267 |
+
>>> den, pivots = ddm_irref_den(M, ZZ)
|
| 268 |
+
>>> M
|
| 269 |
+
[[-3, 0, 3], [0, -3, -6]]
|
| 270 |
+
>>> den
|
| 271 |
+
-3
|
| 272 |
+
>>> pivots
|
| 273 |
+
[0, 1]
|
| 274 |
+
>>> Matrix(M).rref()[0]
|
| 275 |
+
Matrix([
|
| 276 |
+
[1, 0, -1],
|
| 277 |
+
[0, 1, 2]])
|
| 278 |
+
|
| 279 |
+
See Also
|
| 280 |
+
========
|
| 281 |
+
|
| 282 |
+
ddm_irref
|
| 283 |
+
A version of this routine that uses field division.
|
| 284 |
+
sdm_irref
|
| 285 |
+
A sparse version of :func:`ddm_irref`.
|
| 286 |
+
sdm_rref_den
|
| 287 |
+
A sparse version of :func:`ddm_irref_den`.
|
| 288 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.rref_den
|
| 289 |
+
Higher level interface.
|
| 290 |
+
|
| 291 |
+
References
|
| 292 |
+
==========
|
| 293 |
+
|
| 294 |
+
.. [1] Fraction-free algorithms for linear and polynomial equations.
|
| 295 |
+
George C. Nakos , Peter R. Turner , Robert M. Williams.
|
| 296 |
+
https://dl.acm.org/doi/10.1145/271130.271133
|
| 297 |
+
"""
|
| 298 |
+
#
|
| 299 |
+
# A simpler presentation of this algorithm is given in [1]:
|
| 300 |
+
#
|
| 301 |
+
# Given an n x n matrix A and n x 1 matrix b:
|
| 302 |
+
#
|
| 303 |
+
# for i in range(n):
|
| 304 |
+
# if i != 0:
|
| 305 |
+
# d = a[i-1][i-1]
|
| 306 |
+
# for j in range(n):
|
| 307 |
+
# if j == i:
|
| 308 |
+
# continue
|
| 309 |
+
# b[j] = a[i][i]*b[j] - a[j][i]*b[i]
|
| 310 |
+
# for k in range(n):
|
| 311 |
+
# a[j][k] = a[i][i]*a[j][k] - a[j][i]*a[i][k]
|
| 312 |
+
# if i != 0:
|
| 313 |
+
# a[j][k] /= d
|
| 314 |
+
#
|
| 315 |
+
# Our version here is a bit more complicated because:
|
| 316 |
+
#
|
| 317 |
+
# 1. We use row-swaps to avoid zero pivots.
|
| 318 |
+
# 2. We allow for some columns to be missing pivots.
|
| 319 |
+
# 3. We avoid a lot of redundant arithmetic.
|
| 320 |
+
#
|
| 321 |
+
# TODO: Use a non-trivial pivoting strategy. Even just row swapping makes a
|
| 322 |
+
# big difference to performance if e.g. the upper-left entry of the matrix
|
| 323 |
+
# is a huge polynomial.
|
| 324 |
+
|
| 325 |
+
# a is (m x n)
|
| 326 |
+
m = len(a)
|
| 327 |
+
if not m:
|
| 328 |
+
return K.one, []
|
| 329 |
+
n = len(a[0])
|
| 330 |
+
|
| 331 |
+
d = None
|
| 332 |
+
pivots = []
|
| 333 |
+
no_pivots = []
|
| 334 |
+
|
| 335 |
+
# i, j will be the row and column indices of the current pivot
|
| 336 |
+
i = 0
|
| 337 |
+
for j in range(n):
|
| 338 |
+
# next pivot?
|
| 339 |
+
aij = a[i][j]
|
| 340 |
+
|
| 341 |
+
# swap rows if zero
|
| 342 |
+
if not aij:
|
| 343 |
+
for ip in range(i+1, m):
|
| 344 |
+
aij = a[ip][j]
|
| 345 |
+
# row-swap
|
| 346 |
+
if aij:
|
| 347 |
+
a[i], a[ip] = a[ip], a[i]
|
| 348 |
+
break
|
| 349 |
+
else:
|
| 350 |
+
# go to next column
|
| 351 |
+
no_pivots.append(j)
|
| 352 |
+
continue
|
| 353 |
+
|
| 354 |
+
# Now aij is the pivot and i,j are the row and column. We need to clear
|
| 355 |
+
# the column above and below but we also need to keep track of the
|
| 356 |
+
# denominator of the RREF which means also multiplying everything above
|
| 357 |
+
# and to the left by the current pivot aij and dividing by d (which we
|
| 358 |
+
# multiplied everything by in the previous iteration so this is an
|
| 359 |
+
# exact division).
|
| 360 |
+
#
|
| 361 |
+
# First handle the upper left corner which is usually already diagonal
|
| 362 |
+
# with all diagonal entries equal to the current denominator but there
|
| 363 |
+
# can be other non-zero entries in any column that has no pivot.
|
| 364 |
+
|
| 365 |
+
# Update previous pivots in the matrix
|
| 366 |
+
if pivots:
|
| 367 |
+
pivot_val = aij * a[0][pivots[0]]
|
| 368 |
+
# Divide out the common factor
|
| 369 |
+
if d is not None:
|
| 370 |
+
pivot_val = K.exquo(pivot_val, d)
|
| 371 |
+
|
| 372 |
+
# Could defer this until the end but it is pretty cheap and
|
| 373 |
+
# helps when debugging.
|
| 374 |
+
for ip, jp in enumerate(pivots):
|
| 375 |
+
a[ip][jp] = pivot_val
|
| 376 |
+
|
| 377 |
+
# Update columns without pivots
|
| 378 |
+
for jnp in no_pivots:
|
| 379 |
+
for ip in range(i):
|
| 380 |
+
aijp = a[ip][jnp]
|
| 381 |
+
if aijp:
|
| 382 |
+
aijp *= aij
|
| 383 |
+
if d is not None:
|
| 384 |
+
aijp = K.exquo(aijp, d)
|
| 385 |
+
a[ip][jnp] = aijp
|
| 386 |
+
|
| 387 |
+
# Eliminate above, below and to the right as in ordinary division free
|
| 388 |
+
# Gauss-Jordan elmination except also dividing out d from every entry.
|
| 389 |
+
|
| 390 |
+
for jp, aj in enumerate(a):
|
| 391 |
+
|
| 392 |
+
# Skip the current row
|
| 393 |
+
if jp == i:
|
| 394 |
+
continue
|
| 395 |
+
|
| 396 |
+
# Eliminate to the right in all rows
|
| 397 |
+
for kp in range(j+1, n):
|
| 398 |
+
ajk = aij * aj[kp] - aj[j] * a[i][kp]
|
| 399 |
+
if d is not None:
|
| 400 |
+
ajk = K.exquo(ajk, d)
|
| 401 |
+
aj[kp] = ajk
|
| 402 |
+
|
| 403 |
+
# Set to zero above and below the pivot
|
| 404 |
+
aj[j] = K.zero
|
| 405 |
+
|
| 406 |
+
# next row
|
| 407 |
+
pivots.append(j)
|
| 408 |
+
i += 1
|
| 409 |
+
|
| 410 |
+
# no more rows left?
|
| 411 |
+
if i >= m:
|
| 412 |
+
break
|
| 413 |
+
|
| 414 |
+
if not K.is_one(aij):
|
| 415 |
+
d = aij
|
| 416 |
+
else:
|
| 417 |
+
d = None
|
| 418 |
+
|
| 419 |
+
if not pivots:
|
| 420 |
+
denom = K.one
|
| 421 |
+
else:
|
| 422 |
+
denom = a[0][pivots[0]]
|
| 423 |
+
|
| 424 |
+
return denom, pivots
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
def ddm_idet(a, K):
|
| 428 |
+
"""a <-- echelon(a); return det
|
| 429 |
+
|
| 430 |
+
Explanation
|
| 431 |
+
===========
|
| 432 |
+
|
| 433 |
+
Compute the determinant of $a$ using the Bareiss fraction-free algorithm.
|
| 434 |
+
The matrix $a$ is modified in place. Its diagonal elements are the
|
| 435 |
+
determinants of the leading principal minors. The determinant of $a$ is
|
| 436 |
+
returned.
|
| 437 |
+
|
| 438 |
+
The domain $K$ must support exact division (``K.exquo``). This method is
|
| 439 |
+
suitable for most exact rings and fields like :ref:`ZZ`, :ref:`QQ` and
|
| 440 |
+
:ref:`QQ(a)` but not for inexact domains like :ref:`RR` and :ref:`CC`.
|
| 441 |
+
|
| 442 |
+
Examples
|
| 443 |
+
========
|
| 444 |
+
|
| 445 |
+
>>> from sympy import ZZ
|
| 446 |
+
>>> from sympy.polys.matrices.ddm import ddm_idet
|
| 447 |
+
>>> a = [[ZZ(1), ZZ(2), ZZ(3)], [ZZ(4), ZZ(5), ZZ(6)], [ZZ(7), ZZ(8), ZZ(9)]]
|
| 448 |
+
>>> a
|
| 449 |
+
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
| 450 |
+
>>> ddm_idet(a, ZZ)
|
| 451 |
+
0
|
| 452 |
+
>>> a
|
| 453 |
+
[[1, 2, 3], [4, -3, -6], [7, -6, 0]]
|
| 454 |
+
>>> [a[i][i] for i in range(len(a))]
|
| 455 |
+
[1, -3, 0]
|
| 456 |
+
|
| 457 |
+
See Also
|
| 458 |
+
========
|
| 459 |
+
|
| 460 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.det
|
| 461 |
+
|
| 462 |
+
References
|
| 463 |
+
==========
|
| 464 |
+
|
| 465 |
+
.. [1] https://en.wikipedia.org/wiki/Bareiss_algorithm
|
| 466 |
+
.. [2] https://www.math.usm.edu/perry/Research/Thesis_DRL.pdf
|
| 467 |
+
"""
|
| 468 |
+
# Bareiss algorithm
|
| 469 |
+
# https://www.math.usm.edu/perry/Research/Thesis_DRL.pdf
|
| 470 |
+
|
| 471 |
+
# a is (m x n)
|
| 472 |
+
m = len(a)
|
| 473 |
+
if not m:
|
| 474 |
+
return K.one
|
| 475 |
+
n = len(a[0])
|
| 476 |
+
|
| 477 |
+
exquo = K.exquo
|
| 478 |
+
# uf keeps track of the sign change from row swaps
|
| 479 |
+
uf = K.one
|
| 480 |
+
|
| 481 |
+
for k in range(n-1):
|
| 482 |
+
if not a[k][k]:
|
| 483 |
+
for i in range(k+1, n):
|
| 484 |
+
if a[i][k]:
|
| 485 |
+
a[k], a[i] = a[i], a[k]
|
| 486 |
+
uf = -uf
|
| 487 |
+
break
|
| 488 |
+
else:
|
| 489 |
+
return K.zero
|
| 490 |
+
|
| 491 |
+
akkm1 = a[k-1][k-1] if k else K.one
|
| 492 |
+
|
| 493 |
+
for i in range(k+1, n):
|
| 494 |
+
for j in range(k+1, n):
|
| 495 |
+
a[i][j] = exquo(a[i][j]*a[k][k] - a[i][k]*a[k][j], akkm1)
|
| 496 |
+
|
| 497 |
+
return uf * a[-1][-1]
|
| 498 |
+
|
| 499 |
+
|
| 500 |
+
def ddm_iinv(ainv, a, K):
|
| 501 |
+
"""ainv <-- inv(a)
|
| 502 |
+
|
| 503 |
+
Compute the inverse of a matrix $a$ over a field $K$ using Gauss-Jordan
|
| 504 |
+
elimination. The result is stored in $ainv$.
|
| 505 |
+
|
| 506 |
+
Uses division in the ground domain which should be an exact field.
|
| 507 |
+
|
| 508 |
+
Examples
|
| 509 |
+
========
|
| 510 |
+
|
| 511 |
+
>>> from sympy.polys.matrices.ddm import ddm_iinv, ddm_imatmul
|
| 512 |
+
>>> from sympy import QQ
|
| 513 |
+
>>> a = [[QQ(1), QQ(2)], [QQ(3), QQ(4)]]
|
| 514 |
+
>>> ainv = [[None, None], [None, None]]
|
| 515 |
+
>>> ddm_iinv(ainv, a, QQ)
|
| 516 |
+
>>> ainv
|
| 517 |
+
[[-2, 1], [3/2, -1/2]]
|
| 518 |
+
>>> result = [[QQ(0), QQ(0)], [QQ(0), QQ(0)]]
|
| 519 |
+
>>> ddm_imatmul(result, a, ainv)
|
| 520 |
+
>>> result
|
| 521 |
+
[[1, 0], [0, 1]]
|
| 522 |
+
|
| 523 |
+
See Also
|
| 524 |
+
========
|
| 525 |
+
|
| 526 |
+
ddm_irref: the underlying routine.
|
| 527 |
+
"""
|
| 528 |
+
if not K.is_Field:
|
| 529 |
+
raise DMDomainError('Not a field')
|
| 530 |
+
|
| 531 |
+
# a is (m x n)
|
| 532 |
+
m = len(a)
|
| 533 |
+
if not m:
|
| 534 |
+
return
|
| 535 |
+
n = len(a[0])
|
| 536 |
+
if m != n:
|
| 537 |
+
raise DMNonSquareMatrixError
|
| 538 |
+
|
| 539 |
+
eye = [[K.one if i==j else K.zero for j in range(n)] for i in range(n)]
|
| 540 |
+
Aaug = [row + eyerow for row, eyerow in zip(a, eye)]
|
| 541 |
+
pivots = ddm_irref(Aaug)
|
| 542 |
+
if pivots != list(range(n)):
|
| 543 |
+
raise DMNonInvertibleMatrixError('Matrix det == 0; not invertible.')
|
| 544 |
+
ainv[:] = [row[n:] for row in Aaug]
|
| 545 |
+
|
| 546 |
+
|
| 547 |
+
def ddm_ilu_split(L, U, K):
|
| 548 |
+
"""L, U <-- LU(U)
|
| 549 |
+
|
| 550 |
+
Compute the LU decomposition of a matrix $L$ in place and store the lower
|
| 551 |
+
and upper triangular matrices in $L$ and $U$, respectively. Returns a list
|
| 552 |
+
of row swaps that were performed.
|
| 553 |
+
|
| 554 |
+
Uses division in the ground domain which should be an exact field.
|
| 555 |
+
|
| 556 |
+
Examples
|
| 557 |
+
========
|
| 558 |
+
|
| 559 |
+
>>> from sympy.polys.matrices.ddm import ddm_ilu_split
|
| 560 |
+
>>> from sympy import QQ
|
| 561 |
+
>>> L = [[QQ(0), QQ(0)], [QQ(0), QQ(0)]]
|
| 562 |
+
>>> U = [[QQ(1), QQ(2)], [QQ(3), QQ(4)]]
|
| 563 |
+
>>> swaps = ddm_ilu_split(L, U, QQ)
|
| 564 |
+
>>> swaps
|
| 565 |
+
[]
|
| 566 |
+
>>> L
|
| 567 |
+
[[0, 0], [3, 0]]
|
| 568 |
+
>>> U
|
| 569 |
+
[[1, 2], [0, -2]]
|
| 570 |
+
|
| 571 |
+
See Also
|
| 572 |
+
========
|
| 573 |
+
|
| 574 |
+
ddm_ilu
|
| 575 |
+
ddm_ilu_solve
|
| 576 |
+
"""
|
| 577 |
+
m = len(U)
|
| 578 |
+
if not m:
|
| 579 |
+
return []
|
| 580 |
+
n = len(U[0])
|
| 581 |
+
|
| 582 |
+
swaps = ddm_ilu(U)
|
| 583 |
+
|
| 584 |
+
zeros = [K.zero] * min(m, n)
|
| 585 |
+
for i in range(1, m):
|
| 586 |
+
j = min(i, n)
|
| 587 |
+
L[i][:j] = U[i][:j]
|
| 588 |
+
U[i][:j] = zeros[:j]
|
| 589 |
+
|
| 590 |
+
return swaps
|
| 591 |
+
|
| 592 |
+
|
| 593 |
+
def ddm_ilu(a):
|
| 594 |
+
"""a <-- LU(a)
|
| 595 |
+
|
| 596 |
+
Computes the LU decomposition of a matrix in place. Returns a list of
|
| 597 |
+
row swaps that were performed.
|
| 598 |
+
|
| 599 |
+
Uses division in the ground domain which should be an exact field.
|
| 600 |
+
|
| 601 |
+
This is only suitable for domains like :ref:`GF(p)`, :ref:`QQ`, :ref:`QQ_I`
|
| 602 |
+
and :ref:`QQ(a)`. With a rational function field like :ref:`K(x)` it is
|
| 603 |
+
better to clear denominators and use division-free algorithms. Pivoting is
|
| 604 |
+
used to avoid exact zeros but not for floating point accuracy so :ref:`RR`
|
| 605 |
+
and :ref:`CC` are not suitable (use :func:`ddm_irref` instead).
|
| 606 |
+
|
| 607 |
+
Examples
|
| 608 |
+
========
|
| 609 |
+
|
| 610 |
+
>>> from sympy.polys.matrices.dense import ddm_ilu
|
| 611 |
+
>>> from sympy import QQ
|
| 612 |
+
>>> a = [[QQ(1, 2), QQ(1, 3)], [QQ(1, 4), QQ(1, 5)]]
|
| 613 |
+
>>> swaps = ddm_ilu(a)
|
| 614 |
+
>>> swaps
|
| 615 |
+
[]
|
| 616 |
+
>>> a
|
| 617 |
+
[[1/2, 1/3], [1/2, 1/30]]
|
| 618 |
+
|
| 619 |
+
The same example using ``Matrix``:
|
| 620 |
+
|
| 621 |
+
>>> from sympy import Matrix, S
|
| 622 |
+
>>> M = Matrix([[S(1)/2, S(1)/3], [S(1)/4, S(1)/5]])
|
| 623 |
+
>>> L, U, swaps = M.LUdecomposition()
|
| 624 |
+
>>> L
|
| 625 |
+
Matrix([
|
| 626 |
+
[ 1, 0],
|
| 627 |
+
[1/2, 1]])
|
| 628 |
+
>>> U
|
| 629 |
+
Matrix([
|
| 630 |
+
[1/2, 1/3],
|
| 631 |
+
[ 0, 1/30]])
|
| 632 |
+
>>> swaps
|
| 633 |
+
[]
|
| 634 |
+
|
| 635 |
+
See Also
|
| 636 |
+
========
|
| 637 |
+
|
| 638 |
+
ddm_irref
|
| 639 |
+
ddm_ilu_solve
|
| 640 |
+
sympy.matrices.matrixbase.MatrixBase.LUdecomposition
|
| 641 |
+
"""
|
| 642 |
+
m = len(a)
|
| 643 |
+
if not m:
|
| 644 |
+
return []
|
| 645 |
+
n = len(a[0])
|
| 646 |
+
|
| 647 |
+
swaps = []
|
| 648 |
+
|
| 649 |
+
for i in range(min(m, n)):
|
| 650 |
+
if not a[i][i]:
|
| 651 |
+
for ip in range(i+1, m):
|
| 652 |
+
if a[ip][i]:
|
| 653 |
+
swaps.append((i, ip))
|
| 654 |
+
a[i], a[ip] = a[ip], a[i]
|
| 655 |
+
break
|
| 656 |
+
else:
|
| 657 |
+
# M = Matrix([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 1], [0, 0, 1, 2]])
|
| 658 |
+
continue
|
| 659 |
+
for j in range(i+1, m):
|
| 660 |
+
l_ji = a[j][i] / a[i][i]
|
| 661 |
+
a[j][i] = l_ji
|
| 662 |
+
for k in range(i+1, n):
|
| 663 |
+
a[j][k] -= l_ji * a[i][k]
|
| 664 |
+
|
| 665 |
+
return swaps
|
| 666 |
+
|
| 667 |
+
|
| 668 |
+
def ddm_ilu_solve(x, L, U, swaps, b):
|
| 669 |
+
"""x <-- solve(L*U*x = swaps(b))
|
| 670 |
+
|
| 671 |
+
Solve a linear system, $A*x = b$, given an LU factorization of $A$.
|
| 672 |
+
|
| 673 |
+
Uses division in the ground domain which must be a field.
|
| 674 |
+
|
| 675 |
+
Modifies $x$ in place.
|
| 676 |
+
|
| 677 |
+
Examples
|
| 678 |
+
========
|
| 679 |
+
|
| 680 |
+
Compute the LU decomposition of $A$ (in place):
|
| 681 |
+
|
| 682 |
+
>>> from sympy import QQ
|
| 683 |
+
>>> from sympy.polys.matrices.dense import ddm_ilu, ddm_ilu_solve
|
| 684 |
+
>>> A = [[QQ(1), QQ(2)], [QQ(3), QQ(4)]]
|
| 685 |
+
>>> swaps = ddm_ilu(A)
|
| 686 |
+
>>> A
|
| 687 |
+
[[1, 2], [3, -2]]
|
| 688 |
+
>>> L = U = A
|
| 689 |
+
|
| 690 |
+
Solve the linear system:
|
| 691 |
+
|
| 692 |
+
>>> b = [[QQ(5)], [QQ(6)]]
|
| 693 |
+
>>> x = [[None], [None]]
|
| 694 |
+
>>> ddm_ilu_solve(x, L, U, swaps, b)
|
| 695 |
+
>>> x
|
| 696 |
+
[[-4], [9/2]]
|
| 697 |
+
|
| 698 |
+
See Also
|
| 699 |
+
========
|
| 700 |
+
|
| 701 |
+
ddm_ilu
|
| 702 |
+
Compute the LU decomposition of a matrix in place.
|
| 703 |
+
ddm_ilu_split
|
| 704 |
+
Compute the LU decomposition of a matrix and separate $L$ and $U$.
|
| 705 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.lu_solve
|
| 706 |
+
Higher level interface to this function.
|
| 707 |
+
"""
|
| 708 |
+
m = len(U)
|
| 709 |
+
if not m:
|
| 710 |
+
return
|
| 711 |
+
n = len(U[0])
|
| 712 |
+
|
| 713 |
+
m2 = len(b)
|
| 714 |
+
if not m2:
|
| 715 |
+
raise DMShapeError("Shape mismtch")
|
| 716 |
+
o = len(b[0])
|
| 717 |
+
|
| 718 |
+
if m != m2:
|
| 719 |
+
raise DMShapeError("Shape mismtch")
|
| 720 |
+
if m < n:
|
| 721 |
+
raise NotImplementedError("Underdetermined")
|
| 722 |
+
|
| 723 |
+
if swaps:
|
| 724 |
+
b = [row[:] for row in b]
|
| 725 |
+
for i1, i2 in swaps:
|
| 726 |
+
b[i1], b[i2] = b[i2], b[i1]
|
| 727 |
+
|
| 728 |
+
# solve Ly = b
|
| 729 |
+
y = [[None] * o for _ in range(m)]
|
| 730 |
+
for k in range(o):
|
| 731 |
+
for i in range(m):
|
| 732 |
+
rhs = b[i][k]
|
| 733 |
+
for j in range(i):
|
| 734 |
+
rhs -= L[i][j] * y[j][k]
|
| 735 |
+
y[i][k] = rhs
|
| 736 |
+
|
| 737 |
+
if m > n:
|
| 738 |
+
for i in range(n, m):
|
| 739 |
+
for j in range(o):
|
| 740 |
+
if y[i][j]:
|
| 741 |
+
raise DMNonInvertibleMatrixError
|
| 742 |
+
|
| 743 |
+
# Solve Ux = y
|
| 744 |
+
for k in range(o):
|
| 745 |
+
for i in reversed(range(n)):
|
| 746 |
+
if not U[i][i]:
|
| 747 |
+
raise DMNonInvertibleMatrixError
|
| 748 |
+
rhs = y[i][k]
|
| 749 |
+
for j in range(i+1, n):
|
| 750 |
+
rhs -= U[i][j] * x[j][k]
|
| 751 |
+
x[i][k] = rhs / U[i][i]
|
| 752 |
+
|
| 753 |
+
|
| 754 |
+
def ddm_berk(M, K):
|
| 755 |
+
"""
|
| 756 |
+
Berkowitz algorithm for computing the characteristic polynomial.
|
| 757 |
+
|
| 758 |
+
Explanation
|
| 759 |
+
===========
|
| 760 |
+
|
| 761 |
+
The Berkowitz algorithm is a division-free algorithm for computing the
|
| 762 |
+
characteristic polynomial of a matrix over any commutative ring using only
|
| 763 |
+
arithmetic in the coefficient ring.
|
| 764 |
+
|
| 765 |
+
Examples
|
| 766 |
+
========
|
| 767 |
+
|
| 768 |
+
>>> from sympy import Matrix
|
| 769 |
+
>>> from sympy.polys.matrices.dense import ddm_berk
|
| 770 |
+
>>> from sympy.polys.domains import ZZ
|
| 771 |
+
>>> M = [[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]]
|
| 772 |
+
>>> ddm_berk(M, ZZ)
|
| 773 |
+
[[1], [-5], [-2]]
|
| 774 |
+
>>> Matrix(M).charpoly()
|
| 775 |
+
PurePoly(lambda**2 - 5*lambda - 2, lambda, domain='ZZ')
|
| 776 |
+
|
| 777 |
+
See Also
|
| 778 |
+
========
|
| 779 |
+
|
| 780 |
+
sympy.polys.matrices.domainmatrix.DomainMatrix.charpoly
|
| 781 |
+
The high-level interface to this function.
|
| 782 |
+
|
| 783 |
+
References
|
| 784 |
+
==========
|
| 785 |
+
|
| 786 |
+
.. [1] https://en.wikipedia.org/wiki/Samuelson%E2%80%93Berkowitz_algorithm
|
| 787 |
+
"""
|
| 788 |
+
m = len(M)
|
| 789 |
+
if not m:
|
| 790 |
+
return [[K.one]]
|
| 791 |
+
n = len(M[0])
|
| 792 |
+
|
| 793 |
+
if m != n:
|
| 794 |
+
raise DMShapeError("Not square")
|
| 795 |
+
|
| 796 |
+
if n == 1:
|
| 797 |
+
return [[K.one], [-M[0][0]]]
|
| 798 |
+
|
| 799 |
+
a = M[0][0]
|
| 800 |
+
R = [M[0][1:]]
|
| 801 |
+
C = [[row[0]] for row in M[1:]]
|
| 802 |
+
A = [row[1:] for row in M[1:]]
|
| 803 |
+
|
| 804 |
+
q = ddm_berk(A, K)
|
| 805 |
+
|
| 806 |
+
T = [[K.zero] * n for _ in range(n+1)]
|
| 807 |
+
for i in range(n):
|
| 808 |
+
T[i][i] = K.one
|
| 809 |
+
T[i+1][i] = -a
|
| 810 |
+
for i in range(2, n+1):
|
| 811 |
+
if i == 2:
|
| 812 |
+
AnC = C
|
| 813 |
+
else:
|
| 814 |
+
C = AnC
|
| 815 |
+
AnC = [[K.zero] for row in C]
|
| 816 |
+
ddm_imatmul(AnC, A, C)
|
| 817 |
+
RAnC = [[K.zero]]
|
| 818 |
+
ddm_imatmul(RAnC, R, AnC)
|
| 819 |
+
for j in range(0, n+1-i):
|
| 820 |
+
T[i+j][j] = -RAnC[0][0]
|
| 821 |
+
|
| 822 |
+
qout = [[K.zero] for _ in range(n+1)]
|
| 823 |
+
ddm_imatmul(qout, T, q)
|
| 824 |
+
return qout
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/dfm.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
sympy.polys.matrices.dfm
|
| 3 |
+
|
| 4 |
+
Provides the :class:`DFM` class if ``GROUND_TYPES=flint'``. Otherwise, ``DFM``
|
| 5 |
+
is a placeholder class that raises NotImplementedError when instantiated.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from sympy.external.gmpy import GROUND_TYPES
|
| 9 |
+
|
| 10 |
+
if GROUND_TYPES == "flint": # pragma: no cover
|
| 11 |
+
# When python-flint is installed we will try to use it for dense matrices
|
| 12 |
+
# if the domain is supported by python-flint.
|
| 13 |
+
from ._dfm import DFM
|
| 14 |
+
|
| 15 |
+
else: # pragma: no cover
|
| 16 |
+
# Other code should be able to import this and it should just present as a
|
| 17 |
+
# version of DFM that does not support any domains.
|
| 18 |
+
class DFM_dummy:
|
| 19 |
+
"""
|
| 20 |
+
Placeholder class for DFM when python-flint is not installed.
|
| 21 |
+
"""
|
| 22 |
+
def __init__(*args, **kwargs):
|
| 23 |
+
raise NotImplementedError("DFM requires GROUND_TYPES=flint.")
|
| 24 |
+
|
| 25 |
+
@classmethod
|
| 26 |
+
def _supports_domain(cls, domain):
|
| 27 |
+
return False
|
| 28 |
+
|
| 29 |
+
@classmethod
|
| 30 |
+
def _get_flint_func(cls, domain):
|
| 31 |
+
raise NotImplementedError("DFM requires GROUND_TYPES=flint.")
|
| 32 |
+
|
| 33 |
+
# mypy really struggles with this kind of conditional type assignment.
|
| 34 |
+
# Maybe there is a better way to annotate this rather than type: ignore.
|
| 35 |
+
DFM = DFM_dummy # type: ignore
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/domainmatrix.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/domainscalar.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
|
| 3 |
+
Module for the DomainScalar class.
|
| 4 |
+
|
| 5 |
+
A DomainScalar represents an element which is in a particular
|
| 6 |
+
Domain. The idea is that the DomainScalar class provides the
|
| 7 |
+
convenience routines for unifying elements with different domains.
|
| 8 |
+
|
| 9 |
+
It assists in Scalar Multiplication and getitem for DomainMatrix.
|
| 10 |
+
|
| 11 |
+
"""
|
| 12 |
+
from ..constructor import construct_domain
|
| 13 |
+
|
| 14 |
+
from sympy.polys.domains import Domain, ZZ
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class DomainScalar:
|
| 18 |
+
r"""
|
| 19 |
+
docstring
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
def __new__(cls, element, domain):
|
| 23 |
+
if not isinstance(domain, Domain):
|
| 24 |
+
raise TypeError("domain should be of type Domain")
|
| 25 |
+
if not domain.of_type(element):
|
| 26 |
+
raise TypeError("element %s should be in domain %s" % (element, domain))
|
| 27 |
+
return cls.new(element, domain)
|
| 28 |
+
|
| 29 |
+
@classmethod
|
| 30 |
+
def new(cls, element, domain):
|
| 31 |
+
obj = super().__new__(cls)
|
| 32 |
+
obj.element = element
|
| 33 |
+
obj.domain = domain
|
| 34 |
+
return obj
|
| 35 |
+
|
| 36 |
+
def __repr__(self):
|
| 37 |
+
return repr(self.element)
|
| 38 |
+
|
| 39 |
+
@classmethod
|
| 40 |
+
def from_sympy(cls, expr):
|
| 41 |
+
[domain, [element]] = construct_domain([expr])
|
| 42 |
+
return cls.new(element, domain)
|
| 43 |
+
|
| 44 |
+
def to_sympy(self):
|
| 45 |
+
return self.domain.to_sympy(self.element)
|
| 46 |
+
|
| 47 |
+
def to_domain(self, domain):
|
| 48 |
+
element = domain.convert_from(self.element, self.domain)
|
| 49 |
+
return self.new(element, domain)
|
| 50 |
+
|
| 51 |
+
def convert_to(self, domain):
|
| 52 |
+
return self.to_domain(domain)
|
| 53 |
+
|
| 54 |
+
def unify(self, other):
|
| 55 |
+
domain = self.domain.unify(other.domain)
|
| 56 |
+
return self.to_domain(domain), other.to_domain(domain)
|
| 57 |
+
|
| 58 |
+
def __bool__(self):
|
| 59 |
+
return bool(self.element)
|
| 60 |
+
|
| 61 |
+
def __add__(self, other):
|
| 62 |
+
if not isinstance(other, DomainScalar):
|
| 63 |
+
return NotImplemented
|
| 64 |
+
self, other = self.unify(other)
|
| 65 |
+
return self.new(self.element + other.element, self.domain)
|
| 66 |
+
|
| 67 |
+
def __sub__(self, other):
|
| 68 |
+
if not isinstance(other, DomainScalar):
|
| 69 |
+
return NotImplemented
|
| 70 |
+
self, other = self.unify(other)
|
| 71 |
+
return self.new(self.element - other.element, self.domain)
|
| 72 |
+
|
| 73 |
+
def __mul__(self, other):
|
| 74 |
+
if not isinstance(other, DomainScalar):
|
| 75 |
+
if isinstance(other, int):
|
| 76 |
+
other = DomainScalar(ZZ(other), ZZ)
|
| 77 |
+
else:
|
| 78 |
+
return NotImplemented
|
| 79 |
+
|
| 80 |
+
self, other = self.unify(other)
|
| 81 |
+
return self.new(self.element * other.element, self.domain)
|
| 82 |
+
|
| 83 |
+
def __floordiv__(self, other):
|
| 84 |
+
if not isinstance(other, DomainScalar):
|
| 85 |
+
return NotImplemented
|
| 86 |
+
self, other = self.unify(other)
|
| 87 |
+
return self.new(self.domain.quo(self.element, other.element), self.domain)
|
| 88 |
+
|
| 89 |
+
def __mod__(self, other):
|
| 90 |
+
if not isinstance(other, DomainScalar):
|
| 91 |
+
return NotImplemented
|
| 92 |
+
self, other = self.unify(other)
|
| 93 |
+
return self.new(self.domain.rem(self.element, other.element), self.domain)
|
| 94 |
+
|
| 95 |
+
def __divmod__(self, other):
|
| 96 |
+
if not isinstance(other, DomainScalar):
|
| 97 |
+
return NotImplemented
|
| 98 |
+
self, other = self.unify(other)
|
| 99 |
+
q, r = self.domain.div(self.element, other.element)
|
| 100 |
+
return (self.new(q, self.domain), self.new(r, self.domain))
|
| 101 |
+
|
| 102 |
+
def __pow__(self, n):
|
| 103 |
+
if not isinstance(n, int):
|
| 104 |
+
return NotImplemented
|
| 105 |
+
return self.new(self.element**n, self.domain)
|
| 106 |
+
|
| 107 |
+
def __pos__(self):
|
| 108 |
+
return self.new(+self.element, self.domain)
|
| 109 |
+
|
| 110 |
+
def __neg__(self):
|
| 111 |
+
return self.new(-self.element, self.domain)
|
| 112 |
+
|
| 113 |
+
def __eq__(self, other):
|
| 114 |
+
if not isinstance(other, DomainScalar):
|
| 115 |
+
return NotImplemented
|
| 116 |
+
return self.element == other.element and self.domain == other.domain
|
| 117 |
+
|
| 118 |
+
def is_zero(self):
|
| 119 |
+
return self.element == self.domain.zero
|
| 120 |
+
|
| 121 |
+
def is_one(self):
|
| 122 |
+
return self.element == self.domain.one
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/eigen.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
|
| 3 |
+
Routines for computing eigenvectors with DomainMatrix.
|
| 4 |
+
|
| 5 |
+
"""
|
| 6 |
+
from sympy.core.symbol import Dummy
|
| 7 |
+
|
| 8 |
+
from ..agca.extensions import FiniteExtension
|
| 9 |
+
from ..factortools import dup_factor_list
|
| 10 |
+
from ..polyroots import roots
|
| 11 |
+
from ..polytools import Poly
|
| 12 |
+
from ..rootoftools import CRootOf
|
| 13 |
+
|
| 14 |
+
from .domainmatrix import DomainMatrix
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def dom_eigenvects(A, l=Dummy('lambda')):
|
| 18 |
+
charpoly = A.charpoly()
|
| 19 |
+
rows, cols = A.shape
|
| 20 |
+
domain = A.domain
|
| 21 |
+
_, factors = dup_factor_list(charpoly, domain)
|
| 22 |
+
|
| 23 |
+
rational_eigenvects = []
|
| 24 |
+
algebraic_eigenvects = []
|
| 25 |
+
for base, exp in factors:
|
| 26 |
+
if len(base) == 2:
|
| 27 |
+
field = domain
|
| 28 |
+
eigenval = -base[1] / base[0]
|
| 29 |
+
|
| 30 |
+
EE_items = [
|
| 31 |
+
[eigenval if i == j else field.zero for j in range(cols)]
|
| 32 |
+
for i in range(rows)]
|
| 33 |
+
EE = DomainMatrix(EE_items, (rows, cols), field)
|
| 34 |
+
|
| 35 |
+
basis = (A - EE).nullspace(divide_last=True)
|
| 36 |
+
rational_eigenvects.append((field, eigenval, exp, basis))
|
| 37 |
+
else:
|
| 38 |
+
minpoly = Poly.from_list(base, l, domain=domain)
|
| 39 |
+
field = FiniteExtension(minpoly)
|
| 40 |
+
eigenval = field(l)
|
| 41 |
+
|
| 42 |
+
AA_items = [
|
| 43 |
+
[Poly.from_list([item], l, domain=domain).rep for item in row]
|
| 44 |
+
for row in A.rep.to_ddm()]
|
| 45 |
+
AA_items = [[field(item) for item in row] for row in AA_items]
|
| 46 |
+
AA = DomainMatrix(AA_items, (rows, cols), field)
|
| 47 |
+
EE_items = [
|
| 48 |
+
[eigenval if i == j else field.zero for j in range(cols)]
|
| 49 |
+
for i in range(rows)]
|
| 50 |
+
EE = DomainMatrix(EE_items, (rows, cols), field)
|
| 51 |
+
|
| 52 |
+
basis = (AA - EE).nullspace(divide_last=True)
|
| 53 |
+
algebraic_eigenvects.append((field, minpoly, exp, basis))
|
| 54 |
+
|
| 55 |
+
return rational_eigenvects, algebraic_eigenvects
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def dom_eigenvects_to_sympy(
|
| 59 |
+
rational_eigenvects, algebraic_eigenvects,
|
| 60 |
+
Matrix, **kwargs
|
| 61 |
+
):
|
| 62 |
+
result = []
|
| 63 |
+
|
| 64 |
+
for field, eigenvalue, multiplicity, eigenvects in rational_eigenvects:
|
| 65 |
+
eigenvects = eigenvects.rep.to_ddm()
|
| 66 |
+
eigenvalue = field.to_sympy(eigenvalue)
|
| 67 |
+
new_eigenvects = [
|
| 68 |
+
Matrix([field.to_sympy(x) for x in vect])
|
| 69 |
+
for vect in eigenvects]
|
| 70 |
+
result.append((eigenvalue, multiplicity, new_eigenvects))
|
| 71 |
+
|
| 72 |
+
for field, minpoly, multiplicity, eigenvects in algebraic_eigenvects:
|
| 73 |
+
eigenvects = eigenvects.rep.to_ddm()
|
| 74 |
+
l = minpoly.gens[0]
|
| 75 |
+
|
| 76 |
+
eigenvects = [[field.to_sympy(x) for x in vect] for vect in eigenvects]
|
| 77 |
+
|
| 78 |
+
degree = minpoly.degree()
|
| 79 |
+
minpoly = minpoly.as_expr()
|
| 80 |
+
eigenvals = roots(minpoly, l, **kwargs)
|
| 81 |
+
if len(eigenvals) != degree:
|
| 82 |
+
eigenvals = [CRootOf(minpoly, l, idx) for idx in range(degree)]
|
| 83 |
+
|
| 84 |
+
for eigenvalue in eigenvals:
|
| 85 |
+
new_eigenvects = [
|
| 86 |
+
Matrix([x.subs(l, eigenvalue) for x in vect])
|
| 87 |
+
for vect in eigenvects]
|
| 88 |
+
result.append((eigenvalue, multiplicity, new_eigenvects))
|
| 89 |
+
|
| 90 |
+
return result
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/exceptions.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
|
| 3 |
+
Module to define exceptions to be used in sympy.polys.matrices modules and
|
| 4 |
+
classes.
|
| 5 |
+
|
| 6 |
+
Ideally all exceptions raised in these modules would be defined and documented
|
| 7 |
+
here and not e.g. imported from matrices. Also ideally generic exceptions like
|
| 8 |
+
ValueError/TypeError would not be raised anywhere.
|
| 9 |
+
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class DMError(Exception):
|
| 14 |
+
"""Base class for errors raised by DomainMatrix"""
|
| 15 |
+
pass
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class DMBadInputError(DMError):
|
| 19 |
+
"""list of lists is inconsistent with shape"""
|
| 20 |
+
pass
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class DMDomainError(DMError):
|
| 24 |
+
"""domains do not match"""
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class DMNotAField(DMDomainError):
|
| 29 |
+
"""domain is not a field"""
|
| 30 |
+
pass
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class DMFormatError(DMError):
|
| 34 |
+
"""mixed dense/sparse not supported"""
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class DMNonInvertibleMatrixError(DMError):
|
| 39 |
+
"""The matrix in not invertible"""
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class DMRankError(DMError):
|
| 44 |
+
"""matrix does not have expected rank"""
|
| 45 |
+
pass
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class DMShapeError(DMError):
|
| 49 |
+
"""shapes are inconsistent"""
|
| 50 |
+
pass
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class DMNonSquareMatrixError(DMShapeError):
|
| 54 |
+
"""The matrix is not square"""
|
| 55 |
+
pass
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class DMValueError(DMError):
|
| 59 |
+
"""The value passed is invalid"""
|
| 60 |
+
pass
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
__all__ = [
|
| 64 |
+
'DMError', 'DMBadInputError', 'DMDomainError', 'DMFormatError',
|
| 65 |
+
'DMRankError', 'DMShapeError', 'DMNotAField',
|
| 66 |
+
'DMNonInvertibleMatrixError', 'DMNonSquareMatrixError', 'DMValueError'
|
| 67 |
+
]
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/linsolve.py
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# sympy.polys.matrices.linsolve module
|
| 3 |
+
#
|
| 4 |
+
# This module defines the _linsolve function which is the internal workhorse
|
| 5 |
+
# used by linsolve. This computes the solution of a system of linear equations
|
| 6 |
+
# using the SDM sparse matrix implementation in sympy.polys.matrices.sdm. This
|
| 7 |
+
# is a replacement for solve_lin_sys in sympy.polys.solvers which is
|
| 8 |
+
# inefficient for large sparse systems due to the use of a PolyRing with many
|
| 9 |
+
# generators:
|
| 10 |
+
#
|
| 11 |
+
# https://github.com/sympy/sympy/issues/20857
|
| 12 |
+
#
|
| 13 |
+
# The implementation of _linsolve here handles:
|
| 14 |
+
#
|
| 15 |
+
# - Extracting the coefficients from the Expr/Eq input equations.
|
| 16 |
+
# - Constructing a domain and converting the coefficients to
|
| 17 |
+
# that domain.
|
| 18 |
+
# - Using the SDM.rref, SDM.nullspace etc methods to generate the full
|
| 19 |
+
# solution working with arithmetic only in the domain of the coefficients.
|
| 20 |
+
#
|
| 21 |
+
# The routines here are particularly designed to be efficient for large sparse
|
| 22 |
+
# systems of linear equations although as well as dense systems. It is
|
| 23 |
+
# possible that for some small dense systems solve_lin_sys which uses the
|
| 24 |
+
# dense matrix implementation DDM will be more efficient. With smaller systems
|
| 25 |
+
# though the bulk of the time is spent just preprocessing the inputs and the
|
| 26 |
+
# relative time spent in rref is too small to be noticeable.
|
| 27 |
+
#
|
| 28 |
+
|
| 29 |
+
from collections import defaultdict
|
| 30 |
+
|
| 31 |
+
from sympy.core.add import Add
|
| 32 |
+
from sympy.core.mul import Mul
|
| 33 |
+
from sympy.core.singleton import S
|
| 34 |
+
|
| 35 |
+
from sympy.polys.constructor import construct_domain
|
| 36 |
+
from sympy.polys.solvers import PolyNonlinearError
|
| 37 |
+
|
| 38 |
+
from .sdm import (
|
| 39 |
+
SDM,
|
| 40 |
+
sdm_irref,
|
| 41 |
+
sdm_particular_from_rref,
|
| 42 |
+
sdm_nullspace_from_rref
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
from sympy.utilities.misc import filldedent
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _linsolve(eqs, syms):
|
| 49 |
+
|
| 50 |
+
"""Solve a linear system of equations.
|
| 51 |
+
|
| 52 |
+
Examples
|
| 53 |
+
========
|
| 54 |
+
|
| 55 |
+
Solve a linear system with a unique solution:
|
| 56 |
+
|
| 57 |
+
>>> from sympy import symbols, Eq
|
| 58 |
+
>>> from sympy.polys.matrices.linsolve import _linsolve
|
| 59 |
+
>>> x, y = symbols('x, y')
|
| 60 |
+
>>> eqs = [Eq(x + y, 1), Eq(x - y, 2)]
|
| 61 |
+
>>> _linsolve(eqs, [x, y])
|
| 62 |
+
{x: 3/2, y: -1/2}
|
| 63 |
+
|
| 64 |
+
In the case of underdetermined systems the solution will be expressed in
|
| 65 |
+
terms of the unknown symbols that are unconstrained:
|
| 66 |
+
|
| 67 |
+
>>> _linsolve([Eq(x + y, 0)], [x, y])
|
| 68 |
+
{x: -y, y: y}
|
| 69 |
+
|
| 70 |
+
"""
|
| 71 |
+
# Number of unknowns (columns in the non-augmented matrix)
|
| 72 |
+
nsyms = len(syms)
|
| 73 |
+
|
| 74 |
+
# Convert to sparse augmented matrix (len(eqs) x (nsyms+1))
|
| 75 |
+
eqsdict, const = _linear_eq_to_dict(eqs, syms)
|
| 76 |
+
Aaug = sympy_dict_to_dm(eqsdict, const, syms)
|
| 77 |
+
K = Aaug.domain
|
| 78 |
+
|
| 79 |
+
# sdm_irref has issues with float matrices. This uses the ddm_rref()
|
| 80 |
+
# function. When sdm_rref() can handle float matrices reasonably this
|
| 81 |
+
# should be removed...
|
| 82 |
+
if K.is_RealField or K.is_ComplexField:
|
| 83 |
+
Aaug = Aaug.to_ddm().rref()[0].to_sdm()
|
| 84 |
+
|
| 85 |
+
# Compute reduced-row echelon form (RREF)
|
| 86 |
+
Arref, pivots, nzcols = sdm_irref(Aaug)
|
| 87 |
+
|
| 88 |
+
# No solution:
|
| 89 |
+
if pivots and pivots[-1] == nsyms:
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
# Particular solution for non-homogeneous system:
|
| 93 |
+
P = sdm_particular_from_rref(Arref, nsyms+1, pivots)
|
| 94 |
+
|
| 95 |
+
# Nullspace - general solution to homogeneous system
|
| 96 |
+
# Note: using nsyms not nsyms+1 to ignore last column
|
| 97 |
+
V, nonpivots = sdm_nullspace_from_rref(Arref, K.one, nsyms, pivots, nzcols)
|
| 98 |
+
|
| 99 |
+
# Collect together terms from particular and nullspace:
|
| 100 |
+
sol = defaultdict(list)
|
| 101 |
+
for i, v in P.items():
|
| 102 |
+
sol[syms[i]].append(K.to_sympy(v))
|
| 103 |
+
for npi, Vi in zip(nonpivots, V):
|
| 104 |
+
sym = syms[npi]
|
| 105 |
+
for i, v in Vi.items():
|
| 106 |
+
sol[syms[i]].append(sym * K.to_sympy(v))
|
| 107 |
+
|
| 108 |
+
# Use a single call to Add for each term:
|
| 109 |
+
sol = {s: Add(*terms) for s, terms in sol.items()}
|
| 110 |
+
|
| 111 |
+
# Fill in the zeros:
|
| 112 |
+
zero = S.Zero
|
| 113 |
+
for s in set(syms) - set(sol):
|
| 114 |
+
sol[s] = zero
|
| 115 |
+
|
| 116 |
+
# All done!
|
| 117 |
+
return sol
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def sympy_dict_to_dm(eqs_coeffs, eqs_rhs, syms):
|
| 121 |
+
"""Convert a system of dict equations to a sparse augmented matrix"""
|
| 122 |
+
elems = set(eqs_rhs).union(*(e.values() for e in eqs_coeffs))
|
| 123 |
+
K, elems_K = construct_domain(elems, field=True, extension=True)
|
| 124 |
+
elem_map = dict(zip(elems, elems_K))
|
| 125 |
+
neqs = len(eqs_coeffs)
|
| 126 |
+
nsyms = len(syms)
|
| 127 |
+
sym2index = dict(zip(syms, range(nsyms)))
|
| 128 |
+
eqsdict = []
|
| 129 |
+
for eq, rhs in zip(eqs_coeffs, eqs_rhs):
|
| 130 |
+
eqdict = {sym2index[s]: elem_map[c] for s, c in eq.items()}
|
| 131 |
+
if rhs:
|
| 132 |
+
eqdict[nsyms] = -elem_map[rhs]
|
| 133 |
+
if eqdict:
|
| 134 |
+
eqsdict.append(eqdict)
|
| 135 |
+
sdm_aug = SDM(enumerate(eqsdict), (neqs, nsyms + 1), K)
|
| 136 |
+
return sdm_aug
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def _linear_eq_to_dict(eqs, syms):
|
| 140 |
+
"""Convert a system Expr/Eq equations into dict form, returning
|
| 141 |
+
the coefficient dictionaries and a list of syms-independent terms
|
| 142 |
+
from each expression in ``eqs```.
|
| 143 |
+
|
| 144 |
+
Examples
|
| 145 |
+
========
|
| 146 |
+
|
| 147 |
+
>>> from sympy.polys.matrices.linsolve import _linear_eq_to_dict
|
| 148 |
+
>>> from sympy.abc import x
|
| 149 |
+
>>> _linear_eq_to_dict([2*x + 3], {x})
|
| 150 |
+
([{x: 2}], [3])
|
| 151 |
+
"""
|
| 152 |
+
coeffs = []
|
| 153 |
+
ind = []
|
| 154 |
+
symset = set(syms)
|
| 155 |
+
for e in eqs:
|
| 156 |
+
if e.is_Equality:
|
| 157 |
+
coeff, terms = _lin_eq2dict(e.lhs, symset)
|
| 158 |
+
cR, tR = _lin_eq2dict(e.rhs, symset)
|
| 159 |
+
# there were no nonlinear errors so now
|
| 160 |
+
# cancellation is allowed
|
| 161 |
+
coeff -= cR
|
| 162 |
+
for k, v in tR.items():
|
| 163 |
+
if k in terms:
|
| 164 |
+
terms[k] -= v
|
| 165 |
+
else:
|
| 166 |
+
terms[k] = -v
|
| 167 |
+
# don't store coefficients of 0, however
|
| 168 |
+
terms = {k: v for k, v in terms.items() if v}
|
| 169 |
+
c, d = coeff, terms
|
| 170 |
+
else:
|
| 171 |
+
c, d = _lin_eq2dict(e, symset)
|
| 172 |
+
coeffs.append(d)
|
| 173 |
+
ind.append(c)
|
| 174 |
+
return coeffs, ind
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def _lin_eq2dict(a, symset):
|
| 178 |
+
"""return (c, d) where c is the sym-independent part of ``a`` and
|
| 179 |
+
``d`` is an efficiently calculated dictionary mapping symbols to
|
| 180 |
+
their coefficients. A PolyNonlinearError is raised if non-linearity
|
| 181 |
+
is detected.
|
| 182 |
+
|
| 183 |
+
The values in the dictionary will be non-zero.
|
| 184 |
+
|
| 185 |
+
Examples
|
| 186 |
+
========
|
| 187 |
+
|
| 188 |
+
>>> from sympy.polys.matrices.linsolve import _lin_eq2dict
|
| 189 |
+
>>> from sympy.abc import x, y
|
| 190 |
+
>>> _lin_eq2dict(x + 2*y + 3, {x, y})
|
| 191 |
+
(3, {x: 1, y: 2})
|
| 192 |
+
"""
|
| 193 |
+
if a in symset:
|
| 194 |
+
return S.Zero, {a: S.One}
|
| 195 |
+
elif a.is_Add:
|
| 196 |
+
terms_list = defaultdict(list)
|
| 197 |
+
coeff_list = []
|
| 198 |
+
for ai in a.args:
|
| 199 |
+
ci, ti = _lin_eq2dict(ai, symset)
|
| 200 |
+
coeff_list.append(ci)
|
| 201 |
+
for mij, cij in ti.items():
|
| 202 |
+
terms_list[mij].append(cij)
|
| 203 |
+
coeff = Add(*coeff_list)
|
| 204 |
+
terms = {sym: Add(*coeffs) for sym, coeffs in terms_list.items()}
|
| 205 |
+
return coeff, terms
|
| 206 |
+
elif a.is_Mul:
|
| 207 |
+
terms = terms_coeff = None
|
| 208 |
+
coeff_list = []
|
| 209 |
+
for ai in a.args:
|
| 210 |
+
ci, ti = _lin_eq2dict(ai, symset)
|
| 211 |
+
if not ti:
|
| 212 |
+
coeff_list.append(ci)
|
| 213 |
+
elif terms is None:
|
| 214 |
+
terms = ti
|
| 215 |
+
terms_coeff = ci
|
| 216 |
+
else:
|
| 217 |
+
# since ti is not null and we already have
|
| 218 |
+
# a term, this is a cross term
|
| 219 |
+
raise PolyNonlinearError(filldedent('''
|
| 220 |
+
nonlinear cross-term: %s''' % a))
|
| 221 |
+
coeff = Mul._from_args(coeff_list)
|
| 222 |
+
if terms is None:
|
| 223 |
+
return coeff, {}
|
| 224 |
+
else:
|
| 225 |
+
terms = {sym: coeff * c for sym, c in terms.items()}
|
| 226 |
+
return coeff * terms_coeff, terms
|
| 227 |
+
elif not a.has_xfree(symset):
|
| 228 |
+
return a, {}
|
| 229 |
+
else:
|
| 230 |
+
raise PolyNonlinearError('nonlinear term: %s' % a)
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/lll.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from math import floor as mfloor
|
| 4 |
+
|
| 5 |
+
from sympy.polys.domains import ZZ, QQ
|
| 6 |
+
from sympy.polys.matrices.exceptions import DMRankError, DMShapeError, DMValueError, DMDomainError
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def _ddm_lll(x, delta=QQ(3, 4), return_transform=False):
|
| 10 |
+
if QQ(1, 4) >= delta or delta >= QQ(1, 1):
|
| 11 |
+
raise DMValueError("delta must lie in range (0.25, 1)")
|
| 12 |
+
if x.shape[0] > x.shape[1]:
|
| 13 |
+
raise DMShapeError("input matrix must have shape (m, n) with m <= n")
|
| 14 |
+
if x.domain != ZZ:
|
| 15 |
+
raise DMDomainError("input matrix domain must be ZZ")
|
| 16 |
+
m = x.shape[0]
|
| 17 |
+
n = x.shape[1]
|
| 18 |
+
k = 1
|
| 19 |
+
y = x.copy()
|
| 20 |
+
y_star = x.zeros((m, n), QQ)
|
| 21 |
+
mu = x.zeros((m, m), QQ)
|
| 22 |
+
g_star = [QQ(0, 1) for _ in range(m)]
|
| 23 |
+
half = QQ(1, 2)
|
| 24 |
+
T = x.eye(m, ZZ) if return_transform else None
|
| 25 |
+
linear_dependent_error = "input matrix contains linearly dependent rows"
|
| 26 |
+
|
| 27 |
+
def closest_integer(x):
|
| 28 |
+
return ZZ(mfloor(x + half))
|
| 29 |
+
|
| 30 |
+
def lovasz_condition(k: int) -> bool:
|
| 31 |
+
return g_star[k] >= ((delta - mu[k][k - 1] ** 2) * g_star[k - 1])
|
| 32 |
+
|
| 33 |
+
def mu_small(k: int, j: int) -> bool:
|
| 34 |
+
return abs(mu[k][j]) <= half
|
| 35 |
+
|
| 36 |
+
def dot_rows(x, y, rows: tuple[int, int]):
|
| 37 |
+
return sum(x[rows[0]][z] * y[rows[1]][z] for z in range(x.shape[1]))
|
| 38 |
+
|
| 39 |
+
def reduce_row(T, mu, y, rows: tuple[int, int]):
|
| 40 |
+
r = closest_integer(mu[rows[0]][rows[1]])
|
| 41 |
+
y[rows[0]] = [y[rows[0]][z] - r * y[rows[1]][z] for z in range(n)]
|
| 42 |
+
mu[rows[0]][:rows[1]] = [mu[rows[0]][z] - r * mu[rows[1]][z] for z in range(rows[1])]
|
| 43 |
+
mu[rows[0]][rows[1]] -= r
|
| 44 |
+
if return_transform:
|
| 45 |
+
T[rows[0]] = [T[rows[0]][z] - r * T[rows[1]][z] for z in range(m)]
|
| 46 |
+
|
| 47 |
+
for i in range(m):
|
| 48 |
+
y_star[i] = [QQ.convert_from(z, ZZ) for z in y[i]]
|
| 49 |
+
for j in range(i):
|
| 50 |
+
row_dot = dot_rows(y, y_star, (i, j))
|
| 51 |
+
try:
|
| 52 |
+
mu[i][j] = row_dot / g_star[j]
|
| 53 |
+
except ZeroDivisionError:
|
| 54 |
+
raise DMRankError(linear_dependent_error)
|
| 55 |
+
y_star[i] = [y_star[i][z] - mu[i][j] * y_star[j][z] for z in range(n)]
|
| 56 |
+
g_star[i] = dot_rows(y_star, y_star, (i, i))
|
| 57 |
+
while k < m:
|
| 58 |
+
if not mu_small(k, k - 1):
|
| 59 |
+
reduce_row(T, mu, y, (k, k - 1))
|
| 60 |
+
if lovasz_condition(k):
|
| 61 |
+
for l in range(k - 2, -1, -1):
|
| 62 |
+
if not mu_small(k, l):
|
| 63 |
+
reduce_row(T, mu, y, (k, l))
|
| 64 |
+
k += 1
|
| 65 |
+
else:
|
| 66 |
+
nu = mu[k][k - 1]
|
| 67 |
+
alpha = g_star[k] + nu ** 2 * g_star[k - 1]
|
| 68 |
+
try:
|
| 69 |
+
beta = g_star[k - 1] / alpha
|
| 70 |
+
except ZeroDivisionError:
|
| 71 |
+
raise DMRankError(linear_dependent_error)
|
| 72 |
+
mu[k][k - 1] = nu * beta
|
| 73 |
+
g_star[k] = g_star[k] * beta
|
| 74 |
+
g_star[k - 1] = alpha
|
| 75 |
+
y[k], y[k - 1] = y[k - 1], y[k]
|
| 76 |
+
mu[k][:k - 1], mu[k - 1][:k - 1] = mu[k - 1][:k - 1], mu[k][:k - 1]
|
| 77 |
+
for i in range(k + 1, m):
|
| 78 |
+
xi = mu[i][k]
|
| 79 |
+
mu[i][k] = mu[i][k - 1] - nu * xi
|
| 80 |
+
mu[i][k - 1] = mu[k][k - 1] * mu[i][k] + xi
|
| 81 |
+
if return_transform:
|
| 82 |
+
T[k], T[k - 1] = T[k - 1], T[k]
|
| 83 |
+
k = max(k - 1, 1)
|
| 84 |
+
assert all(lovasz_condition(i) for i in range(1, m))
|
| 85 |
+
assert all(mu_small(i, j) for i in range(m) for j in range(i))
|
| 86 |
+
return y, T
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def ddm_lll(x, delta=QQ(3, 4)):
|
| 90 |
+
return _ddm_lll(x, delta=delta, return_transform=False)[0]
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def ddm_lll_transform(x, delta=QQ(3, 4)):
|
| 94 |
+
return _ddm_lll(x, delta=delta, return_transform=True)
|
vllm/lib/python3.10/site-packages/sympy/polys/matrices/normalforms.py
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''Functions returning normal forms of matrices'''
|
| 2 |
+
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
|
| 5 |
+
from .domainmatrix import DomainMatrix
|
| 6 |
+
from .exceptions import DMDomainError, DMShapeError
|
| 7 |
+
from sympy.ntheory.modular import symmetric_residue
|
| 8 |
+
from sympy.polys.domains import QQ, ZZ
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# TODO (future work):
|
| 12 |
+
# There are faster algorithms for Smith and Hermite normal forms, which
|
| 13 |
+
# we should implement. See e.g. the Kannan-Bachem algorithm:
|
| 14 |
+
# <https://www.researchgate.net/publication/220617516_Polynomial_Algorithms_for_Computing_the_Smith_and_Hermite_Normal_Forms_of_an_Integer_Matrix>
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def smith_normal_form(m):
|
| 18 |
+
'''
|
| 19 |
+
Return the Smith Normal Form of a matrix `m` over the ring `domain`.
|
| 20 |
+
This will only work if the ring is a principal ideal domain.
|
| 21 |
+
|
| 22 |
+
Examples
|
| 23 |
+
========
|
| 24 |
+
|
| 25 |
+
>>> from sympy import ZZ
|
| 26 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
| 27 |
+
>>> from sympy.polys.matrices.normalforms import smith_normal_form
|
| 28 |
+
>>> m = DomainMatrix([[ZZ(12), ZZ(6), ZZ(4)],
|
| 29 |
+
... [ZZ(3), ZZ(9), ZZ(6)],
|
| 30 |
+
... [ZZ(2), ZZ(16), ZZ(14)]], (3, 3), ZZ)
|
| 31 |
+
>>> print(smith_normal_form(m).to_Matrix())
|
| 32 |
+
Matrix([[1, 0, 0], [0, 10, 0], [0, 0, -30]])
|
| 33 |
+
|
| 34 |
+
'''
|
| 35 |
+
invs = invariant_factors(m)
|
| 36 |
+
smf = DomainMatrix.diag(invs, m.domain, m.shape)
|
| 37 |
+
return smf
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def add_columns(m, i, j, a, b, c, d):
|
| 41 |
+
# replace m[:, i] by a*m[:, i] + b*m[:, j]
|
| 42 |
+
# and m[:, j] by c*m[:, i] + d*m[:, j]
|
| 43 |
+
for k in range(len(m)):
|
| 44 |
+
e = m[k][i]
|
| 45 |
+
m[k][i] = a*e + b*m[k][j]
|
| 46 |
+
m[k][j] = c*e + d*m[k][j]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def invariant_factors(m):
|
| 50 |
+
'''
|
| 51 |
+
Return the tuple of abelian invariants for a matrix `m`
|
| 52 |
+
(as in the Smith-Normal form)
|
| 53 |
+
|
| 54 |
+
References
|
| 55 |
+
==========
|
| 56 |
+
|
| 57 |
+
[1] https://en.wikipedia.org/wiki/Smith_normal_form#Algorithm
|
| 58 |
+
[2] https://web.archive.org/web/20200331143852/https://sierra.nmsu.edu/morandi/notes/SmithNormalForm.pdf
|
| 59 |
+
|
| 60 |
+
'''
|
| 61 |
+
domain = m.domain
|
| 62 |
+
if not domain.is_PID:
|
| 63 |
+
msg = "The matrix entries must be over a principal ideal domain"
|
| 64 |
+
raise ValueError(msg)
|
| 65 |
+
|
| 66 |
+
if 0 in m.shape:
|
| 67 |
+
return ()
|
| 68 |
+
|
| 69 |
+
rows, cols = shape = m.shape
|
| 70 |
+
m = list(m.to_dense().rep.to_ddm())
|
| 71 |
+
|
| 72 |
+
def add_rows(m, i, j, a, b, c, d):
|
| 73 |
+
# replace m[i, :] by a*m[i, :] + b*m[j, :]
|
| 74 |
+
# and m[j, :] by c*m[i, :] + d*m[j, :]
|
| 75 |
+
for k in range(cols):
|
| 76 |
+
e = m[i][k]
|
| 77 |
+
m[i][k] = a*e + b*m[j][k]
|
| 78 |
+
m[j][k] = c*e + d*m[j][k]
|
| 79 |
+
|
| 80 |
+
def clear_column(m):
|
| 81 |
+
# make m[1:, 0] zero by row and column operations
|
| 82 |
+
if m[0][0] == 0:
|
| 83 |
+
return m # pragma: nocover
|
| 84 |
+
pivot = m[0][0]
|
| 85 |
+
for j in range(1, rows):
|
| 86 |
+
if m[j][0] == 0:
|
| 87 |
+
continue
|
| 88 |
+
d, r = domain.div(m[j][0], pivot)
|
| 89 |
+
if r == 0:
|
| 90 |
+
add_rows(m, 0, j, 1, 0, -d, 1)
|
| 91 |
+
else:
|
| 92 |
+
a, b, g = domain.gcdex(pivot, m[j][0])
|
| 93 |
+
d_0 = domain.div(m[j][0], g)[0]
|
| 94 |
+
d_j = domain.div(pivot, g)[0]
|
| 95 |
+
add_rows(m, 0, j, a, b, d_0, -d_j)
|
| 96 |
+
pivot = g
|
| 97 |
+
return m
|
| 98 |
+
|
| 99 |
+
def clear_row(m):
|
| 100 |
+
# make m[0, 1:] zero by row and column operations
|
| 101 |
+
if m[0][0] == 0:
|
| 102 |
+
return m # pragma: nocover
|
| 103 |
+
pivot = m[0][0]
|
| 104 |
+
for j in range(1, cols):
|
| 105 |
+
if m[0][j] == 0:
|
| 106 |
+
continue
|
| 107 |
+
d, r = domain.div(m[0][j], pivot)
|
| 108 |
+
if r == 0:
|
| 109 |
+
add_columns(m, 0, j, 1, 0, -d, 1)
|
| 110 |
+
else:
|
| 111 |
+
a, b, g = domain.gcdex(pivot, m[0][j])
|
| 112 |
+
d_0 = domain.div(m[0][j], g)[0]
|
| 113 |
+
d_j = domain.div(pivot, g)[0]
|
| 114 |
+
add_columns(m, 0, j, a, b, d_0, -d_j)
|
| 115 |
+
pivot = g
|
| 116 |
+
return m
|
| 117 |
+
|
| 118 |
+
# permute the rows and columns until m[0,0] is non-zero if possible
|
| 119 |
+
ind = [i for i in range(rows) if m[i][0] != 0]
|
| 120 |
+
if ind and ind[0] != 0:
|
| 121 |
+
m[0], m[ind[0]] = m[ind[0]], m[0]
|
| 122 |
+
else:
|
| 123 |
+
ind = [j for j in range(cols) if m[0][j] != 0]
|
| 124 |
+
if ind and ind[0] != 0:
|
| 125 |
+
for row in m:
|
| 126 |
+
row[0], row[ind[0]] = row[ind[0]], row[0]
|
| 127 |
+
|
| 128 |
+
# make the first row and column except m[0,0] zero
|
| 129 |
+
while (any(m[0][i] != 0 for i in range(1,cols)) or
|
| 130 |
+
any(m[i][0] != 0 for i in range(1,rows))):
|
| 131 |
+
m = clear_column(m)
|
| 132 |
+
m = clear_row(m)
|
| 133 |
+
|
| 134 |
+
if 1 in shape:
|
| 135 |
+
invs = ()
|
| 136 |
+
else:
|
| 137 |
+
lower_right = DomainMatrix([r[1:] for r in m[1:]], (rows-1, cols-1), domain)
|
| 138 |
+
invs = invariant_factors(lower_right)
|
| 139 |
+
|
| 140 |
+
if m[0][0]:
|
| 141 |
+
result = [m[0][0]]
|
| 142 |
+
result.extend(invs)
|
| 143 |
+
# in case m[0] doesn't divide the invariants of the rest of the matrix
|
| 144 |
+
for i in range(len(result)-1):
|
| 145 |
+
if result[i] and domain.div(result[i+1], result[i])[1] != 0:
|
| 146 |
+
g = domain.gcd(result[i+1], result[i])
|
| 147 |
+
result[i+1] = domain.div(result[i], g)[0]*result[i+1]
|
| 148 |
+
result[i] = g
|
| 149 |
+
else:
|
| 150 |
+
break
|
| 151 |
+
else:
|
| 152 |
+
result = invs + (m[0][0],)
|
| 153 |
+
return tuple(result)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def _gcdex(a, b):
|
| 157 |
+
r"""
|
| 158 |
+
This supports the functions that compute Hermite Normal Form.
|
| 159 |
+
|
| 160 |
+
Explanation
|
| 161 |
+
===========
|
| 162 |
+
|
| 163 |
+
Let x, y be the coefficients returned by the extended Euclidean
|
| 164 |
+
Algorithm, so that x*a + y*b = g. In the algorithms for computing HNF,
|
| 165 |
+
it is critical that x, y not only satisfy the condition of being small
|
| 166 |
+
in magnitude -- namely that |x| <= |b|/g, |y| <- |a|/g -- but also that
|
| 167 |
+
y == 0 when a | b.
|
| 168 |
+
|
| 169 |
+
"""
|
| 170 |
+
x, y, g = ZZ.gcdex(a, b)
|
| 171 |
+
if a != 0 and b % a == 0:
|
| 172 |
+
y = 0
|
| 173 |
+
x = -1 if a < 0 else 1
|
| 174 |
+
return x, y, g
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def _hermite_normal_form(A):
|
| 178 |
+
r"""
|
| 179 |
+
Compute the Hermite Normal Form of DomainMatrix *A* over :ref:`ZZ`.
|
| 180 |
+
|
| 181 |
+
Parameters
|
| 182 |
+
==========
|
| 183 |
+
|
| 184 |
+
A : :py:class:`~.DomainMatrix` over domain :ref:`ZZ`.
|
| 185 |
+
|
| 186 |
+
Returns
|
| 187 |
+
=======
|
| 188 |
+
|
| 189 |
+
:py:class:`~.DomainMatrix`
|
| 190 |
+
The HNF of matrix *A*.
|
| 191 |
+
|
| 192 |
+
Raises
|
| 193 |
+
======
|
| 194 |
+
|
| 195 |
+
DMDomainError
|
| 196 |
+
If the domain of the matrix is not :ref:`ZZ`.
|
| 197 |
+
|
| 198 |
+
References
|
| 199 |
+
==========
|
| 200 |
+
|
| 201 |
+
.. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
|
| 202 |
+
(See Algorithm 2.4.5.)
|
| 203 |
+
|
| 204 |
+
"""
|
| 205 |
+
if not A.domain.is_ZZ:
|
| 206 |
+
raise DMDomainError('Matrix must be over domain ZZ.')
|
| 207 |
+
# We work one row at a time, starting from the bottom row, and working our
|
| 208 |
+
# way up.
|
| 209 |
+
m, n = A.shape
|
| 210 |
+
A = A.to_dense().rep.to_ddm().copy()
|
| 211 |
+
# Our goal is to put pivot entries in the rightmost columns.
|
| 212 |
+
# Invariant: Before processing each row, k should be the index of the
|
| 213 |
+
# leftmost column in which we have so far put a pivot.
|
| 214 |
+
k = n
|
| 215 |
+
for i in range(m - 1, -1, -1):
|
| 216 |
+
if k == 0:
|
| 217 |
+
# This case can arise when n < m and we've already found n pivots.
|
| 218 |
+
# We don't need to consider any more rows, because this is already
|
| 219 |
+
# the maximum possible number of pivots.
|
| 220 |
+
break
|
| 221 |
+
k -= 1
|
| 222 |
+
# k now points to the column in which we want to put a pivot.
|
| 223 |
+
# We want zeros in all entries to the left of the pivot column.
|
| 224 |
+
for j in range(k - 1, -1, -1):
|
| 225 |
+
if A[i][j] != 0:
|
| 226 |
+
# Replace cols j, k by lin combs of these cols such that, in row i,
|
| 227 |
+
# col j has 0, while col k has the gcd of their row i entries. Note
|
| 228 |
+
# that this ensures a nonzero entry in col k.
|
| 229 |
+
u, v, d = _gcdex(A[i][k], A[i][j])
|
| 230 |
+
r, s = A[i][k] // d, A[i][j] // d
|
| 231 |
+
add_columns(A, k, j, u, v, -s, r)
|
| 232 |
+
b = A[i][k]
|
| 233 |
+
# Do not want the pivot entry to be negative.
|
| 234 |
+
if b < 0:
|
| 235 |
+
add_columns(A, k, k, -1, 0, -1, 0)
|
| 236 |
+
b = -b
|
| 237 |
+
# The pivot entry will be 0 iff the row was 0 from the pivot col all the
|
| 238 |
+
# way to the left. In this case, we are still working on the same pivot
|
| 239 |
+
# col for the next row. Therefore:
|
| 240 |
+
if b == 0:
|
| 241 |
+
k += 1
|
| 242 |
+
# If the pivot entry is nonzero, then we want to reduce all entries to its
|
| 243 |
+
# right in the sense of the division algorithm, i.e. make them all remainders
|
| 244 |
+
# w.r.t. the pivot as divisor.
|
| 245 |
+
else:
|
| 246 |
+
for j in range(k + 1, n):
|
| 247 |
+
q = A[i][j] // b
|
| 248 |
+
add_columns(A, j, k, 1, -q, 0, 1)
|
| 249 |
+
# Finally, the HNF consists of those columns of A in which we succeeded in making
|
| 250 |
+
# a nonzero pivot.
|
| 251 |
+
return DomainMatrix.from_rep(A.to_dfm_or_ddm())[:, k:]
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
def _hermite_normal_form_modulo_D(A, D):
|
| 255 |
+
r"""
|
| 256 |
+
Perform the mod *D* Hermite Normal Form reduction algorithm on
|
| 257 |
+
:py:class:`~.DomainMatrix` *A*.
|
| 258 |
+
|
| 259 |
+
Explanation
|
| 260 |
+
===========
|
| 261 |
+
|
| 262 |
+
If *A* is an $m \times n$ matrix of rank $m$, having Hermite Normal Form
|
| 263 |
+
$W$, and if *D* is any positive integer known in advance to be a multiple
|
| 264 |
+
of $\det(W)$, then the HNF of *A* can be computed by an algorithm that
|
| 265 |
+
works mod *D* in order to prevent coefficient explosion.
|
| 266 |
+
|
| 267 |
+
Parameters
|
| 268 |
+
==========
|
| 269 |
+
|
| 270 |
+
A : :py:class:`~.DomainMatrix` over :ref:`ZZ`
|
| 271 |
+
$m \times n$ matrix, having rank $m$.
|
| 272 |
+
D : :ref:`ZZ`
|
| 273 |
+
Positive integer, known to be a multiple of the determinant of the
|
| 274 |
+
HNF of *A*.
|
| 275 |
+
|
| 276 |
+
Returns
|
| 277 |
+
=======
|
| 278 |
+
|
| 279 |
+
:py:class:`~.DomainMatrix`
|
| 280 |
+
The HNF of matrix *A*.
|
| 281 |
+
|
| 282 |
+
Raises
|
| 283 |
+
======
|
| 284 |
+
|
| 285 |
+
DMDomainError
|
| 286 |
+
If the domain of the matrix is not :ref:`ZZ`, or
|
| 287 |
+
if *D* is given but is not in :ref:`ZZ`.
|
| 288 |
+
|
| 289 |
+
DMShapeError
|
| 290 |
+
If the matrix has more rows than columns.
|
| 291 |
+
|
| 292 |
+
References
|
| 293 |
+
==========
|
| 294 |
+
|
| 295 |
+
.. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
|
| 296 |
+
(See Algorithm 2.4.8.)
|
| 297 |
+
|
| 298 |
+
"""
|
| 299 |
+
if not A.domain.is_ZZ:
|
| 300 |
+
raise DMDomainError('Matrix must be over domain ZZ.')
|
| 301 |
+
if not ZZ.of_type(D) or D < 1:
|
| 302 |
+
raise DMDomainError('Modulus D must be positive element of domain ZZ.')
|
| 303 |
+
|
| 304 |
+
def add_columns_mod_R(m, R, i, j, a, b, c, d):
|
| 305 |
+
# replace m[:, i] by (a*m[:, i] + b*m[:, j]) % R
|
| 306 |
+
# and m[:, j] by (c*m[:, i] + d*m[:, j]) % R
|
| 307 |
+
for k in range(len(m)):
|
| 308 |
+
e = m[k][i]
|
| 309 |
+
m[k][i] = symmetric_residue((a * e + b * m[k][j]) % R, R)
|
| 310 |
+
m[k][j] = symmetric_residue((c * e + d * m[k][j]) % R, R)
|
| 311 |
+
|
| 312 |
+
W = defaultdict(dict)
|
| 313 |
+
|
| 314 |
+
m, n = A.shape
|
| 315 |
+
if n < m:
|
| 316 |
+
raise DMShapeError('Matrix must have at least as many columns as rows.')
|
| 317 |
+
A = A.to_dense().rep.to_ddm().copy()
|
| 318 |
+
k = n
|
| 319 |
+
R = D
|
| 320 |
+
for i in range(m - 1, -1, -1):
|
| 321 |
+
k -= 1
|
| 322 |
+
for j in range(k - 1, -1, -1):
|
| 323 |
+
if A[i][j] != 0:
|
| 324 |
+
u, v, d = _gcdex(A[i][k], A[i][j])
|
| 325 |
+
r, s = A[i][k] // d, A[i][j] // d
|
| 326 |
+
add_columns_mod_R(A, R, k, j, u, v, -s, r)
|
| 327 |
+
b = A[i][k]
|
| 328 |
+
if b == 0:
|
| 329 |
+
A[i][k] = b = R
|
| 330 |
+
u, v, d = _gcdex(b, R)
|
| 331 |
+
for ii in range(m):
|
| 332 |
+
W[ii][i] = u*A[ii][k] % R
|
| 333 |
+
if W[i][i] == 0:
|
| 334 |
+
W[i][i] = R
|
| 335 |
+
for j in range(i + 1, m):
|
| 336 |
+
q = W[i][j] // W[i][i]
|
| 337 |
+
add_columns(W, j, i, 1, -q, 0, 1)
|
| 338 |
+
R //= d
|
| 339 |
+
return DomainMatrix(W, (m, m), ZZ).to_dense()
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
def hermite_normal_form(A, *, D=None, check_rank=False):
|
| 343 |
+
r"""
|
| 344 |
+
Compute the Hermite Normal Form of :py:class:`~.DomainMatrix` *A* over
|
| 345 |
+
:ref:`ZZ`.
|
| 346 |
+
|
| 347 |
+
Examples
|
| 348 |
+
========
|
| 349 |
+
|
| 350 |
+
>>> from sympy import ZZ
|
| 351 |
+
>>> from sympy.polys.matrices import DomainMatrix
|
| 352 |
+
>>> from sympy.polys.matrices.normalforms import hermite_normal_form
|
| 353 |
+
>>> m = DomainMatrix([[ZZ(12), ZZ(6), ZZ(4)],
|
| 354 |
+
... [ZZ(3), ZZ(9), ZZ(6)],
|
| 355 |
+
... [ZZ(2), ZZ(16), ZZ(14)]], (3, 3), ZZ)
|
| 356 |
+
>>> print(hermite_normal_form(m).to_Matrix())
|
| 357 |
+
Matrix([[10, 0, 2], [0, 15, 3], [0, 0, 2]])
|
| 358 |
+
|
| 359 |
+
Parameters
|
| 360 |
+
==========
|
| 361 |
+
|
| 362 |
+
A : $m \times n$ ``DomainMatrix`` over :ref:`ZZ`.
|
| 363 |
+
|
| 364 |
+
D : :ref:`ZZ`, optional
|
| 365 |
+
Let $W$ be the HNF of *A*. If known in advance, a positive integer *D*
|
| 366 |
+
being any multiple of $\det(W)$ may be provided. In this case, if *A*
|
| 367 |
+
also has rank $m$, then we may use an alternative algorithm that works
|
| 368 |
+
mod *D* in order to prevent coefficient explosion.
|
| 369 |
+
|
| 370 |
+
check_rank : boolean, optional (default=False)
|
| 371 |
+
The basic assumption is that, if you pass a value for *D*, then
|
| 372 |
+
you already believe that *A* has rank $m$, so we do not waste time
|
| 373 |
+
checking it for you. If you do want this to be checked (and the
|
| 374 |
+
ordinary, non-modulo *D* algorithm to be used if the check fails), then
|
| 375 |
+
set *check_rank* to ``True``.
|
| 376 |
+
|
| 377 |
+
Returns
|
| 378 |
+
=======
|
| 379 |
+
|
| 380 |
+
:py:class:`~.DomainMatrix`
|
| 381 |
+
The HNF of matrix *A*.
|
| 382 |
+
|
| 383 |
+
Raises
|
| 384 |
+
======
|
| 385 |
+
|
| 386 |
+
DMDomainError
|
| 387 |
+
If the domain of the matrix is not :ref:`ZZ`, or
|
| 388 |
+
if *D* is given but is not in :ref:`ZZ`.
|
| 389 |
+
|
| 390 |
+
DMShapeError
|
| 391 |
+
If the mod *D* algorithm is used but the matrix has more rows than
|
| 392 |
+
columns.
|
| 393 |
+
|
| 394 |
+
References
|
| 395 |
+
==========
|
| 396 |
+
|
| 397 |
+
.. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
|
| 398 |
+
(See Algorithms 2.4.5 and 2.4.8.)
|
| 399 |
+
|
| 400 |
+
"""
|
| 401 |
+
if not A.domain.is_ZZ:
|
| 402 |
+
raise DMDomainError('Matrix must be over domain ZZ.')
|
| 403 |
+
if D is not None and (not check_rank or A.convert_to(QQ).rank() == A.shape[0]):
|
| 404 |
+
return _hermite_normal_form_modulo_D(A, D)
|
| 405 |
+
else:
|
| 406 |
+
return _hermite_normal_form(A)
|