This package offers an interface to create matrices from a Julia string. It uses wildcards to represent the sparsity pattern of a matrix. For instance, we represent a sparse matrix of dimension 3 x 3 x 5 where all non-zero elements lie in the row
(3,2) by
matrix_s = """
: 2 : 1 : * 1
"""
where the first colon represents the start of a specification, and the three subsequent characters (2,1,*) specify that the value 1 should be stored to all elements of the row (3,2). Our default standard is to adopt a zero-based indexing of the matrix entries, but this can be changed according to the user preference.
To use this package, please run the command
] add WildcardArrays
WildcardArrays.jl exports a julia type called WildcardArray that can parse string representations.
WildcardArray(s::String, values::Vector{T}; default=0.0, startindex=0) where T <: Union{Any, Int, Vector{String}}
The (optional) default parameter is the value to be returned when querying a WildcardArray at an index that is not explicitly specified in its string definition, e.g., this would lead to zero being returned when querying the matrix in the above example at the index 1,1,1. Startindex can be used to change the indexing of a WildcardArray.
The Julia code below can be used to create a WildcardArray from matrix_s
using WildcardArrays
matrix_s = """
: 2 : 1 : * 1
"""
dims = [3,3,5]; wa = WildcardArray(matrix_s, dims)
The current version supports instantiation of
: n_1 : n_2 : ... : n_l a
This syntax allows us to assign the value 'a' to each element specified by the string
: n_1 : n_2 : ... : n_{l-1} a_1 a_2 ... a_{N_l}
This syntax allows us to assign the value 'a_j' to the entry
: n_1 : n_2 : ... : n_{l-1} uniform
: n_1 : n_2 : ... : n_{l-2}
a11 a12 ... a1n_l
a21 a22 ... a2n_l
: : : :
: : : :
an_{l-1}1 an_{l-1}2 ... an_{l-1}n_l
This syntax allows us to assign the value 'a_{jk}' to the entry
: n_1 : n_2 : ... : n_{l-2} uniform
: n_1 : n_2 : ... : n_{l-2} identity
For a fully-fledged example, please refer to the Julia code
# An example of a 4 x 3 x 3 WildcardArray (notice that we are using a zero-based indexing)
matrix_s = """
: 2 : 1 : 2 0.5
: 3 : 2 : * 9.7
: 0 : 2 0.1 0.4 0.5
: 1 : 0 uniform
: 2
0.1 0.9 0
0.3 0.3 0.4
0.7 0.1 0.2
: 3
uniform
: 2
identity
: * : *
0.3 0.4 0.3
: * : *
0.1 0.2 0.7
"""
dims = [4,3,3]
wa = WildcardArray(matrix_s, dims)
The inspiration for this package arose due to our intention to parse the .POMDP file format described in POMDP.org. Recognizing that this data structure could be of wider interest, we decide to wrap its functionality into this package and share it with the Julia community!
In the near future, we will use the above functionalities to bring the examples from POMDP.org into the JuliaPOMDP ecosystem by releasing a new version of the POMDPFiles package. Here is a teaser on how this package can be used to parse reward transitions using the syntax described in POMDP.org.
using WildcardArrays
str = """
R: * : *
1 2
1 2.
3 8.
1 90000
2. 2
1 10
"""
states = ["warm", "cold", "not-to-cold", "very-very-cold", "almost-cold", "ow-this-is-very-cold"]
actions = ["north", "south", "east", "west"]
observations = ["warning", "more-warning"]
vv = [actions, states, states, observations]
wa = WildcardArray(str, vv)
wa[1,1,1,1] # This should be equal to one
wa[4,2,4,2] # This should be equal to 90000