• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FEniCS / ufl / 18170853666

01 Oct 2025 05:47PM UTC coverage: 75.964%. First build
18170853666

push

github

jorgensd
Merge remote-tracking branch 'origin/main' into release

1033 of 1235 new or added lines in 68 files covered. (83.64%)

8960 of 11795 relevant lines covered (75.96%)

0.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.67
/ufl/variable.py
1
"""Define the Variable and Label classes.
2

3
These are used to label expressions as variables for differentiation.
4
"""
5
# Copyright (C) 2008-2016 Martin Sandve Alnæs
6
#
7
# This file is part of UFL (https://www.fenicsproject.org)
8
#
9
# SPDX-License-Identifier:    LGPL-3.0-or-later
10

11
from ufl.constantvalue import as_ufl
1✔
12
from ufl.core.expr import Expr
1✔
13
from ufl.core.operator import Operator
1✔
14
from ufl.core.terminal import Terminal
1✔
15
from ufl.core.ufl_type import ufl_type
1✔
16
from ufl.utils.counted import Counted
1✔
17

18

19
@ufl_type()
1✔
20
class Label(Terminal, Counted):
1✔
21
    """Label."""
22

23
    __slots__ = ("_count", "_counted_class")
1✔
24

25
    def __init__(self, count=None):
1✔
26
        """Initialise."""
27
        Terminal.__init__(self)
1✔
28
        Counted.__init__(self, count, Label)
1✔
29

30
    def __str__(self):
1✔
31
        """Format as a string."""
NEW
32
        return f"Label({self._count})"
×
33

34
    def __repr__(self):
1✔
35
        """Representation."""
36
        return f"Label({self._count})"
1✔
37

38
    @property
1✔
39
    def ufl_shape(self):
1✔
40
        """Get the UFL shape."""
41
        raise ValueError("Label has no shape (it is not a tensor expression).")
×
42

43
    @property
1✔
44
    def ufl_free_indices(self):
1✔
45
        """Get the UFL free indices."""
46
        raise ValueError("Label has no free indices (it is not a tensor expression).")
×
47

48
    @property
1✔
49
    def ufl_index_dimensions(self):
1✔
50
        """Get the UFL index dimensions."""
51
        raise ValueError("Label has no free indices (it is not a tensor expression).")
×
52

53
    def is_cellwise_constant(self):
1✔
54
        """Return true if the object is constant on each cell."""
55
        return True
×
56

57
    def ufl_domains(self):
1✔
58
        """Return tuple of domains related to this terminal object."""
59
        return ()
1✔
60

61
    def _ufl_signature_data_(self, renumbering):
1✔
62
        """UFL signature data."""
63
        if self not in renumbering:
1✔
64
            return ("Label", self._count)
×
65
        return ("Label", renumbering[self])
1✔
66

67

68
@ufl_type(is_shaping=True, is_index_free=True, num_ops=1, inherit_shape_from_operand=0)
1✔
69
class Variable(Operator):
1✔
70
    """A Variable is a representative for another expression.
71

72
    It will be used by the end-user mainly for defining
73
    a quantity to differentiate w.r.t. using diff.
74
    Example::
75

76
      e = <...>
77
      e = variable(e)
78
      f = exp(e**2)
79
      df = diff(f, e)
80
    """
81

82
    __slots__ = ()
1✔
83

84
    def __init__(self, expression, label=None):
1✔
85
        """Initalise."""
86
        # Conversion
87
        expression = as_ufl(expression)
1✔
88
        if label is None:
1✔
89
            label = Label()
1✔
90

91
        # Checks
92
        if not isinstance(expression, Expr):
1✔
93
            raise ValueError("Expecting Expr.")
×
94
        if not isinstance(label, Label):
1✔
95
            raise ValueError("Expecting a Label.")
×
96
        if expression.ufl_free_indices:
1✔
97
            raise ValueError("Variable cannot wrap an expression with free indices.")
×
98

99
        Operator.__init__(self, (expression, label))
1✔
100

101
    def ufl_domains(self):
1✔
102
        """Get the UFL domains."""
103
        return self.ufl_operands[0].ufl_domains()
×
104

105
    def evaluate(self, x, mapping, component, index_values):
1✔
106
        """Evaluate."""
107
        a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
1✔
108
        return a
1✔
109

110
    def expression(self):
1✔
111
        """Get expression."""
112
        return self.ufl_operands[0]
×
113

114
    def label(self):
1✔
115
        """Get label."""
116
        return self.ufl_operands[1]
1✔
117

118
    def __eq__(self, other):
1✔
119
        """Check equality."""
120
        return (
1✔
121
            isinstance(other, Variable)
122
            and self.ufl_operands[1] == other.ufl_operands[1]
123
            and self.ufl_operands[0] == other.ufl_operands[0]
124
        )
125

126
    def __str__(self):
1✔
127
        """Format as a string."""
128
        return f"var{self.ufl_operands[1].count()}({self.ufl_operands[0]})"
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc