Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ConfigFile SDFileSystem mbed
Fork of LAURUS_program by
Vector/Vector.cpp
- Committer:
- ojan
- Date:
- 2015-05-15
- Revision:
- 0:bc6f14fc60c7
- Child:
- 3:5358a691a100
File content as of revision 0:bc6f14fc60c7:
#include "mbed.h"
#include "myConstants.h"
#include "Vector.h"
Vector::Vector(int dim) : dim(dim), components(0){
components = new float[dim];
if (!components) AbortWithMsg("Memory Allocation Error");
for(int i=0; i<dim; i++) {
components[i] = 0.0f;
}
}
Vector::~Vector() {
delete[] components;
}
Vector::Vector(const Vector& v) : dim(v.dim), components(0) {
components = new float[dim];
if (!components) AbortWithMsg("Memory Allocation Error");
memcpy(components, v.GetpComponents(), sizeof(float)*dim);
}
Vector& Vector::operator=(const Vector& v) {
if (this == &v) return *this;
dim = v.dim;
delete[] components;
components = new float[dim];
if (!components) AbortWithMsg("Memory Allocation Error");
memcpy(components, v.GetpComponents(), sizeof(float)*dim);
return *this;
}
Vector& Vector::operator*=(float c) {
for (int i = 0; i < dim; i++) {
components[i] *= c;
}
return *this;
}
Vector& Vector::operator/=(float c) {
if (fabs(c) < NEARLY_ZERO) AbortWithMsg("Division by Zero");
for (int i = 0; i < dim; i++) {
components[i] /= c;
}
return *this;
}
Vector& Vector::operator+=(const Vector& v) {
if (dim != v.dim) AbortWithMsg("failed to add: Irregular Dimention");
for (int i = 0; i < dim; i++) {
components[i] += v.components[i];
}
this->CleanUp();
return *this;
}
Vector& Vector::operator-=(const Vector& v) {
if (dim != v.dim) AbortWithMsg("failed to subtract: Irregular Dimention");
for (int i = 0; i < dim; i++) {
components[i] -= v.components[i];
}
this->CleanUp();
return *this;
}
void Vector::SetComp(int dimNo, float val) {
if (dimNo > dim) AbortWithMsg("Index Out of Bounds Error");
components[dimNo-1] = val;
}
float Vector::GetNorm() const {
float norm = 0.0f;
for (int i = 0; i < dim; i++) {
norm += components[i] * components[i];
}
return sqrt(norm);
}
Vector Vector::Normalize() const {
float norm = GetNorm();
Vector temp(*this);
for (int i = 0; i < dim; i++) {
temp.components[i] /= norm;
}
temp.CleanUp();
return temp;
}
void Vector::CleanUp() {
float maxComp = 0.0f;
for (int i = 0; i < dim; i++) {
if (fabs(components[i]) > maxComp) maxComp = fabs(components[i]);
}
if (maxComp > NEARLY_ZERO) {
for (int i = 0; i < dim; i++) {
if (fabs(components[i]) / maxComp < ZERO_TOLERANCE) components[i] = 0.0f;
}
}
}
Vector operator+(const Vector& lhv, const Vector& rhv) {
Vector retVec(lhv);
retVec += rhv;
return retVec;
}
Vector operator-(const Vector& lhv, const Vector& rhv) {
Vector retVec(lhv);
retVec -= rhv;
return retVec;
}
Vector Cross(const Vector& lhv, const Vector& rhv) {
if (lhv.GetDim() != 3) AbortWithMsg("failed to cross: variable 'dim' must be 3");
if (lhv.GetDim() != rhv.GetDim()) AbortWithMsg("failed to cross: Irregular Dimention");
Vector retVec(lhv.GetDim());
for (int i = 0; i < lhv.GetDim(); i++) {
retVec.SetComp(i + 1, lhv.GetComp((i + 1) % 3 + 1) * rhv.GetComp((i + 2) % 3 + 1)
- lhv.GetComp((i + 2) % 3 + 1) * rhv.GetComp((i + 1) % 3 + 1));
}
return retVec;
}
Vector operator*(const float c, const Vector& rhv) {
Vector retVec(rhv);
retVec *= c;
return retVec;
}
Vector operator*(const Vector& lhv, const float c) {
Vector retVec(lhv);
retVec *= c;
return retVec;
}
float operator*(const Vector& lhv, const Vector& rhv) {
if (lhv.GetDim() != rhv.GetDim()) AbortWithMsg("Irregular Dimention");
float retVal = 0.0f;
for (int i = 1; i <= lhv.GetDim(); i++) {
retVal += lhv.GetComp(i) * rhv.GetComp(i);
}
return retVal;
}
