java – Data-type implementation for vectors and drawing a vector field as a test client

The following is the web exercise 3.2.12. from the book Computer Science An Interdisciplinary Approach by Sedgewick & Wayne:

Write a program that draws a vector field. A vector field associates a vector with every point in a Euclidean
space. Widely used in physics to model speed and direction of a moving
object or strength and direction of a Newtonian force.

Here is my program:

public class Vector {
    private final double() coordinates;

    public Vector(double() coordinates) {
        this.coordinates = coordinates;
    }

    private int getCoordinatesLength() {
        return coordinates.length;
    }

    public double getCoordinate(int index) {
        return coordinates(index - 1);
    }

    public double getLength() {
        double sumOfCoordinatesSquared = 0;
        for (int i = 0; i < getCoordinatesLength(); i++) {
            sumOfCoordinatesSquared += getCoordinate(i + 1) * getCoordinate(i + 1);
        }
        return Math.sqrt(sumOfCoordinatesSquared);
    }

    private double getDirection2D() {
        return Math.atan(getCoordinate(2) / getCoordinate(1));
    }

    public double() getDirection() {
        if (getCoordinatesLength() != 2 && getCoordinatesLength() != 3) {
            throw new IllegalArgumentException("dimention of the vector must be either 2 or 3");
        }
        int dimention = 0;
        if (getCoordinatesLength() == 2) dimention = 1;
        else if (getCoordinatesLength() == 3) dimention = 2;
        double() angles = new double(dimention);
        if (getCoordinatesLength() == 2) {
            angles(0) = Math.atan(getCoordinate(2) / getCoordinate(1));
        } else if (getCoordinatesLength() == 3) {
            double vectorLength = getLength();
            double azimuth = Math.atan(getCoordinate(2) / getCoordinate(1));
            double zenith = Math.acos(getCoordinate(3) / vectorLength);
            angles(0) = azimuth;
            angles(1) = zenith;
        }
        return angles;
    }

    public Vector add(Vector otherVector) {
        if (getCoordinatesLength() != otherVector.getCoordinatesLength()) {
            throw new IllegalArgumentException("length of the vectors must be equal");
        }
        double() newCoordinates = new double(getCoordinatesLength());
        for (int i = 0; i < getCoordinatesLength(); i++) {
            newCoordinates(i) = getCoordinate(i + 1) + otherVector.getCoordinate(i + 1);
        }
        return new Vector(newCoordinates);
    }

    public Vector multiplyByScalar(double scalar) {
        double() newCoordinates = new double(getCoordinatesLength());
        for (int i = 0; i < getCoordinatesLength(); i++) {
            newCoordinates(i) = getCoordinate(i + 1) * scalar;
        }
        return new Vector(newCoordinates);
    }

    public Vector subtract(Vector otherVector) {
        return add(otherVector.multiplyByScalar(-1.0));
    }

    public boolean isEqual(Vector otherVector) {
        if (getCoordinatesLength() != otherVector.getCoordinatesLength()) return false;
        for (int i = 0; i < getCoordinatesLength(); i++) {
            if (getCoordinate(i + 1) != otherVector.getCoordinate(i + 1)) return false;
        }
        return true;
    }

    public double applyDotProduct(Vector otherVector) {
        if (getCoordinatesLength() != otherVector.getCoordinatesLength()) {
            throw new IllegalArgumentException("length of the vectors must be equal");
        }
        double dotProduct = 0;
        for (int i = 0; i < getCoordinatesLength(); i++) {
            dotProduct += getCoordinate(i + 1) * otherVector.getCoordinate(i + 1);
        }
        return dotProduct;
    }

    public Vector applyCrossProduct(Vector otherVector) {
        if (getCoordinatesLength() != otherVector.getCoordinatesLength()) {
            throw new IllegalArgumentException("length of the vectors must be equal");
        }
        if (getCoordinatesLength() != 3) {
            throw new IllegalArgumentException("dimention of the vector must be 3");
        }
        int x = 1;
        int y = 2;
        int z = 3;
        double newXCoordinate = getCoordinate(y) * otherVector.getCoordinate(z) - getCoordinate(z) * otherVector.getCoordinate(y);
        double newYCoordinate = getCoordinate(z) * otherVector.getCoordinate(x) - getCoordinate(x) * otherVector.getCoordinate(z);
        double newZCoordinate = getCoordinate(x) * otherVector.getCoordinate(y) - getCoordinate(y) * otherVector.getCoordinate(x);
        double() newCoordinates = {
            newXCoordinate,
            newYCoordinate,
            newZCoordinate
        };
        return new Vector(newCoordinates);
    }

    public boolean isPerpendicular(Vector otherVector) {
        if (applyDotProduct(otherVector) == 0) return true;
        else return false;
    }

    public boolean isParallel(Vector otherVector) {
        double scalingFactor = 0;
        for (int i = 0; i < getCoordinatesLength(); i++) {
            if (getCoordinate(i + 1) != 0 && otherVector.getCoordinate(i + 1) != 0) {
                scalingFactor = getCoordinate(i + 1) / otherVector.getCoordinate(i + 1);
                break;
            }
        }
        double() newCoordinates = new double(getCoordinatesLength());
        for (int i = 0; i < getCoordinatesLength(); i++) {
            newCoordinates(i) = getCoordinate(i + 1) / scalingFactor;
        }
        Vector newVector = new Vector(newCoordinates);
        if (otherVector.isEqual(newVector)) return true;
        else return false;
    }

    public String toString() {
        String printedCoordinates = "";
        for (int i = 0; i < getCoordinatesLength() - 1; i++) {
            printedCoordinates += (getCoordinate(i + 1) + ", ");
        }
        return "(" + printedCoordinates + getCoordinate(getCoordinatesLength()) + ")";
    }

    public void draw(double originX, double originY, double scaleDownFactor, double arrowHeadSize) {
        if (getCoordinatesLength() != 2) {
            throw new IllegalArgumentException("dimention of the vector must be 3");
        }
        double newX = getCoordinate(1) * scaleDownFactor;
        double newY = getCoordinate(2) * scaleDownFactor;
        double arrowHeadPointX = originX + newX;
        double arrowHeadPointY = originY + newY;
        StdDraw.line(originX, originY, arrowHeadPointX, arrowHeadPointY);
        double arrowHeadBaseX = arrowHeadSize * Math.sin(getDirection2D());
        double arrowHeadBaseY = arrowHeadSize * Math.cos(getDirection2D());
        double() arrowHeadXCoordinates = {-arrowHeadBaseX + (originX + 0.95 * newX),
                                           arrowHeadBaseX + (originX + 0.95 * newX),
                                           arrowHeadPointX
        };
        double() arrowHeadYCoordinates = {
             arrowHeadBaseY + (originY + 0.95 * newY),
            -arrowHeadBaseY + (originY + 0.95 * newY),
             arrowHeadPointY
        };
        StdDraw.filledPolygon(arrowHeadXCoordinates, arrowHeadYCoordinates);
    }

    public static void main(String() args) {
        /*
        double() coordinatesOfVectorA = {1,2};
        double() coordinatesOfVectorB = {0,1};
        Vector vectorA = new Vector(coordinatesOfVectorA);
        Vector vectorB = new Vector(coordinatesOfVectorB);
        double originX = 0.5;
        double originY = 0.5;
        double scaleDownFactor = 0.1; 
        double arrowHeadSize = 0.01;

        System.out.println("Vector A = " + vectorA.toString());
        System.out.println("Vector B = " + vectorB.toString());
        System.out.println("A plus B equals " + vectorA.add(vectorB).toString());
        System.out.println("A minus B equals " + vectorA.subtract(vectorB).toString());
        System.out.println("Dot product of A and B equals " + vectorA.applyDotProduct(vectorB));
        //System.out.println("Cross product of A and B equals " + vectorA.applyCrossProduct(vectorB).toString()); 
        System.out.println(vectorA.isParallel(vectorB));

        vectorA.draw(originX, originY, scaleDownFactor, arrowHeadSize);
        vectorB.draw(originX, originY, scaleDownFactor, arrowHeadSize);
        */
        StdDraw.setXscale(-1, 1);
        StdDraw.setYscale(-1, 1);
        for (int i = -10; i < 11; i++) {
            for (int j = -10; j < 11; j++) {
                if (i == 0 && j == 0) j++;
                double x = 1.0 * i / 10;
                double y = 1.0 * j / 10;
                double vectorXCoordinate = -y;
                double vectorYCoordinate = x;
                double() coordinates = {
                    vectorXCoordinate,
                    vectorYCoordinate
                };
                Vector vector = new Vector(coordinates);
                vector.draw(x, y, 0.1, 0.01);
            }
        }
    }
}

