<commit_before>/* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
 * ------------------------------------------------------------------*/

package com.kotlinnlp.simplednn.core.arrays

import com.kotlinnlp.simplednn.simplemath.ndarray.dense.DenseNDArray
import com.kotlinnlp.simplednn.simplemath.ndarray.NDArray

/**
 * The [AugmentedArray] extends the [ActivableArray] with the errors and relevance.
 *
 * @property size the length of the array
 */
open class AugmentedArray<NDArrayType : NDArray<NDArrayType>>(size: Int) : ActivableArray<NDArrayType>(size) {

  /**
   *
   */
  companion object {

    /**
     * [AugmentedArray] factory by values.
     *
     * @param values the initial values to assign to the [AugmentedArray]
     *
     * @return an [AugmentedArray] with the values already initialized
     */
    operator fun <T : NDArray<T>> invoke(values: T): AugmentedArray<T> {

      val array = AugmentedArray<T>(size = values.length)

      array.assignValues(values)

      return array
    }
  }

  /**
   * Contains the errors of the current values
   */
  val errors: DenseNDArray get() = this._errors

  /**
   * Contains the errors of the current values
   */
  lateinit protected var _errors: DenseNDArray

  /**
   * Contains the relevance of the current values
   */
  val relevance: NDArray<*> get() = this._relevance

  /**
   * Contains the relevance of the current values
   */
  lateinit protected var _relevance: NDArray<*>

  /**
   * Contains the relevance of the current values when involved in recurrent calculations
   */
  val recurrentRelevance: NDArray<*> get() = this._recurrentRelevance

  /**
   * Contains the relevance of the current values when involved in recurrent calculations
   */
  lateinit protected var _recurrentRelevance: NDArray<*>

  /**
   * Assign errors to the array.
   *
   * @param errors the [DenseNDArray] to assign as errors to this [AugmentedArray]. It must have the same size
   *               of the values of this [AugmentedArray].
   */
  fun assignErrors(errors: DenseNDArray) {
    require(errors.length == this.size) { "Errors must have the same size of the values" }

    try {
      this._errors.assignValues(errors)

    } catch (e: UninitializedPropertyAccessException) {
      require(errors.length == this.size)

      this._errors = errors.copy()
    }
  }

  /**
   * Assign the relevance to the array.
   *
   * @param relevance the [NDArray] to assign to this [AugmentedArray] as relevance.
   *                  It must have the same size of the values of this [AugmentedArray].
   */
  fun assignRelevance(relevance: NDArray<*>) {
    require(relevance.length == this.size) { "Relevance must have the same size of the values" }

    try {
      this._relevance.assignValues(relevance)

    } catch (e: UninitializedPropertyAccessException) {
      require(relevance.length == this.size)

      this._relevance = relevance.copy()
    }
  }
<commit_msg>Fix assignment between row and column vectors<commit_after>/* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
 * ------------------------------------------------------------------*/

package com.kotlinnlp.simplednn.core.arrays

import com.kotlinnlp.simplednn.simplemath.ndarray.dense.DenseNDArray
import com.kotlinnlp.simplednn.simplemath.ndarray.NDArray

/**
 * The [AugmentedArray] extends the [ActivableArray] with the errors and relevance.
 *
 * @property size the length of the array
 */
open class AugmentedArray<NDArrayType : NDArray<NDArrayType>>(size: Int) : ActivableArray<NDArrayType>(size) {

  /**
   *
   */
  companion object {

    /**
     * [AugmentedArray] factory by values.
     *
     * @param values the initial values to assign to the [AugmentedArray]
     *
     * @return an [AugmentedArray] with the values already initialized
     */
    operator fun <T : NDArray<T>> invoke(values: T): AugmentedArray<T> {

      val array = AugmentedArray<T>(size = values.length)

      array.assignValues(values)

      return array
    }
  }

  /**
   * Contains the errors of the current values
   */
  val errors: DenseNDArray get() = this._errors

  /**
   * Contains the errors of the current values
   */
  lateinit protected var _errors: DenseNDArray

  /**
   * Contains the relevance of the current values
   */
  val relevance: NDArray<*> get() = this._relevance

  /**
   * Contains the relevance of the current values
   */
  lateinit protected var _relevance: NDArray<*>

  /**
   * Contains the relevance of the current values when involved in recurrent calculations
   */
  val recurrentRelevance: NDArray<*> get() = this._recurrentRelevance

  /**
   * Contains the relevance of the current values when involved in recurrent calculations
   */
  lateinit protected var _recurrentRelevance: NDArray<*>

  /**
   * Assign errors to the array.
   *
   * @param errors the [DenseNDArray] to assign as errors to this [AugmentedArray]. It must have the same size
   *               of the values of this [AugmentedArray].
   */
  fun assignErrors(errors: DenseNDArray) {
    require(errors.length == this.size) { "Errors must have the same size of the values" }

    try {
      this._errors.assignValues(errors)

    } catch (e: UninitializedPropertyAccessException) {
      require(errors.length == this.size)

      if (this._values.columns == 1 && errors.rows == 1 || this._values.rows == 1 && errors.columns == 1) {
        // Assignment between row and column vectors
        this._errors = errors.T
      } else {
        this._errors = errors.copy()
      }
    }
  }

  /**
   * Assign the relevance to the array.
   *
   * @param relevance the [NDArray] to assign to this [AugmentedArray] as relevance.
   *                  It must have the same size of the values of this [AugmentedArray].
   */
  fun assignRelevance(relevance: NDArray<*>) {
    require(relevance.length == this.size) { "Relevance must have the same size of the values" }

    try {
      this._relevance.assignValues(relevance)

    } catch (e: UninitializedPropertyAccessException) {
      require(relevance.length == this.size)

      this._relevance = relevance.copy()
    }
  }

{'fraction_non_alphanumeric': 0.11697596592637663, 'fraction_numerical': 0.0030422878004259203, 'mean_word_length': 2.757142857142857, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1089674', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1854, 'n_words': 614}