File indexing completed on 2024-04-14 03:40:48

0001 /*
0002     KmPlot - a math. function plotter for the KDE-Desktop
0003 
0004     SPDX-FileCopyrightText: 2006 David Saxton <david@bluehaze.org>
0005 
0006     This file is part of the KDE Project.
0007     KmPlot is part of the KDE-EDU Project.
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 
0011 */
0012 
0013 #ifndef VECTOR_H
0014 #define VECTOR_H
0015 
0016 #include <QVector>
0017 
0018 class Value;
0019 
0020 /**
0021  * Mathematical vector.
0022  */
0023 class Vector
0024 {
0025 public:
0026     Vector()
0027     {
0028     }
0029     explicit Vector(int size)
0030         : m_data(size)
0031     {
0032     }
0033     Vector(const Vector &other)
0034         : m_data(other.m_data)
0035     {
0036     }
0037 
0038     int size() const
0039     {
0040         return m_data.size();
0041     }
0042     void resize(int s)
0043     {
0044         if (size() != s)
0045             m_data.resize(s);
0046     }
0047     double *data()
0048     {
0049         return m_data.data();
0050     }
0051     const double *data() const
0052     {
0053         return m_data.data();
0054     }
0055     Vector operator*(double x) const;
0056     Vector &operator*=(double x);
0057     Vector operator+(const Vector &other) const;
0058     Vector &operator+=(const Vector &other);
0059     Vector operator-(const Vector &other) const;
0060     Vector &operator-=(const Vector &other);
0061     Vector &operator=(const Vector &other);
0062     Vector &operator=(const QVector<Value> &other);
0063     bool operator==(const Vector &other) const
0064     {
0065         return m_data == other.m_data;
0066     }
0067     bool operator!=(const Vector &other) const
0068     {
0069         return m_data != other.m_data;
0070     }
0071     double &operator[](int i)
0072     {
0073         return m_data[i];
0074     }
0075     double operator[](int i) const
0076     {
0077         return m_data[i];
0078     }
0079     /**
0080      * Optimization for use in solving differential equations. Sets the
0081      * contents of this vector to a+k*b.
0082      */
0083     void combine(const Vector &a, double k, const Vector &b);
0084     /**
0085      * Another optimization for use in solving differential equations.
0086      */
0087     void addRK4(double dx, const Vector &k1, const Vector &k2, const Vector &k3, const Vector &k4);
0088 
0089 protected:
0090     QVector<double> m_data;
0091 };
0092 
0093 inline Vector operator*(double x, const Vector &v)
0094 {
0095     return v * x;
0096 }
0097 
0098 #endif // VECTOR_H