home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (Vector.java)

This example Java source code file (Vector.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

matharithmeticexception, numberformat, point, space, string, text, vector

The Vector.java Java example source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.math3.geometry;

import java.text.NumberFormat;

import org.apache.commons.math3.exception.MathArithmeticException;

/** This interface represents a generic vector in a vectorial space or a point in an affine space.
 * @param <S> Type of the space.
 * @see Space
 * @see Point
 * @since 3.0
 */
public interface Vector<S extends Space> extends Point {

    /** Get the null vector of the vectorial space or origin point of the affine space.
     * @return null vector of the vectorial space or origin point of the affine space
     */
    Vector<S> getZero();

    /** Get the L<sub>1 norm for the vector.
     * @return L<sub>1 norm for the vector
     */
    double getNorm1();

    /** Get the L<sub>2 norm for the vector.
     * @return Euclidean norm for the vector
     */
    double getNorm();

    /** Get the square of the norm for the vector.
     * @return square of the Euclidean norm for the vector
     */
    double getNormSq();

    /** Get the L<sub>∞ norm for the vector.
     * @return L<sub>∞ norm for the vector
     */
    double getNormInf();

    /** Add a vector to the instance.
     * @param v vector to add
     * @return a new vector
     */
    Vector<S> add(Vector v);

    /** Add a scaled vector to the instance.
     * @param factor scale factor to apply to v before adding it
     * @param v vector to add
     * @return a new vector
     */
    Vector<S> add(double factor, Vector v);

    /** Subtract a vector from the instance.
     * @param v vector to subtract
     * @return a new vector
     */
    Vector<S> subtract(Vector v);

    /** Subtract a scaled vector from the instance.
     * @param factor scale factor to apply to v before subtracting it
     * @param v vector to subtract
     * @return a new vector
     */
    Vector<S> subtract(double factor, Vector v);

    /** Get the opposite of the instance.
     * @return a new vector which is opposite to the instance
     */
    Vector<S> negate();

    /** Get a normalized vector aligned with the instance.
     * @return a new normalized vector
     * @exception MathArithmeticException if the norm is zero
     */
    Vector<S> normalize() throws MathArithmeticException;

    /** Multiply the instance by a scalar.
     * @param a scalar
     * @return a new vector
     */
    Vector<S> scalarMultiply(double a);

    /**
     * Returns true if any coordinate of this vector is infinite and none are NaN;
     * false otherwise
     * @return  true if any coordinate of this vector is infinite and none are NaN;
     * false otherwise
     */
    boolean isInfinite();

    /** Compute the distance between the instance and another vector according to the L<sub>1 norm.
     * <p>Calling this method is equivalent to calling:
     * <code>q.subtract(p).getNorm1() except that no intermediate
     * vector is built</p>
     * @param v second vector
     * @return the distance between the instance and p according to the L<sub>1 norm
     */
    double distance1(Vector<S> v);

    /** Compute the distance between the instance and another vector according to the L<sub>2 norm.
     * <p>Calling this method is equivalent to calling:
     * <code>q.subtract(p).getNorm() except that no intermediate
     * vector is built</p>
     * @param v second vector
     * @return the distance between the instance and p according to the L<sub>2 norm
     */
    double distance(Vector<S> v);

    /** Compute the distance between the instance and another vector according to the L<sub>∞ norm.
     * <p>Calling this method is equivalent to calling:
     * <code>q.subtract(p).getNormInf() except that no intermediate
     * vector is built</p>
     * @param v second vector
     * @return the distance between the instance and p according to the L<sub>∞ norm
     */
    double distanceInf(Vector<S> v);

    /** Compute the square of the distance between the instance and another vector.
     * <p>Calling this method is equivalent to calling:
     * <code>q.subtract(p).getNormSq() except that no intermediate
     * vector is built</p>
     * @param v second vector
     * @return the square of the distance between the instance and p
     */
    double distanceSq(Vector<S> v);

    /** Compute the dot-product of the instance and another vector.
     * @param v second vector
     * @return the dot product this.v
     */
    double dotProduct(Vector<S> v);

    /** Get a string representation of this vector.
     * @param format the custom format for components
     * @return a string representation of this vector
     */
    String toString(final NumberFormat format);

}

Other Java examples (source code examples)

Here is a short list of links related to this Java Vector.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.