Movatterモバイル変換


[0]ホーム

URL:


Bangumi 番组计划
登录注册

番组开发 »讨论
[组件/脚本] 代码高亮 / 一键复制


#1 - 2024-11-9 21:24
试下昵称中间能 不能加 空(Hello darkness my old friend)
预览


功能
- 使用highlight.js 自动检测代码块语言并高亮
- 添加悬浮显示的一键复制按钮
- 支持夜间模式
- 兼容性
  - 兼容bangumi 僞質感設計样式
  - 大概率不兼容代码块超进化!

链接
组件
Greasy Fork
Greasy Fork 镜像站

更新日志
1.7 无悬浮功能设备和键盘操作的按钮直接显示,窄滚动条
1.5 增加作用于短信页
1.3 修复行高;设置 plaintext
1.2 限制最大高度
1.1 删去难绷的识别语言显示,修改为悬浮显示复制按钮


示例
import numpy as np
import torch

def solve_block_tridiagonal(a, b, c, d):

    N = len(b)
    x = np.zeros_like(d)
   
    # Forward elimination with explicit C* and d* storage
    C_star = np.zeros_like(c)
    d_star = np.zeros_like(d)

    # Initial calculations for C_0* and d_0*
    C_star[0] = np.linalg.solve(b[0], c[0])
    d_star[0] = np.linalg.solve(b[0], d[0])

    # Forward elimination
    for i in range(1, N - 1):
        C_star[i] = np.linalg.solve(b[i] - a[i-1] @ C_star[i-1], c[i])
        d_star[i] = np.linalg.solve(b[i] - a[i-1] @ C_star[i-1], d[i] - a[i-1] @ d_star[i-1])

    # Last d_star update for the last block
    d_star[-1] = np.linalg.solve(b[-1] - a[-2] @ C_star[-2], d[-1] - a[-2] @ d_star[-2])

    # Backward substitution
    x[-1] = d_star[-1]
    for i in range(N-2, -1, -1):
        x[i] = d_star[i] - C_star[i] @ x[i+1]

    return x


def test_block_tridiagonal_solver():

    N = 4

    a = np.array([
        [[1, 0.5], [0.5, 1]],  
        [[1, 0.5], [0.5, 1]],
        [[1, 0.5], [0.5, 1]]
    ], dtype=np.float64)
   
    b = np.array([
        [[5, 0.5], [0.5, 5]],  
        [[5, 0.5], [0.5, 5]],
        [[5, 0.5], [0.5, 5]],
        [[5, 0.5], [0.5, 5]]
    ], dtype=np.float64)
   
    c = np.array([
        [[1, 0.5], [0.5, 1]],  
        [[1, 0.5], [0.5, 1]],
        [[1, 0.5], [0.5, 1]]
    ], dtype=np.float64)

    d = np.array([
        [1, 2],
        [2, 3],
        [3, 4],
        [4, 5]
    ], dtype=np.float64)
   
    x = solve_block_tridiagonal(a, b, c, d)

    # Construct the equivalent full matrix A_full and right-hand side d_full
    A_full = np.block([
        [b[0], c[0], np.zeros((2, 2)), np.zeros((2, 2))],
        [a[0], b[1], c[1], np.zeros((2, 2))],
        [np.zeros((2, 2)), a[1], b[2], c[2]],
        [np.zeros((2, 2)), np.zeros((2, 2)), a[2], b[3]]
    ])
   
    d_full = d.flatten()  # Flatten d for compatibility with the full system

    # Solve using numpy's direct solve for comparison
    x_np = np.linalg.solve(A_full, d_full).reshape((N, 2))
    # Print the solutions for comparison
    print("Solution x from block tridiagonal solver (TMDA):\n", x, "\nResidual:", torch.sum(torch.abs(torch.tensor(A_full)@torch.tensor(x).flatten() - torch.tensor(d).flatten())))
    print("Solution x from direct full matrix solver:\n", x_np, "\nResidual np:", torch.sum(torch.abs(torch.tensor(A_full)@torch.tensor(x_np).flatten() - torch.tensor(d).flatten())))
# Run the test function
test_block_tridiagonal_solver()
#2 - 2024-11-13 21:35
唯独惑
哇 ~ 感谢,这就是我想要的!
再提一个建议:
能否对行数过长的代码块,加一个高度限制,并辅以滚轮滑动?
#2-1 - 2024-11-13 21:44
试下昵称中间能 不能加 空
好哦,有空看一下,这个功能有别的网站的示例吗?
#2-2 - 2024-11-13 22:03
唯独惑
茵陳 说: 好哦,有空看一下,这个功能有别的网站的示例吗?
嗯....比如stackoverflow就是如此的,示例网页
#2-3 - 2024-11-13 22:55
试下昵称中间能 不能加 空
唯独惑 说: 嗯....比如stackoverflow就是如此的,示例网页
好了
#3 - 2024-11-14 19:33
唯独惑
我发现了一个问题,就是主楼与回复中的代码块的字体大小一致,但行高不同。
示例可见
分析了下,问题出现在 `unset`。而主楼与回复中的代码块所继承的样式不同,一个是 `topic_content`,一个是 `message` 或 `cmt_sub_content`。而其所呈现的行高,一个是15*1.6px,一个是14*1.6px。它们的具体样式如下:
.pre.beforeState {
  font-size: 12px;
  line-height: unset !important;
}

.pre.afterState {
  font-size: 12px;
  line-height: 120%;
}

.topic_content {
  font-size: 15px;
  line-height: 160%;
}

.message,
.cmt_sub_content {
  font-size: 14px;
  line-height: 1.6;
}

能不能统一到同一个合适的行高呢?
#3-1 - 2024-11-14 22:56
试下昵称中间能 不能加 空
应该好了,现在设置是 line-height: 1.5
/ 返回番组开发小组
© 2008-2025 Bangumi (a.k.a.Chobits), some rights reserved | r545
注册时我很沉默

[8]ページ先頭

©2009-2025 Movatter.jp