10namespace gpr::detail {
15struct EnvironmentBatchView {
22 const double* jacobians;
23 const double* inverse_variance;
26struct EnvironmentPairBatch {
27 EnvironmentBatchView left;
28 EnvironmentBatchView right;
29 const int* left_indices;
30 const int* right_indices;
33 std::size_t pair_count;
39struct EnvironmentPairJets {
40 EnvironmentPairBatch batch;
45 const auto left = batch.left, right = batch.right;
46 const std::size_t b = index % right.atoms;
47 const std::size_t a = (index / right.atoms) % left.atoms;
48 const std::size_t pair = index / (left.atoms * right.atoms);
49 const std::size_t la = batch.left_indices[pair] * left.atoms + a;
50 const std::size_t rb = batch.right_indices[pair] * right.atoms + b;
51 if (left.species[la] == 0 || left.species[la] != right.species[rb])
53 const double* lv = left.values + la * left.features;
54 const double* rv = right.values + rb * right.features;
56 left.jacobians + la * left.dimensions * left.features;
58 right.jacobians + rb * right.dimensions * right.features;
59 double* jet = jets + index * (2 + left.dimensions + right.dimensions);
61 for (
int feature = 0; feature < left.features; ++feature) {
62 const double difference = lv[feature] - rv[feature];
63 norm += difference * difference;
65 jet[0] = left.inverse_variance[la] * norm;
66 jet[1] = batch.magnitude * std::exp(-0.5 * jet[0]);
67 for (
int dimension = 0; dimension < left.dimensions; ++dimension) {
69 for (
int feature = 0; feature < left.features; ++feature)
70 value += lj[feature * left.dimensions + dimension] *
71 (lv[feature] - rv[feature]);
72 jet[2 + dimension] = value;
74 for (
int dimension = 0; dimension < right.dimensions; ++dimension) {
76 for (
int feature = 0; feature < right.features; ++feature)
77 value += rj[feature * right.dimensions + dimension] *
78 (lv[feature] - rv[feature]);
79 jet[2 + left.dimensions + dimension] = value;
86struct EnvironmentWeightedJacobians {
87 EnvironmentPairBatch batch;
93 const auto left = batch.left, right = batch.right;
94 const std::size_t stride = right.dimensions * right.features;
95 const std::size_t matrix = index / stride, entry = index % stride;
96 const std::size_t a = matrix % left.atoms;
97 const std::size_t channel = (matrix / left.atoms) % batch.channel_count;
98 const std::size_t pair = matrix / (left.atoms * batch.channel_count);
99 const std::size_t la = batch.left_indices[pair] * left.atoms + a;
100 const int species = left.species[la];
101 const bool derivative = batch.channels[channel] != 0;
104 (!derivative || batch.channels[channel] == species)) {
105 const double q = left.inverse_variance[la];
106 for (
int b = 0; b < right.atoms; ++b) {
107 const std::size_t rb =
108 batch.right_indices[pair] * right.atoms + b;
109 if (right.species[rb] != species)
continue;
111 jets + ((pair * left.atoms + a) * right.atoms + b) *
112 (2 + left.dimensions + right.dimensions);
113 const GaussianEnvironmentWeights weights{jet[0], jet[1]};
114 value += (weights.kernel * q * weights.gradient(derivative)) *
115 right.jacobians[rb * stride + entry];
118 weighted[index] = value;
125struct EnvironmentBlockEntries {
126 EnvironmentPairBatch batch;
133 const auto left = batch.left, right = batch.right;
134 const std::size_t block_size =
135 (left.dimensions + 1) * (right.dimensions + 1);
136 const std::size_t block = index / block_size,
137 entry = index % block_size;
138 const std::size_t pair = block / batch.channel_count;
139 const std::size_t channel = block % batch.channel_count;
140 const int row = entry % (left.dimensions + 1);
141 const int column = entry / (left.dimensions + 1);
142 const bool derivative = batch.channels[channel] != 0;
144 for (
int a = 0; a < left.atoms; ++a) {
145 const std::size_t la = batch.left_indices[pair] * left.atoms + a;
146 const int species = left.species[la];
148 (derivative && batch.channels[channel] != species))
150 const double q = left.inverse_variance[la];
151 for (
int b = 0; b < right.atoms; ++b) {
152 const std::size_t rb =
153 batch.right_indices[pair] * right.atoms + b;
154 if (species != right.species[rb])
continue;
156 jets + ((pair * left.atoms + a) * right.atoms + b) *
157 (2 + left.dimensions + right.dimensions);
158 const GaussianEnvironmentWeights weights{jet[0], jet[1]};
159 if (row == 0 && column == 0)
160 value += weights.energy(derivative) * weights.kernel;
161 else if (column == 0)
162 value -= weights.gradient(derivative) * weights.kernel * q *
165 value += weights.gradient(derivative) * weights.kernel * q *
166 jet[1 + left.dimensions + column];
168 value -= weights.kernel * q *
169 (weights.product(derivative) * q * jet[1 + row] *
170 jet[1 + left.dimensions + column]);
172 if (row != 0 && column != 0) {
173 const std::size_t matrix =
174 (pair * batch.channel_count + channel) * left.atoms + a;
175 value += grams[matrix * left.dimensions * right.dimensions +
176 (column - 1) * left.dimensions + row - 1];
179 blocks[index] = value;
#define GPR_ENVIRONMENT_FUNCTION