Skip to content

Commit

Permalink
optimal.c: updated memory allocation to R allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhahsler committed Aug 13, 2024
1 parent fca0d04 commit cf3e22a
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 144 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: seriation
Type: Package
Title: Infrastructure for Ordering Objects Using Seriation
Version: 1.5.5.1
Date: 2024-xx-xx
Version: 1.5.6
Date: 2024-08-13
Authors@R: c(
person("Michael", "Hahsler", role = c("aut", "cre", "cph"),
email = "mhahsler@lyle.smu.edu",
Expand Down Expand Up @@ -52,7 +52,7 @@ Suggests: dbscan,
testthat,
umap
Encoding: UTF-8
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
License: GPL-3
Copyright: The code in src/bbwrcg.f, src/arsa.f and src/bburcg.f
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# seriation 1.5.5.1 (xx/xx/2024)
# seriation 1.5.6 (08/13/2024)

## New Features
- Added registered_by field to registries.

## Changes
- We replaced the FORTRAN implementation for BEA with code from package TSP.
- ME is now calculated using C code.
- optimal.c: updated memory allocation to R allocation.

## Bug Fixes
- Added two missing package anchors to palette man page.
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ visual assessment of cluster tendency plots (VAT and iVAT).

Here are some quick guides on applications of seriation:

* [Introduction the R package seriation](https://cran.r-project.org/web/packages/seriation/vignettes/seriation.pdf)
* [Introduction the R package seriation](https://cran.r-project.org/package=seriation/vignettes/seriation.pdf)
* [How to reorder heatmaps](https://mhahsler.github.io/seriation/heatmaps.html)
* [How to reorder correlation matrices](https://mhahsler.github.io/seriation/correlation_matrix.html)
* [How to evaluate clusters using dissimilarity plots](https://mhahsler.github.io/seriation/clustering.html)
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ assessment of cluster tendency plots (VAT and iVAT).
Here are some quick guides on applications of seriation:

- [Introduction the R package
seriation](https://cran.r-project.org/web/packages/seriation/vignettes/seriation.pdf)
seriation](https://cran.r-project.org/package=seriation/vignettes/seriation.pdf)
- [How to reorder
heatmaps](https://mhahsler.github.io/seriation/heatmaps.html)
- [How to reorder correlation
Expand Down Expand Up @@ -230,7 +230,8 @@ install.packages("seriation")

``` r
install.packages("seriation",
repos = c("https://mhahsler.r-universe.dev". "https://cloud.r-project.org/"))
repos = c("https://mhahsler.r-universe.dev",
"https://cloud.r-project.org/"))
```

## Usage
Expand Down Expand Up @@ -298,10 +299,7 @@ measures. Note that some measures are merit measures while others
represent cost. See the manual page for details.

``` r
rbind(
alphabetical = criterion(d),
seriated = criterion(d, order)
)
rbind(alphabetical = criterion(d), seriated = criterion(d, order))
```

## 2SUM AR_deviations AR_events BAR Gradient_raw Gradient_weighted
Expand Down
46 changes: 23 additions & 23 deletions src/optimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ SEXP order_length(SEXP R_dist, SEXP R_order) {
if (LENGTH(R_dist) != n * (n - 1) / 2)
error("order_length: length of \"dist\" and \"order\" do not match");

o = Calloc(n, int);
o = R_Calloc(n, int);

for (k = 0; k < n; k++) /* offset to C indexing */
o[k] = INTEGER(R_order)[k]-1;
o[k] = INTEGER(R_order)[k]-1;

PROTECT(R_obj = NEW_NUMERIC(1));

REAL(R_obj)[0] = orderLength(REAL(R_dist), o, n);
Free(o);
R_Free(o);

UNPROTECT(1);

Expand Down Expand Up @@ -276,14 +276,14 @@ SEXP order_optimal(SEXP R_dist, SEXP R_merge) {

/* copy similarities into lower triangle */

x = Calloc(n*n, double); /* data + part order lengths + temporary */
x = R_Calloc(n*n, double); /* data + part order lengths + temporary */

k = 0;
for (i = 0; i < n-1; i++)
for (j = i+1; j < n; j++) {
z = REAL(R_dist)[k++];
if (!R_FINITE(z)) {
Free(x);
R_Free(x);
error("order_optimal: \"dist\" invalid");
}
else
Expand All @@ -302,12 +302,12 @@ SEXP order_optimal(SEXP R_dist, SEXP R_merge) {

GetRNGstate();

l = Calloc(n, int); /* offset of leftmost leaf of left tree */
r = Calloc(n, int); /* offset of leftmost leaf of right tree;
l = R_Calloc(n, int); /* offset of leftmost leaf of left tree */
r = R_Calloc(n, int); /* offset of leftmost leaf of right tree;
* reverse mapping of order */
c = Calloc(n-1, int); /* number of leaves in a tree */
c = R_Calloc(n-1, int); /* number of leaves in a tree */

e = Calloc(n*n, int); /* inner endpoints */
e = R_Calloc(n*n, int); /* inner endpoints */

/* for each tree count the number of leaves.
*/
Expand Down Expand Up @@ -395,13 +395,13 @@ SEXP order_optimal(SEXP R_dist, SEXP R_merge) {
/* compute temporary sums for all endpoints */

if (!calcAllOrder(x, e, oll, olr, or, cll, clr, cr, n)) {
Free(x); Free(r); Free(l); Free(c); Free(e);
R_Free(x); R_Free(r); R_Free(l); R_Free(c); R_Free(e);
error("order_optimal: non-finite values");
}

if (olr != oll)
if (!calcAllOrder(x, e, olr, oll, or, clr, cll, cr, n)) {
Free(x); Free(r); Free(l); Free(c); Free(e);
R_Free(x); R_Free(r); R_Free(l); R_Free(c); R_Free(e);
error("order_optimal: non-finite values");
}

Expand All @@ -421,13 +421,13 @@ SEXP order_optimal(SEXP R_dist, SEXP R_merge) {
/* compute best orders for all endpoints */

if (!calcAllOrder(x, e, orl, orr, ol, crl, crr, cl, n)) {
Free(x); Free(r); Free(l); Free(c); Free(e);
R_Free(x); R_Free(r); R_Free(l); R_Free(c); R_Free(e);
error("order_optimal: non-finite values");
}

if (orr != orl)
if (!calcAllOrder(x, e, orr, orl, ol, crr, crl, cl, n)) {
Free(x); Free(r); Free(l); Free(c); Free(e);
R_Free(x); R_Free(r); R_Free(l); R_Free(c); R_Free(e);
error("order_optimal: non-finite values");
}

Expand Down Expand Up @@ -467,24 +467,24 @@ SEXP order_optimal(SEXP R_dist, SEXP R_merge) {
*/

if (!calcEndOrder(x, e, oll, olr, cll, clr, n)) {
Free(x); Free(r); Free(l); Free(c); Free(e);
R_Free(x); R_Free(r); R_Free(l); R_Free(c); R_Free(e);
error("order_optimal: non-finite values");
}

if (olr != oll)
if (!calcEndOrder(x, e, olr, oll, clr, cll, n)) {
Free(x); Free(r); Free(l); Free(c); Free(e);
R_Free(x); R_Free(r); R_Free(l); R_Free(c); R_Free(e);
error("order_optimal: non-finite values");
}

if (!calcEndOrder(x, e, orl, orr, crl, crr, n)) {
Free(x); Free(r); Free(l); Free(c); Free(e);
R_Free(x); R_Free(r); R_Free(l); R_Free(c); R_Free(e);
error("order_optimal: non-finite values");
}

if (orr != orl)
if (!calcEndOrder(x, e, orr, orl, crr, crl, n)) {
Free(x); Free(r); Free(l); Free(c); Free(e);
R_Free(x); R_Free(r); R_Free(l); R_Free(c); R_Free(e);
error("order_optimal: non-finite values");
}

Expand Down Expand Up @@ -517,7 +517,7 @@ SEXP order_optimal(SEXP R_dist, SEXP R_merge) {
}
}
if (!R_FINITE(z)) {
Free(x); Free(r); Free(l); Free(c); Free(e);
R_Free(x); R_Free(r); R_Free(l); R_Free(c); R_Free(e);
error("order_optimal: non-finite values");
}
}
Expand Down Expand Up @@ -620,11 +620,11 @@ SEXP order_optimal(SEXP R_dist, SEXP R_merge) {
}
}

Free(x);
Free(l);
Free(r);
Free(c);
Free(e);
R_Free(x);
R_Free(l);
R_Free(r);
R_Free(c);
R_Free(e);

PutRNGstate();

Expand Down
Loading

0 comments on commit cf3e22a

Please sign in to comment.