Showing posts with label square roots. Show all posts
Showing posts with label square roots. Show all posts

Sunday, May 21, 2023

Python: Square Root Simplification

 Python: Square Root Simplification



Introduction


The script SQFACTOR.py attempts to factor and simplify square root expressions in the form of either:


1.  √n

2.  √m + √n


The script was created using the TI-83 Plus Premium CE Python Edition, which is my first Python script using the French calculator.  




Python script:  sqfactor.py


from math import *

# basic code


def sqfactor(n):

  c=1

  k=2

  while k**2<n:

    while (n/k**2)-int(n/k**2)==0:

      n/=k**2

      c*=k

    k+=1

  return [c,n]


# 2 forms

print("Select: ")

print("1. sqrt(n)")

print("2. sqrt(m)+sqrt(n)")

ch=int(input())


if ch==1:

  n=float(input("n? "))

  l=sqfactor(n)

  print("sqrt("+str(n)+")=")

  print(str(l[0])+"*sqrt("+str(l[1])+")")


if ch==2:

  m=float(input("m? "))

  n=float(input("n? "))

  l0=sqfactor(m)

  l1=sqfactor(n)

  c=l0[0]

  d=l0[1]

  r=l1[0]

  s=l1[1]

  print("sqrt("+str(m)+") + sqrt("+str(n)+")")

  if d==s:

    print(str(c+r)+"*sqrt("+str(d)+")")

  else:

    print(str(c)+"*sqrt("+str(d)+")+")

    print(str(r)+"*sqrt("+str(s)+")")



Examples



Example 1:


√1640

Result:  2 * sqrt(410.0)


Example 2:


√56 + √78

Result:  2 * sqrt(14.0) + 1 * sqrt(78.0)


Example 3:


√75 + √27

Result:  8 * sqrt(3.0)



Eddie 


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 

Monday, November 8, 2021

Derivatives with Surprisingly Imaginary Results

Derivatives with Surprisingly Imaginary Results



Here are three derivatives of functions where complex numbers are involved with further algebraic simplification.  




d/dx √(a - x)^(1/2)



d/dx √(a- x)^(1/2)


= 1/2 ∙ (a - x)^(-1/2) ∙ -1


= -1/2 ∙ 1 ÷ (√(a - x))


Going a step further...


= -1/2 ∙ 1 ÷ (√(-1) ∙ √(x - a))


With √(-1) = i ,  1/i = -i


= i ÷ (2 ∙ √(x - a))



d/dx  arcsin(x + a)



d/dx arcsin(x + a)


= 1 ÷ √(1 - (x + a)^2)


= 1 ÷ √(1 - (x^2 + 2 ∙ a ∙ x + a^2))


= 1 ÷ √(-x^2 - 2 ∙ a ∙ x + 1 - a^2)


Factoring out -1 in the denominator: 


= 1 ÷ √((-1) ∙ (x^2 + 2 ∙ a ∙ x - 1 + a^2))


= 1 ÷ (i ∙ √(x^2 + 2 ∙ a ∙ x - 1 + a^2))


= -i ÷ √(x^2 + 2 ∙ a ∙ x - 1 + a^2)



d/dx e^(√(a - x)) 



d/dx e^(√(a - x)) 


= e^(√(a - x)) ∙ d/dx √(a - x)


= -e^(√(a - x)) ÷ (2 ∙ √(a - x))


With:  √(a - x) = i ∙ √(x - a) and e^(i ∙ Θ) = cos Θ + i ∙ sin Θ


= -e^(i ∙ √(x - a)) ÷ (2 ∙ i ∙ √(x - a))


= -e^(i ∙ √(x - a)) ÷ (2 ∙ i ∙ √(x - a))


=  i ∙ e^(i ∙ √(x - a)) ÷ (2 ∙ √(x - a))


