matrixGenerator: Grundgerüst, beschreibung, code example, …
This commit is contained in:
parent
7311be67c5
commit
54d96a1315
Binary file not shown.
@ -0,0 +1,26 @@
|
||||
package generators.maths;
|
||||
|
||||
import generators.framework.Generator;
|
||||
import generators.framework.GeneratorBundle;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import algoanim.animalscript.AnimalScript;
|
||||
|
||||
public class DummyGenerator implements GeneratorBundle {
|
||||
|
||||
@Override
|
||||
public Vector<Generator> getGenerators() {
|
||||
Vector<Generator> generators = new Vector<Generator>(35, 15);
|
||||
generators.add(new AnnotatedSiebdesEratosthenes());
|
||||
generators.add(new FastBitcount());
|
||||
generators.add(new ErweiterterEuklid2());
|
||||
generators.add(new KreuzProd());
|
||||
generators.add(new MatrixMult());
|
||||
generators.add(new ModPow());
|
||||
generators.add(new PascalTriangle());
|
||||
generators.add(new MultiGenerator(new AnimalScript("", "", 0, 0)));
|
||||
generators.add(new MatrixGenerator(new AnimalScript("", "", 0, 0)));
|
||||
return generators;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,248 @@
|
||||
package generators.maths;
|
||||
|
||||
import generators.framework.Generator;
|
||||
import generators.framework.GeneratorType;
|
||||
import interactionsupport.models.FillInBlanksQuestionModel;
|
||||
import interactionsupport.models.QuestionGroupModel;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
|
||||
import algoanim.primitives.SourceCode;
|
||||
import algoanim.primitives.StringMatrix;
|
||||
import algoanim.primitives.generators.Language;
|
||||
import algoanim.properties.AnimationPropertiesKeys;
|
||||
import algoanim.properties.RectProperties;
|
||||
import algoanim.properties.SourceCodeProperties;
|
||||
import algoanim.properties.TextProperties;
|
||||
import algoanim.util.Coordinates;
|
||||
import algoanim.util.Offset;
|
||||
import algoanim.util.Timing;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.apache.commons.collections.functors.FalsePredicate;
|
||||
|
||||
import de.ahrgr.animal.kohnert.asugen.Font;
|
||||
import generators.framework.properties.AnimationPropertiesContainer;
|
||||
import algoanim.animalscript.AnimalScript;
|
||||
|
||||
|
||||
public class MatrixGenerator implements Generator {
|
||||
|
||||
/**
|
||||
* static variables -> simple testing
|
||||
*/
|
||||
static int[][] matrixA;
|
||||
static int[][] matrixB;
|
||||
static SourceCodeProperties SC_PROPS;
|
||||
static SourceCodeProperties TEXT_PROPS;
|
||||
static int HIGHLIGHTING_TIME = 100; // in ticks
|
||||
static int UNHIGHLIGHTING_TIME = 0; // in ticks
|
||||
// =========================================================================================================
|
||||
|
||||
/**
|
||||
* The concrete language object used for creating output
|
||||
*/
|
||||
private Language lang;
|
||||
|
||||
/**
|
||||
* random number generator for user interaction
|
||||
*/
|
||||
private Random rand = new Random();
|
||||
|
||||
/**
|
||||
* The info text as a SourceCode object (first slide)
|
||||
*/
|
||||
private SourceCode info = null;
|
||||
|
||||
/**
|
||||
* The statement text as a SourceCode object (last several slides)
|
||||
*/
|
||||
private SourceCode statement = null;
|
||||
|
||||
/**
|
||||
* The sourceCode
|
||||
*/
|
||||
private SourceCode sc = null;
|
||||
|
||||
// =========================================================================================================
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*
|
||||
* @param l
|
||||
* the concrete language object used for creating output
|
||||
*/
|
||||
public MatrixGenerator(Language language) {
|
||||
this.lang = new AnimalScript("Falksches Schema [DE]", "Ulf Gebhardt, Michael Scholz", 800, 600);
|
||||
|
||||
// This initializes the step mode. Each pair of subsequent steps has to
|
||||
// be divdided by a call of lang.nextStep();
|
||||
lang.setStepMode(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the information text for the first page in the animation
|
||||
*/
|
||||
public void generateInfoText() {
|
||||
|
||||
// create first page of animation (info text) as a code group
|
||||
this.info = lang.newSourceCode(new Coordinates(10, 75), "info", null, this.TEXT_PROPS);
|
||||
this.info.addCodeLine("Das hier vorgestellte Verfahren eignet sich fuer die Multiplikation zweier ganzer Zahlen.", null, 0, null);
|
||||
this.info.addCodeLine("Die Funktionsweise laesst sich in die folgenden fuenf Schritte gliedern:", null, 0, null);
|
||||
this.info.addCodeLine("1. Schreibe die beiden zu multiplizierenden Zahlen nebeneinander.", null, 1, null);
|
||||
this.info.addCodeLine("2. Die linke Zahl wird halbiert (Reste werden abgerundet), die rechte Zahl wird verdoppelt.", null, 1, null);
|
||||
this.info.addCodeLine(" Die beiden berechneten Zahlen werden in die darauffolgende Zeile geschrieben.", null, 2, null);
|
||||
this.info.addCodeLine("3. Schritt 2 wird solange wiederholt, bis in der linken Spalte eine 1 steht.", null, 1, null);
|
||||
this.info.addCodeLine("4. Nun streicht man alle Zeilen, in denen die linke Zahl gerade ist.", null, 1, null);
|
||||
this.info.addCodeLine("5. Schlussendlich werden alle uebrigen Zahlen der rechten Spalte addiert.", null, 1, null);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the header which is shown during the whole animation
|
||||
*/
|
||||
public void generateHeader() {
|
||||
|
||||
// head
|
||||
TextProperties textProperties = new TextProperties();
|
||||
textProperties.set(AnimationPropertiesKeys.COLOR_PROPERTY, Color.BLACK);
|
||||
textProperties.set(AnimationPropertiesKeys.DEPTH_PROPERTY, 1);
|
||||
textProperties.set(AnimationPropertiesKeys.FONT_PROPERTY, new java.awt.Font("Serif", Font.SANSSERIF, 24));
|
||||
lang.newText(new Coordinates(11, 15), "Russische Bauernmultiplikation", "header", null, textProperties);
|
||||
|
||||
// header background
|
||||
RectProperties rectProperties = new RectProperties();
|
||||
rectProperties.set(AnimationPropertiesKeys.FILL_PROPERTY, Color.GRAY);
|
||||
rectProperties.set(AnimationPropertiesKeys.FILLED_PROPERTY, true);
|
||||
rectProperties.set(AnimationPropertiesKeys.DEPTH_PROPERTY, 2);
|
||||
lang.newRect(new Offset(-5, -5, "header", AnimalScript.DIRECTION_NW), new Offset(5, 5, "header", AnimalScript.DIRECTION_SE), "headerBackground", null, rectProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the SourceCode
|
||||
*/
|
||||
public void generateSourceCode() {
|
||||
|
||||
// initialize source code object and add code lines
|
||||
this.sc = lang.newSourceCode(new Coordinates(10, 147), "sourceCode", null, this.SC_PROPS);
|
||||
this.sc.addCodeLine("public int russe(int a, int b){", null, 0, null); // 0
|
||||
this.sc.addCodeLine("if(a == 1){", null, 1, null); // 1
|
||||
this.sc.addCodeLine("return b;", null, 2, null); // 2
|
||||
this.sc.addCodeLine("}", null, 1, null); // 3
|
||||
this.sc.addCodeLine("if(a % 2 == 1){", null, 1, null); // 4
|
||||
this.sc.addCodeLine("return b + russe(a/2, b*2);", null, 2, null); // 5
|
||||
this.sc.addCodeLine("}else{", null, 1, null); // 6
|
||||
this.sc.addCodeLine("return russe(a/2, b*2);", null, 2, null); // 7
|
||||
this.sc.addCodeLine("}", null, 1, null); // 8
|
||||
this.sc.addCodeLine("}", null, 0, null); // 9
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Falksches Schema
|
||||
*
|
||||
* @param matrixA
|
||||
* @param matrixB
|
||||
*/
|
||||
public void falk(int[][] matrixA, int[][] matrixB){
|
||||
int[][] result = new int[matrixA.length][matrixB[0].length];
|
||||
int summe=0;
|
||||
for(int i=0; i<matrixA.length; i++){
|
||||
for( int j=0; j<matrixB[0].length; j++){
|
||||
for(int k=0; k<matrixA[0].length; k++){
|
||||
summe = summe + matrixA[i][k]*matrixB[k][j];
|
||||
result[i][j]=summe;
|
||||
}
|
||||
summe=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
lang = new AnimalScript(getName(), getAnimationAuthor(), 800, 600);
|
||||
lang.setStepMode(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generate(AnimationPropertiesContainer props, Hashtable<String, Object> primitives) {
|
||||
matrixA = (int[][]) primitives.get("matrixA");
|
||||
matrixB = (int[][]) primitives.get("matrixB");
|
||||
//SC_PROPS = (SourceCodeProperties) props.getPropertiesByName("Quelltext");
|
||||
//TEXT_PROPS = (SourceCodeProperties) props.getPropertiesByName("Text");
|
||||
|
||||
|
||||
lang.finalizeGeneration();
|
||||
return lang.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Falksches Schema [DE]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAlgorithmName() {
|
||||
return "Falksches Schema";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnimationAuthor() {
|
||||
return "Ulf Gebhardt, Michael Scholz";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Das hier vorgestellte Verfahren eignet sich zur Multiplikation zweier Matrizen. Zwei Matrizen kšnnen genau dann" +
|
||||
"multipliziert werden, wenn die Spaltenanzahl der linken Matrix mit der Zeilenanzahl der rechten Matrix" +
|
||||
"übereinstimmt. Das Verfahren lässt sich leicht iterativ implementieren." +
|
||||
"<br>Die Funktioneweise ist hierbei wie folgt:" +
|
||||
"<br>- Betrachte die i-te Zeile der linken Matrix und die i-te Spalte der rechten Matrix." +
|
||||
"<br>- Nun wird des j-te Element der i-ten Zeilen mit dem j-ten Element der i-ten Spalte multipliziert." +
|
||||
"<br>- Die einzelnen Ergebnisse der Multiplikationen werden addiert und man erhŠlt das Element e(ij) der Ergebnismatrix E.";
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCodeExample() {
|
||||
return "public1 int[][] falk(int[][] matrixA, int[][] matrixB){" +
|
||||
"\n int[][] result = new int[matrixA.length][matrixB[0].length];" +
|
||||
"\n int summe=0;" +
|
||||
"\n for(int i=0; i < matrixA.length; i++){" +
|
||||
"\n for( int j=0; j < matrixB[0].length; j++){" +
|
||||
"\n for(int k=0; k < matrixA[0].length; k++){" +
|
||||
"\n summe = summe + matrixA[i][k]*matrixB[k][j];" +
|
||||
"\n result[i][j]=summe;" +
|
||||
"\n }" +
|
||||
"\n summe=0;" +
|
||||
"\n }" +
|
||||
"\n }" +
|
||||
"\n return result;" +
|
||||
"\n}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileExtension() {
|
||||
return "asu";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale getContentLocale() {
|
||||
return Locale.GERMAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeneratorType getGeneratorType() {
|
||||
return new GeneratorType(GeneratorType.GENERATOR_TYPE_MATHS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOutputLanguage() {
|
||||
return Generator.JAVA_OUTPUT;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<PropertiesTreeModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="PropertiesTreeModel.xsd">
|
||||
<Folder name="Root">
|
||||
<Primitive type="intMatrix">
|
||||
<name>matrixA</name>
|
||||
<value><intMatrix>1, 2, 3, 4; 5, 6, 7, 8</intMatrix></value>
|
||||
</Primitive>
|
||||
<Primitive type="intMatrix">
|
||||
<name>matrixB</name>
|
||||
<value><intMatrix>1, 2, 3, 4; 5, 6, 7, 8</intMatrix></value>
|
||||
</Primitive>
|
||||
</Folder>
|
||||
</PropertiesTreeModel>
|
||||
Loading…
x
Reference in New Issue
Block a user