Line data Source code
1 : // Copyright 2024, UChicago Argonne, LLC
2 : // All Rights Reserved
3 : // Software Name: NEML2 -- the New Engineering material Model Library, version 2
4 : // By: Argonne National Laboratory
5 : // OPEN SOURCE LICENSE (MIT)
6 : //
7 : // Permission is hereby granted, free of charge, to any person obtaining a copy
8 : // of this software and associated documentation files (the "Software"), to deal
9 : // in the Software without restriction, including without limitation the rights
10 : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 : // copies of the Software, and to permit persons to whom the Software is
12 : // furnished to do so, subject to the following conditions:
13 : //
14 : // The above copyright notice and this permission notice shall be included in
15 : // all copies or substantial portions of the Software.
16 : //
17 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 : // THE SOFTWARE.
24 :
25 : #include "neml2/user_tensors/LogspacePrimitiveTensor.h"
26 : #include "neml2/tensors/tensors.h"
27 : #include "neml2/misc/assertions.h"
28 :
29 : namespace neml2
30 : {
31 : template <typename T>
32 : OptionSet
33 44 : LogspacePrimitiveTensor<T>::expected_options()
34 : {
35 : // This is the only way of getting tensor type in a static method like this...
36 : // Trim 6 chars to remove 'neml2::'
37 44 : auto tensor_type = utils::demangle(typeid(T).name()).substr(7);
38 :
39 44 : OptionSet options = UserTensorBase::expected_options();
40 44 : options.doc() = "Construct a " + tensor_type +
41 : " with exponents linearly spaced on the batch dimensions. See "
42 : "neml2::TensorBase::logspace for a detailed explanation.";
43 :
44 88 : options.set<TensorName<T>>("start");
45 88 : options.set("start").doc() = "The starting tensor";
46 :
47 88 : options.set<TensorName<T>>("end");
48 88 : options.set("end").doc() = "The ending tensor";
49 :
50 88 : options.set<Size>("nstep");
51 88 : options.set("nstep").doc() = "The number of steps with even spacing along the new dimension";
52 :
53 88 : options.set<Size>("dim") = 0;
54 88 : options.set("dim").doc() = "Where to insert the new dimension";
55 :
56 88 : options.set<double>("base") = 10;
57 44 : options.set("base").doc() = "Exponent base";
58 :
59 132 : options.set<TensorShape>("batch_expand") = TensorShape();
60 44 : options.set("batch_expand").doc() = "After construction, perform an additional batch expanding "
61 : "operation into the given batch shape.";
62 :
63 88 : return options;
64 44 : }
65 :
66 : template <typename T>
67 19 : LogspacePrimitiveTensor<T>::LogspacePrimitiveTensor(const OptionSet & options)
68 : : UserTensorBase(options),
69 19 : T(make(options))
70 : {
71 19 : }
72 :
73 : template <typename T>
74 : T
75 19 : LogspacePrimitiveTensor<T>::make(const OptionSet & options) const
76 : {
77 19 : auto * f = this->factory();
78 19 : neml_assert(f, "Internal error: factory != nullptr");
79 :
80 190 : auto t = T::logspace(options.get<TensorName<T>>("start").resolve(f),
81 : options.get<TensorName<T>>("end").resolve(f),
82 : options.get<Size>("nstep"),
83 : options.get<Size>("dim"),
84 : options.get<double>("base"));
85 :
86 : // Expand if requested
87 19 : auto S = options.get<TensorShape>("batch_expand");
88 19 : if (!S.empty())
89 0 : t = t.batch_expand(S);
90 :
91 38 : return t;
92 19 : }
93 :
94 : #define LOGSPACEPRIMITIVETENSOR_REGISTER(T) \
95 : using Logspace##T = LogspacePrimitiveTensor<T>; \
96 : register_NEML2_object_alias(Logspace##T, "Logspace" #T)
97 : FOR_ALL_PRIMITIVETENSOR(LOGSPACEPRIMITIVETENSOR_REGISTER);
98 : } // namespace neml2
|