Skip to main content
Tweeted twitter.com/StackCodeReview/status/1183623634780348416
Became Hot Network Question
edited body
Source Link

Summary: This function generates a truth table for a boolean function of variable number of arguments. The name of the function passed and its arguments are deduced outside of the computable function scope. The returned value is a logical matrix with columns corresponding to function arguments and result.

library(R.utils)
library(stringr)

truthTable <- function(func, valuesOnly = F) {
  numArguments <- length(formals(func))
  if(valuesOnly) {
    values <- vector(length = 2^numArguments)
    for (i in 1:(2^numArguments)) {
      arguments <- rev(as.numericlogical(intToBits(i-1)))[-(1:(32-numArguments))]
      values[i] <- do.call(func, as.list(arguments))
    }
    return(values)
  }
  result <- matrix(nrow = 2^numArguments, ncol = numArguments + 1)
  colnames(result) <- c(names(formals(func)), as.character(substitute(func)))
  for (i in 1:(2^numArguments)) {
    arguments <- rev(as.logical(intToBits(i-1)))[-(1:(32-numArguments))]
    result[i, ] <- c(arguments, do.call(func, as.list(arguments)))
  }
  return(result)
}

Example:

majority <- function(a, b, c) {
  return ((a & b) | (b & c) | (a & c))
}

truthTable(majority)

Result:

         a     b     c majority
[1,] FALSE FALSE FALSE    FALSE
[2,] FALSE FALSE  TRUE    FALSE
[3,] FALSE  TRUE FALSE    FALSE
[4,] FALSE  TRUE  TRUE     TRUE
[5,]  TRUE FALSE FALSE    FALSE
[6,]  TRUE FALSE  TRUE     TRUE
[7,]  TRUE  TRUE FALSE     TRUE
[8,]  TRUE  TRUE  TRUE     TRUE

Possible improvements:

  • Faster conversion of numeric iterator to logical vector
  • Usage of apply instead of iterative computation

What could be improved?

Any advice would be appreciated.

Summary: This function generates a truth table for a boolean function of variable number of arguments. The name of the function passed and its arguments are deduced outside of the computable function scope. The returned value is a logical matrix with columns corresponding to function arguments and result.

library(R.utils)
library(stringr)

truthTable <- function(func, valuesOnly = F) {
  numArguments <- length(formals(func))
  if(valuesOnly) {
    values <- vector(length = 2^numArguments)
    for (i in 1:(2^numArguments)) {
      arguments <- rev(as.numeric(intToBits(i-1)))[-(1:(32-numArguments))]
      values[i] <- do.call(func, as.list(arguments))
    }
    return(values)
  }
  result <- matrix(nrow = 2^numArguments, ncol = numArguments + 1)
  colnames(result) <- c(names(formals(func)), as.character(substitute(func)))
  for (i in 1:(2^numArguments)) {
    arguments <- rev(as.logical(intToBits(i-1)))[-(1:(32-numArguments))]
    result[i, ] <- c(arguments, do.call(func, as.list(arguments)))
  }
  return(result)
}

Example:

majority <- function(a, b, c) {
  return ((a & b) | (b & c) | (a & c))
}

truthTable(majority)

Result:

         a     b     c majority
[1,] FALSE FALSE FALSE    FALSE
[2,] FALSE FALSE  TRUE    FALSE
[3,] FALSE  TRUE FALSE    FALSE
[4,] FALSE  TRUE  TRUE     TRUE
[5,]  TRUE FALSE FALSE    FALSE
[6,]  TRUE FALSE  TRUE     TRUE
[7,]  TRUE  TRUE FALSE     TRUE
[8,]  TRUE  TRUE  TRUE     TRUE

Possible improvements:

  • Faster conversion of numeric iterator to logical vector
  • Usage of apply instead of iterative computation

What could be improved?

Any advice would be appreciated.

Summary: This function generates a truth table for a boolean function of variable number of arguments. The name of the function passed and its arguments are deduced outside of the computable function scope. The returned value is a logical matrix with columns corresponding to function arguments and result.

library(R.utils)
library(stringr)

