1 module faiss.vector_transform;
2 
3 import faiss.common;
4 import faiss.index;
5 
6 /**
7  * Copyright (c) Facebook, Inc. and its affiliates.
8  *
9  * This source code is licensed under the MIT license found in the
10  * LICENSE file in the root directory of this source tree.
11  */
12 
13 // Copyright 2004-present Facebook. All Rights Reserved.
14 // -*- c -*-
15 
16 extern (C):
17 
18 /** Defines a few objects that apply transformations to a set of
19  * vectors Often these are pre-processing steps.
20  */
21 
22 /// Opaque type for referencing to a VectorTransform object
23 struct FaissVectorTransform_H;
24 alias FaissVectorTransform = FaissVectorTransform_H;
25 void faiss_VectorTransform_free (FaissVectorTransform* obj);
26 
27 /// Getter for is_trained
28 int faiss_VectorTransform_is_trained (const(FaissVectorTransform)*);
29 
30 /// Getter for input dimension
31 int faiss_VectorTransform_d_in (const(FaissVectorTransform)*);
32 
33 /// Getter for output dimension
34 int faiss_VectorTransform_d_out (const(FaissVectorTransform)*);
35 
36 /** Perform training on a representative set of vectors
37  *
38  * @param vt     opaque pointer to VectorTransform object
39  * @param n      nb of training vectors
40  * @param x      training vectors, size n * d
41  */
42 int faiss_VectorTransform_train (
43     FaissVectorTransform* vt,
44     idx_t n,
45     const(float)* x);
46 
47 /** apply the random rotation, return new allocated matrix
48  * @param     x size n * d_in
49  * @return    size n * d_out
50  */
51 float* faiss_VectorTransform_apply (
52     const(FaissVectorTransform)* vt,
53     idx_t n,
54     const(float)* x);
55 
56 /** apply transformation and result is pre-allocated
57  * @param     x size n * d_in
58  * @param     xt size n * d_out
59  */
60 void faiss_VectorTransform_apply_noalloc (
61     const(FaissVectorTransform)* vt,
62     idx_t n,
63     const(float)* x,
64     float* xt);
65 
66 /// reverse transformation. May not be implemented or may return
67 /// approximate result
68 void faiss_VectorTransform_reverse_transform (
69     const(FaissVectorTransform)* vt,
70     idx_t n,
71     const(float)* xt,
72     float* x);
73 
74 /// Opaque type for referencing to a LinearTransform object
75 alias FaissLinearTransform = FaissVectorTransform_H;
76 void faiss_LinearTransform_free (FaissLinearTransform* obj);
77 
78 /// compute x = A^T * (x - b)
79 /// is reverse transform if A has orthonormal lines
80 void faiss_LinearTransform_transform_transpose (
81     const(FaissLinearTransform)* vt,
82     idx_t n,
83     const(float)* y,
84     float* x);
85 
86 /// compute A^T * A to set the is_orthonormal flag
87 void faiss_LinearTransform_set_is_orthonormal (FaissLinearTransform* vt);
88 
89 /// Getter for have_bias
90 int faiss_LinearTransform_have_bias (const(FaissLinearTransform)*);
91 
92 /// Getter for is_orthonormal
93 int faiss_LinearTransform_is_orthonormal (const(FaissLinearTransform)*);
94 
95 alias FaissRandomRotationMatrix = FaissVectorTransform_H;
96 void faiss_RandomRotationMatrix_free (FaissRandomRotationMatrix* obj);
97 
98 int faiss_RandomRotationMatrix_new_with (
99     FaissRandomRotationMatrix** p_vt,
100     int d_in,
101     int d_out);
102 
103 alias FaissPCAMatrix = FaissVectorTransform_H;
104 void faiss_PCAMatrix_free (FaissPCAMatrix* obj);
105 
106 int faiss_PCAMatrix_new_with (
107     FaissPCAMatrix** p_vt,
108     int d_in,
109     int d_out,
110     float eigen_power,
111     int random_rotation);
112 
113 /// Getter for eigen_power
114 float faiss_PCAMatrix_eigen_power (const(FaissPCAMatrix)*);
115 
116 /// Getter for random_rotation
117 int faiss_PCAMatrix_random_rotation (const(FaissPCAMatrix)*);
118 
119 alias FaissITQMatrix = FaissVectorTransform_H;
120 void faiss_ITQMatrix_free (FaissITQMatrix* obj);
121 
122 int faiss_ITQMatrix_new_with (FaissITQMatrix** p_vt, int d);
123 
124 alias FaissITQTransform = FaissVectorTransform_H;
125 void faiss_ITQTransform_free (FaissITQTransform* obj);
126 
127 int faiss_ITQTransform_new_with (
128     FaissITQTransform** p_vt,
129     int d_in,
130     int d_out,
131     int do_pca);
132 
133 /// Getter for do_pca
134 int faiss_ITQTransform_do_pca (const(FaissITQTransform)*);
135 
136 alias FaissOPQMatrix = FaissVectorTransform_H;
137 void faiss_OPQMatrix_free (FaissOPQMatrix* obj);
138 
139 int faiss_OPQMatrix_new_with (FaissOPQMatrix** p_vt, int d, int M, int d2);
140 
141 int faiss_OPQMatrix_verbose (const(FaissOPQMatrix)*);
142 void faiss_OPQMatrix_set_verbose (FaissOPQMatrix*, int);
143 int faiss_OPQMatrix_niter (const(FaissOPQMatrix)*);
144 void faiss_OPQMatrix_set_niter (FaissOPQMatrix*, int);
145 int faiss_OPQMatrix_niter_pq (const(FaissOPQMatrix)*);
146 void faiss_OPQMatrix_set_niter_pq (FaissOPQMatrix*, int);
147 
148 alias FaissRemapDimensionsTransform = FaissVectorTransform_H;
149 void faiss_RemapDimensionsTransform_free (FaissRemapDimensionsTransform* obj);
150 
151 int faiss_RemapDimensionsTransform_new_with (
152     FaissRemapDimensionsTransform** p_vt,
153     int d_in,
154     int d_out,
155     int uniform);
156 
157 alias FaissNormalizationTransform = FaissVectorTransform_H;
158 void faiss_NormalizationTransform_free (FaissNormalizationTransform* obj);
159 
160 int faiss_NormalizationTransform_new_with (
161     FaissNormalizationTransform** p_vt,
162     int d,
163     float norm);
164 
165 float faiss_NormalizationTransform_norm (const(FaissNormalizationTransform)*);
166 
167 alias FaissCenteringTransform = FaissVectorTransform_H;
168 void faiss_CenteringTransform_free (FaissCenteringTransform* obj);
169 
170 int faiss_CenteringTransform_new_with (FaissCenteringTransform** p_vt, int d);
171