Skip to content

Commit 8b6f0b5

Browse files
committed
added example to readme
1 parent f138aa1 commit 8b6f0b5

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,49 @@ Method name | Polynomial type | Coefficients | Roots | Reference
3535
[`cmplx_roots_gen`](https://jacobwilliams.github.io/polyroots-fortran/proc/cmplx_roots_gen.html) | General | complex | complex | [Skowron & Gould (2012)](http://www.astrouw.edu.pl/~jskowron/cmplx_roots_sg/)
3636
[`fpml`](https://jacobwilliams.github.io/polyroots-fortran/proc/fpml.html) | General | complex | complex | [Cameron (2019)](https://link.springer.com/article/10.1007/s11075-018-0641-9)
3737

38+
## Example
39+
40+
An example of using `polyroots` to compute the zeros for a 5th order real polynomial $$P(x) = x^5 + 2x^4 + 3x^3 + 4x^2 + 5x + 6$$
41+
42+
```fortran
43+
program example
44+
45+
use iso_fortran_env
46+
use polyroots_module, wp => polyroots_module_rk
47+
48+
implicit none
49+
50+
integer,parameter :: degree = 5 !! polynomial degree
51+
real(wp),dimension(degree+1) :: p = [1,2,3,4,5,6] !! coefficients
52+
53+
integer :: i !! counter
54+
integer :: istatus !! status code
55+
real(wp),dimension(degree) :: zr !! real components of roots
56+
real(wp),dimension(degree) :: zi !! imaginary components of roots
57+
58+
call polyroots(degree, p, zr, zi, istatus)
59+
60+
write(*,'(A,1x,I3)') 'istatus: ', istatus
61+
write(*, '(*(a22,1x))') 'real part', 'imaginary part'
62+
do i = 1, degree
63+
write(*,'(*(e22.15,1x))') zr(i), zi(i)
64+
end do
65+
66+
end program example
67+
```
68+
69+
The result is:
70+
71+
```
72+
istatus: 0
73+
real part imaginary part
74+
0.551685463458982E+00 0.125334886027721E+01
75+
0.551685463458982E+00 -0.125334886027721E+01
76+
-0.149179798813990E+01 0.000000000000000E+00
77+
-0.805786469389031E+00 0.122290471337441E+01
78+
-0.805786469389031E+00 -0.122290471337441E+01
79+
```
80+
3881
## Compiling
3982

4083
A `fmp.toml` file is provided for compiling polyroots-fortran with the [Fortran Package Manager](https://github.com/fortran-lang/fpm). For example, to build:

test/example.f90

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
!*****************************************************************************************
2+
!>
3+
! Example in the readme.
4+
5+
program example
6+
7+
use iso_fortran_env
8+
use polyroots_module, wp => polyroots_module_rk
9+
10+
implicit none
11+
12+
integer,parameter :: degree = 5 !! polynomial degree
13+
real(wp),dimension(degree+1) :: p = [1,2,3,4,5,6] !! coefficients
14+
15+
integer :: i !! counter
16+
integer :: istatus !! status code
17+
real(wp),dimension(degree) :: zr !! real components of roots
18+
real(wp),dimension(degree) :: zi !! imaginary components of roots
19+
20+
call polyroots(degree, p, zr, zi, istatus)
21+
22+
write(*,'(A,1x,I3)') 'istatus: ', istatus
23+
write(*, '(*(a22,1x))') 'real part', 'imaginary part'
24+
do i = 1, degree
25+
write(*,'(*(e22.15,1x))') zr(i), zi(i)
26+
end do
27+
28+
end program example
29+
!*****************************************************************************************

0 commit comments

Comments
 (0)