-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions_vectors.go
155 lines (136 loc) · 3.43 KB
/
functions_vectors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"strconv"
"strings"
)
func displayVector(vectors []string) {
count := len(vectors)
if count == 0 {
return
}
count--
fmt.Print("(")
for i := 0; i < count; i++ {
fmt.Print(vectors[i] + ", ")
}
fmt.Print(vectors[count])
fmt.Println(")")
}
func returnVector(vectors []string) string {
count := len(vectors)
result := ""
if count == 0 {
return ""
}
count--
result += "("
for i := 0; i < count; i++ {
result += vectors[i] + ", "
}
result += vectors[count]
result += ")"
return result
}
func splitVector(expression string) []string {
expression = strings.Replace(expression, "(", "", -1)
expression = strings.Replace(expression, ")", "", -1)
s := strings.Split(expression, ",")
return s
}
func addVectors(vector1 []string, vector2 []string) []string {
count := len(vector1)
if count != len(vector2) || count == 0 {
fmt.Println("Add Vectors: vectors are not compatible")
return []string{}
}
for i := 0; i < count; i++ {
v1, err1 := strconv.ParseFloat(vector1[i], 64)
v2, err2 := strconv.ParseFloat(vector2[i], 64)
if err1 == nil && err2 == nil {
vector1[i] = strconv.FormatFloat(v1+v2, 'f', -1, 64)
} else {
vector1[i] += "+" + vector2[i]
}
}
return vector1
}
func substractVectors(vector1 []string, vector2 []string) []string {
count := len(vector1)
if count != len(vector2) || count == 0 {
fmt.Println("Substract Vectors: vectors are not compatible")
return []string{}
}
for i := 0; i < count; i++ {
v1, err1 := strconv.ParseFloat(vector1[i], 64)
v2, err2 := strconv.ParseFloat(vector2[i], 64)
if err1 == nil && err2 == nil {
vector1[i] = strconv.FormatFloat(v1-v2, 'f', -1, 64)
} else {
vector1[i] += "-" + vector2[i]
}
}
return vector1
}
func homothetie(vector []string, lambda string) []string {
count := len(vector)
l, err2 := strconv.ParseFloat(lambda, 64)
for i := 0; i < count; i++ {
v1, err1 := strconv.ParseFloat(vector[i], 64)
if err1 == nil && err2 == nil {
vector[i] = strconv.FormatFloat(v1*l, 'f', -1, 64)
} else {
vector[i] += "*" + lambda
}
}
return vector
}
func handleExpressionParenthesisVectors(expression string, position int) ([]string, string) {
smallExpression := ""
count := len(expression)
positionLastParenthesis := count
for i := position + 1; i < count; i++ {
if expression[i] != ')' {
smallExpression += string(expression[i])
} else {
positionLastParenthesis = i
break
}
}
result := strings.Split(smallExpression, ",")
end := ""
for i := 0; i < position; i++ {
end += string(expression[i])
}
for i := positionLastParenthesis + 1; i < count; i++ {
end += string(expression[i])
}
return result, end
}
func handleParenthesisVectors(expression string) ([]string, string) {
open, openMax, lastOpenPosition, count := 0, 0, 0, len(expression)
for i := 0; i < count; i++ {
if expression[i] == '(' {
open++
if open > openMax {
openMax = open
lastOpenPosition = i
}
} else if expression[i] == ')' {
open--
}
}
return handleExpressionParenthesisVectors(expression, lastOpenPosition)
}
func removeVector(vectors [][]string, s int, t rune, numbers []string) [][]string {
if (s+1) < len(vectors) && t != '*' {
if t == '+' {
vectors[s] = addVectors(vectors[s], vectors[s+1])
} else if t == '-' {
vectors[s] = substractVectors(vectors[s], vectors[s+1])
}
return append(vectors[:s+1], vectors[s+2:]...)
}
vectors[s] = homothetie(vectors[s], numbers[0])
return vectors
}