本文介绍了在Julia中将DataArray和Array作为参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个可以在Julia中同时使用DataArray和标准Array的函数.实际上,我想写一个函数的方法-一种以两个类似矢量的结构作为参数,而另一个则采用两个类似于矩阵的参数.

I'd like to write a function that can take both a DataArray and a standard Array in Julia. Actually, I'd like to write to methods for a function -- one that takes two vector-like structures as parameters and one that takes two matrix-like parameters.

由于Array和DataArray基本相同,只是DataArray允许使用NA值,所以我真的不想编写每个函数的4个版本,而只是合并Array和DataArray参数的所有不同组合.现在,我使用以下六个(!)函数来实现我的目标:

Since Arrays and DataArrays are basically the same, only that a DataArray allows for NA values, I really do not want to write 4 versions of each function, just to incorporate all different combinations of Array and DataArray parameters. Right now, I use the following six (!) functions, to achieve my goal:

function covar_to_intensity{T<:Number}(covariates::Array{T, 1}, coefficients::Array{T, 1})
  try
    return(exp(covariates' * coefficients)[1])
  catch
    error("Multiplication of covariates and coefficients not possible.")
  end
end

function covar_to_intensity{T<:Number}(covariates::Array{T, 2}, coefficients::Array{T, 2})
  try
    return(exp(covariates * coefficients))
  catch
    error("Multiplication of covariates and coefficients not possible.")
  end
end

function covar_to_intensity{T<:Number}(covariates::DataArray{T, 1}, coefficients::DataArray{T, 1})
  try
    return(exp(covariates' * coefficients)[1])
  catch
    error("Multiplication of covariates and coefficients not possible.")
  end
end

function covar_to_intensity{T<:Number}(covariates::DataArray{T, 2}, coefficients::DataArray{T, 2})
  try
    return(exp(covariates * coefficients))
  catch
    error("Multiplication of covariates and coefficients not possible.")
  end
end

function covar_to_intensity{T<:Number}(covariates::Array{T, 1}, coefficients::DataArray{T, 1})
  try
    return(exp(covariates' * coefficients)[1])
  catch
    error("Multiplication of covariates and coefficients not possible.")
  end
end

function covar_to_intensity{T<:Number}(covariates::Array{T, 2}, coefficients::DataArray{T, 2})
  try
    return(exp(covariates * coefficients))
  catch
    error("Multiplication of covariates and coefficients not possible.")
  end
end

我知道这可能是计算这些乘积的一种低效方法,但是我对如何编写同时使用数组和数据数组的函数也抱有浓厚的兴趣.

I'm aware that this might be an inefficient way to compute these products, but I also have a general interest regarding how to write functions taking both Arrays and DataArrays.

谢谢!

推荐答案

通常,您可以为AbstractArray定义方法:

In general, you can define methods for AbstractArray:

function covar_to_intensity{T<:Number}(covariates::AbstractVector{T}, coefficients::AbstractVector{T})
  try
     return exp(covariates * coefficients)
  catch
    error("Multiplication of covariates and coefficients not possible.")
   end
end

function covar_to_intensity{T<:Number}(covariates::AbstractMatrix{T}, coefficients::AbstractMatrix{T})
  try
     return exp((covariates' * coefficients)[1])
  catch
    error("Multiplication of covariates and coefficients not possible.")
   end
end

这篇关于在Julia中将DataArray和Array作为参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 11:08