truthTable <- function(func, valuesOnly = F) {
  numArguments <- length(formals(func))
  if(valuesOnly) {
    values <- vector(length = 2^numArguments)
    for (i in 1:(2^numArguments)) {
      arguments <- rev(as.logical(intToBits(i-1)))[-(1:(32-numArguments))]
      values[i] <- do.call(func, as.list(arguments))
    }
    return(values)
  }
  result <- matrix(nrow = 2^numArguments, ncol = numArguments + 1)
  colnames(result) <- c(names(formals(func)), as.character(substitute(func)))
  for (i in 1:(2^numArguments)) {
    arguments <- rev(as.logical(intToBits(i-1)))[-(1:(32-numArguments))]
    result[i, ] <- c(arguments, do.call(func, as.list(arguments)))
  }
  return(result)
}

Example:

majority <- function(a, b, c) {
  return ((a & b) | (b & c) | (a & c))
}

truthTable(majority)

Result:

         a     b     c majority
[1,] FALSE FALSE FALSE    FALSE
[2,] FALSE FALSE  TRUE    FALSE
[3,] FALSE  TRUE FALSE    FALSE
[4,] FALSE  TRUE  TRUE     TRUE
[5,]  TRUE FALSE FALSE    FALSE
[6,]  TRUE FALSE  TRUE     TRUE
[7,]  TRUE  TRUE FALSE     TRUE
[8,]  TRUE  TRUE  TRUE     TRUE

Possible improvements:

  • Faster conversion of numeric iterator to logical vector
  • Usage of apply instead of iterative computation

What could be improved?

Any advice would be appreciated.

added 8 characters in body
Source Link

Summary: This function generates a truth table for a boolean function of variable number of arguments. The name of the function passed and its arguments are deduced outside of the computable function scope. The returned value is a logical matrix with columns corresponding to function arguments and result.

library(R.utils)
library(stringr)

truthTable <- function(func, valuesOnly = F) {
  numArguments <- length(formals(func))
  if(valuesOnly) {
    values <- vector(length = 2^numArguments)
    for (i in 1:(2^numArguments)) {
      arguments <- rev(as.numeric(intToBits(i-1)))[-(1:(32-numArguments))]
      values[i] <- do.call(func, as.list(arguments))
    }
    return(values)
  }
  result <- matrix(nrow = 2^numArguments, ncol = numArguments + 1)
  colnames(result) <- c(names(formals(func)), as.character(substitute(func)))
  for (i in 1:(2^numArguments)) {
    arguments <- rev(as.logical(intToBits(i-1)))[-(1:(32-numArguments))]
    result[i, ] <- c(arguments, do.call(func, as.list(arguments)))
  }
  return(result)
}

Example:

majority <- function(a, b, c) {
  return ((a & b) | (b & c) | (a & c))
}

truthTable(majority)

Result:

         a     b     c majority
[1,] FALSE FALSE FALSE    FALSE
[2,] FALSE FALSE  TRUE    FALSE
[3,] FALSE  TRUE FALSE    FALSE
[4,] FALSE  TRUE  TRUE     TRUE
[5,]  TRUE FALSE FALSE    FALSE
[6,]  TRUE FALSE  TRUE     TRUE
[7,]  TRUE  TRUE FALSE     TRUE
[8,]  TRUE  TRUE  TRUE     TRUE

Possible improvements:

  • Faster conversion of numeric iterator to logical vector
  • Usage of apply instead of iterative computation

What could be improved?

Any advice would be appreciated.

Summary: This function generates a truth table for a boolean function of variable number of arguments. The name of the function passed and its arguments are deduced outside of the computable function scope. The returned value is a logical matrix with columns corresponding to function arguments and result.

library(R.utils)
library(stringr)

truthTable <- function(func, valuesOnly = F) {
  numArguments <- length(formals(func))
  if(valuesOnly) {
    values <- vector(length = 2^numArguments)
    for (i in 1:(2^numArguments)) {
      arguments <- rev(as.numeric(intToBits(i-1)))[-(1:(32-numArguments))]
      values[i] <- do.call(func, as.list(arguments))
    }
    return(values)
  }
  result <- matrix(nrow = 2^numArguments, ncol = numArguments + 1)
  colnames(result) <- c(names(formals(func)), as.character(substitute(func)))
  for (i in 1:(2^numArguments)) {
    arguments <- rev(as.logical(intToBits(i-1)))[-(1:(32-numArguments))]
    result[i, ] <- c(arguments, do.call(func, as.list(arguments)))
  }
  return(result)
}

Example:

majority <- function(a, b, c) {
  return ((a & b) | (b & c) | (a & c))
}

truthTable(majority)

Result:

         a     b     c majority
