/- Copyright 2025 The Formal Conjectures Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -/ module public import Mathlib.Analysis.SpecialFunctions.Pow.Real@[expose] public section

nth root operations

This file provides Real.nthRoot n to compute ⁿ√, which operates as expected on negative values when n is odd. The trap this avoids is that using rpow, (-8 : ℝ) ^ (1/3 : ℝ) = 1.

This is being upstreamed to Mathlib in leanprover-community/mathlib4#26935.

namespace Realnoncomputable def nthRoot (n : ) (r : ) : := if Even n then r ^ (n⁻¹ : ) else SignType.sign r ^ n * abs r ^ (n⁻¹ : )theorem nthRoot_of_even {n : } (hn : Even n) (r : ) : nthRoot n r = r ^ (n⁻¹ : ) := if_pos hntheorem nthRoot_of_odd {n : } (hn : Odd n) (r : ) : nthRoot n r = SignType.sign r ^ n * abs r ^ (n⁻¹ : ) := if_neg <| Nat.not_even_iff_odd.mpr hnn:hn:Odd nr:hr✝:r 0hr:r < 0(-1) ^ n * (-r) ^ (↑n)⁻¹ = (-1) ^ n * (-r) ^ (↑n)⁻¹ All goals completed! 🐙n:r:hr✝:0 rho:Odd nhn0:n 0hr:0 < r1 ^ n * r ^ (↑n)⁻¹ = r ^ (↑n)⁻¹ All goals completed! 🐙All goals completed! 🐙n:r:h:n 0 0 r Odd nho:Odd nhn:n 0Odd (n * n) All goals completed! 🐙n:r:h:n 0 0 r Odd nho:Odd nhn:n 0Odd (n * n) All goals completed! 🐙theorem nthRoot_mul_of_even_of_nonneg {n : } {a b : } (hn : Even n) (ha : 0 a) (hb : 0 b) : Real.nthRoot n (a * b) = Real.nthRoot n a * Real.nthRoot n b := n:a:b:hn:Even nha:0 ahb:0 bnthRoot n (a * b) = nthRoot n a * nthRoot n b All goals completed! 🐙theorem nthRoot_mul_of_odd {n : } {a b : } (hn : Odd n) : nthRoot n (a * b) = nthRoot n a * nthRoot n b := n:a:b:hn:Odd nnthRoot n (a * b) = nthRoot n a * nthRoot n b n:a:b:hn:Odd n((SignType.sign a) * (SignType.sign b)) ^ n * (|a| ^ (↑n)⁻¹ * |b| ^ (↑n)⁻¹) = (SignType.sign a) ^ n * |a| ^ (↑n)⁻¹ * ((SignType.sign b) ^ n * |b| ^ (↑n)⁻¹) All goals completed! 🐙end Real