Procházet zdrojové kódy

First version of Gauss Jordan computation

Yentl Van Tendeloo před 8 roky
rodič
revize
0ae9db310c
1 změnil soubory, kde provedl 97 přidání a 0 odebrání
  1. 97 0
      integration/code/cbd_semantics.alc

+ 97 - 0
integration/code/cbd_semantics.alc

@@ -445,3 +445,100 @@ Void function execute_cbd(design_model : Element):
 				output("CONFORMANCE_FAIL " + conforming)
 		else:
 			log("Did not understand command: " + cmd)
+
+Void function eliminateGaussJordan(m : Element):
+	Integer i
+	Integer j
+	Integer f
+	Integer g
+	Boolean searching
+	Element t
+	Float divisor
+
+	i = 0
+	j = 0
+
+	while (i < read_nr_out(m)):
+		// Make sure pivot m[i][j] != 0, swapping if necessary
+		while (m[i][j] == 0):
+			// Is zero, so find row which is not zero
+			f = i + 1
+			searching = True
+			while (searching):
+				if (f >= read_nr_out(m)):
+					// No longer any rows left, so just increase column counter
+					searching = False
+					j = j + 1
+				else:
+					if (m[f][j] == 0):
+						// Also zero, so continue
+						f = f + 1
+					else:
+						// Found non-zero, so swap row
+						t = m[f]
+						dict_overwrite(m, f, m[i])
+						dict_overwrite(m, i, t)
+						searching = False
+			// If we have increased j, we will just start the loop again (possibly), as m[i][j] might be zero again
+
+		// Pivot in m[i][j] guaranteed to not be 0
+		// Now divide complete row by value of m[i][j] to make it equal 1
+		f = j
+		divisor = m[i][j]
+		while (f < read_nr_out(m[i])):
+			dict_overwrite(m[i], f, float_division(m[i][f], divisor))
+			f = f + 1
+
+		// Eliminate all rows in the j-th column, except the i-th row
+		f = 0
+		while (f < read_nr_out(m)):
+			if (bool_not(f == i)):
+				g = j
+				divisor = m[f][j]
+				while (g < read_nr_out(m[f])):
+					dict_overwrite(m[f], g, m[f][g] - (divisor * m[i][g]))
+
+		// Increase row and column
+		i = i + 1
+		j = j + 1
+
+	return !
+
+String function matrix2string(m : Element):
+	Integer i
+	Integer j
+	String result
+
+	result = ""
+	i = 0
+	while (i < read_nr_out(m)):
+		j = 0
+		while (j < read_nr_out(m[i])):
+			result = result + cast_v2s(m[i][j])
+			result = result + ", "
+		result = result + "\n"
+	return result!
+
+Void function main():
+	Element m
+	Element t
+	m = create_node()
+	t = create_node()
+	
+	list_append(t, -1)
+	list_append(t, 1)
+	list_append(t, -4)
+	list_append(m, t)
+
+	list_append(t, 1)
+	list_append(t, 1)
+	list_append(t, 0)
+	list_append(m, t)
+
+	log("Before elimination")
+	log(matrix2string(m))
+	eliminateGaussJordan(m)
+	log("After elimination")
+	log(matrix2string(m))
+
+	return !