
cc.redberry.core.indices.IndicesBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Redberry is an open source computer algebra system designed for tensor
manipulation. It implements basic computer algebra system routines as well as
complex tools for real computations in physics.
This is Redberry core, which contains the implementation of the basic
computer algebra routines and general-purpose transformations.
/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2012:
* Stanislav Poslavsky
* Bolotin Dmitriy
*
* This file is part of Redberry.
*
* Redberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Redberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Redberry. If not, see .
*/
package cc.redberry.core.indices;
import cc.redberry.core.math.MathUtils;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.utils.IntArray;
import cc.redberry.core.utils.IntArrayList;
/**
* This class provides functionality to construct unordered {@code Indices}
* object by combining other {@code Indices} objects. For example, if we have a
* product {@code X_mn*Y_ab} we can construct products indices by appending
* consequentially indices of tensor {@code X} and {@code Y} to
* {@code IndicesBuilder}. As the result this class returns
* {@code SortedIndices} instance.
*
* @see Indices
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public final class IndicesBuilder {
private final IntArrayList data;
public IndicesBuilder() {
data = new IntArrayList();
}
private IndicesBuilder(IntArrayList data) {
this.data = data;
}
public IndicesBuilder(int capacity) {
data = new IntArrayList(capacity);
}
/**
* Appends index representation of specified {@code int}.
*
* @param index index to be appended
*
* @return a reference to this object
*/
public IndicesBuilder append(int index) {
data.add(index);
return this;
}
/**
* Appends indices representation of specified {@code int[]}.
*
* @param indices indices to be appended
*
* @return a reference to this object
*/
public IndicesBuilder append(int[] indices) {
data.addAll(indices);
return this;
}
/**
* Appends indices representation of specified {@code IntArray}.
*
* @param indices indices to be appended
*
* @return a reference to this object
*/
public IndicesBuilder append(IntArray indices) {
data.addAll(indices);
return this;
}
/**
* Appends indices representation of specified {@code IntArrayList}.
*
* @param indices indices to be appended
*
* @return a reference to this object
*/
public IndicesBuilder append(IntArrayList indices) {
data.addAll(indices);
return this;
}
/**
* Appends specified {@code Indices}.
*
* @param indices indices to be appended
*
* @return a reference to this object
*/
public IndicesBuilder append(Indices indices) {
return append(indices.getAllIndices());
}
/**
* Appends specified {@code IndicesBuilder}.
*
* @param ib IndicesBuilder
*
* @return a reference to this object
*/
public IndicesBuilder append(IndicesBuilder ib) {
return append(ib.toArray());
}
/**
* Appends indices of specified {@code Tensor}.
*
* @param tensor a tensor
*
* @return a reference to this object
*/
public IndicesBuilder append(Tensor tensor) {
return append(tensor.getIndices());
}
/**
* Appends consequentially indices of {@code tensors} in specified array.
*
* @param tensors an array of tensors
*
* @return a reference to this object
*/
public IndicesBuilder append(Tensor... tensor) {
for (Tensor t : tensor)
append(t);
return this;
}
/**
* Returns resulting {@code Indices}.
*
* @return resulting {@code Indices}
*/
public Indices getIndices() {
return IndicesFactory.createSorted(data.toArray());
}
/**
* Returns integer array, representing indices, constructed in this
* {@code IndicesBuilder}.
*
* @return integer array, representing indices, constructed in this
* {@code IndicesBuilder}
*/
public int[] toArray() {
return data.toArray();
}
public Indices getDistinct() {
//TODO review performance
return IndicesFactory.createSorted(MathUtils.getSortedDistinct(data.toArray()));
}
@Override
public String toString() {
return getIndices().toString();
}
@Override
public IndicesBuilder clone() {
return new IndicesBuilder(data.clone());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy