Skip to content

Commit

Permalink
compute the function for 3 variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ebouJ committed Aug 2, 2018
1 parent 56f4d13 commit 3d1868b
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/lib/isValid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (a) => {
return !( a && !isNaN(a))
}
33 changes: 32 additions & 1 deletion src/lib/threeVariables.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
import Toast from 'react-native-simple-toast'
export default (matrix: number[]) => {
const roots = []

// replace the respective column
const xMatrix = formMatrixColumn(matrix,0)
const yMatrix = formMatrixColumn(matrix,1)
const zMatrix = formMatrixColumn(matrix,2)

const determinant = Determinant(matrix)
const xdeterminant = Determinant(xMatrix)
const ydeterminant = Determinant(yMatrix)
const zdeterminant = Determinant(zMatrix)

if( determinant == 0) {
Toast.showWithGravity('There is no solution.', Toast.SHORT, Toast.CENTER)
} else {
roots.push(xdeterminant/determinant)
roots.push(ydeterminant/determinant)
roots.push(zdeterminant/determinant)
}
return roots
}

function Determinant(matrix: number[]) {
return ( (matrix[0][0] * matrix[1][1] * matrix[2][2]) + (matrix[0][1] * matrix[1][2] * matrix[2][0]) + (matrix[0][2] * matrix[1][0] * matrix[2][1]))
- ( (matrix[2][0] * matrix[1][1] * matrix[0][2]) + (matrix[2][1] * matrix[1][2] * matrix[0][0]) + (matrix[2][2] * matrix[1][0] * matrix[0][1]))
}

function formMatrixColumn(matrix: number[], index: number){
let clone = JSON.parse(JSON.stringify(matrix))

return roots
clone.forEach(item => {
item[index] = item[3]
})

console.log(clone)


return clone
}
5 changes: 3 additions & 2 deletions src/views/example/cubic-equation/cubic-equation-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { color } from "../../../theme"
import { Header } from '../../shared/header'
import { TextField } from '../../shared/text-field'
import { Button } from '../../shared/button'
import isNotValid from '../../../lib/isValid'
import { NavigationScreenProps } from "react-navigation"
import SolveCubic from '../../../lib/cubic'

Expand Down Expand Up @@ -94,7 +95,7 @@ export class CubicEquation extends React.Component<CubicEquationScreenProps, Sta

