Code Monkey home page Code Monkey logo

magic's Introduction

Manipulation of high-dimensional arrays in R with the magic package

Total Downloads CRAN_Status_Badge

Overview

The magic package implements functionality for manipulating high-dimensional arrays using efficient vectorised methods. The original application was high-dimensional magic hypercubes. This README shows some of the more useful functions in the package.

Installation

You can install the released version of magic from CRAN with:

# install.packages("magic")  # uncomment this to install the package
library("magic")

Package highlights

  • Function adiag() binds arbitrarily-dimensioned arrays corner-to-corner
  • Function apad() pads arbitrarily-dimensioned arrays
  • Function apldrop() is a replacement for APL’s drop
  • Function aplus() superimposes two arrays of different dimensions and returns the sum of overlapping elements
  • Function arev() is a multidimensional generalization of rev()
  • Function arot() is a generalization of matlab’s rotdim
  • Function fnsd() returns the first nonsingleton dimension of an arbitrary dimensioned array
  • Function ashift() shifts the origin of arbitrary dimensioned arrays

Much of the package functionality is vectorised in array dimension.

Further information

For more detail, see the package vignette

vignette("magic")

magic's People

Contributors

robinhankin avatar biogeek avatar

Stargazers

Vladislav Gudkov avatar Superquant avatar

Watchers

James Cloos avatar Superquant avatar  avatar

magic's Issues

poor documentation in latin.Rd

Tim Hesterberg writes:

Thank you for writing the magic package.

library(magic)
help(latin)

brings up a help file that does not include 'latin' in the usage section
(though latin.Rd includes alias{latin}

Trenckler hypercubes

Trenckler hypercubes are not implemented but:

trenckler.2np1 <- function(m,d){  # n = 2m+1
    n <- 2*m+1
    eg <- as.matrix(cbind(expand.grid(rep(list(seq_len(n)),d)),1))
    
    f <- function(k){
        eg %*% 
            c(
                (-1)^(seq_len(k)-1), rep((-1)^k,d-k),   
                (-1)^(k+1) * (d-k-(k+1)%%2)*((n+1)/2)-1 # C_k
              ) %% n
    }
    
    dm <- seq_len(d)-1  # 0:(d-1)
    out <-  1 + sapply(dm,f) %*% n^dm
    dim(out) <- rep(n,d)
    return(force.integer(out))
}

trenckler.4n <- function(m,d){  # n = 4*m
    n <- 4*m
    eg <- as.matrix(expand.grid(rep(list(seq_len(n)),d)))
    f <- function(x){ # condition on top line
       ( sum(x + floor((2*(x-1)/n))) %%2) == 1
    }
    crit <- apply(eg,1,f) 
    eg[crit,] <- n+1-eg[crit,]  # defn on p1
    out <- 1 + (eg-1) %*% n^(seq_len(d)-1)
    dim(out) <- rep(n,d)
    return(force.integer(out))
}

magic.prime exception

magic.prime says "Claimed to work for prime order," but:

> is.magic(magic.prime(2))
[1] FALSE
> magic.prime(2)
     [,1] [,2]
[1,]    1    4
[2,]    2    3
>

and of course 2 is prime!

adiag

I would find it useful if adiag

  1. also took a list as input like Matrix::bdiag does.
  2. accepted all numeric data frames in addition to matrices.

magic rectangles and magic hyperrectangles

Magic rectangles and hyperrectangles are well-defined and here is a little function I wrote a couple of years ago to test for this:

`is.magichyperrectangle` <- function(a, ndim, give.answers = FALSE, func = sum, boolean = FALSE) 
{
    n <- dim(a)[1]
    d <- length(dim(a))
    jj <- d - ndim
    out <- apply(combn(d, jj), 2, function(i){apply(a, i, func)})

    if(boolean){
        answer <- all(out)
    } else {
        answer <- minmax(out)
    }
    
    if (give.answers) {
        dim(out) <- c(rep(n, jj), ncol(out))
        return(list(answer = answer, alice.sums = out))
    } else {
        return(answer)
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.