[1,] FALSE FALSE FALSE    FALSE
[2,] FALSE FALSE  TRUE    FALSE
[3,] FALSE  TRUE FALSE    FALSE
[4,] FALSE  TRUE  TRUE     TRUE
[5,]  TRUE FALSE FALSE    FALSE
[6,]  TRUE FALSE  TRUE     TRUE
[7,]  TRUE  TRUE FALSE     TRUE
[8,]  TRUE  TRUE  TRUE     TRUE

Possible improvements:

  • Faster conversion of iterator to logical vector
  • Usage of apply instead of iterative computation

What could be improved?

Any advice would be appreciated.

Summary: This function generates a truth table for a boolean function of variable number of arguments. The name of the function passed and its arguments are deduced outside of the computable function scope. The returned value is a logical matrix with columns corresponding to function arguments and result.

library(R.utils)
library(stringr)

truthTable <- function(func, valuesOnly = F) {
  numArguments <- length(formals(func))
  if(valuesOnly) {
    values <- vector(length = 2^numArguments)
    for (i in 1:(2^numArguments)) {
      arguments <- rev(as.numeric(intToBits(i-1)))[-(1:(32-numArguments))]
      values[i] <- do.call(func, as.list(arguments))
    }
    return(values)
  }
  result <- matrix(nrow = 2^numArguments, ncol = numArguments + 1)
  colnames(result) <- c(names(formals(func)), as.character(substitute(func)))
  for (i in 1:(2^numArguments)) {
    arguments <- rev(as.logical(intToBits(i-1)))[-(1:(32-numArguments))]
    result[i, ] <- c(arguments, do.call(func, as.list(arguments)))
  }
  return(result)
}

Example:

majority <- function(a, b, c) {
  return ((a & b) | (b & c) | (a & c))
}

truthTable(majority)

Result:

         a     b     c majority
[1,] FALSE FALSE FALSE    FALSE
[2,] FALSE FALSE  TRUE    FALSE
[3,] FALSE  TRUE FALSE    FALSE
[4,] FALSE  TRUE  TRUE     TRUE
[5,]  TRUE FALSE FALSE    FALSE
[6,]  TRUE FALSE  TRUE     TRUE
[7,]  TRUE  TRUE FALSE     TRUE
[8,]  TRUE  TRUE  TRUE     TRUE

Possible improvements:

  • Faster conversion of numeric iterator to logical vector
  • Usage of apply instead of iterative computation

What could be improved?

Any advice would be appreciated.

Source Link

Truth table generator for an arbitrary function

Summary: This function generates a truth table for a boolean function of variable number of arguments. The name of the function passed and its arguments are deduced outside of the computable function scope. The returned value is a logical matrix with columns corresponding to function arguments and result.

library(R.utils)
library(stringr)

truthTable <- function(func, valuesOnly = F) {
  numArguments <- length(formals(func))
  if(valuesOnly) {
    values <- vector(length = 2^numArguments)
    for (i in 1:(2^numArguments)) {
      arguments <- rev(as.numeric(intToBits(i-1)))[-(1:(32-numArguments))]
      values[i] <- do.call(func, as.list(arguments))
    }
    return(values)
  }
  result <- matrix(nrow = 2^numArguments, ncol = numArguments + 1)
  colnames(result) <- c(names(formals(func)), as.character(substitute(func)))
  for (i in 1:(2^numArguments)) {
    arguments <- rev(as.logical(intToBits(i-1)))[-(1:(32-numArguments))]
    result[i, ] <- c(arguments, do.call(func, as.list(arguments)))
  }
  return(result)
}

Example:

majority <- function(a, b, c) {
  return ((a & b) | (b & c) | (a & c))
}

truthTable(majority)

Result:

         a     b     c majority
[1,] FALSE FALSE FALSE    FALSE
[2,] FALSE FALSE  TRUE    FALSE
[3,] FALSE  TRUE FALSE    FALSE
[4,] FALSE  TRUE  TRUE     TRUE
[5,]  TRUE FALSE FALSE    FALSE
[6,]  TRUE FALSE  TRUE     TRUE
[7,]  TRUE  TRUE FALSE     TRUE
[8,]  TRUE  TRUE  TRUE     TRUE

Possible improvements:

  • Faster conversion of iterator to logical vector
  • Usage of apply instead of iterative computation

What could be improved?

Any advice would be appreciated.