Skip to content

Data tensors in RDF

Unofficial Draft
Date: May 25, 2025

Editors:
 Piotr Marciniak
 Piotr Sowiński


Abstract

This specification defines an approach to represent data tensors (multi-dimensional arrays) as literals in RDF. It introduces two new RDF datatypes – dt:NumericDataTensor and dt:BooleanDataTensor, along with an extension of the SPARQL language. This extension includes 36 functions and 6 aggregates, enabling the efficient processing of tensor data within RDF frameworks.

See our paper for more information

Status of This Document

This document is a draft and does not represent an official standard. It is intended for discussion and gathering feedback within the community.

1. Introduction

1.1 Document Conventions

This section is non-normative.

Examples in this document assume that the following prefixes have been declared to represent the IRIs shown with them here:

Prefixes used:

Prefix Namespace
ex http://example.org/data-tensor#
dt https://w3id.org/rdf-tensor/datatypes#
dtf https://w3id.org/rdf-tensor/functions#
dta https://w3id.org/rdf-tensor/aggregates#
xsd http://www.w3.org/2001/XMLSchema#

2. The dt:NumericDataTensor Datatype

IRI

https://w3id.org/rdf-tensor/datatypes#NumericDataTensor

Definition

Represents a multi-dimensional array (tensor) of numeric values.

Lexical Space

A valid JSON object [RFC-8259] with the following structure:

Key Type Description
type string Must be one of: float16, float32, float64, int16, int32, int64. Defines the type of numbers.
shape array of integers Specifies the size of each dimension. The product of the integers must equal the length of the data array.
data array of numbers A flat array of numbers in row-major (C-style) order. Numbers must use decimal or exponential notation.

Other keys may be present in the JSON object, but they are ignored by the datatype.

Value Space

An n-dimensional numeric tensor, where n is the length of shape array.

Lexical-To-Value Mapping

The lexical representation is parsed as a JSON object. The shape key is used to determine the dimensions of the tensor, the data key contains the numeric values, the type key is used to efficiently choose the number of bytes for storing numbers and set precision. After parsing, the JSON object is converted into a tensor structure.

Example

"{\"type\": \"float32\", \"shape\": [3, 2], \"data\": [0.1, 1.2, 2.2, 3.2, 4.1, 5.4e2]}"^^dt:NumericDataTensor
"{\"type\": \"int32\", \"shape\": [1, 2, 2, 2], \"data\": [1, 3, 4, 12, 22, 32, 41, 5]}"^^dt:NumericDataTensor

3. The dt:BooleanDataTensor Datatype

IRI

https://w3id.org/rdf-tensor/datatypes#BooleanDataTensor

Definition

Represents a multi-dimensional array (tensor) of boolean values.

Lexical Space

A valid JSON object [RFC-8259] with the following structure:

Key Type Description
shape array of integers Specifies the size of each dimension. The product of the integers must equal the length of the data array.
data array of booleans A flat array of boolean values (true or false), stored in row-major (C-style) order.

Other keys may be present in the JSON object, but they are ignored by the datatype.

Value Space

An n-dimensional boolean tensor, where n is the length of shape array.

Lexical-To-Value Mapping

The lexical representation is parsed as a JSON object. The shape key is used to determine the dimensions of the tensor, and the data key contains the boolean values. After parsing, the JSON object is converted into a tensor structure.

Example

"{\"shape\": [2, 2], \"data\": [true, false, false, true]}"^^dt:BooleanDataTensor .
"{\"shape\": [2, 2, 2], \"data\": [true, false, false, true, false, false, false, true]}"^^dt:BooleanDataTensor .

4. SPARQL Functions

4.1. Transforming Functions

dtf:cos

dt:NumericDataTensor dtf:cos (dt:NumericDataTensor term_1)

The result of the function is a tensor of the same shape as the input tensor, where each element is replaced by its cosine value.

Example

Evaluating the SPARQL expression

