lib/numru/misc/misc.rb

# module NumRu::Misc

## Overview

Miscellaneous functions and classes to facilitate programming.

## Index

CLASSES

- class KeywordOpt to support keyward arguments with default values.
- class NArray (enhancement of NArray made by M Tanaka)

MODULES

- module MD_Iterators A Mixin for classes with multi-dimension indexing support (such as NArray).
- module EMath To be included instead of the Math predefined module (or NMath in NArray). Unlike Math and NMath, EMath handles unknown classes by calling its native instance method (assuming the same name).

MODULE FUNCTIONS

- check_shape_consistency

## Module functions

`check_shape_consistency(cshapes, *args)`

  Check the consistency of array shapes (multi-dim such as NArray). Exception is raised if inconsistent.

  ARGUMENTS

  - cshapes (String) : description of the shapes of the args. Delimited by one-or-more spaces between arrays, and the shape of each array is delimited by a comma. The lengths are expressed with string names as identifiers (in that case, length values are unquestioned) or specified as positive integers. Use '..' or '...' for repetition of the last shape. See EXAMPLES below.
  - args (multi-dim arrays such as NArray): arrays to be checked

  RETURN VALUE

  - nil

  POSSIBLE EXCEPTIONS

  - exception is raised if cshapes and args are inconsistent:
    - RuntimeError, if the arrays do not have shapes specified by cshapes.
    - ArgeumentError, if the number of args are inconsistent with cshapes. This is likely a coding error of the user.

  EXAMPLES

  - to check whether three arrays u, v, and w are shaped as u[nx], v[ny], and w[nx,ny], where nx and ny are any integer:

        NumRu::Misc.check_shape_consistency('nx ny nx,ny',u,v,w)

    Or equivalently,

        NumRu::Misc.check_shape_consistency('m  n  m,n',u,v,w)

    because actual strings does not matter.

  - To specify fixed lengths, use integers instead of names:

        NumRu::Misc.check_shape_consistency('4  n  4,n',u,v,w)

    In this case, u,v,w must have shapes [4], [ny], and [4,ny], where ny is any length.

  - Use '..' or '...' to repeat the same shape:

        NumRu::Misc.check_shape_consistency('nx,ny ...',u,v,w)

    This ensures that u, v, and w are 2D arrays with the same shape. Note: '..' and '...' are the same, so you can use whichever you like.