]> git.donarmstrong.com Git - ape.git/blob - src/plot_phylo.c
faster nj()!!!
[ape.git] / src / plot_phylo.c
1 /* plot_phylo.c (2006-10-13) */
2
3 /* Copyright 2004-2006 Emmanuel Paradis
4
5 /* This file is part of the R-package `ape'. */
6 /* See the file ../COPYING for licensing issues. */
7
8 #include <R.h>
9
10 void node_depth_edgelength(int *ntip, int *nnode, int *edge1, int *edge2,
11                            int *nedge, double *edge_length, double *xx)
12 {
13     int i;
14
15     /* We do a preorder tree traversal starting from the bottom */
16     /* of `edge'; we assume `xx' has 0 for the root and the tree */
17     /* is in pruningwise order. */
18     for (i = *nedge - 1; i >= 0; i--)
19       xx[edge2[i] - 1] = xx[edge1[i] - 1] + edge_length[i];
20 }
21
22 void node_depth(int *ntip, int *nnode, int *edge1, int *edge2,
23                 int *nedge, double *xx)
24 {
25     int i;
26
27     /* First set the coordinates for all tips */
28     for (i = 0; i < *ntip; i++) xx[i] = 1;
29
30     /* Then compute recursively for the nodes; we assume `xx' has */
31     /* been initialized with 0's which is true if it has been */
32     /* created in R (the tree must be in pruningwise order) */
33     for (i = 0; i < *nedge; i++)
34       xx[edge1[i] - 1] = xx[edge1[i] - 1] + xx[edge2[i] - 1];
35 }
36
37 void node_height(int *ntip, int *nnode, int *edge1, int *edge2,
38                 int *nedge, double *yy)
39 {
40     int i, k, n;
41     double S;
42
43     /* The coordinates of the tips have been already computed */
44
45     k = 1;
46     S = 0;
47     n = 0;
48     for (i = 0; i < *nedge; i++) {
49         S += yy[edge2[i] - 1];
50         n += 1;
51         if (edge1[i + 1] != edge1[i]) {
52             yy[edge1[i] - 1] = S/n;
53             S = 0;
54             n = 0;
55         }
56     }
57 }
58
59 void node_height_clado(int *ntip, int *nnode, int *edge1, int *edge2,
60                        int *nedge, double *xx, double *yy)
61 {
62     int i, k, n;
63     double S;
64
65     node_depth(ntip, nnode, edge1, edge2, nedge, xx);
66
67     /* The coordinates of the tips have been already computed */
68
69     k = 1;
70     S = 0;
71     n = 0;
72     for (i = 0; i < *nedge; i++) {
73         S += yy[edge2[i] - 1] * xx[edge2[i] - 1];
74         n += xx[edge2[i] - 1];
75         if (edge1[i + 1] != edge1[i]) {
76             yy[edge1[i] - 1] = S/n;
77             S = 0;
78             n = 0;
79         }
80     }
81 }