dtf:cos("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [0, 3.1415]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1, -1]}"^^dt:NumericDataTensor

dtf:exp

dt:NumericDataTensor dtf:exp (dt:NumericDataTensor term_1)

The result of the function is a tensor of the same shape as the input tensor, where each element is replaced by its exponential value.

Example

Evaluating the SPARQL expression

dtf:exp("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [0, 1]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1, 2.7183]}"^^dt:NumericDataTensor

dtf:log

dt:NumericDataTensor dtf:log (dt:NumericDataTensor term_1)

The result of the function is a tensor of the same shape as the input tensor, where each element is replaced by its natural logarithm value.

Example

Evaluating the SPARQL expression

dtf:log("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1, 2.7183]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [0, 1]}"^^dt:NumericDataTensor

dtf:logp

dt:NumericDataTensor dtf:logp (xsd:double p, dt:NumericDataTensor term_1)

The result is a tensor of the same shape, where each element is replaced by its logarithm with base p.

Example

Evaluating the SPARQL expression

dtf:logp(10, "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1, 10]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [0, 1]}"^^dt:NumericDataTensor

dtf:poly

dt:NumericDataTensor dtf:poly (xsd:double n, dt:NumericDataTensor term_1)

The result is a tensor where each element is raised to the power n.

Example

Evaluating the SPARQL expression

dtf:poly(2, "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [2, 3]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [4, 9]}"^^dt:NumericDataTensor

dtf:scale

dt:NumericDataTensor dtf:scale (xsd:double factor, dt:NumericDataTensor term_1)

The result is a tensor of the same shape, where each element is multiplied by the given scalar factor.

Example

Evaluating the SPARQL expression

dtf:scale(3, "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [2, 3]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [6, 9]}"^^dt:NumericDataTensor

dtf:sin

dt:NumericDataTensor dtf:sin (dt:NumericDataTensor term_1)

The result of the function is a tensor of the same shape as the input tensor, where each element is replaced by its sine value.

Example

Evaluating the SPARQL expression

dtf:sin("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [0, 3.1415]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [0, 0]}"^^dt:NumericDataTensor

dtf:abs

dt:NumericDataTensor dtf:abs (dt:NumericDataTensor term_1)

The result of the function is a tensor of the same shape as the input tensor, where each element is replaced by its absolute value.

Example

Evaluating the SPARQL expression

dtf:abs("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [-1, 2]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1, 2]}"^^dt:NumericDataTensor

dtf:cast

dt:NumericDataTensor dtf:cast (dt:NumericDataTensor term_1, xsd:string type)

The result of the function is a tensor of the same shape as the input tensor, where each element is cast to the specified type. The supported types are: float16, float32, float64, int16, int32, and int64.

Example

Evaluating the SPARQL expression

dtf:cast("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1.5, 2.5]}"^^dt:NumericDataTensor, "int32")

returns

"{\"type\": \"int32\", \"shape\": [1, 2], \"data\": [1, 2]}"^^dt:NumericDataTensor

4.2 Operators

When using the binary operators, the input tensors are broadcasted to a common shape. The broadcasting rules are the same as in NumPy[NumPy 8259]. In the case of numeric tensors, the result of the mathematical operation is a tensor with the more precise type of the two input tensors. For example, if one tensor is float32 and the other is int32, the result will be float32.

dtf:not

dt:BooleanDataTensor dtf:not (dt:BooleanDataTensor term_1)

The result of the function is a tensor of the same shape as the input tensor, where each element is logically negated.

Example

Evaluating the SPARQL expression

dtf:not("{\"type\": \"bool\", \"shape\": [1, 2], \"data\": [true, false]}"^^dt:BooleanDataTensor)

returns

"{\"type\": \"bool\", \"shape\": [1, 2], \"data\": [false, true]}"^^dt:BooleanDataTensor

dtf:add

dt:NumericDataTensor dtf:add (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

The result of the function is a tensor of broadcasted shape, where each element is the sum of corresponding elements in the input tensors.

Example 1

Evaluating the SPARQL expression

dtf:add("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1, 2]}"^^dt:NumericDataTensor, "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [3, 4]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [4, 6]}"^^dt:NumericDataTensor

Example 2

Evaluating the SPARQL expression

dtf:add("{\"type\":\"float32\",\"shape\":[1, 2, 2], \"data\":[3, 2, 3, 4]}"^^dt:NumericDataTensor, "{\"type\":\"int32\",\"shape\":[1],\"data\":[1, 2]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2, 2], \"data\": [4, 4, 4, 6]}"^^dt:NumericDataTensor

dtf:subtract

dt:NumericDataTensor dtf:subtract (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

The result of the function is a tensor of broadcasted shape, where each element is the difference between corresponding elements in the input tensors.

Example 1

Evaluating the SPARQL expression

dtf:subtract("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [5, 7]}"^^dt:NumericDataTensor, "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [2, 3]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [3, 4]}"^^dt:NumericDataTensor

Example 2

Evaluating the SPARQL expression

dtf:subtract("{\"type\":\"float32\",\"shape\":[2, 2], \"data\":[3, 2, 3, 4]}"^^dt:NumericDataTensor, "{\"type\":\"int32\",\"shape\":[2],\"data\":[2, 1]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [2, 2], \"data\": [1, 1, 1, 3]}"^^dt:NumericDataTensor

dtf:multiply

dt:NumericDataTensor dtf:multiply (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

The result of the function is a tensor of broadcasted shape, where each element is the product of corresponding elements in the input tensors.

Example 1

Evaluating the SPARQL expression

dtf:multiply("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [2, 3]}"^^dt:NumericDataTensor, "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [4, 5]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [8, 15]}"^^dt:NumericDataTensor

Example 2

Evaluating the SPARQL expression

dtf:multiply("{\"type\":\"int32\",\"shape\":[2, 2], \"data\":[3, 2, 3, 4]}"^^dt:NumericDataTensor, "{\"type\":\"int32\",\"shape\":[2],\"data\":[2, 1]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [2, 2], \"data\": [6, 2, 6, 4]}"^^dt:NumericDataTensor

dtf:divide

dt:NumericDataTensor dtf:divide (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

The result of the function is a tensor of broadcasted shape, where each element is the quotient of corresponding elements in the input tensors.

Example 1

Evaluating the SPARQL expression

dtf:divide("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [8, 9]}", "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [2, 3]}")

returns

"{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [4, 3]}"^^dt:NumericDataTensor

Example 2

Evaluating the SPARQL expression

dtf:divide("{\"type\":\"int32\",\"shape\":[2, 2], \"data\":[3, 2, 3, 4]}"^^dt:NumericDataTensor, "{\"type\":\"int32\",\"shape\":[2],\"data\":[2, 1]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"int32\", \"shape\": [2, 2], \"data\": [1, 2, 1, 2]}"^^dt:NumericDataTensor

dtf:eq

dt:BooleanDataTensor dtf:eq (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

dt:BooleanDataTensor dtf:eq (dt:BooleanDataTensor term_1, dt:BooleanDataTensor term_2)

The function returns a boolean tensor with a broadcasted shape, where each element is true if the corresponding elements in the two tensors are equal, and false otherwise.

Example 1

Evaluating the SPARQL expression

dtf:eq("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1, 2]}", "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1, 3]}")

returns

"{\"shape\": [1, 2], \"data\": [true, false]}"^^dt:BooleanDataTensor

Example 2

Evaluating the SPARQL expression

dtf:eq("\"shape\": [1, 2], \"data\": [true, false]}", "{\"shape\": [1], \"data\": [true]}")

returns

"{\"shape\": [1, 2], \"data\": [true, false]}"^^dt:BooleanDataTensor

dtf:neq

dt:BooleanDataTensor dtf:neq (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

dt:BooleanDataTensor dtf:neq (dt:BooleanDataTensor term_1, dt:BooleanDataTensor term_2)

The function returns a boolean tensor with a broadcasted shape, where each element is true if the corresponding elements in the two tensors are not equal, and false otherwise.

Example 1

Evaluating the SPARQL expression

dtf:neq("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1, 2]}", "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [1, 3]}")

returns

"{\"shape\": [1, 2], \"data\": [false, true]}"^^dt:BooleanDataTensor

Example 2

Evaluating the SPARQL expression

dtf:neq("\"shape\": [1, 2], \"data\": [true, false]}", "{\"shape\": [1], \"data\": [true]}")

returns

"{\"shape\": [1, 2], \"data\": [false, true]}"^^dt:BooleanDataTensor

dtf:and

dt:BooleanDataTensor dtf:and (dt:BooleanDataTensor term_1, dt:BooleanDataTensor term_2)

The function returns a boolean tensor with a broadcasted shape, where each element is the logical AND of the input tensors.

Example

Evaluating the SPARQL expression

dtf:and("{\"shape\": [1, 2], \"data\": [true, false]}", "{\"shape\": [1, 2], \"data\": [true, true]}")

returns

"{\"shape\": [1, 2], \"data\": [true, false]}"^^dt:BooleanDataTensor

dtf:or

dt:BooleanDataTensor dtf:or (dt:BooleanDataTensor term_1, dt:BooleanDataTensor term_2)

The function returns a boolean tensor with a broadcasted shape, where each element is the logical OR of the input tensors.

Example

Evaluating the SPARQL expression

dtf:or("{\"shape\": [1, 2], \"data\": [true, false]}", "{\"shape\": [1, 2], \"data\": [false, true]}")

returns

"{\"shape\": [1, 2], \"data\": [true, true]}"^^dt:BooleanDataTensor

dtf:gt

dt:BooleanDataTensor dtf:gt (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

The function returns a boolean tensor with a broadcasted shape, where each element is true if the corresponding element from term_1 is greater than the corresponding element from term_2, and false otherwise.

Example

Evaluating the SPARQL expression

dtf:gt("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [4, 2]}", "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [3, 3]}")

returns

"{\"shape\": [1, 2], \"data\": [true, false]}"^^dt:BooleanDataTensor

dtf:lt

dt:BooleanDataTensor dtf:lt (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

The function returns a boolean tensor with a broadcasted shape, where each element is true if the corresponding element from term_1 is lesser than the corresponding element from term_2, and false otherwise.

Example

Evaluating the SPARQL expression

dtf:lt("{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [4, 2]}", "{\"type\": \"float32\", \"shape\": [1, 2], \"data\": [3, 3]}")

returns

"{\"shape\": [1, 2], \"data\": [false, true]}"^^dt:BooleanDataTensor

4.3. Indexing Functions

dtf:getSubDT

dt:NumericDataTensor dtf:getSubDT (dt:NumericDataTensor tensor, dt:NumericDataTensor indexTensor)

dt:NumericDataTensor dtf:getSubDT (dt:NumericDataTensor tensor, dt:BooleanDataTensor indexTensor)

dt:BooleanDataTensor dtf:getSubDT (dt:BooleanDataTensor tensor, dt:NumericDataTensor indexTensor)

dt:BooleanDataTensor dtf:getSubDT (dt:BooleanDataTensor tensor, dt:BooleanDataTensor indexTensor)

The result of the function is a sub-tensor extracted from the input numerical tensor using the boolean or numerical index tensor. The selection depends on the structure and values of the index tensor.

  • When the tensor is 1-dimensional, the index tensor is a 1-dimensional tensor of indices, and the result is a 1-dimensional tensor containing the elements at those indices.

Example

Evaluating the SPARQL expression

dtf:getSubDT("{\"type\":\"int32\",\"shape\":[8],\"data\":[3, 2, 3, 4, 3, 2, 3, 4]}"^^dt:NumericDataTensor, "{\"type\":\"int32\",\"shape\":[2],\"data\":[0, 1]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"int32\", \"shape\": [1, 2], \"data\": [1, 3]}"^^dt:NumericDataTensor
  • When the tensor is multi-dimensional, and the number of rows in the index tensor is equal to the number of dimensions in the tensor, the index tensor is a 2-dimensional tensor where each row contains indices for each dimension. The result is a 1-dimensional tensor containing the elements at those indices.

Example

Evaluating the SPARQL expression

dtf:getSubDT("{\"type\":\"int32\",\"shape\":[2, 2],\"data\":[3, 2, 3, 4]}"^^dt:NumericDataTensor, "{\"type\":\"int32\",\"shape\":[2, 2],\"data\":[0, 1, 1, 0]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"int32\", \"shape\": [2], \"data\": [3, 4]}"^^dt:NumericDataTensor
  • When the tensor is multi-dimensional, and the number of rows in the index tensor is not equal to the number of dimensions in the tensor, than slice indexing is performed (depends on the index tensor dimensionality).

  • The index tensor is 1-dimensional (it has to be row vector), than slicing is performed along the first dimension, and the result is a tensor with the same number of dimensions as the input tensor, but with the first dimension reduced to the length of the index tensor.

Example

Evaluating the SPARQL expression

dtf:getSubDT("{\"type\":\"int32\",\"shape\":[2, 2, 2],\"data\":[1, 2, 3, 4, 5, 6, 7, 8]}"^^dt:NumericDataTensor, "{\"type\":\"int32\",\"shape\":[1, 1],\"data\":[1]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"int32\", \"shape\": [2, 2], \"data\": [5, 6, 7, 8]}"^^dt:NumericDataTensor
  • The index tensor is 2-dimensional, than slicing is performed firstly the first dimension, and then the second dimension, and the result is a tensor with the same number of dimensions as the input tensor, but with the first two dimensions reduced to the lengths of the index tensor.

Example

Evaluating the SPARQL expression

dtf:getSubDT("{\"type\":\"int32\",\"shape\":[2, 2, 2],\"data\":[1, 2, 3, 4, 5, 6, 7, 8]}"^^dt:NumericDataTensor, "{\"type\":\"int32\",\"shape\":[2, 2],\"data\":[0, 0, 1, 1]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"int32\", \"shape\": [2], \"data\": [3, 4, 3, 4]}"^^dt:NumericDataTensor
  • When the index tensor is more than 3-dimensional, the function will raise an error, as it is not supported.

  • If the index tensor is a boolean tensor, it is used to select elements from the input tensor based on the true values in the index tensor. The result is a 1-dimensional tensor containing the selected elements from the flattened tensor.

Example

Evaluating the SPARQL expression

dtf:getSubDT("{\"type\":\"int32\",\"shape\":[2, 2],\"data\":[3, 2, 3, 4]}"^^dt:NumericDataTensor, "{\"type\":\"bool\",\"shape\":[2, 2],\"data\":[true, false, true, true]}"^^dt:BooleanDataTensor)

returns

"{\"type\": \"int32\", \"shape\": [3], \"data\": [3, 3, 4]}"^^dt:NumericDataTensor

4.4 Concatenating Functions

dtf:concat

dt:NumericDataTensor dtf:concat (xsd:integer axis, dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

This function returns a tensor that is the concatenation of the two input tensors along the specified axis. The other dimensions must match.

Example

Evaluating the SPARQL expression

dtf:concat(0, "{\"type\": \"float32\", \"shape\": [2, 2], \"data\": [1, 2, 3, 4]}"^^dt:NumericDataTensor, "{\"type\": \"float32\", \"shape\": [2, 2], \"data\": [5, 6, 7, 8]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [4, 2], \"data\": [1, 2, 3, 4, 5, 6, 7, 8]}"^^dt:NumericDataTensor

dtf:hstack

dt:NumericDataTensor dtf:hstack (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

This function returns a tensor that is the result of horizontally stacking the two input tensors (i.e., concatenation along the last axis). The tensors must be broadcast-compatible along other dimensions.

Example

Evaluating the SPARQL expression

dtf:hstack("{\"type\": \"float32\", \"shape\": [2, 2], \"data\": [1, 2, 3, 4]}"^^dt:NumericDataTensor, "{\"type\": \"float32\", \"shape\": [2, 2], \"data\": [5, 6, 7, 8]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [2, 4], \"data\": [1, 2, 5, 6, 3, 4, 7, 8]}"^^dt:NumericDataTensor

dtf:vstack

dt:NumericDataTensor dtf:vstack (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

This function returns a tensor that is the result of vertically stacking the two input tensors (i.e., concatenation along the first axis). The tensors must be broadcast-compatible along other dimensions.

Example

Evaluating the SPARQL expression

dtf:vstack("{\"type\": \"float32\", \"shape\": [2, 2], \"data\": [1, 2, 3, 4]}"^^dt:NumericDataTensor, "{\"type\": \"float32\", \"shape\": [2, 2], \"data\": [5, 6, 7, 8]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [4, 2], \"data\": [1, 2, 3, 4, 5, 6, 7, 8]}"^^dt:NumericDataTensor

4.5. Reduction Functions

dtf:all

xsd:boolean dtf:all (dt:BooleanDataTensor term_1)

This function checks if all elements in the boolean tensor are true. Returns a single boolean value.

Example

Evaluating the SPARQL expression

dtf:all("{\"shape\": [2], \"data\": [true, true]}"^^dt:BooleanDataTensor)  

returns

"true"^^xsd:boolean

dtf:any

dt:BooleanDataTensor dtf:any (dt:BooleanDataTensor term_1)

This function checks if any element in the boolean tensor is true. Returns a single boolean value.

Example

Evaluating the SPARQL expression

dtf:any("{\"shape\": [2], \"data\": [false, true]}"^^dt:BooleanDataTensor)

returns

"true"^^xsd:boolean

dtf:avg

dt:NumericDataTensor dtf:avg (xsd:integer axis, dt:NumericDataTensor term_1) xsd:double dtf:avg (xsd:integer axis, dt:NumericDataTensor term_1)

This function computes the average along the specified axis. If the axis is negative, the average is calculated over the entire tensor. It returns a reduced tensor or a scalar.

Example

Evaluating the SPARQL expression

dtf:avg(1, "{\"type\": \"float32\", \"shape\": [2,2], \"data\": [1, 2, 3, 4]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [2], \"data\": [1.5, 3.5]}"^^dt:NumericDataTensor

dtf:sum

dt:NumericDataTensor dtf:sum (xsd:integer axis, dt:NumericDataTensor term_1) xsd:double dtf:sum (xsd:integer axis, dt:NumericDataTensor term_1)

This function computes the sum along the specified axis. If the axis is negative, the sum is calculated over the entire tensor. It returns a reduced tensor or a scalar.

Example

Evaluating the SPARQL expression

dtf:sum(1, "{\"type\": \"float32\", \"shape\": [2,2], \"data\": [1, 2, 3, 4]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [2], \"data\": [3, 7]}"^^dt:NumericDataTensor

dtf:max

dt:NumericDataTensor dtf:max (xsd:integer axis, dt:NumericDataTensor term_1)

xsd:double dtf:max (xsd:integer axis, dt:NumericDataTensor term_1)

This function computes the maximum along the specified axis. If the axis is negative, the maximum is calculated over the entire tensor. It returns a reduced tensor or a scalar.

Example

Evaluating the SPARQL expression

dtf:max(1, "{\"type\": \"float32\", \"shape\": [2,2], \"data\": [1, 5, 2, 4]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [2], \"data\": [5, 4]}"^^dt:NumericDataTensor

dtf:median

dt:NumericDataTensor dtf:median (xsd:integer axis, dt:NumericDataTensor term_1)

xsd:double dtf:median (xsd:integer axis, dt:NumericDataTensor term_1)

This function computes the median along the specified axis. If the axis is negative, the median is calculated over the entire tensor. It returns a reduced tensor or a scalar.

Example

Evaluating the SPARQL expression

dtf:median(1, "{\"type\": \"float32\", \"shape\": [1, 3], \"data\": [7, 1, 3]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1], \"data\": [3]}"^^dt:NumericDataTensor

dtf:min

dt:NumericDataTensor dtf:min (xsd:integer axis, dt:NumericDataTensor term_1)

xsd:double dtf:min (xsd:integer axis, dt:NumericDataTensor term_1)

This function computes the minimum along the specified axis. If the axis is negative, the minimum is calculated over the entire tensor. It returns a reduced tensor or a scalar.

Example

Evaluating the SPARQL expression

dtf:min(1, "{\"type\": \"float32\", \"shape\": [1,3], \"data\": [7, 1, 3]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1], \"data\": [1]}"^^dt:NumericDataTensor

dtf:std

dt:NumericDataTensor dtf:std (xsd:integer axis, dt:NumericDataTensor term_1)

xsd:double dtf:std (xsd:integer axis, dt:NumericDataTensor term_1)

This function computes the standard deviation along the specified axis. If the axis is negative, the standard deviation is calculated over the entire tensor. It returns a reduced tensor or a scalar.

Example

Evaluating the SPARQL expression

dtf:std(1, "{\"type\": \"float32\", \"shape\": [1,3], \"data\": [1, 2, 3]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1], \"data\": [0.8165]}"^^dt:NumericDataTensor

dtf:var

dt:NumericDataTensor dtf:var (xsd:integer axis, dt:NumericDataTensor term_1)

xsd:double dtf:var (xsd:integer axis, dt:NumericDataTensor term_1)

This function computes the variance along the specified axis. If the axis is negative, the variance is calculated over the entire tensor. It returns a reduced tensor or a scalar.

Example

Evaluating the SPARQL expression

dtf:var(1, "{\"type\": \"float32\", \"shape\": [1,3], \"data\": [1, 2, 3]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [1], \"data\": [0.6667]}"^^dt:NumericDataTensor

dtf:norm1

dt:NumericDataTensor dtf:norm1 (xsd:integer axis, dt:NumericDataTensor term_1)

xsd:double dtf:norm1 (xsd:integer axis, dt:NumericDataTensor term_1)

This function computes the L1 norm (sum of absolute values) along the specified axis. If the axis is negative, the L1 norm (sum of absolute values) is calculated over the entire tensor. It returns a reduced tensor or a scalar.

Example

Evaluating the SPARQL expression

dtf:norm1(1, "{\"type\": \"float32\", \"shape\": [2,2], \"data\": [1, -1, -2, 2]}"^^dt:NumericDataTensor)  

returns

"{\"type\": \"float32\", \"shape\": [2], \"data\": [2, 4]}"^^dt:NumericDataTensor

dtf:norm2

dt:NumericDataTensor dtf:norm2 (xsd:integer axis, dt:NumericDataTensor term_1)

xsd:double dtf:norm1 (xsd:integer axis, dt:NumericDataTensor term_1)

This function computes the L2 norm (Euclidean norm) along the specified axis. If the axis is negative, the L2 norm (Euclidean norm) is calculated over the entire tensor. It returns a reduced tensor or a scalar.

Example

Evaluating the SPARQL expression

dtf:norm2(1, "{\"type\": \"float32\", \"shape\": [2,2], \"data\": [3, 4, 6, 8]}"^^dt:NumericDataTensor)

returns

"{\"type\": \"float32\", \"shape\": [2], \"data\": [5, 10]}"^^dt:NumericDataTensor

4.6. Similarity Functions

dtf:cosineSimilarity

xsd:double dtf:cosineSimilarity (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

This function computes the cosine similarity between two numerical tensors. Returns a numeric scalar value.

Example

Evaluating the SPARQL expression

dtf:cosineSimilarity( "{\"type\": \"float32\", \"shape\": [3], \"data\": [1, 0, 1]}"^^dt:NumericDataTensor, "{\"type\": \"float32\", \"shape\": [3], \"data\": [1, 1, 0]}"^^dt:NumericDataTensor)

returns

"0.5"^^xsd:float

dtf:euclideanDistance

xsd:double dtf:euclideanDistance (dt:NumericDataTensor term_1, dt:NumericDataTensor term_2)

This function computes the Euclidean distance between two numerical tensors. Returns a numeric scalar value.

Example

Evaluating the SPARQL expression

dtf:euclideanDistance("{\"type\": \"float32\", \"shape\": [2], \"data\": [3, 4]}"^^dt:NumericDataTensor, "{\"type\": \"float32\", \"shape\": [2], \"data\": [0, 0]}"^^dt:NumericDataTensor)

returns

`"5.0"^^xsd:float

5. SPARQL Aggregates

The following aggregation functions are implemented as SPARQL extension aggregates. Each function operates over NumericDataTensor values and returns a NumericDataTensor with the most precise type used within each group. These functions do not support the DISTINCT modifier.

dta:sum

  • IRI: https://w3id.org/rdf-tensor/aggregates#sum
  • Description: Sums grouped numeric tensors element-wise.
  • Input: A group of NumericDataTensor values.
  • Output: A NumericDataTensor representing the element-wise sum of all tensors in the group.

dta:avg

  • IRI: https://w3id.org/rdf-tensor/aggregates#avg
  • Description: Computes the element-wise average of grouped tensors.
  • Input: A group of NumericDataTensor values.
  • Output: A NumericDataTensor representing the average tensor.

dta:var

  • IRI: https://w3id.org/rdf-tensor/aggregates#var
  • Description: Calculates the element-wise variance across grouped tensors.
  • Input: A group of NumericDataTensor values.
  • Output: A NumericDataTensor representing the variance tensor.

dta:std

  • IRI: https://w3id.org/rdf-tensor/aggregates#std
  • Description: Computes the element-wise standard deviation across grouped tensors.
  • Input: A group of NumericDataTensor values.
  • Output: A NumericDataTensor representing the standard deviation tensor.

A. References

A.1. Informative references

[rdf11-concepts]

  RDF 1.1 Concepts and Abstract Syntax. Richard Cyganiak; David Wood; Markus Lanthaler. W3C. 25 February 2014. W3C Recommendation. URL: https://www.w3.org/TR/rdf11-concepts/

[RFC-8259]

  RFC 8259. Tim Bray. JSON Data Interchange Format. 2017. URL: https://www.rfc-editor.org/rfc/rfc8259

[NumPy]   NumPy. NumPy Developers. NumPy. 2023. URL: https://numpy.org/