= (i ∙ (cos √(x - a) + i ∙ sin √(x - a)) ÷ (2 ∙ √(x - a))


= (-sin √(x - a) + i ∙cos √(x - a)) ÷ (2 ∙ √(x - a))




Eddie


All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Saturday, November 17, 2018

TI-84+ and Casio (fx-CG 50) Micropython: Simplifying Nested Radicals

TI-84+ and Casio (fx-CG 50) Micropython: Simplifying Nested Radicals

Introduction 

The following program will simplifying the following expression:

√(x + y) = √a + √b

The input is x and y, with output a and b.  The following conditions are implied:  x > y and x > 0.

To see the derivation of simplification and examples, please check out this blog entry:

https://edspi31415.blogspot.com/2018/11/simplifying-nested-radicals.html

TI 84 Plus Program:  DENEST

"EWS 2018-11-12"
Disp "√(X+Y)=√(A)+√(B)","X>Y","X>0"
Prompt X,Y
X/2+1/2*√(X^2-Y^2)→A
X/2-1/2*√(X^2-Y^2) →B
√(A)→Z
round(Z,9)→Z
Disp " "

If Y<0 font="">

Then
If fPart(Z)=0
Then 
Disp √(A),"- √(",B,")"
Else 
Disp "√(",A,")- √(",B,")"
End

Else
If fPart(Z)=0
Then 
Disp √(A),"+ √(",B,")"
Else 
Disp "√(",A,")+ √(",B,")"
End

End

Casio Micropython (fx-CG 50) Script denest.py

Input is the form of:

√(x1 * √x2 + y1 * √y2) = √a + √b

import math
print("sqrt(x+y)=")
print("sqrt(a)+sqrt(b)")
print("x>y","x>0")

print(" ")
print("x1*sqrt(x2)")
x1=float(input("x1:"))
x2=float(input("x2:"))
x=x1*math.sqrt(x2)

print(" ")
print("y1*sqrt(y2)")
y1=float(input("y1:"))
y2=float(input("y2:"))
y=y1*math.sqrt(y2)

a=x/2+math.sqrt(x**2-y**2)
b=x/2-math.sqrt(x**2-y**2)
z=math.sqrt(a)
z=round(z,10)
a=round(a,10)
b=round(b,10)

if y<0: font="">
  if z-int(z)==0:
    print(z,"-sqrt(")
    print(b,"?")
  else:
    print("sqrt(",a)
    print("-sqrt(")
    print(b,")")
else:
  if z-int(z)==0:
    print(z,"+sqrt(")
    print(b,")")
  else:
    print("sqrt(",a)
    print("+sqrt(")
    print(b,")")


Source:

Michael J. Wester, Editor.  Computer Algebra Systems: A Practical Guide John Wiley & Sons: Chichester 1999.  ISBN 978-0-471-983538

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Monday, November 5, 2018

Simplifying Nested Radicals

Simplifying Nested Radicals

Introduction

A nested radical is a radical of the form:

√(x_0 + √(x_1 + √(x_2 + .... + √(x_k) ... )))

A expression of radicals (not necessarily square roots) that contains radicals or surds (unresolved n th roots).  Example of a nested radicals include:

√(8 +  √3)

√(8 +  2 * √3)

√(√8 +  √3)

√(√8 -  √3)

This blog entry will deal with the denesting the following:

√(x + y) and √(x - y) where x and/or y is a radical.  Let's assume that x > y and only principal roots are calculated. We're looking into a neat process that is used in computer algebra systems.

Derivation

We want to obtain the following and determine a and b. 

(I) √(x + y) = √a + √b

and

(II) √(x - y) = √a - √b


Start by squaring both sides of (I):

(√(x + y))^2 = (√a + √b)^2

(III) x + y = a + 2 * √(a*b) + b

(IV)  Set:
x = a + b
y = 2 * √(a*b)

(V)  Determine  x - y:

x - y = a + -1*2 * √(a*b) + b  (see IV)
x - y = a - 2 * √(a*b) + b
√(x - y) = √a - √b

(VI)

√(x + y) * √(x - y) = (√a + √b) * (√a - √b)
√((x + y) * (x - y)) = (√a)^2 - (√b)^2
√(x^2 - y^2) = a - b


We have the following simultaneous equations set up:

(VII)
a + b = x     (from (IV))
a - b = √(x^2 - y^2)   (from (VI))

Solving the system in (VII) for both a and b yield:

(VIII)
a = x/2 + 1/2 * √(x^2 - y^2)
b = x/2 - 1/2 * √(x^2 - y^2)

Substituting a and b back in (I) and (II):

(IX)

√(x + y) = √(x/2 + 1/2 * √(x^2 - y^2)) + √(x/2 - 1/2 * √(x^2 - y^2))

√(x - y) = √(x/2 + 1/2 * √(x^2 - y^2)) - √(x/2 - 1/2 * √(x^2 - y^2))

Examples

Example 1:  Simplify  √(8 + 2 * √15)

x = 8
y = 2 * √15

x^2 - y^2
= 8^2 - (2 * √15)^2
= 64 -  4 * 15
= 4

Then: 

√(x/2 + 1/2 * √(x^2 - y^2))
= √(4 + 1/2 * √4)
= √(4 + 1/2 * 2)
= √5    // √a

And:

√(x/2 - 1/2 * √(x^2 - y^2))
= √(4 - 1/2 * √4)
= √(4 - 1/2 * 2)
= √3     // √b

Hence:

√(8 + 2 * √15) = √5 + √3

Example 2:  Simplify  √(88 + 2 * √567)

x = 88
y = 2 * √567

x^2 - y^2
= 88^2 - (2 * √567)^2
= 7744 - 2268
= 5476

Note √5476 = 74

Then: 

√(x/2 + 1/2 * √(x^2 - y^2))
= √(44 + 1/2 * 74)
= √81
= 9        // √a

And:

√(x/2 + 1/2 * √(x^2 - y^2))
= √(44  - 1/2 * 74)
= √7      // √b

√(88 + 2 * √567) = 9 + √7


Example 3:  Simplify √(19 - 4 * √22))  (note the subtraction)

