/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2012:
* Stanislav Poslavsky <stvlpos@mail.ru>
* Bolotin Dmitriy <bolotin.dmitriy@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package cc.redberry.core.tensor.random;
import java.util.Random;
import cc.redberry.core.context.CC;
import cc.redberry.core.context.NameDescriptor;
import cc.redberry.core.indices.EmptyIndices;
import cc.redberry.core.indices.IndicesTypeStructure;
import cc.redberry.core.tensor.Product;
import cc.redberry.core.tensor.SimpleTensor;
import cc.redberry.core.tensor.Sum;
import cc.redberry.core.tensor.Tensor;
/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public class RandomTensor {
private static final int ALPHABET_SIZE = 25;
private final Random random = new Random();
private final int[] tensorNames;
private static RandomTensor INSTANCE = new RandomTensor(ALPHABET_SIZE);
public RandomTensor(int nameSpaceSize) {
this.tensorNames = new int[nameSpaceSize];
for(int i=0; i< nameSpaceSize; ++i){
int first = i % ALPHABET_SIZE;
int second = i - first;
String sName = new String(new char[]{(char) (0x41 + first), (char) (0x41 + second)});
tensorNames[i] = CC.getNameManager().mapNameDescriptor(new NameDescriptor(sName, new IndicesTypeStructure(EmptyIndices.INSTANCE)));
}
}
public static RandomTensor getRandom() {
return INSTANCE;
}
public static RandomTensor getRandom(int namespaceSize) {
return new RandomTensor(namespaceSize);
}
public Tensor randomScalarSimpleTensor() {
// int length = 1 + (int) (Math.random() * maxNameLength);
// char[] name = new char[length];
// for (int i = 0; i < length; ++i)
// name[i] = (char) (0x41 + (int) (Math.random() * namespaceSize));
// NameDescriptor descriptor = new NameDescriptor(String.valueOf(name), new IndicesTypeStructure(EmptyIndices.INSTANCE));
// int tensorName = CC.getNameManager().mapNameDescriptor(descriptor);
//
return new SimpleTensor(tensorNames[random.nextInt(tensorNames.length)], EmptyIndices.INSTANCE);
}
public Tensor randomScalarProduct(int size) {
if (size <= 0)
throw new IllegalArgumentException("size <= 0");
if (size == 1)
return randomScalarSimpleTensor();
Product product = new Product();
for (int i = 0; i < size; ++i)
product.add(randomScalarSimpleTensor());
return product;
}
public Tensor randomScalarProduct(int size,int maxSumSize) {
if (size <= 0)
throw new IllegalArgumentException("size <= 0");
if (size == 1)
return randomScalarSimpleTensor();
Product product = new Product();
for (int i = 0; i < size; ++i){
int randomProductSize = (int) (1 + Math.random() * maxSumSize);
product.add(randomScalarSimpleTensor());
}
return product;
}
public Tensor randomScalarSum(int size) {
return randomScalarSum(size, 3);
}
public Tensor randomScalarSum(int size, int maxProductsSize) {
if (size <= 0 || maxProductsSize <= 0)
throw new IllegalArgumentException("size <= 0");
if (size == 1)
return randomScalarProduct(maxProductsSize);
Sum sum = new Sum();
for (int i = 0; i < size; ++i) {
int randomProductSize = (int) (1 + Math.random() * maxProductsSize);
sum.add(randomScalarProduct(randomProductSize));
}
return sum;
}
}