aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src/rational.h
blob: a083f861cb14cb2e52d8190c118eab369ae12ad1 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * rational.h
 *
 * A small rational number library
 *
 * Copyright © 2019-2021 Deutsches Elektronen-Synchrotron DESY,
 *                       a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2019 Thomas White <taw@physics.org>
 *
 * This file is part of CrystFEL.
 *
 * CrystFEL 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 3 of the License, or
 * (at your option) any later version.
 *
 * CrystFEL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CrystFEL.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef RATIONAL_H
#define RATIONAL_H

/**
 * \file rational.h
 * %Rational numbers (including rational matrices)
 */

/**
 * The Rational is an opaque-ish data structure representing a rational number.
 *
 * "Opaque-ish" means that the structure isn't technically opaque, allowing you
 * to assign and allocate them easily.  But you shouldn't look at or set its
 * contents, except by using the accessor functions.
 **/
typedef struct {
	/* Private, don't modify */
	signed long long int num;
	signed long long int den;
} Rational;


/**
 * The RationalMatrix is an opaque data structure representing a matrix of
 * rational numbers.
 **/
typedef struct _rationalmatrix RationalMatrix;

#include "integer_matrix.h"

#ifdef __cplusplus
extern "C" {
#endif

extern Rational rtnl_zero(void);
extern Rational rtnl(signed long long int num, signed long long int den);
extern double rtnl_as_double(Rational r);

extern Rational rtnl_mul(Rational a, Rational b);
extern Rational rtnl_div(Rational a, Rational b);
extern Rational rtnl_add(Rational a, Rational b);
extern Rational rtnl_sub(Rational a, Rational b);

extern signed int rtnl_cmp(Rational a, Rational b);
extern Rational rtnl_abs(Rational a);

extern char *rtnl_format(Rational rt);

extern Rational *rtnl_list(signed int num_min, signed int num_max,
                           signed int den_min, signed int den_max,
                           int *pn);

extern RationalMatrix *rtnl_mtx_new(unsigned int rows, unsigned int cols);
extern RationalMatrix *rtnl_mtx_copy(const RationalMatrix *m);
extern Rational rtnl_mtx_get(const RationalMatrix *m, int i, int j);
extern void rtnl_mtx_set(const RationalMatrix *m, int i, int j, Rational v);
extern RationalMatrix *rtnl_mtx_from_intmat(const IntegerMatrix *m);
extern RationalMatrix *rtnl_mtx_identity(int rows);
extern IntegerMatrix *intmat_from_rtnl_mtx(const RationalMatrix *m);
extern void rtnl_mtx_free(RationalMatrix *mtx);
extern RationalMatrix *rtnlmtx_times_rtnlmtx(const RationalMatrix *A,
                                             const RationalMatrix *B);
extern RationalMatrix *rtnlmtx_times_intmat(const RationalMatrix *A,
                                            const IntegerMatrix *B);
extern RationalMatrix *intmat_times_rtnlmtx(const IntegerMatrix *a,
                                            const RationalMatrix *b);

extern int transform_fractional_coords_rtnl(const RationalMatrix *P,
                                            const Rational *ivec,
                                            Rational *ans);
extern void transform_fractional_coords_rtnl_inverse(const RationalMatrix *P,
                                                     const Rational *vec,
                                                     Rational *ans);
extern void rtnl_mtx_print(const RationalMatrix *m);
extern Rational rtnl_mtx_det(const RationalMatrix *m);
extern int rtnl_mtx_is_identity(const RationalMatrix *m);
extern int rtnl_mtx_is_perm(const RationalMatrix *m);

#ifdef __cplusplus
}
#endif

#endif	/* RATIONAL_H */