x = 19
y = 4 * √22

x^2 - y^2
= 19^2 - (4 * √22)^2
= 9

√(x/2 + 1/2 * √(x^2 - y^2))  = √11
√(x/2 + 1/2 * √(x^2 - y^2))  = √8 = 2 * √2

Hence:

√(19 - 4 * √22))  = √11 - 2 * √2


Neat algebra.

Source:

Michael J. Wester, Editor.  Computer Algebra Systems: A Practical Guide John Wiley & Sons: Chichester 1999.  ISBN 978-0-471-983538


Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Wednesday, August 15, 2018

Review: Casio fx-55 Plus

Review:  Casio fx-55 Plus






General Information

Company:  Casio
Type:  Fractions, Elementary Math
Memory:  1
Battery:  Solar with battery backup (1 LR44)
Years:  2012 - current
Cost:  $10.00 - $20.00, I paid $12.99
Operating Logic:  Algebraic

Target Audience:

* Elementary and middle school students
* Anyone who wants a few more functions than the regular four-function calculator, but not a scientific calculator
* You want a calculator with an emphasis on fractions

Features

The major emphasis on the Casio fx-55 Plus is on fractions.  We’ll discuss this in the next section in detail.

The screen has a textbook display, meaning fractions and all other calculations are displayed as you would write them.  Fractions are simplified.  If MathO (Math Output) is selected, square roots and terms of pi (π) shown in exact form.  If LineO is selected, all answers (except fractions) are shown in decimal form.  No matter what the display mode is, you can always decimal approximation by pressing [SHIFT] [ = ].

Example (MathO):

12 returns 2 3

5^2 * π returns 25π

You enter expressions the way you would write them with the Casio fx-55 Plus.  The display is a mutli-line display, the expression on the top of the screen and the answer is on the bottom of the screen. 

The keyboard is simple where only four keys have shifted functions.  The OFF function is shifted as well. 

Other functions include reciprocal, random numbers, including random integers, and calculations in Degrees-Minutes-Seconds [ ° ‘ “ ]. 

The memory has four dedicated keys: store, recall, add to memory (M+), and subtract from memory (M-).

