Skip to content

Commit 1523191

Browse files
committed
Some new convenience methods
1 parent 5955384 commit 1523191

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/java/org/tensorics/core/lang/Tensorics.java

+40
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626
import java.util.Map.Entry;
2727
import java.util.Set;
28+
import java.util.function.BiFunction;
2829
import java.util.function.Function;
2930
import java.util.function.Supplier;
3031
import java.util.stream.Stream;
@@ -194,6 +195,14 @@ public static <V, TB extends Tensorbacked<V>> TB empty(Class<TB> tensorbackedCla
194195
return Tensorbackeds.empty(tensorbackedClass);
195196
}
196197

198+
public static <V> Tensor<V> empty(Iterable<Class<?>> dimensions) {
199+
return ImmutableTensor.<V> builder(dimensions).build();
200+
}
201+
202+
public static <V> Tensor<V> empty(Class<?> ... dimensions) {
203+
return ImmutableTensor.<V> builder(dimensions).build();
204+
}
205+
197206
/**
198207
* @see Tensorbackeds#validitiesOf(Tensorbacked)
199208
*/
@@ -237,6 +246,13 @@ public static <S> Tensor<QuantifiedValue<S>> quantityTensorOf(Tensor<S> tensor,
237246
return QuantityTensors.quantityTensorOf(tensor, unit);
238247
}
239248

249+
/**
250+
* @see QuantityTensors#quantityTensorOf(Tensor, javax.measure.unit.Unit)
251+
*/
252+
public static <S> Tensor<QuantifiedValue<S>> quantityTensorOf(Tensor<S> tensor, javax.measure.unit.Unit<?> unit) {
253+
return QuantityTensors.quantityTensorOf(tensor, JScienceUnit.of(unit));
254+
}
255+
240256
/**
241257
* @see QuantityTensors#unitOf(Tensor)
242258
*/
@@ -309,18 +325,38 @@ public static <S> Tensor<S> sameValues(Shape shape, S value) {
309325
return TensorInternals.sameValues(shape, value);
310326
}
311327

328+
/**
329+
* @deprecated use {@link #tensor(Shape, Supplier)}
330+
*/
331+
@Deprecated
312332
public static <S> Tensor<S> createFrom(Shape shape, Supplier<S> supplier) {
333+
return tensor(shape, supplier);
334+
}
335+
336+
public static <S> Tensor<S> tensor(Shape shape, Supplier<S> supplier) {
313337
return TensorInternals.createFrom(shape, supplier);
314338
}
315339

340+
/**
341+
* @deprecated use {@link #tensor(Shape, Function)}
342+
*/
343+
@Deprecated
316344
public static <S> Tensor<S> createFrom(Shape shape, Function<Position, S> function) {
345+
return tensor(shape, function);
346+
}
347+
348+
public static <S> Tensor<S> tensor(Shape shape, Function<Position, S> function) {
317349
return TensorInternals.createFrom(shape, function);
318350
}
319351

320352
public static <S> OngoingCompletion<S> complete(Tensor<S> tensor) {
321353
return TensorStructurals.complete(tensor);
322354
}
323355

356+
/**
357+
* @deprecated use {@link #map(Tensor, BiFunction)} instead
358+
*/
359+
@Deprecated
324360
public static <S, T> Tensor<T> transformEntries(Tensor<S> tensor, Function<Entry<Position, S>, T> function) {
325361
return TensorStructurals.transformEntries(tensor, function);
326362
}
@@ -329,6 +365,10 @@ public static <S, T> Tensor<T> map(Tensor<S> tensor, Function<S, T> function) {
329365
return TensorStructurals.transformScalars(tensor, function);
330366
}
331367

368+
public static <S, T> Tensor<T> map(Tensor<S> tensor, BiFunction<Position, S, T> function) {
369+
return TensorStructurals.transformScalars(tensor, function);
370+
}
371+
332372
public static final Scalar<QuantifiedValue<Double>> zeroDimensionalOf(double value,
333373
javax.measure.unit.Unit<?> unit) {
334374
QuantifiedValue<Double> quantity = quantityOf(value, unit);

src/java/org/tensorics/core/tensor/lang/QuantityTensors.java

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.tensorics.core.tensor.Position;
3030
import org.tensorics.core.tensor.Tensor;
3131
import org.tensorics.core.tensor.operations.TensorInternals;
32+
import org.tensorics.core.units.JScienceUnit;
3233
import org.tensorics.core.units.Unit;
3334

3435
import com.google.common.base.Optional;
@@ -83,6 +84,10 @@ public static <S> Tensor<Boolean> validitiesOf(Tensor<QuantifiedValue<S>> tensor
8384
return builder.build();
8485
}
8586

87+
public static <S> Tensor<QuantifiedValue<S>> quantityTensorOf(Tensor<S> tensor, javax.measure.unit.Unit<?> unit) {
88+
return quantityTensorOf(tensor, JScienceUnit.of(unit));
89+
}
90+
8691
public static <S> Tensor<QuantifiedValue<S>> quantityTensorOf(Tensor<S> tensor, Unit unit) {
8792
Builder<QuantifiedValue<S>> builder = ImmutableTensor.builder(tensor.shape().dimensionSet());
8893
builder.context(tensor.context());

src/java/org/tensorics/core/tensor/lang/TensorStructurals.java

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.HashSet;
2828
import java.util.Map.Entry;
2929
import java.util.Set;
30+
import java.util.function.BiFunction;
3031
import java.util.function.Function;
3132

3233
import org.tensorics.core.tensor.ImmutableTensor;
@@ -141,6 +142,15 @@ public static <S, T> Tensor<T> transformScalars(Tensor<S> tensor, Function<S, T>
141142
return builder.build();
142143
}
143144

145+
public static <S, T> Tensor<T> transformScalars(Tensor<S> tensor, BiFunction<Position, S, T> function) {
146+
Builder<T> builder = ImmutableTensor.builder(tensor.shape().dimensionSet());
147+
builder.context(tensor.context());
148+
for (Entry<Position, S> entry : TensorInternals.mapFrom(tensor).entrySet()) {
149+
builder.put(entry.getKey(), function.apply(entry.getKey(), entry.getValue()));
150+
}
151+
return builder.build();
152+
}
153+
144154
public static final <S> Tensor<S> stripContext(Tensor<S> tensor) {
145155
Builder<S> builder = ImmutableTensor.builder(tensor.shape().dimensionSet());
146156
builder.putAll(tensor);

0 commit comments

Comments
 (0)