Debian Codegraph @

native/deprecated/NewNativeLikelihoodCore.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5
6 #include "NewNativeLikelihoodCore.h"
7
8 #define MATRIX_SIZE (STATE_COUNT + 1) * STATE_COUNT
9 #if (STATE_COUNT==4)
10 #define IS_NUCLEOTIDES
11 #endif
12
13 int nodeCount;
14 int stateTipCount;
15 int patternCount;
16 int partialsSize;
17 int matrixCount;
18
19 double* cMatrix;
20 double* storedCMatrix;
21 double* eigenValues;
22 double* storedEigenValues;
23
24 double* frequencies;
25 double* storedFrequencies;
26 double* categoryProportions;
27 double* storedCategoryProportions;
28 double* categoryRates;
29 double* storedCategoryRates;
30
31 double* integrationTmp;
32
33 double*** partials;
34 int** states;
35 double*** matrices;
36
37 int* currentMatricesIndices;
38 int* storedMatricesIndices;
39 int* currentPartialsIndices;
40 int* storedPartialsIndices;
41
42 void updateStatesStates(int nodeIndex1, int nodeIndex2, int nodeIndex3);
43 void updateStatesPartials(int nodeIndex1, int nodeIndex2, int nodeIndex3);
44 void updatePartialsPartials(int nodeIndex1, int nodeIndex2, int nodeIndex3);
45
46 void printArray(char* name, double *array, int length) {
47 fprintf(stdout, "%s:", name);
48 for (int i = 0; i < length; i++) {
49 fprintf(stdout, " %f", array[i]);
50 }
51 fprintf(stdout, "\n");
52 }
53
54 /*
55 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
56 * Method: initialize
57 * Signature: (III)V
58 */
59 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_initialize
60 (JNIEnv *env, jobject obj, jint inNodeCount, jint inStateTipCount, jint inPatternCount, jint inMatrixCount)
61 {
62 nodeCount = inNodeCount;
63 stateTipCount = inStateTipCount;
64 patternCount = inPatternCount;
65 matrixCount = inMatrixCount;
66
67 cMatrix = (double *)malloc(sizeof(double) * STATE_COUNT * STATE_COUNT * STATE_COUNT);
68 storedCMatrix = (double *)malloc(sizeof(double) * STATE_COUNT * STATE_COUNT * STATE_COUNT);
69
70 eigenValues = (double *)malloc(sizeof(double) * STATE_COUNT);
71 storedEigenValues = (double *)malloc(sizeof(double) * STATE_COUNT);
72
73 frequencies = (double *)malloc(sizeof(double) * STATE_COUNT);
74 storedFrequencies = (double *)malloc(sizeof(double) * STATE_COUNT);
75
76 categoryRates = (double *)malloc(sizeof(double) * matrixCount);
77 storedCategoryRates = (double *)malloc(sizeof(double) * matrixCount);
78
79 categoryProportions = (double *)malloc(sizeof(double) * matrixCount);
80 storedCategoryProportions = (double *)malloc(sizeof(double) * matrixCount);
81
82 // a temporary array used in calculating log likelihoods
83 integrationTmp = (double *)malloc(sizeof(double) * patternCount * STATE_COUNT);
84
85 partialsSize = patternCount * STATE_COUNT * matrixCount;
86
87 partials = (double ***)malloc(sizeof(double**) * 2);
88 partials[0] = (double **)malloc(sizeof(double*) * nodeCount);
89 partials[1] = (double **)malloc(sizeof(double*) * nodeCount);
90
91 states = (int **)malloc(sizeof(int*) * nodeCount);
92
93 for (int i = 0; i < nodeCount; i++) {
94 partials[0][i] = (double *)malloc(sizeof(double) * partialsSize);
95 partials[1][i] = (double *)malloc(sizeof(double) * partialsSize);
96 states[i] = (int *)malloc(sizeof(int) * patternCount * matrixCount);
97 }
98
99 currentMatricesIndices = (int *)malloc(sizeof(int) * nodeCount);
100 memset(currentMatricesIndices, 0, sizeof(int) * nodeCount);
101 storedMatricesIndices = (int *)malloc(sizeof(int) * nodeCount);
102
103 currentPartialsIndices = (int *)malloc(sizeof(int) * nodeCount);
104 memset(currentPartialsIndices, 0, sizeof(int) * nodeCount);
105 storedPartialsIndices = (int *)malloc(sizeof(int) * nodeCount);
106
107 matrices = (double ***)malloc(sizeof(double**) * 2);
108 matrices[0] = (double **)malloc(sizeof(double*) * nodeCount);
109 matrices[1] = (double **)malloc(sizeof(double*) * nodeCount);
110 for (int i = 0; i < nodeCount; i++) {
111 matrices[0][i] = (double *)malloc(sizeof(double) * matrixCount * MATRIX_SIZE);
112 matrices[1][i] = (double *)malloc(sizeof(double) * matrixCount * MATRIX_SIZE);
113 }
114
115 }
116
117 /*
118 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
119 * Method: freeNativeMemory
120 * Signature: ()V
121 */
122 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_freeNativeMemory
123 (JNIEnv *env, jobject obj)
124 {
125 // free all that stuff...
126 }
127
128 /*
129 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
130 * Method: setTipPartials
131 * Signature: (I[D)V
132 */
133 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_setTipPartials
134 (JNIEnv *env, jobject obj, jint tipIndex, jdoubleArray inTipPartials)
135 {
136 jdouble *tipPartials = (jdouble*)(*env)->GetPrimitiveArrayCritical(env, inTipPartials, 0);
137
138 int k = 0;
139 for (int i = 0; i < matrixCount; i++) {
140 memcpy(partials[0][tipIndex] + k, tipPartials, sizeof(double) * patternCount * STATE_COUNT);
141 k += patternCount * STATE_COUNT;
142 }
143
144 (*env)->ReleasePrimitiveArrayCritical(env, inTipPartials, tipPartials, JNI_ABORT);
145 }
146
147 /*
148 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
149 * Method: setTipStates
150 * Signature: (I[I)V
151 */
152 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_setTipStates
153 (JNIEnv *env, jobject obj, jint tipIndex, jintArray inTipStates)
154 {
155 jint *tipStates = (jint*)(*env)->GetPrimitiveArrayCritical(env, inTipStates, 0);
156
157 int k = 0;
158 for (int i = 0; i < matrixCount; i++) {
159 for (int j = 0; j < patternCount; j++) {
160 states[tipIndex][k] = (tipStates[j] < STATE_COUNT ? tipStates[j] : STATE_COUNT);
161 k++;
162 }
163 }
164
165 (*env)->ReleasePrimitiveArrayCritical(env, inTipStates, tipStates, JNI_ABORT);
166 }
167
168 /*
169 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
170 * Method: updateRootFrequencies
171 * Signature: ([D)V
172 */
173 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_updateRootFrequencies
174 (JNIEnv *env, jobject obj, jdoubleArray inFrequencies)
175 {
176 (*env)->GetDoubleArrayRegion(env, inFrequencies, 0, STATE_COUNT, frequencies);
177 }
178
179 /*
180 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
181 * Method: updateEigenDecomposition
182 * Signature: ([[D[[D[D)V
183 */
184 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_updateEigenDecomposition
185 (JNIEnv *env, jobject obj, jobjectArray inEigenVectors, jobjectArray inInvEigenVectors, jdoubleArray inEigenValues)
186 {
187 double Evec[STATE_COUNT][STATE_COUNT];
188 double Ievc[STATE_COUNT][STATE_COUNT];
189
190 for (int i = 0; i < STATE_COUNT; i++) {
191 jdoubleArray oneDim = (jdoubleArray)(*env)->GetObjectArrayElement(env, inEigenVectors, i);
192 jdouble *element = (*env)->GetDoubleArrayElements(env, oneDim, 0);
193 for(int j = 0; j < STATE_COUNT; j++) {
194 Evec[i][j]= element[j];
195 }
196 }
197
198 for (int i = 0; i < STATE_COUNT; i++) {
199 jdoubleArray oneDim = (jdoubleArray)(*env)->GetObjectArrayElement(env, inInvEigenVectors, i);
200 jdouble *element = (*env)->GetDoubleArrayElements(env, oneDim, 0);
201 for(int j = 0; j < STATE_COUNT; j++) {
202 Ievc[i][j]= element[j];
203 }
204 }
205
206 int l =0;
207 for (int i = 0; i < STATE_COUNT; i++) {
208 for (int j = 0; j < STATE_COUNT; j++) {
209 for (int k = 0; k < STATE_COUNT; k++) {
210 cMatrix[l] = Evec[i][k] * Ievc[k][j];
211 l++;
212 }
213 }
214 }
215
216 (*env)->GetDoubleArrayRegion(env, inEigenValues, 0, STATE_COUNT, eigenValues);
217 }
218
219 /*
220 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
221 * Method: updateCategoryRates
222 * Signature: ([D)V
223 */
224 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_updateCategoryRates
225 (JNIEnv *env, jobject obj, jdoubleArray inRates)
226 {
227 (*env)->GetDoubleArrayRegion(env, inRates, 0, matrixCount, categoryRates);
228 }
229
230 /*
231 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
232 * Method: updateCategoryProportions
233 * Signature: ([D)V
234 */
235 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_updateCategoryProportions
236 (JNIEnv *env, jobject obj, jdoubleArray inProportions)
237 {
238 (*env)->GetDoubleArrayRegion(env, inProportions, 0, matrixCount, categoryProportions);
239 }
240
241 /*
242 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
243 * Method: updateMatrices
244 * Signature: ([I[DI)V
245 */
246 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_updateMatrices
247 (JNIEnv *env, jobject obj, jintArray inNodeIndices, jdoubleArray inBranchLengths, jint count)
248 {
249 jint *nodeIndices = (jint*)(*env)->GetPrimitiveArrayCritical(env, inNodeIndices, 0);
250 jdouble *branchLengths = (jdouble*)(*env)->GetPrimitiveArrayCritical(env, inBranchLengths, 0);
251
252 static double tmp[STATE_COUNT];
253
254 for (int x = 0; x < count; x++) {
255 int nodeIndex = nodeIndices[x];
256 double branchLength = branchLengths[x];
257
258 currentMatricesIndices[nodeIndex] = 1 - currentMatricesIndices[nodeIndex];
259
260 int n = 0;
261 for (int l = 0; l < matrixCount; l++) {
262 for (int i = 0; i < STATE_COUNT; i++) {
263 tmp[i] = exp(eigenValues[i] * branchLength * categoryRates[l]);
264 }
265
266 int m = 0;
267 for (int i = 0; i < STATE_COUNT; i++) {
268 for (int j = 0; j < STATE_COUNT; j++) {
269 double sum = 0.0;
270 for (int k = 0; k < STATE_COUNT; k++) {
271 sum += cMatrix[m] * tmp[k];
272 m++;
273 }
274 matrices[currentMatricesIndices[nodeIndex]][nodeIndex][n] = sum;
275 n++;
276 }
277 matrices[currentMatricesIndices[nodeIndex]][nodeIndex][n] = 1.0;
278 n++;
279 }
280 }
281
282
283 }
284
285 (*env)->ReleasePrimitiveArrayCritical(env, inBranchLengths, branchLengths, JNI_ABORT);
286 (*env)->ReleasePrimitiveArrayCritical(env, inNodeIndices, nodeIndices, JNI_ABORT);
287 }
288
289 /*
290 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
291 * Method: updatePartials
292 * Signature: ([I[II)V
293 */
294 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_updatePartials
295 (JNIEnv *env, jobject obj, jintArray inOperations, jintArray inDependencies, jint operationCount)
296 {
297 jint *operations = (jint*)(*env)->GetPrimitiveArrayCritical(env, inOperations, 0);
298 /* not required:
299 jint *dependencies = (jint*)(*env)->GetPrimitiveArrayCritical(env, inDependencies, 0);
300 */
301
302 int x = 0;
303 for (int op = 0; op < operationCount; op++) {
304 int nodeIndex1 = operations[x];
305 x++;
306 int nodeIndex2 = operations[x];
307 x++;
308 int nodeIndex3 = operations[x];
309 x++;
310 currentPartialsIndices[nodeIndex3] = 1 - currentPartialsIndices[nodeIndex3];
311
312 if (nodeIndex1 < stateTipCount) {
313 if (nodeIndex2 < stateTipCount) {
314 updateStatesStates(nodeIndex1, nodeIndex2, nodeIndex3);
315 } else {
316 updateStatesPartials(nodeIndex1, nodeIndex2, nodeIndex3);
317 }
318 } else {
319 if (nodeIndex2 < stateTipCount) {
320 updateStatesPartials(nodeIndex2, nodeIndex1, nodeIndex3);
321 } else {
322 updatePartialsPartials(nodeIndex1, nodeIndex2, nodeIndex3);
323 }
324 }
325 }
326 /* not required:
327 (*env)->ReleasePrimitiveArrayCritical(env, inDependencies, dependencies, JNI_ABORT);
328 */
329 (*env)->ReleasePrimitiveArrayCritical(env, inOperations, operations, JNI_ABORT);
330 }
331
332 /*
333 * Calculates partial likelihoods at a node when both children have states.
334 */
335 void updateStatesStates(int nodeIndex1, int nodeIndex2, int nodeIndex3)
336 {
337 double* matrices1 = matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1];
338 double* matrices2 = matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2];
339
340 int* states1 = states[nodeIndex1];
341 int* states2 = states[nodeIndex2];
342
343 double* partials3 = partials[currentPartialsIndices[nodeIndex3]][nodeIndex3];
344
345 #ifdef IS_NUCLEOTIDES
346
347 int v = 0;
348 for (int l = 0; l < matrixCount; l++) {
349
350 for (int k = 0; k < patternCount; k++) {
351
352 int state1 = states1[k];
353 int state2 = states2[k];
354
355 int w = l * MATRIX_SIZE;
356
357 partials3[v] = matrices1[w + state1] * matrices2[w + state2];
358 v++; w += (STATE_COUNT + 1);
359 partials3[v] = matrices1[w + state1] * matrices2[w + state2];
360 v++; w += (STATE_COUNT + 1);
361 partials3[v] = matrices1[w + state1] * matrices2[w + state2];
362 v++; w += (STATE_COUNT + 1);
363 partials3[v] = matrices1[w + state1] * matrices2[w + state2];
364 v++; w += (STATE_COUNT + 1);
365
366 }
367 }
368
369 #else
370
371 int v = 0;
372 for (int l = 0; l < matrixCount; l++) {
373
374 for (int k = 0; k < patternCount; k++) {
375
376 int state1 = states1[k];
377 int state2 = states2[k];
378
379 int w = l * MATRIX_SIZE;
380
381 for (int i = 0; i < STATE_COUNT; i++) {
382
383 partials3[v] = matrices1[w + state1] * matrices2[w + state2];
384
385 v++;
386 w += (STATE_COUNT + 1);
387 }
388
389 }
390 }
391 #endif
392 }
393
394 /*
395 * Calculates partial likelihoods at a node when one child has states and one has partials.
396 */
397 void updateStatesPartials(int nodeIndex1, int nodeIndex2, int nodeIndex3)
398 {
399 double* matrices1 = matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1];
400 double* matrices2 = matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2];
401
402 int* states1 = states[nodeIndex1];
403 double* partials2 = partials[currentPartialsIndices[nodeIndex2]][nodeIndex2];
404
405 double* partials3 = partials[currentPartialsIndices[nodeIndex3]][nodeIndex3];
406
407 #ifdef IS_NUCLEOTIDES
408
409 int u = 0;
410 int v = 0;
411
412 for (int l = 0; l < matrixCount; l++) {
413 for (int k = 0; k < patternCount; k++) {
414
415 int state1 = states1[k];
416
417 int w = l * MATRIX_SIZE;
418
419 partials3[u] = matrices1[w + state1];
420
421 double sum = matrices2[w] * partials2[v]; w++;
422 sum += matrices2[w] * partials2[v + 1]; w++;
423 sum += matrices2[w] * partials2[v + 2]; w++;
424 sum += matrices2[w] * partials2[v + 3]; w++;
425 w++; // increment for the extra column at the end
426 partials3[u] *= sum; u++;
427
428 partials3[u] = matrices1[w + state1];
429
430 sum = matrices2[w] * partials2[v]; w++;
431 sum += matrices2[w] * partials2[v + 1]; w++;
432 sum += matrices2[w] * partials2[v + 2]; w++;
433 sum += matrices2[w] * partials2[v + 3]; w++;
434 w++; // increment for the extra column at the end
435 partials3[u] *= sum; u++;
436
437 partials3[u] = matrices1[w + state1];
438
439 sum = matrices2[w] * partials2[v]; w++;
440 sum += matrices2[w] * partials2[v + 1]; w++;
441 sum += matrices2[w] * partials2[v + 2]; w++;
442 sum += matrices2[w] * partials2[v + 3]; w++;
443 w++; // increment for the extra column at the end
444 partials3[u] *= sum; u++;
445
446 partials3[u] = matrices1[w + state1];
447
448 sum = matrices2[w] * partials2[v]; w++;
449 sum += matrices2[w] * partials2[v + 1]; w++;
450 sum += matrices2[w] * partials2[v + 2]; w++;
451 sum += matrices2[w] * partials2[v + 3]; w++;
452 w++; // increment for the extra column at the end
453 partials3[u] *= sum; u++;
454
455 v += 4;
456
457 }
458 }
459
460 #else
461 int u = 0;
462 int v = 0;
463
464 for (int l = 0; l < matrixCount; l++) {
465 for (int k = 0; k < patternCount; k++) {
466
467 int state1 = states1[k];
468
469 int w = l * MATRIX_SIZE;
470
471 for (int i = 0; i < STATE_COUNT; i++) {
472
473 double tmp = matrices1[w + state1];
474
475 double sum = 0.0;
476 for (int j = 0; j < STATE_COUNT; j++) {
477 sum += matrices2[w] * partials2[v + j];
478 w++;
479 }
480
481 // increment for the extra column at the end
482 w++;
483
484 partials3[u] = tmp * sum;
485 u++;
486 }
487
488 v += STATE_COUNT;
489 }
490 }
491 #endif
492 }
493
494 void updatePartialsPartials(int nodeIndex1, int nodeIndex2, int nodeIndex3)
495 {
496 double* matrices1 = matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1];
497 double* matrices2 = matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2];
498
499 double* partials1 = partials[currentPartialsIndices[nodeIndex1]][nodeIndex1];
500 double* partials2 = partials[currentPartialsIndices[nodeIndex2]][nodeIndex2];
501
502 double* partials3 = partials[currentPartialsIndices[nodeIndex3]][nodeIndex3];
503
504 /* fprintf(stdout, "*** operation %d: %d, %d -> %d\n", op, nodeIndex1, nodeIndex2, nodeIndex3); */
505
506 double sum1, sum2;
507
508 #ifdef IS_NUCLEOTIDES
509
510 int u = 0;
511 int v = 0;
512
513 for (int l = 0; l < matrixCount; l++) {
514 for (int k = 0; k < patternCount; k++) {
515
516 int w = l * MATRIX_SIZE;
517
518 sum1 = matrices1[w] * partials1[v];
519 sum2 = matrices2[w] * partials2[v]; w++;
520 sum1 += matrices1[w] * partials1[v + 1];
521 sum2 += matrices2[w] * partials2[v + 1]; w++;
522 sum1 += matrices1[w] * partials1[v + 2];
523 sum2 += matrices2[w] * partials2[v + 2]; w++;
524 sum1 += matrices1[w] * partials1[v + 3];
525 sum2 += matrices2[w] * partials2[v + 3]; w++;
526 w++; // increment for the extra column at the end
527 partials3[u] = sum1 * sum2; u++;
528
529 sum1 = matrices1[w] * partials1[v];
530 sum2 = matrices2[w] * partials2[v]; w++;
531 sum1 += matrices1[w] * partials1[v + 1];
532 sum2 += matrices2[w] * partials2[v + 1]; w++;
533 sum1 += matrices1[w] * partials1[v + 2];
534 sum2 += matrices2[w] * partials2[v + 2]; w++;
535 sum1 += matrices1[w] * partials1[v + 3];
536 sum2 += matrices2[w] * partials2[v + 3]; w++;
537 w++; // increment for the extra column at the end
538 partials3[u] = sum1 * sum2; u++;
539
540 sum1 = matrices1[w] * partials1[v];
541 sum2 = matrices2[w] * partials2[v]; w++;
542 sum1 += matrices1[w] * partials1[v + 1];
543 sum2 += matrices2[w] * partials2[v + 1]; w++;
544 sum1 += matrices1[w] * partials1[v + 2];
545 sum2 += matrices2[w] * partials2[v + 2]; w++;
546 sum1 += matrices1[w] * partials1[v + 3];
547 sum2 += matrices2[w] * partials2[v + 3]; w++;
548 w++; // increment for the extra column at the end
549 partials3[u] = sum1 * sum2; u++;
550
551 sum1 = matrices1[w] * partials1[v];
552 sum2 = matrices2[w] * partials2[v]; w++;
553 sum1 += matrices1[w] * partials1[v + 1];
554 sum2 += matrices2[w] * partials2[v + 1]; w++;
555 sum1 += matrices1[w] * partials1[v + 2];
556 sum2 += matrices2[w] * partials2[v + 2]; w++;
557 sum1 += matrices1[w] * partials1[v + 3];
558 sum2 += matrices2[w] * partials2[v + 3]; w++;
559 w++; // increment for the extra column at the end
560 partials3[u] = sum1 * sum2; u++;
561
562 v += 4;
563
564 }
565 }
566
567 #else
568
569 int u = 0;
570 int v = 0;
571
572 for (int l = 0; l < matrixCount; l++) {
573
574 for (int k = 0; k < patternCount; k++) {
575
576 int w = l * MATRIX_SIZE;
577
578 for (int i = 0; i < STATE_COUNT; i++) {
579
580 sum1 = sum2 = 0.0;
581
582 for (int j = 0; j < STATE_COUNT; j++) {
583 sum1 += matrices1[w] * partials1[v + j];
584 sum2 += matrices2[w] * partials2[v + j];
585 w++;
586 }
587
588 // increment for the extra column at the end
589 w++;
590
591 partials3[u] = sum1 * sum2;
592
593 u++;
594 }
595 v += STATE_COUNT;
596 }
597 }
598
599 #endif
600 }
601
602 /*
603 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
604 * Method: calculateLogLikelihoods
605 * Signature: (I[D)V
606 */
607
608 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_calculateLogLikelihoods
609 (JNIEnv *env, jobject obj, jint rootNodeIndex, jdoubleArray outLogLikelihoods)
610 {
611 jdouble *logLikelihoods = (jdouble*)(*env)->GetPrimitiveArrayCritical(env, outLogLikelihoods, 0);
612
613 double* rootPartials = partials[currentPartialsIndices[rootNodeIndex]][rootNodeIndex];
614
615 int u = 0;
616 int v = 0;
617 for (int k = 0; k < patternCount; k++) {
618
619 for (int i = 0; i < STATE_COUNT; i++) {
620
621 integrationTmp[u] = rootPartials[v] * categoryProportions[0];
622 u++;
623 v++;
624 }
625 }
626
627
628 for (int l = 1; l < matrixCount; l++) {
629 u = 0;
630
631 for (int k = 0; k < patternCount; k++) {
632
633 for (int i = 0; i < STATE_COUNT; i++) {
634
635 integrationTmp[u] += rootPartials[v] * categoryProportions[l];
636 u++;
637 v++;
638 }
639 }
640 }
641
642 u = 0;
643 for (int k = 0; k < patternCount; k++) {
644
645 double sum = 0.0;
646 for (int i = 0; i < STATE_COUNT; i++) {
647
648 sum += frequencies[i] * integrationTmp[u];
649 u++;
650 }
651 logLikelihoods[k] = log(sum);
652 }
653
654 (*env)->ReleasePrimitiveArrayCritical(env, outLogLikelihoods, logLikelihoods, JNI_ABORT);
655 }
656
657 /*
658 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
659 * Method: storeState
660 * Signature: ()V
661 */
662 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_storeState
663 (JNIEnv *env, jobject obj)
664 {
665 memcpy(storedCMatrix, cMatrix, sizeof(double) * STATE_COUNT * STATE_COUNT * STATE_COUNT);
666 memcpy(storedEigenValues, eigenValues, sizeof(double) * STATE_COUNT);
667
668 memcpy(storedFrequencies, frequencies, sizeof(double) * STATE_COUNT);
669 memcpy(storedCategoryRates, categoryRates, sizeof(double) * matrixCount);
670 memcpy(storedCategoryProportions, categoryProportions, sizeof(double) * matrixCount);
671
672 memcpy(storedMatricesIndices, currentMatricesIndices, sizeof(int) * nodeCount);
673 memcpy(storedPartialsIndices, currentPartialsIndices, sizeof(int) * nodeCount);
674 }
675
676 /*
677 * Class: dr_evomodel_newtreelikelihood_NativeLikelihoodCore
678 * Method: restoreState
679 * Signature: ()V
680 */
681 JNIEXPORT void JNICALL Java_dr_evomodel_newtreelikelihood_NativeLikelihoodCore_restoreState
682 (JNIEnv *env, jobject obj)
683 {
684 // Rather than copying the stored stuff back, just swap the pointers...
685 double* tmp = cMatrix;
686 cMatrix = storedCMatrix;
687 storedCMatrix = tmp;
688
689 tmp = eigenValues;
690 eigenValues = storedEigenValues;
691 storedEigenValues = tmp;
692
693 tmp = frequencies;
694 frequencies = storedFrequencies;
695 storedFrequencies = tmp;
696
697 tmp = categoryRates;
698 categoryRates = storedCategoryRates;
699 storedCategoryRates = tmp;
700
701 tmp = categoryProportions;
702 categoryProportions = storedCategoryProportions;
703 storedCategoryProportions = tmp;
704
705 int* tmp2 = currentMatricesIndices;
706 currentMatricesIndices = storedMatricesIndices;
707 storedMatricesIndices = tmp2;
708
709 tmp2 = currentPartialsIndices;
710 currentPartialsIndices = storedPartialsIndices;
711 storedPartialsIndices = tmp2;
712 }
713