StdDraw is a simple API written by the authors of the book. I checked my program and it works. Here is one instance of it:

Input (taken from here):

enter image description here

Output:

enter image description here

Is there any way that I can improve my program?

Thanks for your attention.

functions – Multiplication of certain terms in a sum within a vector

Is there any way to multiply certain terms within a vector with a constant? e.g. assume that I start with a vector

{f[4] – g[-1] , h[2] , 2 * g[2] + h[-1]}

where f[ i ], g[ i ], h[ i ] are functions that I’d like to keep in their general form. I would like to multiply every function that is evaluated at a negative number with a constant c. The above example would then look like

{f[4] – c * g[-1] , h[2] , 2 * g[2] + c * h[-1]}

design – How to efficiently map unknown vector fields

I’ve got a problem I’ve been wrestling with, and I’m really truly stuck:

I have a bounded, continuous vector field and my goal is to map it using discrete measurements at specific points. I can only observe the value of the vector field at a specific point by taking a “measurement”, and every measurement costs me. My goal is to get my uncertainty about the vector value of any point on the vector field below a certain error bound ε (epsilon), while taking as few measurements and traveling the shortest distance as possible.

For example, below is an example of one such vector field on the left. In the middle is one possible way of “exploring” the vector field. I first measure at point (-0.5, 0.4), which I’ve marked with green, as at the beginning I (like Jon Snow) know nothing about the field so I’m starting at a random point. Then after measuring the value at green the algorithm informs me I should explore point (-0.35, -0.4), which I’ve marked with yellow. As I’ve jumped from green to yellow I incur the cost of the distance of travel, which is the distance between the two points. Then my algorithm tells me to explore point (0.82, 0.3), which I’ve marked with orange, and I incur the cost of the distance between yellow and orange. Then my algorithm tells me to explore point (0, 0.86), which I’ve marked with red, and I incur the cost of the distance between orange and red. Finally, my algorithm tells me I’m done, as it has deduced the value at every point to error bound ε. Cost is below.

cost = distance(green, yellow) + distance(yellow, orange) + distance(orange, red)

As one more example, below on the right is a more efficient way of exploring the field, from the new green point to the new yellow point to the new orange point, using one fewer measurement and a cost around 10% lower by saving on distance, provided the algorithm can deduce the value at every point in the field to below ε using these alternative measurements.

Unexplored Vector Field, One Possibility for Exploring Vector Field, Another Possibility for Exploring Vector Field

I’m sure these kinds of algorithms are used in self-driving cars as well as in other fields. It’s sort of a reduced variant of simultaneous localization and mapping (SLAM), but I can’t find any good references on algorithms to solve this sort of problem.

Anyone know of an algorithm for this?

Turn vector equation into matrix multiplication

I have physical equations of motion that describe the dependence of one vector field on the components of another vector field. Without getting too much into detail, my differential equations involve a double curl that mixes the vector components on one side of the equation. Furthermore, I would like to discretize the space on which these equations are defined. So far I was able to set up the discretized equations:

$$
frac{d}{dt} begin{pmatrix}
vdots\
P_x(i)\
P_y(i)\
P_z(i)\
vdots
end{pmatrix} = begin{pmatrix}
vdots\
A_y(i)-A_y(i+1)\
-A_x(i)+A_x(i+1)-a*A_z(i)\
b*A_y(i)\
vdots
end{pmatrix}tag{1}
$$

