]> Creatis software - CreaPhase.git/blob - octave_packages/fixed-0.7.10/concat.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / fixed-0.7.10 / concat.m
1 ## Copyright (C) 2003  Motorola Inc
2 ## Copyright (C) 2003  David Bateman
3 ##
4 ## This program is free software; you can redistribute it and/or modify
5 ## it under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2 of the License, or
7 ## (at your option) any later version.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ## GNU General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {@var{x} =} concat (@var{a}, @var{b})
19 ## @deftypefnx {Function File} {@var{x} =} concat (@var{a}, @var{b}, @var{dim})
20 ## Concatenate two matrices regardless of their type. Due to the implementation
21 ## of the matrix concatenation in Octave being hard-coded for the types it
22 ## knowns, user types can not use the matrix concatenation operator. Thus
23 ## for the @emph{Galois} and @emph{Fixed Point} types, the in-built matrix
24 ## concatenation functions will return a matrix value as their solution.
25 ##
26 ## This function allows these types to be concatenated. If called with a
27 ## user type that is not known by this function, the in-built concatenate
28 ## function is used.
29 ##
30 ## If @var{dim} is 1, then the matrices are concatenated, else if @var{dim}
31 ## is 2, they are stacked.
32 ## @end deftypefn
33
34 function y = concat ( a, b, dim)
35
36   if (nargin < 2) || (nargin > 3)
37     usage("concat(a, b, [dim])");
38   elseif (nargin == 2)
39     dim = 1;
40   endif
41
42   done = 0;
43   ## Protect this with try for the case is the Galois package isn't installed
44   try
45     if (isgalois(a) || isgalois(b))
46       if (isgalois(a))
47         m = a.m;
48         p = a.prim_poly;
49         ax = a.x;
50         if isgalois(b)
51           if ((b.m != m) || (b.prim_poly != p))
52             error("concat: incompatiable galois variables");
53           else
54             bx = b.x;
55           endif
56         else
57           bx = b;
58         endif
59       else
60         ax = a;
61         m = b.m;
62         p = b.prim_poly;
63         bx = b.x;
64       endif
65       if (dim == 1)
66         y = gf([ax, bx], m, p);
67       else
68         y = gf([ax; bx], m, p);
69       endif
70       done = 1;
71     endif
72   catch
73   end
74
75   ## Protect this with try for the case is the fixed package isn't installed
76   if (! done)
77     try
78       if (isfixed(a) || isfixed(b))
79         if (!isfixed(a))
80           a = fixed(a);
81         endif
82         if (!isfixed(b))
83           b = fixed(b);
84         endif
85         if (dim == 1)
86           y = fixed([a.int, b.int], [a.dec, b.dec], [a.x, b.x]);
87         else
88           y = fixed([a.int; b.int], [a.dec; b.dec], [a.x; b.x]);
89         endif
90         done = 1;
91       endif
92     catch
93     end
94   endif
95
96   if (!done)
97     fprintf("What are we doing here!!\n");
98     if (dim == 1)
99       y = [a, b];
100     else
101       y = [a; b];
102     endif
103   endif
104
105 endfunction