home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + _vector.c
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
-
- /* Vektoren und Matrizen */
-
- #include <LEDA/vector.h>
- #include <math.h>
-
- vector_rep::vector_rep(int d)
- {
- if (d<0) error_handler(1,"vector: negative dimension.");
-
- dim = d;
-
- if (d>0)
- { v = new double[dim];
- while (d--) v[d] = 0.0;
- }
- else v = nil;
-
- }
-
- vector_rep::vector_rep(vector_rep* p)
- {
- int n = p->dim;
- dim = n;
-
- if (n>0)
- { v = new double[n];
- while (n--) v[n] = p->v[n];
- }
- else v = nil;
- }
-
-
- void vector::check_dimensions(const vector& vec) const
- { if (dim()!=vec.dim() )
- error_handler(1,"vector arguments have different dimensions.");
- }
-
-
-
- vector::vector(double x, double y)
- { ptr = new vector_rep(2);
- ptr->v[0] = x;
- ptr->v[1] = y;
- }
-
- vector::vector(double x, double y, double z)
- { ptr = new vector_rep(3);
- ptr->v[0] = x;
- ptr->v[1] = y;
- ptr->v[2] = z;
- }
-
- double vector::operator[](int i) const
- { if (i<0 || i>=dim()) error_handler(1,"vector: index out of range ");
- return ptr->v[i];
- }
-
- double& vector::operator[](int i)
- { if (i<0 || i>=dim()) error_handler(1,"vector: index out of range ");
- return ptr->v[i];
- }
-
- vector vector::operator+(const vector& vec) const
- { check_dimensions(vec);
- register int n = dim();
- vector result(n);
- while (n--) result.ptr->v[n] = ptr->v[n]+vec.ptr->v[n];
- return result;
- }
-
- vector vector::operator-(const vector& vec) const
- { check_dimensions(vec);
- register int n = dim();
- vector result(n);
- while (n--) result.ptr->v[n] = ptr->v[n]-vec.ptr->v[n];
- return result;
- }
-
-
- vector vector::operator*(double d) const
- { int n = dim();
- vector result(n);
- while (n--) result.ptr->v[n] = ptr->v[n] * d;
- return result;
- }
-
- vector vector::operator/(double d) const
- { int n = dim();
- vector result(n);
- while (n--) result.ptr->v[n] = ptr->v[n] / d;
- return result;
- }
-
- //friend
- vector operator*(double f, const vector& v) { return v*f; }
-
- double vector::operator*(const vector& vec) const
- { check_dimensions(vec);
- double result=0;
- register int n = dim();
- while (n--) result = result+ptr->v[n]*vec.ptr->v[n];
- return result;
- }
-
- vector::vector(const vector& vec)
- { register int n = vec.dim();
- ptr = new vector_rep(n);
- while (n--) ptr->v[n] = vec.ptr->v[n];
- }
-
- vector& vector::operator=(const vector& vec)
- { register int d = vec.dim();
- if (d!=dim())
- { clear();
- ptr = new vector_rep(d);
- }
- while (d--) ptr->v[d] = vec.ptr->v[d];
- return *this;
- }
-
-
- int vector::operator==(const vector& vec) const
- { if (vec.dim() != dim()) return false;
- int i = 0;
- int d = dim();
- while ((i<d) && (ptr->v[i]==vec.ptr->v[i])) i++;
- return (i==d) ? true : false;
- }
-
-
- ostream& operator<<(ostream& s, const vector& v)
- { int i;
- char buf[16];
- for (i=0;i<v.dim();i++)
- { sprintf(buf,"%7.2f ",v[i]);
- s << buf;
- }
- return s;
- }
-
- istream& operator>>(istream& s, vector& v)
- { int i=0;
- while (i<v.dim() && s >> v.ptr->v[i++]);
- return s;
- }
-
- double vector::length() const { return sqrt(*this * *this); }
-
- double vector::angle(const vector& y) const
- { double l = length();
- double yl = y.length();
-
- if ( l==0 || yl==0)
- error_handler(1,"angle: zero argument\n");
-
- return acos((*this)*y/(l*yl));
- }
-