The percent keys work a bit differently from the normal four-function calculators (and is not very intuitive). 

[%] divides the argument by 100 and that’s it.  Its fine when you are working with multiplying or dividing percentages, but adding and subtracting percent will take an algorithm:

X + Y%:  enter X + X * Y %
X – Y%:  enter X – X * Y %

[>%] multiplies the result by 100 and displays the answer in percentage notation. 

Example:  3 [ = ] [ SHIFT ] (>%) returns 300%

Let’s Talk Fractions

The fraction keys are marked lime green on the Casio fx-55 Plus.  Let’s go over the keys in detail:

[ []/[] ] (1st key from the left):  fraction template

[ [] []/[] ] (2nd key from the left):  mixed number template

[ F←→D] (3rd key from the left): switches the result between exact (fractions, exact square roots, terms of π) and decimal answer

[ a b/c ←→ d/c ]:  (4th key from the left)

Manual Simplified mode:  This key does nothing.

Auto Simplified mode: This key converts the result between improper and proper fraction notation. 

[ Simp ]:  calls the simplify conversion to the entry.  In Manual Simplified mode, any fraction that is not reduced to its irreducible form will have a down arrow indicator ( ).  Also executing Simp in Manual Simplified mode not only returns the reduced fraction, but the factor used into reducing such fraction.

[x^-1]:  The reciprocal function

I also want to point a very handy key, the remainder division function ( [÷R]), which is located on the bottom right hand corner of the keyboard.  This function returns both the quotient and remainder. 

Example:  77 ÷R 13 returns 5, R = 12 

Only the quotient is stored in last answer (Ans).

Let’s compare this Casio fx-55 Plus with an older fraction oriented calculator, the Texas Instruments Math Explorer (aka TI-12). 

Comparison:  Casio fx-55 Plus (2012-present) vs. Texas Instruments Math Explorer (1978-1998)



TI Math Explorer
Casio fx-55 Plus
1 line display
Multi-line, textbook display
No shifted functions
4 shifted functions
N/A
Degrees-Minutes Seconds calculations, random numbers and integers
Constant arithmetic storage function through the [ Cons ] key. 
N/A
10^n is limited to integers 0-7
10^x allows for all real numbers between up to, but not including, 100.  Used to work with numbers in scientific notation
Int÷ works with only positive integers
÷R works with real, positive numbers (any non-positive numbers makes ÷R work like ÷)

Verdict

I like this calculator for middle school and elementary students. The Casio fx-55 Plus presents a few extra functions but without the many scientific functions that may intimidate some students.  (emphasis on some)  I wish the percent would work like we would write expressions when it comes to addition or subtraction (i.e. X + Y% instead of X + X * Y%), but this is understandable.  I also wish the proper/improper toggle worked in Manual Simplified mode.

The display is great because the user can see the expression and answer at the same time.  I really like that this calculator also returns exact answers in calculations involving square roots and π. 

I can see this calculator being used beyond the classroom, especially in everyday basic math calculations. 

In any case, the Casio fx-55 Plus serves a great introductory calculator.

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Saturday, December 1, 2012

Numeric CAS Part 1: Simplifying Square Roots

Simplifying Square Roots

Goal: Simplify square roots of integers. For example, √180 = 6 √5, √80 = 4 √5

General Algorithm: Start with integer N and C=1. Starting with k=2, divide N by k². If N divides k² evenly, N is adjusted, C is multiplied by k, and the division is test is repeated. If N does not not divide k², k increases by 1 and the division test repeats. The division tests repeat until k² > N.

The program for the Casio Prizm, TI-84+, and HP 39gii are presented below:


Casio Prizm:

SQFACTOR
11/19/2012
Simplifies √N where N is an integer (i.e. √180 = 6 √5, √364 = 2 √91)
156 bytes

