Skip to content

Commit

Permalink
SimSystem and SimTools update
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Herzog committed Nov 2, 2023
1 parent bf8c5e5 commit 6464bf0
Show file tree
Hide file tree
Showing 18 changed files with 334 additions and 87 deletions.
4 changes: 2 additions & 2 deletions SimSystem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.42.0.0</version>
<version>3.43.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
<dependency>
Expand Down Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>2.0.7</version>
<version>2.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.odftoolkit/simple-odf -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,22 @@ public boolean loadFromFile(File file) {
return true;
}

/**
* Erzeugt eine Wertetabelle für die Verteilung und kopiert diese in die Zwischenablage.
*/
public void copyTableOfValues() {
if (distribution==null) return;
DistributionTools.copyTableOfValues(distribution);
}

/**
* Erzeugt und speichert eine Wertetabelle für die Verteilung.
*/
public void saveTableOfValues() {
if (distribution==null) return;
DistributionTools.saveTableOfValues(this,distribution);
}

/**
* Wird die Liste gerade extern aktualisiert?
* (Dann soll {@link #itemStateChanged()} nicht ausgeführt werden.)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,23 @@ public class JDistributionPanel extends JPanel implements JGetImage {
public static String EditButtonLabelDisabled="Daten anzeigen";
/** Bezeichner für Tooltip für Dialogschaltfläche "Kopieren" */
public static String CopyButtonLabel="Kopieren";
/** Bezeichner für Dialogschaltfläche "Kopieren" */
public static String CopyButtonTooltip="Kopiert die Darstellung als Grafik in die Zwischenablage";
/** Bezeichner für Menüpunkt "Wertetabelle kopieren" im Kopieren-Menü */
public static String CopyButtonTable="Wertetabelle kopieren";
/** Bezeichner für Menüpunkt "Bild kopieren" im Kopieren-Menü */
public static String CopyButtonImage="Bild kopieren";
/** Bezeichner für Tooltip für Dialogschaltfläche "Speichern" */
/** Bezeichner für Dialogschaltfläche "Speichern" */
public static String SaveButtonLabel="Speichern";
/** Bezeichner für Tooltip für Dialogschaltfläche "Speichern" */
public static String SaveButtonTooltip="Speichert die Grafik als Bilddatei";
/** Bezeichner für Menüpunkt "Wertetabelle speichern" im Speichern-Menü */
public static String SaveButtonTable="Wertetabelle speichern";
/** Bezeichner für Menüpunkt "Bild kopieren" im Speichern-Menü */
public static String SaveButtonImage="Bild speichern";
/** Bezeichner für Dialogschaltfläche "Hilfe" */
public static String WikiButtonLabel="Hilfe";
/** Bezeichner für Tooltip für Dialogschaltfläche "Hilfe" */
public static String WikiButtonTooltip="Öffnet ein Browserfenster mit weiteren Informationen zu dem gewählten Verteilungstyp";
/** Untermenü zur Schnellauswahl des Verteilungstyps */
public static String ChangeDistributionType="Verteilungstyp";
/** Informationstext in der Verteilungsanzeige zur Veränderung des Verteilungstyps */
public static String ChangeDistributionTypeHighlightList="Hier werden nur die hervorgehobenen Verteilungen dargestellt. Eine vollständige Liste steht im Bearbeiten-Dialog zur Auswahl zur Verfügung.";
/** Bezeichner "Dichte" */
Expand Down Expand Up @@ -236,12 +243,10 @@ public void mouseClicked(MouseEvent e) {
});

copy=new JButton(CopyButtonLabel);
copy.setToolTipText(CopyButtonTooltip);
copy.addActionListener(e->actionCopy());
copy.setIcon(SimSystemsSwingImages.COPY.getIcon());

save=new JButton(SaveButtonLabel);
save.setToolTipText(SaveButtonTooltip);
save.addActionListener(e->actionSave());
save.setIcon(SimSystemsSwingImages.SAVE.getIcon());

Expand Down Expand Up @@ -492,6 +497,22 @@ public void setImageSaveSize(int imageSize) {
this.imageSize=imageSize;
}

