]> Creatis software - CreaPhase.git/blob - octave_packages/quaternion-2.0.0/@quaternion/subsasgn.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / quaternion-2.0.0 / @quaternion / subsasgn.m
1 ## Copyright (C) 2011   Lukas F. Reichlin
2 ##
3 ## This program is free software: you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation, either version 3 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## Subscripted assignment for quaternions.
18 ## Used by Octave for "q.key = value".
19
20 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
21 ## Created: November 2011
22 ## Version: 0.2
23
24 function q = subsasgn (q, idx, val)
25
26   switch (idx(1).type)
27     case "()"                                                    # q(...) = val
28       if (length (idx(1).subs) == 1 && isa (val, "quaternion"))  # required by horzcat, vertcat, cat, ...
29         q(idx(1).subs{:}) = val;                                 # q = cellfun (@quaternion, varargin)
30       else                                                       # general case
31         val = quaternion (val);
32         w = subsasgn (q.w, idx, val.w);
33         x = subsasgn (q.x, idx, val.x);
34         y = subsasgn (q.y, idx, val.y);
35         z = subsasgn (q.z, idx, val.z);
36         q = quaternion (w, x, y, z);
37       endif
38
39     case "."                                                     # q.w = val
40       if (! is_real_array (val))
41         error ("quaternion: subsasgn: invalid argument type, require real array");
42       endif
43       if (! size_equal (subsref (q.w, idx(2:end)), val))
44         error ("quaternion: subsasgn: invalid argument size [%s], require dimensions [%s]", \
45                num2str (size (val), "%d "), num2str (size (subsref (q.w, idx(2:end))), "%d "));
46       endif
47       switch (tolower (idx(1).subs))
48         case {"w", "s"}
49           q.w = subsasgn (q.w, idx(2:end), val);
50         case {"x", "i"}
51           q.x = subsasgn (q.x, idx(2:end), val);
52         case {"y", "j"}
53           q.y = subsasgn (q.y, idx(2:end), val);
54         case {"z", "k"}
55           q.z = subsasgn (q.z, idx(2:end), val);
56         otherwise
57           error ("quaternion: subsasgn: invalid subscript name '%s'", idx(1).subs);
58       endswitch
59
60     otherwise
61       error ("quaternion: subsasgn: invalid subscript type '%s'", idx(1).type);
62   endswitch
63
64 endfunction