假设我要在列向量和行向量之间进行矩阵乘法,该怎么做?
这里是乘法[1,2,3]^T*[1,2,3] =[[1,2,3],[2,4,6],[3,6,9]]
的示例。这是我尝试的方法:np.array([[i] for i in x]).dot(x)
,但不起作用。这里x是一个任意的numpy数字数组
答案 0 :(得分:1)
您可以使用numpy完成此操作
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.controller;
import com.dao.EmployeDAOLocal;
import com.model.Jeeprj;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author EKEUMOU
*/
@WebServlet(name = "JEEServlet", urlPatterns = {"/JEEServlet"})
public class JEEServlet extends HttpServlet {
@EJB
private EmployeDAOLocal employeDao;
boolean selected = true;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String theCommand = request.getParameter("action");
if (theCommand == null){
theCommand = "LIST";
}
switch(theCommand){
case "LIST":
case "Voir Liste":
listEmployes(request, response);
break;
case "Ajouter":
showFormAddEmploye(request, response);
break;
case "Modifier":
showFormModifyEmploye(request, response);
break;
case "Valider":
updateEmploye(request, response);
break;
case "Logout":
logOut(request, response);
break;
case "Supprimer":
deleteEmploye(request, response);
break;
case "Details":
loadEmploye(request,response);
break;
case "UPDATE_EMPLOYE":
updateEmploye(request,response);
break;
default:
listEmployes(request, response);
}
}
catch (IOException | ServletException exc) {
throw new ServletException(exc);
}
}
private void listEmployes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
String userName = (String) session.getAttribute("userName");
if (userName.equals("empl"))
listEmployesEmpl(request, response);
else {
request.setAttribute("allEmployes", employeDao.getAllEmployes());
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/list-employe.jsp");
dispatcher.forward(request, response);
}
}
private void listEmployesEmpl(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("allEmployes", employeDao.getAllEmployes());
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("WEB-INF/list-employe_empl.jsp");
dispatcher.forward(request, response);
}
private void showFormAddEmploye(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/add-employe-form.jsp");
dispatcher.forward(request, response);
}
private void addEmploye(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String nom = request.getParameter("nom");
String prenom = request.getParameter("prenom");
String telDom = request.getParameter("telDom");
String telPor = request.getParameter("telPor");
String telPro = request.getParameter("telPro");
String adresse = request.getParameter("adresse");
String codePostal = request.getParameter("codePostal");
String ville = request.getParameter("ville");
String email = request.getParameter("email");
Jeeprj theEmploye = new Jeeprj(nom, prenom, telDom, telPor, telPro, adresse, codePostal, ville, email);
employeDao.addEmploye(theEmploye);
listEmployes(request, response);
}
private void deleteEmploye(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String theEmployeStrId = request.getParameter("selectedEmploye");
int theEmployeId = 0;
//if(theEmployeStrId!=null && !theEmployeStrId.equals("")){
try{
theEmployeId=Integer.parseInt(theEmployeStrId);
request.getSession().setAttribute("result", true);
employeDao.deleteEmploye(theEmployeId);
}catch (NumberFormatException ex) {
request.getSession().setAttribute("result", false);
}
listEmployes(request, response);
}
private void loadEmploye(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
String theEmployeStrId = request.getParameter("selectedEmploye");
int theEmployeId = 0;
String userName = (String) session.getAttribute("userName");
if(theEmployeStrId != null && !theEmployeStrId.equals("")){
theEmployeId=Integer.parseInt(theEmployeStrId);
Jeeprj theEmploye = employeDao.getEmploye(theEmployeId);
session.setAttribute("THE_EMPLOYE", theEmploye);
if (userName.equals("admin")) {
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/details-employe-form.jsp");
dispatcher.forward(request, response);
}
else {
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/details-employe-form_empl.jsp");
dispatcher.forward(request, response);
}
}else{
if (userName.equals("admin"))
listEmployes(request, response);
else
listEmployesEmpl(request, response);
}
}
private void updateEmploye(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int employeId = Integer.parseInt(request.getParameter("employeId"));
String nom = request.getParameter("nom");
String prenom = request.getParameter("prenom");
String telDom = request.getParameter("telDom");
String telPor = request.getParameter("telPor");
String telPro = request.getParameter("telPro");
String adresse = request.getParameter("adresse");
String codePostal = request.getParameter("codePostal");
String ville = request.getParameter("ville");
String email = request.getParameter("email");
Jeeprj theEmploye = new Jeeprj(employeId, nom, prenom, telDom, telPor, telPro, adresse, codePostal, ville, email);
employeDao.editEmploye(theEmploye);
listEmployes(request, response);
}
private void showFormModifyEmploye(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*HttpSession session = request.getSession(true);
Jeeprj ub = (Jeeprj) session.getAttribute("THE_EMPLOYE");
session.setAttribute("THE_EMPLOYE", ub);*/
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/modify-employe-form.jsp");
dispatcher.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String theCommand = req.getParameter("action");
if (theCommand == null){
theCommand = "Login";
}
switch(theCommand){
case "Login":
logIn(req, resp);
break;
default:
logIn(req, resp);
}
}
catch (IOException | ServletException exc) {
throw new ServletException(exc);
}
}
protected void logIn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userName = req.getParameter("username");
String password = req.getParameter("password");
if (userName != null && userName.trim().length() > 0 &&
password != null && password.trim().length() > 0) {
System.out.println(userName + " : " + password);
if (userName != null && userName.length() != 0 && (userName.equals("admin") || userName.equals("empl")) &&
password != null && password.length() != 0 && (password.equals("admin") || password.equals("empl"))) {
if (req.getParameter("remember") != null) {
String remember = req.getParameter("remember");
System.out.println("remember : " + remember);
Cookie cookUserName = new Cookie("cookUserName", userName.trim());
Cookie cookPassword = new Cookie("cookPassword", password.trim());
Cookie cookRemember = new Cookie("cookRemember", remember.trim());
cookUserName.setMaxAge(60*60*24*3); // 3days
cookPassword.setMaxAge(60*60*24*3);
cookRemember.setMaxAge(60*60*24*3);
resp.addCookie(cookUserName);
resp.addCookie(cookPassword);
resp.addCookie(cookRemember);
}
HttpSession httpSession = req.getSession();
httpSession.setAttribute("sessionUser", userName.trim());
httpSession.setAttribute("userName", userName);
if (userName.equals("admin"))
listEmployes(req, resp);
else
listEmployesEmpl(req, resp);
} else {
System.out.println("Erreur d'Authentification");
req.setAttribute("message", "Erreur d'Authentification");
RequestDispatcher requestDispatcher = req.getRequestDispatcher("WEB-INF/login.jsp");
requestDispatcher.forward(req, resp);
}
} else {
System.out.println("Nom d'Utilisateur et Mot de Passe Requis !");
req.setAttribute("message", "Nom d'Utilisateur et Mot de Passe Requis !");
RequestDispatcher requestDispatcher = req.getRequestDispatcher("WEB-INF/login.jsp");
requestDispatcher.forward(req, resp);
}
}
private void logOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookUserName = new Cookie("cookUserName", null);
Cookie cookPassword = new Cookie("cookPassword", null);
Cookie cookRemember = new Cookie("cookRemember", null);
cookUserName.setMaxAge(0);
cookPassword.setMaxAge(0);
cookRemember.setMaxAge(0);
response.addCookie(cookUserName);
response.addCookie(cookPassword);
response.addCookie(cookRemember);
HttpSession httpSession = request.getSession();
httpSession.invalidate();
request.setAttribute("message", "Deconnexion réussie !");
RequestDispatcher requestDispatcher = request.getRequestDispatcher("WEB-INF/login.jsp");
requestDispatcher.forward(request, response);
}
}
命令import numpy as np
x = np.asarray([1,2,3])
matrix = x[:,None] * x[None,:]
使x[:,None]
1d数组变成(3,1)2d数组,反之亦然,x
反过来。 (3,1)和(1,3)矩阵之间的相乘产生一个(3,3)矩阵,其中相乘是逐元素进行的。两个阵列之间的x[None,:]
,np.matmul
或np.dot
运算符也将起作用。这基本上是您自己的方法的一种更有效的版本,如果您进行了以下稍微的修改,该方法就会起作用:
@
或者,您也可以使用爱因斯坦求和:
np.array([[i] for i in x]).dot([[x]])
语法'i,j-> ij'表示您想要一个矩阵,该矩阵的第一个轴具有与第一个数组相同的维数,而第二个轴具有与第二个数组具有相同的维数。
答案 1 :(得分:1)
您可以尝试
文档说明
行为通过以下方式取决于参数。