where a and b are some arbitrary constants and $vec{A}$ and $vec{P}$ are the two vector fields whose relation I want to determine. The lattice coordinates in one dimension are written as i (e.g. $frac{d}{dt} P_x(7) = A_y(7)-A_y(8)$).

Is there any way to automatically turn this into a matrix multiplication? I would want the result to look something like

$$
frac{d}{dt} begin{pmatrix}
vdots\
P_x(i)\
P_y(i)\
P_z(i)\
P_x(i+1)\
P_y(i+1)\
P_z(i+1)\
vdots
end{pmatrix} = begin{pmatrix}
ddots & ddots & ddots & ddots & ddots & ddots & ddots & ddots \
ddots & 0 & 1 & 0 & 0 & -1 & 0 & ddots\
ddots & -1 & 0 & -a & 1 & 0 & 0 & ddots\
ddots & 0 & b & 0 & 0 & 0 & 0 & ddots\
ddots & ddots & ddots & ddots & ddots & ddots & ddots & ddots\
ddots & ddots & ddots & ddots & ddots & ddots & ddots & ddots\
ddots & ddots & ddots & ddots & ddots & ddots & ddots & ddots\
ddots & ddots & ddots & ddots & ddots & ddots & ddots & ddots\
end{pmatrix}
begin{pmatrix}
vdots\
A_x(i)\
A_y(i)\
A_z(i)\
A_x(i+1)\
A_y(i+1)\
A_z(i+1)\
vdots
end{pmatrix} tag{2}
$$

You can already see from this example that there is some regularity as P(i+1), P(i+2), etc. will have the same entries in the matrix as the ones for P(i), only that the entries within the matrix will be at different positions.

So the question is: If I gave you the vector on the right-hand side of equation (1), could you give me the matrix on the right-hand side of equation (2)?

geometric probability – Let $W$ be random matrix and $b$ be vector. Let $x$ be fixed (unknown) vector, let $(y(x))_j = max(A_jx+b,0)$. Find $x’$ such that $y(x’)=y(x)$ whp

Let $n le n le p$ be large positive integers. $W$ be a random $n times p$ matrix with entries drawn iid from $mathcal N(0,1/p)$ and let $b$ be an $n$-dimensional random vector with coordinates drawn iid from $mathcal N(0,sigma^2)$ for some $sigma^2$. We can also consider the scenario when $b in mathbb R^n_{>0}$ is fixed (i.e not random). Now, fix a vector $x in mathbb R^p$ and consider the $n$-dimensional vector $y(x)$ with coordinates given by $(y(x))_j := max(langle W_j,xrangle + b_j,0)$, for all $j in (n)$. Finally, let $epsilon > 0$.

Question. Is it possible to find a vector $x’ in mathbb R^p$ with $|x’-x| le epsilon$, such that $y(x’)=y(x)$ high probability (over the choice of $W$ and $b$) ?

unity3d – Problem importing Vector shapes in .psb to Unity?

My .PSB file in photoshop has my character that is made up of vector shapes.I haven’t rasterized it. When I import that .psb file to Unity , nothing shows. But if I rasterize it , it works. I want to use vector shapes without rasterizing so that I can make changes to it easily later on. Is there any way to do it ? or Am I just heading into wrong direction ?

I need a solution to this vector calculus question

