File: Cast.cpp

package info (click to toggle)
freemat 4.2%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 142,116 kB
  • sloc: ansic: 126,788; cpp: 62,015; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 107
file content (215 lines) | stat: -rw-r--r-- 6,000 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (c) 2002-2006 Samit Basu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "Array.hpp"
#include "Algorithms.hpp"
#include "Operators.hpp"

//@@Signature
//function logical LogicalFunction
//inputs x
//outputs y
//DOCBLOCK typecast_logical
ArrayVector LogicalFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(Bool));
}

//@@Signature
//function string StringFunction
//inputs x
//outputs y
//DOCBLOCK typecast_string
ArrayVector StringFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1)
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(StringArray));
}

//@@Signature
//function uint8 UInt8Function
//inputs x
//outputs y
//DOCBLOCK typecast_uint8
ArrayVector UInt8Function(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(UInt8));
}

//@@Signature
//function uint16 UInt16Function
//inputs x
//outputs y
//DOCBLOCK typecast_uint16
ArrayVector UInt16Function(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(UInt16));
}

//@@Signature
//function uint32 UInt32Function
//inputs x
//outputs y
//DOCBLOCK typecast_uint32
ArrayVector UInt32Function(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(UInt32));
}

//@@Signature
//function uint64 UInt64Function
//inputs x
//outputs y
//DOCBLOCK typecast_uint64
ArrayVector UInt64Function(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(UInt64));
}

//@@Signature
//function int8 Int8Function
//inputs x
//outputs y
//DOCBLOCK typecast_int8
ArrayVector Int8Function(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(Int8));
}

//@@Signature
//function int16 Int16Function
//inputs x
//outputs y
//DOCBLOCK typecast_int16
ArrayVector Int16Function(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(Int16));
}

//@@Signature
//function int32 Int32Function
//inputs x
//outputs y
//DOCBLOCK typecast_int32
ArrayVector Int32Function(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(Int32));
}

//@@Signature
//function int64 Int64Function
//inputs x
//outputs y
//DOCBLOCK typecast_int64
ArrayVector Int64Function(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(Int64));
}

//DOCBLOCK typecast_single

//@@Signature
//function float FloatFunction
//inputs x
//outputs y
//@@Signature
//function single FloatFunction
//inputs x
//outputs y
//DOCBLOCK typecast_float
ArrayVector FloatFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(Float));
}

//@@Signature
//function double DoubleFunction
//inputs x
//outputs y
//DOCBLOCK typecast_double
ArrayVector DoubleFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(Double));
}

//@@Signature
//function complex ComplexFunction
//inputs x z
//outputs y
//DOCBLOCK typecast_complex

struct OpComplex {
  template <typename T>
  static inline T func(const T& v1, const T& v2) {
	  throw Exception("invalid type conversion");
	  return v1;
  }
  template <typename T>
  static inline void func(const T& ar, const T& ai,
			  const T& br, const T& bi,
			  T& cr, T& ci) {
    cr = ar;
    ci = br;
  }
};

ArrayVector ComplexFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() < 1) 
    throw Exception("type conversion function requires at least one argument");
  if (arg.size() == 1)
    return ArrayVector(arg[0]);
  else {
    Array x(arg[0].asComplex()); 
    Array y(arg[1].asComplex());
    return ArrayVector(DotOp<OpComplex>(x,y));
  }
}

//@@Signature
//function dcomplex DcomplexFunction
//inputs x
//outputs y
//DOCBLOCK typecast_dcomplex
ArrayVector DcomplexFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("type conversion function requires one argument");
  return ArrayVector(arg[0].toClass(Double));
}

//@@Signature
//function typeof TypeOfFunction
//inputs x
//outputs typename
//DOCBLOCK inspection_typeof
ArrayVector TypeOfFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1)
    throw Exception("typeof function requires exactly one argument");
  return ArrayVector(Array(arg[0].className()));
}