Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A How to apply a unit constraint in SymPy

The solution to this is probably easy, but I haven't been able to find it. Using SymPy, I am trying to solve this equation: $$ {x_1}^2 + {x_2}^2 + {x_3}^2 + 3 = a $$ with this constraint: $$ ...

3 answers  ·  posted 2mo ago by Trevor‭  ·  last activity 2mo ago by wjandrea‭

Question sympy
#2: Post edited by user avatar wjandrea‭ · 2026-07-10T08:03:39Z (2 months ago)
Clarify math notation (at first it looked like `x**(2/3)` e.g.). Capitalization.
  • how to apply a unit constraint in Sympy
  • How to apply a unit constraint in SymPy
  • The solution to this is probably easy, but I haven't been able to find it.
  • Using Sympy, I am trying to solve this equation:
  • $$
  • x_1^2 + x_2^2 + x_3^2 + 3 = a
  • $$
  • with this constraint:
  • $$
  • x_1^2 + x_2^2 + x_3^2 = 1
  • $$
  • It is obvious that $a=4$. So, I wrote this code:
  • ```python
  • import sympy as sp
  • a, x1, x2, x3 = sp.symbols('a x1 x2 x3')
  • eq1 = x1**2 + x2**2 + x3**2 + 3 - a
  • eq2 = x1**2 + x2**2 + x3**2 - 1
  • sol = sp.solve([eq1, eq2], a)
  • print(sol)
  • ```
  • Which returns:
  • ```
  • {a: x1**2 + x2**2 + x3**2 + 3}
  • ```
  • The unit-norm constraint on `x` is not applied.
  • How can I get Sympy to apply the unit-norm constraint, which results in $a=4$?
  • The solution to this is probably easy, but I haven't been able to find it.
  • Using SymPy, I am trying to solve this equation:
  • $$
  • {x_1}^2 + {x_2}^2 + {x_3}^2 + 3 = a
  • $$
  • with this constraint:
  • $$
  • {x_1}^2 + {x_2}^2 + {x_3}^2 = 1
  • $$
  • It is obvious that $a=4$. So, I wrote this code:
  • ```python
  • import sympy as sp
  • a, x1, x2, x3 = sp.symbols('a x1 x2 x3')
  • eq1 = x1**2 + x2**2 + x3**2 + 3 - a
  • eq2 = x1**2 + x2**2 + x3**2 - 1
  • sol = sp.solve([eq1, eq2], a)
  • print(sol)
  • ```
  • Which returns:
  • ```
  • {a: x1**2 + x2**2 + x3**2 + 3}
  • ```
  • The unit-norm constraint on `x` is not applied.
  • How can I get SymPy to apply the unit-norm constraint, which results in $a=4$?
#1: Initial revision by user avatar Trevor‭ · 2026-07-07T06:19:30Z (2 months ago)
how to apply a unit constraint in Sympy
The solution to this is probably easy, but I haven't been able to find it.
Using Sympy, I am trying to solve this equation:
$$
x_1^2 + x_2^2 + x_3^2 + 3 = a
$$
with this constraint:
$$
x_1^2 + x_2^2 + x_3^2 = 1
$$
It is obvious that $a=4$. So, I wrote this code:

```python
import sympy as sp

a, x1, x2, x3 = sp.symbols('a x1 x2 x3')
eq1 = x1**2 + x2**2 + x3**2 + 3 - a
eq2 = x1**2 + x2**2 + x3**2 - 1
sol = sp.solve([eq1, eq2], a) 

print(sol)
```
Which returns:
```
{a: x1**2 + x2**2 + x3**2 + 3}
```
The unit-norm constraint on `x` is not applied.
How can I get Sympy to apply the unit-norm constraint, which results in $a=4$?