Código de PHP - Clase para operaciones con matrices

Versión 1

Publicado el 6 de Mayo del 2019gráfica de visualizaciones de la versión: Versión 1
3.287 visualizaciones desde el 6 de Mayo del 2019
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella


Forma parte de Matrix Operations Class
 
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
<?php
$r='';
$M1='';
$M2='';
if (isset($_POST['Submit'])){
	$M1=trim($_POST['m1']);
	$M2=trim($_POST['m2']);
	$m1=explode("\n",$M1);
	$m2=explode("\n",$M2);
	for ($i=0; $i<count($m1);$i++)
		$a[$i]=explode(',',$m1[$i]);
	for ($i=0; $i<count($m2);$i++)
		$b[$i]=explode(',',$m2[$i]);
	$op=$_POST['operation'];
	$mat=new matrix;
	switch ($op){
		case 'Divide':
			$b=$_POST['m2'];
			$rr=$mat->divide($a,$b);
			break;
		case 'Multiply':
			$rr=$mat->multiply($a,$b);
			break;
		case 'Add':
			$rr=$mat->add($a,$b);
			break;
		case 'Subtract':
			$rr=$mat->subtract($a,$b);
			break;
	}
 
	for ($i=0;$i<count($rr);$i++){
		for($j=0;$j<count($rr[$i]);$j++)
			$r.=$rr[$i][$j].',';
		$r=substr($r,0,strlen($r)-1)."\n";
	}
}
class matrix{
	public function multiply($m1,$m2){
		$r=count($m1);
		$c=count($m2[0]);
		$p=count($m2);
		if(count($m1[0])!=$p)die('Matrices incompatibles');
		for ($i=0;$i< $r;$i++){
			for($j=0;$j<$c;$j++){
				$m3[$i][$j]=0;
				for($k=0;$k<$p;$k++){
					$m3[$i][$j]+=$m1[$i][$k]*$m2[$k][$j];
				}
			}
		}
		return($m3);
	}
 
	public function add($m1,$m2){
		if (count($m1)!=count($m2) || count($m1[0])!=count($m2[0])) die('Matrices incompatibles');
		for ($i=0;$i<count($m1);$i++){
			for($j=0;$j<count($m1[0]);$j++){
				$m3[$i][$j]=$m1[$i][$j]+$m2[$i][$j];
			}
		}
	return($m3);
	}
 
	public function subtract($m1,$m2){
		if (count($m1)!=count($m2) || count($m1[0])!=count($m2[0])) die('Matrices incompatibles');
		for ($i=0;$i<count($m1);$i++){
			for($j=0;$j<count($m1[0]);$j++){
				$m3[$i][$j]=$m1[$i][$j]-$m2[$i][$j];
			}
		}
	return($m3);
	}
 
	public function divide($m1,$m2){	//$m2 must be num
		if (is_array($m2)) die ('Pleae enter e number for deviding not an array');
		for ($i=0;$i<count($m1);$i++){
			for($j=0;$j<count($m1[0]);$j++){
				$m3[$i][$j]=$m1[$i][$j]/$m2;
			}
		}
	return($m3);
	}
 
}
?>
 
<style>
* {font-family:'Times New Roman', Times, serif;}
form>div {text-align: center;}
form select {width: 140px; font-size: 14pt;}
form textarea {height: 174px; width: 237px; font-size: 14pt;}
form input[type=submit] {width: 95px; height: 48px; margin-top: 0px; font-size: 20pt;}
table {border: 1px solid #000000;width: 543px; margin:auto;}
table .matriz {padding: 5px;font-size:17pt;text-align: center; width: 269px; color: #2214B9;border-style: solid;border-width: 1px;}
table .matriz~td {padding: 5px;font-size:17pt;text-align: center; color: #2214B9;border-style: solid;border-width: 1px; width: 270px;}
table .opciones {padding: 5px; font-size: 20pt; text-align: center; color: #2214B9;border-style: solid;border-width: 1px; background-color: #D7E5FF;}
</style>
 
<form method="post">
    <div>
        <table>
            <tr>
                <td class="matriz" style="background-color: #FFFFD7;">
                <strong>Matrix 1</strong></td>
                <td style="background-color: #FFFFD7;">
                    <strong>&nbsp;&nbsp;
                    Matrix 2</strong></td>
            </tr>
            <tr>
                <td class="matriz" style="background-color: #D8FFD7;">
                    <textarea name="m1"><?php echo $M1?></textarea>
                </td>
                <td style="background-color: #D8FFD7;">
                    <textarea cols="20" name="m2" rows="1"><?php echo $M2?></textarea>
                </td>
            </tr>
            <tr>
                <td class="opciones" colspan="2">
                    <select name="operation">
                        <option selected="">Multiply</option>
                        <option>Divide</option>
                        <option>Add</option>
                        <option>Subtract</option>
                    </select>
                    <br><br>
                    <input name="Submit" type="submit" value="Do">
                </td>
            </tr>
        </table>
        <br>
        <?php
        if (isset($_POST['Submit'])) {
            ?>
            <span style=" font-size: 20pt; color: #1A7D16">
                Result of <?php echo $op?>ing:
            </span>
            <br>
            <textarea cols="20" name="TextArea3" rows="1" style="background-color: #FFD7F6;"><?php print_r($r)?></textarea>
            <?php
        }
        ?>
    </div>
</form>



Comentarios sobre la versión: Versión 1 (0)


No hay comentarios
 

Comentar la versión: Versión 1

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s5295