Advanced Octave A11 — Units and dimensional compatibility

by Giux GX-TXT, octave-en, scripts-en 2 min read

Advanced GNU Octave with GX-TXT · Module 02

Converting seconds to milliseconds is sensible; converting seconds to volts is not. The GX-TXT drive log allows the Octave script to ask if a conversion is compatible and to apply the available rules in the version in use.

Objectives and prerequisites

After the introductory course and the Dataset lessons, we know that a scientific variable has a declared unit. Now we use UNIT_REGISTRY_API_v1 through the Bridge and distinguish dimensional control, value conversion and modification of a Dataset. The test uses GX-TXT dev112-P011-bis and Octave 9.4.0.

Check before converting

Import conversione_unita.m, record it in the Session and run it from the Runner.

% Advanced Octave course A11: unit compatibility and conversion.
% SPDX-License-Identifier: GPL-3.0-or-later
gxtxt_api_require({'UNIT_REGISTRY_API_v1@1'});
t_s = [0; 0.5; 1.0];
ok_time = gxtxt_api_call('UNIT_REGISTRY_API_v1', 'compatible', {'s','ms'});
ok_voltage = gxtxt_api_call('UNIT_REGISTRY_API_v1', 'compatible', {'s','V'});
assert(ok_time && !ok_voltage);
t_ms = gxtxt_api_call('UNIT_REGISTRY_API_v1', 'convert', {t_s,'s','ms'});
fprintf('s to ms compatible: %d; s to V compatible: %d\n', ok_time, ok_voltage);
fprintf('Input in s and converted values in ms:\n');
disp([t_s, t_ms(:)]);
assert(isequal(size(t_s), size(t_ms)));

compatible returns a logical value for the required torque. Only after the control we convert the carrier; convert returns numerical values in the form of the input array. Here we use the s symbols, ms and V recognized by the test register.

The result shows 1 for s → ms and 0 for s → V; 0,5 s becomes 500 ms and 1 s becomes 1000 ms. The shape control checks that the output remains a column vector. The conversion takes place in memory: the script does not change the data or units of a persistent Dataset.

What does not guarantee compatibility

dimensionally compatible units do not guarantee that two variables represent the same sensor or the same instant. Before comparing data, it also checks role, temporal reference and origin. Not all conversions are reduced to multiply by one factor: some relationships may include an offset or require a formula. Rely on the register contract and document the transformation actually applied.

Exercise

  1. How many milliseconds correspond to 2,5 s?
  2. Why is a s → V conversion rejected?
  3. Does the convert call automatically change the dataset units from which a carrier could come from?

Worked solution

  1. 2500 ms.
  2. Time and tension have different dimensions.
  3. No: in the example only changes the value returned in memory; the persistent change would require an explicit appropriate operation.

Download the Module 02 scripts (ZIP)

#GNU Octave #GX-TXT #GX-TXT public APIs

Other steps of this project

Leave a Reply

Your email address will not be published. Required fields are marked *