Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

r""" 

Fast evaluation of polynomials (Horner's rule) 

  

This file provides fast evaluation of integer polynomials with a real value. We 

consider flint and NTL polynomials and values mpfr_t and mpfi_t. If you intend 

to implement more it would be better to find a template strategy instead of 

duplicating the code. 

  

The code in this file is mostly Sage agnostic and only does library calls. 

  

For appropriate testing see 

:mod:`~sage.rings.polynomial.polynomial_integer_dense_flint` and 

:mod:`~sage.rings.polynomial.polynomial_integer_dense_ntl`. 

  

.. TODO:: 

  

Integrate these functions into 

:mod:`~sage.rings.polynomial.polynomial_compiled` 

""" 

#***************************************************************************** 

# Copyright (C) 2016 Vincent Delecroix <20100.delecroix@gmail.com> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

  

from sage.libs.mpfr cimport * 

from sage.libs.mpfi cimport * 

from sage.libs.gmp.mpz cimport * 

from sage.libs.gmp.mpq cimport * 

from sage.libs.flint.fmpz cimport * 

from sage.libs.flint.fmpz_poly cimport * 

from sage.libs.ntl.ZZ cimport * 

from sage.libs.ntl.ZZX cimport * 

  

cdef fmpz_poly_evaluation_mpfr(mpfr_t res, const fmpz_poly_t poly, const mpfr_t a): 

cdef mpz_t c 

cdef long i 

  

mpfr_set_ui(res, 0, MPFR_RNDN) 

mpz_init(c) 

  

for i in range(fmpz_poly_degree(poly), -1, -1): 

mpfr_mul(res, res, a, MPFR_RNDN) 

if not fmpz_is_zero(fmpz_poly_get_coeff_ptr(poly, i)): 

fmpz_poly_get_coeff_mpz(c, poly, i) 

mpfr_add_z(res, res, c, MPFR_RNDN) 

  

mpz_clear(c) 

  

cdef fmpz_poly_evaluation_mpfi(mpfi_t res, const fmpz_poly_t poly, const mpfi_t a): 

cdef mpz_t c 

cdef long i 

  

mpfi_set_ui(res, 0) 

mpz_init(c) 

  

for i in range(fmpz_poly_degree(poly), -1, -1): 

mpfi_mul(res, res, a) 

if not fmpz_is_zero(fmpz_poly_get_coeff_ptr(poly, i)): 

fmpz_poly_get_coeff_mpz(c, poly, i) 

mpfi_add_z(res, res, c) 

  

mpz_clear(c) 

  

  

cdef ZZX_evaluation_mpfr(mpfr_t res, ZZX_c poly, const mpfr_t a): 

cdef mpz_t c 

cdef long i 

  

mpfr_set_ui(res, 0, MPFR_RNDN) 

mpz_init(c) 

  

for i in range(ZZX_deg(poly), -1, -1): 

mpfr_mul(res, res, a, MPFR_RNDN) 

if not ZZ_IsZero(ZZX_coeff(poly, i)): 

ZZX_getitem_as_mpz(c, &poly, i) 

mpfr_add_z(res, res, c, MPFR_RNDN) 

  

mpz_clear(c) 

  

cdef ZZX_evaluation_mpfi(mpfi_t res, ZZX_c poly, const mpfi_t a): 

cdef mpz_t c 

cdef long i 

  

mpfi_set_ui(res, 0) 

mpz_init(c) 

  

for i in range(ZZX_deg(poly), -1, -1): 

mpfi_mul(res, res, a) 

if not ZZ_IsZero(ZZX_coeff(poly, i)): 

ZZX_getitem_as_mpz(c, &poly, i) 

mpfi_add_z(res, res, c) 

  

mpz_clear(c)