Let Σ be the surface parametrised by
φ(a, b) = (cos a cos b,sin a sin b, cos b + log(tan(b/2)).
where 0 < a < 2π and 0 < b < π.
(a) Show that the vector n = (cos a cos b,sin a cos b, ; sin b) is a unit normal to the surface Σ at the
point φ(a, b).
(b) Compute the area of the surface Σ.
(Possible hint: The function log(tan(b/2)) has a particularly nice derivative. Simplify it as much as
possible.)

ag.algebraic geometry – Hitchin map and vector bundles

I’ve been learning a bit about automorphisms of moduli spaces of vector bundles and the Hitchin map.
I’m reading this paper of Indranil Biswas, Tomas L. Gomez, V. Munoz, and I have a problem about prop $5.1$

Let $E$ be a semistable vector bundle over a smooth projective curve $X$ over $mathbb{C}$ and $K_X$ be the canonical bundle of $X$.

$H$ is the Hitchin map, $H:H^0(End_0Eotimes K_X)rightarrow W$ $(W:=bigoplus H^0(X, K_X^{otimes i}))$
and $h_r$ is the composition of $H$ and the projection $Wrightarrow W_r$ $(W_r:=H^0(X,K_X^{otimes r}))$
$h_r:H^0(End_0Eotimes K_X)rightarrow W_r$

Then prop $5.1$ says that
$H^0(End_0Eotimes K_X(-x))$$={psiin H^0(End_0Eotimes K_X) mid h_r(psi+phi)in H^0(K_X^{otimes r }(-x)),forallphi in h_r^{-1}(H^0(K_X^{otimes r}{(-x)}))}$
By using prop$5.1$, we can get the vector bundle
$mathcal{E}rightarrow X$
whose fiber over $x in X$ is $mathcal{E}_x=H^0(End_0Eotimes K_X(-x))$

But I don’t understand why prop$5.1$ allows us to construct such vector bundle.It shows that $dim_{mathbb{C}}mathcal{E}_{x}$ is constant?

This may be a stupid question.
Thank in advance.

algebraic geometry – Some question about the hitchin map and vector bundles.

I’ve been learning a bit about automorphisms of moduli spaces of vector bundles and the Hitchin map.
I’m reading this paper of Indranil Biswas, Tomas L. Gomez, V. Munoz, and I have a problem about prop $5.1$.

$H$ is the hitchin map and $h_r$ is the composition of $H$ and the projection $Wrightarrow W_r$
$h_r:H^0(End_0Eotimes K_X))rightarrow W_r$
$H:H^0(End_0Eotimes K_X))rightarrow W$
$W=bigoplus H^0(X, K_X^{otimes i})$,$W_r=H^0(X,K_X^{otimes r})$
Then prop $5.1$ says that
$H^0(End_0Eotimes K_X)(-x_0))={psiin H^0(End_0Eotimes K_X) mid h_r(psi+phi)in H^0(K_X^{otimes r }(-x_0)),forallphi in h_r^{-1}(H^0(K_X^{otimes r}{(-x_0)}))}$
and
using prop$5.1$, we can get the vector bundle
$mathcal{E}rightarrow X$
whose fiber over $x in X$ is $mathcal{E}_x=H^0(End_0Eotimes K_X(-x))$

But I don’t understand why prop$5.1$ allows to construct such vector bundle.It shows that $dim_{mathbb{C}}mathcal{E}_{x}$ is constant?

This may be a stupid question.
Thank in advance.

list manipulation – match in vector entries

I have a list composed by vectors, let say with 5 entries each one. The list can have any dimension.

file= {{1, 0, -1, 1, 0}, {-1, 0, -1, 0, 0}, {1, 0, 1, 0, 0}, {1, 0, -1, 0,
1},{1, 0, -1, 1, 0}, {-1, 0, 1, 0, 0}, {1, 0, 1, 0, 0},{1, 0, 0, 0, 0}, {0, 0, 1, 0, 0}, {1, 0, 1, 1, 0}};

The first three components of each vector can be chosen in the set {-1,0,1} (3^3 choices), the 4th and 5th are either 0 or 1.

I want to add the 4th & 5th entry in all the vectors that have identical 1-3rd entry.

I though to compare the first 3 entries using HammingDistance, something like

If(Length(file) != 0,DeleteCases( Flatten(Table(If(i != j &&HammingDistance(Drop(file((i)), -2),Drop(file((j)), -2)) ==0, {file((i))((1)), file((i))((2)),file((i))((3)),file((i))((4)), file((i))((5))}, 0),{i, 1, Length(file)}, {j,1, i}), 1), 0), 0)

but this only selects vectors in which the pattern is found more than once.

Any idea of how to match and clutter all vectors with the same first 3 entries?