/**
* Erzeugt eine Wertetabelle für die Verteilung und kopiert diese in die Zwischenablage.
*/
public void copyTableOfValues() {
if (distribution==null) return;
DistributionTools.copyTableOfValues(distribution);
}

/**
* Erzeugt und speichert eine Wertetabelle für die Verteilung.
*/
public void saveTableOfValues() {
if (distribution==null) return;
DistributionTools.saveTableOfValues(this,distribution);
}

/**
* Eigentlicher Funktionsplotter innerhalb des Gesamt-Panels
*/
Expand Down Expand Up @@ -841,15 +862,24 @@ protected boolean editButtonClicked() {
* @see #copy
*/
private void actionCopy() {
copyImageToClipboard(getToolkit().getSystemClipboard(),imageSize);
final JPopupMenu menu=new JPopupMenu();
JMenuItem item;

menu.add(item=new JMenuItem(CopyButtonTable,SimSystemsSwingImages.COPY_AS_TABLE.getIcon()));
item.addActionListener(e->copyTableOfValues());

menu.add(item=new JMenuItem(CopyButtonImage,SimSystemsSwingImages.COPY_AS_IMAGE.getIcon()));
item.addActionListener(e->copyImageToClipboard(getToolkit().getSystemClipboard(),imageSize));

menu.show(copy,0,copy.getHeight());
}

/**
* Speichern-Aktion auslösen
* @see #save
* Speichert die aktuelle Verteilung als Bild.
*/
private void actionSave() {
File file=getSaveFileName(); if (file==null) return;
private void saveImage() {
final File file=getSaveFileName();
if (file==null) return;
if (file.exists()) {
if (JOptionPane.showConfirmDialog(JDistributionPanel.this,String.format(GraphicsFileOverwriteWarning,file.toString()),GraphicsFileOverwriteWarningTitle,JOptionPane.YES_NO_OPTION)!=JOptionPane.YES_OPTION) return;
}
Expand All @@ -861,6 +891,23 @@ private void actionSave() {
saveImageToFile(file,extension,imageSize);
}

/**
* Speichern-Aktion auslösen
* @see #save
*/
private void actionSave() {
final JPopupMenu menu=new JPopupMenu();
JMenuItem item;

menu.add(item=new JMenuItem(SaveButtonTable,SimSystemsSwingImages.COPY_AS_TABLE.getIcon()));
item.addActionListener(e->copyTableOfValues());

menu.add(item=new JMenuItem(SaveButtonImage,SimSystemsSwingImages.COPY_AS_IMAGE.getIcon()));
item.addActionListener(e->saveImage());

menu.show(save,0,save.getHeight());
}

/**
* Wikipedia-Seite zu der gewählten Verteilung öffnen
* @see #wiki
Expand All @@ -885,7 +932,7 @@ private void buildQuickEdit(final JPopupMenu popup, final JDistributionEditorPan
final String[] distNames=JDistributionEditorPanel.getHighlightedDistributions();
if (distNames.length>0) {
final List<JDistributionEditorPanelRecord> records=JDistributionEditorPanelRecord.getList(null,false,false);
final JMenu typeItem=new JMenu("Verteilungstyp");
final JMenu typeItem=new JMenu(ChangeDistributionType);

popup.add(typeItem);
for (String distName: distNames) {
Expand Down Expand Up @@ -958,6 +1005,7 @@ public void keyReleased(KeyEvent e) {
*/
private void showContextMenu(final MouseEvent e, final boolean showEditButton) {
final JPopupMenu popup=new JPopupMenu();
JMenu sub;
JMenuItem item;

if (showEditButton && distribution!=null) {
Expand All @@ -966,11 +1014,20 @@ private void showContextMenu(final MouseEvent e, final boolean showEditButton) {
popup.addSeparator();
}

popup.add(item=new JMenuItem(copy.getText(),copy.getIcon()));
item.addActionListener(ev->actionCopy());
popup.add(sub=new JMenu(copy.getText()));
sub.setIcon(copy.getIcon());
sub.add(item=new JMenuItem(CopyButtonTable,SimSystemsSwingImages.COPY_AS_TABLE.getIcon()));
item.addActionListener(ev->copyTableOfValues());
sub.add(item=new JMenuItem(CopyButtonImage,SimSystemsSwingImages.COPY_AS_IMAGE.getIcon()));
item.addActionListener(ev->copyImageToClipboard(getToolkit().getSystemClipboard(),imageSize));

popup.add(item=new JMenuItem(save.getText(),save.getIcon()));
popup.add(sub=new JMenu(save.getText()));
sub.setIcon(save.getIcon());
item.addActionListener(ev->actionSave());
sub.add(item=new JMenuItem(SaveButtonTable,SimSystemsSwingImages.COPY_AS_TABLE.getIcon()));
item.addActionListener(ev->copyTableOfValues());
sub.add(item=new JMenuItem(SaveButtonImage,SimSystemsSwingImages.COPY_AS_IMAGE.getIcon()));
item.addActionListener(ev->saveImage());

popup.add(item=new JMenuItem(wiki.getText(),wiki.getIcon()));
item.addActionListener(ev->actionWiki());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package mathtools.distribution.tools;

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.stream.Stream;
Expand All @@ -24,8 +28,11 @@
import org.apache.commons.math3.distribution.AbstractRealDistribution;

import mathtools.NumberTools;
import mathtools.Table;
import mathtools.distribution.AbstractDiscreteRealDistribution;
import mathtools.distribution.DataDistributionImpl;
import mathtools.distribution.NeverDistributionImpl;
import mathtools.distribution.swing.JDistributionPanel;

/**
* Liefert verschiedene Informationen zu kontinuierlichen Verteilungen
Expand Down Expand Up @@ -543,7 +550,11 @@ public static boolean canSetMeanExact(final AbstractRealDistribution distributio
*/
public static AbstractRealDistribution setMean(final AbstractRealDistribution distribution, final double value) {
final AbstractDistributionWrapper wrapper=getWrapper(distribution);
if (wrapper!=null) return wrapper.setMean(distribution,value);
if (wrapper!=null) try {
return wrapper.setMean(distribution,value);
} catch(Exception e) {
return null;
}
return null;
}

Expand Down Expand Up @@ -611,7 +622,11 @@ public static boolean canSetStandardDeviationExactIndependent(final AbstractReal
*/
public static AbstractRealDistribution setStandardDeviation(final AbstractRealDistribution distribution, final double value) {
final AbstractDistributionWrapper wrapper=getWrapper(distribution);
if (wrapper!=null) return wrapper.setStandardDeviation(distribution,value);
if (wrapper!=null) try {
return wrapper.setStandardDeviation(distribution,value);
} catch(Exception e) {
return null;
}
return null;
}

Expand Down Expand Up @@ -764,4 +779,71 @@ public static AbstractRealDistribution normalizeDistribution(final AbstractRealD
}
return distribution;
}

/**
* Erzeugt eine Wertetabelle für eine Verteilung
* @param distribution Verteilung
* @return Wertetabelle
*/
public static Table getTableOfValues(final AbstractRealDistribution distribution) {
final Table table=new Table();

if (distribution instanceof AbstractDiscreteRealDistribution) {
/* Diskrete Verteilung */
table.addLine(new String[]{"k","P(X=k)","P(X<=k)"});
double sumLast=0;
for (int k=0;k<10_000;k++) {
final double sum=((AbstractDiscreteRealDistribution)distribution).cumulativeProbability(k);
table.addLine(new String[]{
""+k,
NumberTools.formatNumberMax(sum-sumLast),
NumberTools.formatNumberMax(sum)
});
sumLast=sum;
if (sum>0.999) break;
}
} else {
/* Kontinuierliche Verteilung */
table.addLine(new String[]{"x","f(x)","F(x)=P(X<=x)"});
final double min=Math.max(-10_000,distribution.getSupportLowerBound());
final double max=Math.min(10_000,distribution.getSupportUpperBound());
final double step=0.1;
double x=min;
while (x<=max) {
final double f=distribution.density(x);
final double F=distribution.cumulativeProbability(x);
if (F>=0.001) table.addLine(new String[]{
NumberTools.formatNumberMax(x),
NumberTools.formatNumberMax(f),
NumberTools.formatNumberMax(F)
});
if (F>0.999) break;
x+=step;
x=Math.round(x*1000)/1000.0;
}
}

return table;
}

/**
* Erzeugt eine Wertetabelle für eine Verteilung und kopiert diese in die Zwischenablage.
* @param distribution Verteilung
* @see #getTableOfValues(AbstractRealDistribution)
*/
public static void copyTableOfValues(final AbstractRealDistribution distribution) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(getTableOfValues(distribution).toString()),null);
}

/**
* Erzeugt und speichert eine Wertetabelle für eine Verteilung.
* @param parent Übergeordnetes Element (zur Ausrichtung des Dialogs)
* @param distribution Verteilung
* @see #getTableOfValues(AbstractRealDistribution)
*/
public static void saveTableOfValues(final Component parent, final AbstractRealDistribution distribution) {
final File file=Table.showSaveDialog(parent,JDistributionPanel.SaveButtonTable);
if (file==null) return;
getTableOfValues(distribution).save(file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.commons.math3.util.FastMath;

import mathtools.NumberTools;
import mathtools.distribution.OnePointDistributionImpl;

/**
* Zusätzliche Daten für ein Objekt vom Typ {@link GammaDistribution}
Expand Down Expand Up @@ -89,7 +90,7 @@ protected AbstractRealDistribution setMeanInt(AbstractRealDistribution distribut
final double shape=((GammaDistribution)distribution).getShape();
final double scale=((GammaDistribution)distribution).getScale();
final double var=shape*scale*scale;
final double shapeNew=mean*mean/Math.max(var,0.00001);
final double shapeNew=(var>0)?(mean*mean/Math.max(var,0.00001)):10E-10;
final double scaleNew=mean/Math.max(shapeNew,0.00001);
return new GammaDistribution(shapeNew,scaleNew);
}
Expand All @@ -105,7 +106,10 @@ protected AbstractRealDistribution setStandardDeviationInt(AbstractRealDistribut
final double shape=((GammaDistribution)distribution).getShape();
final double scale=((GammaDistribution)distribution).getScale();
final double E=shape*scale;
final double shapeNew=E*E/(sd*sd);

if (sd<=0.0) return new OnePointDistributionImpl(E);

final double shapeNew=(sd>0)?(E*E/(sd*sd)):10E-10;
final double scaleNew=E/Math.max(shapeNew,0.00001);
return new GammaDistribution(shapeNew,scaleNew);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public final boolean setParameter(CalcSymbol[] symbols) {
}

@Override
public final Object getSimplify() {
public Object getSimplify() {
if (left==null || right==null) return this;
Object l=left.getSimplify();
Object r=right.getSimplify();
Expand All @@ -132,18 +132,27 @@ public final Object getSimplify() {
CalcSymbolNumber num=new CalcSymbolNumber();
num.setValue((Double)l);
clone.left=num;
clone.right=right.cloneSymbol();
clone.right=(r instanceof CalcSymbol)?((CalcSymbol)r):right.cloneSymbol();
return clone;
}
if (r instanceof Double) {
CalcSymbolMiddleOperator clone;
try {clone=(CalcSymbolMiddleOperator)clone();} catch (CloneNotSupportedException e) {return null;}
CalcSymbolNumber num=new CalcSymbolNumber();
num.setValue((Double)r);
clone.left=left.cloneSymbol();
clone.left=(l instanceof CalcSymbol)?((CalcSymbol)l):left.cloneSymbol();
clone.right=num;
return clone;
}

if ((l instanceof CalcSymbol) && (r instanceof CalcSymbol)) {
CalcSymbolMiddleOperator clone;
try {clone=(CalcSymbolMiddleOperator)clone();} catch (CloneNotSupportedException e) {return null;}
clone.left=(CalcSymbol)l;
clone.right=(CalcSymbol)r;
return clone;
}

return this;
}

Expand Down
Loading

0 comments on commit 6464bf0

Please sign in to comment.