solve = async () => {
const { a, b , c , d } = this.state
if(isNaN(a) || isNaN(b) || isNaN(c) || isNaN(d)){
if(isNotValid(a) || isNotValid(b) || isNotValid(c) || isNotValid(d)){
return;
}

Expand Down Expand Up @@ -175,7 +176,7 @@ export class CubicEquation extends React.Component<CubicEquationScreenProps, Sta
<Button preset="solve" text="Clear" onPress={this.clear} />
</View>
</View>
<View style={{ justifyContent: 'space-around', alignItems: 'center', flex:0.2}}>
<View style={{ justifyContent: 'space-around', alignItems: 'center', flex: 0.2}}>
{
roots && roots.map((item, index) => {
const ind = index + 1
Expand Down
3 changes: 2 additions & 1 deletion src/views/example/linear-equation/linear-equation-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { color } from "../../../theme"
import Toast from 'react-native-simple-toast'
import { Header } from '../../shared/header'
import { Button } from '../../shared/button'
import isNotValid from '../../../lib/isValid'
import { TextField } from '../../shared/text-field'
import { NavigationScreenProps } from "react-navigation"

Expand Down Expand Up @@ -94,7 +95,7 @@ export class LinearEquation extends React.Component<LinearEquationScreenProps, S

solve = async () => {
const { a, b, c } = this.state
if(isNaN(a) || isNaN(b) || isNaN(c) ){
if(isNotValid(a) || isNotValid(b) || isNotValid(c) ){
Toast.showWithGravity('The input should be a number.', Toast.SHORT, Toast.CENTER)
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Toast from 'react-native-simple-toast'
import { Button } from '../../shared/button'
import { TextField } from '../../shared/text-field'
import { Header } from '../../shared/header'
import isNotValid from '../../../lib/isValid'
import { NavigationScreenProps } from "react-navigation"
import QuadracticSolve from '../../../lib/quadratic'

Expand Down Expand Up @@ -95,9 +96,9 @@ export class QuadraticEquation extends React.Component<QuadraticEquationScreenPr
state = initialState
solve = async () => {
const { a, b , c } = this.state
if(isNaN(a) || isNaN(b) || isNaN(c)){
if(isNotValid(a) || isNotValid(b) || isNotValid(c)){
Toast.showWithGravity('The input should be a number.', Toast.SHORT, Toast.CENTER)
return
return
}


Expand All @@ -106,6 +107,7 @@ export class QuadraticEquation extends React.Component<QuadraticEquationScreenPr

}


clear = () => {
this.setState(initialState)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import * as React from "react"
import { ViewStyle, TextStyle, View , Dimensions} from "react-native"
import { Text } from "../../shared/text"
import { color } from "../../../theme"
import Toast from 'react-native-simple-toast'
import { Header } from '../../shared/header'
import { Button } from '../../shared/button'
import { TextField } from '../../shared/text-field'
import { NavigationScreenProps } from "react-navigation"
import isNotValid from '../../../lib/isValid'
import ThreeVariableSolver from '../../../lib/threeVariables'

export interface ThreeLinearEquationScreenProps extends NavigationScreenProps<{}> {
}
Expand Down Expand Up @@ -47,10 +50,70 @@ const ButtonView: ViewStyle = {
margin: 20,
justifyContent: 'space-between'
}
interface State {
x1: string,
y1: string,
z1: string,
w1: string,
x2: string,
y2: string,
z2: string,
w2: string,
x3: string,
y3: string,
z3: string,
w3: string,
roots: string[]
}


const initialState = {
x1: null,
y1: null,
z1: null,
w1: null,
x2: null,
y2: null,
z2: null,
w2: null,
x3: null,
y3: null,
z3: null,
w3: null,
roots: null
}

const variable = {
0: "x",
1: "y",
2: "z"
}
export class ThreeLinearEquation extends React.Component<ThreeLinearEquationScreenProps, State> {
state = initialState

solve = async () => {
const matrix = []
const { x1, x2, x3, y1, y2, y3, z1, z2, z3, w1, w2, w3 } = this.state
if(isNotValid(x1) || isNotValid(x2) || isNotValid(x3) || isNotValid(y1) || isNotValid(y2) || isNotValid(y3) || isNotValid(z1) || isNotValid(z2) || isNotValid(z3) || isNotValid(w1) || isNotValid(w2) || isNotValid(w3)){
Toast.showWithGravity('The input should be a number.', Toast.SHORT, Toast.CENTER)
return
}

matrix.push([Number(x1),Number(y1),Number(z1),Number(w1)])
matrix.push([Number(x2),Number(y2),Number(z2),Number(w2)])
matrix.push([Number(x3),Number(y3),Number(z3),Number(w3)])
const roots = await ThreeVariableSolver(matrix)
this.setState({ roots })

}

clear = () => {
this.setState(initialState)
}

export class ThreeLinearEquation extends React.Component<ThreeLinearEquationScreenProps, {}> {
render () {
const { goBack } = this.props.navigation
const { x1, x2, x3, y1, y2, y3, z1, z2, z3, w1, w2, w3, roots } = this.state
return (
<View style={ROOT}>
<Header
Expand All @@ -60,87 +123,118 @@ export class ThreeLinearEquation extends React.Component<ThreeLinearEquationScre
leftIcon="chevron-left"
onLeftPress={() => goBack()}
/>
<View style={{flex: 0.6, justifyContent: 'space-around'}} >
<View style={{flex: 0.7, justifyContent: 'space-around'}} >
<View style={InputView}>
<TextField
style={Input}
value={x1}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={x1 => this.setState({ x1})}
/>
<Text style={textStyle}> x +</Text>
<TextField
style={Input}
value={y1}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={y1 => this.setState({y1})}
/>
<Text style={textStyle}> y +</Text>
<TextField
style={Input}
value={z1}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={z1 => this.setState({z1})}
/>
<Text style={textStyle}> z =</Text>
<TextField
style={Input}
value={w1}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={w1 => this.setState({w1})}
/>
</View>
<View style={InputView}>
<TextField
style={Input}
value={x2}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={x2 => this.setState({ x2})}
/>
<Text style={textStyle}> x +</Text>
<TextField
style={Input}
value={y2}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={y2 => this.setState({y2})}
/>
<Text style={textStyle}> y +</Text>
<TextField
style={Input}
value={z2}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={z2 => this.setState({z2})}
/>
<Text style={textStyle}> z =</Text>
<TextField
style={Input}
value={w2}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={w2 => this.setState({w2})}
/>
</View>
<View style={InputView}>
<TextField
style={Input}
value={x3}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={x3 => this.setState({x3})}
/>
<Text style={textStyle}> x +</Text>
<TextField
style={Input}
value={y3}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={y3 => this.setState({y3})}
/>
<Text style={textStyle}> y +</Text>
<TextField
style={Input}
value={z3}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={z3 => this.setState({z3})}
/>
<Text style={textStyle}> z =</Text>
<TextField
style={Input}
value={w3}
inputStyle={inputStyle}
keyboardType={'numeric'}
onChangeText={w3 => this.setState({w3})}
/>
</View>
<View style={ButtonView}>
<Button preset="solve" text="Solve" />
<Button preset="solve" text="Clear" />
<Button preset="solve" text="Solve" onPress={this.solve} />
<Button preset="solve" text="Clear" onPress={this.clear} />
</View>
</View>
<View style={{ justifyContent: 'center', alignItems: 'center', flex: 0.3}}>
{
roots && roots.map((item, index) => {
return <Text key={index} style={textStyle}>{variable[index] + " = " + item}</Text>
})
}
</View>
</View>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Header } from '../../shared/header'
import { Button } from '../../shared/button'
import Toast from 'react-native-simple-toast'
import { TextField } from '../../shared/text-field'
import isNotValid from '../../../lib/isValid'
import { NavigationScreenProps } from "react-navigation"
import TwoVariableSolver from '../../../lib/twoVariables'

Expand Down Expand Up @@ -80,7 +81,7 @@ export class TwoLinearEquation extends React.Component<TwoLinearEquationScreenPr
state = initialState
solve = async () => {
const { x1, x2 , y1, y2, z1, z2 } = this.state
if(isNaN(x1) || isNaN(x2) || isNaN(y1) || isNaN(y2) || isNaN(z2) || isNaN(z1)){
if(isNotValid(x1) || isNotValid(x2) || isNotValid(y1) || isNotValid(y2) || isNotValid(z2) || isNotValid(z1)){
Toast.showWithGravity('The input should be a number.', Toast.SHORT, Toast.CENTER)
return
}
Expand Down

0 comments on commit 3d1868b

Please sign in to comment.