1 // Copyright (c) 1997-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
20 #define No_Standard_RangeError
21 #define No_Standard_OutOfRange
22 #define No_Standard_DimensionError
27 #include <math_Vector.ixx>
28 #include <math_Matrix.hxx>
30 #include <Standard_DimensionError.hxx>
31 #include <Standard_DivideByZero.hxx>
32 #include <Standard_RangeError.hxx>
33 #include <Standard_NullValue.hxx>
36 math_Vector::math_Vector(const Standard_Integer Lower,
37 const Standard_Integer Upper):
42 Standard_RangeError_Raise_if(Lower > Upper, "");
45 math_Vector::math_Vector(const Standard_Integer Lower,
46 const Standard_Integer Upper,
47 const Standard_Real InitialValue):
53 Standard_RangeError_Raise_if(Lower > Upper, "");
54 Array.Init(InitialValue);
57 math_Vector::math_Vector(const Standard_Address Tab,
58 const Standard_Integer Lower,
59 const Standard_Integer Upper) :
63 Array(*((const Standard_Real *)Tab), Lower,Upper)
65 Standard_RangeError_Raise_if((Lower > Upper) , "");
68 void math_Vector::Init(const Standard_Real InitialValue) {
69 Array.Init(InitialValue);
72 math_Vector::math_Vector(const math_Vector& Other):
74 LowerIndex(Other.LowerIndex),
75 UpperIndex(Other.UpperIndex),
79 void math_Vector::SetLower(const Standard_Integer Lower) {
81 Array.SetLower(Lower);
82 UpperIndex = UpperIndex - LowerIndex + Lower;
86 Standard_Real math_Vector::Norm() const {
88 Standard_Real Result = 0;
90 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
91 Result = Result + Array(Index) * Array(Index);
96 Standard_Real math_Vector::Norm2() const {
98 Standard_Real Result = 0;
100 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
101 Result = Result + Array(Index) * Array(Index);
106 Standard_Integer math_Vector::Max() const {
108 Standard_Integer I=0;
109 Standard_Real X = RealFirst();
111 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
112 if(Array(Index) > X) {
120 Standard_Integer math_Vector::Min() const {
122 Standard_Integer I=0;
123 Standard_Real X = RealLast();
125 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
126 if(Array(Index) < X) {
134 void math_Vector::Set(const Standard_Integer I1,
135 const Standard_Integer I2,
136 const math_Vector &V) {
138 Standard_RangeError_Raise_if((I1 < LowerIndex) ||
141 (I2 - I1 + 1 != V.Length()), "");
143 Standard_Integer I = V.Lower();
144 for(Standard_Integer Index = I1; Index <= I2; Index++) {
145 Array(Index) = V.Array(I);
150 void math_Vector::Normalize() {
152 Standard_Real Result = Norm();
153 Standard_NullValue_Raise_if((Result <= RealEpsilon()), "");
154 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
155 Array(Index) = Array(Index) / Result;
159 math_Vector math_Vector::Normalized() const {
161 math_Vector Result = *this;
167 void math_Vector::Invert() {
170 for(Standard_Integer Index = LowerIndex;
171 // Index <= LowerIndex + (Length()) >> 1 ; Index++) {
172 Index <= (LowerIndex + Length()) >> 1 ; Index++) {
173 J = UpperIndex + LowerIndex - Index;
175 Array(Index) = Array(J);
180 math_Vector math_Vector::Inverse() const {
181 math_Vector Result = *this;
186 math_Vector math_Vector::Multiplied(const Standard_Real Right) const{
188 math_Vector Result (LowerIndex, UpperIndex);
190 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
191 Result.Array(Index) = Array(Index) * Right;
196 math_Vector math_Vector::TMultiplied(const Standard_Real Right) const{
198 math_Vector Result (LowerIndex, UpperIndex);
200 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
201 Result.Array(Index) = Array(Index) * Right;
207 void math_Vector::Multiply(const Standard_Real Right) {
209 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
210 Array(Index) = Array(Index) * Right;
215 void math_Vector::Divide(const Standard_Real Right) {
217 Standard_DivideByZero_Raise_if(Abs(Right) <= RealEpsilon(), "");
219 for(Standard_Integer Index =LowerIndex; Index <=UpperIndex; Index++) {
220 Array(Index) = Array(Index) / Right;
225 math_Vector math_Vector::Divided (const Standard_Real Right) const {
227 Standard_DivideByZero_Raise_if(Abs(Right) <= RealEpsilon(), "");
228 math_Vector temp = Multiplied(1./Right);
233 void math_Vector::Add(const math_Vector& Right) {
235 Standard_DimensionError_Raise_if(Length() != Right.Length(), "");
237 Standard_Integer I = Right.LowerIndex;
238 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
239 Array(Index) = Array(Index) + Right.Array(I);
244 math_Vector math_Vector::Added(const math_Vector& Right) const{
246 Standard_DimensionError_Raise_if(Length() != Right.Length(), "");
248 math_Vector Result(LowerIndex, UpperIndex);
250 Standard_Integer I = Right.LowerIndex;
251 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
252 Result.Array(Index) = Array(Index) + Right.Array(I);
260 void math_Vector::Subtract(const math_Vector& Right) {
262 Standard_DimensionError_Raise_if(Length() != Right.Length(), "");
264 Standard_Integer I = Right.LowerIndex;
265 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
266 Array(Index) = Array(Index) - Right.Array(I);
272 math_Vector math_Vector::Subtracted (const math_Vector& Right) const {
274 Standard_DimensionError_Raise_if(Length() != Right.Length(), "");
276 math_Vector Result(LowerIndex, UpperIndex);
278 Standard_Integer I = Right.LowerIndex;
279 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
280 Result.Array(Index) = Array(Index) - Right.Array(I);
287 math_Vector math_Vector::Slice(const Standard_Integer I1,
288 const Standard_Integer I2) const
291 Standard_RangeError_Raise_if((I1 < LowerIndex) ||
294 (I2 > UpperIndex) , "");
298 math_Vector Result(I1, I2);
299 for(Standard_Integer Index = I1; Index <= I2; Index++) {
300 Result.Array(Index) = Array(Index);
305 math_Vector Result(I2, I1);
306 for(Standard_Integer Index = I1; Index >= I2; Index--) {
307 Result.Array(Index) = Array(Index);
314 void math_Vector::Add (const math_Vector& Left, const math_Vector& Right) {
316 Standard_DimensionError_Raise_if((Length() != Right.Length()) ||
317 (Right.Length() != Left.Length()), "");
320 Standard_Integer I = Left.LowerIndex;
321 Standard_Integer J = Right.LowerIndex;
322 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
323 Array(Index) = Left.Array(I) + Right.Array(J);
329 void math_Vector::Subtract (const math_Vector& Left,
330 const math_Vector& Right) {
332 Standard_DimensionError_Raise_if((Length() != Right.Length()) ||
333 (Right.Length() != Left.Length()), "");
335 Standard_Integer I = Left.LowerIndex;
336 Standard_Integer J = Right.LowerIndex;
337 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
338 Array(Index) = Left.Array(I) - Right.Array(J);
344 void math_Vector::Multiply(const math_Matrix& Left,
345 const math_Vector& Right) {
347 Standard_DimensionError_Raise_if((Length() != Left.RowNumber()) ||
348 (Left.ColNumber() != Right.Length()),
351 Standard_Integer Index = LowerIndex;
352 for(Standard_Integer I = Left.LowerRowIndex; I <= Left.UpperRowIndex; I++) {
354 Standard_Integer K = Right.LowerIndex;
355 for(Standard_Integer J = Left.LowerColIndex; J <= Left.UpperColIndex; J++) {
356 Array(Index) = Array(Index) + Left.Array(I, J) * Right.Array(K);
363 void math_Vector::Multiply(const math_Vector& Left,
364 const math_Matrix& Right) {
366 Standard_DimensionError_Raise_if((Length() != Right.ColNumber()) ||
367 (Left.Length() != Right.RowNumber()),
370 Standard_Integer Index = LowerIndex;
371 for(Standard_Integer J = Right.LowerColIndex; J <= Right.UpperColIndex; J++) {
373 Standard_Integer K = Left.LowerIndex;
374 for(Standard_Integer I = Right.LowerRowIndex; I <= Right.UpperRowIndex; I++) {
375 Array(Index) = Array(Index) + Left.Array(K) * Right.Array(I, J);
382 void math_Vector::TMultiply(const math_Matrix& TLeft,
383 const math_Vector& Right) {
385 Standard_DimensionError_Raise_if((Length() != TLeft.ColNumber()) ||
386 (TLeft.RowNumber() != Right.Length()),
389 Standard_Integer Index = LowerIndex;
390 for(Standard_Integer I = TLeft.LowerColIndex; I <= TLeft.UpperColIndex; I++) {
392 Standard_Integer K = Right.LowerIndex;
393 for(Standard_Integer J = TLeft.LowerRowIndex; J <= TLeft.UpperRowIndex; J++) {
394 Array(Index) = Array(Index) + TLeft.Array(J, I) * Right.Array(K);
401 void math_Vector::TMultiply(const math_Vector& Left,
402 const math_Matrix& TRight) {
404 Standard_DimensionError_Raise_if((Length() != TRight.RowNumber()) ||
405 (Left.Length() != TRight.ColNumber()),
408 Standard_Integer Index = LowerIndex;
409 for(Standard_Integer J = TRight.LowerRowIndex; J <= TRight.UpperRowIndex; J++) {
411 Standard_Integer K = Left.LowerIndex;
412 for(Standard_Integer I = TRight.LowerColIndex;
413 I <= TRight.UpperColIndex; I++) {
414 Array(Index) = Array(Index) + Left.Array(K) * TRight.Array(J, I);
424 Standard_Real math_Vector::Multiplied(const math_Vector& Right) const{
425 Standard_Real Result = 0;
427 Standard_DimensionError_Raise_if(Length() != Right.Length(), "");
429 Standard_Integer I = Right.LowerIndex;
430 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
431 Result = Result + Array(Index) * Right.Array(I);
437 math_Vector math_Vector::Opposite() {
438 math_Vector Result(LowerIndex, UpperIndex);
440 for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) {
441 Result.Array(Index) = - Array(Index);
446 math_Vector math_Vector::Multiplied(const math_Matrix& Right)const {
447 Standard_DimensionError_Raise_if(Length() != Right.RowNumber(), "");
449 math_Vector Result(Right.LowerColIndex, Right.UpperColIndex);
450 for(Standard_Integer J2 = Right.LowerColIndex;
451 J2 <= Right.UpperColIndex; J2++) {
453 Standard_Integer I2 = Right.LowerRowIndex;
454 for(Standard_Integer I = LowerIndex; I <= UpperIndex; I++) {
455 Result.Array(J2) = Result.Array(J2) + Array(I) *
464 void math_Vector::Multiply(const Standard_Real Left,
465 const math_Vector& Right)
467 Standard_DimensionError_Raise_if((Length() != Right.Length()),
469 for(Standard_Integer I = LowerIndex; I <= UpperIndex; I++) {
470 Array(I) = Left * Right.Array(I);
475 math_Vector& math_Vector::Initialized(const math_Vector& Other) {
477 Standard_DimensionError_Raise_if(Length() != Other.Length(), "");
479 (Other.Array).Copy(Array);
485 void math_Vector::Dump(Standard_OStream& o) const
487 o << "math_Vector of Length = " << Length() << "\n";
488 for(Standard_Integer Index = LowerIndex;
489 Index <= UpperIndex; Index++) {
490 o << "math_Vector(" << Index << ") = " << Array(Index) << "\n";