"√N="? → N
N → M
1 → C
2 → K
Do
Lbl 1
If Frac(N ÷ K²)=0
Then
N ÷ K² → N
C × K → C
Goto 1
IfEnd
K + 1 → K
LpWhile K²
ClrText
Locate 1,1,"√"
Locate 2,1,M
Locate 1,3,C
Locate 10,3,"×√"
Locate 12,3,N


TI-84+:

SQFACTOR
Square Root Simplification
(I.E. √180 = 6 √5 , √364 = 2 √91)
11/19/2012
133 bytes

Input "√(", N
N → M
1 → C
2 → K
Lbl 0
If fPart(N/K²)=0
Goto 1
1 + K → K
If K² < N
Goto 0
ClrHome
Output(1,1,"√(")
Output(1,3,M)
Output(3,1,C)
Output(3,7,"√(")
Output(3,9,N)
Stop
Lbl 1
N/K² → N
C*K → C
Goto 0


HP 39gii:

SQFACTOR
11/23/2012
Simplifies √N where N is an integer (i.e. √180 = 6 √5, √364 = 2 √91)

Input: SQFACTOR(N)

EXPORT SQFACTOR(N)
BEGIN
LOCAL C,K;
1 → C;
2 → K;
WHILE K² < N DO
WHILE FRAC(N/K²) == 0 DO
N/K² → N;
C*K→ C;
END;
K+1→K;
END;
RETURN string(C)+"√"+string(N);
END;




This blog is property of Edward Shore. 2012

Sunday, May 6, 2012

The Integral of ∫ √x * √(1-x) dx


I was recently asked by Mike Grigsby to integrate:

∫ √x √(1-x) dx

-----
Make the substitution:

x = sin^2 θ


Note:

√x = sin θ ,
θ = asin x ,
dx = 2 sin θ cos θ dθ ,
and
cos θ = √(1 - sin^2 θ )
-----

Hence:

∫ √x √(1-x) dx (x to θ)
= ∫ sin θ * √(1 - sin^2 θ )* 2 *sin θ * cos θ dθ
= 2 ∫ sin^2 θ cos^2 θ dθ
= 2 ∫ (1 - cos^2 θ ) * cos^2 θ dθ
= 2 ∫ cos^2 θ - cos^4 θ dθ
= 2 ∫ 1/8 - 1/8 * cos (4θ) dθ (See Note A)
= 2 ( θ/8 - 1/8 * 1/4* sin (4θ))
= 2 ( θ/8 - 1/32 * sin (4θ))
= 2 ( θ / 8 - 1/32 * [8 sin θ cos^3 θ - 4 sin θ cos θ]) (See Note B)
(θ to x)
= 2 ( asin √x / 8 - 1/32 * [8 √x (1 - x)^3/2 - 4 √x √(1-x)] )
= 2 ( asin √x / 8 - (√x √(1-x) (2 - 2x - 1))/8 )
= asin √x / 4 - (√x √(1-x) (1 - 2x)) / 4

Final:

∫ √x √(1-x) dx = 1/4 * ( asin √x - (√x √(1-x) (1 - 2x)) + C
for some constant C


-----
Note A

cos 2a = 2 cos^2 a - 1
cos^2 a = 1/2 * (cos 2a + 1)

cos^4 a
= [1/2 * (cos 2a + 1)]^2
= 1/4 * (cos^2 2a + 2 cos 2a + 1)
= 1/4 * ( (cos 4a + 1)/2 + 2 cos 2a + 1)
= 1/8 * (cos 4a + 4 cos 2a + 3)
= 1/8 * cos 4a + 1/2 * cos 2a + 3/8

cos^2 a - cos^4 a
= (1/2 * cos 2a + 1/2) - (1/8 * cos 4a + 1/2 * cos 2a + 3/8)
= 1/8 - 1/8 * cos 4a
-----

-----
Note B

sin 2a = 2 sin a cos a

sin 4a
= 2 sin 2a cos 2a
= 8 sin a cos^3 a - 4 sin a cos a

Using the triangle from above:

sin θ = √x
cos θ = √(1-x)
-----


This blog is property of Edward Shore. © 2012


DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues The programs are listed for the Swiss Micros DM42 an...