Functions that take DataArrays and Arrays as arguments in Julia

北城以北 提交于 2019-12-11 04:13:19

问题


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.

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.

Thanks!


回答1:


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


来源:https://stackoverflow.com/questions/24383449/functions-that-take-dataarrays-and-arrays-as-arguments-in-julia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!